After.php 153 KB

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