Salezx.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\GoodLog;
  4. use app\admin\model\ProcessOrder;
  5. use think\App;
  6. use think\facade\Cache;
  7. use think\facade\Config;
  8. use think\facade\Db;
  9. use app\admin\model\ActionLog;
  10. class Salezx extends \app\BaseController
  11. {
  12. private $cgd_data = [];//存储采购单的数据,目前是为了做供应商api推送而准备的
  13. public $post="";
  14. public function __construct(App $app)
  15. {
  16. parent::__construct($app);
  17. $this->post=$this->request->post();
  18. }
  19. public function create(){
  20. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  21. $orderCode=makeNo("QR");
  22. $customer_code= isset($this->post['customer_code'])&& $this->post['customer_code'] !=="" ? trim($this->post['customer_code']) :"";
  23. if($customer_code==""){
  24. return error_show(1002,"客户code不能为空");
  25. }
  26. $zxinfoNo= isset($this->post['zxinfoNo'])&& $this->post['zxinfoNo'] !=="" ? trim($this->post['zxinfoNo']) :"";
  27. if($zxinfoNo==""){
  28. return error_show(1002,"参数zxinfoNo 不能为空");
  29. }
  30. $customer=Db::name("customer_info")->where(["companyNo"=>$customer_code])->find();
  31. if($customer==false){
  32. return error_show(1004,"未找到客户数据");
  33. }
  34. $supplierNo= isset($this->post['supplierNo']) && $this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']) :"";
  35. if($supplierNo==""){
  36. return error_show(1002,"参数supplierNo不能为空");
  37. }
  38. $supplier=Db::name("business")->where(["companyNo"=>$supplierNo])->find();
  39. if($supplier==false){
  40. return error_show(1004,"未找到平台供应商数据");
  41. }
  42. $sendtype =isset($this->post['sendtype']) && $this->post['sendtype'] !=="" ?trim($this->post['sendtype']):"";
  43. if($sendtype==''){
  44. return error_show(1004,"参数sendtype不能为空");
  45. }
  46. $platform_id =isset($this->post['platform_id']) && $this->post['platform_id'] !=="" ?intval($this->post['platform_id']):"";
  47. if($platform_id==''){
  48. return error_show(1004,"参数platform_id不能为空");
  49. }
  50. $platform_order =isset($this->post['platform_order']) && $this->post['platform_order'] !=="" ?trim($this->post['platform_order']):"";
  51. if($platform_order==''){
  52. return error_show(1004,"参数platform_order不能为空");
  53. }
  54. $good_code =isset($this->post['good_code']) && $this->post['good_code'] !=="" ?trim($this->post['good_code']):"";
  55. if($good_code==''){
  56. return error_show(1004,"参数good_code不能为空");
  57. }
  58. $good=Db::name("good_zixun")->where(["spuCode"=>$good_code])->find();
  59. if($good==false){
  60. return error_show(1004,"未找到商品数据");
  61. }
  62. $goodinfo = Db::name("consult_bids")->where(["spuCode"=>$good['spuCode'],"is_del"=>0])->find();
  63. if($goodinfo==false){
  64. return error_show(1004,"未找到商品数据");
  65. }
  66. $good_num =isset($this->post['good_num']) && $this->post['good_num'] !=="" ?intval($this->post['good_num']):"";
  67. if($good_num===''){
  68. return error_show(1004,"参数good_num不能为空");
  69. }
  70. $good_num =isset($this->post['good_num']) && $this->post['good_num'] !=="" ?intval($this->post['good_num']):"";
  71. if($good_num===''){
  72. return error_show(1004,"参数good_num不能为空");
  73. }
  74. // $goodtype =isset($this->post['goodtype']) && $this->post['goodtype'] !=="" ?intval($this->post['goodtype']):"";
  75. // if($goodtype===''){
  76. // return error_show(1004,"参数goodtype不能为空");
  77. // }
  78. $arrtime = isset($this->post['arrtime'])&&$this->post['arrtime']!="" ?$this->post['arrtime']:"";
  79. if($arrtime==""){
  80. return error_show(1004,"参数arrtime不能为空");
  81. }
  82. $remark =isset($this->post['remark']) && $this->post['remark'] !=="" ?trim($this->post['remark']):"";
  83. if($sendtype==1){
  84. $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !=="" ?$this->post['addrlist']:"";
  85. if($addrlist=="" || empty($addrlist) ||!is_array($addrlist)){
  86. return error_show(1004,"参数addrlist不能为空");
  87. }
  88. }
  89. $apply_id =GetUserInfo($token);
  90. if(empty($apply_id)||$apply_id['code']!=0){
  91. return error_show(1002,"申请人数据不存在");
  92. }
  93. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  94. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  95. Db::startTrans();
  96. try {
  97. $supplier_temp_info=Db::name("supplier")
  98. ->field('id,person,personid')
  99. ->where(["code"=>$good['supplierNo']])
  100. ->findOrEmpty();
  101. $data=[
  102. "orderCode"=>$orderCode,
  103. "good_code"=>$good_code,
  104. "customer_code"=>$customer_code,
  105. "good_name"=>isset($good['good_name'])&&$good['good_name']!==''?$good['good_name']:'',
  106. "good_num"=>$good_num,
  107. "cat_id"=>$goodinfo['cat_id'],
  108. "apply_id"=>$rm,
  109. "apply_name"=>$ri,
  110. "origin_price"=>$goodinfo['total_fee'],
  111. "sale_price"=>$goodinfo['sale_price'],
  112. "post_fee"=>0,
  113. "status"=>0,
  114. "send_num"=>0,
  115. "wsend_num"=>$good_num,
  116. "send_status"=>1,
  117. "good_type"=>1,
  118. "send_type"=>$sendtype,
  119. "supplierNo"=>$supplierNo,
  120. "is_del"=>0,
  121. "zxNo"=>$zxinfoNo,
  122. "platform_order"=>$platform_order,
  123. "platform_id"=>$platform_id,
  124. "remark"=>$remark,
  125. "order_type"=>2,//非库存品
  126. "order_source"=>2,//咨询
  127. "addtime"=>date("Y-m-d H:i:s"),
  128. "updatetime"=>date("Y-m-d H:i:s"),
  129. 'total_price'=>round($goodinfo['sale_price']*$good_num,2),
  130. 'cgderid' => $supplier_temp_info['personid'],
  131. 'cgder' => $supplier_temp_info['person'],//采购员(采购单对应的供应商负责人)
  132. 'good_createrid' => $good['createrid'],
  133. 'good_creater' => $good['creater'],//商品创建人id
  134. ];
  135. $datainfo = Db::name('sale')->insert($data,true);
  136. $vat=[];
  137. if($datainfo>0){
  138. $standing_bood_data=[
  139. "orderCode"=>$orderCode,
  140. "sale_id"=>$datainfo,
  141. "customer_code"=>$customer_code,
  142. "supplierNo"=>$supplierNo,
  143. "order_type"=>$data['order_type'],
  144. "order_source"=>$data['order_source'],
  145. "spuCode"=>$good_code,
  146. 'updatetime'=>date("Y-m-d H:i:s"),
  147. ];
  148. $bol= $this->createCgd($data,$good,$goodinfo,$standing_bood_data);
  149. if($bol==false){
  150. Db::rollback();
  151. return error_show(1002,"咨询订单创建失败");
  152. }
  153. if($sendtype!=2){
  154. foreach ($addrlist as $value){
  155. $temp=[];
  156. $addrs=[];
  157. if($value['addr_code']!==''&&is_array($value['addr_code'])){
  158. $addrs['provice_code'] = $value['addr_code'][0];
  159. $addrs['city_code'] = $value['addr_code'][1];
  160. $addrs['area_code'] = $value['addr_code'][2];
  161. $addr = json_encode($addrs);
  162. }else{
  163. $addr = isset($value['addr_code'])?$value['addr_code']:'';
  164. }
  165. $temp['orderCode']=$orderCode;
  166. $temp['contactor']=$value['contactor'];
  167. $temp['mobile'] = $value['mobile'];
  168. $temp['addr'] = $value['addr'];
  169. $temp['addr_code']=$addr;
  170. $temp['customer_code'] =$customer_code;
  171. $temp['receipt_quantity']=$value['receipt_quantity'];
  172. $temp['post_fee'] =0;
  173. $temp['is_del'] =0;
  174. $temp['addtime'] =date("Y-m-d H:i:s");
  175. $temp['updatetime'] =date("Y-m-d H:i:s");
  176. $temp['arrive_time']=$value['arrive_time'];
  177. $vmp = Db::name('order_addr')->insert($temp,true);
  178. if($vmp>0){
  179. $outCode=makeNo("DF");
  180. $out=[
  181. "orderCode"=>$orderCode,
  182. "outCode"=>$outCode,
  183. "apply_id"=>$rm,
  184. "apply_name"=>$ri,
  185. "addrid"=>$vmp,
  186. "post_name"=>"",
  187. "post_code"=>"",
  188. "post_fee"=>0,
  189. "sendtime"=>date("Y-m-d H:i:s"),
  190. "send_num"=>$value['receipt_quantity'],
  191. "check_num"=>0,
  192. "error_num"=>0,
  193. "wsm_code"=>"",
  194. "order_type"=>2,
  195. "status"=>0,
  196. "addtime"=>date("Y-m-d H:i:s"),
  197. "updatetime"=>date("Y-m-d H:i:s")
  198. ];
  199. $ou =Db::name("order_out")->insertGetId($out);
  200. if($ou==false){
  201. Db::rollback();
  202. return error_show(1002,"咨询订单创建失败");
  203. }else{
  204. //修改状态,添加待办
  205. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  206. "order_code" => $outCode,//出库单号
  207. "status" => $out['status'],//这里的status是之前的值
  208. "action_remark" => '',//备注
  209. "action_type" => "create"//新建create,编辑edit,更改状态status
  210. ], "CKD", $out['status'], $out);
  211. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  212. "order_type" => 'CKD',
  213. "order_code" => $outCode,//出库单号
  214. "order_id" => $ou,
  215. "order_status" => $out['status'],
  216. 'before_status'=>0,
  217. 'holder_id' => $out['apply_id']
  218. ]);
  219. $standing_bood_data['outCode'][]=$outCode;
  220. }
  221. }else{
  222. Db::rollback();
  223. return error_show(1002,"咨询订单创建失败");
  224. }
  225. }
  226. }
  227. //修改状态,添加待办
  228. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  229. "order_code" => $orderCode,//销售单code
  230. "status" => 0,//这里的status是之前的值
  231. "action_remark" => '',//备注
  232. "action_type" => "create"//新建create,编辑edit,更改状态status
  233. ], "XSQRD", 0, $data);
  234. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  235. "order_type" => 'XSQRD',
  236. "order_code" => $orderCode,//销售单code
  237. "order_id" => $datainfo,
  238. "order_status" => 0,
  239. 'holder_id=' => $data['apply_id']
  240. ]);
  241. //根据zxinfoNo查找infoNo,找到则更新台账,否则就新增台账
  242. if (isset($standing_bood_data['outCode'])) $standing_bood_data['outCode'] = implode(',', $standing_bood_data['outCode']);
  243. $infoNo = Db::name('consult_bids')->where('bidNo', $zxinfoNo)->value('infoNo');
  244. if (empty($infoNo)) Db::name('standing_book')->insert(array_merge($standing_bood_data, ['addtime' => date('Y-m-d H:i:s'), 'standBookNo' => makeNo("IO")]));
  245. else Db::name('standing_book')->where('infoNo', $infoNo)->update($standing_bood_data);
  246. //将采购单数据塞入到队列中
  247. if ($this->cgd_data) {
  248. $push_data = json_encode([
  249. 'supplierNo' => $this->cgd_data['supplierNo'],
  250. 'type' => 1,//1销售订单(采销的采购单),2上线结果
  251. 'data' => [
  252. 'cgdNo' => $this->cgd_data['cgdNo'],
  253. 'spuCode' => $this->cgd_data['spuCode'],
  254. 'good_name' => $this->cgd_data['good_name'],
  255. 'good_num' => $this->cgd_data['good_num'],
  256. 'good_price' => $this->cgd_data['good_price'],
  257. 'total_fee' => $this->cgd_data['total_fee'],
  258. 'weight' => $this->cgd_data['weight'],
  259. 'addtime' => $this->cgd_data['addtime'],
  260. ],
  261. ], JSON_UNESCAPED_UNICODE);
  262. // Cache::store("redis")->handler()->lPush(Config::get('app.abutment_queue'), $push_data);
  263. }
  264. Db::commit();
  265. return error_show(0,"咨询订单创建成功");
  266. }
  267. Db::rollback();
  268. return error_show(1002,"咨询订单创建失败");
  269. }catch(\Exception $e){
  270. Db::rollback();
  271. return error_show(1005,$e->getMessage());
  272. }
  273. }
  274. public function createCgd($data,$good,$good_info,array &$standing_bood_data=[]){
  275. $cgdCode = makeNo("CG");
  276. $supplier=Db::name("supplier")->where(["code"=>$good['supplierNo'],"is_del"=>0])->find();
  277. if($supplier==false){
  278. return false;
  279. }
  280. $wsm= Db::name("warehouse_info")->where(["supplierNo"=>$good["supplierNo"],"companyNo"=>$data['supplierNo'],"is_del"=>0])->find();
  281. if($wsm==false){
  282. $wsm_code = makeNo("WSM");
  283. $inwsm=[
  284. "wsm_code"=>$wsm_code,
  285. "name"=>$supplier['name'],
  286. "wsm_type"=>2,
  287. "supplierNo"=>$supplier['code'],
  288. "addr"=>"",
  289. "addrs_code"=>"",
  290. "contactor"=>0,
  291. "contactor_name"=>0,
  292. "mobile"=>"",
  293. "position"=>"",
  294. "companyNo"=>$data['supplierNo'],
  295. "status"=>1,
  296. "is_del"=>0,
  297. "addtime"=>date("Y-m-d H:i:s"),
  298. "updatetime"=>date("Y-m-d H:i:s")
  299. ];
  300. $in = Db::name("warehouse_info")->insert($inwsm);
  301. if($in==false){
  302. return false;
  303. }
  304. }else{
  305. $wsm_code =$wsm['wsm_code'];
  306. }
  307. $cg =["cgdNo"=>$cgdCode,
  308. "bkcode"=>"",
  309. "wsm_code"=>$wsm_code,
  310. "cgder"=>$supplier['person'],
  311. "cgder_id"=>$supplier['personid'],
  312. "good_code"=>$good['spuCode'],
  313. "good_name"=>$good['good_name'],
  314. "good_num"=>$data['good_num'],
  315. "good_price"=>$good_info['sale_price'],
  316. "total_fee"=>$good_info['total_fee'],
  317. "pakge_fee"=>$good_info['pakge_fee'],
  318. "cert_fee"=>$good_info['cert_fee'],
  319. "open_fee"=>$good_info['open_fee'],
  320. "delivery_fee"=>$good_info['delivery_fee'],
  321. "mark_fee"=>$good_info['mark_fee'],
  322. "demo_fee"=>$good_info['demo_fee'],
  323. #"weight"=>$data['weight'],
  324. "diff_weight"=>"0",
  325. "diff_fee"=>"0",
  326. "gold_price"=>"0",
  327. "supplierNo"=>$good['supplierNo'],
  328. "supplier_name"=>$supplier['name'],
  329. "send_status"=>1,
  330. "send_num"=>"0",
  331. "wsend_num"=>$data['good_num'],
  332. "remark"=>"",
  333. "status"=>0,
  334. "lasttime"=>date("Y-m-d H:i:s"),
  335. "is_del"=>0,
  336. "order_type"=>$data['order_type'],
  337. "order_source"=>$data['order_source'],
  338. "good_type"=>$good['good_type'],
  339. "addtime"=>date("Y-m-d H:i:s"),
  340. "updatetime"=>date("Y-m-d H:i:s"),
  341. 'good_createrid' => $good['createrid'],
  342. 'good_creater' => $good['creater'],//商品创建人
  343. ];
  344. $up =Db::name("purchease_order")->insert($cg,true);
  345. if($up){
  346. //修改状态,添加待办
  347. ActionLog::logAdd($this->post['token'], [
  348. "order_code" => $cg['cgdNo'],//销售单code
  349. "status" => 0,//这里的status是之前的值
  350. "action_remark" => '',//备注
  351. "action_type" => "create"//新建create,编辑edit,更改状态status
  352. ], "CGD", $cg['status'], $cg);
  353. //当采购单是节点0待与供应商确认,推给供应商负责人
  354. ProcessOrder::AddProcess($this->post['token'], [
  355. "order_type" => 'CGD',
  356. "order_code" => $cg['cgdNo'],//销售单code
  357. "order_id" => $up,
  358. "order_status" => $cg['status'],
  359. 'holder_id'=>$supplier['personid'],
  360. 'wait_id'=>$supplier['personid'],
  361. 'wait_name'=>$supplier['person'],
  362. ]);
  363. $standing_bood_data['cgdNo']=$cgdCode;
  364. $this->cgd_data = $cg;
  365. return true;
  366. }else{
  367. return false;
  368. }
  369. }
  370. // public function edit(){
  371. // $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  372. //
  373. // $zxNo=isset($this->post['id']) && $this->post['id'] !=="" ?intval($this->post['id']):"";
  374. // if($zxNo==''){
  375. // return error_show(1004,"参数id不能为空");
  376. // }
  377. // $saleinfo = Db::name("sale")->where(["id"=>$zxNo,"is_del"=>0])->find();
  378. // if($saleinfo==false){
  379. // return error_show(1003,"未找到咨询单数据");
  380. // }
  381. // $zxinfo = Db::name("consult")->where(["zxNo"=>$saleinfo['zxNo'],"is_del"=>0])->find();
  382. // if($zxinfo==false){
  383. // return error_show(1004,"未找到咨询单数据");
  384. // }
  385. // $customer_code= $this->post['customer_code'] && $this->post['customer_code'] !=="" ? trim($this->post['customer_code']) :"";
  386. // if($customer_code==""){
  387. // return error_show(1002,"客户code不能为空");
  388. // }
  389. // $customer=Db::name("customer_info")->where(["companyNo"=>$customer_code])->find();
  390. // if($customer==false){
  391. // return error_show(1004,"未找到客户数据");
  392. // }
  393. // $supplierNo= $this->post['supplierNo'] && $this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']) :"";
  394. // if($supplierNo==""){
  395. // return error_show(1002,"参数supplierNo不能为空");
  396. // }
  397. // $supplier=Db::name("supplier")->where(["code"=>$supplierNo])->find();
  398. // if($supplier==false){
  399. // return error_show(1004,"未找到平台供应商数据");
  400. // }
  401. // $wsm=Db::name("warehouse_info")->where(["supplierNo"=>$zxinfo['gysNo'],"wsm_type"=>2])->find();
  402. // if(empty($wsm)){
  403. // return error_show(1004,"未找到供应商仓库数据");
  404. // }
  405. // $sendtype =isset($this->post['sendtype']) && $this->post['sendtype'] !=="" ?trim($this->post['sendtype']):"";
  406. // if($sendtype==''){
  407. // return error_show(1004,"参数sendtype不能为空");
  408. // }
  409. // $good_num =isset($this->post['good_num']) && $this->post['good_num'] !=="" ?intval($this->post['good_num']):"";
  410. // if($good_num===''){
  411. // return error_show(1004,"参数good_num不能为空");
  412. // }
  413. // $goodtype =isset($this->post['goodtype']) && $this->post['goodtype'] !=="" ?intval($this->post['goodtype']):"";
  414. // if($goodtype===''){
  415. // return error_show(1004,"参数goodtype不能为空");
  416. // }
  417. // $remark =isset($this->post['remark']) && $this->post['remark'] !=="" ?trim($this->post['remark']):"";
  418. // if($sendtype!=2){
  419. // $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !=="" ?$this->post['addrlist']:"";
  420. // if($addrlist=="" || empty($addrlist)){
  421. // return error_show(1004,"参数addrlist不能为空");
  422. // }
  423. // }
  424. // $apply_id =GetUserInfo($token);
  425. // if(empty($apply_id)||$apply_id['code']!=0){
  426. // return error_show(1002,"申请人数据不存在");
  427. // }
  428. // $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  429. // $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  430. // Db::startTrans();
  431. // try {
  432. // $data=[
  433. // "customer_code"=>$customer_code,
  434. // "good_num"=>$good_num,
  435. // "apply_id"=>$rm,
  436. // "apply_name"=>$ri,
  437. // "wsend_num"=>$good_num,
  438. // "good_type"=>$goodtype,
  439. // "send_type"=>$sendtype,
  440. // "supplierNo"=>$supplierNo,
  441. // "remark"=>$remark,
  442. // "updatetime"=>date("Y-m-d H:i:s"),
  443. // 'total_price'=>round($zxinfo['last_price']*$good_num,2),
  444. // ];
  445. // $datainfo = Db::name('sale')->where(["id"=>$zxNo])->save($data);
  446. // if($datainfo!=false){
  447. // $st = ["order_code"=>$saleinfo['orderCode'],"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  448. // ActionLog::logAdd($this->post['token'],$st,"ZXQRD",0,$data);
  449. // $order=["order_type"=>"ZXQRD","order_code"=>$saleinfo['orderCode'],"order_id"=>$saleinfo['id'],"order_status"=>$saleinfo['status']];
  450. // ProcessOrder::AddProcess($this->post['token'],$order);
  451. // $saleinfos=[
  452. // "wsm_code"=>$wsm['wsm_code'],
  453. // "num"=>$good_num,
  454. // "sale_price"=>$zxinfo['last_price'],
  455. // "total_price"=>round($zxinfo['last_price']*$good_num,2),
  456. // "updatetime"=>date("Y-m-d H:i:s"),
  457. // ];
  458. // $info = Db::name("sale_info")->where(["orderCode"=>$saleinfo['orderCode']])->save($saleinfos);
  459. // if($info==false){
  460. // Db::rollback();
  461. // return error_show(1002,"咨询订单创建失败");
  462. // }
  463. // if($sendtype!=2){
  464. // foreach ($addrlist as $value){
  465. // $temp=[];
  466. // $addrs=[];
  467. // if($value['addr_code']!==''&&is_array($value['addr_code'])){
  468. // $addrs['provice_code'] = $value['addr_code'][0];
  469. // $addrs['city_code'] = $value['addr_code'][1];
  470. // $addrs['area_code'] = $value['addr_code'][2];
  471. // $addr = json_encode($addrs);
  472. // }else{
  473. // $addr = isset($value['addr_code'])?$value['addr_code']:'';
  474. // }
  475. // isset($value["id"])&&$value["id"]!=""? $temp['id']=$value['id']:"";
  476. // $temp['contactor']=$value['contactor'];
  477. // $temp['mobile'] = $value['mobile'];
  478. // $temp['addr'] = $value['addr'];
  479. // $temp['addr_code']=$addr;
  480. // $temp['orderCode']=$saleinfo['orderCode'];
  481. // $temp['customer_code'] =$customer_code;
  482. // $temp['receipt_quantity']=$value['receipt_quantity'];
  483. // $temp['post_fee'] =0;
  484. // $temp['is_del'] =$value['is_del'];
  485. // $temp['updatetime'] =date("Y-m-d H:i:s");
  486. // isset($value["id"])&&$value["id"]!=""?"":$temp['addtime'] =date("Y-m-d H:i:s");
  487. // $temp['arrive_time']=$value['arrive_time'];
  488. // $vmp = Db::name('order_addr')->save($temp);
  489. // if(!$vmp){
  490. // Db::rollback();
  491. // return error_show(0,"咨询订单地址更新失败");
  492. // }
  493. // }
  494. // }else{
  495. // if($saleinfo['send_type']==1 && $sendtype==2){
  496. // $up=Db::name('order_addr')->where(["orderCode"=>$saleinfo['orderCode']])->save(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  497. // if(!$up){
  498. // Db::rollback();
  499. // return error_show(0,"咨询订单地址更新失败");
  500. // }
  501. // }
  502. // }
  503. // Db::commit();
  504. // return error_show(0,"咨询订单更新成功");
  505. // }
  506. // Db::rollback();
  507. // return error_show(1002,"咨询订单更新失败");
  508. // }catch(\Exception $e){
  509. // Db::rollback();
  510. // return error_show(1005,$e->getMessage());
  511. // }
  512. // }
  513. public function out(){
  514. $token= isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  515. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !=="" ? trim($this->post['outCode']) :"";
  516. if($outCode==""){
  517. return error_show(1002,"参数outCode不能为空");
  518. }
  519. $der = Db::name('order_out')->where(['outCode'=>$outCode])->find();
  520. if($der==""){
  521. return error_show(1002,"未找到出库单数据");
  522. }
  523. $order= Db::name('sale')->where(['orderCode'=>$der['orderCode']])->find();
  524. if(empty($order)){
  525. return error_show(1002,"未找到咨询订单信息");
  526. }
  527. $stock= Db::name("good_stock")->where(["spuCode"=>$order['good_code'],"wsm_code"=>$der['wsm_code']])
  528. ->find();
  529. if(empty($stock)){
  530. return error_show(1002,"未找到仓库库存信息");
  531. }
  532. if($der['send_num'] > $stock['wait_out_stock']){
  533. return error_show(1002,"仓库库存不足");
  534. }
  535. $post_name= isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
  536. if($post_name==""){
  537. return error_show(1002,"物流公司不能为空");
  538. }
  539. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
  540. if($post_code==""){
  541. return error_show(1002,"物流单号不能为空");
  542. }
  543. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? intval($this->post['post_fee']) :"";
  544. if($post_fee==""){
  545. return error_show(1002,"物流费不能为空");
  546. }
  547. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !=="" ? $this->post['sendtime']:date("Y-m-d H:i:s");
  548. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? $this->post['status'] :"1";
  549. Db::startTrans();
  550. try {
  551. $var = $der['status'];
  552. $der['post_name'] = $post_name;
  553. $der['post_code'] = $post_code;
  554. $der['post_fee'] = $post_fee;
  555. $der['sendtime'] = $sendtime;
  556. $der['status'] = 1;
  557. $der['updatetime'] = date("Y-m-d H:i:s");
  558. $datainfo = Db::name('order_out')->save($der);
  559. $st = ["order_code"=>$outCode,"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  560. ActionLog::logAdd($this->post['token'],$st,"resign_info",$der['status'] ,$st);
  561. if($datainfo) {
  562. $st = ["order_code"=>$der['outCode'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  563. ActionLog::logAdd($this->post['token'],$st,"CKD",1,$der);
  564. //是否库存品,待办数据推送到的人有区别,
  565. $ord = [];
  566. // if ($order['is_stock'] == 1) {
  567. // //库存品,推给库管和库管-张凯旋
  568. // $roleid = config('app.wsm_cgder_role');
  569. // $uids = Db::name('user_role')
  570. // ->where('is_del', 0)
  571. // ->whereIn('roleid', $roleid)
  572. // ->column('uid');
  573. // $ord = ["order_type" => "CKD", "order_code" => $der['outCode'], "order_id" => $der['id'], "order_status" => $der['status'], 'holder_id' => $der['apply_id'], 'handle_user_list' => implode(',', $uids)];
  574. // } elseif ($order['is_stock'] == 0 || $order['order_typee'] == 3) {
  575. //推给供应商负责人
  576. $supplier = Db::name('supplier')
  577. ->alias('a')
  578. ->field('a.id,a.person,a.personid')
  579. ->leftJoin('purchease_order b', 'b.is_del=0 AND b.supplierNo=a.code')
  580. ->leftJoin('order_num c', 'c.cgdNo=b.cgdNo')
  581. ->where('c.orderCode', $der['orderCode'])
  582. ->findOrEmpty();
  583. $ord = ["order_type" => "CKD", "order_code" => $der['outCode'], "order_id" => $der['id'], "order_status" => $der['status'], 'holder_id' => $der['apply_id'], 'wait_id' => $supplier['personid'], 'wait_name' => $supplier['person']];
  584. // }
  585. // if (empty($ord)) $ord = ["order_type" => "CKD", "order_code" => $der['outCode'], "order_id" => $der['id'], "order_status" => $der['status'], 'holder_id' => $der['apply_id']];
  586. ProcessOrder::AddProcess($this->post['token'], $ord);
  587. // $send =Db::name("order_out")->where(["wsm_code"=>$der['wsm_code'],'orderCode'=>$der['orderCode']])->sum("send_num");
  588. // $order['send_num']+= $der['send_num'];
  589. // $order['wsend_num']-=$der['send_num'] ;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  590. // $order['send_status']=$order['send_num']==0 ? 1 :($order['wsend_num']==0?3:2);
  591. // $order['status']=$order['send_num']==0 ? 2 :($order['wsend_num']==0?4:3);
  592. // $order['updatetime'] = date("Y-m-d H:i:s");
  593. // $si = Db::name('sale')->save($order);
  594. // if($si==false){
  595. // Db::rollback();
  596. // return error_show(1003,"更新失败");
  597. // }
  598. $str = Db::name('good_stock')->where(['wsm_code' => $der['wsm_code'],'spuCode'=>$order['good_code']])
  599. ->find();
  600. if ($str == false) {
  601. Db::rollback();
  602. return error_show(1002, "商品数据未找到");
  603. }
  604. if ($der['send_num'] > $str['wait_out_stock']) {
  605. Db::rollback();
  606. return error_show(1002, "超出库存数量");
  607. }
  608. $str['wait_out_stock'] -= $der['send_num'];
  609. $str['intra_stock'] += $der['send_num'];
  610. $str['updatetime'] = date("Y-m-d H:i:s");
  611. $stre = Db::name('good_stock')->save($str);
  612. if ($stre == false) {
  613. Db::rollback();
  614. return error_show(1002, "状态更新失败");
  615. }
  616. $good_data=[];
  617. //商品变动日志表,good_log_code字段存储销售订单编号
  618. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $str['id'], "type" => 1, 'stock' => $der['send_num'], "stock_name" => "intra_stock"];
  619. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $str['id'], "type" => 2, 'stock' => $der['send_num'], "stock_name" => "wait_out_stock"];
  620. GoodLog::LogAdd($this->post['token'], $good_data, 'CKD');
  621. Db::commit();
  622. return error_show(0,"创建成功");
  623. }else{
  624. Db::rollback();
  625. return error_show(1003,"创建失败");
  626. }
  627. }catch (\Exception $e) {
  628. Db::rollback();
  629. return error_show(1005, $e->getMessage());
  630. }
  631. }
  632. public function outaddr(){
  633. $token= isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  634. $outCode=makeNo("DF");
  635. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  636. if($orderCode==""){
  637. return error_show(1002,"参数orderCoder不能为空");
  638. }
  639. $der = Db::name('sale')->where(['orderCode'=>$orderCode])->find();
  640. if($der==""){
  641. return error_show(1002,"未找到出库订单单号");
  642. }
  643. if($der['order_type']!=2){
  644. return error_show(1002,"订单不是咨询单无法出库");
  645. }
  646. $wsm= Db::name('sale_info')->where(['orderCode'=>$der['orderCode']])->find();
  647. if(empty($wsm)){
  648. return error_show(1002,"未找到仓库信息");
  649. }
  650. $apply_id =GetUserInfo($token);
  651. if(empty($apply_id)||$apply_id['code']!=0){
  652. return error_show(1002,"申请人数据不存在");
  653. }
  654. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  655. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  656. if($der['send_type']==2){
  657. $contactor= isset($this->post['contactor']) && $this->post['contactor'] !=="" ? trim($this->post['contactor'])
  658. :"";
  659. if($contactor==""){
  660. return error_show(1002,"收货人不能为空");
  661. }
  662. $mobile = isset($this->post['mobile']) && $this->post['mobile'] !=="" ? trim($this->post['mobile'])
  663. :"";
  664. if($mobile==""){
  665. return error_show(1002,"收货人联系方式不能为空");
  666. }
  667. $addr = isset($this->post['addr']) && $this->post['addr'] !=="" ? trim($this->post['addr']) :"";
  668. if($addr==""){
  669. return error_show(1002,"参数addr不能为空");
  670. }
  671. $receipt_quantity= isset($this->post['receipt_quantity']) && $this->post['receipt_quantity'] !=="" ? intval
  672. ($this->post['receipt_quantity']) :"";
  673. if($receipt_quantity===""){
  674. return error_show(1002,"参数receipt_quantity不能为空");
  675. }
  676. $addrcode = isset($this->post['addr_code']) && $this->post['addr_code'] !=="" ? $this->post['addr_code'] :"";
  677. if($addrcode==""){
  678. return error_show(1002,"参数addr_code不能为空");
  679. }
  680. $addrs=[];
  681. if(is_array($addrcode)){
  682. $addrs['provice_code'] = $addrcode['addr_code'][0];
  683. $addrs['city_code'] = $addrcode['addr_code'][1];
  684. $addrs['area_code'] = $addrcode['addr_code'][2];
  685. $addrstr = json_encode($addrs);
  686. }else{
  687. $addrstr = $addrcode;
  688. }
  689. }else{
  690. $addrid = isset($this->post['addrid']) && $this->post['addrid'] !=="" ? intval($this->post['addrid']) :"";
  691. $dr = Db::name('order_addr')->where(['id'=>$addrid,"orderCode"=>$orderCode,'is_del'=>0])->find();
  692. if(empty($dr)){
  693. return error_show(1003,"收货人信息未找到");
  694. }
  695. $receipt_quantity = $dr['receipt_quantity'];
  696. }
  697. // if($send_num>$stock['usable_stock']){
  698. // return error_show(1002,"超出可发货数量");
  699. // }
  700. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !=="" ? $this->post['sendtime']:date("Y-m-d H:i:s");
  701. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? $this->post['status'] :"1";
  702. Db::startTrans();
  703. try {
  704. if($der['send_type']==2){
  705. $addr=[
  706. "orderCode"=>$orderCode,
  707. "addr"=>$addr,
  708. "addr_code"=>$addrstr,
  709. "contactor"=>$contactor,
  710. "mobile"=>$mobile,
  711. "customer_code"=>$der['customer_code'],
  712. "post_fee"=>0,
  713. "is_del"=>0,
  714. "receipt_quantity"=>$receipt_quantity,
  715. "addtime"=>date("Y-m-d H:i:s"),
  716. "updatetime"=>date("Y-m-d H:i:s"),
  717. "arrive_time"=>date("Y-m-d H:i:s"),
  718. ];
  719. $addrid = Db::name('order_addr')->insert($addr,true);
  720. if($addrid<=0){
  721. $st = ["order_code"=>$orderCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  722. ActionLog::logAdd($this->post['token'],$st,"zxd",0,$st);
  723. Db::rollback();
  724. return error_show(1003,"创建失败");
  725. }
  726. }
  727. $data=[
  728. "wsm_code"=>$wsm['wsm_code'],
  729. "orderCode"=>$orderCode,
  730. "outCode"=>$outCode,
  731. "order_type"=>$der['order_type'],
  732. "apply_id"=>$rm,
  733. "apply_name"=>$ri,
  734. "addrid"=>$addrid,
  735. "post_name"=>'',
  736. "post_code"=>'',
  737. "post_fee"=>'',
  738. "sendtime"=>$sendtime,
  739. "send_num"=>$receipt_quantity,
  740. "check_num"=>0,
  741. "error_num"=>0,
  742. "status"=>0,
  743. "addtime"=>date("Y-m-d H:i:s"),
  744. "updatetime"=>date("Y-m-d H:i:s")
  745. ];
  746. $datainfo = Db::name('order_out')->insert($data,true);
  747. if($datainfo<=0){
  748. Db::rollback();
  749. return error_show(1003,"创建失败");
  750. }
  751. $sio = ["order_code"=>$outCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  752. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri],$sio,"CKD",0,$data);
  753. $order=["order_type"=>"CKD","order_code"=>$data['outCode'],"order_id"=>$datainfo,"order_status"=>$data['status'],'before_status'=>0, 'holder_id' => $data['apply_id']];
  754. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri],$order);
  755. $item = $der['status'];
  756. $der['send_num']+= $receipt_quantity;
  757. $der['wsend_num']-=$receipt_quantity;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  758. $der['send_status']=$der['send_num']==0 ? 1 :($der['wsend_num']==0?3:2);
  759. $der['status']=$der['send_num']==0 ? 2 :($der['wsend_num']==0?4:3);
  760. $der['updatetime'] = date("Y-m-d H:i:s");
  761. $si = Db::name('sale')->save($der);
  762. if($si==false){
  763. Db::rollback();
  764. return error_show(1003,"创建失败");
  765. }else{
  766. //修改状态,添加待办
  767. ActionLog::logAdd($this->post['token'], [
  768. "order_code" => $der['orderCode'],//销售单code
  769. "status" => $item,//这里的status是之前的值
  770. "action_remark" => '',//备注
  771. "action_type" => "status"//新建create,编辑edit,更改状态status
  772. ], "XSQRD", $der['status'], $der);
  773. ProcessOrder::AddProcess($this->post['token'], [
  774. "order_type" => 'XSQRD',
  775. "order_code" => $der['orderCode'],//销售单code
  776. "order_id" => $der['id'],
  777. "order_status" => $der['status'],
  778. 'holder_id=' => $der['apply_id']
  779. ]);
  780. }
  781. $sio = ["order_code"=>$orderCode,"status"=>$item,"action_remark"=>'',"action_type"=>"edit"];
  782. ActionLog::logAdd($this->post['token'],$sio,"zxd",$der['status'],$der);
  783. $order=["order_type"=>"CKD","order_code"=>$orderCode,"order_id"=>$der['id'],"order_status"=>$der['status'], 'holder_id' => $data['apply_id']];
  784. ProcessOrder::AddProcess($this->post['token'],$order);
  785. Db::commit();
  786. return error_show(0,"创建成功");
  787. }catch (\Exception $e) {
  788. Db::rollback();
  789. return error_show(1005, $e->getMessage());
  790. }
  791. }
  792. }