137 lines
3.7 KiB
PHP
137 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace backend\models;
|
|
|
|
use Yii;
|
|
use yii\base\Model;
|
|
use common\models\Card;
|
|
use common\models\Customer;
|
|
use common\models\Ticket;
|
|
use common\models\Account;
|
|
use yii\web\UploadedFile;
|
|
use common\models\TicketInstallmentRequest;
|
|
use common\models\Ugiro;
|
|
use common\components\giro\GiroBeszed;
|
|
use yii\helpers\FileHelper;
|
|
use yii\helpers\Inflector;
|
|
use yii\helpers\BaseInflector;
|
|
use common\models\UgiroRequestAssignment;
|
|
|
|
/**
|
|
* ContactForm is the model behind the contact form.
|
|
* @property \Yii\web\UploadedFile $file
|
|
*/
|
|
class GiroKotegForm extends Model{
|
|
|
|
public $action;
|
|
|
|
public $requests;
|
|
public $content;
|
|
public $koteg;
|
|
public $success;
|
|
|
|
public function rules(){
|
|
return [
|
|
[['action'], 'safe']
|
|
|
|
];
|
|
}
|
|
|
|
|
|
public function createKoteg(){
|
|
$this->readRequests();
|
|
$this->success = true;
|
|
if ( count( $this->requests ) > 0 ){
|
|
$connection = \Yii::$app->db;
|
|
$transaction = $connection->beginTransaction();
|
|
|
|
try {
|
|
$this->createUGiroKoteg();
|
|
$this->assignRequestsToUgiro();
|
|
$this->changeRequestsStatusToSent();
|
|
$this->generateFileContent();
|
|
$this->saveFile();
|
|
|
|
if ($this->success) {
|
|
$transaction->commit();
|
|
\Yii::$app->session->setFlash('success',"Fájl létrehozva");
|
|
return true;
|
|
} else {
|
|
$transaction->rollback();
|
|
throw new NotFoundHttpException( "Hiba történt!");
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
$transaction->rollback();
|
|
throw $e;
|
|
}
|
|
}else{
|
|
\Yii::$app->session->setFlash('danger', "Megbízások száma 0!");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function changeRequestsStatusToSent(){
|
|
foreach ($this->requests as $request){
|
|
$request->status = TicketInstallmentRequest::$STATUS_SENT;
|
|
$this->success &= $request->save(false);
|
|
}
|
|
}
|
|
|
|
public function assignRequestsToUgiro(){
|
|
foreach ($this->requests as $request){
|
|
$assignment = new UgiroRequestAssignment();
|
|
$assignment->id_request = $request->id_ticket_installment_request;
|
|
$assignment->id_ugiro = $this->koteg->id_ugiro;
|
|
$this->success &= $assignment->save(false);
|
|
}
|
|
}
|
|
|
|
public function createUGiroKoteg(){
|
|
$this->koteg = new Ugiro();
|
|
$this->koteg->status = Ugiro::$STATUS_SENT;
|
|
$this->koteg->id_user = \Yii::$app->user->id;
|
|
$this->success &= $this->koteg->save(false);
|
|
}
|
|
public function readRequests(){
|
|
$this->requests = TicketInstallmentRequest::find()->andWhere(['status' => TicketInstallmentRequest::$STATUS_MARKED_TO_SEND])->all();
|
|
}
|
|
|
|
public function generateFileContent(){
|
|
$this->content = GiroBeszed::createFileContent($this->koteg->id_ugiro, $this->requests);
|
|
}
|
|
|
|
public function saveFile( ) {
|
|
// $data = static::transliterate($this->content);
|
|
$data = $this->content;
|
|
$data = iconv("utf-8","ASCII",$data);
|
|
$path = Ugiro::$PATH_MEGBIZAS . DIRECTORY_SEPARATOR ."giro" . $this->koteg->id_ugiro."_". date('Ymd' ) .".txt";
|
|
$filename = Yii::getAlias('@backend/web').DIRECTORY_SEPARATOR .$path;
|
|
$dir = Yii::getAlias('@backend/web').DIRECTORY_SEPARATOR .Ugiro::$PATH_MEGBIZAS;
|
|
$this->koteg->path = $path;
|
|
$this->koteg->save(false);
|
|
if(!FileHelper::createDirectory($dir)){
|
|
throw new HttpException(500, 'Cannot create "'.$dir.'". Please check write permissions.');
|
|
}
|
|
$myfile = fopen($filename,'a');
|
|
fwrite($myfile, $data);
|
|
fclose($myfile);
|
|
}
|
|
|
|
public static function transliterate($string)
|
|
{
|
|
// if (static::hasIntl()) {
|
|
// return transliterator_transliterate(BaseInflector::$transliterator, $string);
|
|
// } else {
|
|
return str_replace(array_keys(BaseInflector::$transliteration), BaseInflector::$transliteration, $string);
|
|
// }
|
|
}
|
|
|
|
/**
|
|
* @return boolean if intl extension is loaded
|
|
*/
|
|
protected static function hasIntl()
|
|
{
|
|
return extension_loaded('intl');
|
|
}
|
|
} |