Browse Source

批量设置价格、库存

master
李可松 4 years ago
parent
commit
3f96e2b48b
  1. 7
      app/AdminAgent/Controllers/AgentProductController.php
  2. 5
      app/AdminAgent/Forms/LoadSupplierSpec.php
  3. 100
      resources/js/agent-spec-edit.js
  4. 6
      resources/js/select-supplier-product-change.js

7
app/AdminAgent/Controllers/AgentProductController.php

@ -199,7 +199,7 @@ class AgentProductController extends AdminController
$form->hidden('type')->value(0)->default(0); $form->hidden('type')->value(0)->default(0);
$js = file_get_contents(resource_path('js/select-supplier-product-change.js')); $js = file_get_contents(resource_path('js/select-supplier-product-change.js'));
$js = str_replace( $js = str_replace(
['{{url}}', '{{class}}', '{{profit}}', '\\'],
['`{{url}}`', '`{{class}}`', '`{{profit}}`', '\\'],
[route(admin_api_route_name('form')), LoadSupplierSpec::class, AgentSetting::val(Admin::user()->id, 'profit'), '\\\\'], [route(admin_api_route_name('form')), LoadSupplierSpec::class, AgentSetting::val(Admin::user()->id, 'profit'), '\\\\'],
$js $js
); );
@ -233,8 +233,13 @@ class AgentProductController extends AdminController
.has-many-spec .form-group .remove{margin-top:10px;} .has-many-spec .form-group .remove{margin-top:10px;}
.has-many-spec .input-group-prepend{display:none;} .has-many-spec .input-group-prepend{display:none;}
.has-many-spec .input-group>.form-control:not(:first-child){border-radius:.25rem;}'); .has-many-spec .input-group>.form-control:not(:first-child){border-radius:.25rem;}');
Admin::script(file_get_contents(resource_path('js/agent-spec-edit.js')));
})->useTable()->required(); })->useTable()->required();
if ($form->isCreating()) {
Admin::style('.has-many-spec .spec-sync{display:none;}');
}
/*$form->radio('type') /*$form->radio('type')
->options(['单品销售']) ->options(['单品销售'])
->default(Admin::user()->type == AgentType::CLUSTER ? 1 : 0)->required() ->default(Admin::user()->type == AgentType::CLUSTER ? 1 : 0)->required()

5
app/AdminAgent/Forms/LoadSupplierSpec.php

@ -14,7 +14,7 @@ class LoadSupplierSpec extends Form implements LazyRenderable
// 处理请求 // 处理请求
public function handle(array $input) public function handle(array $input)
{ {
$id = $input['id'];
$id = $input['product_id'];
$spec = ProductSpec::where('product_id', $id)->get()->toArray(); $spec = ProductSpec::where('product_id', $id)->get()->toArray();
return $this->response()->data($spec); return $this->response()->data($spec);
@ -22,6 +22,7 @@ class LoadSupplierSpec extends Form implements LazyRenderable
public function form() public function form()
{ {
$this->hidden('id');
$this->hidden('product_id');
$this->hidden('agent_product_id');
} }
} }

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

@ -0,0 +1,100 @@
$(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>');
//同步最新规格
var template = $('template.spec-tpl').html();
$('.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');
//如果是原来数据库已经保存有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 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];
}
row.find('input.field_' + key2).val(insert_value);
}
forms.append(row);
}
},
error: function () {
Dcat.error('服务器出现未知错误,获取供应商产品规格失败');
}
});
});
//批量库存
$('.batch-stock').click(function () {
var num = prompt('请输入你的库存(注:库存不能多于供应商库存)');
if (!/^\d+$/.test(num)) {
return;
}
$('.has-many-spec input.field_stock').each(function () {
var value = $(this).parents('tr').find('.field_supplier_stock').val();
var result = num > value ? value : num;
$(this).val(result);
});
});
//批量设置原价
$('.batch-original-price').click(function () {
var num = prompt('请输入加价金额(在供应商售价基础上加)');
if (!/^\d+$/.test(num)) {
return;
}
$('.has-many-spec input.field_original_price').each(function () {
var value = $(this).parents('tr').find('.field_supplier_price').val();
var result = parseFloat(value) + parseFloat(num);
$(this).val(result.toFixed(2));
});
});
//批量售价
$('.batch-price').click(function () {
var num = prompt('请输入加价金额(在供应商售价基础上加)');
if (!/^\d+$/.test(num)) {
return;
}
$('.has-many-spec input.field_price').each(function () {
var value = $(this).parents('tr').find('.field_supplier_price').val();
var result = parseFloat(value) + parseFloat(num);
$(this).val(result.toFixed(2));
});
});
//
});

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

@ -2,9 +2,9 @@ $(function () {
var template = $('template.spec-tpl').html(); var template = $('template.spec-tpl').html();
$('input[name="product_id"]').change(function () { $('input[name="product_id"]').change(function () {
$.ajax({ $.ajax({
url: '{{url}}',
url: '`{{url}`}',
method: 'POST', method: 'POST',
data: {id: $(this).val(), _form_: '{{class}}'},
data: {product_id: $(this).val(), _form_: '`{{class}}`'},
success: function (res) { success: function (res) {
var fields = { var fields = {
supplier_name: 'name', supplier_name: 'name',
@ -35,7 +35,7 @@ $(function () {
if (key2 === 'original_price') { if (key2 === 'original_price') {
insert_value = (data[key][value] * 2).toFixed(2); insert_value = (data[key][value] * 2).toFixed(2);
} else if (key2 === 'price') { } else if (key2 === 'price') {
insert_value = (data[key][value] * (1 + {{profit}})).toFixed(2);
insert_value = (data[key][value] * (1 + `{{profit}}`)).toFixed(2);
} else { } else {
insert_value = data[key][value]; insert_value = data[key][value];
} }

Loading…
Cancel
Save