After.php 141 KB

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