Exemple #1
0
        private void InitAccountEdit(IAccount acct, IOrg org)
        {
            AccountEdit acctEdit = new AccountEdit();

            // null means adding a new account

            if (acct != null)
            {
                acctEdit.OrgID            = acct.OrgID;
                acctEdit.AccountID        = acct.AccountID;
                acctEdit.AccountName      = acct.AccountName;
                acctEdit.AccountNumber    = acct.AccountNumber;
                acctEdit.AccountTypeID    = acct.AccountTypeID;
                acctEdit.FundingSourceID  = acct.FundingSourceID;
                acctEdit.Managers         = AccountEditUtility.GetManagerEdits(acct.AccountID);
                acctEdit.ShortCode        = acct.ShortCode.Trim();
                acctEdit.SpecialTopicID   = acct.SpecialTopicID;
                acctEdit.TechnicalFieldID = acct.TechnicalFieldID;
                acctEdit.InvoiceNumber    = acct.InvoiceNumber;
                acctEdit.InvoiceLine1     = acct.InvoiceLine1;
                acctEdit.InvoiceLine2     = acct.InvoiceLine2;
                acctEdit.PoEndDate        = acct.PoEndDate;
                acctEdit.PoInitialFunds   = acct.PoInitialFunds;
                acctEdit.PoRemainingFunds = acct.PoRemainingFunds;

                acctEdit.Addresses = new Dictionary <string, AddressEdit>
                {
                    { "billing", AccountEditUtility.GetAddressEdit(acct.BillAddressID) },
                    { "shipping", AccountEditUtility.GetAddressEdit(acct.ShipAddressID) }
                };

                if (AccountChartFields.IsChartFieldOrg(org))
                {
                    var cf = ServiceProvider.Current.Data.Account.GetChartFields(acct);
                    acctEdit.ChartFields = new AccountChartFieldEdit()
                    {
                        Account    = cf.Account,
                        Fund       = cf.Fund,
                        Department = cf.Department,
                        Program    = cf.Program,
                        Class      = cf.Class,
                        Project    = cf.Project,
                        ShortCode  = cf.ShortCode
                    };
                }
            }
            else
            {
                acctEdit.OrgID     = org.OrgID;
                acctEdit.Managers  = new List <AccountManagerEdit>();
                acctEdit.Addresses = new Dictionary <string, AddressEdit>
                {
                    { "billing", AccountEditUtility.GetAddressEdit(org.DefBillAddressID) },
                    { "shipping", AccountEditUtility.GetAddressEdit(org.DefShipAddressID) }
                };
                acctEdit.AccountTypeID = 1;

                if (AccountChartFields.IsChartFieldOrg(org))
                {
                    acctEdit.ChartFields = new AccountChartFieldEdit();
                }
            }

            Session["AccountEdit"] = acctEdit;
        }
Exemple #2
0
        public ActionResult Update(int orgId)
        {
            // save all changes
            if (Session["AccountEdit"] != null)
            {
                var acctEdit = (AccountEdit)Session["AccountEdit"];

                Account acct;
                bool    insert = false;

                if (acctEdit.AccountID > 0)
                {
                    acct = DataSession.Single <Account>(acctEdit.AccountID);
                }
                else
                {
                    acct = new Account {
                        Org = DataSession.Single <Org>(orgId)
                    };
                    insert = true;
                }

                if (acct != null)
                {
                    acct.Name             = acctEdit.AccountName;
                    acct.Number           = AccountEditUtility.GetAccountNumber(acctEdit);
                    acct.ShortCode        = AccountEditUtility.GetShortCode(acctEdit);
                    acct.FundingSourceID  = acctEdit.FundingSourceID;
                    acct.TechnicalFieldID = acctEdit.TechnicalFieldID;
                    acct.SpecialTopicID   = acctEdit.SpecialTopicID;
                    acct.AccountType      = DataSession.Single <AccountType>(acctEdit.AccountTypeID);
                    acct.InvoiceNumber    = acctEdit.InvoiceNumber;
                    acct.InvoiceLine1     = acctEdit.InvoiceLine1;
                    acct.InvoiceLine2     = acctEdit.InvoiceLine2;
                    acct.PoEndDate        = acctEdit.PoEndDate;
                    acct.PoInitialFunds   = acctEdit.PoInitialFunds;

                    // handle addresses
                    foreach (var kvp in acctEdit.Addresses)
                    {
                        if (kvp.Value != null)
                        {
                            Address addr;
                            bool    insertAddr = false;

                            if (kvp.Value.AddressID == 0)
                            {
                                addr       = new Address();
                                insertAddr = true;
                            }
                            else
                            {
                                addr = DataSession.Single <Address>(kvp.Value.AddressID);
                            }

                            addr.InternalAddress = kvp.Value.Attention;
                            addr.StrAddress1     = kvp.Value.AddressLine1;
                            addr.StrAddress2     = kvp.Value.AddressLine2;
                            addr.City            = kvp.Value.City;
                            addr.State           = kvp.Value.State;
                            addr.Zip             = kvp.Value.Zip;
                            addr.Country         = kvp.Value.Country;

                            if (insertAddr)
                            {
                                DataSession.Insert(addr);
                            }

                            if (kvp.Key == "billing")
                            {
                                acct.BillAddressID = addr.AddressID;
                            }

                            if (kvp.Key == "shipping")
                            {
                                acct.ShipAddressID = addr.AddressID;
                            }
                        }
                        else
                        {
                            if (kvp.Key == "billing")
                            {
                                if (acct.BillAddressID > 0)
                                {
                                    DataSession.Delete(DataSession.Single <Address>(acct.BillAddressID));
                                }
                                acct.BillAddressID = 0;
                            }
                            if (kvp.Key == "shipping")
                            {
                                if (acct.ShipAddressID > 0)
                                {
                                    DataSession.Delete(DataSession.Single <Address>(acct.ShipAddressID));
                                }
                                acct.ShipAddressID = 0;
                            }
                        }
                    }

                    if (insert)
                    {
                        DataSession.Insert(acct);
                        Provider.Data.ActiveLog.Enable(acct);
                    }

                    // handle managers
                    var currentManagers = AccountEditUtility.GetManagerEdits(acct.AccountID).ToList();

                    foreach (var mgr in acctEdit.Managers)
                    {
                        if (!currentManagers.Any(x => x.ClientOrgID == mgr.ClientOrgID))
                        {
                            // adding a new manager

                            // check for an existing ClientAccount to make a manager and reactivate if needed
                            ClientAccount ca;

                            ca = DataSession.Query <ClientAccount>().FirstOrDefault(x => x.ClientOrg.ClientOrgID == mgr.ClientOrgID && x.Account == acct);

                            if (ca != null)
                            {
                                ca.Manager = true;
                                if (!ca.Active)
                                {
                                    Provider.Data.ActiveLog.Enable(ca);
                                }
                            }
                            else
                            {
                                ca = new ClientAccount()
                                {
                                    ClientOrg = DataSession.Single <ClientOrg>(mgr.ClientOrgID),
                                    Account   = acct,
                                    Manager   = true,
                                    IsDefault = false
                                };

                                DataSession.Insert(ca);

                                Provider.Data.ActiveLog.Enable(ca);
                            }

                            currentManagers.Add(new AccountManagerEdit()
                            {
                                ClientOrgID = ca.ClientOrg.ClientOrgID,
                                FName       = ca.ClientOrg.Client.FName,
                                LName       = ca.ClientOrg.Client.LName
                            });
                        }
                    }

                    // now check for any deleted managers
                    foreach (var mgr in currentManagers.ToArray())
                    {
                        if (!acctEdit.Managers.Any(x => x.ClientOrgID == mgr.ClientOrgID))
                        {
                            // a current manager was deleted

                            ClientAccount ca = DataSession.Query <ClientAccount>()
                                               .FirstOrDefault(x => x.ClientOrg.ClientOrgID == mgr.ClientOrgID && x.Account == acct);

                            if (ca != null)
                            {
                                RemoveManager(ca);
                                currentManagers.Remove(mgr);
                            }
                        }
                    }
                }

                Session.Remove("AccountEdit");
            }

            return(RedirectToAction("Index", new { orgId }));
        }