After.php 144 KB

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