|
@@ -0,0 +1,208 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\admin\controller;
|
|
|
+
|
|
|
+//销售单退货工单
|
|
|
+use think\Exception;
|
|
|
+use think\facade\Db;
|
|
|
+use think\facade\Validate;
|
|
|
+
|
|
|
+class ReorderChild extends Base
|
|
|
+{
|
|
|
+
|
|
|
+ //退货工单创建
|
|
|
+ public function add()
|
|
|
+ {
|
|
|
+
|
|
|
+ $param = $this->request->only(['returnCode', 'list', 'type'], 'post', 'trim');
|
|
|
+
|
|
|
+ $val = Validate::rule([
|
|
|
+ 'returnCode|退货单编号' => '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')
|
|
|
+ ->where(['is_del' => 0, 'returnCode' => $param['returnCode']])
|
|
|
+ ->findOrEmpty();
|
|
|
+ if (empty($saleReturn)) return json_show(1004, '该退货单不存在');
|
|
|
+ if ($saleReturn['status'] != 11) return json_show(1004, '退货单状态有误');
|
|
|
+
|
|
|
+ $sale = Db::name('sale')
|
|
|
+ ->field('id,sale_price,good_num,total_price')
|
|
|
+ ->where(['is_del' => 0, 'orderCode' => $saleReturn['orderCode']])
|
|
|
+ ->findOrEmpty();
|
|
|
+ if (empty($sale)) return json_show(1004, '销售单不存在');
|
|
|
+
|
|
|
+ if ($param['type'] == 2) {
|
|
|
+ //所有发货工单
|
|
|
+ $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');
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ $val_child = Validate::rule([
|
|
|
+ 'outChildCode|发货工单号' => 'requireIf:type,2',
|
|
|
+ 'return_num|退货数量' => 'require|number|gt:0|max:999999999999',
|
|
|
+ 'return_wsm_code|退货仓库编码' => 'require',
|
|
|
+
|
|
|
+ ]);
|
|
|
+
|
|
|
+ Db::startTrans();
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ $date = date('Y-m-d H:i:s');
|
|
|
+
|
|
|
+ $insert = [];
|
|
|
+ $i = 0;
|
|
|
+ foreach ($param['list'] as $value) {
|
|
|
+
|
|
|
+ $value['type'] = $param['type'];
|
|
|
+
|
|
|
+ if ($val_child->check($value) == false) throw new Exception($val_child->getError());
|
|
|
+
|
|
|
+ $insert[] = [
|
|
|
+ 'orderCode' => $saleReturn['orderCode'],
|
|
|
+ 'returnCode' => $param['returnCode'],
|
|
|
+ 'outCode' => $param['type'] == 2 ? $orderOutChild[$value['outChildCode']]['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),
|
|
|
+ 'type' => $param['type'],
|
|
|
+ 'companyNo' => $saleReturn['companyNo'],
|
|
|
+ 'companyName' => $saleReturn['companyName'],
|
|
|
+ 'customer_code' => $saleReturn['customer_code'],
|
|
|
+ 'customerName' => $saleReturn['customer_name'],
|
|
|
+ 'num' => $sale['good_num'],
|
|
|
+ 'sale_price' => $sale['sale_price'],
|
|
|
+ 'total_price' => $sale['total_price'],
|
|
|
+ 'addtime' => $date,
|
|
|
+ 'updatetime' => $date,
|
|
|
+ 'record' => '',
|
|
|
+ 'send_wsm_code' => $param['type'] == 2 ? $orderOutChild[$value['outChildCode']]['wsm_code'] : '',
|
|
|
+ 'send_num' => $param['type'] == 2 ? $orderOutChild[$value['outChildCode']]['num'] : '',
|
|
|
+ 'return_num' => $value['return_num'],
|
|
|
+ 'return_wsm_code' => $value['return_wsm_code'],
|
|
|
+ 'good_receive_type' => 0,
|
|
|
+ 'loss_num' => 0,
|
|
|
+ 'remark' => '',
|
|
|
+ 'status' => 1,
|
|
|
+ 'is_del' => 0,
|
|
|
+ 'apply_id' => $this->uid,
|
|
|
+ 'apply_name' => $this->uname,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ 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]);
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+ return json_show(0, '设置退货工单成功');
|
|
|
+ } catch (Exception $exception) {
|
|
|
+ Db::rollback();
|
|
|
+ return json_show(1004, $exception->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //退货工单列表
|
|
|
+ public function getList()
|
|
|
+ {
|
|
|
+
|
|
|
+ $param = $this->request->only(['page' => 1, 'size' => 10, 'returnCode' => '', 'status' => '', 'orderCode' => '', 'outCode' => '', 'outChildCode' => '', 'saleReturnChildCode' => ''], 'post', 'trim');
|
|
|
+
|
|
|
+ $where = [['a.is_del', '=', 0]];
|
|
|
+ if ($param['returnCode'] != '') $where[] = ['a.returnCode', 'like', '%' . $param['returnCode'] . '%'];
|
|
|
+ if ($param['status'] !== '') $where[] = ['a.status', '=', $param['status']];
|
|
|
+ if ($param['orderCode'] != '') $where[] = ['a.orderCode', 'like', '%' . $param['orderCode'] . '%'];
|
|
|
+ if ($param['outCode'] != '') $where[] = ['a.outCode', 'like', '%' . $param['outCode'] . '%'];
|
|
|
+ if ($param['outChildCode'] != '') $where[] = ['a.outChildCode', 'like', '%' . $param['outChildCode'] . '%'];
|
|
|
+ if ($param['saleReturnChildCode'] != '') $where[] = ['a.saleReturnChildCode', 'like', '%' . $param['saleReturnChildCode'] . '%'];
|
|
|
+
|
|
|
+ $count = Db::name('sale_return_child')
|
|
|
+ ->alias('a')
|
|
|
+ ->where($where)
|
|
|
+ ->count('a.id');
|
|
|
+
|
|
|
+ $list = Db::name('sale_return_child')
|
|
|
+ ->alias('a')
|
|
|
+ ->field('a.id,a.saleReturnChildCode,a.type,a.outChildCode,a.outCode,a.companyNo,a.companyName,a.customer_code,a.customer_name,a.num,a.sale_price,a.total_price,a.status,a.addtime')
|
|
|
+ ->where($where)
|
|
|
+ ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
|
|
|
+ ->page($param['page'], $param['size'])
|
|
|
+ ->select()
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ return json_show(0, '获取列表成功', ['count' => $count, 'list' => $list]);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //库管收货(退货工单)
|
|
|
+ public function receive()
|
|
|
+ {
|
|
|
+ $param = $this->request->only(['id', 'good_receive_type', 'loss_num', 'remark' => '', 'record' => ''], 'post', 'trim');
|
|
|
+
|
|
|
+ $val = Validate::rule([
|
|
|
+ 'id|ID' => 'require|number|gt:0',
|
|
|
+ 'good_receive_type|货物情况' => 'require|number|between:1,4',
|
|
|
+ 'loss_num|丢失数量' => 'require|number|gt:0',
|
|
|
+ 'remark|备注' => 'max:255'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ if ($val->check($param)) return json_show(1004, $val->getError());
|
|
|
+
|
|
|
+ $info = Db::name('sale_return_child')
|
|
|
+ ->field('id,num,status,returnCode')
|
|
|
+ ->where(['is_del' => 0, 'id' => $param['id']])
|
|
|
+ ->findOrEmpty();
|
|
|
+ if (empty($info)) return json_show(1004, '该退货工单不存在');
|
|
|
+ if ($info['status'] == 1) return json_show(1004, '该退货工单已收货');
|
|
|
+ if ($param['loss_num'] > $info['num']) return json_show(1004, '丢失数量大于下单数量');
|
|
|
+
|
|
|
+ Db::startTrans();
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ $date = date('Y-m-d H:i:s');
|
|
|
+
|
|
|
+ $rs = Db::name('sale_return_child')
|
|
|
+ ->where(['is_del' => 0, 'id' => $param['id'], 'status' => 1])
|
|
|
+ ->update([
|
|
|
+ 'status' => 2,
|
|
|
+ 'updatetime' => $date,
|
|
|
+ 'good_receive_type' => $param['good_receive_type'],
|
|
|
+ 'loss_num' => $param['loss_num'],
|
|
|
+ 'remark' => $param['remark'],
|
|
|
+ 'record' => $param['record']
|
|
|
+ ]);
|
|
|
+
|
|
|
+ //检查所属销售单的退货工单是否全部完成退货
|
|
|
+ $temp = Db::name('sale_return_child')
|
|
|
+ ->field('id')
|
|
|
+ ->where(['is_del' => 0, 'returnCode' => $info['returnCode'], 'status' => 1])
|
|
|
+ ->findOrEmpty();
|
|
|
+ if (empty($temp)) {
|
|
|
+ Db::name('sale_return')
|
|
|
+ ->where(['is_del' => 0, 'returnCode' => $info['returnCode'], 'status' => 12])
|
|
|
+ ->update(['status' => 4, 'updatetime' => $date]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($rs) {
|
|
|
+ Db::commit();
|
|
|
+ return json_show(0, '设置退货工单成功');
|
|
|
+ } else throw new Exception('设置退货工单失败');
|
|
|
+
|
|
|
+ } catch (Exception $exception) {
|
|
|
+ Db::rollback();
|
|
|
+ return json_show(1004, $exception->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|