Purch.php 54 KB

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