Reorder.php 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\DataGroup as DataGroupModel;
  4. use app\admin\model\GoodLog;
  5. use app\admin\model\GoodStockInfo;use app\admin\model\ProcessOrder;
  6. use think\Exception;
  7. use think\facade\Db;
  8. use think\App;
  9. use app\admin\model\ActionLog;
  10. use think\facade\Validate;
  11. //销售单退货
  12. class Reorder extends Base
  13. {
  14. public function __construct(App $app)
  15. {
  16. parent::__construct($app);
  17. }
  18. public function create(){
  19. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  20. if($ordeCode==''){
  21. return error_show(1004,"参数orderCode 不能为空");
  22. }
  23. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  24. if(empty($order)){
  25. return error_show(1005,"未找到订单数据");
  26. }
  27. $retrun =Db::name("sale_return")->where([["orderCode","=",$ordeCode],["is_del","=",0],["status", "in", [1,2,3,7,9,10]]])
  28. ->count();
  29. if($retrun>0){
  30. return error_show(1005,"存在未完成退货订单数据");
  31. }
  32. if($order['order_type']==3){
  33. $goon = Db::name("good_zixun")
  34. ->where(["spuCode"=>$order['good_code'],"is_del"=>0])
  35. ->findOrEmpty();
  36. }else {
  37. $goon = Db::name('good_platform')
  38. ->alias('a')
  39. ->join('good b', 'b.spuCode=a.spuCode', 'left')
  40. ->where(['a.skuCode' => $order['skuCode']])
  41. ->field("b.creater,b.createrid,b.supplierNo")
  42. ->findOrEmpty();
  43. }
  44. if($goon==false){
  45. return error_show(1005,"未找到商品数据");
  46. }
  47. $userCommon = new \app\admin\common\User();
  48. $tmp = $userCommon->handle('sInfo', ['code' => $goon['supplierNo']]);
  49. if (!isset($tmp['code']) || $tmp['code'] != 0) return json_show($tmp['code'], $tmp['message'], $tmp['data']);
  50. $supplier = $tmp['data'];
  51. if (empty($supplier)) return json_show(1005, "未找到商品供应商数据");
  52. // $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  53. // if($supplier==false){
  54. // return error_show(1005,"未找到商品供应商数据");
  55. // }
  56. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  57. if($errorCode==''){
  58. return error_show(1004,"参数errorCode 不能为空");
  59. }
  60. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  61. if(empty($error)){
  62. return error_show(1005,"未找到退货原因数据");
  63. }
  64. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  65. $thnum =isset($this->post['thnum']) &&$this->post['thnum']!=''?intval($this->post['thnum']) :"";
  66. // $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  67. // if($token==''){
  68. // return error_show(105,"参数token不能为空");
  69. // }
  70. // $user =GetUserInfo($token);
  71. // if(empty($user)||$user['code']!=0){
  72. // return error_show(1002,"申请人数据不存在");
  73. // }
  74. $is_addr=0;
  75. $rm= $this->uid;//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  76. $ri= $this->uname;//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  77. $returnadr =isset($this->post['returnAddr'])&& !empty($this->post['returnAddr']) ? $this->post['returnAddr']:"";
  78. if($returnadr!=''){
  79. $thnum=array_sum(array_column($returnadr,"return_num"));
  80. $is_addr=1;
  81. }
  82. $returnCode=makeNo("RN");
  83. Db::startTrans();
  84. try{
  85. $in = [
  86. "returnCode"=>$returnCode,
  87. "orderCode"=>$ordeCode,
  88. "good_code"=>$order['good_code'],
  89. "good_name"=>$order['good_name'],
  90. "apply_id"=>$rm,
  91. "apply_name"=>$ri,
  92. "cgderid"=>$goon['createrid'],
  93. "cgder"=>$goon['creater'],
  94. "person"=>$supplier['person']??'',
  95. "person_id"=>$supplier['personid']??0,
  96. "error_code"=>$errorCode,
  97. "num"=>$thnum,
  98. "total_fee"=>round($order['sale_price']*$thnum,2),
  99. "good_price"=>$order['sale_price'],
  100. "platform_id"=>$order['platform_id'],
  101. "remark"=>$remark,
  102. "order_type"=>$order['order_type'],
  103. "is_addr"=>$is_addr,
  104. "status"=>$order['is_stock']==1?4:1,
  105. "is_del"=>0,
  106. "addtime"=>date("Y-m-d H:i:s"),
  107. "updatetime"=>date("Y-m-d H:i:s")
  108. ];
  109. $create = Db::name("sale_return")->insert($in,true);
  110. if($create>0){
  111. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  112. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$stn,"XSTHD",$in['status'],$in);
  113. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  114. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  115. //维护台账信息
  116. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$ordeCode}'");
  117. if($returnadr!=""){
  118. $inf=[];
  119. foreach ($returnadr as $val){
  120. if ($val['return_num'] == 0) continue;//当退货数量为0时,跳过
  121. $temp=[];
  122. $addrinfo =Db::name("order_addr")->where(['id'=>$val['id'],"orderCode"=>$ordeCode])->find();
  123. if($addrinfo==false){
  124. Db::rollback();
  125. return error_show(1005,"地址信息未找到");
  126. }
  127. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  128. if($send['status']>=2){
  129. Db::rollback();
  130. return error_show(1005,"地址已发货");
  131. }
  132. if($order['is_stock']==1){
  133. if ($addrinfo['receipt_quantity'] < $val['return_num']) {
  134. Db::rollback();
  135. return error_show(1004, "地址发货数量不足");
  136. }
  137. $addrinfo['receipt_quantity'] -= $val['return_num'];
  138. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  139. $addrinfo['updatetime'] = date("Y-m-d H:i:s");
  140. $addrup = Db::name("order_addr")->save($addrinfo);
  141. if ($addrup == false) {
  142. Db::rollback();
  143. return error_show(1004, "地址发货数量更新失败");
  144. }
  145. $out = Db::name("order_out")->where(["addrid"=>$val['id']])->find();
  146. if ($out == false) {
  147. Db::rollback();
  148. return error_show(1004, "地址发货单数据未找到");
  149. }
  150. // if ($send!=false) {
  151. if ($out['status'] >= 2) {
  152. Db::rollback();
  153. return error_show(1004, "地址发货单已发货");
  154. }
  155. if ($out['send_num'] < $val['return_num']) {
  156. Db::rollback();
  157. return error_show(1004, "地址发货单发货数量不足");
  158. }
  159. $out['send_num'] -= $val['return_num'];
  160. $out['is_del'] = $out['send_num']==0?1:0;
  161. $out['updatetime'] = date("Y-m-d H:i:s");
  162. $outup = Db::name("order_out")->save($out);
  163. if ($outup == false) {
  164. Db::rollback();
  165. return error_show(1004, "地址发货单更新失败");
  166. }
  167. // $ordersend = Db::name("order_send")->where(["outCode" => $out['outCode']])->find();
  168. // if ($ordersend['send_num'] < $val['return_num']) {
  169. // Db::rollback();
  170. // return error_show(1004, "发货单发货数量不足");
  171. // }
  172. // $ordersend['send_num'] -= $val['return_num'];
  173. // $ordersend['status'] = $ordersend['send_num'] <= 0 ? 0 : 1;
  174. // $ordersend['updatetime'] = date("Y-m-d H:i:s");
  175. // $sendip = Db::name("order_send")->save($ordersend);
  176. // if ($sendip == false) {
  177. // Db::rollback();
  178. // return error_show(1004, "发货单更新失败");
  179. // }
  180. // }
  181. }
  182. $temp['returnCode']=$returnCode;
  183. $temp['orderCode']=$ordeCode;
  184. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  185. $temp['addrid']=$val['id'];
  186. $temp['send_num']=$addrinfo['receipt_quantity'];
  187. $temp['return_num']=$val['return_num'];
  188. $temp['is_del']=0;
  189. $temp['addtime']=date("Y-m-d H:i:s");
  190. $temp['updatetime']=date("Y-m-d H:i:s");
  191. $inf[]=$temp;
  192. }
  193. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  194. if($inadd==0){
  195. Db::rollback();
  196. return error_show(1005,"退货单新建失败");
  197. }
  198. }
  199. if($order['is_stock']==1){
  200. if ($order['wsend_num'] < $thnum) {
  201. Db::rollback();
  202. return error_show(1004, "销售单未发货数量不足退货");
  203. }
  204. $lor=$order['status'] ;
  205. $order['wsend_num'] -= $thnum;
  206. $order['send_num'] += $thnum;
  207. $order['status'] = $order['wsend_num']==0?2:($order['send_num']==0?0:1);
  208. $order['send_status'] =$order['wsend_num']==0?3:($order['send_num']==0?1:2);
  209. $order['th_num'] += $thnum;
  210. if($order['th_num']==$order['send_num']&& $order['wsend_num']==0){
  211. $order['status']=3;
  212. }
  213. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  214. $order['updatetime'] = date("Y-m-d H:i:s");
  215. $uap = Db::name("sale")->save($order);
  216. if ($uap == false) {
  217. Db::rollback();
  218. return error_show(1005, '销售单订单更新失败');
  219. }
  220. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  221. "order_code" => $order["orderCode"],//出库单号
  222. "status" => $lor,//这里的status是之前的值
  223. "action_remark" => '',//备注
  224. "action_type" => "status"//新建create,编辑edit,更改状态status
  225. ], "XSQRD", $order['status'], $order);
  226. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  227. "order_type" => 'XSQRD',
  228. "order_code" =>$order["orderCode"],//出库单号
  229. "order_id" => $order["id"],
  230. "order_status" =>$order['status'],"before_status"=>$lor
  231. ]);
  232. // $ordernum = Db::name("order_num")->where(['orderCode' => $ordeCode])->find();
  233. // if ($ordernum == false) {
  234. // Db::rollback();
  235. // return error_show(1005, '未找到关联采购单');
  236. // }
  237. // $ordernum['send_num'] -= $thnum;
  238. // $orderup = Db::name("order_num")->save($ordernum);
  239. // if ($orderup == false) {
  240. // Db::rollback();
  241. // return error_show(1005, '关联数据更新失败');
  242. // }
  243. // $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  244. // if ($cgd == false) {
  245. // Db::rollback();
  246. // return error_show(1005, '未找到采购单数据');
  247. // }
  248. // $cgd['th_fee'] += round($cgd['good_price'] * $thnum, 2);
  249. // $cgd['th_num'] += $thnum;
  250. // $cgd['updatetime'] = date("Y-m-d H:i:s");
  251. // $cgdup = Db::name("purchease_order")->save($cgd);
  252. // if ($cgdup == false) {
  253. // Db::rollback();
  254. // return error_show(1005, '采购单数据更新失败');
  255. // }
  256. // if ($cgd['bkcode'] != "") {
  257. // $bk = Db::name("purchease_order")->where(["bkcode" => $cgd['bkcode'], "order_type" => 1, "order_source" =>0, "is_del" => 0])
  258. // ->find();
  259. // if ($bk == false) {
  260. // Db::rollback();
  261. // return error_show(1005, '未找到备库单数据');
  262. // }
  263. // $orderbk = Db::name("order_bk")->where(['cgdNo' => $bk['cgdNo'], "is_del" => 0])->find();
  264. // if ($orderbk == false) {
  265. // Db::rollback();
  266. // return error_show(1005, '备库单未完全入库');
  267. // }
  268. // $merge_num = Db::name("purchease_order")->where(["bkcode" => $bk['bkcode'], "order_type" =>
  269. // 1, "is_del" => 0])->where("order_source","<>",0)->field("sum(send_num)-sum(th_num) as num")
  270. // ->find();
  271. //
  272. // $orderbk['balance_num'] = $orderbk['total_num'] - $merge_num['num'];
  273. // $orderbk['merge_num'] = $merge_num['num'];
  274. // $orderbk['updatetime'] = date("Y-m-d H:i:s");
  275. // $orderbkup = Db::name("order_bk")->save($orderbk);
  276. // if ($orderbkup == false) {
  277. // Db::rollback();
  278. // return error_show(1005, '备库单库存数据释放失败');
  279. // }
  280. // }
  281. $saleinfo=Db::name("sale_info")->where([["orderCode","=",$ordeCode],["num",">",0]])->select()
  282. ->toArray();
  283. if(empty($saleinfo)) {
  284. $ordernum = Db::name("order_num")->where(['orderCode' => $ordeCode])->find();
  285. if ($ordernum == false) {
  286. Db::rollback();
  287. return error_show(1005, '未找到关联采购单');
  288. }
  289. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  290. if ($cgd == false) {
  291. Db::rollback();
  292. return error_show(1005, '未找到采购单数据');
  293. }
  294. $bn =makeNo("BN");
  295. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'wsm_code'=>$cgd['wsm_code'],"is_del"=>0,"status"=>1])->find();
  296. if($stock==false){
  297. Db::rollback();
  298. return error_show(1005, '商品库存数据未找到');
  299. }
  300. $stock['usable_stock']+=$thnum;
  301. $stock['wait_out_stock'] -=$thnum;
  302. $stock['updatetime'] = date("Y-m-d H:i:s");
  303. $st_up = Db::name("good_stock")->save($stock);
  304. if ($st_up == false) {
  305. return error_show(1005, '可售商品入库失败');
  306. }
  307. $yp=GoodStockInfo::AddBn($stock['id'],$bn,$thnum,$cgd['good_price']);
  308. if($yp==false){
  309. Db::rollback();
  310. return error_show(1005, '商品批次退货入库失败');
  311. }
  312. }else{
  313. $tempnum =$thnum;
  314. foreach ( $saleinfo as $va){
  315. if($tempnum ==0) break;
  316. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'id' => $va['stockid']])
  317. ->find();
  318. if($stock==false) {
  319. Db::rollback();
  320. return error_show(1005, '商品库存数据未找到');
  321. }
  322. if($va['num']>=$tempnum) {
  323. $tnm = $tempnum;
  324. $va['num']-= $tempnum;
  325. $va['th_num']+= $tempnum;
  326. $tempnum=0;
  327. }else{
  328. $tnm = $va['num'];
  329. $tempnum -=$va['num'];
  330. $va['th_num']+= $va['num'];
  331. $va['num'] =0;
  332. }
  333. $stock['usable_stock']+=$tnm;
  334. $stock['wait_out_stock'] -=$tnm;
  335. $stock['updatetime'] = date("Y-m-d H:i:s");
  336. $st_up = Db::name("good_stock")->save($stock);
  337. if ($st_up == false) {
  338. return error_show(1005, '可售商品入库失败');
  339. }
  340. $ps = GoodStockInfo::AddBn($va['stockid'],$va['bnCode'],$tnm);
  341. if ($ps == false) {
  342. return error_show(1005, '商品批次退货入库失败');
  343. }
  344. $ret = GoodStockInfo::ReturnBn($returnCode,$va['id'],$tnm);
  345. if ($ret == false) {
  346. return error_show(1005, '商品批次退货入库失败');
  347. }
  348. $va['updatetime']=date("Y-m-d H:i:s");
  349. $sal= Db::name("sale_info")->save($va);
  350. if ($sal == false) {
  351. return error_show(1005, '商品批次退货入库失败');
  352. }
  353. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 1, 'stock' => $thnum, "stock_name" => "usable_stock"];
  354. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 2, 'stock' => $thnum, "stock_name" => "wait_out_stock"];
  355. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname], $good_data, 'XSTHD');
  356. }
  357. }
  358. $data=[
  359. "orderCode"=>$ordeCode,
  360. "th_type"=>1,
  361. "th_num"=>$thnum,
  362. "th_fee"=>round($order['sale_price']*$thnum,2),
  363. "thCode"=>$returnCode,
  364. "spuCode"=>$order['good_code'],
  365. "good_name"=>$order['good_name'],
  366. "cat_id"=>$order['cat_id'],
  367. "apply_id"=>$rm,
  368. "apply_name"=>$ri,
  369. "addtime"=>date("Y-m-d H:i:s"),
  370. "status"=>1,
  371. "is_del"=>0
  372. ];
  373. $inse=Db::name("th_data")->insert($data);
  374. if($inse==false){
  375. Db::rollback();
  376. return error_show(1004,"退货单更新失败");
  377. }
  378. }
  379. Db::commit();
  380. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  381. }
  382. Db::rollback();
  383. return error_show(1005,"退货单新建失败");
  384. }catch (\Exception $e){
  385. Db::rollback();
  386. return error_show(1005,$e->getMessage());
  387. }
  388. }
  389. //退货单列表
  390. public function list(){
  391. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  392. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  393. $where =[['sr.is_del',"=",0]];
  394. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  395. if($bkcode!=""){
  396. $where[]=['sr.returnCode',"like", "%{$bkcode}%"];
  397. }
  398. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  399. if($status!==""){
  400. $where[]=['sr.status',"=", $status];
  401. }
  402. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode']) :"";
  403. if($orderCode!=""){
  404. $where[]=['sr.orderCode',"like", "%{$orderCode}%"];
  405. }
  406. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name']) :"";
  407. if($apply_name!=""){
  408. $where[]=['sr.apply_name',"like", "%{$apply_name}%"];
  409. }
  410. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  411. if($start!==""){
  412. $where[]=['sr.addtime',">=", $start.' 00:00:00'];
  413. }
  414. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  415. if($end!==""){
  416. $where[]=['sr.addtime',"<=", $end.' 23:59:59'];
  417. }
  418. //商品成本编码搜索
  419. $good_code = isset($this->post['good_code']) && $this->post['good_code'] != "" ? trim($this->post['good_code']) : "";
  420. if ($good_code != "") {
  421. $where[] = ['sr.good_code', "like", "%{$good_code}%"];
  422. }
  423. //商品上线编码搜索
  424. $skuCode = isset($this->post['skuCode']) && $this->post['skuCode'] != "" ? trim($this->post['skuCode']) : "";
  425. if ($skuCode != "") {
  426. $where[] = ['b.skuCode', "like", "%{$skuCode}%"];
  427. }
  428. $relaComNo = isset($this->post['relaComNo']) && $this->post['relaComNo'] != "" ? trim($this->post['relaComNo']) : "";
  429. if ($relaComNo != "") $where[] = ['b.supplierNo', '=', $relaComNo];
  430. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] != "" ? trim($this->post['supplierNo']) : "";
  431. if ($supplierNo !== "") {
  432. $spuCode = Db::name('good_basic')
  433. ->where(['is_del' => 0, 'supplierNo' => $supplierNo])
  434. ->column('spuCode');
  435. $where[] = ['b.good_code', "in", $spuCode];
  436. }
  437. $order_source = $order_source = isset($this->post['order_source']) && $this->post['order_source'] != "" ? trim($this->post['order_source']) : "";
  438. if ($order_source !== "") $where[] = ['b.order_source', "=", $order_source];
  439. $condition=[];
  440. // $role=$this->checkRole();
  441. // if(!empty($role['write']) && $this->uid!=""){
  442. // // $where[]=["sr.apply_id","in",$role['write']];
  443. // $condition .="sr.cgderid = {$this->uid} or sr.apply_id in (".implode(',',$role['write']).")";
  444. // }
  445. //只有level2的账号过滤数据权限
  446. if ($this->level == 2) {
  447. $role = $this->checkDataShare();
  448. $hand = resign_hand_user($this->uid, 0);
  449. if (!empty($role[DataGroupModel::$type_全部])) {
  450. $arr = array_unique(array_merge($hand, $role[DataGroupModel::$type_全部]));
  451. // $uidim =implode(",",$hand);
  452. // $condition .= "sr.cgderid in ($uidim) or sr.person_id in ($uidim) or sr.apply_id in (" . implode(',',$arr) .
  453. // ")";
  454. $condition[] = ["sr.apply_id", "in", $arr];
  455. $condition[] = ["sr.cgderid", "in", $hand];
  456. $condition[] = ["sr.person_id", "in", $hand];
  457. }
  458. }
  459. //供应商账号不允许看到库存品数据
  460. if ($this->level == 3) $where[]=['b.is_stock','<>',1];
  461. // $role = $this->checkDataShare();
  462. // if (!empty($role[DataGroupModel::$type_全部])) $condition .= "sr.cgderid = {$this->uid} or sr.person_id = {$this->uid} or sr.apply_id in (" . implode(',', $role[DataGroupModel::$type_全部]) . ")";
  463. if(in_array($this->roleid,config('app.wsm_cgder_role'))){
  464. $where[]=["b.order_type","=",1];
  465. }
  466. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  467. if ($company_name !== "") $where[] = ["sr.apply_id", 'in', get_company_item_user_by_name($company_name)];
  468. $customer_code = trim($this->post['customer_code']??'');
  469. $where[]=['b.customer_code', "like", "%{$customer_code}%"];
  470. $count=Db::name("sale_return")
  471. ->alias('sr')
  472. ->leftJoin("sale b", "b.orderCode=sr.orderCode AND b.is_del=0")
  473. ->where($where)
  474. ->where(function ($query)use ($condition){$query->whereOr($condition);})
  475. ->count('sr.id');
  476. $total = ceil($count/$size);
  477. $page = $total>=$page ? $page :$total;
  478. $list = Db::name("sale_return")
  479. ->alias('sr')
  480. ->field('sr.*,b.skuCode,b.sale_price,b.good_num total_num,b.customer_code,b.supplierNo ,b.order_source')
  481. ->leftJoin("sale b", "b.orderCode=sr.orderCode AND b.is_del=0")
  482. ->where($where)
  483. ->where(function ($query)use ($condition){$query->whereOr($condition);})
  484. ->order("sr.addtime desc")
  485. ->page($page,$size)
  486. ->select()
  487. ->toArray();
  488. // echo Db::name("sale_return")->getLastSql();
  489. $all_createrid = array_column($list,'apply_id');
  490. $item = get_company_name_by_uid($all_createrid);
  491. $userCommon = new \app\admin\common\User();
  492. $names = $userCommon->handle('getCodeAndName',['code'=>array_merge(array_column($list,'supplierNo'),array_column($list,'customer_code'))]);
  493. $data=[];
  494. foreach ($list as $value){
  495. $value['error_msg']='';
  496. if($value['error_code']!=''){
  497. $error = Db::name("result_info")->where(["result_code"=>$value['error_code']])->find();
  498. $value['error_msg']= isset($error['result'])?$error['result']:"";
  499. }
  500. // $order =Db::name("sale")->where(["orderCode"=>$value['orderCode'],"is_del"=>0])->find();
  501. // $value['sale_price'] = isset($order['sale_price']) ?$order['sale_price']:0;
  502. $value['return_total'] =$value['sale_price']*$value['num'] ;
  503. // $value['total_num'] =$order['good_num'] ;
  504. $value['company_name'] = $item[$value['apply_id']]??'';
  505. $value['supplierName'] = $names['data'][$value['supplierNo']] ?? '';//isset($wsm['company']) ? $wsm['company'] : "";
  506. $value['customerName'] = $names['data'][$value['customer_code']] ?? '';//isset($wsm['company']) ? $wsm['company'] : "";
  507. //是否具有编辑权限
  508. // $value['is_allow_update'] = 0;
  509. // if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  510. $data[]=$value ;
  511. }
  512. return app_show(0,"获取成功",["count"=>$count,'list'=>$data]);
  513. }
  514. public function info(){
  515. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode']) :"";
  516. if($code==""){
  517. return error_show(1004,"参数returnCode不能为空");
  518. }
  519. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  520. if(empty($info)){
  521. return error_show(1004,"未找到退货数据");
  522. }
  523. $orderinfo = Db::name("sale")->where(["orderCode"=>$info["orderCode"]])->find();
  524. if($orderinfo['order_type']==3){
  525. $goon = Db::name("good_zixun")->where(["spuCode"=>$orderinfo['good_code'],"is_del"=>0])->find();
  526. }else {
  527. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  528. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  529. }
  530. if ($goon==false) {
  531. return error_show(1003, "未找到商品数据");
  532. }
  533. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  534. $info['is_stock']=isset($goon['is_stock'])?$goon['is_stock']:'0';
  535. $info['good_code'] = isset($orderinfo['good_code'])?$orderinfo['good_code']:'';
  536. $info['send_type'] = isset($orderinfo['send_type'])?$orderinfo['send_type']:'';
  537. $info['skuCode'] = isset($orderinfo['skuCode'])?$orderinfo['skuCode']:'';
  538. $info['good_name'] = isset($orderinfo['good_name'])?$orderinfo['good_name']:'';
  539. $info['good_num'] = isset($orderinfo['good_num'])?$orderinfo['good_num']:'';
  540. $info['sale_price'] = isset($orderinfo['sale_price'])?$orderinfo['sale_price']:'0';
  541. $info['origin_price'] = isset($orderinfo['origin_price'])?$orderinfo['origin_price']:'0';
  542. $info['return_total'] = $info['sale_price']*$info['num'] ;
  543. $info['send_num'] = isset($orderinfo['send_num'])?$orderinfo['send_num']:'0';
  544. $info['wsend_num'] = isset($orderinfo['wsend_num'])?$orderinfo['wsend_num']:'0';
  545. $info['send_status'] = isset($orderinfo['send_status'])?$orderinfo['send_status']:'';
  546. $info['total_price'] = isset($orderinfo['total_price'])?$orderinfo['total_price']:'0';
  547. $info['post_fee'] = isset($orderinfo['post_fee'])?$orderinfo['post_fee']:'0';
  548. $info['customer_code'] = isset($orderinfo['customer_code'])?$orderinfo['customer_code']:'';
  549. $userCommon = new \app\admin\common\User();
  550. if($info['return_wsm']!=""){
  551. $wsmcode = Db::name("warehouse_info")
  552. ->alias("a")
  553. // ->leftJoin("supplier b","a.supplierNo=b.code")
  554. ->where(["a.wsm_code"=>$info['return_wsm']])
  555. ->field("a.name as wsm_name,a.supplierNo")
  556. ->find();
  557. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  558. }
  559. $tmp = $userCommon->handle('getCodeAndName', ['code' => [
  560. $orderinfo['supplierNo'],
  561. $orderinfo['customer_code'],
  562. $wsmcode['supplierNo']??'',
  563. ]]);
  564. if(!empty($wsmcode['supplierNo'])){
  565. $info['wsm_supplier'] =$tmp['data'][$wsmcode['supplierNo']]??'';//isset($wsmcode['name']) ? $wsmcode['name']:"";
  566. $info['wsm_supplierNo'] =$wsmcode['supplierNo']??'';//isset($wsmcode['code']) ? $wsmcode['code']:"";
  567. }
  568. $info['customer_name']='';
  569. if(isset($orderinfo['customer_code'])&&$orderinfo['customer_code']!=''){
  570. // $customerinfo = Db::name("customer_info")->where(['companyNo'=>$orderinfo['customer_code']])->find();
  571. $info['customer_name'] = $tmp['data'][$orderinfo['customer_code']]??'';//isset($customerinfo['companyName']) ? $customerinfo['companyName']:"";
  572. }
  573. $info['supplierNo'] = isset($orderinfo['supplierNo'])?$orderinfo['supplierNo']:'';
  574. $info['supplier_name']='';
  575. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  576. // $customerinfo = Db::name("business")->where(['companyNo'=>$orderinfo['supplierNo']])->find();
  577. // $info['supplier_name'] = isset($customerinfo['company']) ? $customerinfo['company']:"";
  578. $info['supplier_name'] = $tmp['data'][$orderinfo['supplierNo']] ?? '';
  579. }
  580. $info['platform_name']='';
  581. $info['platform_id']=$orderinfo['platform_id'];
  582. if($orderinfo['platform_id']!=0){
  583. $plat=Db::name("platform")->where(['id'=>$orderinfo['platform_id']])->find();
  584. $info['platform_name']=isset($plat['platform_name'])?$plat['platform_name']:"";
  585. }
  586. $info['cgd_wsend']="";
  587. $info['cgd_send']="";
  588. $info['cgd_total']="";
  589. if($orderinfo['order_type']==2){
  590. $cgd = Db::name("purchease_order")->where(["bkcode"=>$info["orderCode"],"order_type"=>2])->find();
  591. $info['cgd_wsend'] = isset($cgd['wsend_num']) ? $cgd['wsend_num']:0;
  592. $info['cgd_send'] = isset($cgd['send_num']) ? $cgd['send_num']:0;
  593. $info['cgd_total'] = isset($cgd['good_num']) ? $cgd['good_num']:0;
  594. }
  595. $info['error_msg']='';
  596. if($info['error_code']!=''){
  597. $error = Db::name("result_info")->where(["result_code"=>$info['error_code']])->find();
  598. $info['error_msg']= isset($error['result'])?$error['result']:"";
  599. }
  600. $wsm_return = Db::name("sale_returninfo")->where(["returnCode"=>$info["returnCode"],"is_del"=>0])->select()->toArray();
  601. $wsm=[];
  602. if(!empty($wsm_return)){
  603. foreach ($wsm_return as $value){
  604. $value['wsm_name']="";
  605. $value['wsm_supplier']="";
  606. $value['wsm_supplierNo']="";
  607. if($value['wsm_code']!=""){
  608. $wsmcode = Db::name("warehouse_info")
  609. ->alias("a")
  610. ->leftJoin("supplier b","a.supplierNo=b.code")
  611. ->where(["a.wsm_code"=>$value['wsm_code']])
  612. ->field("a.name as wsm_name,b.name,b.code")
  613. ->find();
  614. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  615. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  616. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  617. }
  618. $orderwsm = Db::name("sale_info")->where(["orderCode"=>$info["orderCode"],"wsm_code"=>$value["wsm_code"]])->find();
  619. $value["wsm_total"] = isset($orderwsm["num"]) ? $orderwsm["num"]:0;
  620. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $info['orderCode']])->sum("send_num");
  621. $value['wsm_send'] = $send ?? 0;
  622. $value['wsm_wsend'] = $value['wsm_total'] - $value['wsm_send'];
  623. $wsm[]=$value;
  624. }
  625. }
  626. $info['wsminfo']=$wsm;
  627. $addr =Db::name("sale_returnaddr")
  628. ->alias('a')
  629. ->field('a.*,b.addr,b.addr_code,b.contactor,b.mobile,b.post_fee,b.arrive_time,b.customer_code,b.receipt_quantity,b.orderCode')
  630. ->leftJoin('order_addr b','b.id=a.addrid')
  631. ->where(["a.returnCode"=>$info["returnCode"],"a.is_del"=>0])
  632. ->select()
  633. ->toArray();
  634. $addrinfo=[];
  635. if(!empty($addr)){
  636. $customer_name = $userCommon->handle('getCodeAndName',['code'=>array_column($addr,'customer_code')]);
  637. foreach ( $addr as $value){
  638. // $addrlist = Db::name("order_addr")->where(["id"=>$value["addrid"]])->find();
  639. $value['addr']=isset($value['addr'])?$value['addr']:"";
  640. $value['addr_code']=isset($value['addr_code'])?$value['addr_code']:"";
  641. $value['contactor']=isset($value['contactor'])?$value['contactor']:"";
  642. $value['mobile']=isset($value['mobile'])?$value['mobile']:"";
  643. $value['post_fee']=isset($value['post_fee'])?$value['post_fee']:"";
  644. $value['addive_time']=isset($value['addive_time'])?$value['addive_time']:"";
  645. $value['customer_code']=isset($value['customer_code']) ?$value['customer_code']:"" ;
  646. $value['receipt_quantity']=isset($value['receipt_quantity']) ?$value['receipt_quantity']:"" ;
  647. $send = Db::name("order_out")->where(['addrid' => $value['addrid'], 'orderCode' => $value['orderCode'],"is_del"=>0])->where("status",">=",2)->sum("send_num");
  648. $value['addr_send'] = $send ?? 0;
  649. $value['addr_wsend'] = $value['receipt_quantity'] - $value['addr_send'];
  650. // $customer = Db::name("customer_info")->where(['companyNo'=>$addrlist['customer_code']])->find();
  651. $value['customer_name'] = $customer_name['data'][$value['customer_code']]??'';//isset($customer['companyName']) ? $customer['companyName']:"";
  652. $addrinfo[]=$value;
  653. }
  654. }
  655. $info['addrinfo']=$addrinfo;
  656. $info['can']=$int;
  657. return app_show(0,"获取成功",$info);
  658. }
  659. /**
  660. * @return \think\response\Json|void
  661. * @throws \think\db\exception\DataNotFoundException
  662. * @throws \think\db\exception\DbException
  663. * @throws \think\db\exception\ModelNotFoundException
  664. */
  665. public function delete(){
  666. $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode'])
  667. :"";
  668. if($code==""){
  669. return error_show(1004,"参数returnCode不能为空");
  670. }
  671. $info = Db::name("sale_return")->where(["returnCode"=>$code,"is_del"=>0])->find();
  672. if(empty($info)){
  673. return error_show(1004,"未找到退货数据");
  674. }
  675. $info["is_del"]=1;
  676. $info["updatetime"]=date("Y-m-d H:i:s");
  677. $del = Db::name("sale_return")->save($info);
  678. if($del){
  679. $ste = ["order_code"=>$code,"status"=>0,"action_remark"=>'',"action_type"=>"delete"];
  680. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$ste,"XSTHD",0,$ste);
  681. $process=["order_code"=>$code,"order_id"=>$info['id'],"order_status"=>0,"order_type"=>"XSTHD"];
  682. ProcessOrder::workdel($process);
  683. return app_show(0,"删除成功");
  684. }else{
  685. return error_show(1004,"删除失败");
  686. }
  687. }
  688. /**审核
  689. * 1待业务审批
  690. * 2待专员审批(该节点废除)
  691. * 3待主管审批(该节点废除)
  692. * 4退货完成
  693. * 5业务驳回
  694. * 6采购驳回(该节点废除)
  695. * 7专员审批不通过(该节点废除)
  696. * 9待供应商审核
  697. * 8供应商已驳回待采购审核
  698. * 10业务公司修改待供应商确认
  699. */
  700. public function exam()
  701. {
  702. $param = $this->request->only(['returnCode', 'status', 'remark' => '', 'return_wsm' => ''], 'post', 'trim');
  703. $val = Validate::rule([
  704. 'returnCode|退货单编号' => 'require',
  705. 'status|状态' => 'require|in:5,9,8,10,4',
  706. 'remark|备注' => 'requireIf:status,5|requireIf:status,8|requireIf:status,10',
  707. ]);
  708. if ($val->check($param) == false) return json_show(1004, $val->getError());
  709. $code = $param['returnCode'];
  710. $status = $param['status'];
  711. $remark = $param['remark'];
  712. // $code = isset($this->post['returnCode']) && $this->post['returnCode'] !=="" ? trim($this->post['returnCode']):"";
  713. // if($code==""){
  714. // return error_show(1004,"参数returnCode不能为空");
  715. // }
  716. $info = Db::name("sale_return")->where(["returnCode" => $code, "is_del" => 0])->findOrEmpty();
  717. if (empty($info)) return error_show(1004, "未找到退货数据");
  718. switch ($info['status']) {
  719. case 1:
  720. if (in_array($param['status'], [5, 9]) == false) return json_show(1004, '选项错误');
  721. break;
  722. case 9:
  723. if (in_array($param['status'], [4, 8]) == false) return json_show(1004, '选项错误');
  724. break;
  725. case 8:
  726. if (in_array($param['status'], [4, 10]) == false) return json_show(1004, '选项错误');
  727. if ($param['status'] == 4) {
  728. if ($param['return_wsm'] == '') return json_show(1004, '仓库编码不能为空');
  729. $tmp = Db::name("warehouse_info")
  730. ->field('id')
  731. ->where(['wsm_code' => $param['return_wsm']])
  732. ->findOrEmpty();
  733. if (empty($tmp)) return json_show(1004, '未找到仓库数据');
  734. else $info['return_wsm'] = $param['return_wsm'];
  735. }
  736. break;
  737. case 10:
  738. if (in_array($param['status'], [4, 8]) == false) return json_show(1004, '选项错误');
  739. if ($param['status'] == 8) $info['loop_total'] += 1;
  740. break;
  741. }
  742. //当处于以下节点时,level2账号必须是供应商负责人操作,level3账号不做限制
  743. if(in_array($info['status'],[9,10]) && ($this->level==2) && ($this->uid!=$info['person_id'])) return json_show(1004,'您不是供应商负责人,此时无权操作');
  744. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']):"";
  745. // if($status===""){
  746. // return error_show(1004,"参数status不能为空");
  747. // }
  748. // $remark = isset($this->post['remark']) && $this->post['remark'] !="" ? trim($this->post['remark']) :"";
  749. $var = $info['status'];
  750. $orderinfo = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->findOrEmpty();
  751. if (empty($orderinfo)) return error_show(1004, "未找到订单数据");
  752. if ($status == 4) {
  753. if ($info['is_addr'] == 1) {
  754. $addr = Db::name("sale_returnaddr")
  755. ->where(['returnCode' => $info['returnCode'], "is_del" => 0])
  756. ->select()
  757. ->toArray();
  758. if (empty($addr)) return error_show(1004, "未找到发货单地址数据");
  759. }
  760. }
  761. // if($status==3){
  762. // $is_th =isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  763. // if($is_th===""){
  764. // return error_show(1004,"参数is_th不能为空");
  765. // }
  766. // $return_wsm =isset($this->post['return_wsm'])&&$this->post['return_wsm']!=="" ? trim($this->post['return_wsm']):"";
  767. // if($is_th==0){
  768. // if($return_wsm===""){
  769. // return error_show(1004,"参数return_wsm 不能为空");
  770. // }
  771. // $wsmcode = Db::name("warehouse_info")->where(['wsm_code'=>$return_wsm])->find();
  772. // if($wsmcode==false){
  773. // return error_show(1004,"为找到仓库数据");
  774. // }
  775. // }
  776. // $info['return_wsm'] =$return_wsm ;
  777. // $info['is_th'] =$is_th ;
  778. // }
  779. if ($info['status'] == 9 && $param['status'] == 4) $info['is_th'] = 1;
  780. Db::startTrans();
  781. try {
  782. $date = date("Y-m-d H:i:s");
  783. $userCommon = new \app\admin\common\User();
  784. $temp = $info['status'];
  785. $info['status'] = $status;
  786. $remark != "" ? $info['remark'] = $remark : "";
  787. $info["updatetime"] = $date;
  788. $up = Db::name("sale_return")->save($info);
  789. if ($up) {
  790. //如果是节点2(待专员审核),要将待办数据推给供应商负责人
  791. if ($info['status'] == 2) $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "order_type" => "XSTHD", 'before_status' => $temp, 'wait_id' => $info['person_id'], 'wait_name' => $info['person']];
  792. else $process = ["order_code" => $code, "order_id" => $info['id'], "order_status" => $status, "order_type" => "XSTHD", 'before_status' => $temp, 'holder_id' => $info['apply_id']];
  793. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $process);
  794. $ste = ["order_code" => $code, "status" => $temp, "action_remark" => '', "action_type" => "status"];
  795. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $ste, "XSTHD", $status, $info);
  796. if ($status == 4) {
  797. if ($info['is_addr'] == 1) {
  798. if (isset($addr) && !empty($addr)) {
  799. foreach ($addr as $value) {
  800. $addrinfo = Db::name("order_addr")
  801. ->where(['id' => $value['addrid'], "is_del" => 0])
  802. ->find();
  803. if ($addrinfo == false) {
  804. Db::rollback();
  805. return error_show(1004, "地址数据未找到");
  806. }
  807. if ($addrinfo['receipt_quantity'] < $value['return_num']) {
  808. Db::rollback();
  809. return error_show(1004, "地址发货数量不足");
  810. }
  811. $addrinfo['receipt_quantity'] -= $value['return_num'];
  812. $addrinfo['is_del'] = $addrinfo['receipt_quantity'] <= 0 ? 1 : 0;
  813. $addrinfo['updatetime'] = $date;
  814. $addrup = Db::name("order_addr")->save($addrinfo);
  815. if ($addrup == false) throw new Exception('地址发货数量更新失败');
  816. if ($value['outCode'] != "") {
  817. $out = Db::name("order_out")->where(["outCode" => $value['outCode']])->findOrEmpty();
  818. if (empty($out)) throw new Exception('地址发货单数据未找到');
  819. if ($out['status'] >= 2) throw new Exception('地址发货单已发货');
  820. if ($out['send_num'] < $value['return_num']) throw new Exception('地址发货单发货数量不足');
  821. $out['send_num'] -= $value['return_num'];
  822. $out['is_del'] = $out['send_num'] <= 0 ? 1 : 0;
  823. $out['updatetime'] = $date;
  824. $outup = Db::name("order_out")->save($out);
  825. if ($outup == false) throw new Exception('地址发货单更新失败');
  826. }
  827. }
  828. }
  829. }
  830. if ($orderinfo['wsend_num'] < $info['num']) throw new Exception('销售单未发货数量不足退货');
  831. $lor = $orderinfo['status'];
  832. $orderinfo['wsend_num'] -= $info['num'];
  833. $orderinfo['send_num'] += $info['num'];
  834. $orderinfo['status'] = $orderinfo['wsend_num'] == 0 ? 2 : ($orderinfo['send_num'] == 0 ? 0 : 1);
  835. $orderinfo['send_status'] = $orderinfo['wsend_num'] == 0 ? 3 : ($orderinfo['send_num'] == 0 ? 1 : 2);
  836. if ($orderinfo['is_stock'] == 1) {
  837. $orderinfo['th_num'] += $info['num'];
  838. if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
  839. $orderinfo['status'] = 3;
  840. }
  841. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  842. $orderinfo['updatetime'] = $date;
  843. $uap = Db::name("sale")->save($orderinfo);
  844. if ($uap == false) throw new Exception('销售单订单更新失败');
  845. } else {
  846. if ($info['is_th'] == 1) {
  847. $orderinfo['th_num'] += $info['num'];
  848. if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
  849. $orderinfo['status'] = 3;
  850. }
  851. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  852. $orderinfo['updatetime'] = $date;
  853. $uap = Db::name("sale")->save($orderinfo);
  854. if ($uap == false) throw new Exception('销售单订单更新失败');
  855. $ordernum = Db::name("order_num")
  856. ->where(['orderCode' => $orderinfo['orderCode']])
  857. ->findOrEmpty();
  858. if (empty($ordernum)) throw new Exception('未找到关联采购单');
  859. $ordernum['send_num'] -= $info['num'];
  860. $orderup = Db::name("order_num")->save($ordernum);
  861. if ($orderup == false) throw new Exception('关联数据更新失败');
  862. $cgd = Db::name("purchease_order")
  863. ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
  864. ->findOrEmpty();
  865. if ($cgd == false) throw new Exception('未找到采购单数据');
  866. if ($info['is_all'] == 1 && $cgd['send_status'] != 1) throw new Exception('采购单已入库无法全部退货');
  867. $lor = $cgd['status'];
  868. if ($cgd['wsend_num'] < $info['num']) {
  869. $cgd['send_num'] += $cgd['wsend_num'] == 0 ? 0 : ($info['num'] - $cgd['wsend_num']);
  870. $cgd['wsend_num'] = 0;
  871. } else {
  872. $cgd['wsend_num'] -= $info['num'];
  873. $cgd['send_num'] += $info['num'];
  874. }
  875. $cgd['status'] = $cgd['wsend_num'] == 0 ? 2 : ($cgd['send_num'] == 0 ? 0 : 1);
  876. $cgd['send_status'] = $cgd['wsend_num'] == 0 ? 3 : ($cgd['send_num'] == 0 ? 1 : 2);
  877. $cgd['th_num'] += $info['num'];
  878. if ($cgd['th_num'] == $cgd['send_num'] && $cgd['wsend_num'] == 0) {
  879. $cgd['status'] = 4;
  880. }
  881. $cgd['th_fee'] += round($info['num'] * $cgd['good_price'], 2);
  882. $cgd['updatetime'] = $date;
  883. $cgdup = Db::name("purchease_order")->save($cgd);
  884. if ($cgdup == false) throw new Exception('采购单数据更新失败');
  885. $stock = Db::name("good_stock")
  886. ->where(['is_del' => 0, "spuCode" => $info['good_code'], 'wsm_code' => $cgd['wsm_code']])
  887. ->findOrEmpty();
  888. if (empty($stock)) throw new Exception('商品仓库未找到');
  889. if ($stock['wait_in_stock'] < $info['num']) {
  890. $stock['wait_in_stock'] = 0;
  891. if ($stock['usable_stock'] > $info['num'] - $stock['wait_in_stock']) {
  892. $stock['usable_stock'] -= $info['num'] - $stock['wait_in_stock'];
  893. } else {
  894. $stock['usable_stock'] = 0;
  895. $stock['wait_out_stock'] -= $info['num'] - $stock['wait_in_stock'] - $stock['usable_stock'];
  896. }
  897. $stock['total_stock'] = $stock['usable_stock'] + $stock['wait_out_stock'];
  898. } else {
  899. $stock['wait_in_stock'] -= $info['num'];
  900. }
  901. $stock['updatetime'] = $date;
  902. $st_up = Db::name("good_stock")->save($stock);
  903. if ($st_up == false) throw new Exception('可售商品入库失败');
  904. } else {
  905. $orderinfo['th_num'] += $info['num'];
  906. if ($orderinfo['th_num'] == $orderinfo['send_num'] && $orderinfo['wsend_num'] == 0) {
  907. $orderinfo['status'] = 3;
  908. }
  909. $orderinfo['th_fee'] += round($info['num'] * $orderinfo['sale_price'], 2);
  910. $orderinfo['updatetime'] = $date;
  911. $uap = Db::name("sale")->save($orderinfo);
  912. if ($uap == false) throw new Exception('销售单订单更新失败');
  913. $ordernum = Db::name("order_num")
  914. ->where(['orderCode' => $orderinfo['orderCode']])
  915. ->findOrEmpty();
  916. if (empty($ordernum)) throw new Exception('未找到关联采购单');
  917. $ordernum['send_num'] -= $info['num'];
  918. $ordernum['wsend_num'] = $info['num'];
  919. $orderup = Db::name("order_num")->save($ordernum);
  920. if ($orderup == false) throw new Exception('关联数据更新失败');
  921. $cgd = Db::name("purchease_order")
  922. ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
  923. ->findOrEmpty();
  924. if (empty($cgd)) throw new Exception('未找到采购单数据');
  925. $stock = Db::name("good_stock")
  926. ->where(['is_del' => 0, "spuCode" => $orderinfo['good_code'], 'wsm_code' => $cgd['wsm_code']])
  927. ->findOrEmpty();
  928. if (empty($stock)) throw new Exception('商品仓库未找到');
  929. if ($info['is_all'] == 1) {
  930. if ($stock['wait_in_stock'] < $info['num']) throw new Exception('商品可用库存不足退回数量');
  931. else $stock['wait_in_stock'] -= $info['num'];
  932. } else {
  933. if ($stock['usable_stock'] + $stock['wait_out_stock'] < $info['num']) throw new Exception('商品可用库存不足退回数量');
  934. else {
  935. if ($stock['usable_stock'] > $info['num']) {
  936. $stock['usable_stock'] -= $info['num'];
  937. } else {
  938. $stock['usable_stock'] = 0;
  939. $stock['wait_out_stock'] -= $info['num'] - $stock['usable_stock'];
  940. }
  941. $stock['total_stock'] = $stock['usable_stock'] + $stock['wait_out_stock'];
  942. }
  943. }
  944. $stock['updatetime'] = $date;
  945. $st_up = Db::name("good_stock")->save($stock);
  946. if ($st_up == false) throw new Exception('可售商品入库失败');
  947. }
  948. }
  949. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  950. "order_code" => $orderinfo["orderCode"],//出库单号
  951. "status" => $lor,//这里的status是之前的值
  952. "action_remark" => '',//备注
  953. "action_type" => "status"//新建create,编辑edit,更改状态status
  954. ], "XSQRD", $orderinfo['status'], $orderinfo);
  955. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  956. "order_type" => 'XSQRD',
  957. "order_code" => $orderinfo["orderCode"],//出库单号
  958. "order_id" => $orderinfo["id"],
  959. "order_status" => $orderinfo['status'], "before_status" => $lor
  960. ]);
  961. //对应采购单也要处理
  962. // $holder_id = Db::name('supplier')->where(['code' => $cgd['supplierNo'], 'is_del' => 0])->value('personid', 0);
  963. $holder = $userCommon->handle('sInfo', ['code' => $cgd['supplierNo']]);
  964. if (!isset($holder['code']) || $holder['code'] != 0) throw new Exception($holder['message']);
  965. $holder_id = $holder['data']['personid'];
  966. if (in_array($cgd['status'], [4])) {
  967. if ($orderinfo['is_stock'] == 1) {
  968. //库存品,推给31库管人员、41库管-张凯旋
  969. $uid = Db::name('user_role')
  970. ->where([
  971. ['is_del', '=', 0],
  972. ['roleid', 'in', [31, 41]],
  973. ['status', '=', 1]
  974. ])->column('uid');
  975. // if(!in_array($this->uid,$uid)) throw new Exception('库存品订单只能由库管人员操作');
  976. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  977. "order_type" => 'CGD',
  978. "order_code" => $cgd['cgdNo'],
  979. "order_id" => $cgd["id"],
  980. "order_status" => $cgd['status'],
  981. "before_status" => $lor,
  982. 'holder_id' => $holder_id,
  983. 'handle_user_list' => implode(',', $uid),
  984. ]);
  985. } else {
  986. // if($this->uid != $holder_id)throw new Exception('非库存品和采返商品只能由供应商负责人操作');
  987. //非库存品和采返商品,推给供应商负责人
  988. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  989. "order_type" => 'CGD',
  990. "order_code" => $cgd['cgdNo'],
  991. "order_id" => $cgd["id"],
  992. "order_status" => $cgd['status'],
  993. "before_status" => $lor,
  994. 'holder_id' => $holder_id,
  995. 'handle_user_list' => $orderinfo['cgderid'],
  996. ]);
  997. }
  998. }
  999. // ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  1000. // "order_type" => 'CGD',
  1001. // "order_code" => $cgd['cgdNo'],
  1002. // "order_id" => $cgd["id"],
  1003. // "order_status" => $cgd['status'],
  1004. // "before_status" => $lor,
  1005. // 'holder_id' => $holder_id
  1006. // ]);
  1007. //以下判断是当供应商不同意要退回到业务公司的时候触发的,在3.0系统中不需要了2023-02-09@by武
  1008. // if ($orderinfo['is_stock'] == 1 || $info['is_th'] == 0) {
  1009. //
  1010. // if ($orderinfo['order_type'] != 1) {
  1011. // if ($orderinfo['order_type'] == 3) {
  1012. // $goon = Db::name("good_zixun")
  1013. // ->where(["spuCode" => $orderinfo['good_code'], "is_del" => 0])
  1014. // ->findOrEmpty();
  1015. // $isZx = 1;
  1016. // } else {
  1017. // $goon = Db::name('good_basic')
  1018. // ->where(['spuCode' => $orderinfo['good_code']])
  1019. // ->findOrEmpty();
  1020. // $isZx = 2;
  1021. // }
  1022. // $spuCode = $this->CheckGoodZx($goon, $isZx, $code);
  1023. // $wsmcode = $info['return_wsm'];
  1024. // if ($wsmcode == "") throw new Exception('未找到退货仓库');
  1025. //
  1026. // $stock = Db::name("good_stock")
  1027. // ->where(['is_del' => 0, "spuCode" => $spuCode, 'wsm_code' => $wsmcode])
  1028. // ->findOrEmpty();
  1029. // if (empty($stock)) {
  1030. // $stock = [
  1031. // "spuCode" => $spuCode,
  1032. // "wsm_code" => $wsmcode,
  1033. // "usable_stock" => 0,
  1034. // "wait_out_stock" => 0,
  1035. // "wait_in_stock" => 0,
  1036. // "total_stock" => 0,
  1037. // "addtime" => $date,
  1038. // "updatetime" => $date,
  1039. // ];
  1040. // }
  1041. // $stock['usable_stock'] += $info['num'];
  1042. // $stock['updatetime'] = $date;
  1043. // $st_up = Db::name("good_stock")->save($stock);
  1044. // if ($st_up == false) throw new Exception('可售商品入库失败');
  1045. //
  1046. // $stockid = isset($stock['id']) ? $stock['id'] : Db::name("good_stock")->getLastInsID();
  1047. // $sabebn = Db::name("sale_info")
  1048. // ->where(["orderCode" => $orderinfo["orderCode"]])
  1049. // ->select()
  1050. // ->toArray();
  1051. // if (!empty($sabebn)) {
  1052. // $total_num = $info['num'];
  1053. // foreach ($sabebn as $ve) {
  1054. // $tempnum = 0;
  1055. // if ($total_num == 0) break;
  1056. // if ($total_num >= $ve['num']) {
  1057. // $tempnum = $ve['num'];
  1058. // $total_num -= $ve['num'];
  1059. // $ve['th_num'] += $ve['num'];
  1060. // $ve['num'] = 0;
  1061. // } else {
  1062. // $tempnum = $total_num;
  1063. // $ve['num'] -= $total_num;
  1064. // $ve['th_num'] += $total_num;
  1065. // $total_num = 0;
  1066. // }
  1067. // $bnin = GoodStockInfo::AddBn($stockid, $ve['bnCode'], $tempnum, $ve['origin_price']);
  1068. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1069. //
  1070. // $ve['updatetime'] = $date;
  1071. // $up = Db::name("sale_info")->save($ve);
  1072. // if ($up == false) throw new Exception('可售商品Bn库存数入库失败');
  1073. //
  1074. // $bnin = GoodStockInfo::ReturnBn($info['returnCode'], $ve['id'], $tempnum);
  1075. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1076. //
  1077. // }
  1078. // } else {
  1079. // $bn = makeNo("BN");
  1080. // $bnin = GoodStockInfo::AddBn($stockid, $bn, $info['num'], $cgd['good_price'] ?? 0);
  1081. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1082. //
  1083. // }
  1084. // $good_data = ['good_log_code' => $info['returnCode'], "stock_id" => $stockid, "type" => 1, 'stock' => $info['num'], "stock_name" => "usable_stock"];
  1085. // GoodLog::LogAdd(['id' => $this->uid, 'nickname' => $this->uname], $good_data, 'XSTHD');
  1086. // } else {
  1087. // $sabebn = Db::name("sale_info")
  1088. // ->where(["orderCode" => $orderinfo["orderCode"]])
  1089. // ->select()
  1090. // ->toArray();
  1091. // if (!empty($sabebn)) {
  1092. // $total_num = $info['num'];
  1093. // foreach ($sabebn as $ve) {
  1094. // $stock = Db::name("good_stock")
  1095. // ->where(['is_del' => 0, "spuCode" => $orderinfo['good_code'], 'id' => $ve['stockid']])
  1096. // ->findOrEmpty();
  1097. // if ($stock == false) throw new Exception('商品库存数据未找到');
  1098. //
  1099. // $tempnum = 0;
  1100. // if ($total_num == 0) break;
  1101. // if ($total_num >= $ve['num']) {
  1102. // $tempnum = $ve['num'];
  1103. // $total_num -= $ve['num'];
  1104. // $ve['th_num'] += $ve['num'];
  1105. // $ve['num'] = 0;
  1106. // } else {
  1107. // $tempnum = $total_num;
  1108. // $ve['num'] -= $total_num;
  1109. // $ve['th_num'] += $total_num;
  1110. // $total_num = 0;
  1111. // }
  1112. // $stock['usable_stock'] += $tempnum;
  1113. // $stock['wait_out_stock'] -= $tempnum;
  1114. // $stock['updatetime'] = $date;
  1115. // $st_up = Db::name("good_stock")->save($stock);
  1116. // if ($st_up == false) throw new Exception('可售商品入库失败');
  1117. //
  1118. //
  1119. // $bnin = GoodStockInfo::AddBn($ve['stock_id'], $ve['bnCode'], $tempnum, $ve['origin_price']);
  1120. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1121. //
  1122. // $ve['updatetime'] = $date;
  1123. // $up = Db::name("sale_info")->save($ve);
  1124. // if ($up == false) throw new Exception('可售商品Bn库存数入库失败');
  1125. //
  1126. // $bnin = GoodStockInfo::ReturnBn($info['returnCode'], $ve['id'], $tempnum);
  1127. // if ($bnin == false) throw new Exception('可售商品Bn库存数入库失败');
  1128. //
  1129. // }
  1130. // } else {
  1131. //
  1132. // $ordernum = Db::name("order_num")->where(['orderCode' => $orderinfo["orderCode"]])->findOrEmpty();
  1133. // if ($ordernum == false) throw new Exception('未找到关联采购单');
  1134. //
  1135. // $cgd = Db::name("purchease_order")
  1136. // ->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])
  1137. // ->findOrEmpty();
  1138. // if ($cgd == false) throw new Exception('未找到采购单数据');
  1139. //
  1140. // $bn = makeNo("BN");
  1141. // $stock = Db::name("good_stock")
  1142. // ->where(["spuCode" => $orderinfo['good_code'], 'wsm_code' => $cgd['wsm_code'], "is_del" => 0, "status" => 1])
  1143. // ->findOrEmpty();
  1144. // if ($stock == false) throw new Exception('商品库存数据未找到');
  1145. //
  1146. // $stock['usable_stock'] += $info['num'];
  1147. // $stock['wait_out_stock'] -= $info['num'];
  1148. // $stock['updatetime'] = $date;
  1149. // $st_up = Db::name("good_stock")->save($stock);
  1150. // if ($st_up == false) throw new Exception('可售商品入库失败');
  1151. //
  1152. // $yp = GoodStockInfo::AddBn($stock['id'], $bn, $info['num'], $cgd['good_price']);
  1153. // if ($yp == false) throw new Exception('商品批次退货入库失败');
  1154. //
  1155. // }
  1156. // }
  1157. // }
  1158. $data = [
  1159. "orderCode" => $info['orderCode'],
  1160. "th_type" => 1,
  1161. "th_num" => $info['num'],
  1162. "th_fee" => round($info['num'] * $orderinfo['sale_price'], 2),
  1163. "thCode" => $info['returnCode'],
  1164. "spuCode" => $orderinfo['good_code'],
  1165. "good_name" => $orderinfo['good_name'],
  1166. "cat_id" => $orderinfo['cat_id'],
  1167. "apply_id" => $info['apply_id'],
  1168. "apply_name" => $info['apply_name'],
  1169. "addtime" => $date,
  1170. "status" => 1,
  1171. "is_del" => 0
  1172. ];
  1173. $inse = Db::name("th_data")->insert($data);
  1174. if ($inse == false) throw new Exception('退货单更新失败');
  1175. }
  1176. }
  1177. Db::commit();
  1178. return app_show(0, "更新成功");
  1179. } catch (Exception $e) {
  1180. Db::rollback();
  1181. return error_show(1004, $e->getMessage() . '|' . $e->getFile() . ':' . $e->getLine());
  1182. }
  1183. }
  1184. public function zxcreate(){
  1185. $ordeCode = isset($this->post['orderCode']) &&$this->post['orderCode']!=''?trim($this->post['orderCode']) :"";
  1186. if($ordeCode==''){
  1187. return error_show(1004,"参数orderCode 不能为空");
  1188. }
  1189. $order= Db::name("sale")->where(["orderCode"=>$ordeCode,"is_del"=>0])->find();
  1190. if(empty($order)){
  1191. return error_show(1005,"未找到订单数据");
  1192. }
  1193. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  1194. if($errorCode==''){
  1195. return error_show(1004,"参数errorCode 不能为空");
  1196. }
  1197. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  1198. if(empty($error)){
  1199. return error_show(1005,"未找到退货原因数据");
  1200. }
  1201. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  1202. // $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  1203. // if($token==''){
  1204. // return error_show(105,"参数token不能为空");
  1205. // }
  1206. // $user =GetUserInfo($token);
  1207. // if(empty($user)||$user['code']!=0){
  1208. // return error_show(1002,"申请人数据不存在");
  1209. // }
  1210. $rm= $this->uid;//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  1211. $ri= $this->uname;//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  1212. $num = isset($this->post['num'])&&$this->post['num']!='' ? intval($this->post['num']):"";
  1213. if($num==''){
  1214. return error_show(1005,"参数num不能为空");
  1215. }
  1216. if($order['wsend_num']<$num){
  1217. return error_show(1002,"仓库未发货数量不足退货");
  1218. }
  1219. $returnCode=makeNo("RS");
  1220. Db::startTrans();
  1221. try{
  1222. $in = [
  1223. "returnCode"=>$returnCode,
  1224. "orderCode"=>$ordeCode,
  1225. "good_code"=>$order['good_code'],
  1226. "good_name"=>$order['good_name'],
  1227. "apply_id"=>$rm,
  1228. "apply_name"=>$ri,
  1229. "error_code"=>$errorCode,
  1230. "num"=>$num,
  1231. "remark"=>$remark,
  1232. "order_type"=>2,
  1233. "status"=>0,
  1234. "is_del"=>0,
  1235. "addtime"=>date("Y-m-d H:i:s"),
  1236. "updatetime"=>date("Y-m-d H:i:s")
  1237. ];
  1238. $create = Db::name("sale_return")->insert($in,true);
  1239. if($create>0) {
  1240. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>0,"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  1241. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  1242. $ste = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',
  1243. "action_type"=>"create"];
  1244. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$ste,"XSTHD",3,$in);
  1245. //维护台账记录
  1246. // Db::name('standing_book')
  1247. // ->where('ordeCode', $ordeCode)
  1248. // ->update([
  1249. // 'returnGoodCode' => $returnCode,
  1250. // 'updatetime' => date("Y-m-d H:i:s")
  1251. // ]);
  1252. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$ordeCode}'");
  1253. if ($order['send_type'] == 1) {
  1254. $wsend = Db::name("order_out")->where(['orderCode' => $ordeCode, "status" => 1, "order_type"=>2])->select();
  1255. // ->save(["status" => 0, "updatetime" => date("Y-m-d H:i:s")]);
  1256. if(!empty($wsend)){
  1257. foreach ($wsend as $value){
  1258. $tt = $value['status'];
  1259. $value['status']=0;
  1260. $value['updatetime']=date("Y-m-d H:i:s");
  1261. $up =Db::name("order_out")->save($value);
  1262. if($up){
  1263. $process=["order_code"=> $value['outCode'],"order_id"=>$value['id'],"order_status"=>0,"order_type"=>'CKD',"before_status"=>$tt,'holder_id' => $value['apply_id']];
  1264. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  1265. $ste = ["order_code"=>$value['outCode'],"status"=>$tt,"action_remark"=>'',"action_type"=>"status"];
  1266. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$ste,"CKD",0,$value);
  1267. }else{
  1268. Db::rollback();
  1269. return error_show(1005,"退货单新建失败");
  1270. }
  1271. }
  1272. }
  1273. }
  1274. Db::commit();
  1275. return error_show(0,"退货单新建成功");
  1276. }
  1277. Db::rollback();
  1278. return error_show(1005,"退货单新建失败");
  1279. }catch (\Exception $e){
  1280. Db::rollback();
  1281. return error_show(1005,$e->getMessage());
  1282. }
  1283. }
  1284. //全部退货
  1285. public function allReturn(){
  1286. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''? trim($this->post['orderCode']):"";
  1287. if($orderCode==''){
  1288. return error_show(1004,"参数orderCode 不能为空");
  1289. }
  1290. $order= Db::name("sale")->where(["orderCode"=>$orderCode,"is_del"=>0])->find();
  1291. if(empty($order)){
  1292. return error_show(1005,"未找到订单数据");
  1293. }
  1294. if($order['wsend_num']!=$order['good_num']){
  1295. return error_show(1005,"订单未发货数量与总数不等,无法全退");
  1296. }
  1297. $retrun =Db::name("sale_return")->where([["orderCode","=",$orderCode],["is_del","=",0],["status", "in", [1,2,3,7,9,10]]])->count();
  1298. if($retrun>0){
  1299. return error_show(1005,"存在退货订单数据");
  1300. }
  1301. if($order['order_type']==3){
  1302. $goon = Db::name("good_zixun")
  1303. ->where(["spuCode"=>$order['good_code'],"is_del"=>0])
  1304. ->findOrEmpty();
  1305. }else {
  1306. $goon = Db::name('good_platform')
  1307. ->alias('a')
  1308. ->join('good b', 'b.spuCode=a.spuCode', 'left')
  1309. ->where(['a.skuCode' => $order['skuCode']])
  1310. ->field("b.creater,b.createrid,b.supplierNo")
  1311. ->findOrEmpty();
  1312. }
  1313. if(empty($goon)){
  1314. return error_show(1005,"未找到商品数据");
  1315. }
  1316. // $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  1317. // if($supplier==false){
  1318. // return error_show(1005,"未找到商品供应商数据");
  1319. // }
  1320. $userCommon = new \app\admin\common\User();
  1321. $tmp = $userCommon->handle('sInfo', ['code' => $goon['supplierNo']]);
  1322. if (!isset($tmp['code']) || $tmp['code'] != 0) return json_show($tmp['code'], $tmp['message'], $tmp['data']);
  1323. $supplier = $tmp['data'];
  1324. if (empty($supplier)) return json_show(1005, "未找到商品供应商数据");
  1325. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  1326. if($errorCode==''){
  1327. return error_show(1004,"参数errorCode 不能为空");
  1328. }
  1329. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  1330. if(empty($error)){
  1331. return error_show(1005,"未找到退货原因数据");
  1332. }
  1333. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  1334. $addr =Db::name("order_addr")->where([["orderCode","=",$orderCode],["is_del","=",0]])->select()->toArray();
  1335. if($order['is_stock']==0){
  1336. $ordernum = Db::name("order_num")->where(['orderCode' => $orderCode])->find();
  1337. if ($ordernum == false) {
  1338. return error_show(1005, '未找到关联采购单');
  1339. }
  1340. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  1341. if ($cgd == false) {
  1342. return error_show(1005, '未找到采购单数据');
  1343. }
  1344. }
  1345. if($order['is_stock']==0&&$cgd['send_status']!=1){
  1346. return error_show(1005, '采购单已发起入库');
  1347. }
  1348. $returnCode=makeNo("RN");
  1349. Db::startTrans();
  1350. try{
  1351. $in = [
  1352. "returnCode"=>$returnCode,
  1353. "orderCode"=>$orderCode,
  1354. "good_code"=>$order['good_code'],
  1355. "good_name"=>$order['good_name'],
  1356. "apply_id"=>$this->uid,
  1357. "apply_name"=>$this->uname,
  1358. "cgderid"=>$goon['createrid'],
  1359. "cgder"=>$goon['creater'],
  1360. "person"=>$supplier['person']??'',
  1361. "person_id"=>$supplier['personid']??0,
  1362. "error_code"=>$errorCode,
  1363. "num"=>$order['wsend_num'],
  1364. "total_fee"=>round($order['sale_price']*$order['wsend_num'],2),
  1365. "good_price"=>$order['sale_price'],
  1366. "platform_id"=>$order['platform_id'],
  1367. "remark"=>$remark,
  1368. "order_type"=>$order['order_type'],
  1369. "is_addr"=>count($addr)>0 ?1:0,
  1370. "status"=>$order['is_stock']==1?4:1,
  1371. "is_del"=>0,
  1372. "is_all"=>1,
  1373. "addtime"=>date("Y-m-d H:i:s"),
  1374. "updatetime"=>date("Y-m-d H:i:s")
  1375. ];
  1376. $create = Db::name("sale_return")->insert($in,true);
  1377. if($create>0){
  1378. $stn = ["order_code"=>$returnCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  1379. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname],$stn,"XSTHD",$in['status'],$in);
  1380. $process=["order_code"=>$returnCode,"order_id"=>$create,"order_status"=>$in['status'],"order_type"=>'XSTHD',"before_status"=>0,'holder_id'=>$in['apply_id']];
  1381. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname],$process);
  1382. //维护台账信息
  1383. Db::execute("UPDATE `wsm_standing_book` SET `returnGoodCode`=CONCAT(IFNULL(`returnGoodCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$orderCode}'");
  1384. if(count($addr)!=0){
  1385. $inf=[];
  1386. foreach ($addr as $val){
  1387. if ($val['receipt_quantity'] == 0) continue;//当退货数量为0时,跳过
  1388. $temp=[];
  1389. $send =Db::name('order_out')->where([["addrid","=",$val['id']]])->find();
  1390. if ($send == false) {
  1391. Db::rollback();
  1392. return error_show(1004, "地址发货单未找到");
  1393. }
  1394. if($send['status']>=2){
  1395. Db::rollback();
  1396. return error_show(1005,"地址已发货");
  1397. }
  1398. if($order['is_stock']==1){
  1399. $val['is_del'] = 1;
  1400. $val['updatetime'] = date("Y-m-d H:i:s");
  1401. $addrup = Db::name("order_addr")->save($val);
  1402. if ($addrup == false) {
  1403. Db::rollback();
  1404. return error_show(1004, "地址更新失败");
  1405. }
  1406. $send['is_del'] = 1;
  1407. $send['remark'] ='全退';
  1408. $send['updatetime'] = date("Y-m-d H:i:s");
  1409. $outup = Db::name("order_out")->save($send);
  1410. if ($outup == false) {
  1411. Db::rollback();
  1412. return error_show(1004, "地址发货单更新失败");
  1413. }
  1414. //处理发货申请单流程
  1415. ProcessOrder::AddProcess(["id" => $this->uid, "nickname" => $this->uname], [
  1416. "order_type" => 'CKD',
  1417. "order_code" => $send["outCode"],//出库单号
  1418. "order_id" => $send["id"],
  1419. "order_status" => 4,//全部退货
  1420. "before_status" => $send['status'],
  1421. 'holder_id=' => $send['apply_id']
  1422. ]);
  1423. // $ordersend = Db::name("order_send")->where(["outCode" => $send['outCode']])->find();
  1424. // if($ordersend==false){
  1425. // Db::rollback();
  1426. // return error_show(1004, "发货单关联数据未找到");
  1427. // }
  1428. // $ordersend['status'] = 0;
  1429. // $ordersend['updatetime'] = date("Y-m-d H:i:s");
  1430. // $sendip = Db::name("order_send")->save($ordersend);
  1431. // if ($sendip == false) {
  1432. // Db::rollback();
  1433. // return error_show(1004, "发货单更新失败");
  1434. // }
  1435. }
  1436. $temp['returnCode']=$returnCode;
  1437. $temp['orderCode']=$orderCode;
  1438. $temp['outCode']=isset($send['outCode'])?$send['outCode']:"";
  1439. $temp['addrid']=$val['id'];
  1440. $temp['send_num']=$val['receipt_quantity'];
  1441. $temp['return_num']=$val['receipt_quantity'];
  1442. $temp['is_del']=0;
  1443. $temp['addtime']=date("Y-m-d H:i:s");
  1444. $temp['updatetime']=date("Y-m-d H:i:s");
  1445. $inf[]=$temp;
  1446. }
  1447. $inadd=Db::name("sale_returnaddr")->insertAll($inf);
  1448. if($inadd==0){
  1449. Db::rollback();
  1450. return error_show(1005,"退货单新建失败");
  1451. }
  1452. }
  1453. if($order['is_stock']==1){
  1454. $lor=$order['status'];
  1455. $thnum = $order['wsend_num'];
  1456. $order['send_num'] += $thnum;
  1457. $order['th_num'] += $thnum;
  1458. $order['wsend_num'] =0;
  1459. $order['status'] = 3;
  1460. $order['send_status']=3;
  1461. $order['th_fee'] += round($thnum * $order['sale_price'], 2);
  1462. $order['updatetime'] = date("Y-m-d H:i:s");
  1463. $uap = Db::name("sale")->save($order);
  1464. if ($uap == false) {
  1465. Db::rollback();
  1466. return error_show(1005, '销售单订单更新失败');
  1467. }
  1468. ActionLog::logAdd(["id"=>$this->uid,"nickname"=>$this->uname], [
  1469. "order_code" => $order["orderCode"],//出库单号
  1470. "status" => $lor,//这里的status是之前的值
  1471. "action_remark" => '',//备注
  1472. "action_type" => "status"//新建create,编辑edit,更改状态status
  1473. ], "XSQRD", $order['status'], $order);
  1474. ProcessOrder::AddProcess(["id"=>$this->uid,"nickname"=>$this->uname], [
  1475. "order_type" => 'XSQRD',
  1476. "order_code" =>$order["orderCode"],//出库单号
  1477. "order_id" => $order["id"],
  1478. "order_status" =>$order['status'],
  1479. "before_status"=>$lor,
  1480. 'holder_id=' => $order['apply_id']
  1481. ]);
  1482. $saleinfo=Db::name("sale_info")->where([["orderCode","=",$orderCode],["num",">",0]])->select()->toArray();
  1483. if(empty($saleinfo)) {
  1484. $ordernum = Db::name("order_num")->where(['orderCode' => $orderCode])->find();
  1485. if ($ordernum == false) {
  1486. Db::rollback();
  1487. return error_show(1005, '未找到关联采购单');
  1488. }
  1489. $cgd = Db::name("purchease_order")->where(["cgdNo" => $ordernum['cgdNo'], "is_del" => 0])->find();
  1490. if ($cgd == false) {
  1491. Db::rollback();
  1492. return error_show(1005, '未找到采购单数据');
  1493. }
  1494. $bn =makeNo("BN");
  1495. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'wsm_code'=>$cgd['wsm_code'],"is_del"=>0,"status"=>1])->find();
  1496. if($stock==false){
  1497. Db::rollback();
  1498. return error_show(1005, '商品库存数据未找到');
  1499. }
  1500. $stock['usable_stock']+=$thnum;
  1501. $stock['wait_out_stock'] -=$thnum;
  1502. $stock['updatetime'] = date("Y-m-d H:i:s");
  1503. $st_up = Db::name("good_stock")->save($stock);
  1504. if ($st_up == false) {
  1505. return error_show(1005, '可售商品入库失败');
  1506. }
  1507. $yp=GoodStockInfo::AddBn($stock['id'],$bn,$thnum,$cgd['good_price']);
  1508. if($yp==false){
  1509. Db::rollback();
  1510. return error_show(1005, '商品批次退货入库失败');
  1511. }
  1512. }else{
  1513. $tempnum =$thnum;
  1514. foreach ($saleinfo as $va){
  1515. if($tempnum ==0) break;
  1516. $stock = Db::name("good_stock")->where(["spuCode" => $order['good_code'], 'id' => $va['stockid']])->find();
  1517. if($stock==false) {
  1518. Db::rollback();
  1519. return error_show(1005, '商品库存数据未找到');
  1520. }
  1521. if($va['num']>=$tempnum) {
  1522. $tnm = $tempnum;
  1523. $va['num']-= $tempnum;
  1524. $va['th_num']+= $tempnum;
  1525. $tempnum=0;
  1526. }else{
  1527. $tnm = $va['num'];
  1528. $tempnum -=$va['num'];
  1529. $va['th_num']+= $va['num'];
  1530. $va['num'] =0;
  1531. }
  1532. $stock['usable_stock']+=$tnm;
  1533. $stock['wait_out_stock'] -=$tnm;
  1534. $stock['updatetime'] = date("Y-m-d H:i:s");
  1535. $st_up = Db::name("good_stock")->save($stock);
  1536. if ($st_up == false) {
  1537. return error_show(1005, '可售商品入库失败');
  1538. }
  1539. $ps = GoodStockInfo::AddBn($va['stockid'],$va['bnCode'],$tnm);
  1540. if ($ps == false) {
  1541. return error_show(1005, '商品批次退货入库失败');
  1542. }
  1543. $ret = GoodStockInfo::ReturnBn($returnCode,$va['id'],$tnm);
  1544. if ($ret == false) {
  1545. return error_show(1005, '商品批次退货入库失败');
  1546. }
  1547. $va['updatetime']=date("Y-m-d H:i:s");
  1548. $sal= Db::name("sale_info")->save($va);
  1549. if ($sal == false) {
  1550. return error_show(1005, '商品批次退货入库失败');
  1551. }
  1552. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 1, 'stock' => $tnm, "stock_name" => "usable_stock"];
  1553. $good_data[]= ['good_log_code' => $returnCode, "stock_id" => $va['stockid'], "type" => 2, 'stock' => $tnm, "stock_name" => "wait_out_stock"];
  1554. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname], $good_data, 'XSTHD');
  1555. }
  1556. }
  1557. $data=[
  1558. "orderCode"=>$orderCode,
  1559. "th_type"=>1,
  1560. "th_num"=>$thnum,
  1561. "th_fee"=>round($order['sale_price']*$thnum,2),
  1562. "thCode"=>$returnCode,
  1563. "spuCode"=>$order['good_code'],
  1564. "good_name"=>$order['good_name'],
  1565. "cat_id"=>$order['cat_id'],
  1566. "apply_id"=>$this->uid,
  1567. "apply_name"=>$this->uname,
  1568. "addtime"=>date("Y-m-d H:i:s"),
  1569. "status"=>1,
  1570. "is_del"=>0
  1571. ];
  1572. $inse=Db::name("th_data")->insert($data);
  1573. if($inse==false){
  1574. Db::rollback();
  1575. return error_show(1004,"退货单更新失败");
  1576. }
  1577. }
  1578. Db::commit();
  1579. return app_show(0,"退货单新建成功",["returnCode"=>$returnCode]);
  1580. }
  1581. Db::rollback();
  1582. return error_show(1005,"退货单新建失败");
  1583. }catch (\Exception $e){
  1584. Db::rollback();
  1585. return error_show(1005, $e->getMessage());
  1586. }
  1587. }
  1588. //导出
  1589. public function export()
  1590. {
  1591. $param = $this->request->only([
  1592. 'returnCode'=>'',
  1593. 'status'=>'',
  1594. 'orderCode'=>'',
  1595. 'apply_name'=>'',
  1596. 'start'=>'',
  1597. 'end'=>'',
  1598. 'good_code'=>'',
  1599. 'skuCode'=>'',
  1600. 'relaComNo'=>'',
  1601. 'supplierNo'=>'',
  1602. 'company_name'=>'',
  1603. ],'post','trim');
  1604. $where = [['sr.is_del', "=", 0]];
  1605. if (!empty($param['returnCode'])) $where[] = ['sr.returnCode', "in", $param['returnCode']];
  1606. if ($param['status'] !== "") $where[] = ['sr.status', "=", $param['status']];
  1607. if ($param['orderCode'] != "") $where[] = ['sr.orderCode', "like", "%{$param['orderCode']}%"];
  1608. if ($param['apply_name'] != "") $where[] = ['sr.apply_name', "like", "%{$param['apply_name']}%"];
  1609. if ($param['start'] !== "") $where[] = ['sr.addtime', ">=", $param['start'] . ' 00:00:00'];
  1610. if ($param['end'] !== "") $where[] = ['sr.addtime', "<=", $param['end'] . ' 23:59:59'];
  1611. if ($param['good_code'] != "") $where[] = ['sr.good_code', "like", "%{$param['good_code']}%"]; //商品成本编码搜索
  1612. if ($param['skuCode'] != "") $where[] = ['b.skuCode', "like", "%{$param['skuCode']}%"];//商品上线编码搜索
  1613. if ($param['relaComNo'] != "") $where[] = ['b.supplierNo', '=', $param['relaComNo']];
  1614. if ($param['supplierNo'] != "") $where[] = ['b.supplierNo', '=', $param['supplierNo']];
  1615. if ($param['company_name'] !== "") $where[] = ["sr.apply_id", 'in', get_company_item_user_by_name($param['company_name'])];
  1616. $condition = [];
  1617. //只有level2的账号过滤数据权限
  1618. if ($this->level == 2) {
  1619. $role = $this->checkDataShare();
  1620. $hand = resign_hand_user($this->uid, 0);
  1621. if (!empty($role[DataGroupModel::$type_全部])) {
  1622. $arr = array_unique(array_merge($hand, $role[DataGroupModel::$type_全部]));
  1623. $condition[] = ["sr.apply_id", "in", $arr];
  1624. $condition[] = ["sr.cgderid", "in", $hand];
  1625. $condition[] = ["sr.person_id", "in", $hand];
  1626. }
  1627. }
  1628. //供应商账号不允许看到库存品数据
  1629. if ($this->level == 3) $where[] = ['b.is_stock', '<>', 1];
  1630. if (in_array($this->roleid, config('app.wsm_cgder_role'))) $where[] = ["b.order_type", "=", 1];
  1631. $list = Db::name("sale_return")
  1632. ->alias('sr')
  1633. ->field('sr.returnCode 退货单编号,sr.orderCode 销售订单编号,sr.apply_id,sr.apply_name 申请人,"" 申请人部门,sr.cgder 采购员,sr.person 供应商负责人,c.result 错误原因,sr.good_code 商品成本编码,b.skuCode 上线商品编码,sr.good_name 商品名称,sr.good_price 单价,sr.num 退货数量,sr.total_fee 退货金额,d.platform_name 平台,sr.remark 备注,sr.order_type 退货单类型,sr.status 状态,sr.is_all 是否全退货,sr.is_th 供应商是否同意退货,sr.is_addr,e.addr_code,e.addr 发货地址,sr.return_wsm 退货仓库,sr.loop_total 循环次数')
  1634. ->leftJoin("sale b", "b.orderCode=sr.orderCode AND b.is_del=0")
  1635. ->leftJoin("result_info c", "c.result_code=sr.error_code")
  1636. ->leftJoin("platform d", "d.id=sr.platform_id")
  1637. ->leftJoin("order_addr e", "e.id=sr.is_addr")
  1638. ->where($where)
  1639. ->where(function ($query) use ($condition) {
  1640. $query->whereOr($condition);
  1641. })
  1642. ->order("sr.addtime desc")
  1643. ->select()
  1644. ->toArray();
  1645. $all_createrid = array_column($list, 'apply_id');
  1646. $item = get_company_name_by_uid($all_createrid);
  1647. $order_type = [1 => '直接下单', 2 => '咨询', 3 => '项目', 4 => '平台', 5 => '有赞', 6 => '售后补换货', 7 => '报备转单', 8 => '支付渠道'];
  1648. $bool = [0 => '否', 1 => '是'];
  1649. $status = [1 => '待业务审批', 4 => '退货完成', 5 => '业务驳回', 9 => '待供应商审核', 8 => '供应商已驳回待采购审核', 10 => '业务公司修改待供应商确认'];
  1650. foreach ($list as &$value) {
  1651. // $value['退货金额'] = round($value['sale_price'] * $value['退货数量'],2);
  1652. $value['申请人部门'] = $item[$value['apply_id']] ?? '';
  1653. $value['是否全退货'] = $bool[$value['是否全退货']]??'';
  1654. $value['供应商是否同意退货'] = $bool[$value['供应商是否同意退货']]??'';
  1655. $value['退货单类型'] = $order_type[$value['退货单类型']]??'';
  1656. $value['状态'] = $status[$value['状态']]??'';
  1657. if($value['is_addr']==0) $value['发货地址']='无地址';
  1658. else $value['发货地址']=GetAddr($value['addr_code']).$value['发货地址'];
  1659. unset($value['apply_id']);
  1660. unset($value['good_price']);
  1661. unset($value['addr_code']);
  1662. unset($value['is_addr']);
  1663. }
  1664. if (empty($list)) $list[] = ['没有相关可导出的数据'];
  1665. excelSave('退货单' . date('YmdHis'), array_keys($list[0]), $list);
  1666. }
  1667. }