After.php 123 KB

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