Check.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ProcessOrder;
  4. use app\BaseController;
  5. use think\App;
  6. use think\facade\Db;
  7. use app\admin\model\ActionLog;
  8. //盘点 wsm_good_check(主表,记录了哪个仓库,那个人,什么时间)和wsm_check_item(盘点的商品明细)
  9. class Check extends BaseController
  10. {
  11. public $post = "";
  12. public function __construct(App $app)
  13. {
  14. parent::__construct($app);
  15. $this->post=$this->request->post();
  16. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  17. if($token==""){
  18. return error_show(101,'token不能为空');
  19. }
  20. $effetc = VerifyTokens($token);
  21. if(!empty($effetc) && $effetc['code']!=0){
  22. return error_show($effetc['code'],$effetc['message']);
  23. }
  24. }
  25. public function flist(){
  26. $page = isset($this->post['page']) &&$this->post['page'] !=="" ? intval($this->post['page']):1;
  27. $size = isset($this->post['size']) &&$this->post['size'] !=="" ? intval($this->post['size']):10;
  28. $condtion=[['b.is_del',"=",0],['c.is_del',"=",0]];
  29. $wsmcode =isset($this->post['wsm_code']) &&$this->post['wsm_code'] !=="" ? trim($this->post['wsm_code']):"";
  30. if($wsmcode!==""){
  31. //return error_show(1002,"仓库code不能为空");
  32. $condtion[]=['c.wsm_code',"=",$wsmcode];
  33. // $typecode= Db::name("good_stock")->where(["wsm_code"=>$wsmcode,'is_del'=>0])->column("good_type_code");
  34. }
  35. // $good_code =isset($this->post['type_code']) &&$this->post['type_code'] !=="" ? trim($this->post['type_code']):"";
  36. // if($good_code!=""){
  37. // $condtion[]=['b.type_code',"=",$good_code];
  38. // }
  39. $goodcode =isset($this->post['good_code']) &&$this->post['good_code'] !=="" ? trim($this->post['good_code'])
  40. :"";
  41. if($goodcode!=""){
  42. // $condtion['a.good_code'] = Db::raw("like '%{$goodcode}%'");
  43. $condtion[]=['b.spuCode',"like","%{$goodcode}%"];
  44. }
  45. $good_name =isset($this->post['good_name']) &&$this->post['good_name'] !=="" ? trim($this->post['good_name'])
  46. :"";
  47. if($good_name!=""){
  48. // $condtion['a.good_name'] = Db::raw("like '%{$good_name}%'");
  49. $condtion[]=['b.good_name',"like","%{$good_name}%"];
  50. }
  51. $supplierNo =isset($this->post['supplierNo']) &&$this->post['supplierNo'] !=="" ? trim($this->post['supplierNo']):"";
  52. if($supplierNo!=""){
  53. $supplier = Db::name("supplier")->where(["code"=>$supplierNo])->find();
  54. if(empty($supplier)){
  55. return error_show(1004,"未找到供应商信息");
  56. }
  57. $wsmcode = Db::name("warehouse_info")->where(["is_del"=>0,"supplierNo"=>$supplierNo])->column("wsm_code");
  58. $condtion[]=['c.wsm_code',"in",$wsmcode];
  59. }
  60. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  61. if ($companyNo !== "") {
  62. $wsmcode = Db::name("warehouse_info")->where(['companyNo'=>$companyNo,"is_del"=>0])->column("wsm_code");
  63. $condition[]=["c.wsm_code","in",$wsmcode];
  64. }
  65. $stock_low =isset($this->post['stock_low']) &&$this->post['stock_low'] !=="" ? intval($this->post['stock_low'])
  66. :"";
  67. if($stock_low!=""){
  68. $condtion[]=['c.usable_stock',">=",$stock_low];
  69. }
  70. $stock_max =isset($this->post['stock_max']) &&$this->post['stock_max'] !=="" ? intval($this->post['stock_max'])
  71. :"";
  72. if($stock_max!=""){
  73. $condtion[]=['c.usable_stock',">=",$stock_max];
  74. }
  75. $stock_up =isset($this->post['stock_up']) &&$this->post['stock_up'] !=="" ? trim($this->post['stock_up'])
  76. :"";
  77. if($stock_up!=""){
  78. $condtion[]=['c.usable_stock',"<=",$stock_up];
  79. }
  80. $warn_low =isset($this->post['warn_low']) &&$this->post['warn_low'] !=="" ? intval($this->post['warn_low'])
  81. :"";
  82. if($warn_low!==""){
  83. $condtion[]=['c.warn_stock',">=",$warn_low];
  84. }
  85. $warn_up =isset($this->post['warn_up']) &&$this->post['warn_up'] !=="" ? intval($this->post['warn_up'])
  86. :"";
  87. if($warn_up!=""){
  88. $condtion[]=['c.warn_stock',"<=",$warn_up];
  89. }
  90. $cat_id =isset($this->post['cat_id']) &&$this->post['cat_id'] !=="" ? intval($this->post['cat_id']) :"";
  91. if($cat_id!=""){
  92. $at = Db::name('cat')->where(['id'=>$cat_id])->find();
  93. if(empty($at)){
  94. return error_show(1004,"未找到分类信息");
  95. }
  96. $cat=manger([$at['id']],$at['level']);
  97. $condtion[]=['b.cat_id',"in",$cat];
  98. }
  99. $count = Db::name("good")->alias("b")
  100. ->join("good_stock c","c.spuCode=b.spuCode","left")
  101. ->where($condtion)->count();
  102. $page>=ceil($count/$size) ? $page=ceil($count/$size) :"";
  103. $list = Db::name("good")
  104. ->alias("b")
  105. ->join("good_stock c","c.spuCode=b.spuCode","left")
  106. ->where($condtion)
  107. ->page($page,$size)
  108. ->select()
  109. ->toArray();
  110. $data=[];
  111. if($list) {
  112. //品牌信息
  113. $brands = Db::name('brand')->whereIn('id', array_column($list, 'brand_id'))->where('is_del', 0)->column('brand_name', 'id');
  114. foreach ($list as $key=>$value){
  115. $supplier = Db::name("supplier")->where(["code"=>$value['supplierNo']])->find();
  116. $value['supplier_name'] =isset($supplier['name']) ?$supplier['name'] :"";
  117. $wsmcode = Db::name("warehouse_info")->alias("a")->leftJoin("supplier b","a.supplierNo=b.code")->where(["a.wsm_code"=>$value['wsm_code']])->field("a.name as wsm_name,b.name,b.code")->find();
  118. $value['wsm_name'] =isset($wsmcode['wsm_name']) ? $wsmcode['wsm_name']:"";
  119. $value['wsm_supplier'] =isset($wsmcode['name']) ? $wsmcode['name']:"";
  120. $value['wsm_supplierNo'] =isset($wsmcode['code']) ? $wsmcode['code']:"";
  121. $value['can']= isset($value['cat_id']) && $value['cat_id'] !=0 ? made($value['cat_id']):[];
  122. $value['brand_name'] = $brands[$value['brand_id']]??'';
  123. //规格信息
  124. $spec = Db::name("good_spec")->where(["spuCode" => $value['spuCode'], "is_del" => 0])->select()->toArray();
  125. $speclist = [];
  126. if (!empty($spec)) {
  127. foreach ($spec as $val) {
  128. $temp = [];
  129. $temp['spec_id'] = $val['spec_id'];
  130. $temp['spec_value_id'] = $val['spec_value_id'];
  131. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  132. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  133. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  134. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  135. $speclist[] = $temp;
  136. }
  137. }
  138. $value['specinfo'] = $speclist;
  139. $data[]=$value;
  140. }
  141. }
  142. return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  143. }
  144. public function list()
  145. {
  146. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  147. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  148. $where = [['a.is_del',"=",0],['b.is_del',"=",0]];
  149. $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
  150. if ($start !="") {
  151. // $where["a.addtime"]=Db::raw(">= '{$start}'");
  152. $where[]=['a.addtime',">=",$start];
  153. }
  154. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
  155. if($end !=""){
  156. // $where["a.addtime"] = Db::raw("<= '{$end}'");
  157. $where[]=['a.addtime',"<=",$end];
  158. }
  159. $check_code = isset($this->post['check_code']) && $this->post['check_code'] !== "" ? trim($this->post['check_code']) : "";
  160. if ($check_code !== "") {
  161. // $where['a.check_code'] = Db::raw("like '%$check_code%'");
  162. $where[]=['a.check_code',"like","%$check_code%"];
  163. }
  164. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] !== "" ? trim($this->post['wsm_code']) : "";
  165. if ($wsm_code !== "") {
  166. // $where['a.wsm_code'] = Db::raw("like '%$wsm_code%'");
  167. $where[]=['a.wsm_code',"like","%$wsm_code%"];
  168. }
  169. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !== "" ? trim($this->post['apply_name']) : "";
  170. if ($apply_name !== "") {
  171. // $where['a.apply_name'] = $apply_name;
  172. $where[]=['a.apply_name',"like","%$apply_name%"];
  173. }
  174. $type = isset($this->post['type']) && $this->post['type'] !== "" ? trim($this->post['type']) : "";
  175. if ($type !== "") {
  176. // $where['a.type'] = $type;
  177. $where[]=['a.type',"=",$type];
  178. }
  179. $apply_id = isset($this->post['apply_id']) && $this->post['apply_id'] !== "" ? intval($this->post['apply_id']) : "";
  180. if ($apply_id!== "") {
  181. // $where['a.apply_id'] = $apply_id;
  182. $where[]=['a.apply_id',"=",$apply_id];
  183. }
  184. $status= isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  185. if ($status !== "") {
  186. // $where['a.status '] = $status;
  187. $where[]=['a.status',"=",$status];
  188. }
  189. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  190. if ($companyNo !== "") {
  191. $condition[]=["a.companyNo ","=",$companyNo ];
  192. }
  193. $count = Db::name('good_check')->alias('a')->join("warehouse_info b","a.wsm_code = b.wsm_code","left")
  194. ->join("supplier v","v.code=b.supplierNo","left")
  195. ->where($where)->count();
  196. $total = ceil($count / $size);
  197. $page = $page <= $total ? $page : $total;
  198. $list = Db::name('good_check')->alias('a')->join("warehouse_info b","a.wsm_code = b.wsm_code","left")
  199. ->join("supplier v","v.code=b.supplierNo","left")
  200. ->field("a.status,a.apply_id,a.apply_name,a.type,a.addtime,a.wsm_code,a.check_code,b.name,a.id,v.name as 'caname',v.code")->where($where)->page($page,$size)->order("a.id desc")->select();
  201. $data=[];
  202. foreach ($list as $key=>$value){
  203. $value['rename'] ='';
  204. if($value['apply_id']!=0){
  205. $depart = Db::name("depart_user")->alias("a")->leftJoin("company_item b","a.itemid = b.id")->where(['a.uid'=>$value['apply_id'],'a.is_del'=>0])
  206. ->column('b.name');
  207. // var_dump(Db::name("depart_user")->getLastSql());
  208. $value['rename'] =implode(",",$depart);
  209. }
  210. $data[] = $value;
  211. }
  212. return app_show(0,"获取成功",['count'=>$count,'list'=>$data]);
  213. }
  214. public function goodlist(){
  215. $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']) :"1";
  216. $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']) :"10";
  217. $where = ['is_del'=>0];
  218. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  219. if($id==""){
  220. return error_show(1002,"参数id不能为空");
  221. }
  222. $fo = Db::name('good_check')->alias('a')->join('warehouse_info b',"b.wsm_code=a.wsm_code","left")
  223. ->join('supplier c',"c.code=b.supplierNo","left")->field("c.name,c.code,a.*")
  224. ->where(['a.id'=>$id,'a.is_del'=>0])->find();
  225. if(empty($fo)){
  226. return error_show(1003,"未找到盘点信息");
  227. }
  228. if ($fo['check_code'] !== "") {
  229. $where['check_code'] = $fo['check_code'];
  230. }
  231. $count = Db::name('check_item')->where($where)->count();
  232. $total = ceil($count / $size);
  233. $page = $page >= $total ? $total: $page;
  234. $list = Db::name('check_item')->where($where)->page($page,$size)->select();
  235. return app_show(0,"获取成功",['count'=>$count,'list'=>$list]);
  236. }
  237. public function create(){
  238. $token =isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  239. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] !=="" ? trim($this->post['wsm_code']):"";
  240. if($wsm_code==""){
  241. return error_show(1002,"盘点仓库编号不能为空");
  242. }
  243. $type = isset($this->post['type']) && $this->post['type'] !=="" ? intval($this->post['type']) :"";
  244. if($type==""){
  245. return error_show(1002,"盘点类型不能为空");
  246. }
  247. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !=="" ? trim($this->post['companyNo']) :"";
  248. if($companyNo==""){
  249. return error_show(1002,"参数companyNo不能为空");
  250. }
  251. $company =Db::name("business")->where(['companyNo'=>$companyNo,"is_del"=>0])->find();
  252. if($company==false){
  253. return error_show(1002,"未找到业务公司");
  254. }
  255. $check_code = makeNo("CK");
  256. $apply_id=GetUserInfo($token);
  257. if(empty($apply_id)||$apply_id['code']!=0){
  258. return error_show(1002,"申请人数据不存在");
  259. }
  260. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  261. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  262. $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"0";
  263. $remark = isset($this->post['remark']) && $this->post['remark'] !=="" ? intval($this->post['remark']) :"";
  264. Db::startTrans();
  265. try{
  266. $data=[
  267. "check_code"=>$check_code,
  268. "wsm_code"=>$wsm_code,
  269. "type"=>$type,
  270. "status"=>$status,
  271. "companyNo"=>$companyNo,
  272. "remark"=>$remark,
  273. "apply_id"=>$rm,
  274. "apply_name"=>$ri,
  275. "is_del"=>0,
  276. "addtime"=>date('Y-m-d H:i:s'),
  277. "updatetime"=>date("Y-m-d H:i:s")
  278. ];
  279. $da= Db::name('good_check')->insert($data,true);
  280. // $dio=[];
  281. // if($da>0){
  282. // foreach ($dain as $value){
  283. // $st=Db::name("good_type")->alias("b")->join("good a","a.good_code = b.good_code","left")
  284. // ->join("good_stock c","c.good_type_code = b.type_code","left")->where(['c.wsm_code'=>$wsm_code,'good_type_code'=>$value['type_code'],'b.is_del'=>0,'a.is_del'=>0])->where("c.is_del=0 or c.is_del is null")
  285. // ->field("b.type_code,a.good_name,c.wsm_code,c.wait_in_stock,c.wait_out_stock,c.usable_stock,c.good_type_code,a.original_price")->find();
  286. //
  287. // $temp=[];
  288. // $temp['good_name']=$st['good_name'];
  289. // $temp['origin_price']=$st['original_price'];
  290. // $temp['good_type_code']=$st['good_type_code'];
  291. // $temp['origin_num']=$st['usable_stock'];
  292. // $temp['check_num']=0;
  293. // $temp['diff_num']=0;
  294. // $temp['status']=0;
  295. // $temp['remark']="";
  296. // $temp['is_del']=0;
  297. // $temp['check_time']=date('Y-m-d H:i:s');
  298. // $temp['check_code']=$check_code;
  299. // $temp['addtime']=date("Y-m-d H:i:s");
  300. // $temp['updatetime']=date("Y-m-d H:i:s");
  301. // $dio[] =$temp;
  302. // }
  303. // $in= Db::name('check_item')->insertAll($dio);
  304. // }
  305. if($da){
  306. $orde = ["order_code"=>$check_code,"status"=>$data['status'],"action_remark"=>'',"action_type"=>"create"];
  307. ActionLog::logAdd($this->post['token'],$orde,'qrd',$data['status'],$orde);
  308. $process=["order_code"=>$check_code,"order_id"=>$da,"order_status"=>$data['status'],"order_type"=>'PDD'];
  309. ProcessOrder::AddProcess($this->post['token'],$process);
  310. Db::commit();
  311. return error_show(0,"盘点创建成功");
  312. }else{
  313. Db::rollback();
  314. return error_show(1002,"创建失败");
  315. }
  316. }catch (\Exception $e){
  317. Db::rollback();
  318. return error_show(1005,$e->getMessage());
  319. }
  320. }
  321. public function info(){
  322. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  323. if($id==""){
  324. return error_show(1002,"参数id不能为空");
  325. }
  326. $fo = Db::name('good_check')->alias('a')->join('warehouse_info b',"b.wsm_code=a.wsm_code","left")
  327. ->join('supplier c',"c.code=b.supplierNo","left")->field("c.name,c.code,a.*")
  328. ->where(['a.id'=>$id,'a.is_del'=>0])->find();
  329. if(empty($fo)){
  330. return error_show(1003,"未找到盘点信息");
  331. }
  332. $fi = Db::name('check_item')->where(['check_code'=>$fo['check_code'],'is_del'=>0])->select();
  333. $fo['item']=$fi;
  334. if(empty($fo)){
  335. return error_show(1002,"未找到盘点编号");
  336. }else{
  337. return app_show(0,"获取成功",$fo);
  338. }
  339. }
  340. public function edit()
  341. {
  342. $token =isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
  343. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  344. $eid = Db::name('good_check')->where(['id' => $id, 'is_del' => 0])->find();
  345. if ($eid ==="") {
  346. return error_show(1002, "未找到盘点编号信息");
  347. }
  348. $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] !== "" ? trim($this->post['wsm_code']) : "";
  349. if ($wsm_code == "") {
  350. return error_show(1002, "盘点仓库不能为空");
  351. }
  352. $type = isset($this->post['type']) && $this->post['type'] !== "" ? trim($this->post['type']) : "";
  353. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  354. if ($type == "") {
  355. return error_show(1002, "盘点类型不能为空");
  356. }
  357. $apply_id=GetUserInfo($token);
  358. if(empty($apply_id)||$apply_id['code']!=0){
  359. return error_show(1002,"申请人数据不存在");
  360. }
  361. $rm= isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  362. $ri= isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  363. // $dain=isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
  364. // if($type==2){
  365. // // var_dump($dain);
  366. // if($dain==""||empty($dain)){
  367. // return error_show(1002,"商品不能为空");
  368. // }
  369. // }else{
  370. // if($type==1) {
  371. //
  372. // $dain= Db::name("good_type")->alias("b")->join("good a","a.good_code = b.good_code","left")
  373. // ->join("good_stock c","c.good_type_code = b.type_code","left")->where(['c.wsm_code'=>$wsm_code,'b.is_del'=>0,'a.is_del'=>0])->where("c.is_del=0 or c.is_del is null")
  374. // ->field("b.type_code,a.good_name")->select();
  375. // //var_dump($dain);
  376. // }
  377. // if($dain==""){
  378. // return error_show(1003,"商品不能为空");
  379. // }
  380. // }
  381. Db::startTrans();
  382. try {
  383. $var = [
  384. "wsm_code" => $wsm_code,
  385. "type" => $type,
  386. "apply_id" => $rm,
  387. "apply_name" => $ri,
  388. "remark" => $remark,
  389. "updatetime" => date("Y-m-d H:i:s")
  390. ];
  391. $up = Db::name('good_check')->where(['id'=>$id,'is_del' => 0])->save($var);
  392. // $dn =[];
  393. // if($up>0){
  394. // foreach ($dain as $value){
  395. // $st=Db::name("good_type")->alias("b")->join("good a","a.good_code = b.good_code","left")
  396. // ->join("good_stock c","c.good_type_code = b.type_code","left")->where(['c.wsm_code'=>$wsm_code,'good_type_code'=>$value['type_code'],'b.is_del'=>0,'a.is_del'=>0])->where("c.is_del=0 or c.is_del is null")
  397. // ->field("b.type_code,a.good_name,c.wsm_code,c.wait_in_stock,c.wait_out_stock,c.usable_stock,c.good_type_code,a.original_price")->find();
  398. // $temp=[];
  399. // $temp['good_name']=$st['good_name'];
  400. // $temp['origin_price']=$st['original_price'];
  401. // $temp['good_type_code']=$st['good_type_code'];
  402. // $temp['origin_num']=$st['usable_stock'];
  403. // $temp['check_num']=0;
  404. // $temp['diff_num']=0;
  405. // $temp['status']=0;
  406. // $temp['remark']="";
  407. // $temp['is_del']=0;
  408. // $temp['check_time']=date('Y-m-d H:i:s');
  409. // $temp['check_code']=$eid['check_code'];
  410. // $temp['addtime']=date("Y-m-d H:i:s");
  411. // $temp['updatetime']=date("Y-m-d H:i:s");
  412. // $dn[] =$temp;
  413. // }
  414. // $np = Db::name('check_item')->where(['check_code'=>$eid['check_code'],'is_del'=>0])->update(['is_del'=>1,'updatetime'=>date('Y-m-d H:i:s')]);
  415. // $io = db::name('check_item')->insertAll($up);
  416. // var_dump(Db::name('check_item')->getLastSql());
  417. if ($up) {
  418. $orde = ["order_code"=>$eid['check_code'],"status"=>$eid['status'],"action_remark"=>'',"action_type"=>"edit"];
  419. ActionLog::logAdd($this->post['token'],$orde,'qrd',$eid['status'],$orde);
  420. Db::commit();
  421. return error_show(0, "盘点更新成功");
  422. }
  423. Db::rollback();
  424. return error_show(1003,"盘点更新失败");
  425. } catch (\Exception $e) {
  426. Db::rollback();
  427. return error_show(1005, $e->getMessage());
  428. }
  429. }
  430. /**
  431. * @return \think\response\Json|void
  432. * @throws \think\db\exception\DataNotFoundException
  433. * @throws \think\db\exception\DbException
  434. * @throws \think\db\exception\ModelNotFoundException
  435. */
  436. public function dell(){
  437. $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  438. if($id===""){
  439. return error_show(1004,"参数id不能为空");
  440. }
  441. $deln= Db::name('good_check')->where(['id'=>$id,'is_del'=>0])->find();
  442. if(empty($deln)){
  443. return error_show(1002,"未找到数据");
  444. }
  445. $sv = Db::name('good_check')->update(['id'=>$id,'is_del'=>1,'updatetime'=>date("Y-m-d H:i:s")]);
  446. if($sv){
  447. $orde = ["order_code"=>$deln['check_code'],"status"=>$deln['status'],"action_remark"=>'',"action_type"=>"delete"];
  448. ActionLog::logAdd($this->post['token'],$orde,'qrd',$deln['status'],['id'=>$id,'is_del'=>1,'updatetime'=>date("Y-m-d H:i:s")]);
  449. $process=["order_code"=>$deln['check_code'],"order_status"=>$deln['status'],"order_type"=>'PDD'];
  450. ProcessOrder::workdel($process);
  451. return error_show(0,"删除成功");
  452. }else{
  453. return error_show(1002,"删除失败");
  454. }
  455. }
  456. /**
  457. * @return \think\response\Json|void
  458. * @throws \think\db\exception\DataNotFoundException
  459. * @throws \think\db\exception\DbException
  460. * @throws \think\db\exception\ModelNotFoundException
  461. */
  462. public function statu()
  463. {
  464. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  465. if($id==""){
  466. return error_show(1002,"盘点id不能为空");
  467. }
  468. $stauts = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
  469. $remark = isset($this->post['remark']) && $this->post['remark'] !=="" ? trim($this->post['remark']) :"";
  470. if($stauts===""){
  471. return error_show(1002,"状态不能为空");
  472. }
  473. $st = Db::name('good_check')->where(['id'=>$id,"is_del"=>0])->find();
  474. if(empty($st)){
  475. return error_show(1002,"盘点信息未找到");
  476. }
  477. Db::startTrans();
  478. try{
  479. $temp = $st['status'];
  480. $st['status']=$stauts;
  481. $st['remark']=$remark;
  482. $st['updatetime']= date('Y-m-d H:i:s');
  483. $sv = Db::name('good_check')->save($st);
  484. if($sv){
  485. $orde = ["order_code"=>$st['check_code'],$temp,"action_remark"=>'',"action_type"=>"status"];
  486. ActionLog::logAdd($this->post['token'],$orde,'PDD',$st['status'],$orde);
  487. if($stauts==1&&$temp==2){
  488. $up = Db::name('check_item')->where(['check_code'=>$st['check_code'],"is_del"=>0])->save(['is_del'=>1,
  489. "updatetime"=>date("Y-m-d H:i:s")]);
  490. if($up==false){
  491. Db::rollback();
  492. return error_show(1003,"盘点结果删除失败");
  493. }
  494. }
  495. if($stauts==2){
  496. $nu = Db::name('check_item')->where(['check_code'=>$st['check_code'],"is_del"=>0])->select()->toArray();
  497. if(empty($nu)){
  498. Db::rollback();
  499. return error_show(1003,"未上传盘点结果");
  500. }
  501. }
  502. $process=["order_code"=>$st['check_code'],"order_id"=>$st['id'],"order_status"=>$stauts,"order_type"=>'PDD'];
  503. ProcessOrder::AddProcess($this->post['token'],$process);
  504. Db::commit();
  505. return error_show(0,"状态更新成功");
  506. }
  507. Db::rollback();
  508. return error_show(1002,"状态更新失败");
  509. }catch (\Exception $e){
  510. Db::rollback();
  511. return error_show(1002,$e->getMessage());
  512. }
  513. }
  514. //导出(盘点的明细)
  515. public function exportGood(){
  516. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  517. if($id==""){
  518. return error_show(1002,"参数id不能为空");
  519. }
  520. $fo = Db::name('good_check')->alias('a')->join('warehouse_info b',"b.wsm_code=a.wsm_code","left")
  521. ->join('supplier c',"c.code=b.supplierNo","left")->field("c.name,c.code,b.name,a.*")
  522. ->where(['a.id'=>$id,'a.is_del'=>0])->find();
  523. if(empty($fo)){
  524. return error_show(1003,"未找到盘点信息");
  525. }
  526. $list = Db::name("good_basic")->alias("b")
  527. ->join("good_stock c","c.spuCode=b.spuCode","left")
  528. ->where(['c.wsm_code'=>$fo['wsm_code'],"b.is_del"=>0,"c.is_del"=>0])->field("b.spuCode'商品编码',
  529. b.good_name '商品名称',b.craft_desc '商品描述',b.brand_id '品牌',b.good_unit '单位',b.supplierNo '供应商编码','' as '供应商名称','' as '一级分类','' as '二级分类','' as '三级分类',
  530. b.addtime '新建时间',c.wsm_code '仓库编码','' as '仓库名称',c.usable_stock '可用库存','' as '盘点库存'")
  531. ->select();
  532. $data=[];
  533. foreach ($list as $key=>$value){
  534. $wsm = Db::name("warehouse_info")->where(['wsm_code'=>$value['仓库编码']])->find();
  535. $supplier = Db::name("supplier")->where(["code"=>$value['供应商编码']])->find();
  536. $value['供应商名称'] =isset($supplier['name']) ?$supplier['name'] :"";
  537. $value['仓库名称'] =isset($wsm['name']) ?$wsm['name'] :"";
  538. $unit =Db::name("unit")->where(["id"=>$value['单位']])->find();
  539. $brand =Db::name("brand")->where(["id"=>$value['品牌']])->find();
  540. $value['单位'] =isset($unit['unit']) ?$unit['unit'] :"";
  541. $value['品牌'] =isset($brand['brand_name']) ?$brand['brand_name'] :"";
  542. $data[]=$value;
  543. }
  544. if(empty($data)){
  545. $data[]=["暂无商品信息"=>''];
  546. }
  547. $headerArr= array_keys($data[0]);
  548. excelSave('盘点商品',$headerArr,$data);
  549. // return app_show(0,"获取成功",$data);
  550. }
  551. //导入
  552. public function importGood(){
  553. $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
  554. if($id==""){
  555. return error_show(1002,"参数id不能为空");
  556. }
  557. $fo = Db::name('good_check')->alias('a')->join('warehouse_info b',"b.wsm_code=a.wsm_code","left")
  558. ->join('supplier c',"c.code=b.supplierNo","left")->field("c.name,c.code,b.name,a.*")
  559. ->where(['a.id'=>$id,'a.is_del'=>0])->find();
  560. if(empty($fo)){
  561. return error_show(1003,"未找到盘点信息");
  562. }
  563. if($fo['status']>=2){
  564. return error_show(1003,"盘点已提交审核,不可提交盘点数据");
  565. }
  566. $data = isset($this->post['data']) && $this->post['data'] !=="" ?$this->post['data'] :"";
  567. if($data=="" || empty($data)){
  568. return error_show(1003,"盘点数据不能为空");
  569. }
  570. $insert=[];
  571. foreach ($data as $value){
  572. if($value["value1"]==""){
  573. return error_show(1003,"商品属性code 不能为空");
  574. }
  575. if($value["value15"]===""){
  576. continue;
  577. }
  578. $stock = Db::name("good_stock")->where(["good_type_code"=>$value["value1"],"wsm_code"=>$fo['wsm_code'],
  579. "is_del"=>0])->find();
  580. $stock_num = isset($stock) ? $stock['usable_stock']:0;
  581. $insert[]=[
  582. "good_type_code"=>$value["value1"],
  583. "good_name"=>$value["value2"],
  584. "origin_price"=>0,
  585. "origin_num"=>isset($stock) ? $stock['usable_stock']:0,
  586. "check_num"=>$value["value15"],
  587. "is_profit"=>intval($value["value15"])>=$stock_num ?0:1,
  588. "diff_num"=>intval($value["value15"])>=$stock_num ?intval($value["value15"])-$stock_num:$stock_num-intval($value["value15"]),
  589. 'addtime'=>date("Y-m-d H:i:s"),
  590. 'updatetime'=>date("Y-m-d H:i:s"),
  591. "check_code"=>$fo['check_code']
  592. ];
  593. }
  594. if(empty($insert)){
  595. return error_show(1003,"盘点数据不可用");
  596. }
  597. Db::startTrans();
  598. try{
  599. $inst = Db::name("check_item")->insertAll($insert);
  600. if($inst){
  601. $check_up = Db::name("good_check")->where(["check_code"=>$fo['check_code'],"is_del"=>0])->save(["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  602. if($check_up){
  603. $orde = ["order_code"=>$fo['check_code'],$fo['status'],"action_remark"=>'',"action_type"=>"edit"];
  604. ActionLog::logAdd($this->post['token'],$orde,'PDD',2,["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  605. $process=["order_code"=>$fo['check_code'],"order_id"=>$fo['id'],"order_status"=>2,"order_type"=>'PDD'];
  606. ProcessOrder::AddProcess($this->post['token'],$process);
  607. Db::commit();
  608. return app_show(0,"盘点数据导入成功");
  609. }
  610. }
  611. Db::rollback();
  612. return error_show(1002,"盘点数据导入失败");
  613. }catch (\Exception $e){
  614. Db::rollback();
  615. return error_show(1002,"盘点数据导入失败");
  616. }
  617. }
  618. }