After.php 109 KB

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