Purch.php 62 KB

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