From 420817d102a0683a7535980bfa0123ce8adf5406 Mon Sep 17 00:00:00 2001 From: liapples Date: Wed, 29 Sep 2021 14:42:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=A7=E5=93=81=E8=A7=84=E6=A0=BC=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/AgentProductController.php | 27 +++--- resources/js/agent-spec-edit.js | 90 +++++++++---------- 2 files changed, 58 insertions(+), 59 deletions(-) diff --git a/app/AdminAgent/Controllers/AgentProductController.php b/app/AdminAgent/Controllers/AgentProductController.php index daa680d..3067b2a 100644 --- a/app/AdminAgent/Controllers/AgentProductController.php +++ b/app/AdminAgent/Controllers/AgentProductController.php @@ -196,8 +196,8 @@ class AgentProductController extends AdminController $form->editor('know'); $form->editor('content'); } else { + $form->hidden('type')->value(0); if ($form->isCreating()) { - $form->hidden('type')->value(0); $js = file_get_contents(resource_path('js/select-supplier-product-change.js')); $class = str_replace('\\', '\\\\', LoadSupplierSpec::class); $js = str_replace( @@ -225,9 +225,11 @@ class AgentProductController extends AdminController $form->date('supplier_date', '日期')->disable()->customFormat(fn() => $this->product_spec['date'] ?? '供应商已删除规格'); $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', '您的库存')->customFormat(fn() => isset($this->product_spec['stock'], $this->stock) && $this->stock > $this->product_spec['stock'] ? $this->product_spec['stock'] : $this->stock); - $form->text('original_price', '您的原价'); - $form->text('price', '您的售价'); + $form->text('stock', '您的库存')->required() + //如果库存大于供应商库存,则取供应商库存 + ->customFormat(fn() => isset($this->product_spec['stock'], $this->stock) && $this->stock > $this->product_spec['stock'] ? $this->product_spec['stock'] : $this->stock); + $form->text('original_price', '您的原价')->required(); + $form->text('price', '您的售价')->required(); Admin::style('.has-many-spec .add.btn{display:none;} .has-many-spec .field_date{width:100px!important;} .has-many-spec .col-md-12{padding:0;} @@ -238,8 +240,8 @@ class AgentProductController extends AdminController $js = file_get_contents(resource_path('js/agent-spec-edit.js')); $class = str_replace('\\', '\\\\', LoadSupplierSpec::class); $js = str_replace( - ['`{{url}}`', '`{{class}}`'], - [route(admin_api_route_name('form')), $class], + ['`{{url}}`', '`{{class}}`', '`{{product_id}}`', '`{{agent_product_id}}`'], + [route(admin_api_route_name('form')), $class, $form->model()->product_id, $form->model()->id], $js ); Admin::script($js); @@ -373,12 +375,11 @@ class AgentProductController extends AdminController if ($form->stock !== null || $form->status !== null) { $ids = explode(',', $form->model()->product_ids); - $count = Product::where([ - ['status', '=', ProductStatus::ON_SALE], - ['stock', '>=', $form->stock ?? $form->model()->stock], - ])->whereIn('id', $ids)->count(); - if (count($ids) != $count) { - return $form->response()->error('供应商产品已下架或库存小于你设置的库存' . $form->stock)->refresh(); + $exists = Product::where([ + ['status', '<>', ProductStatus::ON_SALE], + ])->whereIn('id', $ids)->exists(); + if ($exists) { + return $form->response()->error('供应商产品已下架')->refresh(); } //修改库存 @@ -439,7 +440,7 @@ class AgentProductController extends AdminController //将供应商产品写入title,pictures,know,content $product = Product::find($form->product_id); if ($product->status != ProductStatus::ON_SALE) { - return $form->response()->error('产品ID '. $form->product_id .' 已下架'); + return $form->response()->error('供应商产品 '. $product->title .' 已下架'); /*} else if ($product->stock < $form->stock) { return $form->response()->error("供应商当前库存为{$product->stock},你设置的库存不能超过该数值");*/ } else if ($form->price < $product->price) { diff --git a/resources/js/agent-spec-edit.js b/resources/js/agent-spec-edit.js index 5c1cf02..b193c56 100644 --- a/resources/js/agent-spec-edit.js +++ b/resources/js/agent-spec-edit.js @@ -1,57 +1,57 @@ $(function () { $('.has-many-spec .add.btn') - .after('
 同步最新规格
 批量设置库存
 批量设置原价
 批量设置售价
'); + .after('
 同步供应商最新规格
 批量设置库存
 批量设置原价
 批量设置售价
'); //同步最新规格 var template = $('template.spec-tpl').html(); + var is_load = false; $('.spec-sync').click(function () { - $.ajax({ - url: '`{{url}}`', - method: 'POST', - data: {product_id: $(this).val(), _form_: '`{{class}}`'}, - success: function (res) { - var fields = { - supplier_name: 'name', - supplier_date: 'date', - supplier_price: 'price', - supplier_stock: 'stock', - price: 'price', - stock: 'stock', - original_price: 'original_price', - product_spec_id: 'id' - }; - var data = res.data; - var forms = $('.has-many-spec-forms'); + if (is_load) { + Dcat.confirm('本次载入将清除上次载入的所有数据,是否继续?', null, post()); + } else { + post(); + is_load = true; + } + function post() { + $.ajax({ + url: '`{{url}}`', + method: 'POST', + data: { + product_id: `{{product_id}}`, + agent_product_id: `{{agent_product_id}}`, + _form_: '`{{class}}`', + }, + success: function (res) { + var fields = ['supplier_name', 'supplier_date', 'supplier_price', 'supplier_stock', + 'price', 'stock', 'original_price', 'product_spec_id']; + var data = res.data; + var forms = $('.has-many-spec-forms'); - //如果是原来数据库已经保存有id,调用click,提交的时候会同时删除数据库,否则直接remove掉,减小HTML体积 - forms.children().each(function () { - if ($(this).find('.field_id').val()) { - $(this).find('.remove.btn').click(); - } else { - $(this).remove(); - } - }); + //只remove掉上次载入的,原来数据库保存的记录(.field_id不为空)不处理 + forms.children().each(function () { + if (!$(this).find('.field_id').val()) { + $(this).remove(); + } + //清理供应商已删除的规格 + else if ($(this).find('.field_supplier_stock').val() == 0 && $(this).find('.field_supplier_price').val() == 0) { + $(this).find('.remove.btn').click(); + } + }); - for (var key in data) { - var row = $(template.replace(/new___LA_KEY__/g, key)); - for(var key2 in fields) { - var value = fields[key2], insert_value; - if (key2 === 'original_price') { - insert_value = (data[key][value] * 2).toFixed(2); - } else if (key2 === 'price') { - insert_value = (data[key][value] * 1.5).toFixed(2); - } else { - insert_value = data[key][value]; + for (var key in data) { + var row = $(template.replace(/new___LA_KEY__/g, key)); + for (var i=0; i value ? value : num; + var result = parseFloat(num) > parseFloat(value) ? value : num; $(this).val(result); }); }); @@ -95,6 +95,4 @@ $(function () { $(this).val(result.toFixed(2)); }); }); - - // });