Reorder.php 74 KB

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