AfterChild.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ActionLog;use app\admin\model\DataGroup as DataGroupModel;
  4. use app\admin\model\ProcessOrder;use think\Exception;
  5. use think\facade\Db;
  6. use think\facade\Validate;
  7. //退货工单
  8. class AfterChild extends Base
  9. {
  10. //退货工单列表
  11. public function getList()
  12. {
  13. $param = $this->request->only(['page' => 1, 'size' => 10, 'returnCode' => '', 'status' => '', 'orderCode' => '', 'outCode' => '', 'order_type' => '', 'supplierNo' => '', 'is_authority' => 0, 'type' => '', 'outChildCode' => '', 'return_wsm_code' => '', 'companyNo' => '', 'supplierNo' => '', 'customer_code' => '','use_type'=>''], 'post', 'trim');
  14. $db = Db::name('order_return_child')
  15. ->alias('a')
  16. ->leftJoin('warehouse_info b', 'b.wsm_code=a.send_wsm_code')
  17. ->leftJoin('warehouse_info c', 'c.wsm_code=a.return_wsm_code')
  18. ->leftJoin('order_return d', 'd.returnCode=a.returnCode and d.is_del=0')
  19. ->leftJoin('sale e','e.orderCode=a.orderCode AND e.is_del=0')
  20. ->leftJoin('platform p', 'p.id=e.platform_id AND p.is_del=0')
  21. ->where('a.is_del', 0);
  22. if ($param['returnCode'] != '') $db->whereLike('a.returnCode', '%' . $param['returnCode'] . '%');
  23. if ($param['status'] !== '') $db->where('a.status', $param['status']);
  24. if ($param['orderCode'] != '') $db->whereLike('a.orderCode', '%' . $param['orderCode'] . '%');
  25. if ($param['outCode'] != '') $db->whereLike('a.outCode', '%' . $param['outCode'] . '%');
  26. if ($param['order_type'] !== '') $db->where('d.order_type', $param['order_type']);
  27. if ($param['supplierNo'] != '') $db->whereLike('d.supplierNo', '%' . $param['supplierNo'] . '%');
  28. if ($param['type'] !== '') $db->where('a.type', $param['type']);
  29. if ($param['outChildCode'] !== '') $db->whereLike('a.outChildCode', '%' . $param['outChildCode'] . '%');
  30. if ($param['return_wsm_code'] !== '') $db->whereLike('a.return_wsm_code', '%' . $param['return_wsm_code'] . '%');
  31. if ($param['companyNo'] !== '') $db->whereLike('d.companyNo', '%' . $param['companyNo'] . '%');
  32. if ($param['supplierNo'] !== '') $db->whereLike('d.supplierNo', '%' . $param['supplierNo'] . '%');
  33. if ($param['customer_code'] !== '') $db->whereLike('d.customer_code', '%' . $param['customer_code'] . '%');
  34. if ($param['use_type'] !== '') $db->where('p.use_type', $param['use_type']);
  35. if ($param['is_authority'] == '0' && $param['returnCode'] == '' && $param['orderCode'] == '' && $param['outCode'] == '') return json_show('请选择筛选条件');
  36. //数据权限相关
  37. if ($param['is_authority'] == 1) {
  38. //供应商账号,只查询该供应商下所有数据
  39. if ($this->level == 3 && $param['supplierNo'] == '') return json_show(1004, '供应商账号时供应商编码不能为空');
  40. //1.超管,查看全部;
  41. //2.业务公司账号-申请人,初始状态只查看自己创建的;
  42. //3.业务公司账号-退回仓库负责人,只查看自己负责仓库的数据;
  43. //4.业务公司账号-数据共享接受人,共享给自己的数据;
  44. if ($this->level == 2) {
  45. //是否仓库负责人
  46. $is_contactor = Db::name('warehouse_info')
  47. ->field('id')
  48. ->where(['is_del' => 0, 'contactor' => $this->uid])
  49. ->findOrEmpty();
  50. if (empty($is_contactor)) {
  51. $role = $this->checkDataShare();
  52. $hand = resign_hand_user($this->uid, 0);
  53. $db->whereIn('d.apply_id', array_unique(array_merge($role[DataGroupModel::$type_全部], $hand)));
  54. } else {
  55. $db->where('`a`.`return_wsm_code` in ' . Db::name('warehouse_info')->field('wsm_code')->where(['is_del' => 0, 'contactor' => $this->uid])->buildSql());
  56. }
  57. }
  58. }
  59. $count = $db
  60. ->count('a.id');
  61. $list = $db
  62. ->field('a.id,a.returnCode,a.orderCode,a.outCode,a.order_out_child_status,a.outChildCode,b.supplierName send_supplierName,b.supplierNo send_supplierNo,b.name send_wsm_name,a.send_wsm_code,b.contactor_name send_contactor_name,a.send_num,a.return_num,c.supplierName return_supplierName,c.supplierNo return_supplierNo,c.name return_wsm_name,a.return_wsm_code,c.contactor_name return_contactor_name,a.status,d.companyNo,d.companyName,d.customer_code,d.customer_name,d.order_type,d.apply_id,d.apply_name,a.type,a.can_sell_num,a.defective_num,a.loss_num,a.return_num_total,d.good_code,d.good_name,p.use_type,e.order_source')
  63. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  64. ->page($param['page'], $param['size'])
  65. ->select()
  66. ->toArray();
  67. return json_show(0, '获取列表成功', ['count' => $count, 'list' => $list]);
  68. }
  69. //设置退货工单
  70. public function setOrderReturnChild()
  71. {
  72. $param = $this->request->only(['returnCode', 'list'], 'post', 'trim');
  73. $val = Validate::rule([
  74. 'returnCode|售后申请单编码' => 'require',
  75. 'list|工单集合' => 'require|array|max:100',
  76. ]);
  77. if ($val->check($param) == false) return json_show(1004, $val->getError());
  78. $info = Db::name('order_return')
  79. ->where(['is_del' => 0, 'returnCode' => $param['returnCode']])
  80. ->findOrEmpty();
  81. if (empty($info)) return json_show(1004, '售后申请单不存在');
  82. if ($info['status'] != 11) return json_show(1004, '售后申请单状态错误');
  83. if ($info['is_receive'] != 1) return json_show(1004, '未收到货,无需设置退货工单');
  84. $order_out = Db::name('order_out')
  85. ->field('id,status order_out_status,orderCode')
  86. ->where(['is_del' => 0, 'outCode' => $info['outCode']])
  87. ->findOrEmpty();
  88. if (empty($order_out)) return json_show(1004, '未找到对应的发货单');
  89. $val_child = Validate::rule([
  90. 'outChildCode|发货工单号' => 'require',
  91. 'return_num|退货数量' => 'require|number|egt:0|max:999999999999',
  92. 'return_wsm_code|退货仓库编码' => 'require'
  93. ]);
  94. //发货工单详情
  95. $order_out_child = Db::name('order_out_child')
  96. ->where(['is_del' => 0, 'outCode' => $info['outCode'], 'outChildCode' => array_column($param['list'], 'outChildCode')])
  97. ->column('num,wsm_code,status', 'outChildCode');
  98. //所有仓库详情
  99. $wsm_info = Db::name('warehouse_info')
  100. ->where(['is_del' => 0, 'wsm_code' => array_unique(array_merge(array_column($order_out_child, 'wsm_code'), array_column($param['list'], 'return_wsm_code')))])
  101. ->column('id', 'wsm_code');
  102. if (array_sum(array_column($param['list'], 'return_num')) != $info['error_num']) return json_show(1004, '数量错误');
  103. Db::startTrans();
  104. try {
  105. $insert = [];
  106. $date = date('Y-m-d H:i:s');
  107. foreach ($param['list'] as $value) {
  108. if ($val_child->check($value) == false) throw new Exception($val_child->getError());
  109. if (!isset($order_out_child[$value['outChildCode']])) throw new Exception($value['outChildCode'] . '发货工单不存在');
  110. if (!isset($wsm_info[$value['return_wsm_code']])) throw new Exception($value['return_wsm_code'] . '退货仓库不存在');
  111. if ($value['return_num'] <= 0) continue;//数量为0的不再处理
  112. $insert[] = [
  113. 'returnCode' => $param['returnCode'],
  114. 'outChildCode' => $value['outChildCode'],
  115. 'order_out_child_status' => $order_out_child[$value['outChildCode']]['status'],
  116. 'return_num' => $value['return_num'],
  117. 'return_wsm_code' => $value['return_wsm_code'],
  118. 'apply_id' => $this->uid,
  119. 'apply_name' => $this->uname,
  120. 'is_del' => 0,
  121. 'status' => 1,
  122. 'remark' => '',
  123. 'addtime' => $date,
  124. 'updatetime' => $date
  125. ];
  126. }
  127. if ($insert) Db::name('order_return_prepare')->insertAll($insert);
  128. Db::name('order_return')
  129. ->where(['is_del' => 0, 'id' => $info['id'], 'status' => 11])
  130. ->update(['status' => 4, 'updatetime' => $date]);
  131. Db::commit();
  132. return json_show(0, '设置退货工单成功');
  133. } catch (Exception $exception) {
  134. Db::rollback();
  135. return json_show(1004, $exception->getMessage());
  136. }
  137. }
  138. //库管收货
  139. public function deliveryOrderReturnChild()
  140. {
  141. $param = $this->request->only(['returnCode', 'list'], 'post', 'trim');
  142. $val = Validate::rule([
  143. 'returnCode|退货申请单号' => 'require',
  144. 'list|退货工单集合' => 'require|array|max:100',
  145. ]);
  146. if ($val->check($param) == false) return json_show(1004, $val->getError());
  147. $order_return = Db::name('order_return')
  148. ->alias('a')
  149. ->field('a.id,a.good_code,a.good_name,c.sale_price,c.cat_id,a.error_num,a.apply_id,a.apply_name,a.outCode,b.orderCode,c.is_stock,c.order_type,a.status')
  150. ->leftJoin('order_out b', 'b.outCode=a.outCode AND b.is_del=0')
  151. ->leftJoin('sale c', 'c.orderCode=b.orderCode AND c.is_del=0')
  152. ->where(['a.is_del' => 0, 'a.returnCode' => $param['returnCode']])
  153. ->findOrEmpty();
  154. if (empty($order_return)) return json_show(1004, '售后申请单不存在');
  155. if ($order_return['status'] != 12) return json_show(1004, '售后申请单状态有误');
  156. //退货工单设置信息
  157. $info = Db::name('order_return_prepare')
  158. ->alias('a')
  159. ->leftJoin('order_return b', 'b.returnCode=a.returnCode and b.is_del=0')
  160. ->leftJoin('order_out_child c', 'c.outChildCode=a.outChildCode and c.is_del=0')
  161. ->leftJoin('warehouse_info d', 'd.wsm_code=c.wsm_code')
  162. ->leftJoin('warehouse_info e', 'e.wsm_code=a.return_wsm_code')
  163. ->where(['a.is_del' => 0, 'a.returnCode' => $param['returnCode']])
  164. ->column('a.id,a.returnCode,b.orderCode,c.outCode,a.order_out_child_status,a.outChildCode,d.supplierName send_supplierName,d.supplierNo send_supplierNo,d.name send_wsm_name,c.wsm_code send_wsm_code,d.contactor_name send_contactor_name,c.num send_num,a.return_num,e.supplierName return_supplierName,e.supplierNo return_supplierNo,e.name return_wsm_name,a.return_wsm_code,e.contactor_name return_contactor_name,a.status', 'a.id');
  165. // $info = Db::name('order_return_prepare')
  166. // ->alias('a')
  167. // ->leftJoin('order_out_child b', 'b.outChildCode=a.outChildCode')
  168. // ->leftJoin('warehouse_info c', 'c.wsm_code=a.return_wsm_code')
  169. // ->where(['a.is_del' => 0, 'a.returnCode' => $param['returnCode'], 'a.id' => array_column($param['list'], 'id')])
  170. // ->column('a.*,b.wsm_code,c.addr,c.addrs_code,c.mobile,c.contactor_name', 'a.id');
  171. if (empty($info)) return json_show(1004, '退货工单设置信息不存在');
  172. $val_child = Validate::rule([
  173. 'id|退货工单设置信息id' => 'require|gt:0|max:999999999',
  174. 'can_sell_num|销售仓入库数量' => 'require|number|egt:0|max:999999999999',
  175. 'defective_num|次品仓入库数量' => 'require|number|egt:0|max:999999999999',
  176. 'loss_num|丢失数量' => 'require|number|egt:0|max:999999999999',
  177. 'remark|备注' => 'max:255'
  178. ]);
  179. Db::startTrans();
  180. try {
  181. $date = date('Y-m-d H:i:s');
  182. $insert = [];
  183. foreach ($param['list'] as $value) {
  184. if ($val_child->check($value) == false) throw new Exception($val_child->getError());
  185. if (!isset($info[$value['id']])) throw new Exception($value['id'] . '退货工单设置信息不存在');
  186. if ($info[$value['id']]['status'] != 1) throw new Exception($value['id'] . '退货工单设置信息状态有误');
  187. //退货工单
  188. if ($value['can_sell_num'] > 0) {
  189. $insert[] = [
  190. 'returnCode' => $param['returnCode'],
  191. 'orderCode' => $info[$value['id']]['orderCode'],
  192. 'outCode' => $info[$value['id']]['outCode'],
  193. 'order_out_child_status' => $info[$value['id']]['order_out_child_status'],
  194. 'outChildCode' => $info[$value['id']]['outChildCode'],
  195. 'send_wsm_code' => $info[$value['id']]['send_wsm_code'],
  196. 'send_num' => $info[$value['id']]['send_num'],
  197. 'return_num' => $info[$value['id']]['return_num'],
  198. 'return_wsm_code' => $info[$value['id']]['return_wsm_code'],
  199. 'can_sell_num' => $value['can_sell_num'],
  200. 'defective_num' => $value['defective_num'],
  201. 'loss_num' => $value['loss_num'],
  202. 'apply_id' => $this->uid,
  203. 'apply_name' => $this->uname,
  204. 'status' => 2,
  205. 'is_del' => 0,
  206. 'remark' => '',
  207. 'addtime' => $date,
  208. 'updatetime' => $date,
  209. 'type' => 1,
  210. 'return_num_total' => $value['can_sell_num'],
  211. ];
  212. }
  213. //如果次品仓入库数量大于0,另生成退货工单,以类型区分
  214. if ($value['defective_num'] > 0) {
  215. $insert[] = [
  216. 'returnCode' => $param['returnCode'],
  217. 'orderCode' => $info[$value['id']]['orderCode'],
  218. 'outCode' => $info[$value['id']]['outCode'],
  219. 'order_out_child_status' => $info[$value['id']]['order_out_child_status'],
  220. 'outChildCode' => $info[$value['id']]['outChildCode'],
  221. 'send_wsm_code' => $info[$value['id']]['send_wsm_code'],
  222. 'send_num' => $info[$value['id']]['send_num'],
  223. 'return_num' => $info[$value['id']]['return_num'],
  224. 'return_wsm_code' => $info[$value['id']]['return_wsm_code'],
  225. 'can_sell_num' => $value['can_sell_num'],
  226. 'defective_num' => $value['defective_num'],
  227. 'loss_num' => $value['loss_num'],
  228. 'apply_id' => $this->uid,
  229. 'apply_name' => $this->uname,
  230. 'status' => 2,
  231. 'is_del' => 0,
  232. 'remark' => '',
  233. 'addtime' => $date,
  234. 'updatetime' => $date,
  235. 'type' => 2,
  236. 'return_num_total' => $value['defective_num'],
  237. ];
  238. }
  239. if ($insert) Db::name('order_return_child')->insertAll($insert);
  240. //发货工单数量减少
  241. $ot=Db::name('order_out_child')
  242. ->data(['updatetime' => $date])
  243. ->where(['is_del' => 0, 'outChildCode' => $info[$value['id']]['outChildCode']])
  244. ->dec('num', $info[$value['id']]['return_num'])
  245. ->update();
  246. if($ot==false) throw new \Exception('发货工单更新失败');
  247. $orde = Db::name('order_out')
  248. ->field('id,outCode,status')
  249. ->where(['outCode' => $info[$value['id']]['outCode'], 'is_del' => 0])
  250. ->findOrEmpty();
  251. if (empty($orde)) throw new \Exception('未找到出库单数据');
  252. $sale = Db::name('sale')
  253. ->field('id,th_num,th_fee,sale_price,good_code,good_name,cat_id')
  254. ->where(['is_del' => 0, 'orderCode' => $info[$value['id']]['orderCode']])
  255. ->findOrEmpty();
  256. if (empty($sale)) throw new \Exception('未找到销售单数据');
  257. $od_status = $orde['status'];
  258. $orde['status'] = 4;
  259. $orde['updatetime'] = $date;
  260. $out = Db::name('order_out')->save($orde);
  261. if ($out == false) throw new Exception('发货单更新失败');
  262. $action_log_user = ['id' => $this->uid, 'nickname' => $this->uname];
  263. $order = ['order_code' => $orde['outCode'], 'status' => $od_status, 'action_remark' => '', 'action_type' => 'edit'];
  264. ActionLog::logAdd($action_log_user, $order, 'CKD', $orde['status'], $this->post);
  265. $process = ['order_code' => $orde['outCode'], 'order_id' => $orde['id'], 'order_status' => $orde['status'], 'order_type' => 'CKD', 'before_status' => $od_status];
  266. ProcessOrder::AddProcess($action_log_user, $process);
  267. $sale['th_num'] += $info[$value['id']]['return_num'];
  268. $sale['th_fee'] += round($info[$value['id']]['return_num']* $sale['sale_price'], 2);
  269. $sale['updatetime'] = $date;
  270. $sup = Db::name('sale')->save($sale);
  271. if ($sup == false) throw new \Exception('销售单更新失败');
  272. }
  273. $total = array_sum(array_column($param['list'], 'can_sell_num'));
  274. if ($total > 0) {
  275. if ($order_return['is_stock'] != 1) {
  276. //根据转化表判断该非库存品是否转化为库存品
  277. $spuCode = Db::name('good_change_stock')
  278. ->where(['old_spuCode' => $order_return['good_code'], 'old_order_type' => $order_return['order_type']])
  279. ->value('spuCode', '');
  280. if ($spuCode == '') {
  281. //将非库存品转化为库存品
  282. $spuCode = makeNo('SKU');
  283. if ($order_return['order_type'] == 2) {
  284. $good = Db::name('good_basic')
  285. ->where('spuCode', $order_return['good_code'])
  286. ->findOrEmpty();
  287. if (!empty($good)) {
  288. $up2=Db::name('good_basic')->insert(array_merge($good, [
  289. 'id' => null,
  290. 'is_stock' => 1,
  291. 'spuCode' => $spuCode,
  292. 'addtime' => $date,
  293. 'updatetime' => $date,
  294. 'status' => 7,
  295. 'createrid'=>0,
  296. 'creater'=>'system'
  297. ]));
  298. if($up2==false) throw new \Exception("{$order_return['good_code']} 商品库新增失败");
  299. }
  300. } else {
  301. $good = Db::name('good_zixun')
  302. ->where(['spuCode' => $order_return['good_code']])
  303. ->findOrEmpty();
  304. if (!empty($good)) {
  305. $up= Db::name('good_basic')->insert([
  306. 'spuCode' => $spuCode,
  307. 'good_code' => $good['good_code'],
  308. 'good_name' => $good['good_name'],
  309. 'cat_id' => $good['cat_id'],
  310. 'brand_id' => $good['brand_id'],
  311. 'companyNo' => $good['companyNo'],
  312. 'companyName' => $good['companyName'],
  313. 'good_unit' => $good['good_unit'],
  314. 'good_type' => $good['good_type'],
  315. 'moq' => $good['moq'],
  316. 'is_exclusive' => 0,
  317. 'customized' => $good['customized'],
  318. 'tax' => $good['tax'],
  319. 'supplierNo' => $good['supplierNo'],
  320. 'supplierName' => $good['supplierName'],
  321. 'is_auth' => $good['is_auth'],
  322. 'auth_img' => $good['auth_img'],
  323. 'is_stock' => 1,
  324. 'after_sales' => '',
  325. 'craft_desc' => $good['craft_desc'],
  326. 'good_remark' => $good['good_remark'],
  327. 'good_size' => '',
  328. 'weight' => $good['weight'],
  329. 'packing_way' => '',
  330. 'packing_size' => '',
  331. 'packing_spec' => '',
  332. 'packing_list' => '',
  333. 'packing_weight' => 0,
  334. 'good_bar' => '',
  335. 'supply_area' => $good['supply_area'],
  336. 'delivery_place' => '',
  337. 'origin_place' => '',
  338. 'delivery_day' => '',
  339. 'lead_time' => '',
  340. 'sample_day' => '',
  341. 'sample_fee' => '',
  342. 'good_img' => $good['good_img'],
  343. 'good_thumb_img' => $good['good_thumb_img'],
  344. 'good_info_img' => $good['good_info_img'],
  345. 'cert_fee' => 0,
  346. 'packing_fee' => 0,
  347. 'cost_fee' => 0,
  348. 'mark_fee' => 0,
  349. 'demo_fee' => 0,
  350. 'open_fee' => 0,
  351. 'noble_metal' => $good['noble_metal'],
  352. 'noble_weight' => 0,
  353. 'is_gold_price' => $good['is_gold_price'],
  354. 'cgd_gold_price' => 0,
  355. 'market_price' => 0,
  356. 'nake_price' => 0,
  357. 'is_step' => 0,
  358. 'is_online' => 0,
  359. 'status' => 7,
  360. 'createrid' => 0,
  361. 'creater' => 'system',
  362. 'field_change' => '',
  363. 'is_del' => 0,
  364. 'addtime' => $date,
  365. 'updatetime' => $date,
  366. 'is_diff' => 0,
  367. 'config' => '',
  368. 'other_config' => '',
  369. 'stock_moq' => 0,
  370. 'is_support_barter' => 1,
  371. 'chargerid' => $good['createrid'],
  372. 'charger' => $good['creater'],
  373. 'is_support_stock' => 1
  374. ]);
  375. if($up==false) throw new \Exception("咨询商品录入商品库失败");
  376. }
  377. }
  378. if (!empty($good)) {
  379. //关联表增数据
  380. $dd= Db::name('good_change_stock')
  381. ->insert([
  382. 'old_spuCode' => $order_return['good_code'],
  383. 'old_order_type' => $order_return['order_type'],
  384. 'spuCode' => $spuCode,
  385. 'addtime' => $date,
  386. 'updatetime' => $date,
  387. ]);
  388. if($dd==false) throw new \Exception('关联表新增失败');
  389. } else throw new Exception('未找到对应的商品数据');
  390. }
  391. //新增bn记录,维护旧有bn记录
  392. $child_bns = Db::name('child_bn')
  393. ->field('id,num,origin_price')
  394. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode']])
  395. ->order(['num' => 'desc', 'id' => 'asc'])
  396. ->cursor();
  397. $origin_price = $j = 0;
  398. foreach ($child_bns as $child_bn) {
  399. if ($origin_price === 0) $origin_price = $child_bn['origin_price'];
  400. $bn= Db::name('child_bn')
  401. ->data(['updatetime' => $date])
  402. ->where(['id' => $child_bn['id']])
  403. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  404. ->update();
  405. if($bn==false) throw new \Exception('商品库bn数据更新失败');
  406. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  407. if ($total == 0) break;
  408. }
  409. foreach ($param['list'] as $value) {
  410. if ($value['can_sell_num'] > 0) {
  411. $stockid = Db::name('good_stock')
  412. ->insertGetId([
  413. 'project_code' => '',
  414. 'spuCode' => $spuCode,
  415. 'wsm_code' => $info[$value['id']]['return_wsm_code'],
  416. 'wait_in_stock' => 0,
  417. 'wait_out_stock' => 0,
  418. 'usable_stock' => $value['can_sell_num'],
  419. 'intra_stock' => 0,
  420. 'total_stock' => $value['can_sell_num'],
  421. 'status' => 1,
  422. 'is_del' => 0,
  423. 'warn_stock' => 0,
  424. 'addtime' => $date,
  425. 'updatetime' => $date,
  426. ]);
  427. if($stockid==false) throw new \Exception('商品库存新增失败');
  428. $stockinfo= Db::name('good_stock_info')
  429. ->insert([
  430. 'stockid' => $stockid,
  431. 'bnCode' => substr(makeNo("BN"), 0, -2) . str_pad($j++, 2, '0', STR_PAD_LEFT),
  432. 'total_num' => $value['can_sell_num'],
  433. 'used_num' => 0,
  434. 'balance_num' => $value['can_sell_num'],
  435. 'origin_price' => $origin_price,
  436. 'addtime' => $date,
  437. 'updatetime' => $date,
  438. ]);
  439. if($stockinfo==false) throw new \Exception('商品库存BN新增失败');
  440. }
  441. }
  442. }
  443. else {
  444. //库存品,直接维护stock和info
  445. $stockids = Db::name('good_stock')
  446. ->where(['is_del' => 0, 'spuCode' => $order_return['good_code'], 'wsm_code' => array_column($info, 'return_wsm_code')])
  447. ->column('id', 'wsm_code');
  448. foreach ($param['list'] as $value) {
  449. //定义了stockid,维护bn
  450. if (isset($stockids[$info[$value['id']]['return_wsm_code']])) {
  451. $good_stock_info = Db::name('good_stock_info')
  452. ->where(['stockid' => $stockids[$info[$value['id']]['return_wsm_code']]])
  453. ->findOrEmpty();
  454. if (empty($good_stock_info)) {
  455. //从child_bn查询bn号并维护,新建到good_stock_info中
  456. $child_bns = Db::name('child_bn')
  457. ->field('id,num,bnCode,origin_price')
  458. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode'], 'childCode' => $info[$value['id']]['outChildCode']])
  459. ->order(['num' => 'desc', 'id' => 'asc'])
  460. ->cursor();
  461. $total = $value['can_sell_num'];
  462. foreach ($child_bns as $child_bn) {
  463. $bns= Db::name('child_bn')
  464. ->data(['updatetime' => $date])
  465. ->where(['id' => $child_bn['id']])
  466. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  467. ->update();
  468. if($bns==false) throw new \Exception('商品库存BN数量新增失败');
  469. $bninfo= Db::name('good_stock_info')
  470. ->insert([
  471. 'stockid' => $stockids[$info[$value['id']]['return_wsm_code']],
  472. 'bnCode' => $child_bn['bnCode'],
  473. 'total_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  474. 'used_num' => 0,
  475. 'balance_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  476. 'origin_price' => $child_bn['origin_price'],
  477. 'addtime' => $date,
  478. 'updatetime' => $date,
  479. ]);
  480. if($bninfo==false) throw new \Exception('商品库存BN更新失败');
  481. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  482. if ($total == 0) break;
  483. }
  484. } else {
  485. //维护记录
  486. $bninfo= Db::name('good_stock_info')
  487. ->data(['updatetime' => $date])
  488. ->where(['id' => $good_stock_info['id']])
  489. ->inc('balance_num', $value['can_sell_num'])//可用数量增加
  490. ->dec('used_num', $value['can_sell_num'])//已用数量减少
  491. ->update();
  492. if($bninfo==false) throw new \Exception('商品库存BN更新失败');
  493. }
  494. //维护good_stock
  495. $stockinfoe= Db::name('good_stock')
  496. ->data(['updatetime' => $date])
  497. ->where(['is_del' => 0, 'id' => $stockids[$info[$value['id']]['return_wsm_code']]])
  498. ->inc('usable_stock', $value['can_sell_num'])
  499. ->inc('total_stock', $value['can_sell_num'])
  500. ->update();
  501. if($stockinfoe==false) throw new \Exception('商品库存更新失败');
  502. } else {
  503. //新建good_stock和good_stock_info
  504. $stockid = Db::name('good_stock')
  505. ->insertGetId([
  506. 'project_code' => '',
  507. 'spuCode' => $order_return['good_code'],
  508. 'wsm_code' => $value['return_wsm_code'],
  509. 'wait_in_stock' => 0,
  510. 'wait_out_stock' => 0,
  511. 'usable_stock' => $value['can_sell_num'],
  512. 'intra_stock' => 0,
  513. 'total_stock' => $value['can_sell_num'],
  514. 'status' => 1,
  515. 'is_del' => 0,
  516. 'warn_stock' => 0,
  517. 'addtime' => $date,
  518. 'updatetime' => $date,
  519. ]);
  520. if($stockid==false) throw new \Exception('商品库存新增失败');
  521. //从child_bn查询bn号并维护,新建到good_stock_info中
  522. $child_bns = Db::name('child_bn')
  523. ->field('id,num,bnCode,origin_price')
  524. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode'], 'childCode' => $info[$value['id']]['outChildCode']])
  525. ->order(['num' => 'desc', 'id' => 'asc'])
  526. ->cursor();
  527. $total = $value['can_sell_num'];
  528. foreach ($child_bns as $child_bn) {
  529. $bn=Db::name('child_bn')
  530. ->data(['updatetime' => $date])
  531. ->where(['id' => $child_bn['id']])
  532. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  533. ->update();
  534. if($bn==false) throw new \Exception('商品库存BN更新失败');
  535. $bninfo=Db::name('good_stock_info')
  536. ->insert([
  537. 'stockid' => $stockid,
  538. 'bnCode' => $child_bn['bnCode'],
  539. 'total_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  540. 'used_num' => 0,
  541. 'balance_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  542. 'origin_price' => $child_bn['origin_price'],
  543. 'addtime' => $date,
  544. 'updatetime' => $date,
  545. ]);
  546. if($bninfo==false) throw new \Exception('商品库存BN新增失败');
  547. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  548. if ($total == 0) break;
  549. }
  550. }
  551. }
  552. //维护商品层面的库存数
  553. $goodinfo= Db::name('good')
  554. ->data(['updatetime' => $date])
  555. ->where(['is_del' => 0, 'spuCode' => $order_return['good_code']])
  556. ->inc('usable_stock', array_sum(array_column($param['list'], 'can_sell_num')))
  557. ->update();
  558. if($goodinfo==false) throw new \Exception('商品可售库存数更新失败');
  559. }
  560. }
  561. //售后申请单状态维护
  562. $Reup=Db::name('order_return')
  563. ->where(['is_del' => 0, 'returnCode' => $param['returnCode'], 'status' => 12])
  564. ->update(['status' => 5, 'updatetime' => $date]);
  565. if($Reup==false) throw new \Exception('售后申请退货状态更新失败');
  566. //售后退货工单设置信息维护
  567. $Reupchild=Db::name('order_return_prepare')
  568. ->where(['is_del' => 0, 'status' => 1, 'id' => array_column($info, 'id')])
  569. ->update(['updatetime' => $date, 'status' => 3]);
  570. if($Reupchild==false) throw new \Exception('售后申请退货工单状态更新失败');
  571. $data = [
  572. "orderCode" => $order_return['orderCode'],
  573. "th_type" => 2,
  574. "th_num" => $order_return['error_num'],
  575. "th_fee" => round($order_return['error_num'] * $order_return['sale_price'], 2),
  576. "thCode" => $param['returnCode'],
  577. "spuCode" => $order_return['good_code'],
  578. "good_name" => $order_return['good_name'],
  579. "cat_id" => $order_return['cat_id'],
  580. "apply_id" => $order_return['apply_id'],
  581. "apply_name" => $order_return['apply_name'],
  582. "addtime" =>$date,
  583. "status" => 1,
  584. "is_del" => 0
  585. ];
  586. $inse = Db::name("th_data")->insert($data);
  587. if ($inse == false) throw new Exception('退回单更新失败');
  588. Db::commit();
  589. return json_show(0, '库管收货成功');
  590. } catch (Exception $exception) {
  591. Db::rollback();
  592. return json_show(1004, $exception->getMessage());
  593. }
  594. }
  595. //更新退货工单标记
  596. public function setRecord()
  597. {
  598. $param = $this->request->only(['id', 'record'], 'post', 'trim');
  599. $val = Validate::rule([
  600. 'id|退货工单id' => 'require|number|gt:0',
  601. 'record|标记内容' => 'require',
  602. ]);
  603. if ($val->check($param) == false) return json_show(1004, $val->getError());
  604. $temp = Db::name('order_return_child')
  605. ->field('id')
  606. ->where(['is_del' => 0, 'id' => $param['id']])
  607. ->findOrEmpty();
  608. if (empty($temp)) return json_show(1004, '该退货工单不存在');
  609. $rs = Db::name('order_return_child')
  610. ->where(['is_del' => 0, 'id' => $param['id']])
  611. ->update(['record' => $param['record'], 'updatetime' => date('Y-m-d H:i:s')]);
  612. return $rs ? json_show(0, '更新退货工单标记内容成功') : json_show(1004, '更新退货工单标记内容失败');
  613. }
  614. //详情
  615. public function info()
  616. {
  617. $param = $this->request->only(['id'], 'post', 'trim');
  618. $val = Validate::rule([
  619. 'id|退货工单id' => 'require|number|gt:0',
  620. ]);
  621. if ($val->check($param) == false) return json_show(1004, $val->getError());
  622. $info = Db::name('order_return_child')
  623. ->alias('a')
  624. ->field('a.*,b.supplierName send_supplierName,b.supplierNo send_supplierNo,b.name send_wsm_name,b.contactor_name send_contactor_name,c.supplierName return_supplierName,c.supplierNo return_supplierNo,c.name return_wsm_name,c.contactor_name return_contactor_name,d.companyNo,d.companyName,d.customer_code,d.customer_name,d.order_type,d.good_code spuCode')
  625. ->leftJoin('warehouse_info b', 'b.wsm_code=a.send_wsm_code')
  626. ->leftJoin('warehouse_info c', 'c.wsm_code=a.return_wsm_code')
  627. ->leftJoin('order_return d', 'd.returnCode=a.returnCode and d.is_del=0')
  628. ->where(['a.is_del' => 0, 'a.id' => $param['id']])
  629. ->findOrEmpty();
  630. if (empty($info)) return json_show(1004, '该退货工单不存在');
  631. return empty($info) ? json_show(1004, '该退货工单不存在') : json_show(0, '获取退货工单详情成功', $info);
  632. }
  633. //获取 售后退货工单设置信息
  634. public function getPreList()
  635. {
  636. $param = $this->request->only(['page' => 1, 'size' => 10, 'returnCode' => '', 'status' => ''], 'post', 'trim');
  637. $db = Db::name('order_return_prepare')
  638. ->alias('a')
  639. ->leftJoin('order_return b', 'b.returnCode=a.returnCode and b.is_del=0')
  640. ->leftJoin('order_out_child c', 'c.outChildCode=a.outChildCode and c.is_del=0')
  641. ->leftJoin('warehouse_info d', 'd.wsm_code=c.wsm_code')
  642. ->leftJoin('warehouse_info e', 'e.wsm_code=a.return_wsm_code')
  643. ->where('a.is_del', 0);
  644. if ($param['returnCode'] != '') $db->whereLike('a.returnCode', '%' . $param['returnCode'] . '%');
  645. if ($param['status'] !== '') $db->where('a.status', $param['status']);
  646. $count = $db
  647. ->count('a.id');
  648. $list = $db
  649. ->field(['a.id',
  650. 'a.returnCode',
  651. 'b.orderCode',
  652. 'c.outCode',
  653. 'a.order_out_child_status',
  654. 'a.outChildCode',
  655. 'd.supplierName send_supplierName',
  656. 'd.supplierNo send_supplierNo',
  657. 'd.name send_wsm_name',
  658. 'c.wsm_code send_wsm_code',
  659. 'd.contactor_name send_contactor_name',
  660. 'c.num send_num',
  661. 'a.return_num',
  662. 'e.supplierName return_supplierName',
  663. 'e.supplierNo return_supplierNo',
  664. 'e.name return_wsm_name',
  665. 'a.return_wsm_code',
  666. 'e.contactor_name return_contactor_name',
  667. 'a.status'])
  668. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  669. ->page($param['page'], $param['size'])
  670. ->select()
  671. ->toArray();
  672. return json_show(0, '获取列表成功', ['count' => $count, 'list' => $list]);
  673. }
  674. }