MateReport.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\util\ExcelUtil;
  4. use think\Db;
  5. class MateReport extends Auth {
  6. public function index() {
  7. if (request()->isAjax()) {
  8. //分页参数
  9. $length = input('rows', 10, 'intval'); //每页条数
  10. $page = input('page', 1, 'intval'); //第几页
  11. $start = ($page - 1) * $length; //分页开始位置
  12. //排序
  13. $sortRow = input('sidx', 'id', 'trim'); //排序列
  14. $sort = input('sord', 'asc', 'trim'); //排序方式
  15. $order = $sortRow . ' ' . $sort;
  16. $title = input('title', '', 'trim');
  17. if ($title) {
  18. $map[] = ['title', 'like', '%' . $title . '%'];
  19. }
  20. $map[] = ['org_id', '=', $this->orgId];
  21. $map = empty($map) ? true : $map;
  22. //数据查询
  23. $lists = Db::name('mate_report')
  24. ->where($map)->limit($start, $length)
  25. ->order($order)->select();
  26. //数据返回
  27. $totalCount = Db::name('mate_report')->where($map)->count();
  28. $totalPage = ceil($totalCount / $length);
  29. $result['page'] = $page;
  30. $result['total'] = $totalPage;
  31. $result['records'] = $totalCount;
  32. $result['rows'] = $lists;
  33. return json($result);
  34. }
  35. else {
  36. $this->assign('meta_title', '月份统计记录');
  37. return $this->fetch();
  38. }
  39. }
  40. public function detail($rid=0) {
  41. if (request()->isAjax()) {
  42. //分页参数
  43. $length = input('rows', 10, 'intval'); //每页条数
  44. $page = input('page', 1, 'intval'); //第几页
  45. $start = ($page - 1) * $length; //分页开始位置
  46. //排序
  47. $sortRow = input('sidx', 'a.id', 'trim'); //排序列
  48. $sort = input('sord', 'asc', 'trim'); //排序方式
  49. $order = $sortRow . ' ' . $sort;
  50. $title = input('title', '', 'trim');
  51. if ($title) {
  52. $map[] = ['b.title', 'like', '%' . $title . '%'];
  53. }
  54. $map[] = ['a.report_id', '=', $rid];
  55. $map = empty($map) ? true : $map;
  56. //数据查询
  57. $lists = Db::name('mate_report_goods')
  58. ->alias('a')
  59. ->join('mate_goods b','a.goods_id = b.id')
  60. ->field('a.*,b.title,b.brand,b.spec')
  61. ->where($map)->limit($start, $length)
  62. ->order($order)->select();
  63. //数据返回
  64. $totalCount = Db::name('mate_report_goods')
  65. ->alias('a')
  66. ->join('mate_goods b','a.goods_id = b.id')
  67. ->where($map)->count();
  68. $totalPage = ceil($totalCount / $length);
  69. $result['page'] = $page;
  70. $result['total'] = $totalPage;
  71. $result['records'] = $totalCount;
  72. $result['rows'] = $lists;
  73. return json($result);
  74. }
  75. else {
  76. $this->assign('rid',$rid);
  77. $this->assign('meta_title', '月份统计记录详情');
  78. return $this->fetch();
  79. }
  80. }
  81. public function add(){
  82. $sday = date('Y-m', strtotime('last month')).'-25 ';
  83. $starttime = $sday." 00:00:00";
  84. $eday = date('Y-m').'-25 ';
  85. $endtime = $eday."00:00:00";
  86. $orgs = Db::name("org")->where('type',2)->where('enable',1)->where('del',0)->select();
  87. foreach($orgs as $k=>$v){
  88. $goods = Db::name('mate_goods')
  89. ->where('org_id',$v['id'])
  90. ->where('del',0)
  91. ->where('enable',1)
  92. ->field('id,nums')
  93. ->select();
  94. if(!$goods){ // 无商品不生成记录
  95. continue;
  96. }
  97. $innums = 0;
  98. $outnums = 0;
  99. $usenums = 0;
  100. $cznums = 0;
  101. $nums = 0;
  102. $reportGoods = [];
  103. foreach ($goods as $kk=>$vv){
  104. $innums1 = Db::name('mate_apply_goods')
  105. ->alias('a')
  106. ->join('mate_apply b','a.apply_id = b.id')
  107. ->where('b.del',0)
  108. ->where('b.type',1)
  109. ->where('b.org_id',$v['id'])
  110. ->where('a.goods_id',$vv['id'])
  111. ->where('b.create_time','>=',$starttime)
  112. ->where('b.create_time','<',$endtime)
  113. ->sum('a.nums');
  114. $innums += $innums1;
  115. $outnums1 = Db::name('mate_apply_goods')
  116. ->alias('a')
  117. ->join('mate_apply b','a.apply_id = b.id')
  118. ->where('b.del',0)
  119. ->where('b.type',2)
  120. ->where('b.org_id',$v['id'])
  121. ->where('a.goods_id',$vv['id'])
  122. ->where('b.create_time','>=',$starttime)
  123. ->where('b.create_time','<',$endtime)
  124. ->sum('a.nums');
  125. $outnums += $outnums1;
  126. $usenums1 = Db::name('todo_mate_item')
  127. ->alias('a')
  128. ->join('todo_mate b','a.todo_mate_id = b.id')
  129. ->where('b.org_id',$v['id'])
  130. ->where('a.items_id',$vv['id'])
  131. ->where('b.create_time','>=',$starttime)
  132. ->where('b.create_time','<',$endtime)
  133. ->sum('a.total');
  134. $usenums += $usenums1;
  135. $cznums1 = Db::name('mate_goods_log')
  136. ->where('type',1)
  137. ->where('org_id',$v['id'])
  138. ->where('goods_id',$vv['id'])
  139. ->where('create_time','>=',$starttime)
  140. ->where('create_time','<',$endtime)
  141. ->sum('nums');
  142. $cznums += $cznums1;
  143. $nums += $vv['nums'];
  144. $reportGoods[] = [
  145. 'org_id' => $v['id'],
  146. 'goods_id' => $vv['id'],
  147. 'in_nums' => $innums1,
  148. 'out_nums' => $outnums1,
  149. 'use_nums' => $usenums1,
  150. 'cz_nums' => $cznums1,
  151. 'nums' => $vv['nums'],
  152. ];
  153. }
  154. Db::startTrans();
  155. try{
  156. $reportId = Db::name("mate_report")->insertGetId([
  157. 'org_id' => $v['id'],
  158. 'start_time' => $sday,
  159. 'end_time' => $eday,
  160. 'in_nums' => $innums,
  161. 'out_nums' => $outnums,
  162. 'use_nums' => $usenums,
  163. 'cz_nums' => $cznums,
  164. 'nums' => $nums,
  165. 'create_time' => date("Y-m-d H:i:s")
  166. ]);
  167. $arr = [];
  168. foreach ($reportGoods as $kk=>$vv){
  169. $vv['report_id'] = $reportId;
  170. $arr[] = $vv;
  171. if(count($arr) == 200){
  172. $res = Db::name("mate_report_goods")->insertAll($arr);
  173. if($res != count($arr)){
  174. exception("操作失败");
  175. }
  176. $arr = [];
  177. }
  178. }
  179. if(count($arr) > 0){
  180. $res = Db::name("mate_report_goods")->insertAll($arr);
  181. if($res != count($arr)){
  182. exception("操作失败");
  183. }
  184. }
  185. Db::commit();
  186. }catch (\Exception $e){
  187. Db::rollback();
  188. }
  189. }
  190. }
  191. public function export($rid=0){
  192. $title = input('title', '', 'trim');
  193. if ($title) {
  194. $map[] = ['b.title', 'like', '%' . $title . '%'];
  195. }
  196. $map[] = ['a.report_id', '=', $rid];
  197. $map = empty($map) ? true : $map;
  198. //数据查询
  199. $lists = Db::name('mate_report_goods')
  200. ->alias('a')
  201. ->join('mate_goods b','a.goods_id = b.id')
  202. ->field('a.*,b.title,b.brand,b.spec')
  203. ->where($map)
  204. ->order('a.id asc')->select();
  205. $header = [
  206. ['title' => '名称', 'name' => 'title','width'=>'20'],
  207. ['title' => '品牌', 'name' => 'brand','width'=>'20'],
  208. ['title' => '规格', 'name' => 'spec','width'=>'20'],
  209. ['title' => '入库', 'name' => 'in_nums','width'=>'20'],
  210. ['title' => '出库', 'name' => 'out_nums','width'=>'20'],
  211. ['title' => '订单消耗', 'name' => 'use_nums','width'=>'20'],
  212. ['title' => '处置', 'name' => 'cz_nums','width'=>'20'],
  213. ['title' => '结余', 'name' => 'nums','width'=>'20']
  214. ];
  215. $filename = '统计详情';
  216. ExcelUtil::export($filename,$header,$lists);
  217. }
  218. }