After.php 144 KB

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