Reorder.php 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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("RS");
  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::name('standing_book')
  101. ->where('orderCode', $ordeCode)
  102. ->update([
  103. "returnGoodCode" => $returnCode,
  104. "updatetime" => date("Y-m-d H:i:s")
  105. ]);
  106. if($returnadr!=""){
  107. $inf=[];
  108. foreach ($returnadr as $val){
  109. if ($val['return_num'] == 0) continue;//当退货数量为0时,跳过
  110. $temp=[];
  111. $addrinfo =Db::name("order_addr")->where(['id'=>$val['id'],"orderCode"=>$ordeCode])->find();
  112. if($addrinfo==false){
  113. Db::rollback();
  114. return error_show(1005,"地址信息未找到");
  115. }
  116. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  117. if($send['status']>=2){
  118. Db::rollback();
  119. return error_show(1005,"地址已发货");
  120. }
  121. if($order['is_stock']==1){
  122. if ($addrinfo['receipt_quantity'] < $val['return_num']) {
  123. Db::rollback();
  124. return error_show(1004, "地址发货数量不足");
  125. }
  126. $addrinfo['receipt_quantity'] -= $val['return_num'];
  127. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  128. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  129. $addrup = Db::name("order_addr")->save($addrinfo);
  130. if ($addrup == false) {
  131. Db::rollback();
  132. return error_show(1004, "地址发货数量更新失败");
  133. }
  134. $out = Db::name("order_out")->where(["addrid"=>$val['id']])->find();
  135. if ($out == false) {
  136. Db::rollback();
  137. return error_show(1004, "地址发货单数据未找到");
  138. }
  139. // if ($send!=false) {
  140. if ($out['status'] >= 2) {
  141. Db::rollback();
  142. return error_show(1004, "地址发货单已发货");
  143. }
  144. if ($out['send_num'] < $val['return_num']) {
  145. Db::rollback();
  146. return error_show(1004, "地址发货单发货数量不足");
  147. }
  148. $out['send_num'] -= $val['return_num'];
  149. $out['updatetime'] = date("Y-m-d H:i:s");
  150. $outup = Db::name("order_out")->save($out);
  151. if ($outup == false) {
  152. Db::rollback();
  153. return error_show(1004, "地址发货单更新失败");
  154. }
  155. $ordersend = Db::name("order_send")->where(["outCode" => $out['outCode']])->find();
  156. if ($ordersend['send_num'] < $val['return_num']) {
  157. Db::rollback();
  158. return error_show(1004, "发货单发货数量不足");
  159. }
  160. $ordersend['send_num'] -= $val['return_num'];
  161. $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  162. $ordersend['updatetime'] = date("Y-m-d H:i:s");
  163. $sendip = Db::name("order_send")->save($ordersend);
  164. if ($sendip == false) {
  165. Db::rollback();
  166. return error_show(1004, "发货单更新失败");
  167. }
  168. // }
  169. }
  170. $temp['returnCode']=$returnCode;
  171. $temp['orderCode']=$ordeCode;
  172. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  173. $temp['addrid']=$val['id'];
  174. $temp['send_num']=$addrinfo['receipt_quantity'];
  175. $temp['return_num']=$val['return_num'];
  176. $temp['is_del']=0;
  177. $temp['addtime']=date("Y-m-d H:i:s");
  178. $temp['updatetime']=date("Y-m-d H:i:s");
  179. $inf[]=$temp;
  180. }
  181. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  182. if($inadd==0){
  183. Db::rollback();
  184. return error_show(1005,"退货单新建失败");
  185. }
  186. }
  187. if($order['is_stock']==1){
  188. if ($order['wsend_num'] < $thnum) {
  189. Db::rollback();
  190. return error_show(1004, "销售单未发货数量不足退货");
  191. }
  192. $lor=$order['status'] ;
  193. $order['wsend_num'] -= $thnum;
  194. $order['send_num'] += $thnum;
  195. $order['status'] = $order['wsend_num']==0?2:($order['send_num']==0?0:1);
  196. $order['send_status'] =$order['wsend_num']==0?3:($order['send_num']==0?1:2);
  197. $order['th_num'] += $thnum;
  198. if($order['th_num']==$order['send_num']&& $order['wsend_num']==0){
  199. $order['status']=3;
  200. }
  201. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  202. $order['updatetime'] = date("Y-m-d H:i:s");
  203. $uap = Db::name("sale")->save($order);
  204. if ($uap == false) {
  205. Db::rollback();
  206. return error_show(1005, '销售单订单更新失败');
  207. }
  208. ActionLog::logAdd($this->post['token'], [
  209. "order_code" => $order["orderCode"],//出库单号
  210. "status" => $lor,//这里的status是之前的值
  211. "action_remark" => '',//备注
  212. "action_type" => "status"//新建create,编辑edit,更改状态status
  213. ], "XSQRD", $order['status'], $order);
  214. ProcessOrder::AddProcess($this->post['token'], [
  215. "order_type" => 'XSQRD',
  216. "order_code" =>$order["orderCode"],//出库单号
  217. "order_id" => $order["id"],
  218. "order_status" =>$order['status'],"before_status"=>$lor
  219. ]);
  220. $ordernum = Db::name("order_num")->where(['orderCode' => $ordeCode])->find();
  221. if ($ordernum == false) {
  222. Db::rollback();
  223. return error_show(1005, '未找到关联采购单');
  224. }
  225. $ordernum['send_num'] -= $thnum;
  226. $orderup = Db::name("order_num")->save($ordernum);
  227. if ($orderup == false) {
  228. Db::rollback();
  229. return error_show(1005, '关联数据更新失败');
  230. }
  231. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  232. if ($cgd == false) {
  233. Db::rollback();
  234. return error_show(1005, '未找到采购单数据');
  235. }
  236. $cgd['th_fee'] += round($cgd['good_price'] * $thnum, 2);
  237. $cgd['th_num'] += $thnum;
  238. $cgd['updatetime'] = date("Y-m-d H:i:s");
  239. $cgdup = Db::name("purchease_order")->save($cgd);
  240. if ($cgdup == false) {
  241. Db::rollback();
  242. return error_show(1005, '采购单数据更新失败');
  243. }
  244. if ($cgd['bkcode'] != "") {
  245. $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "is_del" => 0])
  246. ->find();
  247. if ($bk == false) {
  248. Db::rollback();
  249. return error_show(1005, '未找到备库单数据');
  250. }
  251. $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  252. if ($orderbk == false) {
  253. Db::rollback();
  254. return error_show(1005, '备库单未完全入库');
  255. }
  256. $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" => 1, "is_del" => 0])->field("sum(send_num)-sum(th_num) as num")->find();
  257. $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  258. $orderbk['merge_num'] = $merge_num['num'];
  259. $orderbk['updatetime'] = date("Y-m-d H:i:s");
  260. $orderbkup = Db::name("order_bk")->save($orderbk);
  261. if ($orderbkup == false) {
  262. Db::rollback();
  263. return error_show(1005, '备库单库存数据释放失败');
  264. }
  265. }
  266. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'wsm_code' => $cgd['wsm_code']])
  267. ->find();
  268. if (empty($stock)) {
  269. $stock = [
  270. "spuCode" => $order['good_code'],
  271. "wsm_code" => $cgd['wsm_code'],
  272. "usable_stock" => 0,
  273. "wait_out_stock" => 0,
  274. "wait_in_stock" => 0,
  275. "total_stock" => 0,
  276. "addtime" => date("Y-m-d H:i:s"),
  277. "updatetime" => date("Y-m-d H:i:s"),
  278. ];
  279. }
  280. // if($stock['presale_stock']>0){
  281. // if($stock['presale_stock']>=$thnum){
  282. // $stock['presale_stock']-=$thnum;
  283. // }else{
  284. // $stock['presale_stock']=0;
  285. // $stock['usable_stock']+=$thnum;
  286. // $stock['wait_out_stock'] -= ($thnum -$stock['presale_stock']);
  287. // }
  288. // }else{
  289. $stock['usable_stock']+=$thnum;
  290. $stock['wait_out_stock'] -= $thnum;
  291. // }
  292. $stock['updatetime'] = date("Y-m-d H:i:s");
  293. $st_up = Db::name("good_stock")->save($stock);
  294. if ($st_up == false) {
  295. return error_show(1005, '可售商品入库失败');
  296. }
  297. $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"];
  298. $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"];
  299. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  300. $data=[
  301. "orderCode"=>$ordeCode,
  302. "th_type"=>1,
  303. "th_num"=>$thnum,
  304. "th_fee"=>round($order['sale_price']*$thnum,2),
  305. "thCode"=>$returnCode,
  306. "spuCode"=>$order['good_code'],
  307. "good_name"=>$order['good_name'],
  308. "cat_id"=>$order['cat_id'],
  309. "apply_id"=>$rm,
  310. "apply_name"=>$ri,
  311. "addtime"=>date("Y-m-d H:i:s"),
  312. "status"=>1,
  313. "is_del"=>0
  314. ];
  315. $inse=Db::name("th_data")->insert($data);
  316. if($inse==false){
  317. Db::rollback();
  318. return error_show(1004,"退货单更新失败");
  319. }
  320. }
  321. Db::commit();
  322. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  323. }
  324. Db::rollback();
  325. return error_show(1005,"退货单新建失败");
  326. }catch (\Exception $e){
  327. Db::rollback();
  328. return error_show(1005,$e->getMessage());
  329. }
  330. }
  331. public function list(){
  332. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  333. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  334. $where =[['sr.is_del',"=",0]];
  335. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  336. if($bkcode!=""){
  337. $where[]=['sr.returnCode',"like", "%{$bkcode}%"];
  338. }
  339. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  340. if($status!==""){
  341. $where[]=['sr.status',"=", $status];
  342. }
  343. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode'])
  344. :"";
  345. if($orderCode!=""){
  346. $where[]=['sr.orderCode',"like", "%{$orderCode}%"];
  347. }
  348. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  349. :"";
  350. if($apply_name!=""){
  351. $where[]=['sr.apply_name',"like", "%{$apply_name}%"];
  352. }
  353. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  354. if($start!==""){
  355. $where[]=['sr.addtime',">=", $start.' 00:00:00'];
  356. }
  357. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  358. if($end!==""){
  359. $where[]=['sr.addtime',"<=", $end.' 23:59:59'];
  360. }
  361. $role=$this->checkRole();
  362. $condition='';
  363. if(!empty($role['write']) && $this->uid!=""){
  364. // $where[]=["sr.apply_id","in",$role['write']];
  365. $condition .="sr.cgderid = {$this->uid} or sr.apply_id in (".implode(',',$role['write']).")";
  366. }
  367. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  368. if ($company_name !== "") $where[] = ["sr.apply_id", 'in', get_company_item_user_by_name($company_name)];
  369. $count=Db::name("sale_return")->alias('sr')->where($where)->where($condition)->count();
  370. $total = ceil($count/$size);
  371. $page = $total>=$page ? $page :$total;
  372. $list = Db::name("sale_return")
  373. ->alias('sr')
  374. ->field('sr.*,u.itemid')
  375. ->leftJoin("depart_user u", "u.uid=sr.apply_id AND u.is_del=0")
  376. ->where($where)
  377. ->where($condition)
  378. ->order("addtime desc")
  379. ->page($page,$size)
  380. ->cursor();
  381. // echo Db::name("sale_return")->getLastSql();
  382. $data=[];
  383. foreach ($list as $value){
  384. $value['error_msg']='';
  385. if($value['error_code']!=''){
  386. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  387. $value['error_msg']= isset($error['result'])?$error['result']:"";
  388. }
  389. $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  390. $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  391. $value['return_total'] =$value['sale_price']*$value['num'] ;
  392. $value['total_num'] =$order['good_num'] ;
  393. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  394. $data[]=$value ;
  395. }
  396. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  397. }
  398. public function info(){
  399. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  400. :"";
  401. if($code==""){
  402. return error_show(1004,"参数returnCode不能为空");
  403. }
  404. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  405. if(empty($info)){
  406. return error_show(1004,"未找到退货数据");
  407. }
  408. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  409. if($orderinfo['order_type']==3){
  410. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  411. }else {
  412. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  413. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  414. }
  415. if ($goon==false) {
  416. return error_show(1003, "未找到商品数据");
  417. }
  418. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  419. $info['is_stock']=isset($goon['is_stock'])?$goon['is_stock']:'0';
  420. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  421. $info['send_type'] = isset($orderinfo['send_type'])?$orderinfo['send_type']:'';
  422. $info['skuCode'] = isset($orderinfo['skuCode'])?$orderinfo['skuCode']:'';
  423. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  424. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  425. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  426. $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0';
  427. $info['return_total'] = $info['sale_price']*$info['num'] ;
  428. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  429. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  430. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  431. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  432. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  433. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  434. $info['customer_name']='';
  435. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  436. $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  437. $info['customer_name'] = isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  438. }
  439. $info['supplierNo'] = isset($orderinfo['supplierNo'])?$orderinfo['supplierNo']:'';
  440. $info['supplier_name']='';
  441. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  442. $customerinfo = Db::name("business")->where(['companyNo'=>$orderinfo['supplierNo']])->find();
  443. $info['supplier_name'] = isset($customerinfo['company']) ? $customerinfo['company']:"";
  444. }
  445. $info['platform_name']='';
  446. $info['platform_id']=$orderinfo['platform_id'];
  447. if($orderinfo['platform_id']!=0){
  448. $plat=Db::name("platform")->where(['id'=>$orderinfo['platform_id']])->find();
  449. $info['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:"";
  450. }
  451. $info['cgd_wsend']="";
  452. $info['cgd_send']="";
  453. $info['cgd_total']="";
  454. if($orderinfo['order_type']==2){
  455. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find();
  456. $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0;
  457. $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0;
  458. $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0;
  459. }
  460. $info['error_msg']='';
  461. if($info['error_code']!=''){
  462. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  463. $info['error_msg']= isset($error['result'])?$error['result']:"";
  464. }
  465. if($info['return_wsm']!=""){
  466. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  467. ->where(["a.wsm_code"=>$info['return_wsm']])->field("a.name as wsm_name,b.name,b.code")->find();
  468. }
  469. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  470. // $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  471. // $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  472. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  473. $wsm=[];
  474. if(!empty($wsm_return)){
  475. foreach ($wsm_return as $value){
  476. $value['wsm_name']="";
  477. $value['wsm_supplier']="";
  478. $value['wsm_supplierNo']="";
  479. if($value['wsm_code']!=""){
  480. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  481. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  482. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  483. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  484. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  485. }
  486. $orderwsm = Db::name("sale_info")->where(["orderCode"=>$info["orderCode"],"wsm_code"=>$value["wsm_code"]])->find();
  487. $value["wsm_total"] = isset($orderwsm["num"]) ? $orderwsm["num"]:0;
  488. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $info['orderCode']])->sum("send_num");
  489. $value['wsm_send'] = $send ?? 0;
  490. $value['wsm_wsend'] = $value['wsm_total'] - $value['wsm_send'];
  491. $wsm[]=$value;
  492. }
  493. }
  494. $info['wsminfo']=$wsm;
  495. $addr =Db::name("sale_returnaddr")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  496. $addrinfo=[];
  497. if(!empty($addr)){
  498. foreach ( $addr as $value){
  499. $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  500. $value['addr']=isset($addrlist['addr'])?$addrlist['addr']:"";
  501. $value['addr_code']=isset($addrlist['addr_code'])?$addrlist['addr_code']:"";
  502. $value['contactor']=isset($addrlist['contactor'])?$addrlist['contactor']:"";
  503. $value['mobile']=isset($addrlist['mobile'])?$addrlist['mobile']:"";
  504. $value['post_fee']=isset($addrlist['post_fee'])?$addrlist['post_fee']:"";
  505. $value['addive_time']=isset($addrlist['addive_time'])?$addrlist['addive_time']:"";
  506. $value['customer_code']=isset($addrlist['customer_code']) ?$addrlist['customer_code']:"" ;
  507. $value['receipt_quantity']=isset($addrlist['receipt_quantity']) ?$addrlist['receipt_quantity']:"" ;
  508. $send = Db::name("order_out")->where(['addrid' => $addrlist['id'], 'orderCode' => $addrlist['orderCode'],"is_del"=>0])->where("status",">=",2)->sum("send_num");
  509. $value['addr_send'] = $send ?? 0;
  510. $value['addr_wsend'] = $value['receipt_quantity'] - $value['addr_send'];
  511. $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  512. $value['customer_name'] = isset($customer['companyName']) ? $customer['companyName']:"";
  513. $addrinfo[]=$value;
  514. }
  515. }
  516. $info['addrinfo']=$addrinfo;
  517. $info['can']=$int;
  518. return app_show(0,"获取成功",$info);
  519. }
  520. /**
  521. * @return \think\response\Json|void
  522. * @throws \think\db\exception\DataNotFoundException
  523. * @throws \think\db\exception\DbException
  524. * @throws \think\db\exception\ModelNotFoundException
  525. */
  526. public function delete(){
  527. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  528. :"";
  529. if($code==""){
  530. return error_show(1004,"参数returnCode不能为空");
  531. }
  532. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  533. if(empty($info)){
  534. return error_show(1004,"未找到退货数据");
  535. }
  536. $info["is_del"]=1;
  537. $info["updatetime"]=date("Y-m-d H:i:s");
  538. $del = Db::name("sale_return")->save($info);
  539. if($del){
  540. $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  541. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",0,$ste);
  542. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>"XSTHD"];
  543. ProcessOrder::workdel($process);
  544. return app_show(0,"删除成功");
  545. }else{
  546. return error_show(1004,"删除失败");
  547. }
  548. }
  549. /**审核
  550. * @return \think\response\Json|void
  551. * @throws \think\db\exception\DataNotFoundException
  552. * @throws \think\db\exception\DbException
  553. * @throws \think\db\exception\ModelNotFoundException
  554. */
  555. public function exam(){
  556. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  557. :"";
  558. if($code==""){
  559. return error_show(1004,"参数returnCode不能为空");
  560. }
  561. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  562. if(empty($info)){
  563. return error_show(1004,"未找到退货数据");
  564. }
  565. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status'])
  566. :"";
  567. if($status===""){
  568. return error_show(1004,"参数status不能为空");
  569. }
  570. $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  571. $var = $info['status'];
  572. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  573. $apply_id =GetUserInfo($token);
  574. if(empty($apply_id)||$apply_id['code']!=0){
  575. return error_show(1002,"申请人数据不存在");
  576. }
  577. $rid= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  578. $rname= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  579. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  580. if($orderinfo==false){
  581. return error_show(1004,"未找到订单数据");
  582. }
  583. if($status==4){
  584. if($info['is_addr']==1){
  585. $addr=Db::name("sale_returnaddr")->where(['returnCode'=>$info['returnCode'],"is_del"=>0])->select()
  586. ->toArray();
  587. if(empty($addr)){
  588. return error_show(1004,"未找到发货单地址数据");
  589. }
  590. }
  591. }
  592. if($status==3){
  593. $is_th =isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  594. if($is_th===""){
  595. return error_show(1004,"参数is_th不能为空");
  596. }
  597. $return_wsm =isset($this->post['return_wsm'])&&$this->post['return_wsm']!=="" ? trim($this->post['return_wsm']):"";
  598. if($is_th==0){
  599. if($return_wsm===""){
  600. return error_show(1004,"参数return_wsm 不能为空");
  601. }
  602. $wsmcode = Db::name("warehouse_info")->where(['wsm_code'=>$return_wsm])->find();
  603. if($wsmcode==false){
  604. return error_show(1004,"为找到仓库数据");
  605. }
  606. }
  607. $info['return_wsm'] =$return_wsm ;
  608. $info['is_th'] =$is_th ;
  609. }
  610. Db::startTrans();
  611. try{
  612. $temp= $info['status'];
  613. $info['status'] =$status;
  614. $remark!=""? $info['remark'] =$remark:"";
  615. $info["updatetime"]=date("Y-m-d H:i:s");
  616. $up = Db::name("sale_return")->save($info);
  617. if($up) {
  618. $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "order_type"=>"XSTHD",'before_status'=>$temp];
  619. ProcessOrder::AddProcess($this->post['token'], $process);
  620. $ste = ["order_code" => $code, "status" => $temp, "action_remark" => '', "action_type" => "status"];
  621. ActionLog::logAdd($this->post['token'], $ste, "XSTHD", $status, $info);
  622. if ($status == 4) {
  623. if ($info['is_addr'] == 1) {
  624. if (isset($addr) && !empty($addr)) {
  625. foreach ($addr as $value) {
  626. $addrinfo = Db::name("order_addr")->where(['id' => $value['addrid'], "is_del" => 0])->find();
  627. if ($addrinfo == false) {
  628. Db::rollback();
  629. return error_show(1004, "地址数据未找到");
  630. }
  631. if ($addrinfo['receipt_quantity'] < $value['return_num']) {
  632. Db::rollback();
  633. return error_show(1004, "地址发货数量不足");
  634. }
  635. $addrinfo['receipt_quantity'] -= $value['return_num'];
  636. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  637. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  638. $addrup = Db::name("order_addr")->save($addrinfo);
  639. if ($addrup == false) {
  640. Db::rollback();
  641. return error_show(1004, "地址发货数量更新失败");
  642. }
  643. if ($value['outCode'] != "") {
  644. $out = Db::name("order_out")->where(["outCode" => $value['outCode']])->find();
  645. if ($out == false) {
  646. Db::rollback();
  647. return error_show(1004, "地址发货单数据未找到");
  648. }
  649. if ($out['status'] >= 2) {
  650. Db::rollback();
  651. return error_show(1004, "地址发货单已发货");
  652. }
  653. if ($out['send_num'] < $value['return_num']) {
  654. Db::rollback();
  655. return error_show(1004, "地址发货单发货数量不足");
  656. }
  657. $out['send_num'] -= $value['return_num'];
  658. $out['is_del'] = $out['send_num'] <= 0 ? 1 : 0;
  659. $out['updatetime'] = date("Y-m-d H:i:s");
  660. $outup = Db::name("order_out")->save($out);
  661. if ($outup == false) {
  662. Db::rollback();
  663. return error_show(1004, "地址发货单更新失败");
  664. }
  665. $ordersend = Db::name("order_send")->where(["outCode" => $value['outCode']])->find();
  666. if ($ordersend['send_num'] < $value['return_num']) {
  667. Db::rollback();
  668. return error_show(1004, "发货单发货数量不足");
  669. }
  670. $ordersend['send_num'] -= $value['return_num'];
  671. $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  672. $ordersend['updatetime'] = date("Y-m-d H:i:s");
  673. $sendip = Db::name("order_send")->save($ordersend);
  674. if ($sendip == false) {
  675. Db::rollback();
  676. return error_show(1004, "发货单更新失败");
  677. }
  678. }
  679. }
  680. }
  681. }
  682. if ($orderinfo['wsend_num'] < $info['num']) {
  683. Db::rollback();
  684. return error_show(1004, "销售单未发货数量不足退货");
  685. }
  686. $lor=$orderinfo['status'];
  687. $orderinfo['wsend_num'] -= $info['num'];
  688. $orderinfo['send_num'] += $info['num'];
  689. $orderinfo['status'] = $orderinfo['wsend_num']==0?2:($orderinfo['send_num']==0?0:1);
  690. $orderinfo['send_status'] =$orderinfo['wsend_num']==0?3:($orderinfo['send_num']==0?1:2);
  691. if ($orderinfo['is_stock'] == 1) {
  692. $orderinfo['th_num'] += $info['num'];
  693. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  694. $orderinfo['status']=3;
  695. }
  696. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  697. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  698. $uap = Db::name("sale")->save($orderinfo);
  699. if ($uap == false) {
  700. Db::rollback();
  701. return error_show(1005, '销售单订单更新失败');
  702. }
  703. $ordernum = Db::name("order_num")->where(['orderCode' => $info['orderCode']])->find();
  704. if ($ordernum == false) {
  705. Db::rollback();
  706. return error_show(1005, '未找到关联采购单');
  707. }
  708. $ordernum['send_num'] -= $info['num'];
  709. $orderup = Db::name("order_num")->save($ordernum);
  710. if ($orderup == false) {
  711. Db::rollback();
  712. return error_show(1005, '关联数据更新失败');
  713. }
  714. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  715. if ($cgd == false) {
  716. Db::rollback();
  717. return error_show(1005, '未找到采购单数据');
  718. }
  719. $cgd['th_fee'] += round($cgd['good_price'] * $info['num'], 2);
  720. $cgd['th_num'] += $info['num'];
  721. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  722. $cgd['status']=4;
  723. }
  724. $cgd['updatetime'] = date("Y-m-d H:i:s");
  725. $cgdup = Db::name("purchease_order")->save($cgd);
  726. if ($cgdup == false) {
  727. Db::rollback();
  728. return error_show(1005, '采购单数据更新失败');
  729. }
  730. if ($cgd['bkcode'] != "") {
  731. $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "is_del" => 0])
  732. ->find();
  733. if ($bk == false) {
  734. Db::rollback();
  735. return error_show(1005, '未找到备库单数据');
  736. }
  737. $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  738. if ($orderbk == false) {
  739. Db::rollback();
  740. return error_show(1005, '备库单未完全入库');
  741. }
  742. $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" => 1, "is_del" => 0])
  743. ->field("sum(send_num)-sum(th_num) as num")->find();
  744. $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  745. $orderbk['merge_num'] = $merge_num['num'];
  746. $orderbk['updatetime'] = date("Y-m-d H:i:s");
  747. $orderbkup = Db::name("order_bk")->save($orderbk);
  748. if ($orderbkup == false) {
  749. Db::rollback();
  750. return error_show(1005, '备库单库存数据释放失败');
  751. }
  752. }
  753. } else {
  754. if ($info['is_th'] == 1) {
  755. $orderinfo['th_num'] += $info['num'];
  756. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  757. $orderinfo['status']=3;
  758. }
  759. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  760. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  761. $uap = Db::name("sale")->save($orderinfo);
  762. if ($uap == false) {
  763. Db::rollback();
  764. return error_show(1005, '销售单订单更新失败');
  765. }
  766. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  767. if ($ordernum == false) {
  768. Db::rollback();
  769. return error_show(1005, '未找到关联采购单');
  770. }
  771. $ordernum['send_num'] -= $info['num'];
  772. $orderup = Db::name("order_num")->save($ordernum);
  773. if ($orderup == false) {
  774. Db::rollback();
  775. return error_show(1005, '关联数据更新失败');
  776. }
  777. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  778. if ($cgd == false) {
  779. Db::rollback();
  780. return error_show(1005, '未找到采购单数据');
  781. }
  782. $lor=$cgd['status'];
  783. if($cgd['wsend_num']<$info['num']){
  784. $cgd['wsend_num']=0;
  785. $cgd['send_num'] += $info['num']-$cgd['wsend_num'];
  786. }else{
  787. $cgd['wsend_num'] -= $info['num'];
  788. $cgd['send_num'] += $info['num'];
  789. }
  790. $cgd['status'] = $cgd['wsend_num']==0?2:($cgd['send_num']==0?0:1);
  791. $cgd['send_status'] =$cgd['wsend_num']==0?3:($cgd['send_num']==0?1:2);
  792. $cgd['th_num'] += $info['num'];
  793. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  794. $cgd['status']=4;
  795. }
  796. $cgd['th_fee'] += round($info['num'] * $cgd['good_price'], 2);
  797. $cgd['updatetime'] = date("Y-m-d H:i:s");
  798. $cgdup = Db::name("purchease_order")->save($cgd);
  799. if ($cgdup == false) {
  800. Db::rollback();
  801. return error_show(1005, '采购单数据更新失败');
  802. }
  803. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find();
  804. if ($stock==false) {
  805. Db::rollback();
  806. return error_show(1005, '商品仓库未找到');
  807. }
  808. if($stock['wait_in_stock']<$info['num']){
  809. $stock['wait_in_stock']=0;
  810. if( $stock['usable_stock']>$info['num']-$stock['wait_in_stock']){
  811. $stock['usable_stock'] -= $info['num']-$stock['wait_in_stock'];
  812. }else{
  813. $stock['usable_stock'] = 0;
  814. $stock['wait_out_stock']-= $info['num']-$stock['wait_in_stock']- $stock['usable_stock'] ;
  815. }
  816. $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock'];
  817. }else{
  818. $stock['wait_in_stock']-=$info['num'];
  819. }
  820. $stock['updatetime'] = date("Y-m-d H:i:s");
  821. $st_up = Db::name("good_stock")->save($stock);
  822. if ($st_up == false) {
  823. Db::rollback();
  824. return error_show(1005, '可售商品入库失败');
  825. }
  826. } else {
  827. $orderinfo['th_num'] += $info['num'];
  828. if($orderinfo['th_num']==$orderinfo['send_num']&& $orderinfo['wsend_num']==0){
  829. $orderinfo['status']=3;
  830. }
  831. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  832. $orderinfo['updatetime'] = date("Y-m-d H:i:s");
  833. $uap = Db::name("sale")->save($orderinfo);
  834. if ($uap == false) {
  835. Db::rollback();
  836. return error_show(1005, '销售单订单更新失败');
  837. }
  838. $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo['orderCode']])->find();
  839. if ($ordernum == false) {
  840. Db::rollback();
  841. return error_show(1005, '未找到关联采购单');
  842. }
  843. $ordernum['send_num'] -= $info['num'];
  844. $ordernum['wsend_num'] = $info['num'];
  845. $orderup = Db::name("order_num")->save($ordernum);
  846. if ($orderup == false) {
  847. Db::rollback();
  848. return error_show(1005, '关联数据更新失败');
  849. }
  850. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  851. if ($cgd == false) {
  852. Db::rollback();
  853. return error_show(1005, '未找到采购单数据');
  854. }
  855. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' =>$cgd['wsm_code']])->find();
  856. if ($stock==false) {
  857. Db::rollback();
  858. return error_show(1005, '商品仓库未找到');
  859. }
  860. if($stock['usable_stock']+$stock['wait_out_stock']<$info['num']){
  861. Db::rollback();
  862. return error_show(1005, '商品可用库存不足退回数量');
  863. }else{
  864. if($stock['usable_stock']>$info['num']){
  865. $stock['usable_stock'] -= $info['num'];
  866. }else{
  867. $stock['usable_stock'] = 0;
  868. $stock['wait_out_stock']-= $info['num']- $stock['usable_stock'] ;
  869. }
  870. $stock['total_stock']= $stock['usable_stock']+ $stock['wait_out_stock'];
  871. }
  872. $stock['updatetime'] = date("Y-m-d H:i:s");
  873. $st_up = Db::name("good_stock")->save($stock);
  874. if ($st_up == false) {
  875. Db::rollback();
  876. return error_show(1005, '可售商品入库失败');
  877. }
  878. }
  879. }
  880. ActionLog::logAdd($this->post['token'], [
  881. "order_code" => $orderinfo["orderCode"],//出库单号
  882. "status" => $lor,//这里的status是之前的值
  883. "action_remark" => '',//备注
  884. "action_type" => "status"//新建create,编辑edit,更改状态status
  885. ], "XSQRD", $orderinfo['status'], $orderinfo);
  886. ProcessOrder::AddProcess($this->post['token'], [
  887. "order_type" => 'XSQRD',
  888. "order_code" =>$orderinfo["orderCode"],//出库单号
  889. "order_id" => $orderinfo["id"],
  890. "order_status" =>$orderinfo['status'],"before_status"=>$lor
  891. ]);
  892. if($orderinfo['is_stock'] == 1 || $info['is_th'] == 0){
  893. $wsmcode = $orderinfo['is_stock'] == 1 ? $cgd['wsm_code'] : $info['return_wsm'];
  894. if ($wsmcode == "") {
  895. Db::rollback();
  896. return error_show(1005, '未找到退货仓库');
  897. }
  898. //::todo 非库存品订单退货 采购单退货 虚拟仓如何减库存
  899. $stock = Db::name("good_stock")->where(["spuCode" => $info['good_code'], 'wsm_code' => $wsmcode])->find();
  900. if (empty($stock)) {
  901. $stock = [
  902. "spuCode" => $info['good_code'],
  903. "wsm_code" => $wsmcode,
  904. "usable_stock" => 0,
  905. "wait_out_stock" => 0,
  906. "wait_in_stock" => 0,
  907. "total_stock" => 0,
  908. "addtime" => date("Y-m-d H:i:s"),
  909. "updatetime" => date("Y-m-d H:i:s"),
  910. ];
  911. }
  912. $stock['usable_stock'] += $info['num'];
  913. $stock['updatetime'] = date("Y-m-d H:i:s");
  914. $st_up = Db::name("good_stock")->save($stock);
  915. if ($st_up == false) {
  916. Db::rollback();
  917. return error_show(1005, '可售商品入库失败');
  918. }
  919. $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"];
  920. GoodLog::LogAdd($this->post['token'], $good_data, 'XSTHD');
  921. }
  922. $data=[
  923. "orderCode"=>$info['orderCode'],
  924. "th_type"=>1,
  925. "th_num"=>$info['num'],
  926. "th_fee"=>round($info['num']*$orderinfo['sale_price'],2),
  927. "thCode"=>$info['returnCode'],
  928. "spuCode"=>$orderinfo['good_code'],
  929. "good_name"=>$orderinfo['good_name'],
  930. "cat_id"=>$orderinfo['cat_id'],
  931. "apply_id"=>$info['apply_id'],
  932. "apply_name"=>$info['apply_name'],
  933. "addtime"=>date("Y-m-d H:i:s"),
  934. "status"=>1,
  935. "is_del"=>0
  936. ];
  937. $inse=Db::name("th_data")->insert($data);
  938. if($inse==false){
  939. Db::rollback();
  940. return error_show(1004,"退货单更新失败");
  941. }
  942. }
  943. }
  944. Db::commit();
  945. return app_show(0,"更新成功");
  946. }catch (\Exception $e){
  947. Db::rollback();
  948. return error_show(1004,$e->getMessage()."|".$e->getLine());
  949. }
  950. }
  951. public function zxcreate(){
  952. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  953. if($ordeCode==''){
  954. return error_show(1004,"参数orderCode 不能为空");
  955. }
  956. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  957. if(empty($order)){
  958. return error_show(1005,"未找到订单数据");
  959. }
  960. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  961. if($errorCode==''){
  962. return error_show(1004,"参数errorCode 不能为空");
  963. }
  964. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  965. if(empty($error)){
  966. return error_show(1005,"未找到退货原因数据");
  967. }
  968. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  969. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  970. if($token==''){
  971. return error_show(105,"参数token不能为空");
  972. }
  973. $user =GetUserInfo($token);
  974. if(empty($user)||$user['code']!=0){
  975. return error_show(1002,"申请人数据不存在");
  976. }
  977. $rm= isset($user["data"]['id']) ? $user["data"]['id'] : "";
  978. $ri= isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  979. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  980. if($num==''){
  981. return error_show(1005,"参数num不能为空");
  982. }
  983. if($order['wsend_num']<$num){
  984. return error_show(1002,"仓库未发货数量不足退货");
  985. }
  986. $returnCode=makeNo("RS");
  987. Db::startTrans();
  988. try{
  989. $in = [
  990. "returnCode"=>$returnCode,
  991. "orderCode"=>$ordeCode,
  992. "good_code"=>$order['good_code'],
  993. "good_name"=>$order['good_name'],
  994. "apply_id"=>$rm,
  995. "apply_name"=>$ri,
  996. "error_code"=>$errorCode,
  997. "num"=>$num,
  998. "remark"=>$remark,
  999. "order_type"=>2,
  1000. "status"=>0,
  1001. "is_del"=>0,
  1002. "addtime"=>date("Y-m-d H:i:s"),
  1003. "updatetime"=>date("Y-m-d H:i:s")
  1004. ];
  1005. $create = Db::name("sale_return")->insert($in,true);
  1006. if($create>0) {
  1007. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'XSTHD',"before_status"=>0];
  1008. ProcessOrder::AddProcess($this->post['token'],$process);
  1009. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  1010. "action_type"=>"create"];
  1011. ActionLog::logAdd($this->post['token'],$ste,"XSTHD",3,$in);
  1012. //维护台账记录
  1013. Db::name('standing_book')
  1014. ->where('ordeCode', $ordeCode)
  1015. ->update([
  1016. 'returnGoodCode' => $returnCode,
  1017. 'updatetime' => date("Y-m-d H:i:s")
  1018. ]);
  1019. if ($order['send_type'] == 1) {
  1020. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"=>2])->select();
  1021. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  1022. if(!empty($wsend)){
  1023. foreach ($wsend as $value){
  1024. $tt = $value['status'];
  1025. $value['status']=0;
  1026. $value['updatetime']=date("Y-m-d H:i:s");
  1027. $up =Db::name("order_out")->save($value);
  1028. if($up){
  1029. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD',"before_status"=>$tt];
  1030. ProcessOrder::AddProcess($this->post['token'],$process);
  1031. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  1032. ActionLog::logAdd($this->post['token'],$ste,"CKD",0,$value);
  1033. }else{
  1034. Db::rollback();
  1035. return error_show(1005,"退货单新建失败");
  1036. }
  1037. }
  1038. }
  1039. }
  1040. Db::commit();
  1041. return error_show(0,"退货单新建成功");
  1042. }
  1043. Db::rollback();
  1044. return error_show(1005,"退货单新建失败");
  1045. }catch (\Exception $e){
  1046. Db::rollback();
  1047. return error_show(1005,$e->getMessage());
  1048. }
  1049. }
  1050. }