AfterChild.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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 app\admin\model\SaleCgdPrice;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,a.companyNo,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,c.id sale_id')
  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. $defective_sum =array_sum(array_column($param['list'],"defective_num"));
  180. if($defective_sum>0){
  181. $defective_wsm = Db::name("order_returninfo")->where(["returnCode"=>$param['returnCode']])->findOrEmpty();
  182. if(empty($defective_wsm)|| $defective_wsm['return_wsm']=='') return json_show(1004, '退货工单次品仓库不存在');
  183. }
  184. Db::startTrans();
  185. try {
  186. $date = date('Y-m-d H:i:s');
  187. $insert = [];
  188. foreach ($param['list'] as $value) {
  189. if ($val_child->check($value) == false) throw new Exception($val_child->getError());
  190. if (!isset($info[$value['id']])) throw new Exception($value['id'] . '退货工单设置信息不存在');
  191. if ($info[$value['id']]['status'] != 1) throw new Exception($value['id'] . '退货工单设置信息状态有误');
  192. //退货工单
  193. if ($value['can_sell_num'] > 0) {
  194. $insert[] = [
  195. 'returnCode' => $param['returnCode'],
  196. 'orderCode' => $info[$value['id']]['orderCode'],
  197. 'outCode' => $info[$value['id']]['outCode'],
  198. 'order_out_child_status' => $info[$value['id']]['order_out_child_status'],
  199. 'outChildCode' => $info[$value['id']]['outChildCode'],
  200. 'send_wsm_code' => $info[$value['id']]['send_wsm_code'],
  201. 'send_num' => $info[$value['id']]['send_num'],
  202. 'return_num' => $info[$value['id']]['return_num'],
  203. 'return_wsm_code' => $info[$value['id']]['return_wsm_code'],
  204. 'can_sell_num' => $value['can_sell_num'],
  205. 'defective_num' => $value['defective_num'],
  206. 'loss_num' => $value['loss_num'],
  207. 'apply_id' => $this->uid,
  208. 'apply_name' => $this->uname,
  209. 'status' => 2,
  210. 'is_del' => 0,
  211. 'remark' => '',
  212. 'addtime' => $date,
  213. 'updatetime' => $date,
  214. 'type' => 1,
  215. 'return_num_total' => $value['can_sell_num'],
  216. ];
  217. }
  218. //如果次品仓入库数量大于0,另生成退货工单,以类型区分
  219. if ($value['defective_num'] > 0) {
  220. $insert[] = [
  221. 'returnCode' => $param['returnCode'],
  222. 'orderCode' => $info[$value['id']]['orderCode'],
  223. 'outCode' => $info[$value['id']]['outCode'],
  224. 'order_out_child_status' => $info[$value['id']]['order_out_child_status'],
  225. 'outChildCode' => $info[$value['id']]['outChildCode'],
  226. 'send_wsm_code' => $info[$value['id']]['send_wsm_code'],
  227. 'send_num' => $info[$value['id']]['send_num'],
  228. 'return_num' => $info[$value['id']]['return_num'],
  229. 'return_wsm_code' => $defective_wsm['return_wsm'],
  230. 'can_sell_num' => $value['can_sell_num'],
  231. 'defective_num' => $value['defective_num'],
  232. 'loss_num' => $value['loss_num'],
  233. 'apply_id' => $this->uid,
  234. 'apply_name' => $this->uname,
  235. 'status' => 2,
  236. 'is_del' => 0,
  237. 'remark' => '',
  238. 'addtime' => $date,
  239. 'updatetime' => $date,
  240. 'type' => 2,
  241. 'return_num_total' => $value['defective_num'],
  242. ];
  243. }
  244. if ($insert) Db::name('order_return_child')->insertAll($insert);
  245. //发货工单数量减少
  246. $ot=Db::name('order_out_child')
  247. ->data(['updatetime' => $date])
  248. ->where(['is_del' => 0, 'outChildCode' => $info[$value['id']]['outChildCode']])
  249. ->dec('num', $info[$value['id']]['return_num'])
  250. ->update();
  251. if($ot==false) throw new \Exception('发货工单更新失败');
  252. $orde = Db::name('order_out')
  253. ->field('id,outCode,status,send_num,error_num')
  254. ->where(['outCode' => $info[$value['id']]['outCode'], 'is_del' => 0])
  255. ->findOrEmpty();
  256. if (empty($orde)) throw new \Exception('未找到出库单数据');
  257. $sale = Db::name('sale')
  258. ->field('id,th_num,th_fee,sale_price,good_code,good_name,cat_id')
  259. ->where(['is_del' => 0, 'orderCode' => $info[$value['id']]['orderCode']])
  260. ->findOrEmpty();
  261. if (empty($sale)) throw new \Exception('未找到销售单数据');
  262. $od_status = $orde['status'];
  263. if ($info[$value['id']]['return_num'] >= $orde['send_num']) $orde['status'] = 4;
  264. $orde['send_num'] -=$info[$value['id']]['return_num'];
  265. $orde['error_num'] += $info[$value['id']]['return_num'];
  266. $orde['updatetime'] = $date;
  267. $out = Db::name('order_out')->save($orde);
  268. if ($out == false) throw new Exception('发货单更新失败');
  269. $action_log_user = ['id' => $this->uid, 'nickname' => $this->uname];
  270. $order = ['order_code' => $orde['outCode'], 'status' => $od_status, 'action_remark' => '', 'action_type' => 'edit'];
  271. ActionLog::logAdd($action_log_user, $order, 'CKD', $orde['status'], $this->post);
  272. $process = ['order_code' => $orde['outCode'], 'order_id' => $orde['id'], 'order_status' => $orde['status'], 'order_type' => 'CKD', 'before_status' => $od_status];
  273. ProcessOrder::AddProcess($action_log_user, $process);
  274. $sale['th_num'] += $info[$value['id']]['return_num'];
  275. $sale['th_fee'] += round($info[$value['id']]['return_num']* $sale['sale_price'], 2);
  276. $sale['updatetime'] = $date;
  277. $sup = Db::name('sale')->save($sale);
  278. if ($sup == false) throw new \Exception('销售单更新失败');
  279. }
  280. $total = array_sum(array_column($param['list'], 'can_sell_num'));
  281. // if ($total > 0 || ) {
  282. if ($order_return['is_stock'] != 1) {
  283. //根据转化表判断该非库存品是否转化为库存品
  284. $spuCode = Db::name('good_change_stock')
  285. ->where(['old_spuCode' => $order_return['good_code'], 'old_order_type' => $order_return['order_type']])
  286. ->value('spuCode', '');
  287. if ($spuCode == '') {
  288. //将非库存品转化为库存品
  289. $spuCode = makeNo('SKU');
  290. if ($order_return['order_type'] == 2) {
  291. $good = Db::name('good_basic')
  292. ->where('spuCode', $order_return['good_code'])
  293. ->findOrEmpty();
  294. if (!empty($good)) {
  295. $up2=Db::name('good_basic')->insert(array_merge($good, [
  296. 'id' => null,
  297. 'is_stock' => 1,
  298. 'spuCode' => $spuCode,
  299. 'addtime' => $date,
  300. 'updatetime' => $date,
  301. 'status' => 7,
  302. 'createrid'=>0,
  303. 'creater'=>'system'
  304. ]));
  305. if($up2==false) throw new Exception("{$order_return['good_code']} 商品库新增失败");
  306. }
  307. } else {
  308. $good = Db::name('good_zixun')
  309. ->where(['spuCode' => $order_return['good_code']])
  310. ->findOrEmpty();
  311. if (!empty($good)) {
  312. $up= Db::name('good_basic')->insert([
  313. 'spuCode' => $spuCode,
  314. 'good_code' => $good['good_code'],
  315. 'good_name' => $good['good_name'],
  316. 'cat_id' => $good['cat_id'],
  317. 'brand_id' => $good['brand_id'],
  318. 'companyNo' => $good['companyNo'],
  319. 'companyName' => $good['companyName'],
  320. 'good_unit' => $good['good_unit'],
  321. 'good_type' => $good['good_type'],
  322. 'moq' => $good['moq'],
  323. 'is_exclusive' => 0,
  324. 'customized' => $good['customized'],
  325. 'tax' => $good['tax'],
  326. 'supplierNo' => $good['supplierNo'],
  327. 'supplierName' => $good['supplierName'],
  328. 'is_auth' => $good['is_auth'],
  329. 'auth_img' => $good['auth_img'],
  330. 'is_stock' => 1,
  331. 'after_sales' => '',
  332. 'craft_desc' => $good['craft_desc'],
  333. 'good_remark' => $good['good_remark'],
  334. 'good_size' => '',
  335. 'weight' => $good['weight'],
  336. 'packing_way' => '',
  337. 'packing_size' => '',
  338. 'packing_spec' => '',
  339. 'packing_list' => '',
  340. 'packing_weight' => 0,
  341. 'good_bar' => '',
  342. 'supply_area' => $good['supply_area'],
  343. 'delivery_place' => '',
  344. 'origin_place' => '',
  345. 'delivery_day' => '',
  346. 'lead_time' => '',
  347. 'sample_day' => '',
  348. 'sample_fee' => '',
  349. 'good_img' => $good['good_img'],
  350. 'good_thumb_img' => $good['good_thumb_img'],
  351. 'good_info_img' => $good['good_info_img'],
  352. 'cert_fee' => 0,
  353. 'packing_fee' => 0,
  354. 'cost_fee' => 0,
  355. 'mark_fee' => 0,
  356. 'demo_fee' => 0,
  357. 'open_fee' => 0,
  358. 'noble_metal' => $good['noble_metal'],
  359. 'noble_weight' => 0,
  360. 'is_gold_price' => $good['is_gold_price'],
  361. 'cgd_gold_price' => 0,
  362. 'market_price' => 0,
  363. 'nake_price' => 0,
  364. 'is_step' => 0,
  365. 'is_online' => 0,
  366. 'status' => 7,
  367. 'createrid' => 0,
  368. 'creater' => 'system',
  369. 'field_change' => '',
  370. 'is_del' => 0,
  371. 'addtime' => $date,
  372. 'updatetime' => $date,
  373. 'is_diff' => 0,
  374. 'config' => '',
  375. 'other_config' => '',
  376. 'stock_moq' => 0,
  377. 'is_support_barter' => 1,
  378. 'chargerid' => $good['createrid'],
  379. 'charger' => $good['creater'],
  380. 'is_support_stock' => 1
  381. ]);
  382. if($up==false) throw new Exception("咨询商品录入商品库失败");
  383. }
  384. }
  385. if (!empty($good)) {
  386. //关联表增数据
  387. $dd= Db::name('good_change_stock')
  388. ->insert([
  389. 'old_spuCode' => $order_return['good_code'],
  390. 'old_order_type' => $order_return['order_type'],
  391. 'spuCode' => $spuCode,
  392. 'addtime' => $date,
  393. 'updatetime' => $date,
  394. ]);
  395. if($dd==false) throw new Exception('关联表新增失败');
  396. } else throw new Exception('未找到对应的商品数据');
  397. }
  398. if($defective_sum>0){
  399. $defective_stock= Db::name('good_stock')
  400. ->where(['is_del' => 0, 'spuCode' => $spuCode, 'wsm_code' => $defective_wsm['return_wsm']])
  401. ->findOrEmpty();
  402. if(empty($defective_stock)){
  403. $defective_stockid = Db::name('good_stock')
  404. ->insertGetId([
  405. 'project_code' => '',
  406. 'spuCode' => $spuCode,
  407. 'wsm_code' => $defective_wsm['return_wsm'],
  408. 'wait_in_stock' => 0,
  409. 'wait_out_stock' => 0,
  410. 'usable_stock' => $defective_sum,
  411. 'intra_stock' => 0,
  412. 'total_stock' => $defective_sum,
  413. 'status' => 1,
  414. 'is_del' => 0,
  415. 'warn_stock' => 0,
  416. 'addtime' => $date,
  417. 'updatetime' => $date,
  418. ]);
  419. if($defective_stockid==0) throw new Exception('次品仓库库存更新失败');
  420. }else{
  421. $defective_stockid = $defective_stock['id'];
  422. $defective_up= Db::name('good_stock')
  423. ->data(['updatetime' => $date])
  424. ->where(['is_del' => 0, 'id' => $defective_stockid])
  425. ->inc('usable_stock', $defective_sum)
  426. ->inc('total_stock', $defective_sum)
  427. ->update();
  428. if($defective_up==false) throw new Exception('次品仓库库存更新失败');
  429. }
  430. $total+=$defective_sum;
  431. }
  432. //新增bn记录,维护旧有bn记录
  433. $child_bns = Db::name('child_bn')
  434. ->field('id,num,origin_price')
  435. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode']])
  436. ->order(['num' => 'desc', 'id' => 'asc'])
  437. ->cursor();
  438. $origin_price = $j = 0;
  439. foreach ($child_bns as $child_bn) {
  440. if ($origin_price === 0) $origin_price = $child_bn['origin_price'];
  441. $bn= Db::name('child_bn')
  442. ->data(['updatetime' => $date])
  443. ->where(['id' => $child_bn['id']])
  444. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  445. ->update();
  446. if($bn==false) throw new \Exception('商品库bn数据更新失败');
  447. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  448. if ($total == 0) break;
  449. }
  450. foreach ($param['list'] as $value) {
  451. if ($value['can_sell_num'] > 0) {
  452. $cell_stock= Db::name('good_stock')
  453. ->where(['is_del' => 0, 'spuCode' => $spuCode, 'wsm_code' =>$info[$value['id']]['return_wsm_code']])
  454. ->findOrEmpty();
  455. if(empty($cell_stock)){
  456. $stockid = Db::name('good_stock')
  457. ->insertGetId([
  458. 'project_code' => '',
  459. 'spuCode' => $spuCode,
  460. 'wsm_code' => $info[$value['id']]['return_wsm_code'],
  461. 'wait_in_stock' => 0,
  462. 'wait_out_stock' => 0,
  463. 'usable_stock' => $value['can_sell_num'],
  464. 'intra_stock' => 0,
  465. 'total_stock' => $value['can_sell_num'],
  466. 'status' => 1,
  467. 'is_del' => 0,
  468. 'warn_stock' => 0,
  469. 'addtime' => $date,
  470. 'updatetime' => $date,
  471. ]);
  472. if($stockid==false) throw new \Exception('商品库存新增失败');
  473. }else{
  474. $stockid = $cell_stock['id'];
  475. $cell_up= Db::name('good_stock')
  476. ->data(['updatetime' => $date])
  477. ->where(['is_del' => 0, 'id' =>$stockid])
  478. ->inc('usable_stock',$value['can_sell_num'])
  479. ->inc('total_stock', $value['can_sell_num'])
  480. ->update();
  481. if($cell_up==false) throw new Exception('次品仓库库存更新失败');
  482. }
  483. $stockinfo= Db::name('good_stock_info')
  484. ->insert([
  485. 'stockid' => $stockid,
  486. 'bnCode' => substr(makeNo("BN"), 0, -2) . str_pad($j++, 2, '0', STR_PAD_LEFT),
  487. 'total_num' => $value['can_sell_num'],
  488. 'used_num' => 0,
  489. 'balance_num' => $value['can_sell_num'],
  490. 'origin_price' => $origin_price,
  491. 'addtime' => $date,
  492. 'updatetime' => $date,
  493. ]);
  494. if($stockinfo==false) throw new \Exception('商品库存BN新增失败');
  495. }
  496. if($value['defective_num']>0){
  497. $defective_info= Db::name('good_stock_info')
  498. ->insert([
  499. 'stockid' => $defective_stockid,
  500. 'bnCode' => substr(makeNo('BN'), 0, -2) . str_pad($j++, 2, '0', STR_PAD_LEFT),
  501. 'total_num' => $value['defective_num'],
  502. 'used_num' => 0,
  503. 'balance_num' => $value['defective_num'],
  504. 'origin_price' => $origin_price,
  505. 'addtime' => $date,
  506. 'updatetime' => $date,
  507. ]);
  508. if($defective_info==false) throw new \Exception('次品仓商品库存BN新增失败');
  509. }
  510. }
  511. }
  512. else {
  513. $spuCode = $order_return['good_code'];
  514. //库存品,直接维护stock和info
  515. $stockids = Db::name('good_stock')
  516. ->where(['is_del' => 0, 'spuCode' => $order_return['good_code'], 'wsm_code' => array_column($info, 'return_wsm_code')])
  517. ->column('id', 'wsm_code');
  518. if($defective_sum>0){
  519. $defective_stock= Db::name('good_stock')
  520. ->where(['is_del' => 0, 'spuCode' => $spuCode, 'wsm_code' => $defective_wsm['return_wsm']])
  521. ->findOrEmpty();
  522. if(empty($defective_stock)){
  523. $defective_stockid = Db::name('good_stock')
  524. ->insertGetId([
  525. 'project_code' => '',
  526. 'spuCode' => $spuCode,
  527. 'wsm_code' => $defective_wsm['return_wsm'],
  528. 'wait_in_stock' => 0,
  529. 'wait_out_stock' => 0,
  530. 'usable_stock' => $defective_sum,
  531. 'intra_stock' => 0,
  532. 'total_stock' => $defective_sum,
  533. 'status' => 1,
  534. 'is_del' => 0,
  535. 'warn_stock' => 0,
  536. 'addtime' => $date,
  537. 'updatetime' => $date,
  538. ]);
  539. if($defective_stockid==0) throw new Exception('次品仓库库存更新失败');
  540. }else{
  541. $defective_stockid = $defective_stock['id'];
  542. $defective_up= Db::name('good_stock')
  543. ->data(['updatetime' => $date])
  544. ->where(['is_del' => 0, 'id' => $defective_stockid])
  545. ->inc('usable_stock', $defective_sum)
  546. ->inc('total_stock', $defective_sum)
  547. ->update();
  548. if($defective_up==false) throw new Exception('次品仓库库存更新失败');
  549. }
  550. // $total+=$defective_sum;
  551. }
  552. foreach ($param['list'] as $value) {
  553. //定义了stockid,维护bn
  554. if($value['can_sell_num']>0){
  555. if (isset($stockids[$info[$value['id']]['return_wsm_code']])) {
  556. $good_stock_info = Db::name('good_stock_info')
  557. ->where(['stockid' => $stockids[$info[$value['id']]['return_wsm_code']]])
  558. ->findOrEmpty();
  559. if (empty($good_stock_info)) {
  560. //从child_bn查询bn号并维护,新建到good_stock_info中
  561. $child_bns = Db::name('child_bn')
  562. ->field('id,num,bnCode,origin_price')
  563. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode'], 'childCode' => $info[$value['id']]['outChildCode']])
  564. ->order(['num' => 'desc', 'id' => 'asc'])
  565. ->cursor();
  566. $total = $value['can_sell_num'];
  567. foreach ($child_bns as $child_bn) {
  568. $bns= Db::name('child_bn')
  569. ->data(['updatetime' => $date])
  570. ->where(['id' => $child_bn['id']])
  571. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  572. ->update();
  573. if($bns==false) throw new \Exception('商品库存BN数量新增失败');
  574. $bninfo= Db::name('good_stock_info')
  575. ->insert([
  576. 'stockid' => $stockids[$info[$value['id']]['return_wsm_code']],
  577. 'bnCode' => $child_bn['bnCode'],
  578. 'total_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  579. 'used_num' => 0,
  580. 'balance_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  581. 'origin_price' => $child_bn['origin_price'],
  582. 'addtime' => $date,
  583. 'updatetime' => $date,
  584. ]);
  585. if($bninfo==false) throw new \Exception('商品库存BN更新失败');
  586. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  587. if ($total == 0) break;
  588. }
  589. } else {
  590. //维护记录
  591. $bninfo= Db::name('good_stock_info')
  592. ->data(['updatetime' => $date])
  593. ->where(['id' => $good_stock_info['id']])
  594. ->inc('balance_num', $value['can_sell_num'])//可用数量增加
  595. ->dec('used_num', $value['can_sell_num'])//已用数量减少
  596. ->update();
  597. if($bninfo==false) throw new \Exception('商品库存BN更新失败');
  598. }
  599. //维护good_stock
  600. $stockinfoe= Db::name('good_stock')
  601. ->data(['updatetime' => $date])
  602. ->where(['is_del' => 0, 'id' => $stockids[$info[$value['id']]['return_wsm_code']]])
  603. ->inc('usable_stock', $value['can_sell_num'])
  604. ->inc('total_stock', $value['can_sell_num'])
  605. ->update();
  606. if($stockinfoe==false) throw new \Exception('商品库存更新失败');
  607. } else {
  608. //新建good_stock和good_stock_info
  609. $stockid = Db::name('good_stock')
  610. ->insertGetId([
  611. 'project_code' => '',
  612. 'spuCode' => $order_return['good_code'],
  613. 'wsm_code' => $value['return_wsm_code'],
  614. 'wait_in_stock' => 0,
  615. 'wait_out_stock' => 0,
  616. 'usable_stock' => $value['can_sell_num'],
  617. 'intra_stock' => 0,
  618. 'total_stock' => $value['can_sell_num'],
  619. 'status' => 1,
  620. 'is_del' => 0,
  621. 'warn_stock' => 0,
  622. 'addtime' => $date,
  623. 'updatetime' => $date,
  624. ]);
  625. if($stockid==false) throw new \Exception('商品库存新增失败');
  626. $stockids[$info[$value['id']]['return_wsm_code']] = $stockid;
  627. //从child_bn查询bn号并维护,新建到good_stock_info中
  628. $child_bns = Db::name('child_bn')
  629. ->field('id,num,bnCode,origin_price')
  630. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode'], 'childCode' => $info[$value['id']]['outChildCode']])
  631. ->order(['num' => 'desc', 'id' => 'asc'])
  632. ->cursor();
  633. $total = $value['can_sell_num'];
  634. foreach ($child_bns as $child_bn) {
  635. $bn=Db::name('child_bn')
  636. ->data(['updatetime' => $date])
  637. ->where(['id' => $child_bn['id']])
  638. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  639. ->update();
  640. if($bn==false) throw new \Exception('商品库存BN更新失败');
  641. $bninfo=Db::name('good_stock_info')
  642. ->insert([
  643. 'stockid' => $stockid,
  644. 'bnCode' => $child_bn['bnCode'],
  645. 'total_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  646. 'used_num' => 0,
  647. 'balance_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  648. 'origin_price' => $child_bn['origin_price'],
  649. 'addtime' => $date,
  650. 'updatetime' => $date,
  651. ]);
  652. if($bninfo==false) throw new \Exception('商品库存BN新增失败');
  653. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  654. if ($total == 0) break;
  655. }
  656. }
  657. }
  658. if($value['defective_num']>0){
  659. //从child_bn查询bn号并维护,新建到good_stock_info中
  660. $child_bns = Db::name('child_bn')
  661. ->field('id,num,bnCode,origin_price')
  662. ->where(['orderCode' => $order_return['orderCode'], 'outCode' => $order_return['outCode'], 'childCode' => $info[$value['id']]['outChildCode']])
  663. ->order(['num' => 'desc', 'id' => 'asc'])
  664. ->cursor();
  665. $total = $value['defective_num'];
  666. foreach ($child_bns as $child_bn) {
  667. $bn=Db::name('child_bn')
  668. ->data(['updatetime' => $date])
  669. ->where(['id' => $child_bn['id']])
  670. ->dec('num', ($child_bn['num'] >= $total) ? $total : $child_bn['num'])
  671. ->update();
  672. if($bn==false) throw new \Exception('商品库存BN更新失败');
  673. $bninfo=Db::name('good_stock_info')
  674. ->insert([
  675. 'stockid' => $defective_stockid,
  676. 'bnCode' => $child_bn['bnCode'],
  677. 'total_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  678. 'used_num' => 0,
  679. 'balance_num' => ($child_bn['num'] >= $total) ? $total : $child_bn['num'],
  680. 'origin_price' => $child_bn['origin_price'],
  681. 'addtime' => $date,
  682. 'updatetime' => $date,
  683. ]);
  684. if($bninfo==false) throw new \Exception('次品商品库存BN新增失败');
  685. $total = ($child_bn['num'] >= $total) ? 0 : ($total - $child_bn['num']);
  686. if ($total == 0) break;
  687. }
  688. }
  689. }
  690. //维护商品层面的库存数
  691. $goodinfo= Db::name('good')
  692. ->data(['updatetime' => $date])
  693. ->where(['is_del' => 0, 'spuCode' => $order_return['good_code']])
  694. ->inc('usable_stock', array_sum(array_column($param['list'], 'can_sell_num')))
  695. ->update();
  696. if($goodinfo==false) throw new \Exception('商品可售库存数更新失败');
  697. }
  698. // }
  699. //售后申请单状态维护
  700. $Reup=Db::name('order_return')
  701. ->where(['is_del' => 0, 'returnCode' => $param['returnCode'], 'status' => 12])
  702. ->update(['status' => 5, 'updatetime' => $date]);
  703. if($Reup==false) throw new Exception('售后申请退货状态更新失败');
  704. //售后退货工单设置信息维护
  705. $process = ['order_code' => $param['returnCode'], 'order_id' => $order_return['id'], 'order_status'
  706. =>5, 'order_type' => 'SHD', 'before_status' => 0, 'holder_id' => $this->uid];
  707. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  708. $orde = ['order_code' =>$param['returnCode'], 'status' => 5, 'action_remark' => '', 'action_type' => 'create'];
  709. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $orde, 'SHD', 1, $orde);
  710. $Reupchild=Db::name('order_return_prepare')
  711. ->where(['is_del' => 0, 'status' => 1, 'id' => array_column($info, 'id')])
  712. ->update(['updatetime' => $date, 'status' => 3]);
  713. if($Reupchild==false) throw new Exception('售后申请退货工单状态更新失败');
  714. $data = [
  715. "orderCode" => $order_return['orderCode'],
  716. "th_type" => 2,
  717. "th_num" => $order_return['error_num'],
  718. "th_fee" => round($order_return['error_num'] * $order_return['sale_price'], 2),
  719. "thCode" => $param['returnCode'],
  720. "spuCode" => $order_return['good_code'],
  721. "good_name" => $order_return['good_name'],
  722. "cat_id" => $order_return['cat_id'],
  723. "apply_id" => $order_return['apply_id'],
  724. "apply_name" => $order_return['apply_name'],
  725. "addtime" =>$date,
  726. "status" => 1,
  727. "is_del" => 0
  728. ];
  729. $inse = Db::name("th_data")->insert($data);
  730. if ($inse == false) throw new Exception('退回单更新失败');
  731. if($order_return['order_type']==1)SaleCgdPrice::RePrice($order_return['sale_id'],$order_return['error_num']);
  732. Db::commit();
  733. return json_show(0, '库管收货成功');
  734. } catch (Exception $exception) {
  735. Db::rollback();
  736. return json_show(1004, $exception->getMessage());
  737. }
  738. }
  739. //更新退货工单标记
  740. public function setRecord()
  741. {
  742. $param = $this->request->only(['id', 'record'], 'post', 'trim');
  743. $val = Validate::rule([
  744. 'id|退货工单id' => 'require|number|gt:0',
  745. 'record|标记内容' => 'require',
  746. ]);
  747. if ($val->check($param) == false) return json_show(1004, $val->getError());
  748. $temp = Db::name('order_return_child')
  749. ->field('id')
  750. ->where(['is_del' => 0, 'id' => $param['id']])
  751. ->findOrEmpty();
  752. if (empty($temp)) return json_show(1004, '该退货工单不存在');
  753. $rs = Db::name('order_return_child')
  754. ->where(['is_del' => 0, 'id' => $param['id']])
  755. ->update(['record' => $param['record'], 'updatetime' => date('Y-m-d H:i:s')]);
  756. return $rs ? json_show(0, '更新退货工单标记内容成功') : json_show(1004, '更新退货工单标记内容失败');
  757. }
  758. //详情
  759. public function info()
  760. {
  761. $param = $this->request->only(['id'], 'post', 'trim');
  762. $val = Validate::rule([
  763. 'id|退货工单id' => 'require|number|gt:0',
  764. ]);
  765. if ($val->check($param) == false) return json_show(1004, $val->getError());
  766. $info = Db::name('order_return_child')
  767. ->alias('a')
  768. ->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')
  769. ->leftJoin('warehouse_info b', 'b.wsm_code=a.send_wsm_code')
  770. ->leftJoin('warehouse_info c', 'c.wsm_code=a.return_wsm_code')
  771. ->leftJoin('order_return d', 'd.returnCode=a.returnCode and d.is_del=0')
  772. ->where(['a.is_del' => 0, 'a.id' => $param['id']])
  773. ->findOrEmpty();
  774. if (empty($info)) return json_show(1004, '该退货工单不存在');
  775. return empty($info) ? json_show(1004, '该退货工单不存在') : json_show(0, '获取退货工单详情成功', $info);
  776. }
  777. //获取 售后退货工单设置信息
  778. public function getPreList()
  779. {
  780. $param = $this->request->only(['page' => 1, 'size' => 10, 'returnCode' => '', 'status' => ''], 'post', 'trim');
  781. $db = Db::name('order_return_prepare')
  782. ->alias('a')
  783. ->leftJoin('order_return b', 'b.returnCode=a.returnCode and b.is_del=0')
  784. ->leftJoin('order_out_child c', 'c.outChildCode=a.outChildCode and c.is_del=0')
  785. ->leftJoin('warehouse_info d', 'd.wsm_code=c.wsm_code')
  786. ->leftJoin('warehouse_info e', 'e.wsm_code=a.return_wsm_code')
  787. ->where('a.is_del', 0);
  788. if ($param['returnCode'] != '') $db->whereLike('a.returnCode', '%' . $param['returnCode'] . '%');
  789. if ($param['status'] !== '') $db->where('a.status', $param['status']);
  790. $count = $db
  791. ->count('a.id');
  792. $list = $db
  793. ->field(['a.id',
  794. 'a.returnCode',
  795. 'b.orderCode',
  796. 'c.outCode',
  797. 'a.order_out_child_status',
  798. 'a.outChildCode',
  799. 'd.supplierName send_supplierName',
  800. 'd.supplierNo send_supplierNo',
  801. 'd.name send_wsm_name',
  802. 'c.wsm_code send_wsm_code',
  803. 'd.contactor_name send_contactor_name',
  804. 'c.num send_num',
  805. 'a.return_num',
  806. 'e.supplierName return_supplierName',
  807. 'e.supplierNo return_supplierNo',
  808. 'e.name return_wsm_name',
  809. 'a.return_wsm_code',
  810. 'e.contactor_name return_contactor_name',
  811. 'a.status'])
  812. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  813. ->page($param['page'], $param['size'])
  814. ->select()
  815. ->toArray();
  816. return json_show(0, '获取列表成功', ['count' => $count, 'list' => $list]);
  817. }
  818. }