Check.php 35 KB

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