Purch.php 55 KB

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