Check.php 30 KB

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