add reception collection basics

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

View File

@ -132,13 +132,14 @@ class Account extends \yii\db\ActiveRecord
$query = Account::find();
$query->innerJoinWith('userAccountAssignments');
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id]);
$query->andWhere(['status' => Account::STATUS_ACTIVE]);
if ( $forceIncludeAccount == null){
$query->andWhere(['status' => Account::STATUS_ACTIVE])->all();
}else{
$query->andWhere( ['or', ['status' => Account::STATUS_ACTIVE], ['id_account' => $forceIncludeAccount ] ])->all();
}
$query->orderBy( ['name' => SORT_ASC]);
$accounts = $query->all();
return $accounts;
}
@ -156,4 +157,21 @@ class Account extends \yii\db\ActiveRecord
}
/**
* read assigned account
* */
public static function readOne($idAccount){
$accounts = null;
$query = Account::find();
$query->innerJoinWith('userAccountAssignments');
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id]);
$query->andWhere(['status' => Account::STATUS_ACTIVE]);
$query->andWhere(['account.id_account' => $idAccount]);
$accounts = $query->one();
return $accounts;
}
}

View File

@ -26,6 +26,12 @@ class CollectionCreate extends \common\models\Collection
{
public $lastCollection;
public $accounts;
public $account;
public $totals;
}

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

View File

@ -0,0 +1,28 @@
<?php
namespace frontend\components;
use yii\base\Widget;
class TransferTotalWidget extends Widget{
public $options = [
'totalHeading' => 'Összessen'
];
public $viewFile = '//common/_transfer_total';
public function init(){
parent::init();
if ( !isset($this->options['totalHeading'] ) ){
$this->options['totalHeading'] = 'Összesen';
}
}
public function run(){
echo $this->render($this->viewFile,[ 'options' => $this->options ]);
}
}

View File

@ -11,6 +11,9 @@ use yii\filters\VerbFilter;
use common\models\Account;
use common\models\User;
use common\models\CollectionCreate;
use common\models\Transfer;
use frontend\models\CreateAccountSelectForm;
use yii\helpers\ArrayHelper;
/**
* CollectionController implements the CRUD actions for Collection model.
@ -64,15 +67,27 @@ class CollectionController extends Controller
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
public function actionCreate($id_account)
{
$model = new CollectionCreate();
$user = User::findOne(Yii::$app->user->id);
// $accounts = Account::find()->orderBy("name asc")->all();
$model->accounts = Account::read();
$model->lastCollection = Collection::readLast($user);
$model->id_user = $user->id;
$model->id_account = Account::readDefault();
$model->account = Account::readOne($model->id_account);
$accountMap = ArrayHelper::map( $model->accounts ,'id_account','name' );
$model->start = isset($model->lastCollection) ? $model->lastCollection->end :null;
$model->end = date("Y-m-d H:i:s");
$model->totals = Transfer::mkTotals($model->start, $model->end, $model->user->id, null, $model->account->id_account, $model->accounts, $accountMap);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id_collection]);
} else {
@ -81,6 +96,26 @@ class CollectionController extends Controller
]);
}
}
/**
* Select account for which we will create collection
* If selection is successful, the browser will be redirected to the 'create' page.
* @return mixed
*/
public function actionSelectAccount()
{
$model = new CreateAccountSelectForm();
$model->accounts = Account::read();
$model->id_account = Account::readDefault();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
return $this->redirect(['create', 'id_account' => $model->id_account]);
}
return $this->render('_create_select_account.php', [
'model' => $model,
]);
}
/**
* Updates an existing Collection model.

View File

@ -0,0 +1,57 @@
<?php
namespace frontend\models;
use Yii;
use yii\base\Model;
use common\models\Card;
use common\models\Customer;
use common\models\Product;
use common\models\Transfer;
use yii\base\Object;
use common\models\Account;
use common\models\Discount;
use common\models\Currency;
use common\models\UserSoldItem;
use common\models\Sale;
use common\models\ShoppingCart;
/**
* ContactForm is the model behind the contact form.
*
* @property integer id_account
* @property common\models\Account $account
*
*
*/
class CreateAccountSelectForm extends Model
{
public $id_account;
public $account;
public $accounts;
public function rules(){
return [
[['id_account'], 'integer' ],
[['id_account'], 'validateAccount' ],
];
}
public function validateAccount($attribute,$params){
$this->account = Account::readOne($this->id_account);
if ( !isset($this->account)){
$this->addError($attribute, Yii::t('frontend/collection', 'Invalid transfer!'));
}
}
public function attributeLabels(){
return [
'id_account' => Yii::t('frontend/collection', 'Account'),
];
}
}

View File

@ -199,7 +199,6 @@ class TransferSearch extends Transfer
$totals['accounts'] = [];
foreach ($this->accounts as $account){
// echo 'account="'. $this->id_account .'"<br>';
if ( isset($this->id_account) && is_numeric($this->id_account) && $this->id_account != $account->id_account ){
continue ;
}
@ -227,7 +226,6 @@ class TransferSearch extends Transfer
$command = $query->createCommand();
$result = $command->queryAll();
// $paidAtTotals = $this->mkTotalsResult($result, $accountMap);
$paidAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap);
return $paidAtTotals;

View File

@ -0,0 +1,50 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use frontend\components\HtmlHelper;
/* @var $this yii\web\View */
/* @var $model common\models\Collection */
$this->title = Yii::t('frontend/collection', 'Create Collection - Select Account');
$this->params['breadcrumbs'][] = ['label' => Yii::t('frontend/collection', 'Collections'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
$accountOptions = HtmlHelper::mkAccountOptions($model->accounts);
?>
<div class="collection-create select-account">
<h1><?= Html::encode($this->title) ?></h1>
<div class="collection-form">
<?php $form = ActiveForm::begin(
[
'method' => 'post',
'action' => ['collection/select-account'],
]
); ?>
<div class="row">
<div class="col-md-4">
<?= $form->field($model, 'id_account')->dropDownList($accountOptions) ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton( Yii::t('frontend/collection', 'Next') ,['class' =>'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>

View File

@ -2,26 +2,23 @@
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use frontend\components\HtmlHelper;
/* @var $this yii\web\View */
/* @var $model common\models\Collection */
/* @var $form yii\widgets\ActiveForm */
?>
<?php
?>
<div class="collection-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'id_account')->textInput() ?>
<?= $form->field($model, 'money')->textInput() ?>
<?= $form->field($model, 'start')->textInput() ?>
<?= $form->field($model, 'end')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('frontend/collection', 'Create') : Yii::t('frontend/collection', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

View File

@ -1,6 +1,7 @@
<?php
use yii\helpers\Html;
use frontend\components\TransferTotalWidget;
/* @var $this yii\web\View */
@ -15,13 +16,41 @@ $this->params['breadcrumbs'][] = $this->title;
<h1><?= Html::encode($this->title) ?></h1>
<dl class='dl-horizontal'>
<dt>Utolsó zárás</dt>
<dd>asdf</dd>
<dt></dt>
<dd></dd>
<dt></dt>
<dd></dd>
</dl>
<dt><?= Yii::t('frontend/collection', "Last collection")?></dt>
<dd><?php echo isset($model->lastCollection) ? Yii::$app->formatter->asDatetime($model->lastCollection->end) : Yii::t('frontend/collection', "No collection found") ?></dd>
<dt><?= Yii::t('frontend/collection', "Start of interval")?></dt>
<dd><?php echo Yii::$app->formatter->asDatetime($model->start) ?></dd>
<dt><?= Yii::t('frontend/collection', "End of interval")?></dt>
<dd><?php echo Yii::$app->formatter->asDatetime($model->end) ?></dd>
</dl>
<?php
echo TransferTotalWidget::widget([
'options' => [
'statistic' => $model->totals['paid_at'],
'panelHeading' => 'Fizetve' ,
]
]);
echo TransferTotalWidget::widget([
'options' => [
'statistic' => $model->totals['created_at'],
'panelHeading' => 'Kiadva',
]
]);
echo TransferTotalWidget::widget([
'options' => [
'statistic' => $model->totals['created_at_paid'],
'panelHeading' => 'Kiadva és fizetve',
]
]);
?>
<?= $this->render('_form', [
'model' => $model,

View File

@ -16,7 +16,7 @@ $this->params['breadcrumbs'][] = $this->title;
<?php echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a(Yii::t('frontend/collection', 'Create Collection'), ['create'], ['class' => 'btn btn-success']) ?>
<?= Html::a(Yii::t('frontend/collection', 'Create Collection'), ['select-account'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([

View File

@ -0,0 +1,21 @@
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-info">
<div class="panel-heading"><?php echo $options['panelHeading']?></div>
<div class="panel-body">
<dl class="dl-horizontal dl-totals">
<?php
foreach ($options['statistic']['accounts'] as $acc ){
?>
<dt><?php echo $acc['label'] ?></dt>
<dd class="text-right"><?php echo $acc['money'] ?></dd>
<?php
}
?>
<dt><?php echo $options['totalHeading']?></dt>
<dd class="text-right"><?php echo $options['statistic']['total'] ?></dd>
</dl>
</div>
</div>
</div>
</div>