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

@@ -1,7 +1,10 @@
<?php
return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
// 'language' => 'hu-HU',
'timezone' => 'Europe/Budapest',
'language' => 'hu',
'components' => [
'cache' => [
@@ -21,8 +24,8 @@ return [
'datetimeFormat' => 'yyyy.MM.dd HH:mm',
'timeFormat' => 'HH:mm',
'locale' => 'hu-Hu',
'timeZone' => 'UTC',
'defaultTimeZone' => 'UTC' ,
'timeZone' => 'EUROPE/BUDAPEST',
'defaultTimeZone' => 'EUROPE/BUDAPEST',
'nullDisplay' => "-",
],
'authManager' => [

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

View File

@@ -126,7 +126,33 @@ class Product extends \common\models\BaseFitnessActiveRecord {
return $warehouses;
}
public static function findProduct($query){
/**
* $param int $forceIncludeAccount id warehouse, that should be included in list, even if it is inactive
* */
public static function readForDefaultAccount($forceIncludeObjectWithId = null){
$result = null;
$account = Account::readDefault();
if ( $forceIncludeObjectWithId == null){
$query = Product::find()->andWhere(['status' => Product::STATUS_ACTIVE])->orderBy(['product.name' => SORT_ASC]);
if ( $account )
$query->andWhere(["product.id_account" => $account]);
$result = $query->all();
}else{
$query = Product::find()->andWhere( ['or', ['status' => Product::STATUS_ACTIVE], ['id_product' => $forceIncludeObjectWithId ] ])->orderBy(['product.name' => SORT_ASC]);
if ( $account )
$query->andWhere(["product.id_account" => $account]);
$result = $query->all();
}
return $result;
}
public static function findProduct($query, $account = null){
$result = [];
$product = null;
@@ -137,6 +163,7 @@ class Product extends \common\models\BaseFitnessActiveRecord {
['barcode' => $query ],
]
)->andWhere(['status' =>Product::STATUS_ACTIVE])
->andFilterWhere(['product.id_account' => $account])
->all();

View File

@@ -198,13 +198,13 @@ class TicketType extends \common\models\BaseFitnessActiveRecord {
/**
* $param int $forceIncludeAccount id account, that should be included in list, even if it is inactive
* */
public static function read($forceIncludeObjectWithId = null){
public static function read($forceIncludeObjectWithId = null, $account = null){
$ticketTypes = null;
if ( $forceIncludeObjectWithId == null){
$ticketTypes = TicketType::find()->andWhere(['status' => self::STATUS_ACTIVE])->all();
$ticketTypes = TicketType::find()->andWhere(['status' => self::STATUS_ACTIVE])->andFilterWhere(['ticket_type.id_account' => $account])->all();
}else{
$ticketTypes = TicketType::find()->andWhere( ['or', ['status' => self::STATUS_ACTIVE], ['id_ticket_type' => $forceIncludeObjectWithId ] ])->all();
$ticketTypes = TicketType::find()->andWhere( ['or', ['status' => self::STATUS_ACTIVE], ['id_ticket_type' => $forceIncludeObjectWithId ] ])->andFilterWhere(['ticket_type.id_account' => $account])->all();
}
return $ticketTypes;

View File

@@ -59,7 +59,7 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
return ArrayHelper::merge( [
[
'class' => TimestampBehavior::className(),
'value' => function(){ return date('Y-m-d H:i:s' ); }
'value' => function(){ return date('Y-m-d H:i:s' ); }
],
[
'class' => DiscountAwareBehavior::className(),
@@ -168,7 +168,7 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
}else if ( $this->type == Transfer::TYPE_PRODUCT ){
$result = $this->productName;
}else if ( $this->type == Transfer::TYPE_MONEY_MOVEMENT_OUT ){
$result = "Pénz kivétel - " . $this->moneyMovement->name;
$result = $this->moneyMovement->humanType;
}
return $result;
}
@@ -677,4 +677,19 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
return $result;
}
public static function readPaid($start,$end,$idUser){
$query = (new \yii\db\Query());
$query->select(['coalesce(sum( case when transfer.direction = ' . Transfer::DIRECTION_IN. ' then transfer.money else -1 * transfer.money end ),0) AS transfer_money']);
$query->from('transfer');
$query->andWhere(['transfer.id_user' => $idUser ]);
$created_condition = ['and',[ '>=', 'transfer.created_at', $start ] ,[ '<', 'transfer.created_at', $end ] ];
$paid_condition = ['and',[ '>=', 'transfer.paid_at', $start] ,[ '<', 'transfer.paid_at', $end ] ];
$query->andFilterWhere(['or' , $created_condition , $paid_condition]);
$query->andWhere(['transfer.status' => Transfer::STATUS_PAID]);
return $query->scalar();
}
}

View File

@@ -0,0 +1,390 @@
<?php
namespace common\models;
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;
use common\models\MoneyMovement;
/**
* TransferSearch represents the model behind the search form about `common\models\Transfer`.
*/
class TransferListSearch 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 $totalsPaidAtNotCreatedAt= ['total' => 0, 'accounts' =>[]];
public $totals;
public $accounts;
public $types;
public $users;
/**
* all money gained with ticket sell
* */
public $ticketMoney;
/**
* all money gained with product sell
* */
public $productMoney;
/**
* all money gained with product sell grouped by category
* */
public $productMoneies;
/**
* all money lost by money movement
* */
public $moneyMovementMoneis;
public $moneyMovementMoney;
/**
* total gained money
* */
public $total;
/**
* ticket sale statisitc
* */
public $ticketStats;
/**
* money movements by type
* */
public $moneyMovementsByType;
public $tickets;
/**
* all product transfer
* */
public $products;
public $moneyMovements;
public $productsByCategory;
/**
* @inheritdoc
*/
public function rules()
{
return [
[[ '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','id_user' ] , 'integer'],
['types', 'each', 'rule' => ['integer']],
];
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Transfer::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
}
$this->readTicketMoney();
$this->readProductsMoney();
$this->readMoneyMovementMoney();
$this->calcTotal();
$this->readProductsByCategory();
$this->readProductsByCategoryDetailed();
$this->readTicketStas();
$this->readMoneyMovementsStats();
$this->readTickets();
$this->readProducts();
$this->readMoneyMovements();
}
protected function calcTotal(){
$this->total = 0;
$this->total += $this->ticketMoney;
$this->total += $this->productMoney;
$this->total += $this->moneyMovementMoneis;
}
protected function addAccountConstraint($query){
$query->innerJoin("user_account_assignment",'transfer.id_account = user_account_assignment.id_account' );
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id ]);
$query->andWhere(['transfer.id_user' => Yii::$app->user->id ]);
}
protected function addQueryFilters($query){
$this->addAccountConstraint($query);
$query->andFilterWhere([
'transfer.id_account' => $this->id_account,
'transfer.id_user' => $this->id_user,
'transfer.type' => $this->type,
'transfer.id_user' => $this->id_user,
]);
$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]);
$query->andWhere(['transfer.status' => Transfer::STATUS_PAID]);
}
protected function readTicketStas(){
$query = (new \yii\db\Query());
$query->select(['ticket_type.name as ticket_type_name' , 'coalesce(sum(abs(transfer.count)),0) AS ticket_count', 'coalesce(sum(abs(transfer.money)),0) AS ticket_money']);
$query->from('transfer');
$query->andWhere(['transfer.type' => Transfer::TYPE_TICKET]);
$query->innerJoin("ticket", "ticket.id_ticket = transfer.id_object");
$query->innerJoin("ticket_type", "ticket.id_ticket_type = ticket_type.id_ticket_type");
$query->groupBy(['ticket_type.id_ticket_type','ticket_type.name']);
$this->addQueryFilters($query);
$this->ticketStats = $query->all();
}
protected function readTicketMoney(){
$query = (new \yii\db\Query());
$query->select([' coalesce(sum(abs(transfer.money)),0) AS ticket_money']);
$query->from('transfer');
$query->andWhere(['transfer.type' => Transfer::TYPE_TICKET]);
$query->innerJoin("ticket", "ticket.id_ticket = transfer.id_object");
$this->addQueryFilters($query);
$this->ticketMoney = $query->scalar();
}
protected function readProductsByCategory(){
$query = (new \yii\db\Query());
$query->select(['coalesce(sum(transfer.money),0) AS product_money', 'coalesce(sum(transfer.count),0) as category_count', 'product_category.name as category_name']);
$query->from('transfer');
$query->groupBy(['product_category.id_product_category','product_category.name']);
$query->andWhere(['transfer.type' => Transfer::TYPE_PRODUCT]);
$query->innerJoin("sale", "sale.id_sale = transfer.id_object");
$query->innerJoin("product", "sale.id_product = product.id_product");
$query->innerJoin("product_category", "product.id_product_category = product_category.id_product_category");
$this->addQueryFilters($query);
$this->productMoneies = $query->all();
}
protected function readProductsByCategoryDetailed(){
$formatted = [];
$formatted['categories'] = [];
$formatted['total'] = 0;
$prevCategory = null;
$curCategory = null;
$category = null;
$query = (new \yii\db\Query());
$query->select(['product_category.id_product_category as product_category_id','product_category.name as product_category_name', 'product.name as product_name' ,'coalesce(sum(transfer.money),0) AS product_money', 'coalesce(sum(transfer.count),0) as product_count']);
$query->from('transfer');
$query->groupBy(['product.id_product','product.name','product_category.id_product_category','product_category.name']);
$query->andWhere(['transfer.type' => Transfer::TYPE_PRODUCT]);
$query->innerJoin("sale", "sale.id_sale = transfer.id_object");
$query->innerJoin("product", "sale.id_product = product.id_product");
$query->innerJoin("product_category", "product.id_product_category = product_category.id_product_category");
$query->orderBy(['product_category.name' => SORT_ASC,'product.name' => SORT_ASC]);
$this->addQueryFilters($query);
$result = $query->all();
foreach ($result as $row){
$curCategory = $row['product_category_id'];
if ( $curCategory != $prevCategory ){
//store last category
if ( $category != null ){
$formatted['categories'][] = $category;
}
$prevCategory = $curCategory;
//create new category
$category = [];
$category['category'] = [];
$category['category']['name'] = $row['product_category_name'];
$category['category']['id'] = $row['product_category_id'];
$category['products'] = [];
$category['total'] = 0;
}
$category['products'][] = $row;
$category['total'] += $row['product_money'];
$formatted['total'] += $row['product_money'];
}
if ( $category != null ){
$formatted['categories'][]= $category;
}
$this->productsByCategory = $formatted;
}
protected function readProductsMoney(){
$query = (new \yii\db\Query());
$query->select(['coalesce(sum(transfer.money),0) AS product_money' ]);
$query->from('transfer');
$query->andWhere(['transfer.type' => Transfer::TYPE_PRODUCT]);
$query->innerJoin("sale", "sale.id_sale = transfer.id_object");
$this->addQueryFilters($query);
$this->productMoney = $query->scalar();
}
protected function readMoneyMovementMoney(){
$query = (new \yii\db\Query());
$query->select([' coalesce(sum( case when transfer.direction = ' . Transfer::DIRECTION_IN. ' then transfer.money else -1 * transfer.money end ),0) AS money_movement_money']);
$query->from('transfer');
$query->andWhere(['transfer.type' => Transfer::TYPE_MONEY_MOVEMENT_OUT]);
$query->innerJoin("money_movement", "money_movement.id_money_movement = transfer.id_object");
$this->addQueryFilters($query);
$this->moneyMovementMoneis = $query->scalar();
}
protected function readMoneyMovementsStats(){
$query = (new \yii\db\Query());
$query->select([ 'money_movement.type as money_movement_type', ' coalesce(count(transfer.id_transfer),0) AS money_movement_count', 'coalesce(sum( case when transfer.direction = ' . Transfer::DIRECTION_IN. ' then transfer.money else -1 * transfer.money end ),0) AS money_movement_money']);
$query->from('transfer');
$query->andWhere(['transfer.type' => Transfer::TYPE_MONEY_MOVEMENT_OUT]);
$query->innerJoin("money_movement", "money_movement.id_money_movement = transfer.id_object");
$query->groupBy(['money_movement.type']);
$this->addQueryFilters($query);
$this->moneyMovementsByType = $query->all();
for ($i = 0; $i < count($this->moneyMovementsByType) ;$i++ ){
$this->moneyMovementsByType[$i]['name'] = MoneyMovement::typeName($this->moneyMovementsByType[$i]['money_movement_type']);
}
}
protected function readTickets(){
$query = (new \yii\db\Query());
$query->select(['user.username as user_name', 'account.name as account_name','ticket_type.name as ticket_type_name' , 'transfer.count AS ticket_count', 'transfer.money AS ticket_money','transfer.item_price AS ticket_item_price', 'transfer.created_at as ticket_created_at','transfer.paid_at as ticket_paid_at']);
$query->from('transfer');
$query->andWhere(['transfer.type' => Transfer::TYPE_TICKET]);
$query->innerJoin("ticket", "ticket.id_ticket = transfer.id_object");
$query->innerJoin("ticket_type", "ticket.id_ticket_type = ticket_type.id_ticket_type");
$query->innerJoin("account", "transfer.id_account = account.id_account");
$query->innerJoin("user", "transfer.id_user = user.id");
$query->orderBy(['transfer.created_at' => SORT_ASC]);
$this->addQueryFilters($query);
$this->tickets = $query->all();
for ($i = 0; $i < count($this->tickets) ;$i++ ){
$this->tickets[$i]['ticket_created_at'] = Yii::$app->formatter->asDatetime($this->tickets[$i]['ticket_created_at'], 'yyyy.MM.dd HH:mm:ss');
$this->tickets[$i]['ticket_paid_at'] = empty($this->tickets[$i]['ticket_paid_at'] ) ? '-' : Yii::$app->formatter->asDatetime($this->tickets[$i]['ticket_paid_at'], 'yyyy.MM.dd HH:mm:ss');
}
}
protected function readProducts(){
$query = (new \yii\db\Query());
$query->select([ 'user.username as user_name','account.name as account_name' , 'product_category.name as product_category_name', 'product.name as product_name', 'transfer.money AS product_money', 'transfer.count AS product_count', 'transfer.money AS product_money','transfer.item_price AS product_item_price', 'transfer.created_at as product_created_at','transfer.paid_at as product_paid_at']);
$query->from('transfer');
$query->andWhere(['transfer.type' => Transfer::TYPE_PRODUCT]);
$query->innerJoin("sale", "sale.id_sale = transfer.id_object");
$query->innerJoin("product", "sale.id_product = product.id_product");
$query->innerJoin("product_category", "product.id_product_category = product_category.id_product_category");
$query->innerJoin("account", "transfer.id_account = account.id_account");
$query->innerJoin("user", "transfer.id_user = user.id");
$query->orderBy(['transfer.created_at' => SORT_ASC]);
$this->addQueryFilters($query);
$this->products = $query->all();
for ($i = 0; $i < count($this->products) ;$i++ ){
$this->products[$i]['product_created_at'] = Yii::$app->formatter->asDatetime($this->products[$i]['product_created_at'], 'yyyy.MM.dd HH:mm:ss');
$this->products[$i]['product_paid_at'] = empty($this->products[$i]['product_paid_at'] ) ? '-' : Yii::$app->formatter->asDatetime($this->products[$i]['product_paid_at'], 'yyyy.MM.dd HH:mm:ss');
}
}
protected function readMoneyMovements(){
$query = (new \yii\db\Query());
$query->select([ 'user.username as user_name','account.name as account_name', 'transfer.direction as transfer_direction' ,'money_movement.type as money_movement_type', 'transfer.money AS money_movement_money', 'money_movement.name as money_movement_name','transfer.created_at as money_movement_created_at', ]);
$query->from('transfer');
$query->andWhere(['transfer.type' => Transfer::TYPE_MONEY_MOVEMENT_OUT]);
$query->innerJoin("money_movement", "money_movement.id_money_movement = transfer.id_object");
$query->innerJoin("account", "transfer.id_account = account.id_account");
$query->innerJoin("user", "transfer.id_user = user.id");
$query->orderBy(['transfer.created_at' => SORT_ASC]);
$this->addQueryFilters($query);
$this->moneyMovements = $query->all();
for ($i = 0; $i < count($this->moneyMovements) ;$i++ ){
$this->moneyMovements[$i]['money_movement_type_name'] = MoneyMovement::typeName($this->moneyMovements[$i]['money_movement_type']);
$this->moneyMovements[$i]['money_movement_created_at'] = Yii::$app->formatter->asDatetime($this->moneyMovements[$i]['money_movement_created_at'], 'yyyy.MM.dd HH:mm:ss');
$this->moneyMovements[$i]['signed_money'] = Transfer::toSignedMoney($this->moneyMovements[$i]['transfer_direction'],$this->moneyMovements[$i]['money_movement_money']);
}
}
}

View File

@@ -1,6 +1,12 @@
<?php
use common\components\AccountTotalWidget;
?>
<div class='row'>
<div class='col-md-4'>
<a class="btn btn-primary" style="margin-bottom: 6px" onclick="javascript: $('#account_stat_panels').toggle();">Statisztika mutatása/elrejtése</a>
</div>
</div>
<div id='account_stat_panels' style="display: none;">
<div class='row'>
<div class='col-md-4'>
<?php
@@ -61,4 +67,5 @@ use common\components\AccountTotalWidget;
<div class='col-md-4'>
</div>
</div>
</div>