Report.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. <?php
  2. /**
  3. * author xtj
  4. * createtime 2020/4/14 0014 下午 1:21
  5. * return array obj json bool
  6. * param
  7. */
  8. namespace app\admin\controller;
  9. use app\common\controller\Backend;
  10. use think\Db;
  11. use think\Session;
  12. use think\Where;
  13. use PHPExcel_IOFactory;
  14. use PHPExcel;
  15. use think\paginator\driver\Bootstrap;
  16. class Report extends Backend{
  17. protected $searchFields = "*";
  18. //领用报表
  19. public function index(){
  20. //设置过滤方法
  21. $this->request->filter(['strip_tags']);
  22. if ($this->request->isAjax()) {
  23. // list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  24. $params = input('search');
  25. if (!empty($params)){
  26. $product = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = '{$params}' limit 1");
  27. if (empty($product)){
  28. return array('status'=>0,'msg'=>'数据错误,请联系管理员');
  29. }
  30. $product_number = rtrim($product[0]['Gd_cpdh']);
  31. $where[] = ['exp',Db::raw("FIND_IN_SET($product_number,product_number)")];
  32. $list = Db::name('formula')->where($where)->field('id,color')->select();
  33. foreach ($list as $key=>$value){
  34. $bach = Db::name('warehousing_detail')->where('cid',$value['id'])->column('bach_number');
  35. $collarWeight = Db::name('collar_use')->where('order_number',$params)->where('status',1)->where('warehousing','in',$bach)->column('weight');
  36. $backWeight = Db::name('collar_use')->where('order_number',$params)->where('status',2)->where('back','in',$bach)->column('back_weight');
  37. $list[$key]['collarWeight'] = array_sum($collarWeight) / 1000;
  38. $list[$key]['backWeight'] = array_sum($backWeight) / 1000;
  39. $consume = array_sum($collarWeight) - array_sum($backWeight);
  40. $list[$key]['consume'] = $consume / 1000;
  41. $list[$key]['order_number'] = $params;
  42. $list[$key]['product_name'] = trim($product[0]['成品名称']);
  43. }
  44. $total = count($list);
  45. }else{
  46. $total = 0;
  47. $list = [];
  48. }
  49. $result = array("total" => $total, "rows" => $list);
  50. return json($result);
  51. }
  52. return $this->view->fetch();
  53. }
  54. //批次报表
  55. public function bach(){
  56. //设置过滤方法
  57. $this->request->filter(['strip_tags']);
  58. if ($this->request->isAjax()) {
  59. $param = input('');
  60. $wheres = [];
  61. //批次号查询 产品名称查询
  62. if(!empty($param['search'])){
  63. if(is_numeric($param['search'])){
  64. $wheres['order_number'] = ['=',$param['search']];
  65. }else{
  66. $wheres['product_name'] = ['like','%'.$param['search'].'%'];
  67. }
  68. }
  69. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  70. $list = Db::name('warehousing_detail')
  71. ->where('status',0)
  72. ->limit($offset, $limit)
  73. ->where($wheres)
  74. ->order('id desc')
  75. ->field('id,product_name,bach_number,color,weight,create,order_number')
  76. ->select();
  77. $total = Db::name('warehousing_detail')
  78. ->where('status',0)
  79. ->where($wheres)
  80. ->limit($offset, $limit)
  81. ->count();
  82. foreach ($list as $key=>$value){
  83. $list[$key]['ink_status'] = '专色墨';
  84. if ($value['weight'] > 0){
  85. $list[$key]['weight'] = $value['weight'] / 1000;
  86. }
  87. }
  88. // $ink = Db::name('matter')->where('weight','>',0)->field('code,name,weight,create')->select();
  89. // foreach ($ink as $k=>$v){
  90. // $ink[$k]['ink_status'] = '油墨';
  91. // $ink[$k]['color'] = '';
  92. // $ink[$k]['product_name'] = $v['name'];
  93. // $ink[$k]['bach_number'] = $v['code'];
  94. // $ink[$k]['weight'] = $value['weight'] / 1000;
  95. // unset( $ink[$k]['name']);
  96. // unset( $ink[$k]['code']);
  97. // array_push($list,$ink[$k]);
  98. // }
  99. // $total = count($list);
  100. // for ($i=0;$i<$total;$i++){
  101. // $list[$i]['id'] = $i+1;
  102. // }
  103. $result = array("total" => $total, "rows" => $list);
  104. return json($result);
  105. }
  106. return $this->view->fetch();
  107. }
  108. //月度报表
  109. public function mouth(){
  110. //设置过滤方法
  111. $param = $this->request->param();
  112. if (!empty($param['k_time'])) {
  113. $this->assign('k_time',$param['k_time']);
  114. } else {
  115. $this->assign('k_time','');
  116. }
  117. if (!empty($param['j_time'])) {
  118. $this->assign('j_time',$param['j_time']);
  119. } else {
  120. $this->assign('j_time','');
  121. }
  122. $this->request->filter(['strip_tags']);
  123. if ($this->request->isAjax()) {
  124. $wheres = [];
  125. $param = input('');
  126. //开始时间--结束时间查询
  127. if(!empty($param['k_time']) && !empty($param['j_time'])){
  128. $wheres['create'] = ['between time',[$param['k_time'].' 00:00:00',$param['j_time'].' 23:59:59']];
  129. }else{
  130. $mouthStart = date('Y-m-01', strtotime(date("Y-m-d")));
  131. $mouthEnd = date('Y-m-d', strtotime("$mouthStart +1 month -1 day"));
  132. $wheres['create'] = ['between',[$mouthStart.' 00:00:00',$mouthEnd.' 23:59:59']];
  133. }
  134. //批次号查询、产品名称查询
  135. if(!empty($param['search'])){
  136. if(is_numeric($param['search'])){
  137. $wheres['order_number'] = ['=',$param['search']];
  138. }else{
  139. $wheres['product_name'] = ['like','%'.$param['search'].'%'];
  140. }
  141. }
  142. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  143. // $mouthStart = date('Y-m-01', strtotime(date("Y-m-d")));
  144. // $mouthEnd = date('Y-m-d', strtotime("$mouthStart +1 month -1 day"));
  145. $list = Db::name('collar_use')
  146. // ->where('create','between time',[$mouthStart,$mouthEnd])
  147. ->where($wheres)
  148. ->field('id,order_number,product_name,warehousing,weight,back,back_weight,status,machine_number,create')
  149. ->limit($offset, $limit)
  150. ->order('id desc')
  151. ->select();
  152. $total = Db::name('collar_use')
  153. // ->where('create','between time',[$mouthStart,$mouthEnd])
  154. ->where($wheres)
  155. ->limit($offset, $limit)
  156. ->order('id desc')
  157. ->count();
  158. foreach ($list as $key=>$value){
  159. $list[$key]['ink_status'] = '专色墨';
  160. if ($value['status'] == 1){
  161. $list[$key]['status'] = '领用';
  162. }else{
  163. $list[$key]['status'] = '退回';
  164. }
  165. if (!empty($value['weight'])){
  166. $list[$key]['weight'] = $value['weight'] / 1000;
  167. $list[$key]['color'] = Db::name('warehousing_detail')->where('bach_number',$value['warehousing'])->value('color');
  168. unset($list[$key]['back']);
  169. unset($list[$key]['back_weight']);
  170. }
  171. if (!empty($value['back_weight'])){
  172. $list[$key]['weight'] = $value['back_weight'] / 1000;
  173. $list[$key]['color'] = Db::name('warehousing_detail')->where('bach_number',$value['back'])->value('color');
  174. unset($list[$key]['back_weight']);
  175. }
  176. unset($list[$key]['warehousing']);
  177. unset($list[$key]['back']);
  178. }
  179. $result = array("total" => $total, "rows" => $list);
  180. return json($result);
  181. }
  182. return $this->view->fetch();
  183. // $ink = Db::name('matter_use')->where('create','between time',[$mouthStart,$mouthEnd])->field('order_number,ink_name,out,out_weight,back,back_weight,status,machine_number,create')->order('order_number')->select();
  184. // foreach ($ink as $k=>$v){
  185. // $ink[$k]['ink_status'] = '油墨';
  186. // $ink[$k]['product_name'] = $ink[$k]['ink_name'];
  187. // $ink[$k]['color'] = '';
  188. // if ($v['status'] == 1){
  189. // $ink[$k]['status'] = '领用';
  190. // }else{
  191. // $ink[$k]['status'] = '退回';
  192. // }
  193. // if (!empty($v['out_weight'])){
  194. // $ink[$k]['weight'] = $v['out_weight'] / 1000;
  195. //
  196. // }
  197. // if (!empty($v['back_weight'])){
  198. // $ink[$k]['weight'] = $v['back_weight'] / 1000;
  199. //
  200. // }
  201. // unset($ink[$k]['out']);
  202. // unset($ink[$k]['out_weight']);
  203. // unset($ink[$k]['back']);
  204. // unset($ink[$k]['back_weight']);
  205. // array_push($list,$ink[$k]);
  206. // }
  207. }
  208. //专色墨换算成原墨
  209. public function warehousing(){
  210. die;
  211. //设置过滤方法
  212. $this->request->filter(['strip_tags']);
  213. if ($this->request->isAjax()) {
  214. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  215. $result = array("total" => $total, "rows" => $list);
  216. return json($result);
  217. }
  218. return $this->view->fetch();
  219. $params = input('productNumber');
  220. if (empty($params)){
  221. $allOrder = Db::name('collar_use')->where('status',1)->order('create desc')->column('order_number');
  222. $allOrder = array_merge(array_unique($allOrder));
  223. $limitOrder = array_slice($allOrder,0,10);
  224. $bach = [];
  225. foreach ($limitOrder as $key => $value){
  226. //循环查出各个工单领用总重量
  227. $where = [];
  228. $where['order_number'] = $value;
  229. $where['status'] = 1;
  230. $where['l_status'] = 2;
  231. $data = Db::name('collar_use')->where($where)->field('order_number,warehousing,weight')->select();
  232. $count = count($data);
  233. $warehousing = [];
  234. for ($j=0;$j<$count;$j++){
  235. $warehousing[$j]= $data[$j]['warehousing'];
  236. }
  237. $bach[$key] = array_merge(array_unique($warehousing));
  238. }
  239. $i = 0;
  240. foreach ($bach as $k1=>$v1){
  241. foreach ($v1 as $k2=>$v2){
  242. $list[$i]['order_number'] = Db::name('collar_use')->where('warehousing',$v2)->value('order_number');
  243. $list[$i]['product_name'] = Db::name('warehousing_detail')->where('bach_number',$v2)->value('product_name');
  244. $list[$i]['warehousing'] = $v2;
  245. $list[$i]['out_weight'] = Db::name('collar_use')->where(['status'=>1,'l_status'=>2])->where('warehousing',$v2)->sum('weight');
  246. $list[$i]['out_weight'] = $list[$i]['out_weight'] / 1000;
  247. $list[$i]['back_weight'] = Db::name('collar_use')->where('status',2)->where('back',$v2)->sum('weight');
  248. if ($list[$i]['back_weight'] > 0){
  249. $list[$i]['back_weight'] = $list[$i]['back_weight'] / 1000;
  250. }
  251. //报废
  252. $list[$i]['scrap'] = Db::name('scrap_ink')->where(['order_number'=>$list[$i]['order_number'],'bach_number'=>$v2])->sum('weight');
  253. if ($list[$i]['scrap'] > 0){
  254. $list[$i]['scrap'] = $list[$i]['scrap'] / 1000;
  255. }
  256. $list[$i]['color'] = Db::name('warehousing_detail')->where('bach_number',$v2)->value('color');
  257. $list[$i]['total'] = $list[$i]['out_weight'] - $list[$i]['back_weight'] - $list[$i]['scrap'];
  258. $i = $i + 1;
  259. }
  260. }
  261. }else{
  262. $result = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = '{$params}' limit 1");
  263. if (empty($result)){
  264. return array('status'=>0,'msg'=>'未查到相关信息,请核查工单号');
  265. }
  266. $product_name = trim($result[0]['成品名称']);
  267. //领用总重量-退回总重量 = 消耗总重量
  268. $where = [];
  269. $where['order_number'] = $params;
  270. $where['status'] = 1;
  271. $where['l_status'] = 2;
  272. $data = Db::name('collar_use')->where($where)->field('order_number,warehousing,weight')->select();
  273. if (empty($data)){
  274. return $this->error('暂无数据');
  275. }
  276. //获得领用的所有专色批次号
  277. $bach = [];
  278. foreach ($data as $key=>$value){
  279. $bach[$key]= $value['warehousing'];
  280. }
  281. $bach = array_merge(array_unique($bach));
  282. //计算领走的总重量
  283. $mapOut = [];
  284. $mapOut['status'] = 1;
  285. $mapOut['l_status'] = 2;
  286. //退回查询条件
  287. $mapBack['status'] = 2;
  288. $list = [];
  289. foreach ($bach as $k=>$v){
  290. $list[$k]['order_number'] = $params;
  291. $list[$k]['product_name'] = $product_name;
  292. $list[$k]['warehousing'] = $v;
  293. //领走
  294. $list[$k]['out_weight'] = Db::name('collar_use')->where($mapOut)->where('warehousing',$v)->sum('weight');
  295. $list[$k]['out_weight'] = $list[$k]['out_weight'] / 1000;
  296. //退回
  297. $list[$k]['back_weight'] = Db::name('collar_use')->where($mapBack)->where('back',$v)->sum('weight');
  298. if ($list[$k]['back_weight'] > 0){
  299. $list[$k]['back_weight'] = $list[$k]['back_weight'] / 1000;
  300. }
  301. //报废
  302. $list[$k]['scrap'] = Db::name('scrap_ink')->where(['order_number'=>$params,'bach_number'=>$v])->sum('weight');
  303. if ($list[$k]['scrap'] > 0){
  304. $list[$k]['scrap'] = $list[$k]['scrap'] / 1000;
  305. }
  306. $list[$k]['color'] = Db::name('warehousing_detail')->where('bach_number',$v)->value('color');
  307. $list[$k]['total'] = $list[$k]['out_weight'] - $list[$k]['back_weight'] - $list[$k]['scrap'];
  308. }
  309. }
  310. return $this->view->fetch('',compact('list'));
  311. }
  312. //原墨领用报表
  313. public function matter(){
  314. //设置过滤方法
  315. $this->request->filter(['strip_tags']);
  316. if ($this->request->isAjax()) {
  317. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  318. $search = input('search');
  319. $map = [];
  320. if (!empty($search)){
  321. $where = [];
  322. $map['order_number'] = $search;
  323. }
  324. $list = Db::name('matter_use')
  325. ->where($where)
  326. ->where($map)
  327. ->order($sort, $order)
  328. ->limit($offset,$limit)
  329. ->select();
  330. $total = Db::name('matter_use')
  331. ->where($where)
  332. ->where($map)
  333. ->count();
  334. foreach ($list as $k=>$v){
  335. if (!empty($v['out_weight'])){
  336. $list[$k]['out_weight'] = $v['out_weight'] / 1000;
  337. }
  338. if (!empty($v['back_weight'])){
  339. $list[$k]['back_weight'] = $v['back_weight'] / 1000;
  340. }
  341. }
  342. $result = array("total" => $total, "rows" => $list);
  343. // print_r($result);die;
  344. return json($result);
  345. }
  346. return $this->view->fetch();
  347. }
  348. //原墨统计
  349. public function ink(){
  350. //设置过滤方法
  351. $this->request->filter(['strip_tags']);
  352. if ($this->request->isAjax()) {
  353. //如果发送的来源是Selectpage,则转发到Selectpage
  354. if ($this->request->request('keyField')) {
  355. return $this->selectpage();
  356. }
  357. $search = input('search');
  358. $map = [];
  359. if (!empty($search)){
  360. $map['order_number'] = $search;
  361. }
  362. //统计
  363. $res = [];
  364. if (!empty($map)){
  365. $data = Db::name('matter_use')
  366. ->where($map)
  367. ->order('status asc')
  368. ->select();
  369. if (!empty($data)){
  370. $y = 0;
  371. foreach ($data as $key=>$value){
  372. $isHave = $this->deep_in_array($value['ink_name'],$res);
  373. if ($isHave){
  374. foreach ($res as $j=>$i){
  375. if ($value['ink_name'] == $i['ink_name'] && $value['status'] == 1){ //出库重量相加
  376. $res[$j]['out_weight'] = $i['out_weight'] + $value['out_weight'];
  377. }
  378. if ($value['ink_name'] == $i['ink_name'] && $value['status'] == 2){ //退库重量相加
  379. $res[$j]['back_weight'] = $i['back_weight'] + $value['back_weight'];
  380. }
  381. }
  382. }else{
  383. $res[$y]['order_number'] = $value['order_number'];
  384. $res[$y]['ink_name'] = $value['ink_name'];
  385. $res[$y]['out_weight'] = $value['out_weight'];
  386. $res[$y]['back_weight'] = $value['back_weight'];
  387. $y++;
  388. }
  389. }
  390. foreach ($res as &$item){
  391. $item['out_weight'] = $item['out_weight'] / 1000;
  392. $item['back_weight'] = $item['back_weight'] / 1000;
  393. $item['weight'] = $item['out_weight'] - $item['back_weight'];
  394. }
  395. }
  396. }
  397. $total = count($res);
  398. $result = array("total" => $total, "rows" => $res);
  399. return json($result);
  400. }
  401. return $this->view->fetch();
  402. }
  403. //领用报表导出
  404. public function out(){
  405. $params = input('search');
  406. print_r($params);die;
  407. //导出
  408. $path = dirname(__FILE__); //找到当前脚本所在路径
  409. vendor("PHPExcel.PHPExcel");
  410. vendor("PHPExcel.PHPExcel.Writer.Excel5");
  411. vendor("PHPExcel.PHPExcel.Writer.Excel2007");
  412. vendor("PHPExcel.PHPExcel.IOFactory");
  413. $objPHPExcel = new \PHPExcel();
  414. $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
  415. $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
  416. $list = Db::name('collar_use')->alias('a')
  417. ->join('warehousing_detail b','a.warehousing=b.bach_number','LEFT')
  418. ->where('a.status',1)
  419. ->field('a.order_number,a.product_name,a.weight,b.color,a.create')
  420. ->select();
  421. foreach ($list as $key=>$value){
  422. $list[$key]['ink_status'] = '专色墨';
  423. $list[$key]['weight'] = $value['weight'] / 1000;
  424. }
  425. $inkList = Db::name('matter_use')
  426. ->where('status',1)
  427. ->field('product_name,out_weight,create')
  428. ->select();
  429. foreach ($inkList as $k=>$v){
  430. $inkList[$k]['ink_status'] = '油墨';
  431. $inkList[$k]['color'] = '';
  432. $inkList[$k]['weight'] = $v['out_weight'] / 1000;
  433. unset( $inkList[$k]['out_weight']);
  434. array_push($list,$inkList[$k]);
  435. }
  436. // 实例化完了之后就先把数据库里面的数据查出来
  437. $sql = $list;
  438. // 设置表头信息
  439. $objPHPExcel->setActiveSheetIndex(0)
  440. ->setCellValue('A1', '名称')
  441. ->setCellValue('B1', '油墨类别')
  442. ->setCellValue('C1', '领用重量(kg)')
  443. ->setCellValue('D1', '领用日期');
  444. /*--------------开始从数据库提取信息插入Excel表中------------------*/
  445. $i=2; //定义一个i变量,目的是在循环输出数据是控制行数
  446. $count = count($sql); //计算有多少条数据
  447. for ($i = 2; $i <= $count+1; $i++) {
  448. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $sql[$i-2]['product_name']);
  449. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $sql[$i-2]['ink_status']);
  450. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $sql[$i-2]['weight']);
  451. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $sql[$i-2]['create']);
  452. }
  453. /*--------------下面是设置其他信息------------------*/
  454. $objPHPExcel->getActiveSheet()->setTitle('领用库存报表'); //设置sheet的名称
  455. $objPHPExcel->setActiveSheetIndex(0); //设置sheet的起始位置
  456. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //通过PHPExcel_IOFactory的写函数将上面数据写出来
  457. $PHPWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel,"Excel2007");
  458. header('Content-Disposition: attachment;filename="领用库存报表.xlsx"');
  459. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  460. $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
  461. }
  462. //批次报表导出
  463. public function bachOut(){
  464. $params = input('');
  465. //导出
  466. $path = dirname(__FILE__); //找到当前脚本所在路径
  467. vendor("PHPExcel.PHPExcel");
  468. vendor("PHPExcel.PHPExcel.Writer.Excel5");
  469. vendor("PHPExcel.PHPExcel.Writer.Excel2007");
  470. vendor("PHPExcel.PHPExcel.IOFactory");
  471. $objPHPExcel = new \PHPExcel();
  472. $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
  473. $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
  474. $list = Db::name('warehousing_detail')->where('status',0)->field('product_name,bach_number,color,weight,create')->select();
  475. foreach ($list as $key=>$value){
  476. $list[$key]['ink_status'] = '专色墨';
  477. if ($value['weight'] > 0){
  478. $list[$key]['weight'] = $value['weight'] / 1000;
  479. }
  480. }
  481. $ink = Db::name('matter')->where('weight','>',0)->field('code,name,weight,create')->select();
  482. foreach ($ink as $k=>$v){
  483. $ink[$k]['ink_status'] = '油墨';
  484. $ink[$k]['color'] = '';
  485. $ink[$k]['product_name'] = $v['name'];
  486. $ink[$k]['bach_number'] = $v['code'];
  487. $ink[$k]['weight'] = $value['weight'] / 1000;
  488. unset( $ink[$k]['name']);
  489. unset( $ink[$k]['code']);
  490. array_push($list,$ink[$k]);
  491. }
  492. // 实例化完了之后就先把数据库里面的数据查出来
  493. $sql = $list;
  494. // 设置表头信息
  495. $objPHPExcel->setActiveSheetIndex(0)
  496. ->setCellValue('A1', '产品名称')
  497. ->setCellValue('B1', '油墨类别')
  498. ->setCellValue('C1', '颜色')
  499. ->setCellValue('D1', '批次号')
  500. ->setCellValue('E1', '重量(kg)')
  501. ->setCellValue('F1', '入库时间');
  502. /*--------------开始从数据库提取信息插入Excel表中------------------*/
  503. $i=2; //定义一个i变量,目的是在循环输出数据是控制行数
  504. $count = count($sql); //计算有多少条数据
  505. for ($i = 2; $i <= $count+1; $i++) {
  506. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $sql[$i-2]['product_name']);
  507. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $sql[$i-2]['ink_status']);
  508. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $sql[$i-2]['color']);
  509. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $sql[$i-2]['bach_number']);
  510. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $sql[$i-2]['weight']);
  511. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $sql[$i-2]['create']);
  512. }
  513. /*--------------下面是设置其他信息------------------*/
  514. $objPHPExcel->getActiveSheet()->setTitle('批次报表'); //设置sheet的名称
  515. $objPHPExcel->setActiveSheetIndex(0); //设置sheet的起始位置
  516. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //通过PHPExcel_IOFactory的写函数将上面数据写出来
  517. $PHPWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel,"Excel2007");
  518. header('Content-Disposition: attachment;filename="批次报表.xlsx"');
  519. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  520. $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
  521. }
  522. //月度报表导出
  523. public function mouthOut(){
  524. $params = input('');
  525. //导出
  526. $path = dirname(__FILE__); //找到当前脚本所在路径
  527. vendor("PHPExcel.PHPExcel");
  528. vendor("PHPExcel.PHPExcel.Writer.Excel5");
  529. vendor("PHPExcel.PHPExcel.Writer.Excel2007");
  530. vendor("PHPExcel.PHPExcel.IOFactory");
  531. $objPHPExcel = new \PHPExcel();
  532. $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
  533. $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
  534. $mouthStart = date('Y-m-01', strtotime(date("Y-m-d")));
  535. $mouthEnd = date('Y-m-d', strtotime("$mouthStart +1 month -1 day"));
  536. $list = Db::name('collar_use')->where('create','between time',[$mouthStart,$mouthEnd])->field('order_number,product_name,warehousing,weight,back,back_weight,status,machine_number,create')->select();
  537. foreach ($list as $key=>$value){
  538. $list[$key]['ink_status'] = '专色墨';
  539. if ($value['status'] == 1){
  540. $list[$key]['status'] = '领用';
  541. }else{
  542. $list[$key]['status'] = '退回';
  543. }
  544. if (!empty($value['weight'])){
  545. $list[$key]['weight'] = $value['weight'] / 1000;
  546. $list[$key]['color'] = Db::name('warehousing_detail')->where('bach_number',$value['warehousing'])->value('color');
  547. unset($list[$key]['back']);
  548. unset($list[$key]['back_weight']);
  549. }
  550. if (!empty($value['back_weight'])){
  551. $list[$key]['weight'] = $value['back_weight'] / 1000;
  552. $list[$key]['color'] = Db::name('warehousing_detail')->where('bach_number',$value['back'])->value('color');
  553. unset($list[$key]['back_weight']);
  554. }
  555. unset($list[$key]['warehousing']);
  556. unset($list[$key]['back']);
  557. }
  558. $ink = Db::name('matter_use')->where('create','between time',[$mouthStart,$mouthEnd])->field('order_number,product_name,out,out_weight,back,back_weight,status,machine_number,create')->order('order_number')->select();
  559. foreach ($ink as $k=>$v){
  560. $ink[$k]['ink_status'] = '油墨';
  561. $ink[$k]['color'] = '';
  562. if ($v['status'] == 1){
  563. $ink[$k]['status'] = '领用';
  564. }else{
  565. $ink[$k]['status'] = '退回';
  566. }
  567. if (!empty($v['out_weight'])){
  568. $ink[$k]['weight'] = $v['out_weight'] / 1000;
  569. }
  570. if (!empty($v['back_weight'])){
  571. $ink[$k]['weight'] = $v['back_weight'] / 1000;
  572. }
  573. unset($ink[$k]['out']);
  574. unset($ink[$k]['out_weight']);
  575. unset($ink[$k]['back']);
  576. unset($ink[$k]['back_weight']);
  577. array_push($list,$ink[$k]);
  578. }
  579. // 实例化完了之后就先把数据库里面的数据查出来
  580. $sql = $list;
  581. // 设置表头信息
  582. $objPHPExcel->setActiveSheetIndex(0)
  583. ->setCellValue('A1', '工单编号')
  584. ->setCellValue('B1', '产品名称')
  585. ->setCellValue('C1', '状态')
  586. ->setCellValue('D1', '重量')
  587. ->setCellValue('E1', '油墨类别')
  588. ->setCellValue('F1', '颜色')
  589. ->setCellValue('G1', '机台号')
  590. ->setCellValue('H1', '日期');
  591. /*--------------开始从数据库提取信息插入Excel表中------------------*/
  592. $i=2; //定义一个i变量,目的是在循环输出数据是控制行数
  593. $count = count($sql); //计算有多少条数据
  594. for ($i = 2; $i <= $count+1; $i++) {
  595. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $sql[$i-2]['order_number']);
  596. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $sql[$i-2]['product_name']);
  597. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $sql[$i-2]['status']);
  598. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $sql[$i-2]['weight']);
  599. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $sql[$i-2]['ink_status']);
  600. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $sql[$i-2]['color']);
  601. $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $sql[$i-2]['machine_number']);
  602. $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $sql[$i-2]['create']);
  603. }
  604. /*--------------下面是设置其他信息------------------*/
  605. $objPHPExcel->getActiveSheet()->setTitle('月度报表'); //设置sheet的名称
  606. $objPHPExcel->setActiveSheetIndex(0); //设置sheet的起始位置
  607. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //通过PHPExcel_IOFactory的写函数将上面数据写出来
  608. $PHPWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel,"Excel2007");
  609. header('Content-Disposition: attachment;filename="月度报表.xlsx"');
  610. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  611. $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
  612. }
  613. //专色墨换算成原墨导出
  614. public function warehousingOut(){
  615. $params = input('productNumber');
  616. if (empty($params)){
  617. return $this->error('请输入工单号导出');
  618. }
  619. //查出产品编号
  620. $result = Db::connect('db2')->query("select * from 工单_基本资料 where Gd_gdbh = '{$params}' limit 1");
  621. if (empty($result)){
  622. return array('status'=>0,'msg'=>'未查到相关信息,请核查工单号');
  623. }
  624. // $product_name = trim($result[0]['成品名称']);
  625. $product_number = trim($result[0]['Gd_cpdh']);
  626. //领用总重量-退回总重量 = 消耗总重量
  627. $where = [];
  628. $where['order_number'] = $params;
  629. $where['status'] = 1;
  630. $where['l_status'] = 2;
  631. $data = Db::name('collar_use')->where($where)->field('order_number,warehousing,weight')->select();
  632. if (empty($data)){
  633. return $this->error('暂无数据');
  634. }
  635. //获得领用的所有专色批次号
  636. $bach = [];
  637. foreach ($data as $key=>$value){
  638. $bach[$key]= $value['warehousing'];
  639. }
  640. $bach = array_merge(array_unique($bach));
  641. //计算领走的总重量
  642. $mapOut = [];
  643. $mapOut['status'] = 1;
  644. $mapOut['l_status'] = 2;
  645. //退回查询条件
  646. $mapBack['status'] = 2;
  647. $list = [];
  648. foreach ($bach as $k=>$v){
  649. // $list[$k]['order_number'] = $params;
  650. // $list[$k]['product_name'] = $product_name;
  651. // $list[$k]['warehousing'] = $v;
  652. //领走
  653. $outWeight = Db::name('collar_use')->where($mapOut)->where('warehousing',$v)->sum('weight');
  654. $outWeight = $outWeight / 1000;
  655. //退回
  656. $backWeight = Db::name('collar_use')->where($mapBack)->where('back',$v)->sum('weight');
  657. if ($backWeight > 0){
  658. $backWeight = $backWeight / 1000;
  659. }
  660. //报废
  661. $scrap = Db::name('scrap_ink')->where(['order_number'=>$params,'bach_number'=>$v])->sum('weight');
  662. if ($scrap > 0){
  663. $scrap = $scrap / 1000;
  664. }
  665. $list[$k]['cid'] = Db::name('warehousing_detail')->where('bach_number',$v)->value('cid');
  666. $list[$k]['total'] = $outWeight - $backWeight - $scrap;
  667. }
  668. //查出所有专色,去掉重量为0的专色
  669. $map[] = ['exp',Db::raw("FIND_IN_SET($product_number,product_number)")];
  670. $allColor = Db::name('formula')->where($map)->field('id')->select();
  671. foreach ($allColor as $k1=>$v1){
  672. $allColor[$k1]['weight'] = 0;
  673. foreach ($list as $key1=>$value1){
  674. if ($value1['cid'] == $allColor[$k1]['id']){
  675. $allColor[$k1]['weight'] = $allColor[$k1]['weight'] + $value1['total'];
  676. }
  677. }
  678. if ($allColor[$k1]['weight'] == 0){
  679. unset($allColor[$k1]);
  680. }
  681. }
  682. //根据配方计算消耗原墨重量
  683. $allColor = array_merge($allColor);
  684. $formulaData = Db::name('formula_detail')->where($map)->column('ink');
  685. $formulaData = array_merge(array_unique($formulaData));
  686. $returnData = [];
  687. $i = 0;
  688. foreach ($allColor as $key2=>$value2){
  689. $formula = Db::name('formula_detail')->where('father',$value2['id'])->field('father,ink,ink_number')->select();
  690. $totalInk = array_sum(array_column($formula,'ink_number'));
  691. foreach ($formula as $k2=>$v2){
  692. // $detailInk[$key2][$k2]['cid'] = $formula[$k2]['father'];
  693. // $detailInk[$key2][$k2]['ink'] = $formula[$k2]['ink'];
  694. $ratio = $formula[$k2]['ink_number'] /$totalInk ;
  695. $total = $value2['weight'] * $ratio * 1000;
  696. // $detailInk[$key2][$k2]['total'] = round($total);
  697. $returnData[$i]['ink'] = $v2['ink'];
  698. $returnData[$i]['total'] = round($total);
  699. $i++;
  700. }
  701. }
  702. foreach ($formulaData as $k3=>$v3){
  703. $lastData[$k3]['weight'] = 0;
  704. $lastData[$k3]['ink'] = $v3;
  705. foreach ($returnData as $key3=>$value3){
  706. if ($value3['ink'] == $v3){
  707. $lastData[$k3]['weight'] = $lastData[$k3]['weight'] + $value3['total'];
  708. }
  709. }
  710. }
  711. //导出
  712. $path = dirname(__FILE__); //找到当前脚本所在路径
  713. vendor("PHPExcel.PHPExcel");
  714. vendor("PHPExcel.PHPExcel.Writer.Excel5");
  715. vendor("PHPExcel.PHPExcel.Writer.Excel2007");
  716. vendor("PHPExcel.PHPExcel.IOFactory");
  717. $objPHPExcel = new \PHPExcel();
  718. $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
  719. $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
  720. // 实例化完了之后就先把数据库里面的数据查出来
  721. $sql = $lastData;
  722. // 设置表头信息
  723. $objPHPExcel->setActiveSheetIndex(0)
  724. ->setCellValue('A1', '序号')
  725. ->setCellValue('B1', '原墨颜色')
  726. ->setCellValue('C1', '消耗重量(g)');
  727. /*--------------开始从数据库提取信息插入Excel表中------------------*/
  728. $i=2; //定义一个i变量,目的是在循环输出数据是控制行数
  729. $count = count($sql); //计算有多少条数据
  730. for ($i = 2; $i <= $count+1; $i++) {
  731. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $i-1);
  732. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $sql[$i-2]['ink']);
  733. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $sql[$i-2]['weight']);
  734. }
  735. /*--------------下面是设置其他信息------------------*/
  736. $objPHPExcel->getActiveSheet()->setTitle($params.'专墨换算原墨报表'); //设置sheet的名称
  737. $objPHPExcel->setActiveSheetIndex(0); //设置sheet的起始位置
  738. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //通过PHPExcel_IOFactory的写函数将上面数据写出来
  739. $PHPWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel,"Excel2007");
  740. header('Content-Disposition: attachment;filename="'.$params.'专墨换算原墨报表.xlsx"');
  741. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  742. $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
  743. }
  744. //原墨导出
  745. public function matterOut(){
  746. $params = input('order_number');
  747. $where = [];
  748. if (!empty($params)){
  749. $where['order_number'] = $params;
  750. }
  751. //导出
  752. $path = dirname(__FILE__); //找到当前脚本所在路径
  753. vendor("PHPExcel.PHPExcel");
  754. vendor("PHPExcel.PHPExcel.Writer.Excel5");
  755. vendor("PHPExcel.PHPExcel.Writer.Excel2007");
  756. vendor("PHPExcel.PHPExcel.IOFactory");
  757. $objPHPExcel = new \PHPExcel();
  758. $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
  759. $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
  760. $list = Db::name('matter_use')
  761. ->where('status',1)
  762. ->where($where)
  763. ->field('product_name,out_weight,create')
  764. ->order('order_number asc')
  765. ->select();
  766. foreach ($list as $k=>$v){
  767. $list[$k]['ink_status'] = '原墨';
  768. $list[$k]['color'] = '';
  769. $list[$k]['weight'] = $v['out_weight'] / 1000;
  770. unset( $list[$k]['out_weight']);
  771. }
  772. // 实例化完了之后就先把数据库里面的数据查出来
  773. $sql = $list;
  774. // 设置表头信息
  775. $objPHPExcel->setActiveSheetIndex(0)
  776. ->setCellValue('A1', '名称')
  777. ->setCellValue('B1', '油墨类别')
  778. ->setCellValue('C1', '领用重量(kg)')
  779. ->setCellValue('D1', '领用日期');
  780. /*--------------开始从数据库提取信息插入Excel表中------------------*/
  781. $i=2; //定义一个i变量,目的是在循环输出数据是控制行数
  782. $count = count($sql); //计算有多少条数据
  783. for ($i = 2; $i <= $count+1; $i++) {
  784. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $sql[$i-2]['product_name']);
  785. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $sql[$i-2]['ink_status']);
  786. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $sql[$i-2]['weight']);
  787. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $sql[$i-2]['create']);
  788. }
  789. /*--------------下面是设置其他信息------------------*/
  790. $objPHPExcel->getActiveSheet()->setTitle('原墨领用报表'); //设置sheet的名称
  791. $objPHPExcel->setActiveSheetIndex(0); //设置sheet的起始位置
  792. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //通过PHPExcel_IOFactory的写函数将上面数据写出来
  793. $PHPWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel,"Excel2007");
  794. header('Content-Disposition: attachment;filename="原墨领用报表.xlsx"');
  795. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  796. $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
  797. }
  798. //php判断二维数组中是否含有某个值
  799. public function deep_in_array($value, $array) {
  800. foreach($array as $item) {
  801. if(!is_array($item)) {
  802. if ($item == $value) {
  803. return true;
  804. } else {
  805. continue;
  806. }
  807. }
  808. if(in_array($value, $item)) {
  809. return true;
  810. } else if($this->deep_in_array($value, $item)) {
  811. return true;
  812. }
  813. }
  814. return false;
  815. }
  816. }