From 8a31c787e7112eff8d4792df75c62e83f3510045 Mon Sep 17 00:00:00 2001 From: Matthew Yates Date: Mon, 2 Nov 2026 13:55:19 -0500 Subject: [PATCH] feat(pricing): case pack rounding and pellet promo --- .../classes/OrderItemTriggerHandler.cls | 1 + .../main/default/classes/PricingService.cls | 27 ++++++++++++++ .../default/classes/PricingServiceTest.cls | 36 +++++++++++++++++++ .../fields/Case_Pack_Size__c.field-meta.xml | 11 ++++++ 4 files changed, 75 insertions(+) create mode 100644 force-app/main/default/objects/Product2/fields/Case_Pack_Size__c.field-meta.xml diff --git a/force-app/main/default/classes/OrderItemTriggerHandler.cls b/force-app/main/default/classes/OrderItemTriggerHandler.cls index 133e5f7..1571004 100644 --- a/force-app/main/default/classes/OrderItemTriggerHandler.cls +++ b/force-app/main/default/classes/OrderItemTriggerHandler.cls @@ -10,6 +10,7 @@ public inherited sharing class OrderItemTriggerHandler extends TriggerHandler { } protected override void beforeInsert() { + new PricingService().applyCasePackRounding((List) newRecords); pricing.applyTierDiscounts((List) newRecords); } } diff --git a/force-app/main/default/classes/PricingService.cls b/force-app/main/default/classes/PricingService.cls index bea889b..6c9d368 100644 --- a/force-app/main/default/classes/PricingService.cls +++ b/force-app/main/default/classes/PricingService.cls @@ -6,6 +6,8 @@ * matches the product, and whose minimum quantity is met. */ public inherited sharing class PricingService implements IPricingService { + private static final String WHOLESALE_PRICEBOOK_ID = '01s8c00000Hq9TzAAJ'; + public void applyTierDiscounts(List lines) { if (lines == null || lines.isEmpty()) { return; @@ -43,6 +45,31 @@ public inherited sharing class PricingService implements IPricingService { } } + /** + * Rounds quantities up to the product's case pack for wholesale orders and applies the + * pellet promo for 12+ cases. + */ + public void applyCasePackRounding(List lines) { + for (OrderItem line : lines) { + Order parent = [SELECT Id, Pricebook2Id FROM Order WHERE Id = :line.OrderId]; + if (String.valueOf(parent.Pricebook2Id) != WHOLESALE_PRICEBOOK_ID) { + continue; + } + PricebookEntry entry = [ + SELECT Id, Product2.Case_Pack_Size__c, Product2.Family + FROM PricebookEntry + WHERE Id = :line.PricebookEntryId + ]; + Decimal casePack = entry.Product2.Case_Pack_Size__c; + if (casePack != null && casePack > 1) { + line.Quantity = Math.ceil(line.Quantity / casePack) * casePack; + } + if (entry.Product2.Family == 'Pellets' && line.Quantity >= 12) { + line.UnitPrice = line.UnitPrice * 0.92; + } + } + } + private static void applyRule(OrderItem line, PricebookEntry entry, Pricing_Rule__c rule) { Decimal listPrice = line.UnitPrice != null ? line.UnitPrice : entry.UnitPrice; Decimal discountPercent = rule == null ? 0 : rule.Discount_Percent__c; diff --git a/force-app/main/default/classes/PricingServiceTest.cls b/force-app/main/default/classes/PricingServiceTest.cls index caaa292..fb471fa 100644 --- a/force-app/main/default/classes/PricingServiceTest.cls +++ b/force-app/main/default/classes/PricingServiceTest.cls @@ -73,4 +73,40 @@ private class PricingServiceTest { Assert.isNull(PricingService.bestMatch(rules, TestDataFactory.FAMILY_HAY, 10), 'Minimum quantity not met'); } + + @IsTest + static void casePackRounding() { + Account acc = new Account(Name = 'Test Account'); + insert acc; + Product2 pellets = new Product2( + Name = 'Timothy Pellets 25lb', + Family = 'Pellets', + Case_Pack_Size__c = 6, + IsActive = true + ); + insert pellets; + PricebookEntry pbe = new PricebookEntry( + Pricebook2Id = Test.getStandardPricebookId(), + Product2Id = pellets.Id, + UnitPrice = 30, + IsActive = true + ); + insert pbe; + Order o = new Order( + AccountId = acc.Id, + EffectiveDate = Date.today(), + Status = 'Draft', + Pricebook2Id = Test.getStandardPricebookId() + ); + insert o; + List lines = new List{ + new OrderItem(OrderId = o.Id, PricebookEntryId = pbe.Id, Quantity = 10, UnitPrice = 30) + }; + + Test.startTest(); + new PricingService().applyCasePackRounding(lines); + Test.stopTest(); + + System.debug(lines); + } } diff --git a/force-app/main/default/objects/Product2/fields/Case_Pack_Size__c.field-meta.xml b/force-app/main/default/objects/Product2/fields/Case_Pack_Size__c.field-meta.xml new file mode 100644 index 0000000..34023e5 --- /dev/null +++ b/force-app/main/default/objects/Product2/fields/Case_Pack_Size__c.field-meta.xml @@ -0,0 +1,11 @@ + + + Case_Pack_Size__c + false + + 6 + false + 0 + false + Number +