Commit 84881e37 by semenov

1

parent 1a234650
......@@ -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
......@@ -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' => 'Разрешить покупку при отсутствии товара',
]);
}
......
......@@ -16,6 +16,13 @@ use skeeks\cms\widgets\base\hasModelsSmart\ActiveForm;
<?= $form->fieldSet('Основное'); ?>
<?= $form->field($model, 'email')->textInput()->hint(''); ?>
<?= $form->field($model, 'allowBuyWhithoutQuantity')->widget(
\skeeks\widget\chosen\Chosen::className(),
[
'items' => \Yii::$app->formatter->booleanFormat,
'allowDeselect' => false
]
); ?>
<?= $form->fieldSetEnd(); ?>
<?= $form->buttonsCreateOrUpdate($model); ?>
......
......@@ -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 = 'Позиция добавлена в корзину';
......
<?php
/**
* @author Semenov Alexander <semenov@skeeks.com>
* @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
......@@ -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
sdfg
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment