After.php 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545
  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||$oder['order_type'] == 4) {
  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 || $orderinfo['order_type'] == 4) {
  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']],["num",">",0]])->select()
  1199. ->toArray();
  1200. if(!empty($sabebn)){
  1201. $total_num =$info["error_num"];
  1202. foreach($sabebn as $ve){
  1203. $tempnum=0;
  1204. if ($total_num==0) break;
  1205. if($total_num>=$ve['num']){
  1206. $tempnum = $ve['num'];
  1207. $total_num-=$ve['num'];
  1208. $ve['th_num']+=$ve['num'];
  1209. $ve['num']=0;
  1210. }else{
  1211. $tempnum = $total_num;
  1212. $ve['num']-=$total_num;
  1213. $ve['th_num']+=$total_num;
  1214. $total_num=0;
  1215. }
  1216. if( $info['except_code']==1){
  1217. // if($sale['is_stock']==1){
  1218. // $bnin=GoodStockInfo::AddBn($ve['stockid'],$ve['bnCode'],$tempnum);
  1219. // if($bnin==false){
  1220. // Db::rollback();
  1221. // return error_show(1005, '可售商品Bn库存数入库失败');
  1222. // }
  1223. // }
  1224. $up=Db::name("sale_info")->save($ve);
  1225. if($up==false){
  1226. Db::rollback();
  1227. return error_show(1005, '可售商品Bn库存数入库失败');
  1228. }
  1229. }
  1230. $bnin=GoodStockInfo::ReturnBn($info['returnCode'],$ve['id'],$tempnum);
  1231. if($bnin==false){
  1232. Db::rollback();
  1233. return error_show(1005, '可售商品Bn库存数入库失败');
  1234. }
  1235. }
  1236. }
  1237. if($info['error_num']>=$out['send_num']){
  1238. $out['status']=4;
  1239. $out['updatetime']=date("Y-m-d H:i:s");
  1240. $upout =Db::name("order_out")->save($out);
  1241. if($upout==false){
  1242. Db::rollback();
  1243. return error_show(1005,'出库单数据更新失败');
  1244. }
  1245. }
  1246. $data=[
  1247. "orderCode"=>$info['orderCode'],
  1248. "th_type"=>3,
  1249. "th_num"=>$info['error_num'],
  1250. "th_fee"=>round($sale['sale_price']*$info["error_num"],2),
  1251. "thCode"=>$thNo,
  1252. "spuCode"=>$sale['good_code'],
  1253. "good_name"=>$sale['good_name'],
  1254. "cat_id"=>$sale['cat_id'],
  1255. "apply_id"=>$info['apply_id'],
  1256. "apply_name"=>$info['apply_name'],
  1257. "addtime"=>date("Y-m-d H:i:s"),
  1258. "status"=>1,
  1259. "is_del"=>0
  1260. ];
  1261. $inse=Db::name("th_data")->insert($data);
  1262. if($inse==false){
  1263. Db::rollback();
  1264. return error_show(1004,"退货单更新失败");
  1265. }
  1266. }
  1267. Db::commit();
  1268. return app_show(0,"退货单新建成功",["thNo"=>$thNo]);
  1269. }
  1270. }
  1271. }
  1272. Db::rollback();
  1273. return error_show(1004,'退货失败');
  1274. }catch (\Exception $e){
  1275. Db::rollback();
  1276. return error_show(1004,$e->getMessage());
  1277. }
  1278. }
  1279. //取消售后
  1280. public function Cancel()
  1281. {
  1282. //接收参数
  1283. $data = $this->request->only(['token', 'returnCode'], 'post', 'trim');
  1284. $val = Validate::rule(['returnCode|售后单编号' => 'require']);
  1285. if (!$val->check($data)) return error_show(1005, $val->getError());
  1286. Db::startTrans();
  1287. try {
  1288. $rs = Db::name('order_return')->field('id,status,apply_id')->where([['returnCode', '=', $data['returnCode']], ['is_del', '=', 0], ['status', '<>', 8]])->find();
  1289. if (!$rs) throw new Exception('该售后单记录不存在');
  1290. //修改记录
  1291. $res = Db::name('order_return')->where(['id' => $rs['id']])->update(['status' => 8, 'updatetime' => date('Y-m-d H:i:s')]);
  1292. if (!$res) throw new Exception('该售后单记录修改失败');
  1293. //记录日志
  1294. ActionLog::logAdd($data['token'], ["order_code" => $data['returnCode'], "status" => $rs['status'], "action_remark" => '', "action_type" => "delete"], 'SHD', 8, $this->post);
  1295. 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']]);
  1296. //提交
  1297. Db::commit();
  1298. return app_show(0, "取消成功");
  1299. } catch (\Exception $e) {
  1300. Db::rollback();
  1301. return error_show(1004, $e->getMessage());
  1302. }
  1303. }
  1304. //根据审核完成的售后申请单,创建新的销售单和采购单
  1305. public function createSaleAndCgdByAfter()
  1306. {
  1307. $param = $this->request->only(['token', 'returnCode'], 'post', 'trim');
  1308. $val = Validate::rule(['token' => 'require', 'returnCode|售后申请单编号' => 'require']);
  1309. if (!$val->check($param)) return error_show(1005, $val->getError());
  1310. //查询售后申请单详情
  1311. $info = Db::name('order_return')
  1312. ->where('returnCode', $param['returnCode'])
  1313. ->where('is_del', 0)
  1314. ->find();
  1315. if (empty($info)) return error_show(1005, '该售后申请单不存在');
  1316. if ($info['status'] != 5) return error_show(1005, '该售后申请单尚未审核完成');
  1317. if ($info['is_reissue'] == 1) return error_show(1005, '该售后申请单已完成补发,无法重复进行');
  1318. $apply_id = GetUserInfo($param['token']);
  1319. if (empty($apply_id) || $apply_id['code'] != 0) return error_show(1002, "申请人数据不存在");
  1320. $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1321. $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1322. //原本销售单信息
  1323. $old_sale = Db::name('sale')->where(['is_del' => 0, 'orderCode' => $info['orderCode']])->find();
  1324. if (empty($old_sale)) return error_show(1005, '没有对应的销售单信息');
  1325. //发货单信息
  1326. $old_out = Db::name('order_out')->where(['is_del' => 0, 'orderCode' => $info['orderCode'], 'outCode' => $info['outCode']])->find();
  1327. if (empty($old_out)) return error_show(1005, '没有对应的发货单信息');
  1328. //根据是否咨询类商品,分别处理
  1329. if ($info['order_type'] == 3||$info['order_type'] == 4) return $this->createSaleZixun($info['id'], $info['good_code'], $info, $old_sale, $rm, $ri, $old_out['addrid'], $param['token']);
  1330. else return $this->createSaleNotZixun($info['id'], $info['good_code'], $info, $old_sale, $rm, $ri, $old_out, $param['token']);
  1331. }
  1332. /**
  1333. * 咨询类商品处理
  1334. * @param int $orid 对应售后记录的id
  1335. * @param string $spuCode 商品的spuCode
  1336. * @param array $info 售后申请单的详情
  1337. * @param array $old_sale 原始销售订单
  1338. * @param int $rm 操作人id
  1339. * @param string $ri 操作人名称
  1340. * @param int $addrid 原始发货单的地址id
  1341. * @param string $token 当前用户的token
  1342. * @throws \think\db\exception\DataNotFoundException
  1343. * @throws \think\db\exception\DbException
  1344. * @throws \think\db\exception\ModelNotFoundException
  1345. */
  1346. private function createSaleZixun(int $orid = 0, string $spuCode = '', array $info = [], array $old_sale = [], int $rm = 0, string $ri = '', int $addrid = 0, string $token = '')
  1347. {
  1348. // $bidNo =isset($this->post['bidNo'])&&$this->post['bidNo']!="" ? trim($this->post['bidNo']):"";
  1349. // if($bidNo==""){
  1350. // return error_show(1003,"参数bidNo不能为空");
  1351. // }
  1352. $zxinfo = Db::name("consult_bids")->where(["bidNo" => $old_sale['zxNo'], "is_del" => 0])->find();
  1353. if ($zxinfo == false) return error_show(1005, '未找到咨询单商品信息');
  1354. // if($zxinfo['status']!=6){
  1355. // return error_show(1003,"咨询单状态有误无法转单");
  1356. // }
  1357. // $zx =Db::name("consult_info")->where(["infoNo"=>$zxinfo['infoNo'],"is_del"=>0])->find();
  1358. // if($zx==false){
  1359. // return error_show(1003,"未找到咨询单信息");
  1360. // }
  1361. // if($zx['status']!=4){
  1362. // return error_show(1003,"咨询单状态有误无法转单");
  1363. // }
  1364. // $zxorder =Db::name("consult_order")->where(["zxNo"=>$zx["zxNo"],"is_del"=>0])->find();
  1365. // if($zxorder==false){
  1366. // return error_show(1003,"未找到咨询单信息");
  1367. // }
  1368. // $good_num =isset($this->post['good_num'])&&$this->post['good_num']!="" ? intval($this->post['good_num']):"";
  1369. // if($good_num===""){
  1370. // return error_show(1003,"参数 good_num 不能为空");
  1371. // }
  1372. $good_num = $info['error_num'];
  1373. $sale_price = 0;
  1374. // $sale_price = isset($this->post['sale_price']) && $this->post['sale_price'] != "" ? floatval($this->post['sale_price']) : "";
  1375. // if ($sale_price === "") {
  1376. // return error_show(1003, "参数 sale_price 销售单价 不能为空");
  1377. // }
  1378. // if ($sale_price < $zxinfo['sale_price']) {
  1379. // return error_show(1003, "修改的销售单价不能低于原来的销售单价");
  1380. // }
  1381. // $sendtype = isset($this->post['sendtype'])&&$this->post['sendtype']!="" ? intval($this->post['sendtype']):"";
  1382. // if($sendtype==""){
  1383. // return error_show(1003,"参数sendtype不能为空");
  1384. // }
  1385. // $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  1386. $orderCode = makeNo("QR");
  1387. // $spuCode = $zxinfo['spuCode'];
  1388. $skuCode = "";
  1389. // $is_stock=0;
  1390. //原有采购单信息
  1391. $old_cgd = Db::name('order_num')
  1392. ->alias('on')
  1393. ->field('on.id onid,po.*')
  1394. ->leftJoin('purchease_order po', 'po.cgdNo=on.cgdNo')
  1395. ->where('on.orderCode', $old_sale['orderCode'])
  1396. ->find();
  1397. $cgd = array_merge($old_cgd, [
  1398. 'id' => null,
  1399. // "supplierNo"=>$zxinfo['supplierNo'],
  1400. // "companyNo"=>$zxorder['companyNo'],
  1401. "spuCode" => $spuCode,
  1402. "skuCode" => $skuCode,
  1403. "orderCode" => $orderCode,
  1404. // "good_name"=>$zxinfo['good_name'],
  1405. "sale_price" => $old_cgd['good_price'],//采购单价格不为0,取原采购单的价格
  1406. "total_fee" => round($old_cgd['good_price'] * $good_num, 2),
  1407. // "pakge_fee"=>$zxinfo['pakge_fee'],
  1408. // "cert_fee"=>$zxinfo['cert_fee'],
  1409. // "open_fee"=>$zxinfo['open_fee'],
  1410. 'cost_fee' => $old_cgd['teach_fee'],
  1411. // $old_sale['cost_fee']=$old_sale['teach_fee'];
  1412. // "mark_fee"=>$zxinfo['mark_fee'],
  1413. // "demo_fee"=>$zxinfo['demo_fee'],
  1414. // "nake_fee"=>$zxinfo['nake_fee'],
  1415. // "delivery_fee"=>$zxinfo['delivery_fee'],
  1416. "good_num" => $good_num,
  1417. "createrid" => $rm,
  1418. "creater" => $ri,
  1419. "good_type" => 1,
  1420. // "weight"=>$zxinfo['good_weight'],
  1421. // "gold_price"=>$zxinfo['gold_price'],
  1422. // "is_diff"=>$zxinfo['is_diff'],
  1423. "order_type" => 3,//咨询商品
  1424. "order_source" => 6,//6售后补换货
  1425. // 'send_way'=>$zxinfo['send_way']
  1426. 'token' => $token,
  1427. 'good_createrid' => $zxinfo['createrid'],
  1428. 'good_creater' => $zxinfo['creater'],//商品创建人
  1429. ]);
  1430. // $token=isset($this->post['token'])&&$this->post['token']!=""? trim($this->post['token']):"";
  1431. // if($token==""){
  1432. // return error_show(102, "参数token不能为空");
  1433. // }
  1434. // $apply_id = GetUserInfo($token);
  1435. // if (empty($apply_id) || $apply_id['code'] != 0) {
  1436. // return error_show(102, "申请人数据不存在");
  1437. // }
  1438. // $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1439. // $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1440. // $va = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  1441. // $send_num=0;
  1442. // if($sendtype==1){
  1443. // if ($va == "") {
  1444. // return error_show(1002, "参数order_addr不能为空");
  1445. // }
  1446. // $send_num = array_sum(array_column($va,"receipt_quantity"));
  1447. // }
  1448. // if($good_num<$send_num){
  1449. // return error_show(1002, "发货数量不能超过购买数量");
  1450. // }
  1451. // $arrtime=isset($this->post['arrtime'])&&$this->post['arrtime']!="" ?$this->post['arrtime']:"";
  1452. // if($arrtime==""){
  1453. // return error_show(1002, "参数arrtime不能为空");
  1454. // }
  1455. //新加字段
  1456. // $platform_order=isset($this->post['platform_order'])&&$this->post['platform_order']!="" ?$this->post['platform_order']:"";
  1457. // $workNo=isset($this->post['workNo'])&&$this->post['workNo']!="" ?$this->post['workNo']:"";
  1458. // $proof_type = isset($this->post['proof_type']) && $this->post['proof_type'] != "" ? intval($this->post['proof_type']) : 0;
  1459. // $proof_url = isset($this->post['proof_url']) && $this->post['proof_url'] != "" ? trim($this->post['proof_url']) : '';
  1460. Db::startTrans();
  1461. try {
  1462. // $zx =Db::name("consult_info")->where(["infoNo"=>$zxinfo['infoNo'],"is_del"=>0])->lock(true)->find();
  1463. // if($zx==false){
  1464. // Db::rollback();
  1465. // return error_show(1003,"未找到咨询单信息");
  1466. // }
  1467. $supplier_temp_info = Db::name('supplier')
  1468. ->field('id,person,personid')
  1469. ->where('code',$old_cgd['supplierNo'])
  1470. ->findOrEmpty();
  1471. $tmp_sale_price = $old_sale['sale_price'];//取原售价
  1472. $data = array_merge($old_sale, [
  1473. 'id' => null,
  1474. "orderCode" => $orderCode,
  1475. "good_code" => $spuCode,
  1476. // "skuCode"=>$skuCode,
  1477. // "customer_code"=>$zxorder['khNo'],
  1478. // "good_name"=>isset($zxinfo['good_name'])&&$zxinfo['good_name']!==''?$zxinfo['good_name']:'',
  1479. "good_num" => $good_num,
  1480. // "cat_id"=>$zxinfo['cat_id'],
  1481. "apply_id" => $rm,
  1482. "apply_name" => $ri,
  1483. // "origin_price"=>$zxinfo['total_fee'],
  1484. // "sale_price" => $old_sale['sale_price'],//取原价格
  1485. // "post_fee"=>0,
  1486. "status" => 0,
  1487. "send_status" => 0,
  1488. "th_num" => 0,
  1489. "th_fee" => 0,//退货金额为0
  1490. "send_num" => 0,
  1491. "wsend_num" => $good_num,
  1492. // "send_status"=>1,
  1493. "good_type" => 1,
  1494. // "send_type"=>$sendtype,
  1495. // "supplierNo"=>$zxorder['companyNo'],
  1496. "is_del" => 0,
  1497. // "zxNo"=>$bidNo,
  1498. // "platform_order"=>$platform_order,
  1499. // "platform_id"=> $zxorder['platform_code'],
  1500. "remark" => $old_sale['orderCode'],
  1501. // "arrive_time"=>$arrtime,
  1502. // "is_stock"=>0,
  1503. // "is_activity"=>0,
  1504. // "proof_id"=>0,
  1505. "order_type" => 3,
  1506. "order_source" => 6,//6售后补换货
  1507. // 'good_weight'=>$zxinfo['good_weight'],
  1508. // 'gold_price'=>$zxinfo['gold_price'],
  1509. // 'cost_price'=>$zxinfo['sale_cost_fee'],
  1510. // 'diff_weight'=>0,
  1511. // 'diff_fee'=>0,
  1512. "addtime" => date("Y-m-d H:i:s"),
  1513. "updatetime" => date("Y-m-d H:i:s"),
  1514. 'returnCode' => $info['returnCode'],
  1515. 'total_price' => round(bcmul($tmp_sale_price, $good_num, 3), 2),
  1516. // 'workNo'=>$workNo,
  1517. 'cgderid' => $supplier_temp_info['personid'],
  1518. 'cgder' => $supplier_temp_info['person'],//采购员(供应商负责人)
  1519. 'good_createrid' => $zxinfo['createrid'],
  1520. 'good_creater' => $zxinfo['creater'],//商品创建人
  1521. ]);
  1522. $datainfo = Db::name('sale')->insertGetId($data);
  1523. if ($datainfo > 0) {
  1524. $standing_book_data = [
  1525. 'sale_id' => $datainfo,
  1526. 'infoNo' => $zxinfo['infoNo'],
  1527. 'updatetime' => date('Y-m-d H:i:s')
  1528. ];
  1529. //修改状态,添加待办
  1530. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1531. "order_code" => $orderCode,//销售单code
  1532. "status" => 0,//这里的status是之前的值
  1533. "action_remark" => '',//备注
  1534. "action_type" => "create"//新建create,编辑edit,更改状态status
  1535. ], "XSQRD", $data['status'], $data);
  1536. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  1537. "order_type" => 'XSQRD',
  1538. "order_code" => $orderCode,//销售单code
  1539. "order_id" => $datainfo,
  1540. "order_status" => $data['status'],
  1541. "before_status" => 0,
  1542. 'holder_id' => $data['apply_id']
  1543. ]);
  1544. // $old_zx_status = $zx['status'];
  1545. // $zx['status']=5;
  1546. // $zx['updatetime']=date("Y-m-d H:i:s");
  1547. // $sa=Db::name("consult_info")->save($zx);
  1548. // if($sa==false){
  1549. // Db::rollback();
  1550. // return error_show(1002,"咨询单修改失败");
  1551. // }else{
  1552. // //修改状态,添加待办
  1553. // ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1554. // "order_code" => $zx['infoNo'],//咨询单详情编号
  1555. // "status" => $old_zx_status,//这里的status是之前的值
  1556. // "action_remark" => '',//备注
  1557. // "action_type" => "status"//新建create,编辑edit,更改状态status
  1558. // ], "ZXD", 5, $zx);
  1559. //
  1560. // }
  1561. $this->createCgd($cgd, $standing_book_data);
  1562. // if($bol==false){
  1563. // Db::rollback();
  1564. // return error_show(1002,"咨询订单创建失败");
  1565. // }
  1566. // $limt=[
  1567. // "spuCode"=>$zxinfo['spuCode'],
  1568. // "good_name"=>$zxinfo['good_name'],
  1569. // "brand_id"=>$zxinfo['brand_id'],
  1570. // "good_unit"=>$zxinfo['unit_id'],
  1571. // "cat_id"=>$zxinfo['cat_id'],
  1572. // "good_type"=>0,
  1573. // "moq"=>1,
  1574. // "customized"=>$zxinfo['work_day'],
  1575. // "tax"=>$zxinfo['tax'],
  1576. // "supplierNo"=>$zxinfo["supplierNo"],
  1577. // "is_auth"=>0,
  1578. // "craft_desc"=>$zxinfo['good_name'],
  1579. // "good_remark"=>"",
  1580. // "platform_id"=>$zxorder['platform_id'],
  1581. // "good_img"=>$zxinfo['good_img'],
  1582. // "good_thumb_img"=>"",
  1583. // "good_info_img"=>"",
  1584. // "specinfo"=>$zxinfo['specinfo'],
  1585. // "work_day"=>$zxinfo['work_day'],
  1586. // "noble_metal"=>$zxinfo['metal_id'],
  1587. // "is_gold_price"=>$zxinfo['is_gold_price'],
  1588. // "config"=>$zxinfo['config'],
  1589. // "other_config"=>$zxinfo['other_config'],
  1590. // "weight"=>$zxinfo['weight'],
  1591. // "good_weight"=>$zxinfo['good_weight'],
  1592. // "is_diff"=>$zxinfo['is_diff'],
  1593. // "supply_area"=>$zxinfo['supply_area'],
  1594. // "pay_way"=>$zxinfo['pay_way'],
  1595. // "send_way"=>$zxinfo['send_way'],
  1596. // "companyNo"=>$zxorder['companyNo'],
  1597. // "status"=>1,
  1598. // "is_del"=>0,
  1599. // "createrid"=>$zxinfo['createrid'],
  1600. // "creater"=>$zxinfo['creater'],
  1601. // "addtime"=>date("Y-m-d H:i:s"),
  1602. // "updatetime"=>date("Y-m-d H:i:s"),
  1603. // 'proof_type' => $proof_type,//凭证类型
  1604. // 'proof_url' => $proof_url,//凭证文件
  1605. // ];
  1606. // $good = Db::name("good_zixun")->insert($limt);
  1607. // if(!$good){
  1608. // Db::rollback();
  1609. // return error_show(1006,"咨询商品录入失败");
  1610. // }
  1611. // if(!empty($va)){
  1612. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])->find();
  1613. if (empty($order)) throw new Exception('采购单关联数据未找到');
  1614. // foreach ($va as $value){
  1615. // $temp=[];
  1616. // $addrs=[];
  1617. // if($value['addr_code']!==''&&is_array($value['addr_code'])&&!empty($value['addr_code'])){
  1618. // $addrs['provice_code'] = $value['addr_code'][0];
  1619. // $addrs['city_code'] = $value['addr_code'][1];
  1620. // $addrs['area_code'] = $value['addr_code'][2];
  1621. // $addr = json_encode($addrs);
  1622. // }else{
  1623. // $addr = isset($value['addr_code'])?$value['addr_code']:'';
  1624. // }
  1625. // $temp['orderCode']=$orderCode;
  1626. // $temp['contactor']=$value['contactor'];
  1627. // $temp['mobile'] = $value['mobile'];
  1628. // $temp['addr'] = $value['addr'];
  1629. // $temp['addr_code']=$addr;
  1630. // $temp['customer_code'] =$zxorder['khNo'];
  1631. // $temp['receipt_quantity']=$value['receipt_quantity'];
  1632. // $temp['post_fee'] =0;
  1633. // $temp['is_del'] =0;
  1634. // $temp['addtime'] =date("Y-m-d H:i:s");
  1635. // $temp['updatetime'] =date("Y-m-d H:i:s");
  1636. // $temp['arrive_time']=$arrtime;
  1637. $old_out_addr_info = Db::name('order_out')
  1638. ->alias('oo')
  1639. ->field('oo.id,oa.addr,oa.addr_code,oa.contactor,oa.mobile,oa.customer_code,oa.post_fee,oa.arrive_time')
  1640. ->leftJoin('order_addr oa', 'oa.id=oo.addrid')
  1641. ->where(['oo.outCode' => $info['outCode'], 'oo.orderCode' => $info['orderCode']])
  1642. ->find();
  1643. if (empty($old_out_addr_info)) throw new Exception('发货单对应的地址信息查不到');
  1644. $temp = [
  1645. 'orderCode' => $orderCode,
  1646. 'addr' => $old_out_addr_info['addr'],
  1647. 'addr_code' => $old_out_addr_info['addr_code'],
  1648. 'contactor' => $old_out_addr_info['contactor'],
  1649. 'mobile' => $old_out_addr_info['mobile'],
  1650. 'customer_code' => $old_out_addr_info['customer_code'],
  1651. 'receipt_quantity' => $good_num,
  1652. 'arrive_time' => $old_out_addr_info['arrive_time'],
  1653. 'addtime' => date("Y-m-d H:i:s"),
  1654. 'updatetime' => date("Y-m-d H:i:s"),
  1655. // ''=>'',
  1656. ];
  1657. $addrid = Db::name('order_addr')->insertGetId($temp);
  1658. // if($vmp>0){
  1659. $outCode = makeNo("DF");
  1660. $out = [
  1661. "orderCode" => $orderCode,
  1662. "outCode" => $outCode,
  1663. "apply_id" => $rm,
  1664. "apply_name" => $ri,
  1665. 'addrid' => $addrid,
  1666. "post_name" => "",
  1667. "post_code" => "",
  1668. "post_fee" => 0,
  1669. "sendtime" => date("Y-m-d H:i:s"),
  1670. "send_num" => $good_num,
  1671. "check_num" => 0,
  1672. "error_num" => 0,
  1673. "wsm_code" => "",
  1674. "order_type" => 3,
  1675. "status" => 0,
  1676. "addtime" => date("Y-m-d H:i:s"),
  1677. "updatetime" => date("Y-m-d H:i:s")
  1678. ];
  1679. $ou = Db::name("order_out")->insert($out);
  1680. if ($ou == false) throw new Exception('发货地址添加创建失败');
  1681. else {
  1682. //修改状态,添加待办
  1683. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1684. "order_code" => $outCode,//出库单号
  1685. "status" => 0,//这里的status是之前的值
  1686. "action_remark" => '',//备注
  1687. "action_type" => "create"//新建create,编辑edit,更改状态status
  1688. ], "CKD", 0, $out);
  1689. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  1690. "order_type" => 'CKD',
  1691. "order_code" => $outCode,//出库单号
  1692. "order_id" => Db::name("order_out")->getLastInsID(),
  1693. "order_status" => 0,
  1694. "before_status" => 0,
  1695. 'holder_id' => $out['apply_id']
  1696. ]);
  1697. //将发货编号添加到台账中
  1698. $standing_book_data['outCode'][] = $outCode;
  1699. }
  1700. $order['send_num'] += $good_num;
  1701. $order['wsend_num'] -= $good_num;
  1702. if ($order['wsend_num'] < 0) throw new Exception('发货数量已超出总数');
  1703. $ups = Db::name("order_num")->save($order);
  1704. if ($ups) {
  1705. $sen = Db::name("order_send")->save([
  1706. "cgdNo" => $order['cgdNo'],
  1707. "outCode" => $outCode,
  1708. "send_num" => $good_num,
  1709. "status" => 1,
  1710. "addtime" => date("Y-m-d H:i:s"),
  1711. "updatetime" => date("Y-m-d H:i:s")
  1712. ]);
  1713. if ($sen == false) throw new Exception('发货地址添加创建失败');
  1714. }
  1715. // }else{
  1716. // Db::rollback();
  1717. // return error_show(1002,"发货地址添加创建失败");
  1718. // }
  1719. // }
  1720. //处理台账
  1721. if (isset($standing_book_data['outCode'])) {
  1722. 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']}'");
  1723. unset($standing_book_data['outCode']);
  1724. }
  1725. Db::name('standing_book')->where('infoNo', $standing_book_data['infoNo'])->update($standing_book_data);
  1726. Db::name('order_return')
  1727. ->where(['id' => $orid, 'is_reissue' => 0])
  1728. ->update([
  1729. 'is_reissue' => 1,
  1730. 'newOrderCode' => $orderCode,
  1731. 'updatetime' => date('Y-m-d H:i:s')
  1732. ]);
  1733. Db::commit();
  1734. return app_show(0, "咨询订单创建成功", ["order_code" => $orderCode]);
  1735. }
  1736. Db::rollback();
  1737. return error_show(1002, "咨询订单创建失败");
  1738. } catch (Exception $e) {
  1739. Db::rollback();
  1740. return error_show(1003, $e->getMessage() . '|' . $e->getFile() . ':' . $e->getLine());
  1741. }
  1742. }
  1743. private function createCgd($data,array &$standing_book_da=[]){
  1744. $cgdCode = makeNo("CG");
  1745. $supplier=Db::name("supplier")->where(["code"=>$data['supplierNo'],"is_del"=>0])->find();
  1746. if($supplier==false) throw new Exception('该供应商不存在');
  1747. $wsm= Db::name("warehouse_info")
  1748. ->where(["supplierNo"=>$data["supplierNo"],"companyNo"=>$data['companyNo'],"wsm_type"=>2,"is_del"=>0])
  1749. ->find();
  1750. if($wsm==false){
  1751. $wsm_code = makeNo("WSM");
  1752. $inwsm=[
  1753. "wsm_code"=>$wsm_code,
  1754. "name"=>$supplier['name'],
  1755. "wsm_type"=>2,
  1756. "supplierNo"=>$supplier['code'],
  1757. "addr"=>"",
  1758. "addrs_code"=>"",
  1759. "contactor"=>$data['order_type']==1? $supplier['personid']:$data['createrid'],
  1760. "contactor_name"=>$data['order_type']==1? $supplier['person']:$data['creater'],
  1761. "mobile"=>"",
  1762. "position"=>"",
  1763. "companyNo"=>$data['companyNo'],
  1764. "status"=>1,
  1765. "is_del"=>0,
  1766. "addtime"=>date("Y-m-d H:i:s"),
  1767. "updatetime"=>date("Y-m-d H:i:s")
  1768. ];
  1769. $in = Db::name("warehouse_info")->insert($inwsm);
  1770. if($in==false) throw new Exception('仓库新增失败');
  1771. }else $wsm_code =$wsm['wsm_code'];
  1772. //判断该供应商是否开通了供应商账号
  1773. $from_tag=checkHasAccountBySupplierNos([$data['supplierNo']]);
  1774. $cg =[
  1775. "cgdNo"=>$cgdCode,
  1776. "bkcode"=>"",
  1777. "wsm_code"=>$wsm_code,
  1778. "cgder"=>$data['good_creater'],
  1779. "cgder_id"=>$data['good_createrid'],
  1780. "spuCode"=>$data['spuCode'],
  1781. "skuCode"=>$data['skuCode'],
  1782. "good_name"=>$data['good_name'],
  1783. "good_num"=>$data['good_num'],
  1784. "good_price"=>$data['sale_price'],
  1785. "total_fee"=>round($data['sale_price']*$data['good_num'],2),
  1786. "pakge_fee"=>$data['pakge_fee'],
  1787. "cert_fee"=>$data['cert_fee'],
  1788. "open_fee"=>$data['open_fee'],
  1789. "teach_fee"=>$data['cost_fee'],
  1790. "mark_fee"=>$data['mark_fee'],
  1791. "demo_fee"=>$data['demo_fee'],
  1792. "nake_fee"=>$data['nake_fee'],
  1793. "weight"=>$data['weight'],
  1794. "delivery_fee"=>$data['delivery_fee'],
  1795. "gold_price"=>$data['gold_price'],
  1796. "diff_weight"=>"0",
  1797. "diff_fee"=>"0",
  1798. "supplierNo"=>$data['supplierNo'],
  1799. "supplier_name"=>$supplier['name'],
  1800. "companyNo"=>$data['companyNo'],
  1801. "send_status"=>1,
  1802. "send_num"=>0,
  1803. "wsend_num"=>$data['good_num'],
  1804. "remark"=>"",
  1805. "status"=>0,//0初始化
  1806. "lasttime"=>date("Y-m-d H:i:s"),
  1807. "is_del"=>0,
  1808. "order_type"=>$data['order_type'],
  1809. "order_source"=>$data['order_source'],
  1810. "good_type"=>$data['good_type'],
  1811. "addtime"=>date("Y-m-d H:i:s"),
  1812. "updatetime"=>date("Y-m-d H:i:s"),
  1813. 'good_createrid' => $data['good_createrid'],
  1814. 'good_creater' => $data['good_createrid'],//商品创建人
  1815. 'from_tag' => isset($from_tag[$data['supplierNo']]) ? 2 : 1,//来源标签:1采销(默认),2供应商端
  1816. ];
  1817. $up =Db::name("purchease_order")->insertGetId($cg);
  1818. if($up){
  1819. //修改状态,添加待办
  1820. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1821. "order_code" => $cg['cgdNo'],//销售单code
  1822. "status" => 0,//这里的status是之前的值
  1823. "action_remark" => '',//备注
  1824. "action_type" => "create"//新建create,编辑edit,更改状态status
  1825. ], "CGD", $cg['status'], $cg);
  1826. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  1827. "order_type" => 'CGD',
  1828. "order_code" => $cg['cgdNo'],//销售单code
  1829. "order_id" => $up,
  1830. "order_status" => $cg['status'],
  1831. "before_status"=> 0,
  1832. 'holder_id' => $supplier['personid'],
  1833. 'wait_id' => $supplier['personid'],
  1834. 'wait_name' => $supplier['person'],
  1835. ]);
  1836. $standing_book_da = array_merge($standing_book_da, [
  1837. 'orderCode' => $data['orderCode'],
  1838. 'cgdNo' => $cgdCode,
  1839. 'spuCode' => $data['spuCode'],
  1840. 'skuCode' => $data['skuCode'],
  1841. 'order_type' => $data['order_type'],
  1842. 'order_source' => $data['order_source'],
  1843. 'supplierNo' => $data['supplierNo'],
  1844. "companyNo" => $data['companyNo'],
  1845. ]);
  1846. $rela=[
  1847. "orderCode"=>$data['orderCode'],
  1848. "cgdNo"=>$cgdCode,
  1849. "spuCode"=>$data['spuCode'],
  1850. "good_num"=>$data['good_num'],
  1851. "wsend_num"=>$data['good_num'],
  1852. "send_num"=>0,
  1853. "wait_num"=>0,
  1854. "status"=>1,
  1855. "source"=>2
  1856. ];
  1857. Db::name("order_num")->save($rela);
  1858. $stokc =Db::name("good_stock")
  1859. ->where(['spuCode'=>$data['spuCode'],"wsm_code"=>$wsm_code, "is_del"=>0])
  1860. ->find();
  1861. if($stokc==false){
  1862. $stokc=[
  1863. "spuCode"=>$data['spuCode'],
  1864. "wsm_code"=>$wsm_code,
  1865. "wait_in_stock"=>$data['good_num'],
  1866. "wait_out_stock"=>0,
  1867. "usable_stock"=>0,
  1868. "intra_stock"=>0,
  1869. "total_stock"=>0,
  1870. "status"=>1,
  1871. "addtime"=>date("Y-m-d H:i:s"),
  1872. "updatetime"=>date("Y-m-d H:i:s")
  1873. ];
  1874. }else{
  1875. $stokc['wait_in_stock']+=$data['good_num'];
  1876. $stokc['updatetime']=date("Y-m-d H:i:s");
  1877. }
  1878. $stoc= Db::name("good_stock")->save($stokc);
  1879. if($stoc==false) throw new Exception('商品仓库库存修改失败');
  1880. $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"];
  1881. GoodLog::LogAdd($data['token'],$good_data,"CGD");
  1882. return true;
  1883. }else throw new Exception('商品仓库库存修改失败');
  1884. }
  1885. //创建采购单
  1886. /**
  1887. * 非咨询类商品处理
  1888. * @param int $orid 售后记录的id
  1889. * @param string $spuCode 商品的spuCode
  1890. * @param array $info 售后申请单的详情
  1891. * @param array $old_sale 原始销售订单信息
  1892. * @param int $rm 操作人id
  1893. * @param string $ri 操作人名称
  1894. * @param array $old_out 原始发货单信息
  1895. * @param string $token 当前用户的token
  1896. * @throws \think\db\exception\DataNotFoundException
  1897. * @throws \think\db\exception\DbException
  1898. * @throws \think\db\exception\ModelNotFoundException
  1899. */
  1900. private function createSaleNotZixun(int $orid = 0, string $spuCode = '', array $info = [], array $old_sale = [], int $rm = 0, string $ri = '', array $old_out = [], string $token = '')
  1901. {
  1902. $goodinfo = Db::name('good')
  1903. ->alias('g')
  1904. ->field('g.*,gp.skuCode')
  1905. ->join('good_platform gp', 'gp.spuCode=g.spuCode AND gp.exam_status=6')
  1906. ->where('g.spuCode', $spuCode)
  1907. ->where('gp.skuCode', $old_sale['skuCode'])
  1908. ->find();
  1909. if (empty($goodinfo)) return error_show(1005, '查不到这个商品,或该商品已下线无法售后');
  1910. $is_activity = $old_sale['is_activity'];
  1911. $actcode = $old_sale['activity_code'];
  1912. $good_num = $info['error_num'];//数量,取售后申请单中的异常数量
  1913. $supplierNo = $old_sale['supplierNo'];
  1914. $sale_price = 0;
  1915. $origin_price = $old_sale['origin_price'];
  1916. $goodtype = $old_sale['good_type'];
  1917. $is_stock = $goodinfo['is_stock'];
  1918. $skuCode = $goodinfo['skuCode'];
  1919. $orderCode = makeNo("QR");
  1920. if ($is_stock == 1) {
  1921. $stock_num = Db::name("good_stock")->alias("a")
  1922. ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")
  1923. ->leftJoin("good_stock_info c", "a.id=c.stockid and c.balance_num>0")
  1924. ->where(["spuCode" => $spuCode, "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5, "b.companyNo" =>$supplierNo])
  1925. ->sum('balance_num');
  1926. if($stock_num < $good_num){
  1927. return error_show(1003, "批次库存数量不足"); //库存不足
  1928. }
  1929. }
  1930. $goodinfo['cgd_gold_price'] = 0;
  1931. if ($goodtype == 1) {
  1932. if ($is_activity == 1) {
  1933. $act = Db::name("activity_info")
  1934. ->alias("a")
  1935. ->leftJoin("good_activity b", "a.activity_code=b.activity_code")
  1936. ->where(["a.skuCode" => $skuCode, "a.activity_code" => $actcode, "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0])
  1937. ->find();
  1938. if ($act == false) return error_show(1003, "未找到相关活动价");
  1939. if ($act['moq_num'] > $good_num) return error_show(1003, "商品不满足活动价起订量{$act['moq_num']}");
  1940. if ($act['activity_stock'] < $good_num) return error_show(1003, "商品活动库存剩余{$act['activity_stock']}");
  1941. }
  1942. // else {
  1943. // $good = Db::name("good_ladder")
  1944. // ->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])
  1945. // ->where([["min_num", "<=", $good_num]])
  1946. // ->order("min_num desc")
  1947. // ->find();
  1948. //
  1949. // if ($good == false) return error_show(1003, "未找到相关阶梯价格");
  1950. //
  1951. // if ($goodinfo['is_gold_price'] == 1 && $is_stock != 1) {
  1952. // $gold = Db::name("gold_price1")
  1953. // ->field('id,price')
  1954. // ->where(["type" => $goodinfo['noble_metal'], "is_del" => 0, "status" => 1])
  1955. // ->order("addtime desc")
  1956. // ->find();
  1957. //
  1958. // $goodinfo['cgd_gold_price'] = $gold["price"];
  1959. // }
  1960. // }
  1961. }
  1962. //原有采购单信息
  1963. if ($is_stock != 1){
  1964. $old_cgd = Db::name('order_num')
  1965. ->alias('on')
  1966. ->field('on.id onid,po.*')
  1967. ->leftJoin('purchease_order po', 'po.cgdNo=on.cgdNo')
  1968. ->where('on.orderCode', $old_sale['orderCode'])
  1969. ->find();
  1970. $cgd = array_merge($old_cgd, [
  1971. 'id' => null,
  1972. // "supplierNo" => $ct['supplierNo'],
  1973. // "companyNo" => $supplierNo,
  1974. "orderCode" => $orderCode,
  1975. "spuCode" => $spuCode,
  1976. "skuCode" => $skuCode,
  1977. // "good_name" => $ct['good_name'],
  1978. "sale_price" => $old_cgd['good_price'],//采购单价格不为0,取原采购单的价格
  1979. "total_fee" => $old_cgd['good_price'] * $good_num,
  1980. // "pakge_fee" => isset($origin['package_fee']) ? $origin['package_fee'] : 0,
  1981. // "cert_fee" => isset($origin['cert_fee']) ? $origin['cert_fee'] : 0,
  1982. // "open_fee" => $ct['open_fee'],
  1983. // "cost_fee" => isset($origin['cost_fee']) ? $origin['cost_fee'] : 0,
  1984. 'cost_fee' => $old_cgd['teach_fee'],
  1985. // "mark_fee" => isset($origin['mark_fee']) ? $origin['mark_fee'] : 0,
  1986. // "nake_fee" => isset($origin['nake_fee']) ? $origin['nake_fee'] : 0,
  1987. // "delivery_fee" => isset($origin['delivery_fee']) ? $origin['delivery_fee'] : 0,
  1988. // "demo_fee" => $ct['demo_fee'],
  1989. "good_num" => $good_num,
  1990. // "good_type" => $goodtype,
  1991. // "weight" => $ct['noble_weight'],
  1992. // "gold_price" => isset($ct['cgd_gold_price']) ? $ct['cgd_gold_price'] : 0,
  1993. // "order_type" => $is_stock == 1 ? 1 : 2,
  1994. "order_source" => 6,//6售后补换货
  1995. "createrid" => $rm,
  1996. "creater" => $ri,
  1997. // 'send_way' => 2,
  1998. 'token' => $token,
  1999. 'addtime' => date('Y-m-d H:i:s'),
  2000. 'updatetime' => date('Y-m-d H:i:s'),
  2001. 'good_createrid' => $goodinfo['createrid'],
  2002. 'good_creater' => $goodinfo['creater'],//商品创建人
  2003. ]);
  2004. $supplier_temp_info = Db::name('supplier')
  2005. ->field('id,person,personid')
  2006. ->where('code',$cgd['supplierNo'])
  2007. ->findOrEmpty();
  2008. }else{
  2009. $supplier_temp_info = Db::name('supplier')
  2010. ->field('id,person,personid')
  2011. ->where('code',$goodinfo['supplierNo'])
  2012. ->findOrEmpty();
  2013. }
  2014. // $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !=="" ?$this->post['addrlist']:"";
  2015. $send_num = 0;
  2016. // if($sendtype==1){
  2017. // if($addrlist=="" || empty($addrlist) ||!is_array($addrlist)){
  2018. // return error_show(1004,"参数addrlist不能为空");
  2019. // }
  2020. // $send_num = array_sum(array_column($addrlist,"receipt_quantity"));
  2021. // }
  2022. // if($send_num>$good_num){
  2023. // return error_show(1004,"发货数量超出订单总数量");
  2024. // }
  2025. // $remark =isset($this->post['remark']) && $this->post['remark'] !=="" ?trim($this->post['remark']):"";
  2026. // $proof_id =isset($this->post['proof_id']) && $this->post['proof_id'] !=="" ?intval($this->post['proof_id']):0;
  2027. // $apply_id = GetUserInfo($param['token']);
  2028. // if (empty($apply_id) || $apply_id['code'] != 0) return error_show(1002, "申请人数据不存在");
  2029. // $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  2030. // $ri = isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  2031. Db::startTrans();
  2032. try {
  2033. $tmp_sale_price = $old_sale['sale_price'];//取原价格
  2034. $data = array_merge($old_sale, [
  2035. 'id' => null,
  2036. "orderCode" => $orderCode,
  2037. "good_code" => $spuCode,
  2038. "skuCode" => $skuCode,
  2039. // "customer_code" => $info['customer_code'],
  2040. // "good_name" => (isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '') ? $goodinfo['good_name'] : '',
  2041. "good_num" => $good_num,
  2042. // "cat_id" => $goodinfo['cat_id'],
  2043. "apply_id" => $rm,
  2044. "apply_name" => $ri,
  2045. "origin_price" => $origin_price,
  2046. // "sale_price" => $old_sale['sale_price'],//取原价格
  2047. "total_price" => round(bcmul($good_num, $tmp_sale_price, 3), 2),//重新计算总价
  2048. // "post_fee" => 0,
  2049. // "status" => 0,
  2050. "th_num" => 0,
  2051. "th_fee" => 0,//退货金额为0
  2052. "send_num" => 0,
  2053. "wsend_num" => $good_num,
  2054. // "send_status" => 0,
  2055. // "good_type" => $goodtype,
  2056. // "send_type" => $old_sale['send_type'],
  2057. // "supplierNo" => $supplierNo,
  2058. // "is_del" => 0,
  2059. // "zxNo" => "",
  2060. // "platform_order" => $old_sale['platform_order'],
  2061. // "platform_id" => $old_sale['platform_id'],
  2062. "remark" => $old_sale['orderCode'],
  2063. // "is_stock" => $is_stock,
  2064. "is_activity" => $is_activity,
  2065. "activity_code" => $actcode,
  2066. // "order_type" => $is_stock == 1 ? 1 : 2,
  2067. "order_source" => 6,//6售后补换货
  2068. // "poNo"=>$poNo,
  2069. // 'good_weight' => $ct['noble_weight'],
  2070. // 'gold_price' => isset($ct['cgd_gold_price']) ? $ct['cgd_gold_price'] : 0,
  2071. // 'cost_price' => isset($good['cost_fee']) ? $good['cost_fee'] : 0,
  2072. // 'diff_weight' => 0,
  2073. // 'diff_fee' => 0,
  2074. // "workNo" => '',
  2075. 'status' => 0,
  2076. "addtime" => date("Y-m-d H:i:s"),
  2077. "updatetime" => date("Y-m-d H:i:s"),
  2078. // 'total_price' => round($sale_price * $good_num, 2),
  2079. // 'proof_id' => $old_sale['proof_id'],
  2080. // 'paytime' => $old_sale['paytime'],
  2081. 'returnCode' => $info['returnCode'],
  2082. 'cgderid' => $supplier_temp_info['personid'],
  2083. 'cgder' => $supplier_temp_info['person'],//采购员(供应商负责人)
  2084. 'good_createrid' => $goodinfo['createrid'],
  2085. 'good_creater' => $goodinfo['creater'],//商品创建人
  2086. ]);
  2087. $datainfo = Db::name('sale')->insertGetId($data);
  2088. if ($datainfo > 0) {
  2089. //修改状态,添加待办
  2090. ActionLog::logAdd($token, [
  2091. "order_code" => $orderCode,//销售单code
  2092. "status" => 0,//这里的status是之前的值
  2093. "action_remark" => '',//备注
  2094. "action_type" => "create"//新建create,编辑edit,更改状态status
  2095. ], "XSQRD", 0, $this->post);
  2096. ProcessOrder::AddProcess($token, [
  2097. "order_type" => 'XSQRD',
  2098. "order_code" => $orderCode,//销售单code
  2099. "order_id" => $datainfo,
  2100. "order_status" => 0,
  2101. "before_status" => 0,
  2102. 'holder_id' => $data['apply_id']
  2103. ]);
  2104. if ($is_activity == 1) {
  2105. $actup = [
  2106. "activity_stock" => $act['activity_stock'] - $good_num,
  2107. "updatetime" => date("Y-m-d H:i:s")
  2108. ];
  2109. $actupp = Db::name("activity_info")
  2110. ->where(["skuCode" => $skuCode, "activity_code" => $actcode, "is_del" => 0, "status" => 1])
  2111. ->save($actup);
  2112. if ($actupp == false) throw new Exception('活动库存修改失败');
  2113. }
  2114. $outstatus = 0;
  2115. $standing_book_da = ['sale_id' => $datainfo, 'customer_code' => $old_sale['customer_code'], 'skuCode' => $skuCode, 'updatetime' => date('Y-m-d H:i:s')];
  2116. if ($is_stock == 0) $this->createCgd($cgd, $standing_book_da);
  2117. else {
  2118. $outstatus = 1;
  2119. $stockid= Db::name("good_stock")->alias("a")
  2120. ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")
  2121. ->where(["spuCode" => $spuCode, "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5, "b.companyNo" =>$supplierNo])
  2122. ->column('a.id');
  2123. $stockinfo =GoodStockInfo::OrderBn($orderCode,$stockid,$good_num);
  2124. if($stockinfo==false){
  2125. throw new Exception('库存商品更新库存失败');
  2126. }
  2127. // $bol = $this->RelaCgd([
  2128. // 'orderCode' => $orderCode,
  2129. // "good_num" => $good_num,
  2130. // "spuCode" => $spuCode,
  2131. // "companyNo" => $supplierNo,
  2132. // "cost_fee" => isset($goodinfo['cost_fee']) ? $goodinfo['cost_fee'] : 0,
  2133. // 'order_type' => $data['order_type'],
  2134. // 'order_source' => $data['order_source'],
  2135. // ], $standing_book_da);
  2136. // if (isset($stock)) {
  2137. //
  2138. // $stck = [
  2139. // "usable_stock" => $stock['usable_stock'] - $good_num,
  2140. // "wait_out_stock" => $stock['wait_out_stock'] + $good_num,
  2141. // "updatetime" => date("Y-m-d H:i:s")
  2142. // ];
  2143. //
  2144. // $upad = Db::name("good_stock")->where($stock)->update($stck);
  2145. // if ($upad == false) throw new Exception('库存商品更新库存失败');
  2146. // $stockinfo =GoodStockInfo::OrderBn($orderCode,$stock['id'],$good_num);
  2147. // if($stockinfo==false){
  2148. // throw new Exception('库存商品更新库存失败'); //库存不足
  2149. // }
  2150. // //商品变动日志表,good_log_code字段存储采购单号
  2151. // $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 2, 'stock' => $good_num, "stock_name" => "usable_stock"];
  2152. // $good_data[] = ['good_log_code' => $orderCode, "stock_id" => $stock['id'], "type" => 1, 'stock' => $good_num, "stock_name" => "wait_out_stock"];
  2153. // GoodLog::LogAdd($token, $good_data, "XSQRD");
  2154. // }
  2155. }
  2156. // $old_out = Db::name('order_out')
  2157. // ->field('id,addrid,wsm_code')
  2158. // ->where(['orderCode' => $info['orderCode'], 'outCode' => $info['outCode']])
  2159. // ->find();
  2160. $old_out_addr_info = Db::name('order_out')
  2161. ->alias('oo')
  2162. ->field('oo.id,oa.addr,oa.addr_code,oa.contactor,oa.mobile,oa.customer_code,oa.post_fee,oa.arrive_time')
  2163. ->leftJoin('order_addr oa', 'oa.id=oo.addrid')
  2164. ->where(['oo.outCode' => $info['outCode'], 'oo.orderCode' => $info['orderCode']])
  2165. ->find();
  2166. if (empty($old_out_addr_info)) throw new Exception('发货单对应的地址信息查不到');
  2167. $temp = [
  2168. 'orderCode' => $orderCode,
  2169. 'addr' => $old_out_addr_info['addr'],
  2170. 'addr_code' => $old_out_addr_info['addr_code'],
  2171. 'contactor' => $old_out_addr_info['contactor'],
  2172. 'mobile' => $old_out_addr_info['mobile'],
  2173. 'customer_code' => $old_out_addr_info['customer_code'],
  2174. 'receipt_quantity' => $good_num,
  2175. 'arrive_time' => $old_out_addr_info['arrive_time'],
  2176. 'addtime' => date("Y-m-d H:i:s"),
  2177. 'updatetime' => date("Y-m-d H:i:s"),
  2178. // ''=>'',
  2179. ];
  2180. $addrid = Db::name('order_addr')->insertGetId($temp);
  2181. $outCode = makeNo("DF");
  2182. $out = array_merge($old_out, [
  2183. 'id' => null,
  2184. "orderCode" => $orderCode,
  2185. "outCode" => $outCode,
  2186. "apply_id" => $rm,
  2187. "apply_name" => $ri,
  2188. "addrid" => $addrid,
  2189. "post_name" => "",
  2190. "post_code" => "",
  2191. "post_fee" => 0,
  2192. "sendtime" => date("Y-m-d H:i:s"),
  2193. "send_num" => $good_num,
  2194. "check_num" => 0,
  2195. "error_num" => 0,
  2196. "wsm_code" => isset($old_out['wsm_code']) ? $old_out['wsm_code'] : '',
  2197. "order_type" => $is_stock == 1 ? 1 : 2,
  2198. "status" => $outstatus,
  2199. "addtime" => date("Y-m-d H:i:s"),
  2200. "updatetime" => date("Y-m-d H:i:s")
  2201. ]);
  2202. Db::name("order_out")->insert($out);
  2203. $standing_book_da['outCode'] = $outCode;
  2204. if($is_stock==0){
  2205. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])->find();
  2206. if (empty($order)) throw new Exception('采购单关联数据未找到');
  2207. $order['send_num'] += $good_num;
  2208. $order['wsend_num'] -= $good_num;
  2209. if ($order['wsend_num'] < 0) throw new Exception('发货数量已超出总数');
  2210. $ups = Db::name("order_num")->save($order);
  2211. if ($ups) {
  2212. $sen = Db::name("order_send")->save([
  2213. "cgdNo" => $order['cgdNo'],
  2214. "outCode" => $outCode,
  2215. "send_num" => $good_num,
  2216. "status" => 1,
  2217. "addtime" => date("Y-m-d H:i:s"),
  2218. "updatetime" => date("Y-m-d H:i:s")
  2219. ]);
  2220. if ($sen == false) throw new Exception('发货地址添加创建失败');
  2221. }
  2222. }
  2223. if (!isset($standing_book_da['returnCode'])) $standing_book_da['returnCode'] = '';
  2224. if (!isset($standing_book_da['thNo'])) $standing_book_da['thNo'] = '';
  2225. if (!isset($standing_book_da['returnGoodCode'])) $standing_book_da['returnGoodCode'] = '';
  2226. if (!isset($standing_book_da['wsm_in_code'])) $standing_book_da['wsm_in_code'] = '';
  2227. if (!isset($standing_book_da['cgdReturnCode'])) $standing_book_da['cgdReturnCode'] = '';
  2228. //处理台账
  2229. 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')]));
  2230. //维护售后申请单
  2231. Db::name('order_return')
  2232. ->where(['id' => $orid, 'is_reissue' => 0])
  2233. ->update([
  2234. 'is_reissue' => 1,
  2235. 'newOrderCode' => $orderCode,
  2236. 'updatetime' => date('Y-m-d H:i:s')
  2237. ]);
  2238. Db::commit();
  2239. return error_show(0, "操作成功");
  2240. } else throw new Exception('咨询订单创建失败');
  2241. } catch (\Exception $e) {
  2242. Db::rollback();
  2243. return error_show(1005, $e->getMessage() . '|' . $e->getFile() . '|' . $e->getLine());
  2244. }
  2245. }
  2246. //创建采购单
  2247. public function RelaCgd($outinfo,array &$standing_book_da=[]){
  2248. $cgd = Db::name("order_bk")->where([["spuCode","=",$outinfo['spuCode']],["is_del","=",0],["balance_num",">=",$outinfo['good_num']],['companyNo',"=",$outinfo['companyNo']]])->lock(true)->find();
  2249. if($cgd==false) throw new Exception('未查询到备库单信息');
  2250. $good=Db::name("good")
  2251. ->where(["spuCode"=>$outinfo['spuCode'],"is_del"=>0])
  2252. ->find();
  2253. if($good==false) throw new Exception('未查询到商品信息');
  2254. $cgdinfo =Db::name("purchease_order")
  2255. ->where(['cgdNo'=>$cgd['cgdNo'],"is_del"=>0])
  2256. ->find();
  2257. if($cgdinfo==false) throw new Exception('未查询到采购单信息');
  2258. //判断该供应商是否开通了供应商账号
  2259. $from_tag=checkHasAccountBySupplierNos([$cgdinfo['supplierNo']]);
  2260. $QrdCgd=[
  2261. "cgdNo"=>makeNo("CG"),
  2262. "bkcode" => $cgdinfo['bkcode'],
  2263. 'wsm_code'=>$cgdinfo['wsm_code'],
  2264. "cgder_id"=>$cgdinfo['cgder_id'],
  2265. "cgder"=>$cgdinfo['cgder'],
  2266. "spuCode"=>$cgdinfo['spuCode'],
  2267. "good_name"=>$cgdinfo['good_name'],
  2268. "good_num"=>$outinfo['good_num'],
  2269. "good_price"=>$cgdinfo['good_price'],
  2270. "total_fee"=>round($cgdinfo['good_price']*$outinfo['good_num'],2),
  2271. "pakge_fee"=>$cgdinfo['pakge_fee'],
  2272. "cert_fee"=>$cgdinfo['cert_fee'],
  2273. "open_fee"=>$cgdinfo['open_fee'],
  2274. "delivery_fee"=>$cgdinfo['delivery_fee'],
  2275. "mark_fee"=>$cgdinfo['mark_fee'],
  2276. "teach_fee"=>$cgdinfo['teach_fee'],
  2277. "nake_fee"=>$cgdinfo['nake_fee'],
  2278. "demo_fee"=>$cgdinfo['demo_fee'],
  2279. "weight"=>$cgdinfo['weight'],
  2280. "diff_weight"=>$cgdinfo['diff_weight'],
  2281. "diff_fee"=>$cgdinfo['diff_fee'],
  2282. "gold_price"=>$cgdinfo['gold_price'],
  2283. "supplierNo"=>$cgdinfo['supplierNo'],
  2284. "supplier_name"=>$cgdinfo['supplier_name'],
  2285. "companyNo"=>$cgdinfo['companyNo'],
  2286. "send_status"=>3,
  2287. "send_num"=>$outinfo['good_num'],
  2288. "wsend_num"=>0,
  2289. "remark"=>$cgdinfo['remark'],
  2290. "status"=>3,
  2291. "lasttime"=>$cgdinfo['lasttime'],
  2292. "is_del"=>0,
  2293. "order_type"=>$outinfo['order_type'],
  2294. "order_source"=>$outinfo['order_source'],
  2295. "good_type"=>$cgdinfo['good_type'],
  2296. "addtime"=>date("Y-m-d H:i:s"),
  2297. "updatetime"=>date("Y-m-d H:i:s"),
  2298. 'good_createrid' => $good['createrid'],
  2299. 'good_creater' => $good['creater'],//商品创建人
  2300. 'from_tag' => isset($from_tag[$cgdinfo['supplierNo']]) ? 2 : 1,//来源标签:1采销(默认),2供应商端
  2301. ];
  2302. $insetrCgd=Db::name("purchease_order")->insert($QrdCgd);
  2303. if($insetrCgd==false) throw new Exception('新增采购单失败');
  2304. else{
  2305. $standing_book_da = array_merge($standing_book_da, [
  2306. 'orderCode' => $outinfo['orderCode'],
  2307. 'cgdNo' => $cgdinfo['cgdNo'],
  2308. 'spuCode' => $outinfo['spuCode'],
  2309. 'order_type' => $QrdCgd['order_type'],
  2310. 'order_source' => $QrdCgd['order_source'],
  2311. 'supplierNo' => $cgdinfo['supplierNo'],
  2312. 'companyNo' => $cgdinfo['companyNo'],
  2313. 'bk_code' => $cgdinfo['bkcode'],
  2314. 'purchease_id' => Db::name('purchease')->where('bk_code', $cgdinfo['bkcode'])->value('id',0),
  2315. ]);
  2316. }
  2317. if($good['is_gold_price']==1 && $good['is_stock']==1){
  2318. $gold = Db::name("gold_price1")
  2319. ->field('id,price')
  2320. ->where(["type" => $good['noble_metal'], "is_del" => 0, "status" => 1])
  2321. ->order("addtime desc")
  2322. ->find();
  2323. $ct['cgd_gold_price']=$gold["price"];
  2324. $updat=[
  2325. "sale_price"=>0,
  2326. "total_price"=>0,
  2327. "origin_price"=>$cgdinfo['good_price'],
  2328. "gold_price"=>$gold["price"],
  2329. ];
  2330. Db::name("sale")->where(["orderCode"=>$outinfo['orderCode']])->update($updat);
  2331. // if($upsale==false){
  2332. // return false;
  2333. // }
  2334. }
  2335. $merge_num = Db::name("purchease_order")
  2336. ->where(["bkcode"=>$cgdinfo['bkcode'],"order_type"=>1,"is_del"=>0])
  2337. ->where("order_source","<>",0)
  2338. ->field("sum(send_num)-sum(th_num) as num")
  2339. ->find();
  2340. $cgd['balance_num']=$cgd['total_num']-$merge_num['num'];
  2341. $cgd['merge_num']=$merge_num['num'];
  2342. $cgd['updatetime']=date("Y-m-d H:i:s");
  2343. $up=Db::name("order_bk")->save($cgd);
  2344. if($up==false) throw new Exception('修改备库单失败');
  2345. $data=[
  2346. "orderCode"=>$outinfo['orderCode'],
  2347. "cgdNo"=>$QrdCgd['cgdNo'],
  2348. "spuCode"=>$outinfo['spuCode'],
  2349. "companyNo"=>$outinfo['companyNo'],
  2350. "good_num"=>$outinfo['good_num'],
  2351. "wsend_num"=>$outinfo['good_num'],
  2352. "send_num"=>0,
  2353. "wait_num"=>$outinfo['good_num'],
  2354. "status"=>1,
  2355. "source"=>1,
  2356. ];
  2357. $order =Db::name("order_num")->save($data);
  2358. if($order==false) throw new Exception('修改采购单销售单关联失败');
  2359. }
  2360. }