From 229130409aa02a7e6d59803e1d0feb33241fcd9d Mon Sep 17 00:00:00 2001
From: Javier Segarra <jsegarra@verdnatura.es>
Date: Tue, 31 Dec 2024 10:45:37 +0100
Subject: [PATCH] fix: init fix

---
 .../Customer/Card/CustomerConsumption.vue     | 40 +++++++++++++------
 1 file changed, 28 insertions(+), 12 deletions(-)

diff --git a/src/pages/Customer/Card/CustomerConsumption.vue b/src/pages/Customer/Card/CustomerConsumption.vue
index 35f366e47..e41c22ee6 100644
--- a/src/pages/Customer/Card/CustomerConsumption.vue
+++ b/src/pages/Customer/Card/CustomerConsumption.vue
@@ -104,18 +104,12 @@ function getParams() {
     };
 }
 const userParams = computed(() => {
-    const minDate = Date.vnNew();
-    minDate.setHours(0, 0, 0, 0);
-    minDate.setMonth(minDate.getMonth() - 2);
-
-    const maxDate = Date.vnNew();
-    maxDate.setHours(23, 59, 59, 59);
-
-    return {
-        campaign: campaignList.value[0]?.id,
-        from: minDate,
-        to: maxDate,
+    const campaign = campaignList.value[0]?.id;
+    const userParams = {
+        campaign,
+        ...updateDateParams(campaign, { from: Date.vnNew(), to: Date.vnNew() }),
     };
+    return userParams;
 });
 const openReportPdf = () => {
     openReport(`Clients/${route.params.id}/campaign-metrics-pdf`, getParams());
@@ -134,6 +128,28 @@ const sendCampaignMetricsEmail = ({ address }) => {
         ...getParams(),
     });
 };
+
+const updateDateParams = (value, params) => {
+    if (!value) {
+        params.from = null;
+        params.to = null;
+        return;
+    }
+    const campaign = campaignList.value.find((c) => c.id === value);
+    if (!campaign) return;
+
+    const { dated, previousDays, scopeDays } = campaign;
+    const _date = new Date(dated);
+    _date.setHours(0, 0, 0, 0);
+    params.from = new Date(
+        new Date(dated).setDate(_date.getDate() - previousDays)
+    ).toISOString();
+    _date.setHours(23, 59, 59, 59);
+    params.to = new Date(
+        new Date(dated).setDate(_date.getDate() + scopeDays)
+    ).toISOString();
+    return params;
+};
 </script>
 
 <template>
@@ -144,7 +160,6 @@ const sendCampaignMetricsEmail = ({ address }) => {
         :order="['itemTypeFk', 'itemName', 'itemSize', 'description']"
         :columns="columns"
         search-url="consumption"
-        :filter="filter"
         :user-params="userParams"
         :default-remove="false"
         :default-reset="false"
@@ -201,6 +216,7 @@ const sendCampaignMetricsEmail = ({ address }) => {
                     class="q-px-sm q-pt-none fit"
                     dense
                     option-label="code"
+                    @update:model-value="(data) => updateDateParams(data, params)"
                 >
                     <template #option="scope">
                         <QItem v-bind="scope.itemProps">