Purch.php 62 KB

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