Reorder.php 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\DataGroup as DataGroupModel;
  4. use app\admin\model\GoodLog;
  5. use app\admin\model\ProcessOrder;
  6. use think\facade\Db;
  7. use think\App;
  8. use app\admin\model\ActionLog;
  9. //销售单退货
  10. class Reorder extends Base
  11. {
  12. public function __construct(App $app)
  13. {
  14. parent::__construct($app);
  15. }
  16. public function create(){
  17. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  18. if($ordeCode==''){
  19. return error_show(1004,"参数orderCode 不能为空");
  20. }
  21. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  22. if(empty($order)){
  23. return error_show(1005,"未找到订单数据");
  24. }
  25. $retrun =Db::name("sale_return")->where([["orderCode","=",$ordeCode],["is_del","=",0],["status","<",4]])
  26. ->count();
  27. if($retrun>0){
  28. return error_show(1005,"存在未完成退货订单数据");
  29. }
  30. if($order['order_type']==3){
  31. $goon = Db::name("good_zixun")->where(["spuCode"=>$order['good_code'],"is_del"=>0])->find();
  32. }else {
  33. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  34. ->where(['a.skuCode' => $order['skuCode']])->field("b.creater,b.createrid,b.supplierNo")->find();
  35. }
  36. if($goon==false){
  37. return error_show(1005,"未找到商品数据");
  38. }
  39. $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  40. if($supplier==false){
  41. return error_show(1005,"未找到商品供应商数据");
  42. }
  43. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  44. if($errorCode==''){
  45. return error_show(1004,"参数errorCode 不能为空");
  46. }
  47. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  48. if(empty($error)){
  49. return error_show(1005,"未找到退货原因数据");
  50. }
  51. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  52. $thnum =isset($this->post['thnum']) &&$this->post['thnum']!=''?intval($this->post['thnum']) :"";
  53. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  54. if($token==''){
  55. return error_show(105,"参数token不能为空");
  56. }
  57. $user =GetUserInfo($token);
  58. if(empty($user)||$user['code']!=0){
  59. return error_show(1002,"申请人数据不存在");
  60. }
  61. $is_addr=0;
  62. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  63. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  64. $returnadr =isset($this->post['returnAddr'])&& !empty($this->post['returnAddr']) ? $this->post['returnAddr']:"";
  65. if($returnadr!=''){
  66. $thnum=array_sum(array_column($returnadr,"return_num"));
  67. $is_addr=1;
  68. }
  69. $returnCode=makeNo("RN");
  70. Db::startTrans();
  71. try{
  72. $in = [
  73. "returnCode"=>$returnCode,
  74. "orderCode"=>$ordeCode,
  75. "good_code"=>$order['good_code'],
  76. "good_name"=>$order['good_name'],
  77. "apply_id"=>$rm,
  78. "apply_name"=>$ri,
  79. "cgderid"=>$goon['createrid'],
  80. "cgder"=>$goon['creater'],
  81. "error_code"=>$errorCode,
  82. "num"=>$thnum,
  83. "total_fee"=>round($order['sale_price']*$thnum,2),
  84. "good_price"=>$order['sale_price'],
  85. "platform_id"=>$order['platform_id'],
  86. "remark"=>$remark,
  87. "order_type"=>$order['order_type'],
  88. "is_addr"=>$is_addr,
  89. "status"=>$order['is_stock']==1?4:1,
  90. "is_del"=>0,
  91. "addtime"=>date("Y-m-d H:i:s"),
  92. "updatetime"=>date("Y-m-d H:i:s")
  93. ];
  94. $create = Db::name("sale_return")->insert($in,true);
  95. if($create>0){
  96. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  97. ActionLog::logAdd($this->post['token'],$stn,"XSTHD",$in['status'],$in);
  98. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0];
  99. ProcessOrder::AddProcess($this->post['token'],$process);
  100. //维护台账信息
  101. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$ordeCode}'");
  102. // $rs = Db::name('standing_book')->where('orderCode', $ordeCode)->order('returnGoodCode')->find();
  103. // if (!empty($rs)) {
  104. // if ($rs['returnGoodCode'] == '') Db::name('standing_book')->where('id', $rs['id'])->update(["returnGoodCode" => $returnCode, "updatetime" => date("Y-m-d H:i:s")]);
  105. // else {
  106. // unset($rs['id']);
  107. // $rs['standBookNo'] = makeNo('IO');
  108. // $rs['addtime'] = $rs['updatetime'] = date("Y-m-d H:i:s");
  109. // $rs['returnGoodCode'] = $returnCode;
  110. // Db::name('standing_book')->insert($rs);
  111. // }
  112. // }
  113. if($returnadr!=""){
  114. $inf=[];
  115. foreach ($returnadr as $val){
  116. if ($val['return_num'] == 0) continue;//当退货数量为0时,跳过
  117. $temp=[];
  118. $addrinfo =Db::name("order_addr")->where(['id'=>$val['id'],"orderCode"=>$ordeCode])->find();
  119. if($addrinfo==false){
  120. Db::rollback();
  121. return error_show(1005,"地址信息未找到");
  122. }
  123. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  124. if($send['status']>=2){
  125. Db::rollback();
  126. return error_show(1005,"地址已发货");
  127. }
  128. if($order['is_stock']==1){
  129. if ($addrinfo['receipt_quantity'] < $val['return_num']) {
  130. Db::rollback();
  131. return error_show(1004, "地址发货数量不足");
  132. }
  133. $addrinfo['receipt_quantity'] -= $val['return_num'];
  134. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  135. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  136. $addrup = Db::name("order_addr")->save($addrinfo);
  137. if ($addrup == false) {
  138. Db::rollback();
  139. return error_show(1004, "地址发货数量更新失败");
  140. }
  141. $out = Db::name("order_out")->where(["addrid"=>$val['id']])->find();
  142. if ($out == false) {
  143. Db::rollback();
  144. return error_show(1004, "地址发货单数据未找到");
  145. }
  146. // if ($send!=false) {
  147. if ($out['status'] >= 2) {
  148. Db::rollback();
  149. return error_show(1004, "地址发货单已发货");
  150. }
  151. if ($out['send_num'] < $val['return_num']) {
  152. Db::rollback();
  153. return error_show(1004, "地址发货单发货数量不足");
  154. }
  155. $out['send_num'] -= $val['return_num'];
  156. $out['is_del'] = $out['send_num']==0?1:0;
  157. $out['updatetime'] = date("Y-m-d H:i:s");
  158. $outup = Db::name("order_out")->save($out);
  159. if ($outup == false) {
  160. Db::rollback();
  161. return error_show(1004, "地址发货单更新失败");
  162. }
  163. $ordersend = Db::name("order_send")->where(["outCode" => $out['outCode']])->find();
  164. if ($ordersend['send_num'] < $val['return_num']) {
  165. Db::rollback();
  166. return error_show(1004, "发货单发货数量不足");
  167. }
  168. $ordersend['send_num'] -= $val['return_num'];
  169. $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  170. $ordersend['updatetime'] = date("Y-m-d H:i:s");
  171. $sendip = Db::name("order_send")->save($ordersend);
  172. if ($sendip == false) {
  173. Db::rollback();
  174. return error_show(1004, "发货单更新失败");
  175. }
  176. // }
  177. }
  178. $temp['returnCode']=$returnCode;
  179. $temp['orderCode']=$ordeCode;
  180. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  181. $temp['addrid']=$val['id'];
  182. $temp['send_num']=$addrinfo['receipt_quantity'];
  183. $temp['return_num']=$val['return_num'];
  184. $temp['is_del']=0;
  185. $temp['addtime']=date("Y-m-d H:i:s");
  186. $temp['updatetime']=date("Y-m-d H:i:s");
  187. $inf[]=$temp;
  188. }
  189. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  190. if($inadd==0){
  191. Db::rollback();
  192. return error_show(1005,"退货单新建失败");
  193. }
  194. }
  195. if($order['is_stock']==1){
  196. if ($order['wsend_num'] < $thnum) {
  197. Db::rollback();
  198. return error_show(1004, "销售单未发货数量不足退货");
  199. }
  200. $lor=$order['status'] ;
  201. $order['wsend_num'] -= $thnum;
  202. $order['send_num'] += $thnum;
  203. $order['status'] = $order['wsend_num']==0?2:($order['send_num']==0?0:1);
  204. $order['send_status'] =$order['wsend_num']==0?3:($order['send_num']==0?1:2);
  205. $order['th_num'] += $thnum;
  206. if($order['th_num']==$order['send_num']&& $order['wsend_num']==0){
  207. $order['status']=3;
  208. }
  209. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  210. $order['updatetime'] = date("Y-m-d H:i:s");
  211. $uap = Db::name("sale")->save($order);
  212. if ($uap == false) {
  213. Db::rollback();
  214. return error_show(1005, '销售单订单更新失败');
  215. }
  216. ActionLog::logAdd($this->post['token'], [
  217. "order_code" => $order["orderCode"],//出库单号
  218. "status" => $lor,//这里的status是之前的值
  219. "action_remark" => '',//备注
  220. "action_type" => "status"//新建create,编辑edit,更改状态status
  221. ], "XSQRD", $order['status'], $order);
  222. ProcessOrder::AddProcess($this->post['token'], [
  223. "order_type" => 'XSQRD',
  224. "order_code" =>$order["orderCode"],//出库单号
  225. "order_id" => $order["id"],
  226. "order_status" =>$order['status'],"before_status"=>$lor
  227. ]);
  228. $ordernum = Db::name("order_num")->where(['orderCode' => $ordeCode])->find();
  229. if ($ordernum == false) {
  230. Db::rollback();
  231. return error_show(1005, '未找到关联采购单');
  232. }
  233. $ordernum['send_num'] -= $thnum;
  234. $orderup = Db::name("order_num")->save($ordernum);
  235. if ($orderup == false) {
  236. Db::rollback();
  237. return error_show(1005, '关联数据更新失败');
  238. }
  239. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  240. if ($cgd == false) {
  241. Db::rollback();
  242. return error_show(1005, '未找到采购单数据');
  243. }
  244. $cgd['th_fee'] += round($cgd['good_price'] * $thnum, 2);
  245. $cgd['th_num'] += $thnum;
  246. $cgd['updatetime'] = date("Y-m-d H:i:s");
  247. $cgdup = Db::name("purchease_order")->save($cgd);
  248. if ($cgdup == false) {
  249. Db::rollback();
  250. return error_show(1005, '采购单数据更新失败');
  251. }
  252. if ($cgd['bkcode'] != "") {
  253. $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "order_source" =>0, "is_del" => 0])
  254. ->find();
  255. if ($bk == false) {
  256. Db::rollback();
  257. return error_show(1005, '未找到备库单数据');
  258. }
  259. $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  260. if ($orderbk == false) {
  261. Db::rollback();
  262. return error_show(1005, '备库单未完全入库');
  263. }
  264. $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" =>
  265. 1, "is_del" => 0])->where("order_source","<>",0)->field("sum(send_num)-sum(th_num) as num")
  266. ->find();
  267. $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  268. $orderbk['merge_num'] = $merge_num['num'];
  269. $orderbk['updatetime'] = date("Y-m-d H:i:s");
  270. $orderbkup = Db::name("order_bk")->save($orderbk);
  271. if ($orderbkup == false) {
  272. Db::rollback();
  273. return error_show(1005, '备库单库存数据释放失败');
  274. }
  275. }
  276. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'wsm_code' => $cgd['wsm_code']])
  277. ->find();
  278. if (empty($stock)) {
  279. $stock = [
  280. "spuCode" => $order['good_code'],
  281. "wsm_code" => $cgd['wsm_code'],
  282. "usable_stock" => 0,
  283. "wait_out_stock" => 0,
  284. "wait_in_stock" => 0,
  285. "total_stock" => 0,
  286. "addtime" => date("Y-m-d H:i:s"),
  287. "updatetime" => date("Y-m-d H:i:s"),
  288. ];
  289. }
  290. // if($stock['presale_stock']>0){
  291. // if($stock['presale_stock']>=$thnum){
  292. // $stock['presale_stock']-=$thnum;
  293. // }else{
  294. // $stock['presale_stock']=0;
  295. // $stock['usable_stock']+=$thnum;
  296. // $stock['wait_out_stock'] -= ($thnum -$stock['presale_stock']);
  297. // }
  298. // }else{
  299. $stock['usable_stock']+=$thnum;
  300. $stock['wait_out_stock'] -= $thnum;
  301. // }
  302. $stock['updatetime'] = date("Y-m-d H:i:s");
  303. $st_up = Db::name("good_stock")->save($stock);
  304. if ($st_up == false) {
  305. return error_show(1005, '可售商品入库失败');
  306. }
  307. $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"];
  308. $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"];
  309. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  310. $data=[
  311. "orderCode"=>$ordeCode,
  312. "th_type"=>1,
  313. "th_num"=>$thnum,
  314. "th_fee"=>round($order['sale_price']*$thnum,2),
  315. "thCode"=>$returnCode,
  316. "spuCode"=>$order['good_code'],
  317. "good_name"=>$order['good_name'],
  318. "cat_id"=>$order['cat_id'],
  319. "apply_id"=>$rm,
  320. "apply_name"=>$ri,
  321. "addtime"=>date("Y-m-d H:i:s"),
  322. "status"=>1,
  323. "is_del"=>0
  324. ];
  325. $inse=Db::name("th_data")->insert($data);
  326. if($inse==false){
  327. Db::rollback();
  328. return error_show(1004,"退货单更新失败");
  329. }
  330. }
  331. Db::commit();
  332. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  333. }
  334. Db::rollback();
  335. return error_show(1005,"退货单新建失败");
  336. }catch (\Exception $e){
  337. Db::rollback();
  338. return error_show(1005,$e->getMessage());
  339. }
  340. }
  341. public function list(){
  342. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  343. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  344. $where =[['sr.is_del',"=",0]];
  345. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  346. if($bkcode!=""){
  347. $where[]=['sr.returnCode',"like", "%{$bkcode}%"];
  348. }
  349. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  350. if($status!==""){
  351. $where[]=['sr.status',"=", $status];
  352. }
  353. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode']) :"";
  354. if($orderCode!=""){
  355. $where[]=['sr.orderCode',"like", "%{$orderCode}%"];
  356. }
  357. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name']) :"";
  358. if($apply_name!=""){
  359. $where[]=['sr.apply_name',"like", "%{$apply_name}%"];
  360. }
  361. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  362. if($start!==""){
  363. $where[]=['sr.addtime',">=", $start.' 00:00:00'];
  364. }
  365. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  366. if($end!==""){
  367. $where[]=['sr.addtime',"<=", $end.' 23:59:59'];
  368. }
  369. $condition='';
  370. // $role=$this->checkRole();
  371. // if(!empty($role['write']) && $this->uid!=""){
  372. // // $where[]=["sr.apply_id","in",$role['write']];
  373. // $condition .="sr.cgderid = {$this->uid} or sr.apply_id in (".implode(',',$role['write']).")";
  374. // }
  375. $role = $this->checkDataShare();
  376. if (!empty($role[DataGroupModel::$type_全部])) $condition .= "sr.cgderid = {$this->uid} or sr.apply_id in (" . implode(',', $role[DataGroupModel::$type_全部]) . ")";
  377. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  378. if ($company_name !== "") $where[] = ["sr.apply_id", 'in', get_company_item_user_by_name($company_name)];
  379. $count=Db::name("sale_return")->alias('sr')->where($where)->where($condition)->count();
  380. $total = ceil($count/$size);
  381. $page = $total>=$page ? $page :$total;
  382. $list = Db::name("sale_return")
  383. ->alias('sr')
  384. ->field('sr.*,u.itemid')
  385. ->leftJoin("depart_user u", "u.uid=sr.apply_id AND u.is_del=0")
  386. ->where($where)
  387. ->where($condition)
  388. ->order("addtime desc")
  389. ->page($page,$size)
  390. ->cursor();
  391. // echo Db::name("sale_return")->getLastSql();
  392. $data=[];
  393. foreach ($list as $value){
  394. $value['error_msg']='';
  395. if($value['error_code']!=''){
  396. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  397. $value['error_msg']= isset($error['result'])?$error['result']:"";
  398. }
  399. $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  400. $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  401. $value['return_total'] =$value['sale_price']*$value['num'] ;
  402. $value['total_num'] =$order['good_num'] ;
  403. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  404. //是否具有编辑权限
  405. $value['is_allow_update'] = 0;
  406. if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  407. $data[]=$value ;
  408. }
  409. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  410. }
  411. public function info(){
  412. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  413. :"";
  414. if($code==""){
  415. return error_show(1004,"参数returnCode不能为空");
  416. }
  417. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  418. if(empty($info)){
  419. return error_show(1004,"未找到退货数据");
  420. }
  421. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  422. if($orderinfo['order_type']==3){
  423. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  424. }else {
  425. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  426. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  427. }
  428. if ($goon==false) {
  429. return error_show(1003, "未找到商品数据");
  430. }
  431. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  432. $info['is_stock']=isset($goon['is_stock'])?$goon['is_stock']:'0';
  433. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  434. $info['send_type'] = isset($orderinfo['send_type'])?$orderinfo['send_type']:'';
  435. $info['skuCode'] = isset($orderinfo['skuCode'])?$orderinfo['skuCode']:'';
  436. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  437. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  438. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  439. $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0';
  440. $info['return_total'] = $info['sale_price']*$info['num'] ;
  441. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  442. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  443. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  444. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  445. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  446. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  447. $info['customer_name']='';
  448. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  449. $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  450. $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  451. }
  452. $info['supplierNo'] = isset($orderinfo['supplierNo'])?$orderinfo['supplierNo']:'';
  453. $info['supplier_name']='';
  454. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  455. $customerinfo = Db::name("business")->where(['companyNo'=>$orderinfo['supplierNo']])->find();
  456. $info['supplier_name'] = isset($customerinfo['company']) ? $customerinfo['company']:"";
  457. }
  458. $info['platform_name']='';
  459. $info['platform_id']=$orderinfo['platform_id'];
  460. if($orderinfo['platform_id']!=0){
  461. $plat=Db::name("platform")->where(['id'=>$orderinfo['platform_id']])->find();
  462. $info['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:"";
  463. }
  464. $info['cgd_wsend']="";
  465. $info['cgd_send']="";
  466. $info['cgd_total']="";
  467. if($orderinfo['order_type']==2){
  468. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find();
  469. $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0;
  470. $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0;
  471. $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0;
  472. }
  473. $info['error_msg']='';
  474. if($info['error_code']!=''){
  475. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  476. $info['error_msg']= isset($error['result'])?$error['result']:"";
  477. }
  478. if($info['return_wsm']!=""){
  479. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  480. ->where(["a.wsm_code"=>$info['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  481. }
  482. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  483. // $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  484. // $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  485. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  486. $wsm=[];
  487. if(!empty($wsm_return)){
  488. foreach ($wsm_return as $value){
  489. $value['wsm_name']="";
  490. $value['wsm_supplier']="";
  491. $value['wsm_supplierNo']="";
  492. if($value['wsm_code']!=""){
  493. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  494. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  495. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  496. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  497. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  498. }
  499. $orderwsm = Db::name("sale_info")->where(["orderCode"=>$info["orderCode"],"wsm_code"=>$value["wsm_code"]])->find();
  500. $value["wsm_total"] = isset($orderwsm["num"]) ? $orderwsm["num"]:0;
  501. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $info['orderCode']])->sum("send_num");
  502. $value['wsm_send'] = $send ?? 0;
  503. $value['wsm_wsend'] = $value['wsm_total'] - $value['wsm_send'];
  504. $wsm[]=$value;
  505. }
  506. }
  507. $info['wsminfo']=$wsm;
  508. $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  509. $addrinfo=[];
  510. if(!empty($addr)){
  511. foreach ( $addr as $value){
  512. $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  513. $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:"";
  514. $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:"";
  515. $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:"";
  516. $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:"";
  517. $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:"";
  518. $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:"";
  519. $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ;
  520. $value['receipt_quantity']=isset($addrlist['receipt_quantity']) ?$addrlist['receipt_quantity']:"" ;
  521. $send = Db::name("order_out")->where(['addrid' => $addrlist['id'], 'orderCode' => $addrlist['orderCode'],"is_del"=>0])->where("status",">=",2)->sum("send_num");
  522. $value['addr_send'] = $send ?? 0;
  523. $value['addr_wsend'] = $value['receipt_quantity'] - $value['addr_send'];
  524. $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  525. $value['customer_name'] = isset($customer['companyName']) ? $customer['companyName']:"";
  526. $addrinfo[]=$value;
  527. }
  528. }
  529. $info['addrinfo']=$addrinfo;
  530. $info['can']=$int;
  531. return app_show(0,"获取成功",$info);
  532. }
  533. /**
  534. * @return \think\response\Json|void
  535. * @throws \think\db\exception\DataNotFoundException
  536. * @throws \think\db\exception\DbException
  537. * @throws \think\db\exception\ModelNotFoundException
  538. */
  539. public function delete(){
  540. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  541. :"";
  542. if($code==""){
  543. return error_show(1004,"参数returnCode不能为空");
  544. }
  545. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  546. if(empty($info)){
  547. return error_show(1004,"未找到退货数据");
  548. }
  549. $info["is_del"]=1;
  550. $info["updatetime"]=date("Y-m-d H:i:s");
  551. $del = Db::name("sale_return")->save($info);
  552. if($del){
  553. $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  554. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",0,$ste);
  555. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>"XSTHD"];
  556. ProcessOrder::workdel($process);
  557. return app_show(0,"删除成功");
  558. }else{
  559. return error_show(1004,"删除失败");
  560. }
  561. }
  562. /**审核
  563. * @return \think\response\Json|void
  564. * @throws \think\db\exception\DataNotFoundException
  565. * @throws \think\db\exception\DbException
  566. * @throws \think\db\exception\ModelNotFoundException
  567. */
  568. public function exam(){
  569. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  570. :"";
  571. if($code==""){
  572. return error_show(1004,"参数returnCode不能为空");
  573. }
  574. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  575. if(empty($info)){
  576. return error_show(1004,"未找到退货数据");
  577. }
  578. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  579. if($status===""){
  580. return error_show(1004,"参数status不能为空");
  581. }
  582. $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  583. $var = $info['status'];
  584. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  585. if($orderinfo==false){
  586. return error_show(1004,"未找到订单数据");
  587. }
  588. if($status==4){
  589. if($info['is_addr']==1){
  590. $addr=Db::name("sale_returnaddr")->where(['returnCode'=>$info['returnCode'],"is_del"=>0])->select()
  591. ->toArray();
  592. if(empty($addr)){
  593. return error_show(1004,"未找到发货单地址数据");
  594. }
  595. }
  596. }
  597. if($status==3){
  598. $is_th =isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  599. if($is_th===""){
  600. return error_show(1004,"参数is_th不能为空");
  601. }
  602. $return_wsm =isset($this->post['return_wsm'])&&$this->post['return_wsm']!=="" ? trim($this->post['return_wsm']):"";
  603. if($is_th==0){
  604. if($return_wsm===""){
  605. return error_show(1004,"参数return_wsm 不能为空");
  606. }
  607. $wsmcode = Db::name("warehouse_info")->where(['wsm_code'=>$return_wsm])->find();
  608. if($wsmcode==false){
  609. return error_show(1004,"为找到仓库数据");
  610. }
  611. }
  612. $info['return_wsm'] =$return_wsm ;
  613. $info['is_th'] =$is_th ;
  614. }
  615. Db::startTrans();
  616. try{
  617. $temp= $info['status'];
  618. $info['status'] =$status;
  619. $remark!=""? $info['remark'] =$remark:"";
  620. $info["updatetime"]=date("Y-m-d H:i:s");
  621. $up = Db::name("sale_return")->save($info);
  622. if($up) {
  623. $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "order_type"=>"XSTHD",'before_status'=>$temp];
  624. ProcessOrder::AddProcess($this->post['token'], $process);
  625. $ste = ["order_code" => $code, "status" => $temp, "action_remark" => '', "action_type" => "status"];
  626. ActionLog::logAdd($this->post['token'], $ste, "XSTHD", $status, $info);
  627. if ($status == 4) {
  628. if ($info['is_addr'] == 1) {
  629. if (isset($addr) && !empty($addr)) {
  630. foreach ($addr as $value) {
  631. $addrinfo = Db::name("order_addr")->where(['id' => $value['addrid'], "is_del" => 0])->find();
  632. if ($addrinfo == false) {
  633. Db::rollback();
  634. return error_show(1004, "地址数据未找到");
  635. }
  636. if ($addrinfo['receipt_quantity'] < $value['return_num']) {
  637. Db::rollback();
  638. return error_show(1004, "地址发货数量不足");
  639. }
  640. $addrinfo['receipt_quantity'] -= $value['return_num'];
  641. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  642. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  643. $addrup = Db::name("order_addr")->save($addrinfo);
  644. if ($addrup == false) {
  645. Db::rollback();
  646. return error_show(1004, "地址发货数量更新失败");
  647. }
  648. if ($value['outCode'] != "") {
  649. $out = Db::name("order_out")->where(["outCode" => $value['outCode']])->find();
  650. if ($out == false) {
  651. Db::rollback();
  652. return error_show(1004, "地址发货单数据未找到");
  653. }
  654. if ($out['status'] >= 2) {
  655. Db::rollback();
  656. return error_show(1004, "地址发货单已发货");
  657. }
  658. if ($out['send_num'] < $value['return_num']) {
  659. Db::rollback();
  660. return error_show(1004, "地址发货单发货数量不足");
  661. }
  662. $out['send_num'] -= $value['return_num'];
  663. $out['is_del'] = $out['send_num'] <= 0 ? 1 : 0;
  664. $out['updatetime'] = date("Y-m-d H:i:s");
  665. $outup = Db::name("order_out")->save($out);
  666. if ($outup == false) {
  667. Db::rollback();
  668. return error_show(1004, "地址发货单更新失败");
  669. }
  670. $ordersend = Db::name("order_send")->where(["outCode" => $value['outCode']])->find();
  671. if ($ordersend['send_num'] < $value['return_num']) {
  672. Db::rollback();
  673. return error_show(1004, "发货单发货数量不足");
  674. }
  675. $ordersend['send_num'] -= $value['return_num'];
  676. $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  677. $ordersend['updatetime'] = date("Y-m-d H:i:s");
  678. $sendip = Db::name("order_send")->save($ordersend);
  679. if ($sendip == false) {
  680. Db::rollback();
  681. return error_show(1004, "发货单更新失败");
  682. }
  683. }
  684. }
  685. }
  686. }
  687. if ($orderinfo['wsend_num'] < $info['num']) {
  688. Db::rollback();
  689. return error_show(1004, "销售单未发货数量不足退货");
  690. }
  691. $lor=$orderinfo['status'];
  692. $orderinfo['wsend_num'] -= $info['num'];
  693. $orderinfo['send_num'] += $info['num'];
  694. $orderinfo['status'] = $orderinfo['wsend_num']==0?2:($orderinfo['send_num']==0?0:1);
  695. $orderinfo['send_status'] =$orderinfo['wsend_num']==0?3:($orderinfo['send_num']==0?1:2);
  696. if ($orderinfo['is_stock'] == 1) {
  697. $orderinfo['th_num'] += $info['num'];
  698. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  699. $orderinfo['status']=3;
  700. }
  701. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  702. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  703. $uap = Db::name("sale")->save($orderinfo);
  704. if ($uap == false) {
  705. Db::rollback();
  706. return error_show(1005, '销售单订单更新失败');
  707. }
  708. $ordernum = Db::name("order_num")->where(['orderCode' => $info['orderCode']])->find();
  709. if ($ordernum == false) {
  710. Db::rollback();
  711. return error_show(1005, '未找到关联采购单');
  712. }
  713. $ordernum['send_num'] -= $info['num'];
  714. $orderup = Db::name("order_num")->save($ordernum);
  715. if ($orderup == false) {
  716. Db::rollback();
  717. return error_show(1005, '关联数据更新失败');
  718. }
  719. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  720. if ($cgd == false) {
  721. Db::rollback();
  722. return error_show(1005, '未找到采购单数据');
  723. }
  724. $cgd['th_fee'] += round($cgd['good_price'] * $info['num'], 2);
  725. $cgd['th_num'] += $info['num'];
  726. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  727. $cgd['status']=4;
  728. }
  729. $cgd['updatetime'] = date("Y-m-d H:i:s");
  730. $cgdup = Db::name("purchease_order")->save($cgd);
  731. if ($cgdup == false) {
  732. Db::rollback();
  733. return error_show(1005, '采购单数据更新失败');
  734. }
  735. if ($cgd['bkcode'] != "") {
  736. $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1,"order_source"=>0, "is_del" => 0])
  737. ->find();
  738. if ($bk == false) {
  739. Db::rollback();
  740. return error_show(1005, '未找到备库单数据');
  741. }
  742. $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  743. if ($orderbk == false) {
  744. Db::rollback();
  745. return error_show(1005, '备库单未完全入库');
  746. }
  747. $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type"
  748. => 1, "is_del" => 0])->where("order_source","<>",0)
  749. ->field("sum(send_num)-sum(th_num) as num")->find();
  750. $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  751. $orderbk['merge_num'] = $merge_num['num'];
  752. $orderbk['updatetime'] = date("Y-m-d H:i:s");
  753. $orderbkup = Db::name("order_bk")->save($orderbk);
  754. if ($orderbkup == false) {
  755. Db::rollback();
  756. return error_show(1005, '备库单库存数据释放失败');
  757. }
  758. }
  759. } else {
  760. if ($info['is_th'] == 1) {
  761. $orderinfo['th_num'] += $info['num'];
  762. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  763. $orderinfo['status']=3;
  764. }
  765. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  766. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  767. $uap = Db::name("sale")->save($orderinfo);
  768. if ($uap == false) {
  769. Db::rollback();
  770. return error_show(1005, '销售单订单更新失败');
  771. }
  772. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  773. if ($ordernum == false) {
  774. Db::rollback();
  775. return error_show(1005, '未找到关联采购单');
  776. }
  777. $ordernum['send_num'] -= $info['num'];
  778. $orderup = Db::name("order_num")->save($ordernum);
  779. if ($orderup == false) {
  780. Db::rollback();
  781. return error_show(1005, '关联数据更新失败');
  782. }
  783. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  784. if ($cgd == false) {
  785. Db::rollback();
  786. return error_show(1005, '未找到采购单数据');
  787. }
  788. if( $info['is_all']==1 && $cgd['send_status']!=1){
  789. Db::rollback();
  790. return error_show(1005, '采购单已入库无法全部退货');
  791. }
  792. $lor=$cgd['status'];
  793. if($cgd['wsend_num']<$info['num']){
  794. $cgd['send_num'] += $cgd['wsend_num']==0?0 :($info['num']-$cgd['wsend_num']);
  795. $cgd['wsend_num']=0;
  796. }else{
  797. $cgd['wsend_num'] -= $info['num'];
  798. $cgd['send_num'] += $info['num'];
  799. }
  800. $cgd['status'] = $cgd['wsend_num']==0?2:($cgd['send_num']==0?0:1);
  801. $cgd['send_status'] =$cgd['wsend_num']==0?3:($cgd['send_num']==0?1:2);
  802. $cgd['th_num'] += $info['num'];
  803. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  804. $cgd['status']=4;
  805. }
  806. $cgd['th_fee'] += round($info['num'] * $cgd['good_price'], 2);
  807. $cgd['updatetime'] = date("Y-m-d H:i:s");
  808. $cgdup = Db::name("purchease_order")->save($cgd);
  809. if ($cgdup == false) {
  810. Db::rollback();
  811. return error_show(1005, '采购单数据更新失败');
  812. }
  813. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find();
  814. if ($stock==false) {
  815. Db::rollback();
  816. return error_show(1005, '商品仓库未找到');
  817. }
  818. if($stock['wait_in_stock']<$info['num']){
  819. $stock['wait_in_stock']=0;
  820. if( $stock['usable_stock']>$info['num']-$stock['wait_in_stock']){
  821. $stock['usable_stock'] -= $info['num']-$stock['wait_in_stock'];
  822. }else{
  823. $stock['usable_stock'] = 0;
  824. $stock['wait_out_stock']-= $info['num']-$stock['wait_in_stock']- $stock['usable_stock'] ;
  825. }
  826. $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock'];
  827. }else{
  828. $stock['wait_in_stock']-=$info['num'];
  829. }
  830. $stock['updatetime'] = date("Y-m-d H:i:s");
  831. $st_up = Db::name("good_stock")->save($stock);
  832. if ($st_up == false) {
  833. Db::rollback();
  834. return error_show(1005, '可售商品入库失败');
  835. }
  836. } else {
  837. $orderinfo['th_num'] += $info['num'];
  838. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  839. $orderinfo['status']=3;
  840. }
  841. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  842. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  843. $uap = Db::name("sale")->save($orderinfo);
  844. if ($uap == false) {
  845. Db::rollback();
  846. return error_show(1005, '销售单订单更新失败');
  847. }
  848. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  849. if ($ordernum == false) {
  850. Db::rollback();
  851. return error_show(1005, '未找到关联采购单');
  852. }
  853. $ordernum['send_num'] -= $info['num'];
  854. $ordernum['wsend_num'] = $info['num'];
  855. $orderup = Db::name("order_num")->save($ordernum);
  856. if ($orderup == false) {
  857. Db::rollback();
  858. return error_show(1005, '关联数据更新失败');
  859. }
  860. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  861. if ($cgd == false) {
  862. Db::rollback();
  863. return error_show(1005, '未找到采购单数据');
  864. }
  865. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find();
  866. if ($stock==false) {
  867. Db::rollback();
  868. return error_show(1005, '商品仓库未找到');
  869. }
  870. if($stock['usable_stock']+$stock['wait_out_stock']<$info['num']){
  871. Db::rollback();
  872. return error_show(1005, '商品可用库存不足退回数量');
  873. }else{
  874. if($stock['usable_stock']>$info['num']){
  875. $stock['usable_stock'] -= $info['num'];
  876. }else{
  877. $stock['usable_stock'] = 0;
  878. $stock['wait_out_stock']-= $info['num']- $stock['usable_stock'] ;
  879. }
  880. $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock'];
  881. }
  882. $stock['updatetime'] = date("Y-m-d H:i:s");
  883. $st_up = Db::name("good_stock")->save($stock);
  884. if ($st_up == false) {
  885. Db::rollback();
  886. return error_show(1005, '可售商品入库失败');
  887. }
  888. }
  889. }
  890. ActionLog::logAdd($this->post['token'], [
  891. "order_code" => $orderinfo["orderCode"],//出库单号
  892. "status" => $lor,//这里的status是之前的值
  893. "action_remark" => '',//备注
  894. "action_type" => "status"//新建create,编辑edit,更改状态status
  895. ], "XSQRD", $orderinfo['status'], $orderinfo);
  896. ProcessOrder::AddProcess($this->post['token'], [
  897. "order_type" => 'XSQRD',
  898. "order_code" =>$orderinfo["orderCode"],//出库单号
  899. "order_id" => $orderinfo["id"],
  900. "order_status" =>$orderinfo['status'],"before_status"=>$lor
  901. ]);
  902. if($orderinfo['is_stock'] == 1 || $info['is_th'] == 0){
  903. $wsmcode = $orderinfo['is_stock'] == 1 ? $cgd['wsm_code'] : $info['return_wsm'];
  904. if ($wsmcode == "") {
  905. Db::rollback();
  906. return error_show(1005, '未找到退货仓库');
  907. }
  908. //::todo 非库存品订单退货 采购单退货 虚拟仓如何减库存
  909. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' => $wsmcode])->find();
  910. if (empty($stock)) {
  911. $stock = [
  912. "spuCode" => $info['good_code'],
  913. "wsm_code" => $wsmcode,
  914. "usable_stock" => 0,
  915. "wait_out_stock" => 0,
  916. "wait_in_stock" => 0,
  917. "total_stock" => 0,
  918. "addtime" => date("Y-m-d H:i:s"),
  919. "updatetime" => date("Y-m-d H:i:s"),
  920. ];
  921. }
  922. $stock['usable_stock'] += $info['num'];
  923. $stock['updatetime'] = date("Y-m-d H:i:s");
  924. $st_up = Db::name("good_stock")->save($stock);
  925. if ($st_up == false) {
  926. Db::rollback();
  927. return error_show(1005, '可售商品入库失败');
  928. }
  929. $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"];
  930. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  931. }
  932. $data=[
  933. "orderCode"=>$info['orderCode'],
  934. "th_type"=>1,
  935. "th_num"=>$info['num'],
  936. "th_fee"=>round($info['num']*$orderinfo['sale_price'],2),
  937. "thCode"=>$info['returnCode'],
  938. "spuCode"=>$orderinfo['good_code'],
  939. "good_name"=>$orderinfo['good_name'],
  940. "cat_id"=>$orderinfo['cat_id'],
  941. "apply_id"=>$info['apply_id'],
  942. "apply_name"=>$info['apply_name'],
  943. "addtime"=>date("Y-m-d H:i:s"),
  944. "status"=>1,
  945. "is_del"=>0
  946. ];
  947. $inse=Db::name("th_data")->insert($data);
  948. if($inse==false){
  949. Db::rollback();
  950. return error_show(1004,"退货单更新失败");
  951. }
  952. }
  953. }
  954. Db::commit();
  955. return app_show(0,"更新成功");
  956. }catch (\Exception $e){
  957. Db::rollback();
  958. return error_show(1004,$e->getMessage()."|".$e->getLine());
  959. }
  960. }
  961. public function zxcreate(){
  962. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  963. if($ordeCode==''){
  964. return error_show(1004,"参数orderCode 不能为空");
  965. }
  966. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  967. if(empty($order)){
  968. return error_show(1005,"未找到订单数据");
  969. }
  970. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  971. if($errorCode==''){
  972. return error_show(1004,"参数errorCode 不能为空");
  973. }
  974. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  975. if(empty($error)){
  976. return error_show(1005,"未找到退货原因数据");
  977. }
  978. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  979. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  980. if($token==''){
  981. return error_show(105,"参数token不能为空");
  982. }
  983. $user =GetUserInfo($token);
  984. if(empty($user)||$user['code']!=0){
  985. return error_show(1002,"申请人数据不存在");
  986. }
  987. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  988. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  989. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  990. if($num==''){
  991. return error_show(1005,"参数num不能为空");
  992. }
  993. if($order['wsend_num']<$num){
  994. return error_show(1002,"仓库未发货数量不足退货");
  995. }
  996. $returnCode=makeNo("RS");
  997. Db::startTrans();
  998. try{
  999. $in = [
  1000. "returnCode"=>$returnCode,
  1001. "orderCode"=>$ordeCode,
  1002. "good_code"=>$order['good_code'],
  1003. "good_name"=>$order['good_name'],
  1004. "apply_id"=>$rm,
  1005. "apply_name"=>$ri,
  1006. "error_code"=>$errorCode,
  1007. "num"=>$num,
  1008. "remark"=>$remark,
  1009. "order_type"=>2,
  1010. "status"=>0,
  1011. "is_del"=>0,
  1012. "addtime"=>date("Y-m-d H:i:s"),
  1013. "updatetime"=>date("Y-m-d H:i:s")
  1014. ];
  1015. $create = Db::name("sale_return")->insert($in,true);
  1016. if($create>0) {
  1017. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'XSTHD',"before_status"=>0];
  1018. ProcessOrder::AddProcess($this->post['token'],$process);
  1019. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  1020. "action_type"=>"create"];
  1021. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",3,$in);
  1022. //维护台账记录
  1023. // Db::name('standing_book')
  1024. // ->where('ordeCode', $ordeCode)
  1025. // ->update([
  1026. // 'returnGoodCode' => $returnCode,
  1027. // 'updatetime' => date("Y-m-d H:i:s")
  1028. // ]);
  1029. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$ordeCode}'");
  1030. if ($order['send_type'] == 1) {
  1031. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"=>2])->select();
  1032. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  1033. if(!empty($wsend)){
  1034. foreach ($wsend as $value){
  1035. $tt = $value['status'];
  1036. $value['status']=0;
  1037. $value['updatetime']=date("Y-m-d H:i:s");
  1038. $up =Db::name("order_out")->save($value);
  1039. if($up){
  1040. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD',"before_status"=>$tt];
  1041. ProcessOrder::AddProcess($this->post['token'],$process);
  1042. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  1043. ActionLog::logAdd($this->post['token'],$ste,"CKD",0,$value);
  1044. }else{
  1045. Db::rollback();
  1046. return error_show(1005,"退货单新建失败");
  1047. }
  1048. }
  1049. }
  1050. }
  1051. Db::commit();
  1052. return error_show(0,"退货单新建成功");
  1053. }
  1054. Db::rollback();
  1055. return error_show(1005,"退货单新建失败");
  1056. }catch (\Exception $e){
  1057. Db::rollback();
  1058. return error_show(1005,$e->getMessage());
  1059. }
  1060. }
  1061. public function allReturn(){
  1062. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''? trim($this->post['orderCode']):"";
  1063. if($orderCode==''){
  1064. return error_show(1004,"参数orderCode 不能为空");
  1065. }
  1066. $order= Db::name("sale")->where(["orderCode"=>$orderCode,"is_del"=>0])->find();
  1067. if(empty($order)){
  1068. return error_show(1005,"未找到订单数据");
  1069. }
  1070. if($order['wsend_num']!=$order['good_num']){
  1071. return error_show(1005,"订单未发货数量与总数不等,无法全退");
  1072. }
  1073. $retrun =Db::name("sale_return")->where([["orderCode","=",$orderCode],["is_del","=",0],["status","<",4]])
  1074. ->count();
  1075. if($retrun>0){
  1076. return error_show(1005,"存在退货订单数据");
  1077. }
  1078. if($order['order_type']==3){
  1079. $goon = Db::name("good_zixun")->where(["spuCode"=>$order['good_code'],"is_del"=>0])->find();
  1080. }else {
  1081. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  1082. ->where(['a.skuCode' => $order['skuCode']])->field("b.creater,b.createrid,b.supplierNo")->find();
  1083. }
  1084. if($goon==false){
  1085. return error_show(1005,"未找到商品数据");
  1086. }
  1087. $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  1088. if($supplier==false){
  1089. return error_show(1005,"未找到商品供应商数据");
  1090. }
  1091. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  1092. if($errorCode==''){
  1093. return error_show(1004,"参数errorCode 不能为空");
  1094. }
  1095. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  1096. if(empty($error)){
  1097. return error_show(1005,"未找到退货原因数据");
  1098. }
  1099. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  1100. $addr =Db::name("order_addr")->where([["orderCode","=",$orderCode],["is_del","=",0]])->select()->toArray();
  1101. $ordernum = Db::name("order_num")->where(['orderCode' => $orderCode])->find();
  1102. if ($ordernum == false) {
  1103. return error_show(1005, '未找到关联采购单');
  1104. }
  1105. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  1106. if ($cgd == false) {
  1107. return error_show(1005, '未找到采购单数据');
  1108. }
  1109. if($order['is_stock']==0&&$cgd['send_status']!=1){
  1110. return error_show(1005, '采购单已发起入库');
  1111. }
  1112. $returnCode=makeNo("RN");
  1113. Db::startTrans();
  1114. try{
  1115. $in = [
  1116. "returnCode"=>$returnCode,
  1117. "orderCode"=>$orderCode,
  1118. "good_code"=>$order['good_code'],
  1119. "good_name"=>$order['good_name'],
  1120. "apply_id"=>$this->uid,
  1121. "apply_name"=>$this->uname,
  1122. "cgderid"=>$goon['createrid'],
  1123. "cgder"=>$goon['creater'],
  1124. "error_code"=>$errorCode,
  1125. "num"=>$order['wsend_num'],
  1126. "total_fee"=>round($order['sale_price']*$order['wsend_num'],2),
  1127. "good_price"=>$order['sale_price'],
  1128. "platform_id"=>$order['platform_id'],
  1129. "remark"=>$remark,
  1130. "order_type"=>$order['order_type'],
  1131. "is_addr"=>count($addr)>0 ?1:0,
  1132. "status"=>$order['is_stock']==1?4:1,
  1133. "is_del"=>0,
  1134. "is_all"=>1,
  1135. "addtime"=>date("Y-m-d H:i:s"),
  1136. "updatetime"=>date("Y-m-d H:i:s")
  1137. ];
  1138. $create = Db::name("sale_return")->insert($in,true);
  1139. if($create>0){
  1140. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  1141. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname],$stn,"XSTHD",$in['status'],$in);
  1142. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0];
  1143. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname],$process);
  1144. //维护台账信息
  1145. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$orderCode}'");
  1146. if(count($addr)!=0){
  1147. $inf=[];
  1148. foreach ($addr as $val){
  1149. if ($val['receipt_quantity'] == 0) continue;//当退货数量为0时,跳过
  1150. $temp=[];
  1151. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  1152. if ($send == false) {
  1153. Db::rollback();
  1154. return error_show(1004, "地址发货单未找到");
  1155. }
  1156. if($send['status']>=2){
  1157. Db::rollback();
  1158. return error_show(1005,"地址已发货");
  1159. }
  1160. if($order['is_stock']==1){
  1161. $val['is_del'] = 1;
  1162. $val['updatetime'] = date("Y-m-d H:i:s");
  1163. $addrup = Db::name("order_addr")->save($val);
  1164. if ($addrup == false) {
  1165. Db::rollback();
  1166. return error_show(1004, "地址更新失败");
  1167. }
  1168. $send['is_del'] = 1;
  1169. $send['updatetime'] = date("Y-m-d H:i:s");
  1170. $outup = Db::name("order_out")->save($send);
  1171. if ($outup == false) {
  1172. Db::rollback();
  1173. return error_show(1004, "地址发货单更新失败");
  1174. }
  1175. $ordersend = Db::name("order_send")->where(["outCode" => $send['outCode']])->find();
  1176. if($ordersend==false){
  1177. Db::rollback();
  1178. return error_show(1004, "发货单关联数据未找到");
  1179. }
  1180. $ordersend['status'] = 0;
  1181. $ordersend['updatetime'] = date("Y-m-d H:i:s");
  1182. $sendip = Db::name("order_send")->save($ordersend);
  1183. if ($sendip == false) {
  1184. Db::rollback();
  1185. return error_show(1004, "发货单更新失败");
  1186. }
  1187. }
  1188. $temp['returnCode']=$returnCode;
  1189. $temp['orderCode']=$orderCode;
  1190. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  1191. $temp['addrid']=$val['id'];
  1192. $temp['send_num']=$val['receipt_quantity'];
  1193. $temp['return_num']=$val['receipt_quantity'];
  1194. $temp['is_del']=0;
  1195. $temp['addtime']=date("Y-m-d H:i:s");
  1196. $temp['updatetime']=date("Y-m-d H:i:s");
  1197. $inf[]=$temp;
  1198. }
  1199. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  1200. if($inadd==0){
  1201. Db::rollback();
  1202. return error_show(1005,"退货单新建失败");
  1203. }
  1204. }
  1205. if($order['is_stock']==1){
  1206. $lor=$order['status'];
  1207. $thnum = $order['wsend_num'];
  1208. $order['send_num'] += $thnum;
  1209. $order['th_num'] += $thnum;
  1210. $order['wsend_num'] =0;
  1211. $order['status'] = 3;
  1212. $order['send_status']=3;
  1213. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  1214. $order['updatetime'] = date("Y-m-d H:i:s");
  1215. $uap = Db::name("sale")->save($order);
  1216. if ($uap == false) {
  1217. Db::rollback();
  1218. return error_show(1005, '销售单订单更新失败');
  1219. }
  1220. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname], [
  1221. "order_code" => $order["orderCode"],//出库单号
  1222. "status" => $lor,//这里的status是之前的值
  1223. "action_remark" => '',//备注
  1224. "action_type" => "status"//新建create,编辑edit,更改状态status
  1225. ], "XSQRD", $order['status'], $order);
  1226. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname], [
  1227. "order_type" => 'XSQRD',
  1228. "order_code" =>$order["orderCode"],//出库单号
  1229. "order_id" => $order["id"],
  1230. "order_status" =>$order['status'],"before_status"=>$lor
  1231. ]);
  1232. $ordernum['send_num'] -= $thnum;
  1233. $orderup = Db::name("order_num")->save($ordernum);
  1234. if ($orderup == false) {
  1235. Db::rollback();
  1236. return error_show(1005, '关联数据更新失败');
  1237. }
  1238. $cgd['th_fee'] += round($cgd['good_price'] * $thnum, 2);
  1239. $cgd['th_num'] += $thnum;
  1240. $cgd['updatetime'] = date("Y-m-d H:i:s");
  1241. $cgdup = Db::name("purchease_order")->save($cgd);
  1242. if ($cgdup == false) {
  1243. Db::rollback();
  1244. return error_show(1005, '采购单数据更新失败');
  1245. }
  1246. if ($cgd['bkcode'] != "") {
  1247. $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "order_source" =>0, "is_del" => 0])
  1248. ->find();
  1249. if ($bk == false) {
  1250. Db::rollback();
  1251. return error_show(1005, '未找到备库单数据');
  1252. }
  1253. $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  1254. if ($orderbk == false) {
  1255. Db::rollback();
  1256. return error_show(1005, '备库单未完全入库');
  1257. }
  1258. $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" =>1, "is_del" => 0])->where("order_source","<>",0)->field("sum(send_num)-sum(th_num) as num")
  1259. ->find();
  1260. $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  1261. $orderbk['merge_num'] = $merge_num['num'];
  1262. $orderbk['updatetime'] = date("Y-m-d H:i:s");
  1263. $orderbkup = Db::name("order_bk")->save($orderbk);
  1264. if ($orderbkup == false) {
  1265. Db::rollback();
  1266. return error_show(1005, '备库单库存数据释放失败');
  1267. }
  1268. }
  1269. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'wsm_code' => $cgd['wsm_code']])
  1270. ->find();
  1271. if (empty($stock)) {
  1272. $stock = [
  1273. "spuCode" => $order['good_code'],
  1274. "wsm_code" => $cgd['wsm_code'],
  1275. "usable_stock" => 0,
  1276. "wait_out_stock" => 0,
  1277. "wait_in_stock" => 0,
  1278. "total_stock" => 0,
  1279. "addtime" => date("Y-m-d H:i:s"),
  1280. "updatetime" => date("Y-m-d H:i:s"),
  1281. ];
  1282. }
  1283. $stock['usable_stock']+=$thnum;
  1284. $stock['wait_out_stock'] -= $thnum;
  1285. $stock['updatetime'] = date("Y-m-d H:i:s");
  1286. $st_up = Db::name("good_stock")->save($stock);
  1287. if ($st_up == false) {
  1288. return error_show(1005, '可售商品入库失败');
  1289. }
  1290. $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"];
  1291. $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"];
  1292. GoodLog::LogAdd(["id"=>$this->uid,"nickname"=>$this->uname], $good_data, 'XSTHD');
  1293. $data=[
  1294. "orderCode"=>$orderCode,
  1295. "th_type"=>1,
  1296. "th_num"=>$thnum,
  1297. "th_fee"=>round($order['sale_price']*$thnum,2),
  1298. "thCode"=>$returnCode,
  1299. "spuCode"=>$order['good_code'],
  1300. "good_name"=>$order['good_name'],
  1301. "cat_id"=>$order['cat_id'],
  1302. "apply_id"=>$this->uid,
  1303. "apply_name"=>$this->uname,
  1304. "addtime"=>date("Y-m-d H:i:s"),
  1305. "status"=>1,
  1306. "is_del"=>0
  1307. ];
  1308. $inse=Db::name("th_data")->insert($data);
  1309. if($inse==false){
  1310. Db::rollback();
  1311. return error_show(1004,"退货单更新失败");
  1312. }
  1313. }
  1314. Db::commit();
  1315. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  1316. }
  1317. Db::rollback();
  1318. return error_show(1005,"退货单新建失败");
  1319. }catch (\Exception $e){
  1320. Db::rollback();
  1321. return error_show(1005, $e->getMessage());
  1322. }
  1323. }
  1324. }