AssetCheck.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class AssetCheck extends Model
  8. {
  9. public function add(){
  10. $data = [
  11. 'user_id' => input('userId/d',0),
  12. 'org_id' => input('orgId/d',0),
  13. 'remark' => input('remark','','trim'),
  14. 'create_time' => date('Y-m-d H:i:s'),
  15. 'status' => 0,
  16. 'items' => input('items','','trim'),
  17. ];
  18. $result = validate('AssetCheck')->check($data,[],'');
  19. if(true !== $result){
  20. HelpHander::error(validate('AssetCheck')->getError());
  21. }
  22. $items = $data['items'];
  23. unset($data['items']);
  24. $items = explode(',',$items);
  25. $assets = Db::name('asset_items')
  26. ->where('id','in',$items)
  27. ->where('del',0)
  28. ->where('enable','in','1,2')
  29. ->field('id as items_id,user_id,dep_id,nums,sn,title,spec,price,unit_id')
  30. ->select();
  31. $assets = $assets?$assets:[];
  32. if(count($assets) != count($items)){
  33. HelpHander::error('有部分资产状态不可盘库');
  34. }
  35. Db::startTrans();
  36. try{
  37. $checkId = Db::name('asset_check')->insertGetId($data);
  38. if(!$checkId){
  39. \exception('操作失败');
  40. }
  41. $arr = [];
  42. foreach ($assets as $k=>$v){
  43. $v['check_id'] = $checkId;
  44. $arr[] = $v;
  45. if(count($arr) == 300){
  46. $res = Db::name('asset_check_items')->insertAll($arr);
  47. if(count($arr) != $res){
  48. \exception('操作失败');
  49. }
  50. }
  51. }
  52. if($arr){
  53. $res = Db::name('asset_check_items')->insertAll($arr);
  54. if(count($arr) != $res){
  55. \exception('操作失败');
  56. }
  57. }
  58. Db::commit();
  59. }catch (Exception $e){
  60. Db::rollback();
  61. HelpHander::error($e->getMessage());
  62. }
  63. return true;
  64. }
  65. public function info($id){
  66. $info = Db::name('asset_check')->where('id',$id)->where('del',0)->find();
  67. if(!$info){
  68. HelpHander::error('数据不存在');
  69. }
  70. $info['userName'] = Db::name('user_info')->where('user_id',$info['user_id'])->value('name');
  71. $info['totalNums'] = Db::name('asset_check_items')->where('check_id',$id)->where('del',0)->sum('nums');
  72. $info['totalCheckNums'] = Db::name('asset_check_items')->where('check_id',$id)->where('del',0)->sum('check_nums');
  73. $info['totalDiffNums'] = $info['totalCheckNums'] - $info['totalNums'];
  74. return $info;
  75. }
  76. public function lists($page,$size,$status,$orgId){
  77. $map[] = ['org_id','=',$orgId];
  78. $map[] = ['del','=',0];
  79. if($status >= 0){
  80. $map[] = ['status','=',$status];
  81. }
  82. $lists = $this
  83. ->where($map)
  84. ->page($page,$size)
  85. ->order('id desc')
  86. ->select();
  87. $lists = $lists?$lists->toArray():[];
  88. foreach ($lists as $k=>$v){
  89. $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['user_id'])->value('name');
  90. }
  91. $total = $this->where($map)->count();
  92. $data = [
  93. 'total' => $total,
  94. 'list' => $lists
  95. ];
  96. return $data;
  97. }
  98. public function del($id){
  99. $ret = $this->where('id',$id)->setField('del',1);
  100. if(!$ret){
  101. HelpHander::error('删除失败');
  102. }
  103. return true;
  104. }
  105. public function finish($id){
  106. $info = Db::name('asset_check')->where('id',$id)->where('del',0)->find();
  107. if(!$info){
  108. HelpHander::error('记录不存在');
  109. }
  110. if($info['status'] == 1){
  111. HelpHander::error('已完成无需重复操作');
  112. }
  113. $ret = Db::name('asset_check')->where('id',$id)->update(['status' => 1,'finish_time'=>date('Y-m-d H:i:s')]);
  114. if(!$ret){
  115. HelpHander::error('操作失败');
  116. }
  117. return true;
  118. }
  119. public function items($page,$size,$checkId,$status,$title){
  120. $map[] = ['check_id','=',$checkId];
  121. $map[] = ['del','=',0];
  122. if($title){
  123. $map[] = ['sn|title','like','%'.$title.'%'];
  124. }
  125. if($status >= 0){
  126. $map[] = ['status','=',$status];
  127. }
  128. $lists = Db::name('asset_check_items')
  129. ->where($map)
  130. ->page($page,$size)
  131. ->order('id asc')
  132. ->select();
  133. foreach ($lists as $k=>$v){
  134. $lists[$k]['depName'] = '';
  135. $lists[$k]['userName'] = '';
  136. if($v['dep_id']){
  137. $lists[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('name');
  138. $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['dep_id'])->value('name');
  139. }
  140. $lists[$k]['diffNums'] = $v['check_nums'] - $v['nums'];
  141. }
  142. $total = Db::name('asset_check_items')->where($map)->count();
  143. $data = [
  144. 'total' => $total,
  145. 'list' => $lists?$lists:[]
  146. ];
  147. return $data;
  148. }
  149. public function allItems($checkId){
  150. $map[] = ['check_id','=',$checkId];
  151. $map[] = ['del','=',0];
  152. $lists = Db::name('asset_check_items')
  153. ->where($map)
  154. ->order('id asc')
  155. ->select();
  156. foreach ($lists as $k=>$v){
  157. $lists[$k]['depName'] = '';
  158. $lists[$k]['userName'] = '';
  159. if($v['dep_id']){
  160. $lists[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('name');
  161. $lists[$k]['userName'] = Db::name('user_info')->where('user_id',$v['dep_id'])->value('name');
  162. }
  163. $lists[$k]['diffNums'] = $v['check_nums'] - $v['nums'];
  164. }
  165. $check = $this->info($checkId);
  166. $data = [
  167. 'check' => $check,
  168. 'list' => $lists?$lists:[]
  169. ];
  170. return $data;
  171. }
  172. public function delItems($id){
  173. $info = Db::name('asset_check_items')->where('id',$id)->where('del',0)->find();
  174. if(!$info){
  175. HelpHander::error('记录不存在');
  176. }
  177. $check = $this->info($info['check_id']);
  178. if($check['status'] == 1){
  179. HelpHander::error('盘点已完成,无法删除');
  180. }
  181. $ret = Db::name('asset_check_items')->where('id',$id)->setField('del',1);
  182. if(!$ret){
  183. HelpHander::error('操作失败');
  184. }
  185. return true;
  186. }
  187. public function finishItems($id){
  188. $info = Db::name('asset_check_items')->where('id',$id)->where('del',0)->find();
  189. if(!$info){
  190. HelpHander::error('记录不存在');
  191. }
  192. if($info['status'] != 0){
  193. HelpHander::error('已盘点');
  194. }
  195. $check = $this->info($info['check_id']);
  196. if($check['status'] == 1){
  197. HelpHander::error('盘点已完成,无法操作');
  198. }
  199. $ret = Db::name('asset_check_items')->where('id',$id)->update([
  200. 'status' => 1,
  201. 'check_nums' => 1,
  202. 'check_time' => date('Y-m-d H:i:s'),
  203. 'remark' => '手动盘点'
  204. ]);
  205. if(!$ret){
  206. HelpHander::error('操作失败');
  207. }
  208. return true;
  209. }
  210. }