Purch.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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], ['order_type', '<>', 5]];//不显示类型为5即订单导入生成的采购单
  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. {
  385. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? $this->post['cgdNo'] : [];
  386. if (empty($cgdNo)) {
  387. return error_show(1004, "参数cgdNo 不能为空");
  388. }
  389. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  390. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  391. if ($status === "") {
  392. return error_show(1004, "参数status 不能为空");
  393. }
  394. if ($status == 0) {
  395. $data = Db::name("purchease_order")
  396. ->field('id,cgdNo')
  397. ->whereIn('cgdNo', $cgdNo)
  398. ->where(["is_del" => 0])
  399. ->where('send_status', '>', 1)
  400. ->find();
  401. if (!empty($data)) {
  402. return error_show(1004, $data['cgdNo'] . "采购单发货中无法取消");
  403. }
  404. }
  405. // if($data['send_status']>1 && $status==0){
  406. // return error_show(1004,"采购单发货中无法取消");
  407. // }
  408. // $order = ["order_code"=>$cgdNo,"status"=> $data['status'] ,"action_remark"=>$remark,"action_type"=>"status"];
  409. // $data['status'] = $status;
  410. // $data['remark'] = $remark;
  411. // $data['updatetime'] =date("Y-m-d H:i:s");
  412. $upd = Db::name("purchease_order")
  413. ->whereIn('cgdNo', $cgdNo)
  414. ->save([
  415. 'status' => $status,
  416. 'remark' => $remark,
  417. 'updatetime' => date("Y-m-d H:i:s"),
  418. ]);
  419. if ($upd) {
  420. $process = ["order_code" => implode(',', $cgdNo), "order_id" => 0, "order_status" => $status, "order_type" => 'CGD'];
  421. ProcessOrder::AddProcess($this->post['token'], $process);
  422. $order = ["order_code" => implode(',', $cgdNo), "status" => '', "action_remark" => $remark, "action_type" => "status"];
  423. ActionLog::logAdd($this->post['token'], $order, 'CGD', $status, $this->post);
  424. return app_show(0, "更新成功");
  425. } else {
  426. return error_show(1004, '更新失败');
  427. }
  428. }
  429. public function diffcreat(){
  430. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  431. //$cgdNo=makeNo("CG");
  432. $cgdNo = $this->post['cgdNo'] && $this->post['cgdNo'] !=="" ? trim($this->post['cgdNo']) :"";
  433. if($cgdNo==""){
  434. return error_show(1002,"参数good_code不能为空");
  435. }
  436. $cg = Db::name('purchease_order')->where(['cgdNo'=>$cgdNo,'is_del'=>0])->find();
  437. if($cg==""){
  438. return error_show(1002,"未找到采购单数据");
  439. }
  440. if($cg['order_type']==3|| $cg['order_type']==4){
  441. $gd = Db::name("good_zixun")->where(["spuCode"=>$cg['spuCode']])->find();
  442. }else {
  443. $gd =Db::name('good_basic')->where(['spuCode'=>$cg['spuCode']])->find();
  444. }
  445. if($gd==""){
  446. return error_show(1002,"未找到商品数据");
  447. }
  448. $diff_weight = isset($this->post['diff_weight']) && $this->post['diff_weight'] !=="" ? floatval($this->post['diff_weight']) :"";
  449. if($diff_weight===""){
  450. return error_show(1002,"参数diff_weight不能为空");
  451. }
  452. $apply_id =GetUserInfo($token);
  453. if(empty($apply_id)||$apply_id['code']!=0){
  454. return error_show(1002,"申请人数据不存在");
  455. }
  456. $rid= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  457. $rname= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  458. $diff_price = round($diff_weight*$cg['gold_price']+$diff_weight*$cg['teach_fee'],2);
  459. $data=[
  460. "cgdNo"=>$cgdNo,
  461. "good_code"=>$cg['spuCode'],
  462. "good_name"=>$cg['good_name'],
  463. "sale_price"=>$cg['good_price'],
  464. "good_weight"=>$cg['weight']*$cg['good_num'],
  465. "good_num"=>$cg['good_num'],
  466. "apply_id"=>$rid,
  467. "apply_name"=>$rname,
  468. "diff_weight"=>$diff_weight,
  469. "diff_price"=>$diff_price,
  470. "gold_price"=>$cg['good_price'],
  471. "status"=>1,
  472. "is_del"=>0,
  473. "addtime"=>date("Y-m-d H:i:s"),
  474. "updatetime"=>date("Y-m-d H:i:s")
  475. ];
  476. Db::startTrans();
  477. try{
  478. $item = Db::name("purchease_diff")->insert($data,true);
  479. if($item>0){
  480. $sto = ["order_code"=>$cgdNo,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  481. ActionLog::logAdd($this->post['token'],$sto,"CGGCD",1,$data);
  482. $process=["order_code"=>$cgdNo,"order_id"=>$item,"order_status"=>1,"order_type"=>'CGGCD'];
  483. ProcessOrder::AddProcess($this->post['token'],$process);
  484. $orderCode = Db::name("order_num")->where([["cgdNo","=",$cg['cgdNo']],["status","=",1]])->find();
  485. if($orderCode!=false){
  486. $order = Db::name("sale")->where("orderCode", '=', $orderCode['orderCode'])->find();
  487. if($order==false){
  488. Db::rollback();
  489. return error_show(1002,"未找到确认单信息");
  490. }
  491. $data=[
  492. "cgd_diffid"=>$item,
  493. "orderCode"=>$orderCode['orderCode'],
  494. "good_code"=>$cg['spuCode'],
  495. "good_name"=>$cg['good_name'],
  496. "diff_weight"=>$diff_weight,
  497. "diff_price"=>round($diff_weight*$order['gold_price']+$diff_weight*$order['cost_price'],2),
  498. "gold_price"=>$order['sale_price'],
  499. "status"=>1,
  500. "addtime"=>date("Y-m-d H:i:s"),
  501. "updatetime"=>date("Y-m-d H:i:s")
  502. ];
  503. $cgd = Db::name('sale_diff')->insert($data,true);
  504. if($cgd==0){
  505. // $sto = ["order_code"=>$cg['bkcode'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  506. // ActionLog::logAdd($this->post['token'],$sto,"ZXGCD",1,$sto);
  507. // $process=["order_code"=>$cg['bkcode'],"order_id"=>$cgd,"order_status"=>1,"order_type"=>'ZXGCD'];
  508. // ProcessOrder::AddProcess($this->post['token'],$process);
  509. // Db::commit();
  510. // return error_show(0,"新建成功");
  511. // }else{
  512. Db::rollback();
  513. return error_show(1002,"新建失败");
  514. }
  515. $sto = ["order_code"=>$orderCode['orderCode'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  516. ActionLog::logAdd($this->post['token'],$sto,"XSGCD",1,$data);
  517. $process=["order_code"=>$orderCode['orderCode'],"order_id"=>$cgd,"order_status"=>1,"order_type"=>'XSGCD'];
  518. ProcessOrder::AddProcess($this->post['token'],$process);
  519. }
  520. Db::commit();
  521. return error_show(0,"新建成功");
  522. }else{
  523. Db::rollback();
  524. return error_show(1002,"新建失败");
  525. }
  526. }catch (\Exception $e){
  527. Db::rollback();
  528. return error_show(1002,$e->getMessage());
  529. }
  530. }
  531. public function difflist(){
  532. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  533. $size = isset($this->post['size']) && $this->post['size'] !==""? intval($this->post['size']):"10";
  534. $where = [['a.is_del',"=",0]];
  535. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo']):"";
  536. if($cgdNo!=""){
  537. $where[]=['a.cgdNo',"like", "%$cgdNo%"];
  538. }
  539. $status = isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']):"";
  540. if($status!==""){
  541. $where[]=['a.status',"=",$status];
  542. }
  543. $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
  544. if ($start !="") {
  545. $where[]= ["a.addtime",'>=',$start];
  546. }
  547. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
  548. if($end !=""){
  549. $where[]= ["a.addtime",'<=',$end];
  550. }
  551. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !==""? intval($this->post['apply_name']):"";
  552. if($apply_name!==""){
  553. $where[]=['a.apply_name',"like","%$apply_name%"];
  554. }
  555. $count = Db::name('purchease_diff')->alias('a')->join("good b","b.spuCode=a.good_code","left")
  556. ->where($where)->count();
  557. $total = ceil($count/$size);
  558. $page = $page >= $total ? $total : $page;
  559. $list = Db::name('purchease_diff')->alias('a')->join("good b","b.spuCode=a.good_code","left")
  560. ->where($where)->page($page,$size)->order("a.addtime desc")->field("a.*,b.cat_id")->select();
  561. $data=[];
  562. foreach ($list as $value){
  563. $value['can']= isset($value['cat_id']) && $value['cat_id'] !=0 ? made($value['cat_id']):[];
  564. $data[]=$value;
  565. }
  566. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  567. }
  568. public function diffinfo(){
  569. $id = isset($this->post['id']) && $this->post['id'] !=="" ? trim($this->post['id']) :"";
  570. if($id==""){
  571. return error_show(1002,"参数id 不能为空");
  572. }
  573. $idinf = Db::name('purchease_diff')->where(['id'=>$id,'is_del'=>0])->find();
  574. if($idinf==false){
  575. return error_show(1002,"未找到采购工差单数据");
  576. }
  577. $dn = Db::name('purchease_order')->where(['cgdNo'=>$idinf['cgdNo'],'is_del'=>0])->find();
  578. if($dn['order_type']==3|| $dn['order_type']==4){
  579. $goon = Db::name("good_zixun")->where(["spuCode"=>$dn['spuCode'],"is_del"=>0])->find();
  580. }else {
  581. $goon =Db::name('good_basic')->where(['spuCode'=>$dn['spuCode']])->find();
  582. }
  583. // $goon = Db::name('good')->where(['spuCode'=>$idinf['good_code']])->find();
  584. if(empty($goon)){
  585. return error_show(1002,"未找到商品数据");
  586. }
  587. $idinf['wsm_name'] ="";
  588. $idinf['wsm_supplier'] ="";
  589. $idinf['wsm_supplierNo'] ="";
  590. $idinf['wsm_contactor'] ="";
  591. $idinf['wsm_mobile'] ="";
  592. $idinf['addr_code'] ="";
  593. $idinf['wsm_addr'] ="";
  594. if($dn['wsm_code']!=""){
  595. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  596. ->leftJoin("warehouse_addr c","a.wsm_code=c.wsm_code and c.is_del=0")
  597. ->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();
  598. $idinf['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  599. $idinf['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  600. $idinf['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  601. $idinf['wsm_contactor'] =isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor']:"";
  602. $idinf['wsm_mobile'] =isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile']:"";
  603. $idinf['addr_code'] =isset($wsmcode['addr_code']) ? $wsmcode['addr_code']:"";
  604. $idinf['addr_cn'] =isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']):"";
  605. $idinf['wsm_addr']=isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  606. }
  607. $idinf['info'] =$dn;
  608. $int= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
  609. $idinf['can']=$int;
  610. $idinf['order_type']=$dn['order_type'];
  611. //取出销售的工差单信息
  612. $diff = Db::name('sale_diff')->field('id,customer_remark,is_act,remark')->where('cgd_diffid',$id)->find();
  613. $idinf['diff_customer_remark']=$diff['customer_remark'];
  614. $idinf['diff_is_act']=$diff['is_act'];
  615. $idinf['diff_remark']=$diff['remark'];
  616. return app_show(0,"获取成功",$idinf);
  617. }
  618. public function diffstatu(){
  619. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  620. if($id==""){
  621. return error_show(1002,"参数id不能为空");
  622. }
  623. $dio = Db::name('purchease_diff')->where(['id'=>$id,'is_del'=>0])->find();
  624. if(empty($dio)){
  625. return error_show(1002,"工差订单未找到");
  626. }
  627. $cgd = Db::name("purchease_order")->where(['cgdNo'=>$dio['cgdNo']])->find();
  628. if(empty($cgd)){
  629. return error_show(1002,"采购单未找到");
  630. }
  631. $salediff=Db::name("sale_diff")->where(["cgd_diffid"=>$dio['id']])->find();
  632. if($salediff==false){
  633. return error_show(1002,"销售工差单未找到");
  634. }
  635. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  636. if($status===""){
  637. return error_show(1002,"参数status不能为空");
  638. }
  639. Db::startTrans();
  640. try{
  641. $temp = $dio['status'];
  642. $dio['status']=$status;
  643. $dio['updatetime']=date("Y-m-d H:i:s");
  644. $item = Db::name("purchease_diff")->save($dio);
  645. if ($item){
  646. if($status==2){
  647. $cgd['diff_fee'] =$dio['diff_price'];
  648. $cgd['diff_weight'] =$dio['diff_weight'];
  649. $cgd['updatetime'] =date("Y-m-d H:i:s");
  650. $cgp=Db::name("purchease_order")->save($cgd);
  651. if($cgp==false){
  652. Db::rollback();
  653. return error_show(1002,"采购单更新失败");
  654. }
  655. if($salediff['is_act']==1){
  656. $saled=[
  657. "diff_fee"=>$salediff['diff_price'],
  658. "diff_weight"=>$salediff['diff_weight'],
  659. "updatetime"=>date("Y-m-d H:i:s")
  660. ];
  661. $up =Db::name("sale")->where(["orderCode"=>$salediff['orderCode']])->save($saled);
  662. if($up==false){
  663. Db::rollback();
  664. return error_show(1002,"销售单更新失败");
  665. }
  666. }
  667. }
  668. $sto = ["order_code"=>$dio['cgdNo'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  669. ActionLog::logAdd($this->post['token'],$sto,"CGGCD",$status,$this->post);
  670. $process=["order_code"=>$dio['cgdNo'],"order_id"=>$dio['id'],"order_status"=>$status,"order_type"=>'CGGCD'];
  671. ProcessOrder::AddProcess($this->post['token'],$process);
  672. Db::commit();
  673. return error_show(0,"更新成功");
  674. }else{
  675. Db::rollback();
  676. return error_show(1002,"更新失败");
  677. }
  678. }catch (\Exception $e){
  679. Db::rollback();
  680. return error_show(1002,$e->getMessage()."|".$e->getLine());
  681. }
  682. }
  683. //修改采购单的实时金价
  684. public function editGoldPrice()
  685. {
  686. $param = $this->request->only(['cgdNo', 'gold_price'], 'post', 'trim');
  687. $val = Validate::rule(['cgdNo|采购单号' => 'require', 'gold_price|实时金价' => 'require|float']);
  688. if (!$val->check($param)) return error_show(1004, $val->getError());
  689. //采购单
  690. $data = Db::name("purchease_order")
  691. ->field('id,demo_fee,good_num,weight,open_fee,teach_fee,pakge_fee,mark_fee,cert_fee,nake_fee,delivery_fee,status')
  692. ->where(["cgdNo" => $param['cgdNo'], "is_del" => 0])
  693. ->find();
  694. if (empty($data)) return error_show(1004, "未找到该采购单数据");
  695. if ($data['status'] != 0) return error_show(1005, '该状态下不允许修改实时金价');
  696. $sale_price = round($data['demo_fee'] / $data['good_num'] + $data['open_fee'] / $data['good_num'] +
  697. $data['weight'] * $param["gold_price"] + $data['teach_fee'] * $data['weight'] +
  698. $data['pakge_fee'] + $data['mark_fee'] + $data['cert_fee'] +$data['nake_fee'] + $data['delivery_fee'],2);
  699. $total_price = round($data['good_num']*$sale_price,2);
  700. $rs = Db::name("purchease_order")
  701. ->where('id', $data['id'])
  702. ->update(['updatetime' => date('Y-m-d H:i:s'), 'gold_price' => $param['gold_price'],
  703. "good_price"=>$sale_price,"total_fee"=>$total_price]);
  704. return $rs ? app_show(0, '修改采购单实时金价成功') : error_show(1005, '修改采购单实时金价失败');
  705. }
  706. //采购单导出
  707. public function exportCgdList()
  708. {
  709. $cgdNos = $this->request->post('cgdNos', [], 'trim');
  710. if (empty($cgdNos)) return error_show(1004, '要导出的采购单编号不能为空');
  711. $i = 1;
  712. $send_type = [1 => '直接发货', 2 => '延时发货'];
  713. $status = [0 => '待与供应商确认', 1 => '待入库', 2 => '部分入库', 3 => '入库完成', 4 => '已取消订单'];
  714. $list = Db::name("purchease_order")
  715. ->alias('po')
  716. ->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')
  717. ->whereIn('po.cgdNo', $cgdNos)
  718. ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
  719. ->leftJoin('sale s', 's.orderCode=on.orderCode')
  720. ->leftJoin('business b', 'b.companyNo=s.supplierNo')
  721. ->order("po.addtime desc")
  722. ->withAttr('序号', function () use (&$i) {
  723. return $i++;
  724. })->withAttr('采购单状态', function ($val) use ($status) {
  725. return isset($status[$val]) ? $status[$val] : '';
  726. })->withAttr('发货方式', function ($val) use ($send_type) {
  727. return isset($send_type[$val]) ? $send_type[$val] : '';
  728. })
  729. ->select()
  730. ->toArray();
  731. foreach ($list as &$value) {
  732. if ($value['order_type'] == 3 || $value['order_type'] == 4) {
  733. $temp = Db::name("good_zixun")
  734. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  735. ->field('id,specinfo,good_unit')
  736. ->find();
  737. $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0;
  738. $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : [];
  739. $speclist = [];
  740. foreach ($specinfo as $val) {
  741. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  742. }
  743. } else {
  744. $good_unit = Db::name('good_basic')
  745. ->where(['spuCode' => $value['产品编号']])
  746. ->value('good_unit', 0);
  747. $spec = Db::name("good_spec")
  748. ->field('id,spec_id,spec_value_id')
  749. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  750. ->select()
  751. ->toArray();
  752. $speclist = [];
  753. if (!empty($spec)) {
  754. foreach ($spec as $val) {
  755. $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', '');
  756. }
  757. }
  758. }
  759. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  760. $value['单位'] = $good_unit ? Db::name('unit')->where(['id' => $good_unit, 'is_del' => 0])->value('unit', '') : '';
  761. unset($value['order_type']);
  762. }
  763. $headerArr = array_keys($list[0]);
  764. excelSave('采购单导出' . date('YmdHis'), $headerArr, $list);
  765. }
  766. // public function create(){
  767. // $customer = isset($this->post['customer_code'])&&$this->post['customer_code']!="" ?trim($this->post['customer_code']):"";
  768. // if($customer==""){
  769. // return error_show(1004,"参数customer_code不能为空");
  770. // }
  771. // $supplier = isset($this->post['supplier_code'])&&$this->post['supplier_code']!="" ?trim($this->post['supplier_code']):"";
  772. // if($supplier==""){
  773. // return error_show(1004,"参数supplier_code不能为空");
  774. // }
  775. // $good_code = isset($this->post['good_code']) && $this->post['good_code']!=""?trim($this->post['good_code']):"";
  776. // if($good_code==""){
  777. // return error_show(1004,"参数good_code不能为空");
  778. // }
  779. // $good_num = isset($this->post['good_num'])&& $this->post['good_num']!==""? intval($this->post['good_num']):"";
  780. // if($good_num===""){
  781. // return error_show(1004,"参数good_code不能为空");
  782. // }
  783. // $file_url = isset($this->post['file_url'])&& $this->post['file_url']!==""? trim($this->post['file_url']):"";
  784. // if($file_url===""){
  785. // return error_show(1004,"参数file_url不能为空");
  786. // }
  787. // $mark =isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  788. // if($mark==""){
  789. // return error_show(1004,"参数remark不能为空");
  790. // }
  791. //
  792. // $data=[
  793. // ""
  794. // ];
  795. // }
  796. }