After.php 143 KB

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