Sale.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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'] !=="" ? trim($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. if($datninfo>0){
  289. foreach ($vi as $ion) {
  290. $iten = [];
  291. isset($ion['id']) && $ion['id'] !== "" ? $iten['id'] = $ion['id'] : '';
  292. $iten['orderCode'] = $etid['orderCode'];
  293. $iten['wsm_code'] = $ion['wsm_code'];
  294. $iten['stock_id'] = $ion['stock_id'];
  295. $iten['num'] = $ion['num'];
  296. $iten['status'] = $ion['status'];
  297. $iten['sale_price'] = $ion['sale_price'];
  298. isset($ion['id']) && $ion['id'] !== "" ? '' : $iten['addtime'] = date("Y-m-d H:i:s");
  299. $iten['updatetime'] = date("Y-m-d H:i:s");
  300. //$inn[]=$iten;
  301. $vp = Db::name('sale_info')->save($iten);
  302. if ($vp == false) {
  303. Db::rollback();
  304. return error_show(1002, "更新失败");
  305. }
  306. }
  307. foreach ($vr as $value) {
  308. $temp = [];
  309. isset($value['id']) && $value['id'] !== "" ? $temp['id'] = $value['id'] : '';
  310. $temp['orderCode'] = $etid['orderCode'];
  311. $temp['contactor'] = $value['contactor'];
  312. $temp['mobile'] = $value['mobile'];
  313. $temp['addr'] = $value['addr'];
  314. $temp['addr_code'] = $value['addr_code'];
  315. $temp['customer_code'] = $customer_code;
  316. $temp['receipt_quantity'] = $value['receipt_quantity'];
  317. $temp['post_fee'] = 0;
  318. $temp['is_del'] = $value['is_del'];
  319. isset($value['id']) && $value['id'] !== "" ? '' : $temp['addtime'] = date("Y-m-d H:i:s");
  320. $temp['updatetime'] = date("Y-m-d H:i:s");
  321. $temp['arrive_time'] = date("Y-m-d H:i:s");
  322. $dat = Db::name('order_addr')->save($temp);
  323. if ($dat == false) {
  324. Db::rollback();
  325. return error_show(1002, "更新失败");
  326. }
  327. }
  328. Db::commit();
  329. return error_show(0,"更新成功");
  330. }
  331. Db::rollback();
  332. return error_show(1002,"更新失败");
  333. }catch(\Exception $e){
  334. Db::rollback();
  335. return error_show(1005,$e->getMessage());
  336. }
  337. }
  338. public function info(){
  339. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']):"";
  340. if($id==""){
  341. return error_show(1002,"参数id不能为空");
  342. }
  343. //->field('good_code,good_name,good_num,customer_code,orderCode')
  344. $einfo = Db::name('sale')->where(['id'=>$id,'is_del'=>0])->find();
  345. if(empty($einfo)){
  346. return error_show(1002,"未找到销售订单数据");
  347. }
  348. $goon = Db::name('good')->alias('b')->join("good_type a","a.good_code=b.good_code","left")
  349. ->where(['a.type_code'=> $einfo['good_code']])->find();
  350. if(empty($goon)){
  351. return error_show(1003,"未找到商品数据");
  352. }
  353. $int= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
  354. $in= Db::name('customer_info')->where(['companyNo'=>$einfo['customer_code']])->field('companyName')->find();
  355. // ->field('addr,contactor,mobile,receipt_quantity,arrive_time')
  356. $einfo['supplierName']='';
  357. if($einfo['supplierNo']!=""){
  358. $supplier=Db::name("supplier")->where(["code"=>$einfo['supplierNo']])->find();
  359. $einfo['supplierName']=isset($supplier['name']) ? $supplier['name']:"";
  360. }
  361. $fo = Db::name('order_addr')->where(['orderCode'=>$einfo['orderCode'],'is_del'=>0])->select();
  362. $bum = Db::name('order_out')->where(['orderCode'=>$einfo['orderCode']])->select();
  363. $ins=[];
  364. if(!empty($bum)){
  365. foreach ($bum as $stn){
  366. $so = Db::name('order_addr')->where(['id'=>$stn['addrid']])->find();
  367. $addr = GetAddr($so['addr_code']);
  368. $stn['addr_info']=$addr;
  369. $stn['add_code']=json_decode($so['addr_code'],true);
  370. $stn['addr']=$so['addr'];
  371. $stn['contact']=$so['contactor'];
  372. $stn['mobile']=$so['mobile'];
  373. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b", "a.supplierNo=b.code")
  374. ->where(["a.wsm_code" => $stn['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  375. $stn['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  376. $stn['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  377. $stn['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  378. $ins[]=$stn;
  379. }
  380. }
  381. $addrs=[];
  382. if(!empty($fo)){
  383. foreach ($fo as $value){
  384. $value['addr_info']="";
  385. $value['send_num']=0;
  386. if($value['addr_code']!=""){
  387. $addr=GetAddr($value['addr_code']);
  388. $value['addr_info'] = $addr;
  389. $value['addr_code'] = json_decode($value['addr_code'],true);
  390. $send = Db::name("order_out")->where(['addrid'=>$value['id'],'orderCode' => $einfo['orderCode']])->sum("send_num");
  391. $value['send_num'] = $send ?? 0;
  392. }
  393. $value['wsend_num']=$value['receipt_quantity']-$value['send_num'];
  394. $addrs[]=$value;
  395. }
  396. }
  397. //->field('num,sale_price,wsm_code,stock_id')
  398. $st = Db::name('sale_info')->where(['orderCode'=>$einfo['orderCode']])->select();
  399. $data=[];
  400. if(!empty($fo)) {
  401. foreach ($st as $value) {
  402. $value['wsm_name'] = "";
  403. $value['wsm_supplier'] = "";
  404. $value['wsm_supplierNo'] = "";
  405. $value['usable_stock'] = 0;
  406. $value['send_num'] = 0;
  407. if ($value['wsm_code'] != "") {
  408. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b", "a.supplierNo=b.code")
  409. ->where(["a.wsm_code" => $value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  410. $value['wsm_name'] = isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name'] : "";
  411. $value['wsm_supplier'] = isset($wsmcode['name']) ? $wsmcode['name'] : "";
  412. $value['wsm_supplierNo'] = isset($wsmcode['code']) ? $wsmcode['code'] : "";
  413. $dn = Db::name('good_stock')->where(['wsm_code' => $value['wsm_code'], 'good_type_code' => $einfo['good_code']])->find();
  414. $value['usable_stock'] = isset($dn['usable_stock']) ? $dn['usable_stock'] : "0";
  415. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], 'orderCode' => $einfo['orderCode']])->sum("send_num");
  416. $value['send_num'] = $send ?? 0;
  417. }
  418. $value['wsend_num'] = $value['num'] - $value['send_num'];
  419. $data[] = $value;
  420. }
  421. }
  422. $einfo['info']=$data;
  423. $einfo['companyName']=isset($in['companyName']) ? $in['companyName']:"";
  424. $einfo['addrs']=$addrs;
  425. $einfo['bum']=$ins;
  426. $einfo['can']=$int;
  427. if(empty($einfo)){
  428. return error_show(1002,"未找到销售订单数据");
  429. }else{
  430. return app_show(0,"获取成功",$einfo);
  431. }
  432. }
  433. public function del(){
  434. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  435. if($id===""){
  436. return error_show(1002,"参数id不能为空");
  437. }
  438. $ed = Db::name('sale')->where(['id'=>$id,'is_del'=>0])->find();
  439. if(empty($ed)){
  440. return error_show(1002,"未找到销售订单数据");
  441. }
  442. $str =Db::name('sale')->update(['id'=>$id,'is_del'=>1,'updatetime'=>date("Y-m-d H:i:s")]);
  443. if($str){
  444. return error_show(0,"删除成功");
  445. }else{
  446. return error_show(1002,"删除失败");
  447. }
  448. }
  449. public function status(){
  450. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  451. if($id==""){
  452. return error_show(1002,"参数id不能为空");
  453. }
  454. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  455. if($status==""){
  456. return error_show(1002,"订单状态不能为空");
  457. }
  458. $dio = Db::name('sale')->where(['id'=>$id,'is_del'=>0])->find();
  459. if(empty($dio)){
  460. return error_show(1002,"销售单信息未找到");
  461. }
  462. if($status==1 && $dio['order_type']==2) {
  463. $zxinfo = Db::name("consult")->where(["zxNo" => $dio['zxNo'], "is_del" => 0])->find();
  464. if ($zxinfo == false) {
  465. return error_show(1004, "未找到咨询单数据");
  466. }
  467. if ($zxinfo['zx_type'] == 2) {
  468. if ($zxinfo['metals'] == "") {
  469. return error_show(1004, "未找到咨询单贵金属类型数据");
  470. }
  471. $metals = $zxinfo['metals'] == '18K' ? 1 : ($zxinfo['metals'] == '24K' ? 2 : 3);
  472. $gold = Db::name("gold_price")->where(["type" => $metals])->order("addtime desc")->find();
  473. if (empty($gold)) {
  474. return error_show(1004, "未找到咨询单贵金属类型数据");
  475. }
  476. }
  477. $wsm = Db::name("warehouse_info")->where(["supplierNo" => $zxinfo['gysNo'], "wsm_type" => 2])->find();
  478. if (empty($wsm)) {
  479. return error_show(1002, "供应商仓库未找到");
  480. }
  481. $dio['gold_price']=$gold['gold_price'];
  482. }
  483. Db::startTrans();
  484. try {
  485. $dio['status']=$status;
  486. $dio['updatetime']=date("Y-m-d H:i:s");
  487. $st = Db::name('sale')->save($dio);
  488. if($st) {
  489. if($status==3){
  490. $dn = Db::name('sale_info')->where(['orderCode' => $st['orderCode']])->select();
  491. foreach ($dn as $value) {
  492. $dm = Db::name('good_stock')->where(['wsm_code' => $value['wsm_code'], 'good_type_code' => $st['good_code']])->find();
  493. if($dm==""){
  494. Db::rollback();
  495. return error_show(1003,"未找到商品数据");
  496. }
  497. if($value['num']>$dm['usable_stock']){
  498. Db::rollback();
  499. return error_show(1002,"超出库存数量");
  500. }
  501. $dm['usable_stock']-=$value['num'];
  502. $dm['wait_out_stock']+=$value['num'];
  503. $send =Db::name("order_out")->where(["wsm_code"=>$value['wsm_code']])->sum("send_num");
  504. $value['send_num'] = $send??0;
  505. $value['wsend_num']=$value['num']-$value['send_num'];
  506. $Db = Db::name('good_stock')->update($dm);
  507. $Db['updatetime']= date('Y-m-d H:i:s');
  508. if($Db==false){
  509. Db::rollback();
  510. return error_show(1002,"状态更新失败");
  511. }
  512. }
  513. }
  514. if($status==1 && $dio['order_type']==2){
  515. $cgd =makeNo("CG");
  516. $clll=[
  517. "cgdNo"=>$cgd,
  518. "bkcode"=>$dio['orderCode'],
  519. "wsm_code"=>$wsm['wsm_code'],
  520. "cgder_id"=>"0",
  521. "cgder"=>$zxinfo['saler'],
  522. "good_code"=>"GD-".$zxinfo['cpNo'],
  523. "good_name"=>$zxinfo['cpName'],
  524. "good_type_code"=>$zxinfo['cpNo'],
  525. "good_num"=>$dio['good_num'],
  526. "good_price"=>$zxinfo['total_fee'],
  527. "total_fee"=>round($zxinfo['total_fee']*$dio['good_num'],2),
  528. "pakge_fee"=>$zxinfo['package_fee'],
  529. "cert_fee"=>$zxinfo['cert_fee'],
  530. "open_fee"=>$zxinfo['open_fee'],
  531. "delivery_fee"=>$zxinfo['delivery_fee'],
  532. "mark_fee"=>$zxinfo['mark_fee'],
  533. "teach_fee"=>$zxinfo['cost_fee'],
  534. "demo_fee"=>$zxinfo['demo_fee'],
  535. "nake_fee"=>$zxinfo['bare_fee'],
  536. "weight"=>isset($zxinfo['weight'])?$zxinfo['weight']:0,
  537. "supplierNo"=>$zxinfo['gysNo'],
  538. "supplier_name"=>$zxinfo['gysname'],
  539. "gold_price"=>$gold['gold_price'],
  540. "send_num"=>0,
  541. "wsend_num"=>$dio['good_num'],
  542. "remark"=>'',
  543. "lasttime"=>date("Y-m-d H:i:s"),
  544. "is_del"=>0,
  545. "status"=>$dio['send_type']==1?1:0,
  546. "order_type"=>2,
  547. "addtime"=>date("Y-m-d H:i:s"),
  548. "updatetime"=>date("Y-m-d H:i:s")
  549. ];
  550. $cgin = Db::name("purchease_order")->insert($clll);
  551. // var_dump(Db::name("purchease_order")->getLastSql(),$clll);
  552. if($cgin==false){
  553. Db::rollback();
  554. return error_show(1002,"咨询采购单创建商品失败");
  555. }
  556. }
  557. }
  558. Db::commit();
  559. return error_show(0,"订单状态更新成功");
  560. }catch (\Exception $e) {
  561. Db::rollback();
  562. return error_show(1005, $e->getMessage());
  563. }
  564. }
  565. public function fee(){
  566. $id = isset($this->post['id']) && $this->post['id'] !=="" ? trim($this->post['id']) :"";
  567. if($id==""){
  568. return error_show(1002,"参数id不能为空");
  569. }
  570. $etid = Db::name("sale")->where(["id"=>$id,"is_del"=>0])->find();
  571. if(empty($etid)){
  572. return error_show(1002,"未找到数据");
  573. }
  574. $vr= isset($this->post['order_addr']) && $this->post['order_addr'] !=="" ? $this->post['order_addr']:"";
  575. if($vr==""){
  576. return error_show(1002,"参数order_addr不能为空");
  577. }
  578. Db::startTrans();
  579. try {
  580. $data=[
  581. "id"=>$id,
  582. "status"=>2,
  583. "is_del"=>0,
  584. "updatetime"=>date("Y-m-d H:i:s")
  585. ];
  586. $datainfo = Db::name('sale')->where(['id'=>$id,'is_del'=>0])->save($data);
  587. if($datainfo){
  588. foreach ($vr as $value){
  589. $tm=[];
  590. $tm['id'] = $value['id'];
  591. $tm['orderCode']=$etid['orderCode'];
  592. $tm['post_fee']=$value['post_fee'];
  593. $tm['updatetime']=date("Y-m-d H:i:s");
  594. $dn = Db::name('order_addr')->save($tm);
  595. if($dn==false){
  596. Db::rollback();
  597. return error_show(1002,"更新失败");
  598. }
  599. }
  600. Db::commit();
  601. return error_show(0,"更新成功");
  602. }
  603. Db::rollback();
  604. return error_show(1003,"更新失败");
  605. }catch(\Exception $e){
  606. Db::rollback();
  607. return error_show(1005,$e->getMessage());
  608. }
  609. }
  610. public function out(){
  611. $token= isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  612. $outCode=makeNo("CK");
  613. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  614. if($orderCode==""){
  615. return error_show(1002,"参数orderCoder不能为空");
  616. }
  617. $der = Db::name('sale')->where(['orderCode'=>$orderCode])->find();
  618. if($der==""){
  619. return error_show(1002,"未找到出库订单单号");
  620. }
  621. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] !=="" ? trim($this->post['wsm_code']) :"";
  622. if($wsm_code==""){
  623. return error_show(1002,"参数wsm_code不能为空");
  624. }
  625. $wsm= Db::name('sale_info')->where(['wsm_code'=>$wsm_code,'orderCode'=>$der['orderCode']])->find();
  626. if($wsm==""){
  627. return error_show(1002,"未找到仓库编码");
  628. }
  629. $post_name= isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
  630. if($post_name==""){
  631. return error_show(1002,"物流公司不能为空");
  632. }
  633. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
  634. if($post_code==""){
  635. return error_show(1002,"物流单号不能为空");
  636. }
  637. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? intval($this->post['post_fee']) :"";
  638. if($post_fee==""){
  639. return error_show(1002,"物流费不能为空");
  640. }
  641. $send_num = isset($this->post['send_num']) && $this->post['send_num'] !=="" ? $this->post['send_num'] :"";
  642. if($send_num==""){
  643. return error_show(1002,"发货数量不能为空");
  644. }
  645. if($send_num>$wsm['num']){
  646. return error_show(1002,"超出可发货数量");
  647. }
  648. $apply_id =GetUserInfo($token);
  649. if(empty($apply_id)||$apply_id['code']!=0){
  650. return error_show(1002,"申请人数据不存在");
  651. }
  652. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  653. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  654. $addrid = isset($this->post['addrid']) && $this->post['addrid'] !=="" ? intval($this->post['addrid']) :"";
  655. $dr = Db::name('order_addr')->where(['id'=>$addrid,'is_del'=>0])->find();
  656. if(empty($dr)){
  657. return error_show(1003,"收货人信息未找到");
  658. }
  659. if($dr['orderCode']!== $orderCode){
  660. return error_show(1002,"订单code不存在");
  661. }
  662. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !=="" ? $this->post['sendtime']:date("Y-m-d H:i:s");
  663. // $check_num = isset($this->post['check_num']) && $this->post['check_num'] !=="" ? $this->post['check_num'] :"";
  664. // $error_num = isset($this->post['error_num']) && $this->post['error_num'] !=="" ? $this->post['error_num'] :"";
  665. $status = isset($this->post['status']) && $this->post['status'] !=="" ? $this->post['status'] :"1";
  666. //$dn=Db::name('sale_info')->where(['wsm_code'=>$outCode])->find();
  667. Db::startTrans();
  668. try {
  669. $data=[
  670. "wsm_code"=>$wsm_code,
  671. "orderCode"=>$orderCode,
  672. "outCode"=>$outCode,
  673. "order_type"=>$der['order_type'],
  674. "apply_id"=>$rm,
  675. "apply_name"=>$ri,
  676. "addrid"=>$dr['id'],
  677. "post_name"=>$post_name,
  678. "post_code"=>$post_code,
  679. "post_fee"=>$post_fee,
  680. "sendtime"=>$sendtime,
  681. "send_num"=>$send_num,
  682. "check_num"=>0,
  683. "error_num"=>0,
  684. "status"=>$status,
  685. "addtime"=>date("Y-m-d H:i:s"),
  686. "updatetime"=>date("Y-m-d H:i:s")
  687. ];
  688. $datainfo = Db::name('order_out')->insert($data);
  689. if($datainfo) {
  690. if ($status==1) {
  691. $var = Db::name('sale_info')->where(['orderCode' =>$orderCode,'wsm_code'=>$wsm_code])->find();
  692. //var_dump(Db::name('sale_info')->getLastSql());
  693. if($var==""){
  694. return error_show(1002,"未找到数据");
  695. }
  696. $send =Db::name("order_out")->where(["wsm_code"=>$wsm_code,'orderCode'=>$orderCode])->sum("send_num");
  697. $der['send_num']+= $send;
  698. $der['wsend_num']-=$send ;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  699. $der['send_status']=$der['send_num']==0 ? 1 :$der['wsend_num']==0?3:2;
  700. $der['status']=$der['send_num']==0 ? 3 :$der['wsend_num']==0?5:4;
  701. $der['updatetime'] = date("Y-m-d H:i:s");
  702. $si = Db::name('sale')->save($der);
  703. if($si==false){
  704. Db::rollback();
  705. return error_show(1003,"更新失败");
  706. }
  707. $str = Db::name('good_stock')->where(['wsm_code' => $var['wsm_code'],'good_type_code'=>$der['good_code']])->find();
  708. if ($str == false) {
  709. Db::rollback();
  710. return error_show(1002, "商品数据未找到");
  711. }
  712. if ($var['num'] > $str['usable_stock']) {
  713. Db::rollback();
  714. return error_show(1002, "超出库存数量");
  715. }
  716. $str['wait_out_stock'] -= $var['num'];
  717. $str['intra_stock'] += $var['num'];
  718. $str['updatetime'] = date("Y-m-d H:i:s");
  719. $stre = Db::name('good_stock')->save($str);
  720. if ($stre == false) {
  721. Db::rollback();
  722. return error_show(1002, "状态更新失败");
  723. }
  724. }
  725. Db::commit();
  726. return error_show(0,"创建成功");
  727. }else{
  728. Db::rollback();
  729. return error_show(1003,"创建失败");
  730. }
  731. }catch (\Exception $e) {
  732. Db::rollback();
  733. return error_show(1005, $e->getMessage());
  734. }
  735. }
  736. public function customer(){
  737. $outCode= isset($this->post['outCode']) && $this->post['outCode'] !=="" ? trim($this->post['outCode']) :"";
  738. if(empty($outCode)){
  739. return error_show(1002,"销售订单编号不能为空");
  740. }
  741. $codeinfo = Db::name('order_out')->where(['outCode'=>$outCode])->find();
  742. if($codeinfo==""){
  743. return error_show(1003,"未找到订单数据");
  744. }
  745. // $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  746. // if($orderCode==""){
  747. // return error_show(1002,"参数orderCoder不能为空");
  748. // }
  749. $dr = Db::name('sale')->where(['orderCode'=>$codeinfo['orderCode']])->find();
  750. if($dr==""){
  751. return error_show(1002,"未找到销售订单");
  752. }
  753. $status = isset($this->post['status']) && $this->post['status'] !== "" ? $this->post['status'] :"";
  754. if($status==""){
  755. return error_show(10002,"状态不能为空");
  756. }
  757. Db::startTrans();
  758. try {
  759. $codeinfo['status']=$status;
  760. $codeinfo['updatetime']=date("Y-m-d H:i:s");
  761. $cust = Db::name('order_out')->save($codeinfo);
  762. if($cust){
  763. if($status==2){
  764. $ct =Db::name('sale_info')->where(['orderCode' => $dr['orderCode'],'wsm_code'=>$codeinfo['wsm_code']])->find();
  765. if($ct==false){
  766. return error_show(1002,"未找到数据");
  767. }
  768. $var = Db::name('good_stock')->where(['wsm_code'=>$codeinfo['wsm_code'],'good_type_code'=>$dr['good_code']])->find();
  769. if($var==false){
  770. Db::rollback();
  771. return error_show(1002,"未找到商品数据");
  772. }
  773. if($ct['num']>$var['intra_stock']){
  774. Db::rollback();
  775. return error_show(1003,"超出库存数量");
  776. }
  777. $var['intra_stock']-=$codeinfo['send_num'];
  778. $var['updatetime']=date("Y-m-d H:i:s");
  779. $stre = Db::name('good_stock')->save($var);
  780. if ($stre == false) {
  781. Db::rollback();
  782. return error_show(1002, "状态更新失败");
  783. }
  784. }
  785. Db::commit();
  786. return error_show(0,"出库订单更新成功");
  787. }else{
  788. Db::rollback();
  789. return error_show(1003,"出库订单更新失败");
  790. }
  791. }catch (\Exception $e) {
  792. Db::rollback();
  793. return error_show(1005, $e->getMessage());
  794. }
  795. }
  796. public function difflist(){
  797. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  798. $size = isset($this->post['size']) && $this->post['size'] !==""? intval($this->post['size']):"10";
  799. $where = [];
  800. $cgdNo = isset($this->post['orderCode']) && $this->post['orderCode']!="" ? trim($this->post['orderCode']):"";
  801. if($cgdNo!=""){
  802. $where[]=['a.orderCode',"like", "%$cgdNo%"];
  803. }
  804. $diffNo = isset($this->post['diffCode']) && $this->post['diffCode']!="" ? trim($this->post['diffCode']):"";
  805. if($diffNo!=""){
  806. $where[]=['a.diffCode',"like", "%$diffNo%"];
  807. }
  808. $status = isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']):"";
  809. if($status!==""){
  810. $where[]=['a.status',"=",$status];
  811. }
  812. $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
  813. if ($start !="") {
  814. $where[]= ["a.addtime",'>=',$start];
  815. }
  816. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
  817. if($end !=""){
  818. $where[]= ["a.addtime",'<=',$end];
  819. }
  820. // $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !==""? intval($this->post['apply_name']):"";
  821. // if($apply_name!==""){
  822. // $where[]=['a.apply_name',"like","%$apply_name%"];
  823. // }
  824. $count = Db::name('sale_diff')->alias('a')->join("good b","b.good_code=a.good_code","left")
  825. ->where($where)->count();
  826. $total = ceil($count/$size);
  827. $page = $page >= $total ? $total : $page;
  828. $list = Db::name('sale_diff')->alias('a')->join("good b","b.good_code=a.good_kode","left")
  829. ->where($where)->page($page,$size)->field("a.*,b.cat_id")->order("a.addtime desc")->select();
  830. $data=[];
  831. foreach ($list as $value){
  832. $value['can']= isset($value['cat_id']) && $value['cat_id'] !=0 ? made($value['cat_id']):[];
  833. $data[]=$value;
  834. }
  835. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  836. }
  837. /**
  838. * @return \think\response\Json|void
  839. * @throws \think\db\exception\DataNotFoundException
  840. * @throws \think\db\exception\DbException
  841. * @throws \think\db\exception\ModelNotFoundException
  842. */
  843. public function diffcheck(){
  844. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  845. if($id===""){
  846. return error_show(1004,"参数id不能为空");
  847. }
  848. $info = Db::name("sale_diff")->where(["id"=>$id])->find();
  849. if(empty($info)){
  850. return error_show(1004,"订单数据未找到");
  851. }
  852. $is_act = isset($this->post['is_act']) && $this->post['is_act'] !==""? intval($this->post['is_act']):"";
  853. if($is_act===''){
  854. return error_show(1004,"参数is_act不能为空");
  855. }
  856. $customer_remark = isset($this->post['customer_remark']) && $this->post['customer_remark'] !==""? trim($this->post['customer_remark']):"";
  857. if($customer_remark===''){
  858. return error_show(1004,"参数customer_remark不能为空");
  859. }
  860. $status = isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']):"2";
  861. $remark = isset($this->post['remark']) && $this->post['remark'] !==""? trim($this->post['remark']):"";
  862. $info['is_act'] =$is_act;
  863. $info['customer_remark'] =$customer_remark;
  864. $info['status'] =$status;
  865. $info['remark'] =$remark;
  866. $info['updatetime'] =date("Y-m-d H:i:s");
  867. $up = Db::name("sale_diff")->save($info);
  868. if($up){
  869. return app_show(0,"更新成功");
  870. }else{
  871. return error_show(1003,"更新失败");
  872. }
  873. }
  874. /**
  875. * @return \think\response\Json|void
  876. * @throws \think\db\exception\DataNotFoundException
  877. * @throws \think\db\exception\DbException
  878. * @throws \think\db\exception\ModelNotFoundException
  879. */
  880. public function diffstatus(){
  881. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  882. if($id===""){
  883. return error_show(1004,"参数id不能为空");
  884. }
  885. $info = Db::name("sale_diff")->where(["id"=>$id])->find();
  886. if(empty($info)){
  887. return error_show(1004,"订单数据未找到");
  888. }
  889. $status = isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']):"3";
  890. $remark = isset($this->post['remark']) && $this->post['remark'] !==""? trim($this->post['remark']):"";
  891. $info['status'] =$status;
  892. $info['remark'] =$remark;
  893. $info['updatetime'] =date("Y-m-d H:i:s");
  894. $up = Db::name("sale_diff")->save($info);
  895. if($up){
  896. return app_show(0,"更新成功");
  897. }else{
  898. return error_show(1003,"更新失败");
  899. }
  900. }
  901. /**
  902. * @return \think\response\Json|void
  903. * @throws \think\db\exception\DataNotFoundException
  904. * @throws \think\db\exception\DbException
  905. * @throws \think\db\exception\ModelNotFoundException
  906. */
  907. public function diffinfo(){
  908. $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
  909. if($id===""){
  910. return error_show(1004,"参数id不能为空");
  911. }
  912. $info = Db::name('sale_diff')->alias('a')->join("good b","b.good_code=a.good_kode","left")->where
  913. (["a.id"=>$id])->field("a.*,b.cat_id")->find();
  914. if(empty($info)){
  915. return error_show(1004,"订单数据未找到");
  916. }
  917. $info['can']= isset($info['cat_id']) && $info['cat_id'] !==0 ? made($info['cat_id']):[];
  918. return app_show(0,"获取成功",$info);
  919. }
  920. }