After.php 124 KB

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