From 2961089271208c9d162d693bc8da72fb34e47765 Mon Sep 17 00:00:00 2001 From: Alexander Smirnov Date: Mon, 10 Feb 2025 18:43:53 +0300 Subject: [PATCH] =?utf8?q?[ERP-283]=20=D1=80=D0=B5=D0=B4=D0=B0=D0=BA=D1=82?= =?utf8?q?=D0=B8=D1=80=D1=83=D0=B5=D0=BC=D1=8B=D0=B5=20=D0=BA=D0=B0=D1=82?= =?utf8?q?=D0=B5=D0=B3=D0=BE=D1=80=D0=B8=D0=B8=20=D0=BF=D0=BB=D0=B0=D0=BD?= =?utf8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/controllers/CategoryPlanController.php | 66 ++++++++++- .../SalesWriteOffsPlanController.php | 1 - erp24/records/CategoryPlan.php | 90 +++++++++++++++ erp24/views/category-plan/index.php | 65 ++++++----- erp24/web/js/category-plan/index.js | 109 ++++++++++++++++++ 5 files changed, 297 insertions(+), 34 deletions(-) create mode 100644 erp24/records/CategoryPlan.php diff --git a/erp24/controllers/CategoryPlanController.php b/erp24/controllers/CategoryPlanController.php index 57d9806f..85a148fb 100644 --- a/erp24/controllers/CategoryPlanController.php +++ b/erp24/controllers/CategoryPlanController.php @@ -4,9 +4,12 @@ namespace app\controllers; use Yii; use yii\base\DynamicModel; +use yii\db\Exception; use yii\helpers\ArrayHelper; +use yii\helpers\Json; use yii\web\Controller; use yii\web\Response; +use yii_app\records\CategoryPlan; use yii_app\records\CityStore; use yii_app\records\CityStoreParams; use yii_app\records\ExportImportTable; @@ -38,6 +41,11 @@ class CategoryPlanController extends Controller { $model->load(Yii::$app->request->get()); + $isEditable = date($model->year . '-' . $model->month . '-d') > date('Y-m-d') && ( + (date('d') < 25) || (date('Y-m-d', strtotime('-1 month', date($model->year . '-' . $model->month . '-d'))) > date('Y-m-d'))); + + $categoryPlan = CategoryPlan::find()->where(['year' => $model->year, 'month' => $model->month, 'store_id' => $model->store_id])->indexBy('category')->asArray()->all(); + /////////////////////////// CHAT GPT STUFF /////////////////////////////////// $currentDate = new \DateTime(); @@ -129,6 +137,30 @@ class CategoryPlanController extends Controller { $types = array_keys($types); ///////////////////////////////////////////////////////////////////////////////////// + foreach ($types as $type) { + if (!isset($categoryPlan[$type])) { + $categoryPlanNew = new CategoryPlan; + $categoryPlanNew->year = $model->year; + $categoryPlanNew->month = $model->month; + $categoryPlanNew->store_id = $model->store_id; + $categoryPlanNew->category = $type; + $categoryPlanNew->offline = $table[$model->store_id][$type] ?? 0; + $categoryPlanNew->internet_shop = $tableOnline[$model->store_id][$type] ?? 0; + $categoryPlanNew->marketplace = 0; + $categoryPlanNew->write_offs = $tableWriteOffs[$type] ?? 0; + $categoryPlanNew->created_at = date('Y-m-d HH:i:s'); + $categoryPlanNew->updated_at = date('Y-m-d HH:i:s'); + $categoryPlanNew->created_by = Yii::$app->user->id; + $categoryPlanNew->updated_by = Yii::$app->user->id; + $categoryPlanNew->save(); + if ($categoryPlanNew->getErrors()) { + throw new Exception(Json::encode($categoryPlanNew->getErrors())); + } + } + } + + $categoryPlan = CategoryPlan::find()->where(['year' => $model->year, 'month' => $model->month, 'store_id' => $model->store_id])->indexBy('category')->asArray()->all(); + $salesWriteOffsPlan = SalesWriteOffsPlan::find()->where(['year' => $model->year, 'month' => $model->month, 'store_id' => $model->store_id])->one(); $years = []; @@ -139,7 +171,7 @@ class CategoryPlanController extends Controller { $stores = ArrayHelper::map(CityStore::find()->andWhere(['visible' => '1'])->all(), 'id', 'name'); return $this->render('index', compact('model', 'years', 'stores', 'table', 'tableOnline', - 'tableWriteOffs', 'types', 'salesWriteOffsPlan')); + 'tableWriteOffs', 'types', 'salesWriteOffsPlan', 'isEditable', 'categoryPlan')); } public function actionGetStores() { @@ -203,4 +235,36 @@ class CategoryPlanController extends Controller { return ArrayHelper::map($stores, 'id', 'name'); } + + public function actionSaveFields() { + $data = Yii::$app->request->post(); + $year = $data['year']; + $month = $data['month']; + $storeId = $data['store_id']; + $productType = $data['type']; + $offline = $data['offline']; + $internet_shop = $data['internet_shop']; + $write_offs = $data['write_offs']; + + $categoryPlan = CategoryPlan::find()->where(['year' => $year, 'month' => $month, 'store_id' => $storeId, 'category' => $productType])->one(); + if (!$categoryPlan) { + $categoryPlan = new CategoryPlan; + $categoryPlan->year = $year; + $categoryPlan->month = $month; + $categoryPlan->store_id = $storeId; + $categoryPlan->category = $productType; + $categoryPlan->created_by = Yii::$app->user->id; + $categoryPlan->created_at = date('Y-m-d H:i:s'); + } + $categoryPlan->offline = $offline; + $categoryPlan->internet_shop = $internet_shop; + $categoryPlan->write_offs = $write_offs; + $categoryPlan->updated_by = Yii::$app->user->id; + $categoryPlan->updated_at = date('Y-m-d H:i:s'); + $categoryPlan->save(); + if ($categoryPlan->getErrors()) { + throw new \Exception(Json::encode($categoryPlan->getErrors())); + } + return 'ok'; + } } diff --git a/erp24/controllers/SalesWriteOffsPlanController.php b/erp24/controllers/SalesWriteOffsPlanController.php index e52cca2e..031461fb 100644 --- a/erp24/controllers/SalesWriteOffsPlanController.php +++ b/erp24/controllers/SalesWriteOffsPlanController.php @@ -35,7 +35,6 @@ class SalesWriteOffsPlanController extends Controller (date('d') < 25) || (date('Y-m-d', strtotime('-1 month', date($model->year . '-' . $model->month . '-d'))) > date('Y-m-d') )); -//var_dump($isEditable); die; $prevPrevMonthStart = date("Y-m-01", strtotime("-2 month", strtotime($model->year . "-" . $model->month . "-01"))); $prevPrevMonthEnd = date("Y-m-t", strtotime("-2 month", strtotime($model->year . "-" . $model->month . "-01"))); diff --git a/erp24/records/CategoryPlan.php b/erp24/records/CategoryPlan.php new file mode 100644 index 00000000..1475b483 --- /dev/null +++ b/erp24/records/CategoryPlan.php @@ -0,0 +1,90 @@ + TimestampBehavior::class, + 'createdAtAttribute' => 'created_at', + 'updatedAtAttribute' => 'updated_at', + 'value' => new Expression('NOW()'), + ], + [ + 'class' => BlameableBehavior::class, + 'createdByAttribute' => 'created_by', + 'updatedByAttribute' => 'updated_by', + ], + ]; + } + + + /** + * {@inheritdoc} + */ + public function rules() + { + return [ + [['year', 'month', 'store_id', 'category', 'created_at', 'updated_at', 'created_by', 'updated_by'], 'required'], + [['year', 'month', 'store_id', 'created_by', 'updated_by'], 'default', 'value' => null], + [['year', 'month', 'store_id', 'created_by', 'updated_by'], 'integer'], + [['offline', 'internet_shop', 'marketplace', 'write_offs'], 'number'], + [['created_at', 'updated_at'], 'safe'], + [['category'], 'string', 'max' => 100], + ]; + } + + /** + * {@inheritdoc} + */ + public function attributeLabels() + { + return [ + 'id' => 'ID', + 'year' => 'Year', + 'month' => 'Month', + 'store_id' => 'Store ID', + 'category' => 'Category', + 'offline' => 'Offline', + 'internet_shop' => 'Internet Shop', + 'marketplace' => 'Marketplace', + 'write_offs' => 'Write Offs', + 'created_at' => 'Created At', + 'updated_at' => 'Updated At', + 'created_by' => 'Created By', + 'updated_by' => 'Updated By', + ]; + } +} diff --git a/erp24/views/category-plan/index.php b/erp24/views/category-plan/index.php index d2818af8..24615c83 100644 --- a/erp24/views/category-plan/index.php +++ b/erp24/views/category-plan/index.php @@ -21,9 +21,17 @@ use yii_app\records\StoreType; /* @var $types array */ /* @var $tableWriteOffs array */ /* @var $salesWriteOffsPlan SalesWriteOffsPlan */ +/* @var $isEditable boolean */ +/* @var $categoryPlan array */ $this->registerJsFile('/js/category-plan/index.js', ['position' => \yii\web\View::POS_END]); +$this->registerCss(' +input[readonly] { + background: lightgray; +} +'); + ?>
@@ -63,21 +71,6 @@ $this->registerJsFile('/js/category-plan/index.js', ['position' => \yii\web\View ])->label(false) ?>
- $stores, - // 'language' => 'ru', - // 'options' => ['placeholder' => 'Магазин...'], - // 'pluginOptions' => [ - // 'allowClear' => true - // ], - // 'pluginEvents' => [ - // 'change' => 'function(e) { - // $("#filter-form").get(0).submit(); - // }' - // ] - // ])->label(false) - ?>
field($model, 'month')->dropDownList(HtmlHelper::getMonthNames(), ['onchange' => '//this.form.submit();'])->label(false) ?> @@ -179,27 +172,35 @@ $this->registerJsFile('/js/category-plan/index.js', ['position' => \yii\web\View ?> КатегорииПлан продажСписания - ОффлайнИнтернет-МагазинСписания - - - - %Сумма%Cумма%Cумма + ОффлайнИнтернет-МагазинСписания + + + + %Сумма%Cумма%Cумма - store_id][$type] ?? 0; ?> - - % - - store_id][$type] ?? 0; ?> - % - - - - % - + + + % + 'number', 'readonly' => !$isEditable, 'onchange' => 'editField(this);']) ?> + + % + 'number', 'readonly' => !$isEditable, 'onchange' => 'editField(this);']) ?> + ?> + + % + 'number', 'readonly' => !$isEditable, 'onchange' => 'editField(this);']) ?> diff --git a/erp24/web/js/category-plan/index.js b/erp24/web/js/category-plan/index.js index 9687070b..ee30d0cc 100644 --- a/erp24/web/js/category-plan/index.js +++ b/erp24/web/js/category-plan/index.js @@ -3,6 +3,10 @@ const param26 = $('meta[name=csrf-param]').attr('content'); const token26 = $('meta[name=csrf-token]').attr('content'); +function isNumeric(value) { + return /^-?\d+\.?\d*$/.test(value); +} + function updateStores() { const city_id = $('#dynamicmodel-city_id').val(); const region_id = $('#dynamicmodel-region_id').val(); @@ -27,6 +31,111 @@ function updateStores() { }); } +function editField(zis) { + const tr = zis.parentNode.parentNode; + const store_id = document.querySelector("#selected-store").value; + const type = tr.querySelector("[data-type]").textContent; + const offline = tr.querySelector("input[name=offline]").value; + const internet_shop = tr.querySelector("input[name=internet_shop]").value; + const write_offs = tr.querySelector("input[name=write_offs]").value; + + const editFields = [ + offline, + internet_shop, + write_offs + ]; + + let succ = true; + editFields.forEach((el) => { + if (!isNumeric(el)) { + succ = false; + alert(el + " не число"); + console.log(el); + } + }); + + if (succ) { + const p1 = tr.querySelector("[data-p1]"); + const p2 = tr.querySelector("[data-p2]"); + const p3 = tr.querySelector("[data-p3]"); + p1.textContent = (p1.dataset.offline > 0 ? Math.round(100.0 * offline / +p1.dataset.offline) : 0) + '%'; + p2.textContent = (p2.dataset.online > 0 ? Math.round(100.0 * internet_shop / +p2.dataset.online) : 0) + '%'; + p3.textContent = (p3.dataset.writeoffs > 0 ? Math.round(100.0 * write_offs / +p3.dataset.writeoffs) : 0) + '%'; + + $.ajax({ + method: "POST", + url: '/category-plan/save-fields', + data: { + year: document.querySelector('#dynamicmodel-year').value, + month: document.querySelector('#dynamicmodel-month').value, + store_id, + type, + offline, + internet_shop, + write_offs, + [param26]: token26 + }, + dataType: "text", + success: function(data) { + console.log(data); + }, + }); + } + + return; + /* + if (succ) { + total_sales_plan.value = +offline_sales_plan.value + (+online_sales_shop_plan.value) + (+online_sales_marketplace_plan.value); + //////////////////////////////////////////////// + const p1 = tr.querySelector('.p1'); + const p1Value = total_sales_fact.value > 0 ? total_sales_plan.value / total_sales_fact.value : 0; + p1.innerHTML = new Intl.NumberFormat().format(Number((p1Value * 100).toFixed(0))); + p1.style.color = colorScheme1(p1Value); + + const p2 = tr.querySelector('.p2'); + const p2Value = total_sales_plan.value > 0 ? write_offs_plan.value / total_sales_plan.value : 0; + p2.innerHTML = new Intl.NumberFormat().format(Number((p2Value * 100).toFixed(1))); + p2.style.color = colorScheme2(p2Value); + + const p3 = tr.querySelector('.p3'); + const p3Value = total_sales_fact.value > 0 ? offline_sales_plan.value / total_sales_fact.value : 0; + p3.innerHTML = new Intl.NumberFormat().format(Number((p3Value * 100).toFixed(0))); + p3.style.color = colorScheme1(p3Value); + + const p4 = tr.querySelector('.p4'); + const p4Value = total_sales_fact.value > 0 ? online_sales_shop_plan.value / total_sales_fact.value : 0; + p4.innerHTML = new Intl.NumberFormat().format(Number((p4Value * 100).toFixed(0))); + p4.style.color = colorScheme1(p4Value); + + const p5 = tr.querySelector('.p5'); + const p5Value = total_sales_fact.value > 0 ? online_sales_marketplace_plan.value / total_sales_fact.value : 0; + p5.innerHTML = new Intl.NumberFormat().format(Number((p5Value * 100).toFixed(0))); + p5.style.color = colorScheme1(p5Value); + //////////////////////////////////////////////// + $.ajax({ + method: "POST", + url: '/sales-write-offs-plan/save-fields', + data: { + year: document.querySelector('#dynamicmodel-year').value, + month: document.querySelector('#dynamicmodel-month').value, + store_id, + total_sales_plan : total_sales_plan.value, + total_sales_fact : total_sales_fact.value, + write_offs_plan : write_offs_plan.value, + offline_sales_plan : offline_sales_plan.value, + online_sales_shop_plan : online_sales_shop_plan.value, + online_sales_marketplace_plan : online_sales_marketplace_plan.value, + [param25]: token25 + }, + dataType: "text", + success: function(data) { + console.log(data); + }, + }); + } + */ +} + $(document).ready(() => { $('#categoryPlan').DataTable({ sorting: false, -- 2.39.5