add frontend collection
This commit is contained in:
@@ -7,6 +7,8 @@ use common\components\AccountAwareBehavior;
|
||||
use common\components\UserAwareBehavior;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
use yii\db\Query;
|
||||
use yii\db\Expression;
|
||||
|
||||
/**
|
||||
* This is the model class for table "collection".
|
||||
@@ -104,4 +106,98 @@ class Collection extends \common\models\BaseFitnessActiveRecord
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* */
|
||||
public static function mkTotalQuery($mode = 'reception', $start,$end,$idUser,$types,$idAccount){
|
||||
|
||||
$query = new Query();
|
||||
|
||||
$query->addSelect( [
|
||||
new Expression( ' collection.id_account as account'),
|
||||
new Expression( ' COALESCE(sum( collection.money ) ,0) as money /** collections total money */' )
|
||||
|
||||
]);
|
||||
$query->from('collection');
|
||||
|
||||
$query->andFilterWhere([
|
||||
'id_account' => $idAccount,
|
||||
]);
|
||||
|
||||
$query->andFilterWhere(['id_user' => $idUser]);
|
||||
$query->andFilterWhere(['in' ,'type', $types]);
|
||||
self::inInterval($query, 'collection.end' , $start, $end);
|
||||
|
||||
$query->groupBy('collection.id_account');
|
||||
|
||||
return $query;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static function mkTotalsResultWithAllAvailableAccount($queryResult,$accounts,$accountMap,$idAccount){
|
||||
|
||||
$totals = [];
|
||||
$totals['total'] = 0;
|
||||
|
||||
$totals['accounts'] = [];
|
||||
|
||||
foreach ($accounts as $account){
|
||||
if ( isset($idAccount) && is_numeric($idAccount) && $idAccount != $account->id_account ){
|
||||
continue ;
|
||||
}
|
||||
|
||||
$accountTotal = [
|
||||
'id_account' => $account->id_account,
|
||||
'label' => $account->name,
|
||||
'money' => 0,
|
||||
];
|
||||
|
||||
$item = self::findByAccountInQueryResult($queryResult, $account);
|
||||
|
||||
if ( isset($item)){
|
||||
$accountTotal['money'] = $item['money'];
|
||||
}
|
||||
|
||||
$totals['accounts'][] = $accountTotal;
|
||||
$totals['total'] += $accountTotal['money'];
|
||||
}
|
||||
return $totals;
|
||||
}
|
||||
|
||||
public static function findByAccountInQueryResult( $queryResult, $account ){
|
||||
$result = null;
|
||||
foreach ($queryResult as $item){
|
||||
if( $item['account'] == $account->id_account ){
|
||||
$result = $item;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function inInterval($query ,$field , $start,$end ){
|
||||
$query->andFilterWhere([ '>=', $field , $start ] );
|
||||
$query->andFilterWhere([ '<' , $field , $end ] );
|
||||
}
|
||||
|
||||
|
||||
/**create and execute a "total" query*/
|
||||
public static function exTotalQuery($mode,$start,$end,$idUser,$types,$idAccount){
|
||||
$query = self::mkTotalQuery($mode, $start, $end, $idUser, $types, $idAccount );
|
||||
$command = $query->createCommand();
|
||||
$result = $command->queryAll();
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*find all transfers which were paid in the period
|
||||
* */
|
||||
public static function mkReceptionTotal( $start, $end, $idUser, $types, $idAccount, $accounts, $accountMap){
|
||||
$result = [];
|
||||
$queryResult = self::exTotalQuery('reception', $start, $end, $idUser, $types, $idAccount );
|
||||
$result = self::mkTotalsResultWithAllAvailableAccount($queryResult, $accounts, $accountMap, $idAccount);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user