Purch.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ActionLog;
  4. use think\App;
  5. use think\facade\Db;
  6. class Purch extends \app\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 list(){
  15. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  16. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  17. $where =[['is_del',"=",0]];
  18. $bkcode = isset($this->post['bk_code']) && $this->post['bk_code']!="" ? trim($this->post['bk_code']):"";
  19. if($bkcode!=""){
  20. $where[]=['bkcode',"like", "%{$bkcode}%"];
  21. }
  22. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  23. if($status!==""){
  24. // $where['status'] = $status;
  25. $where[]=['status',"=", $status];
  26. }
  27. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo'])
  28. :"";
  29. if($cgdNo!=""){
  30. //$where['cgdNo'] = Db::Raw("like '%{$cgdNo}%'");
  31. $where[]=['cgdNo',"like", "%{$cgdNo}%"];
  32. }
  33. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name']):"";
  34. if($apply_name!=""){
  35. // $where['cgder'] =Db::Raw("like '%{$apply_name}%'");
  36. $where[]=['cgder',"like", "%{$apply_name}%"];
  37. }
  38. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']):"";
  39. if($wsm_code!=""){
  40. // $where['wsm_code'] = $wsm_code;
  41. $where[]=['wsm_code',"=", $wsm_code];
  42. }
  43. $wsm_in_code = isset($this->post['wsm_in_code']) && $this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']):"";
  44. if($wsm_in_code!=""){
  45. $incode = Db::name("purchease_in")->where(["wsm_in_code"=>Db::raw(" like %{$wsm_in_code}%"),"is_del"=>0])->column("cgdNo");
  46. if(empty($incode)){
  47. return error_show(1004,"未找到有关入库单信息");
  48. }
  49. $where[]=['cgdNo',"in", $incode];
  50. }
  51. $good_name = isset($this->post['good_name']) && $this->post['good_name']!="" ? trim($this->post['good_name']):"";
  52. if($good_name!=""){
  53. //$where['good_name'] = Db::raw(" like %{$good_name}%");
  54. $where[]=['good_name',"like", "%{$good_name}%"];
  55. }
  56. $good_code = isset($this->post['good_code']) && $this->post['good_code']!="" ? trim($this->post['good_code'])
  57. :"";
  58. if($good_code!=""){
  59. // $where['good_code'] = Db::raw(" like %{$good_code}%");
  60. $where[]=['good_code',"like", "%{$good_code}%"];
  61. }
  62. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo']!="" ? trim($this->post['supplierNo'])
  63. :"";
  64. if($supplierNo!=""){
  65. // $where['good_code'] = Db::raw(" like %{$good_code}%");
  66. $where[]=['supplierNo',"like", "%{$supplierNo}%"];
  67. }
  68. $wsm_supplierNo = isset($this->post['wsm_supplierNo']) && $this->post['wsm_supplierNo']!="" ? trim($this->post['wsm_supplierNo'])
  69. :"";
  70. if($wsm_supplierNo!=""){
  71. $supplier = Db::name("supplier")->where(["code"=>$wsm_supplierNo])->find();
  72. if(empty($supplier)){
  73. return error_show(1004,"未找到供应商信息");
  74. }
  75. $wsmcode = Db::name("warehouse_info")->where(["is_del"=>0,"supplierNo"=>$wsm_supplierNo])->column("wsm_code");
  76. $where[]=['wsm_code',"in", $wsmcode];
  77. }
  78. $start = isset($this->post['start']) && $this->post['start']!="" ?$this->post['start']:"";
  79. if($start!=""){
  80. // $where['addtime'] = Db::raw(" >= '{$start}'");
  81. $where[]=['addtime',">=", $start];
  82. }
  83. $end = isset($this->post['end']) && $this->post['end']!="" ?$this->post['end']:"";
  84. if($end!=""){
  85. $where[]=['addtime',"<=", $end];
  86. }
  87. $last_start = isset($this->post['last_start']) && $this->post['last_start']!="" ?$this->post['last_start']:"";
  88. if($last_start!=""){
  89. //$where['lasttime'] = Db::raw(" >= '{$last_start}'");
  90. $where[]=['lasttime',">=", $last_start];
  91. }
  92. $last_end = isset($this->post['last_end']) && $this->post['last_end']!="" ?$this->post['last_end']:"";
  93. if($last_end!=""){
  94. //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
  95. $where[]=['lasttime',"<=", $last_end];
  96. }
  97. $count=Db::name("purchease_order")->where($where)->count();
  98. $total = ceil($count/$size);
  99. $page = $page >= $total ? $total : $page;
  100. $list = Db::name("purchease_order")->where($where)->page($page,$size)->order("addtime desc")->select();
  101. $data=[];
  102. foreach ($list as $value){
  103. $value['wsm_name']="";
  104. if($value['wsm_code']!=""){
  105. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  106. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  107. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  108. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  109. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  110. }
  111. $inorder= Db::name("purchease_in")->where(['cgdNo'=>$value['cgdNo'],"is_del"=>0])->select();
  112. $value['child']=empty($inorder)? [] : $inorder;
  113. $data[]=$value;
  114. }
  115. return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  116. }
  117. public function info(){
  118. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ?trim($this->post['cgdNo']):"";
  119. if($cgdNo==""){
  120. return error_show(1004,"参数cgdNo 不能为空");
  121. }
  122. $data = Db::name("purchease_order")->where(["cgdNo"=>$cgdNo,"is_del"=>0])->find();
  123. if(empty($data)){
  124. return error_show(1004,"未找到数据");
  125. }
  126. $data['wsm_name']="";
  127. if($data['wsm_code']!=""){
  128. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  129. ->leftJoin("warehouse_addr c","a.wsm_code=c.wsm_code and c.is_del=0")
  130. ->where(["a.wsm_code"=>$data['wsm_code']])->field("a.name as wsm_name,b.name,b.code,c.wsm_name as wsm_contactor,c.wsm_mobile,c.wsm_addr,c.addr_code")->find();
  131. $data['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  132. $data['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  133. $data['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  134. $data['wsm_contactor'] =isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor']:"";
  135. $data['wsm_mobile'] =isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile']:"";
  136. // $data['wsm_addr'] =isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  137. $data['addr_code'] =isset($wsmcode['addr_code']) ? $wsmcode['addr_code']:"";
  138. $data['wsm_addr'] =isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']):"";
  139. $data['wsm_addr'].=isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  140. $inorder= Db::name("purchease_in")->where(['cgdNo'=>$data['cgdNo'],"is_del"=>0])->select();
  141. $data['child']=empty($inorder)? [] : $inorder;
  142. }
  143. $feed = Db::name("purchease_feedback")->where(['bkcode'=>$data['bkcode'],'is_del'=>0])->find();
  144. $data['feedback'] = [];
  145. if(!empty($feed)){
  146. $feed['wsm_name']="";
  147. if($feed['warehouse']!=""){
  148. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  149. ->where(["a.wsm_code"=>$feed['warehouse']])->field("a.name as wsm_name,b.name,b.code")->find();
  150. $feed['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  151. $feed['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  152. $feed['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  153. }
  154. if($feed['supplier']!=""){
  155. $supplie = Db::name("supplier")->where(['code'=>$feed['supplier'],"is_del"=>0])->find();
  156. $feed['supplier_name'] =isset($supplie['name']) ? $supplie['name']:"";
  157. }
  158. $data['feedback'] = $feed;
  159. }
  160. return app_show(0,"获取成功",$data);
  161. }
  162. public function edit(){
  163. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ?trim($this->post['cgdNo']):"";
  164. if($cgdNo==""){
  165. return error_show(1004,"参数cgdNo 不能为空");
  166. }
  167. $data = Db::name("purchease_order")->where(["cgdNo"=>$cgdNo,"is_del"=>0])->find();
  168. if(empty($data)){
  169. return error_show(1004,"未找到数据");
  170. }
  171. $order = ["order_code"=>$cgdNo,"status"=> $data['status'] ,"action_remark"=>$this->post['remark'],"action_type"=>"edit"];
  172. $wsm_code = isset($this->post['wsm_code'])&&$this->post['wsm_code']!=""? trim($this->post['wsm_code']):"";
  173. if($wsm_code!=""){
  174. $ware = Db::name("warehouse_info")->where(["wsm_code"=>$wsm_code,"is_del"=>0])->find();
  175. if(empty($ware)){
  176. return error_show(1004,"未找到仓库信息");
  177. }
  178. // $good = Db::name("good_stock")->where(["wsm_code"=>$wsm_code,"good_type_code"=>$data['good_type_code'],"is_del"=>0])->find();
  179. // if(empty($good)){
  180. // return error_show(1004,"未找到仓库下商品信息");
  181. // }
  182. $data['wsm_code'] = $wsm_code;
  183. }
  184. $cgdid = isset($this->post['cgder_id'])&&$this->post['cgder_id']!=""?intval($this->post['cgder_id']):"";
  185. if($cgdid!=''){
  186. $cgduser = GetInfoById($this->post['token'],["id"=>$cgdid]);
  187. if((!empty($cgduser) && $cgduser['code']!=0) ||empty($cgduser) ){
  188. return error_show($cgduser['code'],$cgduser['message']);
  189. }
  190. $userinfo = $cgduser['data'];
  191. $data['cgder_id'] = $cgdid;
  192. $data['cgder'] = $userinfo['nickname'];
  193. }
  194. $good_num = isset($this->post['good_num'])&&$this->post['good_num']!=""?intval($this->post['good_num']):"";
  195. if($good_num!=""){
  196. $data['good_num'] = $good_num;
  197. }
  198. $good_price = isset($this->post['good_price'])&&$this->post['good_price']!=""?$this->post['good_price']:"";
  199. if($good_price!=""){
  200. $data['good_price'] = $good_price;
  201. }
  202. // $total_fee = isset($this->post['total_fee'])&&$this->post['total_fee']!=""?$this->post['total_fee']:"";
  203. // if($total_fee!=""){
  204. // $data['total_fee'] = $total_fee;
  205. // }
  206. $pakge_fee = isset($this->post['pakge_fee'])&&$this->post['pakge_fee']!=""?$this->post['pakge_fee']:"";
  207. if($pakge_fee!=""){
  208. $data['pakge_fee'] = $pakge_fee;
  209. }
  210. $cert_fee = isset($this->post['cert_fee'])&&$this->post['cert_fee']!=""?$this->post['cert_fee']:"";
  211. if($cert_fee!=""){
  212. $data['cert_fee'] = $cert_fee;
  213. }
  214. $open_fee = isset($this->post['open_fee'])&&$this->post['open_fee']!=""?$this->post['open_fee']:"";
  215. if($open_fee!=""){
  216. $data['open_fee'] = $open_fee;
  217. }
  218. $delivery_fee = isset($this->post['delivery_fee'])&&$this->post['delivery_fee']!=""?$this->post['delivery_fee']:"";
  219. if($delivery_fee!=""){
  220. $data['delivery_fee'] = $delivery_fee;
  221. }
  222. $mark_fee = isset($this->post['mark_fee'])&&$this->post['mark_fee']!=""?$this->post['mark_fee']:"";
  223. if($mark_fee!=""){
  224. $data['mark_fee'] = $mark_fee;
  225. }
  226. $teach_fee = isset($this->post['teach_fee'])&&$this->post['teach_fee']!=""?$this->post['teach_fee']:"";
  227. if($teach_fee!=""){
  228. $data['teach_fee'] = $teach_fee;
  229. }
  230. $nake_fee = isset($this->post['nake_fee'])&&$this->post['nake_fee']!=""?$this->post['nake_fee']:"";
  231. if($nake_fee!=""){
  232. $data['nake_fee'] = $nake_fee;
  233. }
  234. $weight = isset($this->post['weight'])&&$this->post['weight']!=""?$this->post['weight']:"";
  235. if($weight!=""){
  236. $data['weight'] = $weight;
  237. }
  238. $diff_weight = isset($this->post['diff_weight'])&&$this->post['diff_weight']!=""?$this->post['diff_weight']:"";
  239. if($diff_weight!=""){
  240. $data['diff_weight'] = $diff_weight;
  241. }
  242. $diff_fee = isset($this->post['diff_fee'])&&$this->post['diff_fee']!=""?$this->post['diff_fee']:"";
  243. if($diff_fee!=""){
  244. $data['diff_fee'] = $diff_fee;
  245. }
  246. $supplierNo = isset($this->post['supplierNo'])&&$this->post['supplierNo']!=""?trim($this->post['supplierNo'])
  247. :"";
  248. if($supplierNo!=""){
  249. $supplier =Db::name("supplier")->where(['code'=>$supplierNo])->find();
  250. if(empty($supplier)){
  251. return error_show(1004,"未找到供应商信息");
  252. }
  253. $data['supplierNo'] = $supplierNo;
  254. $data['supplier_name'] = $supplier['name'];
  255. }
  256. $remark = isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  257. if($remark!=""){
  258. $data['remark'] = $remark;
  259. }
  260. $data['good_price'] = round(($data['pakge_fee']+$data['weight']*$data['gold_price']+$data['nake_fee']+$data['mark_fee']+$data['cert_fee']+$data['open_fee']/$data['good_num']
  261. +$data['teach_fee']*$data['weight']+$data['delivery_fee']),2);
  262. $data['total_fee'] = round($data['good_price']*$data['good_num'],2);
  263. $data['updatetime'] =date("Y-m-d H:i:s");
  264. $upd=Db::name("purchease_order")->save($data);
  265. if($upd){
  266. ActionLog::logAdd($this->post['token'],$order,'cgd',$data['status'],$this->post);
  267. return app_show(0,"更新成功");
  268. }else{
  269. return error_show(1004,'更新失败');
  270. }
  271. }
  272. public function status(){
  273. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ?trim($this->post['cgdNo']):"";
  274. if($cgdNo==""){
  275. return error_show(1004,"参数cgdNo 不能为空");
  276. }
  277. $data = Db::name("purchease_order")->where(["cgdNo"=>$cgdNo,"is_del"=>0])->find();
  278. if(empty($data)){
  279. return error_show(1004,"未找到数据");
  280. }
  281. $remark= isset($this->post['remark']) && $this->post['remark']!="" ?trim($this->post['remark']):"";
  282. $status = isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  283. if($status===""){
  284. return error_show(1004,"参数status 不能为空");
  285. }
  286. if($data['send_status']>1 && $status==0){
  287. return error_show(1004,"采购单发货中无法取消");
  288. }
  289. $order = ["order_code"=>$cgdNo,"status"=> $data['status'] ,"action_remark"=>$remark,"action_type"=>"status"];
  290. $data['status'] = $status;
  291. $data['remark'] = $remark;
  292. $data['updatetime'] =date("Y-m-d H:i:s");
  293. $upd=Db::name("purchease_order")->save($data);
  294. if($upd){
  295. ActionLog::logAdd($this->post['token'],$order,'cgd',$status,$this->post);
  296. return app_show(0,"更新成功");
  297. }else{
  298. return error_show(1004,'更新失败');
  299. }
  300. }
  301. }