After.php 123 KB

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