add frontend changes

This commit is contained in:
2015-10-30 09:28:58 +01:00
parent 4a04c9efa2
commit e34b150d74
41 changed files with 1083 additions and 254 deletions

View File

@@ -38,5 +38,13 @@ class AccountSelect extends Model
Account::writeDefault($account);
return true;
}
/* */
public function attributeLabels(){
return [
'id_account' =>Yii::t('frontend/account', 'Account')
];
}
}

View File

@@ -6,6 +6,7 @@ use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\MoneyMovement;
use yii\helpers\ArrayHelper;
/**
* MoneyMovementSearch represents the model behind the search form about `common\models\MoneyMovement`.
@@ -69,4 +70,13 @@ class MoneyMovementSearch extends MoneyMovement
return $dataProvider;
}
public function attributeLabels( ) {
return ArrayHelper::merge(parent::attributeLabels(),
[
'start' => Yii::t('frontend/money-movement', 'Start'),
'end' => Yii::t('frontend/money-movement', 'End'),
]
);
}
}

View File

@@ -184,14 +184,16 @@ class ProductSaleForm extends Model
}
protected function saveTransfer(){
$customer = null;
$status = Transfer::STATUS_PAID;
if ( $this->isAppendToUserCart() ){
$status = Transfer::STATUS_NOT_PAID;
}else if ( $this->isAppendToCustomerCart() ){
$status = Transfer::STATUS_NOT_PAID;
$customer = $this->customer;
}
$this->transfer = Transfer::createProductTransfer($this->sale,$this->account, $this->discount, $this->currency, $this->count, $this->product,$status);
$this->transfer = Transfer::createProductTransfer($this->sale,$this->account, $this->discount, $this->currency, $this->count, $this->product,$status,$customer);
if ( isset($this->comment)){
$this->transfer->comment = $this->comment;
}
@@ -229,8 +231,6 @@ class ProductSaleForm extends Model
}
public function appendToCustomerCart(){
// print_r("cart: ".$this->cart );
// print_r("customer:: ".$this->customer );
if ( $this->isAppendToCustomerCart() && isset($this->customer) ){
$item = new ShoppingCart();
$item->id_customer = $this->customer->id_customer;

View File

@@ -7,8 +7,16 @@ use common\models\Account;
use common\models\Discount;
use common\models\Transfer;
use common\models\UserSoldItem;
use common\models\ShoppingCart;
use yii\base\Object;
/**
* @property $cart string name of cart, into we put the ticket
* @property $userCart common\models\Transfer[] items in user cart
* @property $customerCart common\models\Transfer[] items in customer cart
* @property $customer common\models\Customer selected customer
*
* */
class TicketCreate extends Ticket{
public $_currency;
@@ -16,6 +24,12 @@ class TicketCreate extends Ticket{
public $_discount;
public $_transfer;
public $customer;
public $userCart;
public $customerCart;
public $cart;
public function rules()
@@ -88,14 +102,15 @@ class TicketCreate extends Ticket{
public function afterSave($insert, $changedAttributes){
$this->addTransfer();
$this->addToCart();
$this->appendToUserCart();
$this->appendToCustomerCart();
}
protected function addTransfer(){
$transfer = Transfer::createTicketTransfer($this->_account, $this->_discount, null, 1, $this);
$transfer->status = Transfer::STATUS_PAID;
// $transfer->status = Transfer::STATUS_PAID;
if ( isset($this->comment)){
$transfer->comment = $this->comment;
}
@@ -114,12 +129,41 @@ class TicketCreate extends Ticket{
}
public function isAddToCart(){
$result = false;
if ( isset( $this->cart ) && $this->cart == 'add' ){
$result = true;
}
return $result;
}
public function isAppendToUserCart(){
$result = false;
if ( isset( $this->cart ) && $this->cart == 'user' ){
$result = true;
}
return $result;
}
public function isAppendToCustomerCart(){
$result = false;
if ( isset( $this->cart ) && $this->cart == 'customer' ){
$result = true;
}
return $result;
}
public function appendToUserCart(){
if ( $this->isAppendToUserCart() ){
$item = new UserSoldItem();
$item->id_transfer = $this->_transfer->id_transfer;
$item->id_user = \Yii::$app->user->id;
$item->save(false);
}
}
public function appendToCustomerCart(){
if ( $this->isAppendToCustomerCart() && isset($this->customer) ){
$item = new ShoppingCart();
$item->id_customer = $this->customer->id_customer;
$item->id_transfer = $this->_transfer->id_transfer;
$item->save(false);
}
}
}

View File

@@ -6,31 +6,56 @@ 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;
/**
* TransferSearch represents the model behind the search form about `common\models\Transfer`.
*/
class TransferSearch 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 $accounts;
public $types;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id_transfer', 'id_discount', 'id_currency', 'id_object', 'status', 'type', 'item_price', 'count', 'money', 'money_currency', 'rate', 'id_user'], 'integer'],
[['comment', 'created_at', 'updated_at'], 'safe'],
// [[ 'start', ], 'date' , 'timestampAttribute' => 'timestampStart' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
// [[ 'end' , ], 'date' , 'timestampAttribute' => 'timestampEnd' ,'timestampAttributeFormat' => 'yyyy-MM-dd' ],
[[ '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' ] , 'integer'],
['types', 'each', 'rule' => ['integer']],
// [ [ 'types' ], function ($attribute, $params) {
// if (!is_array($this->$attribute)) {
// $this->addError($attribute, 'Invalid array');
// }
// }],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
@@ -56,24 +81,208 @@ class TransferSearch extends Transfer
}
$query->andFilterWhere([
'id_transfer' => $this->id_transfer,
'id_discount' => $this->id_discount,
'id_currency' => $this->id_currency,
'id_object' => $this->id_object,
'status' => $this->status,
'id_account' => $this->id_account,
'type' => $this->type,
'item_price' => $this->item_price,
'count' => $this->count,
'money' => $this->money,
'money_currency' => $this->money_currency,
'rate' => $this->rate,
'id_user' => $this->id_user,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['in' ,'type', $this->types]);
$query->andFilterWhere(['like', 'comment', $this->comment]);
$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]);
return $dataProvider;
}
protected function mkTotalQuery($mode){
$query = new Query();
$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' )
]);
$query->from('transfer');
$query->andFilterWhere([
'id_account' => $this->id_account,
'type' => $this->type,
]);
$query->andWhere(['id_user' => Yii::$app->user->id]);
$query->andFilterWhere(['in' ,'type', $this->types]);
if ( $mode == 'created_at'){
$query->andFilterWhere([ '>=', 'transfer.created_at' , $this->timestampStart ] );
$query->andFilterWhere([ '<' , 'transfer.created_at' , $this->timestampEnd ] );
}else if ( $mode == 'paid_at'){
$query->andFilterWhere([ '>=', 'transfer.paid_at' , $this->timestampStart ] );
$query->andFilterWhere([ '<' , 'transfer.paid_at' , $this->timestampEnd ] );
}else if ( $mode == 'created_at_not_paid'){
$query->andFilterWhere([ "transfer.status" => Transfer::STATUS_NOT_PAID ] );
$query->andFilterWhere([ '>=', 'transfer.created_at' , $this->timestampStart ] );
$query->andFilterWhere([ '<' , 'transfer.created_at' , $this->timestampEnd ] );
}else if ( $mode == 'created_at_paid'){
$query->andFilterWhere([ "transfer.status" => Transfer::STATUS_PAID ] );
$query->andFilterWhere([ '>=', 'transfer.created_at' , $this->timestampStart ] );
$query->andFilterWhere([ '<' , 'transfer.created_at' , $this->timestampEnd ] );
}
$query->groupBy('transfer.id_account');
return $query;
}
protected function mkTransferTotalsWithZeroValue( ){
$result = [];
foreach ( $this->accounts as $a ){
$accountTotal = [
'id_account' => $a->id_account,
'label' => $a->name,
'money' => 0
];
$result[] = $accountTotal;
}
return $result;
}
protected function mkTotalsResult($queryResult,$accountMap){
$totals = [];
$totals['total'] = 0;
$totals['accounts'] = [];
foreach ($queryResult as $item){
$account = "";
if ( array_key_exists($item['account'], $accountMap)){
$account = $accountMap[$item['account']];
}
$accountTotal = [
'id_account' => $item['account'],
'label' => $account,
'money' => $item['money']
];
$totals['accounts'][] = $accountTotal;
$totals['total'] += $item['money'];
}
return $totals;
}
protected function findByAccountInQueryResult($queryResult, $account){
$result = null;
foreach ($queryResult as $item){
if( $item['account'] == $account->id_account ){
$result = $item;
}
}
return $result;
}
protected function mkTotalsResultWithAllAvailableAccount($queryResult,$accountMap){
$totals = [];
$totals['total'] = 0;
$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 ;
}
$accountTotal = [
'id_account' => $account->id_account,
'label' => $account->name,
'money' => 0,
];
$item = $this->findByAccountInQueryResult($queryResult, $account);
if ( isset($item)){
$accountTotal['money'] = $item['money'];
}
$totals['accounts'][] = $accountTotal;
$totals['total'] += $accountTotal['money'];
}
return $totals;
}
public function mkPaidAtTotals($accountMap){
$query = $this->mkTotalQuery('paid_at');
$command = $query->createCommand();
$result = $command->queryAll();
// $paidAtTotals = $this->mkTotalsResult($result, $accountMap);
$paidAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap);
return $paidAtTotals;
}
public function mkCreatedAtTotals($accountMap){
$query = $this->mkTotalQuery('created_at');
$command = $query->createCommand();
$result = $command->queryAll();
$createdAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap);
return $createdAtTotals;
}
public function mkCreatedAtNotPaidTotals($accountMap){
$query = $this->mkTotalQuery('created_at_not_paid');
$command = $query->createCommand();
$result = $command->queryAll();
$createdAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap);
return $createdAtTotals;
}
public function mkCreatedAtPaidTotals($accountMap){
$query = $this->mkTotalQuery('created_at_paid');
$command = $query->createCommand();
$result = $command->queryAll();
$createdAtTotals = $this->mkTotalsResultWithAllAvailableAccount($result, $accountMap);
return $createdAtTotals;
}
public function totalsTransfers($params){
$accountTotals = [];
$fullTotal = [
'label' => Yii::t('common/transfer', 'Total'),
'money' => 0
];
if ( $this->hasErrors() ){
return;
}
$accounts = Account::find()->orderBy("name asc")->all();
$accountMap = ArrayHelper::map( $accounts ,'id_account','name' );
$this->totalsCreatedAt = $this->mkCreatedAtTotals($accountMap);
$this->totalsPaidAt = $this->mkPaidAtTotals($accountMap);
$this->totalsCreatedAtPaid = $this->mkCreatedAtPaidTotals($accountMap);
$this->totalsCreatedAtNotPaid = $this->mkCreatedAtNotPaidTotals($accountMap);
}
}