Purch.php 59 KB

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