fitness-web/backend/controllers/ProductController.php

200 lines
5.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace backend\controllers;
use Yii;
use common\models\Product;
use backend\models\ProductSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\Account;
use common\models\ProductCategory;
use backend\models\ProductStatisticsSearch;
use PHPExcel;
/**
* ProductController implements the CRUD actions for Product model.
*/
class ProductController extends \backend\controllers\BackendController
{
public function behaviors()
{
return [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
// allow authenticated users
[
'actions' => ['create','index','view','update','statistics'],
'allow' => true,
'roles' => ['admin','employee','reception'],
],
// everything else is denied
],
],
];
}
/**
* Lists all Product models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new ProductSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Product model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Product model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Product();
$model->stock = 0;
$model->status = Product::STATUS_ACTIVE;
$accounts = Account::read(null);
$categories = ProductCategory::read(null);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_product]);
} else {
return $this->render('create', [
'model' => $model,
'accounts' => $accounts,
'categories' => $categories ,
]);
}
}
/**
* Updates an existing Product model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$accounts = Account::readAccounts($model->id_account);
$categories = ProductCategory::read($model->id_product_category);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_product]);
} else {
return $this->render('update', [
'model' => $model,
'accounts' => $accounts,
'categories' => $categories ,
]);
}
}
/**
* Deletes an existing Product model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
public function actionStatistics(){
$searchModel = new ProductStatisticsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
if ( $searchModel->output == 'xls' ){
$models = $dataProvider->getModels();
$objPHPExcel = new \PHPExcel();
$sheet = $objPHPExcel->setActiveSheetIndex(0);
$row = 1;
$sheet->setCellValue('A'.$row, "Termék név")
->setCellValue('B'.$row, "Eladási ár")
->setCellValue('C'.$row, "Kassza")
->setCellValue('D'.$row, "Eladott mennyiség")
->setCellValue('E'.$row, "Eladás összege");
foreach ($models as $model ){
$row++;
$sheet->setCellValue('A'.$row, $model['product_name'])
->setCellValue('B'.$row, $model['product_sale_price'])
->setCellValue('C'.$row, $model['father_account_name'])
->setCellValue('D'.$row, $model['transfer_count'])
->setCellValue('E'.$row, $model['transfer_money']);
}
// Redirect output to a clients web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="termek_statisztika.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
return $this->render('statistics', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Finds the Product model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Product the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Product::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}