After.php 152 KB

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