From: Alexander Smirnov Date: Wed, 26 Mar 2025 13:52:45 +0000 (+0300) Subject: [ERP-381] inputed cost X-Git-Url: https://gitweb.erp-flowers.ru/?a=commitdiff_plain;h=9f8aa8c4ce60842b15516373e31e82aefaccd654;p=erp24_rep%2Fyii-erp24%2F.git [ERP-381] inputed cost --- diff --git a/erp24/controllers/BouquetController.php b/erp24/controllers/BouquetController.php index a3c31f76..40862fc6 100644 --- a/erp24/controllers/BouquetController.php +++ b/erp24/controllers/BouquetController.php @@ -65,6 +65,8 @@ class BouquetController extends Controller $model = BouquetComposition::findModel($id); if (Yii::$app->request->isPost && $products = Yii::$app->request->post('products_quantity')) { + $cost = Yii::$app->request->post('cost-value'); + $model->setCost($cost); BouquetCompositionProducts::updateProducts($model->id, $products); return $this->redirect(['view', 'id' => $id]); } @@ -140,16 +142,16 @@ class BouquetController extends Controller ]); } - public function actionGetCalculates() + public function actionDefaultCalculates() { Yii::$app->response->format = Response::FORMAT_JSON; $data = json_decode(Yii::$app->request->getRawBody(), true); - $model = new BouquetComposition(); + $model = BouquetComposition::findOne($data['bouquetId']); return [ 'selfcost' => round($model->getSelfCost($data), 2), - 'cost' => round($model->getCost($data), 2), - 'markup' => $model->getMarkUp($data), + 'cost' => round($model->getBouquetCost($data), 2), + 'markup' => round($model->getBouquetCostMarkup($data), 2), ]; } @@ -207,4 +209,4 @@ class BouquetController extends Controller return $this->redirect(Yii::$app->request->referrer); } -} \ No newline at end of file +} diff --git a/erp24/records/BouquetComposition.php b/erp24/records/BouquetComposition.php index 4b9ba74f..c168cca5 100644 --- a/erp24/records/BouquetComposition.php +++ b/erp24/records/BouquetComposition.php @@ -317,63 +317,97 @@ class BouquetComposition extends ActiveRecord return $this->hasMany(BouquetForecast::class, ['bouquet_id' => 'id']); } - /** - * Рассчитывает стоимость букета. - * - * @param array|null $data Данные продуктов (опционально) - * @return float Стоимость - */ - public function getCost(?array $data = null): float - { - $cost = 0; - $compositionProducts = $this->bouquetCompositionProducts; - - if (!$compositionProducts) { - $compositionProducts = $data; - if (empty($data)) { - return $cost; - } - } - - foreach ($compositionProducts as $item) { - if (!$item || !isset($item['product_guid'])) { - continue; - } - - $priceModel = PricesDynamic::find() - ->where(['product_id' => $item['product_guid']]) - ->andWhere(['=', 'active', PricesDynamic::ACTIVE]) - ->one(); - - if ($priceModel) { - $cost += $priceModel->price * $item['count']; - } +// /** +// * Рассчитывает стоимость букета. +// * +// * @param array|null $data Данные продуктов (опционально) +// * @return float Стоимость +// */ +// public function getCost(?array $data = null): float +// { +// $cost = 0; +// $compositionProducts = $this->bouquetCompositionProducts; +// +// if (!$compositionProducts) { +// $compositionProducts = $data; +// if (empty($data)) { +// return $cost; +// } +// } +// +// foreach ($compositionProducts as $item) { +// if (!$item || !isset($item['product_guid'])) { +// continue; +// } +// +// $priceModel = PricesDynamic::find() +// ->where(['product_id' => $item['product_guid']]) +// ->andWhere(['=', 'active', PricesDynamic::ACTIVE]) +// ->one(); +// +// if ($priceModel) { +// $cost += $priceModel->price * $item['count']; +// } +// } +// +// return $cost; +// } + + public function setCost($cost) { + $priceModel = PricesDynamic::find() + ->where(['product_id' => $this->guid]) + ->andWhere(['=', 'active', PricesDynamic::ACTIVE]) + ->one(); + /* @var PricesDynamic $priceModel */ + if ($priceModel) { + $priceModel->date_to = date('Y-m-d H:i:s'); + $priceModel->active = PricesDynamic::NOT_ACTIVE; + $priceModel->save(); } + $newPriceDynamic = new PricesDynamic; + $newPriceDynamic->product_id = $this->guid; + $newPriceDynamic->active = PricesDynamic::ACTIVE; + $newPriceDynamic->date_from = date('Y-m-d H:i:s'); + $newPriceDynamic->date_to = date('2100-01-01 00:00:00'); + $newPriceDynamic->price = $cost; + $newPriceDynamic->save(); + } - return $cost; + public function getBouquetCost($data = null) { + $priceModel = PricesDynamic::find() + ->where(['product_id' => $this->guid]) + ->andWhere(['=', 'active', PricesDynamic::ACTIVE]) + ->one(); + /* @var PricesDynamic $priceModel */ + return $priceModel ? $priceModel->price : $this->getSelfCost($data) * 1.3 * 1.15; } - /** - * Рассчитывает наценку на букет. - * - * @param array|null $data Данные продуктов (опционально) - * @return string Наценка в формате "+X.XX% / +X.XX" - */ - public function getMarkUp(?array $data = null): string - { + public function getBouquetCostMarkup($data = null) { $selfCost = $this->getSelfCost($data); - $cost = $this->getCost($data); - - if ($selfCost == 0 || $cost == 0) { - return '0'; - } - - return sprintf("+%.2f%% / +%.2f", - ($cost / $selfCost - 1) * 100, - $cost - $selfCost - ); + return $selfCost > 0 ? ($this->getBouquetCost($data) / (1.3 * $selfCost) - 1) * 100 : 0; } +// /** +// * Рассчитывает наценку на букет. +// * +// * @param array|null $data Данные продуктов (опционально) +// * @return string Наценка в формате "+X.XX% / +X.XX" +// */ +// public function getMarkUp(?array $data = null): string +// { +// $selfCost = $this->getSelfCost($data); +// $cost = $this->getCost($data); +// +// if ($selfCost == 0 || $cost == 0) { +// return '0'; +// } +// +// return sprintf("+%.2f%% / +%.2f", +// ($cost / $selfCost - 1) * 100, +// $cost - $selfCost +// ); +// } + /** * Рассчитывает себестоимость букета. * diff --git a/erp24/records/PricesDynamic.php b/erp24/records/PricesDynamic.php index 87386ce6..43139a58 100755 --- a/erp24/records/PricesDynamic.php +++ b/erp24/records/PricesDynamic.php @@ -18,6 +18,7 @@ use Yii; class PricesDynamic extends \yii\db\ActiveRecord { public const ACTIVE = 1; + public const NOT_ACTIVE = 0; /** * {@inheritdoc} */ diff --git a/erp24/views/bouquet/_product_edit.php b/erp24/views/bouquet/_product_edit.php index 3f7d9bf0..284541a4 100644 --- a/erp24/views/bouquet/_product_edit.php +++ b/erp24/views/bouquet/_product_edit.php @@ -2,6 +2,8 @@ use yii\helpers\Html; use yii_app\records\BouquetComposition; +$this->registerCSS('.cost-value {max-width: 75px}'); + ?> 'products', @@ -25,8 +27,9 @@ use yii_app\records\BouquetComposition;

Себестоимость: ₽

-

Наценка:

-

Цена: ₽

+

Наценка: +30% / +₽

+

Цена: ₽. +%

diff --git a/erp24/views/bouquet/_product_list.php b/erp24/views/bouquet/_product_list.php index d044e165..3354d6dd 100644 --- a/erp24/views/bouquet/_product_list.php +++ b/erp24/views/bouquet/_product_list.php @@ -31,9 +31,9 @@
Нижегородская область
- Себестоимость: getSelfCost() ?>
- Наценка: getMarkUp() ?>
- Цена: getCost() ?>
+ Себестоимость: getSelfCost() ?>
+ Наценка: +30% / +getSelfCost()) ?>
+ Цена: getBouquetCost() ?>р. +getBouquetCostMarkup() ?>%
diff --git a/erp24/views/bouquet/index.php b/erp24/views/bouquet/index.php index 675475e1..72b7b224 100644 --- a/erp24/views/bouquet/index.php +++ b/erp24/views/bouquet/index.php @@ -111,8 +111,8 @@ $this->title = 'Содержание матрицы';
Себестоимость: " . $model->getSelfCost() . "
- Наценка: " . $model->getMarkUp() . "
- Цена: " . $model->getCost() . "
+ Наценка: +30% / +" . (0.3 * $model->getSelfCost()) . "
+ Цена: " . $model->getBouquetCost() . "р. +" . $model->getBouquetCostMarkup() . "%
diff --git a/erp24/views/bouquet/update.php b/erp24/views/bouquet/update.php index 6fc7ae1e..0e404d7b 100644 --- a/erp24/views/bouquet/update.php +++ b/erp24/views/bouquet/update.php @@ -17,6 +17,7 @@ $this->title = $model->name; $this->params['breadcrumbs'][] = ['label' => 'Букеты', 'url' => ['index']]; $this->params['breadcrumbs'][] = $this->title; +$this->registerJSVar('bouquetId', $model->id); $this->registerJsFile('/js/bouquet/bouquet.js', ['position' => \yii\web\View::POS_END]); ?> @@ -60,8 +61,8 @@ $this->registerJsFile('/js/bouquet/bouquet.js', ['position' => \yii\web\View::PO 'isCreate' => false, 'listContainerSize' => [], 'selfCost' => $model->getSelfCost(), - 'cost' => $model->getCost(), - 'markUp' => $model->getMarkUp(), + 'cost' => $model->getBouquetCost(), + 'markUp' => $model->getBouquetCostMarkup(), ]); ?> \ No newline at end of file diff --git a/erp24/web/js/bouquet/bouquet.js b/erp24/web/js/bouquet/bouquet.js index 4d74d0ac..7b39edf8 100644 --- a/erp24/web/js/bouquet/bouquet.js +++ b/erp24/web/js/bouquet/bouquet.js @@ -1,3 +1,5 @@ +/* jshint esversion: 6 */ + document.addEventListener('DOMContentLoaded', function () { document.querySelectorAll('.video-preview').forEach(video => { video.addEventListener('click', function () { @@ -56,7 +58,7 @@ $(document).ready(function () { }); $(document).on('click', '.calculate-btn, .btn-add-item, .btn-remove-item', function () { - $('.cost-value, .selfcost-value, .markup-value').text(''); + $('.cost-value, .selfcost-value, .markup-value, .markup-cost-value').text(''); }); $(document).on('input change', '.quantity-input', function () { @@ -64,6 +66,11 @@ $(document).on('input change', '.quantity-input', function () { }); +$('.cost-value').on('change', function () { + const selfCost = $('.selfcost-value'); + $('.markup-cost-value').text(+selfCost.text() > 0 ? Math.round((this.value / (1.3 * (+selfCost.text())) - 1)*10000) / 100 : 0); +}); + $('.calculate-btn').on('click', function () { let data = []; @@ -84,21 +91,23 @@ $('.calculate-btn').on('click', function () { $(".content-wrapper").addClass("position-relative"); $.ajax({ - url: '/bouquet/get-calculates', + url: '/bouquet/default-calculates', type: 'POST', contentType: 'application/json', - data: JSON.stringify(data), + data: JSON.stringify({ data, bouquetId }), dataType: 'json', success: function (response) { if (response) { if (response.cost !== undefined) { - $('.cost-value').text(response.cost); + $('.cost-value').val(+response.cost); } if (response.selfcost !== undefined) { $('.selfcost-value').text(response.selfcost); + $('.markup-value').text(0.3 * (+response.selfcost)); + $('.cost-value').attr('min', 1.3 * (+response.selfcost)); } if (response.markup !== undefined) { - $('.markup-value').text(response.markup); + $('.markup-cost-value').text(+response.markup); } } },