After.php 150 KB

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