Reorder.php 58 KB

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