add customer cart

This commit is contained in:
2015-10-21 17:57:16 +02:00
parent baf7c79d04
commit 23a0390a27
14 changed files with 467 additions and 16 deletions

143
common/models/Sale.php Normal file
View File

@@ -0,0 +1,143 @@
<?php
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use common\components\AccountAwareBehavior;
use common\components\UserAwareBehavior;
use common\components\DiscountAwareBehavior;
use yii\helpers\ArrayHelper;
use common\components\ProductAwareBehavior;
use common\components\CurrencyAwareBehavior;
/**
* This is the model class for table "sale".
*
* @property integer $id_sale
* @property integer $id_account
* @property integer $id_discount
* @property integer $id_currency
* @property integer $id_product
* @property integer $status
* @property integer $type
* @property integer $item_price
* @property integer $count
* @property integer $money
* @property integer $money_currency
* @property integer $rate
* @property integer $id_user
* @property string $comment
* @property string $created_at
* @property string $updated_at
*/
class Sale extends \yii\db\ActiveRecord
{
const TYPE_PRODUCT = 10;
const STATUS_NOT_PAID = 10;
const STATUS_PAID = 20;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'sale';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
];
}
/**
* @inheritdoc
*/
public function behaviors()
{
return ArrayHelper::merge( [
[
'class' => TimestampBehavior::className(),
'value' => function(){ return date('Y-m-d H:i:s' ); }
],
[
'class' => AccountAwareBehavior::className(),
],
[
'class' => UserAwareBehavior::className(),
],
[
'class' => DiscountAwareBehavior::className(),
],
[
'class' => ProductAwareBehavior::className(),
],
[
'class' => CurrencyAwareBehavior::className(),
],
], parent::behaviors());
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id_sale' => Yii::t('common/sale', 'Id Sale'),
'id_discount' => Yii::t('common/sale', 'Id Discount'),
'id_currency' => Yii::t('common/sale', 'Id Currency'),
'id_product' => Yii::t('common/sale', 'Id Product'),
'status' => Yii::t('common/sale', 'Status'),
'type' => Yii::t('common/sale', 'Type'),
'item_price' => Yii::t('common/sale', 'Item Price'),
'count' => Yii::t('common/sale', 'Count'),
'money' => Yii::t('common/sale', 'Money'),
'money_currency' => Yii::t('common/sale', 'Money Currency'),
'rate' => Yii::t('common/sale', 'Rate'),
'id_user' => Yii::t('common/sale', 'Id User'),
'comment' => Yii::t('common/sale', 'Comment'),
'created_at' => Yii::t('common/sale', 'Created At'),
'updated_at' => Yii::t('common/sale', 'Updated At'),
];
}
public static function createSale($account, $discount, $currency, $count,$product , $status = Sale::STATUS_PAID){
$sale = new Sale();
$sale->type = Sale::TYPE_PRODUCT;
$sale->id_product = $product->id_product;
$sale->item_price = $product->sale_price;
$totalPrice = $sale->item_price;
$sale->count = $count;
$totalPrice = $totalPrice * $count;
if ( isset( $discount ) ){
$sale->id_discount = $discount->id_discount;
$totalPrice = Discount::applyDiscount( $totalPrice, $discount);
}
$sale->money = $totalPrice;
if ( isset( $currency ) ){
$sale->rate = $currency->rate;
$sale->money_currency = Currency::applyCurrency($totalPrice, $currency);
}
$sale->status = $status;
$sale->id_account = $account->id_account;
return $sale;
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "shopping_cart".
*
* @property integer $id_shopping_cart
* @property integer $id_customer
* @property integer $id_sale
*/
class ShoppingCart extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'shopping_cart';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id_customer', 'id_sale'], 'integer']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id_shopping_cart' => Yii::t('common/shopping-cart', 'Id Shopping Cart'),
'id_customer' => Yii::t('common/shopping-cart', 'Id Customer'),
'id_sale' => Yii::t('common/shopping-cart', 'Id Sale'),
];
}
public function getSale(){
$this->hasOne(Sale::className(), ['id_sale' => 'id_sale']);
}
public function getProduct(){
$this->hasOne(Product::className(), ['id_product' => 'id_product'])->via('sale');
}
/**
* @param $customer common\models\Customer
* */
public static function readCustomerCart($customer = null){
$sales = [];
if ( isset($customer)){
$query = ShoppingCart::find()->andWhere( ['id_customer' => $customer->id_customer] );
$query->with('product');
$sales = $query->all();
}
return $sales;
}
}

View File

@@ -114,7 +114,7 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
}
public function getProduct(){
return $this->hasOne( Product::className(), ["id_product" =>"id_object" ] );
return $this->hasOne( Product::className(), ["id_product" =>"id_product" ] )->via('sale');
}
public function getMoneyMovement(){
return $this->hasOne( MoneyMovement::className(), ["id_money_movement" =>"id_object" ] );
@@ -144,6 +144,10 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
return $this->hasOne( UserSoldItem::className(), ["id_transfer" =>"id_transfer" ] );
}
public function getSale(){
return $this->hasOne( Sale::className(), ["id_sale" =>"id_object" ] ) ;
}
public function getObjectName(){
$result = "";
if ( $this->type == Transfer::TYPE_TICKET ){
@@ -206,6 +210,18 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
return $result;
}
public function getSaleName(){
$result = "";
$sale = $this->sale;
if (isset($sale)){
$result = $sale->name;
}
return $result;
}
public function getSignedMoney(){
$m = 1;
$result = $this->money;
@@ -237,12 +253,12 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
* @param $currency common\models\Currency
* @param $product common\models\Product
* */
public static function createProductTransfer($account, $discount, $currency, $count,$product , $status = Transfer::STATUS_PAID){
public static function createProductTransfer($sale,$account, $discount, $currency, $count,$product , $status = Transfer::STATUS_PAID){
$transfer = new Transfer();
$transfer->type = Transfer::TYPE_PRODUCT;
$transfer->id_object = $product->id_product;
$transfer->id_object = $sale->id_sale;
$transfer->item_price = $product->sale_price;
$totalPrice = $transfer->item_price;