add editable account on reception/customer-cart, add towel handling

This commit is contained in:
2016-10-21 20:39:02 +02:00
parent 8585286150
commit 90c598f8c6
26 changed files with 649 additions and 37 deletions

View File

@@ -20,7 +20,8 @@ class CustomerCartForm extends Model
public $money = 0;
public $selected = [];
public $customer;
public $hiddenAccounts;
public $id_account;
/**
* @inheritdoc
*/
@@ -28,7 +29,7 @@ class CustomerCartForm extends Model
{
return [
['selected', 'each', 'rule' => ['integer']],
[['money' ,'payment_method'],'integer'],
[['money' ,'payment_method' ,'id_account' ],'integer'],
[['payment_method'],'validatePaymentMethod'],
];
}
@@ -44,15 +45,45 @@ class CustomerCartForm extends Model
public function validatePaymentMethod( $attribute, $params ){
if ( !empty($this->payment_method)){
// echo $this->payment_method;
$arr = Transfer::paymentMethods();
if ( !array_key_exists($this->payment_method, $arr) ){
$this->addError($attribute, "Érvénytelen fizetési mód");
}
}
}
public function validateIdAccount( $attribute, $params ){
if ( !empty($this->id_account)){
$account = $this->loadAvailableOverrideAccounts($this->id_account);
if ( !isset($account) ){
$this->addError($attribute, "Érvénytelen kassza");
}
}
}
private function loadAvailableOverrideAccounts($id_account = null){
$accounts = null;
$query = Account::find();
$query->innerJoinWith('userAccountAssignments');
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id]);
$query->andWhere(['status' => Account::STATUS_ACTIVE])->all();
if ( isset($id_account)){
$query->andWhere(['account.id_account' => $id_account]);
}
$query->orderBy( ['name' => SORT_ASC]);
$accounts = $query->all();
return $accounts;
}
public function payout(){
$valid = $this->validate();
@@ -71,6 +102,7 @@ class CustomerCartForm extends Model
'idAccount' => Account::readDefault (),
'cartType' => 'customer',
'overridePaymentMethod' => $this->payment_method,
'overrideIdAccount' => $this->id_account,
'idCustomer' => $this->customer->id_customer
] );
@@ -82,10 +114,10 @@ class CustomerCartForm extends Model
} catch ( Exception $e ) {
$transaction->rollback ();
Yii::error ( "faled to save :" . $e->getMessage () );
Yii::error ( "failed to save :" . $e->getMessage () );
} catch ( \Exception $e ) {
$transaction->rollback ();
Yii::error ( "faled to save :" . $e->getMessage () );
Yii::error ( "failed to save :" . $e->getMessage () );
}
return false;
@@ -100,7 +132,13 @@ class CustomerCartForm extends Model
$item->payment_method = $this->payment_method;
}
}
public function changeAccount($item){
if ( !empty($this->id_account)){
$item->id_account = $this->id_account;
}
}
public function run(){
$this->readTransfers();
}

View File

@@ -0,0 +1,130 @@
<?php
namespace frontend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Log;
use yii\db\Expression;
use yii\db\Query;
use common\components\Helper;
/**
* LogSearch represents the model behind the search form about `common\models\Log`.
*/
class LogSearch extends Log
{
public $start;
public $end;
public $timestampStart;
public $timestampEnd;
public $card;
public $customer;
/**
* @inheritdoc
*/
public function rules()
{
return [
[[ 'start', ], 'date', 'format' =>Yii::$app->formatter->datetimeFormat , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ],
[[ 'end' , ], 'date' ,'format' =>Yii::$app->formatter->datetimeFormat , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = new Query();
$query->select([
'log.id_log as log_id_log',
'log.created_at as log_created_at',
'log.message as log_message',
'user.username as user_username',
'customer.name as customer_name',
new Expression("case when log.type = " .Log::$TYPE_TOWEL_IN ." then 'Visszaad' ".
" when log.type = ". Log::$TYPE_TOWEL_OUT . " then 'Bérel' ".
" else '-' end as log_type")
]);
$query->from("log");
$query->leftJoin("user"," user.id = log.id_user");
$query->leftJoin("customer"," customer.id_customer = log.id_customer");
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' =>[
'defaultOrder' => [ 'log_created_at' => SORT_DESC],
'attributes' => Helper::mkYiiSortItems([
['log_id_log'],
['log_created_at'],
['log_message'],
['log_app'],
['log_app'],
['user_username'],
['customer_name'],
['log_type'],
])
]
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere(['>=', 'log.created_at', $this->timestampStart]);
$query->andFilterWhere(['<', 'log.created_at', $this->timestampEnd]);
$query->andFilterWhere([
'id_log' => $this->id_log,
'type' => $this->type,
'id_user' => $this->id_user,
'id_transfer' => $this->id_transfer,
'id_money_movement' => $this->id_money_movement,
'id_ticket' => $this->id_ticket,
'id_sale' => $this->id_sale,
'id_customer' => $this->id_customer,
'id_account' => $this->id_account,
'id_account_state' => $this->id_account_state,
'id_key' => $this->id_key,
'id_product' => $this->id_product,
'id_door_log' => $this->id_door_log,
'created_at' => $this->created_at,
]);
$query->andFilterWhere(['like', 'message', $this->message])
->andFilterWhere(['like', 'url', $this->url])
->andFilterWhere(['like', 'app', $this->app]);
$query->andWhere(['in', 'log.type',[Log::$TYPE_TOWEL_IN,Log::$TYPE_TOWEL_OUT]]);
$query->andWhere([ '=', 'customer.id_customer' , $this->customer->id_customer]);
return $dataProvider;
}
}

View File

@@ -0,0 +1,116 @@
<?php
namespace frontend\models;
use common\models\DoorLog;
use common\models\Log;
use Yii;
use yii\base\Model;
use common\models\CardKeyAssignment;
use common\models\Key;
use yii\helpers\ArrayHelper;
use common\components\Helper;
use common\models\Card;
/**
* ContactForm is the model behind the contact form.
*
* @property \common\models\Card $card
* @property \common\models\Key $key
*
*/
class TowelForm extends Model
{
public $count;
public $number;
public $direction;
public $card;
public $customer;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['count'], 'integer', 'min' => 1, 'max' => 10 ],
[['number','direction'], 'safe' ],
[['count', 'number'], 'required'],
[['number'],'validateCard' ],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
];
}
public function validateCard($a,$params){
$this->number = Helper::fixAsciiChars( $this->number );
$query = Card::find();
$query->leftJoin("card_key_assignment", 'card.id_card = card_key_assignment.id_card');
$query->leftJoin("key", 'key.id_key = card_key_assignment.id_key');
$query->andWhere(['or',
['and',[ 'in','card.number' , [$this->number]],"trim(coalesce(card.number, '')) <>'' " ],
['and', ['in','card.rfid_key' ,[ $this->number] ],"trim(coalesce(card.rfid_key, '')) <>'' "],
['and',[ 'in','key.number' , [$this->number]],"trim(coalesce(key.number, '')) <>'' " ],
['and', ['in','key.rfid_key' ,[ $this->number] ],"trim(coalesce(key.rfid_key, '')) <>'' "]
]);
$this->card = $query->one();
if ( $this->card != null ){
$this->customer = $this->card->customer;
}
if ( !isset($this->customer)){
$this->addError($a,"A megadott vendég nem található");
}
}
private function updateTowelCount(){
$count = $this->count;
if ( isset($this->direction) && $this->direction == 'in'){
$count = $this->customer->towel_count - $count;
$log_type = Log::$TYPE_TOWEL_IN;
}else{
$count = $this->customer->towel_count + $count;
$log_type = Log::$TYPE_TOWEL_OUT;
}
$count = max(0, $count );
$this->customer->towel_count = $count;
$result = $this->customer->save(false);
if ( $result ){
Log::log([
'type' => $log_type,
'message' => '' .$this->customer->towel_count,
'id_customer' => $this->customer->id_customer
]);
}
return $result;
}
public function save(){
if ( $this->validate()){
return $this->updateTowelCount();
}
return false;
}
}