After.php 144 KB

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