From 84881e378cc1e598080f127de63f49090418f017 Mon Sep 17 00:00:00 2001 From: Semenov Date: Sat, 4 Apr 2015 22:29:00 +0400 Subject: [PATCH] 1 --- components/Cart.php | 17 +++++++++++++++++ components/shop/Shop.php | 12 ++++++++++-- components/shop/_form.php | 7 +++++++ controllers/BasketController.php | 19 +++++++++++++++++-- controllers/CartController.php | 30 ++++++++++++++++++++++++++++++ models/ShopBasket.php | 40 ++++++++++++++++++++++++++++++++++++++++ views/cart/index.php | 1 + 7 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 controllers/CartController.php create mode 100644 views/cart/index.php diff --git a/components/Cart.php b/components/Cart.php index 41033b6..1c787a3 100644 --- a/components/Cart.php +++ b/components/Cart.php @@ -61,8 +61,12 @@ class Cart extends \skeeks\cms\base\Component { if ($baskets = $shopFuser->getShopBaskets()) { + /** + * @var ShopBasket $basket + */ foreach ($baskets as $basket) { + //TODO:: доработать чтобы нужно обновлять количество $basket->fuser_id = $this->shopFuser->id; $basket->save(); } @@ -174,6 +178,11 @@ class Cart extends \skeeks\cms\base\Component } + /** + * Стоимость корзины. + * + * @return \skeeks\modules\cms\money\Money + */ public function cost() { $money = \Yii::$app->money->newMoney(); @@ -183,6 +192,14 @@ class Cart extends \skeeks\cms\base\Component return $money; } + if ($baskets = $this->getShopBaskets()) + { + foreach ($baskets as $basket) + { + $money = $money->add($basket->price()); + } + } + return $money; } } \ No newline at end of file diff --git a/components/shop/Shop.php b/components/shop/Shop.php index a089334..c473690 100644 --- a/components/shop/Shop.php +++ b/components/shop/Shop.php @@ -22,6 +22,11 @@ class Shop extends \skeeks\cms\base\Component public $email = 'admin@skeeks.com'; /** + * @var int + */ + public $allowBuyWhithoutQuantity = 1; + + /** * @var Cart */ public $cart = null; @@ -39,7 +44,8 @@ class Shop extends \skeeks\cms\base\Component { return [ - 'name' => 'Интернет-магазин', + 'name' => 'Интернет-магазин', + ]; } @@ -47,13 +53,15 @@ class Shop extends \skeeks\cms\base\Component { return ArrayHelper::merge(parent::rules(), [ [['email'], 'email'], + [['allowBuyWhithoutQuantity'], 'integer'], ]); } public function attributeLabels() { return ArrayHelper::merge(parent::attributeLabels(), [ - 'email' => 'Email отдела продаж', + 'email' => 'Email отдела продаж', + 'allowBuyWhithoutQuantity' => 'Разрешить покупку при отсутствии товара', ]); } diff --git a/components/shop/_form.php b/components/shop/_form.php index 9f73392..4bcbd94 100644 --- a/components/shop/_form.php +++ b/components/shop/_form.php @@ -16,6 +16,13 @@ use skeeks\cms\widgets\base\hasModelsSmart\ActiveForm; fieldSet('Основное'); ?> field($model, 'email')->textInput()->hint(''); ?> + field($model, 'allowBuyWhithoutQuantity')->widget( + \skeeks\widget\chosen\Chosen::className(), + [ + 'items' => \Yii::$app->formatter->booleanFormat, + 'allowDeselect' => false + ] + ); ?> fieldSetEnd(); ?> buttonsCreateOrUpdate($model); ?> diff --git a/controllers/BasketController.php b/controllers/BasketController.php index 45871bc..4b14b35 100644 --- a/controllers/BasketController.php +++ b/controllers/BasketController.php @@ -11,13 +11,18 @@ use skeeks\cms\base\Controller; use skeeks\cms\helpers\RequestResponse; use skeeks\modules\cms\shop\models\ShopBasket; use skeeks\modules\cms\shop\models\ShopFuser; - +use skeeks\modules\cms\catalog\models\Product; /** * Class ShopController * @package skeeks\modules\cms\shop\controllers */ class BasketController extends Controller { + /** + * Добавление продукта в корзину. + * + * @return array|\yii\web\Response + */ public function actionAddProduct() { $rr = new RequestResponse(); @@ -27,6 +32,14 @@ class BasketController extends Controller $product_id = \Yii::$app->request->post('product_id'); $quantity = \Yii::$app->request->post('quantity'); + $product = Product::find()->where(['id' => $product_id])->one(); + + if (!$product) + { + $rr->message = 'Товар не найден, возможно его только что удалили.'; + return (array) $rr; + } + \Yii::$app->shop->cart->loadShopFuser(); $shopBasket = ShopBasket::find()->where([ @@ -38,7 +51,7 @@ class BasketController extends Controller { $shopBasket = new ShopBasket([ 'fuser_id' => \Yii::$app->shop->cart->shopFuser->id, - 'name' => $product_id, + 'name' => $product->name, 'product_id' => $product_id, 'quantity' => 0, ]); @@ -47,6 +60,8 @@ class BasketController extends Controller $shopBasket->quantity = $shopBasket->quantity + $quantity; $shopBasket->save(); + $shopBasket->recalculate(); + $rr->success = true; $rr->message = 'Позиция добавлена в корзину'; diff --git a/controllers/CartController.php b/controllers/CartController.php new file mode 100644 index 0000000..f439089 --- /dev/null +++ b/controllers/CartController.php @@ -0,0 +1,30 @@ + + * @link http://skeeks.com/ + * @copyright 2010 SkeekS (СкикС) + * @date 03.04.2015 + */ +namespace skeeks\modules\cms\shop\controllers; + +use skeeks\cms\base\Controller; +use skeeks\cms\helpers\RequestResponse; +use skeeks\modules\cms\shop\models\ShopBasket; +use skeeks\modules\cms\shop\models\ShopFuser; +use skeeks\modules\cms\catalog\models\Product; + +/** + * Class CartController + * @package skeeks\modules\cms\shop\controllers + */ +class CartController extends Controller +{ + /** + * @return array|\yii\web\Response + */ + public function actionIndex() + { + //\Yii::$app->shop->cart; + return $this->render('index'); + } +} \ No newline at end of file diff --git a/models/ShopBasket.php b/models/ShopBasket.php index 745dc81..16ac06a 100644 --- a/models/ShopBasket.php +++ b/models/ShopBasket.php @@ -163,6 +163,20 @@ class ShopBasket extends Core } + protected $_product = null; + + /** + * @return Product + */ + public function product() + { + if ($this->_product === null) + { + $this->_product = $this->getProduct()->one(); + } + + return $this->_product; + } /** * @return \yii\db\ActiveQuery @@ -205,4 +219,30 @@ class ShopBasket extends Core } + /** + * Обновить позицию. + * + * @return $this + */ + public function recalculate() + { + /** + * @var $product Product + */ + $product = $this->getProduct()->one(); + if (!$product) + { + return $this; + } + + $cost = $product->cost(); //цена продукта + $money = $cost->multiply($this->quantity); //умножаем на количество + + $this->price = $money->getAmount() / $money->getCurrency()->getSubUnit(); + $this->currency = (string) $money->getCurrency(); + + $this->save(); + + return $this; + } } \ No newline at end of file diff --git a/views/cart/index.php b/views/cart/index.php new file mode 100644 index 0000000..62d4a40 --- /dev/null +++ b/views/cart/index.php @@ -0,0 +1 @@ +sdfg \ No newline at end of file -- libgit2 0.26.0