using Sarand.BusinessMgr.Net.Standard.Base.App; using Sarand.BusinessMgr.Net.Standard.Base.Bus; using Sarand.Net.Standard.Accounts.QuickBooks.App; using Sarand.Net.Standard.Accounts.Sage.App; using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Utils; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sarand.BusinessMgr.Net.Standard.Base.Consoles { //======================================================================= /// /// Session Console - Transaction processing options/rules. /// partial class SbmSessionConsole { //------------------------------------------------------------------- // Supported Transaction types. public virtual bool AllowTransactionType(Sbm.TransactionType type) { switch (type) { // Transaction types that are allowed. case Sbm.TransactionType.SalesQuote: case Sbm.TransactionType.SalesOrder: //case Sbm.TransactionType.PurchaseOrder: return InstanceConfig.AllowTransactionType(type, true); // Proforma types are only allowed if ther counterparts cannot be created. case Sbm.TransactionType.ProformaPurchaseOrder: return InstanceConfig.AllowTransactionType(type, AllowTransactionType(Sbm.TransactionType.PurchaseOrder) && !CanCreateTransactionLocally(Sbm.TransactionType.PurchaseOrder)); case Sbm.TransactionType.ProformaSalesOrder: return InstanceConfig.AllowTransactionType(type, AllowTransactionType(Sbm.TransactionType.SalesOrder) && !CanCreateTransactionLocally(Sbm.TransactionType.SalesOrder)); case Sbm.TransactionType.ProformaSalesInvoice: return InstanceConfig.AllowTransactionType(type, AllowTransactionType(Sbm.TransactionType.SalesInvoice) && !CanCreateTransactionLocally(Sbm.TransactionType.SalesInvoice)); } return false; } //------------------------------------------------------------------- // Create Transaction. public virtual bool CanCreateTransactionLocally(Sbm.TransactionType type) { if (!AllowTransactionType(type)) return false; // Refresh options. if (dfnSageInt.AffiliateId_Owner != UserContext.OrganisationAffiliateId || dfnQBooksInt.AffiliateId_Owner != UserContext.OrganisationAffiliateId) { // Reset the accounts interface configurations. dfnSageInt.ClearConfig(); dfnSageInt.AffiliateId_Owner = UserContext.OrganisationAffiliateId; dfnQBooksInt.ClearConfig(); dfnQBooksInt.AffiliateId_Owner = UserContext.OrganisationAffiliateId; // Identify the interfaces. dfnSageInt.InterfaceId = Internals.AccountsDfn.GetAccountInterfaceId(Sbm.InterfaceType_Accounts.SageLine50); dfnQBooksInt.InterfaceId = Internals.AccountsDfn.GetAccountInterfaceId(Sbm.InterfaceType_Accounts.QuickBooks); // Sales and purchase orders/invoices can only be created locally if they are not imported from one of these interfaces. if (SysId.IsValid(dfnSageInt.InterfaceId)) { // Interpret interface options. dfnSageInt.importPurchaseOrder = Internals.AccountsDfn.GetSageImportOption(Sage.ItemType.PurchaseOrder); dfnSageInt.importSalesOrder = Internals.AccountsDfn.GetSageImportOption(Sage.ItemType.SalesOrder); dfnSageInt.importProductInvoice = Internals.AccountsDfn.GetSageImportOption(Sage.ItemType.Invoice_Product); dfnSageInt.importServiceInvoice = Internals.AccountsDfn.GetSageImportOption(Sage.ItemType.Invoice_Service); dfnSageInt.importSalesOrderInvoice = Internals.AccountsDfn.GetSageImportOption(Sage.ItemType.Invoice_SalesOrder); dfnSageInt.exportPurchaseOrder = Internals.AccountsDfn.GetSageExportOption(Sage.ItemType.PurchaseOrder); dfnSageInt.exportSalesOrder = Internals.AccountsDfn.GetSageExportOption(Sage.ItemType.SalesOrder); dfnSageInt.exportProductInvoice = Internals.AccountsDfn.GetSageExportOption(Sage.ItemType.Invoice_Product); dfnSageInt.exportServiceInvoice = Internals.AccountsDfn.GetSageExportOption(Sage.ItemType.Invoice_Service); dfnSageInt.exportSalesOrder = Internals.AccountsDfn.GetSageExportOption(Sage.ItemType.Invoice_SalesOrder); } if (SysId.IsValid(dfnQBooksInt.InterfaceId)) { // Interpret interface options. dfnQBooksInt.importPurchaseOrder = Internals.AccountsDfn.GetQuickBooksImportOption(Qbk.EntityType.PurchaseOrder); dfnQBooksInt.importSalesOrder = Internals.AccountsDfn.GetQuickBooksImportOption(Qbk.EntityType.SalesOrder); dfnQBooksInt.importProductInvoice = Internals.AccountsDfn.GetQuickBooksImportOption(Qbk.EntityType.Invoice_Product); dfnQBooksInt.importServiceInvoice = Internals.AccountsDfn.GetQuickBooksImportOption(Qbk.EntityType.Invoice_Service); dfnQBooksInt.importSalesOrderInvoice = false; dfnQBooksInt.exportPurchaseOrder = Internals.AccountsDfn.GetQuickBooksExportOption(Qbk.EntityType.PurchaseOrder); dfnQBooksInt.exportSalesOrder = Internals.AccountsDfn.GetQuickBooksExportOption(Qbk.EntityType.SalesOrder); dfnQBooksInt.exportProductInvoice = Internals.AccountsDfn.GetQuickBooksExportOption(Qbk.EntityType.Invoice_Product); dfnQBooksInt.exportServiceInvoice = Internals.AccountsDfn.GetQuickBooksExportOption(Qbk.EntityType.Invoice_Service); dfnQBooksInt.bexport_SalesOrderInvoice = false; } // Transaction types cannot be created locally if they are either imported from or exported to Sage. // In the case of exports, it is the 'proforma' equivalent that can be created for export. createTransaction_PurchaseOrder = true; createTransaction_SalesQuote = true; createTransaction_SalesOrder = true; createTrans_SalesInvoice = true; if (createTransaction_PurchaseOrder) createTransaction_PurchaseOrder = !dfnSageInt.importPurchaseOrder && !dfnSageInt.exportPurchaseOrder; if (createTransaction_SalesOrder) createTransaction_SalesOrder = !dfnSageInt.importSalesOrder && !dfnSageInt.exportSalesOrder; if (createTrans_SalesInvoice) createTrans_SalesInvoice = !dfnSageInt.importProductInvoice && !dfnSageInt.exportProductInvoice && !dfnSageInt.importServiceInvoice && !dfnSageInt.exportServiceInvoice && !dfnSageInt.importSalesOrderInvoice && !dfnSageInt.bexport_SalesOrderInvoice; if (createTransaction_PurchaseOrder) createTransaction_PurchaseOrder = !dfnQBooksInt.importPurchaseOrder && !dfnQBooksInt.exportPurchaseOrder; if (createTransaction_SalesOrder) createTransaction_SalesOrder = !dfnQBooksInt.importSalesOrder && !dfnQBooksInt.exportSalesOrder; if (createTrans_SalesInvoice) createTrans_SalesInvoice = !dfnQBooksInt.importProductInvoice && !dfnQBooksInt.exportProductInvoice && !dfnQBooksInt.importServiceInvoice && !dfnQBooksInt.exportServiceInvoice && !dfnQBooksInt.importSalesOrderInvoice && !dfnQBooksInt.bexport_SalesOrderInvoice; // Sales quotes are also imported from Sage Sales Orders. createTransaction_SalesQuote = createTransaction_SalesOrder; } switch (type) { case Sbm.TransactionType.SalesQuote: return createTransaction_SalesQuote; case Sbm.TransactionType.SalesOrder: return createTransaction_SalesOrder; case Sbm.TransactionType.SalesInvoice: return createTrans_SalesInvoice; case Sbm.TransactionType.PurchaseOrder: return createTransaction_PurchaseOrder; } return true; } //------------------------------------------------------------------- // Transaction Creation options. public enum AssignTransRefMethod { System, Manual, Option } public virtual AssignTransRefMethod TransactionAssignRefMethod(Sbm.TransactionType transTypes) { return AssignTransRefMethod.System; } public enum AssignTransRefSource { ByOwnerType, ByType } public virtual AssignTransRefSource TransactionAssignRefSource(Sbm.TransactionType transTypes) { return AssignTransRefSource.ByOwnerType; } // By default all types of transaction have their own Id sequence. // Override this method to allow different types of transaction to share Id sequence. public virtual Sbm.TransactionType GetTransactionTypeForNextId(Sbm.TransactionType transTypes) { return transTypes; } // NextId key value. public virtual string GetTransactionNextIdKey(SysId AffiliateId_Owner, SysId idAssoc, Sbm.TransactionType eType) { idAssoc = new SysId(); Sbm.TransactionType transTypes = GetTransactionTypeForNextId(eType); switch (TransactionAssignRefSource(eType)) { case AssignTransRefSource.ByOwnerType: // Next Id depends on both the transaction type and owner. return "TransRef:" + transTypes.ToString() + ":" + AffiliateId_Owner.ToString(); default: // Next Id only depends on the transaction type. return "TransRef:" + transTypes.ToString(); } } // Set the minimum Id for transactions of the specified type. public virtual bool SetMinTransactionId(SysId AffiliateId_Owner, SysId idAssoc, Sbm.TransactionType transTypes, long iId) { bool set = false; if (SbmGlobals.Application.GetBusMgrDatabase(SessionContext) != null) { string nextIdKey = GetTransactionNextIdKey(AffiliateId_Owner, idAssoc, transTypes); // ToDo set = SbmGlobals.Database.SetMinNextId(nextIdKey, iId, false); } return set; } // The implementation in this class will generate simple numeric values for the owner // and global options that can be formatted (eg. to fixed length) by overwridden versions. public virtual string GetTransactionCreateRef (SysId AffiliateId_Owner, SysId idAssoc, Sbm.TransactionType transTypes) { string Ref = ""; if (SbmGlobals.Application.GetBusMgrDatabase(SessionContext) != null) { string NextIdKey = GetTransactionNextIdKey(AffiliateId_Owner, idAssoc, transTypes); // ToDo long iRefNext = SbmGlobals.Database.GetNextId(NextIdKey, false); // Ref = FormatTransactionCreateRef(AffiliateId_Owner, idAssoc, transTypes, iRefNext); } return Ref; } public virtual string FormatTransactionCreateRef (SysId AffiliateId_Owner, SysId idAssoc, Sbm.TransactionType transTypes, long RefInt) { string Ref = ""; if (RefInt > 0) { // Retrieve and validate the sequence padding length. int SeqLen = Math.Max(1, Math.Min(8, GetTransactionRefSeqLength(AffiliateId_Owner, idAssoc, transTypes))); // Pad the sequence number. //Ref = String(RefInt, SeqLen); // Add the reference prefix (max 5 chars) string prefix = GetTransactionRefPrefix(AffiliateId_Owner, idAssoc, transTypes); Ref = prefix.Left(5) + Ref; } return Ref; } public virtual int GetTransactionRefSeqLength(SysId AffiliateId_Owner, SysId idAssoc, Sbm.TransactionType transTypes) { return 5; } //------------------------------------------------------------------- /// Transaction reference prefix by type public virtual string GetTransactionRefPrefix(SysId id, SysId idAssoc, Sbm.TransactionType transactionType) { return ""; } //------------------------------------------------------------------- // Raise Transaction. public virtual bool CanRaiseTransaction(Sbm.TransactionType typeFrom, Sbm.TransactionType typeTo) { if (!AllowTransactionType(typeFrom) || !AllowTransactionType(typeTo)) return false; if (!CanCreateTransactionLocally(typeTo)) return false; if (typeFrom == Sbm.TransactionType.Project) { // Can raise another project from a project. switch (typeTo) { case Sbm.TransactionType.Project: return CanCreateTransactionLocally(typeTo); } } else if (typeFrom == Sbm.TransactionType.SalesLead) { // Can raise sales quote or order from a lead. switch (typeTo) { case Sbm.TransactionType.SalesQuote: case Sbm.TransactionType.ProformaSalesOrder: case Sbm.TransactionType.SalesOrder: return CanCreateTransactionLocally(typeTo); } } else if (typeFrom == Sbm.TransactionType.SalesQuote) { // Can raise sales lead or order from a sales quote. switch (typeTo) { case Sbm.TransactionType.SalesLead: case Sbm.TransactionType.SalesQuote: case Sbm.TransactionType.ProformaSalesOrder: case Sbm.TransactionType.SalesOrder: case Sbm.TransactionType.ProformaSalesInvoice: case Sbm.TransactionType.SalesInvoice: return CanCreateTransactionLocally(typeTo); } } else if (typeFrom == Sbm.TransactionType.SalesOrder || typeFrom == Sbm.TransactionType.ProformaSalesOrder) { // Can raise almost anything from a sales order. switch (typeTo) { case Sbm.TransactionType.SalesLead: case Sbm.TransactionType.SalesQuote: case Sbm.TransactionType.ProformaSalesOrder: case Sbm.TransactionType.SalesOrder: case Sbm.TransactionType.SalesDispatch: case Sbm.TransactionType.ProformaSalesInvoice: case Sbm.TransactionType.SalesInvoice: // Can also raise purchase quote or order from sales order: case Sbm.TransactionType.PurchaseQuote: case Sbm.TransactionType.ProformaPurchaseOrder: case Sbm.TransactionType.PurchaseOrder: return CanCreateTransactionLocally(typeTo); } } else if (typeFrom == Sbm.TransactionType.SalesDispatch) { // Can raise a sales invoice from a dispatch note. switch (typeTo) { case Sbm.TransactionType.ProformaSalesInvoice: case Sbm.TransactionType.SalesInvoice: return CanCreateTransactionLocally(typeTo); } } else if (typeFrom == Sbm.TransactionType.PurchaseQuote) { // Can raise a purchase order from purchase quote. switch (typeTo) { case Sbm.TransactionType.PurchaseQuote: case Sbm.TransactionType.ProformaPurchaseOrder: case Sbm.TransactionType.PurchaseOrder: return CanCreateTransactionLocally(typeTo); } } else if (typeFrom == Sbm.TransactionType.PurchaseOrder || typeFrom == Sbm.TransactionType.ProformaPurchaseOrder) { // Can raise a sales order or purchase quote from a purchase order. switch (typeTo) { case Sbm.TransactionType.SalesQuote: case Sbm.TransactionType.ProformaSalesOrder: case Sbm.TransactionType.SalesOrder: case Sbm.TransactionType.PurchaseQuote: case Sbm.TransactionType.ProformaPurchaseOrder: case Sbm.TransactionType.PurchaseOrder: return CanCreateTransactionLocally(typeTo); } } return false; } //------------------------------------------------------------------- public virtual List GetTransactionTypesForRaiseFrom(Sbm.TransactionType typeFrom) { // Build the list of transaction types that are allowed to be raised from the specified type. List transTypes = new List(); if (transactionTypes_Full.Count == 0) transactionTypes_Full = internals.GetSystemCodes(Sbm.SystemCode.TransactionType); foreach (TypeCodeDesc transTypeTo in transactionTypes_Full) { //if ((Sbm.TransactionType)typeTo.iType == typeFrom) continue; if (CanRaiseTransaction(typeFrom, (Sbm.TransactionType)transTypeTo.Type)) transTypes.Add(transTypeTo); } return transTypes; } //------------------------------------------------------------------- public virtual List GetTransactionTypesForRaiseTo(Sbm.TransactionType typeTo) { // Build the list of transaction types that are allowed to be raised to the specified type. List transTypes = new List(); if (transactionTypes_Full.Count == 0) transactionTypes_Full = internals.GetSystemCodes(Sbm.SystemCode.TransactionType); //transTypes_Full.GotoStart(); foreach (TypeCodeDesc transTypeTo in transactionTypes_Full) { //TypeCodeDesc & typeTo = transTypes_Full.Index(); if ((Sbm.TransactionType)transTypeTo.Type == typeTo) continue; if (CanRaiseTransaction((Sbm.TransactionType)transTypeTo.Type, typeTo)) transTypes.Add(transTypeTo); } return transTypes; } //------------------------------------------------------------------- // Prepare Requirements Transaction. - Prepare copied list of requirements, eg. when raising from another transaction. public virtual bool PrepareCopiedRequirements(SbmTransaction transSource, List requireList, Sbm.TransactionType type) { type = new Sbm.TransactionType(); //transSource; requireList; type; return false; } //------------------------------------------------------------------- // Remove Transaction.- By default, not allowed if transaction has requirements. public virtual bool CanRemoveTransactionWithRequirements(SbmTransaction transaction, int requirementCount) { if (transaction == null || !SysId.IsValid(transaction.Id)) return false; return requirementCount < 1; } //------------------------------------------------------------------- // Tax Effective Date. - TransTypes that should use the 'In Progress' date as tax point as opposed to the 'Raised' date. public virtual bool UseInProgressTaxDateAsTaxPoint(Sbm.TransactionType transactionType) { switch (transactionType) { case Sbm.TransactionType.ProformaSalesOrder: case Sbm.TransactionType.SalesDispatch: case Sbm.TransactionType.ProformaPurchaseOrder: case Sbm.TransactionType.PurchaseDelivery: case Sbm.TransactionType.ProformaSalesInvoice: return false; } return true; } //------------------------------------------------------------------- // R e q u i r e m e n t O p t i o n s //------------------------------------------------------------------- // Requirement Actions. public virtual bool SearchCommodityOnCreateRequirement(Sbm.TransactionType transactionType) { return false; } //------------------------------------------------------------------- // Requirement Costing. public virtual SbmRequirementCostingConfig GetRequirementCostingConfig() { SbmRequirementCostingConfig cfgRequirementCosting = new SbmRequirementCostingConfig(); return cfgRequirementCosting; } //------------------------------------------------------------------- // Transaction Maintenance Dialogs. /* public SapDlgUpdateTransaction GetCreateTransactionDlg (ISbmUserContext userContext, SysId id, Sbm.TransactionType type) { return null; } public SapDlgUpdateTransaction GetUpdateTransactionDlg (ISbmUserContext userContext, Sbm.TransactionType type, bool readOnly) { return null; } */ //------------------------------------------------------------------- } //======================================================================= }