CompanyGoods.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\MateGoodsLog;
  4. use think\App;
  5. use think\Db;
  6. use think\Exception;
  7. class CompanyGoods extends Auth
  8. {
  9. public function __construct(App $app = null) {
  10. parent::__construct($app);
  11. $this->model = new \app\common\model\MateGoods();
  12. $this->table = $this->model->table;
  13. }
  14. public function index(){
  15. if(request()->isAjax()){
  16. //分页参数
  17. $length = input('rows',10,'intval'); //每页条数
  18. $page = input('page',1,'intval'); //第几页
  19. $start = ($page - 1) * $length; //分页开始位置
  20. //排序
  21. $sortRow = input('sidx','id','trim'); //排序列
  22. $sort = input('sord','desc','trim'); //排序方式
  23. $order = $sortRow.' '.$sort;
  24. $title = input('title','','trim');
  25. if($title){
  26. $map[] = ['title','like','%'.$title.'%'];
  27. }
  28. $enable = input('enable','','trim');
  29. if($enable != ''){
  30. $map[] = ['enable','=',$enable];
  31. }
  32. $cate_id = input('cate_id','','trim');
  33. if($cate_id != ''){
  34. $map[] = ['cate_id','=',$cate_id];
  35. }
  36. $map[] = ['del','=',0];
  37. $map[] = ['org_id','=',$this->orgId];
  38. $map= empty($map) ? true: $map;
  39. //数据查询
  40. $lists =db($this->table)
  41. ->where($map)->limit($start,$length)
  42. ->order($order)->select();
  43. foreach ($lists as $k=>$v){
  44. $lists[$k]['cate_name'] = Db::name('mate_cate')->where('id',$v['cate_id'])->value('title');
  45. }
  46. //数据返回
  47. $totalCount = db($this->table)->where($map)->count();
  48. $totalPage = ceil($totalCount/$length);
  49. $result['page'] = $page;
  50. $result['total'] = $totalPage;
  51. $result['records'] = $totalCount;
  52. $result['rows'] = $lists;
  53. return json($result);
  54. }else{
  55. $this->assign('meta_title','物品管理');
  56. $cate = Db::name('mate_cate')
  57. ->where('enable',1)
  58. ->where('del',0)
  59. ->where('org_id',$this->orgId)
  60. ->select();
  61. $this->assign('cate',$cate);
  62. return $this->fetch();
  63. }
  64. }
  65. public function selectGoods(){
  66. if(request()->isAjax()){
  67. //分页参数
  68. $length = input('rows',10,'intval'); //每页条数
  69. $page = input('page',1,'intval'); //第几页
  70. $start = ($page - 1) * $length; //分页开始位置
  71. //排序
  72. $sortRow = input('sidx','id','trim'); //排序列
  73. $sort = input('sord','desc','trim'); //排序方式
  74. $order = $sortRow.' '.$sort;
  75. $title = input('title','','trim');
  76. if($title){
  77. $map[] = ['title','like','%'.$title.'%'];
  78. }
  79. $map[] = ['enable','=',1];
  80. $map[] = ['del','=',0];
  81. $map[] = ['org_id','=',$this->orgId];
  82. $map= empty($map) ? true: $map;
  83. //数据查询
  84. $lists =db($this->table)
  85. ->where($map)->limit($start,$length)
  86. ->order($order)->select();
  87. //数据返回
  88. $totalCount = db($this->table)->where($map)->count();
  89. $totalPage = ceil($totalCount/$length);
  90. $result['page'] = $page;
  91. $result['total'] = $totalPage;
  92. $result['records'] = $totalCount;
  93. $result['rows'] = $lists;
  94. return json($result);
  95. }else{
  96. return $this->fetch();
  97. }
  98. }
  99. /**
  100. * 新增/编辑
  101. */
  102. public function add($id=0){
  103. $model = $this->model;
  104. if(request()->isPost()){
  105. $res = $model->updates();
  106. if($res){
  107. $this->success('操作成功',url('index'));
  108. }else{
  109. $this->error($model->getError());
  110. }
  111. }else{
  112. $meta_title = '新增物品';
  113. if($id){
  114. $info =db($this->table)->where('id',$id)->find();
  115. $this->assign('info',$info);
  116. $meta_title = '编辑物品';
  117. }
  118. $cate = Db::name('mate_cate')->where('del',0)->where('org_id',$this->orgId)->select();
  119. $this->assign('cate',$cate);
  120. $this->assign('meta_title',$meta_title);
  121. return $this->fetch();
  122. }
  123. }
  124. /**
  125. * 删除记录
  126. * @param int $id
  127. */
  128. public function del($id=0){
  129. if(!$id){
  130. $this->error('参数错误');
  131. }
  132. if(db('mate_apply_goods')->where('goods_id',$id)->find()){
  133. $this->error('该商品有入库或出库记录,暂不能删除');
  134. }
  135. $res = db($this->table)->where('id',$id)->update(['del'=>1]);
  136. if($res){
  137. $this->success('删除成功');
  138. }else{
  139. $this->error('删除失败');
  140. }
  141. }
  142. /**
  143. * 改变字段值
  144. * @param int $fv
  145. * @param string $fn
  146. * @param int $fv
  147. */
  148. public function changeField($id=0,$fn='',$fv=0){
  149. if(!$fn||!$id){
  150. $this->error('参数错误');
  151. }
  152. $res = db($this->table)->where('id',$id)->update([$fn => $fv]);
  153. if($res){
  154. $this->success('操作成功');
  155. }else{
  156. $this->error('操作失败');
  157. }
  158. }
  159. /**
  160. * 处置
  161. */
  162. public function option($id=0){
  163. $model = new MateGoodsLog();
  164. if(request()->isPost()){
  165. $data = request()->post();
  166. if(empty($data['nums'])) $this->error('请输入数量');
  167. if(empty($data['remark'])) $this->error('请输入备注');
  168. $info = db('mate_goods')
  169. ->where('id',$id)
  170. ->find();
  171. if($info['nums'] <$data['nums']) $this->error('库存数量不足');
  172. $sData = [
  173. 'org_id'=>$this->orgId,
  174. 'goods_id'=>$id,
  175. 'nums'=>$data['nums'],
  176. 'remark'=>$data['remark'],
  177. 'price'=>$info['price'],
  178. ];
  179. Db::startTrans();
  180. try{
  181. $res = $model->saveData($sData);
  182. if(!$res){
  183. \exception($model->getError());
  184. }
  185. $ret = Db::name('mate_goods')->where('id',$id)->setDec('nums',$data['nums']);
  186. if(!$ret){
  187. \exception('操作失败');
  188. }
  189. Db::commit();
  190. }catch (Exception $e){
  191. Db::rollback();
  192. $this->error($e->getMessage());
  193. }
  194. $this->success('操作成功');
  195. }else{
  196. $this->assign('id',$id);
  197. return $this->fetch();
  198. }
  199. }
  200. public function import(){
  201. return $this->fetch();
  202. }
  203. /**
  204. * 下载点模板
  205. */
  206. public function downloadtem(){
  207. set_time_limit(0);
  208. ini_set("memory_limit","512M");
  209. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  210. $types = model('Address')->getTypes();
  211. ksort($types);
  212. $fileName = '物品模板.xlsx';
  213. $excel = new \PHPExcel();
  214. $sheet = $excel->setActiveSheetIndex(0);
  215. $arr = array('A','B','C','D','E','F');
  216. foreach($arr as $k=>$v){
  217. $excel->getActiveSheet()->getStyle($v)->getAlignment()->setWrapText(true);//换行
  218. $excel->getActiveSheet()->getStyle($v.'1')->getFont()->setBold(true);
  219. $excel->getActiveSheet()->getColumnDimension($v)->setWidth(18);
  220. }
  221. $sheet->setCellValueByColumnAndRow(0,1,'名称');
  222. $sheet->setCellValueByColumnAndRow(1,1,'分类');
  223. $sheet->setCellValueByColumnAndRow(2,1,'单位');
  224. $sheet->setCellValueByColumnAndRow(3,1,'品牌');
  225. $sheet->setCellValueByColumnAndRow(4,1,'规格');
  226. $sheet->setCellValueByColumnAndRow(5,1,'类型(固定资产/耗材)');
  227. $sheet->setCellValueByColumnAndRow(6,1,'备注');
  228. $excel->getActiveSheet()->setCellValue('A2', '测试物品');
  229. $excel->getActiveSheet()->setCellValue('B2', '测试分类编号(2)');
  230. $excel->getActiveSheet()->setCellValue('C2', '个');
  231. $excel->getActiveSheet()->setCellValue('D2', '格力');
  232. $excel->getActiveSheet()->setCellValue('E2', '10个/包');
  233. $excel->getActiveSheet()->setCellValue('F2', '固定资产');
  234. $excel->getActiveSheet()->setCellValue('G2', 'xxxx');
  235. ob_end_clean();//清除缓冲区,避免乱码
  236. header('Content-Type: application/vnd.ms-excel');
  237. header('Content-Disposition: attachment;filename="'.$fileName.'"');
  238. header('Cache-Control: max-age=0');
  239. $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
  240. $objWriter->save('php://output'); //文件通过浏览器下载
  241. }
  242. /**
  243. * 导入
  244. */
  245. public function importexcel(){
  246. set_time_limit(0);
  247. ini_set("memory_limit", -1);
  248. ob_flush();//清空缓存
  249. flush();//刷新缓存
  250. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  251. $orgId = $this->orgId;
  252. if(request()->file()) {
  253. $file = request()->file('file');
  254. //获取文件后缀
  255. $e = explode('.',$_FILES['file']['name']);
  256. $ext = $e[count($e)-1];
  257. if($ext == 'xls'){
  258. \PHPExcel_IOFactory::createReader( 'Excel5');
  259. }else{
  260. \PHPExcel_IOFactory::createReader('Excel2007');
  261. }
  262. $newArr=['xls','xlsx'];
  263. if(!in_array($ext,$newArr)){
  264. exit('文件格式不正确');
  265. }
  266. // 移动到框架应用根目录/uploads/ 目录下
  267. $info = $file->validate([ 'size'=>config('app.max_upload_file_size') ])
  268. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'files');
  269. if(!$info){
  270. exit('文件上传失败');
  271. }
  272. $img = './uploads/files/' . $info->getSaveName();
  273. $filePath = str_replace('\\', '/', $img);
  274. $newPath = \PHPExcel_IOFactory::load($filePath);
  275. $excelArray = $newPath->getsheet(0)->toArray(); //转换为数组格式
  276. array_shift($excelArray); //删除第一个数组(标题);
  277. $a = [
  278. '固定资产'=>1,
  279. '耗材'=>2,
  280. ];
  281. if(empty($excelArray)){
  282. exit('文件内容为空');
  283. }
  284. foreach ($excelArray as $k => $v) {
  285. if(!$v[0]){
  286. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称为空,未导入</font><br />";
  287. continue;
  288. }
  289. $cateId = 0;
  290. if(trim($v[1])){
  291. $cateId = Db::name('mate_cate')->where('id',$v[1])->value('id');
  292. }
  293. if(!$v[5]){
  294. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型为空,未导入</font><br />";
  295. continue;
  296. }
  297. $check = Db::name('mate_goods')
  298. ->where('del',0)
  299. ->where('title',$v[0])
  300. ->find();
  301. if($check){
  302. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称已存在,未导入</font><br />";
  303. continue;
  304. }
  305. if(!isset($a[$v[5]])){
  306. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型格式错误,未导入</font><br />";
  307. continue;
  308. }
  309. $rData = [
  310. 'org_id'=>$this->orgId,
  311. 'title'=>$v[0],
  312. 'cate_id'=>$cateId?$cateId:0,
  313. 'unit'=>$v[2],
  314. 'brand'=>$v[3],
  315. 'spec'=>$v[4],
  316. 'type'=>$a[$v[5]],
  317. 'remark'=>$v[6],
  318. 'create_time'=>getTime(),
  319. 'buy_time'=>date('Y-m-d')
  320. ];
  321. $ret=Db::name('mate_goods')->insert($rData);
  322. if(!$ret){
  323. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  324. }else{
  325. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  326. }
  327. }
  328. }else{
  329. exit('请上传文件');
  330. }
  331. }
  332. }