Reorder.php 82 KB

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