add customer cart

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

View File

@ -0,0 +1,24 @@
<?php
namespace common\components;
use yii\base\Behavior;
use common\models\Currency;
class CurrencyAwareBehavior extends Behavior{
public function getCurrencyName(){
$result = "";
$currency = $this->owner->currency;
if (isset($currency)){
$result = $currency->name;
}
return $result;
}
public function getCurrency(){
return $this->owner->hasOne( Currency::className(), ["id_currency" =>"id_currency" ] ) ;
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace common\components;
use yii\base\Behavior;
use common\models\Account;
use common\models\Discount;
class DiscountAwareBehavior extends Behavior{
public function getDiscountName(){
$result = "";
$discount = $this->owner->discount;
if (isset($discount)){
$result = $discount->name;
}
return $result;
}
public function getDiscount(){
return $this->owner->hasOne( Discount::className(), ["id_discount" =>"id_discount" ] ) ;
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace common\components;
use yii\base\Behavior;
use common\models\Account;
use common\models\Product;
class ProductAwareBehavior extends Behavior{
/** @var $product common\models\Product*/
public function getProductName(){
$result = "";
$product = $this->owner->product;
if (isset($product)){
$result = $product->name;
}
return $result;
}
public function getProduct(){
return $this->owner->hasOne( Product::className(), ["id_product" =>"id_product" ] ) ;
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace common\components;
use yii\base\Behavior;
use common\models\Sale;
class SaleAwareBehavior extends Behavior{
public function getSaleName(){
$result = "";
$sale = $this->owner->sale;
if (isset($sale)){
$result = $sale->name;
}
return $result;
}
public function getSale(){
return $this->owner->hasOne( Sale::className(), ["id_sale" =>"id_sale" ] ) ;
}
}

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(){ 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(){ public function getMoneyMovement(){
return $this->hasOne( MoneyMovement::className(), ["id_money_movement" =>"id_object" ] ); 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" ] ); return $this->hasOne( UserSoldItem::className(), ["id_transfer" =>"id_transfer" ] );
} }
public function getSale(){
return $this->hasOne( Sale::className(), ["id_sale" =>"id_object" ] ) ;
}
public function getObjectName(){ public function getObjectName(){
$result = ""; $result = "";
if ( $this->type == Transfer::TYPE_TICKET ){ if ( $this->type == Transfer::TYPE_TICKET ){
@ -206,6 +210,18 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
return $result; return $result;
} }
public function getSaleName(){
$result = "";
$sale = $this->sale;
if (isset($sale)){
$result = $sale->name;
}
return $result;
}
public function getSignedMoney(){ public function getSignedMoney(){
$m = 1; $m = 1;
$result = $this->money; $result = $this->money;
@ -237,12 +253,12 @@ class Transfer extends \common\models\BaseFitnessActiveRecord
* @param $currency common\models\Currency * @param $currency common\models\Currency
* @param $product common\models\Product * @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 = new Transfer();
$transfer->type = Transfer::TYPE_PRODUCT; $transfer->type = Transfer::TYPE_PRODUCT;
$transfer->id_object = $product->id_product; $transfer->id_object = $sale->id_sale;
$transfer->item_price = $product->sale_price; $transfer->item_price = $product->sale_price;
$totalPrice = $transfer->item_price; $totalPrice = $transfer->item_price;

View File

@ -0,0 +1,30 @@
<?php
use yii\db\Schema;
use yii\db\Migration;
class m151021_091320_alter__table__sale__add__id_account extends Migration
{
public function up()
{
$this->addColumn("sale", "id_account", "integer(11)");
}
public function down()
{
echo "m151021_091320_alter__table__sale__add__id_account cannot be reverted.\n";
return false;
}
/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}

View File

@ -20,6 +20,7 @@ use common\models\Discount;
use common\models\Transfer; use common\models\Transfer;
use common\models\User; use common\models\User;
use common\models\UserSoldItem; use common\models\UserSoldItem;
use common\models\ShoppingCart;
/** /**
* ProductController implements the CRUD actions for Product model. * ProductController implements the CRUD actions for Product model.
@ -71,6 +72,8 @@ class ProductController extends Controller
$user = User::findOne(Yii::$app->user->id ); $user = User::findOne(Yii::$app->user->id );
$model->customer = $this->customer;
if (Yii::$app->request->isAjax) { if (Yii::$app->request->isAjax) {
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
@ -103,6 +106,7 @@ class ProductController extends Controller
// $userTransfers = Transfer::readUserSoldTransfers( $user ); // $userTransfers = Transfer::readUserSoldTransfers( $user );
$userTransfers = Transfer::modelsToArray( Transfer::readUserSoldTransfers($user) ); $userTransfers = Transfer::modelsToArray( Transfer::readUserSoldTransfers($user) );
$model->customerCart = ShoppingCart::readCustomerCart( $this->customer );
return $this->render("sale",[ return $this->render("sale",[
'customer' => $this->customer, 'customer' => $this->customer,
@ -112,7 +116,7 @@ class ProductController extends Controller
'currencies' => $currencies, 'currencies' => $currencies,
'accounts' => $accounts, 'accounts' => $accounts,
'discounts' => $discounts, 'discounts' => $discounts,
'userTransfers' => $userTransfers 'userTransfers' => $userTransfers,
]); ]);
} }

View File

@ -13,6 +13,8 @@ use common\models\Account;
use common\models\Discount; use common\models\Discount;
use common\models\Currency; use common\models\Currency;
use common\models\UserSoldItem; use common\models\UserSoldItem;
use common\models\Sale;
use common\models\ShoppingCart;
/** /**
* ContactForm is the model behind the contact form. * ContactForm is the model behind the contact form.
@ -42,8 +44,12 @@ class ProductSaleForm extends Model
public $account; public $account;
public $currency; public $currency;
public $discount; public $discount;
public $customer;
public $transfer; public $transfer;
public $sale;
public $customerCart;
/** /**
* @inheritdoc * @inheritdoc
@ -121,21 +127,32 @@ class ProductSaleForm extends Model
public function save(){ public function save(){
if ( $this->validate() ){ if ( $this->validate() ){
$this->saveSale();
$this->saveTransfer(); $this->saveTransfer();
$this->saveProduct(); $this->saveProduct();
$this->appendToSoldList(); $this->appendToUserCart();
$this->appendToCustomerCart();
return true; return true;
} }
return false; return false;
} }
// protected function applyDiscount(){
// Discount::applyDiscount($this->, $discount); protected function saveSale(){
// } $this->sale = Sale::createSale($this->account, $this->discount, $this->currency, $this->count, $this->product);
/*
*/
$this->sale->status = Sale::STATUS_PAID;
if ( isset($this->comment)){
$this->sale->comment = $this->comment;
}
$this->sale->id_user = Yii::$app->user->id;
$this->sale->save();
}
protected function saveTransfer(){ protected function saveTransfer(){
$this->transfer = Transfer::createProductTransfer($this->account, $this->discount, $this->currency, $this->count, $this->product); $this->transfer = Transfer::createProductTransfer($this->sale,$this->account, $this->discount, $this->currency, $this->count, $this->product);
/* /*
*/ */
$this->transfer->status = Transfer::STATUS_PAID; $this->transfer->status = Transfer::STATUS_PAID;
@ -151,7 +168,7 @@ class ProductSaleForm extends Model
$this->product->save(); $this->product->save();
} }
public function isAppendToList(){ public function isAppendToUserCart(){
$result = false; $result = false;
if ( isset( $this->append_to_sold_list ) && $this->append_to_sold_list == 'append' ){ if ( isset( $this->append_to_sold_list ) && $this->append_to_sold_list == 'append' ){
$result = true; $result = true;
@ -159,12 +176,28 @@ class ProductSaleForm extends Model
return $result; return $result;
} }
public function appendToSoldList(){ public function appendToUserCart(){
if ( $this->isAppendToList() ){ if ( $this->isAppendToUserCart() ){
$item = new UserSoldItem(); $item = new UserSoldItem();
$item->id_transfer = $this->transfer->id_transfer; $item->id_transfer = $this->transfer->id_transfer;
$item->id_user = Yii::$app->user->id; $item->id_user = Yii::$app->user->id;
$item->save(false); $item->save(false);
} }
} }
public function isAppendToCustomerCart(){
$result = false;
if ( isset( $this->add_to_customer ) && $this->add_to_customer == 'append' ){
$result = true;
}
return $result;
}
public function appendToCustomerCart(){
if ( $this->isAppendToCustomerCart() && isset($this->customer) ){
$item = new ShoppingCart();
$item->id_customer = $this->customer->id_customer;
$item->id_sale = $this->sale->id_sale;
$item->save(false);
}
}
} }

View File

@ -0,0 +1,36 @@
<?php
use yii\bootstrap\Html;
?>
<h1><?php echo Yii::t('frontend/product','Customer cart')?></h1>
<div class="row">
<div class='col-md-12 '>
<div class="customer-cart">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>
<?php echo Yii::t('frontend/product', 'Product name')?>
</th>
<th>
<?php echo Yii::t('frontend/product', 'Count')?>
</th>
<th>
<?php echo Yii::t('frontend/product', 'Currency')?>
</th>
<th>
<?php echo Yii::t('frontend/product', 'Item Price')?>
</th>
<th>
<?php echo Yii::t('frontend/product', 'Total Price')?>
</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
<?php echo Html::a(Yii::t('frontend/product', "Fizetve"),null,[ 'id' => 'btn_paid', 'class' => 'btn btn-primary' ]) ?>

View File

@ -97,11 +97,14 @@ $discountOptions = mkOptions( ArrayHelper::map($discounts, 'id_discount', 'name'
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class='col-md-5'> <div class='col-md-4'>
<?php echo Html::a(Yii::t("frontend/product","Sell product"),null,['class' => 'btn btn-success', 'id' => 'btn_sell'] );?> <?php echo Html::a(Yii::t("frontend/product","Sell product"),null,['class' => 'btn btn-success', 'id' => 'btn_sell'] );?>
</div> </div>
<div class='col-md-7'> <div class='col-md-4'>
<?php echo Html::a(Yii::t("frontend/product","Sell product and add to total"),null,['class' => 'btn btn-success', 'id' => 'btn_sell_append'] );?> <?php echo Html::a(Yii::t("frontend/product","To cart"),null,['class' => 'btn btn-success', 'id' => 'btn_sell_append'] );?>
</div>
<div class='col-md-4'>
<?php echo Html::a(Yii::t("frontend/product","Write up"),null,['class' => 'btn btn-success', 'id' => 'btn_add_to_customer_cart'] );?>
</div> </div>
</div> </div>
<?php ActiveForm::end(); ?> <?php ActiveForm::end(); ?>

View File

@ -74,11 +74,17 @@ $this->registerJs ( 'new ProductSell( '. json_encode($options).');' );
<div class='row '> <div class='row '>
<div class='col-md-6 product-form'> <div class='col-md-6 product-form'>
<h1><?php echo Yii::t('frontend/product','Sell product')?></h1> <h1><?php echo Yii::t('frontend/product','Sell product')?></h1>
<?php echo $this->render('_sale_form' ,['model' =>$model , 'lookupModel' =>$lookupModel, 'currencies' => $currencies, 'accounts' => $accounts,'discounts' => $discounts,]) ?> <?php echo $this->render('_sale_form' ,['model' =>$model , 'lookupModel' =>$lookupModel, 'currencies' => $currencies, 'accounts' => $accounts,'discounts' => $discounts ]) ?>
</div> </div>
<div class='col-md-6'> <div class='col-md-6'>
<?php echo $this->render('_customer_cart' ) ?>
<h1><?php echo Yii::t('frontend/product','Cart')?></h1> <h1><?php echo Yii::t('frontend/product','Cart')?></h1>
<?php echo $this->render('_sold_items' ) ?> <?php echo $this->render('_sold_items' ) ?>
<?php echo Html::a(Yii::t('frontend/product', "Paid"),null,[ 'id' => 'btn_paid', 'class' => 'btn btn-primary' ]) ?> <?php echo Html::a(Yii::t('frontend/product', "Paid"),null,[ 'id' => 'btn_paid', 'class' => 'btn btn-primary' ]) ?>
</div> </div>
</div> </div>

View File

@ -20,6 +20,7 @@ function ProductSell(o){
/**list of sold items by current user*/ /**list of sold items by current user*/
sold_items: [], sold_items: [],
discounts: [], discounts: [],
customer_cart: []
}; };
init(); init();
@ -37,6 +38,7 @@ function ProductSell(o){
addBehaviorCountChangedListener(); addBehaviorCountChangedListener();
addBehaviourDisocuntChanged(); addBehaviourDisocuntChanged();
createUserSoldItemsTable(); createUserSoldItemsTable();
createCustomerCartTable();
addBehaviourBtnPaid(); addBehaviourBtnPaid();
productChanged(); productChanged();
addDocumentKeypressedListener(); addDocumentKeypressedListener();
@ -269,6 +271,10 @@ function ProductSell(o){
function addBehaviourBtnSellAndAppendToBill(){ function addBehaviourBtnSellAndAppendToBill(){
$('#btn_sell_append').on('click',submitSellAndAppend); $('#btn_sell_append').on('click',submitSellAndAppend);
} }
function addBehaviourBtnAddToCustomerCart(){
$('#btn_add_to_customer_cart').on('click',submitAddToCustomerCart);
}
function submitSell(){ function submitSell(){
$('#productsaleform-append_to_sold_list').val(''); $('#productsaleform-append_to_sold_list').val('');
@ -280,6 +286,11 @@ function ProductSell(o){
} }
function submitAddToCustomerCart(){
$('#productsaleform-append_to_sold_list').val('append');
$('#product_form').submit();
}
function refreshSoldUseritems(){ function refreshSoldUseritems(){
$('.sold-items-container').transferList('option','transfers' , app.defaults.sold_items ); $('.sold-items-container').transferList('option','transfers' , app.defaults.sold_items );
} }
@ -364,6 +375,11 @@ function ProductSell(o){
'transfers' : app.defaults.sold_items 'transfers' : app.defaults.sold_items
}); });
} }
function createCustomerCartTable(){
$('.customer-cart').transferList({
'transfers' : app.defaults.customer_cart
});
}
function findDiscount(id_discount){ function findDiscount(id_discount){
var discount; var discount;