Browse Source

产品规格修改完成

master
李可松 4 years ago
parent
commit
420817d102
  1. 27
      app/AdminAgent/Controllers/AgentProductController.php
  2. 56
      resources/js/agent-spec-edit.js

27
app/AdminAgent/Controllers/AgentProductController.php

@ -196,8 +196,8 @@ class AgentProductController extends AdminController
$form->editor('know');
$form->editor('content');
} else {
if ($form->isCreating()) {
$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(
@ -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) {

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

@ -1,49 +1,48 @@
$(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>');
//同步最新规格
var template = $('template.spec-tpl').html();
var is_load = false;
$('.spec-sync').click(function () {
if (is_load) {
Dcat.confirm('本次载入将清除上次载入的所有数据,是否继续?', null, post());
} else {
post();
is_load = true;
}
function post() {
$.ajax({
url: '`{{url}}`',
method: 'POST',
data: {product_id: $(this).val(), _form_: '`{{class}}`'},
data: {
product_id: `{{product_id}}`,
agent_product_id: `{{agent_product_id}}`,
_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 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体积
//只remove掉上次载入的,原来数据库保存的记录(.field_id不为空)不处理
forms.children().each(function () {
if ($(this).find('.field_id').val()) {
$(this).find('.remove.btn').click();
} else {
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];
}
row.find('input.field_' + key2).val(insert_value);
for (var i=0; i<fields.length; i++) {
var field = fields[i];
row.find('input.field_' + field).val(data[key][field]);
}
forms.append(row);
}
@ -52,6 +51,7 @@ $(function () {
Dcat.error('服务器出现未知错误,获取供应商产品规格失败');
}
});
}
});
//批量库存
@ -63,7 +63,7 @@ $(function () {
$('.has-many-spec input.field_stock').each(function () {
var value = $(this).parents('tr').find('.field_supplier_stock').val();
var result = num > value ? value : num;
var result = parseFloat(num) > parseFloat(value) ? value : num;
$(this).val(result);
});
});
@ -95,6 +95,4 @@ $(function () {
$(this).val(result.toFixed(2));
});
});
//
});
Loading…
Cancel
Save