summaryrefslogtreecommitdiff
path: root/src/lib/checkout/utils/functionCheckouit.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/checkout/utils/functionCheckouit.js')
-rw-r--r--src/lib/checkout/utils/functionCheckouit.js92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/lib/checkout/utils/functionCheckouit.js b/src/lib/checkout/utils/functionCheckouit.js
new file mode 100644
index 00000000..a7fa8c5a
--- /dev/null
+++ b/src/lib/checkout/utils/functionCheckouit.js
@@ -0,0 +1,92 @@
+import { m } from 'framer-motion';
+import { min } from 'moment/moment';
+
+export function formatShipmentRange(
+ shipmentDurationRange,
+ shipmentDurationUnit,
+ productSLA
+) {
+ if (!shipmentDurationRange || !shipmentDurationUnit) {
+ return '';
+ }
+ let minRange, maxRange;
+
+ console.log('ini masuk format shipment range', shipmentDurationRange, shipmentDurationUnit, productSLA);
+
+ // Cek apakah durasi berupa range atau angka tunggal
+ if (shipmentDurationRange.includes('-')) {
+ [minRange, maxRange] = shipmentDurationRange.split(' - ').map(Number);
+ // if (minRange === maxRange) {
+ // maxRange = minRange + 3;
+ // }
+ } else {
+ minRange = Number(shipmentDurationRange); // Jika angka tunggal
+ maxRange = Number(shipmentDurationRange);
+ }
+
+ const start = new Date(); // Tanggal saat ini
+
+ let minDate, maxDate;
+
+ // Hitung estimasi berdasarkan unit waktu
+ if (shipmentDurationUnit === 'days' || shipmentDurationUnit === 'day') {
+ minDate = new Date(start);
+ minDate.setDate(start.getDate() + (minRange + productSLA));
+
+ maxDate = new Date(start);
+ maxDate.setDate(start.getDate() + (maxRange + productSLA));
+ } else if (shipmentDurationUnit === 'hours') {
+ minDate = new Date(start);
+ minDate.setDate(start.getDate() + (1 + productSLA));
+
+ maxDate = new Date(start);
+ maxDate.setDate(start.getDate() + (1 + productSLA + 1));
+ // minDate = new Date(start.getTime() + (minRange + 3) * 60 * 60 * 1000);
+ // maxDate = new Date(start.getTime() + (maxRange + 3) * 60 * 60 * 1000);
+ } else {
+ throw new Error("Unsupported unit. Please use 'days' or 'hours'.");
+ }
+
+ const minDateStr = formatDate(minDate);
+ const maxDateStr = formatDate(maxDate);
+ if (minDateStr === maxDateStr) {
+ return `Estimasi tiba ${minDateStr}`;
+ }
+ return `Estimasi tiba ${minDateStr} - ${maxDateStr}`;
+}
+
+export function getToDate(shipmentDurationRange, shipmentDurationUnit) {
+ if (!shipmentDurationRange || !shipmentDurationUnit) {
+ return '';
+ }
+ const start = new Date(); // Tanggal saat ini
+
+ let maxRange;
+
+ // Cek apakah durasi berupa range atau angka tunggal
+ if (shipmentDurationRange.includes('-')) {
+ [, maxRange] = shipmentDurationRange.split(' - ').map(Number);
+ } else {
+ maxRange = Number(shipmentDurationRange); // Jika angka tunggal
+ }
+
+ let maxDate;
+
+ // Hitung estimasi berdasarkan unit waktu
+ if (shipmentDurationUnit === 'days' || shipmentDurationUnit === 'day') {
+ maxDate = new Date(start);
+ maxDate.setDate(start.getDate() + (maxRange + 3));
+ } else if (shipmentDurationUnit === 'hours') {
+ maxDate = new Date(start.getTime() + (maxRange + 3) * 60 * 60 * 1000);
+ } else {
+ throw new Error("Unsupported unit. Please use 'days' or 'hours'.");
+ }
+
+ return maxDate.getDate();
+}
+
+function formatDate(date) {
+ const day = date.getDate();
+ const month = date.toLocaleString('default', { month: 'short' });
+ return `${day} ${month}`;
+}