Reorder.php 74 KB

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