After.php 122 KB

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