Sale.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. class Sale extends BaseController
  7. {
  8. public $post="";
  9. public function __construct(App $app)
  10. {
  11. parent::__construct($app);
  12. $this->post=$this->request->post();
  13. }
  14. public function create(){
  15. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  16. $orderCode=makeNo("CX");
  17. $good_code = $this->post['good_code'] && $this->post['good_code'] !=="" ? trim($this->post['good_code']) :"";
  18. if($good_code==""){
  19. return error_show(1002,"商品code不能为空");
  20. }
  21. $ct = Db::name('good')->alias('a')->join('good_type b','b.good_code=a.good_code','left')
  22. ->where(['b.type_code'=>$good_code])->find();
  23. if($ct==""){
  24. return error_show(1002,"未找到商品数据");
  25. }
  26. $customer_code= $this->post['customer_code'] && $this->post['customer_code'] !=="" ? trim($this->post['customer_code']) :"";
  27. if($customer_code==""){
  28. return error_show(1002,"客户code不能为空");
  29. }
  30. $customer=Db::name("customer_info")->where(["companyNo"=>$customer_code])->find();
  31. if($customer==false){
  32. return error_show(1004,"未找到客户数据");
  33. }
  34. $supplierNo= $this->post['supplierNo'] && $this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']) :"";
  35. if($supplierNo==""){
  36. return error_show(1002,"参数supplierNo不能为空");
  37. }
  38. $supplier=Db::name("supplier")->where(["code"=>$supplierNo])->find();
  39. if($supplier==false){
  40. return error_show(1004,"未找到平台供应商数据");
  41. }
  42. $apply_id =GetUserInfo($token);
  43. if(empty($apply_id)||$apply_id['code']!=0){
  44. return error_show(1002,"申请人数据不存在");
  45. }
  46. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  47. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  48. // $good_name=isset($this->post['good_name']) && $this->post['good_name'] !=="" ? trim($this->post['good_name']) :"";
  49. //$good_num=isset($this->post['good_num']) && $this->post['good_num'] !=="" ? intval($this->post['good_num']) :"";
  50. // if($good_num==""){
  51. // return error_show(1002,"参数good_num不能为空");
  52. //}
  53. // $origin_price = isset($this->post['origin_price']) && $this->post['origin_price'] !=="" ? intval($this->post['origin_price']) :"";
  54. //$sale_price=isset($this->post['sale_price']) && $this->post['sale_price'] !=="" ? trim($this->post['sale_price']) :"";
  55. // $total_price=isset($this->post['total_price']) && $this->post['total_price'] !=="" ? trim($this->post['total_price']) :"";
  56. $post_fee=isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? trim($this->post['post_fee']) :"";
  57. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  58. // $send_num = isset($this->post['send_num']) && $this->post['send_num'] !=="" ? intval($this->post['send_num']) :"";
  59. // $wsend_num = isset($this->post['wsend_num']) && $this->post['wsend_num'] !=="" ? intval($this->post['wsend_num']) :"";
  60. // $send_status = isset($this->post['send_status']) && $this->post['send_status'] !=="" ? intval($this->post['send_status']) :"";
  61. $va= isset($this->post['order_addr']) && $this->post['order_addr'] !=="" ? $this->post['order_addr']:"";
  62. if($va==""){
  63. return error_show(1002,"参数order_addr不能为空");
  64. }
  65. $vi= isset($this->post['good_stock']) && $this->post['good_stock'] !=="" ? $this->post['good_stock']:"";
  66. if($vi==""){
  67. return error_show(1002,"参数good_stock不能为空");
  68. }
  69. $good_num = intval(array_sum(array_column($vi,"num")));
  70. Db::startTrans();
  71. try {
  72. $data=[
  73. "orderCode"=>$orderCode,
  74. "good_code"=>$good_code,
  75. "customer_code"=>$customer_code,
  76. "good_name"=>$ct['good_name'],
  77. "good_num"=>$good_num,
  78. "apply_id"=>$rm,
  79. "apply_name"=>$ri,
  80. "origin_price"=>$ct['original_price'],
  81. "sale_price"=>$ct['original_price'],
  82. "post_fee"=>$post_fee,
  83. "status"=>$status,
  84. "supplierNo"=>$supplierNo,
  85. "send_num"=>0,
  86. "wsend_num"=>$good_num,
  87. "send_status"=>1,
  88. "is_del"=>0,
  89. "addtime"=>date("Y-m-d H:i:s"),
  90. "updatetime"=>date("Y-m-d H:i:s"),
  91. 'total_price'=>$ct['original_price']*$good_num,
  92. ];
  93. $datainfo = Db::name('sale')->insert($data,true);
  94. $vat=[];
  95. $inn=[];
  96. if($datainfo>0){
  97. foreach ($vi as $ion){
  98. $iten=[];
  99. $iten['orderCode']=$orderCode;
  100. $iten['wsm_code']=$ion['wsm_code'];
  101. $iten['stock_id']="";
  102. $iten['num']=$ion['num'];
  103. $iten['status']=0;
  104. $iten['sale_price']=$ct['original_price'];
  105. $iten['addtime'] =date("Y-m-d H:i:s");
  106. $iten['updatetime'] =date("Y-m-d H:i:s");
  107. $inn[]=$iten;
  108. }
  109. // if($vi['usable_stock']<$iten['num']){
  110. // Db::rollback();
  111. // return error_show(1002,"超出库存数量");
  112. // }
  113. $vp= Db::name('sale_info')->insertAll($inn);
  114. if($vp==""){
  115. Db::rollback();
  116. return error_show(1002,"添加失败");
  117. }
  118. foreach ($va as $value){
  119. $temp=[];
  120. $addrs=[];
  121. if($value['addr_code']!==''&&is_array($value['addr_code'])){
  122. $addrs['provice_code'] = $value['addr_code'][0];
  123. $addrs['city_code'] = $value['addr_code'][1];
  124. $addrs['area_code'] = $value['addr_code'][2];
  125. $addr = json_encode($addrs);
  126. }else{
  127. $addr = isset($value['addr_code'])?$value['addr_code']:'';
  128. }
  129. $temp['orderCode']=$orderCode;
  130. $temp['contactor']=$value['contactor'];
  131. $temp['mobile'] = $value['mobile'];
  132. $temp['addr'] = $value['addr'];
  133. $temp['addr_code']=$addr;
  134. $temp['customer_code'] =$customer_code;
  135. $temp['receipt_quantity']=$value['receipt_quantity'];
  136. $temp['post_fee'] =0;
  137. $temp['is_del'] =0;
  138. $temp['addtime'] =date("Y-m-d H:i:s");
  139. $temp['updatetime'] =date("Y-m-d H:i:s");
  140. $temp['arrive_time']=date("Y-m-d H:i:s");
  141. $vat[]=$temp;
  142. }
  143. $vmp = Db::name('order_addr')->insertAll($vat);
  144. if($vmp){
  145. Db::commit();
  146. return error_show(0,"销售订单创建成功");
  147. }
  148. }
  149. Db::rollback();
  150. return error_show(1002,"销售订单创建失败");
  151. }catch(\Exception $e){
  152. Db::rollback();
  153. return error_show(1005,$e->getMessage());
  154. }
  155. }
  156. public function list(){
  157. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  158. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']) :"10";
  159. $where[] = ['is_del',"=",0];
  160. $orderCode=isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']):"";
  161. if($orderCode !==""){
  162. $where[]=['orderCode',"=",$orderCode];
  163. }
  164. $apply_name=isset($this->post['apply_name']) && $this->post['apply_name'] !=="" ? trim($this->post['apply_name'])
  165. :"";
  166. if($apply_name !==""){
  167. $where[]=['apply_name',"like","%$apply_name%"];
  168. }
  169. $good_name=isset($this->post['good_name']) && $this->post['good_name'] !=="" ? trim($this->post['good_name'])
  170. :"";
  171. if($good_name !==""){
  172. $where[]=['good_name',"like","%$good_name%"];
  173. }
  174. $good_code=isset($this->post['good_code']) && $this->post['good_code'] !=="" ? trim($this->post['good_code']):"";
  175. if($good_code !==""){
  176. $where[]=['good_code',"like","%$good_code%"];
  177. }
  178. $customer_code=isset($this->post['customer_code']) && $this->post['customer_code'] !=="" ? trim($this->post['customer_code']):"";
  179. if($customer_code !==""){
  180. $where[]=['customer_code',"like","%$customer_code%"];
  181. }
  182. $supplierNo=isset($this->post['supplierNo']) && $this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']):"";
  183. if($supplierNo !==""){
  184. $where[]=['supplierNo',"like","%$supplierNo%"];
  185. }
  186. $ordertype=isset($this->post['order_type']) && $this->post['order_type'] !=="" ? intval($this->post['order_type'])
  187. :"";
  188. if($ordertype !==""){
  189. $where[]=['order_type',"=",$ordertype];
  190. }
  191. $status=isset($this->post['status']) && $this->post['status'] !=="" ? trim($this->post['status']):"";
  192. if($status !==""){
  193. $where[]=['status',"=",$status];
  194. }
  195. $start = isset($this->post['start']) && $this->post['start'] !=="" ? $this->post['start'] :"";
  196. if($start !=""){
  197. $where[]= ["addtime",'>=',$start];
  198. }
  199. $end = isset($this->post['end']) && $this->post['end'] !=="" ? $this->post['end'] :"";
  200. if($end !=""){
  201. $where[]= ["addtime",'<=',$end];
  202. }
  203. $count = Db::name('sale')->where($where)->count();
  204. $total = ceil($count/$size);
  205. $page = $page >= $total ? $total : $page;
  206. $list= Db::name('sale')->where($where)->order("addtime desc")->page($page,$size)->select();
  207. $data=[];
  208. foreach ($list as $value){
  209. $var = Db::name('good_type')->alias('b')->join("good a","a.good_code=b.good_code","left")
  210. ->where(['b.type_code'=>$value['good_code'],'a.is_del'=>0])->field('a.good_code,a.good_name,a.cat_id')->find();;
  211. $value['can']= isset($var['cat_id']) && $var['cat_id'] !=0 ? made($var['cat_id']):[];
  212. $value['supplierName']='';
  213. if($value['supplierNo']!=""){
  214. $supplier=Db::name("supplier")->where(["code"=>$value['supplierNo']])->find();
  215. $value['supplierName']=isset($supplier['name']) ? $supplier['name']:"";
  216. }
  217. $value['customerName']='';
  218. if($value['customer_code']!=""){
  219. $supplier=Db::name("customer_info")->where(["companyNo"=>$value['customer_code']])->find();
  220. $value['customerName']=isset($supplier['companyName']) ? $supplier['companyName']:"";
  221. }
  222. $data[]=$value;
  223. }
  224. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  225. }
  226. public function edit(){
  227. $token = isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  228. $id = isset($this->post['id']) && $this->post['id'] !=="" ? trim($this->post['id']) :"";
  229. if($id==""){
  230. return error_show(1002,"参数id不能为空");
  231. }
  232. $etid = Db::name("sale")->where(["id"=>$id,"is_del"=>0])->find();
  233. if(empty($etid)){
  234. return error_show(1002,"未找到数据");
  235. }
  236. // $orderCode=isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  237. // if($orderCode==""){
  238. // return error_show(1002,"销售单code不能为空");
  239. // }
  240. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !=="" ? trim($this->post['good_code']) :"";
  241. if($good_code==""){
  242. return error_show(1002,"商品code不能为空");
  243. }
  244. $customer_code=isset($this->post['customer_code']) && $this->post['customer_code'] !=="" ? intval($this->post['customer_code']):"";
  245. if($customer_code==""){
  246. return error_show(1002,"客户code不能为空");
  247. }
  248. $apply_id =GetUserInfo($token);
  249. if(empty($apply_id)||$apply_id['code']!=0){
  250. return error_show(1002,"申请人数据不存在");
  251. }
  252. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  253. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  254. $good_name=isset($this->post['good_name']) && $this->post['good_name'] !=="" ? trim($this->post['good_name']) :"";
  255. $good_num=isset($this->post['good_num']) && $this->post['good_num'] !=="" ? intval($this->post['good_num']) :"";
  256. $origin_price = isset($this->post['origin_price']) && $this->post['origin_price'] !=="" ? intval($this->post['origin_price']) :"";
  257. $sale_price=isset($this->post['sale_price']) && $this->post['sale_price'] !=="" ? trim($this->post['sale_price']) :"";
  258. $total_price=isset($this->post['total_price']) && $this->post['total_price'] !=="" ? trim($this->post['total_price']) :"";
  259. $post_fee=isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? trim($this->post['post_fee']) :"";
  260. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  261. $vr= isset($this->post['order_addr']) && $this->post['order_addr'] !=="" ? $this->post['order_addr']:"";
  262. if($vr==""){
  263. return error_show(1002,"参数order_addr不能为空");
  264. }
  265. $vi= isset($this->post['good_stock']) && $this->post['good_stock'] !=="" ? $this->post['good_stock']:"";
  266. if($vi==""){
  267. return error_show(1002,"参数good_stock不能为空");
  268. }
  269. Db::startTrans();
  270. try {
  271. $datn=[
  272. "id"=>$id,
  273. "good_code"=>$good_code,
  274. "customer_code"=>$customer_code,
  275. "good_name"=>$good_name,
  276. "good_num"=>$good_num,
  277. "apply_id"=>$rm,
  278. "apply_name"=>$ri,
  279. "origin_price"=>$origin_price,
  280. "sale_price"=>$sale_price,
  281. "total_price"=>$total_price,
  282. "post_fee"=>$post_fee,
  283. "status"=>$status,
  284. "is_del"=>0,
  285. "updatetime"=>date("Y-m-d H:i:s")
  286. ];
  287. $datninfo = Db::name('sale')->where(['id'=>$id,'is_del'=>0])->save($datn);
  288. //$vat=[];
  289. if($datninfo>0){
  290. foreach ($vi as $ion){
  291. $iten=[];
  292. isset($ion['id'])&&$ion['id']!=="" ? $iten['id'] = $ion['id']:'';
  293. $iten['orderCode']=$etid['orderCode'];
  294. $iten['wsm_code']=$ion['wsm_code'];
  295. $iten['stock_id']=$ion['stock_id'];
  296. $iten['num']=$ion['num'];
  297. $iten['status']=$ion['status'];
  298. $iten['sale_price']=$ion['sale_price'];
  299. isset($ion['id'])&&$ion['id']!=="" ? '' : $iten['addtime'] =date("Y-m-d H:i:s");
  300. $iten['updatetime'] =date("Y-m-d H:i:s");
  301. //$inn[]=$iten;
  302. $vp= Db::name('sale_info')->save($iten);
  303. }
  304. foreach ($vr as $value){
  305. $temp=[];
  306. isset($value['id'])&&$value['id']!=="" ? $temp['id'] = $value['id']:'';
  307. $temp['orderCode']=$etid['orderCode'];
  308. $temp['contactor']=$value['contactor'];
  309. $temp['mobile'] = $value['mobile'];
  310. $temp['addr'] = $value['addr'];
  311. $temp['addr_code'] =$value['addr_code'];
  312. $temp['customer_code'] =$customer_code;
  313. $temp['receipt_quantity']=$value['receipt_quantity'];
  314. $temp['post_fee'] =0;
  315. $temp['is_del'] =$value['is_del'];
  316. isset($value['id'])&&$value['id']!=="" ? '' : $temp['addtime'] =date("Y-m-d H:i:s");
  317. $temp['updatetime'] =date("Y-m-d H:i:s");
  318. $temp['arrive_time']=date("Y-m-d H:i:s");
  319. $dat = Db::name('order_addr')->save($temp);
  320. if($dat==false){
  321. Db::rollback();
  322. return error_show(1002,"更新失败");
  323. }
  324. }
  325. Db::commit();
  326. return error_show(0,"更新成功");
  327. }
  328. Db::rollback();
  329. return error_show(1002,"更新失败");
  330. }catch(\Exception $e){
  331. Db::rollback();
  332. return error_show(1005,$e->getMessage());
  333. }
  334. }
  335. public function info(){
  336. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  337. if($id==""){
  338. return error_show(1002,"参数id不能为空");
  339. }
  340. //->field('good_code,good_name,good_num,customer_code,orderCode')
  341. $einfo = Db::name('sale')->where(['id'=>$id,'is_del'=>0])->find();
  342. if(empty($einfo)){
  343. return error_show(1002,"未找到销售订单数据");
  344. }
  345. $goon = Db::name('good')->alias('b')->join("good_type a","a.good_code=b.good_code","left")
  346. ->where(['a.type_code'=> $einfo['good_code']])->find();
  347. if(empty($goon)){
  348. return error_show(1003,"未找到商品数据");
  349. }
  350. $int= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
  351. $in= Db::name('customer_info')->where(['companyNo'=>$einfo['customer_code']])->field('companyName')->find();
  352. // ->field('addr,contactor,mobile,receipt_quantity,arrive_time')
  353. $einfo['supplierName']='';
  354. if($einfo['supplierNo']!=""){
  355. $supplier=Db::name("supplier")->where(["code"=>$einfo['supplierNo']])->find();
  356. $einfo['supplierName']=isset($supplier['name']) ? $supplier['name']:"";
  357. }
  358. $fo = Db::name('order_addr')->where(['orderCode'=>$einfo['orderCode'],'is_del'=>0])->select();
  359. $bum = Db::name('order_out')->where(['orderCode'=>$einfo['orderCode']])->select();
  360. $ins=[];
  361. foreach ($bum as $stn){
  362. $so = Db::name('order_addr')->where(['id'=>$stn['addrid']])->find();
  363. $addr = GetAddr($so['addr_code']);
  364. $stn['addr_info']=$addr;
  365. $stn['add_code']=json_decode($so['addr_code'],true);
  366. $stn['addr']=$so['addr'];
  367. $stn['contact']=$so['contactor'];
  368. $stn['mobile']=$so['mobile'];
  369. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b", "a.supplierNo=b.code")
  370. ->where(["a.wsm_code" => $stn['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  371. $stn['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  372. $stn['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  373. $stn['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  374. $ins[]=$stn;
  375. }
  376. $addrs=[];
  377. foreach ($fo as $value){
  378. $value['addr_info']="";
  379. $value['send_num']=0;
  380. if($value['addr_code']!=""){
  381. $addr=GetAddr($value['addr_code']);
  382. $value['addr_info'] = $addr;
  383. $value['addr_code'] = json_decode($value['addr_code'],true);
  384. $send = Db::name("order_out")->where(['addrid'=>$value['id'],'orderCode' => $einfo['orderCode']])->sum("send_num");
  385. $value['send_num'] = $send ?? 0;
  386. }
  387. $value['wsend_num']=$value['receipt_quantity']-$value['send_num'];
  388. $addrs[]=$value;
  389. }
  390. //->field('num,sale_price,wsm_code,stock_id')
  391. $st = Db::name('sale_info')->where(['orderCode'=>$einfo['orderCode']])->select();
  392. $data=[];
  393. foreach ($st as $value){
  394. $value['wsm_name']="";
  395. $value['wsm_supplier']="";
  396. $value['wsm_supplierNo']="";
  397. $value['usable_stock']=0;
  398. $value['send_num']=0;
  399. if($value['wsm_code']!="") {
  400. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b", "a.supplierNo=b.code")
  401. ->where(["a.wsm_code" => $value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  402. $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  403. $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  404. $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  405. $dn = Db::name('good_stock')->where(['wsm_code' => $value['wsm_code'], 'good_type_code' => $einfo['good_code']])->find();
  406. $value['usable_stock'] = isset($dn['usable_stock']) ? $dn['usable_stock'] : "0";
  407. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $einfo['orderCode']])->sum("send_num");
  408. $value['send_num'] = $send ?? 0;
  409. }
  410. $value['wsend_num']=$value['num']-$value['send_num'];
  411. $data[]=$value;
  412. }
  413. $einfo['info']=$data;
  414. $einfo['companyName']=isset($in['companyName']) ? $in['companyName']:"";
  415. $einfo['addrs']=$addrs;
  416. $einfo['bum']=$ins;
  417. $einfo['can']=$int;
  418. if(empty($einfo)){
  419. return error_show(1002,"未找到销售订单数据");
  420. }else{
  421. return app_show(0,"获取成功",$einfo);
  422. }
  423. }
  424. public function del(){
  425. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  426. if($id===""){
  427. return error_show(1002,"参数id不能为空");
  428. }
  429. $ed = Db::name('sale')->where(['id'=>$id,'is_del'=>0])->find();
  430. if(empty($ed)){
  431. return error_show(1002,"未找到销售订单数据");
  432. }
  433. $str =Db::name('sale')->update(['id'=>$id,'is_del'=>1,'updatetime'=>date("Y-m-d H:i:s")]);
  434. if($str){
  435. return error_show(0,"删除成功");
  436. }else{
  437. return error_show(1002,"删除失败");
  438. }
  439. }
  440. public function status(){
  441. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  442. if($id==""){
  443. return error_show(1002,"参数id不能为空");
  444. }
  445. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  446. if($status==""){
  447. return error_show(1002,"订单状态不能为空");
  448. }
  449. $dio = Db::name('sale')->where(['id'=>$id,'is_del'=>0])->find();
  450. if(empty($dio)){
  451. return error_show(1002,"销售单信息未找到");
  452. }
  453. if($status==1 && $dio['order_type']==2) {
  454. $zxinfo = Db::name("consult")->where(["zxNo" => $dio['zxNo'], "is_del" => 0])->find();
  455. if ($zxinfo == false) {
  456. return error_show(1004, "未找到咨询单数据");
  457. }
  458. if ($zxinfo['zx_type'] == 2) {
  459. if ($zxinfo['metals'] == "") {
  460. return error_show(1004, "未找到咨询单贵金属类型数据");
  461. }
  462. $metals = $zxinfo['metals'] == '18K' ? 1 : ($zxinfo['metals'] == '24K' ? 2 : 3);
  463. $gold = Db::name("gold_price")->where(["type" => $metals])->order("addtime desc")->find();
  464. if (empty($gold)) {
  465. return error_show(1004, "未找到咨询单贵金属类型数据");
  466. }
  467. }
  468. $wsm = Db::name("warehouse_info")->where(["supplierNo" => $zxinfo['gysNo'], "wsm_type" => 2])->find();
  469. if (empty($wsm)) {
  470. return error_show(1002, "供应商仓库未找到");
  471. }
  472. $dio['gold_price']=$gold['gold_price'];
  473. }
  474. Db::startTrans();
  475. try {
  476. $dio['status']=$status;
  477. $dio['updatetime']=date("Y-m-d H:i:s");
  478. $st = Db::name('sale')->save($dio);
  479. if($st) {
  480. if($status==3){
  481. $dn = Db::name('sale_info')->where(['orderCode' => $st['orderCode']])->select();
  482. foreach ($dn as $value) {
  483. $dm = Db::name('good_stock')->where(['wsm_code' => $value['wsm_code'], 'good_type_code' => $st['good_code']])->find();
  484. if($dm==""){
  485. Db::rollback();
  486. return error_show(1003,"未找到商品数据");
  487. }
  488. if($value['num']>$dm['usable_stock']){
  489. Db::rollback();
  490. return error_show(1002,"超出库存数量");
  491. }
  492. $dm['usable_stock']-=$value['num'];
  493. $dm['wait_out_stock']+=$value['num'];
  494. $send =Db::name("order_out")->where(["wsm_code"=>$value['wsm_code']])->sum("send_num");
  495. $value['send_num'] = $send??0;
  496. $value['wsend_num']=$value['num']-$value['send_num'];
  497. $Db = Db::name('good_stock')->update($dm);
  498. $Db['updatetime']= date('Y-m-d H:i:s');
  499. if($Db==false){
  500. Db::rollback();
  501. return error_show(1002,"状态更新失败");
  502. }
  503. }
  504. }
  505. if($status==1 && $dio['order_type']==2){
  506. $cgd =makeNo("CG");
  507. $clll=[
  508. "cgdNo"=>$cgd,
  509. "bkcode"=>$dio['orderCode'],
  510. "wsm_code"=>$wsm['wsm_code'],
  511. "cgder_id"=>"0",
  512. "cgder"=>$zxinfo['saler'],
  513. "good_code"=>"GD-".$zxinfo['cpNo'],
  514. "good_name"=>$zxinfo['cpName'],
  515. "good_type_code"=>$zxinfo['cpNo'],
  516. "good_num"=>$dio['good_num'],
  517. "good_price"=>$zxinfo['total_fee'],
  518. "total_fee"=>round($zxinfo['total_fee']*$dio['good_num'],2),
  519. "pakge_fee"=>$zxinfo['package_fee'],
  520. "cert_fee"=>$zxinfo['cert_fee'],
  521. "open_fee"=>$zxinfo['open_fee'],
  522. "delivery_fee"=>$zxinfo['delivery_fee'],
  523. "mark_fee"=>$zxinfo['mark_fee'],
  524. "teach_fee"=>$zxinfo['cost_fee'],
  525. "demo_fee"=>$zxinfo['demo_fee'],
  526. "nake_fee"=>$zxinfo['bare_fee'],
  527. "weight"=>isset($zxinfo['weight'])?$zxinfo['weight']:0,
  528. "supplierNo"=>$zxinfo['gysNo'],
  529. "supplier_name"=>$zxinfo['gysname'],
  530. "gold_price"=>$gold['gold_price'],
  531. "send_num"=>0,
  532. "wsend_num"=>$dio['good_num'],
  533. "remark"=>'',
  534. "lasttime"=>date("Y-m-d H:i:s"),
  535. "is_del"=>0,
  536. "status"=>$dio['send_type']==1?1:0,
  537. "order_type"=>2,
  538. "addtime"=>date("Y-m-d H:i:s"),
  539. "updatetime"=>date("Y-m-d H:i:s")
  540. ];
  541. $cgin = Db::name("purchease_order")->insert($clll);
  542. // var_dump(Db::name("purchease_order")->getLastSql(),$clll);
  543. if($cgin==false){
  544. Db::rollback();
  545. return error_show(1002,"咨询采购单创建商品失败");
  546. }
  547. }
  548. }
  549. Db::commit();
  550. return error_show(0,"订单状态更新成功");
  551. }catch (\Exception $e) {
  552. Db::rollback();
  553. return error_show(1005, $e->getMessage());
  554. }
  555. }
  556. public function fee(){
  557. $id = isset($this->post['id']) && $this->post['id'] !=="" ? trim($this->post['id']) :"";
  558. if($id==""){
  559. return error_show(1002,"参数id不能为空");
  560. }
  561. $etid = Db::name("sale")->where(["id"=>$id,"is_del"=>0])->find();
  562. if(empty($etid)){
  563. return error_show(1002,"未找到数据");
  564. }
  565. $vr= isset($this->post['order_addr']) && $this->post['order_addr'] !=="" ? $this->post['order_addr']:"";
  566. if($vr==""){
  567. return error_show(1002,"参数order_addr不能为空");
  568. }
  569. Db::startTrans();
  570. try {
  571. $data=[
  572. "id"=>$id,
  573. "status"=>2,
  574. "is_del"=>0,
  575. "updatetime"=>date("Y-m-d H:i:s")
  576. ];
  577. $datainfo = Db::name('sale')->where(['id'=>$id,'is_del'=>0])->save($data);
  578. if($datainfo){
  579. foreach ($vr as $value){
  580. $tm=[];
  581. $tm['id'] = $value['id'];
  582. $tm['orderCode']=$etid['orderCode'];
  583. $tm['post_fee']=$value['post_fee'];
  584. $tm['updatetime']=date("Y-m-d H:i:s");
  585. $dn = Db::name('order_addr')->save($tm);
  586. if($dn==false){
  587. Db::rollback();
  588. return error_show(1002,"更新失败");
  589. }
  590. }
  591. Db::commit();
  592. return error_show(0,"更新成功");
  593. }
  594. Db::rollback();
  595. return error_show(1003,"更新失败");
  596. }catch(\Exception $e){
  597. Db::rollback();
  598. return error_show(1005,$e->getMessage());
  599. }
  600. }
  601. public function out(){
  602. $token= isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  603. $outCode=makeNo("CK");
  604. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  605. if($orderCode==""){
  606. return error_show(1002,"参数orderCoder不能为空");
  607. }
  608. $der = Db::name('sale')->where(['orderCode'=>$orderCode])->find();
  609. if($der==""){
  610. return error_show(1002,"未找到出库订单单号");
  611. }
  612. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] !=="" ? trim($this->post['wsm_code']) :"";
  613. if($wsm_code==""){
  614. return error_show(1002,"参数wsm_code不能为空");
  615. }
  616. $wsm= Db::name('sale_info')->where(['wsm_code'=>$wsm_code,'orderCode'=>$der['orderCode']])->find();
  617. if($wsm==""){
  618. return error_show(1002,"未找到仓库编码");
  619. }
  620. $post_name= isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
  621. if($post_name==""){
  622. return error_show(1002,"物流公司不能为空");
  623. }
  624. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
  625. if($post_code==""){
  626. return error_show(1002,"物流单号不能为空");
  627. }
  628. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? intval($this->post['post_fee']) :"";
  629. if($post_fee==""){
  630. return error_show(1002,"物流费不能为空");
  631. }
  632. $send_num = isset($this->post['send_num']) && $this->post['send_num'] !=="" ? $this->post['send_num'] :"";
  633. if($send_num==""){
  634. return error_show(1002,"发货数量不能为空");
  635. }
  636. if($send_num>$wsm['num']){
  637. return error_show(1002,"超出可发货数量");
  638. }
  639. $apply_id =GetUserInfo($token);
  640. if(empty($apply_id)||$apply_id['code']!=0){
  641. return error_show(1002,"申请人数据不存在");
  642. }
  643. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  644. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  645. $addrid = isset($this->post['addrid']) && $this->post['addrid'] !=="" ? intval($this->post['addrid']) :"";
  646. $dr = Db::name('order_addr')->where(['id'=>$addrid,'is_del'=>0])->find();
  647. if(empty($dr)){
  648. return error_show(1003,"收货人信息未找到");
  649. }
  650. if($dr['orderCode']!== $orderCode){
  651. return error_show(1002,"订单code不存在");
  652. }
  653. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !=="" ? $this->post['sendtime']:date("Y-m-d H:i:s");
  654. // $check_num = isset($this->post['check_num']) && $this->post['check_num'] !=="" ? $this->post['check_num'] :"";
  655. // $error_num = isset($this->post['error_num']) && $this->post['error_num'] !=="" ? $this->post['error_num'] :"";
  656. $status = isset($this->post['status']) && $this->post['status'] !=="" ? $this->post['status'] :"1";
  657. //$dn=Db::name('sale_info')->where(['wsm_code'=>$outCode])->find();
  658. Db::startTrans();
  659. try {
  660. $data=[
  661. "wsm_code"=>$wsm_code,
  662. "orderCode"=>$orderCode,
  663. "outCode"=>$outCode,
  664. "order_type"=>$der['order_type'],
  665. "apply_id"=>$rm,
  666. "apply_name"=>$ri,
  667. "addrid"=>$dr['id'],
  668. "post_name"=>$post_name,
  669. "post_code"=>$post_code,
  670. "post_fee"=>$post_fee,
  671. "sendtime"=>$sendtime,
  672. "send_num"=>$send_num,
  673. "check_num"=>0,
  674. "error_num"=>0,
  675. "status"=>$status,
  676. "addtime"=>date("Y-m-d H:i:s"),
  677. "updatetime"=>date("Y-m-d H:i:s")
  678. ];
  679. $datainfo = Db::name('order_out')->insert($data);
  680. if($datainfo) {
  681. if ($status==1) {
  682. $var = Db::name('sale_info')->where(['orderCode' =>$orderCode,'wsm_code'=>$wsm_code])->find();
  683. //var_dump(Db::name('sale_info')->getLastSql());
  684. if($var==""){
  685. return error_show(1002,"未找到数据");
  686. }
  687. $send =Db::name("order_out")->where(["wsm_code"=>$wsm_code,'orderCode'=>$orderCode])->sum("send_num");
  688. $der['send_num']+= $send;
  689. $der['wsend_num']-=$send ;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  690. $der['send_status']=$der['send_num']==0 ? 1 :$der['wsend_num']==0?3:2;
  691. $der['status']=$der['send_num']==0 ? 3 :$der['wsend_num']==0?5:4;
  692. $der['updatetime'] = date("Y-m-d H:i:s");
  693. $si = Db::name('sale')->save($der);
  694. if($si==false){
  695. Db::rollback();
  696. return error_show(1003,"更新失败");
  697. }
  698. $str = Db::name('good_stock')->where(['wsm_code' => $var['wsm_code'],'good_type_code'=>$der['good_code']])->find();
  699. if ($str == false) {
  700. Db::rollback();
  701. return error_show(1002, "商品数据未找到");
  702. }
  703. if ($var['num'] > $str['usable_stock']) {
  704. Db::rollback();
  705. return error_show(1002, "超出库存数量");
  706. }
  707. $str['wait_out_stock'] -= $var['num'];
  708. $str['intra_stock'] += $var['num'];
  709. $str['updatetime'] = date("Y-m-d H:i:s");
  710. $stre = Db::name('good_stock')->save($str);
  711. if ($stre == false) {
  712. Db::rollback();
  713. return error_show(1002, "状态更新失败");
  714. }
  715. }
  716. Db::commit();
  717. return error_show(0,"创建成功");
  718. }else{
  719. Db::rollback();
  720. return error_show(1003,"创建失败");
  721. }
  722. }catch (\Exception $e) {
  723. Db::rollback();
  724. return error_show(1005, $e->getMessage());
  725. }
  726. }
  727. public function customer(){
  728. $outCode= isset($this->post['outCode']) && $this->post['outCode'] !=="" ? trim($this->post['outCode']) :"";
  729. if(empty($outCode)){
  730. return error_show(1002,"销售订单编号不能为空");
  731. }
  732. $codeinfo = Db::name('order_out')->where(['outCode'=>$outCode])->find();
  733. if($codeinfo==""){
  734. return error_show(1003,"未找到订单数据");
  735. }
  736. // $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  737. // if($orderCode==""){
  738. // return error_show(1002,"参数orderCoder不能为空");
  739. // }
  740. $dr = Db::name('sale')->where(['orderCode'=>$codeinfo['orderCode']])->find();
  741. if($dr==""){
  742. return error_show(1002,"未找到销售订单");
  743. }
  744. $status = isset($this->post['status']) && $this->post['status'] !== "" ? $this->post['status'] :"";
  745. if($status==""){
  746. return error_show(10002,"状态不能为空");
  747. }
  748. Db::startTrans();
  749. try {
  750. $codeinfo['status']=$status;
  751. $codeinfo['updatetime']=date("Y-m-d H:i:s");
  752. $cust = Db::name('order_out')->save($codeinfo);
  753. if($cust){
  754. if($status==2){
  755. $ct =Db::name('sale_info')->where(['orderCode' => $dr['orderCode'],'wsm_code'=>$codeinfo['wsm_code']])->find();
  756. if($ct==false){
  757. return error_show(1002,"未找到数据");
  758. }
  759. $var = Db::name('good_stock')->where(['wsm_code'=>$codeinfo['wsm_code'],'good_type_code'=>$dr['good_code']])->find();
  760. if($var==false){
  761. Db::rollback();
  762. return error_show(1002,"未找到商品数据");
  763. }
  764. if($ct['num']>$var['intra_stock']){
  765. Db::rollback();
  766. return error_show(1003,"超出库存数量");
  767. }
  768. $var['intra_stock']-=$codeinfo['send_num'];
  769. $var['updatetime']=date("Y-m-d H:i:s");
  770. $stre = Db::name('good_stock')->save($var);
  771. if ($stre == false) {
  772. Db::rollback();
  773. return error_show(1002, "状态更新失败");
  774. }
  775. }
  776. Db::commit();
  777. return error_show(0,"出库订单更新成功");
  778. }else{
  779. Db::rollback();
  780. return error_show(1003,"出库订单更新失败");
  781. }
  782. }catch (\Exception $e) {
  783. Db::rollback();
  784. return error_show(1005, $e->getMessage());
  785. }
  786. }
  787. public function difflist(){
  788. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  789. $size = isset($this->post['size']) && $this->post['size'] !==""? intval($this->post['size']):"10";
  790. $where = [];
  791. $cgdNo = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode']):"";
  792. if($cgdNo!=""){
  793. $where[]=['a.orderCode',"like", "%$cgdNo%"];
  794. }
  795. $diffNo = isset($this->post['diffCode']) && $this->post['diffCode']!="" ? trim($this->post['diffCode']):"";
  796. if($diffNo!=""){
  797. $where[]=['a.diffCode',"like", "%$diffNo%"];
  798. }
  799. $status = isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']):"";
  800. if($status!==""){
  801. $where[]=['a.status',"=",$status];
  802. }
  803. $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
  804. if ($start !="") {
  805. $where[]= ["a.addtime",'>=',$start];
  806. }
  807. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
  808. if($end !=""){
  809. $where[]= ["a.addtime",'<=',$end];
  810. }
  811. // $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !==""? intval($this->post['apply_name']):"";
  812. // if($apply_name!==""){
  813. // $where[]=['a.apply_name',"like","%$apply_name%"];
  814. // }
  815. $count = Db::name('sale_diff')->alias('a')->join("good b","b.good_code=a.good_code","left")
  816. ->where($where)->count();
  817. $total = ceil($count/$size);
  818. $page = $page >= $total ? $total : $page;
  819. $list = Db::name('sale_diff')->alias('a')->join("good b","b.good_code=a.good_kode","left")
  820. ->where($where)->page($page,$size)->field("a.*,b.cat_id")->order("a.addtime desc")->select();
  821. $data=[];
  822. foreach ($list as $value){
  823. $value['can']= isset($value['cat_id']) && $value['cat_id'] !=0 ? made($value['cat_id']):[];
  824. $data[]=$value;
  825. }
  826. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  827. }
  828. /**
  829. * @return \think\response\Json|void
  830. * @throws \think\db\exception\DataNotFoundException
  831. * @throws \think\db\exception\DbException
  832. * @throws \think\db\exception\ModelNotFoundException
  833. */
  834. public function diffcheck(){
  835. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  836. if($id===""){
  837. return error_show(1004,"参数id不能为空");
  838. }
  839. $info = Db::name("sale_diff")->where(["id"=>$id])->find();
  840. if(empty($info)){
  841. return error_show(1004,"订单数据未找到");
  842. }
  843. $is_act = isset($this->post['is_act']) && $this->post['is_act'] !==""? intval($this->post['is_act']):"";
  844. if($is_act===''){
  845. return error_show(1004,"参数is_act不能为空");
  846. }
  847. $customer_remark = isset($this->post['customer_remark']) && $this->post['customer_remark'] !==""? trim($this->post['customer_remark']):"";
  848. if($customer_remark===''){
  849. return error_show(1004,"参数customer_remark不能为空");
  850. }
  851. $status = isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']):"";
  852. $remark = isset($this->post['remark']) && $this->post['remark'] !==""? trim($this->post['remark']):"";
  853. $info['is_act'] =$is_act;
  854. $info['customer_remark'] =$customer_remark;
  855. $info['status'] =$status;
  856. $info['remark'] =$remark;
  857. $info['updatetime'] =date("Y-m-d H:i:s");
  858. $up = Db::name("sale_diff")->save($info);
  859. if($up){
  860. return app_show(0,"更新成功");
  861. }else{
  862. return error_show(1003,"更新失败");
  863. }
  864. }
  865. /**
  866. * @return \think\response\Json|void
  867. * @throws \think\db\exception\DataNotFoundException
  868. * @throws \think\db\exception\DbException
  869. * @throws \think\db\exception\ModelNotFoundException
  870. */
  871. public function diffstatus(){
  872. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  873. if($id===""){
  874. return error_show(1004,"参数id不能为空");
  875. }
  876. $info = Db::name("sale_diff")->where(["id"=>$id])->find();
  877. if(empty($info)){
  878. return error_show(1004,"订单数据未找到");
  879. }
  880. $status = isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']):"";
  881. $remark = isset($this->post['remark']) && $this->post['remark'] !==""? trim($this->post['remark']):"";
  882. $info['status'] =$status;
  883. $info['remark'] =$remark;
  884. $info['updatetime'] =date("Y-m-d H:i:s");
  885. $up = Db::name("sale_diff")->save($info);
  886. if($up){
  887. return app_show(0,"更新成功");
  888. }else{
  889. return error_show(1003,"更新失败");
  890. }
  891. }
  892. /**
  893. * @return \think\response\Json|void
  894. * @throws \think\db\exception\DataNotFoundException
  895. * @throws \think\db\exception\DbException
  896. * @throws \think\db\exception\ModelNotFoundException
  897. */
  898. public function diffinfo(){
  899. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  900. if($id===""){
  901. return error_show(1004,"参数id不能为空");
  902. }
  903. $info = Db::name('sale_diff')->alias('a')->join("good b","b.good_code=a.good_kode","left")->where
  904. (["a.id"=>$id])->field("a.*,b.cat_id")->find();
  905. if(empty($info)){
  906. return error_show(1004,"订单数据未找到");
  907. }
  908. $info['can']= isset($info['cat_id']) && $info['cat_id'] !==0 ? made($info['cat_id']):[];
  909. return app_show(0,"获取成功",$info);
  910. }
  911. }