diff --git a/MySQL_change.sql b/MySQL_change.sql index 5fd54c4..3fbc3c9 100644 --- a/MySQL_change.sql +++ b/MySQL_change.sql @@ -115,3 +115,11 @@ ALTER TABLE `product_specs` ADD COLUMN `original_price` DECIMAL(20,2) NOT NULL COMMENT '原价' AFTER `stock`, CHANGE COLUMN `price` `price` DECIMAL(20,2) NOT NULL COMMENT '售价' AFTER `original_price`; +# 9:25 2021/9/24 +ALTER TABLE `product_specs` + CHANGE COLUMN `id` `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT FIRST, + ADD COLUMN `date` DATE NULL COMMENT '日期' AFTER `name`, + CHANGE COLUMN `original_price` `original_price` DECIMAL(20,2) NOT NULL DEFAULT 0 COMMENT '原价' AFTER `stock`; +UPDATE `product_specs` SET `date` = NOW(); +ALTER TABLE `product_specs` + CHANGE COLUMN `date` `date` DATE NOT NULL COMMENT '日期' AFTER `name`; diff --git a/app/AdminAgent/Controllers/AgentProductController.php b/app/AdminAgent/Controllers/AgentProductController.php index 13a71c7..7a4dbcc 100644 --- a/app/AdminAgent/Controllers/AgentProductController.php +++ b/app/AdminAgent/Controllers/AgentProductController.php @@ -2,6 +2,7 @@ namespace App\AdminAgent\Controllers; +use App\AdminAgent\Forms\LoadSupplierSpec; use App\AdminAgent\Renderable\SelectAgentCloudProduct; use App\AdminAgent\Renderable\SelectGuide; use App\AdminAgent\Renderable\SelectProduct; @@ -19,6 +20,7 @@ use Dcat\Admin\Form; use Dcat\Admin\Grid; use Dcat\Admin\Show; use Dcat\Admin\Http\Controllers\AdminController; +use Dcat\Admin\Widgets\Modal; class AgentProductController extends AdminController { @@ -168,7 +170,7 @@ class AgentProductController extends AdminController */ protected function form() { - return Form::make(new AgentProduct(), function (Form $form) { + return Form::make(new AgentProduct(['spec.productSpec']), function (Form $form) { $agent_id = Admin::user()->id; //不允许查看非自己的数据 if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) { @@ -181,7 +183,7 @@ class AgentProductController extends AdminController if (Admin::user()->type == AgentType::CLUSTER) { $form->hidden('product_id')->value(0)->default(0); $form->hidden('type')->value(1)->default(1); - /** 组合销售 **/ + // 组合销售 $form->multipleSelectTable('product_ids', '供应商产品') ->help('请选择两个或两个以上的产品组合销售') ->title('选择产品') @@ -189,19 +191,50 @@ class AgentProductController extends AdminController ->from(SelectProduct::make(['ids' => $form->model()->product_ids])) ->model(Product::class); - /** 自定义内容 **/ + // 自定义内容 $form->text('title'); $form->multipleImage('pictures')->removable(false)->uniqueName(); $form->editor('know'); $form->editor('content'); } else { - $form->radio('type') + $form->hidden('type')->value(0)->default(0); + $js = file_get_contents(resource_path('js/select-supplier-product-change.js')); + $js = str_replace(['{{url}}', '{{class}}', '\\'], [route(admin_api_route_name('form')), LoadSupplierSpec::class, '\\\\'], $js); + $form->selectTable('product_id', '供应商产品') + ->required() + ->help('产品列表显示的是该产品的标题和图片') + ->title('选择产品') + ->dialogWidth('80%;min-width:825px;') + ->from(SelectProduct::make(['ids' => $form->model()->product_ids])) + ->model(Product::class) + ->script($js); + + $form->hasMany('spec', function (Form\NestedForm $form) { + $form->hidden('id'); + $form->hidden('product_spec_id'); + $form->text('name', '规格')->readOnly(); + $form->date('date', '日期')->readOnly(); + $form->text('supplier_stock', '供应商库存')->disable()->customFormat(fn() => $this->product_spec['stock'] ?? 0); + $form->text('supplier_price', '供应商价')->disable()->customFormat(fn() => $this->product_spec['price'] ?? 0); + $form->text('stock', '您的库存'); + $form->text('original_price', '您的原价'); + $form->text('price', '您的售价'); + Admin::style('.has-many-spec .add.btn{display:;} + .has-many-spec .field_date{width:100px!important;} + .has-many-spec .col-md-12{padding:0;} + .has-many-spec .form-group{margin-bottom:0;} + .has-many-spec .form-group .remove{margin-top:10px;} + .has-many-spec .input-group-prepend{display:none;} + .has-many-spec .input-group>.form-control:not(:first-child){border-radius:.25rem;}'); + })->useTable()->required(); + + /*$form->radio('type') ->options(['单品销售']) ->default(Admin::user()->type == AgentType::CLUSTER ? 1 : 0)->required() //->help('单品销售无需审核,组合销售需要审核才能上架') ->help('单品销售无需审核') ->when(0, function (Form $form) { - /** 单品销售 **/ + // 单品销售 $form->selectTable('product_id', '供应商产品') ->help('产品列表显示的是该产品的标题和图片') ->title('选择产品') @@ -209,7 +242,7 @@ class AgentProductController extends AdminController ->from(SelectProduct::make(['ids' => $form->model()->product_ids])) ->model(Product::class); })->when(1, function (Form $form) { - /** 组合销售 **/ + // 组合销售 $form->multipleSelectTable('product_ids', '供应商产品') ->help('可单选或多选组合销售') ->title('选择产品') @@ -217,29 +250,29 @@ class AgentProductController extends AdminController ->from(SelectProduct::make(['ids' => $form->model()->product_ids])) ->model(Product::class); - /** 自定义内容 **/ + // 自定义内容 $form->text('title'); $form->multipleImage('pictures')->removable(false)->uniqueName(); $form->editor('know'); $form->editor('content'); })->when(2, function (Form $form) { - /** 计调云产品 **/ + // 计调云产品 $form->selectTable('agent_cloud_pid', '计调云产品') ->help('产品列表显示的是该产品的标题和图片') ->title('选择产品') ->dialogWidth('80%;min-width:825px;') ->from(SelectAgentCloudProduct::make()) ->model(\App\Models\AgentProduct::class); - }); + });*/ } - $form->decimal('price',)->rules('required|numeric|min:0|not_in:0',[ + /*$form->decimal('price',)->rules('required|numeric|min:0|not_in:0',[ '*' => '金额为必填字段且必须大于0', - ]); + ])->required(); $form->decimal('original_price')->rules('required|numeric|min:0|not_in:0',[ '*' => '金额为必填字段且必须大于0', - ]); + ])->required();*/ $form->number('sale')->min(0)->default(0); - $form->number('stock')->min(0)->default(8888)->required(); +// $form->number('stock')->min(0)->default(8888)->required(); $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id)); $form->select('category_id') @@ -350,7 +383,7 @@ class AgentProductController extends AdminController //库存不足,上架失败 if ($form->model()->stock <= 0 && $form->status == 1) { - return $form->response()->error('库存不足,上架失败!')->refresh(); + return $form->response()->error('库存不足,上架失败,请先增加库存!')->refresh(); } $form->model()->status = $form->status == 1 ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT; @@ -363,8 +396,16 @@ class AgentProductController extends AdminController if (is_null($form->type)) { return $form->response()->error('请选择销售类型!'); } + + $form->hidden(['stock', 'price']); + $spec = array_filter($form->spec, fn($v) => !$v['_remove_']); + $form->stock = array_sum(array_column($spec, 'stock')); + $form->original_price = min(array_column($spec, 'original_price')); + $form->price = min(array_column($spec, 'price')); + //单品销售 if ($form->type == 0) { + $form->hidden(['product_ids', 'title', 'pictures', 'know', 'content']); $form->product_id = (int)$form->product_id; if (!$form->product_id) { return $form->response()->error('请选择产品'); @@ -384,7 +425,7 @@ class AgentProductController extends AdminController $form->pictures = $product->pictures; $form->know = $product->know; $form->content = $product->content; - } + /*} //组合销售 else if ($form->type == 1) { if (!$form->product_ids) { @@ -463,12 +504,13 @@ class AgentProductController extends AdminController $form->title = $cloud_product->title; $form->pictures = $cloud_product->pictures; $form->know = $cloud_product->know; - $form->content = $cloud_product->content; + $form->content = $cloud_product->content;*/ } else { return $form->response()->error('不存在此销售方式'); } $agent_id = Admin::user()->id; + //处理特殊字段 $form->hidden(['agent_id', 'status']); //表单没有的字段,必须加这句才能够重写 $form->agent_id = $agent_id; @@ -476,6 +518,7 @@ class AgentProductController extends AdminController if (array_key_exists('guide_id', $form->input())) { $form->guide_id = $form->guide_id ?? 0; } + //组合销售需要审核,编辑时是否需要审核在saved里面判断 if ($form->type == 1) { if ($form->isCreating()) { @@ -483,7 +526,7 @@ class AgentProductController extends AdminController } else if ($form->isEditing() && in_array($form->model()->status, [ProductStatus::UNAUDITED, ProductStatus::REFUSE])) { $form->deleteInput('status'); //待审核和拒绝的状态不允许修改 } - } else if ($form->type == 0 || $form->type == 2) { + } else if (!is_null($form->status) && ($form->type == 0 || $form->type == 2)) { $form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT; } diff --git a/app/AdminAgent/Controllers/OrderController.php b/app/AdminAgent/Controllers/OrderController.php index 9a186d6..7c502eb 100644 --- a/app/AdminAgent/Controllers/OrderController.php +++ b/app/AdminAgent/Controllers/OrderController.php @@ -41,8 +41,18 @@ class OrderController extends AdminController $grid->column('id')->sortable(); $grid->column('order_no')->limit(10); - $grid->column('name'); - $grid->column('mobile'); + $grid->column('custom', '客户信息') + ->display(function () { + return $this->name . ',' . $this->mobile; + }) + ->modal('客户信息', function ($modal) { + if (!$this->info) return ''; + $titles = [ + 'id_card' => '身份证号', + '' + ]; + return Table::make($this->info); + }); $grid->column('product', '产品信息') ->display('查看') ->modal('购买产品信息', function ($modal) { diff --git a/app/AdminAgent/Forms/LoadSupplierSpec.php b/app/AdminAgent/Forms/LoadSupplierSpec.php new file mode 100644 index 0000000..d8fc0ce --- /dev/null +++ b/app/AdminAgent/Forms/LoadSupplierSpec.php @@ -0,0 +1,27 @@ +get()->toArray(); + + return $this->response()->data($spec); + } + + public function form() + { + $this->hidden('id'); + } +} diff --git a/app/AdminSupplier/Controllers/ProductController.php b/app/AdminSupplier/Controllers/ProductController.php index 4f6f584..cc0670b 100644 --- a/app/AdminSupplier/Controllers/ProductController.php +++ b/app/AdminSupplier/Controllers/ProductController.php @@ -109,13 +109,20 @@ class ProductController extends AdminController // $form->currency('original_price')->symbol('¥')->required(); $form->number('service_persons')->required(); // $form->number('stock')->required(); - $form->table('spec', function ($form) { + $form->hasMany('spec', function (NestedForm $form) { $form->hidden('id'); - $form->text('name')->required(); + $form->text('name', '规格')->required()->readonly(); + $form->date('date', '日期')->required(); $form->text('stock')->required(); $form->text('original_price')->required(); $form->text('price')->required(); - })->required(); + Admin::style('.field_date{width:100px!important;} + .has-many-spec .col-md-12{padding:0;} + .has-many-spec .add.btn{display:none;} + .has-many-spec .input-group-prepend{display:none;} + .has-many-spec .input-group>.form-control:not(:first-child){border-radius:.25rem;}'); + Admin::script(file_get_contents(resource_path('js/batch-add-spec.js'))); + })->useTable()->required(); if ($form->isEditing() && in_array($form->model()->status, [ProductStatus::SOLD_OUT, ProductStatus::ON_SALE])) { $form->radio('status')->options([1 => '上架', -2 => '下架'])->default(1); } @@ -238,9 +245,9 @@ class ProductController extends AdminController //处理库存、原价、售价 $form->hidden(['stock', 'original_price', 'price']); - $form->stock = array_sum(array_column($form->spec, 'stock')); - $form->original_price = min(array_column($form->spec, 'original_price')); - $form->price = min(array_column($form->spec, 'price')); + $form->stock = array_sum(array_column($spec, 'stock')); + $form->original_price = min(array_column($spec, 'original_price')); + $form->price = min(array_column($spec, 'price')); //特殊字段处理 if ($form->isCreating()) { @@ -259,7 +266,7 @@ class ProductController extends AdminController DB::beginTransaction(); try { //如果修改标题、价格、产品图片、旅游须知、产品详情,状态将变为未审核 - if ($form->model()->wasChanged(['title', 'price', 'original_price', 'pictures', 'know', 'content', 'extends'])) { + if ($form->model()->wasChanged(['title', 'price', 'original_price', 'pictures', 'know', 'content', 'extends', 'spec'])) { $form->model()->update(['status' => ProductStatus::UNAUDITED]); //下架所有代理商产品,未审核的产品,不能同步信息到代理商产品 diff --git a/app/Http/Controllers/Api/AgentProductController.php b/app/Http/Controllers/Api/AgentProductController.php index 8260770..70b1c29 100644 --- a/app/Http/Controllers/Api/AgentProductController.php +++ b/app/Http/Controllers/Api/AgentProductController.php @@ -64,12 +64,7 @@ class AgentProductController extends Controller } else { $where = ['id' => $id, 'agent_id' => $this->agent_id, 'status' => ProductStatus::ON_SALE]; } - $agent_product = AgentProduct::with([ - 'coupon:tag,agent_product_id', - 'product' => function($query) { - $query->select('id', 'type', 'extends')->with('spec'); - } - ]) + $agent_product = AgentProduct::with(['coupon:tag,agent_product_id', 'product:id,type,extends', 'spec']) ->whereDoesntHave('agentProductItem', function ($query) { return $query->whereHas('product', function ($query) { return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE); diff --git a/app/Http/Controllers/Api/OrderController.php b/app/Http/Controllers/Api/OrderController.php index 680a06a..d40c6de 100644 --- a/app/Http/Controllers/Api/OrderController.php +++ b/app/Http/Controllers/Api/OrderController.php @@ -8,6 +8,7 @@ use App\Http\Controllers\Controller; use App\Models\AdminSetting; use App\Models\Agent; use App\Models\AgentProduct; +use App\Models\AgentProductSpec; use App\Models\AgentSetting; use App\Models\Coupon; use App\Models\OrderProductItem; @@ -138,8 +139,7 @@ class OrderController extends Controller 'id_card.regex' => '身份证号输入不正确', ]); - $ap = AgentProduct::query() - ->with(['coupon', 'product.spec', 'agentCloudProduct:id,price']) + $ap = AgentProduct::with(['coupon', 'product', 'agentCloudProduct:id,price']) ->where('stock', '>=', $formData['num']) ->where(['id' => $formData['id'], 'status' => ProductStatus::ON_SALE, 'agent_id' => $this->agent_id]) //判断agent_id,防止新入驻小程序的演示产品被下单 ->whereDoesntHave('agentProductItem', function ($query) { @@ -152,28 +152,33 @@ class OrderController extends Controller return $this->error('产品已下架或库存不足'); } - if (is_null($ap->product->spec) || !array_filter($ap->product->spec->toArray(), fn($v) => $v['id'] == $formData['spec_id'])) { - return $this->error('你选择的产品规格不存在'); - } - $order_info = []; //0:单品销售;1:组合销售 if ($ap->type == 0) { + $spec = AgentProductSpec::where('agent_product_id', $formData['id'])->find($formData['spec_id']); + if (!$spec) { + return $this->error('你选择的产品规格不存在'); + } + $order_price = $spec->price * $formData['num']; //订单总价 + //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项 => admin_trans('product.options.publish_type') switch ($ap->product->type) { case 0: + case 3: + case 4: + case 5: if (empty($formData['departure_time']) || !strtotime($formData['departure_time'])) { return $this->error('请选择出发时间'); } - if (empty($formData['return_time']) || !strtotime($formData['return_time'])) { + /*if (empty($formData['return_time']) || !strtotime($formData['return_time'])) { return $this->error('请选择回程时间'); - } + }*/ if (empty($formData['id_card'])) { return $this->error('请输入身份证号'); } $order_info['departure_time'] = $formData['departure_time']; //出发时间 - $order_info['return_time'] = $formData['return_time']; //回程时间 +// $order_info['return_time'] = $formData['return_time']; //回程时间 $order_info['id_card'] = $formData['id_card']; //身份证号 $order_info['sex'] = $formData['sex'] ?? 0; //性别 $order_info['age'] = $formData['age'] ?? 0; //年龄 @@ -211,8 +216,8 @@ class OrderController extends Controller $order_info['age'] = $formData['age'] ?? 0; //年龄 break; } - } else if ($ap->type == 1) { - + } else { + return $this->error('不存在此类型产品'); } //支付小程序的产品不允许购买 @@ -229,8 +234,6 @@ class OrderController extends Controller DB::beginTransaction(); try { - $price = $this->calc($ap->price, $formData['num'], $formData['pay_type'], $ap); - //供应商产品表减库存 $product_ids = explode(',', $ap->product_ids); $affect_row = Product::query() @@ -241,24 +244,33 @@ class OrderController extends Controller throw new \Exception('供应产品库存不足'); } - //产品规格减表减库存 + //供应商产品规格减表减库存 $affect_row = ProductSpec::query() ->where('stock', '>=', $formData['num']) //乐观锁 - ->where('id', $formData['spec_id']) + ->where('id', $spec['product_spec_id']) ->decrement('stock', $formData['num']); if (!$affect_row) { - throw new \Exception('你选择的产品规格库存不足'); + throw new \Exception('你选择的供应产品规格库存不足'); } //代理商产品表减库存 $affect_row = AgentProduct::query() ->where('stock', '>=', $formData['num']) //乐观锁 - ->where('id', $ap->id) + ->where('id', $formData['id']) ->decrement('stock', $formData['num']); if (!$affect_row) { throw new \Exception('商户产品库存不足'); } + //代理商产品规格表减库存 + $affect_row = AgentProductSpec::query() + ->where('stock', '>=', $formData['num']) //乐观锁 + ->where('id', $spec['id']) + ->decrement('stock', $formData['num']); + if (!$affect_row) { + throw new \Exception('你选择的商户产品规格库存不足'); + } + //未付款时定金/订金的超时时间依然使用默认的订单超时时间,付定金/订金之后才使用定金超时时间 $order_timeout = AgentSetting::val($this->agent_id, 'order_timeout') ?? 60; //默认60分钟 $timeout = date('Y-m-d H:i:s', time() + $order_timeout * 60); //60 * 分钟转为秒 @@ -266,20 +278,21 @@ class OrderController extends Controller //处理预付金额 if ($formData['pay_type'] == PayType::DEPOSIT_PAY) { //订金支付 - $prepayPrice = $ap->deposit; + $prepayPrice = $ap->deposit * $formData['num']; $prepayTimeout = $ap->deposit_timeout; } else if($formData['pay_type'] == PayType::EARNEST_PAY) { //定金支付 - $prepayPrice = $ap->earnest; + $prepayPrice = $ap->earnest * $formData['num']; $prepayTimeout = $ap->earnest_timeout; } + // 存入订单表 $order = Order::query()->create([ 'user_id' => $this->user_id, 'agent_id' => $this->agent_id, 'order_no' => $this->getOrderNo(), 'num' => $formData['num'], - 'price' => $price, + 'price' => $order_price, 'name' => $formData['name'], 'mobile' => $formData['mobile'], 'title' => $ap->title, @@ -295,7 +308,7 @@ class OrderController extends Controller 'timeout' => $timeout, 'agent_cloud_pid' => $ap->agent_cloud_pid, 'agent_cloud_price' => $ap->agentCloudProduct->price ?? 0, - 'prepay_price ' => $prepayPrice ?? 0, + 'prepay_price' => $prepayPrice ?? 0, 'prepay_timeout' => $prepayTimeout ?? 0, 'service_persons' => SystemSetting::val('single', 'price'), 'info' => $order_info, @@ -384,11 +397,12 @@ class OrderController extends Controller //获取应付金额及相关产品信息 public function getPrice(Request $request) { - $formData = $request->only(['id', 'num', 'pay_type']); + $formData = $request->only(['id', 'num', 'pay_type', 'spec_id']); $request->validate([ 'id' => 'required|integer', 'num' => 'required|integer', 'pay_type' => 'required|integer', + 'spec_id' => 'required|integer', ], [ '*.required' => '参数缺失', '*.integer' => '参数类型错误', @@ -398,9 +412,8 @@ class OrderController extends Controller return $this->error('未指定产品数量'); } - $ap = AgentProduct::query() + $ap = AgentProduct::with(['coupon:agent_product_id,type,detail,agent_id,tag,start_at,end_at']) ->has('product') - ->with('coupon:agent_product_id,type,detail,agent_id,tag,start_at,end_at') ->find($formData['id'], ['id', 'price', 'original_price', 'product_id', 'title', 'pictures', 'earnest', 'earnest_timeout', 'deposit', 'deposit_timeout']); if (!$ap) { @@ -411,11 +424,22 @@ class OrderController extends Controller $ap->pictures = array_map(fn($v) => $prefix . $v, $ap->pictures); //如果是线下支付,显示的价格跟在线全款支付价格一样 - if ($formData['pay_type'] == PayType::OFFLINE) { - $formData['pay_type'] = PayType::ONLINE; + + //订金支付 + if ($formData['pay_type'] == PayType::DEPOSIT_PAY) { + $ap->final_price = $ap->deposit * $formData['num']; + } + //定金支付 + else if($formData['pay_type'] == PayType::EARNEST_PAY) { + $ap->final_price = $ap->earnest * $formData['num']; + } else { + $spec = AgentProductSpec::where('product_id', $formData['id'])->find($formData['spec_id']); + if (!$spec) { + return $this->error('你选择的产品规格不存在'); + } + $ap->final_price = $spec->price * $formData['num']; } - $ap->final_price = $this->calc($ap->price, $formData['num'], $formData['pay_type'], $ap); $ap->num = $formData['num']; return $this->success($ap); @@ -544,47 +568,6 @@ class OrderController extends Controller return $this->success($order); } - /** - * 计算最终价格(扣除优惠券之后的价格) - * $price:原价;$coupon:优惠券;$num:产品数量;$pay_type:支付方式 - * @param float $price - * @param float $num - * @param int $pay_type - * @param AgentProduct $agent_product - * @return float - */ - private function calc(float $price, float $num, int $pay_type, AgentProduct $agent_product): float - { - /** 修改需要同步修改sharePay里面的 */ - //根据支付方式计算价格 - if (in_array($pay_type, [PayType::DEPOSIT_PAY, PayType::EARNEST_PAY, PayType::DOWN_PAYMENT])) { - if ($pay_type == PayType::DEPOSIT_PAY && $agent_product->deposit && $agent_product->deposit_timeout) { - return $agent_product->deposit; - } else if ($pay_type == PayType::EARNEST_PAY && $agent_product->earnest && $agent_product->earnest_timeout) { - return $agent_product->earnest; - } - } - - $total_price = $price * $num; - /*//没有任何优惠券时直接返回最终价 - if ($coupon && $coupon->isEmpty()) { - return $total_price; - } - - $coupon = $coupon->toArray(); - foreach ($coupon as $v) { - // 未判断优惠券有效期 - if ($v['type'] == 1 && !empty($v['detail']['full']) && !empty($v['detail']['reduction'])) { //满减 - if ($total_price >= $v['detail']['full']) { - $total_price -= $v['detail']['reduction']; - } - } else if ($v['type'] == 2 && !empty($v['detail']['discount'])) { //打折 - $total_price *= $v['detail']['discount']; - } - }*/ - return round($total_price, 2); - } - // 生成订单号 private function getOrderNo(): string { diff --git a/app/Models/AgentProduct.php b/app/Models/AgentProduct.php index 4ca2b11..12714b1 100644 --- a/app/Models/AgentProduct.php +++ b/app/Models/AgentProduct.php @@ -100,4 +100,9 @@ class AgentProduct extends BaseModel { return $this->hasOne(AgentProductItem::class); } + + public function spec() + { + return $this->hasMany(AgentProductSpec::class); + } } diff --git a/app/Models/AgentProductSpec.php b/app/Models/AgentProductSpec.php new file mode 100644 index 0000000..3f11256 --- /dev/null +++ b/app/Models/AgentProductSpec.php @@ -0,0 +1,29 @@ +timestamps = false; + } + + public function agentProduct() + { + return $this->belongsTo(AgentProduct::class); + } + + public function productSpec() + { + return $this->belongsTo(ProductSpec::class); + } +} diff --git a/app/Models/ProductSpec.php b/app/Models/ProductSpec.php index a65d223..f6bb574 100644 --- a/app/Models/ProductSpec.php +++ b/app/Models/ProductSpec.php @@ -9,7 +9,7 @@ class ProductSpec extends Model { use HasFactory; - protected $fillable = ['name', 'stock', 'original_price', 'price']; + protected $guarded = ['id']; public function __construct(array $attributes = []) { diff --git a/resources/js/batch-add-spec.js b/resources/js/batch-add-spec.js new file mode 100644 index 0000000..037b357 --- /dev/null +++ b/resources/js/batch-add-spec.js @@ -0,0 +1,78 @@ +Date.prototype.format = function(fmt) { + var o = { + "M+" : this.getMonth()+1, //月份 + "d+" : this.getDate(), //日 + "h+" : this.getHours(), //小时 + "m+" : this.getMinutes(), //分 + "s+" : this.getSeconds(), //秒 + "q+" : Math.floor((this.getMonth()+3)/3), //季度 + "S" : this.getMilliseconds() //毫秒 + }; + if(/(y+)/.test(fmt)) { + fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); + } + for(var k in o) { + if(new RegExp("("+ k +")").test(fmt)){ + fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); + } + } + return fmt; +} + +$(function() { + $(".has-many-spec .add.btn") + .after('
 删除所有
') + .after('
 批量新增
'); + + //批量新增 + $(".has-many-spec .batch-add").click(function() { + var name = prompt("请输入规格名称"); + if(!name) return; + + var num = parseInt(prompt("请输入新增数量,最多只能是100", 30)); + if(isNaN(num) || !num) return; + if(num > 100) num = 100; + + var date = new Date(); + date = prompt("请输入起始日期", date.format("yyyy-MM-dd")); + if(!date) return; + + var stock = prompt("请输入默认库存", 9999); + if(!stock) return; + + var original_price = prompt("请输入默认原价"); + if(!original_price) return; + + var price = prompt("请输入默认售价"); + if(!price) return; + + $("template.spec-tpl .field_name").prop("value", name); + var html = $("template.spec-tpl").html() + + var fields = {'name' : name, 'date' : date, 'stock' : stock, 'original_price' : original_price, 'price' : price}; + for(var key in fields) { + html = html.replace('name="spec[new___LA_KEY__][' + key + ']" value=""', 'name="spec[new___LA_KEY__][' + key + ']" value="' + fields[key] + '"'); + } + + var nestedIndex = $(".has-many-spec-forms tr").length - 1; + function replaceNestedFormIndex(value) { + return String(value).replace(/__LA_KEY__/g, nestedIndex); + } + + for(var i=0; i '产品图片', 'guide_id' => '地接ID', 'earnest' => '定金', + 'spec' => '产品规格', 'earnest_timeout' => '定金超时时间', 'deposit' => '订金', 'deposit_timeout' => '订金超时时间',