Purch.php 62 KB

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