Reorder.php 74 KB

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