Reorder.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\GoodLog;
  4. use app\admin\model\ProcessOrder;
  5. use think\facade\Db;
  6. use think\App;
  7. use app\admin\model\ActionLog;
  8. //销售单退货
  9. class Reorder extends Base
  10. {
  11. public function __construct(App $app)
  12. {
  13. parent::__construct($app);
  14. }
  15. public function create(){
  16. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  17. if($ordeCode==''){
  18. return error_show(1004,"参数orderCode 不能为空");
  19. }
  20. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  21. if(empty($order)){
  22. return error_show(1005,"未找到订单数据");
  23. }
  24. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  25. if($errorCode==''){
  26. return error_show(1004,"参数errorCode 不能为空");
  27. }
  28. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  29. if(empty($error)){
  30. return error_show(1005,"未找到退货原因数据");
  31. }
  32. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  33. $thnum =isset($this->post['thnum']) &&$this->post['thnum']!=''?intval($this->post['thnum']) :"";
  34. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  35. if($token==''){
  36. return error_show(105,"参数token不能为空");
  37. }
  38. $user =GetUserInfo($token);
  39. if(empty($user)||$user['code']!=0){
  40. return error_show(1002,"申请人数据不存在");
  41. }
  42. $is_addr=0;
  43. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  44. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  45. $returnadr =isset($this->post['returnAddr'])&& !empty($this->post['returnAddr']) ? $this->post['returnAddr']:"";
  46. if($returnadr!=''){
  47. $thnum=array_sum(array_column($returnadr,"return_num"));
  48. $is_addr=1;
  49. }
  50. $returnCode=makeNo("RS");
  51. Db::startTrans();
  52. try{
  53. $in = [
  54. "returnCode"=>$returnCode,
  55. "orderCode"=>$ordeCode,
  56. "good_code"=>$order['good_code'],
  57. "good_name"=>$order['good_name'],
  58. "apply_id"=>$rm,
  59. "apply_name"=>$ri,
  60. "error_code"=>$errorCode,
  61. "num"=>$thnum,
  62. "remark"=>$remark,
  63. "order_type"=>$order['order_type'],
  64. "is_addr"=>$is_addr,
  65. "status"=>$order['is_stock']==1?4:1,
  66. "is_del"=>0,
  67. "addtime"=>date("Y-m-d H:i:s"),
  68. "updatetime"=>date("Y-m-d H:i:s")
  69. ];
  70. $create = Db::name("sale_return")->insert($in,true);
  71. if($create>0){
  72. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  73. ActionLog::logAdd($this->post['token'],$stn,"XSTHD",$in['status'],$in);
  74. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD'];
  75. ProcessOrder::AddProcess($this->post['token'],$process);
  76. if($returnadr!=""){
  77. $inf=[];
  78. foreach ($returnadr as $val){
  79. $temp=[];
  80. $addrinfo =Db::name("order_addr")->where(['id'=>$val['id'],"orderCode"=>$ordeCode])->find();
  81. if($addrinfo==false){
  82. Db::rollback();
  83. return error_show(1005,"地址信息未找到");
  84. }
  85. $send =Db::name('order_out')->where([["addrid","=",$val['id']],["status",">=",2]])->find();
  86. if($send!=false){
  87. Db::rollback();
  88. return error_show(1005,"地址已发货");
  89. }
  90. if($order['is_stock']==1){
  91. if ($addrinfo['receipt_quantity'] < $val['return_num']) {
  92. Db::rollback();
  93. return error_show(1004, "地址发货数量不足");
  94. }
  95. $addrinfo['receipt_quantity'] -= $val['return_num'];
  96. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  97. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  98. $addrup = Db::name("order_addr")->save($addrinfo);
  99. if ($addrup == false) {
  100. Db::rollback();
  101. return error_show(1004, "地址发货数量更新失败");
  102. }
  103. $out = Db::name("order_out")->where(["addrid"=>$val['id']])->find();
  104. if ($out == false) {
  105. Db::rollback();
  106. return error_show(1004, "地址发货单数据未找到");
  107. }
  108. // if ($send!=false) {
  109. if ($out['status'] >= 2) {
  110. Db::rollback();
  111. return error_show(1004, "地址发货单已发货");
  112. }
  113. if ($out['send_num'] < $val['return_num']) {
  114. Db::rollback();
  115. return error_show(1004, "地址发货单发货数量不足");
  116. }
  117. $out['send_num'] -= $val['return_num'];
  118. $out['updatetime'] = date("Y-m-d H:i:s");
  119. $outup = Db::name("order_out")->save($out);
  120. if ($outup == false) {
  121. Db::rollback();
  122. return error_show(1004, "地址发货单更新失败");
  123. }
  124. $ordersend = Db::name("order_send")->where(["outCode" => $out['outCode']])->find();
  125. if ($ordersend['send_num'] < $val['return_num']) {
  126. Db::rollback();
  127. return error_show(1004, "发货单发货数量不足");
  128. }
  129. $ordersend['send_num'] -= $val['return_num'];
  130. $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  131. $ordersend['updatetime'] = date("Y-m-d H:i:s");
  132. $sendip = Db::name("order_send")->save($ordersend);
  133. if ($sendip == false) {
  134. Db::rollback();
  135. return error_show(1004, "发货单更新失败");
  136. }
  137. // }
  138. }
  139. $temp['returnCode']=$returnCode;
  140. $temp['orderCode']=$ordeCode;
  141. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  142. $temp['addrid']=$val['id'];
  143. $temp['send_num']=$addrinfo['receipt_quantity'];
  144. $temp['return_num']=$val['return_num'];
  145. $temp['is_del']=0;
  146. $temp['addtime']=date("Y-m-d H:i:s");
  147. $temp['updatetime']=date("Y-m-d H:i:s");
  148. $inf[]=$temp;
  149. }
  150. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  151. if($inadd==0){
  152. Db::rollback();
  153. return error_show(1005,"退货单新建失败");
  154. }
  155. }
  156. if($order['is_stock']==1){
  157. if ($order['wsend_num'] < $order['num']) {
  158. Db::rollback();
  159. return error_show(1004, "销售单未发货数量不足退货");
  160. }
  161. $lor=$order['status'] ;
  162. $order['wsend_num'] -= $thnum;
  163. $order['send_num'] += $thnum;
  164. $order['status'] = $order['wsend_num']==0?2:($order['send_num']==0?0:1);
  165. $order['send_status'] =$order['wsend_num']==0?3:($order['send_num']==0?1:2);
  166. $order['th_num'] += $thnum;
  167. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  168. $order['updatetime'] = date("Y-m-d H:i:s");
  169. $uap = Db::name("sale")->save($order);
  170. if ($uap == false) {
  171. Db::rollback();
  172. return error_show(1005, '销售单订单更新失败');
  173. }
  174. ActionLog::logAdd($this->post['token'], [
  175. "order_code" => $order["orderCode"],//出库单号
  176. "status" => $lor,//这里的status是之前的值
  177. "action_remark" => '',//备注
  178. "action_type" => "status"//新建create,编辑edit,更改状态status
  179. ], "XSQRD", $order['status'], $order);
  180. ProcessOrder::AddProcess($this->post['token'], [
  181. "order_type" => 'XSQRD',
  182. "order_code" =>$order["orderCode"],//出库单号
  183. "order_id" => $order["id"],
  184. "order_status" =>$order['status']
  185. ]);
  186. $ordernum = Db::name("order_num")->where(['orderCode' => $ordeCode])->find();
  187. if ($ordernum == false) {
  188. Db::rollback();
  189. return error_show(1005, '未找到关联采购单');
  190. }
  191. $ordernum['send_num'] -= $thnum;
  192. $orderup = Db::name("order_num")->save($ordernum);
  193. if ($orderup == false) {
  194. Db::rollback();
  195. return error_show(1005, '关联数据更新失败');
  196. }
  197. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  198. if ($cgd == false) {
  199. Db::rollback();
  200. return error_show(1005, '未找到采购单数据');
  201. }
  202. $cgd['th_fee'] += round($cgd['good_price'] * $thnum, 2);
  203. $cgd['th_num'] += $thnum;
  204. $cgd['updatetime'] = date("Y-m-d H:i:s");
  205. $cgdup = Db::name("purchease_order")->save($cgd);
  206. if ($cgdup == false) {
  207. Db::rollback();
  208. return error_show(1005, '采购单数据更新失败');
  209. }
  210. if ($cgd['bkcode'] != "") {
  211. $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "is_del" => 0])
  212. ->find();
  213. if ($bk == false) {
  214. Db::rollback();
  215. return error_show(1005, '未找到备库单数据');
  216. }
  217. $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  218. if ($orderbk == false) {
  219. Db::rollback();
  220. return error_show(1005, '备库单未完全入库');
  221. }
  222. $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" => 5, "is_del" => 0])
  223. ->field("sum(send_num)-sum(th_num) as num")->find();
  224. $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  225. $orderbk['merge_num'] = $merge_num['num'];
  226. $orderbk['updatetime'] = date("Y-m-d H:i:s");
  227. $orderbkup = Db::name("order_bk")->save($orderbk);
  228. if ($orderbkup == false) {
  229. Db::rollback();
  230. return error_show(1005, '备库单库存数据释放失败');
  231. }
  232. }
  233. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'wsm_code' => $cgd['wsm_code']])
  234. ->find();
  235. if (empty($stock)) {
  236. $stock = [
  237. "spuCode" => $order['good_code'],
  238. "wsm_code" => $cgd['wsm_code'],
  239. "usable_stock" => 0,
  240. "wait_out_stock" => 0,
  241. "wait_in_stock" => 0,
  242. "total_stock" => 0,
  243. "addtime" => date("Y-m-d H:i:s"),
  244. "updatetime" => date("Y-m-d H:i:s"),
  245. ];
  246. }
  247. $stock['usable_stock'] += $thnum;
  248. $stock['wait_out_stock'] -= $thnum;
  249. $stock['updatetime'] = date("Y-m-d H:i:s");
  250. $st_up = Db::name("good_stock")->save($stock);
  251. if ($st_up == false) {
  252. return error_show(1005, '可售商品入库失败');
  253. }
  254. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $thnum, "stock_name" => "usable_stock"];
  255. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID(), "type" => 2, 'stock' => $thnum, "stock_name" => "wait_out_stock"];
  256. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  257. }
  258. Db::commit();
  259. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  260. }
  261. Db::rollback();
  262. return error_show(1005,"退货单新建失败");
  263. }catch (\Exception $e){
  264. Db::rollback();
  265. return error_show(1005,$e->getMessage());
  266. }
  267. }
  268. public function list(){
  269. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  270. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  271. $where =[['is_del',"=",0]];
  272. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  273. if($bkcode!=""){
  274. $where[]=['returnCode',"like", "%{$bkcode}%"];
  275. }
  276. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  277. if($status!==""){
  278. $where[]=['status',"=", $status];
  279. }
  280. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode'])
  281. :"";
  282. if($orderCode!=""){
  283. $where[]=['orderCode',"like", "%{$orderCode}%"];
  284. }
  285. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  286. :"";
  287. if($apply_name!=""){
  288. $where[]=['apply_name',"like", "%{$apply_name}%"];
  289. }
  290. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  291. if($start!==""){
  292. $where[]=['addtime',">=", $start];
  293. }
  294. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  295. if($end!==""){
  296. $where[]=['addtime',"<=", $end];
  297. }
  298. $role=$this->checkRole();
  299. if(!empty($role['write'])){
  300. $where[]=["apply_id","in",$role['write']];
  301. }
  302. $count=Db::name("sale_return")->where($where)->count();
  303. $total = ceil($count/$size);
  304. $page = $total>=$page ? $page :$total;
  305. $list = Db::name("sale_return")->where($where)->order("addtime desc")->page($page,$size)->select();
  306. // echo Db::name("sale_return")->getLastSql();
  307. $data=[];
  308. foreach ($list as $value){
  309. $value['error_msg']='';
  310. if($value['error_code']!=''){
  311. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  312. $value['error_msg']= isset($error['result'])?$error['result']:"";
  313. }
  314. $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  315. $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  316. $value['return_total'] =$value['sale_price']*$value['num'] ;
  317. $value['total_num'] =$order['good_num'] ;
  318. $data[]=$value ;
  319. }
  320. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  321. }
  322. public function info(){
  323. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  324. :"";
  325. if($code==""){
  326. return error_show(1004,"参数returnCode不能为空");
  327. }
  328. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  329. if(empty($info)){
  330. return error_show(1004,"未找到退货数据");
  331. }
  332. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  333. if($orderinfo['order_type']==3|| $orderinfo['order_type']==4){
  334. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  335. }else {
  336. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  337. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  338. }
  339. if ($goon==false) {
  340. return error_show(1003, "未找到商品数据");
  341. }
  342. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  343. $info['is_stock']=isset($goon['is_stock'])?$goon['is_stock']:'0';
  344. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  345. $info['send_type'] = isset($orderinfo['send_type'])?$orderinfo['send_type']:'';
  346. $info['skuCode'] = isset($orderinfo['skuCode'])?$orderinfo['skuCode']:'';
  347. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  348. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  349. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  350. $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0';
  351. $info['return_total'] = $info['sale_price']*$info['num'] ;
  352. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  353. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  354. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  355. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  356. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  357. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  358. $info['customer_name']='';
  359. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  360. $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  361. $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  362. }
  363. $info['supplierNo'] = isset($orderinfo['supplierNo'])?$orderinfo['supplierNo']:'';
  364. $info['supplier_name']='';
  365. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  366. $customerinfo = Db::name("business")->where(['companyNo'=>$orderinfo['supplierNo']])->find();
  367. $info['supplier_name'] = isset($customerinfo['company']) ? $customerinfo['company']:"";
  368. }
  369. $info['platform_name']='';
  370. $info['platform_id']=$orderinfo['platform_id'];
  371. if($orderinfo['platform_id']!=0){
  372. $plat=Db::name("platform")->where(['id'=>$orderinfo['platform_id']])->find();
  373. $info['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:"";
  374. }
  375. $info['cgd_wsend']="";
  376. $info['cgd_send']="";
  377. $info['cgd_total']="";
  378. if($orderinfo['order_type']==2){
  379. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find();
  380. $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0;
  381. $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0;
  382. $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0;
  383. }
  384. $info['error_msg']='';
  385. if($info['error_code']!=''){
  386. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  387. $info['error_msg']= isset($error['result'])?$error['result']:"";
  388. }
  389. if($info['return_wsm']!=""){
  390. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  391. ->where(["a.wsm_code"=>$info['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  392. }
  393. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  394. // $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  395. // $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  396. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  397. $wsm=[];
  398. if(!empty($wsm_return)){
  399. foreach ($wsm_return as $value){
  400. $value['wsm_name']="";
  401. $value['wsm_supplier']="";
  402. $value['wsm_supplierNo']="";
  403. if($value['wsm_code']!=""){
  404. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  405. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  406. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  407. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  408. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  409. }
  410. $orderwsm = Db::name("sale_info")->where(["orderCode"=>$info["orderCode"],"wsm_code"=>$value["wsm_code"]])->find();
  411. $value["wsm_total"] = isset($orderwsm["num"]) ? $orderwsm["num"]:0;
  412. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $info['orderCode']])->sum("send_num");
  413. $value['wsm_send'] = $send ?? 0;
  414. $value['wsm_wsend'] = $value['wsm_total'] - $value['wsm_send'];
  415. $wsm[]=$value;
  416. }
  417. }
  418. $info['wsminfo']=$wsm;
  419. $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  420. $addrinfo=[];
  421. if(!empty($addr)){
  422. foreach ( $addr as $value){
  423. $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  424. $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:"";
  425. $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:"";
  426. $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:"";
  427. $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:"";
  428. $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:"";
  429. $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:"";
  430. $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ;
  431. $value['receipt_quantity']=isset($addrlist['receipt_quantity']) ?$addrlist['receipt_quantity']:"" ;
  432. $send = Db::name("order_out")->where(['addrid' => $addrlist['id'], 'orderCode' => $addrlist['orderCode']])->sum("send_num");
  433. $value['addr_send'] = $send ?? 0;
  434. $value['addr_wsend'] = $value['receipt_quantity'] - $value['addr_send'];
  435. $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  436. $value['customer_name'] = isset($customer['companyName']) ? $customer['companyName']:"";
  437. $addrinfo[]=$value;
  438. }
  439. }
  440. $info['addrinfo']=$addrinfo;
  441. $info['can']=$int;
  442. return app_show(0,"获取成功",$info);
  443. }
  444. /**
  445. * @return \think\response\Json|void
  446. * @throws \think\db\exception\DataNotFoundException
  447. * @throws \think\db\exception\DbException
  448. * @throws \think\db\exception\ModelNotFoundException
  449. */
  450. public function delete(){
  451. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  452. :"";
  453. if($code==""){
  454. return error_show(1004,"参数returnCode不能为空");
  455. }
  456. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  457. if(empty($info)){
  458. return error_show(1004,"未找到退货数据");
  459. }
  460. $info["is_del"]=1;
  461. $info["updatetime"]=date("Y-m-d H:i:s");
  462. $del = Db::name("sale_return")->save($info);
  463. if($del){
  464. $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  465. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",0,$ste);
  466. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>"XSTHD"];
  467. ProcessOrder::workdel($process);
  468. return app_show(0,"删除成功");
  469. }else{
  470. return error_show(1004,"删除失败");
  471. }
  472. }
  473. /**审核
  474. * @return \think\response\Json|void
  475. * @throws \think\db\exception\DataNotFoundException
  476. * @throws \think\db\exception\DbException
  477. * @throws \think\db\exception\ModelNotFoundException
  478. */
  479. public function exam(){
  480. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  481. :"";
  482. if($code==""){
  483. return error_show(1004,"参数returnCode不能为空");
  484. }
  485. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  486. if(empty($info)){
  487. return error_show(1004,"未找到退货数据");
  488. }
  489. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
  490. :"";
  491. if($status===""){
  492. return error_show(1004,"参数status不能为空");
  493. }
  494. $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  495. $var = $info['status'];
  496. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  497. $apply_id =GetUserInfo($token);
  498. if(empty($apply_id)||$apply_id['code']!=0){
  499. return error_show(1002,"申请人数据不存在");
  500. }
  501. $rid= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  502. $rname= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  503. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  504. if($orderinfo==false){
  505. return error_show(1004,"未找到订单数据");
  506. }
  507. if($status==4){
  508. if($info['is_addr']==1){
  509. $addr=Db::name("sale_returnaddr")->where(['returnCode'=>$info['returnCode'],"is_del"=>0])->select()
  510. ->toArray();
  511. if(empty($addr)){
  512. return error_show(1004,"未找到发货单地址数据");
  513. }
  514. }
  515. }
  516. if($status==3){
  517. $is_th =isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  518. if($is_th===""){
  519. return error_show(1004,"参数is_th不能为空");
  520. }
  521. $return_wsm =isset($this->post['return_wsm'])&&$this->post['return_wsm']!=="" ? trim($this->post['return_wsm']):"";
  522. if($return_wsm===""){
  523. return error_show(1004,"参数return_wsm 不能为空");
  524. }
  525. $wsmcode = Db::name("warehouse_info")->where(['wsm_code'=>$return_wsm])->find();
  526. if($wsmcode==false){
  527. return error_show(1004,"为找到仓库数据");
  528. }
  529. $info['return_wsm'] =$return_wsm ;
  530. $info['is_th'] =$is_th ;
  531. }
  532. Db::startTrans();
  533. try{
  534. $temp= $info['status'];
  535. $info['status'] =$status;
  536. $remark!=""? $info['remark'] =$remark:"";
  537. $info["updatetime"]=date("Y-m-d H:i:s");
  538. $up = Db::name("sale_return")->save($info);
  539. if($up) {
  540. $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "XSTHD"];
  541. ProcessOrder::AddProcess($this->post['token'], $process);
  542. $ste = ["order_code" => $code, "status" => $temp, "action_remark" => '', "action_type" => "status"];
  543. ActionLog::logAdd($this->post['token'], $ste, "XSTHD", $status, $info);
  544. if ($status == 4) {
  545. if ($info['is_addr'] == 1) {
  546. if (isset($addr) && !empty($addr)) {
  547. foreach ($addr as $value) {
  548. $addrinfo = Db::name("order_addr")->where(['id' => $value['addrid'], "is_del" => 0])->find();
  549. if ($addrinfo == false) {
  550. Db::rollback();
  551. return error_show(1004, "地址数据未找到");
  552. }
  553. if ($addrinfo['receipt_quantity'] < $value['return_num']) {
  554. Db::rollback();
  555. return error_show(1004, "地址发货数量不足");
  556. }
  557. $addrinfo['receipt_quantity'] -= $value['return_num'];
  558. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  559. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  560. $addrup = Db::name("order_addr")->save($addrinfo);
  561. if ($addrup == false) {
  562. Db::rollback();
  563. return error_show(1004, "地址发货数量更新失败");
  564. }
  565. if ($value['outCode'] != "") {
  566. $out = Db::name("order_out")->where(["outCode" => $value['outCode'], "is_del" => 0])
  567. ->find();
  568. if ($out == false) {
  569. Db::rollback();
  570. return error_show(1004, "地址发货单数据未找到");
  571. }
  572. if ($out['status'] >= 2) {
  573. Db::rollback();
  574. return error_show(1004, "地址发货单已发货");
  575. }
  576. if ($out['send_num'] < $value['return_num']) {
  577. Db::rollback();
  578. return error_show(1004, "地址发货单发货数量不足");
  579. }
  580. $out['send_num'] -= $value['return_num'];
  581. $out['is_del'] = $out['send_num'] <= 0 ? 1 : 0;
  582. $out['updatetime'] = date("Y-m-d H:i:s");
  583. $outup = Db::name("order_out")->save($out);
  584. if ($outup == false) {
  585. Db::rollback();
  586. return error_show(1004, "地址发货单更新失败");
  587. }
  588. $ordersend = Db::name("order_send")->where(["outCode" => $value['outCode']])->find();
  589. if ($ordersend['send_num'] < $value['return_num']) {
  590. Db::rollback();
  591. return error_show(1004, "发货单发货数量不足");
  592. }
  593. $ordersend['send_num'] -= $value['return_num'];
  594. $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  595. $ordersend['updatetime'] = date("Y-m-d H:i:s");
  596. $sendip = Db::name("order_send")->save($ordersend);
  597. if ($sendip == false) {
  598. Db::rollback();
  599. return error_show(1004, "发货单更新失败");
  600. }
  601. }
  602. }
  603. }
  604. }
  605. if ($orderinfo['wsend_num'] < $info['num']) {
  606. Db::rollback();
  607. return error_show(1004, "销售单未发货数量不足退货");
  608. }
  609. $lor=$orderinfo['status'];
  610. $orderinfo['wsend_num'] -= $info['num'];
  611. $orderinfo['send_num'] += $info['num'];
  612. $orderinfo['status'] = $orderinfo['wsend_num']==0?2:($orderinfo['send_num']==0?0:1);
  613. $orderinfo['send_status'] =$orderinfo['wsend_num']==0?3:($orderinfo['send_num']==0?1:2);
  614. if ($orderinfo['is_stock'] == 1) {
  615. $orderinfo['th_num'] += $info['num'];
  616. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  617. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  618. $uap = Db::name("sale")->save($orderinfo);
  619. if ($uap == false) {
  620. Db::rollback();
  621. return error_show(1005, '销售单订单更新失败');
  622. }
  623. $ordernum = Db::name("order_num")->where(['orderCode' => $info['orderCode']])->find();
  624. if ($ordernum == false) {
  625. Db::rollback();
  626. return error_show(1005, '未找到关联采购单');
  627. }
  628. $ordernum['send_num'] -= $info['num'];
  629. $orderup = Db::name("order_num")->save($ordernum);
  630. if ($orderup == false) {
  631. Db::rollback();
  632. return error_show(1005, '关联数据更新失败');
  633. }
  634. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  635. if ($cgd == false) {
  636. Db::rollback();
  637. return error_show(1005, '未找到采购单数据');
  638. }
  639. $cgd['th_fee'] += round($cgd['good_price'] * $info['num'], 2);
  640. $cgd['th_num'] += $info['num'];
  641. $cgd['updatetime'] = date("Y-m-d H:i:s");
  642. $cgdup = Db::name("purchease_order")->save($cgd);
  643. if ($cgdup == false) {
  644. Db::rollback();
  645. return error_show(1005, '采购单数据更新失败');
  646. }
  647. if ($cgd['bkcode'] != "") {
  648. $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "is_del" => 0])
  649. ->find();
  650. if ($bk == false) {
  651. Db::rollback();
  652. return error_show(1005, '未找到备库单数据');
  653. }
  654. $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  655. if ($orderbk == false) {
  656. Db::rollback();
  657. return error_show(1005, '备库单未完全入库');
  658. }
  659. $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" => 5, "is_del" => 0])
  660. ->field("sum(send_num)-sum(th_num) as num")->find();
  661. $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  662. $orderbk['merge_num'] = $merge_num['num'];
  663. $orderbk['updatetime'] = date("Y-m-d H:i:s");
  664. $orderbkup = Db::name("order_bk")->save($orderbk);
  665. if ($orderbkup == false) {
  666. Db::rollback();
  667. return error_show(1005, '备库单库存数据释放失败');
  668. }
  669. }
  670. } else {
  671. if ($info['is_th'] == 1) {
  672. $orderinfo['th_num'] += $info['num'];
  673. $orderinfo['th_fee'] += round($info['return_num'] * $orderinfo['sale_price'], 2);
  674. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  675. $uap = Db::name("sale")->save($orderinfo);
  676. if ($uap == false) {
  677. Db::rollback();
  678. return error_show(1005, '销售单订单更新失败');
  679. }
  680. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  681. if ($ordernum == false) {
  682. Db::rollback();
  683. return error_show(1005, '未找到关联采购单');
  684. }
  685. $ordernum['send_num'] -= $info['num'];
  686. $orderup = Db::name("order_num")->save($ordernum);
  687. if ($orderup == false) {
  688. Db::rollback();
  689. return error_show(1005, '关联数据更新失败');
  690. }
  691. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  692. if ($cgd == false) {
  693. Db::rollback();
  694. return error_show(1005, '未找到采购单数据');
  695. }
  696. $cgd['th_num'] += $info['num'];
  697. $cgd['th_fee'] += round($info['num'] * $cgd['good_price'], 2);
  698. $cgd['updatetime'] = date("Y-m-d H:i:s");
  699. $cgdup = Db::name("purchease_order")->save($cgd);
  700. if ($cgdup == false) {
  701. Db::rollback();
  702. return error_show(1005, '采购单数据更新失败');
  703. }
  704. } else {
  705. $orderinfo['th_num'] += $info['num'];
  706. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  707. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  708. $uap = Db::name("sale")->save($orderinfo);
  709. if ($uap == false) {
  710. Db::rollback();
  711. return error_show(1005, '销售单订单更新失败');
  712. }
  713. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  714. if ($ordernum == false) {
  715. Db::rollback();
  716. return error_show(1005, '未找到关联采购单');
  717. }
  718. $ordernum['send_num'] -= $info['num'];
  719. $ordernum['wsend_num'] = $info['return_num'];
  720. $orderup = Db::name("order_num")->save($ordernum);
  721. if ($orderup == false) {
  722. Db::rollback();
  723. return error_show(1005, '关联数据更新失败');
  724. }
  725. }
  726. }
  727. ActionLog::logAdd($this->post['token'], [
  728. "order_code" => $orderinfo["orderCode"],//出库单号
  729. "status" => $lor,//这里的status是之前的值
  730. "action_remark" => '',//备注
  731. "action_type" => "status"//新建create,编辑edit,更改状态status
  732. ], "XSQRD", $orderinfo['status'], $orderinfo);
  733. ProcessOrder::AddProcess($this->post['token'], [
  734. "order_type" => 'XSQRD',
  735. "order_code" =>$orderinfo["orderCode"],//出库单号
  736. "order_id" => $orderinfo["id"],
  737. "order_status" =>$orderinfo['status']
  738. ]);
  739. $wsmcode = $orderinfo['is_stock'] == 1 ? $cgd['wsm_code'] : $info['return_wsm'];
  740. if ($wsmcode == "") {
  741. Db::rollback();
  742. return error_show(1005, '未找到退货仓库');
  743. }
  744. //::todo 非库存品订单退货 采购单退货 虚拟仓如何减库存
  745. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' => $wsmcode])->find();
  746. if (empty($stock)) {
  747. $stock = [
  748. "spuCode" => $info['good_code'],
  749. "wsm_code" => $wsmcode,
  750. "usable_stock" => 0,
  751. "wait_out_stock" => 0,
  752. "wait_in_stock" => 0,
  753. "total_stock" => 0,
  754. "addtime" => date("Y-m-d H:i:s"),
  755. "updatetime" => date("Y-m-d H:i:s"),
  756. ];
  757. }
  758. $stock['usable_stock'] += $info['num'];
  759. $stock['updatetime'] = date("Y-m-d H:i:s");
  760. $st_up = Db::name("good_stock")->save($stock);
  761. if ($st_up == false) {
  762. return error_show(1005, '可售商品入库失败');
  763. }
  764. $good_data = ['good_log_code' => $info['returnCode'], "stock_id" => isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1, 'stock' => $info['num'], "stock_name" => "usable_stock"];
  765. GoodLog::LogAdd($this->post['token'], $good_data, 'QRTHD');
  766. }
  767. }
  768. Db::commit();
  769. return app_show(0,"更新成功");
  770. }catch (\Exception $e){
  771. Db::rollback();
  772. return error_show(1004,"更新失败");
  773. }
  774. }
  775. public function zxcreate(){
  776. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  777. if($ordeCode==''){
  778. return error_show(1004,"参数orderCode 不能为空");
  779. }
  780. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  781. if(empty($order)){
  782. return error_show(1005,"未找到订单数据");
  783. }
  784. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  785. if($errorCode==''){
  786. return error_show(1004,"参数errorCode 不能为空");
  787. }
  788. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  789. if(empty($error)){
  790. return error_show(1005,"未找到退货原因数据");
  791. }
  792. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  793. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  794. if($token==''){
  795. return error_show(105,"参数token不能为空");
  796. }
  797. $user =GetUserInfo($token);
  798. if(empty($user)||$user['code']!=0){
  799. return error_show(1002,"申请人数据不存在");
  800. }
  801. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  802. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  803. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  804. if($num==''){
  805. return error_show(1005,"参数num不能为空");
  806. }
  807. if($order['wsend_num']<$num){
  808. return error_show(1002,"仓库未发货数量不足退货");
  809. }
  810. $returnCode=makeNo("RS");
  811. Db::startTrans();
  812. try{
  813. $in = [
  814. "returnCode"=>$returnCode,
  815. "orderCode"=>$ordeCode,
  816. "good_code"=>$order['good_code'],
  817. "good_name"=>$order['good_name'],
  818. "apply_id"=>$rm,
  819. "apply_name"=>$ri,
  820. "error_code"=>$errorCode,
  821. "num"=>$num,
  822. "remark"=>$remark,
  823. "order_type"=>2,
  824. "status"=>0,
  825. "is_del"=>0,
  826. "addtime"=>date("Y-m-d H:i:s"),
  827. "updatetime"=>date("Y-m-d H:i:s")
  828. ];
  829. $create = Db::name("sale_return")->insert($in,true);
  830. if($create>0) {
  831. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'XSTHD'];
  832. ProcessOrder::AddProcess($this->post['token'],$process);
  833. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  834. "action_type"=>"create"];
  835. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",3,$in);
  836. if ($order['send_type'] == 1) {
  837. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"
  838. =>2])->select();
  839. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  840. if(!empty($wsend)){
  841. foreach ($wsend as $value){
  842. $tt = $value['status'];
  843. $value['status']=0;
  844. $value['updatetime']=date("Y-m-d H:i:s");
  845. $up =Db::name("order_out")->save($value);
  846. if($up){
  847. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD'];
  848. ProcessOrder::AddProcess($this->post['token'],$process);
  849. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  850. ActionLog::logAdd($this->post['token'],$ste,"CKD",0,$value);
  851. }else{
  852. Db::rollback();
  853. return error_show(1005,"退货单新建失败");
  854. }
  855. }
  856. }
  857. }
  858. Db::commit();
  859. return error_show(0,"退货单新建成功");
  860. }
  861. Db::rollback();
  862. return error_show(1005,"退货单新建失败");
  863. }catch (\Exception $e){
  864. Db::rollback();
  865. return error_show(1005,$e->getMessage());
  866. }
  867. }
  868. }