|
@@ -17,17 +17,18 @@ class ReorderChild extends Base
|
|
|
public function add()
|
|
|
{
|
|
|
|
|
|
- $param = $this->request->only(['returnCode', 'list', 'type'], 'post', 'trim');
|
|
|
+ $param = $this->request->only(['returnCode', 'outCode', 'list', 'type'], 'post', 'trim');
|
|
|
|
|
|
$val = Validate::rule([
|
|
|
'returnCode|退货单编号' => 'require',
|
|
|
+ 'outCode|发货单编号' => 'require',
|
|
|
'list|退货工单集合' => 'require|array|max:100',
|
|
|
'type|工单退货类型' => 'require|number|in:1,2',
|
|
|
]);
|
|
|
if ($val->check($param) == false) return json_show(1004, $val->getError());
|
|
|
|
|
|
$saleReturn = Db::name('sale_return')
|
|
|
- ->field('id,orderCode,status,companyNo,companyName,customer_code,customer_name')
|
|
|
+ ->field('id,orderCode,status,companyNo,companyName,customer_code,customer_name,num')
|
|
|
->where(['is_del' => 0, 'returnCode' => $param['returnCode']])
|
|
|
->findOrEmpty();
|
|
|
if (empty($saleReturn)) return json_show(1004, '该退货单不存在');
|
|
@@ -44,22 +45,38 @@ class ReorderChild extends Base
|
|
|
$orderOutChild = Db::name('order_out_child')
|
|
|
->where(['is_del' => 0, 'outChildCode' => array_unique(array_column($param['list'], 'outChildCode'))])
|
|
|
->column('outCode,wsm_code,num,status', 'outChildCode');
|
|
|
- }
|
|
|
|
|
|
+ $saleReturnAddr = Db::name('sale_returnaddr')
|
|
|
+ ->field('id,return_num,is_sale_return_child')
|
|
|
+ ->where(['is_del' => 0, 'returnCode' => $param['returnCode'], 'outCode' => $param['outCode']])
|
|
|
+ ->findOrEmpty();
|
|
|
+ if (empty($saleReturnAddr)) return json_show(1004, '该退货地址不存在');
|
|
|
+ if ($saleReturnAddr['is_sale_return_child'] == 1) return json_show(1004, '该退货地址不能重复设置退货工单');
|
|
|
+ if ($saleReturnAddr['return_num'] != array_sum(array_column($param['list'], 'return_num'))) return json_show(1004, '数量错误');
|
|
|
+ }
|
|
|
|
|
|
$val_child = Validate::rule([
|
|
|
'outChildCode|发货工单号' => 'requireIf:type,2',
|
|
|
'return_num|退货数量' => 'require|number|gt:0|max:999999999999',
|
|
|
- 'return_wsm_code|退货仓库编码' => 'require',
|
|
|
-
|
|
|
+ 'return_wsm_code|退货仓库编码' => 'require'
|
|
|
]);
|
|
|
|
|
|
+ if ($saleReturn['sale_return'] < array_sum(array_column($param['list'], 'return_num'))) return json_show(1004, '数量错误');
|
|
|
+
|
|
|
Db::startTrans();
|
|
|
|
|
|
try {
|
|
|
|
|
|
$date = date('Y-m-d H:i:s');
|
|
|
|
|
|
+ //维护退货地址
|
|
|
+ if ($param['type'] == 2) {
|
|
|
+ Db::name('sale_returnaddr')
|
|
|
+ ->where(['id' => $saleReturnAddr['id'], 'is_del' => 0, 'is_sale_return_child' => 0])
|
|
|
+ ->update(['is_sale_return_child' => 1, 'updatetime' => $date]);
|
|
|
+ }
|
|
|
+
|
|
|
+ //生成售前退货工单
|
|
|
$insert = [];
|
|
|
$i = 0;
|
|
|
foreach ($param['list'] as $value) {
|
|
@@ -71,7 +88,7 @@ class ReorderChild extends Base
|
|
|
$insert[] = [
|
|
|
'orderCode' => $saleReturn['orderCode'],
|
|
|
'returnCode' => $param['returnCode'],
|
|
|
- 'outCode' => $param['type'] == 2 ? $orderOutChild[$value['outChildCode']]['outCode'] : '',
|
|
|
+ 'outCode' => $param['type'] == 2 ? $param['outCode'] : '',
|
|
|
'outChildCode' => $value['outChildCode'] ?? '',
|
|
|
'order_out_child_status' => $param['type'] == 2 ? $orderOutChild[$value['outChildCode']]['status'] : '',
|
|
|
'saleReturnChildCode' => substr(makeNo('KCC'), 0, -2) . str_pad($i++, 2, '0', STR_PAD_LEFT),
|
|
@@ -102,9 +119,13 @@ class ReorderChild extends Base
|
|
|
|
|
|
if ($insert) Db::name('sale_return_child')->insertAll($insert);
|
|
|
|
|
|
- Db::name('sale_return')
|
|
|
- ->where(['is_del' => 0, 'id' => $saleReturn['id'], 'status' => 11])
|
|
|
- ->update(['status' => 12, 'updatetime' => $date]);
|
|
|
+ //维护售前退货单
|
|
|
+ $total_return_num = Db::name('sale_return_child')->where(['is_del' => 0, 'returnCode' => $param['returnCode'], 'status' => 1])->sum('return_num');
|
|
|
+ if ($total_return_num >= $saleReturn['num']) {
|
|
|
+ Db::name('sale_return')
|
|
|
+ ->where(['is_del' => 0, 'id' => $saleReturn['id'], 'status' => 11])
|
|
|
+ ->update(['status' => 12, 'updatetime' => $date]);
|
|
|
+ }
|
|
|
|
|
|
Db::commit();
|
|
|
return json_show(0, '设置退货工单成功');
|