Finish version/v.0.0.82

This commit is contained in:
Roland Schneider 2016-07-30 20:11:18 +02:00
commit ad3ab64d8e
3 changed files with 318 additions and 292 deletions

View File

@ -21,203 +21,213 @@ use common\components\Upload;
/** /**
* CardPackageController implements the CRUD actions for CardPackage model. * CardPackageController implements the CRUD actions for CardPackage model.
*/ */
class CardPackageController extends \backend\controllers\BackendController { class CardPackageController extends \backend\controllers\BackendController
{
public function behaviors() public function behaviors()
{ {
return [ return [
'access' => [ 'access' => [
'class' => \yii\filters\AccessControl::className(), 'class' => \yii\filters\AccessControl::className(),
'rules' => [ 'rules' => [
// allow authenticated users // allow authenticated users
[ [
'actions' => ['create','index','view','import','download'], 'actions' => ['create', 'index', 'view', 'import', 'download'],
'allow' => true, 'allow' => true,
'roles' => ['admin','employee','reception'], 'roles' => ['admin', 'employee', 'reception'],
], ],
// everything else is denied // everything else is denied
], ],
], ],
]; ];
} }
/**
* Lists all CardPackage models.
*
* @return mixed
*/
public function actionIndex() {
$searchModel = new CardPackageSearch ();
$dataProvider = $searchModel->search ( Yii::$app->request->queryParams );
return $this->render ( 'index', [ /**
'searchModel' => $searchModel, * Lists all CardPackage models.
'dataProvider' => $dataProvider *
] ); * @return mixed
} */
public function actionIndex()
{
$searchModel = new CardPackageSearch ();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
/** return $this->render('index', [
* Displays a single CardPackage model. 'searchModel' => $searchModel,
* 'dataProvider' => $dataProvider
* @param integer $id ]);
* @return mixed }
*/
public function actionView($id) {
$model = $this->findModel ( $id );
$query = Card::find (); /**
$query->innerJoin ( "card_card_package_assignment", "card_card_package_assignment.id_card = card.id_card " ); * Displays a single CardPackage model.
$query->andWhere ( [ *
'card_card_package_assignment.id_card_package' => $id * @param integer $id
] ); * @return mixed
*/
public function actionView($id)
{
$model = $this->findModel($id);
$dataProvider = new ActiveDataProvider ( [ $query = Card::find();
'query' => $query $query->innerJoin("card_card_package_assignment", "card_card_package_assignment.id_card = card.id_card ");
] ); $query->andWhere([
'card_card_package_assignment.id_card_package' => $id
]);
return $this->render ( 'view', [ $dataProvider = new ActiveDataProvider ([
'model' => $model, 'query' => $query
'dataProvider' => $dataProvider ]);
] );
}
public function actionDownload($id) {
$model = $this->findModel ( $id );
$model->updateCounters ( [ return $this->render('view', [
'printed' => 1 'model' => $model,
] ); 'dataProvider' => $dataProvider
]);
}
$query = Card::find (); public function actionDownload($id)
$query->innerJoin ( "card_card_package_assignment", "card_card_package_assignment.id_card = card.id_card " ); {
$query->andWhere ( [ $model = $this->findModel($id);
'card_card_package_assignment.id_card_package' => $id
] );
$cards = $query->all (); $model->updateCounters([
'printed' => 1
]);
$numbers = [ ]; $query = Card::find();
foreach ( $cards as $card ) { $query->innerJoin("card_card_package_assignment", "card_card_package_assignment.id_card = card.id_card ");
$numbers [] = $card->number; $query->andWhere([
} 'card_card_package_assignment.id_card_package' => $id
]);
$this->generateXLS ( $model, $numbers ); $cards = $query->all();
}
/** $numbers = [];
* Creates a new CardPackage model. foreach ($cards as $card) {
* If creation is successful, the browser will be redirected to the 'view' page. $numbers [] = $card->number;
* }
* @return mixed
*/
public function actionCreate() {
$model = new CardPackage ();
$model->id_user = \Yii::$app->user->id;
$model->printed = 0;
if ($model->load ( Yii::$app->request->post () ) && $model->validate ()) { $this->generateXLS($model, $numbers);
$conn = \Yii::$app->db; }
$tx = $conn->beginTransaction ();
$model->save ( false ); /**
* Creates a new CardPackage model.
* If creation is successful, the browser will be redirected to the 'view' page.
*
* @return mixed
*/
public function actionCreate()
{
$model = new CardPackage ();
$model->id_user = \Yii::$app->user->id;
$model->printed = 0;
$count = $model->count; if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$conn = \Yii::$app->db;
$tx = $conn->beginTransaction();
$numGen = new FreeUniqueCardNumberGenerator ( [ $model->save(false);
'count' => $model->count,
'prefix' => '10'
] );
$numGen->generate (); $count = $model->count;
$numbers = $numGen->cache; $numGen = new FreeUniqueCardNumberGenerator ([
'count' => $model->count,
'prefix' => '10'
]);
try { $numGen->generate();
foreach ( $numbers as $number ) {
$card = new Card (); $numbers = $numGen->cache;
$card->number = $number;
$card->type = Card::TYPE_RFID;
$card->status = Card::STATUS_ACTIVE;
$card->save ( false );
$cardAssignment = new CardCardPackageAssignment (); try {
$cardAssignment->id_card = $card->id_card; foreach ($numbers as $number) {
$cardAssignment->id_card_package = $model->id_card_package;
$cardAssignment->save ( false );
}
$tx->commit ();
return $this->redirect ( [ $card = new Card ();
'index' $card->number = $number;
] ); $card->type = Card::TYPE_RFID;
} catch ( \Exception $e ) { $card->status = Card::STATUS_ACTIVE;
$tx->rollBack (); $card->save(false);
}
return $this->render ( 'create', [ $cardAssignment = new CardCardPackageAssignment ();
'model' => $model $cardAssignment->id_card = $card->id_card;
] ); $cardAssignment->id_card_package = $model->id_card_package;
} else { $cardAssignment->save(false);
return $this->render ( 'create', [ }
'model' => $model $tx->commit();
] );
}
}
protected function generateXLS($model, $numbers) {
$objPHPExcel = new \PHPExcel ();
$sheet = $objPHPExcel->setActiveSheetIndex ( 0 ); return $this->redirect([
'index'
]);
} catch (\Exception $e) {
$tx->rollBack();
}
// $row = 1; return $this->render('create', [
// $sheet->setCellValue('A'.$row, "Termék név") 'model' => $model
// ->setCellValue('B'.$row, "Eladási ár") ]);
// ->setCellValue('C'.$row, "Kassza") } else {
// ->setCellValue('D'.$row, "Eladott mennyiség") return $this->render('create', [
// ->setCellValue('E'.$row, "Eladás összege"); 'model' => $model
$row = 0; ]);
}
}
foreach ( $numbers as $number ) { protected function generateXLS($model, $numbers)
$row ++; {
$sheet->setCellValue ( 'A' . $row, $number ); $objPHPExcel = new \PHPExcel ();
}
$fileName = "kartya_csomag"; $sheet = $objPHPExcel->setActiveSheetIndex(0);
$fileName .= "_" . $model->id_card_package;
$fileName .= "_" . date ( "Ymd_His" );
$fileName .= ".xls";
// Redirect output to a clients web browser (Excel5) // $row = 1;
header ( 'Content-Type: application/vnd.ms-excel' ); // $sheet->setCellValue('A'.$row, "Termék név")
header ( 'Content-Disposition: attachment;filename="' . $fileName . '"' ); // ->setCellValue('B'.$row, "Eladási ár")
header ( 'Cache-Control: max-age=0' ); // ->setCellValue('C'.$row, "Kassza")
// If you're serving to IE 9, then the following may be needed // ->setCellValue('D'.$row, "Eladott mennyiség")
header ( 'Cache-Control: max-age=1' ); // ->setCellValue('E'.$row, "Eladás összege");
// If you're serving to IE over SSL, then the following may be needed $row = 0;
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 ();
}
public function actionImport() {
$model = new CardPackageImportForm ();
if (Yii::$app->request->isPost) { foreach ($numbers as $number) {
$model->file = UploadedFile::getInstance ( $model, 'file' ); $row++;
$sheet->setCellValue('A' . $row, $number);
}
$fileName = "kartya_csomag";
$fileName .= "_" . $model->id_card_package;
$fileName .= "_" . date("Ymd_His");
$fileName .= ".xls";
// Redirect output to a clients web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $fileName . '"');
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 ();
}
public function actionImport()
{
$model = new CardPackageImportForm ();
if (Yii::$app->request->isPost) {
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->validate()) {
if ($model->validate ()) {
// print_r($model->file); // print_r($model->file);
// $model->message = "ok"; // $model->message = "ok";
$file = $model->file->tempName; $file = $model->file->tempName;
$xlsUtil = new XLSUtil (); $xlsUtil = new XLSUtil ();
$xlsUtil->loadFromFileName ( $file ); $xlsUtil->loadFromFileName($file);
$array = $xlsUtil->toArray (); $array = $xlsUtil->toArray();
// print_r($array); // print_r($array);
// print_r( array_column($array, 2)); // print_r( array_column($array, 2));
@ -226,96 +236,110 @@ class CardPackageController extends \backend\controllers\BackendController {
// echo $item[2]; // echo $item[2];
// } // }
foreach ( $array as $row ) { foreach ($array as $row) {
try { try {
$tx = \Yii::$app->db->beginTransaction (); $tx = \Yii::$app->db->beginTransaction();
$card = Card::find ()->andWhere ( [ $rfidIsValid = false;
'number' => $row [0] $rfid = $row[2];
] )->one ();
if ( isset( $card )) { if (isset($rfid) && !empty($rfid) && strlen($rfid) > 8) {
$card->rfid_key = Helper::fixAsciiChars( $row [2 ] ); $rfidIsValid = true;
$card->save(false); }
}else{
throw new \Exception("Card not found");
}
if ( !$rfidIsValid ){
throw new \Exception("Rfid is invalid");
}
$tx->commit (); $card = Card::find()->andWhere([
'number' => $row [0]
])->one();
$model->done = $model->done + 1; if (isset($card)) {
} catch ( \Exception $e ) { $card->rfid_key = Helper::fixAsciiChars($row [2]);
$tx->rollBack (); $card->save(false);
$model->failed = $model->failed + 1; } else {
\Yii::error ( "Failed to import card rfid: " . print_r ( $row, true ) ); throw new \Exception("Card not found");
} }
}
\Yii::$app->session->setFlash ( 'success', "Az importálás eredménye: sikeres:" . $model->done . "; Sikertelen: " . $model->failed ); $tx->commit();
\Yii::info('Importálás: sikeres: ' .$model->done . " , sikertelen: " . $model->failed); $model->done = $model->done + 1;
return $this->redirect ( [
'card-package/import'
] );
}
}
return $this->render ( "import", [ } catch (\Exception $e) {
'model' => $model $tx->rollBack();
] ); $model->failed = $model->failed + 1;
} \Yii::error("Failed to import card rfid: " . print_r($row, true));
}
}
/** \Yii::$app->session->setFlash('success', "Az importálás eredménye: sikeres:" . $model->done . "; Sikertelen: " . $model->failed);
* Updates an existing CardPackage 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 );
if ($model->load ( Yii::$app->request->post () ) && $model->save ()) { \Yii::info('Importálás: sikeres: ' . $model->done . " , sikertelen: " . $model->failed);
return $this->redirect ( [ return $this->redirect([
'view', 'card-package/import'
'id' => $model->id_card_package ]);
] ); }
} else { }
return $this->render ( 'update', [
'model' => $model
] );
}
}
/** return $this->render("import", [
* Deletes an existing CardPackage model. 'model' => $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' * Updates an existing CardPackage 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);
/** if ($model->load(Yii::$app->request->post()) && $model->save()) {
* Finds the CardPackage model based on its primary key value. return $this->redirect([
* If the model is not found, a 404 HTTP exception will be thrown. 'view',
* 'id' => $model->id_card_package
* @param integer $id ]);
* @return CardPackage the loaded model } else {
* @throws NotFoundHttpException if the model cannot be found return $this->render('update', [
*/ 'model' => $model
protected function findModel($id) { ]);
if (($model = CardPackage::findOne ( $id )) !== null) { }
return $model; }
} else {
throw new NotFoundHttpException ( 'The requested page does not exist.' ); /**
} * Deletes an existing CardPackage 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'
]);
}
/**
* Finds the CardPackage model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*
* @param integer $id
* @return CardPackage the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = CardPackage::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException ('The requested page does not exist.');
}
}
} }

View File

@ -1,3 +1,5 @@
-0.0.82
- add rfid check to card package import
-0.0.81 -0.0.81
- add comment to transaction/index - add comment to transaction/index
-0.0.80 -0.0.80

View File

@ -5,7 +5,7 @@ return [
'supportEmail' => 'rocho02@gmail.com', 'supportEmail' => 'rocho02@gmail.com',
'infoEmail' => 'info@rocho-net.hu', 'infoEmail' => 'info@rocho-net.hu',
'user.passwordResetTokenExpire' => 3600, 'user.passwordResetTokenExpire' => 3600,
'version' => 'v0.0.81', 'version' => 'v0.0.82',
'company' => 'movar',//gyor 'company' => 'movar',//gyor
'company_name' => "Freimann Kft.", 'company_name' => "Freimann Kft.",
'product_visiblity' => 'account',// on reception which products to display. account or global 'product_visiblity' => 'account',// on reception which products to display. account or global