Check.php 30 KB

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