123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- <?php
- namespace app\admin\controller;
- //销售单退货工单
- use app\admin\model\ActionLog;
- use app\admin\model\DataGroup as DataGroupModel;
- use app\admin\model\ProcessOrder;
- use think\Exception;
- use think\facade\Db;
- use think\facade\Validate;
- class ReorderChild extends Base
- {
- //退货工单创建
- public function add()
- {
- $param = $this->request->only(['returnCode', 'outCode' => '', 'list'], 'post', 'trim');
- $val = Validate::rule([
- 'returnCode|退货单编号' => 'require',
- // 'outCode|发货单编号' => 'require',
- 'list|退货工单集合' => 'require|array|max:100',
- ]);
- 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,num')
- ->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,is_stock')
- ->where(['is_del' => 0, 'orderCode' => $saleReturn['orderCode']])
- ->findOrEmpty();
- if (empty($sale)) return json_show(1004, '销售单不存在');
- if ($param['outCode'] != '') {
- //所有发货工单
- $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|发货工单号' => 'max:255',
- 'return_num|退货数量' => 'require|number|gt:0|max:999999999999',
- 'return_wsm_code|退货仓库编码' => 'require'
- ]);
- if ($saleReturn['num'] < array_sum(array_column($param['list'], 'return_num'))) return json_show(1004, '数量错误');
- Db::startTrans();
- try {
- $date = date('Y-m-d H:i:s');
- //维护退货地址
- if (isset($saleReturnAddr)) {
- 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) {
- if ($val_child->check($value) == false) throw new Exception($val_child->getError());
- $insert[] = [
- 'orderCode' => $saleReturn['orderCode'],
- 'returnCode' => $param['returnCode'],
- 'outCode' => $param['outCode'],
- 'outChildCode' => $value['outChildCode'] ?? '',
- 'order_out_child_status' => isset($value['outChildCode']) ? ($orderOutChild[$value['outChildCode']]['status'] ?? 0) : 0,
- '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' => isset($value['outChildCode']) ? ($orderOutChild[$value['outChildCode']]['wsm_code'] ?? '') : "",
- 'send_num' => isset($value['outChildCode']) ? ($orderOutChild[$value['outChildCode']]['num'] ?? 0) : "",
- '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);
- //维护售前退货单
- $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, '设置退货工单成功');
- } 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' => '', 'order_type' => '', 'supplierNo' => '', 'is_authority' => 1], 'post', 'trim');
- if ($param['is_authority'] == '0' && $param['returnCode'] == '' && $param['orderCode'] == '' && $param['outCode'] == '' && $param['outChildCode'] == '' && $param['saleReturnChildCode'] == '') return json_show('请选择筛选条件');
- $db = Db::name('sale_return_child')
- ->alias('a')
- ->leftJoin('sale_return b', 'b.returnCode=a.returnCode and b.is_del=0')
- ->leftJoin('warehouse_info c', 'c.wsm_code=a.return_wsm_code and c.is_del=0')
- ->where('a.is_del', 0);
- if ($param['returnCode'] != '') $db->whereLike('a.returnCode', '%' . $param['returnCode'] . '%');
- if ($param['status'] !== '') $db->where('a.status', $param['status']);
- if ($param['orderCode'] != '') $db->whereLike('a.orderCode', '%' . $param['orderCode'] . '%');
- if ($param['outCode'] != '') $db->whereLike('a.outCode', '%' . $param['outCode'] . '%');
- if ($param['outChildCode'] != '') $db->whereLike('a.outChildCode', '%' . $param['outChildCode'] . '%');
- if ($param['saleReturnChildCode'] != '') $db->whereLike('a.saleReturnChildCode', '%' . $param['saleReturnChildCode'] . '%');
- if ($param['order_type'] !== '') $db->where('b.order_type', $param['order_type']);
- if ($param['supplierNo'] != '') $db->whereLike('b.supplierNo', '%' . $param['supplierNo'] . '%');
- //启用数据权限
- if ($param['is_authority'] == 1) {
- //供应商账号,只查询该供应商下所有数据
- if ($this->level == 3 && $param['supplierNo'] == '') return json_show(1004, '供应商账号时供应商编码不能为空');
- //1.超管,查看全部;
- //2.业务公司账号-申请人,初始状态只查看自己创建的;
- //3.业务公司账号-退回仓库负责人,只查看自己负责仓库的数据;
- //4.业务公司账号-数据共享接受人,共享给自己的数据;
- if ($this->level == 2) {
- //是否仓库负责人
- $is_contactor = Db::name('warehouse_info')
- ->field('id')
- ->where(['is_del' => 0, 'contactor' => $this->uid])
- ->findOrEmpty();
- if (empty($is_contactor)) {
- $role = $this->checkDataShare();
- $hand = resign_hand_user($this->uid, 0);
- $db->whereIn('b.apply_id', array_unique(array_merge($role[DataGroupModel::$type_全部], $hand)));
- } else {
- $db->where('`a`.`return_wsm_code` in ' . Db::name('warehouse_info')->field('wsm_code')->where(['is_del' => 0, 'contactor' => $this->uid])->buildSql());
- }
- }
- }
- $count = $db->count('a.id');
- $list = $db
- ->field('a.id,a.saleReturnChildCode,a.outChildCode,a.outCode,a.companyNo,a.companyName,a.customer_code,a.customerName,a.num,a.sale_price,a.total_price,a.status,a.addtime,b.supplierNo,b.supplierName,a.return_wsm_code,c.name return_wsm_name,c.supplierNo return_supplierNo,c.supplierName return_supplierName,c.contactor_name return_contactor_name,b.order_type,a.orderCode,a.returnCode,b.apply_name,b.apply_id')
- ->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' => ''], '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) == false) return json_show(1004, $val->getError());
- $info = Db::name('sale_return_child')
- ->alias('a')
- ->field('a.id,a.num,a.status,a.returnCode,a.outChildCode,a.return_num,a.return_wsm_code,a.orderCode,a.outCode,a.saleReturnChildCode,a.apply_id,b.good_code spuCode,c.status order_out_status,b.id saleid,d.num thnum,d.id sr_id,d.apply_id sr_apply_id')
- ->leftJoin('sale b', 'b.orderCode=a.orderCode')
- ->leftJoin('order_out_child c', 'c.outChildCode=a.outChildCode AND c.is_del=0')
- ->leftJoin('sale_return d', 'd.returnCode=a.returnCode')
- ->where(['a.is_del' => 0, 'a.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');
- //维护退货工单
- 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'],
- ]);
- //修改状态,添加待办
- ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
- "order_code" => $info['saleReturnChildCode'],//单号
- "status" => $info['status'],//这里的status是之前的值
- "action_remark" => '',//备注
- "action_type" => "status"//新建create,编辑edit,更改状态status
- ], "THGD", 2, [
- 'status' => 2,
- 'updatetime' => $date,
- 'good_receive_type' => $param['good_receive_type'],
- 'loss_num' => $param['loss_num'],
- 'remark' => $param['remark'],
- ]);
- ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
- "order_type" => 'THGD',
- "order_code" => $info['saleReturnChildCode'],//单号
- "order_id" => $info['id'],
- "order_status" => 2,
- "before_status" => $info['status'],
- 'holder_id' => $info['apply_id']
- ]);
- //检查所属销售单的退货工单是否全部完成退货
- $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]);
- //修改状态,添加待办
- ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
- "order_code" => $info['returnCode'],//单号
- "status" => 12,//这里的status是之前的值
- "action_remark" => '',//备注
- "action_type" => "status"//新建create,编辑edit,更改状态status
- ], "XSTHD", 4, ['status' => 4, 'updatetime' => $date]);
- ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
- "order_type" => 'XSTHD',
- "order_code" => $info['returnCode'],//单号
- "order_id" => $info['sr_id'],
- "order_status" => 4,
- "before_status" => 12,
- 'holder_id' => $info['sr_apply_id']
- ]);
- //维护销售单
- $order = Db::name('sale')->where(['is_del' => 0, 'id' => $info['saleid']])->findOrEmpty();
- //未发货数量要减去发货单上的发货数量
- // $out_send_num = Db::name('order_out')
- // ->where(['is_del' => 0, 'orderCode' => $info['orderCode'], 'status' => [0, 1]])
- // ->sum('send_num');
- // $order['wsend_num'] -= $out_send_num;
- $old_status = $order['status'];
- $thnum = $info['thnum'];//退货总数量
- // if ($order['wsend_num'] < $thnum) throw new Exception("销售单未发货数量不足退货");
- //如果 发货 维护销售单的
- //else 未发货数量减,
- if ($info['order_out_status'] == 1) {
- $order['wsend_num'] -= $thnum;
- $order['send_num'] += $thnum;
- }
- // $lor = $order['status'];
- $order['status'] = $order['wsend_num'] == 0 ? 2 : ($order['send_num'] == 0 ? 0 : 1);
- $order['send_status'] = $order['wsend_num'] == 0 ? 3 : ($order['send_num'] == 0 ? 1 : 2);
- $order['th_num'] += $thnum;
- if ($order['th_num'] == $order['send_num'] && $order['wsend_num'] == 0) {
- $order['status'] = 3;
- }
- $order['th_fee'] += round($thnum * $order['sale_price'], 2);
- $order['updatetime'] = $date;
- $uap = Db::name("sale")->save($order);
- if ($uap == false) throw new Exception('销售单订单更新失败');
- if ($old_status != $order['status']) {
- ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
- "order_code" => $order["orderCode"],//出库单号
- "status" => $old_status,//这里的status是之前的值
- "action_remark" => '',//备注
- "action_type" => "status"//新建create,编辑edit,更改状态status
- ], "XSQRD", $order['status'], $order);
- ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
- "order_type" => 'XSQRD',
- "order_code" => $order["orderCode"],//出库单号
- "order_id" => $order["id"],
- "order_status" => $order['status'],
- "before_status" => $old_status
- ]);
- }
- }
- //发货工单数量减少
- $tmp = Db::name('order_out_child')
- ->where(['is_del' => 0, 'outChildCode' => $info['outChildCode']])
- ->field('id,num')
- ->findOrEmpty();
- $tmp['num'] -= $info['return_num'];
- $tmp['updatetime'] = $date;
- if ($tmp['num'] <= 0) $tmp['is_del'] = 1;
- Db::name('order_out_child')
- ->where(['is_del' => 0, 'id' => $tmp['id']])
- ->update($tmp);
- //维护发货单
- $temp = Db::name('order_out')
- ->field('id,send_num')
- ->where(['is_del' => 0, 'outCode' => $info['outCode']])
- ->findOrEmpty();
- $temp['send_num'] -= $info['return_num'];
- $temp['updatetime'] = $date;
- if ($temp['send_num'] <= 0) $temp['is_del'] = 1;
- Db::name('order_out')
- ->where('id', $temp['id'])
- ->update($temp);
- //维护bn
- if ($info['order_out_status'] == 1) {
- //待发货,说明此时有库存,有bn号
- Db::name('child_bn')
- ->where(['orderCode' => $info['orderCode'], 'outCode' => $info['outCode'], 'childCode' => $info['outChildCode']])
- ->inc('num', $info['return_num'])
- ->update(['updatetime' => $date]);
- Db::name('good_stock')
- ->where(['is_del' => 0, 'spuCode' => $info['spuCode'], 'wsm_code' => $info['return_wsm_code']])
- ->inc('usable_stock', $info['return_num'])
- ->dec('wait_out_stock', $info['return_num'])
- ->update(['updatetime' => $date]);
- }
- Db::commit();
- return json_show(0, '设置退货工单成功');
- } catch (Exception $exception) {
- Db::rollback();
- return json_show(1004, $exception->getMessage());
- }
- }
- //详情
- public function info()
- {
- $param = $this->request->only(['id'], 'post', 'trim');
- $val = Validate::rule(['id' => 'require|number|gt:0']);
- if ($val->check($param) == false) return json_show(1004, $val->getError());
- $info = Db::name('sale_return_child')
- ->alias('a')
- ->field('a.*,b.supplierNo,b.supplierName,c.name return_wsm_name,c.supplierNo return_supplierNo,c.supplierName return_supplierName,c.contactor_name return_contactor_name,b.good_code spuCode,b.order_type')
- ->leftJoin('sale_return b', 'b.returnCode=a.returnCode and b.is_del=0')
- ->leftJoin('warehouse_info c', 'c.wsm_code=a.return_wsm_code and c.is_del=0')
- ->where(['a.is_del' => 0, 'a.id' => $param['id']])
- ->findOrEmpty();
- return empty($info) ? json_show(1004, '该退货工单不存在') : json_show(0, '获取退货工单详情成功', $info);
- }
- //更新退货工单标记
- public function setRecord()
- {
- $param = $this->request->only(['id', 'record'], 'post', 'trim');
- $val = Validate::rule([
- 'id|退货工单id' => 'require|number|gt:0',
- 'record|标记内容' => 'require',
- ]);
- if ($val->check($param) == false) return json_show(1004, $val->getError());
- $temp = Db::name('sale_return_child')
- ->field('id')
- ->where(['is_del' => 0, 'id' => $param['id']])
- ->findOrEmpty();
- if (empty($temp)) return json_show(1004, '该退货工单不存在');
- $rs = Db::name('sale_return_child')
- ->where(['is_del' => 0, 'id' => $param['id']])
- ->update(['record' => $param['record'], 'updatetime' => date('Y-m-d H:i:s')]);
- return $rs ? json_show(0, '更新退货工单标记内容成功') : json_show(1004, '更新退货工单标记内容失败');
- }
- }
|