'marketplace' => $marketplaceList,
];
}
+ public function actionGetCalculates()
+ {
+ Yii::$app->response->format = Response::FORMAT_JSON;
+ $data = json_decode(Yii::$app->request->getRawBody(), true);
+ $model = new BouquetComposition();
+
+ return [
+ 'selfcost' => round($model->getSelfCost($data), 2),
+ 'cost' => round($model->getCost($data), 2),
+ 'markup' => $model->getMarkUp($data),
+ ];
+ }
+
+
}
->andWhere(['entity' => self::VIDEO_BUILD_PROCESS]);
}
- public function getCost()
+ public function getCost($data = null)
{
$cost = 0;
$compositionProducts = $this->bouquetCompositionProducts;
if (!$compositionProducts) {
- return $cost;
+ $compositionProducts = $data;
+ if (empty($data)) {
+ return $cost;
+ }
}
foreach ($compositionProducts as $item) {
- if (!$item || !isset($item->product_guid)) {
+ if (!$item || !isset($item['product_guid'])) {
continue;
}
$priceModel = PricesDynamic::find()
- ->where(['product_id' => $item->product_guid])
+ ->where(['product_id' => $item['product_guid']])
->andWhere(['=', 'active', PricesDynamic::ACTIVE])
->one();
if ($priceModel) {
- $cost += $priceModel->price * $item->count;
+ $cost += $priceModel->price * $item['count'];
}
}
return $cost;
}
- public function getMarkUp()
+ public function getMarkUp($data = null)
{
- if ($this->getSelfCost() == 0 || $this->getCost() == 0)
+ if ($this->getSelfCost($data) == 0 || $this->getCost($data) == 0)
return 0;
return sprintf("+%.2f%% / +%.2f",
- (self::getCost() / self::getSelfCost() - 1) * 100,
- self::getCost() - self::getSelfCost()
+ (self::getCost($data) / self::getSelfCost($data) - 1) * 100,
+ self::getCost($data) - self::getSelfCost($data)
);
}
- public function getSelfCost()
+ public function getSelfCost($data = null)
{
$selfCost = 0;
$compositionProducts = $this->bouquetCompositionProducts;
if (!$compositionProducts) {
- return $selfCost;
+ $compositionProducts = $data;
+ if (empty($data)) {
+ return $selfCost;
+ }
}
-
- $productGuids = array_filter(array_column($compositionProducts, 'product_guid'));
-
+
+ $productGuids = array_filter(array_column($compositionProducts, 'product_guid'));
+
if (empty($productGuids)) {
return $selfCost;
}
}
foreach ($compositionProducts as $item) {
- if (!$item || !isset($item->product_guid)) {
+ if (!$item || !isset($item['product_guid'])) {
continue;
}
- $prices = $pricesByProduct[$item->product_guid] ?? [];
+ $prices = $pricesByProduct[$item['product_guid']] ?? [];
$count = count($prices);
- if ($count === 0) {
+
+ if ($count == 0) {
continue;
}
? ($prices[$middle - 1] + $prices[$middle]) / 2
: $prices[$middle];
- $selfCost += $median * $item->count;
+ $selfCost += $median * $item['count'];
}
return $selfCost;
]) ?>
<br>
<div class="row">
- <div class="col-md-3">
+ <div class="col-md-4">
<div class="pt-2">
- <p class="mb-1"><strong>Себестоимость:</strong> <span class="cost-value">0</span> ₽</p>
- <p class="mb-1"><strong>Наценка:</strong> <span class="markup-value">0</span> %</p>
- <p class="mb-0"><strong>Цена:</strong> <span class="price-value">0</span> ₽</p>
+ <p class="mb-1"><strong>Себестоимость:</strong> <span class="selfcost-value"><?= $selfCost ?? 0 ?></span> ₽</p>
+ <p class="mb-1"><strong>Наценка:</strong> <span class="markup-value"><?= $markUp?? 0 ?></span></p>
+ <p class="mb-0"><strong>Цена:</strong> <span class="cost-value"><?= $cost ?? 0 ?></span> ₽</p>
</div>
</div>
- <div class="col-md-6"></div>
+<div class="col-md-4"></div>
<?php if (!$isCreate) {?>
+ <div class="col-md-2 d-flex justify-content-end align-items-end px-3 w-100">
+ <?= Html::button('Рассчитать', ['class' => 'btn btn-warning calculate-btn w-100']) ?>
+ </div>
<div class="col-md-2 d-flex justify-content-end align-items-end px-3 w-100">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success w-100']) ?>
</div>
- <?php } ?>
+ <?php } else { ?>
+ <div class="col-md-4 d-flex justify-content-end align-items-end px-3 w-100">
+ <?= Html::button('Рассчитать', ['class' => 'btn btn-warning calculate-btn w-100']) ?>
+ </div>
+ <?php } ?>
</div>
$this->title = 'Состав букета';
$this->params['breadcrumbs'][] = ['label' => 'Букеты', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
+
+$this->registerJsFile('/js/bouquet/bouquet.js', ['position' => \yii\web\View::POS_END]);
?>
+
<div class="bouquet-create border-bottom-4 p-6">
<?= $this->render('_form', [
'onlineStoresList' => $onlineStoresList,
use app\widgets\DualList;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
+use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii_app\records\Products1c;
use yii_app\records\Products1cNomenclature;
<h2 class="d-inline"><strong><?= Html::encode($this->title) ?></strong></h2>
<div class="row">
<div class="col-md-2">
- <?= Html::a('Назад', ['index'], ['class' => 'btn btn-primary ms-3 mt-4 w-45']) ?>
+ <?= Html::a('Назад', Url::to(['bouquet/view', 'id' => $model->id], true), ['class' => 'btn btn-primary ms-3 mt-4 w-45']) ?>
</div>
</div>
'selectedItems' => $selectedItems,
'isCreate' => false,
'listContainerSize' => [],
+ 'selfCost' => $model->getSelfCost(),
+ 'cost' => $model->getCost(),
+ 'markUp' => $model->getMarkUp(),
]); ?>
<?php ActiveForm::end(); ?>
</div>
\ No newline at end of file
data: { year: year, month: month, id: id },
dataType: 'json',
success: function(response) {
- console.log("Ответ сервера:", response);
if (response) {
['offline', 'online', 'marketplace'].forEach(type => {
$.each(response[type], function(_, value) {
let inputSelector = `input[name='BouquetForecast[type_sales_value][${type}][${value.id}]']`;
- console.log(`Поиск инпута: ${inputSelector}, установка значения: ${value.value}`);
$(inputSelector).val(value.value);
});
});
});
}
});
+
+$('.calculate-btn').on('click', function () {
+ let data = [];
+
+ $('.selected-item').each(function () {
+ let id = $(this).data('id');
+ let quantity = $(this).find('.quantity-input').val();
+
+ if (id && quantity) {
+ data.push({
+ product_guid: id,
+ count: quantity
+ });
+ }
+ });
+
+ $.ajax({
+ url: '/bouquet/get-calculates',
+ type: 'POST',
+ contentType: 'application/json',
+ data: JSON.stringify(data),
+ dataType: 'json',
+ success: function(response) {
+ if (response) {
+ if (response.cost !== undefined) {
+ $('.price-value').text(response.cost);
+ }
+ if (response.selfcost !== undefined) {
+ $('.selfcost-value').text(response.selfcost);
+ }
+ if (response.markup !== undefined) {
+ $('.markup-value').text(response.markup);
+ }
+ }
+ },
+ error: function(xhr, status, error) {
+ console.error("Ошибка загрузки данных:", status, error);
+ }
+ });
+});