After.php 151 KB

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