After.php 122 KB

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