Purch.php 54 KB

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