Reorder.php 82 KB

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