After.php 133 KB

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