Check.php 35 KB

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