add reception collection basics

This commit is contained in:
2015-11-03 17:27:14 +01:00
parent 33bf52df60
commit 931b83040b
12 changed files with 385 additions and 26 deletions

View File

@@ -10,6 +10,8 @@ use common\components\AccountAwareBehavior;
use common\components\UserAwareBehavior;
use common\components\DiscountAwareBehavior;
use common\components\CustomerAwareBehavior;
use yii\db\Query;
use yii\db\Expression;
/**
* This is the model class for table "transfer".
@@ -486,7 +488,7 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
$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' )
new Expression( ' COALESCE(sum( ( case when direction = '.Transfer::DIRECTION_OUT.' then -1 else 1 end )* transfer.money ),0) as money /** '. $mode.'*/' )
]);
$query->from('transfer');
@@ -518,7 +520,8 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
$query->andFilterWhere([ "transfer.status" => Transfer::STATUS_PAID ] );
$query->andFilterWhere([ '>=', 'transfer.paid_at' , $start ] );
$query->andFilterWhere([ '<' , 'transfer.paid_at' , $end ] );
$query->andFilterWhere( ['or', [ '<', 'transfer.created_at' , $start ] ,[ '>=' , 'transfer.created_at' , $end ] ] );
$query->andFilterWhere( ['or', [ '<', 'transfer.created_at' , isset( $start ) ? $start : '1900-01-01' ] ,[ '>=' , 'transfer.created_at' , $end ] ] );
}
$query->groupBy('transfer.id_account');
@@ -529,4 +532,121 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
}
/**
*
* Parse query results so, that all available account will be display, even then , if $queryResult does not contain any result for the given account
*
* @param mixed $queryResult an array, wchic contains items. each item has to key - value pairs: [ id_account => 0, money => 0 ]
* */
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;
}
/**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 mkPaidAtTotals( $start, $end, $idUser, $types, $idAccount, $accounts, $accountMap){
$result = [];
$queryResult = self::exTotalQuery('paid_at', $start, $end, $idUser, $types, $idAccount );
$result = self::mkTotalsResultWithAllAvailableAccount($queryResult, $accounts, $accountMap, $idAccount);
return $result;
}
/**find all transfers in the period ( doesn't matter if paid or not )*/
public static function mkCreatedAtTotals( $start, $end, $idUser, $types, $idAccount, $accounts, $accountMap){
$result = [];
$queryResult = self::exTotalQuery('created_at', $start, $end, $idUser, $types, $idAccount );
$result = self::mkTotalsResultWithAllAvailableAccount($queryResult, $accounts, $accountMap, $idAccount);
return $result;
}
/**find transfers which were created but not paid in the period*/
public static function mkCreatedAtNotPaidTotals( $start, $end, $idUser, $types, $idAccount, $accounts, $accountMap){
$result = [];
$queryResult = self::exTotalQuery('created_at_not_paid', $start, $end, $idUser, $types, $idAccount );
$result = self::mkTotalsResultWithAllAvailableAccount($queryResult, $accounts, $accountMap, $idAccount);
return $result;
}
/**
* find transfers which were created and paid in the period
*
* */
public static function mkCreatedAtPaidTotals( $start, $end, $idUser, $types, $idAccount, $accounts, $accountMap){
$result = [];
$queryResult = self::exTotalQuery('created_at_paid', $start, $end, $idUser, $types, $idAccount );
$result = self::mkTotalsResultWithAllAvailableAccount($queryResult, $accounts, $accountMap, $idAccount);
return $result;
}
/**
*
* find transfers, where depth was paid
* */
public static function mkPaidAtNotCreatedAtPaidTotals( $start, $end, $idUser, $types, $idAccount, $accounts, $accountMap){
$result = [];
$queryResult = self::exTotalQuery('paid_at_not_created_at', $start, $end, $idUser, $types, $idAccount );
$result = self::mkTotalsResultWithAllAvailableAccount($queryResult, $accounts, $accountMap, $idAccount);
return $result;
}
public static function mkTotals( $start, $end, $idUser, $types, $idAccount, $accounts, $accountMap ){
$result = [];
$result['paid_at'] = self::mkPaidAtTotals($start, $end, $idUser, $types, $idAccount, $accounts, $accountMap);
$result['created_at'] = self::mkCreatedAtTotals($start, $end, $idUser, $types, $idAccount, $accounts, $accountMap);
$result['created_at_not_paid'] = self::mkCreatedAtNotPaidTotals($start, $end, $idUser, $types, $idAccount, $accounts, $accountMap);
$result['created_at_paid'] = self::mkCreatedAtPaidTotals($start, $end, $idUser, $types, $idAccount, $accounts, $accountMap);
$result['paid_at_not_created_at'] = self::mkPaidAtNotCreatedAtPaidTotals($start, $end, $idUser, $types, $idAccount, $accounts, $accountMap);
return $result;
}
}