After.php 153 KB

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