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