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