0
0

AssetItems.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class AssetItems extends Auth
  5. {
  6. public function index($cateId=0){
  7. $title = input('title','','trim');
  8. if($title){
  9. $map[] = ['title|sn','like','%'.$title.'%'];
  10. }
  11. $classId = input('class_id','');
  12. if($classId !=''){
  13. $map[] = ['class_id','=',$classId];
  14. }
  15. $addId = input('add_id','');
  16. if($addId !=''){
  17. $map[] = ['add_id','=',$addId];
  18. }
  19. $enable = input('enable','');
  20. if($enable !=''){
  21. $map[] = ['enable','=',$enable];
  22. }
  23. if($cateId > 0){
  24. $map[] = ['cate_id','=',$cateId];
  25. }
  26. $map[] = ['org_id','=',cur_org_id()];
  27. $map[] = ['del','=',0];
  28. if(request()->isAjax()){
  29. //分页参数
  30. $length = input('rows',10,'intval'); //每页条数
  31. $page = input('page',1,'intval'); //第几页
  32. $start = ($page - 1) * $length; //分页开始位置
  33. //排序
  34. $sortRow = input('sidx','id','trim'); //排序列
  35. $sort = input('sort','desc','trim'); //排序方式
  36. $order = $sortRow.' '.$sort;
  37. $map= empty($map) ? true: $map;
  38. //数据查询
  39. $lists = Db::name('asset_items')->where($map)->limit($start,$length)->order('id desc')->select();
  40. foreach ($lists as $k=>$v){
  41. $lists[$k]['statusTxt'] = model('asset_items')->statusTxt[$v['enable']];
  42. $lists[$k]['cateName'] = Db::name('asset_cate')->where('id',$v['cate_id'])->value('title');
  43. $lists[$k]['className'] = Db::name('asset_class')->where('id',$v['class_id'])->value('title');
  44. $lists[$k]['addName'] = Db::name('asset_add')->where('id',$v['add_id'])->value('title');
  45. $lists[$k]['unitName'] = Db::name('asset_unit')->where('id',$v['unit_id'])->value('title');
  46. $lists[$k]['print'] = $v['print'] == 1 ?'是':'否';
  47. $ly = Db::name('asset_receive_items')
  48. ->alias('ari')
  49. ->join('asset_items ai','ai.id=ari.items_id')
  50. ->where('ari.items_id',$v['id'])
  51. ->where('ai.enable',2)
  52. ->find();
  53. $lists[$k]['is_ly'] = $ly ? 1 : 0;
  54. $lists[$k]['userName'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
  55. $lists[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('title');
  56. }
  57. //数据返回
  58. $totalCount = Db::name('asset_items')->where($map)->count();
  59. $totalPage = ceil($totalCount/$length);
  60. $result['page'] = $page;
  61. $result['total'] = $totalPage;
  62. $result['records'] = $totalCount;
  63. $result['rows'] = $lists;
  64. $countPrice = Db::name('asset_items')->where($map)->sum('price');
  65. $result['countPrice'] = $countPrice;
  66. return json($result);
  67. }else{
  68. $class = model('AssetClass')->getList($this->orgId);
  69. $this->assign('class',$class);
  70. $add = model('AssetAdd')->getList($this->orgId);
  71. $this->assign('add',$add);
  72. $tree = model('AssetCate')->showAllTree();
  73. $this->assign('tree',$tree);
  74. $this->assign('cateId',$cateId);
  75. $cateTitle = Db::name('asset_cate')->where('id',$cateId)->value('title');
  76. $this->assign('cateTitle',$cateTitle);
  77. $countPrice = Db::name('asset_items')->where($map)->sum('price');
  78. $this->assign('countPrice',$countPrice);
  79. return $this->fetch();
  80. }
  81. }
  82. /**
  83. * 新增/编辑
  84. */
  85. public function add($id=0){
  86. if(request()->isPost()){
  87. $res = model('asset_items')->updates();
  88. if($res){
  89. $this->success('操作成功',url('index'));
  90. }else{
  91. $this->error(model('asset_items')->getError());
  92. }
  93. }else{
  94. if($id){
  95. $info = db('asset_items')->where('id',$id)->find();
  96. $this->assign('info',$info);
  97. }
  98. $class = model('AssetClass')->getList($this->orgId);
  99. $this->assign('class',$class);
  100. $add = model('AssetAdd')->getList($this->orgId);
  101. $this->assign('add',$add);
  102. $unit= model('AssetUnit')->getList($this->orgId);
  103. $this->assign('unit',$unit);
  104. $tree = model('AssetCate')->formatAllTree();
  105. $this->assign('tree',$tree);
  106. return $this->fetch();
  107. }
  108. }
  109. public function edit($id=0){
  110. if(request()->isPost()){
  111. $res = model('asset_items')->updates();
  112. if($res){
  113. $this->success('操作成功',url('index'));
  114. }else{
  115. $this->error(model('asset_items')->getError());
  116. }
  117. }else{
  118. if($id){
  119. $info = db('asset_items')->where('id',$id)->find();
  120. $this->assign('info',$info);
  121. }
  122. $class = model('AssetClass')->getList($this->orgId);
  123. $this->assign('class',$class);
  124. $add = model('AssetAdd')->getList($this->orgId);
  125. $this->assign('add',$add);
  126. $unit= model('AssetUnit')->getList($this->orgId);
  127. $this->assign('unit',$unit);
  128. $tree = model('AssetCate')->formatAllTree();
  129. $this->assign('tree',$tree);
  130. return $this->fetch();
  131. }
  132. }
  133. /**
  134. * 删除记录
  135. * @param int $id
  136. */
  137. public function del($id=0){
  138. if(!$id){
  139. $this->error('参数错误');
  140. }
  141. $res = Db::name('asset_items')->where('id',$id)->setField('del',1);
  142. if($res){
  143. $this->success('删除成功');
  144. }else{
  145. $this->error('删除失败');
  146. }
  147. }
  148. public function print($id=0){
  149. $info = Db::name('asset_items')->where('id',$id)->find();
  150. $this->assign('info',$info);
  151. return $this->fetch();
  152. }
  153. public function export(){
  154. $title = input('title','','trim');
  155. if($title){
  156. $map[] = ['title|sn','like','%'.$title.'%'];
  157. }
  158. $classId = input('class_id','');
  159. if($classId !=''){
  160. $map[] = ['class_id','=',$classId];
  161. }
  162. $addId = input('add_id','');
  163. if($addId !=''){
  164. $map[] = ['add_id','=',$addId];
  165. }
  166. $enable = input('enable','');
  167. if($enable !=''){
  168. $map[] = ['enable','=',$enable];
  169. }
  170. $cateId = input('cateId',0);
  171. if($cateId > 0){
  172. $map[] = ['cate_id','=',$cateId];
  173. }
  174. $map[] = ['org_id','=',cur_org_id()];
  175. $map[] = ['del','=',0];
  176. $lists = Db::name('asset_items')->where($map)->order('id desc')->select();
  177. foreach ($lists as $k=>$v){
  178. $lists[$k]['statusTxt'] = model('asset_items')->statusTxt[$v['enable']];
  179. $lists[$k]['cateName'] = Db::name('asset_cate')->where('id',$v['cate_id'])->value('title');
  180. $lists[$k]['className'] = Db::name('asset_class')->where('id',$v['class_id'])->value('title');
  181. $lists[$k]['addName'] = Db::name('asset_add')->where('id',$v['add_id'])->value('title');
  182. $lists[$k]['unitName'] = Db::name('asset_unit')->where('id',$v['unit_id'])->value('title');
  183. $lists[$k]['print'] = $v['print'] == 1 ?'是':'否';
  184. }
  185. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  186. //实例化PHPExcel类
  187. $objPHPExcel = new \PHPExcel();
  188. //激活当前的sheet表
  189. $objPHPExcel->setActiveSheetIndex(0);
  190. //设置表格头(即excel表格的第一行)
  191. $objPHPExcel->setActiveSheetIndex(0)
  192. ->setCellValue('A1', '编号')
  193. ->setCellValue('B1', '名称')
  194. ->setCellValue('C1', '规格')
  195. ->setCellValue('D1', '数量')
  196. ->setCellValue('E1', '状态')
  197. ->setCellValue('F1', '类型')
  198. ->setCellValue('G1', '类别')
  199. ->setCellValue('H1', '增加方式')
  200. ->setCellValue('I1', '品牌')
  201. ->setCellValue('J1', '原值')
  202. ->setCellValue('K1', '存放地点')
  203. ->setCellValue('L1', '资产用途')
  204. ->setCellValue('M1', '购买日期')
  205. ->setCellValue('N1', '是否打印标签')
  206. ->setCellValue('O1', '备注')
  207. ;
  208. // 设置表格头水平居中
  209. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
  210. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  211. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
  212. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  213. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
  214. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  215. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
  216. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  217. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
  218. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  219. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
  220. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  221. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
  222. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  223. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
  224. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  225. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()
  226. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  227. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J1')->getAlignment()
  228. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  229. $objPHPExcel->setActiveSheetIndex(0)->getStyle('K1')->getAlignment()
  230. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  231. $objPHPExcel->setActiveSheetIndex(0)->getStyle('L1')->getAlignment()
  232. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  233. $objPHPExcel->setActiveSheetIndex(0)->getStyle('M1')->getAlignment()
  234. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  235. $objPHPExcel->setActiveSheetIndex(0)->getStyle('N1')->getAlignment()
  236. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  237. $objPHPExcel->setActiveSheetIndex(0)->getStyle('O1')->getAlignment()
  238. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  239. //设置列水平居中
  240. $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
  241. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  242. $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
  243. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  244. $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
  245. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  246. $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
  247. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  248. $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
  249. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  250. $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
  251. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  252. $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
  253. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  254. $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
  255. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  256. $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()
  257. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  258. $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()
  259. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  260. $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()
  261. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  262. $objPHPExcel->setActiveSheetIndex(0)->getStyle('L')->getAlignment()
  263. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  264. $objPHPExcel->setActiveSheetIndex(0)->getStyle('M')->getAlignment()
  265. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  266. $objPHPExcel->setActiveSheetIndex(0)->getStyle('N')->getAlignment()
  267. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  268. $objPHPExcel->setActiveSheetIndex(0)->getStyle('O')->getAlignment()
  269. ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  270. //设置单元格宽度
  271. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(30);
  272. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(30);
  273. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(30);
  274. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(30);
  275. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(30);
  276. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(30);
  277. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(30);
  278. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(30);
  279. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(30);
  280. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(30);
  281. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('K')->setWidth(30);
  282. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('L')->setWidth(30);
  283. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('M')->setWidth(30);
  284. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('N')->setWidth(30);
  285. $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('O')->setWidth(30);
  286. //循环刚取出来的数组,将数据逐一添加到excel表格。
  287. for ($i = 0; $i < count($lists); $i++) {
  288. $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $lists[$i]['sn']);
  289. $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $lists[$i]['title']);
  290. $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $lists[$i]['spec']);
  291. $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $lists[$i]['nums']);
  292. $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $lists[$i]['statusTxt']);
  293. $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $lists[$i]['cateName']);
  294. $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $lists[$i]['className']);
  295. $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $lists[$i]['addName']);
  296. $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $lists[$i]['brand']);
  297. $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), $lists[$i]['price']);
  298. $objPHPExcel->getActiveSheet()->setCellValue('K' . ($i + 2), $lists[$i]['address']);
  299. $objPHPExcel->getActiveSheet()->setCellValue('L' . ($i + 2), $lists[$i]['used']);
  300. $objPHPExcel->getActiveSheet()->setCellValue('M' . ($i + 2), $lists[$i]['buy_time']);
  301. $objPHPExcel->getActiveSheet()->setCellValue('N' . ($i + 2), $lists[$i]['print']);
  302. $objPHPExcel->getActiveSheet()->setCellValue('O' . ($i + 2), $lists[$i]['remark']);
  303. }
  304. //设置保存的Excel表格名称
  305. $filename = '资产列表' . date('YmdHis', time()) . '.xls';
  306. //设置当前激活的sheet表格名称
  307. $objPHPExcel->getActiveSheet()->setTitle('资产列表');
  308. //设置浏览器窗口下载表格
  309. ob_end_clean();
  310. header("Content-Type: application/force-download");
  311. header("Content-Type: application/octet-stream");
  312. header("Content-Type: application/download");
  313. header('Content-Disposition:inline;filename="' . $filename);
  314. //生成excel文件
  315. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  316. //下载文件在浏览器窗口
  317. return $objWriter->save('php://output');
  318. }
  319. public function qrcode($id=0){
  320. $info = Db::name('asset_items')->where('org_id',$this->orgId)->where('id',$id)->find();
  321. $code = get_qrcode_str('asset',$id);
  322. $config = config('app.addr_url');
  323. $code = $config.$code;
  324. $this->assign('code',$code);
  325. $this->assign('info',$info);
  326. return $this->fetch();
  327. }
  328. public function allot($id=0){
  329. if(request()->isPost()){
  330. $data = request()->post();
  331. if($data['receive_user_id'] < 1){
  332. $this->error('被调拨人不能为空');
  333. }
  334. $receiveDepId = db('user_dep')->where('user_id',$data['receive_user_id'])->value('dep_id');
  335. $adata = [
  336. 'user_id'=>is_login(),
  337. 'allot_user_id'=>$data['receive_user_id'],
  338. 'allot_dep_id'=>$receiveDepId,
  339. 'items_id'=>$data['id'],
  340. 'org_id'=>cur_org_id(),
  341. 'create_time'=>date('Y-m-d H:i:s')
  342. ];
  343. Db::name('asset_allot')->insert($adata);
  344. $res = Db::name('asset_items')->where('id',$data['id'])->update(['user_id'=>$data['receive_user_id'],'dep_id'=>$receiveDepId]);
  345. if($res){
  346. $this->success('操作成功',url('index'));
  347. }else{
  348. $this->error('操作失败');
  349. }
  350. }else{
  351. if($id){
  352. $info = db('asset_receive')
  353. ->alias('ar')
  354. ->field('ar.*,ari.items_id')
  355. ->join('asset_receive_items ari','ari.receive_id=ar.id')
  356. ->where('ari.items_id',$id)
  357. ->find();
  358. $info['user_name'] = Db::name('user')->where('id',$info['receive_user_id'])->value('real_name');
  359. $this->assign('info',$info);
  360. }
  361. $userList = model('WorkTypeMode')->getWorkerUser(cur_org_id());
  362. $this->assign('userList',$userList);
  363. return $this->fetch();
  364. }
  365. return $this->fetch();
  366. }
  367. }