After.php 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\GoodLog;
  5. use app\admin\model\GoodStockInfo;
  6. use app\admin\model\ProcessOrder;
  7. use think\App;
  8. use think\Exception;
  9. use think\facade\Db;
  10. use think\facade\Validate;
  11. //售后
  12. class After extends Base
  13. {
  14. public function __construct(App $app)
  15. {
  16. parent::__construct($app);
  17. }
  18. public function create(){
  19. $outCode = isset($this->post['outCode']) &&$this->post['outCode']!=''?trim($this->post['outCode']) :"";
  20. if($outCode==''){
  21. return error_show(1004,"参数outCode 不能为空");
  22. }
  23. $orderout= Db::name("order_out")->where(["outCode"=>$outCode,'is_del'=>0])->find();
  24. if($orderout==false){
  25. return error_show(1005,"未找到订单数据");
  26. }
  27. $after_num =Db::name("order_return")->where(["orderCode"=>$orderout['orderCode'],"outCode"=>$outCode])
  28. ->where("status","not in",[5,6,8])->sum("error_num");
  29. $th_num =Db::name("order_back")->where(["orderCode"=>$orderout['orderCode'],"outCode"=>$outCode,"is_del"=>0])
  30. ->where("status","<",4)->sum("return_num");
  31. if($th_num!=0 || $after_num!=0){
  32. return error_show(1005,"存在售后或售后退货未结束流程");
  33. }
  34. $oder= Db::name("sale")->where(["orderCode"=>$orderout['orderCode'],"is_del"=>0])->find();
  35. if(empty($oder)){
  36. return error_show(1005,"未找到订单数据");
  37. }
  38. if($oder['order_type']!=1){
  39. $wsm =Db::name("warehouse_info")->where(["wsm_code"=>$orderout['wsm_code']])->find();
  40. if($wsm==false){
  41. return error_show(1005,"未找到仓库数据");
  42. }
  43. // $cgder =$wsm['contactor_name'] ??'';
  44. // $cgderid =$wsm['contactor'] ??0;
  45. }
  46. // else{
  47. // $roleArr=\think\facade\Config::get("app");
  48. // $kuguan = Db::name("user_role")->where(["roleid"=>$roleArr['wsm_cgder_role'],"is_del"=>0])->column("uid");
  49. // if(empty($kuguan)){
  50. // return error_show(1005,"未找到仓库管理员数据");
  51. // }
  52. // $userinfo = Db::name("dapart_user")
  53. // $cgder =$wsm['contactor_name'] ??'';
  54. // $cgderid =$wsm['contactor'] ??0;
  55. // }
  56. if ($oder['order_type'] == 3) {
  57. $goon = Db::name("good_zixun")->where(["spuCode" => $oder['good_code'], "is_del" => 0])->find();
  58. } else {
  59. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  60. ->where(['a.skuCode' => $oder['skuCode']])->field("b.creater,b.createrid,b.supplierNo")->find();
  61. }
  62. $errorCode = isset($this->post['errorCode']) &&$this->post['errorCode']!=''?trim($this->post['errorCode']) :"";
  63. if($outCode==''){
  64. return error_show(1004,"参数errorCode 不能为空");
  65. }
  66. $error =Db::name('result_info')->where(["result_code"=>$errorCode,"is_del"=>0])->find();
  67. if(empty($error)){
  68. return error_show(1005,"未找到退货原因数据");
  69. }
  70. $supplier =Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  71. if($supplier==false){
  72. return error_show(1005,"未找到商品供应商数据");
  73. }
  74. $remark =isset($this->post['remark']) &&$this->post['remark']!=''?trim($this->post['remark']) :"";
  75. $is_receive =isset($this->post['is_receive']) &&$this->post['is_receive']!==''?intval($this->post['is_receive']):"";
  76. if($is_receive===""){
  77. return error_show(1004,"参数 is_receive 不能为空");
  78. }
  79. if($is_receive==0){
  80. $num=$orderout['send_num'];
  81. }else{
  82. $num =isset($this->post['num']) &&$this->post['num']!=''?intval($this->post['num']) :"";
  83. if($num==""){
  84. return error_show(1004,"参数 num 不能为空或零");
  85. }
  86. }
  87. $thnum =Db::name("order_back")->where(["orderCode"=>$orderout['orderCode'],"outCode"=>$outCode,"is_del"=>0])
  88. ->where("status","=",4)->sum("return_num");
  89. if($num>$orderout['send_num']-$thnum){
  90. return error_show(1004,"可用售后数量不足");
  91. }
  92. $except_code =isset($this->post['except_code']) &&$this->post['except_code']!=''?trim($this->post['except_code']) :"";
  93. $img = isset($this->post['img']) &&$this->post['img']!=''?trim($this->post['img']) :"";
  94. $token = isset($this->post['token'])&&$this->post['token']!='' ? trim($this->post['token']):"";
  95. if($token==''){
  96. return error_show(105,"参数token不能为空");
  97. }
  98. // $user =GetUserInfo($token);
  99. // if(empty($user)||$user['code']!=0){
  100. // return error_show(102,"申请人数据不存在");
  101. // }
  102. $rm = $this->uid;//isset($user["data"]['id']) ? $user["data"]['id'] : "";
  103. $ri = $this->uname;//isset($user["data"]['nickname']) ? $user["data"]['nickname'] : "";
  104. $returnCode=makeNo("RS");
  105. Db::startTrans();
  106. try{
  107. $in = [
  108. "returnCode"=>$returnCode,
  109. "outCode"=>$outCode,
  110. "orderCode"=> $oder['orderCode'],
  111. "good_code"=>$oder['good_code'],
  112. "good_name"=>$oder['good_name'],
  113. "customer_code"=>$oder['customer_code'],
  114. "apply_id"=>$rm,
  115. "apply_name"=>$ri,
  116. "cgder"=>$wsm['contactor_name'] ??'',
  117. "cgderid"=>$wsm['contactor']??0,
  118. "person"=>$supplier['person']??'',
  119. "person_id"=>$supplier['personid']??0,
  120. 'total_num'=>$orderout['send_num'],
  121. "error_code"=>$errorCode,
  122. "error_num"=>$num,
  123. "error_img"=>$img,
  124. "error_msg"=>$error['result'],
  125. "error_remark"=>$remark,
  126. "is_receive"=>$is_receive,
  127. "remark"=>'',
  128. "except_code"=>$except_code,
  129. "order_type"=>$orderout['order_type'],
  130. "status"=>1,
  131. "is_del"=>0,
  132. "addtime"=>date("Y-m-d H:i:s"),
  133. "updatetime"=>date("Y-m-d H:i:s")
  134. ];
  135. $create = Db::name("order_return")->insert($in,true);
  136. $orde = ["order_code"=>$returnCode,"status"=>$in['status'],"action_remark"=>'',"action_type"=>"create"];
  137. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$orde,'SHD',1,$orde);
  138. if($create>0){
  139. //维护台账
  140. // Db::name('standing_book')
  141. // ->whereFindInSet('outCode', $outCode)
  142. // ->update([
  143. // 'returnCode' => $returnCode,
  144. // 'updatetime' => date("Y-m-d H:i:s")
  145. // ]);
  146. Db::execute("UPDATE `wsm_standing_book` SET `returnCode`=CONCAT(IFNULL(`returnCode`,''),',{$returnCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE FIND_IN_SET('{$outCode}',`outCode`)");
  147. //当状态为1(即待业务审核)时,要将待办数据推给销售主管,即角色为'客服负责人'的用户
  148. $user = Db::name('user_role')
  149. ->where([
  150. ['is_del', '=', 0],
  151. ['roleid', 'in', [12, 13, 17, 28]],//12客服负责人,13项目负责人,17平台负责人,28网络负责人
  152. ['status', '=', 1],
  153. ])->column('uid');
  154. $process = ["order_code" => $returnCode, "order_id" => $create, "order_status" => $in['status'], "order_type" => 'SHD', "before_status" => 0, 'handle_user_list'=>implode($user)];
  155. ProcessOrder::AddProcess($token,$process);
  156. Db::commit();
  157. return app_show(0,"售后申请单新建成功",["returnCode"=>$returnCode]);
  158. }
  159. Db::rollback();
  160. return error_show(1005,"售后申请单新建失败");
  161. }catch (\Exception $e){
  162. Db::rollback();
  163. return error_show(1005,$e->getMessage());
  164. }
  165. }
  166. public function list(){
  167. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  168. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  169. $where =[['a.is_del',"=",0]];
  170. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  171. if($bkcode!=""){
  172. $where[]=['a.returnCode',"like", "%{$bkcode}%"];
  173. }
  174. $out = isset($this->post['outCode']) && $this->post['outCode']!="" ? trim($this->post['outCode']):"";
  175. if($out!=""){
  176. $where[]=['a.outCode',"like", "%{$out}%"];
  177. }
  178. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  179. if($status!==""){
  180. $where[]=['a.status',"=", $status];
  181. }
  182. $order_type = isset($this->post['order_type']) && $this->post['order_type']!=="" ? intval($this->post['order_type']):"";
  183. if($order_type!==""){
  184. $where[]=['a.order_type',"=", $order_type];
  185. }
  186. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode']) :"";
  187. if($orderCode!=""){
  188. $where[]=['a.orderCode',"like", "%{$orderCode}%"];
  189. }
  190. $good_code = isset($this->post['good_code']) && $this->post['good_code']!="" ? trim($this->post['good_code'])
  191. :"";
  192. if($good_code!=""){
  193. $where[]=['a.good_code',"like", "%{$good_code}%"];
  194. }
  195. $good_name = isset($this->post['good_name']) && $this->post['good_name']!="" ? trim($this->post['good_name'])
  196. :"";
  197. if($good_name!=""){
  198. $where[]=['a.good_name',"like", "%{$good_name}%"];
  199. }
  200. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code']!="" ? trim($this->post['customer_code'])
  201. :"";
  202. if($customer_code!=""){
  203. $where[]=['a.customer_code',"like", "%{$customer_code}%"];
  204. }
  205. $except_code = isset($this->post['except_code']) && $this->post['except_code']!="" ? trim($this->post['except_code']):"";
  206. if($except_code!=""){
  207. $where[]=['a.except_code',"=", $except_code];
  208. }
  209. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name'])
  210. :"";
  211. if($apply_name!=""){
  212. $where[]=['a.apply_name',"like", "%{$apply_name}%"];
  213. }
  214. $start =isset($this->post['start'])&&$this->post['start']!='' ? $this->post['start']:"";
  215. if($start!==""){
  216. $where[]=['a.addtime',">=", $start];
  217. }
  218. $end =isset($this->post['end'])&&$this->post['end']!='' ? $this->post['end']:"";
  219. if($end!==""){
  220. $where[]=['a.addtime',"<=", $end];
  221. }
  222. $role=$this->checkRole();
  223. $condition='';
  224. if(!empty($role['write']) && $this->uid!=""){
  225. $cgdArr =\think\facade\Config::get("app");
  226. $cgdids =implode(",",$cgdArr['wsm_cgder_role']);
  227. $condition .="a.cgderid = {$this->uid} or a.apply_id in (".implode(',',$role['write']).") or a.person_id={$this->uid} or (order_type=1 and {$this->roleid} in ($cgdids))";
  228. }
  229. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  230. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  231. $count=Db::name("order_return")->alias("a")->leftJoin("order_returninfo b","a.returnCode=b.returnCode")->where
  232. ($where)->where($condition)->count();
  233. $total = ceil($count/$size);
  234. $page = $total>=$page ? $page :$total;
  235. $list =Db::name("order_return")
  236. ->alias("a")
  237. ->leftJoin("order_returninfo b","a.returnCode=b.returnCode")
  238. ->where($where)
  239. ->where($condition)
  240. ->field("a.*,b.return_wsm,b.contactor,b.mobile,b.addr,b.addr_code,b.post_code,b.post_company,b.post_fee,b.gys_remark,u.itemid,a.orderCode")
  241. ->page($page,$size)
  242. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  243. ->order("addtime desc")
  244. ->cursor();
  245. $data=[];
  246. foreach ($list as $value){
  247. $customer = Db::name("customer_info")->where(["companyNo"=>$value['customer_code']])->find();
  248. $value['customer_name'] = isset($customer['companyName']) ?trim($customer['companyName']):'';
  249. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  250. $data[]=$value;
  251. }
  252. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  253. }
  254. /**
  255. * @return \think\response\Json|void
  256. * @throws \think\db\exception\DataNotFoundException
  257. * @throws \think\db\exception\DbException
  258. * @throws \think\db\exception\ModelNotFoundException
  259. */
  260. public function info()
  261. {
  262. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode'] != "" ? trim($this->post['returnCode']) : "";
  263. if ($bkcode == "") {
  264. return error_show(1005, "参数returnCode 不能为空");
  265. }
  266. $info = Db::name("order_return")->alias("a")->leftJoin("order_returninfo b", "a.returnCode=b.returnCode")
  267. ->field("a.*,b.return_wsm,b.contactor,b.mobile,b.addr,b.addr_code,b.post_code,b.post_company,b.post_fee,b.gys_remark")
  268. ->where(['a.returnCode' => $bkcode])->find();
  269. if (empty($info)) {
  270. return error_show(1005, "未找到售后数据");
  271. }
  272. $orderinfo = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->find();
  273. $info['origin_price'] = $orderinfo['origin_price'];
  274. $info['order_type'] = $orderinfo['order_type'];
  275. $info['sale_price'] = $orderinfo['sale_price'];
  276. $info['total_price'] = sprintf("%1\$.2f", $orderinfo['sale_price'] * $info['error_num']);
  277. if ($orderinfo['order_type'] == 3) {
  278. $goon = Db::name("good_zixun")->where(["spuCode" => $orderinfo['good_code'], "is_del" => 0])->find();
  279. } else {
  280. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  281. ->where(['a.skuCode' => $orderinfo['skuCode']])->find();
  282. }
  283. if (empty($goon)) {
  284. return error_show(1003, "未找到商品数据");
  285. }
  286. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  287. $addcode = '';
  288. if ($info['addr_code'] != "") {
  289. list($a, $b, $c) = explode(",", $info['addr_code']);
  290. $addcode = json_encode(["provice_code" => $a, "city_code" => $b, "area_code" => $c]);
  291. }
  292. $info["addr_cn"]=GetAddr($addcode);
  293. // $info["addr_code"]=json_decode($addcode,true);
  294. $out =Db::name("order_out")->where(["outCode"=>$info['outCode'],"is_del"=>0])->find();
  295. $info['out_num'] = isset($out['send_num'])?$out['send_num']:'';
  296. // $customer = Db::name("customer_info")->where(["companyNo"=>$info['customer_code']])->find();
  297. $wsmcode=[];
  298. if($info['return_wsm']!=''){
  299. $wsmcode = Db::name("warehouse_info")
  300. ->alias("a")
  301. ->leftJoin("supplier b","a.supplierNo=b.code")
  302. ->where(["a.wsm_code"=>$info['return_wsm']])
  303. ->field("a.name as wsm_name,b.name,b.code,a.wsm_code,a.contactor_name,a.position,a.mobile,a.addrs_code,a.addr,a.companyNo")
  304. ->find();
  305. }
  306. $info['wsm_code'] =isset($wsmcode['wsm_code']) ? $wsmcode['wsm_code']:"";
  307. $info['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  308. $info['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  309. $info['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  310. $customer=[];
  311. if(isset($orderinfo['supplierNo'])&&$orderinfo['supplierNo']!=''){
  312. $customer = Db::name("business")->field('id,company,companyNo')->where(["companyNo"=>$orderinfo['supplierNo']])->find();
  313. }
  314. $info['customer_name'] = isset($customer['company']) ?trim($customer['company']):'';
  315. $info['customer_code'] = isset($customer['companyNo']) ?trim($customer['companyNo']):'';
  316. $info['can']=$int;
  317. //仓库的联系人、职位、电话、省市区和地址
  318. isset($wsmcode['contactor_name'])? $info['contactor'] =$wsmcode['contactor_name']:'';//联系人
  319. isset($wsmcode['mobile'])? $info['mobile'] =$wsmcode['mobile']:'';//联系人
  320. isset($wsmcode['addrs_code'])? $info['addr_code'] =$wsmcode['addrs_code']:'';//联系人
  321. isset($wsmcode['addr'])? $info['addr'] =$wsmcode['addr']:'';//联系人
  322. isset($wsmcode['contactor_name'])? $info['contactor'] =$wsmcode['contactor_name']:'';//联系人
  323. // $info['position'] = $wsmcode['position'];//职位
  324. // $info['mobile'] = $wsmcode['mobile'];//电话
  325. // $info['addrs_code'] = json_decode($wsmcode['addrs_code'], true);//省市区编码
  326. // $info['addr'] = $wsmcode['addr'];//地址
  327. $info['spuCode'] = $orderinfo['good_code'];//地址
  328. $info['skuCode'] = $orderinfo['skuCode'];//地址
  329. $info['return_tag'] = $info['return_tag']==0?"":$info['return_tag'];//地址
  330. $info['is_th'] = $info['is_th']==0?"":$info['is_th'];//地址
  331. return app_show(0,"获取成功",$info);
  332. }
  333. public function status(){
  334. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  335. if($bkcode==""){
  336. return error_show(1005,"参数returnCode 不能为空");
  337. }
  338. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  339. if(empty($info)){
  340. return error_show(1005,"未找到售后数据");
  341. }
  342. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']) :"";
  343. if($status===""){
  344. return error_show(1005,"参数status 不能为空");
  345. }
  346. $orde= Db::name("order_out")->where(["outCode"=>$info['outCode'],"is_del"=>0])->find();
  347. if($orde==false){
  348. return error_show(1005,"未找到出库单数据");
  349. }
  350. $sale = Db::name("sale")->where(["orderCode"=> $info['orderCode']])->find();
  351. if($sale==false){
  352. return error_show(1005,"未找到销售单数据");
  353. }
  354. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  355. if($remark!=''){
  356. $info['remark'] =$remark;
  357. }
  358. if($status==3 && $info['is_receive']==1){
  359. $post =isset($this->post['is_post'])&&$this->post['is_post']!==""? intval($this->post['is_post']):"";
  360. if($post===""){
  361. return error_show(1005,"参数is_post不能为空");
  362. }
  363. $info['is_post']=$post;
  364. $return_tag =isset($this->post['return_tag'])&&$this->post['return_tag']!==""? intval($this->post['return_tag']):"";
  365. if($return_tag===""){
  366. return error_show(1005,"参数return_tag不能为空");
  367. }
  368. $info['return_tag']=$return_tag;
  369. $is_th =isset($this->post['is_th'])&&$this->post['is_th']!==""? intval($this->post['is_th']):"0";
  370. $info['is_th']=$is_th;
  371. }
  372. // $status = $info['order_status']==1&&$info['order_type']==1 && $status==1?4:$status;
  373. Db::startTrans();
  374. try{
  375. $var=$info['status'];
  376. $info['status']=$status;
  377. $info['updatetime']=date("Y-m-d H:i:s");
  378. $up =Db::name("order_return")->save($info);
  379. if($up){
  380. if($status==5 && $info['is_receive']==0){
  381. $od_status = $orde['status'];
  382. $orde['status']=4;
  383. $orde['updatetime']=date("Y-m-d H:i:s");
  384. $out = Db::name("order_out")->save($orde);
  385. if($out==false){
  386. Db::rollback();
  387. return error_show(1004,"发货单更新失败");
  388. }
  389. $order = ["order_code"=>$orde['outCode'],"status"=>$od_status,"action_remark"=>'',"action_type"=>"edit"];
  390. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"CKD", $orde['status'],$this->post);
  391. $process=["order_code"=>$orde['outCode'],"order_id"=>$orde['id'],"order_status"=>$orde['status'],"order_type"=>"CKD","before_status"=>$od_status];
  392. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  393. $sale['th_num'] += $info['error_num'];
  394. $sale['th_fee'] += round($info['error_num']*$sale['sale_price'],2);
  395. $sale['updatetime']=date("Y-m-d H:i:s");
  396. $sup =Db::name("sale")->save($sale);
  397. if($sup==false){
  398. Db::rollback();
  399. return error_show(1004,"销售单更新失败");
  400. }
  401. $data=[
  402. "orderCode"=>$info['orderCode'],
  403. "th_type"=>2,
  404. "th_num"=>$info['error_num'],
  405. "th_fee"=>round($info['error_num']*$sale['sale_price'],2),
  406. "thCode"=>$info['returnCode'],
  407. "spuCode"=>$sale['good_code'],
  408. "good_name"=>$sale['good_name'],
  409. "cat_id"=>$sale['cat_id'],
  410. "apply_id"=>$info['apply_id'],
  411. "apply_name"=>$info['apply_name'],
  412. "addtime"=>date("Y-m-d H:i:s"),
  413. "status"=>1,
  414. "is_del"=>0
  415. ];
  416. $inse=Db::name("th_data")->insert($data);
  417. if($inse==false){
  418. Db::rollback();
  419. return error_show(1004,"退回单更新失败");
  420. }
  421. }
  422. $order = ["order_code"=>$info['returnCode'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  423. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"SHD", $info['status'],$this->post);
  424. // switch ($status) {
  425. // //待采购审核
  426. // case 2:
  427. // $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $status, "order_type" => "SHD", "before_status" => $var, 'wait_id' => $info['person_id'], 'wait_name' => $info['person'], 'holder_id' => $info['person_id']];
  428. // break;
  429. //
  430. // //待设置仓库
  431. // case 3:
  432. // $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $status, "order_type" => "SHD", "before_status" => $var, 'wait_id' => $info['cgderid'], 'wait_name' => $info['cgder'], 'holder_id' => $info['cgderid']];
  433. // break;
  434. //
  435. // //待客户退货
  436. // case 4:
  437. // $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $status, "order_type" => "SHD", "before_status" => $var, 'wait_id' => $info['apply_id'], 'wait_name' => $info['apply_name'], 'holder_id' => $info['apply_id']];
  438. // break;
  439. //
  440. // default:
  441. // $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $status, "order_type" => "SHD", "before_status" => $var, 'holder_id' => $info['apply_id']];
  442. // }
  443. // if (in_array($status, [2, 3])) $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $status, "order_type" => "SHD", "before_status" => $var, 'wait_id' => $info['cgderid'], 'wait_name' => $info['cgder'],'holder_id'=>$info['cgderid']];
  444. // elseif ($status == 4) $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $status, "order_type" => "SHD", "before_status" => $var, 'wait_id' => $info['apply_id'], 'wait_name' => $info['apply_name'],'holder_id'=>$info['apply_id']];
  445. // else $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $status, "order_type" => "SHD", "before_status" => $var,'holder_id'=>$info['apply_id']];
  446. $process = ["order_code" => $info['returnCode'], "order_id" => $info['id'], "order_status" => $status, "order_type" => "SHD", "before_status" => $var, 'wait_id' => $info['person_id'], 'wait_name' => $info['person'], 'holder_id' => $info['person_id']];
  447. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  448. Db::commit();
  449. return app_show(0,"更新成功");
  450. }else{
  451. Db::rollback();
  452. return error_show(1004,"更新失败");
  453. }
  454. }catch (\Exception $e){
  455. Db::rollback();
  456. return error_show(1004,$e->getMessage());
  457. }
  458. }
  459. public function setWsm(){
  460. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  461. if($bkcode==""){
  462. return error_show(1005,"参数returnCode 不能为空");
  463. }
  464. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  465. if(empty($info)){
  466. return error_show(1005,"未找到售后数据");
  467. }
  468. if($info['status']!=3){
  469. return error_show(1005,"采购主管未审核无法设置退回仓库");
  470. }
  471. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']) :"";
  472. if($info['return_tag']==2) {
  473. if($wsm_code==""){
  474. return error_show(1005,"参数 wsm_code 不能为空");
  475. }
  476. $warehouse =Db::name("warehouse_info")->where(["wsm_code"=>$wsm_code])->find();
  477. if($warehouse==false){
  478. return error_show(1005,"仓库数据未找到");
  479. }
  480. $wareaddr =Db::name("warehouse_addr")->where(["wsm_code"=>$wsm_code,"is_del"=>0,"status"=>1])->find();
  481. }
  482. $addrcode=isset($this->post['addr_code'])&&$this->post['addr_code']!=""?$this->post['addr_code']:"";
  483. if($addrcode==""){
  484. return error_show(1005,"参数 addr_code 不能为空");
  485. }
  486. $addr=isset($this->post['addr'])&&$this->post['addr']!=""?trim($this->post['addr']):"";
  487. if($addr==""){
  488. return error_show(1005,"参数 addr 不能为空");
  489. }
  490. $mobile=isset($this->post['mobile'])&&$this->post['mobile']!=""?trim($this->post['mobile']):"";
  491. if($mobile==""){
  492. return error_show(1005,"参数 mobile 不能为空");
  493. }
  494. $contactor=isset($this->post['contactor'])&&$this->post['contactor']!=""?trim($this->post['contactor']):"";
  495. if($contactor==""){
  496. return error_show(1005,"参数 contactor 不能为空");
  497. }
  498. $returninfo=[
  499. "returnCode"=>$bkcode,
  500. "return_wsm"=>$wsm_code,
  501. "contactor"=>$contactor,
  502. "mobile"=>$mobile ,
  503. "addr"=>$addr,
  504. "addr_code"=>$addrcode,
  505. "addtime"=>date("Y-m-d H:i:s")
  506. ];
  507. Db::startTrans();
  508. try {
  509. $in=Db::name("order_returninfo")->insert($returninfo);
  510. if($in){
  511. if($info['return_tag']==2 &&$wareaddr==false) {
  512. $addrs =[
  513. "wsm_code"=>$wsm_code,
  514. "wsm_type"=>1,
  515. "wsm_name"=>$contactor,
  516. "wsm_addr"=>$addr,
  517. "addr_code"=>$addrcode,
  518. "wsm_mobile"=>$mobile,
  519. "status"=>'1',
  520. "is_del"=>'0',
  521. "addtime"=>date("Y-m-d H:i:s"),
  522. "updatetime"=>date("Y-m-d H:i:s")
  523. ];
  524. $wareaddr =Db::name("warehouse_addr")->insert($addrs);
  525. if($wareaddr==false){
  526. Db::rollback();
  527. return app_show(1004,"退货仓库地址录入失败");
  528. }
  529. }
  530. $old_info_status = $info['status'];
  531. $info['status']=4;
  532. $info["updatetime"]=date("Y-m-d H:i:s");
  533. $up =Db::name("order_return")->save($info);
  534. if($up){
  535. //修改状态,添加待办
  536. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  537. "order_code" => $info['returnCode'],//销售单code
  538. "status" => $old_info_status,//这里的status是之前的值
  539. "action_remark" => '',//备注
  540. "action_type" => "status"//新建create,编辑edit,更改状态status
  541. ], "SHD", $info['status'], $info);
  542. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  543. "order_type" => 'SHD',
  544. "order_code" => $info['returnCode'],//销售单code
  545. "order_id" => $info['id'],
  546. "order_status" => $info['status'],
  547. "before_status"=>$old_info_status,
  548. 'holder_id'=>$info['apply_id']
  549. ]);
  550. Db::commit();
  551. return app_show(0,"退货仓库设置成功");
  552. }
  553. }
  554. Db::rollback();
  555. return error_show(1004,"设置失败");
  556. }catch (\Exception $e){
  557. Db::rollback();
  558. return error_show(1004,$e->getMessage());
  559. }
  560. }
  561. /**
  562. * @return \think\response\Json|void
  563. * @throws \think\db\exception\DataNotFoundException
  564. * @throws \think\db\exception\DbException
  565. * @throws \think\db\exception\ModelNotFoundException
  566. */
  567. public function postfeed(){
  568. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  569. if($bkcode==""){
  570. return error_show(1005,"参数returnCode 不能为空");
  571. }
  572. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  573. if(empty($info)){
  574. return error_show(1005,"未找到售后数据");
  575. }
  576. $contactor = isset($this->post['contactor']) && $this->post['contactor']!="" ? trim($this->post['contactor']) :"";
  577. $mobile = isset($this->post['mobile']) && $this->post['mobile']!="" ? trim($this->post['mobile']):"";
  578. $addr = isset($this->post['addr']) && $this->post['addr']!="" ? trim($this->post['addr']):"";
  579. $addr_code = isset($this->post['addr_code']) && $this->post['addr_code']!=""&&!empty($this->post['addr_code']) ?
  580. $this->post['addr_code']:"";
  581. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']):"";
  582. if($wsm_code==""){
  583. return error_show(1005,"参数 wsm_code不能为空");
  584. }
  585. if($info['is_th']==0&&$info['order_type']==1) {
  586. $wsm = Db::name("warehouse_info")->where(["wsm_code" => $wsm_code])->find();
  587. if (empty($wsm)) {
  588. return error_show(1005, "未找到仓库数据");
  589. }
  590. $sm = Db::name("warehouse_addr")->where(["wsm_code" => $wsm_code,"is_del"=>0])->find();
  591. $contactor==""?(isset($sm['wsm_name']) ? $contactor=$sm['wsm_name']:""):"";
  592. $sm['wsm_name'] =isset($sm['wsm_name']) ? $sm['wsm_name']:"";
  593. $mobile==""?(isset($sm['wsm_mobile']) ? $mobile=$sm['wsm_mobile']:""):"";
  594. $sm['wsm_mobile'] =isset($sm['wsm_mobile']) ? $sm['wsm_mobile']:"";
  595. $addr==""?(isset($sm['wsm_addr']) ? $addr=$sm['wsm_addr']:""):"";
  596. $sm['wsm_addr'] =isset($sm['wsm_addr']) ? $sm['wsm_addr']:"";
  597. $addr_code==""?(isset($sm['addr_code']) ? $addr=$sm['addr_code']:""):"";
  598. $sm['addr_code'] =isset($sm['addr_code']) ? $sm['addr_code']:"";
  599. }
  600. $remark = isset($this->post['remark']) && $this->post['remark']!="" ? trim($this->post['remark']):"";
  601. if($info['is_th']==1 && $info['order_type']==1){
  602. if($contactor==''){
  603. return error_show(1005,"参数 contactor 不能为空");
  604. }
  605. if($mobile==''){
  606. return error_show(1005,"参数 mobile 不能为空");
  607. }
  608. if($addr==''){
  609. return error_show(1005,"参数 addr 不能为空");
  610. }
  611. if($addr_code==''){
  612. return error_show(1005,"参数 addr_code 不能为空");
  613. }
  614. if($addr_code!==''&&is_array($addr_code)){
  615. $addrs=[];
  616. $addrs['provice_code'] = $addr_code[0];
  617. $addrs['city_code'] = $addr_code[1];
  618. $addrs['area_code'] = $addr_code[2];
  619. $addr_code = json_encode($addrs);
  620. }
  621. }
  622. Db::startTrans();
  623. try{
  624. $var=$info['status'];
  625. $info['status']=$info['order_type']==1?3:4;
  626. $info['remark']=$remark;
  627. $info['updatetime']=date("Y-m-d H:i:s");
  628. $up =Db::name("order_return")->save($info);
  629. if($up){
  630. // var_dump($contactor);
  631. $returninfo = Db::name("order_returninfo")->where(["returnCode"=>$bkcode])->find();
  632. if(empty($returninfo)){
  633. $returninfo=[
  634. "returnCode"=>$bkcode,
  635. "return_wsm"=>$wsm_code,
  636. "contactor"=>$contactor,
  637. "mobile"=>$mobile ,
  638. "addr"=>$addr,
  639. "addr_code"=>$addr_code,
  640. "addtime"=>date("Y-m-d H:i:s")
  641. ];
  642. }else{
  643. $returninfo['return_wsm'] = $wsm_code;
  644. //if($info['is_th']==1 && $info['order_type']==1) {
  645. $contactor !==""? $returninfo['contactor'] = $contactor:"";
  646. $mobile!==""?$returninfo['mobile'] = $mobile:"";
  647. $addr!==""? $returninfo['addr'] = $addr:"";
  648. $addr_code!==""?$returninfo['addr_code'] = $addr_code:"";
  649. // }
  650. }
  651. // var_dump($contactor);
  652. // var_dump($returninfo);
  653. $in=Db::name("order_returninfo")->save($returninfo);
  654. if($in){
  655. $order = ["order_code"=>$info['returnCode'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  656. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"SHD",$info['status'],$this->post);
  657. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$info['status'],"order_type"=>"SHD","before_status"=>$var,'holder_id'=>$info['apply_id']];
  658. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  659. Db::commit();
  660. return app_show(0,"更新成功");
  661. }
  662. }
  663. Db::rollback();
  664. return error_show(1004,"更新失败");
  665. }catch (\Exception $e){
  666. Db::rollback();
  667. return error_show(1004,$e->getMessage());
  668. }
  669. }
  670. /**
  671. * @return \think\response\Json|void
  672. * @throws \think\db\exception\DataNotFoundException
  673. * @throws \think\db\exception\DbException
  674. * @throws \think\db\exception\ModelNotFoundException
  675. */
  676. public function addpost(){
  677. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  678. if($bkcode==""){
  679. return error_show(1005,"参数returnCode 不能为空");
  680. }
  681. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  682. if(empty($info)){
  683. return error_show(1005,"未找到售后数据");
  684. }
  685. $return_info = Db::name("order_returninfo")->where(['returnCode'=>$bkcode])->find();
  686. if(empty($return_info)){
  687. $return_info=[
  688. "returnCode"=>$bkcode,
  689. "return_wsm"=>'',
  690. "contactor"=>'',
  691. "mobile"=>'',
  692. "addr"=>'',
  693. "addr_code"=>'',
  694. "addtime"=>date("Y-m-d H:i:s"),
  695. "post_company"=>'',
  696. "post_code"=>'',
  697. "post_fee"=>'',
  698. ];
  699. }
  700. $post_own = isset($this->post['own']) &&$this->post['own']!='' ? trim($this->post['own']):"";
  701. if($post_own==''){
  702. return error_show(1005,"参数own不能为空");
  703. }
  704. $info['post_own'] = $post_own;
  705. $post_company = isset($this->post['company'])&&$this->post['company']!='' ? trim($this->post['company']):"";
  706. if($post_company==''){
  707. return error_show(1005,"参数company不能为空");
  708. }
  709. $return_info['post_company'] = $post_company;
  710. $post_code = isset($this->post['post_code'])&&$this->post['post_code']!='' ? trim($this->post['post_code']):"";
  711. if($post_code==''){
  712. return error_show(1005,"参数post_code不能为空");
  713. }
  714. $return_info['post_code'] = $post_code;
  715. $fee= isset($this->post['post_fee'])&&$this->post['post_fee']!=='' ? round($this->post['post_fee'],2):"";
  716. if($fee===''){
  717. return error_show(1005,"参数post_fee不能为空");
  718. }
  719. $return_info['post_fee'] = $fee;
  720. Db::startTrans();
  721. try{
  722. $str=$info['status'];
  723. $info['status']=$info['order_type']==1?4:5;
  724. $info['updatetime']=date("Y-m-d H:i:s");
  725. $up =Db::name("order_return")->save($info);
  726. if($up){
  727. $order = ["order_code"=>$info['orderCode'],"status"=>$str,"action_remark"=>'',"action_type"=>"edit"];
  728. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"SHD",$info['status'],$this->post);
  729. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$info['status'],"order_type"=>"SHD","before_status"=>$str,'holder_id'=>$info['apply_id']];
  730. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  731. $in=Db::name("order_returninfo")->save($return_info);
  732. if($in){
  733. $data=[
  734. 'thNo'=>makeNo("ST"),
  735. "orderCode"=>$info['orderCode'],
  736. "order_type"=>$info['order_type'],
  737. "outCode"=>$info['outCode'],
  738. "returnCode"=>$info['returnCode'],
  739. "good_code"=>$info['good_code'],
  740. "good_name"=>$info['good_name'],
  741. "return_wsm"=>$return_info['return_wsm'],
  742. "return_num"=>$info['error_num'],
  743. "contactor"=>$return_info['contactor'],
  744. "mobile"=>$return_info['mobile'],
  745. "addr"=>$return_info['addr'],
  746. "addr_code"=>$return_info['addr_code'],
  747. "return_code"=>$info['error_code'],
  748. "return_msg"=>$info['error_msg'],
  749. "post_fee"=>$return_info['post_fee'],
  750. "post_code"=>$return_info['post_code'],
  751. "post_company"=>$return_info['post_company'],
  752. "customer_code"=>$info['customer_code'],
  753. "addtime"=>date("Y-m-d H:i:s"),
  754. "updatetime"=>date("Y-m-d H:i:s")
  755. ];
  756. $back =Db::name("order_back")->insert($data,true);
  757. if($back>0){
  758. //维护台账
  759. // Db::name('standing_book')
  760. // ->where('returnCode', $bkcode)
  761. // ->update([
  762. // 'thNo' => $data['thNo'],
  763. // 'updatetime' => date("Y-m-d H:i:s")
  764. // ]);
  765. Db::execute("UPDATE `wsm_standing_book` SET `thNo`=CONCAT(IFNULL(`thNo`,''),',{$data['thNo']}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE FIND_IN_SET('{$bkcode}',`returnCode`)");//这里的returnCode可能有多个
  766. $order = ["order_code"=>$data['thNo'],"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  767. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"CKTHD", 0,$data);
  768. $process=["order_code"=>$data['thNo'],"order_id"=>$back,"order_status"=>0,"order_type"=>"CKTHD","before_status"=>0];
  769. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  770. Db::commit();
  771. return app_show(0,"更新成功");
  772. }
  773. }
  774. }
  775. Db::rollback();
  776. return error_show(1004,"更新失败");
  777. }catch (\Exception $e){
  778. Db::rollback();
  779. return error_show(1004,$e->getMessage());
  780. }
  781. }
  782. /**
  783. * @return \think\response\Json|void
  784. * @throws \think\db\exception\DataNotFoundException
  785. * @throws \think\db\exception\DbException
  786. * @throws \think\db\exception\ModelNotFoundException
  787. */
  788. public function GysFeed(){
  789. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  790. if($bkcode==""){
  791. return error_show(1005,"参数returnCode 不能为空");
  792. }
  793. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  794. if(empty($info)){
  795. return error_show(1005,"未找到售后数据");
  796. }
  797. $isth = isset($this->post['is_th']) && $this->post['is_th']!="" ? intval($this->post['is_th']):"0";
  798. $contactor = isset($this->post['contactor']) && $this->post['contactor']!="" ? trim($this->post['contactor'])
  799. :"";
  800. $mobile = isset($this->post['mobile']) && $this->post['mobile']!="" ? trim($this->post['mobile']):"";
  801. $addr = isset($this->post['addr']) && $this->post['addr']!="" ? trim($this->post['addr']):"";
  802. $addr_code = isset($this->post['addr_code']) && $this->post['addr_code']!=""&&!empty($this->post['addr_code']) ? $this->post['addr_code']:"";
  803. $remark = isset($this->post['remark']) && $this->post['remark']!="" ? trim($this->post['remark']):"";
  804. if($isth==1){
  805. if($contactor==''){
  806. return error_show(1005,"参数 contactor 不能为空");
  807. }
  808. if($mobile==''){
  809. return error_show(1005,"参数 mobile 不能为空");
  810. }
  811. if($addr==''){
  812. return error_show(1005,"参数 addr 不能为空");
  813. }
  814. // var_dump(isset($this->post['addr_code']) , $this->post['addr_code']!="",!empty($addr_code));
  815. if($addr_code==''){
  816. return error_show(1005,"参数 addr_code 不能为空");
  817. }
  818. if(is_array($addr_code)){
  819. $addrs=[];
  820. $addrs['provice_code'] = $addr_code[0];
  821. $addrs['city_code'] = $addr_code[1];
  822. $addrs['area_code'] = $addr_code[2];
  823. $addr_code = json_encode($addrs);
  824. }
  825. }
  826. $orde= Db::name("order_out")->where(["outCode"=>$info['outCode'],"is_del"=>0])->find();
  827. if(empty($orde)){
  828. return error_show(1005,"未找到出库单数据");
  829. }
  830. Db::startTrans();
  831. try{
  832. $stat=$info['status'];
  833. $info['status']=$info['order_status']==1?5:3;
  834. $info['is_th']=$isth;
  835. $info['updatetime']=date("Y-m-d H:i:s");
  836. $up =Db::name("order_return")->save($info);
  837. if($up){
  838. $returninfo = Db::name("order_returninfo")->where(["returnCode"=>$bkcode])->find();
  839. if(empty($returninfo)){
  840. $returninfo=[
  841. "returnCode"=>$bkcode,
  842. "return_wsm"=>'',
  843. "contactor"=>$contactor,
  844. "mobile"=>$mobile ,
  845. "addr"=>$addr,
  846. "addr_code"=>$addr_code,
  847. "gys_remark"=>$remark,
  848. "addtime"=>date("Y-m-d H:i:s")
  849. ];
  850. }else{
  851. $returninfo['return_wsm'] = '';
  852. $returninfo['contactor'] = $contactor;
  853. $returninfo['mobile'] = $mobile;
  854. $returninfo['addr'] = $addr;
  855. $returninfo['gys_remark'] = $remark;
  856. $returninfo['addr_code'] = $addr_code;
  857. }
  858. $in=Db::name("order_returninfo")->save($returninfo);
  859. if($in){
  860. $order = ["order_code"=>$info['returnCode'],"status"=>$stat,"action_remark"=>'',"action_type"=>"edit"];
  861. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname],$order,"SHD", $info['status'],$this->post);
  862. $process=["order_code"=>$info['returnCode'],"order_id"=>$info['id'],"order_status"=>$info['status'],"SHD","before_status"=>$stat,'holder_id'=>$info['apply_id']];
  863. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$process);
  864. Db::commit();
  865. return app_show(0,"更新成功");
  866. }
  867. }
  868. Db::rollback();
  869. return error_show(1004,"更新失败");
  870. }catch (\Exception $e){
  871. Db::rollback();
  872. return error_show(1004,$e->getMessage());
  873. }
  874. }
  875. public function GetWsm(){
  876. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  877. if($bkcode==""){
  878. return error_show(1005,"参数returnCode 不能为空");
  879. }
  880. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  881. if($info==false){
  882. return error_show(1005,"未找到售后数据");
  883. }
  884. $list=[];
  885. if($info['is_th']==1){
  886. if($info["order_type"]==1){
  887. $out =Db::name("order_out")->where(["outCode"=>$info['outCode'],"is_del"=>0])->find();
  888. if(empty($out)){
  889. return error_show(1005,"未找到出库单数据");
  890. }
  891. $ware = Db::name("warehouse_info")->alias("a")->leftJoin("warehouse_addr b","a.wsm_code=b.wsm_code and b.status=1 and b.is_del=0")
  892. ->where(["a.status"=>1,"wsm_type"=>1,"a.is_del"=>0,"a.supplierNo"=>$out['wsm_code']])->field("a.wsm_code,a.name,b.wsm_name,b.wsm_mobile,b.wsm_addr,b.addr_code")->find();
  893. if(empty($ware)){
  894. return error_show(1005,"未找到仓库数据");
  895. }
  896. $temp['wsm_code']=isset($ware['wsm_code'])?$ware['wsm_code']:"";
  897. $temp['wsm_name']=isset($ware['name'])?$ware['name']:"";
  898. $temp['contactor'] = isset($ware['wsm_name'])?$ware['wsm_name']:"";
  899. $temp['mobile'] =isset($ware['wsm_mobile'])?$ware['wsm_mobile']:"";
  900. $temp['addr'] =isset($ware['wsm_addr'])?$ware['wsm_addr']:"";
  901. $temp['addr_code'] = isset($ware['addr_code'])?$ware['addr_code']:"";
  902. $temp['addr_cn'] =GetAddr( $ware['addr_code']);
  903. $list[]=$temp;
  904. }else {
  905. $returninfo = Db::name("order_returninfo")->where(["returnCode" => $bkcode])->find();
  906. if (empty($returninfo)) {
  907. return error_show(1005, "未找到供应商仓库数据");
  908. }
  909. $data = [];
  910. $data['wsm_code'] = isset($returninfo['return_wsm']) && $returninfo['return_wsm'] != "" ? $returninfo['return_wsm'] : "-";
  911. $data['wsm_name'] = '供应商仓';
  912. $data['supplier_name'] = '供应商';
  913. $data['contactor'] = $returninfo['contactor'];
  914. $data['mobile'] = $returninfo['mobile'];
  915. $data['addr'] = $returninfo['addr'];
  916. $data['addr_cn'] =GetAddr( $returninfo['addr_code']);
  917. $data['addr_code'] = $returninfo['addr_code'];
  918. $list[] = $data;
  919. }
  920. }
  921. $supplier = Db::name("supplier")->where(["is_platform"=>1,"status"=>1,"is_del"=>0])->column("code,name");
  922. if(empty($supplier)){
  923. return error_show(1005,"未找到供应商仓库数据");
  924. }
  925. foreach ($supplier as $value){
  926. $temp=[];
  927. $ware = Db::name("warehouse_info")->alias("a")->leftJoin("warehouse_addr b","a.wsm_code=b.wsm_code and b.status=1 and b.is_del=0")
  928. ->where(["a.wsm_type"=>1,"a.status"=>1,"a.is_del"=>0,"a.supplierNo"=>$value['code']])->field("a.wsm_code,a.name,b.wsm_name,b.wsm_mobile,b.wsm_addr,b.addr_code")->find();
  929. if(empty($ware)){
  930. continue;
  931. }
  932. $temp['wsm_code']=isset($ware['wsm_code'])?$ware['wsm_code']:"";
  933. $temp['wsm_name']=isset($ware['name'])?$ware['name']:"";
  934. $temp['supplier_name']=$value['name'];
  935. $temp['contactor'] = isset($ware['wsm_name'])?$ware['wsm_name']:"";
  936. $temp['mobile'] =isset($ware['wsm_mobile'])?$ware['wsm_mobile']:"";
  937. $temp['addr'] =isset($ware['wsm_addr'])?$ware['wsm_addr']:"";
  938. $temp['addr_cn'] =GetAddr($ware['addr_code']);
  939. $temp['addr_code'] = isset($ware['addr_code'])?$ware['addr_code']:"";
  940. $list[]=$temp;
  941. }
  942. return app_show(0,"获取成功",$list);
  943. }
  944. public function setdelivery(){
  945. $bkcode = isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  946. if($bkcode==""){
  947. return error_show(1005,"参数returnCode 不能为空");
  948. }
  949. $info = Db::name("order_return")->where(['returnCode'=>$bkcode])->find();
  950. if(empty($info)){
  951. return error_show(1005,"未找到售后数据");
  952. }
  953. if(($info['is_receive']==0&&$info['status']!=3)||($info['is_receive']==1&&$info['status']!=4) ){
  954. return error_show(1005,"售后单流程状态有误");
  955. }
  956. $out=Db::name("order_out")->where(["outCode"=>$info["outCode"],"is_del"=>0])->find();
  957. if($out==false){
  958. return error_show(1005,"未找到发货单数据");
  959. }
  960. $is_th = isset($this->post['is_th'])&&$this->post['is_th']!=="" ? intval($this->post['is_th']):"";
  961. if($is_th===""){
  962. return error_show(1005,"参数is_th不能为空");
  963. }
  964. $company=isset($this->post['company'])&&$this->post['company']!=""?trim($this->post['company']):"";
  965. $post_code=isset($this->post['post_code'])&&$this->post['post_code']!=""?trim($this->post['post_code']):"";
  966. $post_fee=isset($this->post['post_fee'])&&$this->post['post_fee']!==""?floatval($this->post['post_fee']):"";
  967. if($is_th==1){
  968. if($company==""){
  969. return error_show(1005,"参数company不能为空");
  970. }
  971. if($post_code==""){
  972. return error_show(1005,"参数post_code不能为空");
  973. }
  974. if($post_fee===""){
  975. return error_show(1005,"参数post_fee不能为空");
  976. }
  977. }
  978. $sale= Db::name('sale')->where(['orderCode'=>$info['orderCode'],"is_del"=>0])->find();
  979. if($sale==false){
  980. Db::rollback();
  981. return error_show(1004,"未找到销售单数据");
  982. }
  983. Db::startTrans();
  984. try {
  985. $returninfo = Db::name("order_returninfo")->where(["returnCode"=>$bkcode])->find();
  986. if($returninfo==false){
  987. $returninfo=[
  988. "returnCode"=>$bkcode,
  989. "return_wsm"=>"",
  990. "contactor"=>"",
  991. "mobile"=>"",
  992. "addr"=>"",
  993. "addr_code"=>"",
  994. "post_code"=>$post_code,
  995. "post_company"=>$company,
  996. "post_fee"=>$post_fee,
  997. "gys_remark"=>"",
  998. "addtime"=>date("Y-m-d H:i:s")
  999. ];
  1000. }else{
  1001. $returninfo['post_fee']=$post_fee;
  1002. $returninfo['post_company']=$company;
  1003. $returninfo['post_code']=$post_code;
  1004. }
  1005. $up =Db::name("order_returninfo")->save($returninfo);
  1006. if($up){
  1007. $old_info_status = $info['status'];
  1008. $info['status']=5;
  1009. $info['updatetime']=date("y-m-d H:i:s");
  1010. $ro =Db::name("order_return")->save($info);
  1011. if($ro){
  1012. //修改状态,添加待办
  1013. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1014. "order_code" => $info['returnCode'],//销售单code
  1015. "status" => $old_info_status,//这里的status是之前的值
  1016. "action_remark" => '',//备注
  1017. "action_type" => "status"//新建create,编辑edit,更改状态status
  1018. ], "SHD", $info['status'], $this->post);
  1019. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  1020. "order_type" => 'SHD',
  1021. "order_code" => $info['returnCode'],//销售单code
  1022. "order_id" => $info['id'],
  1023. "order_status" => $info['status'],
  1024. "before_status"=>$old_info_status,
  1025. 'holder_id'=>$info['apply_id']
  1026. ]);
  1027. $thNo =makeNo("ST");
  1028. $thdata=[
  1029. "thNo"=>$thNo,
  1030. "orderCode"=>$out["orderCode"],
  1031. "outCode"=>$out["outCode"],
  1032. "order_type"=>$info["order_type"],
  1033. "returnCode"=>$info["returnCode"],
  1034. "good_code"=>$info["good_code"],
  1035. "good_name"=>$info["good_name"],
  1036. "return_wsm"=>$returninfo["return_wsm"],
  1037. "total_fee"=>round($sale['sale_price']*$info["error_num"],2),
  1038. "good_price"=>$sale['sale_price'],
  1039. "platform_id"=>$sale['platform_id'],
  1040. "return_msg"=>$info["error_msg"],
  1041. "return_num"=>$info["error_num"],
  1042. "normal_num"=>0,
  1043. "received_num"=>0,
  1044. "apply_id"=>$info['apply_id'],
  1045. "apply_name"=>$info['apply_name'],
  1046. "cgder"=>$info['cgder'],
  1047. "cgderid"=>$info['cgderid'],
  1048. "person"=>$info['person'],
  1049. "person_id"=>$info['person_id'],
  1050. "contactor"=>$returninfo["contactor"],
  1051. "mobile"=>$returninfo["mobile"],
  1052. "addr"=>$returninfo["addr"],
  1053. "addr_code"=>$returninfo["addr_code"],
  1054. "return_code"=>$info["error_code"],
  1055. "post_fee"=>$returninfo["post_fee"],
  1056. "post_code"=>$returninfo["post_code"],
  1057. "post_company"=>$returninfo["post_company"],
  1058. "customer_code"=>$info["customer_code"],
  1059. "status"=>$info['return_tag']==1?4:1,
  1060. "addtime"=>date("Y-m-d H:i:s"),
  1061. "updatetime"=>date("Y-m-d H:i:s")
  1062. ];
  1063. $sav= Db::name("order_back")->insert($thdata,true);
  1064. if($sav){
  1065. //修改状态,添加待办
  1066. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1067. "order_code" => $thdata['thNo'],//销售单code
  1068. "status" => $thdata['status'],//这里的status是之前的值
  1069. "action_remark" => '',//备注
  1070. "action_type" => "create"//新建create,编辑edit,更改状态status
  1071. ], "CKTHD", $thdata['status'], $thdata);
  1072. //售后退货单,供应商不同意退货,退回到业务公司仓的时候,要把待办数据推给供应商负责人和库管人员
  1073. if($thdata['status']==1){
  1074. //31库管人员,41库管-张凯旋
  1075. $uids = Db::name('user_role')
  1076. ->where(['is_del' => 0, 'roleid' => [31, 41], 'status' => 1])
  1077. ->column('uid');
  1078. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  1079. "order_type" => 'CKTHD',
  1080. "order_code" => $thdata['thNo'],//销售单code
  1081. "order_id" => $sav,
  1082. "order_status" => $thdata['status'],
  1083. "before_status" => 0,
  1084. 'handle_user_list' => implode(',', array_merge($uids,[$sale['cgderid']])),
  1085. ]);
  1086. }else{
  1087. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  1088. "order_type" => 'CKTHD',
  1089. "order_code" => $thdata['thNo'],//销售单code
  1090. "order_id" => $sav,
  1091. "order_status" => $thdata['status'],
  1092. "before_status"=>0
  1093. ]);
  1094. }
  1095. //维护台账记录
  1096. // Db::name('standing_book')
  1097. // ->where(['returnCode' => $bkcode])
  1098. // ->update([
  1099. // 'thNo' => $thNo,
  1100. // 'updatetime' => date("Y-m-d H:i:s")
  1101. // ]);
  1102. Db::execute("UPDATE `wsm_standing_book` SET `thNo`=CONCAT(IFNULL(`thNo`,''),',{$thNo}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE FIND_IN_SET('{$bkcode}',`returnCode`)");//这里的returnCode可能有多个
  1103. // 库存品退货 不退商品 需要计算成本
  1104. // if($info['except_code']==1){
  1105. // $sabebn =Db::name("sale_info")->where(["orderCode"=>$info['orderCode']])->select()->toArray();
  1106. // if(!empty($sabebn)){
  1107. // $total_num =$info["error_num"];
  1108. // foreach ($sabebn as $ve){
  1109. //
  1110. // $tempnum=0;
  1111. // if ($total_num==0) break;
  1112. // if($total_num>=$ve['num']){
  1113. // $tempnum = $ve['num'];
  1114. // $total_num-=$ve['num'];
  1115. // $ve['num']=0;
  1116. // }else{
  1117. // $tempnum = $total_num;
  1118. // $ve['num']-=$total_num;
  1119. // $total_num=0;
  1120. // }
  1121. // $bnin=GoodStockInfo::ReturnAdd($stockid,$ve['bnCode'],$tempnum,$ve['stockid']);
  1122. // if($bnin==false){
  1123. // Db::rollback();
  1124. // return error_show(1005, '可售商品Bn库存数入库失败');
  1125. // }
  1126. // $up=Db::name("sale_info")->save($ve);
  1127. // if($up==false){
  1128. // Db::rollback();
  1129. // return error_show(1005, '可售商品Bn库存数入库失败');
  1130. // }
  1131. // $bnin=GoodStockInfo::ReturnBn($info['returnCode'],$ve['id'],$tempnum);
  1132. // if($bnin==false){
  1133. // Db::rollback();
  1134. // return error_show(1005, '可售商品Bn库存数入库失败');
  1135. // }
  1136. // }
  1137. // }
  1138. // }else{
  1139. //
  1140. // }
  1141. if($info['return_tag']==1){
  1142. $sale['th_num']+= $info['error_num'];
  1143. if($sale['th_num']==$sale['send_num']&& $sale['wsend_num']==0){
  1144. $sale['status']=3;
  1145. }
  1146. $sale['th_fee']+= round($info['error_num']*$sale['sale_price'],2);
  1147. $sale['updatetime']= date("Y-m-d H:i:s");
  1148. $uap=Db::name("sale")->save($sale);
  1149. if($uap==false){
  1150. Db::rollback();
  1151. return error_show(1005,'销售单订单更新失败');
  1152. }
  1153. if($sale['is_stock']==0){
  1154. $ordernum =Db::name("order_num")->where(['orderCode'=>$info['orderCode']])->find();
  1155. if($ordernum==false){
  1156. Db::rollback();
  1157. return error_show(1005,'未找到关联采购单');
  1158. }
  1159. $ordernum['send_num']-=$info['error_num'];
  1160. $orderup =Db::name("order_num")->save($ordernum);
  1161. if($orderup==false){
  1162. Db::rollback();
  1163. return error_show(1005,'关联数据更新失败');
  1164. }
  1165. $cgd =Db::name("purchease_order")->where(["cgdNo"=>$ordernum['cgdNo'],"is_del"=>0])->find();
  1166. if($cgd==false){
  1167. Db::rollback();
  1168. return error_show(1005,'未找到采购单数据');
  1169. }
  1170. $cgd['th_num']+= $info['error_num'];
  1171. if($cgd['th_num']==$cgd['send_num']&& $cgd['wsend_num']==0){
  1172. $cgd['status']=4;
  1173. }
  1174. $cgd['th_fee']+= round($info['error_num']*$cgd['good_price'],2);
  1175. $cgd['updatetime']= date("Y-m-d H:i:s");
  1176. $cgdup =Db::name("purchease_order")->save($cgd);
  1177. if($cgdup==false){
  1178. Db::rollback();
  1179. return error_show(1005,'采购单数据更新失败');
  1180. }
  1181. }
  1182. $sabebn =Db::name("sale_info")->where(["orderCode"=>$info['orderCode']])->select()->toArray();
  1183. if(!empty($sabebn)){
  1184. $total_num =$info["error_num"];
  1185. foreach($sabebn as $ve){
  1186. $tempnum=0;
  1187. if ($total_num==0) break;
  1188. if($total_num>=$ve['num']){
  1189. $tempnum = $ve['num'];
  1190. $total_num-=$ve['num'];
  1191. $ve['th_num']+=$ve['num'];
  1192. $ve['num']=0;
  1193. }else{
  1194. $tempnum = $total_num;
  1195. $ve['num']-=$total_num;
  1196. $ve['th_num']+=$total_num;
  1197. $total_num=0;
  1198. }
  1199. if( $info['except_code']==1){
  1200. // if($sale['is_stock']==1){
  1201. // $bnin=GoodStockInfo::AddBn($ve['stockid'],$ve['bnCode'],$tempnum);
  1202. // if($bnin==false){
  1203. // Db::rollback();
  1204. // return error_show(1005, '可售商品Bn库存数入库失败');
  1205. // }
  1206. // }
  1207. $up=Db::name("sale_info")->save($ve);
  1208. if($up==false){
  1209. Db::rollback();
  1210. return error_show(1005, '可售商品Bn库存数入库失败');
  1211. }
  1212. }
  1213. $bnin=GoodStockInfo::ReturnBn($info['returnCode'],$ve['id'],$tempnum);
  1214. if($bnin==false){
  1215. Db::rollback();
  1216. return error_show(1005, '可售商品Bn库存数入库失败');
  1217. }
  1218. }
  1219. }
  1220. if($info['error_num']>=$out['send_num']){
  1221. $out['status']=4;
  1222. $out['updatetime']=date("Y-m-d H:i:s");
  1223. $upout =Db::name("order_out")->save($out);
  1224. if($upout==false){
  1225. Db::rollback();
  1226. return error_show(1005,'出库单数据更新失败');
  1227. }
  1228. }
  1229. $data=[
  1230. "orderCode"=>$info['orderCode'],
  1231. "th_type"=>3,
  1232. "th_num"=>$info['error_num'],
  1233. "th_fee"=>round($sale['sale_price']*$info["error_num"],2),
  1234. "thCode"=>$thNo,
  1235. "spuCode"=>$sale['good_code'],
  1236. "good_name"=>$sale['good_name'],
  1237. "cat_id"=>$sale['cat_id'],
  1238. "apply_id"=>$info['apply_id'],
  1239. "apply_name"=>$info['apply_name'],
  1240. "addtime"=>date("Y-m-d H:i:s"),
  1241. "status"=>1,
  1242. "is_del"=>0
  1243. ];
  1244. $inse=Db::name("th_data")->insert($data);
  1245. if($inse==false){
  1246. Db::rollback();
  1247. return error_show(1004,"退货单更新失败");
  1248. }
  1249. }
  1250. Db::commit();
  1251. return app_show(0,"退货单新建成功",["thNo"=>$thNo]);
  1252. }
  1253. }
  1254. }
  1255. Db::rollback();
  1256. return error_show(1004,'退货失败');
  1257. }catch (\Exception $e){
  1258. Db::rollback();
  1259. return error_show(1004,$e->getMessage());
  1260. }
  1261. }
  1262. //取消售后
  1263. public function Cancel()
  1264. {
  1265. //接收参数
  1266. $data = $this->request->only(['token', 'returnCode'], 'post', 'trim');
  1267. $val = Validate::rule(['returnCode|售后单编号' => 'require']);
  1268. if (!$val->check($data)) return error_show(1005, $val->getError());
  1269. Db::startTrans();
  1270. try {
  1271. $rs = Db::name('order_return')->field('id,status,apply_id')->where([['returnCode', '=', $data['returnCode']], ['is_del', '=', 0], ['status', '<>', 8]])->find();
  1272. if (!$rs) throw new Exception('该售后单记录不存在');
  1273. //修改记录
  1274. $res = Db::name('order_return')->where(['id' => $rs['id']])->update(['status' => 8, 'updatetime' => date('Y-m-d H:i:s')]);
  1275. if (!$res) throw new Exception('该售后单记录修改失败');
  1276. //记录日志
  1277. ActionLog::logAdd($data['token'], ["order_code" => $data['returnCode'], "status" => $rs['status'], "action_remark" => '', "action_type" => "delete"], 'SHD', 8, $this->post);
  1278. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], ["order_code" => $data['returnCode'], "order_id" => $rs['id'], "order_status" => 8, "order_type" => 'SHD',"before_status"=>$rs['status'],'holder_id'=>$rs['apply_id']]);
  1279. //提交
  1280. Db::commit();
  1281. return app_show(0, "取消成功");
  1282. } catch (\Exception $e) {
  1283. Db::rollback();
  1284. return error_show(1004, $e->getMessage());
  1285. }
  1286. }
  1287. //根据审核完成的售后申请单,创建新的销售单和采购单
  1288. public function createSaleAndCgdByAfter()
  1289. {
  1290. $param = $this->request->only(['token', 'returnCode'], 'post', 'trim');
  1291. $val = Validate::rule(['token' => 'require', 'returnCode|售后申请单编号' => 'require']);
  1292. if (!$val->check($param)) return error_show(1005, $val->getError());
  1293. //查询售后申请单详情
  1294. $info = Db::name('order_return')
  1295. ->where('returnCode', $param['returnCode'])
  1296. ->where('is_del', 0)
  1297. ->find();
  1298. if (empty($info)) return error_show(1005, '该售后申请单不存在');
  1299. if ($info['status'] != 5) return error_show(1005, '该售后申请单尚未审核完成');
  1300. if ($info['is_reissue'] == 1) return error_show(1005, '该售后申请单已完成补发,无法重复进行');
  1301. $apply_id = GetUserInfo($param['token']);
  1302. if (empty($apply_id) || $apply_id['code'] != 0) return error_show(1002, "申请人数据不存在");
  1303. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1304. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1305. //原本销售单信息
  1306. $old_sale = Db::name('sale')->where(['is_del' => 0, 'orderCode' => $info['orderCode']])->find();
  1307. if (empty($old_sale)) return error_show(1005, '没有对应的销售单信息');
  1308. //发货单信息
  1309. $old_out = Db::name('order_out')->where(['is_del' => 0, 'orderCode' => $info['orderCode'], 'outCode' => $info['outCode']])->find();
  1310. if (empty($old_out)) return error_show(1005, '没有对应的发货单信息');
  1311. //根据是否咨询类商品,分别处理
  1312. if ($info['order_type'] == 3) return $this->createSaleZixun($info['id'], $info['good_code'], $info, $old_sale, $rm, $ri, $old_out['addrid'], $param['token']);
  1313. else return $this->createSaleNotZixun($info['id'], $info['good_code'], $info, $old_sale, $rm, $ri, $old_out, $param['token']);
  1314. }
  1315. /**
  1316. * 咨询类商品处理
  1317. * @param int $orid 对应售后记录的id
  1318. * @param string $spuCode 商品的spuCode
  1319. * @param array $info 售后申请单的详情
  1320. * @param array $old_sale 原始销售订单
  1321. * @param int $rm 操作人id
  1322. * @param string $ri 操作人名称
  1323. * @param int $addrid 原始发货单的地址id
  1324. * @param string $token 当前用户的token
  1325. * @throws \think\db\exception\DataNotFoundException
  1326. * @throws \think\db\exception\DbException
  1327. * @throws \think\db\exception\ModelNotFoundException
  1328. */
  1329. private function createSaleZixun(int $orid = 0, string $spuCode = '', array $info = [], array $old_sale = [], int $rm = 0, string $ri = '', int $addrid = 0, string $token = '')
  1330. {
  1331. // $bidNo =isset($this->post['bidNo'])&&$this->post['bidNo']!="" ? trim($this->post['bidNo']):"";
  1332. // if($bidNo==""){
  1333. // return error_show(1003,"参数bidNo不能为空");
  1334. // }
  1335. $zxinfo = Db::name("consult_bids")->where(["bidNo" => $old_sale['zxNo'], "is_del" => 0])->find();
  1336. if ($zxinfo == false) return error_show(1005, '未找到咨询单商品信息');
  1337. // if($zxinfo['status']!=6){
  1338. // return error_show(1003,"咨询单状态有误无法转单");
  1339. // }
  1340. // $zx =Db::name("consult_info")->where(["infoNo"=>$zxinfo['infoNo'],"is_del"=>0])->find();
  1341. // if($zx==false){
  1342. // return error_show(1003,"未找到咨询单信息");
  1343. // }
  1344. // if($zx['status']!=4){
  1345. // return error_show(1003,"咨询单状态有误无法转单");
  1346. // }
  1347. // $zxorder =Db::name("consult_order")->where(["zxNo"=>$zx["zxNo"],"is_del"=>0])->find();
  1348. // if($zxorder==false){
  1349. // return error_show(1003,"未找到咨询单信息");
  1350. // }
  1351. // $good_num =isset($this->post['good_num'])&&$this->post['good_num']!="" ? intval($this->post['good_num']):"";
  1352. // if($good_num===""){
  1353. // return error_show(1003,"参数 good_num 不能为空");
  1354. // }
  1355. $good_num = $info['error_num'];
  1356. $sale_price = 0;
  1357. // $sale_price = isset($this->post['sale_price']) && $this->post['sale_price'] != "" ? floatval($this->post['sale_price']) : "";
  1358. // if ($sale_price === "") {
  1359. // return error_show(1003, "参数 sale_price 销售单价 不能为空");
  1360. // }
  1361. // if ($sale_price < $zxinfo['sale_price']) {
  1362. // return error_show(1003, "修改的销售单价不能低于原来的销售单价");
  1363. // }
  1364. // $sendtype = isset($this->post['sendtype'])&&$this->post['sendtype']!="" ? intval($this->post['sendtype']):"";
  1365. // if($sendtype==""){
  1366. // return error_show(1003,"参数sendtype不能为空");
  1367. // }
  1368. // $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  1369. $orderCode = makeNo("QR");
  1370. // $spuCode = $zxinfo['spuCode'];
  1371. $skuCode = "";
  1372. // $is_stock=0;
  1373. //原有采购单信息
  1374. $old_cgd = Db::name('order_num')
  1375. ->alias('on')
  1376. ->field('on.id onid,po.*')
  1377. ->leftJoin('purchease_order po', 'po.cgdNo=on.cgdNo')
  1378. ->where('on.orderCode', $old_sale['orderCode'])
  1379. ->find();
  1380. $cgd = array_merge($old_cgd, [
  1381. 'id' => null,
  1382. // "supplierNo"=>$zxinfo['supplierNo'],
  1383. // "companyNo"=>$zxorder['companyNo'],
  1384. "spuCode" => $spuCode,
  1385. "skuCode" => $skuCode,
  1386. "orderCode" => $orderCode,
  1387. // "good_name"=>$zxinfo['good_name'],
  1388. "sale_price" => $old_cgd['good_price'],//采购单价格不为0,取原采购单的价格
  1389. "total_fee" => round($old_cgd['good_price'] * $good_num, 2),
  1390. // "pakge_fee"=>$zxinfo['pakge_fee'],
  1391. // "cert_fee"=>$zxinfo['cert_fee'],
  1392. // "open_fee"=>$zxinfo['open_fee'],
  1393. 'cost_fee' => $old_cgd['teach_fee'],
  1394. // $old_sale['cost_fee']=$old_sale['teach_fee'];
  1395. // "mark_fee"=>$zxinfo['mark_fee'],
  1396. // "demo_fee"=>$zxinfo['demo_fee'],
  1397. // "nake_fee"=>$zxinfo['nake_fee'],
  1398. // "delivery_fee"=>$zxinfo['delivery_fee'],
  1399. "good_num" => $good_num,
  1400. "createrid" => $rm,
  1401. "creater" => $ri,
  1402. "good_type" => 1,
  1403. // "weight"=>$zxinfo['good_weight'],
  1404. // "gold_price"=>$zxinfo['gold_price'],
  1405. // "is_diff"=>$zxinfo['is_diff'],
  1406. "order_type" => 3,//咨询商品
  1407. "order_source" => 6,//6售后补换货
  1408. // 'send_way'=>$zxinfo['send_way']
  1409. 'token' => $token,
  1410. 'good_createrid' => $zxinfo['createrid'],
  1411. 'good_creater' => $zxinfo['creater'],//商品创建人
  1412. ]);
  1413. // $token=isset($this->post['token'])&&$this->post['token']!=""? trim($this->post['token']):"";
  1414. // if($token==""){
  1415. // return error_show(102, "参数token不能为空");
  1416. // }
  1417. // $apply_id = GetUserInfo($token);
  1418. // if (empty($apply_id) || $apply_id['code'] != 0) {
  1419. // return error_show(102, "申请人数据不存在");
  1420. // }
  1421. // $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1422. // $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1423. // $va = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  1424. // $send_num=0;
  1425. // if($sendtype==1){
  1426. // if ($va == "") {
  1427. // return error_show(1002, "参数order_addr不能为空");
  1428. // }
  1429. // $send_num = array_sum(array_column($va,"receipt_quantity"));
  1430. // }
  1431. // if($good_num<$send_num){
  1432. // return error_show(1002, "发货数量不能超过购买数量");
  1433. // }
  1434. // $arrtime=isset($this->post['arrtime'])&&$this->post['arrtime']!="" ?$this->post['arrtime']:"";
  1435. // if($arrtime==""){
  1436. // return error_show(1002, "参数arrtime不能为空");
  1437. // }
  1438. //新加字段
  1439. // $platform_order=isset($this->post['platform_order'])&&$this->post['platform_order']!="" ?$this->post['platform_order']:"";
  1440. // $workNo=isset($this->post['workNo'])&&$this->post['workNo']!="" ?$this->post['workNo']:"";
  1441. // $proof_type = isset($this->post['proof_type']) && $this->post['proof_type'] != "" ? intval($this->post['proof_type']) : 0;
  1442. // $proof_url = isset($this->post['proof_url']) && $this->post['proof_url'] != "" ? trim($this->post['proof_url']) : '';
  1443. Db::startTrans();
  1444. try {
  1445. // $zx =Db::name("consult_info")->where(["infoNo"=>$zxinfo['infoNo'],"is_del"=>0])->lock(true)->find();
  1446. // if($zx==false){
  1447. // Db::rollback();
  1448. // return error_show(1003,"未找到咨询单信息");
  1449. // }
  1450. $supplier_temp_info = Db::name('supplier')
  1451. ->field('id,person,personid')
  1452. ->where('code',$old_cgd['supplierNo'])
  1453. ->findOrEmpty();
  1454. $data = array_merge($old_sale, [
  1455. 'id' => null,
  1456. "orderCode" => $orderCode,
  1457. "good_code" => $spuCode,
  1458. // "skuCode"=>$skuCode,
  1459. // "customer_code"=>$zxorder['khNo'],
  1460. // "good_name"=>isset($zxinfo['good_name'])&&$zxinfo['good_name']!==''?$zxinfo['good_name']:'',
  1461. "good_num" => $good_num,
  1462. // "cat_id"=>$zxinfo['cat_id'],
  1463. "apply_id" => $rm,
  1464. "apply_name" => $ri,
  1465. // "origin_price"=>$zxinfo['total_fee'],
  1466. // "sale_price" => $old_sale['sale_price'],//取原价格
  1467. // "post_fee"=>0,
  1468. "status" => 0,
  1469. "send_status" => 0,
  1470. "th_num" => 0,
  1471. "th_fee" => 0,//退货金额为0
  1472. "send_num" => 0,
  1473. "wsend_num" => $good_num,
  1474. // "send_status"=>1,
  1475. "good_type" => 1,
  1476. // "send_type"=>$sendtype,
  1477. // "supplierNo"=>$zxorder['companyNo'],
  1478. "is_del" => 0,
  1479. // "zxNo"=>$bidNo,
  1480. // "platform_order"=>$platform_order,
  1481. // "platform_id"=> $zxorder['platform_code'],
  1482. "remark" => $old_sale['orderCode'],
  1483. // "arrive_time"=>$arrtime,
  1484. // "is_stock"=>0,
  1485. // "is_activity"=>0,
  1486. // "proof_id"=>0,
  1487. "order_type" => 3,
  1488. "order_source" => 6,//6售后补换货
  1489. // 'good_weight'=>$zxinfo['good_weight'],
  1490. // 'gold_price'=>$zxinfo['gold_price'],
  1491. // 'cost_price'=>$zxinfo['sale_cost_fee'],
  1492. // 'diff_weight'=>0,
  1493. // 'diff_fee'=>0,
  1494. "addtime" => date("Y-m-d H:i:s"),
  1495. "updatetime" => date("Y-m-d H:i:s"),
  1496. 'returnCode' => $info['returnCode'],
  1497. // 'total_price' => round($sale_price * $good_num, 2),
  1498. // 'workNo'=>$workNo,
  1499. 'cgderid' => $supplier_temp_info['personid'],
  1500. 'cgder' => $supplier_temp_info['person'],//采购员(供应商负责人)
  1501. 'good_createrid' => $zxinfo['createrid'],
  1502. 'good_creater' => $zxinfo['creater'],//商品创建人
  1503. ]);
  1504. $datainfo = Db::name('sale')->insertGetId($data);
  1505. if ($datainfo > 0) {
  1506. $standing_book_data = [
  1507. 'sale_id' => $datainfo,
  1508. 'infoNo' => $zxinfo['infoNo'],
  1509. 'updatetime' => date('Y-m-d H:i:s')
  1510. ];
  1511. //修改状态,添加待办
  1512. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1513. "order_code" => $orderCode,//销售单code
  1514. "status" => 0,//这里的status是之前的值
  1515. "action_remark" => '',//备注
  1516. "action_type" => "create"//新建create,编辑edit,更改状态status
  1517. ], "XSQRD", $data['status'], $data);
  1518. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  1519. "order_type" => 'XSQRD',
  1520. "order_code" => $orderCode,//销售单code
  1521. "order_id" => $datainfo,
  1522. "order_status" => $data['status'],
  1523. "before_status" => 0,
  1524. 'holder_id' => $data['apply_id']
  1525. ]);
  1526. // $old_zx_status = $zx['status'];
  1527. // $zx['status']=5;
  1528. // $zx['updatetime']=date("Y-m-d H:i:s");
  1529. // $sa=Db::name("consult_info")->save($zx);
  1530. // if($sa==false){
  1531. // Db::rollback();
  1532. // return error_show(1002,"咨询单修改失败");
  1533. // }else{
  1534. // //修改状态,添加待办
  1535. // ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1536. // "order_code" => $zx['infoNo'],//咨询单详情编号
  1537. // "status" => $old_zx_status,//这里的status是之前的值
  1538. // "action_remark" => '',//备注
  1539. // "action_type" => "status"//新建create,编辑edit,更改状态status
  1540. // ], "ZXD", 5, $zx);
  1541. //
  1542. // }
  1543. $this->createCgd($cgd, $standing_book_data);
  1544. // if($bol==false){
  1545. // Db::rollback();
  1546. // return error_show(1002,"咨询订单创建失败");
  1547. // }
  1548. // $limt=[
  1549. // "spuCode"=>$zxinfo['spuCode'],
  1550. // "good_name"=>$zxinfo['good_name'],
  1551. // "brand_id"=>$zxinfo['brand_id'],
  1552. // "good_unit"=>$zxinfo['unit_id'],
  1553. // "cat_id"=>$zxinfo['cat_id'],
  1554. // "good_type"=>0,
  1555. // "moq"=>1,
  1556. // "customized"=>$zxinfo['work_day'],
  1557. // "tax"=>$zxinfo['tax'],
  1558. // "supplierNo"=>$zxinfo["supplierNo"],
  1559. // "is_auth"=>0,
  1560. // "craft_desc"=>$zxinfo['good_name'],
  1561. // "good_remark"=>"",
  1562. // "platform_id"=>$zxorder['platform_id'],
  1563. // "good_img"=>$zxinfo['good_img'],
  1564. // "good_thumb_img"=>"",
  1565. // "good_info_img"=>"",
  1566. // "specinfo"=>$zxinfo['specinfo'],
  1567. // "work_day"=>$zxinfo['work_day'],
  1568. // "noble_metal"=>$zxinfo['metal_id'],
  1569. // "is_gold_price"=>$zxinfo['is_gold_price'],
  1570. // "config"=>$zxinfo['config'],
  1571. // "other_config"=>$zxinfo['other_config'],
  1572. // "weight"=>$zxinfo['weight'],
  1573. // "good_weight"=>$zxinfo['good_weight'],
  1574. // "is_diff"=>$zxinfo['is_diff'],
  1575. // "supply_area"=>$zxinfo['supply_area'],
  1576. // "pay_way"=>$zxinfo['pay_way'],
  1577. // "send_way"=>$zxinfo['send_way'],
  1578. // "companyNo"=>$zxorder['companyNo'],
  1579. // "status"=>1,
  1580. // "is_del"=>0,
  1581. // "createrid"=>$zxinfo['createrid'],
  1582. // "creater"=>$zxinfo['creater'],
  1583. // "addtime"=>date("Y-m-d H:i:s"),
  1584. // "updatetime"=>date("Y-m-d H:i:s"),
  1585. // 'proof_type' => $proof_type,//凭证类型
  1586. // 'proof_url' => $proof_url,//凭证文件
  1587. // ];
  1588. // $good = Db::name("good_zixun")->insert($limt);
  1589. // if(!$good){
  1590. // Db::rollback();
  1591. // return error_show(1006,"咨询商品录入失败");
  1592. // }
  1593. // if(!empty($va)){
  1594. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])->find();
  1595. if (empty($order)) throw new Exception('采购单关联数据未找到');
  1596. // foreach ($va as $value){
  1597. // $temp=[];
  1598. // $addrs=[];
  1599. // if($value['addr_code']!==''&&is_array($value['addr_code'])&&!empty($value['addr_code'])){
  1600. // $addrs['provice_code'] = $value['addr_code'][0];
  1601. // $addrs['city_code'] = $value['addr_code'][1];
  1602. // $addrs['area_code'] = $value['addr_code'][2];
  1603. // $addr = json_encode($addrs);
  1604. // }else{
  1605. // $addr = isset($value['addr_code'])?$value['addr_code']:'';
  1606. // }
  1607. // $temp['orderCode']=$orderCode;
  1608. // $temp['contactor']=$value['contactor'];
  1609. // $temp['mobile'] = $value['mobile'];
  1610. // $temp['addr'] = $value['addr'];
  1611. // $temp['addr_code']=$addr;
  1612. // $temp['customer_code'] =$zxorder['khNo'];
  1613. // $temp['receipt_quantity']=$value['receipt_quantity'];
  1614. // $temp['post_fee'] =0;
  1615. // $temp['is_del'] =0;
  1616. // $temp['addtime'] =date("Y-m-d H:i:s");
  1617. // $temp['updatetime'] =date("Y-m-d H:i:s");
  1618. // $temp['arrive_time']=$arrtime;
  1619. $old_out_addr_info = Db::name('order_out')
  1620. ->alias('oo')
  1621. ->field('oo.id,oa.addr,oa.addr_code,oa.contactor,oa.mobile,oa.customer_code,oa.post_fee,oa.arrive_time')
  1622. ->leftJoin('order_addr oa', 'oa.id=oo.addrid')
  1623. ->where(['oo.outCode' => $info['outCode'], 'oo.orderCode' => $info['orderCode']])
  1624. ->find();
  1625. if (empty($old_out_addr_info)) throw new Exception('发货单对应的地址信息查不到');
  1626. $temp = [
  1627. 'orderCode' => $orderCode,
  1628. 'addr' => $old_out_addr_info['addr'],
  1629. 'addr_code' => $old_out_addr_info['addr_code'],
  1630. 'contactor' => $old_out_addr_info['contactor'],
  1631. 'mobile' => $old_out_addr_info['mobile'],
  1632. 'customer_code' => $old_out_addr_info['customer_code'],
  1633. 'receipt_quantity' => $good_num,
  1634. 'arrive_time' => $old_out_addr_info['arrive_time'],
  1635. 'addtime' => date("Y-m-d H:i:s"),
  1636. 'updatetime' => date("Y-m-d H:i:s"),
  1637. // ''=>'',
  1638. ];
  1639. $addrid = Db::name('order_addr')->insertGetId($temp);
  1640. // if($vmp>0){
  1641. $outCode = makeNo("DF");
  1642. $out = [
  1643. "orderCode" => $orderCode,
  1644. "outCode" => $outCode,
  1645. "apply_id" => $rm,
  1646. "apply_name" => $ri,
  1647. 'addrid' => $addrid,
  1648. "post_name" => "",
  1649. "post_code" => "",
  1650. "post_fee" => 0,
  1651. "sendtime" => date("Y-m-d H:i:s"),
  1652. "send_num" => $good_num,
  1653. "check_num" => 0,
  1654. "error_num" => 0,
  1655. "wsm_code" => "",
  1656. "order_type" => 3,
  1657. "status" => 0,
  1658. "addtime" => date("Y-m-d H:i:s"),
  1659. "updatetime" => date("Y-m-d H:i:s")
  1660. ];
  1661. $ou = Db::name("order_out")->insert($out);
  1662. if ($ou == false) throw new Exception('发货地址添加创建失败');
  1663. else {
  1664. //修改状态,添加待办
  1665. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1666. "order_code" => $outCode,//出库单号
  1667. "status" => 0,//这里的status是之前的值
  1668. "action_remark" => '',//备注
  1669. "action_type" => "create"//新建create,编辑edit,更改状态status
  1670. ], "CKD", 0, $out);
  1671. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  1672. "order_type" => 'CKD',
  1673. "order_code" => $outCode,//出库单号
  1674. "order_id" => Db::name("order_out")->getLastInsID(),
  1675. "order_status" => 0,
  1676. "before_status" => 0,
  1677. 'holder_id' => $out['apply_id']
  1678. ]);
  1679. //将发货编号添加到台账中
  1680. $standing_book_data['outCode'][] = $outCode;
  1681. }
  1682. $order['send_num'] += $good_num;
  1683. $order['wsend_num'] -= $good_num;
  1684. if ($order['wsend_num'] < 0) throw new Exception('发货数量已超出总数');
  1685. $ups = Db::name("order_num")->save($order);
  1686. if ($ups) {
  1687. $sen = Db::name("order_send")->save([
  1688. "cgdNo" => $order['cgdNo'],
  1689. "outCode" => $outCode,
  1690. "send_num" => $good_num,
  1691. "status" => 1,
  1692. "addtime" => date("Y-m-d H:i:s"),
  1693. "updatetime" => date("Y-m-d H:i:s")
  1694. ]);
  1695. if ($sen == false) throw new Exception('发货地址添加创建失败');
  1696. }
  1697. // }else{
  1698. // Db::rollback();
  1699. // return error_show(1002,"发货地址添加创建失败");
  1700. // }
  1701. // }
  1702. //处理台账
  1703. if (isset($standing_book_data['outCode'])) {
  1704. Db::execute("UPDATE `wsm_standing_book` SET `outCode`=CONCAT(IFNULL(`outCode`,''),'," . implode(',', $standing_book_data['outCode']) . "'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `infoNo`='{$standing_book_data['infoNo']}'");
  1705. unset($standing_book_data['outCode']);
  1706. }
  1707. Db::name('standing_book')->where('infoNo', $standing_book_data['infoNo'])->update($standing_book_data);
  1708. Db::name('order_return')
  1709. ->where(['id' => $orid, 'is_reissue' => 0])
  1710. ->update([
  1711. 'is_reissue' => 1,
  1712. 'newOrderCode' => $orderCode,
  1713. 'updatetime' => date('Y-m-d H:i:s')
  1714. ]);
  1715. Db::commit();
  1716. return app_show(0, "咨询订单创建成功", ["order_code" => $orderCode]);
  1717. }
  1718. Db::rollback();
  1719. return error_show(1002, "咨询订单创建失败");
  1720. } catch (Exception $e) {
  1721. Db::rollback();
  1722. return error_show(1003, $e->getMessage() . '|' . $e->getFile() . ':' . $e->getLine());
  1723. }
  1724. }
  1725. private function createCgd($data,array &$standing_book_da=[]){
  1726. $cgdCode = makeNo("CG");
  1727. $supplier=Db::name("supplier")->where(["code"=>$data['supplierNo'],"is_del"=>0])->find();
  1728. if($supplier==false) throw new Exception('该供应商不存在');
  1729. $wsm= Db::name("warehouse_info")
  1730. ->where(["supplierNo"=>$data["supplierNo"],"companyNo"=>$data['companyNo'],"wsm_type"=>2,"is_del"=>0])
  1731. ->find();
  1732. if($wsm==false){
  1733. $wsm_code = makeNo("WSM");
  1734. $inwsm=[
  1735. "wsm_code"=>$wsm_code,
  1736. "name"=>$supplier['name'],
  1737. "wsm_type"=>2,
  1738. "supplierNo"=>$supplier['code'],
  1739. "addr"=>"",
  1740. "addrs_code"=>"",
  1741. "contactor"=>$data['order_type']==1? $supplier['personid']:$data['createrid'],
  1742. "contactor_name"=>$data['order_type']==1? $supplier['person']:$data['creater'],
  1743. "mobile"=>"",
  1744. "position"=>"",
  1745. "companyNo"=>$data['companyNo'],
  1746. "status"=>1,
  1747. "is_del"=>0,
  1748. "addtime"=>date("Y-m-d H:i:s"),
  1749. "updatetime"=>date("Y-m-d H:i:s")
  1750. ];
  1751. $in = Db::name("warehouse_info")->insert($inwsm);
  1752. if($in==false) throw new Exception('仓库新增失败');
  1753. }else $wsm_code =$wsm['wsm_code'];
  1754. $cg =[
  1755. "cgdNo"=>$cgdCode,
  1756. "bkcode"=>"",
  1757. "wsm_code"=>$wsm_code,
  1758. "cgder"=>$data['good_creater'],
  1759. "cgder_id"=>$data['good_createrid'],
  1760. "spuCode"=>$data['spuCode'],
  1761. "skuCode"=>$data['skuCode'],
  1762. "good_name"=>$data['good_name'],
  1763. "good_num"=>$data['good_num'],
  1764. "good_price"=>$data['sale_price'],
  1765. "total_fee"=>round($data['sale_price']*$data['good_num'],2),
  1766. "pakge_fee"=>$data['pakge_fee'],
  1767. "cert_fee"=>$data['cert_fee'],
  1768. "open_fee"=>$data['open_fee'],
  1769. "teach_fee"=>$data['cost_fee'],
  1770. "mark_fee"=>$data['mark_fee'],
  1771. "demo_fee"=>$data['demo_fee'],
  1772. "nake_fee"=>$data['nake_fee'],
  1773. "weight"=>$data['weight'],
  1774. "delivery_fee"=>$data['delivery_fee'],
  1775. "gold_price"=>$data['gold_price'],
  1776. "diff_weight"=>"0",
  1777. "diff_fee"=>"0",
  1778. "supplierNo"=>$data['supplierNo'],
  1779. "supplier_name"=>$supplier['name'],
  1780. "companyNo"=>$data['companyNo'],
  1781. "send_status"=>1,
  1782. "send_num"=>0,
  1783. "wsend_num"=>$data['good_num'],
  1784. "remark"=>"",
  1785. "status"=>0,//0初始化
  1786. "lasttime"=>date("Y-m-d H:i:s"),
  1787. "is_del"=>0,
  1788. "order_type"=>$data['order_type'],
  1789. "order_source"=>$data['order_source'],
  1790. "good_type"=>$data['good_type'],
  1791. "addtime"=>date("Y-m-d H:i:s"),
  1792. "updatetime"=>date("Y-m-d H:i:s"),
  1793. 'good_createrid' => $data['good_createrid'],
  1794. 'good_creater' => $data['good_createrid'],//商品创建人
  1795. ];
  1796. $up =Db::name("purchease_order")->insertGetId($cg);
  1797. if($up){
  1798. //修改状态,添加待办
  1799. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1800. "order_code" => $cg['cgdNo'],//销售单code
  1801. "status" => 0,//这里的status是之前的值
  1802. "action_remark" => '',//备注
  1803. "action_type" => "create"//新建create,编辑edit,更改状态status
  1804. ], "CGD", $cg['status'], $cg);
  1805. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  1806. "order_type" => 'CGD',
  1807. "order_code" => $cg['cgdNo'],//销售单code
  1808. "order_id" => $up,
  1809. "order_status" => $cg['status'],
  1810. "before_status"=> 0,
  1811. 'holder_id' => $supplier['personid'],
  1812. ]);
  1813. $standing_book_da = array_merge($standing_book_da, [
  1814. 'orderCode' => $data['orderCode'],
  1815. 'cgdNo' => $cgdCode,
  1816. 'spuCode' => $data['spuCode'],
  1817. 'skuCode' => $data['skuCode'],
  1818. 'order_type' => $data['order_type'],
  1819. 'order_source' => $data['order_source'],
  1820. 'supplierNo' => $data['supplierNo'],
  1821. "companyNo" => $data['companyNo'],
  1822. ]);
  1823. $rela=[
  1824. "orderCode"=>$data['orderCode'],
  1825. "cgdNo"=>$cgdCode,
  1826. "spuCode"=>$data['spuCode'],
  1827. "good_num"=>$data['good_num'],
  1828. "wsend_num"=>$data['good_num'],
  1829. "send_num"=>0,
  1830. "wait_num"=>0,
  1831. "status"=>1,
  1832. "source"=>2
  1833. ];
  1834. Db::name("order_num")->save($rela);
  1835. $stokc =Db::name("good_stock")
  1836. ->where(['spuCode'=>$data['spuCode'],"wsm_code"=>$wsm_code, "is_del"=>0])
  1837. ->find();
  1838. if($stokc==false){
  1839. $stokc=[
  1840. "spuCode"=>$data['spuCode'],
  1841. "wsm_code"=>$wsm_code,
  1842. "wait_in_stock"=>$data['good_num'],
  1843. "wait_out_stock"=>0,
  1844. "usable_stock"=>0,
  1845. "intra_stock"=>0,
  1846. "total_stock"=>0,
  1847. "status"=>1,
  1848. "addtime"=>date("Y-m-d H:i:s"),
  1849. "updatetime"=>date("Y-m-d H:i:s")
  1850. ];
  1851. }else{
  1852. $stokc['wait_in_stock']+=$data['good_num'];
  1853. $stokc['updatetime']=date("Y-m-d H:i:s");
  1854. }
  1855. $stoc= Db::name("good_stock")->save($stokc);
  1856. if($stoc==false) throw new Exception('商品仓库库存修改失败');
  1857. $good_data[] = ['good_log_code' =>$cgdCode, "stock_id" => isset($stoc['id'])?$stoc['id']:Db::name("good_stock")->getLastInsID(), "type" => 1,'stock'=>$data['good_num'], "stock_name" => "wait_in_stock"];
  1858. GoodLog::LogAdd($data['token'],$good_data,"CGD");
  1859. return true;
  1860. }else throw new Exception('商品仓库库存修改失败');
  1861. }
  1862. //创建采购单
  1863. /**
  1864. * 非咨询类商品处理
  1865. * @param int $orid 售后记录的id
  1866. * @param string $spuCode 商品的spuCode
  1867. * @param array $info 售后申请单的详情
  1868. * @param array $old_sale 原始销售订单信息
  1869. * @param int $rm 操作人id
  1870. * @param string $ri 操作人名称
  1871. * @param array $old_out 原始发货单信息
  1872. * @param string $token 当前用户的token
  1873. * @throws \think\db\exception\DataNotFoundException
  1874. * @throws \think\db\exception\DbException
  1875. * @throws \think\db\exception\ModelNotFoundException
  1876. */
  1877. private function createSaleNotZixun(int $orid = 0, string $spuCode = '', array $info = [], array $old_sale = [], int $rm = 0, string $ri = '', array $old_out = [], string $token = '')
  1878. {
  1879. $goodinfo = Db::name('good')
  1880. ->alias('g')
  1881. ->field('g.*,gp.skuCode')
  1882. ->join('good_platform gp', 'gp.spuCode=g.spuCode AND gp.exam_status=6')
  1883. ->where('g.spuCode', $spuCode)
  1884. ->where('gp.skuCode', $old_sale['skuCode'])
  1885. ->find();
  1886. if (empty($goodinfo)) return error_show(1005, '查不到这个商品,或该商品已下线无法售后');
  1887. $is_activity = $old_sale['is_activity'];
  1888. $actcode = $old_sale['activity_code'];
  1889. $good_num = $info['error_num'];//数量,取售后申请单中的异常数量
  1890. $supplierNo = $old_sale['supplierNo'];
  1891. $sale_price = 0;
  1892. $origin_price = $old_sale['origin_price'];
  1893. $goodtype = $old_sale['good_type'];
  1894. $is_stock = $goodinfo['is_stock'];
  1895. $skuCode = $goodinfo['skuCode'];
  1896. $orderCode = makeNo("QR");
  1897. if ($is_stock == 1) {
  1898. $stock_num = Db::name("good_stock")->alias("a")
  1899. ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")
  1900. ->leftJoin("good_stock_info c", "a.id=c.stockid and c.balance_num>0")
  1901. ->where(["spuCode" => $spuCode, "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5, "b.companyNo" =>$supplierNo])
  1902. ->sum('balance_num');
  1903. if($stock_num < $good_num){
  1904. return error_show(1003, "批次库存数量不足"); //库存不足
  1905. }
  1906. }
  1907. $goodinfo['cgd_gold_price'] = 0;
  1908. if ($goodtype == 1) {
  1909. if ($is_activity == 1) {
  1910. $act = Db::name("activity_info")
  1911. ->alias("a")
  1912. ->leftJoin("good_activity b", "a.activity_code=b.activity_code")
  1913. ->where(["a.skuCode" => $skuCode, "a.activity_code" => $actcode, "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0])
  1914. ->find();
  1915. if ($act == false) return error_show(1003, "未找到相关活动价");
  1916. if ($act['moq_num'] > $good_num) return error_show(1003, "商品不满足活动价起订量{$act['moq_num']}");
  1917. if ($act['activity_stock'] < $good_num) return error_show(1003, "商品活动库存剩余{$act['activity_stock']}");
  1918. }
  1919. // else {
  1920. // $good = Db::name("good_ladder")
  1921. // ->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])
  1922. // ->where([["min_num", "<=", $good_num]])
  1923. // ->order("min_num desc")
  1924. // ->find();
  1925. //
  1926. // if ($good == false) return error_show(1003, "未找到相关阶梯价格");
  1927. //
  1928. // if ($goodinfo['is_gold_price'] == 1 && $is_stock != 1) {
  1929. // $gold = Db::name("gold_price1")
  1930. // ->field('id,price')
  1931. // ->where(["type" => $goodinfo['noble_metal'], "is_del" => 0, "status" => 1])
  1932. // ->order("addtime desc")
  1933. // ->find();
  1934. //
  1935. // $goodinfo['cgd_gold_price'] = $gold["price"];
  1936. // }
  1937. // }
  1938. }
  1939. //原有采购单信息
  1940. if ($is_stock != 1){
  1941. $old_cgd = Db::name('order_num')
  1942. ->alias('on')
  1943. ->field('on.id onid,po.*')
  1944. ->leftJoin('purchease_order po', 'po.cgdNo=on.cgdNo')
  1945. ->where('on.orderCode', $old_sale['orderCode'])
  1946. ->find();
  1947. $cgd = array_merge($old_cgd, [
  1948. 'id' => null,
  1949. // "supplierNo" => $ct['supplierNo'],
  1950. // "companyNo" => $supplierNo,
  1951. "orderCode" => $orderCode,
  1952. "spuCode" => $spuCode,
  1953. "skuCode" => $skuCode,
  1954. // "good_name" => $ct['good_name'],
  1955. "sale_price" => $old_cgd['good_price'],//采购单价格不为0,取原采购单的价格
  1956. "total_fee" => $old_cgd['good_price'] * $good_num,
  1957. // "pakge_fee" => isset($origin['package_fee']) ? $origin['package_fee'] : 0,
  1958. // "cert_fee" => isset($origin['cert_fee']) ? $origin['cert_fee'] : 0,
  1959. // "open_fee" => $ct['open_fee'],
  1960. // "cost_fee" => isset($origin['cost_fee']) ? $origin['cost_fee'] : 0,
  1961. 'cost_fee' => $old_cgd['teach_fee'],
  1962. // "mark_fee" => isset($origin['mark_fee']) ? $origin['mark_fee'] : 0,
  1963. // "nake_fee" => isset($origin['nake_fee']) ? $origin['nake_fee'] : 0,
  1964. // "delivery_fee" => isset($origin['delivery_fee']) ? $origin['delivery_fee'] : 0,
  1965. // "demo_fee" => $ct['demo_fee'],
  1966. "good_num" => $good_num,
  1967. // "good_type" => $goodtype,
  1968. // "weight" => $ct['noble_weight'],
  1969. // "gold_price" => isset($ct['cgd_gold_price']) ? $ct['cgd_gold_price'] : 0,
  1970. // "order_type" => $is_stock == 1 ? 1 : 2,
  1971. "order_source" => 6,//6售后补换货
  1972. "createrid" => $rm,
  1973. "creater" => $ri,
  1974. // 'send_way' => 2,
  1975. 'token' => $token,
  1976. 'addtime' => date('Y-m-d H:i:s'),
  1977. 'updatetime' => date('Y-m-d H:i:s'),
  1978. 'good_createrid' => $goodinfo['createrid'],
  1979. 'good_creater' => $goodinfo['creater'],//商品创建人
  1980. ]);
  1981. $supplier_temp_info = Db::name('supplier')
  1982. ->field('id,person,personid')
  1983. ->where('code',$cgd['supplierNo'])
  1984. ->findOrEmpty();
  1985. }else{
  1986. $supplier_temp_info = Db::name('supplier')
  1987. ->field('id,person,personid')
  1988. ->where('code',$goodinfo['supplierNo'])
  1989. ->findOrEmpty();
  1990. }
  1991. // $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !=="" ?$this->post['addrlist']:"";
  1992. $send_num = 0;
  1993. // if($sendtype==1){
  1994. // if($addrlist=="" || empty($addrlist) ||!is_array($addrlist)){
  1995. // return error_show(1004,"参数addrlist不能为空");
  1996. // }
  1997. // $send_num = array_sum(array_column($addrlist,"receipt_quantity"));
  1998. // }
  1999. // if($send_num>$good_num){
  2000. // return error_show(1004,"发货数量超出订单总数量");
  2001. // }
  2002. // $remark =isset($this->post['remark']) && $this->post['remark'] !=="" ?trim($this->post['remark']):"";
  2003. // $proof_id =isset($this->post['proof_id']) && $this->post['proof_id'] !=="" ?intval($this->post['proof_id']):0;
  2004. // $apply_id = GetUserInfo($param['token']);
  2005. // if (empty($apply_id) || $apply_id['code'] != 0) return error_show(1002, "申请人数据不存在");
  2006. // $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  2007. // $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  2008. Db::startTrans();
  2009. try {
  2010. $data = array_merge($old_sale, [
  2011. 'id' => null,
  2012. "orderCode" => $orderCode,
  2013. "good_code" => $spuCode,
  2014. "skuCode" => $skuCode,
  2015. // "customer_code" => $info['customer_code'],
  2016. // "good_name" => (isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '') ? $goodinfo['good_name'] : '',
  2017. "good_num" => $good_num,
  2018. // "cat_id" => $goodinfo['cat_id'],
  2019. "apply_id" => $rm,
  2020. "apply_name" => $ri,
  2021. "origin_price" => $origin_price,
  2022. // "sale_price" => $old_sale['sale_price'],//取原价格
  2023. // "total_price" => $sale_price,//取原总价
  2024. // "post_fee" => 0,
  2025. // "status" => 0,
  2026. "th_num" => 0,
  2027. "th_fee" => 0,//退货金额为0
  2028. "send_num" => 0,
  2029. "wsend_num" => $good_num,
  2030. // "send_status" => 0,
  2031. // "good_type" => $goodtype,
  2032. // "send_type" => $old_sale['send_type'],
  2033. // "supplierNo" => $supplierNo,
  2034. // "is_del" => 0,
  2035. // "zxNo" => "",
  2036. // "platform_order" => $old_sale['platform_order'],
  2037. // "platform_id" => $old_sale['platform_id'],
  2038. "remark" => $old_sale['orderCode'],
  2039. // "is_stock" => $is_stock,
  2040. "is_activity" => $is_activity,
  2041. "activity_code" => $actcode,
  2042. // "order_type" => $is_stock == 1 ? 1 : 2,
  2043. "order_source" => 6,//6售后补换货
  2044. // "poNo"=>$poNo,
  2045. // 'good_weight' => $ct['noble_weight'],
  2046. // 'gold_price' => isset($ct['cgd_gold_price']) ? $ct['cgd_gold_price'] : 0,
  2047. // 'cost_price' => isset($good['cost_fee']) ? $good['cost_fee'] : 0,
  2048. // 'diff_weight' => 0,
  2049. // 'diff_fee' => 0,
  2050. // "workNo" => '',
  2051. 'status' => 0,
  2052. "addtime" => date("Y-m-d H:i:s"),
  2053. "updatetime" => date("Y-m-d H:i:s"),
  2054. // 'total_price' => round($sale_price * $good_num, 2),
  2055. // 'proof_id' => $old_sale['proof_id'],
  2056. // 'paytime' => $old_sale['paytime'],
  2057. 'returnCode' => $info['returnCode'],
  2058. 'cgderid' => $supplier_temp_info['personid'],
  2059. 'cgder' => $supplier_temp_info['person'],//采购员(供应商负责人)
  2060. 'good_createrid' => $goodinfo['createrid'],
  2061. 'good_creater' => $goodinfo['creater'],//商品创建人
  2062. ]);
  2063. $datainfo = Db::name('sale')->insertGetId($data);
  2064. if ($datainfo > 0) {
  2065. //修改状态,添加待办
  2066. ActionLog::logAdd($token, [
  2067. "order_code" => $orderCode,//销售单code
  2068. "status" => 0,//这里的status是之前的值
  2069. "action_remark" => '',//备注
  2070. "action_type" => "create"//新建create,编辑edit,更改状态status
  2071. ], "XSQRD", 0, $this->post);
  2072. ProcessOrder::AddProcess($token, [
  2073. "order_type" => 'XSQRD',
  2074. "order_code" => $orderCode,//销售单code
  2075. "order_id" => $datainfo,
  2076. "order_status" => 0,
  2077. "before_status" => 0,
  2078. 'holder_id' => $data['apply_id']
  2079. ]);
  2080. if ($is_activity == 1) {
  2081. $actup = [
  2082. "activity_stock" => $act['activity_stock'] - $good_num,
  2083. "updatetime" => date("Y-m-d H:i:s")
  2084. ];
  2085. $actupp = Db::name("activity_info")
  2086. ->where(["skuCode" => $skuCode, "activity_code" => $actcode, "is_del" => 0, "status" => 1])
  2087. ->save($actup);
  2088. if ($actupp == false) throw new Exception('活动库存修改失败');
  2089. }
  2090. $outstatus = 0;
  2091. $standing_book_da = ['sale_id' => $datainfo, 'customer_code' => $old_sale['customer_code'], 'skuCode' => $skuCode, 'updatetime' => date('Y-m-d H:i:s')];
  2092. if ($is_stock == 0) $this->createCgd($cgd, $standing_book_da);
  2093. else {
  2094. $outstatus = 1;
  2095. $stockid= Db::name("good_stock")->alias("a")
  2096. ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")
  2097. ->where(["spuCode" => $spuCode, "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5, "b.companyNo" =>$supplierNo])
  2098. ->column('a.id');
  2099. $stockinfo =GoodStockInfo::OrderBn($orderCode,$stockid,$good_num);
  2100. if($stockinfo==false){
  2101. throw new Exception('库存商品更新库存失败');
  2102. }
  2103. // $bol = $this->RelaCgd([
  2104. // 'orderCode' => $orderCode,
  2105. // "good_num" => $good_num,
  2106. // "spuCode" => $spuCode,
  2107. // "companyNo" => $supplierNo,
  2108. // "cost_fee" => isset($goodinfo['cost_fee']) ? $goodinfo['cost_fee'] : 0,
  2109. // 'order_type' => $data['order_type'],
  2110. // 'order_source' => $data['order_source'],
  2111. // ], $standing_book_da);
  2112. // if (isset($stock)) {
  2113. //
  2114. // $stck = [
  2115. // "usable_stock" => $stock['usable_stock'] - $good_num,
  2116. // "wait_out_stock" => $stock['wait_out_stock'] + $good_num,
  2117. // "updatetime" => date("Y-m-d H:i:s")
  2118. // ];
  2119. //
  2120. // $upad = Db::name("good_stock")->where($stock)->update($stck);
  2121. // if ($upad == false) throw new Exception('库存商品更新库存失败');
  2122. // $stockinfo =GoodStockInfo::OrderBn($orderCode,$stock['id'],$good_num);
  2123. // if($stockinfo==false){
  2124. // throw new Exception('库存商品更新库存失败'); //库存不足
  2125. // }
  2126. // //商品变动日志表,good_log_code字段存储采购单号
  2127. // $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 2, 'stock' => $good_num, "stock_name" => "usable_stock"];
  2128. // $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 1, 'stock' => $good_num, "stock_name" => "wait_out_stock"];
  2129. // GoodLog::LogAdd($token, $good_data, "XSQRD");
  2130. // }
  2131. }
  2132. // $old_out = Db::name('order_out')
  2133. // ->field('id,addrid,wsm_code')
  2134. // ->where(['orderCode' => $info['orderCode'], 'outCode' => $info['outCode']])
  2135. // ->find();
  2136. $old_out_addr_info = Db::name('order_out')
  2137. ->alias('oo')
  2138. ->field('oo.id,oa.addr,oa.addr_code,oa.contactor,oa.mobile,oa.customer_code,oa.post_fee,oa.arrive_time')
  2139. ->leftJoin('order_addr oa', 'oa.id=oo.addrid')
  2140. ->where(['oo.outCode' => $info['outCode'], 'oo.orderCode' => $info['orderCode']])
  2141. ->find();
  2142. if (empty($old_out_addr_info)) throw new Exception('发货单对应的地址信息查不到');
  2143. $temp = [
  2144. 'orderCode' => $orderCode,
  2145. 'addr' => $old_out_addr_info['addr'],
  2146. 'addr_code' => $old_out_addr_info['addr_code'],
  2147. 'contactor' => $old_out_addr_info['contactor'],
  2148. 'mobile' => $old_out_addr_info['mobile'],
  2149. 'customer_code' => $old_out_addr_info['customer_code'],
  2150. 'receipt_quantity' => $good_num,
  2151. 'arrive_time' => $old_out_addr_info['arrive_time'],
  2152. 'addtime' => date("Y-m-d H:i:s"),
  2153. 'updatetime' => date("Y-m-d H:i:s"),
  2154. // ''=>'',
  2155. ];
  2156. $addrid = Db::name('order_addr')->insertGetId($temp);
  2157. $outCode = makeNo("DF");
  2158. $out = array_merge($old_out, [
  2159. 'id' => null,
  2160. "orderCode" => $orderCode,
  2161. "outCode" => $outCode,
  2162. "apply_id" => $rm,
  2163. "apply_name" => $ri,
  2164. "addrid" => $addrid,
  2165. "post_name" => "",
  2166. "post_code" => "",
  2167. "post_fee" => 0,
  2168. "sendtime" => date("Y-m-d H:i:s"),
  2169. "send_num" => $good_num,
  2170. "check_num" => 0,
  2171. "error_num" => 0,
  2172. "wsm_code" => isset($old_out['wsm_code']) ? $old_out['wsm_code'] : '',
  2173. "order_type" => $is_stock == 1 ? 1 : 2,
  2174. "status" => $outstatus,
  2175. "addtime" => date("Y-m-d H:i:s"),
  2176. "updatetime" => date("Y-m-d H:i:s")
  2177. ]);
  2178. Db::name("order_out")->insert($out);
  2179. $standing_book_da['outCode'] = $outCode;
  2180. if($is_stock==0){
  2181. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])->find();
  2182. if (empty($order)) throw new Exception('采购单关联数据未找到');
  2183. $order['send_num'] += $good_num;
  2184. $order['wsend_num'] -= $good_num;
  2185. if ($order['wsend_num'] < 0) throw new Exception('发货数量已超出总数');
  2186. $ups = Db::name("order_num")->save($order);
  2187. if ($ups) {
  2188. $sen = Db::name("order_send")->save([
  2189. "cgdNo" => $order['cgdNo'],
  2190. "outCode" => $outCode,
  2191. "send_num" => $good_num,
  2192. "status" => 1,
  2193. "addtime" => date("Y-m-d H:i:s"),
  2194. "updatetime" => date("Y-m-d H:i:s")
  2195. ]);
  2196. if ($sen == false) throw new Exception('发货地址添加创建失败');
  2197. }
  2198. }
  2199. if (!isset($standing_book_da['returnCode'])) $standing_book_da['returnCode'] = '';
  2200. if (!isset($standing_book_da['thNo'])) $standing_book_da['thNo'] = '';
  2201. if (!isset($standing_book_da['returnGoodCode'])) $standing_book_da['returnGoodCode'] = '';
  2202. if (!isset($standing_book_da['wsm_in_code'])) $standing_book_da['wsm_in_code'] = '';
  2203. if (!isset($standing_book_da['cgdReturnCode'])) $standing_book_da['cgdReturnCode'] = '';
  2204. //处理台账
  2205. Db::name('standing_book')->insert(array_merge($standing_book_da, ['addtime' => date('Y-m-d H:i:s'), 'updatetime' => date('Y-m-d H:i:s'), 'standBookNo' => makeNo('IO')]));
  2206. //维护售后申请单
  2207. Db::name('order_return')
  2208. ->where(['id' => $orid, 'is_reissue' => 0])
  2209. ->update([
  2210. 'is_reissue' => 1,
  2211. 'newOrderCode' => $orderCode,
  2212. 'updatetime' => date('Y-m-d H:i:s')
  2213. ]);
  2214. Db::commit();
  2215. return error_show(0, "操作成功");
  2216. } else throw new Exception('咨询订单创建失败');
  2217. } catch (\Exception $e) {
  2218. Db::rollback();
  2219. return error_show(1005, $e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine());
  2220. }
  2221. }
  2222. //创建采购单
  2223. public function RelaCgd($outinfo,array &$standing_book_da=[]){
  2224. $cgd = Db::name("order_bk")->where([["spuCode","=",$outinfo['spuCode']],["is_del","=",0],["balance_num",">=",$outinfo['good_num']],['companyNo',"=",$outinfo['companyNo']]])->lock(true)->find();
  2225. if($cgd==false) throw new Exception('未查询到备库单信息');
  2226. $good=Db::name("good")
  2227. ->where(["spuCode"=>$outinfo['spuCode'],"is_del"=>0])
  2228. ->find();
  2229. if($good==false) throw new Exception('未查询到商品信息');
  2230. $cgdinfo =Db::name("purchease_order")
  2231. ->where(['cgdNo'=>$cgd['cgdNo'],"is_del"=>0])
  2232. ->find();
  2233. if($cgdinfo==false) throw new Exception('未查询到采购单信息');
  2234. $QrdCgd=[
  2235. "cgdNo"=>makeNo("CG"),
  2236. "bkcode" => $cgdinfo['bkcode'],
  2237. 'wsm_code'=>$cgdinfo['wsm_code'],
  2238. "cgder_id"=>$cgdinfo['cgder_id'],
  2239. "cgder"=>$cgdinfo['cgder'],
  2240. "spuCode"=>$cgdinfo['spuCode'],
  2241. "good_name"=>$cgdinfo['good_name'],
  2242. "good_num"=>$outinfo['good_num'],
  2243. "good_price"=>$cgdinfo['good_price'],
  2244. "total_fee"=>round($cgdinfo['good_price']*$outinfo['good_num'],2),
  2245. "pakge_fee"=>$cgdinfo['pakge_fee'],
  2246. "cert_fee"=>$cgdinfo['cert_fee'],
  2247. "open_fee"=>$cgdinfo['open_fee'],
  2248. "delivery_fee"=>$cgdinfo['delivery_fee'],
  2249. "mark_fee"=>$cgdinfo['mark_fee'],
  2250. "teach_fee"=>$cgdinfo['teach_fee'],
  2251. "nake_fee"=>$cgdinfo['nake_fee'],
  2252. "demo_fee"=>$cgdinfo['demo_fee'],
  2253. "weight"=>$cgdinfo['weight'],
  2254. "diff_weight"=>$cgdinfo['diff_weight'],
  2255. "diff_fee"=>$cgdinfo['diff_fee'],
  2256. "gold_price"=>$cgdinfo['gold_price'],
  2257. "supplierNo"=>$cgdinfo['supplierNo'],
  2258. "supplier_name"=>$cgdinfo['supplier_name'],
  2259. "companyNo"=>$cgdinfo['companyNo'],
  2260. "send_status"=>3,
  2261. "send_num"=>$outinfo['good_num'],
  2262. "wsend_num"=>0,
  2263. "remark"=>$cgdinfo['remark'],
  2264. "status"=>3,
  2265. "lasttime"=>$cgdinfo['lasttime'],
  2266. "is_del"=>0,
  2267. "order_type"=>$outinfo['order_type'],
  2268. "order_source"=>$outinfo['order_source'],
  2269. "good_type"=>$cgdinfo['good_type'],
  2270. "addtime"=>date("Y-m-d H:i:s"),
  2271. "updatetime"=>date("Y-m-d H:i:s"),
  2272. 'good_createrid' => $good['createrid'],
  2273. 'good_creater' => $good['creater'],//商品创建人
  2274. ];
  2275. $insetrCgd=Db::name("purchease_order")->insert($QrdCgd);
  2276. if($insetrCgd==false) throw new Exception('新增采购单失败');
  2277. else{
  2278. $standing_book_da = array_merge($standing_book_da, [
  2279. 'orderCode' => $outinfo['orderCode'],
  2280. 'cgdNo' => $cgdinfo['cgdNo'],
  2281. 'spuCode' => $outinfo['spuCode'],
  2282. 'order_type' => $QrdCgd['order_type'],
  2283. 'order_source' => $QrdCgd['order_source'],
  2284. 'supplierNo' => $cgdinfo['supplierNo'],
  2285. 'companyNo' => $cgdinfo['companyNo'],
  2286. 'bk_code' => $cgdinfo['bkcode'],
  2287. 'purchease_id' => Db::name('purchease')->where('bk_code', $cgdinfo['bkcode'])->value('id',0),
  2288. ]);
  2289. }
  2290. if($good['is_gold_price']==1 && $good['is_stock']==1){
  2291. $gold = Db::name("gold_price1")
  2292. ->field('id,price')
  2293. ->where(["type" => $good['noble_metal'], "is_del" => 0, "status" => 1])
  2294. ->order("addtime desc")
  2295. ->find();
  2296. $ct['cgd_gold_price']=$gold["price"];
  2297. $updat=[
  2298. "sale_price"=>0,
  2299. "total_price"=>0,
  2300. "origin_price"=>$cgdinfo['good_price'],
  2301. "gold_price"=>$gold["price"],
  2302. ];
  2303. Db::name("sale")->where(["orderCode"=>$outinfo['orderCode']])->update($updat);
  2304. // if($upsale==false){
  2305. // return false;
  2306. // }
  2307. }
  2308. $merge_num = Db::name("purchease_order")
  2309. ->where(["bkcode"=>$cgdinfo['bkcode'],"order_type"=>1,"is_del"=>0])
  2310. ->where("order_source","<>",0)
  2311. ->field("sum(send_num)-sum(th_num) as num")
  2312. ->find();
  2313. $cgd['balance_num']=$cgd['total_num']-$merge_num['num'];
  2314. $cgd['merge_num']=$merge_num['num'];
  2315. $cgd['updatetime']=date("Y-m-d H:i:s");
  2316. $up=Db::name("order_bk")->save($cgd);
  2317. if($up==false) throw new Exception('修改备库单失败');
  2318. $data=[
  2319. "orderCode"=>$outinfo['orderCode'],
  2320. "cgdNo"=>$QrdCgd['cgdNo'],
  2321. "spuCode"=>$outinfo['spuCode'],
  2322. "companyNo"=>$outinfo['companyNo'],
  2323. "good_num"=>$outinfo['good_num'],
  2324. "wsend_num"=>$outinfo['good_num'],
  2325. "send_num"=>0,
  2326. "wait_num"=>$outinfo['good_num'],
  2327. "status"=>1,
  2328. "source"=>1,
  2329. ];
  2330. $order =Db::name("order_num")->save($data);
  2331. if($order==false) throw new Exception('修改采购单销售单关联失败');
  2332. }
  2333. }