After.php 145 KB

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