public void Run(IHR1Context db, AccountingEntity accountingEntity) { var amtInput = new JournalTemplateInput() { AppTenantID = accountingEntity.AppTenantID, Name = "Salary", UserInputType = db.UserInputTypes.First(x => x.Code == "CURRENCY") }; var template = new JournalTemplate(); template.AppTenantID = accountingEntity.AppTenantID; template.JournalType = db.JournalTypes.First(x => x.Code == "PAYSTUB"); template.Name = "Salary"; template.OriginKey = "EmploymentContract"; template.UserInputs.Add(amtInput); var templateTxn = new JournalTemplateTxn(); templateTxn.AppTenantID = accountingEntity.AppTenantID; templateTxn.AmountInput = amtInput; templateTxn.Description = "Salary"; templateTxn.JournalTxnType = template.JournalType.JournalTxnTypes.First(x => x.IsCode == "AMT"); template.JournalTemplateTxns.Add(templateTxn); accountingEntity.JournalTemplates.Add(template); }
private JournalTemplate CreateExpenseTemplate(AccountingEntity entity, IDbContext dataContext, LedgerAccount payableAcct, LedgerAccount expenseAcct) { var template = new JournalTemplate(); template.AppTenantID = entity.AppTenantID; template.JournalType = dataContext.Set <JournalType>().Single(x => x.Code == "SEXP"); template.OriginKey = "ProviderAccount"; template.Name = "Service Expense"; JournalTemplateInput refInput = new JournalTemplateInput(); refInput.Name = "Reference"; refInput.AppTenantID = entity.AppTenantID; refInput.Description = ""; refInput.UserInputType = dataContext.Set <UserInputType>().Single(x => x.Code == "TEXT"); template.UserInputs.Add(refInput); JournalTemplateInput amtInput = new JournalTemplateInput(); amtInput.Name = "Amount"; amtInput.AppTenantID = entity.AppTenantID; amtInput.Description = ""; amtInput.UserInputType = dataContext.Set <UserInputType>().Single(x => x.Code == "CURRENCY"); template.UserInputs.Add(amtInput); //template.ReferenceInput = refInput; JournalTemplateTxn mainTxn = new JournalTemplateTxn(); mainTxn.AppTenantID = entity.AppTenantID; mainTxn.AmountInput = amtInput; mainTxn.Description = "Service Expense"; mainTxn.PrimaryFactorSource = "INPUT"; mainTxn.IncludeInTotal = true; mainTxn.JournalTxnType = template.JournalType.JournalTxnTypes.Single(x => x.PrimaryFactorCode == "AMT" && x.SecondaryFactorCode == null); mainTxn.Postings.Add(new JournalTemplateTxnPosting() { AppTenantID = entity.AppTenantID, AddBaseAmount = true, LedgerAccount = payableAcct, PostType = "C" }); mainTxn.Postings.Add(new JournalTemplateTxnPosting() { AppTenantID = entity.AppTenantID, AddBaseAmount = true, LedgerAccount = expenseAcct, PostType = "D" }); template.JournalTemplateTxns.Add(mainTxn); JournalTemplateTxn splitTxn = new JournalTemplateTxn(); splitTxn.AppTenantID = entity.AppTenantID; splitTxn.AmountInput = amtInput; splitTxn.Description = "Split"; splitTxn.SecondaryFactorSource = "INPUT"; splitTxn.IncludeInTotal = false; splitTxn.JournalTxnType = template.JournalType.JournalTxnTypes.Single(x => x.PrimaryFactorCode == "PART" && x.SecondaryFactorCode == "AMT"); template.JournalTemplateTxns.Add(splitTxn); dataContext.Set <JournalTemplate>().Add(template); return(template); }
public void OnChangeJournalTemplateTxn(JournalTemplateTxn entity, UpdateOperations operations) { InterceptChange(entity, operations); }
private decimal ResolveContextParameter(IDataContext db, TransactionTrigger trigger, JournalTemplateTxn templateTxn, Public resolvedPublic) { if (trigger.PolicyTransactionTrigger != null) { var policyTransactionTrigger = (trigger.PolicyTransactionTrigger); if (policyTransactionTrigger.Policy != null) { return(_contextParameterResolver.Resolve(db, trigger.TxnDate.Value, templateTxn.AmountContextParameterID.Value, policyTransactionTrigger.Policy)); } } throw new NotImplementedException(); }
private decimal ResolveLedgerBalance(IDataContext db, TransactionTrigger transactionTrigger, JournalTemplateTxn templateTxn, Public resolvedPublic) { if (templateTxn.AmountLedgerAccount == null) { throw new Exception("Ledger account not specified"); } PolicyTransactionTrigger policyTransactionTrigger = null; if (transactionTrigger.PolicyTransactionTrigger != null) { policyTransactionTrigger = (transactionTrigger.PolicyTransactionTrigger); } var query = db.LedgerTxns.Where(x => x.ReportingEntityID == transactionTrigger.ReportingEntityID && x.LedgerAccountID == templateTxn.AmountLedgerAccountID && x.TxnDate <= transactionTrigger.TxnDate); switch (templateTxn.BalanceOrigin) { case "P": query = query.Where(x => x.PolicyID == policyTransactionTrigger.PolicyID); break; case "U": if (resolvedPublic == null) { throw new Exception("No public to resolve ledger balance for"); } query = query.Where(x => x.PublicID == resolvedPublic.ID); break; case "X": if (resolvedPublic == null) { throw new Exception("No public to resolve ledger balance for"); } query = query.Where(x => x.PublicID == resolvedPublic.ID && x.PolicyID == policyTransactionTrigger.PolicyID); break; //todo: agents, sps, etc... } return(query.Sum(x => x.Amount)); }
private decimal ResolveAmount(IDataContext db, TransactionTrigger transactionTrigger, Public resolvedPublic, JournalTemplateTxn templateTxn) { decimal?result = null; if (templateTxn.Amount.HasValue) { result = templateTxn.Amount.Value; } if (templateTxn.AmountInputID.HasValue) { var amountInput = transactionTrigger.Inputs.First(x => x.JournalTemplateInputID == templateTxn.AmountInputID); result = Convert.ToDecimal(amountInput.Value); } if (templateTxn.AmountContextParameterID.HasValue) { result = ResolveContextParameter(db, transactionTrigger, templateTxn, resolvedPublic); } if (!result.HasValue) { throw new Exception("Unable to resolve amount"); } if (templateTxn.InvertPercentage) { result = (1 - result.Value); } return(result.Value); }
private void BuildCoverageTxns(IDataContext db, TransactionTrigger transactionTrigger, Public resolvedPublic, Journal journal, JournalTemplateTxn templateTxn, decimal percentage) { if (transactionTrigger.PolicyTransactionTrigger != null) { var policyTransactionTrigger = (transactionTrigger.PolicyTransactionTrigger); foreach (var coverage in policyTransactionTrigger.Policy.Coverages) { if (!coverage.Premium.HasValue) { throw new Exception("Coverage premium not set"); } JournalTxn txn = new JournalTxn(); txn.Amount = ((ResolveAmount(db, transactionTrigger, resolvedPublic, templateTxn) * coverage.Premium.Value) * percentage); txn.Description = templateTxn.Description + " - " + coverage.CoverageType.Name; txn.Policy = policyTransactionTrigger.Policy; txn.Public = resolvedPublic; //sp ?? txn.ReportingEntityBranch = transactionTrigger.ReportingEntityBranch; //txn.TransactionOrigin = transactionTrigger.TransactionOrigin; txn.TxnDate = transactionTrigger.TxnDate.Value.Date; txn.JournalTemplateTxn = templateTxn; txn.PolicyCoverage = coverage; txn.Agent = journal.Agent; journal.JournalTxns.Add(txn); } } }
private void BuildStandardTxn(IDataContext db, TransactionTrigger transactionTrigger, Public resolvedPublic, Journal journal, JournalTemplateTxn templateTxn, decimal percentage) { JournalTxn txn = new JournalTxn(); if (templateTxn.JournalTxnClass.IsDefinedAmount) { txn.Amount = (ResolveAmount(db, transactionTrigger, resolvedPublic, templateTxn) * percentage); } if (templateTxn.JournalTxnClass.OfLedgerAccount) { txn.Amount = ((ResolveAmount(db, transactionTrigger, resolvedPublic, templateTxn) * ResolveLedgerBalance(db, transactionTrigger, templateTxn, resolvedPublic)) * percentage); } txn.Description = templateTxn.Description; if (transactionTrigger.PolicyTransactionTrigger != null) { txn.Policy = transactionTrigger.PolicyTransactionTrigger.Policy; } txn.Public = resolvedPublic; txn.ReportingEntityBranch = transactionTrigger.ReportingEntityBranch; //txn.TransactionOrigin = transactionTrigger.TransactionOrigin; txn.TxnDate = transactionTrigger.TxnDate.Value.Date; txn.JournalTemplateTxn = templateTxn; txn.Agent = journal.Agent; journal.JournalTxns.Add(txn); }