Browse Source

代理商规格编辑

master
李可松 4 years ago
parent
commit
a3e77775d1
  1. 26
      app/AdminAgent/Controllers/AgentProductController.php
  2. 35
      app/AdminAgent/Forms/LoadSupplierSpec.php
  3. 18
      resources/js/agent-spec-edit.js
  4. 41
      resources/js/select-supplier-product-change.js

26
app/AdminAgent/Controllers/AgentProductController.php

@ -190,13 +190,6 @@ class AgentProductController extends AdminController
} else {
$form->hidden('type')->value(0);
if ($form->isCreating()) {
$js = file_get_contents(resource_path('js/select-supplier-product-change.js'));
$class = str_replace('\\', '\\\\', LoadSupplierSpec::class);
$js = str_replace(
['`{{url}}`', '`{{class}}`'],
[route(admin_api_route_name('form')), $class],
$js
);
$form->selectTable('product_id', '供应商产品')
->required()
->help('产品列表显示的是该产品的标题和图片')
@ -204,7 +197,13 @@ class AgentProductController extends AdminController
->dialogWidth('80%;min-width:825px;')
->from(SelectProduct::make(['ids' => $form->model()->product_ids]))
->model(Product::class)
->script($js);
->script("
$(function () {
$('input[name=\"product_id\"]').change(function () {
$('.spec-sync').click();
});
});
");
} else {
$form->hidden('product_id');
$form->display('product.title');
@ -216,19 +215,24 @@ class AgentProductController extends AdminController
$form->text('supplier_name', '规格')->disable()->customFormat(fn() => $this->product_spec['name'] ?? '供应商已删除规格');
$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('supplier_cost_price', '供应商成本价')->disable()->customFormat(fn() => $this->product_spec['cost_price'] ?? 0);
$form->text('supplier_price', '建议销售价')->disable()->customFormat(fn() => $this->product_spec['price'] ?? 0);
$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();
$form->text('price', '您的销售价')->required()
//如果代理商价格小于供应商建议售价,则取供应商售价
->customFormat(fn() => isset($this->product_spec['price'], $this->price) && $this->price < $this->product_spec['price'] ? $this->product_spec['price'] : $this->price);
Admin::style('.has-many-spec .add.btn{display:none;}
.has-many-spec .field_date{width:100px!important;}
.has-many-spec .field_supplier_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;}');
$js = file_get_contents(resource_path('js/agent-spec-edit.js'));
$class = str_replace('\\', '\\\\', LoadSupplierSpec::class);
$js = str_replace(

35
app/AdminAgent/Forms/LoadSupplierSpec.php

@ -16,31 +16,26 @@ class LoadSupplierSpec extends Form implements LazyRenderable
public function handle(array $input)
{
$id = $input['product_id'];
$spec = ProductSpec::where('product_id', $id);
if (!empty($input['agent_product_id'])) {
$agent_spec = AgentProductSpec::where('agent_product_id', $input['agent_product_id'])
->pluck('product_spec_id')->toArray();
$spec = ProductSpec::where('product_id', $id)
->whereNotIn('id', $agent_spec ?? [0])
->orderBy('supplier_date')
->get([
'id AS product_spec_id',
'name AS supplier_name',
'date AS supplier_date',
'price AS supplier_price',
'stock AS supplier_stock',
])->toArray();
} else {
$spec = ProductSpec::where('product_id', $id)
->orderBy('supplier_date')
->get([
'id AS product_spec_id',
'name AS supplier_name',
'date AS supplier_date',
'price AS supplier_price',
'stock AS supplier_stock',
])->toArray();
if ($agent_spec) {
$spec = $spec->whereNotIn('id', $agent_spec);
}
}
$spec = $spec->orderBy('name')->orderBy('supplier_date')
->get([
'id AS product_spec_id',
'name AS supplier_name',
'date AS supplier_date',
'original_price AS supplier_original_price',
'price AS supplier_price',
'cost_price AS supplier_cost_price',
'stock AS supplier_stock',
])->toArray();
return $this->response()->data($spec);
}

18
resources/js/agent-spec-edit.js

@ -1,6 +1,7 @@
$(function () {
$('.has-many-spec .add.btn')
.after('<div class="btn btn-spec spec-sync btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i>&nbsp;同步供应商最新规格</div><div class="btn btn-spec batch-stock btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i>&nbsp;批量设置库存</div><div class="btn btn-spec batch-original-price btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i>&nbsp;批量设置市场价</div><div class="btn btn-spec batch-price btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i>&nbsp;批量设置销售价</div>');
.after('<div class="btn btn-spec spec-sync btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i>&nbsp;同步供应商最新规格</div><div class="btn btn-spec batch-stock btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i>&nbsp;批量设置库存</div><div class="btn btn-spec batch-original-price btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i>&nbsp;批量设置市场价</div><div class="btn btn-spec batch-price btn-primary btn-outline btn-sm" style="margin-left:10px;"><i class="feather icon-plus"></i>&nbsp;批量设置销售价</div>')
.remove();
//同步最新规格
var template = $('template.spec-tpl').html();
@ -26,7 +27,7 @@ $(function () {
_form_: '`{{class}}`',
},
success: function (res) {
var fields = ['supplier_name', 'supplier_date', 'supplier_price', 'supplier_stock',
var fields = ['supplier_name', 'supplier_date', 'supplier_price', 'supplier_stock', 'supplier_cost_price',
'price', 'stock', 'original_price', 'product_spec_id'];
var data = res.data;
var forms = $('.has-many-spec-forms');
@ -46,7 +47,16 @@ $(function () {
var row = $(template.replace(/new___LA_KEY__/g, key));
for (var i=0; i<fields.length; i++) {
var field = fields[i];
row.find('input.field_' + field).val(data[key][field]);
if (['original_price', 'price', 'stock'].indexOf(field) !== -1) {
var value = data[key]['supplier_' + field], rule = '';
//不处理市场价
if (field === 'original_price') {
rule = 'if(!this.value || isNaN(this.value) || parseFloat(this.value)<' + value + ') this.value=' + value;
}
row.find('input.field_' + field).val(value).attr({onblur: rule});
} else {
row.find('input.field_' + field).val(data[key][field]);
}
}
forms.append(row);
}
@ -62,7 +72,7 @@ $(function () {
//批量库存
$('.batch-stock').click(function () {
var num = prompt('请输入你的库存(注:库存不能于供应商库存)');
var num = prompt('请输入你的库存(注:库存不能于供应商库存)');
if (!/^\d+$/.test(num)) {
return;
}

41
resources/js/select-supplier-product-change.js

@ -1,41 +0,0 @@
$(function () {
var template = $('template.spec-tpl').html();
//新增产品时,选择供应商产品载入规格
$('input[name="product_id"]').change(function () {
$.ajax({
url: '`{{url}}`',
method: 'POST',
data: {
product_id: $(this).val(),
_form_: '`{{class}}`',
},
success: function (res) {
var fields = ['supplier_name', 'supplier_date', 'supplier_price', 'supplier_stock', 'product_spec_id'];
var data = res.data;
var forms = $('.has-many-spec-forms');
//如果是已经保存有记录(.field_id不为空),调用click,提交的时候会同时删除数据库里的记录,否则直接remove掉,减小HTML体积
forms.children().each(function () {
if ($(this).find('.field_id').val()) {
$(this).find('.remove.btn').click();
} else {
$(this).remove();
}
});
for (var key in data) {
var row = $(template.replace(/new___LA_KEY__/g, key));
for (var i=0; i<fields.length; i++) {
var field = fields[i];
row.find('input.field_' + field).val(data[key][field]);
}
forms.append(row);
}
},
error: function () {
Dcat.error('服务器出现未知错误,获取供应商产品规格失败');
}
});
});
});
Loading…
Cancel
Save