After.php 153 KB

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