|
- <?php
-
- namespace backend\modules\motorcade\controllers;
-
- use backend\modules\motorcade\models\BusDepartment;
- use backend\modules\motorcade\models\BusOrder;
- use backend\modules\motorcade\models\DriverFeeSearch;
- use backend\modules\motorcade\models\ReportModel;
- use backend\modules\motorcade\models\search\searchBusIncome;
- use backend\modules\motorcade\models\BusReport;
- use common\components\zPhpExcel;
- use Yii;
- use yii\data\SqlDataProvider;
- use yii\filters\AccessControl;
- use yii\helpers\ArrayHelper;
-
- class ReportController extends BaseController
- {
-
- public $layout = "@backend/modules/motorcade/views/layouts/iframe_new";
-
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'actions' => ['bus-income', 'hybrid', 'hybrid-detail', 'road-bridge', 'driver-fee', 'driver-fee-detail', 'bus-detail', 'export', 'excel'],
- 'allow' => true,
- 'roles' => ['@'],
- ],
- ],
- ]
- ];
- }
-
- public function actionIndex()
- {
- return $this->render('index');
- }
-
- /**
- * 出车收统计
- * @return string
- */
- public function actionBusIncome()
- {
- $searchModel = new searchBusIncome();
- $searchModel->date_from = date('Y-m-01');
- $searchModel->date_to = date('Y-m-t');
- $searchModel->line_type = 1;
- $searchModel->date_type = 1;
- $searchModel->search_type = 0;
-
- $search = Yii::$app->request->queryParams;
- $dataProvider = $searchModel->search($search);
-
- return $this->render('report', ['view' => '_busIncome', 'dataProvider' => $dataProvider, 'searchModel' => $searchModel]);
-
- }
-
-
- // /*by luocj
- // * 油电费用统计
- // */
- // public function actionHybrid()
- // {
- // $obj = new BusReport();
- // $cur_date = date('Y-m-d');
- // $start_date = $obj->getCurMonthFirstDay($cur_date);
- // $end_date = $obj->getCurMonthLastDay($cur_date);
- // //获取数据
- // $get_start_date = isset($_REQUEST['start_date']) ? $_REQUEST['start_date'] : $start_date;
- // $get_end_date = isset($_REQUEST['end_date']) ? $_REQUEST['end_date'] : $end_date;
- // $get_bus_license = isset($_REQUEST['bus_license']) && $_REQUEST['bus_license'] != '' ? $_REQUEST['bus_license'] : -1;
- // $get_status = isset($_REQUEST['status']) && $_REQUEST['status'] != '' ? $_REQUEST['status'] : 479;
- // $bus_license = $obj->getBusLicense();
- // $data = $obj->getHybrid($get_start_date, $get_end_date, $get_bus_license, $get_status);
- // $dataProvider1 = new SqlDataProvider([
- // 'sql' => $data['sql_jiayou'],
- // 'pagination' => [
- // 'pagesize' => '50',
- // ],
- // 'totalCount' => $data['data_total_jiayou'],
- // ]);
- // $dataProvider2 = new SqlDataProvider([
- // 'sql' => $data['sql_chongdian'],
- // 'pagination' => [
- // 'pagesize' => '50',
- // ],
- // 'totalCount' => $data['data_total_chongdian'],
- // ]);
- // return $this->render('report', ['view' => '_hybrid', 'bus_license' => $bus_license, 'dataProvider1' => $dataProvider1, 'dataProvider2' => $dataProvider2, 'status' => $get_status, 'start_date' => $get_start_date, 'end_date' => $get_end_date]);
- // }
- /*by luocj
- * 油电费用统计
- */
- public function actionHybrid()
- {
- $obj1 = new BusOrder();
- $obj2 = new BusOrder();
- $cur_date = date('Y-m-d');
- $start_date = BusReport::getCurMonthFirstDay($cur_date);
- $end_date = BusReport::getCurMonthLastDay($cur_date);
- //获取数据
- $get_start_date = isset($_REQUEST['start_date']) ? $_REQUEST['start_date'] : $start_date;
- $get_end_date = isset($_REQUEST['end_date']) ? $_REQUEST['end_date'] : $end_date;
- $get_bus_license = isset($_REQUEST['bus_license']) && $_REQUEST['bus_license'] != '' ? $_REQUEST['bus_license'] : -1;
- $get_status = isset($_REQUEST['status']) && $_REQUEST['status'] != '' ? $_REQUEST['status'] : 479;
- $op_you = 'hybrid_you';
- $op_dian = 'hybrid_dian';
- $show = true;
- $params1 = ['start_date' => $get_start_date, 'end_date' => $get_end_date, 'bus_id' => $get_bus_license, 'op' => $op_you, 'show' => $show];
- $params2 = ['start_date' => $get_start_date, 'end_date' => $get_end_date, 'bus_id' => $get_bus_license, 'op' => $op_dian, 'show' => $show];
- $dataProvider1 = $obj1->getExport($params1);
- $dataProvider2 = $obj2->getExport($params2);
- $bus_license = BusReport::getBusLicense();
- return $this->render('report', ['view' => '_hybrid', 'bus_license' => $bus_license, 'dataProvider1' => $dataProvider1, 'dataProvider2' => $dataProvider2, 'status' => $get_status, 'start_date' => $get_start_date, 'end_date' => $get_end_date]);
- }
-
- /**
- * by luocj
- * 油电费用详情统计
- */
- public function actionHybridDetail()
- {
- $obj = new BusReport();
- $cur_date = date('Y-m-d');
- $start_date = $obj->getCurMonthFirstDay($cur_date);
- $end_date = $obj->getCurMonthLastDay($cur_date);
- //获取数据
- $get_start_date = isset($_REQUEST['start_date']) ? $_REQUEST['start_date'] : $start_date;
- $get_end_date = isset($_REQUEST['end_date']) ? $_REQUEST['end_date'] : $end_date;
- $get_bus_license = isset($_REQUEST['bus_license']) && $_REQUEST['bus_license'] != '' ? $_REQUEST['bus_license'] : -1;
- $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'jiayou';
- $data = $obj->getHybridDetail($get_start_date, $get_end_date, $get_bus_license);
- $dataProvider1 = new SqlDataProvider([
- 'sql' => $data['sql_jiayou'],
- 'pagination' => [
- 'pagesize' => '15',
- ],
- 'totalCount' => $data['data_total_jiayou'],
- ]);
- $dataProvider2 = new SqlDataProvider([
- 'sql' => $data['sql_chongdian'],
- 'pagination' => [
- 'pagesize' => '10',
- ],
- 'totalCount' => $data['data_total_chongdian'],
- ]);
- return $this->render('report', ['view' => '_hybridDetail', 'dataProvider1' => $dataProvider1, 'dataProvider2' => $dataProvider2, 'start_date' => $get_start_date, 'end_date' => $get_end_date, 'bus_license' => $get_bus_license, 'type' => $type]);
- }
-
- /*by luocj
- * 路桥费用统计
- */
- public function actionRoadBridge()
- {
- $obj = new BusOrder();
- $cur_date = date('Y-m-d');
- $start_date = BusReport::getCurMonthFirstDay($cur_date);
- $end_date = BusReport::getCurMonthLastDay($cur_date);
- //获取数据
- $get_start_date = isset($_REQUEST['start_date']) ? $_REQUEST['start_date'] : $start_date;
- $get_end_date = isset($_REQUEST['end_date']) ? $_REQUEST['end_date'] : $end_date;
- $get_bus_license = isset($_REQUEST['bus_license']) && $_REQUEST['bus_license'] != '' ? $_REQUEST['bus_license'] : -1;
- $op = 'road-bridge';
- $show = true;
- $params = ['start_date' => $get_start_date, 'end_date' => $get_end_date, 'bus_id' => $get_bus_license, 'op' => $op, 'show' => $show];
- $dataProvider = $obj->getExport($params);
- $bus_license = BusReport::getBusLicense();
- return $this->render('report', ['view' => '_roadBridge', 'dataProvider' => $dataProvider, 'bus_license' => $bus_license, 'start_date' => $get_start_date, 'end_date' => $get_end_date]);
- }
-
- // /**
- // * User: wangxj
- // *
- // * 司机费用统计
- // *
- // * @return string
- // */
- // public function actionDriverFee()
- // {
- // $searchModel = new DriverFeeSearch();
- // $dataProvider = $searchModel->search(Yii::$app->request->post('DriverFeeSearch'));
- // $driver_name = ReportModel::getBusDriver();
- // return $this->render('report', [
- // 'view' => '_driverFee',
- // 'searchModel' => $searchModel,
- // 'dataProvider' => $dataProvider,
- // 'driver_name' => $driver_name,
- // ]);
- // }
- /**
- * User: wangxj
- *
- * 司机费用统计
- *
- * @return string
- */
- public function actionDriverFee()
- {
- $obj = new BusOrder();
- $cur_date = date('Y-m-d');
- $start_date = BusReport::getCurMonthFirstDay($cur_date);
- $end_date = BusReport::getCurMonthLastDay($cur_date);
- //获取数据
- $tmp = Yii::$app->request->post('BusOrder');
- $get_date_type = isset($tmp['date_type']) ? $tmp['date_type'] : 1;
- $get_start_date = isset($tmp['start_date']) ? $tmp['start_date'] : $start_date;
- $get_end_date = isset($tmp['end_date']) ? $tmp['end_date'] : $end_date;
- $get_driver_id = isset($tmp['driver_id']) ? $tmp['driver_id'] : -1;
- $op = 'driver-fee';
- $show = true;
- $params = ['get_date_type' => $get_date_type, 'start_date' => $get_start_date, 'end_date' => $get_end_date, 'driver_id' => $get_driver_id, 'op' => $op, 'show' => $show];
-
- $dataProvider = $obj->getExport($params);
- $driver_name = ReportModel::getBusDriver();
- return $this->render('report', [
- 'view' => '_driverFee',
- 'searchModel' => $obj,
- 'dataProvider' => $dataProvider,
- 'driver_name' => $driver_name,
- ]);
- }
-
- /**
- * User: wangxj
- *
- * 司机费用中的明细
- *
- * @param $driver_id integer 司机id
- *
- * @return
- */
- public function actionDriverFeeDetail($bus_driver_res_id)
- {
- $searchModel = new DriverFeeSearch();
- $dataProvider = $searchModel->detailSearch($bus_driver_res_id, Yii::$app->request->post('DriverFeeSearch'));
- $dataProvider->setSort(false);
-
- //用车单位搜索
- $depart = BusDepartment::findAll(['cancel_flag' => 0]);
- return $this->render('report', [
- 'view' => '_driverDetailFee',
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- 'data' => ['depart' => $depart]
- ]);
-
- }
-
- /**
- * 出车收入统计详情
- * @return string
- */
- public function actionBusDetail()
- {
- $obj = new BusOrder();
- $cur_date = date('Y-m-d');
- $start_date = BusReport::getCurMonthFirstDay($cur_date);
- $end_date = BusReport::getCurMonthLastDay($cur_date);
- $get_start_date = isset($_REQUEST['start_date']) ? $_REQUEST['start_date'] : $start_date;
- $get_end_date = isset($_REQUEST['end_date']) ? $_REQUEST['end_date'] : $end_date;
- $get_bus_license = isset($_REQUEST['bus_id']) ? $_REQUEST['bus_id'] : '-1';
- $get_bus_license = isset($_REQUEST['bus_license']) ? $_REQUEST['bus_license'] : $get_bus_license;
- $get_bus_number = isset($_REQUEST['bus_number']) ? $_REQUEST['bus_number'] : '';
- $get_date_type = isset($_REQUEST['date_type']) ? $_REQUEST['date_type'] : '1';
- $get_bus_user = isset($_REQUEST['bus_user']) ? $_REQUEST['bus_user'] : '-1';
- $get_line_type = isset($_REQUEST['line_type']) ? $_REQUEST['line_type'] : '-1';
- $get_driver_id = isset($_REQUEST['bus_driver_res_id']) ? $_REQUEST['bus_driver_res_id'] : '-1';
- $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : 'detail';
- $show = true;
- $params = ['date_type' => $get_date_type, 'start_date' => $get_start_date, 'end_date' => $get_end_date,
- 'bus_id' => $get_bus_license, 'driver_id' => $get_driver_id, 'line_type' => $get_line_type,
- 'use_bus_org_id' => $get_bus_user, 'op' => $op, 'show' => $show, 'bus_number' => $get_bus_number];
- $dataProvider = $obj->getExport($params);
- $dataProvider->pagination = ['pagesize' => 10];
- $bus_user = BusReport::getBusDepartment();
- return $this->render('report', ['view' => '_busDetail', 'dataProvider' => $dataProvider, 'bus_user' => $bus_user, 'bus_id' => $get_bus_license, 'start_date' => $get_start_date, 'end_date' => $get_end_date]);
- }
-
- //车管报表导出
- public function actionExport()
- {
- $date_type = isset($_REQUEST['date_type']) ? $_REQUEST['date_type'] : '1';
- $start_date = isset($_REQUEST['start_date']) ? $_REQUEST['start_date'] : '';
- $end_date = isset($_REQUEST['end_date']) ? $_REQUEST['end_date'] : '';
- // $use_bus_org_id = isset($_POST['use_bus_org_id']) ? $_POST['use_bus_org_id'] : (isset($_GET['use_bus_org_id']) ? $_GET['use_bus_org_id'] : -1);
- $use_bus_org_id = $_POST['bus_user'];
- $driver_id = isset($_REQUEST['driver_id']) ? $_REQUEST['driver_id'] : -1;
- $bus_id = isset($_REQUEST['bus_id']) ? $_REQUEST['bus_id'] : -1;
- $line_type = isset($_REQUEST['line_type']) ? $_REQUEST['line_type'] : -1;
- $bus_number = isset($_REQUEST['bus_number']) ? $_REQUEST['bus_number'] : -1;
- //op
- //bus-income 出车收入
- //hybrid 油电费用
- //road-bridge 路桥
- //driver-fee 司机费用
- //detail 订单明细
- $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
- $obj = new BusOrder();
- $params = array(
- 'date_type' => $date_type,
- 'start_date' => $start_date,
- 'end_date' => $end_date,
- 'use_bus_org_id' => $use_bus_org_id,
- 'driver_id' => $driver_id,
- 'bus_id' => $bus_id,
- 'line_type' => $line_type,
- 'op' => $op
- );
- $data = $obj->getExport($params);
- if ($op == 'bus-income') {
- $res = $this->actionExportBusIncome($data);
- } elseif ($op == 'road-bridge') {
- $res = $this->actionExportRoadBridge($data);
- } elseif ($op == 'driver-fee') {
- $res = $this->actionExportDriverFee($data);
- } elseif ($op == 'detail') {
- $res = $this->actionExportDetail($data);
- } elseif (in_array($op, ['hybrid_you', 'hybrid_dian'])) {
- $res = $this->actionExportHybrid($op, $data);
- } else {
-
- }
- if (in_array($op, ['bus-income', 'road-bridge', 'driver-fee', 'detail', 'hybrid_you', 'hybrid_dian']) &&
- isset($obj) && isset($res['file_name']) && isset($res['data_title']) && isset($res['data_all']) && isset($res['data_list'])
- ) {
- $this->exportExcel($obj, $res['file_name'], $res['data_title'], $res['data_all'], $res['data_list']);
- } else {
- return "<script>Command: parent.toastr[\"warning\"](\"报表导出失败\");</script>";
- }
- }
-
- private function actionExportBusIncome($data)
- {
- $res['file_name'] = '出车收入统计';
- $res['data_title'] = array('用车单位', '结算方式', '结算周期', '出车数', '收入', '运营成本', '毛利', '公里数', '预估油电费', '路桥费', '司贴', '停车费', '餐饮费', '住宿费');
- $res['data_all'][0]['heji'] = '总计';
- $res['data_all'][0]['heji1'] = '-';
- $res['data_all'][0]['heji2'] = '-';
- $res['data_all'][0]['chuche_count'] = 0;
- $res['data_all'][0]['income'] = 0;
- $res['data_all'][0]['all_cost'] = 0;
- $res['data_all'][0]['maoli'] = 0;
- $res['data_all'][0]['lichengshu'] = 0;
- $res['data_all'][0]['youdian'] = 0;
- $res['data_all'][0]['luqiao'] = 0;
- $res['data_all'][0]['sitie'] = 0;
- $res['data_all'][0]['tingche'] = 0;
- $res['data_all'][0]['canyin'] = 0;
- $res['data_all'][0]['zhusu'] = 0;
- foreach ($data as $k => $v) {
- $res['data_list'][$k][] = $v['use_bus_org_name'];
- $res['data_list'][$k][] = $v['depart_type'];
- $res['data_list'][$k][] = $v['depart_cycle'];
- $res['data_list'][$k][] = $v['chuche_count'];
- $res['data_list'][$k][] = $v['income'];
- $res['data_list'][$k][] = $v['all_cost'];
- $res['data_list'][$k][] = $v['maoli'];
- $res['data_list'][$k][] = $v['lichengshu'];
- $res['data_list'][$k][] = $v['youdian'];
- $res['data_list'][$k][] = $v['luqiao'];
- $res['data_list'][$k][] = $v['sitie'];
- $res['data_list'][$k][] = $v['tingche'];
- $res['data_list'][$k][] = $v['canyin'];
- $res['data_list'][$k][] = $v['zhusu'];
- $res['data_all'][0]['chuche_count'] += $v['chuche_count'];
- $res['data_all'][0]['income'] += $v['income'];
- $res['data_all'][0]['all_cost'] += $v['all_cost'];
- $res['data_all'][0]['maoli'] += $v['maoli'];
- $res['data_all'][0]['lichengshu'] += $v['lichengshu'];
- $res['data_all'][0]['youdian'] += $v['youdian'];
- $res['data_all'][0]['luqiao'] += $v['luqiao'];
- $res['data_all'][0]['sitie'] += $v['sitie'];
- $res['data_all'][0]['tingche'] += $v['tingche'];
- $res['data_all'][0]['canyin'] += $v['canyin'];
- $res['data_all'][0]['zhusu'] += $v['zhusu'];
- }
- return $res;
- }
-
- private function actionExportRoadBridge($data)
- {
- $res['file_name'] = '路桥费用统计';
- $res['data_title'] = array('车辆', '品牌', '座位', '出车数', '出车公里数', '出车路桥费');
- $res['data_all'][0]['heji'] = '总计';
- $res['data_all'][0]['heji1'] = '-';
- $res['data_all'][0]['heji2'] = '-';
- $res['data_all'][0]['chuche_count'] = 0;
- $res['data_all'][0]['lichengshu'] = 0;
- $res['data_all'][0]['luqiao'] = 0;
- foreach ($data as $k => $v) {
- $res['data_list'][$k][] = $v['bus_name'];
- $res['data_list'][$k][] = $v['brand_name'];
- $res['data_list'][$k][] = $v['seat_desc'];
- $res['data_list'][$k][] = $v['chuche_count'];
- $res['data_list'][$k][] = $v['lichengshu'];
- $res['data_list'][$k][] = $v['luqiao'];
- $res['data_all'][0]['chuche_count'] += $v['chuche_count'];
- $res['data_all'][0]['lichengshu'] += $v['lichengshu'];
- $res['data_all'][0]['luqiao'] += $v['luqiao'];
- }
- return $res;
- }
-
- private function actionExportDriverFee($data)
- {
- $res['file_name'] = '司机费用统计';
- $res['data_title'] = array('司机姓名', '出车数', '公里数', '司贴', '停车费', '餐饮费', '住宿费');
- $res['data_all'][0]['heji'] = '总计';
- $res['data_all'][0]['chuche_count'] = 0;
- $res['data_all'][0]['lichengshu'] = 0;
- $res['data_all'][0]['sitie'] = 0;
- $res['data_all'][0]['tingche'] = 0;
- $res['data_all'][0]['canyin'] = 0;
- $res['data_all'][0]['zhusu'] = 0;
- foreach ($data as $k => $v) {
- $res['data_list'][$k][] = $v['driver_name'];
- $res['data_list'][$k][] = $v['chuche_count'];
- $res['data_list'][$k][] = $v['lichengshu'];
- $res['data_list'][$k][] = $v['sitie'];
- $res['data_list'][$k][] = $v['tingche'];
- $res['data_list'][$k][] = $v['canyin'];
- $res['data_list'][$k][] = $v['zhusu'];
- $res['data_all'][0]['chuche_count'] += $v['chuche_count'];
- $res['data_all'][0]['lichengshu'] += $v['lichengshu'];
- $res['data_all'][0]['sitie'] += $v['sitie'];
- $res['data_all'][0]['tingche'] += $v['tingche'];
- $res['data_all'][0]['canyin'] += $v['canyin'];
- $res['data_all'][0]['zhusu'] += $v['zhusu'];
- }
- return $res;
- }
-
- private function actionExportDetail($data)
- {
- $res['file_name'] = '出车明细';
- $res['data_title'] = array('出车日期', '收车日期', '出车单号', '车辆', '司机', '线路', '用车单位', '收入', '运营成本', '毛利', '公里数', '开始公里数', '结束公里数', '预估油电费', '路桥费', '司贴', '停车费', '餐饮费', '住宿费', '报账状态');
- $res['data_all'][0]['heji'] = '总计';
- $res['data_all'][0]['heji1'] = '-';
- $res['data_all'][0]['heji2'] = '-';
- $res['data_all'][0]['heji3'] = '-';
- $res['data_all'][0]['heji4'] = '-';
- $res['data_all'][0]['heji5'] = '-';
- $res['data_all'][0]['heji6'] = '-';
- $res['data_all'][0]['income'] = 0;
- $res['data_all'][0]['all_cost'] = 0;
- $res['data_all'][0]['maoli'] = 0;
- $res['data_all'][0]['lichengshu'] = 0;
- $res['data_all'][0]['start_lichengshu'] = 0;
- $res['data_all'][0]['end_lichengshu'] = 0;
- $res['data_all'][0]['youdian'] = 0;
- $res['data_all'][0]['luqiao'] = 0;
- $res['data_all'][0]['sitie'] = 0;
- $res['data_all'][0]['tingche'] = 0;
- $res['data_all'][0]['canyin'] = 0;
- $res['data_all'][0]['zhusu'] = 0;
- $res['data_all'][0]['heji8'] = '-';
- foreach ($data as $k => $v) {
- $res['data_list'][$k][] = $v['run_date'];
- $res['data_list'][$k][] = $v['finish_date'];
- $res['data_list'][$k][] = $v['bus_number'];
- $res['data_list'][$k][] = $v['bus_name'];
- $res['data_list'][$k][] = $v['driver_name'];
- $res['data_list'][$k][] = $v['itinerary_name'];
- $res['data_list'][$k][] = $v['use_bus_org_name'];
- $res['data_list'][$k][] = $v['income'];
- $res['data_list'][$k][] = $v['all_cost'];
- $res['data_list'][$k][] = $v['maoli'];
- $res['data_list'][$k][] = $v['lichengshu'];
- $res['data_list'][$k][] = $v['start_lichengshu'];
- $res['data_list'][$k][] = $v['end_lichengshu'];
- $res['data_list'][$k][] = $v['youdian'];
- $res['data_list'][$k][] = $v['luqiao'];
- $res['data_list'][$k][] = $v['sitie'];
- $res['data_list'][$k][] = $v['tingche'];
- $res['data_list'][$k][] = $v['canyin'];
- $res['data_list'][$k][] = $v['zhusu'];
- $res['data_list'][$k][] = $v['finance_status'];
- $res['data_all'][0]['income'] += $v['income'];
- $res['data_all'][0]['all_cost'] += $v['all_cost'];
- $res['data_all'][0]['maoli'] += $v['maoli'];
- $res['data_all'][0]['lichengshu'] += $v['lichengshu'];
- $res['data_all'][0]['start_lichengshu'] += $v['start_lichengshu'];
- $res['data_all'][0]['end_lichengshu'] += $v['end_lichengshu'];
- $res['data_all'][0]['youdian'] += $v['youdian'];
- $res['data_all'][0]['luqiao'] += $v['luqiao'];
- $res['data_all'][0]['sitie'] += $v['sitie'];
- $res['data_all'][0]['tingche'] += $v['tingche'];
- $res['data_all'][0]['canyin'] += $v['canyin'];
- $res['data_all'][0]['zhusu'] += $v['zhusu'];
- }
- return $res;
- }
-
- private function actionExportHybrid($op, $data)
- {
- $res['file_name'] = '油电费用统计';
- if ($op == 'hybrid_you') {
- $res['data_title'] = array('车辆', '车龄', '品牌', '座位', '加油量(升)', '加油金额', '总里程数', '出车里程数', '平均油耗(升/百公里)');
- } else {
- $res['data_title'] = array('车辆', '车龄', '品牌', '座位', '充电量(度)', '充电金额', '总里程数', '出车里程数', '平均电耗(度/百公里)');
- }
- $res['data_all'][0]['heji'] = '总计';
- $res['data_all'][0]['heji1'] = '-';
- $res['data_all'][0]['heji2'] = '-';
- $res['data_all'][0]['heji3'] = '-';
- $res['data_all'][0]['receive_value'] = 0;
- $res['data_all'][0]['expense_price'] = 0;
- $res['data_all'][0]['all_lichengshu'] = 0;
- $res['data_all'][0]['lichengshu'] = 0;
- $res['data_all'][0]['ave'] = 0;
- foreach ($data as $k => $v) {
- $res['data_list'][$k][] = $v['bus_no'];
- $res['data_list'][$k][] = $v['buy_date'];
- $res['data_list'][$k][] = $v['brand_name'];
- $res['data_list'][$k][] = $v['seat_desc'];
- $res['data_list'][$k][] = $v['receive_value'];
- $res['data_list'][$k][] = $v['expense_price'];
- $res['data_list'][$k][] = $v['all_lichengshu'];
- $res['data_list'][$k][] = $v['lichengshu'];
- if ($v['all_lichengshu'] != 0) {
- $res['data_list'][$k][] = $v['receive_value'] / $v['all_lichengshu'] * 100;
- } else {
- $res['data_list'][$k][] = 0;
- }
- $res['data_all'][0]['receive_value'] += $v['receive_value'];
- $res['data_all'][0]['expense_price'] += $v['expense_price'];
- $res['data_all'][0]['all_lichengshu'] += $v['all_lichengshu'];
- $res['data_all'][0]['lichengshu'] += $v['lichengshu'];
- }
- if ($res['data_all'][0]['all_lichengshu'] != 0) {
- $res['data_all'][0]['ave'] += $res['data_all'][0]['receive_value'] / $res['data_all'][0]['all_lichengshu'] * 100;
- } else {
- $res['data_all'][0]['ave'] = 0;
- }
-
- return $res;
- }
-
- /**
- * User:Steven
- *
- * 导出Excel文件
- * @param $obj Report report对象
- * @param $file_name string 导出文件名称后缀
- * @param $data_title array 数据表头,title
- * @param $data_all array 合计数据列表
- * @param $data_list array 数据列表
- */
- public function exportExcel($obj, $file_name, $data_title, $data_all, $data_list)
- {
- require Yii::getAlias('@backend') . "/common/PHPExcel/PHPExcel.php";
- $objPHPExcel = new \PHPExcel(); //实例化PHPExcel类
- $objSheet = $objPHPExcel->getActiveSheet(); //获取当前活动sheet的操作对象
- $sheet_title = $obj['start_date'] . '~' . $obj['end_date'] . ($obj['date_type'] == 1 ? '(出车日期)' : '收车日期');
- $objSheet->setTitle($file_name); //给当前活动sheet的操作对象
- $directory = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
- $cell_num = count($data_title) - 1;
- $cell = $directory[$cell_num];
- $objSheet->mergeCells("A1:{$cell}1"); //合并单元格
- $objSheet->setCellValue('A1', $sheet_title . $file_name);
- $objSheet->getStyle("A1:{$cell}1")->getFont()->setName('微软雅黑')->setSize(15)->setBold(True);
- $objSheet->getStyle("A1:{$cell}1")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);;
- $objSheet->getStyle("A1:{$cell}1")->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('A6A6A6');
- $objSheet->getStyle("A2:{$cell}2")->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('CCCCCC');
-
-
- foreach ($data_title as $k => $v) {
- $objSheet->setCellValue($directory[$k] . '2', $v);
- }
- // $objSheet->setCellValue('A3', '-');
- // $objSheet->setCellValue('B3', '合计');
- for ($i = 0; $i < count($data_all); $i++) {
- $list = array_values($data_all[$i]);
- foreach ($list as $k => $v) {
- $objSheet->setCellValue($directory[$k] . '3', $v);
- }
- }
- for ($j = 0; $j < count($data_list); $j++) {
- $list = array_values($data_list[$j]);
- foreach ($list as $k => $v) {
- $objSheet->setCellValue($directory[$k] . ($j + 4), $v);
- }
- }
- $objWrite = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //按照指定格式生成excel文件
- $this->browserExport('05', $sheet_title . $file_name . '.xls');
- $objWrite->save('php://output');
- }
-
- /**
- * 导出到浏览器
- * @param $type
- * @param $fileName
- */
- private function browserExport($type, $fileName)
- {
- if ($type == '05') { //输出xls文件
- header('Content-Type: application/vnd.ms-excel;');
- } else { //输出xlsx文件
- header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
- }
- header('Content-Disposition: attachment;filename="' . $fileName . '"');
- header('Cache-Control: max-age=0');//禁止缓存
- }
-
- /**
- * 新统计报表导出
- */
- public function actionExcel()
- {
- $searchModel = new searchBusIncome();
- $search = Yii::$app->request->queryParams;
- $dataProvider = $searchModel->search($search);
- /* $query ActiveQuery */
- // $query = $dataProvider->query;
- $dataProvider->pagination = false;
- $models = $dataProvider->getModels();
-
- $objPHPExcel = new zPhpExcel(); //实例化PHPExcel类
- $objSheet = $objPHPExcel->getActiveSheet(); //获取当前活动sheet的操作对象
- $objPHPExcel->file_name = '报表' . ($searchModel->date_type == 1 ? '出车日期' : '收车日期') . ' ' . ($searchModel->date_from . '-' . $searchModel->date_to);
- $objSheet->setTitle('报表');
- if ($searchModel->search_type == 0) {
- $order_sort = ['department.depart_name', 'department.settleType.TYPE_NAME', 'department.settleCycle.TYPE_NAME',
- 'chuche_count', 'income', 'all_cost', 'maoli', 'lichengshu', 'youdian', 'luqiao', 'sitie', 'tingche', 'canyin', 'zhusu'];
- $title = array('用车单位', '结算方式', '结算周期', '出车数', '收入', '运营成本', '毛利', '公里数', '预估油电费', '路桥费', '司贴', '停车费', '餐饮费', '住宿费');
- } else {
- $order_sort = ['bus.BUS_NO', 'bus.SEAT_DESC', 'bus.brand.RES_NAME', 'chuche_count', 'income', 'all_cost', 'maoli',
- 'lichengshu', 'youdian', 'luqiao', 'sitie', 'tingche', 'canyin', 'zhusu'];
- $title = array('车牌号', '车座数', '品牌', '出车数', '收入', '运营成本', '毛利', '公里数', '预估油电费', '路桥费', '司贴', '停车费', '餐饮费', '住宿费');
- }
-
- //表头
- $a_k = range('B', 'O');
- foreach ($a_k as $key => $item) {
- $objSheet->setCellValue($item . '1', $title[$key]);
- }
-
- //数据
- ini_set("memory_limit", "-1");
- $count = 0;
- foreach ($models as $key => $datum) {
- $objSheet->setCellValue('A' . ($key + 2), $key + 1);
- foreach ($a_k as $index => $item) {
- $objSheet->setCellValue($item . ($key + 2), ArrayHelper::getValue($datum, $order_sort[$index]));
- }
- $count = $key + 2;
- }
- //合计
- $objSheet->setCellValue('B' . ($count + 1), '合计');
- foreach (range('E', 'O') as $item) {
- $objSheet->setCellValue($item . ($count + 1), "=sum({$item}2:{$item}" . $count . ')');
- }
-
- $count += 1;
- $objSheet->getStyle("B{$count}:O{$count}")->getFont()->setBold(true);
- //设置宽度
- $objPHPExcel->setColumnSize(range('B', 'O'), [25, 12, 12, 12, 12, 10, 10, 10, 15, 10, 10, 10, 10]);
- $objPHPExcel->output();
- }
- }
-
- ?>
|