After.php 146 KB

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