From 23a0390a27661a743831728ab7ea243f51c0b4f8 Mon Sep 17 00:00:00 2001 From: rocho Date: Wed, 21 Oct 2015 17:57:16 +0200 Subject: [PATCH] add customer cart --- common/components/CurrencyAwareBehavior.php | 24 +++ common/components/DiscountAwareBehavior.php | 25 +++ common/components/ProductAwareBehavior.php | 25 +++ common/components/SaleAwareBehavior.php | 24 +++ common/models/Sale.php | 143 ++++++++++++++++++ common/models/ShoppingCart.php | 66 ++++++++ common/models/Transfer.php | 22 ++- ...20_alter__table__sale__add__id_account.php | 30 ++++ frontend/controllers/ProductController.php | 6 +- frontend/models/ProductSaleForm.php | 49 +++++- frontend/views/product/_customer_cart.php | 36 +++++ frontend/views/product/_sale_form.php | 9 +- frontend/views/product/sale.php | 8 +- frontend/web/js/product.sell.js | 16 ++ 14 files changed, 467 insertions(+), 16 deletions(-) create mode 100644 common/components/CurrencyAwareBehavior.php create mode 100644 common/components/DiscountAwareBehavior.php create mode 100644 common/components/ProductAwareBehavior.php create mode 100644 common/components/SaleAwareBehavior.php create mode 100644 common/models/Sale.php create mode 100644 common/models/ShoppingCart.php create mode 100644 console/migrations/m151021_091320_alter__table__sale__add__id_account.php create mode 100644 frontend/views/product/_customer_cart.php diff --git a/common/components/CurrencyAwareBehavior.php b/common/components/CurrencyAwareBehavior.php new file mode 100644 index 0000000..18f9380 --- /dev/null +++ b/common/components/CurrencyAwareBehavior.php @@ -0,0 +1,24 @@ +owner->currency; + if (isset($currency)){ + $result = $currency->name; + } + return $result; + } + + + public function getCurrency(){ + return $this->owner->hasOne( Currency::className(), ["id_currency" =>"id_currency" ] ) ; + } + +} \ No newline at end of file diff --git a/common/components/DiscountAwareBehavior.php b/common/components/DiscountAwareBehavior.php new file mode 100644 index 0000000..68ccdc3 --- /dev/null +++ b/common/components/DiscountAwareBehavior.php @@ -0,0 +1,25 @@ +owner->discount; + if (isset($discount)){ + $result = $discount->name; + } + return $result; + } + + + public function getDiscount(){ + return $this->owner->hasOne( Discount::className(), ["id_discount" =>"id_discount" ] ) ; + } + +} \ No newline at end of file diff --git a/common/components/ProductAwareBehavior.php b/common/components/ProductAwareBehavior.php new file mode 100644 index 0000000..da4427b --- /dev/null +++ b/common/components/ProductAwareBehavior.php @@ -0,0 +1,25 @@ +owner->product; + if (isset($product)){ + $result = $product->name; + } + return $result; + } + + + public function getProduct(){ + return $this->owner->hasOne( Product::className(), ["id_product" =>"id_product" ] ) ; + } + +} \ No newline at end of file diff --git a/common/components/SaleAwareBehavior.php b/common/components/SaleAwareBehavior.php new file mode 100644 index 0000000..cc934bc --- /dev/null +++ b/common/components/SaleAwareBehavior.php @@ -0,0 +1,24 @@ +owner->sale; + if (isset($sale)){ + $result = $sale->name; + } + return $result; + } + + + public function getSale(){ + return $this->owner->hasOne( Sale::className(), ["id_sale" =>"id_sale" ] ) ; + } + +} \ No newline at end of file diff --git a/common/models/Sale.php b/common/models/Sale.php new file mode 100644 index 0000000..d3a6e5c --- /dev/null +++ b/common/models/Sale.php @@ -0,0 +1,143 @@ + 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; + } + +} diff --git a/common/models/ShoppingCart.php b/common/models/ShoppingCart.php new file mode 100644 index 0000000..5af2b9b --- /dev/null +++ b/common/models/ShoppingCart.php @@ -0,0 +1,66 @@ + 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; + } + +} diff --git a/common/models/Transfer.php b/common/models/Transfer.php index ad0812e..037bfbc 100644 --- a/common/models/Transfer.php +++ b/common/models/Transfer.php @@ -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; diff --git a/console/migrations/m151021_091320_alter__table__sale__add__id_account.php b/console/migrations/m151021_091320_alter__table__sale__add__id_account.php new file mode 100644 index 0000000..6fd73b2 --- /dev/null +++ b/console/migrations/m151021_091320_alter__table__sale__add__id_account.php @@ -0,0 +1,30 @@ +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() + { + } + */ +} diff --git a/frontend/controllers/ProductController.php b/frontend/controllers/ProductController.php index e39b015..3202a6e 100644 --- a/frontend/controllers/ProductController.php +++ b/frontend/controllers/ProductController.php @@ -20,6 +20,7 @@ use common\models\Discount; use common\models\Transfer; use common\models\User; use common\models\UserSoldItem; +use common\models\ShoppingCart; /** * ProductController implements the CRUD actions for Product model. @@ -71,6 +72,8 @@ class ProductController extends Controller $user = User::findOne(Yii::$app->user->id ); + $model->customer = $this->customer; + if (Yii::$app->request->isAjax) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; @@ -103,6 +106,7 @@ class ProductController extends Controller // $userTransfers = Transfer::readUserSoldTransfers( $user ); $userTransfers = Transfer::modelsToArray( Transfer::readUserSoldTransfers($user) ); + $model->customerCart = ShoppingCart::readCustomerCart( $this->customer ); return $this->render("sale",[ 'customer' => $this->customer, @@ -112,7 +116,7 @@ class ProductController extends Controller 'currencies' => $currencies, 'accounts' => $accounts, 'discounts' => $discounts, - 'userTransfers' => $userTransfers + 'userTransfers' => $userTransfers, ]); } diff --git a/frontend/models/ProductSaleForm.php b/frontend/models/ProductSaleForm.php index 3085f80..f49502c 100644 --- a/frontend/models/ProductSaleForm.php +++ b/frontend/models/ProductSaleForm.php @@ -13,6 +13,8 @@ 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. @@ -42,8 +44,12 @@ class ProductSaleForm extends Model public $account; public $currency; public $discount; + public $customer; public $transfer; + public $sale; + + public $customerCart; /** * @inheritdoc @@ -121,21 +127,32 @@ class ProductSaleForm extends Model public function save(){ if ( $this->validate() ){ + $this->saveSale(); $this->saveTransfer(); $this->saveProduct(); - $this->appendToSoldList(); + $this->appendToUserCart(); + $this->appendToCustomerCart(); return true; } 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(){ - $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; @@ -151,7 +168,7 @@ class ProductSaleForm extends Model $this->product->save(); } - public function isAppendToList(){ + public function isAppendToUserCart(){ $result = false; if ( isset( $this->append_to_sold_list ) && $this->append_to_sold_list == 'append' ){ $result = true; @@ -159,12 +176,28 @@ class ProductSaleForm extends Model return $result; } - public function appendToSoldList(){ - if ( $this->isAppendToList() ){ + 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 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); + } + } } diff --git a/frontend/views/product/_customer_cart.php b/frontend/views/product/_customer_cart.php new file mode 100644 index 0000000..b5b5dd6 --- /dev/null +++ b/frontend/views/product/_customer_cart.php @@ -0,0 +1,36 @@ + + + +

+
+
+
+ + + + + + + + + + + + +
+ + + + + + + + + +
+
+
+
+ 'btn_paid', 'class' => 'btn btn-primary' ]) ?> \ No newline at end of file diff --git a/frontend/views/product/_sale_form.php b/frontend/views/product/_sale_form.php index 60c9a03..3c23e63 100644 --- a/frontend/views/product/_sale_form.php +++ b/frontend/views/product/_sale_form.php @@ -97,11 +97,14 @@ $discountOptions = mkOptions( ArrayHelper::map($discounts, 'id_discount', 'name'
-
+
'btn btn-success', 'id' => 'btn_sell'] );?>
-
- 'btn btn-success', 'id' => 'btn_sell_append'] );?> +
+ 'btn btn-success', 'id' => 'btn_sell_append'] );?> +
+
+ 'btn btn-success', 'id' => 'btn_add_to_customer_cart'] );?>
\ No newline at end of file diff --git a/frontend/views/product/sale.php b/frontend/views/product/sale.php index 602350e..0173e78 100644 --- a/frontend/views/product/sale.php +++ b/frontend/views/product/sale.php @@ -74,11 +74,17 @@ $this->registerJs ( 'new ProductSell( '. json_encode($options).');' );

- render('_sale_form' ,['model' =>$model , 'lookupModel' =>$lookupModel, 'currencies' => $currencies, 'accounts' => $accounts,'discounts' => $discounts,]) ?> + render('_sale_form' ,['model' =>$model , 'lookupModel' =>$lookupModel, 'currencies' => $currencies, 'accounts' => $accounts,'discounts' => $discounts ]) ?>
+ render('_customer_cart' ) ?> +

render('_sold_items' ) ?> 'btn_paid', 'class' => 'btn btn-primary' ]) ?> + + + +
diff --git a/frontend/web/js/product.sell.js b/frontend/web/js/product.sell.js index e734e23..b58e6db 100644 --- a/frontend/web/js/product.sell.js +++ b/frontend/web/js/product.sell.js @@ -20,6 +20,7 @@ function ProductSell(o){ /**list of sold items by current user*/ sold_items: [], discounts: [], + customer_cart: [] }; init(); @@ -37,6 +38,7 @@ function ProductSell(o){ addBehaviorCountChangedListener(); addBehaviourDisocuntChanged(); createUserSoldItemsTable(); + createCustomerCartTable(); addBehaviourBtnPaid(); productChanged(); addDocumentKeypressedListener(); @@ -269,6 +271,10 @@ function ProductSell(o){ function addBehaviourBtnSellAndAppendToBill(){ $('#btn_sell_append').on('click',submitSellAndAppend); } + + function addBehaviourBtnAddToCustomerCart(){ + $('#btn_add_to_customer_cart').on('click',submitAddToCustomerCart); + } function submitSell(){ $('#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(){ $('.sold-items-container').transferList('option','transfers' , app.defaults.sold_items ); } @@ -364,6 +375,11 @@ function ProductSell(o){ 'transfers' : app.defaults.sold_items }); } + function createCustomerCartTable(){ + $('.customer-cart').transferList({ + 'transfers' : app.defaults.customer_cart + }); + } function findDiscount(id_discount){ var discount;