Salezx.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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\Db;
  7. use app\admin\model\ActionLog;
  8. class Salezx extends \app\BaseController
  9. {
  10. public $post="";
  11. public function __construct(App $app)
  12. {
  13. parent::__construct($app);
  14. $this->post=$this->request->post();
  15. }
  16. public function create(){
  17. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  18. $orderCode=makeNo("CX");
  19. $zxNo=isset($this->post['zxNo']) && $this->post['zxNo'] !=="" ?trim($this->post['zxNo']):"";
  20. if($zxNo==''){
  21. return error_show(1004,"参数zxNo不能为空");
  22. }
  23. $zxinfo = Db::name("consult")->where(["zxNo"=>$zxNo,"is_del"=>0])->find();
  24. if($zxinfo==false){
  25. return error_show(1004,"未找到咨询单数据");
  26. }
  27. $customer_code= $this->post['customer_code'] && $this->post['customer_code'] !=="" ? trim($this->post['customer_code']) :"";
  28. if($customer_code==""){
  29. return error_show(1002,"客户code不能为空");
  30. }
  31. $customer=Db::name("customer_info")->where(["companyNo"=>$customer_code])->find();
  32. if($customer==false){
  33. return error_show(1004,"未找到客户数据");
  34. }
  35. $supplierNo= $this->post['supplierNo'] && $this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']) :"";
  36. if($supplierNo==""){
  37. return error_show(1002,"参数supplierNo不能为空");
  38. }
  39. $supplier=Db::name("supplier")->where(["code"=>$supplierNo])->find();
  40. if($supplier==false){
  41. return error_show(1004,"未找到平台供应商数据");
  42. }
  43. $wsm=Db::name("warehouse_info")->where(["supplierNo"=>$zxinfo['gysNo'],"wsm_type"=>2])->find();
  44. if(empty($wsm)){
  45. return error_show(1004,"未找到供应商仓库数据");
  46. }
  47. $sendtype =isset($this->post['sendtype']) && $this->post['sendtype'] !=="" ?trim($this->post['sendtype']):"";
  48. if($sendtype==''){
  49. return error_show(1004,"参数sendtype不能为空");
  50. }
  51. $good_num =isset($this->post['good_num']) && $this->post['good_num'] !=="" ?intval($this->post['good_num']):"";
  52. if($good_num===''){
  53. return error_show(1004,"参数good_num不能为空");
  54. }
  55. $goodtype =isset($this->post['goodtype']) && $this->post['goodtype'] !=="" ?intval($this->post['goodtype']):"";
  56. if($goodtype===''){
  57. return error_show(1004,"参数goodtype不能为空");
  58. }
  59. $remark =isset($this->post['remark']) && $this->post['remark'] !=="" ?trim($this->post['remark']):"";
  60. if($sendtype!=2){
  61. $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !=="" ?$this->post['addrlist']:"";
  62. if($addrlist=="" || empty($addrlist)){
  63. return error_show(1004,"参数addrlist不能为空");
  64. }
  65. }
  66. $apply_id =GetUserInfo($token);
  67. if(empty($apply_id)||$apply_id['code']!=0){
  68. return error_show(1002,"申请人数据不存在");
  69. }
  70. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  71. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  72. Db::startTrans();
  73. try {
  74. $data=[
  75. "orderCode"=>$orderCode,
  76. "good_code"=>isset($zxinfo['cpNo'])&&$zxinfo['cpNo']!==''?$zxinfo['cpNo']:'',
  77. "customer_code"=>$customer_code,
  78. "good_name"=>isset($zxinfo['cpName'])&&$zxinfo['cpName']!==''?$zxinfo['cpName']:'',
  79. "good_num"=>$good_num,
  80. "apply_id"=>$rm,
  81. "apply_name"=>$ri,
  82. "origin_price"=>$zxinfo['sale_fee'],
  83. "sale_price"=>$zxinfo['last_price'],
  84. "post_fee"=>$zxinfo['delivery_fee'],
  85. "status"=>0,
  86. "send_num"=>0,
  87. "wsend_num"=>$good_num,
  88. "send_status"=>1,
  89. "good_type"=>$goodtype,
  90. "send_type"=>$sendtype,
  91. "supplierNo"=>$supplierNo,
  92. "is_del"=>0,
  93. "zxNo"=>$zxNo,
  94. "remark"=>$remark,
  95. "order_type"=>2,
  96. "addtime"=>date("Y-m-d H:i:s"),
  97. "updatetime"=>date("Y-m-d H:i:s"),
  98. 'total_price'=>round($zxinfo['last_price']*$good_num,2),
  99. ];
  100. $datainfo = Db::name('sale')->insert($data,true);
  101. $vat=[];
  102. if($datainfo>0){
  103. $st = ["order_code"=>$orderCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  104. ActionLog::logAdd($this->post['token'],$st,"ZXQRD",0,$st);
  105. $order=["order_type"=>"ZXQRD","order_code"=>$orderCode,"order_id"=>$datainfo,"order_status"=>$data['status']];
  106. ProcessOrder::AddProcess($this->post['token'],$order);
  107. $addgood=$this->addGood($zxinfo);
  108. if($addgood==false){
  109. Db::rollback();
  110. return error_show(1002,"咨询订单创建商品失败");
  111. }
  112. $saleinfo=[
  113. "orderCode"=>$orderCode,
  114. "wsm_code"=>$wsm['wsm_code'],
  115. "num"=>$good_num,
  116. "sale_price"=>$zxinfo['last_price'],
  117. "total_price"=>round($zxinfo['last_price']*$good_num,2),
  118. "addtime"=>date("Y-m-d H:i:s"),
  119. "updatetime"=>date("Y-m-d H:i:s"),
  120. ];
  121. $info = Db::name("sale_info")->insert($saleinfo);
  122. if($info==false){
  123. Db::rollback();
  124. return error_show(1002,"咨询订单创建失败");
  125. }
  126. if($sendtype!=2){
  127. foreach ($addrlist as $value){
  128. $temp=[];
  129. $addrs=[];
  130. if($value['addr_code']!==''&&is_array($value['addr_code'])){
  131. $addrs['provice_code'] = $value['addr_code'][0];
  132. $addrs['city_code'] = $value['addr_code'][1];
  133. $addrs['area_code'] = $value['addr_code'][2];
  134. $addr = json_encode($addrs);
  135. }else{
  136. $addr = isset($value['addr_code'])?$value['addr_code']:'';
  137. }
  138. $temp['orderCode']=$orderCode;
  139. $temp['contactor']=$value['contactor'];
  140. $temp['mobile'] = $value['mobile'];
  141. $temp['addr'] = $value['addr'];
  142. $temp['addr_code']=$addr;
  143. $temp['customer_code'] =$customer_code;
  144. $temp['receipt_quantity']=$value['receipt_quantity'];
  145. $temp['post_fee'] =0;
  146. $temp['is_del'] =0;
  147. $temp['addtime'] =date("Y-m-d H:i:s");
  148. $temp['updatetime'] =date("Y-m-d H:i:s");
  149. $temp['arrive_time']=$value['arrive_time'];
  150. $vat[]=$temp;
  151. }
  152. $vmp = Db::name('order_addr')->insertAll($vat);
  153. if($vmp){
  154. Db::commit();
  155. return error_show(0,"咨询订单创建成功");
  156. }
  157. }else{
  158. Db::commit();
  159. return error_show(0,"咨询订单创建成功");
  160. }
  161. }
  162. Db::rollback();
  163. return error_show(1002,"咨询订单创建失败");
  164. }catch(\Exception $e){
  165. Db::rollback();
  166. return error_show(1005,$e->getMessage());
  167. }
  168. }
  169. /**咨询新建商品
  170. * @param $data
  171. * @return bool
  172. */
  173. public function addGood($data){
  174. $isgood = Db::name("good")->where(["good_code"=>"GD-".$data["cpNo"]])->find();
  175. if($isgood){
  176. return true;
  177. }
  178. $good=[
  179. "good_code"=>"GD-".$data["cpNo"],
  180. "item"=>'咨询订单',
  181. "good_name"=>$data["cpName"],
  182. "good_desc"=>$data["desc"],
  183. "brand"=>$data["brank"],
  184. "original_price"=>$data["sale_fee"],
  185. "cg_saler"=>$data["saler"],
  186. "good_type"=>$data["zxtype"],
  187. "unit"=>$data["unit"],
  188. "good_addr"=>$data["addr"],
  189. "gys_code"=>$data["gysNo"],
  190. "market_fee"=>$data["sale_fee"],
  191. "is_online"=>'产品上线',
  192. "sort_f"=>$data["cat_f"],
  193. "sort_s"=>$data["cat_s"],
  194. "cat_id"=>$data["cat_id"],
  195. "status"=>1,
  196. "is_del"=>0,
  197. "addtime"=>date("Y-m-d H:i:s"),
  198. "updatetime"=>date("Y-m-d H:i:s")
  199. ];
  200. $insert = Db::name("good")->insert($good);
  201. if($insert){
  202. $st = ["order_code"=>$data["cpNo"],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  203. ActionLog::logAdd($this->post['token'],$st,"resign_info",1,$st);
  204. $type=[
  205. "type_code"=>$data['cpNo'],
  206. "good_code"=>"GD-".$data["cpNo"],
  207. "color"=>$data["color"],
  208. "material"=>$data["material"],
  209. "attribute"=>$data["model"],
  210. "specs"=>$data["specs"],
  211. "is_diff"=>$data["is_diff"],
  212. "cert_fee"=>$data["cert_fee"],
  213. "packing_fee"=>$data["package_fee"],
  214. "mark_fee"=>$data["mark_fee"],
  215. "cost_fee"=>$data["bare_fee"],
  216. "logistics_fee"=>$data["delivery_fee"],
  217. "logistics"=>$data["delivery_day"],
  218. "min_num"=>$data["min_num"],
  219. "unit_price"=>$data["sale_price"],
  220. "status"=>1,
  221. "is_del"=>0,
  222. "addtime"=>date("Y-m-d H:i:s"),
  223. "updatetime"=>date("Y-m-d H:i:s"),
  224. ];
  225. $ert = Db::name("good_type")->insert($type);
  226. if($ert){
  227. $sti = ["order_code"=>$data["cpNo"],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  228. ActionLog::logAdd($this->post['token'],$sti,"resign_info",1,$sti);
  229. return true;
  230. }
  231. }
  232. return false;
  233. }
  234. public function edit(){
  235. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  236. $zxNo=isset($this->post['id']) && $this->post['id'] !=="" ?intval($this->post['id']):"";
  237. if($zxNo==''){
  238. return error_show(1004,"参数id不能为空");
  239. }
  240. $saleinfo = Db::name("sale")->where(["id"=>$zxNo,"is_del"=>0])->find();
  241. if($saleinfo==false){
  242. return error_show(1003,"未找到咨询单数据");
  243. }
  244. $zxinfo = Db::name("consult")->where(["zxNo"=>$saleinfo['zxNo'],"is_del"=>0])->find();
  245. if($zxinfo==false){
  246. return error_show(1004,"未找到咨询单数据");
  247. }
  248. $customer_code= $this->post['customer_code'] && $this->post['customer_code'] !=="" ? trim($this->post['customer_code']) :"";
  249. if($customer_code==""){
  250. return error_show(1002,"客户code不能为空");
  251. }
  252. $customer=Db::name("customer_info")->where(["companyNo"=>$customer_code])->find();
  253. if($customer==false){
  254. return error_show(1004,"未找到客户数据");
  255. }
  256. $supplierNo= $this->post['supplierNo'] && $this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']) :"";
  257. if($supplierNo==""){
  258. return error_show(1002,"参数supplierNo不能为空");
  259. }
  260. $supplier=Db::name("supplier")->where(["code"=>$supplierNo])->find();
  261. if($supplier==false){
  262. return error_show(1004,"未找到平台供应商数据");
  263. }
  264. $wsm=Db::name("warehouse_info")->where(["supplierNo"=>$zxinfo['gysNo'],"wsm_type"=>2])->find();
  265. if(empty($wsm)){
  266. return error_show(1004,"未找到供应商仓库数据");
  267. }
  268. $sendtype =isset($this->post['sendtype']) && $this->post['sendtype'] !=="" ?trim($this->post['sendtype']):"";
  269. if($sendtype==''){
  270. return error_show(1004,"参数sendtype不能为空");
  271. }
  272. $good_num =isset($this->post['good_num']) && $this->post['good_num'] !=="" ?intval($this->post['good_num']):"";
  273. if($good_num===''){
  274. return error_show(1004,"参数good_num不能为空");
  275. }
  276. $goodtype =isset($this->post['goodtype']) && $this->post['goodtype'] !=="" ?intval($this->post['goodtype']):"";
  277. if($goodtype===''){
  278. return error_show(1004,"参数goodtype不能为空");
  279. }
  280. $remark =isset($this->post['remark']) && $this->post['remark'] !=="" ?trim($this->post['remark']):"";
  281. if($sendtype!=2){
  282. $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !=="" ?$this->post['addrlist']:"";
  283. if($addrlist=="" || empty($addrlist)){
  284. return error_show(1004,"参数addrlist不能为空");
  285. }
  286. }
  287. $apply_id =GetUserInfo($token);
  288. if(empty($apply_id)||$apply_id['code']!=0){
  289. return error_show(1002,"申请人数据不存在");
  290. }
  291. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  292. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  293. Db::startTrans();
  294. try {
  295. $data=[
  296. "customer_code"=>$customer_code,
  297. "good_num"=>$good_num,
  298. "apply_id"=>$rm,
  299. "apply_name"=>$ri,
  300. "wsend_num"=>$good_num,
  301. "good_type"=>$goodtype,
  302. "send_type"=>$sendtype,
  303. "supplierNo"=>$supplierNo,
  304. "remark"=>$remark,
  305. "updatetime"=>date("Y-m-d H:i:s"),
  306. 'total_price'=>round($zxinfo['last_price']*$good_num,2),
  307. ];
  308. $datainfo = Db::name('sale')->where(["id"=>$zxNo])->save($data);
  309. if($datainfo!=false){
  310. $st = ["order_code"=>$saleinfo['orderCode'],"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
  311. ActionLog::logAdd($this->post['token'],$st,"ZXQRD",0,$data);
  312. $order=["order_type"=>"ZXQRD","order_code"=>$saleinfo['orderCode'],"order_id"=>$saleinfo['id'],"order_status"=>$saleinfo['status']];
  313. ProcessOrder::AddProcess($this->post['token'],$order);
  314. $saleinfos=[
  315. "wsm_code"=>$wsm['wsm_code'],
  316. "num"=>$good_num,
  317. "sale_price"=>$zxinfo['last_price'],
  318. "total_price"=>round($zxinfo['last_price']*$good_num,2),
  319. "updatetime"=>date("Y-m-d H:i:s"),
  320. ];
  321. $info = Db::name("sale_info")->where(["orderCode"=>$saleinfo['orderCode']])->save($saleinfos);
  322. if($info==false){
  323. Db::rollback();
  324. return error_show(1002,"咨询订单创建失败");
  325. }
  326. if($sendtype!=2){
  327. foreach ($addrlist as $value){
  328. $temp=[];
  329. $addrs=[];
  330. if($value['addr_code']!==''&&is_array($value['addr_code'])){
  331. $addrs['provice_code'] = $value['addr_code'][0];
  332. $addrs['city_code'] = $value['addr_code'][1];
  333. $addrs['area_code'] = $value['addr_code'][2];
  334. $addr = json_encode($addrs);
  335. }else{
  336. $addr = isset($value['addr_code'])?$value['addr_code']:'';
  337. }
  338. isset($value["id"])&&$value["id"]!=""? $temp['id']=$value['id']:"";
  339. $temp['contactor']=$value['contactor'];
  340. $temp['mobile'] = $value['mobile'];
  341. $temp['addr'] = $value['addr'];
  342. $temp['addr_code']=$addr;
  343. $temp['orderCode']=$saleinfo['orderCode'];
  344. $temp['customer_code'] =$customer_code;
  345. $temp['receipt_quantity']=$value['receipt_quantity'];
  346. $temp['post_fee'] =0;
  347. $temp['is_del'] =$value['is_del'];
  348. $temp['updatetime'] =date("Y-m-d H:i:s");
  349. isset($value["id"])&&$value["id"]!=""?"":$temp['addtime'] =date("Y-m-d H:i:s");
  350. $temp['arrive_time']=$value['arrive_time'];
  351. $vmp = Db::name('order_addr')->save($temp);
  352. if(!$vmp){
  353. Db::rollback();
  354. return error_show(0,"咨询订单地址更新失败");
  355. }
  356. }
  357. }else{
  358. if($saleinfo['send_type']==1 && $sendtype==2){
  359. $up=Db::name('order_addr')->where(["orderCode"=>$saleinfo['orderCode']])->save(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  360. if(!$up){
  361. Db::rollback();
  362. return error_show(0,"咨询订单地址更新失败");
  363. }
  364. }
  365. }
  366. Db::commit();
  367. return error_show(0,"咨询订单更新成功");
  368. }
  369. Db::rollback();
  370. return error_show(1002,"咨询订单更新失败");
  371. }catch(\Exception $e){
  372. Db::rollback();
  373. return error_show(1005,$e->getMessage());
  374. }
  375. }
  376. public function out(){
  377. $token= isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  378. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !=="" ? trim($this->post['outCode']) :"";
  379. if($outCode==""){
  380. return error_show(1002,"参数outCode不能为空");
  381. }
  382. $der = Db::name('order_out')->where(['outCode'=>$outCode])->find();
  383. if($der==""){
  384. return error_show(1002,"未找到出库单数据");
  385. }
  386. $order= Db::name('sale')->where(['orderCode'=>$der['orderCode']])->find();
  387. if(empty($order)){
  388. return error_show(1002,"未找到咨询订单信息");
  389. }
  390. $stock= Db::name("good_stock")->where(["good_type_code"=>$order['good_code'],"wsm_code"=>$der['wsm_code']])
  391. ->find();
  392. // echo Db::name("good_stock")->getLastSql();
  393. if(empty($stock)){
  394. return error_show(1002,"未找到仓库库存信息");
  395. }
  396. if($der['send_num'] > $stock['wait_out_stock']){
  397. return error_show(1002,"仓库库存不足");
  398. }
  399. $post_name= isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
  400. if($post_name==""){
  401. return error_show(1002,"物流公司不能为空");
  402. }
  403. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
  404. if($post_code==""){
  405. return error_show(1002,"物流单号不能为空");
  406. }
  407. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? intval($this->post['post_fee']) :"";
  408. if($post_fee==""){
  409. return error_show(1002,"物流费不能为空");
  410. }
  411. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !=="" ? $this->post['sendtime']:date("Y-m-d H:i:s");
  412. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? $this->post['status'] :"1";
  413. Db::startTrans();
  414. try {
  415. $var = $der['status'];
  416. $der['post_name'] = $post_name;
  417. $der['post_code'] = $post_code;
  418. $der['post_fee'] = $post_fee;
  419. $der['sendtime'] = $sendtime;
  420. $der['status'] = 1;
  421. $der['updatetime'] = date("Y-m-d H:i:s");
  422. $datainfo = Db::name('order_out')->save($der);
  423. $st = ["order_code"=>$outCode,"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  424. ActionLog::logAdd($this->post['token'],$st,"resign_info",$der['status'] ,$st);
  425. if($datainfo) {
  426. $st = ["order_code"=>$der['outCode'],"status"=>$var,"action_remark"=>'',"action_type"=>"edit"];
  427. ActionLog::logAdd($this->post['token'],$st,"CKD",1,$der);
  428. $ord=["order_type"=>"CKD","order_code"=>$der['outCode'],"order_id"=>$der['id'],
  429. "order_status"=>$der['status']];
  430. ProcessOrder::AddProcess($this->post['token'],$ord);
  431. // $send =Db::name("order_out")->where(["wsm_code"=>$der['wsm_code'],'orderCode'=>$der['orderCode']])->sum("send_num");
  432. // $order['send_num']+= $der['send_num'];
  433. // $order['wsend_num']-=$der['send_num'] ;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  434. // $order['send_status']=$order['send_num']==0 ? 1 :($order['wsend_num']==0?3:2);
  435. // $order['status']=$order['send_num']==0 ? 2 :($order['wsend_num']==0?4:3);
  436. // $order['updatetime'] = date("Y-m-d H:i:s");
  437. // $si = Db::name('sale')->save($order);
  438. // if($si==false){
  439. // Db::rollback();
  440. // return error_show(1003,"更新失败");
  441. // }
  442. $str = Db::name('good_stock')->where(['wsm_code' => $der['wsm_code'],'good_type_code'=>$order['good_code']])->find();
  443. if ($str == false) {
  444. Db::rollback();
  445. return error_show(1002, "商品数据未找到");
  446. }
  447. if ($der['send_num'] > $str['wait_out_stock']) {
  448. Db::rollback();
  449. return error_show(1002, "超出库存数量");
  450. }
  451. $str['wait_out_stock'] -= $der['send_num'];
  452. $str['intra_stock'] += $der['send_num'];
  453. $str['updatetime'] = date("Y-m-d H:i:s");
  454. $stre = Db::name('good_stock')->save($str);
  455. if ($stre == false) {
  456. Db::rollback();
  457. return error_show(1002, "状态更新失败");
  458. }
  459. $good_data[]=["stock_id"=>$str['id'],"type"=>1,'stock'=>$der['send_num'],"stock_name"=>"intra_stock"];
  460. $good_data[]= ["stock_id"=>$str['id'],"type"=>2,'stock'=>$der['send_num'],"stock_name"=>"wait_out_stock"];
  461. GoodLog::LogAdd($this->post['token'],$good_data,'CKD');
  462. Db::commit();
  463. return error_show(0,"创建成功");
  464. }else{
  465. Db::rollback();
  466. return error_show(1003,"创建失败");
  467. }
  468. }catch (\Exception $e) {
  469. Db::rollback();
  470. return error_show(1005, $e->getMessage());
  471. }
  472. }
  473. public function outaddr(){
  474. $token= isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  475. $outCode=makeNo("CK");
  476. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  477. if($orderCode==""){
  478. return error_show(1002,"参数orderCoder不能为空");
  479. }
  480. $der = Db::name('sale')->where(['orderCode'=>$orderCode])->find();
  481. if($der==""){
  482. return error_show(1002,"未找到出库订单单号");
  483. }
  484. if($der['order_type']!=2){
  485. return error_show(1002,"订单不是咨询单无法出库");
  486. }
  487. $wsm= Db::name('sale_info')->where(['orderCode'=>$der['orderCode']])->find();
  488. if(empty($wsm)){
  489. return error_show(1002,"未找到仓库信息");
  490. }
  491. $apply_id =GetUserInfo($token);
  492. if(empty($apply_id)||$apply_id['code']!=0){
  493. return error_show(1002,"申请人数据不存在");
  494. }
  495. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  496. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  497. if($der['send_type']==2){
  498. $contactor= isset($this->post['contactor']) && $this->post['contactor'] !=="" ? trim($this->post['contactor'])
  499. :"";
  500. if($contactor==""){
  501. return error_show(1002,"收货人不能为空");
  502. }
  503. $mobile = isset($this->post['mobile']) && $this->post['mobile'] !=="" ? trim($this->post['mobile'])
  504. :"";
  505. if($mobile==""){
  506. return error_show(1002,"收货人联系方式不能为空");
  507. }
  508. $addr = isset($this->post['addr']) && $this->post['addr'] !=="" ? trim($this->post['addr']) :"";
  509. if($addr==""){
  510. return error_show(1002,"参数addr不能为空");
  511. }
  512. $receipt_quantity= isset($this->post['receipt_quantity']) && $this->post['receipt_quantity'] !=="" ? intval
  513. ($this->post['receipt_quantity']) :"";
  514. if($receipt_quantity===""){
  515. return error_show(1002,"参数receipt_quantity不能为空");
  516. }
  517. $addrcode = isset($this->post['addr_code']) && $this->post['addr_code'] !=="" ? $this->post['addr_code'] :"";
  518. if($addrcode==""){
  519. return error_show(1002,"参数addr_code不能为空");
  520. }
  521. $addrs=[];
  522. if(is_array($addrcode)){
  523. $addrs['provice_code'] = $addrcode['addr_code'][0];
  524. $addrs['city_code'] = $addrcode['addr_code'][1];
  525. $addrs['area_code'] = $addrcode['addr_code'][2];
  526. $addrstr = json_encode($addrs);
  527. }else{
  528. $addrstr = $addrcode;
  529. }
  530. }else{
  531. $addrid = isset($this->post['addrid']) && $this->post['addrid'] !=="" ? intval($this->post['addrid']) :"";
  532. $dr = Db::name('order_addr')->where(['id'=>$addrid,"orderCode"=>$orderCode,'is_del'=>0])->find();
  533. if(empty($dr)){
  534. return error_show(1003,"收货人信息未找到");
  535. }
  536. $receipt_quantity = $dr['receipt_quantity'];
  537. }
  538. // if($send_num>$stock['usable_stock']){
  539. // return error_show(1002,"超出可发货数量");
  540. // }
  541. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !=="" ? $this->post['sendtime']:date("Y-m-d H:i:s");
  542. // $status = isset($this->post['status']) && $this->post['status'] !=="" ? $this->post['status'] :"1";
  543. Db::startTrans();
  544. try {
  545. if($der['send_type']==2){
  546. $addr=[
  547. "orderCode"=>$orderCode,
  548. "addr"=>$addr,
  549. "addr_code"=>$addrstr,
  550. "contactor"=>$contactor,
  551. "mobile"=>$mobile,
  552. "customer_code"=>$der['customer_code'],
  553. "post_fee"=>0,
  554. "is_del"=>0,
  555. "receipt_quantity"=>$receipt_quantity,
  556. "addtime"=>date("Y-m-d H:i:s"),
  557. "updatetime"=>date("Y-m-d H:i:s"),
  558. "arrive_time"=>date("Y-m-d H:i:s"),
  559. ];
  560. $addrid = Db::name('order_addr')->insert($addr,true);
  561. if($addrid<=0){
  562. $st = ["order_code"=>$orderCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  563. ActionLog::logAdd($this->post['token'],$st,"zxd",0,$st);
  564. Db::rollback();
  565. return error_show(1003,"创建失败");
  566. }
  567. }
  568. $data=[
  569. "wsm_code"=>$wsm['wsm_code'],
  570. "orderCode"=>$orderCode,
  571. "outCode"=>$outCode,
  572. "order_type"=>$der['order_type'],
  573. "apply_id"=>$rm,
  574. "apply_name"=>$ri,
  575. "addrid"=>$addrid,
  576. "post_name"=>'',
  577. "post_code"=>'',
  578. "post_fee"=>'',
  579. "sendtime"=>$sendtime,
  580. "send_num"=>$receipt_quantity,
  581. "check_num"=>0,
  582. "error_num"=>0,
  583. "status"=>0,
  584. "addtime"=>date("Y-m-d H:i:s"),
  585. "updatetime"=>date("Y-m-d H:i:s")
  586. ];
  587. $datainfo = Db::name('order_out')->insert($data,true);
  588. if($datainfo<=0){
  589. Db::rollback();
  590. return error_show(1003,"创建失败");
  591. }
  592. $sio = ["order_code"=>$outCode,"status"=>0,"action_remark"=>'',"action_type"=>"create"];
  593. ActionLog::logAdd($this->post['token'],$sio,"CKD",0,$data);
  594. $order=["order_type"=>"CKD","order_code"=>$data['outCode'],"order_id"=>$datainfo,"order_status"=>$data['status']];
  595. ProcessOrder::AddProcess($this->post['token'],$order);
  596. $item = $der['status'];
  597. $der['send_num']+= $receipt_quantity;
  598. $der['wsend_num']-=$receipt_quantity;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  599. $der['send_status']=$der['send_num']==0 ? 1 :($der['wsend_num']==0?3:2);
  600. $der['status']=$der['send_num']==0 ? 2 :($der['wsend_num']==0?4:3);
  601. $der['updatetime'] = date("Y-m-d H:i:s");
  602. $si = Db::name('sale')->save($der);
  603. if($si==false){
  604. Db::rollback();
  605. return error_show(1003,"创建失败");
  606. }
  607. $sio = ["order_code"=>$orderCode,"status"=>$item,"action_remark"=>'',"action_type"=>"edit"];
  608. ActionLog::logAdd($this->post['token'],$sio,"zxd",$der['status'],$der);
  609. $order=["order_type"=>"CKD","order_code"=>$orderCode,"order_id"=>$der['id'],"order_status"=>$der['status']];
  610. ProcessOrder::AddProcess($this->post['token'],$order);
  611. Db::commit();
  612. return error_show(0,"创建成功");
  613. }catch (\Exception $e) {
  614. Db::rollback();
  615. return error_show(1005, $e->getMessage());
  616. }
  617. }
  618. }