fix account_state ( collection_money ), display product/ticket on reception only for selected account, add daily transfers (reception/admin)

This commit is contained in:
2015-12-31 13:52:22 +01:00
parent 72fc139674
commit f59eadd8cc
26 changed files with 1153 additions and 404 deletions

View File

@@ -26,6 +26,7 @@ use yii\helpers\ArrayHelper;
* @property integer $banknote_10000_ft
* @property integer $banknote_20000_ft
* @property integer $id_user
* @property integer $collection_money
* @property string $created_at
* @property string $updated_at
*/
@@ -53,6 +54,7 @@ class AccountState extends \common\models\BaseFitnessActiveRecord
[['id_account', 'type', 'money', 'banknote_5_ft', 'banknote_10_ft', 'banknote_20_ft', 'banknote_50_ft', 'banknote_100_ft', 'banknote_200_ft', 'banknote_500_ft', 'banknote_1000_ft', 'banknote_2000_ft', 'banknote_5000_ft', 'banknote_10000_ft', 'banknote_20000_ft' ], 'integer'],
[['comment' ], 'string' ,'max' => 255],
[['prev_state' ], 'integer'],
[['id_account'] , 'validatePrevState'],
];
}
@@ -87,6 +89,11 @@ class AccountState extends \common\models\BaseFitnessActiveRecord
];
}
public function validatePrevState($attribute,$params){
}
public static function banknoteValues()
{
return [
@@ -124,6 +131,10 @@ class AccountState extends \common\models\BaseFitnessActiveRecord
return $this->hasOne( User::className(), ["id" =>"id_user" ] );
}
public function getPrevObject(){
return $this->hasOne( AccountState::className(), ["id_account_state" =>"prev_state" ] );
}
public function getUserName(){
$result = "";
$user = $this->user;
@@ -164,40 +175,45 @@ class AccountState extends \common\models\BaseFitnessActiveRecord
* @param $type int the type of accountstate to load
* @param $user $id of user to load the account
* */
public static function readLast($type, $user = null){
public static function readLast($type, $user = null,$account = null){
$result = null;
$query = AccountState::find();
//filter user
if ( isset($user)){
$query->innerJoinWith("account");
$query->innerJoinWith("account.userAccountAssignments");
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id]);
}
//filter type
if ( isset($type)){
$query->andWhere(['account_state.type' => $type]);
}
//filter last
$query->join("INNER JOIN ",
"(select max(created_at) as cdate, id_account from account_state group by id_account) as sq1"
,"sq1.cdate = account_state.created_at and sq1.id_account = account_state.id_account");
if ( $account ){
$query->andWhere(["account_state.id_account" => $account ]);
}
$query->limit(1);
$query->orderBy(['created_at' => SORT_DESC]);
$result = $query->all();
$result = $query->one();
return $result;
}
function beforeSave($insert){
$result = parent::beforeSave($insert);
if ( $result ){
$start = null;
$end = null;
$prev = null;
$this->prev_money = 0;
if ( isset($this->prev_state) ){
$prev = AccountState::findOne($this->prev_state);
if ( $this->type == AccountState::TYPE_CLOSE){
$lastOpen = AccountState::readLast(AccountState::TYPE_OPEN,null, $this->id_account);
if ( $lastOpen != null ){
$start = $lastOpen->created_at;
$this->prev_state = $lastOpen->id_account_state;
$this->prev_money = $lastOpen->money;
}
$end = date("Y-m-d H:i:s");
$this->collection_money = Transfer::readPaid($start, $end, Yii::$app->user->id);
}
if ( $prev != null){
$this->prev_money = $prev->money;
}
}
return $result;
@@ -205,12 +221,39 @@ class AccountState extends \common\models\BaseFitnessActiveRecord
public function hasDifferenceToPrevState(){
$result = false;
if ( isset( $this->prev_state ) && $this->type == self::TYPE_OPEN){
$result = $this->prev_money != $this->money;
if ( $this->type == self::TYPE_CLOSE){
$result = ( $this->prev_money + $this->collection_money) != $this->money;
}
return $result;
}
public function hasPlus(){
$result = false;
if ( $this->type == self::TYPE_CLOSE){
$result = ( $this->prev_money + $this->collection_money) < $this->money;
}
return $result;
}
public function hasMinus(){
$result = false;
if ( $this->type == self::TYPE_CLOSE){
$result =( $this->prev_money + $this->collection_money) > $this->money;
}
return $result;
}
public function getSignedDiff(){
$result = $this->money - ( $this->prev_money + $this->collection_money);
return $result;
}
public function getExpected(){
$result = $this->prev_money + $this->collection_money ;
return $result;
}
public static function readLastForUser($type){
return static::readLast($type, Yii::$app->user->id);
}