CompanyGoods.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\MateGoodsLog;
  4. use app\common\util\ExcelUtil;
  5. use think\App;
  6. use think\Db;
  7. use think\Exception;
  8. class CompanyGoods extends Auth
  9. {
  10. public function __construct(App $app = null) {
  11. parent::__construct($app);
  12. $this->model = new \app\common\model\MateGoods();
  13. $this->table = $this->model->table;
  14. }
  15. public function index(){
  16. if(request()->isAjax()){
  17. //分页参数
  18. $length = input('rows',10,'intval'); //每页条数
  19. $page = input('page',1,'intval'); //第几页
  20. $start = ($page - 1) * $length; //分页开始位置
  21. //排序
  22. $sortRow = input('sidx','id','trim'); //排序列
  23. $sort = input('sord','desc','trim'); //排序方式
  24. $order = $sortRow.' '.$sort;
  25. $title = input('title','','trim');
  26. if($title){
  27. $map[] = ['title','like','%'.$title.'%'];
  28. }
  29. $enable = input('enable','','trim');
  30. if($enable != ''){
  31. $map[] = ['enable','=',$enable];
  32. }
  33. $having = '1=1';
  34. $snums = input('kc_status','','trim');
  35. if($snums == 1) { // 正常
  36. $having = 'mnums >= 0 and nnums >= 0';
  37. }else if($snums == 2){ // 偏多
  38. $having = 'mnums < 0';
  39. }else if($snums == 3){ // 偏少
  40. $having = 'nnums < 0';
  41. }
  42. $cate_id = input('cate_id','','trim');
  43. if($cate_id != ''){
  44. $map[] = ['cate_id','=',$cate_id];
  45. }
  46. $map[] = ['del','=',0];
  47. $map[] = ['org_id','=',$this->orgId];
  48. $map= empty($map) ? true: $map;
  49. //数据查询
  50. $lists =db($this->table)
  51. // ->field('id,price,nums,type,title,unit,brand,total_price,spec,buy_time,remark,create_time,enable,cate_id,s_limit,x_limit,IFNULL(s_limit-nums,0) as mnums,IFNULL(nums- x_limit,0) as nnums')
  52. ->where($map)
  53. ->limit($start,$length)
  54. // ->group('id')
  55. // ->having($having)
  56. ->order($order)
  57. ->select();
  58. foreach ($lists as $k=>$v){
  59. $lists[$k]['cate_name'] = Db::name('mate_cate')->where('id',$v['cate_id'])->value('title');
  60. $kc_txt = '正常';
  61. // if($lists[$k]['s_limit'] != 0 && $lists[$k]['x_limit'] != 0){
  62. // if($lists[$k]['s_limit'] > 0 && $v['nums'] > $lists[$k]['s_limit']){
  63. // $kc_txt = '<span style="color: #DCAC6C">偏多</span> ';
  64. // }
  65. // if($lists[$k]['s_limit'] > 0 && $v['nums'] < $lists[$k]['s_limit']){
  66. // $kc_txt = '<span style="color: red">偏少</span> ';
  67. // }
  68. // }
  69. if($v['s_limit'] > 0){
  70. if($v['nums'] > $v['s_limit']){
  71. $kc_txt = '<span style="color: #DCAC6C">偏多</span> ';
  72. }elseif ($v['nums'] < $v['s_limit'] && $v['nums'] > $v['x_limit']){
  73. $kc_txt = '正常';
  74. }elseif ($v['nums'] == 0 || $v['nums'] < $v['x_limit']){
  75. $kc_txt = '<span style="color: red">偏少</span> ';
  76. }
  77. }
  78. $lists[$k]['kc_txt'] = $kc_txt;
  79. }
  80. //数据返回
  81. $totalCount = db($this->table)->where($map)->count();
  82. $totalPage = ceil($totalCount/$length);
  83. $result['page'] = $page;
  84. $result['total'] = $totalPage;
  85. $result['records'] = $totalCount;
  86. $result['rows'] = $lists;
  87. return json($result);
  88. }else{
  89. $this->assign('meta_title','物品管理');
  90. $cate = Db::name('mate_cate')
  91. ->where('enable',1)
  92. ->where('del',0)
  93. ->where('org_id',$this->orgId)
  94. ->select();
  95. $this->assign('cate',$cate);
  96. return $this->fetch();
  97. }
  98. }
  99. public function selectGoods(){
  100. if(request()->isAjax()){
  101. //分页参数
  102. $length = input('rows',10,'intval'); //每页条数
  103. $page = input('page',1,'intval'); //第几页
  104. $start = ($page - 1) * $length; //分页开始位置
  105. //排序
  106. $sortRow = input('sidx','id','trim'); //排序列
  107. $sort = input('sord','desc','trim'); //排序方式
  108. $order = $sortRow.' '.$sort;
  109. $title = input('title','','trim');
  110. if($title){
  111. $map[] = ['title','like','%'.$title.'%'];
  112. }
  113. $map[] = ['enable','=',1];
  114. $map[] = ['del','=',0];
  115. $map[] = ['org_id','=',$this->orgId];
  116. $map= empty($map) ? true: $map;
  117. //数据查询
  118. $lists =db($this->table)
  119. ->where($map)->limit($start,$length)
  120. ->order($order)->select();
  121. //数据返回
  122. $totalCount = db($this->table)->where($map)->count();
  123. $totalPage = ceil($totalCount/$length);
  124. $result['page'] = $page;
  125. $result['total'] = $totalPage;
  126. $result['records'] = $totalCount;
  127. $result['rows'] = $lists;
  128. return json($result);
  129. }else{
  130. return $this->fetch();
  131. }
  132. }
  133. /**
  134. * 新增/编辑
  135. */
  136. public function add($id=0){
  137. $model = $this->model;
  138. if(request()->isPost()){
  139. $res = $model->updates();
  140. if($res){
  141. $this->success('操作成功',url('index'));
  142. }else{
  143. $this->error($model->getError());
  144. }
  145. }else{
  146. $meta_title = '新增物品';
  147. if($id){
  148. $info =db($this->table)->where('id',$id)->find();
  149. $this->assign('info',$info);
  150. $meta_title = '编辑物品';
  151. }
  152. $cate = Db::name('mate_cate')->where('del',0)->where('org_id',$this->orgId)->select();
  153. $this->assign('cate',$cate);
  154. $this->assign('meta_title',$meta_title);
  155. return $this->fetch();
  156. }
  157. }
  158. /**
  159. * 删除记录
  160. * @param int $id
  161. */
  162. public function del($id=0){
  163. if(!$id){
  164. $this->error('参数错误');
  165. }
  166. if(db('mate_apply_goods')->where('goods_id',$id)->find()){
  167. $this->error('该商品有入库或出库记录,暂不能删除');
  168. }
  169. $res = db($this->table)->where('id',$id)->update(['del'=>1]);
  170. if($res){
  171. $this->success('删除成功');
  172. }else{
  173. $this->error('删除失败');
  174. }
  175. }
  176. /**
  177. * 改变字段值
  178. * @param int $fv
  179. * @param string $fn
  180. * @param int $fv
  181. */
  182. public function changeField($id=0,$fn='',$fv=0){
  183. if(!$fn||!$id){
  184. $this->error('参数错误');
  185. }
  186. $res = db($this->table)->where('id',$id)->update([$fn => $fv]);
  187. if($res){
  188. $this->success('操作成功');
  189. }else{
  190. $this->error('操作失败');
  191. }
  192. }
  193. /**
  194. * 处置
  195. */
  196. public function option($id=0){
  197. $model = new MateGoodsLog();
  198. if(request()->isPost()){
  199. $data = request()->post();
  200. if(empty($data['nums'])) $this->error('请输入数量');
  201. if(empty($data['remark'])) $this->error('请输入备注');
  202. $info = db('mate_goods')
  203. ->where('id',$id)
  204. ->find();
  205. if($info['nums'] <$data['nums']) $this->error('库存数量不足');
  206. $sData = [
  207. 'org_id'=>$this->orgId,
  208. 'goods_id'=>$id,
  209. 'nums'=>$data['nums'],
  210. 'remark'=>$data['remark'],
  211. 'price'=>$info['price'],
  212. ];
  213. Db::startTrans();
  214. try{
  215. $res = $model->saveData($sData);
  216. if(!$res){
  217. \exception($model->getError());
  218. }
  219. $ret = Db::name('mate_goods')->where('id',$id)->setDec('nums',$data['nums']);
  220. if(!$ret){
  221. \exception('操作失败');
  222. }
  223. Db::commit();
  224. }catch (Exception $e){
  225. Db::rollback();
  226. $this->error($e->getMessage());
  227. }
  228. $this->success('操作成功');
  229. }else{
  230. $this->assign('id',$id);
  231. return $this->fetch();
  232. }
  233. }
  234. public function import(){
  235. return $this->fetch();
  236. }
  237. /**
  238. * 下载点模板
  239. */
  240. public function downloadtem(){
  241. set_time_limit(0);
  242. ini_set("memory_limit","512M");
  243. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  244. $types = model('Address')->getTypes();
  245. ksort($types);
  246. $fileName = '物品模板.xlsx';
  247. $excel = new \PHPExcel();
  248. $sheet = $excel->setActiveSheetIndex(0);
  249. $arr = array('A','B','C','D','E','F','H','I','J','K');
  250. foreach($arr as $k=>$v){
  251. $excel->getActiveSheet()->getStyle($v)->getAlignment()->setWrapText(true);//换行
  252. $excel->getActiveSheet()->getStyle($v.'1')->getFont()->setBold(true);
  253. $excel->getActiveSheet()->getColumnDimension($v)->setWidth(18);
  254. }
  255. $sheet->setCellValueByColumnAndRow(0,1,'名称');
  256. $sheet->setCellValueByColumnAndRow(1,1,'分类');
  257. $sheet->setCellValueByColumnAndRow(2,1,'单位');
  258. $sheet->setCellValueByColumnAndRow(3,1,'品牌');
  259. $sheet->setCellValueByColumnAndRow(4,1,'规格');
  260. $sheet->setCellValueByColumnAndRow(5,1,'类型(固定资产/耗材)');
  261. $sheet->setCellValueByColumnAndRow(6,1,'备注');
  262. $sheet->setCellValueByColumnAndRow(7,1,'单价');
  263. $sheet->setCellValueByColumnAndRow(8,1,'数量');
  264. $sheet->setCellValueByColumnAndRow(9,1,'库存上限');
  265. $sheet->setCellValueByColumnAndRow(10,1,'库存下限');
  266. $excel->getActiveSheet()->setCellValue('A2', '测试物品');
  267. $excel->getActiveSheet()->setCellValue('B2', '测试分类编号(2)');
  268. $excel->getActiveSheet()->setCellValue('C2', '个');
  269. $excel->getActiveSheet()->setCellValue('D2', '格力');
  270. $excel->getActiveSheet()->setCellValue('E2', '10个/包');
  271. $excel->getActiveSheet()->setCellValue('F2', '固定资产');
  272. $excel->getActiveSheet()->setCellValue('G2', 'xxxx');
  273. $excel->getActiveSheet()->setCellValue('H2', '10');
  274. $excel->getActiveSheet()->setCellValue('I2', '100');
  275. $excel->getActiveSheet()->setCellValue('J2', '100');
  276. $excel->getActiveSheet()->setCellValue('K2', '0');
  277. ob_end_clean();//清除缓冲区,避免乱码
  278. header('Content-Type: application/vnd.ms-excel');
  279. header('Content-Disposition: attachment;filename="'.$fileName.'"');
  280. header('Cache-Control: max-age=0');
  281. $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
  282. $objWriter->save('php://output'); //文件通过浏览器下载
  283. }
  284. /**
  285. * 导入
  286. */
  287. public function importexcel_bak(){
  288. set_time_limit(0);
  289. ini_set("memory_limit", -1);
  290. ob_flush();//清空缓存
  291. flush();//刷新缓存
  292. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  293. $orgId = $this->orgId;
  294. if(request()->file()) {
  295. $file = request()->file('file');
  296. //获取文件后缀
  297. $e = explode('.',$_FILES['file']['name']);
  298. $ext = $e[count($e)-1];
  299. if($ext == 'xls'){
  300. \PHPExcel_IOFactory::createReader( 'Excel5');
  301. }else{
  302. \PHPExcel_IOFactory::createReader('Excel2007');
  303. }
  304. $newArr=['xls','xlsx'];
  305. if(!in_array($ext,$newArr)){
  306. exit('文件格式不正确');
  307. }
  308. // 移动到框架应用根目录/uploads/ 目录下
  309. $info = $file->validate([ 'size'=>config('app.max_upload_file_size') ])
  310. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'files');
  311. if(!$info){
  312. exit('文件上传失败');
  313. }
  314. $img = './uploads/files/' . $info->getSaveName();
  315. $filePath = str_replace('\\', '/', $img);
  316. $newPath = \PHPExcel_IOFactory::load($filePath);
  317. $excelArray = $newPath->getsheet(0)->toArray(); //转换为数组格式
  318. array_shift($excelArray); //删除第一个数组(标题);
  319. $a = [
  320. '固定资产'=>1,
  321. '耗材'=>2,
  322. ];
  323. if(empty($excelArray)){
  324. exit('文件内容为空');
  325. }
  326. foreach ($excelArray as $k => $v) {
  327. if(!$v[1]){
  328. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称为空,未导入</font><br />";
  329. continue;
  330. }
  331. $cateId = 0;
  332. if(trim($v[2])){
  333. $cateId = Db::name('mate_cate')
  334. ->where('org_id',$this->orgId)
  335. ->where('title',$v[2])
  336. ->where('del',0)
  337. ->value('id');
  338. if(empty($cateId)){
  339. $ps =[
  340. 'title'=>$v[2],
  341. 'org_id'=>$this->orgId,
  342. 'create_time'=>getTime(),
  343. ];
  344. $cateId = Db::name('mate_cate')->insertGetId($ps);
  345. }
  346. }
  347. if(!$v[6]){
  348. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型为空,未导入</font><br />";
  349. continue;
  350. }
  351. $tt = !empty($v[5])?$v[1].'('.$v[5].')':$v[1];
  352. $check = Db::name('mate_goods')
  353. ->where('del',0)
  354. ->where('org_id',$this->orgId)
  355. ->where('title',$tt)
  356. ->find();
  357. if($check){
  358. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称已存在,未导入</font><br />";
  359. continue;
  360. }
  361. if(!isset($a[$v[6]])){
  362. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型格式错误,未导入</font><br />";
  363. continue;
  364. }
  365. $rData = [
  366. 'org_id'=>$this->orgId,
  367. 'title'=>$tt,
  368. 'cate_id'=>$cateId?$cateId:0,
  369. 'unit'=>$v[3],
  370. 'brand'=>$v[4],
  371. 'spec'=>$v[5],
  372. 'type'=>$a[$v[6]],
  373. 'remark'=>$v[7],
  374. 'price'=>floatval($v[8]),
  375. 'nums'=>!empty($v[9])?intval($v[9]):0,
  376. 's_limit'=>$v[10],
  377. 'x_limit'=>$v[11],
  378. 'create_time'=>getTime(),
  379. 'buy_time'=>date('Y-m-d')
  380. ];
  381. $ret=Db::name('mate_goods')->insert($rData);
  382. if(!$ret){
  383. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  384. }else{
  385. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  386. }
  387. }
  388. }else{
  389. exit('请上传文件');
  390. }
  391. }
  392. public function importexcel(){
  393. set_time_limit(0);
  394. ini_set("memory_limit", -1);
  395. ob_flush();//清空缓存
  396. flush();//刷新缓存
  397. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  398. $orgId = $this->orgId;
  399. if(request()->file()) {
  400. $file = request()->file('file');
  401. //获取文件后缀
  402. $e = explode('.',$_FILES['file']['name']);
  403. $ext = $e[count($e)-1];
  404. if($ext == 'xls'){
  405. \PHPExcel_IOFactory::createReader( 'Excel5');
  406. }else{
  407. \PHPExcel_IOFactory::createReader('Excel2007');
  408. }
  409. $newArr=['xls','xlsx'];
  410. if(!in_array($ext,$newArr)){
  411. exit('文件格式不正确');
  412. }
  413. // 移动到框架应用根目录/uploads/ 目录下
  414. $info = $file->validate([ 'size'=>config('app.max_upload_file_size') ])
  415. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'files');
  416. if(!$info){
  417. exit('文件上传失败');
  418. }
  419. $img = './uploads/files/' . $info->getSaveName();
  420. $filePath = str_replace('\\', '/', $img);
  421. $newPath = \PHPExcel_IOFactory::load($filePath);
  422. $excelArray = $newPath->getsheet(0)->toArray(); //转换为数组格式
  423. array_shift($excelArray); //删除第一个数组(标题);
  424. $a = [
  425. '固定资产'=>1,
  426. '耗材'=>2,
  427. ];
  428. if(empty($excelArray)){
  429. exit('文件内容为空');
  430. }
  431. foreach ($excelArray as $k => $v) {
  432. if(!$v[0]){
  433. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称为空,未导入</font><br />";
  434. continue;
  435. }
  436. $cateId = 0;
  437. if(trim($v[1])){
  438. $cateId = Db::name('mate_cate')
  439. ->where('org_id',$this->orgId)
  440. ->where('title',$v[1])
  441. ->where('del',0)
  442. ->value('id');
  443. if(empty($cateId)){
  444. $ps =[
  445. 'title'=>$v[1],
  446. 'org_id'=>$this->orgId,
  447. 'create_time'=>getTime(),
  448. ];
  449. $cateId = Db::name('mate_cate')->insertGetId($ps);
  450. }
  451. }
  452. if(!$v[5]){
  453. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型为空,未导入</font><br />";
  454. continue;
  455. }
  456. $tt = !empty($v[4])?$v[0].'('.$v[4].')':$v[0];
  457. $check = Db::name('mate_goods')
  458. ->where('del',0)
  459. ->where('org_id',$this->orgId)
  460. ->where('title',$tt)
  461. ->find();
  462. if($check){
  463. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,名称已存在,未导入</font><br />";
  464. continue;
  465. }
  466. if(!isset($a[$v[5]])){
  467. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型格式错误,未导入</font><br />";
  468. continue;
  469. }
  470. $rData = [
  471. 'org_id'=>$this->orgId,
  472. 'title'=>$v[0],
  473. 'cate_id'=>$cateId?$cateId:0,
  474. 'unit'=>$v[2],
  475. 'brand'=>$v[3],
  476. 'spec'=>$v[4],
  477. 'type'=>$a[$v[5]],
  478. 'remark'=>$v[6],
  479. 'price'=>floatval($v[7]),
  480. 'nums'=>!empty($v[8])?intval($v[8]):0,
  481. 's_limit'=>$v[9],
  482. 'x_limit'=>$v[10],
  483. 'create_time'=>getTime(),
  484. 'buy_time'=>date('Y-m-d')
  485. ];
  486. $ret=Db::name('mate_goods')->insert($rData);
  487. if(!$ret){
  488. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  489. }else{
  490. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  491. }
  492. }
  493. }else{
  494. exit('请上传文件');
  495. }
  496. }
  497. public function xhtj(){
  498. $cur = date('Y-m-d');
  499. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 month')));
  500. $end = input('end', date('Y-m-d'));
  501. $start1 = $start . ' 00:00:00';
  502. $end1 = $end . ' 00:00:00';
  503. $list = $this->xhtjData($start1, $end1);
  504. $this->assign('list', $list['list']);
  505. $this->assign('zj', $list['zj']);
  506. $this->assign('start', $start);
  507. $this->assign('end', $end);
  508. return $this->fetch();
  509. }
  510. public function xhtjData($start1, $end1){
  511. $map[] = ['ma.create_time', '>=', $start1];
  512. $map[] = ['ma.create_time', '<=', $end1];
  513. $map[] = ['ma.org_id', '=', $this->orgId];
  514. $list = Db::name('mate_apply')
  515. ->alias('ma')
  516. ->join('mate_apply_goods mag','ma.id=mag.apply_id')
  517. ->join('mate_goods mg','mg.id=mag.goods_id')
  518. ->where($map)
  519. ->field('mag.goods_id,mag.price,mg.title')
  520. ->group('mag.goods_id')
  521. ->select();
  522. foreach ($list as $k => $v) {
  523. $list[$k]['num'] = Db::name('mate_apply')
  524. ->alias('ma')
  525. ->join('mate_apply_goods mag','ma.id=mag.apply_id')
  526. ->join('mate_goods mg','mg.id=mag.goods_id')
  527. ->where('mag.goods_id',$v['goods_id'])
  528. ->where($map)
  529. ->sum('mag.nums');
  530. $list[$k]['total_price'] = round($list[$k]['num']*$v['price'],1);
  531. }
  532. $map1[] = ['tm.create_time', '>=', $start1];
  533. $map1[] = ['tm.create_time', '<=', $end1];
  534. $map1[] = ['tm.org_id', '=', $this->orgId];
  535. $list1 = Db::name('todo_mate')
  536. ->alias('tm')
  537. ->join('todo_mate_item tmi','tmi.todo_mate_id=tm.id')
  538. ->join('mate_goods mg','mg.id=tmi.items_id')
  539. ->where($map1)
  540. ->group('tmi.items_id')
  541. ->field('tmi.items_id as goods_id,tmi.money as price,mg.title')
  542. ->select();
  543. foreach ($list1 as $k=>$v){
  544. $list1[$k]['num'] = Db::name('todo_mate')
  545. ->alias('tm')
  546. ->join('todo_mate_item tmi','tmi.todo_mate_id=tm.id')
  547. ->where($map1)
  548. ->where('tmi.items_id',$v['goods_id'])
  549. ->sum('tmi.total');
  550. $list1[$k]['total_price'] = round($list1[$k]['num']*$v['price'],1);
  551. }
  552. // $map2[] = ['mau.create_time', '>=', $start1];
  553. // $map2[] = ['mau.create_time', '<=', $end1];
  554. // $map2[] = ['mau.org_id', '=', $this->orgId];
  555. // $list2 = Db::name('mate_apply_use')
  556. // ->alias('mau')
  557. // ->join('mate_apply_use_goods maug','mau.id=maug.apply_id')
  558. // ->join('mate_goods mg','mg.id=maug.goods_id')
  559. // ->where($map2)
  560. // ->field('maug.goods_id,maug.price,mg.title')
  561. // ->group('maug.goods_id')
  562. // ->select();
  563. //
  564. // foreach ($list2 as $k => $v) {
  565. // $list2[$k]['num'] = Db::name('mate_apply_use')
  566. // ->alias('mau')
  567. // ->join('mate_apply_use_goods maug','mau.id=maug.apply_id')
  568. // ->join('mate_goods mg','mg.id=maug.goods_id')
  569. // ->where('maug.goods_id',$v['goods_id'])
  570. // ->where($map2)
  571. // ->sum('maug.nums');
  572. // $list2[$k]['total_price'] = round($list2[$k]['num']*$v['price'],1);
  573. // }
  574. foreach ($list as $k=>$v){
  575. $totalPrice = 0;
  576. $tnum = 0;
  577. foreach ($list1 as $kk=>$vv){
  578. if($v['goods_id'] == $vv['goods_id']){
  579. $totalPrice = $v['total_price'] + $vv['total_price'];
  580. $tnum = $v['num'] + $vv['num'];
  581. unset($list1[$kk]);
  582. }
  583. /* foreach ($list2 as $kkk=>$vvv){
  584. if($v['goods_id'] == $vv['goods_id']){
  585. $totalPrice = $v['total_price'] + $vv['total_price'];
  586. $tnum = $v['num'] + $vv['num'];
  587. unset($list1[$kk]);
  588. }elseif ($v['goods_id'] == $vvv['goods_id']){
  589. $totalPrice = $v['total_price'] + $vvv['total_price'];
  590. $tnum = $v['num'] + $vvv['num'];
  591. unset($list2[$kkk]);
  592. }elseif ($v['goods_id'] == $vv['goods_id'] && $v['goods_id'] == $vvv['goods_id']){
  593. $totalPrice = $v['total_price'] + $vv['total_price'] + $vvv['total_price'];
  594. $tnum = $v['num'] + $vv['num'] + $vvv['num'];
  595. unset($list1[$kk]);
  596. unset($list2[$kkk]);
  597. }
  598. }*/
  599. }
  600. if($totalPrice > 0 && $tnum > 0){
  601. $list[$k]['total_price'] = $totalPrice;
  602. $list[$k]['num'] = $tnum;
  603. }
  604. }
  605. $array = array_merge($list,$list1);
  606. $zj = 0;
  607. foreach ($array as $k=>$v){
  608. $zj +=$v['total_price'];
  609. }
  610. return ['list'=>$array,'zj'=>$zj];
  611. }
  612. public function qrcode($id=0){
  613. $info = Db::name('mate_goods')->where('id',$id)->find();
  614. $code = get_qrcode_str('mate_goods',$id);
  615. $this->assign('code',$code);
  616. $this->assign('info',$info);
  617. return $this->fetch();
  618. }
  619. public function wptj(){
  620. $cur = date('Y-m-d');
  621. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 month')));
  622. $end = input('end', date('Y-m-d'));
  623. $start1 = $start . ' 00:00:00';
  624. $end1 = $end . ' 23:59:59';
  625. $title = input('title','','trim');
  626. $cateTitle = input('cateTitle','');
  627. $list = $this->wptjData($start1, $end1,$title,$cateTitle);
  628. $this->assign('list', $list);
  629. $this->assign('start', $start);
  630. $this->assign('end', $end);
  631. $this->assign('title', $title);
  632. $this->assign('cateTitle', $cateTitle);
  633. return $this->fetch();
  634. }
  635. public function wptjData($start1, $end1,$title,$cateTitle) {
  636. $map1[] = ['ma.create_time', '>=', $start1];
  637. $map1[] = ['ma.create_time', '<=', $end1];
  638. $map =[];
  639. if($title){
  640. $map[] = ['title','like','%'.$title.'%'];
  641. }
  642. if($cateTitle){
  643. $cateIds = Db::name('mate_cate')->where('title','like','%'.$cateTitle.'%')->column('id');
  644. $map[] = ['cate_id','in',$cateIds];
  645. }
  646. $list = Db::name('mate_goods')
  647. ->where('org_id', $this->orgId)
  648. ->where('del', 0)
  649. ->where('enable', 1)
  650. ->where($map)
  651. ->select();
  652. foreach ($list as $k => $v) {
  653. $list[$k]['ck'] = Db::name('mate_apply')
  654. ->alias('ma')
  655. ->join('mate_apply_goods mag','mag.apply_id=ma.id')
  656. ->where('ma.type',1)
  657. ->where('mag.goods_id',$v['id'])
  658. ->where($map1)
  659. ->sum('mag.nums');
  660. $list[$k]['rk'] = Db::name('mate_apply')
  661. ->alias('ma')
  662. ->join('mate_apply_goods mag','mag.apply_id=ma.id')
  663. ->where('ma.type',2)
  664. ->where('mag.goods_id',$v['id'])
  665. ->where($map1)
  666. ->sum('mag.nums');
  667. $tt = '';
  668. if($v['consume'] == 1){
  669. $tt ='使用消耗';
  670. }elseif ($v['consume'] == 2){
  671. $tt ='出库消耗';
  672. }
  673. $list[$k]['consume_title'] = $tt;
  674. $list[$k]['cate_title'] = Db::name('mate_cate')->where('id',$v['cate_id'])->value('title');
  675. }
  676. return $list;
  677. }
  678. public function wptjDataExport() {
  679. $cur = date('Y-m-d');
  680. $start = input('start', date('Y-m-d', strtotime('' . $cur . ' -1 week')));
  681. $end = input('end', date('Y-m-d'));
  682. $start1 = $start . ' 00:00:00';
  683. $end1 = $end . ' 23:59:59';
  684. $title = input('title','','trim');
  685. $cateTitle = input('cateTitle','');
  686. $list = $this->wptjData($start1, $end1,$title,$cateTitle);
  687. $filename = '物品统计';
  688. $header = [
  689. ['title' => '分类', 'name' => 'cate_title','width'=>'20'],
  690. ['title' => '物品名称', 'name' => 'title','width'=>'20'],
  691. ['title' => '品牌', 'name' => 'brand','width'=>'20'],
  692. ['title' => '规格', 'name' => 'spec','width'=>'20'],
  693. ['title' => '入库数量', 'name' => 'ck','width'=>'20'],
  694. ['title' => '出库数量', 'name' => 'rk','width'=>'20'],
  695. ['title' => '当前余量', 'name' => 'nums','width'=>'20'],
  696. ['title' => '类型', 'name' => 'consume_title','width'=>'20'],
  697. ];
  698. ExcelUtil::export($filename,$header,$list);
  699. }
  700. }