After.php 144 KB

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