After.php 152 KB

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