Created
May 30, 2023 00:45
-
-
Save cnaccio/b5f6d374b5a48949f32261e8da0fa06d to your computer and use it in GitHub Desktop.
AccountingSeed Test Data Helper Class [just execute TestsUtils.setupAccountingSeedTestData()]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class TestsUtils { | |
// Setup global/account data | |
public static void setupAccountingSeedTestData() { | |
User adminUser = TestsUtils.getAdminUser(); | |
System.runAs(adminUser) { | |
TestsUtils.createAccountingSettings(); | |
} | |
} | |
// Get an admin user | |
public static User getAdminUser() { | |
// Replace with username of an actual admin | |
User u = [SELECT Id FROM User WHERE Username = '[email protected]']; | |
return u; | |
} | |
// Create accounting settings | |
public static AcctSeed__Accounting_Settings__c createAccountingSettings() { | |
// Create default ledger | |
AcctSeed__Ledger__c defaultLedger = createLedger(); | |
AcctSeed.LedgerService.setTestOrgDefaultLedger(defaultLedger.Id); | |
// Setup default purchase order format | |
AcctSeed__Billing_Format__c defaultPurchaseOrderFormat = createPurchaseOrderFormat(); | |
// Create accounting settings | |
AcctSeed__Accounting_Settings__c settings = new AcctSeed__Accounting_Settings__c( | |
Name = 'AccountingSettings', | |
AcctSeed__Default_Ledger__c = defaultLedger.Id, | |
AcctSeed__Enable_Billing_Period_Sensitive_Aging__c = true, | |
AcctSeed__Enable_AP_Period_Sensitive_Aging__c = true, | |
AcctSeed__Enable_Product_Costing__c = true, | |
AcctSeed__Post_Settings__c = 'Header-Level Post', | |
AcctSeed__Inventory_Valuation_Method__c = 'Average Cost', | |
AcctSeed__Display_Billable_Flag_in_Time_Card_Entry__c = true, | |
AcctSeed__Default_Purchase_Order_Format__c = defaultPurchaseOrderFormat.Id | |
); | |
// Setup additiona/optional settings | |
AcctSeed__GL_Account__c inventoryVarianceGLAccount = createGLAccount( | |
'50300-Inventory Adjustments', | |
'Expense', | |
'Cost of Goods Sold', | |
'Materials', | |
false | |
); | |
settings.AcctSeed__Inventory_Variance_GL_Account__c = inventoryVarianceGLAccount.Id; | |
// Save settings | |
insert settings; | |
// Create GL Defaults | |
createGLDefaults(defaultLedger.Id); | |
// Return settings | |
return settings; | |
} | |
// Setup all require default GL accounts | |
public static void createGLDefaults(Id defaultLedgerId) { | |
// Create default ledger if not given | |
if (defaultLedgerId == null) { | |
defaultLedgerId = createLedger().Id; | |
} | |
// Create the GL Account records | |
AcctSeed__GL_Account__c arControlGLAccount = createGLAccount( | |
'12000-Accounts Receivable', | |
'Balance Sheet', | |
'Assets', | |
'Current Assets', | |
false | |
); | |
AcctSeed__GL_Account__c revenueGLAccount = createGLAccount( | |
'40000-Product Revenue', | |
'Revenue', | |
'Operating Revenue', | |
'Product', | |
false | |
); | |
AcctSeed__GL_Account__c customerPaymentAdjustmentGLAccount = createGLAccount( | |
'40200-Sales Discounts', | |
'Revenue', | |
'Operating Revenue', | |
'Other', | |
false | |
); | |
AcctSeed__GL_Account__c apControlGLAccount = createGLAccount( | |
'20000-Accounts Payable', | |
'Balance Sheet', | |
'Liabilities', | |
'Current Liabilities', | |
false | |
); | |
AcctSeed__GL_Account__c vendorPaymentDiscountGLAccount = createGLAccount( | |
'50200-Purchase Discounts', | |
'Expense', | |
'Cost of Goods Sold', | |
'Materials', | |
false | |
); | |
AcctSeed__GL_Account__c unappliedARGLAccount = createGLAccount( | |
'11999-Suspense Unapplied Cash', | |
'Balance Sheet', | |
'Assets', | |
'Current Assets', | |
false | |
); | |
AcctSeed__GL_Account__c workInProcessGLAccount = createGLAccount( | |
'2010-Vouchers Payable', | |
'Balance Sheet', | |
'Assets', | |
'Current Assets', | |
false | |
); | |
// Setup new GL Defaults | |
List<AcctSeed__GL_Account_Default__c> glAccountDefaults = | |
new List<AcctSeed__GL_Account_Default__c> { | |
// AR Control GL Account | |
new AcctSeed__GL_Account_Default__c( | |
AcctSeed__Ledger__c = defaultLedgerId, | |
AcctSeed__GL_Account_Specification__c = 'AR_Control_GL_Account', | |
AcctSeed__GL_Account__c = arControlGLAccount.Id | |
), | |
// Default Revenue GL Account | |
new AcctSeed__GL_Account_Default__c( | |
AcctSeed__Ledger__c = defaultLedgerId, | |
AcctSeed__GL_Account_Specification__c = 'Revenue_GL_Account', | |
AcctSeed__GL_Account__c = revenueGLAccount.Id | |
), | |
// Customer Payment Adjustment GL Account | |
new AcctSeed__GL_Account_Default__c( | |
AcctSeed__Ledger__c = defaultLedgerId, | |
AcctSeed__GL_Account_Specification__c = | |
'Customer_Payment_Adjustment_GL_Account', | |
AcctSeed__GL_Account__c = customerPaymentAdjustmentGLAccount.Id | |
), | |
// AP Control GL Account | |
new AcctSeed__GL_Account_Default__c( | |
AcctSeed__Ledger__c = defaultLedgerId, | |
AcctSeed__GL_Account_Specification__c = 'AP_Control_GL_Account', | |
AcctSeed__GL_Account__c = apControlGLAccount.Id | |
), | |
// Vendor Payment Discount GL Account | |
new AcctSeed__GL_Account_Default__c( | |
AcctSeed__Ledger__c = defaultLedgerId, | |
AcctSeed__GL_Account_Specification__c = 'Payment_Discount_GL_Account', | |
AcctSeed__GL_Account__c = vendorPaymentDiscountGLAccount.Id | |
), | |
// Unapplied A/R GL Account | |
new AcctSeed__GL_Account_Default__c( | |
AcctSeed__Ledger__c = defaultLedgerId, | |
AcctSeed__GL_Account_Specification__c = 'Unapplied_Cash_GL_Account', | |
AcctSeed__GL_Account__c = unappliedARGLAccount.Id | |
), | |
// Work in Process GL Account | |
new AcctSeed__GL_Account_Default__c( | |
AcctSeed__Ledger__c = defaultLedgerId, | |
AcctSeed__GL_Account_Specification__c = 'Work_in_Process_GL_Account', | |
AcctSeed__GL_Account__c = workInProcessGLAccount.Id | |
) | |
}; | |
// Save GL Defaults | |
insert glAccountDefaults; | |
} | |
public static AcctSeed__Ledger__c createLedger() { | |
AcctSeed__Ledger__c ld; | |
for (AcctSeed__Ledger__c ledgerRecord : [SELECT Id FROM AcctSeed__Ledger__c]) { | |
ld = ledgerRecord; | |
break; | |
} | |
if (ld != null) { | |
ld = (AcctSeed__Ledger__c)TestsUtils.selectAll(ld.Id); | |
} else { | |
ld = new AcctSeed__Ledger__c( | |
Name = 'Actual', | |
AcctSeed__Type__c = 'Transactional', | |
AcctSeed__Billing_Activity_Statement_Format__c = TestsUtils.createActivityFormat().id, | |
AcctSeed__Billing_Outstanding_Statement_Format__c = TestsUtils.createOutstandingFormat().id, | |
AcctSeed__Default_Bank_Account__c = TestsUtils.createGLAccount( | |
'Testing GL Account', | |
'Balance Sheet', | |
'Assets', | |
null, | |
true | |
) | |
.Id, | |
AcctSeed__Default_Billing_Format__c = TestsUtils.createInvoiceFormat().id, | |
AcctSeed__Default_Packing_Slip_Format__c = TestsUtils.createPackingSlipFormat().id, | |
AcctSeed__Default_Purchase_Order_Format__c = TestsUtils.createFormat().id, | |
AcctSeed__Tax_Settings__c = TestsUtils.createTaxSettings().Id | |
); | |
insert ld; | |
} | |
return ld; | |
} | |
public static AcctSeed__Billing_Format__c createActivityFormat() { | |
AcctSeed__Billing_Format__c format = new AcctSeed__Billing_Format__c( | |
Name = 'Default Billing Statement', | |
AcctSeed__Type__c = 'Activity Statement', | |
AcctSeed__Visualforce_PDF_Page__c = 'BillingActivityStatementPDF', | |
AcctSeed__Default_Email_Template__c = 'Activity_Statement_Email_Template' | |
); | |
insert format; | |
return format; | |
} | |
public static AcctSeed__Billing_Format__c createOutstandingFormat() { | |
AcctSeed__Billing_Format__c format = new AcctSeed__Billing_Format__c( | |
Name = 'Default Outstanding Statement', | |
AcctSeed__Type__c = 'Outstanding Statement', | |
AcctSeed__Visualforce_PDF_Page__c = 'BillingOutstandingStatementPDF', | |
AcctSeed__Default_Email_Template__c = 'Outstanding_Statement_Email_Template' | |
); | |
insert format; | |
return format; | |
} | |
public static AcctSeed__Billing_Format__c createInvoiceFormat() { | |
AcctSeed__Billing_Format__c invFormat = null; | |
List<AcctSeed__Billing_Format__c> invFormats = [ | |
SELECT Id | |
FROM AcctSeed__Billing_Format__c | |
LIMIT 1 | |
]; | |
if (invFormats == null || invFormats.size() < 1) { | |
invFormat = new AcctSeed__Billing_Format__c( | |
Name = 'Default Billing Product', | |
AcctSeed__Type__c = 'Billing', | |
AcctSeed__Visualforce_PDF_Page__c = 'BillingProductPDF', | |
AcctSeed__Default_Email_Template__c = 'Billing_Email_Template' | |
); | |
insert invFormat; | |
} else { | |
invFormat = invFormats[0]; | |
} | |
return invFormat; | |
} | |
public static AcctSeed__Billing_Format__c createPackingSlipFormat() { | |
AcctSeed__Billing_Format__c psFormat = null; | |
List<AcctSeed__Billing_Format__c> psFormats = [ | |
SELECT Id | |
FROM AcctSeed__Billing_Format__c | |
LIMIT 1 | |
]; | |
if (psFormats == null || psFormats.size() < 1) { | |
psFormat = new AcctSeed__Billing_Format__c( | |
Name = 'Default Packing Slip', | |
AcctSeed__Type__c = 'Packing Slip', | |
AcctSeed__Visualforce_PDF_Page__c = 'ShipmentPDF', | |
AcctSeed__Default_Email_Template__c = 'Shipment_Email_Template_v2' | |
); | |
insert psFormat; | |
} else { | |
psFormat = psFormats[0]; | |
} | |
return psFormat; | |
} | |
public static AcctSeed__Billing_Format__c createPurchaseOrderFormat() { | |
AcctSeed__Billing_Format__c format = new AcctSeed__Billing_Format__c( | |
Name = 'Test', | |
AcctSeed__Type__c = 'Purchase Order', | |
AcctSeed__Visualforce_PDF_Page__c = 'PurchaseOrderPDF', | |
AcctSeed__Default_Email_Template__c = 'Purchase_Order_Email_Template' | |
); | |
insert format; | |
return format; | |
} | |
public static AcctSeed__Billing_Format__c createFormat() { | |
AcctSeed__Billing_Format__c format = new AcctSeed__Billing_Format__c( | |
Name = 'Default Purchase Order', | |
AcctSeed__Type__c = 'Purchase Order', | |
AcctSeed__Visualforce_PDF_Page__c = 'PurchaseOrderPDF', | |
AcctSeed__Default_Email_Template__c = 'Purchase_Order_Email_Template' | |
); | |
insert format; | |
return format; | |
} | |
public static AcctSeed__GL_Account__c createGLAccount( | |
String accountName, | |
String type, | |
String subType, | |
String subType2, | |
Boolean isBank | |
) { | |
AcctSeed__GL_Account__c glAccount = new AcctSeed__GL_Account__c( | |
Name = accountName, | |
AcctSeed__Type__c = type, | |
AcctSeed__Sub_Type_1__c = subType, | |
AcctSeed__Sub_Type_2__c = subType2, | |
AcctSeed__Bank__c = isBank, | |
AcctSeed__Active__c = true | |
); | |
insert glAccount; | |
return glAccount; | |
} | |
public static AcctSeed__Tax_Settings__c createTaxSettings() { | |
AcctSeed__Tax_Settings__c settings = new AcctSeed__Tax_Settings__c( | |
Name = 'Accounting Seed Native Tax', | |
AcctSeed__Tax_Settings_Method__c = 'Accounting Seed Native Tax', | |
AcctSeed__Sales_Tax_Calculation_Mode__c = 'Point of Sale' | |
); | |
insert settings; | |
return settings; | |
} | |
// Select all fields for given record id | |
public static SObject selectAll(Id recordId) { | |
SObject record; | |
// Grab all fields for given sobject record type | |
DescribeSObjectResult describeResult = recordId.getSObjectType().getDescribe(); | |
List<String> fieldNames = new List<String>(describeResult.fields.getMap().keySet()); | |
// Build query | |
String query = | |
' SELECT ' + | |
String.join(fieldNames, ',') + | |
' FROM ' + | |
describeResult.getName() + | |
' WHERE ' + | |
' id = :recordId ' + | |
' LIMIT 1 '; | |
// Return generic list of sobjects or typecast to expected type | |
List<SObject> records = Database.query(query); | |
if (records.size() == 1) { | |
record = records.get(0); | |
} | |
return record; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hit me up if you guys have any questions, or run into any issues using this.