After.php 150 KB

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