@managesome/eposmatic-toolkit 0.3.8 → 0.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/constants/ANALYTICS.js +146 -0
- package/esm/constants/API_ERRORS.js +6 -0
- package/esm/constants/COMMON.js +369 -0
- package/esm/constants/DELIVERY.js +45 -0
- package/esm/constants/DIALER_ENUMS.js +11 -0
- package/esm/constants/ENUMS.js +1988 -0
- package/esm/constants/FBR_ENUMS.js +18 -0
- package/esm/constants/HUBRISE_ENUMS.js +179 -0
- package/esm/constants/INTERNAL_ENUMS.js +49 -0
- package/esm/constants/NOTIFICATIONS.js +6 -0
- package/esm/constants/ORDER.js +251 -0
- package/esm/constants/PRA_ENUMS.js +18 -0
- package/esm/constants/REPORT_WEBVIEW.js +28 -0
- package/esm/constants/RFQ_GO.js +83 -0
- package/esm/constants/SMARTPAY_ENUMS.js +55 -0
- package/esm/constants/SOCKET.js +149 -0
- package/esm/constants/SRB_ENUMS.js +13 -0
- package/esm/constants/SWITCHPAY.js +28 -0
- package/esm/constants/TENANT_ENUMS.js +90 -0
- package/esm/constants/TENANT_PURCHASE_ORDER.js +91 -0
- package/esm/constants/WALLET.js +51 -0
- package/esm/constants/WAREHOUSE_ANALYTICS.js +28 -0
- package/esm/constants/WAREHOUSE_ENUMS.js +420 -0
- package/esm/constants/WAREHOUSE_PURCHASE_ORDER.js +97 -0
- package/esm/constants/WEBVIEW.js +43 -0
- package/esm/constants/WORKER_ENDPOINTS.js +79 -0
- package/esm/constants/index.js +27 -0
- package/esm/constants/types.js +2 -0
- package/esm/helpers/fbr/generate-invoice.js +94 -0
- package/esm/helpers/fbr/index.js +1 -0
- package/esm/helpers/hubrise/hubrise-catalog.js +304 -0
- package/esm/helpers/hubrise/index.js +1 -0
- package/esm/helpers/index.js +9 -0
- package/esm/helpers/kpra/generate-invoice.js +26 -0
- package/esm/helpers/kpra/index.js +1 -0
- package/esm/helpers/order/generate-order.js +508 -0
- package/esm/helpers/order/generate-warehouse-internal-movement.js +71 -0
- package/esm/helpers/order/generate-warehouse-purchase-order-rfq.js +176 -0
- package/esm/helpers/order/generate-warehouse-purchase-order.js +187 -0
- package/esm/helpers/order/generate-warehouse-sale-order-rfq.js +176 -0
- package/esm/helpers/order/generate-warehouse-sale-order.js +149 -0
- package/esm/helpers/order/index.js +6 -0
- package/esm/helpers/pra/generate-invoice.js +94 -0
- package/esm/helpers/pra/index.js +1 -0
- package/esm/helpers/srb/generate-invoice.js +68 -0
- package/esm/helpers/srb/index.js +1 -0
- package/esm/helpers/store/all-orders-actions.js +17 -0
- package/esm/helpers/store/all-tenant-purchase-orders-actions.js +45 -0
- package/esm/helpers/store/all-warehouse-purchase-order-rfqs-actions.js +66 -0
- package/esm/helpers/store/all-warehouse-purchase-order-vendor-return-actions.js +50 -0
- package/esm/helpers/store/all-warehouse-purchase-orders-actions.js +49 -0
- package/esm/helpers/store/all-warehouse-sale-order-invoices-actions.js +132 -0
- package/esm/helpers/store/all-warehouse-sale-order-rfqs-actions.js +66 -0
- package/esm/helpers/store/all-warehouse-sale-orders-actions.js +72 -0
- package/esm/helpers/store/cart-actions.js +3563 -0
- package/esm/helpers/store/catalog-actions.js +379 -0
- package/esm/helpers/store/common-order-actions.js +937 -0
- package/esm/helpers/store/common-tenant-purchase-order-actions.js +440 -0
- package/esm/helpers/store/common-warehouse-internal-movement-actions.js +95 -0
- package/esm/helpers/store/common-warehouse-purchase-order-actions.js +530 -0
- package/esm/helpers/store/common-warehouse-purchase-order-receipt-actions.js +33 -0
- package/esm/helpers/store/common-warehouse-purchase-order-rfq-actions.js +523 -0
- package/esm/helpers/store/common-warehouse-purchase-order-vendor-return-actions.js +25 -0
- package/esm/helpers/store/common-warehouse-sale-order-actions.js +483 -0
- package/esm/helpers/store/common-warehouse-sale-order-invoice-actions.js +54 -0
- package/esm/helpers/store/common-warehouse-sale-order-rfq-actions.js +523 -0
- package/esm/helpers/store/index.js +26 -0
- package/esm/helpers/store/offline-ordering.js +251 -0
- package/esm/helpers/store/tenant-purchase-orders-cart-actions.js +379 -0
- package/esm/helpers/store/warehouse-purchase-order-rfq-cart-actions.js +360 -0
- package/esm/helpers/store/warehouse-purchase-orders-cart-actions.js +470 -0
- package/esm/helpers/store/warehouse-sale-order-rfq-cart-actions.js +359 -0
- package/esm/helpers/store/warehouse-sale-orders-cart-actions.js +456 -0
- package/esm/helpers/types/api-types.js +1 -0
- package/esm/helpers/types/cart-actions.js +1 -0
- package/esm/helpers/types/cart-helpers.js +1 -0
- package/esm/helpers/types/catalog.js +1 -0
- package/esm/helpers/types/common.js +1 -0
- package/esm/helpers/types/deliverect_menu.js +1 -0
- package/esm/helpers/types/email-templates.js +1 -0
- package/esm/helpers/types/fbr-generate-invoice.js +1 -0
- package/esm/helpers/types/halfhalf.js +1 -0
- package/esm/helpers/types/hubrise_catalog.js +1 -0
- package/esm/helpers/types/hubrise_customers.js +1 -0
- package/esm/helpers/types/hubrise_deliveries.js +1 -0
- package/esm/helpers/types/hubrise_location.js +1 -0
- package/esm/helpers/types/hubrise_orders.js +1 -0
- package/esm/helpers/types/index.js +33 -0
- package/esm/helpers/types/internal-leads.js +1 -0
- package/esm/helpers/types/inventory-catalog.js +1 -0
- package/esm/helpers/types/kpra-generate-invoice.js +1 -0
- package/esm/helpers/types/marketing.js +1 -0
- package/esm/helpers/types/order-engine.js +1 -0
- package/esm/helpers/types/pra-generate-invoice.js +1 -0
- package/esm/helpers/types/report-webview.js +1 -0
- package/esm/helpers/types/reports.js +1 -0
- package/esm/helpers/types/smartpay-apis.js +2 -0
- package/esm/helpers/types/specials.js +1 -0
- package/esm/helpers/types/split_bill.js +1 -0
- package/esm/helpers/types/srb-generate-invoice.js +1 -0
- package/esm/helpers/types/switchpay.js +1 -0
- package/esm/helpers/types/tenant-purchase-order-cart-actions.js +1 -0
- package/esm/helpers/types/warehouse-catalog.js +1 -0
- package/esm/helpers/types/warehouse-purchase-order-cart-actions.js +1 -0
- package/esm/helpers/types/warehouse-purchase-order-rfq-cart-actions.js +1 -0
- package/esm/helpers/types/warehouse-sale-order-cart-actions.js +1 -0
- package/esm/helpers/types/warehouse-sale-order-rfq-cart-actions.js +1 -0
- package/esm/helpers/types/workers.js +1 -0
- package/esm/helpers/utils/asaanbill-identifier-helpers.js +31 -0
- package/esm/helpers/utils/business-day-helpers.js +201 -0
- package/esm/helpers/utils/cart-helpers.js +156 -0
- package/esm/helpers/utils/catalog-conversion.js +129 -0
- package/esm/helpers/utils/code-redemption-helpers.js +452 -0
- package/esm/helpers/utils/color-helpers.js +31 -0
- package/esm/helpers/utils/common.js +368 -0
- package/esm/helpers/utils/crypt.js +40 -0
- package/esm/helpers/utils/customers-helpers.js +15 -0
- package/esm/helpers/utils/delivery-area-helpers.js +42 -0
- package/esm/helpers/utils/delivery-helpers.js +87 -0
- package/esm/helpers/utils/denominations-helpers.js +53 -0
- package/esm/helpers/utils/dinein.js +39 -0
- package/esm/helpers/utils/discount-helpers.js +55 -0
- package/esm/helpers/utils/email-helpers.js +111 -0
- package/esm/helpers/utils/error-helpers.js +14 -0
- package/esm/helpers/utils/expense.js +26 -0
- package/esm/helpers/utils/getFormFieldsDiff.js +15 -0
- package/esm/helpers/utils/gift-card-helpers.js +258 -0
- package/esm/helpers/utils/halfhalf-setup-helpers.js +779 -0
- package/esm/helpers/utils/hourly-comparison-report-helpers.js +851 -0
- package/esm/helpers/utils/hr-helpers.js +62 -0
- package/esm/helpers/utils/hubrise-utils.js +32 -0
- package/esm/helpers/utils/image-helpers.js +95 -0
- package/esm/helpers/utils/image-url-helpers.js +96 -0
- package/esm/helpers/utils/index.js +96 -0
- package/esm/helpers/utils/ingredients.js +26 -0
- package/esm/helpers/utils/internal-amount-helpers.js +49 -0
- package/esm/helpers/utils/inventory.js +123 -0
- package/esm/helpers/utils/inventory_entries.js +91 -0
- package/esm/helpers/utils/inventory_transactions.js +91 -0
- package/esm/helpers/utils/item-report-helpers.js +2603 -0
- package/esm/helpers/utils/items-comparison-report-helpers.js +1057 -0
- package/esm/helpers/utils/kds-helpers.js +43 -0
- package/esm/helpers/utils/location-helpers.js +13 -0
- package/esm/helpers/utils/menu-helpers.js +44 -0
- package/esm/helpers/utils/minified-catalog.js +428 -0
- package/esm/helpers/utils/offer-apply-helpers.js +613 -0
- package/esm/helpers/utils/offers-helpers.js +3351 -0
- package/esm/helpers/utils/operational-roles-helpers.js +33 -0
- package/esm/helpers/utils/order-engine.js +1409 -0
- package/esm/helpers/utils/order-history.js +208 -0
- package/esm/helpers/utils/payment-comparison-report-helpers.js +873 -0
- package/esm/helpers/utils/payment-helpers.js +302 -0
- package/esm/helpers/utils/pdf-generator.js +110 -0
- package/esm/helpers/utils/platform-catalog.js +306 -0
- package/esm/helpers/utils/platform-num.js +188 -0
- package/esm/helpers/utils/platform-specific-tenant-shifts.js +19 -0
- package/esm/helpers/utils/price-conversion.js +24 -0
- package/esm/helpers/utils/printer-helpers.js +319 -0
- package/esm/helpers/utils/public-holidays-helpers.js +8 -0
- package/esm/helpers/utils/ratings-comparison-report-helpers.js +1187 -0
- package/esm/helpers/utils/receipt-engine.js +1050 -0
- package/esm/helpers/utils/review-helpers.js +391 -0
- package/esm/helpers/utils/rider-tracking.js +12 -0
- package/esm/helpers/utils/riders-report-helpers.js +1590 -0
- package/esm/helpers/utils/sales-comparison-report-helpers.js +1259 -0
- package/esm/helpers/utils/sales-comparitive-report-helpers.js +939 -0
- package/esm/helpers/utils/sales-overview-report-helpers.js +1046 -0
- package/esm/helpers/utils/sales-summary-report-helpers.js +877 -0
- package/esm/helpers/utils/scan-code-helpers.js +31 -0
- package/esm/helpers/utils/selection-model.js +68 -0
- package/esm/helpers/utils/shift-window-helpers.js +148 -0
- package/esm/helpers/utils/simplified-catalog.js +107 -0
- package/esm/helpers/utils/simplified-halfhalf.js +101 -0
- package/esm/helpers/utils/simplified-specials.js +107 -0
- package/esm/helpers/utils/simplified-tenant.js +411 -0
- package/esm/helpers/utils/specials-helpers.js +727 -0
- package/esm/helpers/utils/tenant-calendar-helpers.js +67 -0
- package/esm/helpers/utils/tenant-cash-drawer-sessions-helpers.js +248 -0
- package/esm/helpers/utils/tenant-cash-drawers-helpers.js +31 -0
- package/esm/helpers/utils/tenant-cash-in-out-helpers.js +47 -0
- package/esm/helpers/utils/tenant-day-sessions-helpers.js +167 -0
- package/esm/helpers/utils/tenant-instore-shift-sessions-helpers.js +227 -0
- package/esm/helpers/utils/tenant-staff-helpers.js +19 -0
- package/esm/helpers/utils/tenant_cash_in_hand_transactions.js +31 -0
- package/esm/helpers/utils/time-helpers.js +142 -0
- package/esm/helpers/utils/void-items-report-helpers.js +529 -0
- package/esm/helpers/utils/void-order-items-helpers.js +32 -0
- package/esm/helpers/utils/voidations-summary-report-helpers.js +800 -0
- package/esm/helpers/utils/wallet-helpers.js +22 -0
- package/esm/helpers/utils/wallet-redemption-helpers.js +46 -0
- package/esm/helpers/utils/warehouse-catalog-conversion.js +117 -0
- package/esm/helpers/utils/warehouse-inventory.js +59 -0
- package/esm/helpers/utils/warehouse-purchase-order-engine.js +27 -0
- package/esm/helpers/utils/warehouse-purchase-order-receipt-engine.js +205 -0
- package/esm/helpers/utils/warehouse-purchase-order-x-report-helpers.js +1432 -0
- package/esm/helpers/utils/warehouse-sale-order-engine.js +27 -0
- package/esm/helpers/utils/warehouse-sale-order-receipt-engine.js +207 -0
- package/esm/helpers/utils/warehouse-sale-order-x-report-helpers.js +1 -0
- package/esm/helpers/utils/warehouse-sale-x-report-helpers.js +1434 -0
- package/esm/helpers/utils/warehouse-settings-helpers.js +37 -0
- package/esm/helpers/utils/warehouse-void-purchase-order-items-helpers.js +34 -0
- package/esm/helpers/utils/warehouse-void-purchase-order-items-report-helpers.js +18 -0
- package/esm/helpers/utils/warehouse-void-sale-order-items-helpers.js +34 -0
- package/esm/helpers/utils/warehouse-void-sale-order-items-report-helpers.js +18 -0
- package/esm/helpers/utils/warehouse_inventory_transactions.js +91 -0
- package/esm/helpers/utils/x-report-helpers.js +1850 -0
- package/esm/index.js +3 -0
- package/esm/schemas/index.js +2 -0
- package/esm/schemas/models/app_configs.js +36 -0
- package/esm/schemas/models/applied_order_discounts.js +103 -0
- package/esm/schemas/models/brands.js +56 -0
- package/esm/schemas/models/categories.js +74 -0
- package/esm/schemas/models/close_dates.js +63 -0
- package/esm/schemas/models/common-models.js +449 -0
- package/esm/schemas/models/courses.js +62 -0
- package/esm/schemas/models/customer_redemptions.js +82 -0
- package/esm/schemas/models/customers.js +269 -0
- package/esm/schemas/models/deliveries.js +137 -0
- package/esm/schemas/models/delivery_areas.js +97 -0
- package/esm/schemas/models/dinein_floors.js +55 -0
- package/esm/schemas/models/dinein_tables.js +97 -0
- package/esm/schemas/models/dinein_tables_fingerprints.js +96 -0
- package/esm/schemas/models/discounts.js +87 -0
- package/esm/schemas/models/expense_categories.js +61 -0
- package/esm/schemas/models/expense_details.js +130 -0
- package/esm/schemas/models/gift_cards.js +342 -0
- package/esm/schemas/models/halfhalf_setup.js +142 -0
- package/esm/schemas/models/index.js +111 -0
- package/esm/schemas/models/internal_customers.js +139 -0
- package/esm/schemas/models/internal_invoices.js +119 -0
- package/esm/schemas/models/internal_leads.js +78 -0
- package/esm/schemas/models/internal_operational_roles.js +85 -0
- package/esm/schemas/models/internal_staffs.js +65 -0
- package/esm/schemas/models/inventories.js +103 -0
- package/esm/schemas/models/inventory_categories.js +52 -0
- package/esm/schemas/models/inventory_transactions.js +143 -0
- package/esm/schemas/models/item_transactions.js +117 -0
- package/esm/schemas/models/items.js +341 -0
- package/esm/schemas/models/marketing_fingerprints.js +92 -0
- package/esm/schemas/models/menus.js +66 -0
- package/esm/schemas/models/messaging_devices.js +99 -0
- package/esm/schemas/models/modifier_extras.js +120 -0
- package/esm/schemas/models/offers.js +470 -0
- package/esm/schemas/models/offers_fingerprints.js +61 -0
- package/esm/schemas/models/operational_roles.js +155 -0
- package/esm/schemas/models/orders.js +1051 -0
- package/esm/schemas/models/printer_settings.js +245 -0
- package/esm/schemas/models/public_holidays.js +58 -0
- package/esm/schemas/models/reservations.js +127 -0
- package/esm/schemas/models/reviews.js +145 -0
- package/esm/schemas/models/rewards_usage.js +61 -0
- package/esm/schemas/models/riders.js +75 -0
- package/esm/schemas/models/specials.js +169 -0
- package/esm/schemas/models/staff_shifts.js +66 -0
- package/esm/schemas/models/tenant-activity-logs.js +126 -0
- package/esm/schemas/models/tenant_active_jobs.js +77 -0
- package/esm/schemas/models/tenant_calendar.js +89 -0
- package/esm/schemas/models/tenant_cash_drawer_sessions.js +208 -0
- package/esm/schemas/models/tenant_cash_drawers.js +60 -0
- package/esm/schemas/models/tenant_cash_in_hand_transactions.js +69 -0
- package/esm/schemas/models/tenant_cash_in_out.js +104 -0
- package/esm/schemas/models/tenant_clients.js +66 -0
- package/esm/schemas/models/tenant_day_sessions.js +166 -0
- package/esm/schemas/models/tenant_hr_attendance_records.js +87 -0
- package/esm/schemas/models/tenant_hr_biometric_enrollments.js +69 -0
- package/esm/schemas/models/tenant_hr_departments.js +42 -0
- package/esm/schemas/models/tenant_hr_documents.js +71 -0
- package/esm/schemas/models/tenant_hr_employees.js +172 -0
- package/esm/schemas/models/tenant_hr_leave_requests.js +80 -0
- package/esm/schemas/models/tenant_hr_payrolls.js +131 -0
- package/esm/schemas/models/tenant_hr_performance_reviews.js +58 -0
- package/esm/schemas/models/tenant_hr_policies.js +57 -0
- package/esm/schemas/models/tenant_hr_positions.js +48 -0
- package/esm/schemas/models/tenant_hr_shift_schedules.js +84 -0
- package/esm/schemas/models/tenant_hr_shifts.js +64 -0
- package/esm/schemas/models/tenant_instore_shift_sessions.js +204 -0
- package/esm/schemas/models/tenant_instore_shifts.js +63 -0
- package/esm/schemas/models/tenant_inventory_department_transactions.js +116 -0
- package/esm/schemas/models/tenant_inventory_department_utilities.js +85 -0
- package/esm/schemas/models/tenant_inventory_departments.js +55 -0
- package/esm/schemas/models/tenant_inventory_entries.js +131 -0
- package/esm/schemas/models/tenant_metadata.js +88 -0
- package/esm/schemas/models/tenant_order_item_instructions.js +66 -0
- package/esm/schemas/models/tenant_purchase_order_vendor_returns.js +80 -0
- package/esm/schemas/models/tenant_purchase_orders.js +158 -0
- package/esm/schemas/models/tenant_shifts.js +57 -0
- package/esm/schemas/models/tenant_staffs.js +129 -0
- package/esm/schemas/models/tenant_vendors.js +99 -0
- package/esm/schemas/models/tenants.js +811 -0
- package/esm/schemas/models/terminal_devices.js +63 -0
- package/esm/schemas/models/tokens.js +73 -0
- package/esm/schemas/models/track_order_numbers.js +42 -0
- package/esm/schemas/models/void_order_items.js +210 -0
- package/esm/schemas/models/vouchers.js +101 -0
- package/esm/schemas/models/wallet_transactions.js +128 -0
- package/esm/schemas/models/wallets.js +44 -0
- package/esm/schemas/models/warehouse-activity-logs.js +144 -0
- package/esm/schemas/models/warehouse_categories.js +83 -0
- package/esm/schemas/models/warehouse_clients.js +81 -0
- package/esm/schemas/models/warehouse_communications.js +226 -0
- package/esm/schemas/models/warehouse_internal_movements.js +463 -0
- package/esm/schemas/models/warehouse_inventory_entries.js +125 -0
- package/esm/schemas/models/warehouse_inventory_lot_serial_numbers.js +188 -0
- package/esm/schemas/models/warehouse_inventory_transactions.js +133 -0
- package/esm/schemas/models/warehouse_item_transactions.js +97 -0
- package/esm/schemas/models/warehouse_items.js +173 -0
- package/esm/schemas/models/warehouse_operational_roles.js +87 -0
- package/esm/schemas/models/warehouse_purchase_order_receipts.js +163 -0
- package/esm/schemas/models/warehouse_purchase_order_rfqs.js +339 -0
- package/esm/schemas/models/warehouse_purchase_order_vendor_returns.js +199 -0
- package/esm/schemas/models/warehouse_purchase_orders.js +334 -0
- package/esm/schemas/models/warehouse_sale_deliveries.js +155 -0
- package/esm/schemas/models/warehouse_sale_invoices.js +229 -0
- package/esm/schemas/models/warehouse_sale_order_rfqs.js +340 -0
- package/esm/schemas/models/warehouse_sale_orders.js +322 -0
- package/esm/schemas/models/warehouse_staffs.js +104 -0
- package/esm/schemas/models/warehouse_track_order_numbers.js +73 -0
- package/esm/schemas/models/warehouse_vendors.js +99 -0
- package/esm/schemas/models/warehouse_void_purchase_order_items.js +174 -0
- package/esm/schemas/models/warehouse_void_sale_order_items.js +174 -0
- package/esm/schemas/models/warehouses.js +253 -0
- package/esm/schemas/types/app_configs.js +1 -0
- package/esm/schemas/types/applied_order_discounts.js +1 -0
- package/esm/schemas/types/brands.js +1 -0
- package/esm/schemas/types/categories.js +1 -0
- package/esm/schemas/types/close_dates.js +1 -0
- package/esm/schemas/types/common.js +1 -0
- package/esm/schemas/types/courses.js +1 -0
- package/esm/schemas/types/customer_redemptions.js +1 -0
- package/esm/schemas/types/customers.js +1 -0
- package/esm/schemas/types/deliveries.js +1 -0
- package/esm/schemas/types/delivery_areas.js +1 -0
- package/esm/schemas/types/dinein_floors.js +1 -0
- package/esm/schemas/types/dinein_tables.js +1 -0
- package/esm/schemas/types/dinein_tables_fingerprints.js +1 -0
- package/esm/schemas/types/discounts.js +1 -0
- package/esm/schemas/types/expense_categories.js +1 -0
- package/esm/schemas/types/expense_details.js +1 -0
- package/esm/schemas/types/gift_cards.js +1 -0
- package/esm/schemas/types/halfhalf_setup.js +1 -0
- package/esm/schemas/types/index.js +113 -0
- package/esm/schemas/types/internal_customers.js +1 -0
- package/esm/schemas/types/internal_invoices.js +1 -0
- package/esm/schemas/types/internal_leads.js +1 -0
- package/esm/schemas/types/internal_operational_roles.js +16 -0
- package/esm/schemas/types/internal_staffs.js +1 -0
- package/esm/schemas/types/inventories.js +1 -0
- package/esm/schemas/types/inventory_categories.js +1 -0
- package/esm/schemas/types/inventory_department_transactions.js +1 -0
- package/esm/schemas/types/inventory_department_utilities.js +1 -0
- package/esm/schemas/types/inventory_departments.js +1 -0
- package/esm/schemas/types/inventory_transactions.js +1 -0
- package/esm/schemas/types/item_transactions.js +1 -0
- package/esm/schemas/types/items.js +1 -0
- package/esm/schemas/types/marketing_fingerprints.js +1 -0
- package/esm/schemas/types/menus.js +1 -0
- package/esm/schemas/types/messaging_devices.js +1 -0
- package/esm/schemas/types/modifier_extras.js +1 -0
- package/esm/schemas/types/multi_tenants.js +1 -0
- package/esm/schemas/types/offers.js +1 -0
- package/esm/schemas/types/offers_fingerprints.js +1 -0
- package/esm/schemas/types/operational_roles.js +15 -0
- package/esm/schemas/types/orders.js +1 -0
- package/esm/schemas/types/printer_settings.js +1 -0
- package/esm/schemas/types/public_holidays.js +1 -0
- package/esm/schemas/types/reservations.js +1 -0
- package/esm/schemas/types/reviews.js +1 -0
- package/esm/schemas/types/rewards_usage.js +1 -0
- package/esm/schemas/types/riders.js +1 -0
- package/esm/schemas/types/specials.js +1 -0
- package/esm/schemas/types/staff_shifts.js +1 -0
- package/esm/schemas/types/tenant-activity-logs.js +1 -0
- package/esm/schemas/types/tenant_active_jobs.js +1 -0
- package/esm/schemas/types/tenant_calendar.js +1 -0
- package/esm/schemas/types/tenant_cash_drawer_sessions.js +1 -0
- package/esm/schemas/types/tenant_cash_drawers.js +1 -0
- package/esm/schemas/types/tenant_cash_in_hand_transactions.js +1 -0
- package/esm/schemas/types/tenant_cash_in_out.js +1 -0
- package/esm/schemas/types/tenant_clients.js +1 -0
- package/esm/schemas/types/tenant_day_sessions.js +1 -0
- package/esm/schemas/types/tenant_hr_attendance_records.js +1 -0
- package/esm/schemas/types/tenant_hr_biometric_enrollments.js +1 -0
- package/esm/schemas/types/tenant_hr_departments.js +1 -0
- package/esm/schemas/types/tenant_hr_documents.js +1 -0
- package/esm/schemas/types/tenant_hr_employees.js +1 -0
- package/esm/schemas/types/tenant_hr_leave_requests.js +1 -0
- package/esm/schemas/types/tenant_hr_payrolls.js +1 -0
- package/esm/schemas/types/tenant_hr_performance_reviews.js +1 -0
- package/esm/schemas/types/tenant_hr_policies.js +1 -0
- package/esm/schemas/types/tenant_hr_positions.js +1 -0
- package/esm/schemas/types/tenant_hr_shift_schedules.js +1 -0
- package/esm/schemas/types/tenant_hr_shifts.js +1 -0
- package/esm/schemas/types/tenant_instore_shift_sessions.js +1 -0
- package/esm/schemas/types/tenant_instore_shifts.js +1 -0
- package/esm/schemas/types/tenant_inventory_entries.js +1 -0
- package/esm/schemas/types/tenant_metadata.js +1 -0
- package/esm/schemas/types/tenant_order_item_instructions.js +1 -0
- package/esm/schemas/types/tenant_purchase_order_vendor_returns.js +1 -0
- package/esm/schemas/types/tenant_purchase_orders.js +1 -0
- package/esm/schemas/types/tenant_shifts.js +1 -0
- package/esm/schemas/types/tenant_staffs.js +1 -0
- package/esm/schemas/types/tenant_vendors.js +1 -0
- package/esm/schemas/types/tenants.js +1 -0
- package/esm/schemas/types/terminal_devices.js +1 -0
- package/esm/schemas/types/tokens.js +1 -0
- package/esm/schemas/types/track_order_numbers.js +1 -0
- package/esm/schemas/types/void_order_items.js +1 -0
- package/esm/schemas/types/vouchers.js +1 -0
- package/esm/schemas/types/wallet_transactions.js +1 -0
- package/esm/schemas/types/wallets.js +1 -0
- package/esm/schemas/types/warehouse-activity-logs.js +1 -0
- package/esm/schemas/types/warehouse_categories.js +1 -0
- package/esm/schemas/types/warehouse_clients.js +1 -0
- package/esm/schemas/types/warehouse_communications.js +1 -0
- package/esm/schemas/types/warehouse_internal_movements.js +1 -0
- package/esm/schemas/types/warehouse_inventory_entries.js +1 -0
- package/esm/schemas/types/warehouse_inventory_lot_serial_numbers.js +1 -0
- package/esm/schemas/types/warehouse_inventory_transactions.js +1 -0
- package/esm/schemas/types/warehouse_item_transactions.js +1 -0
- package/esm/schemas/types/warehouse_items.js +1 -0
- package/esm/schemas/types/warehouse_operational_roles.js +15 -0
- package/esm/schemas/types/warehouse_purchase_order_receipts.js +18 -0
- package/esm/schemas/types/warehouse_purchase_order_rfqs.js +1 -0
- package/esm/schemas/types/warehouse_purchase_order_vendor_returns.js +1 -0
- package/esm/schemas/types/warehouse_purchase_orders.js +1 -0
- package/esm/schemas/types/warehouse_sale_deliveries.js +1 -0
- package/esm/schemas/types/warehouse_sale_invoices.js +1 -0
- package/esm/schemas/types/warehouse_sale_order_rfqs.js +1 -0
- package/esm/schemas/types/warehouse_sale_orders.js +1 -0
- package/esm/schemas/types/warehouse_staffs.js +1 -0
- package/esm/schemas/types/warehouse_track_order_numbers.js +1 -0
- package/esm/schemas/types/warehouse_vendors.js +1 -0
- package/esm/schemas/types/warehouse_void_purchase_order_items.js +1 -0
- package/esm/schemas/types/warehouse_void_sale_order_items.js +1 -0
- package/esm/schemas/types/warehouses.js +1 -0
- package/package.json +6 -3
|
@@ -0,0 +1,3563 @@
|
|
|
1
|
+
import ObjectID from 'bson-objectid';
|
|
2
|
+
import { isEqual } from 'lodash';
|
|
3
|
+
import { DISCOUNT_APPLICABILITY_ENUM, GIFT_CARD_REWARD_TYPE_ENUM, OFFER_NO_LONGER_QUALIFIED_VOID_REASON, OFFER_REWARD_TYPE_ENUM, ORDER_STATUS_ENUM, ORDER_TYPE_ENUM, PLATFORMS_BY_ORDER_TYPE, SORT_TYPE_ENUM, TAX_INVOICE_PROVIDER_CHARGES_MAP, TAX_INVOICE_PROVIDER_ENUM, WALLET_CASHBACK_LOYALTY_DISCOUNT_ID, WALLET_CASHBACK_LOYALTY_DISCOUNT_NAME, WALLET_CASHBACK_LOYALTY_REFERENCE, } from '../../constants';
|
|
4
|
+
import { calculateEligibleOfferDiscountDetails, getCappedOfferDiscountAmount, getCardChargesAndAmountCollected, getDeliveryAmt, getNetItemsValueAfterItemDiscount, getPercentageAmount, getPriceEnum, parseFloatCeil, parseNum, } from '../utils';
|
|
5
|
+
import { buildClearVoucherDiscountDetails } from '../utils/offers-helpers';
|
|
6
|
+
import { convertItemToWorkableItem, convertOptionToWorkableOption } from '../utils/platform-catalog';
|
|
7
|
+
export function getItemQuantityChangeResults({ prevQuantity, newQuantity, prevItemDiscountDetails, }) {
|
|
8
|
+
let item_discount_details = prevItemDiscountDetails;
|
|
9
|
+
const getScaledFixedDiscount = (discountValue, isPercentage) => {
|
|
10
|
+
if (isPercentage)
|
|
11
|
+
return discountValue;
|
|
12
|
+
if (prevQuantity <= 0)
|
|
13
|
+
return discountValue;
|
|
14
|
+
const perUnitDiscount = discountValue / prevQuantity;
|
|
15
|
+
return parseNum(perUnitDiscount * newQuantity);
|
|
16
|
+
};
|
|
17
|
+
let hasUpdatedDiscount = false;
|
|
18
|
+
if (item_discount_details != null &&
|
|
19
|
+
item_discount_details.special_discount_id != null &&
|
|
20
|
+
item_discount_details.special_discount != null) {
|
|
21
|
+
item_discount_details = {
|
|
22
|
+
...item_discount_details,
|
|
23
|
+
special_discount: getScaledFixedDiscount(item_discount_details.special_discount, item_discount_details.is_special_discount_percentage),
|
|
24
|
+
};
|
|
25
|
+
hasUpdatedDiscount = true;
|
|
26
|
+
}
|
|
27
|
+
if (item_discount_details != null &&
|
|
28
|
+
item_discount_details.loyalty_discount_id != null &&
|
|
29
|
+
item_discount_details.loyalty_discount != null) {
|
|
30
|
+
item_discount_details = {
|
|
31
|
+
...item_discount_details,
|
|
32
|
+
loyalty_discount: getScaledFixedDiscount(item_discount_details.loyalty_discount, item_discount_details.is_loyalty_discount_percentage),
|
|
33
|
+
};
|
|
34
|
+
hasUpdatedDiscount = true;
|
|
35
|
+
}
|
|
36
|
+
if (item_discount_details != null &&
|
|
37
|
+
item_discount_details.voucher_discount_id != null &&
|
|
38
|
+
item_discount_details.voucher_discount != null) {
|
|
39
|
+
item_discount_details = {
|
|
40
|
+
...item_discount_details,
|
|
41
|
+
voucher_discount: getScaledFixedDiscount(item_discount_details.voucher_discount, item_discount_details.is_voucher_discount_percentage),
|
|
42
|
+
};
|
|
43
|
+
hasUpdatedDiscount = true;
|
|
44
|
+
}
|
|
45
|
+
if (item_discount_details != null && hasUpdatedDiscount) {
|
|
46
|
+
item_discount_details = {
|
|
47
|
+
...item_discount_details,
|
|
48
|
+
total_discount: 0,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return item_discount_details;
|
|
52
|
+
}
|
|
53
|
+
// ==========================================================><====================================================
|
|
54
|
+
export function getModifier(_id, allModifiers) {
|
|
55
|
+
if (!allModifiers)
|
|
56
|
+
return null;
|
|
57
|
+
let foundModifier = null;
|
|
58
|
+
let found = false;
|
|
59
|
+
const recursive = (modifiers) => {
|
|
60
|
+
if (found)
|
|
61
|
+
return;
|
|
62
|
+
outer: for (let i = 0; i < modifiers.length; i++) {
|
|
63
|
+
if (found)
|
|
64
|
+
return;
|
|
65
|
+
const modifier = modifiers[i];
|
|
66
|
+
if (modifier._id === _id) {
|
|
67
|
+
foundModifier = modifier;
|
|
68
|
+
found = true;
|
|
69
|
+
break outer;
|
|
70
|
+
}
|
|
71
|
+
for (let j = 0; j < modifier.options.length; j++) {
|
|
72
|
+
const option = modifier.options[j];
|
|
73
|
+
if (option.modifiers != null && option.modifiers.length > 0) {
|
|
74
|
+
recursive(option.modifiers);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
recursive(allModifiers);
|
|
80
|
+
return foundModifier;
|
|
81
|
+
}
|
|
82
|
+
/* ======================><====================== */
|
|
83
|
+
export function syncOrderCoursesWithOrderItemsInCart(order, courses) {
|
|
84
|
+
let dinein_details = order.dinein_details;
|
|
85
|
+
// we need to check if the order is dinein
|
|
86
|
+
if (PLATFORMS_BY_ORDER_TYPE[ORDER_TYPE_ENUM.DINE_IN].includes(order.platform)) {
|
|
87
|
+
// we need to check if the dineinCourses exist
|
|
88
|
+
if (dinein_details?.courses != null) {
|
|
89
|
+
// we get all the items ids from the order
|
|
90
|
+
const categories = order.categories ?? [];
|
|
91
|
+
const itemsIds = [];
|
|
92
|
+
for (let i = 0; i < categories.length; i++) {
|
|
93
|
+
const category = categories[i];
|
|
94
|
+
const items = category.items ?? [];
|
|
95
|
+
for (let j = 0; j < items.length; j++) {
|
|
96
|
+
itemsIds.push(items[j]._id);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
// we delete the dineinCourses that are not in the itemsObj
|
|
100
|
+
let dineinCourses = [];
|
|
101
|
+
// we loop through the dineinCourses
|
|
102
|
+
for (let i = 0; i < dinein_details.courses.length; i++) {
|
|
103
|
+
// we get the course
|
|
104
|
+
const course = dinein_details.courses[i];
|
|
105
|
+
// we filter the items ids that are in the course
|
|
106
|
+
const courseItemIds = course.item_ids.filter((id) => itemsIds.includes(id));
|
|
107
|
+
if (courseItemIds.length === 0)
|
|
108
|
+
continue;
|
|
109
|
+
dineinCourses.push({
|
|
110
|
+
...course,
|
|
111
|
+
item_ids: Array.from(new Set(courseItemIds)),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
// if any of the items are not in the dineinCourses we need to assign them to the default
|
|
115
|
+
// this would ensure that all the items are in the dineinCourses
|
|
116
|
+
const itemsNotInCourses = itemsIds.filter((id) => !dineinCourses.some((course) => course.item_ids.includes(id)));
|
|
117
|
+
if (itemsNotInCourses.length > 0) {
|
|
118
|
+
// we check if there is a default course
|
|
119
|
+
const defaultCourse = courses.find((course) => course.default);
|
|
120
|
+
if (defaultCourse != null) {
|
|
121
|
+
// we check if the default course is already in the dineinCourses
|
|
122
|
+
const defaultCourseInDineinCourses = dineinCourses.find((course) => course.course_id === defaultCourse._id);
|
|
123
|
+
if (defaultCourseInDineinCourses) {
|
|
124
|
+
// we add the items to the default course
|
|
125
|
+
defaultCourseInDineinCourses.item_ids = Array.from(new Set(defaultCourseInDineinCourses.item_ids.concat(itemsNotInCourses)));
|
|
126
|
+
}
|
|
127
|
+
// if the default course is not in the dineinCourses we add it
|
|
128
|
+
else {
|
|
129
|
+
dineinCourses.push({
|
|
130
|
+
name: defaultCourse.name,
|
|
131
|
+
course_id: defaultCourse._id,
|
|
132
|
+
item_ids: Array.from(new Set(itemsNotInCourses)),
|
|
133
|
+
called: false,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
// if there is no default course we add the items to the first course
|
|
139
|
+
const firstCourse = dineinCourses[0];
|
|
140
|
+
firstCourse.item_ids = Array.from(new Set(firstCourse.item_ids.concat(itemsNotInCourses)));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
// we need to find totalCoursesAssigned, if totalCoursesAssigned is not equal to itemsIds.length
|
|
144
|
+
// This would mean there is an error in the algorithm
|
|
145
|
+
// So we would set the dineinCourses to null
|
|
146
|
+
const totalCoursesAssigned = dineinCourses.reduce((acc, course) => acc + course.item_ids.length, 0);
|
|
147
|
+
if (totalCoursesAssigned === 0 || totalCoursesAssigned !== itemsIds.length) {
|
|
148
|
+
dineinCourses = null;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
// we need to sort the dineinCourses by the sort_id of the course mentioned in courses array
|
|
152
|
+
const sortIds = courses.reduce((acc, course) => {
|
|
153
|
+
acc[course._id] = course.sort_id;
|
|
154
|
+
return acc;
|
|
155
|
+
}, {});
|
|
156
|
+
dineinCourses.sort((a, b) => sortIds[a.course_id] - sortIds[b.course_id]);
|
|
157
|
+
}
|
|
158
|
+
dinein_details = {
|
|
159
|
+
...dinein_details,
|
|
160
|
+
courses: dineinCourses,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
dinein_details = null;
|
|
166
|
+
}
|
|
167
|
+
const newOrder = {
|
|
168
|
+
...order,
|
|
169
|
+
dinein_details,
|
|
170
|
+
};
|
|
171
|
+
return newOrder;
|
|
172
|
+
}
|
|
173
|
+
/* ======================><====================== */
|
|
174
|
+
export function calculateItemModifiersPrice(item) {
|
|
175
|
+
let modifiersTotal = 0;
|
|
176
|
+
const calculations = (modifiers) => {
|
|
177
|
+
const modifiersLength = modifiers.length;
|
|
178
|
+
for (let i = 0; i < modifiersLength; i++) {
|
|
179
|
+
const ex = modifiers[i];
|
|
180
|
+
const options = ex.options;
|
|
181
|
+
const optionsLength = options.length;
|
|
182
|
+
for (let j = 0; j < optionsLength; j++) {
|
|
183
|
+
const op = options[j];
|
|
184
|
+
if (op.is_free)
|
|
185
|
+
continue;
|
|
186
|
+
modifiersTotal += op.price * op.quantity;
|
|
187
|
+
calculations(op.modifiers ?? []);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
calculations(item.modifiers ?? []);
|
|
192
|
+
return modifiersTotal;
|
|
193
|
+
}
|
|
194
|
+
/* ======================><====================== */
|
|
195
|
+
export function getDiscountDetails(item_discount_details, itemTotalWithTaxAmount) {
|
|
196
|
+
if (!item_discount_details)
|
|
197
|
+
return null;
|
|
198
|
+
const { special_discount_id, special_discount, is_special_discount_percentage, loyalty_discount_id, loyalty_discount, is_loyalty_discount_percentage, voucher_discount_id, voucher_discount, is_voucher_discount_percentage, } = item_discount_details;
|
|
199
|
+
const discount_details = {
|
|
200
|
+
...item_discount_details,
|
|
201
|
+
total_discount: 0,
|
|
202
|
+
};
|
|
203
|
+
let remainingPositiveDiscountableAmount = parseNum(Math.max(0, itemTotalWithTaxAmount));
|
|
204
|
+
const consumePositiveDiscountAmount = (discountAmount) => {
|
|
205
|
+
if (discountAmount <= 0)
|
|
206
|
+
return;
|
|
207
|
+
remainingPositiveDiscountableAmount = parseNum(Math.max(0, remainingPositiveDiscountableAmount - discountAmount));
|
|
208
|
+
};
|
|
209
|
+
const clearSpecialDiscountSlot = () => {
|
|
210
|
+
discount_details.special_discount_id = undefined;
|
|
211
|
+
discount_details.special_discount_name = undefined;
|
|
212
|
+
discount_details.special_discount = undefined;
|
|
213
|
+
discount_details.is_special_discount_percentage = false;
|
|
214
|
+
discount_details.special_discount_reason = undefined;
|
|
215
|
+
discount_details.special_reference_number = undefined;
|
|
216
|
+
};
|
|
217
|
+
const clearLoyaltyDiscountSlot = () => {
|
|
218
|
+
discount_details.loyalty_discount_id = undefined;
|
|
219
|
+
discount_details.loyalty_discount_name = undefined;
|
|
220
|
+
discount_details.loyalty_discount = undefined;
|
|
221
|
+
discount_details.is_loyalty_discount_percentage = false;
|
|
222
|
+
discount_details.loyalty_discount_reason = undefined;
|
|
223
|
+
discount_details.loyalty_reference_number = undefined;
|
|
224
|
+
};
|
|
225
|
+
const clearVoucherDiscountSlot = () => {
|
|
226
|
+
discount_details.voucher_discount_id = undefined;
|
|
227
|
+
discount_details.voucher_discount_name = undefined;
|
|
228
|
+
discount_details.voucher_discount = undefined;
|
|
229
|
+
discount_details.is_voucher_discount_percentage = false;
|
|
230
|
+
discount_details.voucher_discount_reason = undefined;
|
|
231
|
+
discount_details.voucher_reference_number = undefined;
|
|
232
|
+
};
|
|
233
|
+
if (special_discount_id && special_discount) {
|
|
234
|
+
const specialDiscountAmount = is_special_discount_percentage
|
|
235
|
+
? getPercentageAmount(itemTotalWithTaxAmount, special_discount)
|
|
236
|
+
: special_discount;
|
|
237
|
+
if (specialDiscountAmount > remainingPositiveDiscountableAmount) {
|
|
238
|
+
clearSpecialDiscountSlot();
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
discount_details.total_discount += specialDiscountAmount;
|
|
242
|
+
consumePositiveDiscountAmount(specialDiscountAmount);
|
|
243
|
+
}
|
|
244
|
+
// NOTE: we only keep the slot if it can be fully applied.
|
|
245
|
+
// Partial application is avoided so stale/invalid discounts are removed explicitly.
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
delete discount_details.special_discount_reason;
|
|
249
|
+
}
|
|
250
|
+
if (loyalty_discount_id && loyalty_discount) {
|
|
251
|
+
const loyaltyDiscountAmount = is_loyalty_discount_percentage
|
|
252
|
+
? getPercentageAmount(itemTotalWithTaxAmount, loyalty_discount)
|
|
253
|
+
: loyalty_discount;
|
|
254
|
+
if (loyaltyDiscountAmount > remainingPositiveDiscountableAmount) {
|
|
255
|
+
clearLoyaltyDiscountSlot();
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
discount_details.total_discount += loyaltyDiscountAmount;
|
|
259
|
+
consumePositiveDiscountAmount(loyaltyDiscountAmount);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
delete discount_details.loyalty_discount_reason;
|
|
264
|
+
}
|
|
265
|
+
if (voucher_discount_id && voucher_discount) {
|
|
266
|
+
const voucherDiscountAmount = is_voucher_discount_percentage
|
|
267
|
+
? getPercentageAmount(itemTotalWithTaxAmount, voucher_discount)
|
|
268
|
+
: voucher_discount;
|
|
269
|
+
if (voucherDiscountAmount > remainingPositiveDiscountableAmount) {
|
|
270
|
+
clearVoucherDiscountSlot();
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
discount_details.total_discount += voucherDiscountAmount;
|
|
274
|
+
consumePositiveDiscountAmount(voucherDiscountAmount);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
delete discount_details.voucher_discount_reason;
|
|
279
|
+
}
|
|
280
|
+
discount_details.total_discount = parseNum(discount_details.total_discount);
|
|
281
|
+
return discount_details;
|
|
282
|
+
}
|
|
283
|
+
/* ======================><====================== */
|
|
284
|
+
export function calculateItemTotalWithTax(item) {
|
|
285
|
+
const modifiersTotal = calculateItemModifiersPrice(item);
|
|
286
|
+
// Item total without tax amount and without discount amount
|
|
287
|
+
// total is 20
|
|
288
|
+
const itemTotalWithoutTaxAndWithoutDiscount = (item.price + modifiersTotal) * item.quantity;
|
|
289
|
+
// Item discount details
|
|
290
|
+
// discount is 50%
|
|
291
|
+
// 50% of 20 is 10
|
|
292
|
+
const item_discount_details = getDiscountDetails(item.item_discount_details, itemTotalWithoutTaxAndWithoutDiscount);
|
|
293
|
+
// Item total without tax amount and with discount amount
|
|
294
|
+
// 20 - 10 = 10
|
|
295
|
+
// 10 is the item total without tax amount and with discount amount
|
|
296
|
+
const itemTotalWithoutTaxAndWithDiscount = itemTotalWithoutTaxAndWithoutDiscount - (item_discount_details?.total_discount ?? 0);
|
|
297
|
+
// Item tax amount with discount amount
|
|
298
|
+
// tax is 50%
|
|
299
|
+
// tax amount of 50% of 10 is 5
|
|
300
|
+
const itemTaxAmount = getPercentageAmount(itemTotalWithoutTaxAndWithDiscount, item.tax_rate);
|
|
301
|
+
const itemSubtotal = parseNum(itemTotalWithoutTaxAndWithDiscount + itemTaxAmount);
|
|
302
|
+
return { itemSubtotal, item_discount_details };
|
|
303
|
+
}
|
|
304
|
+
export function getServiceFeeAmount(total, service_fee = 0, is_service_fee_percentage = false) {
|
|
305
|
+
// Add service fee to orderSubtotal
|
|
306
|
+
// Service fee is represented in percentage. We convert it to decimal
|
|
307
|
+
let serviceFeeAmt = service_fee ?? 0;
|
|
308
|
+
if (service_fee && service_fee > 0 && is_service_fee_percentage) {
|
|
309
|
+
serviceFeeAmt = getPercentageAmount(total, service_fee);
|
|
310
|
+
}
|
|
311
|
+
// Add service fee as a fixed amount
|
|
312
|
+
return parseNum(serviceFeeAmt);
|
|
313
|
+
}
|
|
314
|
+
export function getSurchargeAmount(total, surcharge = 0, is_surcharge_percentage = false) {
|
|
315
|
+
// Add holiday surcharge to orderSubtotal
|
|
316
|
+
let surchargeAmt = surcharge ?? 0;
|
|
317
|
+
if (surcharge && surcharge > 0 && is_surcharge_percentage) {
|
|
318
|
+
// Add holiday surcharge to orderSubtotal
|
|
319
|
+
surchargeAmt = getPercentageAmount(total, surchargeAmt);
|
|
320
|
+
}
|
|
321
|
+
return parseNum(surchargeAmt);
|
|
322
|
+
}
|
|
323
|
+
export function calculateGstForItem({ subtotal, totalOrderDiscount, applyGSTOnOriginalPrice, is_gst_inclusive, gst, totalItemsDiscount, grossAmount, netAmount, payablePrice, originalItemsPrice, surcharge, is_surcharge_taxable, is_surcharge_percentage, service_fee, is_service_fee_taxable, is_service_fee_percentage, apply_service_fee_on_original_price, }) {
|
|
324
|
+
let grand_total = subtotal;
|
|
325
|
+
// ===============================><===============================
|
|
326
|
+
if (is_surcharge_taxable) {
|
|
327
|
+
let surcharge_amount = 0;
|
|
328
|
+
if (is_surcharge_percentage) {
|
|
329
|
+
surcharge_amount = getSurchargeAmount(netAmount, surcharge, is_surcharge_percentage);
|
|
330
|
+
}
|
|
331
|
+
else if (surcharge) {
|
|
332
|
+
const proportion = netAmount / payablePrice;
|
|
333
|
+
surcharge_amount = proportion * surcharge;
|
|
334
|
+
}
|
|
335
|
+
grand_total += surcharge_amount;
|
|
336
|
+
}
|
|
337
|
+
if (is_service_fee_taxable) {
|
|
338
|
+
const p = apply_service_fee_on_original_price ? grossAmount : netAmount;
|
|
339
|
+
// Commented out because service fee does not apply on surcharge
|
|
340
|
+
// if (surcharge_amount) {
|
|
341
|
+
// p += surcharge_amount;
|
|
342
|
+
// }
|
|
343
|
+
let service_fee_amount = 0;
|
|
344
|
+
if (is_service_fee_percentage) {
|
|
345
|
+
service_fee_amount = getServiceFeeAmount(p, service_fee, is_service_fee_percentage);
|
|
346
|
+
}
|
|
347
|
+
else if (service_fee) {
|
|
348
|
+
const totalP = apply_service_fee_on_original_price ? originalItemsPrice : payablePrice;
|
|
349
|
+
const proportion = p / totalP;
|
|
350
|
+
service_fee_amount = proportion * service_fee;
|
|
351
|
+
}
|
|
352
|
+
grand_total += service_fee_amount;
|
|
353
|
+
}
|
|
354
|
+
// GST is represented in percentage. We convert it to decimal
|
|
355
|
+
if (applyGSTOnOriginalPrice) {
|
|
356
|
+
grand_total += totalItemsDiscount;
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
grand_total -= totalOrderDiscount;
|
|
360
|
+
}
|
|
361
|
+
let gst_amount = 0;
|
|
362
|
+
if (is_gst_inclusive) {
|
|
363
|
+
// If GST is inclusive, it means that the Tax is already included in the Sale Value
|
|
364
|
+
// However, we need to calculate the Sale Value without the Tax
|
|
365
|
+
// And then calculate the Tax amount from the Sale Value without Tax
|
|
366
|
+
gst_amount = (grand_total * gst) / (100 + gst);
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
gst_amount = getPercentageAmount(grand_total, gst);
|
|
370
|
+
// Add GST amount to orderSubtotal
|
|
371
|
+
grand_total += gst_amount;
|
|
372
|
+
}
|
|
373
|
+
return gst_amount;
|
|
374
|
+
}
|
|
375
|
+
// ==============================>TODO: Remove Below<===============================
|
|
376
|
+
function getNormalizedOfferId(offerId) {
|
|
377
|
+
if (offerId == null)
|
|
378
|
+
return null;
|
|
379
|
+
const normalizedOfferId = offerId.toString().trim();
|
|
380
|
+
return normalizedOfferId.length > 0 ? normalizedOfferId : null;
|
|
381
|
+
}
|
|
382
|
+
function toPositiveInteger(value, fallback = 0) {
|
|
383
|
+
const parsedValue = Number(value);
|
|
384
|
+
if (!Number.isFinite(parsedValue) || parsedValue <= 0) {
|
|
385
|
+
return fallback;
|
|
386
|
+
}
|
|
387
|
+
return Math.floor(parsedValue);
|
|
388
|
+
}
|
|
389
|
+
function getNormalizedRedeemedOffersMap(redeemedOffers) {
|
|
390
|
+
if (redeemedOffers == null)
|
|
391
|
+
return null;
|
|
392
|
+
const normalizedRedeemedOffers = {};
|
|
393
|
+
const offerIds = Object.keys(redeemedOffers);
|
|
394
|
+
for (let i = 0; i < offerIds.length; i++) {
|
|
395
|
+
const offerId = offerIds[i];
|
|
396
|
+
const normalizedOfferId = getNormalizedOfferId(offerId);
|
|
397
|
+
if (normalizedOfferId == null)
|
|
398
|
+
continue;
|
|
399
|
+
const safeCount = toPositiveInteger(redeemedOffers[offerId], 0);
|
|
400
|
+
if (safeCount <= 0)
|
|
401
|
+
continue;
|
|
402
|
+
normalizedRedeemedOffers[normalizedOfferId] = safeCount;
|
|
403
|
+
}
|
|
404
|
+
return Object.keys(normalizedRedeemedOffers).length > 0 ? normalizedRedeemedOffers : null;
|
|
405
|
+
}
|
|
406
|
+
function getRedeemedOffersMap(existingRedeemedOffers) {
|
|
407
|
+
return getNormalizedRedeemedOffersMap(existingRedeemedOffers);
|
|
408
|
+
}
|
|
409
|
+
function decrementRedeemedOfferUsageCount(redeemedOffers, offerId) {
|
|
410
|
+
const normalizedOfferId = getNormalizedOfferId(offerId);
|
|
411
|
+
if (normalizedOfferId == null || redeemedOffers == null) {
|
|
412
|
+
return redeemedOffers;
|
|
413
|
+
}
|
|
414
|
+
const previousCount = toPositiveInteger(redeemedOffers[normalizedOfferId], 0);
|
|
415
|
+
if (previousCount <= 0) {
|
|
416
|
+
return redeemedOffers;
|
|
417
|
+
}
|
|
418
|
+
const nextRedeemedOffers = {
|
|
419
|
+
...redeemedOffers,
|
|
420
|
+
};
|
|
421
|
+
const nextCount = previousCount - 1;
|
|
422
|
+
if (nextCount <= 0) {
|
|
423
|
+
delete nextRedeemedOffers[normalizedOfferId];
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
nextRedeemedOffers[normalizedOfferId] = nextCount;
|
|
427
|
+
}
|
|
428
|
+
return getRedeemedOffersMap(nextRedeemedOffers);
|
|
429
|
+
}
|
|
430
|
+
function getSyncedRedeemedOffersAfterOrderDiscountOverflowCleanup({ redeemedOffers, previousOrderDiscountDetails, nextOrderDiscountDetails, }) {
|
|
431
|
+
let nextRedeemedOffers = redeemedOffers;
|
|
432
|
+
const previousLoyaltyDiscountId = getNormalizedOfferId(previousOrderDiscountDetails?.loyalty_discount_id);
|
|
433
|
+
const nextLoyaltyDiscountId = getNormalizedOfferId(nextOrderDiscountDetails?.loyalty_discount_id);
|
|
434
|
+
if (previousLoyaltyDiscountId != null && previousLoyaltyDiscountId !== nextLoyaltyDiscountId) {
|
|
435
|
+
nextRedeemedOffers = decrementRedeemedOfferUsageCount(nextRedeemedOffers, previousLoyaltyDiscountId);
|
|
436
|
+
}
|
|
437
|
+
const previousVoucherDiscountId = getNormalizedOfferId(previousOrderDiscountDetails?.voucher_discount_id);
|
|
438
|
+
const nextVoucherDiscountId = getNormalizedOfferId(nextOrderDiscountDetails?.voucher_discount_id);
|
|
439
|
+
if (previousVoucherDiscountId != null && previousVoucherDiscountId !== nextVoucherDiscountId) {
|
|
440
|
+
nextRedeemedOffers = decrementRedeemedOfferUsageCount(nextRedeemedOffers, previousVoucherDiscountId);
|
|
441
|
+
}
|
|
442
|
+
return getRedeemedOffersMap(nextRedeemedOffers);
|
|
443
|
+
}
|
|
444
|
+
function getWalletRedemptionsMap(walletRedemptions) {
|
|
445
|
+
if (walletRedemptions == null)
|
|
446
|
+
return null;
|
|
447
|
+
const normalized = {};
|
|
448
|
+
const cashbackAmount = Number(walletRedemptions.cashback_amount);
|
|
449
|
+
if (Number.isFinite(cashbackAmount) && cashbackAmount > 0) {
|
|
450
|
+
normalized.cashback_amount = cashbackAmount;
|
|
451
|
+
}
|
|
452
|
+
const giftCardRedemptions = walletRedemptions.gift_cards;
|
|
453
|
+
if (Array.isArray(giftCardRedemptions)) {
|
|
454
|
+
const normalizedGiftCardRedemptionsMap = {};
|
|
455
|
+
for (let i = 0; i < giftCardRedemptions.length; i++) {
|
|
456
|
+
const giftCardRedemption = giftCardRedemptions[i];
|
|
457
|
+
const giftCardId = giftCardRedemption.gift_card_id?.toString().trim();
|
|
458
|
+
const legacyValueRedeemed = giftCardRedemption.value_redeemed;
|
|
459
|
+
const redeemedTimes = toPositiveInteger(giftCardRedemption.redeemed_times ?? legacyValueRedeemed, 0);
|
|
460
|
+
if (giftCardId == null || giftCardId.length === 0 || redeemedTimes <= 0) {
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
const existingRedemption = normalizedGiftCardRedemptionsMap[giftCardId];
|
|
464
|
+
normalizedGiftCardRedemptionsMap[giftCardId] = {
|
|
465
|
+
gift_card_id: giftCardId,
|
|
466
|
+
redeemed_times: (existingRedemption?.redeemed_times ?? 0) + redeemedTimes,
|
|
467
|
+
...(giftCardRedemption.mystery_gift_type != null
|
|
468
|
+
? { mystery_gift_type: giftCardRedemption.mystery_gift_type }
|
|
469
|
+
: existingRedemption?.mystery_gift_type != null
|
|
470
|
+
? { mystery_gift_type: existingRedemption.mystery_gift_type }
|
|
471
|
+
: {}),
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
const normalizedGiftCardRedemptions = Object.values(normalizedGiftCardRedemptionsMap);
|
|
475
|
+
if (normalizedGiftCardRedemptions.length > 0) {
|
|
476
|
+
normalized.gift_cards = normalizedGiftCardRedemptions;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
return Object.keys(normalized).length > 0 ? normalized : null;
|
|
480
|
+
}
|
|
481
|
+
function getSyncedWalletRedemptionsAfterOrderDiscountOverflowCleanup({ walletRedemptions, previousOrderDiscountDetails, nextOrderDiscountDetails, }) {
|
|
482
|
+
if (walletRedemptions == null) {
|
|
483
|
+
return walletRedemptions;
|
|
484
|
+
}
|
|
485
|
+
const previousLoyaltyDiscountId = getNormalizedOfferId(previousOrderDiscountDetails?.loyalty_discount_id);
|
|
486
|
+
const nextLoyaltyDiscountId = getNormalizedOfferId(nextOrderDiscountDetails?.loyalty_discount_id);
|
|
487
|
+
if (previousLoyaltyDiscountId == null || previousLoyaltyDiscountId === nextLoyaltyDiscountId) {
|
|
488
|
+
return walletRedemptions;
|
|
489
|
+
}
|
|
490
|
+
const normalizedWalletRedemptions = getWalletRedemptionsMap(walletRedemptions);
|
|
491
|
+
const normalizedGiftCardRedemptions = normalizedWalletRedemptions?.gift_cards ?? [];
|
|
492
|
+
const filteredGiftCardRedemptions = normalizedGiftCardRedemptions.filter((giftCardRedemption) => getNormalizedOfferId(giftCardRedemption.gift_card_id) !== previousLoyaltyDiscountId);
|
|
493
|
+
const shouldClearWalletCashback = previousLoyaltyDiscountId === WALLET_CASHBACK_LOYALTY_DISCOUNT_ID &&
|
|
494
|
+
Number(walletRedemptions.cashback_amount ?? 0) > 0;
|
|
495
|
+
const shouldClearGiftCardRedemption = filteredGiftCardRedemptions.length !== normalizedGiftCardRedemptions.length;
|
|
496
|
+
if (!shouldClearWalletCashback && !shouldClearGiftCardRedemption) {
|
|
497
|
+
return walletRedemptions;
|
|
498
|
+
}
|
|
499
|
+
return getWalletRedemptionsMap({
|
|
500
|
+
...(normalizedWalletRedemptions ?? {}),
|
|
501
|
+
...(shouldClearWalletCashback ? { cashback_amount: undefined } : {}),
|
|
502
|
+
...(shouldClearGiftCardRedemption
|
|
503
|
+
? {
|
|
504
|
+
gift_cards: filteredGiftCardRedemptions.length > 0 ? filteredGiftCardRedemptions : undefined,
|
|
505
|
+
}
|
|
506
|
+
: {}),
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
function clearSpecialDiscountFields(orderDiscountDetails) {
|
|
510
|
+
return {
|
|
511
|
+
...orderDiscountDetails,
|
|
512
|
+
special_discount_id: undefined,
|
|
513
|
+
special_discount_name: undefined,
|
|
514
|
+
special_discount: undefined,
|
|
515
|
+
is_special_discount_percentage: false,
|
|
516
|
+
special_discount_reason: undefined,
|
|
517
|
+
special_reference_number: undefined,
|
|
518
|
+
};
|
|
519
|
+
}
|
|
520
|
+
function clearLoyaltyDiscountFields(orderDiscountDetails) {
|
|
521
|
+
return {
|
|
522
|
+
...orderDiscountDetails,
|
|
523
|
+
loyalty_discount_id: undefined,
|
|
524
|
+
loyalty_discount_name: undefined,
|
|
525
|
+
loyalty_discount: undefined,
|
|
526
|
+
is_loyalty_discount_percentage: false,
|
|
527
|
+
loyalty_discount_reason: undefined,
|
|
528
|
+
loyalty_reference_number: undefined,
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
function clearVoucherDiscountFields(orderDiscountDetails) {
|
|
532
|
+
return {
|
|
533
|
+
...orderDiscountDetails,
|
|
534
|
+
voucher_discount_id: undefined,
|
|
535
|
+
voucher_discount_name: undefined,
|
|
536
|
+
voucher_discount: undefined,
|
|
537
|
+
is_voucher_discount_percentage: false,
|
|
538
|
+
voucher_discount_reason: undefined,
|
|
539
|
+
voucher_reference_number: undefined,
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
function hasRedeemedOfferEvidenceForDiscountId({ discountId, redeemedOffers, }) {
|
|
543
|
+
const normalizedDiscountId = getNormalizedOfferId(discountId);
|
|
544
|
+
if (normalizedDiscountId == null) {
|
|
545
|
+
return false;
|
|
546
|
+
}
|
|
547
|
+
return toPositiveInteger(redeemedOffers?.[normalizedDiscountId], 0) > 0;
|
|
548
|
+
}
|
|
549
|
+
function hasWalletGiftCardEvidenceForDiscountId({ discountId, walletRedemptions, }) {
|
|
550
|
+
const normalizedDiscountId = getNormalizedOfferId(discountId);
|
|
551
|
+
if (normalizedDiscountId == null) {
|
|
552
|
+
return false;
|
|
553
|
+
}
|
|
554
|
+
const normalizedWalletRedemptions = getWalletRedemptionsMap(walletRedemptions);
|
|
555
|
+
if (normalizedWalletRedemptions?.gift_cards == null) {
|
|
556
|
+
return false;
|
|
557
|
+
}
|
|
558
|
+
for (let i = 0; i < normalizedWalletRedemptions.gift_cards.length; i++) {
|
|
559
|
+
const giftCardRedemption = normalizedWalletRedemptions.gift_cards[i];
|
|
560
|
+
const normalizedGiftCardId = getNormalizedOfferId(giftCardRedemption.gift_card_id);
|
|
561
|
+
if (normalizedGiftCardId === normalizedDiscountId) {
|
|
562
|
+
return true;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
567
|
+
function hasWalletCashbackEvidence(walletRedemptions) {
|
|
568
|
+
return Number(walletRedemptions?.cashback_amount ?? 0) > 0;
|
|
569
|
+
}
|
|
570
|
+
function hasLoyaltyDiscountEvidence({ discountId, redeemedOffers, walletRedemptions, }) {
|
|
571
|
+
const normalizedDiscountId = getNormalizedOfferId(discountId);
|
|
572
|
+
if (normalizedDiscountId == null) {
|
|
573
|
+
return false;
|
|
574
|
+
}
|
|
575
|
+
if (normalizedDiscountId === WALLET_CASHBACK_LOYALTY_DISCOUNT_ID) {
|
|
576
|
+
return hasWalletCashbackEvidence(walletRedemptions);
|
|
577
|
+
}
|
|
578
|
+
if (hasRedeemedOfferEvidenceForDiscountId({ discountId: normalizedDiscountId, redeemedOffers })) {
|
|
579
|
+
return true;
|
|
580
|
+
}
|
|
581
|
+
return hasWalletGiftCardEvidenceForDiscountId({
|
|
582
|
+
discountId: normalizedDiscountId,
|
|
583
|
+
walletRedemptions,
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
function hasVoucherDiscountEvidence({ discountId, redeemedOffers, walletRedemptions, }) {
|
|
587
|
+
const normalizedDiscountId = getNormalizedOfferId(discountId);
|
|
588
|
+
if (normalizedDiscountId == null) {
|
|
589
|
+
return false;
|
|
590
|
+
}
|
|
591
|
+
if (hasRedeemedOfferEvidenceForDiscountId({ discountId: normalizedDiscountId, redeemedOffers })) {
|
|
592
|
+
return true;
|
|
593
|
+
}
|
|
594
|
+
return hasWalletGiftCardEvidenceForDiscountId({
|
|
595
|
+
discountId: normalizedDiscountId,
|
|
596
|
+
walletRedemptions,
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
function getSyncedOrderDiscountDetailsWithoutRewardSpecialDiscount({ orderDiscountDetails, redeemedOffers, walletRedemptions, }) {
|
|
600
|
+
if (orderDiscountDetails == null) {
|
|
601
|
+
return orderDiscountDetails;
|
|
602
|
+
}
|
|
603
|
+
let syncedOrderDiscountDetails = orderDiscountDetails;
|
|
604
|
+
const normalizedSpecialDiscountId = getNormalizedOfferId(syncedOrderDiscountDetails.special_discount_id);
|
|
605
|
+
if (normalizedSpecialDiscountId != null) {
|
|
606
|
+
const hasRedeemedOfferForSpecialDiscount = redeemedOffers?.[normalizedSpecialDiscountId] != null;
|
|
607
|
+
const normalizedGiftCardIds = (walletRedemptions?.gift_cards ?? [])
|
|
608
|
+
.map((giftCardRedemption) => getNormalizedOfferId(giftCardRedemption.gift_card_id))
|
|
609
|
+
.filter((giftCardId) => giftCardId != null);
|
|
610
|
+
const isGiftCardRewardDiscount = normalizedGiftCardIds.includes(normalizedSpecialDiscountId);
|
|
611
|
+
const isWalletCashbackRewardDiscount = normalizedSpecialDiscountId === WALLET_CASHBACK_LOYALTY_DISCOUNT_ID;
|
|
612
|
+
const isRewardLinkedSpecialDiscount = hasRedeemedOfferForSpecialDiscount || isGiftCardRewardDiscount || isWalletCashbackRewardDiscount;
|
|
613
|
+
if (isRewardLinkedSpecialDiscount) {
|
|
614
|
+
const hasExistingRewardSlot = syncedOrderDiscountDetails.loyalty_discount_id != null ||
|
|
615
|
+
syncedOrderDiscountDetails.voucher_discount_id != null;
|
|
616
|
+
if (hasExistingRewardSlot) {
|
|
617
|
+
syncedOrderDiscountDetails = clearSpecialDiscountFields(syncedOrderDiscountDetails);
|
|
618
|
+
}
|
|
619
|
+
else {
|
|
620
|
+
syncedOrderDiscountDetails = {
|
|
621
|
+
...clearSpecialDiscountFields(syncedOrderDiscountDetails),
|
|
622
|
+
loyalty_discount_id: normalizedSpecialDiscountId,
|
|
623
|
+
loyalty_discount_name: syncedOrderDiscountDetails.loyalty_discount_name ?? syncedOrderDiscountDetails.special_discount_name,
|
|
624
|
+
loyalty_discount: syncedOrderDiscountDetails.loyalty_discount ?? syncedOrderDiscountDetails.special_discount,
|
|
625
|
+
is_loyalty_discount_percentage: syncedOrderDiscountDetails.loyalty_discount_id != null ||
|
|
626
|
+
syncedOrderDiscountDetails.loyalty_discount != null
|
|
627
|
+
? syncedOrderDiscountDetails.is_loyalty_discount_percentage
|
|
628
|
+
: syncedOrderDiscountDetails.is_special_discount_percentage,
|
|
629
|
+
loyalty_discount_reason: syncedOrderDiscountDetails.loyalty_discount_reason ?? syncedOrderDiscountDetails.special_discount_reason,
|
|
630
|
+
loyalty_reference_number: syncedOrderDiscountDetails.loyalty_reference_number ?? syncedOrderDiscountDetails.special_reference_number,
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
if (syncedOrderDiscountDetails.loyalty_discount_id != null &&
|
|
636
|
+
!hasLoyaltyDiscountEvidence({
|
|
637
|
+
discountId: syncedOrderDiscountDetails.loyalty_discount_id,
|
|
638
|
+
redeemedOffers,
|
|
639
|
+
walletRedemptions,
|
|
640
|
+
})) {
|
|
641
|
+
syncedOrderDiscountDetails = clearLoyaltyDiscountFields(syncedOrderDiscountDetails);
|
|
642
|
+
}
|
|
643
|
+
if (syncedOrderDiscountDetails.voucher_discount_id != null &&
|
|
644
|
+
!hasVoucherDiscountEvidence({
|
|
645
|
+
discountId: syncedOrderDiscountDetails.voucher_discount_id,
|
|
646
|
+
redeemedOffers,
|
|
647
|
+
walletRedemptions,
|
|
648
|
+
})) {
|
|
649
|
+
syncedOrderDiscountDetails = clearVoucherDiscountFields(syncedOrderDiscountDetails);
|
|
650
|
+
}
|
|
651
|
+
return syncedOrderDiscountDetails;
|
|
652
|
+
}
|
|
653
|
+
function getSyncedItemDiscountDetailsWithoutRewardSpecialDiscount({ itemDiscountDetails, redeemedOffers, walletRedemptions, }) {
|
|
654
|
+
return getSyncedOrderDiscountDetailsWithoutRewardSpecialDiscount({
|
|
655
|
+
orderDiscountDetails: itemDiscountDetails,
|
|
656
|
+
redeemedOffers,
|
|
657
|
+
walletRedemptions,
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
/**
|
|
661
|
+
* Keeps only the cashier-applied (special) discount slot of the given
|
|
662
|
+
* discount details. Loyalty and voucher slots are cleared because they
|
|
663
|
+
* represent system-granted rewards (offers, wallet cashback, gift cards)
|
|
664
|
+
* which must not count toward the staff role's discount cap.
|
|
665
|
+
*/
|
|
666
|
+
function getCashierSpecialOnlyDiscountDetails(discountDetails) {
|
|
667
|
+
if (discountDetails == null) {
|
|
668
|
+
return null;
|
|
669
|
+
}
|
|
670
|
+
return clearLoyaltyDiscountFields(clearVoucherDiscountFields(discountDetails));
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Returns the amount of discount that was applied by the cashier/staff on the
|
|
674
|
+
* order (order-level + per-item `special_discount` slots, resolved to actual
|
|
675
|
+
* amounts) — i.e. the portion of the total discount that is subject to the
|
|
676
|
+
* staff role's discount cap.
|
|
677
|
+
*
|
|
678
|
+
* System-granted rewards are excluded:
|
|
679
|
+
* - offer discounts that live on the `loyalty_discount` / `voucher_discount`
|
|
680
|
+
* slots (order-level and per-item),
|
|
681
|
+
* - wallet cashback and gift-card rewards (which also ride the loyalty slot),
|
|
682
|
+
* - any reward-linked special discount (recognized via `redeemed_offers` /
|
|
683
|
+
* `wallet_redemptions` evidence and normalized into the loyalty slot).
|
|
684
|
+
*/
|
|
685
|
+
export function getCashierAppliedDiscountAmount(order) {
|
|
686
|
+
if (order == null) {
|
|
687
|
+
return 0;
|
|
688
|
+
}
|
|
689
|
+
const redeemedOffers = order.redeemed_offers ?? null;
|
|
690
|
+
const walletRedemptions = order.wallet_redemptions ?? null;
|
|
691
|
+
// Resolve the order-level cashier (special) discount.
|
|
692
|
+
const syncedOrderDiscountDetails = getSyncedOrderDiscountDetailsWithoutRewardSpecialDiscount({
|
|
693
|
+
orderDiscountDetails: order.order_discount_details,
|
|
694
|
+
redeemedOffers,
|
|
695
|
+
walletRedemptions,
|
|
696
|
+
});
|
|
697
|
+
const cashierOnlyOrderDiscountDetails = getCashierSpecialOnlyDiscountDetails(syncedOrderDiscountDetails ?? order.order_discount_details);
|
|
698
|
+
const orderDiscountBaseAmount = getOrderSubtotalFromCategoriesForOfferSync(order.categories);
|
|
699
|
+
let totalCashierDiscount = parseNum(getDiscountDetails(cashierOnlyOrderDiscountDetails, orderDiscountBaseAmount)?.total_discount ?? 0);
|
|
700
|
+
// Resolve per-item cashier (special) discounts.
|
|
701
|
+
const categories = order.categories;
|
|
702
|
+
if (categories != null) {
|
|
703
|
+
for (let c = 0; c < categories.length; c++) {
|
|
704
|
+
const items = categories[c].items;
|
|
705
|
+
for (let i = 0; i < items.length; i++) {
|
|
706
|
+
const item = items[i];
|
|
707
|
+
const syncedItemDiscountDetails = getSyncedItemDiscountDetailsWithoutRewardSpecialDiscount({
|
|
708
|
+
itemDiscountDetails: item.item_discount_details,
|
|
709
|
+
redeemedOffers,
|
|
710
|
+
walletRedemptions,
|
|
711
|
+
});
|
|
712
|
+
const cashierOnlyItemDiscountDetails = getCashierSpecialOnlyDiscountDetails(syncedItemDiscountDetails ?? item.item_discount_details);
|
|
713
|
+
const itemDiscountBaseAmount = (Number(item.price ?? 0) + calculateItemModifiersPrice(item)) * Number(item.quantity ?? 0);
|
|
714
|
+
const itemCashierDiscount = getDiscountDetails(cashierOnlyItemDiscountDetails, Math.max(0, itemDiscountBaseAmount))?.total_discount ?? 0;
|
|
715
|
+
totalCashierDiscount = parseNum(totalCashierDiscount + itemCashierDiscount);
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
return parseNum(totalCashierDiscount);
|
|
720
|
+
}
|
|
721
|
+
function isWalletCashbackLoyaltyDiscount(orderDiscountDetails) {
|
|
722
|
+
return orderDiscountDetails?.loyalty_discount_id === WALLET_CASHBACK_LOYALTY_DISCOUNT_ID;
|
|
723
|
+
}
|
|
724
|
+
function getWalletCashbackAmountFromOrderData({ orderDiscountDetails, walletRedemptions, }) {
|
|
725
|
+
if (!isWalletCashbackLoyaltyDiscount(orderDiscountDetails)) {
|
|
726
|
+
return 0;
|
|
727
|
+
}
|
|
728
|
+
const loyaltyDiscountAmount = Number(orderDiscountDetails?.loyalty_discount ?? 0);
|
|
729
|
+
if (Number.isFinite(loyaltyDiscountAmount) && loyaltyDiscountAmount > 0) {
|
|
730
|
+
return loyaltyDiscountAmount;
|
|
731
|
+
}
|
|
732
|
+
const walletRedemptionAmount = Number(walletRedemptions?.cashback_amount ?? 0);
|
|
733
|
+
return Number.isFinite(walletRedemptionAmount) && walletRedemptionAmount > 0 ? walletRedemptionAmount : 0;
|
|
734
|
+
}
|
|
735
|
+
function getSyncedWalletCashbackOrderDiscountDetails({ orderDiscountDetails, walletCashbackAmount, }) {
|
|
736
|
+
if (orderDiscountDetails == null) {
|
|
737
|
+
return orderDiscountDetails;
|
|
738
|
+
}
|
|
739
|
+
if (walletCashbackAmount <= 0) {
|
|
740
|
+
return {
|
|
741
|
+
...orderDiscountDetails,
|
|
742
|
+
loyalty_discount_id: undefined,
|
|
743
|
+
loyalty_discount_name: undefined,
|
|
744
|
+
loyalty_discount: undefined,
|
|
745
|
+
is_loyalty_discount_percentage: false,
|
|
746
|
+
loyalty_reference_number: undefined,
|
|
747
|
+
total_discount: 0,
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
return {
|
|
751
|
+
...orderDiscountDetails,
|
|
752
|
+
loyalty_discount_id: WALLET_CASHBACK_LOYALTY_DISCOUNT_ID,
|
|
753
|
+
loyalty_discount_name: WALLET_CASHBACK_LOYALTY_DISCOUNT_NAME,
|
|
754
|
+
loyalty_discount: parseNum(walletCashbackAmount),
|
|
755
|
+
is_loyalty_discount_percentage: false,
|
|
756
|
+
loyalty_reference_number: WALLET_CASHBACK_LOYALTY_REFERENCE,
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
function getOrderSubtotalFromCategoriesForOfferSync(categories) {
|
|
760
|
+
if (!categories?.length)
|
|
761
|
+
return 0;
|
|
762
|
+
let subtotal = 0;
|
|
763
|
+
for (let i = 0; i < categories.length; i++) {
|
|
764
|
+
const items = categories[i].items;
|
|
765
|
+
for (let j = 0; j < items.length; j++) {
|
|
766
|
+
subtotal += items[j].subtotal ?? 0;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
return subtotal;
|
|
770
|
+
}
|
|
771
|
+
function isRedeemedOffersMapNormalizedForOfferSync(redeemedOffers) {
|
|
772
|
+
if (redeemedOffers == null) {
|
|
773
|
+
return true;
|
|
774
|
+
}
|
|
775
|
+
const normalizedRedeemedOffers = getRedeemedOffersMap(redeemedOffers);
|
|
776
|
+
const rawOfferIds = Object.keys(redeemedOffers);
|
|
777
|
+
const normalizedOfferIds = Object.keys(normalizedRedeemedOffers ?? {});
|
|
778
|
+
if (rawOfferIds.length !== normalizedOfferIds.length) {
|
|
779
|
+
return false;
|
|
780
|
+
}
|
|
781
|
+
for (let i = 0; i < rawOfferIds.length; i++) {
|
|
782
|
+
const rawOfferId = rawOfferIds[i];
|
|
783
|
+
const normalizedOfferId = getNormalizedOfferId(rawOfferId);
|
|
784
|
+
const safeCount = toPositiveInteger(redeemedOffers[rawOfferId], 0);
|
|
785
|
+
if (normalizedOfferId == null || rawOfferId !== normalizedOfferId || safeCount <= 0) {
|
|
786
|
+
return false;
|
|
787
|
+
}
|
|
788
|
+
if ((normalizedRedeemedOffers?.[normalizedOfferId] ?? 0) !== safeCount) {
|
|
789
|
+
return false;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
return true;
|
|
793
|
+
}
|
|
794
|
+
function areRedeemedOffersEqualForOfferSync(currentRedeemedOffers, nextRedeemedOffers) {
|
|
795
|
+
const normalizedCurrentRedeemedOffers = getRedeemedOffersMap(currentRedeemedOffers);
|
|
796
|
+
const normalizedNextRedeemedOffers = getRedeemedOffersMap(nextRedeemedOffers);
|
|
797
|
+
if (normalizedCurrentRedeemedOffers == null || normalizedNextRedeemedOffers == null) {
|
|
798
|
+
return normalizedCurrentRedeemedOffers == null && normalizedNextRedeemedOffers == null;
|
|
799
|
+
}
|
|
800
|
+
const currentOfferIds = Object.keys(normalizedCurrentRedeemedOffers);
|
|
801
|
+
const nextOfferIds = Object.keys(normalizedNextRedeemedOffers);
|
|
802
|
+
if (currentOfferIds.length !== nextOfferIds.length) {
|
|
803
|
+
return false;
|
|
804
|
+
}
|
|
805
|
+
for (let i = 0; i < currentOfferIds.length; i++) {
|
|
806
|
+
const offerId = currentOfferIds[i];
|
|
807
|
+
if (normalizedCurrentRedeemedOffers[offerId] !== normalizedNextRedeemedOffers[offerId]) {
|
|
808
|
+
return false;
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
return true;
|
|
812
|
+
}
|
|
813
|
+
function getItemRewardApplicableQuantityForOfferSync(offer) {
|
|
814
|
+
if (offer.reward_type === OFFER_REWARD_TYPE_ENUM.ITEMS) {
|
|
815
|
+
return toPositiveInteger(offer.reward_items?.num_of_items, 1);
|
|
816
|
+
}
|
|
817
|
+
if (offer.reward_type === OFFER_REWARD_TYPE_ENUM.DISCOUNT_ITEMS) {
|
|
818
|
+
return toPositiveInteger(offer.reward_discount_items?.num_of_items, 1);
|
|
819
|
+
}
|
|
820
|
+
return null;
|
|
821
|
+
}
|
|
822
|
+
function getTotalSelectedOfferItemsAgainstOfferForOfferSync(categories, offerId) {
|
|
823
|
+
let totalSelectedOfferItems = 0;
|
|
824
|
+
for (let i = 0; i < categories.length; i++) {
|
|
825
|
+
const category = categories[i];
|
|
826
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
827
|
+
const item = category.items[j];
|
|
828
|
+
const discountDetails = item.item_discount_details;
|
|
829
|
+
if (discountDetails == null)
|
|
830
|
+
continue;
|
|
831
|
+
const loyaltyDiscountId = getNormalizedOfferId(discountDetails.loyalty_discount_id);
|
|
832
|
+
const voucherDiscountId = getNormalizedOfferId(discountDetails.voucher_discount_id);
|
|
833
|
+
if (loyaltyDiscountId !== offerId && voucherDiscountId !== offerId)
|
|
834
|
+
continue;
|
|
835
|
+
totalSelectedOfferItems += toPositiveInteger(item.quantity, 0);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
return totalSelectedOfferItems;
|
|
839
|
+
}
|
|
840
|
+
function getReconciledRedeemedOffersMapForOfferSync({ existingRedeemedOffers, categories, offers, removedOfferIds, unlockedOffersApplicableTimes, }) {
|
|
841
|
+
const nextRedeemedOffers = {
|
|
842
|
+
...(getRedeemedOffersMap(existingRedeemedOffers) ?? {}),
|
|
843
|
+
};
|
|
844
|
+
const removedIds = Array.from(removedOfferIds);
|
|
845
|
+
for (let i = 0; i < removedIds.length; i++) {
|
|
846
|
+
const normalizedOfferId = getNormalizedOfferId(removedIds[i]);
|
|
847
|
+
if (normalizedOfferId == null)
|
|
848
|
+
continue;
|
|
849
|
+
delete nextRedeemedOffers[normalizedOfferId];
|
|
850
|
+
}
|
|
851
|
+
for (let i = 0; i < offers.length; i++) {
|
|
852
|
+
const offer = offers[i];
|
|
853
|
+
const normalizedOfferId = getNormalizedOfferId(offer._id);
|
|
854
|
+
if (normalizedOfferId == null)
|
|
855
|
+
continue;
|
|
856
|
+
const applicableQuantity = getItemRewardApplicableQuantityForOfferSync(offer);
|
|
857
|
+
if (applicableQuantity != null) {
|
|
858
|
+
const selectedItemsCount = getTotalSelectedOfferItemsAgainstOfferForOfferSync(categories, normalizedOfferId);
|
|
859
|
+
// Any partial usage of an item reward still counts as one redeemed slot.
|
|
860
|
+
const redeemedTimes = selectedItemsCount > 0 ? Math.ceil(selectedItemsCount / applicableQuantity) : 0;
|
|
861
|
+
if (redeemedTimes > 0) {
|
|
862
|
+
nextRedeemedOffers[normalizedOfferId] = redeemedTimes;
|
|
863
|
+
}
|
|
864
|
+
else {
|
|
865
|
+
delete nextRedeemedOffers[normalizedOfferId];
|
|
866
|
+
}
|
|
867
|
+
continue;
|
|
868
|
+
}
|
|
869
|
+
const existingRedeemedTimes = toPositiveInteger(nextRedeemedOffers[normalizedOfferId], 0);
|
|
870
|
+
if (existingRedeemedTimes <= 0) {
|
|
871
|
+
delete nextRedeemedOffers[normalizedOfferId];
|
|
872
|
+
continue;
|
|
873
|
+
}
|
|
874
|
+
const hasUnlockContext = unlockedOffersApplicableTimes != null;
|
|
875
|
+
const maxApplicableTimes = toPositiveInteger(unlockedOffersApplicableTimes?.[offer._id], 0);
|
|
876
|
+
if (maxApplicableTimes <= 0) {
|
|
877
|
+
if (!hasUnlockContext) {
|
|
878
|
+
nextRedeemedOffers[normalizedOfferId] = existingRedeemedTimes;
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
delete nextRedeemedOffers[normalizedOfferId];
|
|
882
|
+
continue;
|
|
883
|
+
}
|
|
884
|
+
nextRedeemedOffers[normalizedOfferId] = Math.min(existingRedeemedTimes, maxApplicableTimes);
|
|
885
|
+
}
|
|
886
|
+
return getRedeemedOffersMap(nextRedeemedOffers);
|
|
887
|
+
}
|
|
888
|
+
function getOfferSynchronizedCartDataForCalculation({ data, syncContext, }) {
|
|
889
|
+
const unlockedOffersMap = {
|
|
890
|
+
...syncContext.unlockedOffersApplicableTimes,
|
|
891
|
+
};
|
|
892
|
+
const removedOfferIds = new Set();
|
|
893
|
+
let hasChanges = false;
|
|
894
|
+
const categories = data.categories ?? [];
|
|
895
|
+
const orderLevelDiscountBaseAmount = getOrderSubtotalFromCategoriesForOfferSync(categories);
|
|
896
|
+
let order_discount_details = data.order_discount_details;
|
|
897
|
+
let redeemed_offers = getRedeemedOffersMap(data.redeemed_offers);
|
|
898
|
+
if (!isRedeemedOffersMapNormalizedForOfferSync(data.redeemed_offers)) {
|
|
899
|
+
hasChanges = true;
|
|
900
|
+
}
|
|
901
|
+
const hasOrderLevelLoyaltyDiscount = order_discount_details?.loyalty_discount_id != null;
|
|
902
|
+
const hasOrderLevelVoucherDiscount = order_discount_details?.voucher_discount_id != null;
|
|
903
|
+
// Empty carts should not keep offer-driven discount slots or redeemed-offers state.
|
|
904
|
+
if (orderLevelDiscountBaseAmount <= 0) {
|
|
905
|
+
if (hasOrderLevelLoyaltyDiscount && order_discount_details != null) {
|
|
906
|
+
order_discount_details = {
|
|
907
|
+
...order_discount_details,
|
|
908
|
+
loyalty_discount_id: undefined,
|
|
909
|
+
loyalty_discount_name: undefined,
|
|
910
|
+
loyalty_discount: undefined,
|
|
911
|
+
is_loyalty_discount_percentage: false,
|
|
912
|
+
loyalty_reference_number: undefined,
|
|
913
|
+
total_discount: 0,
|
|
914
|
+
};
|
|
915
|
+
hasChanges = true;
|
|
916
|
+
}
|
|
917
|
+
if (hasOrderLevelVoucherDiscount && order_discount_details != null) {
|
|
918
|
+
order_discount_details = {
|
|
919
|
+
...order_discount_details,
|
|
920
|
+
voucher_discount_id: undefined,
|
|
921
|
+
voucher_discount_name: undefined,
|
|
922
|
+
voucher_discount: undefined,
|
|
923
|
+
is_voucher_discount_percentage: false,
|
|
924
|
+
total_discount: 0,
|
|
925
|
+
};
|
|
926
|
+
hasChanges = true;
|
|
927
|
+
}
|
|
928
|
+
if (redeemed_offers != null) {
|
|
929
|
+
redeemed_offers = null;
|
|
930
|
+
hasChanges = true;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
const redeemedOffersPresenceMap = getReconciledRedeemedOffersMapForOfferSync({
|
|
934
|
+
existingRedeemedOffers: redeemed_offers,
|
|
935
|
+
categories,
|
|
936
|
+
offers: syncContext.offers,
|
|
937
|
+
removedOfferIds: new Set(),
|
|
938
|
+
unlockedOffersApplicableTimes: syncContext.unlockedOffersApplicableTimes,
|
|
939
|
+
});
|
|
940
|
+
const clearOrderLevelDiscount = (discountType) => {
|
|
941
|
+
if (order_discount_details == null) {
|
|
942
|
+
return;
|
|
943
|
+
}
|
|
944
|
+
const appliedOfferId = discountType === 'loyalty'
|
|
945
|
+
? order_discount_details.loyalty_discount_id
|
|
946
|
+
: order_discount_details.voucher_discount_id;
|
|
947
|
+
const normalizedAppliedOfferId = getNormalizedOfferId(appliedOfferId);
|
|
948
|
+
if (normalizedAppliedOfferId != null) {
|
|
949
|
+
removedOfferIds.add(normalizedAppliedOfferId);
|
|
950
|
+
}
|
|
951
|
+
if (discountType === 'loyalty') {
|
|
952
|
+
order_discount_details = {
|
|
953
|
+
...order_discount_details,
|
|
954
|
+
loyalty_discount_id: undefined,
|
|
955
|
+
loyalty_discount_name: undefined,
|
|
956
|
+
loyalty_discount: undefined,
|
|
957
|
+
is_loyalty_discount_percentage: false,
|
|
958
|
+
loyalty_reference_number: undefined,
|
|
959
|
+
};
|
|
960
|
+
}
|
|
961
|
+
else {
|
|
962
|
+
order_discount_details = {
|
|
963
|
+
...order_discount_details,
|
|
964
|
+
voucher_discount_id: undefined,
|
|
965
|
+
voucher_discount_name: undefined,
|
|
966
|
+
voucher_discount: undefined,
|
|
967
|
+
is_voucher_discount_percentage: false,
|
|
968
|
+
};
|
|
969
|
+
}
|
|
970
|
+
hasChanges = true;
|
|
971
|
+
};
|
|
972
|
+
if (order_discount_details != null) {
|
|
973
|
+
const loyaltyDiscountId = order_discount_details.loyalty_discount_id;
|
|
974
|
+
if (loyaltyDiscountId != null) {
|
|
975
|
+
const hasLoyaltyEvidence = hasLoyaltyDiscountEvidence({
|
|
976
|
+
discountId: loyaltyDiscountId,
|
|
977
|
+
redeemedOffers: redeemedOffersPresenceMap,
|
|
978
|
+
walletRedemptions: data.wallet_redemptions,
|
|
979
|
+
});
|
|
980
|
+
if (!hasLoyaltyEvidence) {
|
|
981
|
+
clearOrderLevelDiscount('loyalty');
|
|
982
|
+
}
|
|
983
|
+
else if (loyaltyDiscountId !== WALLET_CASHBACK_LOYALTY_DISCOUNT_ID) {
|
|
984
|
+
const loyaltyOffer = syncContext.offers.find((o) => o._id === loyaltyDiscountId);
|
|
985
|
+
// Non-offer loyalty ids may still be valid when they are backed by wallet evidence.
|
|
986
|
+
if (loyaltyOffer != null) {
|
|
987
|
+
if (unlockedOffersMap[loyaltyDiscountId] == null || unlockedOffersMap[loyaltyDiscountId] <= 0) {
|
|
988
|
+
clearOrderLevelDiscount('loyalty');
|
|
989
|
+
}
|
|
990
|
+
else {
|
|
991
|
+
if (loyaltyOffer.reward_discount != null) {
|
|
992
|
+
const { discount_value, excluded_refs, is_discount_percentage, max_discount_amount, max_discount_percentage, } = loyaltyOffer.reward_discount;
|
|
993
|
+
const { subtotal: eligibleSubtotal, hasExcludedRefs } = excluded_refs != null && excluded_refs.length > 0
|
|
994
|
+
? calculateEligibleOfferDiscountDetails(categories, excluded_refs)
|
|
995
|
+
: {
|
|
996
|
+
subtotal: orderLevelDiscountBaseAmount,
|
|
997
|
+
hasExcludedRefs: false,
|
|
998
|
+
};
|
|
999
|
+
if (hasExcludedRefs && !is_discount_percentage) {
|
|
1000
|
+
clearOrderLevelDiscount('loyalty');
|
|
1001
|
+
}
|
|
1002
|
+
else {
|
|
1003
|
+
const hasRelevantPercentageCap = !is_discount_percentage &&
|
|
1004
|
+
max_discount_percentage != null &&
|
|
1005
|
+
Number.isFinite(max_discount_percentage) &&
|
|
1006
|
+
max_discount_percentage >= 0;
|
|
1007
|
+
const hasMaxAmountCap = is_discount_percentage &&
|
|
1008
|
+
max_discount_amount != null &&
|
|
1009
|
+
Number.isFinite(max_discount_amount) &&
|
|
1010
|
+
max_discount_amount >= 0;
|
|
1011
|
+
const hasFlatMaxPercentage = !is_discount_percentage && hasRelevantPercentageCap;
|
|
1012
|
+
if (hasExcludedRefs && !is_discount_percentage && !hasFlatMaxPercentage) {
|
|
1013
|
+
clearOrderLevelDiscount('loyalty');
|
|
1014
|
+
}
|
|
1015
|
+
else if (hasExcludedRefs) {
|
|
1016
|
+
// Excluded items present: must apply percentage against eligible subtotal only.
|
|
1017
|
+
const eligibleBase = hasFlatMaxPercentage
|
|
1018
|
+
? parseNum(getNetItemsValueAfterItemDiscount({
|
|
1019
|
+
itemsPrice: eligibleSubtotal,
|
|
1020
|
+
totalDiscountApplied: 0,
|
|
1021
|
+
orderLevelDiscount: 0,
|
|
1022
|
+
}))
|
|
1023
|
+
: eligibleSubtotal;
|
|
1024
|
+
if (eligibleBase <= 0) {
|
|
1025
|
+
clearOrderLevelDiscount('loyalty');
|
|
1026
|
+
}
|
|
1027
|
+
else {
|
|
1028
|
+
const calculatedDiscount = getCappedOfferDiscountAmount({
|
|
1029
|
+
baseAmount: eligibleBase,
|
|
1030
|
+
discountValue: discount_value,
|
|
1031
|
+
isDiscountPercentage: is_discount_percentage,
|
|
1032
|
+
maxDiscountAmount: max_discount_amount,
|
|
1033
|
+
maxDiscountPercentage: max_discount_percentage,
|
|
1034
|
+
});
|
|
1035
|
+
order_discount_details = {
|
|
1036
|
+
...order_discount_details,
|
|
1037
|
+
loyalty_discount: parseNum(calculatedDiscount),
|
|
1038
|
+
is_loyalty_discount_percentage: false,
|
|
1039
|
+
};
|
|
1040
|
+
hasChanges = true;
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
else if (hasMaxAmountCap) {
|
|
1044
|
+
// Percentage offer with max_amount cap (no excluded items).
|
|
1045
|
+
const projectedPercentage = getPercentageAmount(orderLevelDiscountBaseAmount, discount_value);
|
|
1046
|
+
if (projectedPercentage > max_discount_amount) {
|
|
1047
|
+
// Cap would be breached -- normalize to flat at cap.
|
|
1048
|
+
order_discount_details = {
|
|
1049
|
+
...order_discount_details,
|
|
1050
|
+
loyalty_discount: parseNum(max_discount_amount),
|
|
1051
|
+
is_loyalty_discount_percentage: false,
|
|
1052
|
+
};
|
|
1053
|
+
}
|
|
1054
|
+
else {
|
|
1055
|
+
// Cap not breached -- un-cap back to percentage (or stay percentage).
|
|
1056
|
+
order_discount_details = {
|
|
1057
|
+
...order_discount_details,
|
|
1058
|
+
loyalty_discount: parseNum(discount_value),
|
|
1059
|
+
is_loyalty_discount_percentage: true,
|
|
1060
|
+
};
|
|
1061
|
+
}
|
|
1062
|
+
hasChanges = true;
|
|
1063
|
+
}
|
|
1064
|
+
else if (hasFlatMaxPercentage) {
|
|
1065
|
+
// Flat discount with max_percentage cap -- resolve against full cart.
|
|
1066
|
+
if (orderLevelDiscountBaseAmount <= 0) {
|
|
1067
|
+
clearOrderLevelDiscount('loyalty');
|
|
1068
|
+
}
|
|
1069
|
+
else {
|
|
1070
|
+
const calculatedDiscount = getCappedOfferDiscountAmount({
|
|
1071
|
+
baseAmount: orderLevelDiscountBaseAmount,
|
|
1072
|
+
discountValue: discount_value,
|
|
1073
|
+
isDiscountPercentage: false,
|
|
1074
|
+
maxDiscountAmount: undefined,
|
|
1075
|
+
maxDiscountPercentage: max_discount_percentage,
|
|
1076
|
+
});
|
|
1077
|
+
order_discount_details = {
|
|
1078
|
+
...order_discount_details,
|
|
1079
|
+
loyalty_discount: parseNum(calculatedDiscount),
|
|
1080
|
+
is_loyalty_discount_percentage: false,
|
|
1081
|
+
};
|
|
1082
|
+
hasChanges = true;
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
else if (is_discount_percentage) {
|
|
1086
|
+
// Plain percentage offer, no excluded items, no caps.
|
|
1087
|
+
const shouldRevert = order_discount_details.is_loyalty_discount_percentage === false &&
|
|
1088
|
+
order_discount_details.loyalty_discount != null;
|
|
1089
|
+
if (shouldRevert) {
|
|
1090
|
+
order_discount_details = {
|
|
1091
|
+
...order_discount_details,
|
|
1092
|
+
loyalty_discount: parseNum(discount_value),
|
|
1093
|
+
is_loyalty_discount_percentage: true,
|
|
1094
|
+
};
|
|
1095
|
+
hasChanges = true;
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
else {
|
|
1099
|
+
// Plain flat offer, no caps -- keep as-is.
|
|
1100
|
+
if (orderLevelDiscountBaseAmount <= 0) {
|
|
1101
|
+
clearOrderLevelDiscount('loyalty');
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
unlockedOffersMap[loyaltyDiscountId] -= 1;
|
|
1107
|
+
}
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
const voucherDiscountId = order_discount_details.voucher_discount_id;
|
|
1112
|
+
if (voucherDiscountId != null) {
|
|
1113
|
+
const hasVoucherEvidence = hasVoucherDiscountEvidence({
|
|
1114
|
+
discountId: voucherDiscountId,
|
|
1115
|
+
redeemedOffers: redeemedOffersPresenceMap,
|
|
1116
|
+
walletRedemptions: data.wallet_redemptions,
|
|
1117
|
+
});
|
|
1118
|
+
if (!hasVoucherEvidence) {
|
|
1119
|
+
clearOrderLevelDiscount('voucher');
|
|
1120
|
+
}
|
|
1121
|
+
else if (unlockedOffersMap[voucherDiscountId] == null || unlockedOffersMap[voucherDiscountId] <= 0) {
|
|
1122
|
+
clearOrderLevelDiscount('voucher');
|
|
1123
|
+
}
|
|
1124
|
+
else {
|
|
1125
|
+
const voucherOffer = syncContext.offers.find((o) => o._id === voucherDiscountId);
|
|
1126
|
+
if (voucherOffer?.reward_discount != null) {
|
|
1127
|
+
const { discount_value, excluded_refs, is_discount_percentage, max_discount_amount, max_discount_percentage, } = voucherOffer.reward_discount;
|
|
1128
|
+
const { subtotal: eligibleSubtotal, hasExcludedRefs } = excluded_refs != null && excluded_refs.length > 0
|
|
1129
|
+
? calculateEligibleOfferDiscountDetails(categories, excluded_refs)
|
|
1130
|
+
: {
|
|
1131
|
+
subtotal: orderLevelDiscountBaseAmount,
|
|
1132
|
+
hasExcludedRefs: false,
|
|
1133
|
+
};
|
|
1134
|
+
const hasRelevantPercentageCap = !is_discount_percentage &&
|
|
1135
|
+
max_discount_percentage != null &&
|
|
1136
|
+
Number.isFinite(max_discount_percentage) &&
|
|
1137
|
+
max_discount_percentage >= 0;
|
|
1138
|
+
const hasMaxAmountCap = is_discount_percentage &&
|
|
1139
|
+
max_discount_amount != null &&
|
|
1140
|
+
Number.isFinite(max_discount_amount) &&
|
|
1141
|
+
max_discount_amount >= 0;
|
|
1142
|
+
const hasFlatMaxPercentage = !is_discount_percentage && hasRelevantPercentageCap;
|
|
1143
|
+
if (hasExcludedRefs && !is_discount_percentage && !hasFlatMaxPercentage) {
|
|
1144
|
+
clearOrderLevelDiscount('voucher');
|
|
1145
|
+
}
|
|
1146
|
+
else if (hasExcludedRefs) {
|
|
1147
|
+
// Excluded items present: must apply percentage against eligible subtotal only.
|
|
1148
|
+
const eligibleBase = hasFlatMaxPercentage
|
|
1149
|
+
? parseNum(getNetItemsValueAfterItemDiscount({
|
|
1150
|
+
itemsPrice: eligibleSubtotal,
|
|
1151
|
+
totalDiscountApplied: 0,
|
|
1152
|
+
orderLevelDiscount: 0,
|
|
1153
|
+
}))
|
|
1154
|
+
: eligibleSubtotal;
|
|
1155
|
+
if (eligibleBase <= 0) {
|
|
1156
|
+
clearOrderLevelDiscount('voucher');
|
|
1157
|
+
}
|
|
1158
|
+
else {
|
|
1159
|
+
const calculatedDiscount = getCappedOfferDiscountAmount({
|
|
1160
|
+
baseAmount: eligibleBase,
|
|
1161
|
+
discountValue: discount_value,
|
|
1162
|
+
isDiscountPercentage: is_discount_percentage,
|
|
1163
|
+
maxDiscountAmount: max_discount_amount,
|
|
1164
|
+
maxDiscountPercentage: max_discount_percentage,
|
|
1165
|
+
});
|
|
1166
|
+
order_discount_details = {
|
|
1167
|
+
...order_discount_details,
|
|
1168
|
+
voucher_discount: parseNum(calculatedDiscount),
|
|
1169
|
+
is_voucher_discount_percentage: false,
|
|
1170
|
+
};
|
|
1171
|
+
hasChanges = true;
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
else if (hasMaxAmountCap) {
|
|
1175
|
+
// Percentage offer with max_amount cap (no excluded items).
|
|
1176
|
+
const projectedPercentage = getPercentageAmount(orderLevelDiscountBaseAmount, discount_value);
|
|
1177
|
+
if (projectedPercentage > max_discount_amount) {
|
|
1178
|
+
// Cap would be breached -- normalize to flat at cap.
|
|
1179
|
+
order_discount_details = {
|
|
1180
|
+
...order_discount_details,
|
|
1181
|
+
voucher_discount: parseNum(max_discount_amount),
|
|
1182
|
+
is_voucher_discount_percentage: false,
|
|
1183
|
+
};
|
|
1184
|
+
}
|
|
1185
|
+
else {
|
|
1186
|
+
// Cap not breached -- un-cap back to percentage (or stay percentage).
|
|
1187
|
+
order_discount_details = {
|
|
1188
|
+
...order_discount_details,
|
|
1189
|
+
voucher_discount: parseNum(discount_value),
|
|
1190
|
+
is_voucher_discount_percentage: true,
|
|
1191
|
+
};
|
|
1192
|
+
}
|
|
1193
|
+
hasChanges = true;
|
|
1194
|
+
}
|
|
1195
|
+
else if (hasFlatMaxPercentage) {
|
|
1196
|
+
if (orderLevelDiscountBaseAmount <= 0) {
|
|
1197
|
+
clearOrderLevelDiscount('voucher');
|
|
1198
|
+
}
|
|
1199
|
+
else {
|
|
1200
|
+
const calculatedDiscount = getCappedOfferDiscountAmount({
|
|
1201
|
+
baseAmount: orderLevelDiscountBaseAmount,
|
|
1202
|
+
discountValue: discount_value,
|
|
1203
|
+
isDiscountPercentage: false,
|
|
1204
|
+
maxDiscountAmount: undefined,
|
|
1205
|
+
maxDiscountPercentage: max_discount_percentage,
|
|
1206
|
+
});
|
|
1207
|
+
order_discount_details = {
|
|
1208
|
+
...order_discount_details,
|
|
1209
|
+
voucher_discount: parseNum(calculatedDiscount),
|
|
1210
|
+
is_voucher_discount_percentage: false,
|
|
1211
|
+
};
|
|
1212
|
+
hasChanges = true;
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
else if (is_discount_percentage) {
|
|
1216
|
+
// Plain percentage offer, no excluded items, no caps.
|
|
1217
|
+
const shouldRevert = order_discount_details.is_voucher_discount_percentage === false &&
|
|
1218
|
+
order_discount_details.voucher_discount != null;
|
|
1219
|
+
if (shouldRevert) {
|
|
1220
|
+
order_discount_details = {
|
|
1221
|
+
...order_discount_details,
|
|
1222
|
+
voucher_discount: parseNum(discount_value),
|
|
1223
|
+
is_voucher_discount_percentage: true,
|
|
1224
|
+
};
|
|
1225
|
+
hasChanges = true;
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
else {
|
|
1229
|
+
if (orderLevelDiscountBaseAmount <= 0) {
|
|
1230
|
+
clearOrderLevelDiscount('voucher');
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
unlockedOffersMap[voucherDiscountId] -= 1;
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
// Items dropped below because their offer/reward is no longer qualified.
|
|
1239
|
+
// The last-synced iteration keeps them with a system-generated void reason
|
|
1240
|
+
// so the removal is traceable and never carries staff name/uid (manual
|
|
1241
|
+
// "Permitted By" attribution only applies to explicit cashier voids).
|
|
1242
|
+
const removedItemsReasonMap = {};
|
|
1243
|
+
const newCategories = [];
|
|
1244
|
+
for (let i = 0; i < categories.length; i++) {
|
|
1245
|
+
const category = categories[i];
|
|
1246
|
+
const newItems = [];
|
|
1247
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
1248
|
+
const item = category.items[j];
|
|
1249
|
+
if (item.item_discount_details != null) {
|
|
1250
|
+
const loyaltyDiscountId = item.item_discount_details.loyalty_discount_id;
|
|
1251
|
+
if (loyaltyDiscountId != null) {
|
|
1252
|
+
const normalizedLoyaltyDiscountId = getNormalizedOfferId(loyaltyDiscountId);
|
|
1253
|
+
const hasRedeemedEntryForLoyalty = normalizedLoyaltyDiscountId != null && (redeemedOffersPresenceMap?.[normalizedLoyaltyDiscountId] ?? 0) > 0;
|
|
1254
|
+
if (!hasRedeemedEntryForLoyalty) {
|
|
1255
|
+
hasChanges = true;
|
|
1256
|
+
if (normalizedLoyaltyDiscountId != null) {
|
|
1257
|
+
removedOfferIds.add(normalizedLoyaltyDiscountId);
|
|
1258
|
+
}
|
|
1259
|
+
removedItemsReasonMap[item._id] = OFFER_NO_LONGER_QUALIFIED_VOID_REASON;
|
|
1260
|
+
continue;
|
|
1261
|
+
}
|
|
1262
|
+
if (unlockedOffersMap[loyaltyDiscountId] == null || unlockedOffersMap[loyaltyDiscountId] <= 0) {
|
|
1263
|
+
hasChanges = true;
|
|
1264
|
+
if (normalizedLoyaltyDiscountId != null) {
|
|
1265
|
+
removedOfferIds.add(normalizedLoyaltyDiscountId);
|
|
1266
|
+
}
|
|
1267
|
+
removedItemsReasonMap[item._id] = OFFER_NO_LONGER_QUALIFIED_VOID_REASON;
|
|
1268
|
+
continue;
|
|
1269
|
+
}
|
|
1270
|
+
unlockedOffersMap[loyaltyDiscountId] -= toPositiveInteger(item.quantity, 1);
|
|
1271
|
+
}
|
|
1272
|
+
const voucherDiscountId = item.item_discount_details.voucher_discount_id;
|
|
1273
|
+
if (voucherDiscountId != null) {
|
|
1274
|
+
const normalizedVoucherDiscountId = getNormalizedOfferId(voucherDiscountId);
|
|
1275
|
+
const hasRedeemedEntryForVoucher = normalizedVoucherDiscountId != null && (redeemedOffersPresenceMap?.[normalizedVoucherDiscountId] ?? 0) > 0;
|
|
1276
|
+
if (!hasRedeemedEntryForVoucher) {
|
|
1277
|
+
hasChanges = true;
|
|
1278
|
+
if (normalizedVoucherDiscountId != null) {
|
|
1279
|
+
removedOfferIds.add(normalizedVoucherDiscountId);
|
|
1280
|
+
}
|
|
1281
|
+
removedItemsReasonMap[item._id] = OFFER_NO_LONGER_QUALIFIED_VOID_REASON;
|
|
1282
|
+
continue;
|
|
1283
|
+
}
|
|
1284
|
+
if (unlockedOffersMap[voucherDiscountId] == null || unlockedOffersMap[voucherDiscountId] <= 0) {
|
|
1285
|
+
hasChanges = true;
|
|
1286
|
+
if (normalizedVoucherDiscountId != null) {
|
|
1287
|
+
removedOfferIds.add(normalizedVoucherDiscountId);
|
|
1288
|
+
}
|
|
1289
|
+
removedItemsReasonMap[item._id] = OFFER_NO_LONGER_QUALIFIED_VOID_REASON;
|
|
1290
|
+
continue;
|
|
1291
|
+
}
|
|
1292
|
+
unlockedOffersMap[voucherDiscountId] -= toPositiveInteger(item.quantity, 1);
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
newItems.push(item);
|
|
1296
|
+
}
|
|
1297
|
+
newCategories.push({
|
|
1298
|
+
...category,
|
|
1299
|
+
items: newItems,
|
|
1300
|
+
});
|
|
1301
|
+
}
|
|
1302
|
+
const reconciledRedeemedOffers = getReconciledRedeemedOffersMapForOfferSync({
|
|
1303
|
+
existingRedeemedOffers: redeemed_offers,
|
|
1304
|
+
categories: newCategories,
|
|
1305
|
+
offers: syncContext.offers,
|
|
1306
|
+
removedOfferIds,
|
|
1307
|
+
unlockedOffersApplicableTimes: syncContext.unlockedOffersApplicableTimes,
|
|
1308
|
+
});
|
|
1309
|
+
if (!areRedeemedOffersEqualForOfferSync(redeemed_offers, reconciledRedeemedOffers)) {
|
|
1310
|
+
redeemed_offers = reconciledRedeemedOffers;
|
|
1311
|
+
hasChanges = true;
|
|
1312
|
+
}
|
|
1313
|
+
if (!hasChanges) {
|
|
1314
|
+
return null;
|
|
1315
|
+
}
|
|
1316
|
+
const syncedIterations = getIterationsWithAutoRemovedVoidReasons({
|
|
1317
|
+
iterations: data.iterations,
|
|
1318
|
+
removedItemsReasonMap,
|
|
1319
|
+
});
|
|
1320
|
+
return {
|
|
1321
|
+
...data,
|
|
1322
|
+
...(syncedIterations != null && syncedIterations !== data.iterations ? { iterations: syncedIterations } : {}),
|
|
1323
|
+
categories: newCategories,
|
|
1324
|
+
order_discount_details,
|
|
1325
|
+
redeemed_offers,
|
|
1326
|
+
};
|
|
1327
|
+
}
|
|
1328
|
+
/**
|
|
1329
|
+
* Stamp the system-generated void reason on the last-synced iteration items
|
|
1330
|
+
* for items the offer synchronization removed from the live cart.
|
|
1331
|
+
*
|
|
1332
|
+
* Removed items' reasons live on the first iteration (mirrors
|
|
1333
|
+
* `getRemoveItemFromCartData`) so the cart, receipts and the backend
|
|
1334
|
+
* void-items pipeline (`getVoidItemsWithReason`) can surface why the item was
|
|
1335
|
+
* removed. Items that never existed in the last-synced iteration (added during
|
|
1336
|
+
* the current edit) and items that already carry an explicit cashier reason
|
|
1337
|
+
* are left untouched.
|
|
1338
|
+
*/
|
|
1339
|
+
function getIterationsWithAutoRemovedVoidReasons({ iterations, removedItemsReasonMap, }) {
|
|
1340
|
+
if (Object.keys(removedItemsReasonMap).length === 0 || iterations == null || iterations.length === 0) {
|
|
1341
|
+
return iterations;
|
|
1342
|
+
}
|
|
1343
|
+
const [firstIteration, ...restIterations] = iterations;
|
|
1344
|
+
let hasChanges = false;
|
|
1345
|
+
const firstIterationCategories = firstIteration.categories.map((category) => {
|
|
1346
|
+
let categoryHasChanges = false;
|
|
1347
|
+
const items = category.items.map((prevItem) => {
|
|
1348
|
+
const autoVoidReason = removedItemsReasonMap[prevItem._id];
|
|
1349
|
+
if (autoVoidReason == null) {
|
|
1350
|
+
return prevItem;
|
|
1351
|
+
}
|
|
1352
|
+
// Keep an explicit cashier-provided reason when one already exists.
|
|
1353
|
+
if (prevItem.void_reason != null && prevItem.void_reason.length > 0) {
|
|
1354
|
+
return prevItem;
|
|
1355
|
+
}
|
|
1356
|
+
categoryHasChanges = true;
|
|
1357
|
+
hasChanges = true;
|
|
1358
|
+
return {
|
|
1359
|
+
...prevItem,
|
|
1360
|
+
void_reason: autoVoidReason,
|
|
1361
|
+
};
|
|
1362
|
+
});
|
|
1363
|
+
return categoryHasChanges ? { ...category, items } : category;
|
|
1364
|
+
});
|
|
1365
|
+
if (!hasChanges) {
|
|
1366
|
+
return iterations;
|
|
1367
|
+
}
|
|
1368
|
+
return [{ ...firstIteration, categories: firstIterationCategories }, ...restIterations];
|
|
1369
|
+
}
|
|
1370
|
+
function hasItemLevelLoyaltyDiscountForId(categories, discountId) {
|
|
1371
|
+
for (let categoryIndex = 0; categoryIndex < categories.length; categoryIndex++) {
|
|
1372
|
+
const items = categories[categoryIndex].items;
|
|
1373
|
+
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
|
1374
|
+
if (items[itemIndex].item_discount_details?.loyalty_discount_id === discountId) {
|
|
1375
|
+
return true;
|
|
1376
|
+
}
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
return false;
|
|
1380
|
+
}
|
|
1381
|
+
function getSyncedWalletRedemptionsAfterGiftCardItemRewardCleanup({ walletRedemptions, orderDiscountDetails, categories, }) {
|
|
1382
|
+
const giftCardRedemptions = walletRedemptions?.gift_cards;
|
|
1383
|
+
if (!Array.isArray(giftCardRedemptions) || giftCardRedemptions.length === 0) {
|
|
1384
|
+
return walletRedemptions;
|
|
1385
|
+
}
|
|
1386
|
+
let hasRemovedGiftCardRedemptions = false;
|
|
1387
|
+
const nextGiftCardRedemptions = giftCardRedemptions.filter((giftCardRedemption) => {
|
|
1388
|
+
const claimedGiftCardId = giftCardRedemption.gift_card_id?.trim();
|
|
1389
|
+
const legacyValueRedeemed = giftCardRedemption.value_redeemed;
|
|
1390
|
+
const claimedTimes = toPositiveInteger(giftCardRedemption.redeemed_times ?? legacyValueRedeemed, 0);
|
|
1391
|
+
if (claimedGiftCardId == null || claimedGiftCardId.length === 0 || claimedTimes <= 0) {
|
|
1392
|
+
hasRemovedGiftCardRedemptions = true;
|
|
1393
|
+
return false;
|
|
1394
|
+
}
|
|
1395
|
+
const rewardType = giftCardRedemption.mystery_gift_type;
|
|
1396
|
+
const isItemRewardType = rewardType === GIFT_CARD_REWARD_TYPE_ENUM.ITEMS || rewardType === GIFT_CARD_REWARD_TYPE_ENUM.DISCOUNT_ITEMS;
|
|
1397
|
+
if (!isItemRewardType) {
|
|
1398
|
+
return true;
|
|
1399
|
+
}
|
|
1400
|
+
const hasOrderLevelDiscount = getNormalizedOfferId(orderDiscountDetails?.loyalty_discount_id) === claimedGiftCardId;
|
|
1401
|
+
const hasItemLevelDiscount = hasItemLevelLoyaltyDiscountForId(categories, claimedGiftCardId);
|
|
1402
|
+
if (hasOrderLevelDiscount || hasItemLevelDiscount) {
|
|
1403
|
+
return true;
|
|
1404
|
+
}
|
|
1405
|
+
hasRemovedGiftCardRedemptions = true;
|
|
1406
|
+
return false;
|
|
1407
|
+
});
|
|
1408
|
+
if (!hasRemovedGiftCardRedemptions) {
|
|
1409
|
+
return walletRedemptions;
|
|
1410
|
+
}
|
|
1411
|
+
return getWalletRedemptionsMap({
|
|
1412
|
+
...walletRedemptions,
|
|
1413
|
+
gift_cards: nextGiftCardRedemptions.length > 0 ? nextGiftCardRedemptions : undefined,
|
|
1414
|
+
});
|
|
1415
|
+
}
|
|
1416
|
+
export function calculateCartOrderInfoAndSyncData(data, courses, listnerSettings, calculateItemMetadata, syncContext) {
|
|
1417
|
+
if (syncContext?.offers != null && syncContext.unlockedOffersApplicableTimes != null) {
|
|
1418
|
+
const offerSynchronizedData = getOfferSynchronizedCartDataForCalculation({
|
|
1419
|
+
data,
|
|
1420
|
+
syncContext,
|
|
1421
|
+
});
|
|
1422
|
+
if (offerSynchronizedData != null) {
|
|
1423
|
+
return calculateCartOrderInfoAndSyncData(offerSynchronizedData, courses, listnerSettings, calculateItemMetadata);
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
const isCompleted = data.order_status === ORDER_STATUS_ENUM.COMPLETED;
|
|
1427
|
+
/* ===============================><=============================== */
|
|
1428
|
+
let subtotal = 0;
|
|
1429
|
+
let totalItemsDiscount = 0;
|
|
1430
|
+
const categories = [];
|
|
1431
|
+
let syncedRedeemedOffers = getRedeemedOffersMap(data.redeemed_offers);
|
|
1432
|
+
let syncedWalletRedemptions = getWalletRedemptionsMap(data.wallet_redemptions);
|
|
1433
|
+
const length = data.categories?.length ?? 0;
|
|
1434
|
+
for (let c = 0; c < length; c++) {
|
|
1435
|
+
const category = data.categories[c];
|
|
1436
|
+
const newItems = [];
|
|
1437
|
+
const itemsLength = category.items.length;
|
|
1438
|
+
for (let i = 0; i < itemsLength; i++) {
|
|
1439
|
+
const item = category.items[i];
|
|
1440
|
+
const syncedItemDiscountDetails = getSyncedItemDiscountDetailsWithoutRewardSpecialDiscount({
|
|
1441
|
+
itemDiscountDetails: item.item_discount_details,
|
|
1442
|
+
redeemedOffers: syncedRedeemedOffers,
|
|
1443
|
+
walletRedemptions: syncedWalletRedemptions,
|
|
1444
|
+
});
|
|
1445
|
+
const { itemSubtotal, item_discount_details } = calculateItemTotalWithTax({
|
|
1446
|
+
...item,
|
|
1447
|
+
item_discount_details: syncedItemDiscountDetails ?? null,
|
|
1448
|
+
});
|
|
1449
|
+
// A completed order marks every not-ready item as ready — keep
|
|
1450
|
+
// ready_minutes in sync with that transition (mirrors the order status
|
|
1451
|
+
// updates): measured from the item's own prep clock, falling back to the
|
|
1452
|
+
// order's stage clock. Already-ready items keep their stored duration.
|
|
1453
|
+
// Keep in sync with items-service updateOrderStatus (aggregation pipeline
|
|
1454
|
+
// stamp) and updateOrder (new-item prep stamp).
|
|
1455
|
+
let readyMinutes;
|
|
1456
|
+
if (item.is_ready !== true && isCompleted) {
|
|
1457
|
+
const readyMinutesAnchor = item.prep_started_at ?? data.order_status_updated_at;
|
|
1458
|
+
const readyMinutesAnchorTime = readyMinutesAnchor != null ? new Date(readyMinutesAnchor).getTime() : undefined;
|
|
1459
|
+
if (readyMinutesAnchorTime != null && !Number.isNaN(readyMinutesAnchorTime)) {
|
|
1460
|
+
readyMinutes = Math.max(0, Math.floor((Date.now() - readyMinutesAnchorTime) / 60000));
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1463
|
+
const newItem = {
|
|
1464
|
+
...item,
|
|
1465
|
+
...(readyMinutes != null ? { ready_minutes: readyMinutes } : {}),
|
|
1466
|
+
is_ready: item.is_ready || isCompleted,
|
|
1467
|
+
item_discount_details,
|
|
1468
|
+
subtotal: itemSubtotal,
|
|
1469
|
+
// These values will be auto calculated
|
|
1470
|
+
_discount_amount: 0,
|
|
1471
|
+
_gross_amount: 0,
|
|
1472
|
+
_gst_amount: 0,
|
|
1473
|
+
_net_amount: 0,
|
|
1474
|
+
};
|
|
1475
|
+
delete newItem.void_reason;
|
|
1476
|
+
newItems.push(newItem);
|
|
1477
|
+
if (item_discount_details != null && item_discount_details.total_discount > 0) {
|
|
1478
|
+
totalItemsDiscount += item_discount_details.total_discount;
|
|
1479
|
+
}
|
|
1480
|
+
subtotal += itemSubtotal;
|
|
1481
|
+
}
|
|
1482
|
+
categories.push({
|
|
1483
|
+
...category,
|
|
1484
|
+
items: newItems,
|
|
1485
|
+
});
|
|
1486
|
+
}
|
|
1487
|
+
let syncedOrderDiscountDetails = data.order_discount_details;
|
|
1488
|
+
syncedOrderDiscountDetails = getSyncedOrderDiscountDetailsWithoutRewardSpecialDiscount({
|
|
1489
|
+
orderDiscountDetails: syncedOrderDiscountDetails,
|
|
1490
|
+
redeemedOffers: syncedRedeemedOffers,
|
|
1491
|
+
walletRedemptions: syncedWalletRedemptions,
|
|
1492
|
+
});
|
|
1493
|
+
const hasWalletCashbackLoyaltyDiscount = isWalletCashbackLoyaltyDiscount(syncedOrderDiscountDetails);
|
|
1494
|
+
const hasWalletCashbackMetadata = Number(syncedWalletRedemptions?.cashback_amount ?? 0) > 0;
|
|
1495
|
+
if (hasWalletCashbackLoyaltyDiscount) {
|
|
1496
|
+
const requestedWalletCashbackAmount = getWalletCashbackAmountFromOrderData({
|
|
1497
|
+
orderDiscountDetails: syncedOrderDiscountDetails,
|
|
1498
|
+
walletRedemptions: syncedWalletRedemptions,
|
|
1499
|
+
});
|
|
1500
|
+
const maxApplicableWalletCashback = parseNum(Math.max(0, subtotal));
|
|
1501
|
+
const syncedWalletCashbackAmount = parseNum(Math.min(requestedWalletCashbackAmount, maxApplicableWalletCashback));
|
|
1502
|
+
syncedOrderDiscountDetails = getSyncedWalletCashbackOrderDiscountDetails({
|
|
1503
|
+
orderDiscountDetails: syncedOrderDiscountDetails,
|
|
1504
|
+
walletCashbackAmount: syncedWalletCashbackAmount,
|
|
1505
|
+
});
|
|
1506
|
+
syncedWalletRedemptions = getWalletRedemptionsMap({
|
|
1507
|
+
...(syncedWalletRedemptions ?? {}),
|
|
1508
|
+
cashback_amount: syncedWalletCashbackAmount > 0 ? syncedWalletCashbackAmount : undefined,
|
|
1509
|
+
});
|
|
1510
|
+
}
|
|
1511
|
+
else if (hasWalletCashbackMetadata) {
|
|
1512
|
+
// Offers cannot create an active cashback order discount; keep cashback metadata consistent.
|
|
1513
|
+
syncedWalletRedemptions = getWalletRedemptionsMap({
|
|
1514
|
+
...(syncedWalletRedemptions ?? {}),
|
|
1515
|
+
cashback_amount: undefined,
|
|
1516
|
+
});
|
|
1517
|
+
}
|
|
1518
|
+
syncedWalletRedemptions = getSyncedWalletRedemptionsAfterGiftCardItemRewardCleanup({
|
|
1519
|
+
walletRedemptions: syncedWalletRedemptions,
|
|
1520
|
+
orderDiscountDetails: syncedOrderDiscountDetails,
|
|
1521
|
+
categories,
|
|
1522
|
+
});
|
|
1523
|
+
const hasWalletGiftCardMetadata = (syncedWalletRedemptions?.gift_cards?.length ?? 0) > 0;
|
|
1524
|
+
// Empty carts must not retain offer-redemption state or gift-card wallet claims.
|
|
1525
|
+
if (subtotal <= 0) {
|
|
1526
|
+
if (syncedRedeemedOffers != null) {
|
|
1527
|
+
syncedRedeemedOffers = null;
|
|
1528
|
+
}
|
|
1529
|
+
if (hasWalletGiftCardMetadata) {
|
|
1530
|
+
syncedWalletRedemptions = getWalletRedemptionsMap({
|
|
1531
|
+
...(syncedWalletRedemptions ?? {}),
|
|
1532
|
+
gift_cards: undefined,
|
|
1533
|
+
});
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
const applyGSTOnOriginalPrice = data.exclude_discount_from_gst;
|
|
1537
|
+
const previousOrderDiscountDetails = syncedOrderDiscountDetails;
|
|
1538
|
+
const order_discount_details = getDiscountDetails(previousOrderDiscountDetails, subtotal);
|
|
1539
|
+
syncedRedeemedOffers = getSyncedRedeemedOffersAfterOrderDiscountOverflowCleanup({
|
|
1540
|
+
redeemedOffers: syncedRedeemedOffers,
|
|
1541
|
+
previousOrderDiscountDetails,
|
|
1542
|
+
nextOrderDiscountDetails: order_discount_details,
|
|
1543
|
+
});
|
|
1544
|
+
syncedWalletRedemptions = getSyncedWalletRedemptionsAfterOrderDiscountOverflowCleanup({
|
|
1545
|
+
walletRedemptions: syncedWalletRedemptions,
|
|
1546
|
+
previousOrderDiscountDetails,
|
|
1547
|
+
nextOrderDiscountDetails: order_discount_details,
|
|
1548
|
+
});
|
|
1549
|
+
// This can be both negative or positive
|
|
1550
|
+
const orderDiscount = order_discount_details?.total_discount ?? 0;
|
|
1551
|
+
// However total order discount can never be negative
|
|
1552
|
+
let totalOrderDiscount = orderDiscount;
|
|
1553
|
+
if (totalOrderDiscount < 0) {
|
|
1554
|
+
// if the total order discount is negative, then it is a surcharge
|
|
1555
|
+
totalOrderDiscount = 0;
|
|
1556
|
+
}
|
|
1557
|
+
// Payable price is the price after applying order discount
|
|
1558
|
+
// A discount in this case can be both negative or positive
|
|
1559
|
+
// So in actual, payable price is the price of all items after applying all discounts and surcharges
|
|
1560
|
+
const payablePrice = subtotal - orderDiscount;
|
|
1561
|
+
const isDineInOrder = PLATFORMS_BY_ORDER_TYPE[ORDER_TYPE_ENUM.DINE_IN].includes(data.platform);
|
|
1562
|
+
const isDeliveryOrder = PLATFORMS_BY_ORDER_TYPE[ORDER_TYPE_ENUM.DELIVERY].includes(data.platform);
|
|
1563
|
+
const isTakeawayOrder = PLATFORMS_BY_ORDER_TYPE[ORDER_TYPE_ENUM.TAKE_AWAY].includes(data.platform);
|
|
1564
|
+
const isPackagingOrder = isDeliveryOrder || isTakeawayOrder;
|
|
1565
|
+
let service_fee = data.service_fee ?? 0;
|
|
1566
|
+
if (isDineInOrder) {
|
|
1567
|
+
if (listnerSettings != null) {
|
|
1568
|
+
if (listnerSettings.min_service_fee_order_value > 0 &&
|
|
1569
|
+
payablePrice < listnerSettings.min_service_fee_order_value) {
|
|
1570
|
+
service_fee = 0;
|
|
1571
|
+
}
|
|
1572
|
+
else if (listnerSettings.dinein_service_fee > 0) {
|
|
1573
|
+
service_fee = listnerSettings.dinein_service_fee;
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
else {
|
|
1578
|
+
service_fee = 0;
|
|
1579
|
+
}
|
|
1580
|
+
let packaging_charges = data.packaging_charges ?? 0;
|
|
1581
|
+
if (isPackagingOrder) {
|
|
1582
|
+
if (listnerSettings != null) {
|
|
1583
|
+
if (listnerSettings.min_packaging_charges_order_value > 0 &&
|
|
1584
|
+
payablePrice < listnerSettings.min_packaging_charges_order_value) {
|
|
1585
|
+
packaging_charges = 0;
|
|
1586
|
+
}
|
|
1587
|
+
else if (listnerSettings.packaging_charges_free_over > 0 &&
|
|
1588
|
+
payablePrice >= listnerSettings.packaging_charges_free_over) {
|
|
1589
|
+
packaging_charges = 0;
|
|
1590
|
+
}
|
|
1591
|
+
else if (listnerSettings.packaging_charges > 0) {
|
|
1592
|
+
packaging_charges = listnerSettings.packaging_charges;
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
else {
|
|
1597
|
+
packaging_charges = 0;
|
|
1598
|
+
}
|
|
1599
|
+
// Original items price is the price of all items without any discount
|
|
1600
|
+
// A discount in this case can only be positive
|
|
1601
|
+
const originalItemsPrice = subtotal + totalItemsDiscount;
|
|
1602
|
+
let grand_total = subtotal;
|
|
1603
|
+
if (!applyGSTOnOriginalPrice) {
|
|
1604
|
+
grand_total -= totalOrderDiscount;
|
|
1605
|
+
}
|
|
1606
|
+
// ===============================><===============================
|
|
1607
|
+
let surcharge_amount = data.surcharge;
|
|
1608
|
+
if (data.is_surcharge_taxable) {
|
|
1609
|
+
surcharge_amount = getSurchargeAmount(payablePrice, data.surcharge, data.is_surcharge_percentage);
|
|
1610
|
+
grand_total += surcharge_amount;
|
|
1611
|
+
}
|
|
1612
|
+
let service_fee_amount = service_fee;
|
|
1613
|
+
if (data.is_service_fee_taxable) {
|
|
1614
|
+
const p = data.apply_service_fee_on_original_price ? originalItemsPrice : payablePrice;
|
|
1615
|
+
// Commented out because service fee does not apply on surcharge
|
|
1616
|
+
// if (surcharge_amount) {
|
|
1617
|
+
// p += surcharge_amount;
|
|
1618
|
+
// }
|
|
1619
|
+
service_fee_amount = getServiceFeeAmount(p, service_fee, data.is_service_fee_percentage);
|
|
1620
|
+
grand_total += service_fee_amount;
|
|
1621
|
+
}
|
|
1622
|
+
// GST is represented in percentage. We convert it to decimal
|
|
1623
|
+
let amount = grand_total;
|
|
1624
|
+
if (applyGSTOnOriginalPrice) {
|
|
1625
|
+
amount += totalItemsDiscount;
|
|
1626
|
+
}
|
|
1627
|
+
let gst_amount = 0;
|
|
1628
|
+
const gst = data.gst ?? 0;
|
|
1629
|
+
if (data.is_gst_inclusive) {
|
|
1630
|
+
// If GST is inclusive, it means that the Tax is already included in the Sale Value
|
|
1631
|
+
// However, we need to calculate the Sale Value without the Tax
|
|
1632
|
+
// And then calculate the Tax amount from the Sale Value without Tax
|
|
1633
|
+
gst_amount = parseFloatCeil((amount * gst) / (100 + gst));
|
|
1634
|
+
}
|
|
1635
|
+
else {
|
|
1636
|
+
gst_amount = getPercentageAmount(amount, gst);
|
|
1637
|
+
// Add GST amount to orderSubtotal
|
|
1638
|
+
grand_total += gst_amount;
|
|
1639
|
+
}
|
|
1640
|
+
if (applyGSTOnOriginalPrice) {
|
|
1641
|
+
grand_total -= totalOrderDiscount;
|
|
1642
|
+
}
|
|
1643
|
+
// ===============================><===============================
|
|
1644
|
+
if (!data.is_surcharge_taxable) {
|
|
1645
|
+
surcharge_amount = getSurchargeAmount(payablePrice, data.surcharge, data.is_surcharge_percentage);
|
|
1646
|
+
grand_total += surcharge_amount;
|
|
1647
|
+
}
|
|
1648
|
+
if (!data.is_service_fee_taxable) {
|
|
1649
|
+
const p = data.apply_service_fee_on_original_price ? originalItemsPrice : payablePrice;
|
|
1650
|
+
// Commented out because service fee does not apply on surcharge
|
|
1651
|
+
// if (surcharge_amount) {
|
|
1652
|
+
// p += surcharge_amount;
|
|
1653
|
+
// }
|
|
1654
|
+
service_fee_amount = getServiceFeeAmount(p, service_fee, data.is_service_fee_percentage);
|
|
1655
|
+
grand_total += service_fee_amount;
|
|
1656
|
+
}
|
|
1657
|
+
// ===============================><===============================
|
|
1658
|
+
// Add delivery fee to grand_total
|
|
1659
|
+
let delivery_cost = 0;
|
|
1660
|
+
if (isDeliveryOrder) {
|
|
1661
|
+
if (listnerSettings != null) {
|
|
1662
|
+
delivery_cost = getDeliveryAmt({
|
|
1663
|
+
platform: data.platform,
|
|
1664
|
+
contemporary_delivery_cost: data.delivery_details?.delivery_cost ?? 0,
|
|
1665
|
+
delivery_free_over: listnerSettings.delivery_free_over,
|
|
1666
|
+
delivery_min_cost: listnerSettings.delivery_min_cost,
|
|
1667
|
+
payable_price: payablePrice,
|
|
1668
|
+
});
|
|
1669
|
+
}
|
|
1670
|
+
else if (data.delivery_details != null) {
|
|
1671
|
+
delivery_cost = data.delivery_details.delivery_cost;
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
grand_total += delivery_cost;
|
|
1675
|
+
// Add packaging charges to grand_total
|
|
1676
|
+
grand_total += packaging_charges;
|
|
1677
|
+
// Add adjustment amount (can be positive or negative)
|
|
1678
|
+
grand_total += data.adjustment_amount ?? 0;
|
|
1679
|
+
// TODO: We are not using tax_amount for now
|
|
1680
|
+
const tax_amount = 0;
|
|
1681
|
+
// Add tax amount to orderSubtotal
|
|
1682
|
+
// grand_total += tax_amount;
|
|
1683
|
+
// Add tip amount to grand_total
|
|
1684
|
+
grand_total += data.tip_amount ?? 0;
|
|
1685
|
+
// Add Card Charges to grand_total
|
|
1686
|
+
const { cardCharges, amountCollected } = getCardChargesAndAmountCollected(data.payments);
|
|
1687
|
+
grand_total += cardCharges;
|
|
1688
|
+
// ===============================><===============================
|
|
1689
|
+
const delivery_details = data.delivery_details
|
|
1690
|
+
? {
|
|
1691
|
+
...data.delivery_details,
|
|
1692
|
+
delivery_cost,
|
|
1693
|
+
}
|
|
1694
|
+
: null;
|
|
1695
|
+
grand_total = parseNum(grand_total);
|
|
1696
|
+
// We will not add orderDiscount or itemDiscount if their value is negative
|
|
1697
|
+
// because if they are negative, then they are surcharge
|
|
1698
|
+
let total_discount_applied = 0;
|
|
1699
|
+
total_discount_applied += totalOrderDiscount;
|
|
1700
|
+
total_discount_applied += totalItemsDiscount;
|
|
1701
|
+
let tax_invoice_provider_charges = 0;
|
|
1702
|
+
if (gst > 0) {
|
|
1703
|
+
tax_invoice_provider_charges = data.tax_invoice_provider_charges ?? 0;
|
|
1704
|
+
if (listnerSettings != null && listnerSettings.tax_invoice_provider !== TAX_INVOICE_PROVIDER_ENUM.NONE) {
|
|
1705
|
+
const tax_invoice_charges = TAX_INVOICE_PROVIDER_CHARGES_MAP.get(listnerSettings.tax_invoice_provider);
|
|
1706
|
+
if (tax_invoice_charges != null) {
|
|
1707
|
+
tax_invoice_provider_charges = tax_invoice_charges.fixed_charges;
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
}
|
|
1711
|
+
grand_total += tax_invoice_provider_charges;
|
|
1712
|
+
const amount_collected = parseNum(amountCollected);
|
|
1713
|
+
const amount_earned = parseNum(grand_total - cardCharges);
|
|
1714
|
+
if (calculateItemMetadata) {
|
|
1715
|
+
let leftGrossAmount = originalItemsPrice;
|
|
1716
|
+
let leftNetAmount = payablePrice;
|
|
1717
|
+
let leftDiscountAmount = total_discount_applied;
|
|
1718
|
+
let leftGstAmount = gst_amount;
|
|
1719
|
+
categories.forEach((category, categoryIndex) => {
|
|
1720
|
+
const isLastCategory = categoryIndex === categories.length - 1;
|
|
1721
|
+
category.items.forEach((item, itemIndex) => {
|
|
1722
|
+
const isLastItem = isLastCategory && itemIndex === category.items.length - 1;
|
|
1723
|
+
let itemDiscountAmount = item.item_discount_details?.total_discount ?? 0;
|
|
1724
|
+
if (itemDiscountAmount < 0) {
|
|
1725
|
+
itemDiscountAmount = 0;
|
|
1726
|
+
}
|
|
1727
|
+
let grossAmount = item.subtotal + itemDiscountAmount;
|
|
1728
|
+
const orderDiscountAmount = (item.subtotal / (subtotal || 1)) * totalOrderDiscount;
|
|
1729
|
+
let netAmount = item.subtotal - orderDiscountAmount;
|
|
1730
|
+
let itemTotalDiscountAmount = itemDiscountAmount + orderDiscountAmount;
|
|
1731
|
+
let itemGst = 0;
|
|
1732
|
+
if (gst_amount > 0) {
|
|
1733
|
+
itemGst += calculateGstForItem({
|
|
1734
|
+
apply_service_fee_on_original_price: data.apply_service_fee_on_original_price,
|
|
1735
|
+
applyGSTOnOriginalPrice,
|
|
1736
|
+
gst,
|
|
1737
|
+
is_gst_inclusive: data.is_gst_inclusive,
|
|
1738
|
+
is_service_fee_percentage: data.is_service_fee_percentage,
|
|
1739
|
+
is_service_fee_taxable: data.is_service_fee_taxable,
|
|
1740
|
+
is_surcharge_percentage: data.is_surcharge_percentage,
|
|
1741
|
+
is_surcharge_taxable: data.is_surcharge_taxable,
|
|
1742
|
+
grossAmount,
|
|
1743
|
+
netAmount,
|
|
1744
|
+
originalItemsPrice,
|
|
1745
|
+
payablePrice,
|
|
1746
|
+
subtotal: item.subtotal,
|
|
1747
|
+
service_fee,
|
|
1748
|
+
surcharge: data.surcharge,
|
|
1749
|
+
totalItemsDiscount: itemDiscountAmount,
|
|
1750
|
+
totalOrderDiscount: orderDiscountAmount,
|
|
1751
|
+
});
|
|
1752
|
+
}
|
|
1753
|
+
grossAmount = parseFloatCeil(grossAmount);
|
|
1754
|
+
netAmount = parseFloatCeil(netAmount);
|
|
1755
|
+
itemTotalDiscountAmount = parseFloatCeil(itemTotalDiscountAmount);
|
|
1756
|
+
itemGst = parseFloatCeil(itemGst);
|
|
1757
|
+
leftGrossAmount -= grossAmount;
|
|
1758
|
+
leftNetAmount -= netAmount;
|
|
1759
|
+
leftDiscountAmount -= itemTotalDiscountAmount;
|
|
1760
|
+
leftGstAmount -= itemGst;
|
|
1761
|
+
if (isLastItem) {
|
|
1762
|
+
grossAmount += leftGrossAmount;
|
|
1763
|
+
netAmount += leftNetAmount;
|
|
1764
|
+
itemTotalDiscountAmount += leftDiscountAmount;
|
|
1765
|
+
itemGst += leftGstAmount;
|
|
1766
|
+
}
|
|
1767
|
+
item._gross_amount = Math.max(grossAmount, 0);
|
|
1768
|
+
item._net_amount = Math.max(netAmount, 0);
|
|
1769
|
+
item._discount_amount = Math.max(itemTotalDiscountAmount, 0);
|
|
1770
|
+
item._gst_amount = Math.max(itemGst, 0);
|
|
1771
|
+
});
|
|
1772
|
+
});
|
|
1773
|
+
}
|
|
1774
|
+
const cartData = {
|
|
1775
|
+
...data,
|
|
1776
|
+
categories,
|
|
1777
|
+
order_discount_details,
|
|
1778
|
+
redeemed_offers: syncedRedeemedOffers,
|
|
1779
|
+
wallet_redemptions: syncedWalletRedemptions,
|
|
1780
|
+
total_discount_applied,
|
|
1781
|
+
delivery_details,
|
|
1782
|
+
tax_amount,
|
|
1783
|
+
service_fee_amount,
|
|
1784
|
+
surcharge_amount,
|
|
1785
|
+
amount_collected,
|
|
1786
|
+
amount_earned,
|
|
1787
|
+
tax_invoice_provider_charges,
|
|
1788
|
+
gst_amount,
|
|
1789
|
+
price: parseNum(originalItemsPrice),
|
|
1790
|
+
service_fee,
|
|
1791
|
+
packaging_charges,
|
|
1792
|
+
grand_total,
|
|
1793
|
+
};
|
|
1794
|
+
// ===============================><===============================
|
|
1795
|
+
let syncedData = cartData;
|
|
1796
|
+
if (courses != null) {
|
|
1797
|
+
syncedData = syncOrderCoursesWithOrderItemsInCart(cartData, courses);
|
|
1798
|
+
}
|
|
1799
|
+
return syncedData;
|
|
1800
|
+
}
|
|
1801
|
+
function getNormalizedRewardClaim(rewardClaim) {
|
|
1802
|
+
if (rewardClaim == null) {
|
|
1803
|
+
return null;
|
|
1804
|
+
}
|
|
1805
|
+
if (rewardClaim.type === 'offer') {
|
|
1806
|
+
const offerId = getNormalizedOfferId(rewardClaim.offerId);
|
|
1807
|
+
if (offerId == null) {
|
|
1808
|
+
return null;
|
|
1809
|
+
}
|
|
1810
|
+
return {
|
|
1811
|
+
type: 'offer',
|
|
1812
|
+
offerId,
|
|
1813
|
+
};
|
|
1814
|
+
}
|
|
1815
|
+
const giftCardId = getNormalizedOfferId(rewardClaim.giftCardId);
|
|
1816
|
+
if (giftCardId == null) {
|
|
1817
|
+
return null;
|
|
1818
|
+
}
|
|
1819
|
+
return {
|
|
1820
|
+
type: 'gift-card',
|
|
1821
|
+
giftCardId,
|
|
1822
|
+
...(rewardClaim.mysteryGiftType != null
|
|
1823
|
+
? {
|
|
1824
|
+
mysteryGiftType: rewardClaim.mysteryGiftType,
|
|
1825
|
+
}
|
|
1826
|
+
: {}),
|
|
1827
|
+
};
|
|
1828
|
+
}
|
|
1829
|
+
function getItemDiscountDetailsWithoutRewardMarkers(itemDiscountDetails) {
|
|
1830
|
+
if (itemDiscountDetails == null) {
|
|
1831
|
+
return null;
|
|
1832
|
+
}
|
|
1833
|
+
const discountDetailsWithoutLoyaltyReward = clearLoyaltyDiscountFields(itemDiscountDetails);
|
|
1834
|
+
return clearVoucherDiscountFields(discountDetailsWithoutLoyaltyReward);
|
|
1835
|
+
}
|
|
1836
|
+
function getValidatedRewardItemAddContext({ item, rewardClaim, }) {
|
|
1837
|
+
const itemDiscountDetails = item.item_discount_details;
|
|
1838
|
+
const loyaltyDiscountId = getNormalizedOfferId(itemDiscountDetails?.loyalty_discount_id);
|
|
1839
|
+
const voucherDiscountId = getNormalizedOfferId(itemDiscountDetails?.voucher_discount_id);
|
|
1840
|
+
const hasRewardMarkers = loyaltyDiscountId != null || voucherDiscountId != null;
|
|
1841
|
+
if (!hasRewardMarkers) {
|
|
1842
|
+
return {
|
|
1843
|
+
item,
|
|
1844
|
+
rewardClaim: null,
|
|
1845
|
+
};
|
|
1846
|
+
}
|
|
1847
|
+
if (rewardClaim == null) {
|
|
1848
|
+
return {
|
|
1849
|
+
item: {
|
|
1850
|
+
...item,
|
|
1851
|
+
item_discount_details: getItemDiscountDetailsWithoutRewardMarkers(itemDiscountDetails),
|
|
1852
|
+
},
|
|
1853
|
+
rewardClaim: null,
|
|
1854
|
+
};
|
|
1855
|
+
}
|
|
1856
|
+
const isMatchingRewardClaim = rewardClaim.type === 'gift-card'
|
|
1857
|
+
? loyaltyDiscountId != null && voucherDiscountId == null && rewardClaim.giftCardId === loyaltyDiscountId
|
|
1858
|
+
: rewardClaim.offerId === loyaltyDiscountId || rewardClaim.offerId === voucherDiscountId;
|
|
1859
|
+
if (isMatchingRewardClaim) {
|
|
1860
|
+
return {
|
|
1861
|
+
item,
|
|
1862
|
+
rewardClaim,
|
|
1863
|
+
};
|
|
1864
|
+
}
|
|
1865
|
+
return {
|
|
1866
|
+
item: {
|
|
1867
|
+
...item,
|
|
1868
|
+
item_discount_details: getItemDiscountDetailsWithoutRewardMarkers(itemDiscountDetails),
|
|
1869
|
+
},
|
|
1870
|
+
rewardClaim: null,
|
|
1871
|
+
};
|
|
1872
|
+
}
|
|
1873
|
+
function getOrderDataWithSyncedGiftCardRewardClaim({ data, rewardClaim, }) {
|
|
1874
|
+
if (rewardClaim == null || rewardClaim.type !== 'gift-card') {
|
|
1875
|
+
return data;
|
|
1876
|
+
}
|
|
1877
|
+
const claimedGiftCardId = rewardClaim.giftCardId?.toString().trim();
|
|
1878
|
+
if (claimedGiftCardId == null || claimedGiftCardId.length === 0) {
|
|
1879
|
+
return data;
|
|
1880
|
+
}
|
|
1881
|
+
const normalizedWalletRedemptions = getWalletRedemptionsMap(data.wallet_redemptions);
|
|
1882
|
+
const currentGiftCardRedemptions = normalizedWalletRedemptions?.gift_cards ?? [];
|
|
1883
|
+
let hasUpdatedExistingClaim = false;
|
|
1884
|
+
const nextGiftCardRedemptions = currentGiftCardRedemptions.map((giftCardRedemption) => {
|
|
1885
|
+
if (giftCardRedemption.gift_card_id !== claimedGiftCardId) {
|
|
1886
|
+
return giftCardRedemption;
|
|
1887
|
+
}
|
|
1888
|
+
hasUpdatedExistingClaim = true;
|
|
1889
|
+
return {
|
|
1890
|
+
...giftCardRedemption,
|
|
1891
|
+
redeemed_times: toPositiveInteger(giftCardRedemption.redeemed_times, 0) + 1,
|
|
1892
|
+
...(rewardClaim.mysteryGiftType != null
|
|
1893
|
+
? {
|
|
1894
|
+
mystery_gift_type: rewardClaim.mysteryGiftType,
|
|
1895
|
+
}
|
|
1896
|
+
: {}),
|
|
1897
|
+
};
|
|
1898
|
+
});
|
|
1899
|
+
if (!hasUpdatedExistingClaim) {
|
|
1900
|
+
nextGiftCardRedemptions.push({
|
|
1901
|
+
gift_card_id: claimedGiftCardId,
|
|
1902
|
+
redeemed_times: 1,
|
|
1903
|
+
...(rewardClaim.mysteryGiftType != null
|
|
1904
|
+
? {
|
|
1905
|
+
mystery_gift_type: rewardClaim.mysteryGiftType,
|
|
1906
|
+
}
|
|
1907
|
+
: {}),
|
|
1908
|
+
});
|
|
1909
|
+
}
|
|
1910
|
+
const nextWalletRedemptions = getWalletRedemptionsMap({
|
|
1911
|
+
...(normalizedWalletRedemptions ?? {}),
|
|
1912
|
+
gift_cards: nextGiftCardRedemptions,
|
|
1913
|
+
});
|
|
1914
|
+
if (isEqual(nextWalletRedemptions, normalizedWalletRedemptions)) {
|
|
1915
|
+
return data;
|
|
1916
|
+
}
|
|
1917
|
+
return {
|
|
1918
|
+
...data,
|
|
1919
|
+
wallet_redemptions: nextWalletRedemptions,
|
|
1920
|
+
};
|
|
1921
|
+
}
|
|
1922
|
+
function getOrderDataWithSyncedItemRewardRedemptionEvidence({ data, itemDiscountDetails, rewardClaim, }) {
|
|
1923
|
+
if (itemDiscountDetails == null || rewardClaim == null) {
|
|
1924
|
+
return data;
|
|
1925
|
+
}
|
|
1926
|
+
const evidenceOfferIds = new Set();
|
|
1927
|
+
const loyaltyDiscountId = getNormalizedOfferId(itemDiscountDetails.loyalty_discount_id);
|
|
1928
|
+
const voucherDiscountId = getNormalizedOfferId(itemDiscountDetails.voucher_discount_id);
|
|
1929
|
+
if (rewardClaim.type === 'offer') {
|
|
1930
|
+
if (rewardClaim.offerId === loyaltyDiscountId && loyaltyDiscountId != null) {
|
|
1931
|
+
evidenceOfferIds.add(loyaltyDiscountId);
|
|
1932
|
+
}
|
|
1933
|
+
if (rewardClaim.offerId === voucherDiscountId && voucherDiscountId != null) {
|
|
1934
|
+
evidenceOfferIds.add(voucherDiscountId);
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
if (evidenceOfferIds.size === 0) {
|
|
1938
|
+
return data;
|
|
1939
|
+
}
|
|
1940
|
+
const nextRedeemedOffers = {
|
|
1941
|
+
...(getRedeemedOffersMap(data.redeemed_offers) ?? {}),
|
|
1942
|
+
};
|
|
1943
|
+
let hasChanges = false;
|
|
1944
|
+
evidenceOfferIds.forEach((offerId) => {
|
|
1945
|
+
const currentCount = toPositiveInteger(nextRedeemedOffers[offerId], 0);
|
|
1946
|
+
if (currentCount <= 0) {
|
|
1947
|
+
nextRedeemedOffers[offerId] = 1;
|
|
1948
|
+
hasChanges = true;
|
|
1949
|
+
}
|
|
1950
|
+
});
|
|
1951
|
+
if (!hasChanges) {
|
|
1952
|
+
return data;
|
|
1953
|
+
}
|
|
1954
|
+
return {
|
|
1955
|
+
...data,
|
|
1956
|
+
redeemed_offers: getRedeemedOffersMap(nextRedeemedOffers),
|
|
1957
|
+
};
|
|
1958
|
+
}
|
|
1959
|
+
export const getOrderItemComaparableData = (item) => ({
|
|
1960
|
+
item_id: item.item_id,
|
|
1961
|
+
menu_id: item.menu_id,
|
|
1962
|
+
item_type: item.item_type,
|
|
1963
|
+
name: item.name,
|
|
1964
|
+
price: item.price,
|
|
1965
|
+
item_discount_details: item.item_discount_details,
|
|
1966
|
+
tax_rate: item.tax_rate,
|
|
1967
|
+
special_instructions: item.special_instructions,
|
|
1968
|
+
modifiers: item.modifiers,
|
|
1969
|
+
_id: item._id,
|
|
1970
|
+
quantity: item.quantity,
|
|
1971
|
+
});
|
|
1972
|
+
/**
|
|
1973
|
+
* Merge-comparable projection of an order item: everything that makes two
|
|
1974
|
+
* items "the same" for quantity merging (item, modifiers, per-unit terms),
|
|
1975
|
+
* with the quantity-dependent amounts normalized to a single unit.
|
|
1976
|
+
*
|
|
1977
|
+
* Two items whose projections are deeply equal merge into ONE with a summed
|
|
1978
|
+
* quantity — the cart-line merge (`getAddItemToCartData`) and the specials
|
|
1979
|
+
* slot merge (`convertSpecialItemToOrderItem`, e.g. the same pizza picked in
|
|
1980
|
+
* several slots of a deal) both key on this projection.
|
|
1981
|
+
*/
|
|
1982
|
+
export function getComparableOrderItem(item) {
|
|
1983
|
+
return {
|
|
1984
|
+
item_id: item.item_id,
|
|
1985
|
+
menu_id: item.menu_id,
|
|
1986
|
+
item_type: item.item_type,
|
|
1987
|
+
name: item.name,
|
|
1988
|
+
price: item.price,
|
|
1989
|
+
item_discount_details: getItemQuantityChangeResults({
|
|
1990
|
+
prevQuantity: item.quantity,
|
|
1991
|
+
newQuantity: 1,
|
|
1992
|
+
prevItemDiscountDetails: item.item_discount_details,
|
|
1993
|
+
}),
|
|
1994
|
+
tax_rate: item.tax_rate,
|
|
1995
|
+
special_instructions: item.special_instructions,
|
|
1996
|
+
is_ready: item.is_ready ?? false,
|
|
1997
|
+
modifiers: item.modifiers,
|
|
1998
|
+
};
|
|
1999
|
+
}
|
|
2000
|
+
export function getAddItemToCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
|
|
2001
|
+
const { category_id, name, item, chosen_course_id, replace_id, rewardClaim } = payload;
|
|
2002
|
+
const normalizedRewardClaim = getNormalizedRewardClaim(rewardClaim);
|
|
2003
|
+
const validatedRewardItemAddContext = getValidatedRewardItemAddContext({
|
|
2004
|
+
item,
|
|
2005
|
+
rewardClaim: normalizedRewardClaim,
|
|
2006
|
+
});
|
|
2007
|
+
const itemWithValidatedRewardClaim = validatedRewardItemAddContext.item;
|
|
2008
|
+
const validatedRewardClaim = validatedRewardItemAddContext.rewardClaim;
|
|
2009
|
+
const comparableItem = getComparableOrderItem(itemWithValidatedRewardClaim);
|
|
2010
|
+
let mainItemId = itemWithValidatedRewardClaim._id;
|
|
2011
|
+
const orderData = { ...data };
|
|
2012
|
+
// =====================><=====================
|
|
2013
|
+
const categories = [...(orderData.categories ?? [])];
|
|
2014
|
+
let didFindCategory = false;
|
|
2015
|
+
const categoriesLength = categories.length;
|
|
2016
|
+
for (let i = 0; i < categoriesLength; i++) {
|
|
2017
|
+
const category = categories[i];
|
|
2018
|
+
if (category.category_id !== category_id)
|
|
2019
|
+
continue;
|
|
2020
|
+
const allItems = [...category.items];
|
|
2021
|
+
let didFindItem = false;
|
|
2022
|
+
let itemExists = false;
|
|
2023
|
+
const totalItems = allItems.length;
|
|
2024
|
+
for (let j = 0; j < totalItems; j++) {
|
|
2025
|
+
const itm = allItems[j];
|
|
2026
|
+
if (itm._id === itemWithValidatedRewardClaim._id) {
|
|
2027
|
+
allItems[j] = itemWithValidatedRewardClaim;
|
|
2028
|
+
didFindItem = true;
|
|
2029
|
+
break;
|
|
2030
|
+
}
|
|
2031
|
+
if (itm.item_id === itemWithValidatedRewardClaim.item_id && !itm.is_split) {
|
|
2032
|
+
itemExists = true;
|
|
2033
|
+
}
|
|
2034
|
+
}
|
|
2035
|
+
if (!didFindItem && itemExists) {
|
|
2036
|
+
for (let j = 0; j < totalItems; j++) {
|
|
2037
|
+
const itm = allItems[j];
|
|
2038
|
+
if (itm.item_id === itemWithValidatedRewardClaim.item_id &&
|
|
2039
|
+
!itm.is_split &&
|
|
2040
|
+
// We make sure that the item does not have loyalty_discount_id and voucher_discount_id
|
|
2041
|
+
itm.item_discount_details?.loyalty_discount_id == null &&
|
|
2042
|
+
itm.item_discount_details?.voucher_discount_id == null &&
|
|
2043
|
+
isEqual(getComparableOrderItem(itm), comparableItem)) {
|
|
2044
|
+
mainItemId = replace_id ?? itm._id;
|
|
2045
|
+
const newQuantity = itemWithValidatedRewardClaim.quantity + itm.quantity;
|
|
2046
|
+
allItems[j] = {
|
|
2047
|
+
...itemWithValidatedRewardClaim,
|
|
2048
|
+
_id: mainItemId,
|
|
2049
|
+
quantity: newQuantity,
|
|
2050
|
+
item_discount_details: getItemQuantityChangeResults({
|
|
2051
|
+
prevQuantity: itemWithValidatedRewardClaim.quantity,
|
|
2052
|
+
newQuantity,
|
|
2053
|
+
prevItemDiscountDetails: itemWithValidatedRewardClaim.item_discount_details,
|
|
2054
|
+
}),
|
|
2055
|
+
};
|
|
2056
|
+
didFindItem = true;
|
|
2057
|
+
break;
|
|
2058
|
+
}
|
|
2059
|
+
}
|
|
2060
|
+
}
|
|
2061
|
+
if (!didFindItem) {
|
|
2062
|
+
allItems.push(itemWithValidatedRewardClaim);
|
|
2063
|
+
}
|
|
2064
|
+
categories[i] = {
|
|
2065
|
+
...category,
|
|
2066
|
+
items: allItems,
|
|
2067
|
+
};
|
|
2068
|
+
didFindCategory = true;
|
|
2069
|
+
break;
|
|
2070
|
+
}
|
|
2071
|
+
if (!didFindCategory) {
|
|
2072
|
+
categories.push({
|
|
2073
|
+
category_id,
|
|
2074
|
+
name,
|
|
2075
|
+
items: [item],
|
|
2076
|
+
});
|
|
2077
|
+
}
|
|
2078
|
+
// =====================><=====================
|
|
2079
|
+
if (chosen_course_id && orderData.dinein_details != null) {
|
|
2080
|
+
const course = courses.find((c) => c._id === chosen_course_id);
|
|
2081
|
+
if (course != null) {
|
|
2082
|
+
const originalCourses = orderData.dinein_details.courses ?? [];
|
|
2083
|
+
const originalCoursesLength = originalCourses.length;
|
|
2084
|
+
const dineinCourses = [];
|
|
2085
|
+
let isCourseFound = false;
|
|
2086
|
+
for (let i = 0; i < originalCoursesLength; i++) {
|
|
2087
|
+
const c = originalCourses[i];
|
|
2088
|
+
const dineinCourseItemIds = c.item_ids.filter((id) => id !== mainItemId);
|
|
2089
|
+
if (c.course_id === course._id) {
|
|
2090
|
+
dineinCourses.push({
|
|
2091
|
+
...c,
|
|
2092
|
+
item_ids: dineinCourseItemIds.concat(mainItemId),
|
|
2093
|
+
});
|
|
2094
|
+
isCourseFound = true;
|
|
2095
|
+
}
|
|
2096
|
+
else {
|
|
2097
|
+
dineinCourses.push({
|
|
2098
|
+
...c,
|
|
2099
|
+
item_ids: dineinCourseItemIds,
|
|
2100
|
+
});
|
|
2101
|
+
}
|
|
2102
|
+
}
|
|
2103
|
+
if (!isCourseFound) {
|
|
2104
|
+
dineinCourses.push({
|
|
2105
|
+
course_id: course._id,
|
|
2106
|
+
name: course.name,
|
|
2107
|
+
item_ids: [mainItemId],
|
|
2108
|
+
called: false,
|
|
2109
|
+
});
|
|
2110
|
+
}
|
|
2111
|
+
const dinein_details = {
|
|
2112
|
+
...orderData.dinein_details,
|
|
2113
|
+
courses: dineinCourses,
|
|
2114
|
+
};
|
|
2115
|
+
// This is not a mutation. We are just updating the orderData
|
|
2116
|
+
orderData.dinein_details = dinein_details;
|
|
2117
|
+
}
|
|
2118
|
+
}
|
|
2119
|
+
const iterations = [];
|
|
2120
|
+
if (orderData.iterations != null && orderData.iterations.length > 0) {
|
|
2121
|
+
const orderItemIdsMap = {};
|
|
2122
|
+
for (let i = 0; i < categories.length; i++) {
|
|
2123
|
+
const category = categories[i];
|
|
2124
|
+
const items = category.items;
|
|
2125
|
+
for (let j = 0; j < items.length; j++) {
|
|
2126
|
+
const item = items[j];
|
|
2127
|
+
orderItemIdsMap[item._id] = item.quantity;
|
|
2128
|
+
}
|
|
2129
|
+
}
|
|
2130
|
+
const [firstIteration, ...restIterations] = orderData.iterations;
|
|
2131
|
+
const firstIterationCategories = [];
|
|
2132
|
+
for (let i = 0; i < firstIteration.categories.length; i++) {
|
|
2133
|
+
const category = firstIteration.categories[i];
|
|
2134
|
+
const allPrevItems = [];
|
|
2135
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
2136
|
+
const prevItem = category.items[j];
|
|
2137
|
+
const currentItemQuantity = orderItemIdsMap[prevItem._id];
|
|
2138
|
+
const isRemovedItem = !currentItemQuantity || currentItemQuantity < prevItem.quantity;
|
|
2139
|
+
if (isRemovedItem) {
|
|
2140
|
+
allPrevItems.push(prevItem);
|
|
2141
|
+
}
|
|
2142
|
+
else {
|
|
2143
|
+
const updatedPrevItem = {
|
|
2144
|
+
...prevItem,
|
|
2145
|
+
};
|
|
2146
|
+
delete updatedPrevItem.void_reason;
|
|
2147
|
+
allPrevItems.push(updatedPrevItem);
|
|
2148
|
+
}
|
|
2149
|
+
}
|
|
2150
|
+
if (allPrevItems.length > 0) {
|
|
2151
|
+
firstIterationCategories.push({
|
|
2152
|
+
...category,
|
|
2153
|
+
items: allPrevItems,
|
|
2154
|
+
});
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
const firstIterationData = {
|
|
2158
|
+
...firstIteration,
|
|
2159
|
+
categories: firstIterationCategories,
|
|
2160
|
+
};
|
|
2161
|
+
iterations.push(firstIterationData);
|
|
2162
|
+
iterations.push(...restIterations);
|
|
2163
|
+
}
|
|
2164
|
+
// =====================><=====================
|
|
2165
|
+
const order = {
|
|
2166
|
+
...orderData,
|
|
2167
|
+
...(iterations.length > 0 && { iterations }),
|
|
2168
|
+
categories,
|
|
2169
|
+
};
|
|
2170
|
+
const orderWithSyncedRewardClaim = getOrderDataWithSyncedGiftCardRewardClaim({
|
|
2171
|
+
data: order,
|
|
2172
|
+
rewardClaim: validatedRewardClaim,
|
|
2173
|
+
});
|
|
2174
|
+
const orderWithSyncedItemRewardEvidence = getOrderDataWithSyncedItemRewardRedemptionEvidence({
|
|
2175
|
+
data: orderWithSyncedRewardClaim,
|
|
2176
|
+
itemDiscountDetails: itemWithValidatedRewardClaim.item_discount_details,
|
|
2177
|
+
rewardClaim: validatedRewardClaim,
|
|
2178
|
+
});
|
|
2179
|
+
const newData = calculateCartOrderInfoAndSyncData(orderWithSyncedItemRewardEvidence, courses, listnerSettings);
|
|
2180
|
+
if (appliedCustomDiscounts && appliedCustomDiscounts.length > 0) {
|
|
2181
|
+
return applyCustomDiscountCapsPostSync(newData, appliedCustomDiscounts, (orderWithSyncedItemRewardEvidence.platform ?? data.platform), courses, listnerSettings);
|
|
2182
|
+
}
|
|
2183
|
+
return newData;
|
|
2184
|
+
}
|
|
2185
|
+
export function getSplitCartItemToTwo({ data, orderItemId, courses, listnerSettings }) {
|
|
2186
|
+
if (data.categories == null)
|
|
2187
|
+
return data;
|
|
2188
|
+
const newCategories = [];
|
|
2189
|
+
for (let i = 0; i < data.categories.length; i++) {
|
|
2190
|
+
const category = data.categories[i];
|
|
2191
|
+
const newItems = [];
|
|
2192
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
2193
|
+
const item = {
|
|
2194
|
+
...category.items[j],
|
|
2195
|
+
};
|
|
2196
|
+
if (item._id !== orderItemId) {
|
|
2197
|
+
newItems.push(item);
|
|
2198
|
+
continue;
|
|
2199
|
+
}
|
|
2200
|
+
if (item.quantity === 1) {
|
|
2201
|
+
newItems.push(item);
|
|
2202
|
+
continue;
|
|
2203
|
+
}
|
|
2204
|
+
const prevQuantity = item.quantity;
|
|
2205
|
+
const newQuantity = item.quantity - 1;
|
|
2206
|
+
const prevItemDiscountDetails = getItemQuantityChangeResults({
|
|
2207
|
+
prevQuantity,
|
|
2208
|
+
newQuantity,
|
|
2209
|
+
prevItemDiscountDetails: item.item_discount_details,
|
|
2210
|
+
});
|
|
2211
|
+
const prevItem = {
|
|
2212
|
+
...item,
|
|
2213
|
+
quantity: newQuantity,
|
|
2214
|
+
item_discount_details: prevItemDiscountDetails,
|
|
2215
|
+
};
|
|
2216
|
+
newItems.push(prevItem);
|
|
2217
|
+
const _id = new ObjectID(Math.floor(new ObjectID(item._id).getTimestamp().getTime() / 1000) + 1).toHexString();
|
|
2218
|
+
const newItemsDiscountDetails = getItemQuantityChangeResults({
|
|
2219
|
+
prevQuantity,
|
|
2220
|
+
newQuantity: 1,
|
|
2221
|
+
prevItemDiscountDetails: item.item_discount_details,
|
|
2222
|
+
});
|
|
2223
|
+
newItems.push({
|
|
2224
|
+
...item,
|
|
2225
|
+
_id,
|
|
2226
|
+
quantity: 1,
|
|
2227
|
+
item_discount_details: newItemsDiscountDetails,
|
|
2228
|
+
});
|
|
2229
|
+
}
|
|
2230
|
+
newCategories.push({
|
|
2231
|
+
...category,
|
|
2232
|
+
items: newItems,
|
|
2233
|
+
});
|
|
2234
|
+
}
|
|
2235
|
+
const order = {
|
|
2236
|
+
...data,
|
|
2237
|
+
categories: newCategories,
|
|
2238
|
+
};
|
|
2239
|
+
const newData = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
|
|
2240
|
+
return newData;
|
|
2241
|
+
}
|
|
2242
|
+
/* ======================><====================== */
|
|
2243
|
+
export function getAddItemsToCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
|
|
2244
|
+
const newData = payload.reduce((acc, item) => getAddItemToCartData(acc, item, courses, listnerSettings, appliedCustomDiscounts), data);
|
|
2245
|
+
return newData;
|
|
2246
|
+
}
|
|
2247
|
+
/**
|
|
2248
|
+
* Post-sync cap enforcement for custom discounts.
|
|
2249
|
+
*
|
|
2250
|
+
* After `calculateCartOrderInfoAndSyncData` has computed correct `total_discount`
|
|
2251
|
+
* values (handling GST, modifiers, item totals, etc.), this function checks
|
|
2252
|
+
* whether any order-level or item-level percentage discount exceeds its configured
|
|
2253
|
+
* `max_amount` cap. If so, the discount is collapsed to a fixed amount equal to
|
|
2254
|
+
* the cap and the cart is re-synced.
|
|
2255
|
+
*
|
|
2256
|
+
* All manual subtotal / tax computation is eliminated — the sync result is the
|
|
2257
|
+
* single source of truth for the discountable base.
|
|
2258
|
+
*/
|
|
2259
|
+
function applyCustomDiscountCapsPostSync(synced, appliedCustomDiscounts, platform, courses, listnerSettings) {
|
|
2260
|
+
let needsResync = false;
|
|
2261
|
+
let result = synced;
|
|
2262
|
+
// ── Order-level cap check ──────────────────────────────────────────
|
|
2263
|
+
const orderDetails = result.order_discount_details;
|
|
2264
|
+
if (orderDetails?.special_discount_id) {
|
|
2265
|
+
const matched = appliedCustomDiscounts.find((d) => d._id === orderDetails.special_discount_id && d.applicability === DISCOUNT_APPLICABILITY_ENUM.ORDER_WIDE);
|
|
2266
|
+
if (matched?.is_percentage && matched.max_amount) {
|
|
2267
|
+
const maxCap = parseNum(matched.max_amount[platform] ?? matched.max_amount.BASE_PRICE);
|
|
2268
|
+
if (Number.isFinite(maxCap) && maxCap > 0) {
|
|
2269
|
+
// Cap breach: total_discount exceeds maxCap → switch to fixed
|
|
2270
|
+
if ((orderDetails.total_discount ?? 0) > maxCap) {
|
|
2271
|
+
result = {
|
|
2272
|
+
...result,
|
|
2273
|
+
order_discount_details: {
|
|
2274
|
+
...orderDetails,
|
|
2275
|
+
special_discount: maxCap,
|
|
2276
|
+
is_special_discount_percentage: false,
|
|
2277
|
+
total_discount: 0,
|
|
2278
|
+
},
|
|
2279
|
+
};
|
|
2280
|
+
needsResync = true;
|
|
2281
|
+
}
|
|
2282
|
+
// Un-cap: currently fixed (capped) but percentage would fit under cap → revert
|
|
2283
|
+
else if (orderDetails.is_special_discount_percentage === false && orderDetails.special_discount != null) {
|
|
2284
|
+
const discountPct = parseNum(matched.value[platform] ?? matched.value.BASE_PRICE);
|
|
2285
|
+
if (Number.isFinite(discountPct) && discountPct > 0 && discountPct <= 100) {
|
|
2286
|
+
const computedDiscount = (discountPct / 100) * (result.price ?? 0);
|
|
2287
|
+
if (computedDiscount <= maxCap) {
|
|
2288
|
+
result = {
|
|
2289
|
+
...result,
|
|
2290
|
+
order_discount_details: {
|
|
2291
|
+
...orderDetails,
|
|
2292
|
+
special_discount: discountPct,
|
|
2293
|
+
is_special_discount_percentage: true,
|
|
2294
|
+
total_discount: 0,
|
|
2295
|
+
},
|
|
2296
|
+
};
|
|
2297
|
+
needsResync = true;
|
|
2298
|
+
}
|
|
2299
|
+
// Re-cap: percentage still exceeds cap, but the current flat amount
|
|
2300
|
+
// is below cap (e.g. scaled down by a quantity change). Push it
|
|
2301
|
+
// back to maxCap.
|
|
2302
|
+
else if (computedDiscount > maxCap && orderDetails.special_discount < maxCap) {
|
|
2303
|
+
result = {
|
|
2304
|
+
...result,
|
|
2305
|
+
order_discount_details: {
|
|
2306
|
+
...orderDetails,
|
|
2307
|
+
special_discount: maxCap,
|
|
2308
|
+
is_special_discount_percentage: false,
|
|
2309
|
+
total_discount: 0,
|
|
2310
|
+
},
|
|
2311
|
+
};
|
|
2312
|
+
needsResync = true;
|
|
2313
|
+
}
|
|
2314
|
+
}
|
|
2315
|
+
}
|
|
2316
|
+
}
|
|
2317
|
+
}
|
|
2318
|
+
}
|
|
2319
|
+
// ── Item-level cap check ───────────────────────────────────────────
|
|
2320
|
+
const syncedCategories = result.categories;
|
|
2321
|
+
if (syncedCategories) {
|
|
2322
|
+
for (let ci = 0; ci < syncedCategories.length; ci++) {
|
|
2323
|
+
const category = syncedCategories[ci];
|
|
2324
|
+
let categoryChanged = false;
|
|
2325
|
+
const newItems = [];
|
|
2326
|
+
for (let ji = 0; ji < category.items.length; ji++) {
|
|
2327
|
+
const item = category.items[ji];
|
|
2328
|
+
const itemDetails = item.item_discount_details;
|
|
2329
|
+
if (!itemDetails?.special_discount_id) {
|
|
2330
|
+
newItems.push(item);
|
|
2331
|
+
continue;
|
|
2332
|
+
}
|
|
2333
|
+
const matched = appliedCustomDiscounts.find((d) => d._id === itemDetails.special_discount_id && d.applicability === DISCOUNT_APPLICABILITY_ENUM.ITEM_SPECIFIC);
|
|
2334
|
+
if (!matched?.is_percentage || !matched.max_amount) {
|
|
2335
|
+
newItems.push(item);
|
|
2336
|
+
continue;
|
|
2337
|
+
}
|
|
2338
|
+
const maxCap = parseNum(matched.max_amount[platform] ?? matched.max_amount.BASE_PRICE);
|
|
2339
|
+
if (!Number.isFinite(maxCap) || maxCap <= 0) {
|
|
2340
|
+
newItems.push(item);
|
|
2341
|
+
continue;
|
|
2342
|
+
}
|
|
2343
|
+
// Cap breach: total_discount exceeds maxCap → switch to fixed
|
|
2344
|
+
if ((itemDetails.total_discount ?? 0) > maxCap) {
|
|
2345
|
+
newItems.push({
|
|
2346
|
+
...item,
|
|
2347
|
+
item_discount_details: {
|
|
2348
|
+
...itemDetails,
|
|
2349
|
+
special_discount: maxCap,
|
|
2350
|
+
is_special_discount_percentage: false,
|
|
2351
|
+
total_discount: 0,
|
|
2352
|
+
},
|
|
2353
|
+
});
|
|
2354
|
+
needsResync = true;
|
|
2355
|
+
categoryChanged = true;
|
|
2356
|
+
continue;
|
|
2357
|
+
}
|
|
2358
|
+
// Un-cap: currently fixed (capped) but percentage would fit under cap → revert
|
|
2359
|
+
if (itemDetails.is_special_discount_percentage === false && itemDetails.special_discount != null) {
|
|
2360
|
+
const discountPct = parseNum(matched.value[platform] ?? matched.value.BASE_PRICE);
|
|
2361
|
+
if (Number.isFinite(discountPct) && discountPct > 0 && discountPct <= 100) {
|
|
2362
|
+
const computedDiscount = (discountPct / 100) * (item.subtotal ?? 0);
|
|
2363
|
+
if (computedDiscount <= maxCap) {
|
|
2364
|
+
newItems.push({
|
|
2365
|
+
...item,
|
|
2366
|
+
item_discount_details: {
|
|
2367
|
+
...itemDetails,
|
|
2368
|
+
special_discount: discountPct,
|
|
2369
|
+
is_special_discount_percentage: true,
|
|
2370
|
+
total_discount: 0,
|
|
2371
|
+
},
|
|
2372
|
+
});
|
|
2373
|
+
needsResync = true;
|
|
2374
|
+
categoryChanged = true;
|
|
2375
|
+
continue;
|
|
2376
|
+
}
|
|
2377
|
+
// Re-cap: percentage still exceeds cap, but the current flat amount
|
|
2378
|
+
// is below cap (e.g. scaled down proportionally by a quantity
|
|
2379
|
+
// decrease). Push it back to maxCap so the discount stays at the
|
|
2380
|
+
// configured limit instead of an incorrect scaled-down value.
|
|
2381
|
+
if (computedDiscount > maxCap && itemDetails.special_discount < maxCap) {
|
|
2382
|
+
newItems.push({
|
|
2383
|
+
...item,
|
|
2384
|
+
item_discount_details: {
|
|
2385
|
+
...itemDetails,
|
|
2386
|
+
special_discount: maxCap,
|
|
2387
|
+
is_special_discount_percentage: false,
|
|
2388
|
+
total_discount: 0,
|
|
2389
|
+
},
|
|
2390
|
+
});
|
|
2391
|
+
needsResync = true;
|
|
2392
|
+
categoryChanged = true;
|
|
2393
|
+
continue;
|
|
2394
|
+
}
|
|
2395
|
+
}
|
|
2396
|
+
}
|
|
2397
|
+
newItems.push(item);
|
|
2398
|
+
}
|
|
2399
|
+
if (categoryChanged) {
|
|
2400
|
+
result = {
|
|
2401
|
+
...result,
|
|
2402
|
+
categories: result.categories.map((c, idx) => (idx === ci ? { ...c, items: newItems } : c)),
|
|
2403
|
+
};
|
|
2404
|
+
}
|
|
2405
|
+
}
|
|
2406
|
+
}
|
|
2407
|
+
return needsResync ? calculateCartOrderInfoAndSyncData(result, courses, listnerSettings) : result;
|
|
2408
|
+
}
|
|
2409
|
+
export function getUpdateItemInCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
|
|
2410
|
+
const orderItemIdsMap = {};
|
|
2411
|
+
let categories;
|
|
2412
|
+
if (data.categories != null) {
|
|
2413
|
+
categories = [];
|
|
2414
|
+
for (let i = 0; i < data.categories.length; i++) {
|
|
2415
|
+
const category = data.categories[i];
|
|
2416
|
+
const items = [];
|
|
2417
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
2418
|
+
const item = category.items[j];
|
|
2419
|
+
let newItem = item;
|
|
2420
|
+
if (item._id === payload._id) {
|
|
2421
|
+
newItem = {
|
|
2422
|
+
...item,
|
|
2423
|
+
...payload,
|
|
2424
|
+
};
|
|
2425
|
+
}
|
|
2426
|
+
if (newItem.quantity > 0) {
|
|
2427
|
+
items.push(newItem);
|
|
2428
|
+
orderItemIdsMap[newItem._id] = newItem.quantity;
|
|
2429
|
+
}
|
|
2430
|
+
}
|
|
2431
|
+
if (items.length > 0) {
|
|
2432
|
+
categories.push({
|
|
2433
|
+
...category,
|
|
2434
|
+
items,
|
|
2435
|
+
});
|
|
2436
|
+
}
|
|
2437
|
+
}
|
|
2438
|
+
}
|
|
2439
|
+
const iterations = [];
|
|
2440
|
+
if (payload.quantity != null && data.iterations != null && data.iterations.length > 0) {
|
|
2441
|
+
const [firstIteration, ...restIterations] = data.iterations;
|
|
2442
|
+
const firstIterationCategories = [];
|
|
2443
|
+
for (let i = 0; i < firstIteration.categories.length; i++) {
|
|
2444
|
+
const category = firstIteration.categories[i];
|
|
2445
|
+
const allPrevItems = [];
|
|
2446
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
2447
|
+
const prevItem = category.items[j];
|
|
2448
|
+
if (prevItem._id === payload._id && payload.void_reason != null) {
|
|
2449
|
+
const updatedPrevItem = {
|
|
2450
|
+
...prevItem,
|
|
2451
|
+
void_reason: payload.void_reason,
|
|
2452
|
+
};
|
|
2453
|
+
if (payload.is_ready != null) {
|
|
2454
|
+
updatedPrevItem.is_ready = payload.is_ready;
|
|
2455
|
+
}
|
|
2456
|
+
allPrevItems.push(updatedPrevItem);
|
|
2457
|
+
}
|
|
2458
|
+
else {
|
|
2459
|
+
// A Removed Item is one that is not in the orderItemIdsMap
|
|
2460
|
+
// Or it is in the orderItemIdsMap but its quantity is less than newItem.quantity
|
|
2461
|
+
const currentItemQuantity = orderItemIdsMap[prevItem._id];
|
|
2462
|
+
const isRemovedItem = !currentItemQuantity || currentItemQuantity < prevItem.quantity;
|
|
2463
|
+
if (isRemovedItem) {
|
|
2464
|
+
allPrevItems.push(prevItem);
|
|
2465
|
+
}
|
|
2466
|
+
else {
|
|
2467
|
+
// If the item is not removed, then we make sure to remove the void_reason
|
|
2468
|
+
const updatedPrevItem = {
|
|
2469
|
+
...prevItem,
|
|
2470
|
+
};
|
|
2471
|
+
delete updatedPrevItem.void_reason;
|
|
2472
|
+
allPrevItems.push(updatedPrevItem);
|
|
2473
|
+
}
|
|
2474
|
+
}
|
|
2475
|
+
}
|
|
2476
|
+
if (allPrevItems.length > 0) {
|
|
2477
|
+
firstIterationCategories.push({
|
|
2478
|
+
...category,
|
|
2479
|
+
items: allPrevItems,
|
|
2480
|
+
});
|
|
2481
|
+
}
|
|
2482
|
+
}
|
|
2483
|
+
const firstIterationData = {
|
|
2484
|
+
...firstIteration,
|
|
2485
|
+
categories: firstIterationCategories,
|
|
2486
|
+
};
|
|
2487
|
+
iterations.push(firstIterationData);
|
|
2488
|
+
iterations.push(...restIterations);
|
|
2489
|
+
}
|
|
2490
|
+
const order = {
|
|
2491
|
+
...data,
|
|
2492
|
+
...(iterations.length > 0 && { iterations }),
|
|
2493
|
+
categories,
|
|
2494
|
+
};
|
|
2495
|
+
const synced = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
|
|
2496
|
+
if (appliedCustomDiscounts && appliedCustomDiscounts.length > 0) {
|
|
2497
|
+
return applyCustomDiscountCapsPostSync(synced, appliedCustomDiscounts, data.platform, courses, listnerSettings);
|
|
2498
|
+
}
|
|
2499
|
+
return synced;
|
|
2500
|
+
}
|
|
2501
|
+
/* ======================><====================== */
|
|
2502
|
+
export function getUpdateItemsInCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
|
|
2503
|
+
const newData = payload.reduce((acc, item) => getUpdateItemInCartData(acc, item, courses, listnerSettings, appliedCustomDiscounts), data);
|
|
2504
|
+
return newData;
|
|
2505
|
+
}
|
|
2506
|
+
/* ======================><====================== */
|
|
2507
|
+
export function getUpdateCartInfoData(data, payload, courses, listnerSettings, syncContext) {
|
|
2508
|
+
const order = {
|
|
2509
|
+
...data,
|
|
2510
|
+
...payload,
|
|
2511
|
+
};
|
|
2512
|
+
const newData = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings, undefined, syncContext);
|
|
2513
|
+
if (newData.redeemed_offers != null) {
|
|
2514
|
+
return newData;
|
|
2515
|
+
}
|
|
2516
|
+
const categories = newData.categories ?? [];
|
|
2517
|
+
let hasItemsInCart = false;
|
|
2518
|
+
outer: for (let i = 0; i < categories.length; i++) {
|
|
2519
|
+
const items = categories[i].items ?? [];
|
|
2520
|
+
for (let j = 0; j < items.length; j++) {
|
|
2521
|
+
const quantity = Number(items[j].quantity ?? 0);
|
|
2522
|
+
if (Number.isFinite(quantity) && quantity > 0) {
|
|
2523
|
+
hasItemsInCart = true;
|
|
2524
|
+
break outer;
|
|
2525
|
+
}
|
|
2526
|
+
}
|
|
2527
|
+
}
|
|
2528
|
+
if (!hasItemsInCart) {
|
|
2529
|
+
return newData;
|
|
2530
|
+
}
|
|
2531
|
+
const hasRedeemedOffersInPayload = Object.prototype.hasOwnProperty.call(payload, 'redeemed_offers');
|
|
2532
|
+
// Respect explicit clear requests.
|
|
2533
|
+
if (hasRedeemedOffersInPayload && payload.redeemed_offers == null) {
|
|
2534
|
+
return newData;
|
|
2535
|
+
}
|
|
2536
|
+
const fallbackRedeemedOffers = getRedeemedOffersMap(hasRedeemedOffersInPayload ? payload.redeemed_offers : data.redeemed_offers);
|
|
2537
|
+
if (fallbackRedeemedOffers == null) {
|
|
2538
|
+
return newData;
|
|
2539
|
+
}
|
|
2540
|
+
return {
|
|
2541
|
+
...newData,
|
|
2542
|
+
redeemed_offers: fallbackRedeemedOffers,
|
|
2543
|
+
};
|
|
2544
|
+
}
|
|
2545
|
+
/* ======================><====================== */
|
|
2546
|
+
export function getRemoveItemFromCartData(data, payload, courses, listnerSettings, appliedCustomDiscounts) {
|
|
2547
|
+
const orderItemIdsMap = {};
|
|
2548
|
+
let categories;
|
|
2549
|
+
if (data.categories != null) {
|
|
2550
|
+
categories = [];
|
|
2551
|
+
for (let i = 0; i < data.categories.length; i++) {
|
|
2552
|
+
const category = data.categories[i];
|
|
2553
|
+
const items = [];
|
|
2554
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
2555
|
+
const item = category.items[j];
|
|
2556
|
+
if (item._id !== payload._id) {
|
|
2557
|
+
items.push(item);
|
|
2558
|
+
orderItemIdsMap[item._id] = item.quantity;
|
|
2559
|
+
}
|
|
2560
|
+
}
|
|
2561
|
+
if (items.length > 0) {
|
|
2562
|
+
categories.push({
|
|
2563
|
+
...category,
|
|
2564
|
+
items,
|
|
2565
|
+
});
|
|
2566
|
+
}
|
|
2567
|
+
}
|
|
2568
|
+
}
|
|
2569
|
+
const iterations = [];
|
|
2570
|
+
if (data.iterations != null && data.iterations.length > 0) {
|
|
2571
|
+
const [firstIteration, ...restIterations] = data.iterations;
|
|
2572
|
+
const firstIterationCategories = [];
|
|
2573
|
+
for (let i = 0; i < firstIteration.categories.length; i++) {
|
|
2574
|
+
const category = firstIteration.categories[i];
|
|
2575
|
+
const allPrevItems = [];
|
|
2576
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
2577
|
+
const prevItem = category.items[j];
|
|
2578
|
+
if (prevItem._id === payload._id && payload.void_reason != null) {
|
|
2579
|
+
const newItem = {
|
|
2580
|
+
...prevItem,
|
|
2581
|
+
void_reason: payload.void_reason,
|
|
2582
|
+
};
|
|
2583
|
+
if (payload.is_ready != null) {
|
|
2584
|
+
newItem.is_ready = payload.is_ready;
|
|
2585
|
+
}
|
|
2586
|
+
allPrevItems.push(newItem);
|
|
2587
|
+
}
|
|
2588
|
+
else {
|
|
2589
|
+
// A Removed Item is one that is not in the orderItemIdsMap
|
|
2590
|
+
// Or it is in the orderItemIdsMap but its quantity is less than newItem.quantity
|
|
2591
|
+
const currentItemQuantity = orderItemIdsMap[prevItem._id];
|
|
2592
|
+
const isRemovedItem = !currentItemQuantity || currentItemQuantity < prevItem.quantity;
|
|
2593
|
+
if (isRemovedItem) {
|
|
2594
|
+
allPrevItems.push(prevItem);
|
|
2595
|
+
}
|
|
2596
|
+
else {
|
|
2597
|
+
// If the item is not removed, then we make sure to remove the void_reason
|
|
2598
|
+
const updatedPrevItem = {
|
|
2599
|
+
...prevItem,
|
|
2600
|
+
};
|
|
2601
|
+
delete updatedPrevItem.void_reason;
|
|
2602
|
+
allPrevItems.push(updatedPrevItem);
|
|
2603
|
+
}
|
|
2604
|
+
}
|
|
2605
|
+
}
|
|
2606
|
+
if (allPrevItems.length > 0) {
|
|
2607
|
+
firstIterationCategories.push({
|
|
2608
|
+
...category,
|
|
2609
|
+
items: allPrevItems,
|
|
2610
|
+
});
|
|
2611
|
+
}
|
|
2612
|
+
}
|
|
2613
|
+
const firstIterationData = {
|
|
2614
|
+
...firstIteration,
|
|
2615
|
+
categories: firstIterationCategories,
|
|
2616
|
+
};
|
|
2617
|
+
iterations.push(firstIterationData);
|
|
2618
|
+
iterations.push(...restIterations);
|
|
2619
|
+
}
|
|
2620
|
+
const order = {
|
|
2621
|
+
...data,
|
|
2622
|
+
...(iterations.length > 0 && { iterations }),
|
|
2623
|
+
categories,
|
|
2624
|
+
};
|
|
2625
|
+
const synced = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
|
|
2626
|
+
if (appliedCustomDiscounts && appliedCustomDiscounts.length > 0) {
|
|
2627
|
+
return applyCustomDiscountCapsPostSync(synced, appliedCustomDiscounts, data.platform, courses, listnerSettings);
|
|
2628
|
+
}
|
|
2629
|
+
return synced;
|
|
2630
|
+
}
|
|
2631
|
+
/* ======================>Order Object<====================== */
|
|
2632
|
+
export function getUpdatedCartInfoOnInit(data, order, courses, platform, categories, mappedItems, tenantId, listnerSettings, offers) {
|
|
2633
|
+
let payload = order;
|
|
2634
|
+
if (platform && data.platform !== order.platform && categories) {
|
|
2635
|
+
const newCategories = [];
|
|
2636
|
+
for (let i = 0; i < categories.length; i++) {
|
|
2637
|
+
const cat = categories[i];
|
|
2638
|
+
const newItems = [];
|
|
2639
|
+
for (let j = 0; j < cat.items.length; j++) {
|
|
2640
|
+
const item = cat.items[j];
|
|
2641
|
+
let catalogItem = mappedItems[item.item_id];
|
|
2642
|
+
if (catalogItem == null)
|
|
2643
|
+
continue;
|
|
2644
|
+
catalogItem = convertItemToWorkableItem(catalogItem, platform, tenantId);
|
|
2645
|
+
if (catalogItem == null)
|
|
2646
|
+
continue;
|
|
2647
|
+
let newItem = item;
|
|
2648
|
+
// we get the price object from the catalogs
|
|
2649
|
+
const priceObj = catalogItem.price;
|
|
2650
|
+
const price = getPriceEnum(priceObj, platform);
|
|
2651
|
+
// we get the discount object from the catalogs
|
|
2652
|
+
const discountObj = catalogItem.discount_amount;
|
|
2653
|
+
const discount_amount = getPriceEnum(discountObj, platform);
|
|
2654
|
+
const is_discount_percentage = catalogItem.is_discount_percentage;
|
|
2655
|
+
// we get the tax object from the catalogs
|
|
2656
|
+
const taxObj = catalogItem.tax_rate;
|
|
2657
|
+
const tax_rate = getPriceEnum(taxObj, platform);
|
|
2658
|
+
let modifiers = item.modifiers;
|
|
2659
|
+
if (item.modifiers != null && item.modifiers.length > 0) {
|
|
2660
|
+
const recursivelyUpdateModifiers = (modifiers) => {
|
|
2661
|
+
const newModifiers = [];
|
|
2662
|
+
for (let k = 0; k < modifiers.length; k++) {
|
|
2663
|
+
const orderItemModifier = modifiers[k];
|
|
2664
|
+
const catalogItemModifier = getModifier(orderItemModifier._id, catalogItem?.modifiers);
|
|
2665
|
+
if (catalogItemModifier != null) {
|
|
2666
|
+
const newOptions = [];
|
|
2667
|
+
for (let l = 0; l < orderItemModifier.options.length; l++) {
|
|
2668
|
+
const orderItemOption = orderItemModifier.options[l];
|
|
2669
|
+
let catalogModifierOption = catalogItemModifier.options.find((o) => o._id === orderItemOption._id);
|
|
2670
|
+
if (catalogModifierOption == null) {
|
|
2671
|
+
continue;
|
|
2672
|
+
}
|
|
2673
|
+
catalogModifierOption = convertOptionToWorkableOption(catalogModifierOption, platform);
|
|
2674
|
+
if (catalogModifierOption == null)
|
|
2675
|
+
continue;
|
|
2676
|
+
const optionPrice = getPriceEnum(catalogModifierOption.price, platform);
|
|
2677
|
+
// =====================><=====================
|
|
2678
|
+
let optionModifiers = orderItemOption.modifiers;
|
|
2679
|
+
if (orderItemOption.modifiers != null && orderItemOption.modifiers.length > 0) {
|
|
2680
|
+
optionModifiers = recursivelyUpdateModifiers(orderItemOption.modifiers);
|
|
2681
|
+
}
|
|
2682
|
+
newOptions.push({
|
|
2683
|
+
...orderItemOption,
|
|
2684
|
+
price: optionPrice,
|
|
2685
|
+
modifiers: optionModifiers,
|
|
2686
|
+
});
|
|
2687
|
+
}
|
|
2688
|
+
if (newOptions.length > 0) {
|
|
2689
|
+
const newModifier = {
|
|
2690
|
+
...orderItemModifier,
|
|
2691
|
+
options: newOptions,
|
|
2692
|
+
};
|
|
2693
|
+
newModifiers.push(newModifier);
|
|
2694
|
+
}
|
|
2695
|
+
}
|
|
2696
|
+
}
|
|
2697
|
+
return newModifiers;
|
|
2698
|
+
};
|
|
2699
|
+
modifiers = recursivelyUpdateModifiers(item.modifiers);
|
|
2700
|
+
}
|
|
2701
|
+
newItem = {
|
|
2702
|
+
...item,
|
|
2703
|
+
price,
|
|
2704
|
+
tax_rate,
|
|
2705
|
+
modifiers,
|
|
2706
|
+
item_discount_details: discountObj != null || item.item_discount_details != null
|
|
2707
|
+
? getItemQuantityChangeResults({
|
|
2708
|
+
prevQuantity: 1,
|
|
2709
|
+
newQuantity: item.quantity,
|
|
2710
|
+
prevItemDiscountDetails: {
|
|
2711
|
+
...item.item_discount_details,
|
|
2712
|
+
special_discount: discount_amount,
|
|
2713
|
+
is_special_discount_percentage: is_discount_percentage,
|
|
2714
|
+
total_discount: discount_amount,
|
|
2715
|
+
},
|
|
2716
|
+
})
|
|
2717
|
+
: null,
|
|
2718
|
+
};
|
|
2719
|
+
newItems.push(newItem);
|
|
2720
|
+
}
|
|
2721
|
+
if (newItems.length === 0)
|
|
2722
|
+
continue;
|
|
2723
|
+
newCategories.push({
|
|
2724
|
+
...cat,
|
|
2725
|
+
items: newItems,
|
|
2726
|
+
});
|
|
2727
|
+
}
|
|
2728
|
+
payload = {
|
|
2729
|
+
...payload,
|
|
2730
|
+
categories: newCategories,
|
|
2731
|
+
};
|
|
2732
|
+
}
|
|
2733
|
+
const orderData = getUpdateCartInfoData(data, payload, courses, listnerSettings);
|
|
2734
|
+
if (platform == null || data.platform === order.platform || offers == null || offers.length === 0) {
|
|
2735
|
+
return orderData;
|
|
2736
|
+
}
|
|
2737
|
+
return pruneInvalidRedeemedOffersForPlatform(orderData, courses, listnerSettings, offers, platform);
|
|
2738
|
+
}
|
|
2739
|
+
/* =================================><================================= */
|
|
2740
|
+
/**
|
|
2741
|
+
* Drop entries from `redeemed_offers` (and any related discount/wallet state)
|
|
2742
|
+
* for offers whose `included_platforms` does not contain the supplied platform.
|
|
2743
|
+
*
|
|
2744
|
+
* Use this when the platform changes outside of `getUpdatedCartInfoOnInit` (for
|
|
2745
|
+
* example, on platform switch in a long POS cashier session) to keep
|
|
2746
|
+
* platform-incompatible rewards from being silently preserved.
|
|
2747
|
+
*/
|
|
2748
|
+
export function pruneInvalidRedeemedOffersForPlatform(data, courses, listnerSettings, offers, platform) {
|
|
2749
|
+
if (platform == null || offers == null || offers.length === 0) {
|
|
2750
|
+
return data;
|
|
2751
|
+
}
|
|
2752
|
+
const redeemedOfferIds = Object.keys(data.redeemed_offers ?? {});
|
|
2753
|
+
if (redeemedOfferIds.length === 0) {
|
|
2754
|
+
return data;
|
|
2755
|
+
}
|
|
2756
|
+
const offersMap = new Map();
|
|
2757
|
+
for (let i = 0; i < offers.length; i++) {
|
|
2758
|
+
offersMap.set(offers[i]._id, offers[i]);
|
|
2759
|
+
}
|
|
2760
|
+
const invalidRedeemedOfferIds = [];
|
|
2761
|
+
for (let i = 0; i < redeemedOfferIds.length; i++) {
|
|
2762
|
+
const redeemedOfferId = redeemedOfferIds[i];
|
|
2763
|
+
const offer = offersMap.get(redeemedOfferId);
|
|
2764
|
+
if (offer == null || !offer.included_platforms.includes(platform)) {
|
|
2765
|
+
invalidRedeemedOfferIds.push(redeemedOfferId);
|
|
2766
|
+
}
|
|
2767
|
+
}
|
|
2768
|
+
if (invalidRedeemedOfferIds.length === 0) {
|
|
2769
|
+
return data;
|
|
2770
|
+
}
|
|
2771
|
+
return getRemoveAllOffersFromCartData(data, courses, listnerSettings, invalidRedeemedOfferIds);
|
|
2772
|
+
}
|
|
2773
|
+
export function getChangeCourseForItemData(data, itemId, new_course_id, courses, listnerSettings) {
|
|
2774
|
+
if (data.dinein_details == null)
|
|
2775
|
+
return data;
|
|
2776
|
+
// case 1: The course_id needs to be within courses
|
|
2777
|
+
const course = courses.find((c) => c._id === new_course_id);
|
|
2778
|
+
// if we can't find the course, then we return the data
|
|
2779
|
+
if (course == null)
|
|
2780
|
+
return data;
|
|
2781
|
+
// we make a shallow copy of the dinein_courses
|
|
2782
|
+
let dineinCourses = [...(data.dinein_details.courses ?? [])];
|
|
2783
|
+
// if the course is in the dineinCourses
|
|
2784
|
+
// we need to find the course that the item is in
|
|
2785
|
+
const foundCourse = dineinCourses.find((c) => c.item_ids.includes(itemId));
|
|
2786
|
+
// if the course is found and it has the same course_id as the new_course_id
|
|
2787
|
+
// then we don't need to do anything
|
|
2788
|
+
// so we return the data
|
|
2789
|
+
if (foundCourse?.course_id === new_course_id)
|
|
2790
|
+
return data;
|
|
2791
|
+
if (foundCourse != null) {
|
|
2792
|
+
// if the course is found then we remove the item from the course
|
|
2793
|
+
dineinCourses = dineinCourses.map((c) => c.course_id === foundCourse.course_id ? { ...c, item_ids: c.item_ids.filter((id) => id !== itemId) } : c);
|
|
2794
|
+
}
|
|
2795
|
+
// if the course is not in the dineinCourses, then we add it
|
|
2796
|
+
if (!dineinCourses.some((c) => c.course_id === course._id)) {
|
|
2797
|
+
dineinCourses.push({
|
|
2798
|
+
course_id: course._id,
|
|
2799
|
+
name: course.name,
|
|
2800
|
+
item_ids: [itemId],
|
|
2801
|
+
called: false,
|
|
2802
|
+
});
|
|
2803
|
+
}
|
|
2804
|
+
else {
|
|
2805
|
+
// we add the item to the new course
|
|
2806
|
+
dineinCourses = dineinCourses.map((c) => c.course_id === new_course_id ? { ...c, item_ids: c.item_ids.concat(itemId) } : c);
|
|
2807
|
+
}
|
|
2808
|
+
const newOrderData = {
|
|
2809
|
+
...data,
|
|
2810
|
+
dinein_details: {
|
|
2811
|
+
...data.dinein_details,
|
|
2812
|
+
courses: dineinCourses,
|
|
2813
|
+
},
|
|
2814
|
+
};
|
|
2815
|
+
const syncedData = calculateCartOrderInfoAndSyncData(newOrderData, courses, listnerSettings);
|
|
2816
|
+
return syncedData;
|
|
2817
|
+
}
|
|
2818
|
+
/* ================================><================================= */
|
|
2819
|
+
export function getChangeCourseStatusData(data, course_id, called, courses, listnerSettings) {
|
|
2820
|
+
const order = {
|
|
2821
|
+
...data,
|
|
2822
|
+
dinein_details: data.dinein_details
|
|
2823
|
+
? {
|
|
2824
|
+
...data.dinein_details,
|
|
2825
|
+
courses: data.dinein_details?.courses?.map((course) => course.course_id === course_id ? { ...course, called } : course) ?? null,
|
|
2826
|
+
}
|
|
2827
|
+
: null,
|
|
2828
|
+
};
|
|
2829
|
+
const newData = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
|
|
2830
|
+
return newData;
|
|
2831
|
+
}
|
|
2832
|
+
export function getCartItemsObj({ currentIterationCategories, currentDineinCourses, previousIterationCategories, previousDineinCourses, mappedItems, shouldSortItemsViaId = true, platform, isAnEdit, sortType, }) {
|
|
2833
|
+
const isDineinOrder = PLATFORMS_BY_ORDER_TYPE[ORDER_TYPE_ENUM.DINE_IN].includes(platform);
|
|
2834
|
+
const newMappedItems = { ...mappedItems };
|
|
2835
|
+
if (Object.keys(newMappedItems).length === 0) {
|
|
2836
|
+
previousIterationCategories?.forEach((category) => {
|
|
2837
|
+
category.items.forEach((item) => {
|
|
2838
|
+
if (newMappedItems[item.item_id] == null) {
|
|
2839
|
+
newMappedItems[item.item_id] = {
|
|
2840
|
+
_id: item.item_id,
|
|
2841
|
+
type: item.item_type,
|
|
2842
|
+
name: item.name,
|
|
2843
|
+
category_id: category.category_id,
|
|
2844
|
+
category_name: category.name,
|
|
2845
|
+
tenants: [],
|
|
2846
|
+
category_image_url: '',
|
|
2847
|
+
image_url: '',
|
|
2848
|
+
description: '',
|
|
2849
|
+
};
|
|
2850
|
+
}
|
|
2851
|
+
});
|
|
2852
|
+
});
|
|
2853
|
+
currentIterationCategories?.forEach((category) => {
|
|
2854
|
+
category.items.forEach((item) => {
|
|
2855
|
+
if (newMappedItems[item.item_id] == null) {
|
|
2856
|
+
newMappedItems[item.item_id] = {
|
|
2857
|
+
_id: item.item_id,
|
|
2858
|
+
type: item.item_type,
|
|
2859
|
+
name: item.name,
|
|
2860
|
+
category_id: category.category_id,
|
|
2861
|
+
category_name: category.name,
|
|
2862
|
+
tenants: [],
|
|
2863
|
+
category_image_url: '',
|
|
2864
|
+
image_url: '',
|
|
2865
|
+
description: '',
|
|
2866
|
+
};
|
|
2867
|
+
}
|
|
2868
|
+
});
|
|
2869
|
+
});
|
|
2870
|
+
}
|
|
2871
|
+
// =====================><=====================
|
|
2872
|
+
const getCartItems = (items) => {
|
|
2873
|
+
const orderItems = items.map((item) => {
|
|
2874
|
+
const selectedItem = newMappedItems[item.item_id] ?? {};
|
|
2875
|
+
return {
|
|
2876
|
+
orderItem: item,
|
|
2877
|
+
image_url: selectedItem.image_url ?? '',
|
|
2878
|
+
description: selectedItem.description ?? '',
|
|
2879
|
+
category_name: selectedItem.category_name ?? '',
|
|
2880
|
+
category_id: selectedItem.category_id ?? '',
|
|
2881
|
+
};
|
|
2882
|
+
});
|
|
2883
|
+
if (shouldSortItemsViaId) {
|
|
2884
|
+
if (sortType === SORT_TYPE_ENUM.DESC) {
|
|
2885
|
+
orderItems.sort((a, b) => ObjectID(b.orderItem._id).getTimestamp().getTime() - ObjectID(a.orderItem._id).getTimestamp().getTime());
|
|
2886
|
+
}
|
|
2887
|
+
else {
|
|
2888
|
+
orderItems.sort((a, b) => ObjectID(a.orderItem._id).getTimestamp().getTime() - ObjectID(b.orderItem._id).getTimestamp().getTime());
|
|
2889
|
+
}
|
|
2890
|
+
}
|
|
2891
|
+
return orderItems;
|
|
2892
|
+
};
|
|
2893
|
+
// =====================><=====================
|
|
2894
|
+
/*
|
|
2895
|
+
If the order has iterations in it, then we get the last iteration.
|
|
2896
|
+
We would compare the changes in the last iteration with the current iteration
|
|
2897
|
+
and show the changes in the cart.
|
|
2898
|
+
*/
|
|
2899
|
+
let totalItemsInCart = 0;
|
|
2900
|
+
const currentItems = [];
|
|
2901
|
+
if (currentIterationCategories != null) {
|
|
2902
|
+
for (let i = 0; i < currentIterationCategories.length; i++) {
|
|
2903
|
+
const category = currentIterationCategories[i];
|
|
2904
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
2905
|
+
const item = { ...category.items[j] };
|
|
2906
|
+
currentItems.push(item);
|
|
2907
|
+
totalItemsInCart += item.quantity;
|
|
2908
|
+
}
|
|
2909
|
+
}
|
|
2910
|
+
}
|
|
2911
|
+
let unChangedItems = currentItems;
|
|
2912
|
+
const editedItems = [];
|
|
2913
|
+
let removedItems = [];
|
|
2914
|
+
let newItems = [];
|
|
2915
|
+
let orderCourses = [];
|
|
2916
|
+
let disabledReason = '';
|
|
2917
|
+
// If previous iteration exists, then it's an update to the order.
|
|
2918
|
+
// In that case we would keep the previous iteration as it is and
|
|
2919
|
+
// compare the current iteration with the previous iteration.
|
|
2920
|
+
// If the current iteration is different from the previous iteration in any way
|
|
2921
|
+
// then we would update the current iteration with only those items that are different
|
|
2922
|
+
const isAnUpdate = isAnEdit ?? (previousIterationCategories != null && previousIterationCategories.length > 0);
|
|
2923
|
+
if (isAnUpdate) {
|
|
2924
|
+
// we need to compare the current iteration with the previous iteration
|
|
2925
|
+
const currentItemsMap = currentItems.reduce((acc, currentItem) => {
|
|
2926
|
+
acc[currentItem._id] = currentItem;
|
|
2927
|
+
return acc;
|
|
2928
|
+
}, {});
|
|
2929
|
+
const prevItems = [];
|
|
2930
|
+
const prevItemsMap = {};
|
|
2931
|
+
const prevCategories = previousIterationCategories ?? [];
|
|
2932
|
+
for (let i = 0; i < prevCategories.length; i++) {
|
|
2933
|
+
const category = prevCategories[i];
|
|
2934
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
2935
|
+
const prevItem = category.items[j];
|
|
2936
|
+
let item = prevItem;
|
|
2937
|
+
const currentItem = currentItemsMap[prevItem._id];
|
|
2938
|
+
if (currentItem != null && currentItem.is_ready != null) {
|
|
2939
|
+
item = {
|
|
2940
|
+
...item,
|
|
2941
|
+
is_ready: currentItem.is_ready,
|
|
2942
|
+
};
|
|
2943
|
+
}
|
|
2944
|
+
prevItems.push(item);
|
|
2945
|
+
prevItemsMap[item._id] = item;
|
|
2946
|
+
}
|
|
2947
|
+
}
|
|
2948
|
+
// NOTE: We will always set the items from currentItemsMap because we are dealing with current iteration
|
|
2949
|
+
unChangedItems = [];
|
|
2950
|
+
let totalTempRemovedItems = 0;
|
|
2951
|
+
for (let i = 0; i < prevItems.length; i++) {
|
|
2952
|
+
const prevItem = {
|
|
2953
|
+
...prevItems[i],
|
|
2954
|
+
};
|
|
2955
|
+
const currentItem = {
|
|
2956
|
+
...currentItemsMap[prevItem._id],
|
|
2957
|
+
};
|
|
2958
|
+
// Step 1: Get the items that are not present in the current iteration
|
|
2959
|
+
if (currentItemsMap[prevItem._id] == null) {
|
|
2960
|
+
removedItems.push(prevItem);
|
|
2961
|
+
}
|
|
2962
|
+
// Step 2: Get the items that are same in the current iteration
|
|
2963
|
+
else if (isEqual(getOrderItemComaparableData(currentItem), getOrderItemComaparableData(prevItem))) {
|
|
2964
|
+
unChangedItems.push(prevItem);
|
|
2965
|
+
}
|
|
2966
|
+
// Step 3: Get the items that are present in the previous iteration but
|
|
2967
|
+
// This is turn for edit items
|
|
2968
|
+
// but we change logic for display purpose
|
|
2969
|
+
else {
|
|
2970
|
+
const currQty = currentItem.quantity;
|
|
2971
|
+
const prevQty = prevItem.quantity;
|
|
2972
|
+
if (currQty !== prevQty) {
|
|
2973
|
+
const subtotalForOne = currentItem.subtotal / currentItem.quantity;
|
|
2974
|
+
// currQty = 8
|
|
2975
|
+
// prevQty = 3
|
|
2976
|
+
// diff = 5
|
|
2977
|
+
const diff = currQty - prevQty;
|
|
2978
|
+
const changeQty = Math.abs(diff);
|
|
2979
|
+
const itm = {
|
|
2980
|
+
...currentItem,
|
|
2981
|
+
quantity: changeQty,
|
|
2982
|
+
subtotal: parseNum(changeQty * subtotalForOne),
|
|
2983
|
+
};
|
|
2984
|
+
if (diff > 0) {
|
|
2985
|
+
// We need to show it as a new item
|
|
2986
|
+
newItems.push(itm);
|
|
2987
|
+
const unchangedQty = currQty - changeQty;
|
|
2988
|
+
const tempUnchangedItm = {
|
|
2989
|
+
...currentItem,
|
|
2990
|
+
quantity: unchangedQty,
|
|
2991
|
+
subtotal: parseNum(unchangedQty * subtotalForOne),
|
|
2992
|
+
};
|
|
2993
|
+
unChangedItems.push(tempUnchangedItm);
|
|
2994
|
+
}
|
|
2995
|
+
else {
|
|
2996
|
+
itm.void_reason = prevItem.void_reason ?? currentItem.void_reason;
|
|
2997
|
+
// We need to show it as a removed item
|
|
2998
|
+
removedItems.push(itm);
|
|
2999
|
+
const unchangedQty = prevQty - changeQty;
|
|
3000
|
+
const tempUnchangedItm = {
|
|
3001
|
+
...currentItem,
|
|
3002
|
+
quantity: unchangedQty,
|
|
3003
|
+
subtotal: parseNum(unchangedQty * subtotalForOne),
|
|
3004
|
+
};
|
|
3005
|
+
unChangedItems.push(tempUnchangedItm);
|
|
3006
|
+
totalTempRemovedItems++;
|
|
3007
|
+
}
|
|
3008
|
+
}
|
|
3009
|
+
else {
|
|
3010
|
+
const curr = {
|
|
3011
|
+
_id: currentItem._id,
|
|
3012
|
+
item_id: currentItem.item_id,
|
|
3013
|
+
menu_id: currentItem.menu_id,
|
|
3014
|
+
item_type: currentItem.item_type,
|
|
3015
|
+
name: currentItem.name,
|
|
3016
|
+
// price: number;
|
|
3017
|
+
// subtotal: number;
|
|
3018
|
+
// tax_rate: number;
|
|
3019
|
+
// quantity: number;
|
|
3020
|
+
special_instructions: currentItem.special_instructions,
|
|
3021
|
+
// item_discount_details: currentItem.item_discount_details,
|
|
3022
|
+
modifiers: currentItem.modifiers,
|
|
3023
|
+
};
|
|
3024
|
+
const prev = {
|
|
3025
|
+
_id: prevItem._id,
|
|
3026
|
+
item_id: prevItem.item_id,
|
|
3027
|
+
menu_id: prevItem.menu_id,
|
|
3028
|
+
item_type: prevItem.item_type,
|
|
3029
|
+
name: prevItem.name,
|
|
3030
|
+
// price: number;
|
|
3031
|
+
// subtotal: number;
|
|
3032
|
+
// tax_rate: number;
|
|
3033
|
+
// quantity: number;
|
|
3034
|
+
special_instructions: prevItem.special_instructions,
|
|
3035
|
+
// item_discount_details: prevItem.item_discount_details,
|
|
3036
|
+
modifiers: prevItem.modifiers,
|
|
3037
|
+
};
|
|
3038
|
+
const isBothEqual = isEqual(curr, prev);
|
|
3039
|
+
if (isBothEqual) {
|
|
3040
|
+
unChangedItems.push(currentItem);
|
|
3041
|
+
}
|
|
3042
|
+
else {
|
|
3043
|
+
// For edited item, we don't need to have tempUnchangedItemIds
|
|
3044
|
+
// because it's the turn for edited item
|
|
3045
|
+
editedItems.push(currentItem);
|
|
3046
|
+
}
|
|
3047
|
+
}
|
|
3048
|
+
}
|
|
3049
|
+
}
|
|
3050
|
+
// Step 4: Get the items that are present in the current iteration
|
|
3051
|
+
// but not present in the previous iteration
|
|
3052
|
+
for (let i = 0; i < currentItems.length; i++) {
|
|
3053
|
+
const item = currentItems[i];
|
|
3054
|
+
if (prevItemsMap[item._id] == null) {
|
|
3055
|
+
newItems.push(item);
|
|
3056
|
+
}
|
|
3057
|
+
}
|
|
3058
|
+
// Lastly, we need to check if any same item has been removed and added
|
|
3059
|
+
// If so, the we need to remove the item from removedItems and newItems, and add it to unChangedItems
|
|
3060
|
+
if (removedItems.length > 0 && newItems.length > 0) {
|
|
3061
|
+
const newItemsMap = newItems.reduce((acc, item) => {
|
|
3062
|
+
acc[item.item_id] = [...(acc[item.item_id] ?? []), item];
|
|
3063
|
+
return acc;
|
|
3064
|
+
}, {});
|
|
3065
|
+
const revertedRemovedItems = [];
|
|
3066
|
+
const revertedNewItems = [];
|
|
3067
|
+
const additionalNewItems = [];
|
|
3068
|
+
for (let i = 0; i < removedItems.length; i++) {
|
|
3069
|
+
const removedItem = removedItems[i];
|
|
3070
|
+
// Need to check if the removed item is present in the newItems
|
|
3071
|
+
const newItemsForItem = newItemsMap[removedItem.item_id];
|
|
3072
|
+
if (newItemsForItem == null)
|
|
3073
|
+
continue;
|
|
3074
|
+
if (newItemsForItem.length > 0) {
|
|
3075
|
+
const newItemsForItemLength = newItemsForItem.length;
|
|
3076
|
+
for (let j = 0; j < newItemsForItemLength; j++) {
|
|
3077
|
+
const newItem = newItemsForItem[j];
|
|
3078
|
+
const newI = {
|
|
3079
|
+
// _id: newItem._id,
|
|
3080
|
+
item_id: newItem.item_id,
|
|
3081
|
+
item_type: newItem.item_type,
|
|
3082
|
+
menu_id: newItem.menu_id,
|
|
3083
|
+
name: newItem.name,
|
|
3084
|
+
price: newItem.price,
|
|
3085
|
+
// subtotal: number;
|
|
3086
|
+
tax_rate: newItem.tax_rate,
|
|
3087
|
+
// quantity: newItem.quantity,
|
|
3088
|
+
special_instructions: newItem.special_instructions,
|
|
3089
|
+
// item_discount_details: newItem.item_discount_details,
|
|
3090
|
+
modifiers: newItem.modifiers,
|
|
3091
|
+
};
|
|
3092
|
+
const removedI = {
|
|
3093
|
+
// _id: removedItem._id,
|
|
3094
|
+
item_id: removedItem.item_id,
|
|
3095
|
+
item_type: removedItem.item_type,
|
|
3096
|
+
menu_id: removedItem.menu_id,
|
|
3097
|
+
name: removedItem.name,
|
|
3098
|
+
price: removedItem.price,
|
|
3099
|
+
// subtotal: number;
|
|
3100
|
+
tax_rate: removedItem.tax_rate,
|
|
3101
|
+
// quantity: removedItem.quantity,
|
|
3102
|
+
special_instructions: removedItem.special_instructions,
|
|
3103
|
+
// item_discount_details: removedItem.item_discount_details,
|
|
3104
|
+
modifiers: removedItem.modifiers,
|
|
3105
|
+
};
|
|
3106
|
+
if (isEqual(newI, removedI)) {
|
|
3107
|
+
if (newItem.quantity === removedItem.quantity) {
|
|
3108
|
+
newItemsMap[removedItem.item_id] = newItemsForItem.filter((item) => item._id !== newItem._id);
|
|
3109
|
+
if (newItemsMap[removedItem.item_id].length === 0) {
|
|
3110
|
+
delete newItemsMap[removedItem.item_id];
|
|
3111
|
+
}
|
|
3112
|
+
revertedNewItems.push(newItem);
|
|
3113
|
+
revertedRemovedItems.push(removedItem);
|
|
3114
|
+
// We will break the loop when ever we push to revertedRemovedItems
|
|
3115
|
+
break;
|
|
3116
|
+
}
|
|
3117
|
+
else {
|
|
3118
|
+
if (removedItem.quantity > newItem.quantity) {
|
|
3119
|
+
newItemsMap[removedItem.item_id] = newItemsForItem.filter((item) => item._id !== newItem._id);
|
|
3120
|
+
if (newItemsMap[removedItem.item_id].length === 0) {
|
|
3121
|
+
delete newItemsMap[removedItem.item_id];
|
|
3122
|
+
}
|
|
3123
|
+
revertedNewItems.push(newItem);
|
|
3124
|
+
const subtotalForSingle = removedItem.subtotal / removedItem.quantity;
|
|
3125
|
+
// We will only minus the quantity from removedItem
|
|
3126
|
+
const newQuantity = removedItem.quantity - newItem.quantity;
|
|
3127
|
+
const newSubtotal = parseNum(newQuantity * subtotalForSingle);
|
|
3128
|
+
removedItem.quantity = newQuantity;
|
|
3129
|
+
removedItem.subtotal = newSubtotal;
|
|
3130
|
+
}
|
|
3131
|
+
else {
|
|
3132
|
+
revertedRemovedItems.push(removedItem);
|
|
3133
|
+
const nQuantity = newItem.quantity;
|
|
3134
|
+
const subtotalForSingle = newItem.subtotal / nQuantity;
|
|
3135
|
+
const newQuantity = nQuantity - removedItem.quantity;
|
|
3136
|
+
const newSubtotal = parseNum(newQuantity * subtotalForSingle);
|
|
3137
|
+
newItem.quantity = newQuantity;
|
|
3138
|
+
newItem.subtotal = newSubtotal;
|
|
3139
|
+
// =====================><=====================
|
|
3140
|
+
const remainingQuantity = nQuantity - newQuantity;
|
|
3141
|
+
const adItem = {
|
|
3142
|
+
...newItem,
|
|
3143
|
+
quantity: remainingQuantity,
|
|
3144
|
+
subtotal: parseNum(remainingQuantity * subtotalForSingle),
|
|
3145
|
+
};
|
|
3146
|
+
additionalNewItems.push(adItem);
|
|
3147
|
+
// We will break the loop when ever we push to revertedRemovedItems
|
|
3148
|
+
break;
|
|
3149
|
+
}
|
|
3150
|
+
}
|
|
3151
|
+
}
|
|
3152
|
+
}
|
|
3153
|
+
}
|
|
3154
|
+
else {
|
|
3155
|
+
delete newItemsMap[removedItem.item_id];
|
|
3156
|
+
}
|
|
3157
|
+
}
|
|
3158
|
+
for (let i = 0; i < revertedRemovedItems.length; i++) {
|
|
3159
|
+
const rItem = revertedRemovedItems[i];
|
|
3160
|
+
removedItems = removedItems.filter((item) => item._id !== rItem._id);
|
|
3161
|
+
}
|
|
3162
|
+
for (let i = 0; i < revertedNewItems.length; i++) {
|
|
3163
|
+
const nItem = revertedNewItems[i];
|
|
3164
|
+
newItems = newItems.filter((item) => item._id !== nItem._id);
|
|
3165
|
+
unChangedItems.push(nItem);
|
|
3166
|
+
}
|
|
3167
|
+
for (let i = 0; i < additionalNewItems.length; i++) {
|
|
3168
|
+
const aItem = additionalNewItems[i];
|
|
3169
|
+
unChangedItems.push(aItem);
|
|
3170
|
+
}
|
|
3171
|
+
}
|
|
3172
|
+
// =====================><=====================
|
|
3173
|
+
// we need to check if courses have been removed in current iteration
|
|
3174
|
+
let coursesChanged = false;
|
|
3175
|
+
const previousCourses = previousDineinCourses ?? [];
|
|
3176
|
+
const currentCourses = currentDineinCourses ?? [];
|
|
3177
|
+
if (isDineinOrder) {
|
|
3178
|
+
if (previousCourses.length > 0 && currentCourses.length === 0) {
|
|
3179
|
+
orderCourses = previousCourses.map((course) => ({
|
|
3180
|
+
cartItemsObj: {
|
|
3181
|
+
unChangedItems: [],
|
|
3182
|
+
editedItems: [],
|
|
3183
|
+
removedItems: getCartItems(course.item_ids.map((itemId) => prevItemsMap[itemId])),
|
|
3184
|
+
newItems: [],
|
|
3185
|
+
},
|
|
3186
|
+
course_id: course.course_id,
|
|
3187
|
+
name: course.name,
|
|
3188
|
+
called: course.called,
|
|
3189
|
+
has_already_been_called: false,
|
|
3190
|
+
is_removed: true,
|
|
3191
|
+
total_amount: 0,
|
|
3192
|
+
totalSelectedItems: 0,
|
|
3193
|
+
}));
|
|
3194
|
+
}
|
|
3195
|
+
else if (previousCourses.length > 0 || currentCourses.length > 0) {
|
|
3196
|
+
const prevCoursesMap = {};
|
|
3197
|
+
const currentCoursesMap = {};
|
|
3198
|
+
let allCourseIds = new Set();
|
|
3199
|
+
for (let i = 0; i < previousCourses.length; i++) {
|
|
3200
|
+
const course = previousCourses[i];
|
|
3201
|
+
prevCoursesMap[course.course_id] = course;
|
|
3202
|
+
allCourseIds.add(course.course_id);
|
|
3203
|
+
}
|
|
3204
|
+
for (let i = 0; i < currentCourses.length; i++) {
|
|
3205
|
+
const course = currentCourses[i];
|
|
3206
|
+
currentCoursesMap[course.course_id] = course;
|
|
3207
|
+
allCourseIds.add(course.course_id);
|
|
3208
|
+
}
|
|
3209
|
+
allCourseIds = Array.from(allCourseIds);
|
|
3210
|
+
orderCourses = allCourseIds.map((courseId) => {
|
|
3211
|
+
const prevCourse = prevCoursesMap[courseId];
|
|
3212
|
+
const currentCourse = currentCoursesMap[courseId];
|
|
3213
|
+
let called;
|
|
3214
|
+
let has_already_been_called;
|
|
3215
|
+
let total_amount;
|
|
3216
|
+
let totalCourseItems;
|
|
3217
|
+
let name;
|
|
3218
|
+
let unChangedCourseItems = [];
|
|
3219
|
+
let editedCourseItems = [];
|
|
3220
|
+
let removedCourseItems = [];
|
|
3221
|
+
let newCourseItems = [];
|
|
3222
|
+
if (!prevCourse && currentCourse) {
|
|
3223
|
+
called = currentCourse.called;
|
|
3224
|
+
has_already_been_called = false;
|
|
3225
|
+
name = currentCourse.name;
|
|
3226
|
+
newCourseItems = [];
|
|
3227
|
+
total_amount = 0;
|
|
3228
|
+
totalCourseItems = 0;
|
|
3229
|
+
for (let i = 0; i < currentCourse.item_ids.length; i++) {
|
|
3230
|
+
const itemId = currentCourse.item_ids[i];
|
|
3231
|
+
const item = currentItemsMap[itemId];
|
|
3232
|
+
if (item == null)
|
|
3233
|
+
continue;
|
|
3234
|
+
newCourseItems.push(item);
|
|
3235
|
+
total_amount += item.subtotal;
|
|
3236
|
+
totalCourseItems += item.quantity;
|
|
3237
|
+
}
|
|
3238
|
+
}
|
|
3239
|
+
else {
|
|
3240
|
+
const prevCourseItemIds = new Set(prevCourse?.item_ids ?? []);
|
|
3241
|
+
const currentCourseItemIds = new Set(currentCourse?.item_ids ?? []);
|
|
3242
|
+
const course = currentCourse ?? prevCourse;
|
|
3243
|
+
name = course.name;
|
|
3244
|
+
called = course.called;
|
|
3245
|
+
has_already_been_called = prevCourse?.called ?? false;
|
|
3246
|
+
total_amount = 0;
|
|
3247
|
+
totalCourseItems = 0;
|
|
3248
|
+
unChangedCourseItems = [];
|
|
3249
|
+
editedCourseItems = [];
|
|
3250
|
+
removedCourseItems = [];
|
|
3251
|
+
newCourseItems = [];
|
|
3252
|
+
for (let i = 0; i < unChangedItems.length; i++) {
|
|
3253
|
+
const item = unChangedItems[i];
|
|
3254
|
+
const existsInCurrent = currentCourseItemIds.has(item._id);
|
|
3255
|
+
if (!existsInCurrent)
|
|
3256
|
+
continue;
|
|
3257
|
+
const existsInPrev = prevCourseItemIds.has(item._id);
|
|
3258
|
+
if (existsInPrev) {
|
|
3259
|
+
unChangedCourseItems.push(item);
|
|
3260
|
+
}
|
|
3261
|
+
else {
|
|
3262
|
+
item.special_instructions = `[Course changed]\n${item.special_instructions}`.trim();
|
|
3263
|
+
editedCourseItems.push(item);
|
|
3264
|
+
}
|
|
3265
|
+
total_amount += item.subtotal;
|
|
3266
|
+
totalCourseItems += item.quantity;
|
|
3267
|
+
}
|
|
3268
|
+
for (let i = 0; i < editedItems.length; i++) {
|
|
3269
|
+
const item = editedItems[i];
|
|
3270
|
+
const existsInCurrent = currentCourseItemIds.has(item._id);
|
|
3271
|
+
if (!existsInCurrent)
|
|
3272
|
+
continue;
|
|
3273
|
+
const existsInPrev = prevCourseItemIds.has(item._id);
|
|
3274
|
+
if (!existsInPrev) {
|
|
3275
|
+
item.special_instructions = `[Course changed]\n${item.special_instructions}`.trim();
|
|
3276
|
+
}
|
|
3277
|
+
editedCourseItems.push(item);
|
|
3278
|
+
total_amount += item.subtotal;
|
|
3279
|
+
totalCourseItems += item.quantity;
|
|
3280
|
+
}
|
|
3281
|
+
for (let i = 0; i < removedItems.length; i++) {
|
|
3282
|
+
const item = removedItems[i];
|
|
3283
|
+
// A removed item can only be in the prevCourseItemIds
|
|
3284
|
+
if (prevCourseItemIds.has(item._id)) {
|
|
3285
|
+
removedCourseItems.push(item);
|
|
3286
|
+
}
|
|
3287
|
+
}
|
|
3288
|
+
for (let i = 0; i < newItems.length; i++) {
|
|
3289
|
+
const item = newItems[i];
|
|
3290
|
+
if (currentCourseItemIds.has(item._id)) {
|
|
3291
|
+
newCourseItems.push(item);
|
|
3292
|
+
total_amount += item.subtotal;
|
|
3293
|
+
totalCourseItems += item.quantity;
|
|
3294
|
+
}
|
|
3295
|
+
}
|
|
3296
|
+
}
|
|
3297
|
+
if (!coursesChanged) {
|
|
3298
|
+
coursesChanged = unChangedCourseItems.length !== prevCourse?.item_ids.length;
|
|
3299
|
+
}
|
|
3300
|
+
const courseItem = {
|
|
3301
|
+
cartItemsObj: {
|
|
3302
|
+
unChangedItems: getCartItems(unChangedCourseItems),
|
|
3303
|
+
editedItems: getCartItems(editedCourseItems),
|
|
3304
|
+
removedItems: getCartItems(removedCourseItems),
|
|
3305
|
+
newItems: getCartItems(newCourseItems),
|
|
3306
|
+
},
|
|
3307
|
+
course_id: courseId,
|
|
3308
|
+
is_removed: false,
|
|
3309
|
+
called,
|
|
3310
|
+
has_already_been_called,
|
|
3311
|
+
total_amount,
|
|
3312
|
+
totalSelectedItems: parseNum(totalCourseItems, true),
|
|
3313
|
+
name,
|
|
3314
|
+
};
|
|
3315
|
+
return courseItem;
|
|
3316
|
+
});
|
|
3317
|
+
}
|
|
3318
|
+
}
|
|
3319
|
+
// =====================><=====================
|
|
3320
|
+
disabledReason =
|
|
3321
|
+
!coursesChanged &&
|
|
3322
|
+
unChangedItems.length === prevItems.length &&
|
|
3323
|
+
newItems.length === 0 &&
|
|
3324
|
+
removedItems.length === 0 &&
|
|
3325
|
+
editedItems.length === 0 &&
|
|
3326
|
+
previousCourses.length === currentCourses.length
|
|
3327
|
+
? 'No changes in the order'
|
|
3328
|
+
: prevItems.length === removedItems.length - totalTempRemovedItems &&
|
|
3329
|
+
editedItems.length === 0 &&
|
|
3330
|
+
newItems.length === 0
|
|
3331
|
+
? 'At least one item is required'
|
|
3332
|
+
: '';
|
|
3333
|
+
}
|
|
3334
|
+
else if (isDineinOrder && currentDineinCourses != null && currentDineinCourses.length > 0) {
|
|
3335
|
+
orderCourses = currentDineinCourses.map((course) => {
|
|
3336
|
+
const unChangedCourseItems = [];
|
|
3337
|
+
let total_amount = 0;
|
|
3338
|
+
let totalCourseItems = 0;
|
|
3339
|
+
for (let i = 0; i < unChangedItems.length; i++) {
|
|
3340
|
+
const item = unChangedItems[i];
|
|
3341
|
+
if (course.item_ids.includes(item._id)) {
|
|
3342
|
+
unChangedCourseItems.push(item);
|
|
3343
|
+
total_amount += item.subtotal;
|
|
3344
|
+
totalCourseItems += item.quantity;
|
|
3345
|
+
}
|
|
3346
|
+
}
|
|
3347
|
+
return {
|
|
3348
|
+
cartItemsObj: {
|
|
3349
|
+
unChangedItems: getCartItems(unChangedCourseItems),
|
|
3350
|
+
editedItems: [],
|
|
3351
|
+
removedItems: [],
|
|
3352
|
+
newItems: [],
|
|
3353
|
+
},
|
|
3354
|
+
course_id: course.course_id,
|
|
3355
|
+
name: course.name,
|
|
3356
|
+
called: course.called,
|
|
3357
|
+
has_already_been_called: false,
|
|
3358
|
+
is_removed: false,
|
|
3359
|
+
total_amount,
|
|
3360
|
+
totalSelectedItems: parseNum(totalCourseItems, true),
|
|
3361
|
+
};
|
|
3362
|
+
});
|
|
3363
|
+
}
|
|
3364
|
+
if (sortType === SORT_TYPE_ENUM.DESC) {
|
|
3365
|
+
orderCourses.reverse();
|
|
3366
|
+
}
|
|
3367
|
+
const cartItemsObj = {
|
|
3368
|
+
unChangedItems: getCartItems(unChangedItems),
|
|
3369
|
+
editedItems: getCartItems(editedItems),
|
|
3370
|
+
removedItems: getCartItems(removedItems),
|
|
3371
|
+
newItems: getCartItems(newItems),
|
|
3372
|
+
totalSelectedItems: parseNum(totalItemsInCart, true),
|
|
3373
|
+
disabledReason,
|
|
3374
|
+
orderCourses,
|
|
3375
|
+
isAnUpdate,
|
|
3376
|
+
};
|
|
3377
|
+
return cartItemsObj;
|
|
3378
|
+
}
|
|
3379
|
+
// ================================><================================= //
|
|
3380
|
+
export function getRemoveOfferFromCartData(data, offer_id, courses, listnerSettings) {
|
|
3381
|
+
const categories = data.categories ?? [];
|
|
3382
|
+
let order_discount_details = data.order_discount_details;
|
|
3383
|
+
let redeemed_offers = data.redeemed_offers;
|
|
3384
|
+
if (order_discount_details != null) {
|
|
3385
|
+
// we check if either the loyalty_discount_id or voucher_discount_id
|
|
3386
|
+
if (order_discount_details.loyalty_discount_id === offer_id) {
|
|
3387
|
+
order_discount_details = {
|
|
3388
|
+
...order_discount_details,
|
|
3389
|
+
loyalty_discount_id: undefined,
|
|
3390
|
+
loyalty_discount_name: undefined,
|
|
3391
|
+
loyalty_discount: undefined,
|
|
3392
|
+
is_loyalty_discount_percentage: false,
|
|
3393
|
+
};
|
|
3394
|
+
}
|
|
3395
|
+
else if (order_discount_details.voucher_discount_id === offer_id) {
|
|
3396
|
+
order_discount_details = {
|
|
3397
|
+
...order_discount_details,
|
|
3398
|
+
voucher_discount_id: undefined,
|
|
3399
|
+
voucher_discount_name: undefined,
|
|
3400
|
+
voucher_discount: undefined,
|
|
3401
|
+
is_voucher_discount_percentage: false,
|
|
3402
|
+
};
|
|
3403
|
+
}
|
|
3404
|
+
}
|
|
3405
|
+
if (redeemed_offers != null && redeemed_offers[offer_id] != null) {
|
|
3406
|
+
const nextRedeemedOffers = { ...redeemed_offers };
|
|
3407
|
+
delete nextRedeemedOffers[offer_id];
|
|
3408
|
+
redeemed_offers = Object.keys(nextRedeemedOffers).length > 0 ? nextRedeemedOffers : null;
|
|
3409
|
+
}
|
|
3410
|
+
const newCategories = [];
|
|
3411
|
+
for (let i = 0; i < categories.length; i++) {
|
|
3412
|
+
const category = categories[i];
|
|
3413
|
+
const newItems = [];
|
|
3414
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
3415
|
+
const item = category.items[j];
|
|
3416
|
+
// we check if either the loyalty_discount_id or voucher_discount_id
|
|
3417
|
+
// is present in the item_discount_details
|
|
3418
|
+
// if it is then we remove the items from cart
|
|
3419
|
+
if (item.item_discount_details != null) {
|
|
3420
|
+
if (item.item_discount_details.loyalty_discount_id === offer_id) {
|
|
3421
|
+
continue;
|
|
3422
|
+
}
|
|
3423
|
+
else if (item.item_discount_details.voucher_discount_id === offer_id) {
|
|
3424
|
+
continue;
|
|
3425
|
+
}
|
|
3426
|
+
else {
|
|
3427
|
+
newItems.push(item);
|
|
3428
|
+
}
|
|
3429
|
+
}
|
|
3430
|
+
else {
|
|
3431
|
+
newItems.push(item);
|
|
3432
|
+
}
|
|
3433
|
+
}
|
|
3434
|
+
newCategories.push({
|
|
3435
|
+
...category,
|
|
3436
|
+
items: newItems,
|
|
3437
|
+
});
|
|
3438
|
+
}
|
|
3439
|
+
const order = {
|
|
3440
|
+
...data,
|
|
3441
|
+
categories: newCategories,
|
|
3442
|
+
order_discount_details,
|
|
3443
|
+
redeemed_offers,
|
|
3444
|
+
wallet_redemptions: getWalletRedemptionsMap(data.wallet_redemptions),
|
|
3445
|
+
};
|
|
3446
|
+
const newData = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
|
|
3447
|
+
return newData;
|
|
3448
|
+
}
|
|
3449
|
+
// ================================><================================= //
|
|
3450
|
+
export function getRemoveAllOffersFromCartData(data, courses, listnerSettings, offerIds) {
|
|
3451
|
+
const categories = data.categories ?? [];
|
|
3452
|
+
let order_discount_details = data.order_discount_details;
|
|
3453
|
+
let redeemed_offers = data.redeemed_offers;
|
|
3454
|
+
if (order_discount_details != null) {
|
|
3455
|
+
// we check if either the loyalty_discount_id or voucher_discount_id
|
|
3456
|
+
if (order_discount_details.loyalty_discount_id != null &&
|
|
3457
|
+
(offerIds == null || offerIds.includes(order_discount_details.loyalty_discount_id))) {
|
|
3458
|
+
order_discount_details = {
|
|
3459
|
+
...order_discount_details,
|
|
3460
|
+
loyalty_discount_id: undefined,
|
|
3461
|
+
loyalty_discount_name: undefined,
|
|
3462
|
+
loyalty_discount: undefined,
|
|
3463
|
+
is_loyalty_discount_percentage: false,
|
|
3464
|
+
};
|
|
3465
|
+
}
|
|
3466
|
+
if (order_discount_details.voucher_discount_id != null &&
|
|
3467
|
+
(offerIds == null || offerIds.includes(order_discount_details.voucher_discount_id))) {
|
|
3468
|
+
order_discount_details = {
|
|
3469
|
+
...order_discount_details,
|
|
3470
|
+
voucher_discount_id: undefined,
|
|
3471
|
+
voucher_discount_name: undefined,
|
|
3472
|
+
voucher_discount: undefined,
|
|
3473
|
+
is_voucher_discount_percentage: false,
|
|
3474
|
+
};
|
|
3475
|
+
}
|
|
3476
|
+
}
|
|
3477
|
+
if (redeemed_offers != null) {
|
|
3478
|
+
if (offerIds == null) {
|
|
3479
|
+
redeemed_offers = null;
|
|
3480
|
+
}
|
|
3481
|
+
else {
|
|
3482
|
+
const nextRedeemedOffers = { ...redeemed_offers };
|
|
3483
|
+
for (let i = 0; i < offerIds.length; i++) {
|
|
3484
|
+
delete nextRedeemedOffers[offerIds[i]];
|
|
3485
|
+
}
|
|
3486
|
+
redeemed_offers = Object.keys(nextRedeemedOffers).length > 0 ? nextRedeemedOffers : null;
|
|
3487
|
+
}
|
|
3488
|
+
}
|
|
3489
|
+
const newCategories = [];
|
|
3490
|
+
for (let i = 0; i < categories.length; i++) {
|
|
3491
|
+
const category = categories[i];
|
|
3492
|
+
const newItems = [];
|
|
3493
|
+
for (let j = 0; j < category.items.length; j++) {
|
|
3494
|
+
const item = category.items[j];
|
|
3495
|
+
// we check if either the loyalty_discount_id or voucher_discount_id
|
|
3496
|
+
// is present in the item_discount_details
|
|
3497
|
+
// if it is then we remove the items from cart
|
|
3498
|
+
if (item.item_discount_details != null) {
|
|
3499
|
+
if (item.item_discount_details.loyalty_discount_id != null &&
|
|
3500
|
+
(offerIds == null || offerIds.includes(item.item_discount_details.loyalty_discount_id))) {
|
|
3501
|
+
continue;
|
|
3502
|
+
}
|
|
3503
|
+
else if (item.item_discount_details.voucher_discount_id != null &&
|
|
3504
|
+
(offerIds == null || offerIds.includes(item.item_discount_details.voucher_discount_id))) {
|
|
3505
|
+
continue;
|
|
3506
|
+
}
|
|
3507
|
+
else {
|
|
3508
|
+
newItems.push(item);
|
|
3509
|
+
}
|
|
3510
|
+
}
|
|
3511
|
+
else {
|
|
3512
|
+
newItems.push(item);
|
|
3513
|
+
}
|
|
3514
|
+
}
|
|
3515
|
+
newCategories.push({
|
|
3516
|
+
...category,
|
|
3517
|
+
items: newItems,
|
|
3518
|
+
});
|
|
3519
|
+
}
|
|
3520
|
+
const order = {
|
|
3521
|
+
...data,
|
|
3522
|
+
categories: newCategories,
|
|
3523
|
+
order_discount_details,
|
|
3524
|
+
redeemed_offers,
|
|
3525
|
+
wallet_redemptions: getWalletRedemptionsMap(data.wallet_redemptions),
|
|
3526
|
+
};
|
|
3527
|
+
const newData = calculateCartOrderInfoAndSyncData(order, courses, listnerSettings);
|
|
3528
|
+
return newData;
|
|
3529
|
+
}
|
|
3530
|
+
export function getClearVoucherCodeAndOffersFromCartData(data, courses, listnerSettings, offerIds) {
|
|
3531
|
+
const orderWithoutOffers = getRemoveAllOffersFromCartData(data, courses, listnerSettings, offerIds);
|
|
3532
|
+
const nextOrderDiscountDetails = buildClearVoucherDiscountDetails(orderWithoutOffers.order_discount_details);
|
|
3533
|
+
return getUpdateCartInfoData(orderWithoutOffers, {
|
|
3534
|
+
order_discount_details: nextOrderDiscountDetails,
|
|
3535
|
+
}, courses, listnerSettings);
|
|
3536
|
+
}
|
|
3537
|
+
// ================================><================================= //
|
|
3538
|
+
/**
|
|
3539
|
+
* Wipe all reward metadata (`redeemed_offers`, `wallet_redemptions`) and any
|
|
3540
|
+
* loyalty/voucher fields on `order_discount_details`, then resync the cart.
|
|
3541
|
+
*
|
|
3542
|
+
* Use this on hard reward-session boundaries — for example, in a POS cashier
|
|
3543
|
+
* session when the customer phone changes — where every reward applied for the
|
|
3544
|
+
* previous customer must be dropped before the next customer's eligibility is
|
|
3545
|
+
* evaluated. Items in the cart are preserved.
|
|
3546
|
+
*
|
|
3547
|
+
* Unlike `getClearVoucherCodeAndOffersFromCartData`, this also clears
|
|
3548
|
+
* `wallet_redemptions.gift_cards` and `wallet_redemptions.cashback_amount`,
|
|
3549
|
+
* along with any wallet-cashback `loyalty_discount_*` mirror on
|
|
3550
|
+
* `order_discount_details`.
|
|
3551
|
+
*/
|
|
3552
|
+
export function getResetRewardStateForNewCustomer(data, courses, listnerSettings) {
|
|
3553
|
+
const orderWithoutOffers = getRemoveAllOffersFromCartData(data, courses, listnerSettings, undefined);
|
|
3554
|
+
const orderDiscountDetailsWithoutVoucher = buildClearVoucherDiscountDetails(orderWithoutOffers.order_discount_details);
|
|
3555
|
+
const orderDiscountDetails = orderDiscountDetailsWithoutVoucher != null &&
|
|
3556
|
+
orderDiscountDetailsWithoutVoucher.loyalty_discount_id === WALLET_CASHBACK_LOYALTY_DISCOUNT_ID
|
|
3557
|
+
? clearLoyaltyDiscountFields(orderDiscountDetailsWithoutVoucher)
|
|
3558
|
+
: orderDiscountDetailsWithoutVoucher;
|
|
3559
|
+
return getUpdateCartInfoData(orderWithoutOffers, {
|
|
3560
|
+
order_discount_details: orderDiscountDetails,
|
|
3561
|
+
wallet_redemptions: null,
|
|
3562
|
+
}, courses, listnerSettings);
|
|
3563
|
+
}
|