After.php 122 KB

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