add backed transfer list chages. Add conffir dkailogsW

This commit is contained in:
2016-07-13 18:37:41 +02:00
parent 3e3cd83945
commit a580b3d295
11 changed files with 514 additions and 168 deletions

View File

@@ -19,37 +19,43 @@ use common\components\RoleDefinition;
*/
class TransferSearch extends Transfer
{
public $searchObjectName;
public $searchTypeName;
public $searchUserName;
public $start;
public $end;
public $timestampStart;
public $timestampEnd;
public $accountTotals;
public $fullTotal;
public $types;
public $totals;
public $customer_name;
public $searchObjectName;
public $searchTypeName;
public $searchUserName;
public $start;
public $end;
public $timestampStart;
public $timestampEnd;
public $accountTotals;
public $fullTotal;
public $types;
public $totals;
public $customer_name;
public $transfer_name;
public $card_number;
public $all;
public $output;
/**
* @inheritdoc
*/
public function rules()
{
return [
[[ 'id_account','id_user', 'type','status','payment_method'], 'integer'],
[['customer_name'],'safe'],
[['id_account', 'id_user', 'type', 'status', 'payment_method'], 'integer'],
[['customer_name','output','transfer_name', 'card_number' ], 'safe'],
// [[ 'searchObjectName' ], 'string'],
// [[ 'start', ], 'date' , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
// [[ 'end' , ], 'date' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
[[ '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' ],
['types', 'each', 'rule' => ['integer']],
[['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'],
['types', 'each', 'rule' => ['integer']],
];
}
@@ -62,6 +68,134 @@ class TransferSearch extends Transfer
return Model::scenarios();
}
/**
* Create the payment method case when sql expression
* @return Expression the payment method expression
*/
private function buildPaymentMethodExpression(){
$s = "case";
$arr = Transfer::paymentMethods();
foreach ( $arr as $key =>$value ){
$s .= " when transfer.payment_method = $key then '$value' ";
}
$s .=" end as transfer_payment_method";
return new Expression($s);
}
private function buildTransferNameCondition($transferName){
$exp = new Expression( 'case
when transfer.type = ' . Transfer::TYPE_TICKET . ' then ticket_type.name
when transfer.type = ' . Transfer::TYPE_PRODUCT . ' then product.name
when transfer.type = ' . Transfer::TYPE_MONEY_MOVEMENT_OUT . ' then \'Pénzmozgás\' end = \''.$transferName.'\' ');
return $exp->__toString() ;
}
/**
* @param $mode string the select mode: select or total
* @param $valid boolean the form validation result
* @return \Yii\db\Query the query object
*/
private function createQuery($mode, $valid)
{
$query = new Query();
if ($mode == 'select') {
$query->select([
'transfer.id_transfer as transfer_id_transfer',
new Expression('case
when transfer.type = ' . Transfer::TYPE_TICKET . ' then \'Bérlet\'
when transfer.type = ' . Transfer::TYPE_PRODUCT . ' then \'Termék\'
when transfer.type = ' . Transfer::TYPE_MONEY_MOVEMENT_OUT . ' then \'Pénzmozgás\' end as transfer_type'),
new Expression('case
when transfer.type = ' . Transfer::TYPE_TICKET . ' then ticket_type.name
when transfer.type = ' . Transfer::TYPE_PRODUCT . ' then product.name
when transfer.type = ' . Transfer::TYPE_MONEY_MOVEMENT_OUT . ' then \'Pénzmozgás\' end as transfer_name'),
'user.username as user_username',
'paid_by.username as paid_by_username',
'customer.name as customer_name',
'account.name as account_name',
'transfer.item_price as transfer_item_price',
'transfer.count as transfer_count',
new Expression('case
when transfer.direction = ' . Transfer::DIRECTION_IN . ' then transfer.money
when transfer.direction = ' . Transfer::DIRECTION_OUT . ' then transfer.money * -1 end as transfer_money'),
new Expression('case
when transfer.status = ' . Transfer::STATUS_NOT_PAID . ' then \'Nincs fizetve\'
when transfer.status = ' . Transfer::STATUS_PAID . ' then \'Fizetve\'
when transfer.status = ' . Transfer::STATUS_STORNO . ' then \'Törölve\' end as transfer_status'),
$this->buildPaymentMethodExpression(),
'transfer.created_at as transfer_created_at',
'transfer.paid_at as transfer_paid_at',
'ticket.start as ticket_start',
'ticket.end as ticket_end'
]);
}else if ( $mode == 'total'){
$query->select([
new Expression('coalesce(sum(transfer.count),0) as total_count'),
new Expression('coalesce(sum(transfer.money),0) as total_money'),
]);
}
$query->from("transfer");
$query->innerJoin('account', 'account.id_account = transfer.id_account');
$query->innerJoin('user', " user.id = transfer.id_user");
$query->leftJoin('customer', " transfer.id_customer = customer.id_customer");
$query->leftJoin('card', " card.id_card = customer.id_customer_card");
$query->leftJoin('user as paid_by', " paid_by.id = transfer.paid_by");
$query->leftJoin('ticket', "ticket.id_ticket = transfer.id_object and transfer.type = " . Transfer::TYPE_TICKET);
$query->leftJoin('sale', "sale.id_sale = transfer.id_object and transfer.type = " . Transfer::TYPE_PRODUCT);
$query->leftJoin('money_movement', "money_movement.id_money_movement = transfer.id_object and transfer.type = " . Transfer::TYPE_MONEY_MOVEMENT_OUT);
$query->leftJoin('ticket_type', "ticket_type.id_ticket_type = ticket.id_ticket_type");
$query->leftJoin('product', 'product.id_product = sale.id_product');
if (!$valid) {
$query->where('0=1');
return $query;
}
if (!RoleDefinition::isAdmin()) {
$query->innerJoin("user_account_assignment", 'transfer.id_account = user_account_assignment.id_account');
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id]);
$query->andWhere(['account.type' => Account::TYPE_ALL]);
if (RoleDefinition::isReception()) {
$query->andWhere(['transfer.id_user' => Yii::$app->user->id]);
}
}
$query->andFilterWhere([
'transfer.id_account' => $this->id_account,
'transfer.status' => $this->status,
'transfer.payment_method' => $this->payment_method,
'customer.name' => $this->customer_name,
'card.number' => $this->card_number
]);
if ( !empty($this->transfer_name)) {
$query->andWhere($this->buildTransferNameCondition($this->transfer_name) );
}
$query->andFilterWhere(['in', 'transfer.type', $this->types]);
$created_condition = ['and', ['>=', 'transfer.created_at', $this->timestampStart], ['<', 'transfer.created_at', $this->timestampEnd]];
$paid_condition = ['and', ['>=', 'transfer.paid_at', $this->timestampStart], ['<', 'transfer.paid_at', $this->timestampEnd]];
$query->andFilterWhere(['or', $created_condition, $paid_condition]);
if (isset($this->id_user)) {
$query->andFilterWhere([
'or',
['transfer.id_user' => $this->id_user],
['transfer.paid_by' => $this->id_user],
]);
}
if (!RoleDefinition::isAdmin()) {
Helper::restrictIfNotAdminTheStartDate($query, $this->timestampStart, ['transfer.created_at', 'transfer.paid_at']);
}
return $query;
}
/**
* Creates data provider instance with search query applied
*
@@ -71,139 +205,75 @@ class TransferSearch extends Transfer
*/
public function search($params)
{
$query = Transfer::find();
$query->innerJoinWith('account');
$query->innerJoin('user', " user.id = transfer.id_user");
$query->leftJoin('customer', " transfer.id_customer = customer.id_customer");
if ( !RoleDefinition::isAdmin() ){
$query->innerJoin("user_account_assignment",'transfer.id_account = user_account_assignment.id_account' );
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id ]);
$query->andWhere(['account.type' => Account::TYPE_ALL ]);
if ( RoleDefinition::isReception()){
$query->andWhere(['transfer.id_user' => Yii::$app->user->id ]);
}
}
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' =>[
'defaultOrder' =>[
'created_at' => SORT_DESC
],
'attributes' =>[
'paid_at' =>[
'asc' => ['transfer.paid_at' => SORT_ASC ],
'desc' => ['transfer.paid_at' => SORT_DESC],
],
'created_at' =>[
'asc' => ['transfer.created_at' => SORT_ASC ],
'desc' => ['transfer.created_at' => SORT_DESC],
],
'status' =>[
'asc' => ['transfer.status' => SORT_ASC ],
'desc' => ['transfer.status' => SORT_DESC],
],
'money' =>[
'asc' => ['transfer.money' => SORT_ASC ],
'desc' => ['transfer.money' => SORT_DESC],
],
'count' =>[
'asc' => ['transfer.count' => SORT_ASC ],
'desc' => ['transfer.count' => SORT_DESC],
],
'item_price' =>[
'asc' => ['transfer.item_price' => SORT_ASC ],
'desc' => ['transfer.item_price' => SORT_DESC],
],
'id_account' =>[
'asc' => ['account.name' => SORT_ASC ],
'desc' => ['account.name' => SORT_DESC],
],
'id_user' =>[
'asc' => ['user.username' => SORT_ASC ],
'desc' => ['user.username' => SORT_DESC],
],
'id_customer' =>[
'asc' => ['customer.name' => SORT_ASC ],
'desc' => ['customer.name' => SORT_DESC],
],
]
]
]);
// $query->addSelect( ['*' ]);
$this->load($params);
$valid = $this->validate();
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 = $this->createQuery('total',$valid);
$this->all = $query->one();
$query = $this->createQuery( 'select', $valid);
$dataProviderSetup = [
'query' => $query,
'sort' => [
'defaultOrder' => [
'transfer_created_at' => SORT_DESC
],
'attributes' => Helper::mkYiiSortItems([
['transfer_id_transfer'],
['transfer_created_at'],
['transfer_type'],
['transfer_name'],
['user_username'],
['paid_by_username'],
['customer_name'],
['account_name'],
['transfer_item_price'],
['transfer_count'],
['transfer_money'],
['transfer_status'],
['transfer_payment_method'],
['transfer_paid_at'],
['ticket_start'],
['ticket_end'],
])
]
];
if ($this->output == 'pdf' || $this->output == 'xls') {
$dataProviderSetup['pagination'] = false;
};
$dataProvider = new ActiveDataProvider($dataProviderSetup);
$query->andFilterWhere([
'transfer.id_account' => $this->id_account,
'transfer.type' => $this->type,
'transfer.status' => $this->status,
'transfer.payment_method' => $this->payment_method,
'customer.name' => $this->customer_name
]);
$query->andFilterWhere(['in' ,'transfer.type', $this->types]);
$created_condition = ['and',[ '>=', 'transfer.created_at', $this->timestampStart ] ,[ '<', 'transfer.created_at', $this->timestampEnd ] ];
$paid_condition = ['and',[ '>=', 'transfer.paid_at', $this->timestampStart ] ,[ '<', 'transfer.paid_at', $this->timestampEnd ] ];
$query->andFilterWhere(['or' , $created_condition , $paid_condition]);
if ( isset($this->id_user)){
$query->andFilterWhere([
'or',
['transfer.id_user' => $this->id_user],
['transfer.paid_by' => $this->id_user],
]);
}
if (!RoleDefinition::isAdmin()){
Helper::restrictIfNotAdminTheStartDate($query, $this->timestampStart,['transfer.created_at','transfer.paid_at']);
}
return $dataProvider;
}
public function totalsTransfers( ){
$accountTotals = [];
$fullTotal = [
'label' => Yii::t('common/transfer', 'Total'),
'money' => 0
];
$accounts = Account::read();
$accountMap = ArrayHelper::map( $accounts ,'id_account','name' );
$idUser = $this->id_user;
/**mk totals need date time format*/
$start = $this->timestampStart;
if ( isset($start) && !empty($start)){
$start .= " 00:00";
}
$this->totals = Transfer::mkTotals($start, $this->timestampEnd, $idUser, $this->types, $this->id_account, $accounts, $accountMap);
public function totalsTransfers()
{
$accountTotals = [];
$fullTotal = [
'label' => Yii::t('common/transfer', 'Total'),
'money' => 0
];
$accounts = Account::read();
$accountMap = ArrayHelper::map($accounts, 'id_account', 'name');
$idUser = $this->id_user;
/**mk totals need date time format*/
$start = $this->timestampStart;
if (isset($start) && !empty($start)) {
$start .= " 00:00";
}
$this->totals = Transfer::mkTotals($start, $this->timestampEnd, $idUser, $this->types, $this->id_account, $accounts, $accountMap);
}
}