After.php 123 KB

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