Purch.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\ProcessOrder;
  5. use think\App;
  6. use think\facade\Db;
  7. use think\facade\Validate;
  8. //采购单相关功能
  9. class Purch extends Base
  10. {
  11. public $noble=[];
  12. public function __construct(App $app)
  13. {
  14. parent::__construct($app);
  15. $this->noble=\think\facade\Config::get("noble");
  16. }
  17. public function list(){
  18. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  19. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  20. $where =[['is_del',"=",0]];
  21. $bkcode = isset($this->post['bk_code']) && $this->post['bk_code']!="" ? trim($this->post['bk_code']):"";
  22. if($bkcode!=""){
  23. $where[]=['bkcode',"like", "%{$bkcode}%"];
  24. }
  25. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  26. if($status!==""){
  27. // $where['status'] = $status;
  28. $where[]=['status',"=", $status];
  29. }
  30. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo'])
  31. :"";
  32. if($cgdNo!=""){
  33. //$where['cgdNo'] = Db::Raw("like '%{$cgdNo}%'");
  34. $where[]=['cgdNo',"like", "%{$cgdNo}%"];
  35. }
  36. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name']):"";
  37. if($apply_name!=""){
  38. // $where['cgder'] =Db::Raw("like '%{$apply_name}%'");
  39. $where[]=['cgder',"like", "%{$apply_name}%"];
  40. }
  41. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']):"";
  42. if($wsm_code!=""){
  43. // $where['wsm_code'] = $wsm_code;
  44. $where[]=['wsm_code',"=", $wsm_code];
  45. }
  46. $wsm_in_code = isset($this->post['wsm_in_code']) && $this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']):"";
  47. if($wsm_in_code!=""){
  48. $incode = Db::name("purchease_in")->where(["wsm_in_code"=>Db::raw(" like %{$wsm_in_code}%"),"is_del"=>0])->column("cgdNo");
  49. if(empty($incode)){
  50. return error_show(1004,"未找到有关入库单信息");
  51. }
  52. $where[]=['cgdNo',"in", $incode];
  53. }
  54. $good_name = isset($this->post['good_name']) && $this->post['good_name']!="" ? trim($this->post['good_name']):"";
  55. if($good_name!=""){
  56. //$where['good_name'] = Db::raw(" like %{$good_name}%");
  57. $where[]=['good_name',"like", "%{$good_name}%"];
  58. }
  59. $good_code = isset($this->post['good_code']) && $this->post['good_code']!="" ? trim($this->post['good_code'])
  60. :"";
  61. if($good_code!=""){
  62. // $where['good_code'] = Db::raw(" like %{$good_code}%");
  63. $where[]=['spuCode',"like", "%{$good_code}%"];
  64. }
  65. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo']!="" ? trim($this->post['supplierNo'])
  66. :"";
  67. if($supplierNo!=""){
  68. // $where['good_code'] = Db::raw(" like %{$good_code}%");
  69. $where[]=['supplierNo',"like", "%{$supplierNo}%"];
  70. }
  71. $wsm_supplierNo = isset($this->post['wsm_supplierNo']) && $this->post['wsm_supplierNo']!="" ? trim($this->post['wsm_supplierNo'])
  72. :"";
  73. if($wsm_supplierNo!=""){
  74. $supplier = Db::name("supplier")->where(["code"=>$wsm_supplierNo])->find();
  75. if(empty($supplier)){
  76. return error_show(1004,"未找到供应商信息");
  77. }
  78. $wsmcode = Db::name("warehouse_info")->where(["is_del"=>0,"supplierNo"=>$wsm_supplierNo])->column("wsm_code");
  79. $where[]=['wsm_code',"in", $wsmcode];
  80. }
  81. $start = isset($this->post['start']) && $this->post['start']!="" ?$this->post['start']:"";
  82. if($start!=""){
  83. // $where['addtime'] = Db::raw(" >= '{$start}'");
  84. $where[]=['addtime',">=", $start];
  85. }
  86. $end = isset($this->post['end']) && $this->post['end']!="" ?$this->post['end']:"";
  87. if($end!=""){
  88. $where[]=['addtime',"<=", $end];
  89. }
  90. $last_start = isset($this->post['last_start']) && $this->post['last_start']!="" ?$this->post['last_start']:"";
  91. if($last_start!=""){
  92. //$where['lasttime'] = Db::raw(" >= '{$last_start}'");
  93. $where[]=['lasttime',">=", $last_start];
  94. }
  95. $last_end = isset($this->post['last_end']) && $this->post['last_end']!="" ?$this->post['last_end']:"";
  96. if($last_end!=""){
  97. //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
  98. $where[]=['lasttime',"<=", $last_end];
  99. }
  100. $role=$this->checkRole();
  101. if(!empty($role['write'])){
  102. $where[]=["cgder_id","in",$role['write']];
  103. }
  104. $count=Db::name("purchease_order")->where($where)->count();
  105. $total = ceil($count/$size);
  106. $page = $page >= $total ? $total : $page;
  107. $list = Db::name("purchease_order")->where($where)->page($page,$size)->order("addtime desc")->select();
  108. $data=[];
  109. foreach ($list as $value){
  110. $value['wsm_name']="";
  111. if($value['wsm_code']!=""){
  112. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  113. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  114. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  115. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  116. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  117. }
  118. if($value['order_type']==3|| $value['order_type']==4){
  119. $goon = Db::name("good_zixun")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->find();
  120. $value["speclist"]=isset($goon['specinfo'])&&$goon['specinfo']!=""? json_decode($goon['specinfo'],true):"";
  121. }else {
  122. $goon =Db::name('good_basic')->where(['spuCode'=>$value['spuCode']])->find();
  123. $spec = Db::name("good_spec")->where(["spuCode"=>$goon['spuCode'],"is_del"=>0])->select()->toArray();
  124. $speclist=[];
  125. if(!empty($spec)){
  126. foreach ($spec as $val){
  127. $temp=[];
  128. $temp['id']=$val['id'];
  129. $temp['spuCode']=$val['spuCode'];
  130. $temp['spec_id']=$val['spec_id'];
  131. $temp['spec_value_id']=$val['spec_value_id'];
  132. $sp = Db::name("specs")->where(["id"=>$val['spec_id']])->find();
  133. $temp['spec_name']=isset($sp["spec_name"]) ? $sp["spec_name"]:"";
  134. $spv = Db::name("spec_value")->where(["id"=>$val['spec_value_id']])->find();
  135. $temp['spec_value']=isset($spv["spec_value"]) ? $spv["spec_value"]:"";
  136. $speclist[]=$temp;
  137. }
  138. }
  139. $value["speclist"]=empty($speclist)?[]:$speclist;
  140. }
  141. //采购单详情字段order_type 为1或者2时,取线上商品库 商品创建人 order_type 为3或者4时 取采购反馈的商品库 商品反馈人
  142. // $value['cgder_id'] = $goon['createrid'];
  143. // $value['cgder'] = $goon['creater'];
  144. $inorder= Db::name("purchease_in")->where(['cgdNo'=>$value['cgdNo'],"is_del"=>0])->select();
  145. $value['child']=empty($inorder)? [] : $inorder;
  146. $data[]=$value;
  147. }
  148. return app_show(0,"获取成功",["list"=>$data ,"count"=>$count]);
  149. }
  150. public function info(){
  151. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ?trim($this->post['cgdNo']):"";
  152. if($cgdNo==""){
  153. return error_show(1004,"参数cgdNo 不能为空");
  154. }
  155. //采购单
  156. $data = Db::name("purchease_order")->where(["cgdNo"=>$cgdNo,"is_del"=>0])->find();
  157. if(empty($data)){
  158. return error_show(1004,"未找到数据");
  159. }
  160. //采购退货单
  161. $im = Db::name('purchease_back')->where(['cgdNo'=>$data['cgdNo'],'is_del'=>0])->select();
  162. $var=[];
  163. foreach ($im as $value){
  164. if($data['order_type']==3|| $data['order_type']==4){
  165. $goo = Db::name("good_zixun")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->find();
  166. }else {
  167. $goo =Db::name('good_basic')->where(['spuCode'=>$value['spuCode']])->find();
  168. }
  169. if($goo==false) {
  170. return error_show(1002, "未找到商品数据");
  171. }
  172. $cat= isset($goo['cat_id']) && $goo['cat_id'] !=0 ? made($goo['cat_id']):[];
  173. $value['cant']=$cat;
  174. $var[]=$value;
  175. }
  176. //采购工差单
  177. $dom = Db::name("purchease_diff")->where(['cgdNo'=>$data['cgdNo'],'is_del'=>0])->find();
  178. if($data['order_type']==3|| $data['order_type']==4){
  179. $goon = Db::name("good_zixun")->where(["spuCode"=>$data['spuCode'],"is_del"=>0])->find();
  180. }else {
  181. $goon =Db::name('good_basic')->where(['spuCode'=>$data['spuCode']])->find();
  182. }
  183. if(empty($goon)){
  184. return error_show(1002,"未找到商品数据");
  185. }else{
  186. $goon['exclusive']=isset($goon['is_exclusive'])?makeExcluse($goon['is_exclusive']):"";
  187. $unit =Db::name("unit")->where(["id"=>$goon['good_unit']])->find();
  188. $goon['unit'] = isset($unit['unit'])?$unit['unit']:'';
  189. $spec = Db::name("good_spec")->where(["spuCode"=>$goon['spuCode'],"is_del"=>0])->select()->toArray();
  190. $speclist=[];
  191. if(!empty($spec)){
  192. foreach ($spec as $value){
  193. $temp=[];
  194. $temp['id']=$value['id'];
  195. $temp['spuCode']=$value['spuCode'];
  196. $temp['spec_id']=$value['spec_id'];
  197. $temp['spec_value_id']=$value['spec_value_id'];
  198. $temp['is_del']=$value['is_del'];
  199. $sp = Db::name("specs")->where(["id"=>$value['spec_id']])->find();
  200. $temp['spec_name']=isset($sp["spec_name"]) ? $sp["spec_name"]:"";
  201. $spv = Db::name("spec_value")->where(["id"=>$value['spec_value_id']])->find();
  202. $temp['spec_value']=isset($spv["spec_value"]) ? $spv["spec_value"]:"";
  203. $speclist[]=$temp;
  204. }
  205. }
  206. $goon["speclist"]=empty($speclist)?[]:$speclist;
  207. $proof =Db::name("good_proof")->where(["spuCode"=>$goon['spuCode'],"is_del"=>0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
  208. $goon['proof'] = isset($proof)&&$proof!=false? $proof:[];
  209. $goon['origin_place_cn']="";
  210. $goon['delivery_place_cn']="";
  211. if(isset($goon['delivery_place'])&&$goon['delivery_place']!==""){
  212. $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
  213. list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$goon['delivery_place']);
  214. $goon['delivery_place_cn']=GetAddr(json_encode($place));
  215. }
  216. if(isset($goon['delivery_place'])&&$goon['origin_place']!==""){
  217. $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
  218. list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$goon['origin_place']);
  219. $goon['origin_place_cn']=GetAddr(json_encode($place));
  220. }
  221. if($goon['brand_id']!=0){
  222. $brand=Db::name("brand")->where(["id"=>$goon['brand_id']])->find();
  223. $goon["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
  224. }else{
  225. $goon["brand_name"]="";
  226. $goon["brand_id"]="";
  227. }
  228. $supplier = Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  229. $goon['supplierName'] = isset($supplier['name'])?$supplier['name']:"";
  230. $goon['noble_name']=isset($goon['noble_metal'])&&$goon['noble_metal']!=0?$this->noble[$goon['noble_metal']] :"";
  231. if(isset($goon['companyNo'])&&$goon['companyNo']!=""){
  232. $company = Db::name("business")->where(["companyNo"=>$goon['companyNo']])->find();
  233. }
  234. $goon['company'] = isset($company['company'])?$company['company']:"";
  235. }
  236. $int= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
  237. $data['wsm_name']="";
  238. if($data['wsm_code']!=""){
  239. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  240. ->leftJoin("warehouse_addr c","a.wsm_code=c.wsm_code and c.is_del=0")
  241. ->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();
  242. $data['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  243. $data['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  244. $data['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  245. $data['wsm_contactor'] =isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor']:"";
  246. $data['wsm_mobile'] =isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile']:"";
  247. // $data['wsm_addr'] =isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  248. $data['addr_code'] =isset($wsmcode['addr_code']) ? $wsmcode['addr_code']:"";
  249. $data['wsm_addr'] =isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']):"";
  250. $data['wsm_addr'].=isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  251. $inorder= Db::name("purchease_in")->where(['cgdNo'=>$data['cgdNo'],"is_del"=>0])->select();
  252. $data['child']=empty($inorder)? [] : $inorder;
  253. }
  254. $data['can'] = $int;
  255. $data['goodinfo'] = $goon;
  256. $data['purcheasediff'] = $dom;
  257. if(isset($data['companyNo'])&&$data['companyNo']!=""){
  258. $company = Db::name("business")->where(["companyNo"=>$data['companyNo']])->find();
  259. }
  260. $data['company'] = isset($company['company'])?$company['company']:"";
  261. //$data['info'] = $var;
  262. $data['purcheaseback'] = $var;
  263. return app_show(0,"获取成功",$data);
  264. }
  265. public function edit(){
  266. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ?trim($this->post['cgdNo']):"";
  267. if($cgdNo==""){
  268. return error_show(1004,"参数cgdNo 不能为空");
  269. }
  270. $data = Db::name("purchease_order")->where(["cgdNo"=>$cgdNo,"is_del"=>0])->find();
  271. if(empty($data)){
  272. return error_show(1004,"未找到数据");
  273. }
  274. $order = ["order_code"=>$cgdNo,"status"=> $data['status'] ,"action_remark"=>$this->post['remark'],"action_type"=>"edit"];
  275. $wsm_code = isset($this->post['wsm_code'])&&$this->post['wsm_code']!=""? trim($this->post['wsm_code']):"";
  276. if($wsm_code!=""){
  277. $ware = Db::name("warehouse_info")->where(["wsm_code"=>$wsm_code,"is_del"=>0])->find();
  278. if(empty($ware)){
  279. return error_show(1004,"未找到仓库信息");
  280. }
  281. // $good = Db::name("good_stock")->where(["wsm_code"=>$wsm_code,"good_type_code"=>$data['good_type_code'],"is_del"=>0])->find();
  282. // if(empty($good)){
  283. // return error_show(1004,"未找到仓库下商品信息");
  284. // }
  285. $data['wsm_code'] = $wsm_code;
  286. }
  287. $cgdid = isset($this->post['cgder_id'])&&$this->post['cgder_id']!=""?intval($this->post['cgder_id']):"";
  288. if($cgdid!=''){
  289. $cgduser = GetInfoById($this->post['token'],["id"=>$cgdid]);
  290. if((!empty($cgduser) && $cgduser['code']!=0) ||empty($cgduser) ){
  291. return error_show($cgduser['code'],$cgduser['message']);
  292. }
  293. $userinfo = $cgduser['data'];
  294. $data['cgder_id'] = $cgdid;
  295. $data['cgder'] = $userinfo['nickname'];
  296. }
  297. $good_num = isset($this->post['good_num'])&&$this->post['good_num']!=""?intval($this->post['good_num']):"";
  298. if($good_num!=""){
  299. $data['good_num'] = $good_num;
  300. }
  301. $good_price = isset($this->post['good_price'])&&$this->post['good_price']!=""?$this->post['good_price']:"";
  302. if($good_price!=""){
  303. $data['good_price'] = $good_price;
  304. }
  305. // $total_fee = isset($this->post['total_fee'])&&$this->post['total_fee']!=""?$this->post['total_fee']:"";
  306. // if($total_fee!=""){
  307. // $data['total_fee'] = $total_fee;
  308. // }
  309. $pakge_fee = isset($this->post['pakge_fee'])&&$this->post['pakge_fee']!=""?$this->post['pakge_fee']:"";
  310. if($pakge_fee!=""){
  311. $data['pakge_fee'] = $pakge_fee;
  312. }
  313. $cert_fee = isset($this->post['cert_fee'])&&$this->post['cert_fee']!=""?$this->post['cert_fee']:"";
  314. if($cert_fee!=""){
  315. $data['cert_fee'] = $cert_fee;
  316. }
  317. $open_fee = isset($this->post['open_fee'])&&$this->post['open_fee']!=""?$this->post['open_fee']:"";
  318. if($open_fee!=""){
  319. $data['open_fee'] = $open_fee;
  320. }
  321. $delivery_fee = isset($this->post['delivery_fee'])&&$this->post['delivery_fee']!=""?$this->post['delivery_fee']:"";
  322. if($delivery_fee!=""){
  323. $data['delivery_fee'] = $delivery_fee;
  324. }
  325. $mark_fee = isset($this->post['mark_fee'])&&$this->post['mark_fee']!=""?$this->post['mark_fee']:"";
  326. if($mark_fee!=""){
  327. $data['mark_fee'] = $mark_fee;
  328. }
  329. $teach_fee = isset($this->post['teach_fee'])&&$this->post['teach_fee']!=""?$this->post['teach_fee']:"";
  330. if($teach_fee!=""){
  331. $data['teach_fee'] = $teach_fee;
  332. }
  333. $nake_fee = isset($this->post['nake_fee'])&&$this->post['nake_fee']!=""?$this->post['nake_fee']:"";
  334. if($nake_fee!=""){
  335. $data['nake_fee'] = $nake_fee;
  336. }
  337. $weight = isset($this->post['weight'])&&$this->post['weight']!=""?$this->post['weight']:"";
  338. if($weight!=""){
  339. $data['weight'] = $weight;
  340. }
  341. $diff_weight = isset($this->post['diff_weight'])&&$this->post['diff_weight']!=""?$this->post['diff_weight']:"";
  342. if($diff_weight!=""){
  343. $data['diff_weight'] = $diff_weight;
  344. }
  345. $diff_fee = isset($this->post['diff_fee'])&&$this->post['diff_fee']!=""?$this->post['diff_fee']:"";
  346. if($diff_fee!=""){
  347. $data['diff_fee'] = $diff_fee;
  348. }
  349. $supplierNo = isset($this->post['supplierNo'])&&$this->post['supplierNo']!=""?trim($this->post['supplierNo'])
  350. :"";
  351. if($supplierNo!=""){
  352. $supplier =Db::name("supplier")->where(['code'=>$supplierNo])->find();
  353. if(empty($supplier)){
  354. return error_show(1004,"未找到供应商信息");
  355. }
  356. $data['supplierNo'] = $supplierNo;
  357. $data['supplier_name'] = $supplier['name'];
  358. }
  359. $remark = isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  360. if($remark!=""){
  361. $data['remark'] = $remark;
  362. }
  363. $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']
  364. +$data['teach_fee']*$data['weight']+$data['delivery_fee']),2);
  365. $data['total_fee'] = round($data['good_price']*$data['good_num'],2);
  366. $data['updatetime'] =date("Y-m-d H:i:s");
  367. $upd=Db::name("purchease_order")->save($data);
  368. if($upd){
  369. // ActionLog::logAdd($this->post['token'],$order,'cgd',$data['status'],$this->post);
  370. //修改状态,添加待办
  371. ActionLog::logAdd($this->post['token'], $order, "CGD", $data['status'], $this->post);
  372. ProcessOrder::AddProcess($this->post['token'], [
  373. "order_type" => 'CGD',
  374. "order_code" => $cgdNo,//销售单code
  375. "order_id" => $data['id'],
  376. "order_status" => $data['status']
  377. ]);
  378. return app_show(0,"更新成功");
  379. }else{
  380. return error_show(1004,'更新失败');
  381. }
  382. }
  383. public function status(){
  384. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ?trim($this->post['cgdNo']):"";
  385. if($cgdNo==""){
  386. return error_show(1004,"参数cgdNo 不能为空");
  387. }
  388. $data = Db::name("purchease_order")->where(["cgdNo"=>$cgdNo,"is_del"=>0])->find();
  389. if(empty($data)){
  390. return error_show(1004,"未找到数据");
  391. }
  392. $remark= isset($this->post['remark']) && $this->post['remark']!="" ?trim($this->post['remark']):"";
  393. $status = isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  394. if($status===""){
  395. return error_show(1004,"参数status 不能为空");
  396. }
  397. if($data['send_status']>1 && $status==0){
  398. return error_show(1004,"采购单发货中无法取消");
  399. }
  400. $order = ["order_code"=>$cgdNo,"status"=> $data['status'] ,"action_remark"=>$remark,"action_type"=>"status"];
  401. $data['status'] = $status;
  402. $data['remark'] = $remark;
  403. $data['updatetime'] =date("Y-m-d H:i:s");
  404. $upd=Db::name("purchease_order")->save($data);
  405. if($upd){
  406. $process=["order_code"=>$cgdNo,"order_id"=>$data['id'],"order_status"=>$status,"order_type"=>'CGD'];
  407. ProcessOrder::AddProcess($this->post['token'],$process);
  408. ActionLog::logAdd($this->post['token'],$order,'CGD',$status,$this->post);
  409. return app_show(0,"更新成功");
  410. }else{
  411. return error_show(1004,'更新失败');
  412. }
  413. }
  414. public function diffcreat(){
  415. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  416. //$cgdNo=makeNo("CG");
  417. $cgdNo = $this->post['cgdNo'] && $this->post['cgdNo'] !=="" ? trim($this->post['cgdNo']) :"";
  418. if($cgdNo==""){
  419. return error_show(1002,"参数good_code不能为空");
  420. }
  421. $cg = Db::name('purchease_order')->where(['cgdNo'=>$cgdNo,'is_del'=>0])->find();
  422. if($cg==""){
  423. return error_show(1002,"未找到采购单数据");
  424. }
  425. if($cg['order_type']==3|| $cg['order_type']==4){
  426. $gd = Db::name("good_zixun")->where(["spuCode"=>$cg['spuCode']])->find();
  427. }else {
  428. $gd =Db::name('good_basic')->where(['spuCode'=>$cg['spuCode']])->find();
  429. }
  430. if($gd==""){
  431. return error_show(1002,"未找到商品数据");
  432. }
  433. $diff_weight = isset($this->post['diff_weight']) && $this->post['diff_weight'] !=="" ? floatval($this->post['diff_weight']) :"";
  434. if($diff_weight===""){
  435. return error_show(1002,"参数diff_weight不能为空");
  436. }
  437. $apply_id =GetUserInfo($token);
  438. if(empty($apply_id)||$apply_id['code']!=0){
  439. return error_show(1002,"申请人数据不存在");
  440. }
  441. $rid= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  442. $rname= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  443. $diff_price = round($diff_weight*$cg['gold_price']+$diff_weight*$cg['teach_fee'],2);
  444. $data=[
  445. "cgdNo"=>$cgdNo,
  446. "good_code"=>$cg['spuCode'],
  447. "good_name"=>$cg['good_name'],
  448. "sale_price"=>$cg['good_price'],
  449. "good_weight"=>$cg['weight']*$cg['good_num'],
  450. "good_num"=>$cg['good_num'],
  451. "apply_id"=>$rid,
  452. "apply_name"=>$rname,
  453. "diff_weight"=>$diff_weight,
  454. "diff_price"=>$diff_price,
  455. "gold_price"=>$cg['good_price'],
  456. "status"=>1,
  457. "is_del"=>0,
  458. "addtime"=>date("Y-m-d H:i:s"),
  459. "updatetime"=>date("Y-m-d H:i:s")
  460. ];
  461. Db::startTrans();
  462. try{
  463. $item = Db::name("purchease_diff")->insert($data,true);
  464. if($item>0){
  465. $orderCode = Db::name("order_num")->where([["cgdNo","=",$cg['cgdNo']],["status","=",1]])->find();
  466. if($orderCode!=false){
  467. $order = Db::name("sale")->where("orderCode", '=', $orderCode['orderCode'])->find();
  468. if($order==false){
  469. Db::rollback();
  470. return error_show(1002,"未找到确认单信息");
  471. }
  472. $data=[
  473. "cgd_diffid"=>$item,
  474. "orderCode"=>$orderCode['orderCode'],
  475. "good_code"=>$cg['spuCode'],
  476. "good_name"=>$cg['good_name'],
  477. "diff_weight"=>$diff_weight,
  478. "diff_price"=>round($diff_weight*$order['gold_price']+$diff_weight*$order['cost_price'],2),
  479. "gold_price"=>$order['sale_price'],
  480. "status"=>1,
  481. "addtime"=>date("Y-m-d H:i:s"),
  482. "updatetime"=>date("Y-m-d H:i:s")
  483. ];
  484. $cgd = Db::name('sale_diff')->insert($data,true);
  485. if($cgd==0){
  486. // $sto = ["order_code"=>$cg['bkcode'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  487. // ActionLog::logAdd($this->post['token'],$sto,"ZXGCD",1,$sto);
  488. // $process=["order_code"=>$cg['bkcode'],"order_id"=>$cgd,"order_status"=>1,"order_type"=>'ZXGCD'];
  489. // ProcessOrder::AddProcess($this->post['token'],$process);
  490. // Db::commit();
  491. // return error_show(0,"新建成功");
  492. // }else{
  493. Db::rollback();
  494. return error_show(1002,"新建失败");
  495. }
  496. }
  497. Db::commit();
  498. return error_show(0,"新建成功");
  499. }else{
  500. Db::rollback();
  501. return error_show(1002,"新建失败");
  502. }
  503. }catch (\Exception $e){
  504. Db::rollback();
  505. return error_show(1002,$e->getMessage());
  506. }
  507. }
  508. public function difflist(){
  509. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  510. $size = isset($this->post['size']) && $this->post['size'] !==""? intval($this->post['size']):"10";
  511. $where = [['a.is_del',"=",0]];
  512. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo']):"";
  513. if($cgdNo!=""){
  514. $where[]=['a.cgdNo',"like", "%$cgdNo%"];
  515. }
  516. $status = isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']):"";
  517. if($status!==""){
  518. $where[]=['a.status',"=",$status];
  519. }
  520. $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
  521. if ($start !="") {
  522. $where[]= ["a.addtime",'>=',$start];
  523. }
  524. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
  525. if($end !=""){
  526. $where[]= ["a.addtime",'<=',$end];
  527. }
  528. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !==""? intval($this->post['apply_name']):"";
  529. if($apply_name!==""){
  530. $where[]=['a.apply_name',"like","%$apply_name%"];
  531. }
  532. $count = Db::name('purchease_diff')->alias('a')->join("good b","b.spuCode=a.good_code","left")
  533. ->where($where)->count();
  534. $total = ceil($count/$size);
  535. $page = $page >= $total ? $total : $page;
  536. $list = Db::name('purchease_diff')->alias('a')->join("good b","b.spuCode=a.good_code","left")
  537. ->where($where)->page($page,$size)->order("a.addtime desc")->field("a.*,b.cat_id")->select();
  538. $data=[];
  539. foreach ($list as $value){
  540. $value['can']= isset($value['cat_id']) && $value['cat_id'] !=0 ? made($value['cat_id']):[];
  541. $data[]=$value;
  542. }
  543. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  544. }
  545. public function diffinfo(){
  546. $id = isset($this->post['id']) && $this->post['id'] !=="" ? trim($this->post['id']) :"";
  547. if($id==""){
  548. return error_show(1002,"参数id 不能为空");
  549. }
  550. $idinf = Db::name('purchease_diff')->where(['id'=>$id,'is_del'=>0])->find();
  551. if($idinf==false){
  552. return error_show(1002,"未找到采购工差单数据");
  553. }
  554. $dn = Db::name('purchease_order')->where(['cgdNo'=>$idinf['cgdNo'],'is_del'=>0])->find();
  555. if($dn['order_type']==3|| $dn['order_type']==4){
  556. $goon = Db::name("good_zixun")->where(["spuCode"=>$dn['spuCode'],"is_del"=>0])->find();
  557. }else {
  558. $goon =Db::name('good_basic')->where(['spuCode'=>$dn['spuCode']])->find();
  559. }
  560. // $goon = Db::name('good')->where(['spuCode'=>$idinf['good_code']])->find();
  561. if(empty($goon)){
  562. return error_show(1002,"未找到商品数据");
  563. }
  564. $idinf['wsm_name'] ="";
  565. $idinf['wsm_supplier'] ="";
  566. $idinf['wsm_supplierNo'] ="";
  567. $idinf['wsm_contactor'] ="";
  568. $idinf['wsm_mobile'] ="";
  569. $idinf['addr_code'] ="";
  570. $idinf['wsm_addr'] ="";
  571. if($dn['wsm_code']!=""){
  572. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  573. ->leftJoin("warehouse_addr c","a.wsm_code=c.wsm_code and c.is_del=0")
  574. ->where(["a.wsm_code"=>$dn['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();
  575. $idinf['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  576. $idinf['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  577. $idinf['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  578. $idinf['wsm_contactor'] =isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor']:"";
  579. $idinf['wsm_mobile'] =isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile']:"";
  580. $idinf['addr_code'] =isset($wsmcode['addr_code']) ? $wsmcode['addr_code']:"";
  581. $idinf['addr_cn'] =isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']):"";
  582. $idinf['wsm_addr']=isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  583. }
  584. $idinf['info'] =$dn;
  585. $int= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
  586. $idinf['can']=$int;
  587. $idinf['order_type']=$dn['order_type'];
  588. //取出销售的工差单信息
  589. $diff = Db::name('sale_diff')->field('id,customer_remark,is_act,remark')->where('cgd_diffid',$id)->find();
  590. $idinf['diff_customer_remark']=$diff['customer_remark'];
  591. $idinf['diff_is_act']=$diff['is_act'];
  592. $idinf['diff_remark']=$diff['remark'];
  593. return app_show(0,"获取成功",$idinf);
  594. }
  595. public function diffstatu(){
  596. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  597. if($id==""){
  598. return error_show(1002,"参数id不能为空");
  599. }
  600. $dio = Db::name('purchease_diff')->where(['id'=>$id,'is_del'=>0])->find();
  601. if(empty($dio)){
  602. return error_show(1002,"工差订单未找到");
  603. }
  604. $cgd = Db::name("purchease_order")->where(['cgdNo'=>$dio['cgdNo']])->find();
  605. if(empty($cgd)){
  606. return error_show(1002,"采购单未找到");
  607. }
  608. $salediff=Db::name("sale_diff")->where(["cgd_diffid"=>$dio['id']])->find();
  609. if($salediff==false){
  610. return error_show(1002,"销售工差单未找到");
  611. }
  612. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  613. if($status===""){
  614. return error_show(1002,"参数status不能为空");
  615. }
  616. Db::startTrans();
  617. try{
  618. $temp = $dio['status'];
  619. $dio['status']=$status;
  620. $dio['updatetime']=date("Y-m-d H:i:s");
  621. $item = Db::name("purchease_diff")->save($dio);
  622. if ($item){
  623. if($status==2){
  624. $cgd['diff_fee'] =$dio['diff_price'];
  625. $cgd['diff_weight'] =$dio['diff_weight'];
  626. $cgd['updatetime'] =date("Y-m-d H:i:s");
  627. $cgp=Db::name("purchease_order")->save($cgd);
  628. if($cgp==false){
  629. Db::rollback();
  630. return error_show(1002,"采购单更新失败");
  631. }
  632. if($salediff['is_act']==1){
  633. $saled=[
  634. "diff_fee"=>$salediff['diff_price'],
  635. "diff_weight"=>$salediff['diff_weight'],
  636. "updatetime"=>date("Y-m-d H:i:s")
  637. ];
  638. $up =Db::name("sale")->where(["orderCode"=>$salediff['orderCode']])->save($saled);
  639. if($up==false){
  640. Db::rollback();
  641. return error_show(1002,"销售单更新失败");
  642. }
  643. }
  644. }
  645. Db::commit();
  646. return error_show(0,"更新成功");
  647. }else{
  648. Db::rollback();
  649. return error_show(1002,"更新失败");
  650. }
  651. }catch (\Exception $e){
  652. Db::rollback();
  653. return error_show(1002,$e->getMessage()."|".$e->getLine());
  654. }
  655. }
  656. //修改采购单的实时金价
  657. public function editGoldPrice()
  658. {
  659. $param = $this->request->only(['cgdNo', 'gold_price'], 'post', 'trim');
  660. $val = Validate::rule(['cgdNo|采购单号' => 'require', 'gold_price|实时金价' => 'require|float']);
  661. if (!$val->check($param)) return error_show(1004, $val->getError());
  662. //采购单
  663. $data = Db::name("purchease_order")
  664. ->field('id,demo_fee,good_num,weight,open_fee,teach_fee,pakge_fee,mark_fee,cert_fee,nake_fee,delivery_fee,status')
  665. ->where(["cgdNo" => $param['cgdNo'], "is_del" => 0])
  666. ->find();
  667. if (empty($data)) return error_show(1004, "未找到该采购单数据");
  668. if ($data['status'] != 0) return error_show(1005, '该状态下不允许修改实时金价');
  669. $sale_price = round($data['demo_fee'] / $data['good_num'] + $data['open_fee'] / $data['good_num'] +
  670. $data['weight'] * $param["gold_price"] + $data['teach_fee'] * $data['weight'] +
  671. $data['pakge_fee'] + $data['mark_fee'] + $data['cert_fee'] +$data['nake_fee'] + $data['delivery_fee'],2);
  672. $total_price = round($data['good_num']*$sale_price,2);
  673. $rs = Db::name("purchease_order")
  674. ->where('id', $data['id'])
  675. ->update(['updatetime' => date('Y-m-d H:i:s'), 'gold_price' => $param['gold_price'],
  676. "good_price"=>$sale_price,"total_fee"=>$total_price]);
  677. return $rs ? app_show(0, '修改采购单实时金价成功') : error_show(1005, '修改采购单实时金价失败');
  678. }
  679. //采购单导出
  680. public function exportCgdList()
  681. {
  682. $cgdNos = $this->request->post('cgdNos', [], 'trim');
  683. if (empty($cgdNos)) return error_show(1004, '要导出的采购单编号不能为空');
  684. $i = 1;
  685. $send_type = [1 => '直接发货', 2 => '延时发货'];
  686. $status = [0 => '待与供应商确认', 1 => '待入库', 2 => '部分入库', 3 => '入库完成', 4 => '已取消订单'];
  687. $list = Db::name("purchease_order")
  688. ->alias('po')
  689. ->field('"" as 序号,po.addtime as 创建时间,po.cgdNo as 采购单编号,po.status as 采购单状态,po.cgder as 采购员,s.orderCode as 确认单号,s.addtime as 确认单时间,po.spuCode as 产品编号,po.good_name as 产品名称,"" as 规格,"" as 单位,po.supplier_name as 供应商名称,po.supplierNo as 供应商编号,po.nake_fee as 裸价,po.delivery_fee as 物流费,s.send_type as 发货方式,po.good_price as 采购单价,s.remark as 确认单备注,po.good_num as 采购数量,po.total_fee as 采购货款,b.company as 购买方公司,s.arrive_time as 到货时间,po.order_type')
  690. ->whereIn('po.cgdNo', $cgdNos)
  691. ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
  692. ->leftJoin('sale s', 's.orderCode=on.orderCode')
  693. ->leftJoin('business b', 'b.companyNo=s.supplierNo')
  694. ->order("po.addtime desc")
  695. ->withAttr('序号', function () use (&$i) {
  696. return $i++;
  697. })->withAttr('采购单状态', function ($val) use ($status) {
  698. return isset($status[$val]) ? $status[$val] : '';
  699. })->withAttr('发货方式', function ($val) use ($send_type) {
  700. return isset($send_type[$val]) ? $send_type[$val] : '';
  701. })
  702. ->select()
  703. ->toArray();
  704. foreach ($list as &$value) {
  705. if ($value['order_type'] == 3 || $value['order_type'] == 4) {
  706. $temp = Db::name("good_zixun")
  707. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  708. ->field('id,specinfo,good_unit')
  709. ->find();
  710. $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0;
  711. $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : [];
  712. $speclist = [];
  713. foreach ($specinfo as $val) {
  714. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  715. }
  716. } else {
  717. $good_unit = Db::name('good_basic')
  718. ->where(['spuCode' => $value['产品编号']])
  719. ->value('good_unit', 0);
  720. $spec = Db::name("good_spec")
  721. ->field('id,spec_id,spec_value_id')
  722. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  723. ->select()
  724. ->toArray();
  725. $speclist = [];
  726. if (!empty($spec)) {
  727. foreach ($spec as $val) {
  728. $speclist[] = Db::name("specs")->where(["id" => $val['spec_id']])->value('spec_name', '') . ':' . Db::name("spec_value")->where(["id" => $val['spec_value_id']])->value('spec_value', '');
  729. }
  730. }
  731. }
  732. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  733. $value['单位'] = $good_unit ? Db::name('unit')->where(['id' => $good_unit, 'is_del' => 0])->value('unit', '') : '';
  734. unset($value['order_type']);
  735. }
  736. $headerArr = array_keys($list[0]);
  737. excelSave('采购单导出' . date('YmdHis'), $headerArr, $list);
  738. }
  739. // public function create(){
  740. // $customer = isset($this->post['customer_code'])&&$this->post['customer_code']!="" ?trim($this->post['customer_code']):"";
  741. // if($customer==""){
  742. // return error_show(1004,"参数customer_code不能为空");
  743. // }
  744. // $supplier = isset($this->post['supplier_code'])&&$this->post['supplier_code']!="" ?trim($this->post['supplier_code']):"";
  745. // if($supplier==""){
  746. // return error_show(1004,"参数supplier_code不能为空");
  747. // }
  748. // $good_code = isset($this->post['good_code']) && $this->post['good_code']!=""?trim($this->post['good_code']):"";
  749. // if($good_code==""){
  750. // return error_show(1004,"参数good_code不能为空");
  751. // }
  752. // $good_num = isset($this->post['good_num'])&& $this->post['good_num']!==""? intval($this->post['good_num']):"";
  753. // if($good_num===""){
  754. // return error_show(1004,"参数good_code不能为空");
  755. // }
  756. // $file_url = isset($this->post['file_url'])&& $this->post['file_url']!==""? trim($this->post['file_url']):"";
  757. // if($file_url===""){
  758. // return error_show(1004,"参数file_url不能为空");
  759. // }
  760. // $mark =isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  761. // if($mark==""){
  762. // return error_show(1004,"参数remark不能为空");
  763. // }
  764. //
  765. // $data=[
  766. // ""
  767. // ];
  768. // }
  769. }