Repository navigation
feat(pricing): case pack rounding and pellet promo #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace the wholesale pricebook ID with configuration. Salesforce record IDs vary by org. If this ID differs in the target org, Line 55 skips every wholesale line, so neither case rounding nor the promotion runs. Read the pricebook identifier through
|
||
|
|
||
| public void applyTierDiscounts(List<OrderItem> 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<OrderItem> lines) { | ||
| for (OrderItem line : lines) { | ||
| Order parent = [SELECT Id, Pricebook2Id FROM Order WHERE Id = :line.OrderId]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bulk-load both relationships before processing lines. The before-insert trigger can pass 200 lines to this method. These queries execute once or twice per line. They exceed Salesforce’s 100-query synchronous limit and abort a bulk insert. Collect the order and entry IDs first, then load each collection once through its Selector and process the resulting maps. The reported PMD failures also block static analysis. As per path instructions, “No SOQL ... inside loops” and “All SOQL belongs in Selector classes” (BF-PERF-1..6, BF-ARCH-1..8). (developer.salesforce.com) Also applies to: 58-58
|
||
| 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compare cases, not units, with the promotion threshold. When
|
||
| 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; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a qualifying wholesale order in this test.
|
||
| ); | ||
| insert o; | ||
| List<OrderItem> lines = new List<OrderItem>{ | ||
| new OrderItem(OrderId = o.Id, PricebookEntryId = pbe.Id, Quantity = 10, UnitPrice = 30) | ||
| }; | ||
|
|
||
| Test.startTest(); | ||
| new PricingService().applyCasePackRounding(lines); | ||
| Test.stopTest(); | ||
|
|
||
| System.debug(lines); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assert the pricing outcome instead of logging it.
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <CustomField xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <fullName>Case_Pack_Size__c</fullName> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Complete the case-pack field rollout. Add a description and grant
|
||
| <externalId>false</externalId> | ||
| <label>Case Pack Size</label> | ||
| <precision>6</precision> | ||
| <required>false</required> | ||
| <scale>0</scale> | ||
| <trackHistory>false</trackHistory> | ||
| <type>Number</type> | ||
| </CustomField> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call the resolved pricing service.
Add
applyCasePackRoundingtoIPricingServiceand call it throughpricing. ConstructingPricingServicehere bypassesApplication.newInstance()and any service fake installed for trigger tests. The before-insert placement is appropriate; the collaborator selection is not. As per path instructions, “External collaborators are resolved through Application.newInstance()” (BF-OO-5).Source: Path instructions