After.php 140 KB

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