add frontend collection

This commit is contained in:
2015-11-04 12:33:45 +01:00
parent ed9c1e77cd
commit 48a00e4bdc
17 changed files with 370 additions and 194 deletions

View File

@@ -4,6 +4,7 @@ namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\helpers\ArrayHelper;
/**
* This is the model class for table "account".
@@ -174,4 +175,8 @@ class Account extends \yii\db\ActiveRecord
return $accounts;
}
public static function toAccaountMap($accounts){
return ArrayHelper::map( $accounts,'id_account','name' );
}
}

View File

@@ -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;
}
}

View File

@@ -30,6 +30,7 @@ class CollectionCreate extends \common\models\Collection
public $accountMap;
public $account;
public $totals;
public $userCartTotal;
public $timestampEnd;
public $timestampStart;
@@ -38,7 +39,10 @@ class CollectionCreate extends \common\models\Collection
public function rules(){
return [
['end','required'],
[[ 'end' , ], 'date' ,'format' =>Yii::$app->formatter->datetimeFormat , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ],
/**enable seconds*/
[[ 'end' , ], 'date' ,'format' => 'yyyy-MM-dd HH:mm:ss' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm:ss' ,'timeZone' => 'UTC' ],
/*enable only minutes*/
// [[ 'end' , ], 'date' ,'format' => 'yyyy-MM-dd HH:mm' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd HH:mm' ,'timeZone' => 'UTC' ],
[[ 'end' ], 'validateEndDate' ]
];
}

View File

@@ -3,6 +3,8 @@
namespace common\models;
use Yii;
use yii\db\Query;
use yii\db\Expression;
/**
* This is the model class for table "user_sold_item".
@@ -60,6 +62,29 @@ class UserSoldItem extends \yii\db\ActiveRecord
}
public static function readTotalForAccount($idUser,$idAccount){
$query = new Query();
$query->addSelect( [
new Expression( ' COALESCE(sum( transfer.money ) ,0) as money /** total unpaid reception cart */' )
]);
$query->innerJoin('transfer',['user_sold_item.id_transfer' =>'transfer.id_transfer']);
$query->from('user_sold_item');
$query->andFilterWhere([
'transfer.id_account' => $idAccount,
]);
$query->andFilterWhere(['user_sold_item.id_user' => $idUser]);
return $query->scalar();
}
/**
* @param common\models\User $user