add frontend changes
This commit is contained in:
@@ -6,31 +6,56 @@ use Yii;
|
||||
use yii\base\Model;
|
||||
use yii\data\ActiveDataProvider;
|
||||
use common\models\Transfer;
|
||||
use yii\base\Object;
|
||||
use yii\db\Query;
|
||||
use yii\db\Expression;
|
||||
use common\models\Account;
|
||||
|
||||
use yii\helpers\ArrayHelper;
|
||||
/**
|
||||
* TransferSearch represents the model behind the search form about `common\models\Transfer`.
|
||||
*/
|
||||
class TransferSearch extends Transfer
|
||||
{
|
||||
|
||||
public $start;
|
||||
public $end;
|
||||
|
||||
public $timestampStart;
|
||||
public $timestampEnd;
|
||||
|
||||
public $totalsCreatedAt = ['total' => 0, 'accounts' =>[] ];
|
||||
public $totalsCreatedAtNotPaid= ['total' => 0, 'accounts' =>[]];
|
||||
public $totalsCreatedAtPaid= ['total' => 0, 'accounts' =>[]];
|
||||
public $totalsPaidAt= ['total' => 0, 'accounts' =>[]];
|
||||
|
||||
|
||||
public $accounts;
|
||||
|
||||
public $types;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['id_transfer', 'id_discount', 'id_currency', 'id_object', 'status', 'type', 'item_price', 'count', 'money', 'money_currency', 'rate', 'id_user'], 'integer'],
|
||||
[['comment', 'created_at', 'updated_at'], 'safe'],
|
||||
// [[ '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' ],
|
||||
|
||||
[ [ 'id_account' ] , 'integer'],
|
||||
['types', 'each', 'rule' => ['integer']],
|
||||
// [ [ 'types' ], function ($attribute, $params) {
|
||||
// if (!is_array($this->$attribute)) {
|
||||
// $this->addError($attribute, 'Invalid array');
|
||||
// }
|
||||
// }],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function scenarios()
|
||||
{
|
||||
// bypass scenarios() implementation in the parent class
|
||||
return Model::scenarios();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates data provider instance with search query applied
|
||||
@@ -56,24 +81,208 @@ class TransferSearch extends Transfer
|
||||
}
|
||||
|
||||
$query->andFilterWhere([
|
||||
'id_transfer' => $this->id_transfer,
|
||||
'id_discount' => $this->id_discount,
|
||||
'id_currency' => $this->id_currency,
|
||||
'id_object' => $this->id_object,
|
||||
'status' => $this->status,
|
||||
'id_account' => $this->id_account,
|
||||
'type' => $this->type,
|
||||
'item_price' => $this->item_price,
|
||||
'count' => $this->count,
|
||||
'money' => $this->money,
|
||||
'money_currency' => $this->money_currency,
|
||||
'rate' => $this->rate,
|
||||
'id_user' => $this->id_user,
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
]);
|
||||
|
||||
$query->andFilterWhere(['in' ,'type', $this->types]);
|
||||
|
||||
$query->andFilterWhere(['like', 'comment', $this->comment]);
|
||||
|
||||
$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]);
|
||||
|
||||
return $dataProvider;
|
||||
}
|
||||
|
||||
|
||||
protected function mkTotalQuery($mode){
|
||||
|
||||
$query = new Query();
|
||||
|
||||
$query->addSelect( [
|
||||
new Expression( 'transfer.id_account as account'),
|
||||
new Expression( ' COALESCE(sum( ( case when direction = '.Transfer::DIRECTION_OUT.' then -1 else 1 end )* transfer.money ),0) as money' )
|
||||
|
||||
]);
|
||||
$query->from('transfer');
|
||||
|
||||
$query->andFilterWhere([
|
||||
'id_account' => $this->id_account,
|
||||
'type' => $this->type,
|
||||
]);
|
||||
|
||||
$query->andWhere(['id_user' => Yii::$app->user->id]);
|
||||
|
||||
$query->andFilterWhere(['in' ,'type', $this->types]);
|
||||
|
||||
|
||||
if ( $mode == 'created_at'){
|
||||
$query->andFilterWhere([ '>=', 'transfer.created_at' , $this->timestampStart ] );
|
||||
$query->andFilterWhere([ '<' , 'transfer.created_at' , $this->timestampEnd ] );
|
||||
}else if ( $mode == 'paid_at'){
|
||||
$query->andFilterWhere([ '>=', 'transfer.paid_at' , $this->timestampStart ] );
|
||||
$query->andFilterWhere([ '<' , 'transfer.paid_at' , $this->timestampEnd ] );
|
||||
}else if ( $mode == 'created_at_not_paid'){
|
||||
$query->andFilterWhere([ "transfer.status" => Transfer::STATUS_NOT_PAID ] );
|
||||
$query->andFilterWhere([ '>=', 'transfer.created_at' , $this->timestampStart ] );
|
||||
$query->andFilterWhere([ '<' , 'transfer.created_at' , $this->timestampEnd ] );
|
||||
}else if ( $mode == 'created_at_paid'){
|
||||
$query->andFilterWhere([ "transfer.status" => Transfer::STATUS_PAID ] );
|
||||
$query->andFilterWhere([ '>=', 'transfer.created_at' , $this->timestampStart ] );
|
||||
$query->andFilterWhere([ '<' , 'transfer.created_at' , $this->timestampEnd ] );
|
||||
}
|
||||
|
||||
$query->groupBy('transfer.id_account');
|
||||
|
||||
return $query;
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected function mkTransferTotalsWithZeroValue( ){
|
||||
$result = [];
|
||||
|
||||
foreach ( $this->accounts as $a ){
|
||||
$accountTotal = [
|
||||
'id_account' => $a->id_account,
|
||||
'label' => $a->name,
|
||||
'money' => 0
|
||||
];
|
||||
|
||||
$result[] = $accountTotal;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected function mkTotalsResult($queryResult,$accountMap){
|
||||
|
||||
$totals = [];
|
||||
$totals['total'] = 0;
|
||||
|
||||
$totals['accounts'] = [];
|
||||
|
||||
foreach ($queryResult as $item){
|
||||
$account = "";
|
||||
if ( array_key_exists($item['account'], $accountMap)){
|
||||
$account = $accountMap[$item['account']];
|
||||
}
|
||||
|
||||
$accountTotal = [
|
||||
'id_account' => $item['account'],
|
||||
'label' => $account,
|
||||
'money' => $item['money']
|
||||
];
|
||||
$totals['accounts'][] = $accountTotal;
|
||||
$totals['total'] += $item['money'];
|
||||
}
|
||||
return $totals;
|
||||
}
|
||||
|
||||
protected function findByAccountInQueryResult($queryResult, $account){
|
||||
$result = null;
|
||||
foreach ($queryResult as $item){
|
||||
if( $item['account'] == $account->id_account ){
|
||||
$result = $item;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function mkTotalsResultWithAllAvailableAccount($queryResult,$accountMap){
|
||||
|
||||
$totals = [];
|
||||
$totals['total'] = 0;
|
||||
|
||||
$totals['accounts'] = [];
|
||||
|
||||
foreach ($this->accounts as $account){
|
||||
// echo 'account="'. $this->id_account .'"<br>';
|
||||
if ( isset($this->id_account) && is_numeric($this->id_account) && $this->id_account != $account->id_account ){
|
||||
continue ;
|
||||
}
|
||||
$accountTotal = [
|
||||
'id_account' => $account->id_account,
|
||||
'label' => $account->name,
|
||||
'money' => 0,
|
||||
];
|
||||
|
||||
$item = $this->findByAccountInQueryResult($queryResult, $account);
|
||||
|
||||
if ( isset($item)){
|
||||
$accountTotal['money'] = $item['money'];
|
||||
}
|
||||
|
||||
$totals['accounts'][] = $accountTotal;
|
||||
$totals['total'] += $accountTotal['money'];
|
||||
}
|
||||
return $totals;
|
||||
}
|
||||
|
||||
|
||||
public function mkPaidAtTotals($accountMap){
|
||||
$query = $this->mkTotalQuery('paid_at');
|
||||
$command = $query->createCommand();
|
||||
$result = $command->queryAll();
|
||||
|
||||
// $paidAtTotals = $this->mkTotalsResult($result, $accountMap);
|
||||
$paidAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap);
|
||||
|
||||
return $paidAtTotals;
|
||||
}
|
||||
|
||||
public function mkCreatedAtTotals($accountMap){
|
||||
$query = $this->mkTotalQuery('created_at');
|
||||
$command = $query->createCommand();
|
||||
$result = $command->queryAll();
|
||||
|
||||
$createdAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap);
|
||||
|
||||
return $createdAtTotals;
|
||||
}
|
||||
|
||||
public function mkCreatedAtNotPaidTotals($accountMap){
|
||||
$query = $this->mkTotalQuery('created_at_not_paid');
|
||||
$command = $query->createCommand();
|
||||
$result = $command->queryAll();
|
||||
|
||||
$createdAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap);
|
||||
|
||||
return $createdAtTotals;
|
||||
}
|
||||
|
||||
public function mkCreatedAtPaidTotals($accountMap){
|
||||
$query = $this->mkTotalQuery('created_at_paid');
|
||||
$command = $query->createCommand();
|
||||
$result = $command->queryAll();
|
||||
|
||||
$createdAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap);
|
||||
|
||||
return $createdAtTotals;
|
||||
}
|
||||
|
||||
|
||||
public function totalsTransfers($params){
|
||||
$accountTotals = [];
|
||||
$fullTotal = [
|
||||
'label' => Yii::t('common/transfer', 'Total'),
|
||||
'money' => 0
|
||||
];
|
||||
|
||||
if ( $this->hasErrors() ){
|
||||
return;
|
||||
}
|
||||
|
||||
$accounts = Account::find()->orderBy("name asc")->all();
|
||||
$accountMap = ArrayHelper::map( $accounts ,'id_account','name' );
|
||||
|
||||
$this->totalsCreatedAt = $this->mkCreatedAtTotals($accountMap);
|
||||
$this->totalsPaidAt = $this->mkPaidAtTotals($accountMap);
|
||||
$this->totalsCreatedAtPaid = $this->mkCreatedAtPaidTotals($accountMap);
|
||||
$this->totalsCreatedAtNotPaid = $this->mkCreatedAtNotPaidTotals($accountMap);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user