Purch.php 61 KB

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