Purch.php 57 KB

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