After.php 122 KB

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