Purch.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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\Exception;
  7. use think\facade\Db;
  8. use think\facade\Validate;
  9. //采购单相关功能
  10. class Purch extends Base
  11. {
  12. public $noble=[];
  13. public function __construct(App $app)
  14. {
  15. parent::__construct($app);
  16. $this->noble=\think\facade\Config::get("noble");
  17. }
  18. public function list(){
  19. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']):"1";
  20. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']):"10";
  21. $where = [['po.is_del', "=", 0], ['po.order_type', '<>', 5]];//不显示类型为5即订单导入生成的采购单
  22. $bkcode = isset($this->post['bk_code']) && $this->post['bk_code']!="" ? trim($this->post['bk_code']):"";
  23. if($bkcode!=""){
  24. $where[]=['po.bkcode',"like", "%{$bkcode}%"];
  25. }
  26. $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
  27. if($status!==""){
  28. // $where['status'] = $status;
  29. $where[]=['po.status',"=", $status];
  30. }
  31. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo'])
  32. :"";
  33. if($cgdNo!=""){
  34. //$where['cgdNo'] = Db::Raw("like '%{$cgdNo}%'");
  35. $where[]=['po.cgdNo',"like", "%{$cgdNo}%"];
  36. }
  37. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name']!="" ? trim($this->post['apply_name']):"";
  38. if($apply_name!=""){
  39. // $where['cgder'] =Db::Raw("like '%{$apply_name}%'");
  40. $where[]=['po.cgder',"like", "%{$apply_name}%"];
  41. }
  42. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code']!="" ? trim($this->post['wsm_code']):"";
  43. if($wsm_code!=""){
  44. // $where['wsm_code'] = $wsm_code;
  45. $where[]=['po.wsm_code',"=", $wsm_code];
  46. }
  47. $wsm_in_code = isset($this->post['wsm_in_code']) && $this->post['wsm_in_code']!="" ? trim($this->post['wsm_in_code']):"";
  48. if($wsm_in_code!=""){
  49. $incode = Db::name("purchease_in")->where(["wsm_in_code"=>Db::raw(" like %{$wsm_in_code}%"),"is_del"=>0])->column("cgdNo");
  50. if(empty($incode)){
  51. return error_show(1004,"未找到有关入库单信息");
  52. }
  53. $where[]=['po.cgdNo',"in", $incode];
  54. }
  55. $good_name = isset($this->post['good_name']) && $this->post['good_name']!="" ? trim($this->post['good_name']):"";
  56. if($good_name!=""){
  57. //$where['good_name'] = Db::raw(" like %{$good_name}%");
  58. $where[]=['po.good_name',"like", "%{$good_name}%"];
  59. }
  60. $good_code = isset($this->post['good_code']) && $this->post['good_code']!="" ? trim($this->post['good_code'])
  61. :"";
  62. if($good_code!=""){
  63. // $where['good_code'] = Db::raw(" like %{$good_code}%");
  64. $where[]=['po.spuCode',"like", "%{$good_code}%"];
  65. }
  66. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo']!="" ? trim($this->post['supplierNo'])
  67. :"";
  68. if($supplierNo!=""){
  69. // $where['good_code'] = Db::raw(" like %{$good_code}%");
  70. $where[]=['po.supplierNo',"like", "%{$supplierNo}%"];
  71. }
  72. $wsm_supplierNo = isset($this->post['wsm_supplierNo']) && $this->post['wsm_supplierNo']!="" ? trim($this->post['wsm_supplierNo'])
  73. :"";
  74. if($wsm_supplierNo!=""){
  75. $supplier = Db::name("supplier")->where(["code"=>$wsm_supplierNo])->find();
  76. if(empty($supplier)){
  77. return error_show(1004,"未找到供应商信息");
  78. }
  79. $wsmcode = Db::name("warehouse_info")->where(["is_del"=>0,"supplierNo"=>$wsm_supplierNo])->column("wsm_code");
  80. $where[]=['po.wsm_code',"in", $wsmcode];
  81. }
  82. $start = isset($this->post['start']) && $this->post['start']!="" ?$this->post['start']:"";
  83. if($start!=""){
  84. // $where['addtime'] = Db::raw(" >= '{$start}'");
  85. $where[]=['po.addtime',">=", $start];
  86. }
  87. $end = isset($this->post['end']) && $this->post['end']!="" ?$this->post['end']:"";
  88. if($end!=""){
  89. $where[]=['po.addtime',"<=", $end];
  90. }
  91. $last_start = isset($this->post['last_start']) && $this->post['last_start']!="" ?$this->post['last_start']:"";
  92. if($last_start!=""){
  93. //$where['lasttime'] = Db::raw(" >= '{$last_start}'");
  94. $where[]=['po.lasttime',">=", $last_start];
  95. }
  96. $last_end = isset($this->post['last_end']) && $this->post['last_end']!="" ?$this->post['last_end']:"";
  97. if($last_end!=""){
  98. //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
  99. $where[]=['po.lasttime',"<=", $last_end];
  100. }
  101. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode']!="" ?$this->post['orderCode']:"";
  102. if($orderCode!=""){
  103. //$where['lasttime'] = Db::raw(" <= '{$last_end}'");
  104. $where[]=['on.orderCode',"like", '%'.$orderCode.'%'];
  105. }
  106. $role=$this->checkRole();
  107. if(!empty($role['write'])){
  108. $where[]=["po.cgder_id","in",$role['write']];
  109. }
  110. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  111. if ($company_name !== "") $where[] = ["po.cgder_id", 'in', get_company_item_user_by_name($company_name)];
  112. // if(!empty($role['platform']) ){
  113. // $where[]=["po.platform_id","in",$role['platform']];
  114. // }
  115. $count=Db::name("purchease_order")->alias('po')->leftJoin('order_num on','on.cgdNo=po.cgdNo')->where($where)->count();
  116. $total = ceil($count/$size);
  117. $page = $page >= $total ? $total : $page;
  118. $list = Db::name("purchease_order")
  119. ->alias('po')
  120. ->leftJoin('order_num on','on.cgdNo=po.cgdNo')
  121. ->leftJoin("depart_user u", "u.uid=po.cgder_id AND u.is_del=0")
  122. ->field('po.*,on.orderCode,u.itemid')
  123. ->where($where)
  124. ->page($page,$size)
  125. ->order("po.addtime desc")
  126. ->cursor();
  127. // $all_orderCodes = Db::name('order_num')
  128. // ->whereIn('cgdNo',array_column($list,'cgdNo'))
  129. // ->column('orderCode','cgdNo');
  130. $data=[];
  131. foreach ($list as $value){
  132. $value['wsm_name']="";
  133. if($value['wsm_code']!=""){
  134. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  135. ->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  136. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  137. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  138. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  139. }
  140. if($value['order_type']==3|| $value['order_type']==4){
  141. $goon = Db::name("good_zixun")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->find();
  142. $value["speclist"]=isset($goon['specinfo'])&&$goon['specinfo']!=""? json_decode($goon['specinfo'],true):"";
  143. }else {
  144. $goon =Db::name('good_basic')->where(['spuCode'=>$value['spuCode']])->find();
  145. $spec = Db::name("good_spec")->where(["spuCode"=>$goon['spuCode'],"is_del"=>0])->select()->toArray();
  146. $speclist=[];
  147. if(!empty($spec)){
  148. foreach ($spec as $val){
  149. $temp=[];
  150. $temp['id']=$val['id'];
  151. $temp['spuCode']=$val['spuCode'];
  152. $temp['spec_id']=$val['spec_id'];
  153. $temp['spec_value_id']=$val['spec_value_id'];
  154. $sp = Db::name("specs")->where(["id"=>$val['spec_id']])->find();
  155. $temp['spec_name']=isset($sp["spec_name"]) ? $sp["spec_name"]:"";
  156. $spv = Db::name("spec_value")->where(["id"=>$val['spec_value_id']])->find();
  157. $temp['spec_value']=isset($spv["spec_value"]) ? $spv["spec_value"]:"";
  158. $speclist[]=$temp;
  159. }
  160. }
  161. $value["speclist"]=empty($speclist)?[]:$speclist;
  162. }
  163. //采购单详情字段order_type 为1或者2时,取线上商品库 商品创建人 order_type 为3或者4时 取采购反馈的商品库 商品反馈人
  164. // $value['cgder_id'] = $goon['createrid'];
  165. // $value['cgder'] = $goon['creater'];
  166. $inorder= Db::name("purchease_in")->where(['cgdNo'=>$value['cgdNo'],"is_del"=>0])->select();
  167. $value['child']=empty($inorder)? [] : $inorder;
  168. $value['send_num'] -=$value['th_num'];
  169. $value['total_fee'] =round($value['total_fee']-$value['th_fee'],2);
  170. //补充orderCode
  171. // $value['orderCode']=isset($all_orderCodes[$value['cgdNo']])?$all_orderCodes[$value['cgdNo']]:'';
  172. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  173. $data[]=$value;
  174. }
  175. return app_show(0,"获取成功",["list"=>$data ,"count"=>$count]);
  176. }
  177. public function info(){
  178. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ?trim($this->post['cgdNo']):"";
  179. if($cgdNo==""){
  180. return error_show(1004,"参数cgdNo 不能为空");
  181. }
  182. //采购单
  183. $data = Db::name("purchease_order")->where(["cgdNo"=>$cgdNo,"is_del"=>0])->find();
  184. if(empty($data)){
  185. return error_show(1004,"未找到数据");
  186. }
  187. //采购退货单
  188. $im = Db::name('purchease_back')->where(['cgdNo'=>$data['cgdNo'],'is_del'=>0])->select();
  189. $var=[];
  190. foreach ($im as $value){
  191. if($data['order_type']==3|| $data['order_type']==4){
  192. $goo = Db::name("good_zixun")->where(["spuCode"=>$value['spuCode'],"is_del"=>0])->find();
  193. }else {
  194. $goo =Db::name('good_basic')->where(['spuCode'=>$value['spuCode']])->find();
  195. }
  196. if($goo==false) {
  197. return error_show(1002, "未找到商品数据");
  198. }
  199. $cat= isset($goo['cat_id']) && $goo['cat_id'] !=0 ? made($goo['cat_id']):[];
  200. $value['cant']=$cat;
  201. $var[]=$value;
  202. }
  203. //采购工差单
  204. $dom = Db::name("purchease_diff")->where(['cgdNo'=>$data['cgdNo'],'is_del'=>0])->find();
  205. if($data['order_type']==3|| $data['order_type']==4){
  206. $goon = Db::name("good_zixun")->where(["spuCode"=>$data['spuCode'],"is_del"=>0])->find();
  207. }else {
  208. $goon =Db::name('good_basic')->where(['spuCode'=>$data['spuCode']])->find();
  209. }
  210. if(empty($goon)){
  211. return error_show(1002,"未找到商品数据");
  212. }else{
  213. $goon['exclusive']=isset($goon['is_exclusive'])?makeExcluse($goon['is_exclusive']):"";
  214. $unit =Db::name("unit")->where(["id"=>$goon['good_unit']])->find();
  215. $goon['unit'] = isset($unit['unit'])?$unit['unit']:'';
  216. $spec = Db::name("good_spec")->where(["spuCode"=>$goon['spuCode'],"is_del"=>0])->select()->toArray();
  217. $speclist=[];
  218. if(!empty($spec)){
  219. foreach ($spec as $value){
  220. $temp=[];
  221. $temp['id']=$value['id'];
  222. $temp['spuCode']=$value['spuCode'];
  223. $temp['spec_id']=$value['spec_id'];
  224. $temp['spec_value_id']=$value['spec_value_id'];
  225. $temp['is_del']=$value['is_del'];
  226. $sp = Db::name("specs")->where(["id"=>$value['spec_id']])->find();
  227. $temp['spec_name']=isset($sp["spec_name"]) ? $sp["spec_name"]:"";
  228. $spv = Db::name("spec_value")->where(["id"=>$value['spec_value_id']])->find();
  229. $temp['spec_value']=isset($spv["spec_value"]) ? $spv["spec_value"]:"";
  230. $speclist[]=$temp;
  231. }
  232. }
  233. $goon["speclist"]=empty($speclist)?[]:$speclist;
  234. $proof =Db::name("good_proof")->where(["spuCode"=>$goon['spuCode'],"is_del"=>0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
  235. $goon['proof'] = isset($proof)&&$proof!=false? $proof:[];
  236. $goon['origin_place_cn']="";
  237. $goon['delivery_place_cn']="";
  238. if(isset($goon['delivery_place'])&&$goon['delivery_place']!==""){
  239. $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
  240. list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$goon['delivery_place']);
  241. $goon['delivery_place_cn']=GetAddr(json_encode($place));
  242. }
  243. if(isset($goon['delivery_place'])&&$goon['origin_place']!==""){
  244. $place = ["provice_code"=>"","city_code"=>"","area_code"=>""];
  245. list($place['provice_code'],$place['city_code'],$place['area_code'])=explode(",",$goon['origin_place']);
  246. $goon['origin_place_cn']=GetAddr(json_encode($place));
  247. }
  248. if($goon['brand_id']!=0){
  249. $brand=Db::name("brand")->where(["id"=>$goon['brand_id']])->find();
  250. $goon["brand_name"]=isset($brand['brand_name'])?$brand['brand_name']:"";
  251. }else{
  252. $goon["brand_name"]="";
  253. $goon["brand_id"]="";
  254. }
  255. $supplier = Db::name("supplier")->where(["code"=>$goon['supplierNo']])->find();
  256. $goon['supplierName'] = isset($supplier['name'])?$supplier['name']:"";
  257. $goon['noble_name']=isset($goon['noble_metal'])&&$goon['noble_metal']!=0?$this->noble[$goon['noble_metal']] :"";
  258. if(isset($goon['companyNo'])&&$goon['companyNo']!=""){
  259. $company = Db::name("business")->where(["companyNo"=>$goon['companyNo']])->find();
  260. }
  261. $goon['company'] = isset($company['company'])?$company['company']:"";
  262. }
  263. $int= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
  264. $data['wsm_name']="";
  265. if($data['wsm_code']!=""){
  266. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  267. ->leftJoin("warehouse_addr c","a.wsm_code=c.wsm_code and c.is_del=0")
  268. ->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();
  269. $data['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  270. $data['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  271. $data['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  272. $data['wsm_contactor'] =isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor']:"";
  273. $data['wsm_mobile'] =isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile']:"";
  274. // $data['wsm_addr'] =isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  275. $data['addr_code'] =isset($wsmcode['addr_code']) ? $wsmcode['addr_code']:"";
  276. $data['wsm_addr'] =isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']):"";
  277. $data['wsm_addr'].=isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  278. $inorder= Db::name("purchease_in")->where(['cgdNo'=>$data['cgdNo'],"is_del"=>0])->select();
  279. $data['child']=empty($inorder)? [] : $inorder;
  280. }
  281. $data['can'] = $int;
  282. $data['goodinfo'] = $goon;
  283. $data['purcheasediff'] = $dom;
  284. $data['send_num'] -=$data['th_num'];
  285. $data['total_fee'] =round($data['total_fee']-$data['th_fee'],2);
  286. if(isset($data['companyNo'])&&$data['companyNo']!=""){
  287. $company = Db::name("business")->where(["companyNo"=>$data['companyNo']])->find();
  288. }
  289. $data['company'] = isset($company['company'])?$company['company']:"";
  290. //$data['info'] = $var;
  291. $data['purcheaseback'] = $var;
  292. //补充orderCode
  293. $data['orderCode'] = Db::name('order_num')
  294. ->where('cgdNo', $data['cgdNo'])
  295. ->value('orderCode', '');
  296. //补充收货地址
  297. $addr = Db::name('order_addr')
  298. ->field('id,addr,addr_code,contactor,mobile,receipt_quantity')
  299. ->where(['orderCode' => $data['orderCode'], 'is_del' => 0])
  300. ->select()
  301. ->toArray();
  302. if (!empty($addr)) {
  303. foreach ($addr as &$vv){
  304. $temp = explode(',', $vv['addr_code']);
  305. $temp_ = GetAddr(json_encode(['provice_code' => isset($temp[0]) ? $temp[0] : '', 'city_code' => isset($temp[1]) ? $temp[1] : '', 'area_code' => isset($temp[2]) ? $temp[2] : '']));
  306. $vv['addr_code'] = $temp_ . $vv['addr'];
  307. }
  308. }else $addr=[];
  309. $data['addr_info'] = $addr;
  310. //补充客户名称
  311. if (empty($data['orderCode'])) $data['customer'] = [];
  312. else {
  313. $data['customer'] = Db::name('sale')
  314. ->alias('s')
  315. ->field('c.companyNo,c.companyName')
  316. ->leftJoin('customer_info c', 'c.companyNo=s.customer_code AND c.is_del=0')
  317. ->where(['s.orderCode' => $data['orderCode']])
  318. ->find();
  319. }
  320. return app_show(0, "获取成功", $data);
  321. }
  322. public function edit(){
  323. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ?trim($this->post['cgdNo']):"";
  324. if($cgdNo==""){
  325. return error_show(1004,"参数cgdNo 不能为空");
  326. }
  327. $data = Db::name("purchease_order")->where(["cgdNo"=>$cgdNo,"is_del"=>0])->find();
  328. if(empty($data)){
  329. return error_show(1004,"未找到数据");
  330. }
  331. $order = ["order_code"=>$cgdNo,"status"=> $data['status'] ,"action_remark"=>$this->post['remark'],"action_type"=>"edit"];
  332. $wsm_code = isset($this->post['wsm_code'])&&$this->post['wsm_code']!=""? trim($this->post['wsm_code']):"";
  333. if($wsm_code!=""){
  334. $ware = Db::name("warehouse_info")->where(["wsm_code"=>$wsm_code,"is_del"=>0])->find();
  335. if(empty($ware)){
  336. return error_show(1004,"未找到仓库信息");
  337. }
  338. // $good = Db::name("good_stock")->where(["wsm_code"=>$wsm_code,"good_type_code"=>$data['good_type_code'],"is_del"=>0])->find();
  339. // if(empty($good)){
  340. // return error_show(1004,"未找到仓库下商品信息");
  341. // }
  342. $data['wsm_code'] = $wsm_code;
  343. }
  344. $cgdid = isset($this->post['cgder_id'])&&$this->post['cgder_id']!=""?intval($this->post['cgder_id']):"";
  345. if($cgdid!=''){
  346. $cgduser = GetInfoById($this->post['token'],["id"=>$cgdid]);
  347. if((!empty($cgduser) && $cgduser['code']!=0) ||empty($cgduser) ){
  348. return error_show($cgduser['code'],$cgduser['message']);
  349. }
  350. $userinfo = $cgduser['data'];
  351. $data['cgder_id'] = $cgdid;
  352. $data['cgder'] = $userinfo['nickname'];
  353. }
  354. $good_num = isset($this->post['good_num'])&&$this->post['good_num']!=""?intval($this->post['good_num']):"";
  355. if($good_num!=""){
  356. $data['good_num'] = $good_num;
  357. }
  358. $good_price = isset($this->post['good_price'])&&$this->post['good_price']!=""?$this->post['good_price']:"";
  359. if($good_price!=""){
  360. $data['good_price'] = $good_price;
  361. }
  362. // $total_fee = isset($this->post['total_fee'])&&$this->post['total_fee']!=""?$this->post['total_fee']:"";
  363. // if($total_fee!=""){
  364. // $data['total_fee'] = $total_fee;
  365. // }
  366. $pakge_fee = isset($this->post['pakge_fee'])&&$this->post['pakge_fee']!=""?$this->post['pakge_fee']:"";
  367. if($pakge_fee!=""){
  368. $data['pakge_fee'] = $pakge_fee;
  369. }
  370. $cert_fee = isset($this->post['cert_fee'])&&$this->post['cert_fee']!=""?$this->post['cert_fee']:"";
  371. if($cert_fee!=""){
  372. $data['cert_fee'] = $cert_fee;
  373. }
  374. $open_fee = isset($this->post['open_fee'])&&$this->post['open_fee']!=""?$this->post['open_fee']:"";
  375. if($open_fee!=""){
  376. $data['open_fee'] = $open_fee;
  377. }
  378. $delivery_fee = isset($this->post['delivery_fee'])&&$this->post['delivery_fee']!=""?$this->post['delivery_fee']:"";
  379. if($delivery_fee!=""){
  380. $data['delivery_fee'] = $delivery_fee;
  381. }
  382. $mark_fee = isset($this->post['mark_fee'])&&$this->post['mark_fee']!=""?$this->post['mark_fee']:"";
  383. if($mark_fee!=""){
  384. $data['mark_fee'] = $mark_fee;
  385. }
  386. $teach_fee = isset($this->post['teach_fee'])&&$this->post['teach_fee']!=""?$this->post['teach_fee']:"";
  387. if($teach_fee!=""){
  388. $data['teach_fee'] = $teach_fee;
  389. }
  390. $nake_fee = isset($this->post['nake_fee'])&&$this->post['nake_fee']!=""?$this->post['nake_fee']:"";
  391. if($nake_fee!=""){
  392. $data['nake_fee'] = $nake_fee;
  393. }
  394. $weight = isset($this->post['weight'])&&$this->post['weight']!=""?$this->post['weight']:"";
  395. if($weight!=""){
  396. $data['weight'] = $weight;
  397. }
  398. $diff_weight = isset($this->post['diff_weight'])&&$this->post['diff_weight']!=""?$this->post['diff_weight']:"";
  399. if($diff_weight!=""){
  400. $data['diff_weight'] = $diff_weight;
  401. }
  402. $diff_fee = isset($this->post['diff_fee'])&&$this->post['diff_fee']!=""?$this->post['diff_fee']:"";
  403. if($diff_fee!=""){
  404. $data['diff_fee'] = $diff_fee;
  405. }
  406. $supplierNo = isset($this->post['supplierNo'])&&$this->post['supplierNo']!=""?trim($this->post['supplierNo'])
  407. :"";
  408. if($supplierNo!=""){
  409. $supplier =Db::name("supplier")->where(['code'=>$supplierNo])->find();
  410. if(empty($supplier)){
  411. return error_show(1004,"未找到供应商信息");
  412. }
  413. $data['supplierNo'] = $supplierNo;
  414. $data['supplier_name'] = $supplier['name'];
  415. }
  416. $remark = isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  417. if($remark!=""){
  418. $data['remark'] = $remark;
  419. }
  420. $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']+$data['teach_fee']*$data['weight']+$data['delivery_fee']),2);
  421. $data['total_fee'] = round($data['good_price']*$data['good_num'],2);
  422. $data['updatetime'] =date("Y-m-d H:i:s");
  423. $upd=Db::name("purchease_order")->save($data);
  424. if($upd){
  425. // ActionLog::logAdd($this->post['token'],$order,'cgd',$data['status'],$this->post);
  426. //修改状态,添加待办
  427. ActionLog::logAdd($this->post['token'], $order, "CGD", $data['status'], $this->post);
  428. // ProcessOrder::AddProcess($this->post['token'], [
  429. // "order_type" => 'CGD',
  430. // "order_code" => $cgdNo,//销售单code
  431. // "order_id" => $data['id'],
  432. // "order_status" => $data['status'],"before_status"=>1
  433. // ]);
  434. return app_show(0,"更新成功");
  435. }else{
  436. return error_show(1004,'更新失败');
  437. }
  438. }
  439. //修改采购单
  440. public function editNew()
  441. {
  442. $param = $this->request->only(['cgdNo', 'supplierNo', 'token', 'wsm_code' => ''], 'post', 'trim');
  443. $val = Validate::rule([
  444. 'cgdNo|采购单号' => 'require',
  445. 'supplierNo|供应商编号' => 'require',
  446. 'token' => 'require',
  447. ]);
  448. if (!$val->check($param)) return error_show(1004, $val->getError());
  449. Db::startTrans();
  450. try {
  451. $rs = Db::name('purchease_order')
  452. ->alias('po')
  453. ->field('po.id,po.order_type,po.status,po.cgder_id,po.cgder,po.supplierNo,po.supplier_name,po.wsm_code,wi.name wsm_name')
  454. ->leftJoin('warehouse_info wi', 'wi.wsm_code=po.wsm_code AND wi.is_del=0')
  455. ->where([
  456. 'po.cgdNo' => $param['cgdNo'],
  457. 'po.is_del' => 0,
  458. 'po.status' => 0
  459. ])->find();
  460. if (empty($rs)) throw new Exception('采购单不存在或不允许修改');
  461. $update = ['updatetime' => date('Y-m-d H:i:s')];
  462. //order_type==1备库
  463. if ($rs['order_type'] == 1) {
  464. if (empty($param['wsm_code'])) throw new Exception('备库单请传入仓库编码');
  465. else {
  466. $wsm_rs = Db::name('warehouse_info')
  467. ->field('id,wsm_code,name wsm_name')
  468. ->where([
  469. 'wsm_code' => $param['wsm_code'],
  470. 'is_del' => 0,
  471. ])->find();
  472. if (empty($wsm_rs)) throw new Exception('查不到该仓库的记录');
  473. else $update['wsm_code'] = $wsm_rs['wsm_code'];
  474. }
  475. }
  476. //供应商信息
  477. $supplier_rs = Db::name('supplier')
  478. ->field('id,name supplierName,code supplierNo,person,personid')
  479. ->where(['is_del' => 0, 'code' => $param['supplierNo']])
  480. ->find();
  481. if (empty($supplier_rs)) throw new Exception('查不到该供应商的记录');
  482. else {
  483. $update['cgder_id'] = $supplier_rs['personid'];
  484. $update['cgder'] = $supplier_rs['person'];
  485. $update['supplierNo'] = $supplier_rs['supplierNo'];
  486. $update['supplier_name'] = $supplier_rs['supplierName'];
  487. }
  488. //是否修改内容确定
  489. if (isset($update['wsm_code'])) {
  490. if ($update['wsm_code'] == $rs['wsm_code'] && $update['supplierNo'] == $rs['supplierNo']) throw new Exception('没有修改的内容,不能提交');
  491. } elseif ($update['supplierNo'] == $rs['supplierNo']) throw new Exception('没有修改的内容,不能提交');
  492. if ($update['supplierNo'] == $rs['supplierNo'] && $param['wsm_code'] == $rs['wsm_code']) throw new Exception('没有修改的内容');
  493. //修改
  494. Db::name('purchease_order')
  495. ->where([
  496. 'id' => $rs['id'],
  497. 'is_del' => 0,
  498. 'status' => 0
  499. ])->update($update);
  500. $user = GetUserInfo($param['token']);
  501. $creater_id = isset($user['data']['id']) ? $user['data']['id'] : 0;
  502. $creater = isset($user['data']['nickname']) ? $user['data']['nickname'] : '';
  503. //添加修改记录
  504. Db::name('purchease_change_supplier_log')
  505. ->insert([
  506. 'cgdNo' => $param['cgdNo'],
  507. 'cgder_id_old' => $rs['cgder_id'],
  508. 'cgder_id' => $update['cgder_id'],
  509. 'cgder_old' => $rs['cgder'],
  510. 'cgder' => $update['cgder'],
  511. 'supplierNo_old' => $rs['supplierNo'],
  512. 'supplierNo' => $update['supplierNo'],
  513. 'supplier_name_old' => $rs['supplier_name'],
  514. 'supplier_name' => $update['supplier_name'],
  515. 'wsm_code_old' => $rs['wsm_code'],
  516. 'wsm_code' => isset($update['wsm_code']) ? $update['wsm_code'] : $rs['wsm_code'],
  517. 'wsm_name_old' => $rs['wsm_name'],
  518. 'wsm_name' => isset($wsm_rs['wsm_name']) ? $wsm_rs['wsm_name'] : $rs['wsm_name'],
  519. 'addtime' => date('Y-m-d H:i:s'),
  520. 'updatetime' => date('Y-m-d H:i:s'),
  521. 'creater_id' => $creater_id,
  522. 'creater' => $creater,
  523. ]);
  524. //记录日志
  525. ActionLog::logAdd(['id' => $creater_id, 'nickname' => $creater], $rs, "CGD", $rs['status'], $param);
  526. // ProcessOrder::AddProcess($this->post['token'], [
  527. // "order_type" => 'CGD',
  528. // "order_code" => $param['cgdNo'],//销售单code
  529. // "order_id" => $rs['id'],
  530. // "order_status" => $rs['status']
  531. // ]);
  532. Db::commit();
  533. return app_show(0, '修改成功');
  534. } catch (Exception $exception) {
  535. Db::rollback();
  536. return error_show(1005, $exception->getMessage());
  537. }
  538. }
  539. public function status()
  540. {
  541. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] != "" ? $this->post['cgdNo'] : [];
  542. if (empty($cgdNo)) {
  543. return error_show(1004, "参数cgdNo 不能为空");
  544. }
  545. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  546. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  547. if ($status === "") {
  548. return error_show(1004, "参数status 不能为空");
  549. }
  550. if ($status == 0) {
  551. $data = Db::name("purchease_order")
  552. ->field('id,cgdNo')
  553. ->whereIn('cgdNo', $cgdNo)
  554. ->where(["is_del" => 0])
  555. ->where('send_status', '>', 1)
  556. ->find();
  557. if (!empty($data)) {
  558. return error_show(1004, $data['cgdNo'] . "采购单发货中无法取消");
  559. }
  560. }
  561. // if($data['send_status']>1 && $status==0){
  562. // return error_show(1004,"采购单发货中无法取消");
  563. // }
  564. // $order = ["order_code"=>$cgdNo,"status"=> $data['status'] ,"action_remark"=>$remark,"action_type"=>"status"];
  565. // $data['status'] = $status;
  566. // $data['remark'] = $remark;
  567. // $data['updatetime'] =date("Y-m-d H:i:s");
  568. $upd = Db::name("purchease_order")
  569. ->whereIn('cgdNo', $cgdNo)
  570. ->save([
  571. 'status' => $status,
  572. 'remark' => $remark,
  573. 'updatetime' => date("Y-m-d H:i:s"),
  574. ]);
  575. if ($upd) {
  576. $process = ["order_code" => implode(',', $cgdNo), "order_id" => 0, "order_status" => $status, "order_type" => 'CGD',"before_status"=>0];
  577. ProcessOrder::AddProcess($this->post['token'], $process);
  578. $order = ["order_code" => implode(',', $cgdNo), "status" => '', "action_remark" => $remark, "action_type" => "status"];
  579. ActionLog::logAdd($this->post['token'], $order, 'CGD', $status, $this->post);
  580. return app_show(0, "更新成功");
  581. } else {
  582. return error_show(1004, '更新失败');
  583. }
  584. }
  585. //创建采购工差单
  586. public function diffcreat(){
  587. $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
  588. //$cgdNo=makeNo("CG");
  589. $cgdNo = $this->post['cgdNo'] && $this->post['cgdNo'] !=="" ? trim($this->post['cgdNo']) :"";
  590. if($cgdNo==""){
  591. return error_show(1002,"参数good_code不能为空");
  592. }
  593. $cg = Db::name('purchease_order')->where(['cgdNo'=>$cgdNo,'is_del'=>0])->find();
  594. if($cg==""){
  595. return error_show(1002,"未找到采购单数据");
  596. }
  597. if($cg['order_type']==3|| $cg['order_type']==4){
  598. $gd = Db::name("good_zixun")->where(["spuCode"=>$cg['spuCode']])->find();
  599. }else {
  600. $gd =Db::name('good_basic')->where(['spuCode'=>$cg['spuCode']])->find();
  601. }
  602. if($gd==""){
  603. return error_show(1002,"未找到商品数据");
  604. }
  605. $diff_weight = isset($this->post['diff_weight']) && $this->post['diff_weight'] !=="" ? floatval($this->post['diff_weight']) :"";
  606. if($diff_weight===""){
  607. return error_show(1002,"参数diff_weight不能为空");
  608. }
  609. $apply_id =GetUserInfo($token);
  610. if(empty($apply_id)||$apply_id['code']!=0){
  611. return error_show(1002,"申请人数据不存在");
  612. }
  613. $rid= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  614. $rname= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  615. $diff_price = round($diff_weight*$cg['gold_price']+$diff_weight*$cg['teach_fee'],2);
  616. //如果有未处理的工差单,不允许创建新的
  617. $wait_check = Db::name('purchease_diff')
  618. ->field('id')
  619. ->where(['apply_id'=>$rid,'status'=>1,'is_del'=>0])
  620. ->find();
  621. if(!empty($wait_check)) return error_show('1005','您还有未处理的工差单,请先处理');
  622. $data=[
  623. "cgdNo"=>$cgdNo,
  624. "good_code"=>$cg['spuCode'],
  625. "good_name"=>$cg['good_name'],
  626. "sale_price"=>$cg['good_price'],
  627. "good_weight"=>$cg['weight']*$cg['good_num'],
  628. "good_num"=>$cg['good_num'],
  629. "apply_id"=>$rid,
  630. "apply_name"=>$rname,
  631. "diff_weight"=>$diff_weight,
  632. "diff_price"=>$diff_price,
  633. "gold_price"=>$cg['good_price'],
  634. "status"=>1,
  635. "is_del"=>0,
  636. "addtime"=>date("Y-m-d H:i:s"),
  637. "updatetime"=>date("Y-m-d H:i:s")
  638. ];
  639. Db::startTrans();
  640. try{
  641. $item = Db::name("purchease_diff")->insert($data,true);
  642. if($item>0){
  643. $sto = ["order_code"=>$cgdNo,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  644. ActionLog::logAdd($this->post['token'],$sto,"CGGCD",1,$data);
  645. $process=["order_code"=>$cgdNo,"order_id"=>$item,"order_status"=>1,"order_type"=>'CGGCD',"before_status"=>1];
  646. ProcessOrder::AddProcess($this->post['token'],$process);
  647. $orderCode = Db::name("order_num")->where([["cgdNo","=",$cg['cgdNo']],["status","=",1]])->find();
  648. if($orderCode!=false){
  649. $order = Db::name("sale")->where("orderCode", '=', $orderCode['orderCode'])->find();
  650. if($order==false){
  651. Db::rollback();
  652. return error_show(1002,"未找到确认单信息");
  653. }
  654. $data=[
  655. "cgd_diffid"=>$item,
  656. "orderCode"=>$orderCode['orderCode'],
  657. "good_code"=>$cg['spuCode'],
  658. "good_name"=>$cg['good_name'],
  659. "diff_weight"=>$diff_weight,
  660. "diff_price"=>round($diff_weight*$order['gold_price']+$diff_weight*$order['cost_price'],2),
  661. "gold_price"=>$order['sale_price'],
  662. "status"=>1,
  663. "addtime"=>date("Y-m-d H:i:s"),
  664. "updatetime"=>date("Y-m-d H:i:s")
  665. ];
  666. $cgd = Db::name('sale_diff')->insert($data,true);
  667. if($cgd==0){
  668. // $sto = ["order_code"=>$cg['bkcode'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  669. // ActionLog::logAdd($this->post['token'],$sto,"ZXGCD",1,$sto);
  670. // $process=["order_code"=>$cg['bkcode'],"order_id"=>$cgd,"order_status"=>1,"order_type"=>'ZXGCD'];
  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. $sto = ["order_code"=>$orderCode['orderCode'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  679. ActionLog::logAdd($this->post['token'],$sto,"XSGCD",1,$data);
  680. $process=["order_code"=>$orderCode['orderCode'],"order_id"=>$cgd,"order_status"=>1,"order_type"=>'XSGCD',"before_status"=>1];
  681. ProcessOrder::AddProcess($this->post['token'],$process);
  682. }
  683. Db::commit();
  684. return error_show(0,"新建成功");
  685. }else{
  686. Db::rollback();
  687. return error_show(1002,"新建失败");
  688. }
  689. }catch (\Exception $e){
  690. Db::rollback();
  691. return error_show(1002,$e->getMessage());
  692. }
  693. }
  694. public function difflist(){
  695. $page = isset($this->post['page']) && $this->post['page'] !==""? intval($this->post['page']):"1";
  696. $size = isset($this->post['size']) && $this->post['size'] !==""? intval($this->post['size']):"10";
  697. $where = [['a.is_del',"=",0]];
  698. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo']):"";
  699. if($cgdNo!=""){
  700. $where[]=['a.cgdNo',"like", "%$cgdNo%"];
  701. }
  702. $status = isset($this->post['status']) && $this->post['status'] !==""? intval($this->post['status']):"";
  703. if($status!==""){
  704. $where[]=['a.status',"=",$status];
  705. }
  706. $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
  707. if ($start !="") {
  708. $where[]= ["a.addtime",'>=',$start];
  709. }
  710. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
  711. if($end !=""){
  712. $where[]= ["a.addtime",'<=',$end];
  713. }
  714. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !==""? intval($this->post['apply_name']):"";
  715. if($apply_name!==""){
  716. $where[]=['a.apply_name',"like","%$apply_name%"];
  717. }
  718. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  719. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  720. $count = Db::name('purchease_diff')->alias('a')->join("good b","b.spuCode=a.good_code","left")
  721. ->where($where)->count();
  722. $total = ceil($count/$size);
  723. $page = $page >= $total ? $total : $page;
  724. $list = Db::name('purchease_diff')
  725. ->alias('a')
  726. ->join("good b","b.spuCode=a.good_code","left")
  727. ->leftJoin("depart_user u", "u.uid=a.apply_id AND u.is_del=0")
  728. ->where($where)
  729. ->page($page,$size)
  730. ->order("a.addtime desc")
  731. ->field("a.*,b.cat_id,u.itemid")
  732. ->cursor();
  733. $data=[];
  734. foreach ($list as $value){
  735. $value['can']= isset($value['cat_id']) && $value['cat_id'] !=0 ? made($value['cat_id']):[];
  736. $value['company_name'] = implode('/', array_column(GetPart($value['itemid']), 'name'));
  737. $data[]=$value;
  738. }
  739. return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
  740. }
  741. public function diffinfo(){
  742. $id = isset($this->post['id']) && $this->post['id'] !=="" ? trim($this->post['id']) :"";
  743. if($id==""){
  744. return error_show(1002,"参数id 不能为空");
  745. }
  746. $idinf = Db::name('purchease_diff')->where(['id'=>$id,'is_del'=>0])->find();
  747. if($idinf==false){
  748. return error_show(1002,"未找到采购工差单数据");
  749. }
  750. $dn = Db::name('purchease_order')->where(['cgdNo'=>$idinf['cgdNo'],'is_del'=>0])->find();
  751. if($dn['order_type']==3|| $dn['order_type']==4){
  752. $goon = Db::name("good_zixun")->where(["spuCode"=>$dn['spuCode'],"is_del"=>0])->find();
  753. }else {
  754. $goon =Db::name('good_basic')->where(['spuCode'=>$dn['spuCode']])->find();
  755. }
  756. // $goon = Db::name('good')->where(['spuCode'=>$idinf['good_code']])->find();
  757. if(empty($goon)){
  758. return error_show(1002,"未找到商品数据");
  759. }
  760. $idinf['wsm_name'] ="";
  761. $idinf['wsm_supplier'] ="";
  762. $idinf['wsm_supplierNo'] ="";
  763. $idinf['wsm_contactor'] ="";
  764. $idinf['wsm_mobile'] ="";
  765. $idinf['addr_code'] ="";
  766. $idinf['wsm_addr'] ="";
  767. if($dn['wsm_code']!=""){
  768. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")
  769. ->leftJoin("warehouse_addr c","a.wsm_code=c.wsm_code and c.is_del=0")
  770. ->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();
  771. $idinf['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  772. $idinf['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  773. $idinf['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  774. $idinf['wsm_contactor'] =isset($wsmcode['wsm_contactor']) ? $wsmcode['wsm_contactor']:"";
  775. $idinf['wsm_mobile'] =isset($wsmcode['wsm_mobile']) ? $wsmcode['wsm_mobile']:"";
  776. $idinf['addr_code'] =isset($wsmcode['addr_code']) ? $wsmcode['addr_code']:"";
  777. $idinf['addr_cn'] =isset($wsmcode['addr_code']) ? GetAddr($wsmcode['addr_code']):"";
  778. $idinf['wsm_addr']=isset($wsmcode['wsm_addr']) ? $wsmcode['wsm_addr']:"";
  779. }
  780. $idinf['info'] =$dn;
  781. $int= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
  782. $idinf['can']=$int;
  783. $idinf['order_type']=$dn['order_type'];
  784. //取出销售的工差单信息
  785. $diff = Db::name('sale_diff')->field('id,customer_remark,is_act,remark')->where('cgd_diffid',$id)->find();
  786. $idinf['diff_customer_remark']=$diff['customer_remark'];
  787. $idinf['diff_is_act']=$diff['is_act'];
  788. $idinf['diff_remark']=$diff['remark'];
  789. return app_show(0,"获取成功",$idinf);
  790. }
  791. public function diffstatu(){
  792. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  793. if($id==""){
  794. return error_show(1002,"参数id不能为空");
  795. }
  796. $dio = Db::name('purchease_diff')->where(['id'=>$id,'is_del'=>0])->find();
  797. if(empty($dio)){
  798. return error_show(1002,"工差订单未找到");
  799. }
  800. $cgd = Db::name("purchease_order")->where(['cgdNo'=>$dio['cgdNo']])->find();
  801. if(empty($cgd)){
  802. return error_show(1002,"采购单未找到");
  803. }
  804. $salediff=Db::name("sale_diff")->where(["cgd_diffid"=>$dio['id']])->find();
  805. if($salediff==false){
  806. return error_show(1002,"销售工差单未找到");
  807. }
  808. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  809. if($status===""){
  810. return error_show(1002,"参数status不能为空");
  811. }
  812. Db::startTrans();
  813. try{
  814. $temp = $dio['status'];
  815. $dio['status']=$status;
  816. $dio['updatetime']=date("Y-m-d H:i:s");
  817. $item = Db::name("purchease_diff")->save($dio);
  818. if ($item){
  819. if($status==2){
  820. $cgd['diff_fee'] =$dio['diff_price'];
  821. $cgd['diff_weight'] =$dio['diff_weight'];
  822. $cgd['updatetime'] =date("Y-m-d H:i:s");
  823. $cgp=Db::name("purchease_order")->save($cgd);
  824. if($cgp==false){
  825. Db::rollback();
  826. return error_show(1002,"采购单更新失败");
  827. }
  828. if($salediff['is_act']==1){
  829. $saled=[
  830. "diff_fee"=>$salediff['diff_price'],
  831. "diff_weight"=>$salediff['diff_weight'],
  832. "updatetime"=>date("Y-m-d H:i:s")
  833. ];
  834. $up =Db::name("sale")->where(["orderCode"=>$salediff['orderCode']])->save($saled);
  835. if($up==false){
  836. Db::rollback();
  837. return error_show(1002,"销售单更新失败");
  838. }
  839. }
  840. }
  841. $sto = ["order_code"=>$dio['cgdNo'],"status"=>1,"action_remark"=>'',"action_type"=>"create"];
  842. ActionLog::logAdd($this->post['token'],$sto,"CGGCD",$status,$this->post);
  843. $process=["order_code"=>$dio['cgdNo'],"order_id"=>$dio['id'],"order_status"=>$status,"order_type"=>'CGGCD',"before_status"=>1];
  844. ProcessOrder::AddProcess($this->post['token'],$process);
  845. Db::commit();
  846. return error_show(0,"更新成功");
  847. }else{
  848. Db::rollback();
  849. return error_show(1002,"更新失败");
  850. }
  851. }catch (\Exception $e){
  852. Db::rollback();
  853. return error_show(1002,$e->getMessage()."|".$e->getLine());
  854. }
  855. }
  856. //修改采购单的实时金价
  857. public function editGoldPrice()
  858. {
  859. $param = $this->request->only(['cgdNo', 'gold_price'], 'post', 'trim');
  860. $val = Validate::rule(['cgdNo|采购单号' => 'require', 'gold_price|实时金价' => 'require|float']);
  861. if (!$val->check($param)) return error_show(1004, $val->getError());
  862. //采购单
  863. $data = Db::name("purchease_order")
  864. ->field('id,demo_fee,good_num,weight,open_fee,teach_fee,pakge_fee,mark_fee,cert_fee,nake_fee,delivery_fee,status')
  865. ->where(["cgdNo" => $param['cgdNo'], "is_del" => 0])
  866. ->find();
  867. if (empty($data)) return error_show(1004, "未找到该采购单数据");
  868. if ($data['status'] != 0) return error_show(1005, '该状态下不允许修改实时金价');
  869. $sale_price = round($data['demo_fee'] / $data['good_num'] + $data['open_fee'] / $data['good_num'] +$data['weight'] * $param["gold_price"] + $data['teach_fee'] * $data['weight'] +
  870. $data['pakge_fee'] + $data['mark_fee'] + $data['cert_fee'] +$data['nake_fee'] + $data['delivery_fee'],2);
  871. $total_price = round($data['good_num']*$sale_price,2);
  872. $rs = Db::name("purchease_order")
  873. ->where('id', $data['id'])
  874. ->update(['updatetime' => date('Y-m-d H:i:s'), 'gold_price' => $param['gold_price'],"good_price"=>$sale_price,"total_fee"=>$total_price]);
  875. return $rs ? app_show(0, '修改采购单实时金价成功') : error_show(1005, '修改采购单实时金价失败');
  876. }
  877. //采购单导出
  878. public function exportCgdList()
  879. {
  880. $cgdNos = $this->request->post('cgdNos', [], 'trim');
  881. if (empty($cgdNos)) return error_show(1004, '要导出的采购单编号不能为空');
  882. $i = 1;
  883. $send_type = [1 => '直接发货', 2 => '延时发货'];
  884. $status = [0 => '待与供应商确认', 1 => '待入库', 2 => '部分入库', 3 => '入库完成', 4 => '已取消订单'];
  885. $list = Db::name("purchease_order")
  886. ->alias('po')
  887. ->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 到货时间,"" 税率,oa.addr 收货地址,oa.mobile 电话,oa.contactor 联系人,s.platform_order 平台订单编号, oa.addr_code,po.order_type')
  888. ->whereIn('po.cgdNo', $cgdNos)
  889. ->leftJoin('order_num on', 'on.cgdNo=po.cgdNo')
  890. ->leftJoin('sale s', 's.orderCode=on.orderCode AND s.is_del=0')
  891. ->leftJoin('order_addr oa', 'oa.orderCode=on.orderCode AND oa.is_del=0')
  892. ->leftJoin('business b', 'b.companyNo=s.supplierNo AND b.is_del=0')
  893. ->order("po.addtime desc")
  894. ->withAttr('序号', function () use (&$i) {
  895. return $i++;
  896. })->withAttr('采购单状态', function ($val) use ($status) {
  897. return isset($status[$val]) ? $status[$val] : '';
  898. })->withAttr('发货方式', function ($val) use ($send_type) {
  899. return isset($send_type[$val]) ? $send_type[$val] : '';
  900. })->withAttr('发货方式', function ($val) use ($send_type) {
  901. return isset($send_type[$val]) ? $send_type[$val] : '';
  902. })
  903. ->select()
  904. ->toArray();
  905. foreach ($list as &$value) {
  906. if ($value['order_type'] == 3 || $value['order_type'] == 4) {
  907. $temp = Db::name("good_zixun")
  908. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  909. ->field('id,specinfo,good_unit,tax')
  910. ->find();
  911. $good_unit = isset($temp['good_unit']) ? $temp['good_unit'] : 0;
  912. $specinfo = isset($temp['specinfo']) ? json_decode($temp['specinfo'], true) : [];
  913. $speclist = [];
  914. foreach ($specinfo as $val) {
  915. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  916. }
  917. $value['税率'] = $temp['tax'] . '%';
  918. } else {
  919. $good_unit = Db::name('good_basic')
  920. ->field('id,good_unit,tax')
  921. ->where(['spuCode' => $value['产品编号']])
  922. ->find();
  923. $spec = Db::name("good_spec")
  924. ->field('id,spec_id,spec_value_id')
  925. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  926. ->select()
  927. ->toArray();
  928. $speclist = [];
  929. if (!empty($spec)) {
  930. foreach ($spec as $val) {
  931. $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', '');
  932. }
  933. }
  934. $value['税率'] = isset($good_unit['tax']) ? $good_unit['tax'] . '%' : '';
  935. }
  936. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  937. $value['单位'] = isset($good_unit['good_unit']) ? Db::name('unit')->where(['id' => $good_unit['good_unit'], 'is_del' => 0])->value('unit', '') : '';
  938. if (!empty($value['addr_code'])) {
  939. $temp = explode(',', $value['addr_code']);
  940. $temp_ = GetAddr(json_encode(['provice_code' => $temp[0], 'city_code' => $temp[1], 'area_code' => $temp[2]]));
  941. $value['收货地址'] = $temp_ . $value['收货地址'];
  942. }
  943. unset($value['addr_code']);
  944. unset($value['order_type']);
  945. }
  946. $headerArr = array_keys($list[0]);
  947. excelSave('采购单导出' . date('YmdHis'), $headerArr, $list);
  948. }
  949. //获取采购单修改供应商的记录列表
  950. public function getChangeSupplierList()
  951. {
  952. $param = $this->request->only(['cgdNo' => '', 'start' => '', 'end' => '', 'creater' => '', 'page' => 1, 'size' => 15, 'company_name' => ''], 'post', 'trim');
  953. $where = [['a.is_del', '=', 0]];
  954. if (!empty($param['cgdNo'])) $where[] = ['a.cgdNo', 'like', '%' . $param['cgdNo'] . '%'];
  955. if (!empty($param['start'])) $where[] = ['a.addtime', '>=', $param['start']];
  956. if (!empty($param['end'])) $where[] = ['a.addtime', '<=', $param['end'] . ' 23:59:59'];
  957. if (!empty($param['creater'])) $where[] = ['a.creater', 'like', '%' . $param['creater'] . '%'];
  958. if (!empty($param['company_name'])) $where[] = ["a.creater_id", 'in', get_company_item_user_by_name($param['company_name'])];
  959. $count = Db::name('purchease_change_supplier_log')
  960. ->alias('a')
  961. ->where($where)
  962. ->count('a.id');
  963. $list = Db::name('purchease_change_supplier_log')
  964. ->alias('a')
  965. ->field('a.*,u.itemid')
  966. ->leftJoin("depart_user u", "u.uid=a.creater_id AND u.is_del=0")
  967. ->where($where)
  968. ->order('a.id', 'desc')
  969. ->page($param['page'], $param['size'])
  970. ->append(['company_name'])
  971. ->withAttr('company_name', function ($val, $data) {
  972. return implode('/', array_column(GetPart($data['itemid']), 'name'));
  973. })
  974. ->select()
  975. ->toArray();
  976. return app_show(0, '获取成功', ['list' => $list, 'count' => $count]);
  977. }
  978. // public function create(){
  979. // $customer = isset($this->post['customer_code'])&&$this->post['customer_code']!="" ?trim($this->post['customer_code']):"";
  980. // if($customer==""){
  981. // return error_show(1004,"参数customer_code不能为空");
  982. // }
  983. // $supplier = isset($this->post['supplier_code'])&&$this->post['supplier_code']!="" ?trim($this->post['supplier_code']):"";
  984. // if($supplier==""){
  985. // return error_show(1004,"参数supplier_code不能为空");
  986. // }
  987. // $good_code = isset($this->post['good_code']) && $this->post['good_code']!=""?trim($this->post['good_code']):"";
  988. // if($good_code==""){
  989. // return error_show(1004,"参数good_code不能为空");
  990. // }
  991. // $good_num = isset($this->post['good_num'])&& $this->post['good_num']!==""? intval($this->post['good_num']):"";
  992. // if($good_num===""){
  993. // return error_show(1004,"参数good_code不能为空");
  994. // }
  995. // $file_url = isset($this->post['file_url'])&& $this->post['file_url']!==""? trim($this->post['file_url']):"";
  996. // if($file_url===""){
  997. // return error_show(1004,"参数file_url不能为空");
  998. // }
  999. // $mark =isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  1000. // if($mark==""){
  1001. // return error_show(1004,"参数remark不能为空");
  1002. // }
  1003. //
  1004. // $data=[
  1005. // ""
  1006. // ];
  1007. // }
  1008. }