示例#1
0
        public string EnableClientAccount()
        {
            ClientAccount ca = GetClientAccount();

            if (ca == null)
            {
                //No record exists yet so this must be the first association of this client and account.
                ca = new ClientAccount
                {
                    ClientOrg = DataSession.Single <ClientOrg>((int)UserClientOrgID),
                    Account   = DataSession.Single <Account>((int)AccountID),
                    Manager   = false,
                    IsDefault = false
                };

                // Need to save to get the ClientAccountID.
                DataSession.Insert(ca);
            }

            Provider.Data.ActiveLog.Enable(ca);

            IClient c = DataSession.Single <ClientInfo>(ca.ClientOrg.Client.ClientID);

            Provider.Data.Client.UpdatePhysicalAccess(c, out string alert);

            //A final check...
            if (ca.ClientOrg.ClientOrgID != UserClientOrgID)
            {
                throw new Exception(string.Format("EnableClientAccount failed. Expected ClientOrgID: {0}, Actual ClientOrgID: {1}", UserClientOrgID, ca.ClientOrg.ClientOrgID));
            }

            return(alert);
        }
示例#2
0
        public Order Add(int clientId, int vendorId, int?accountId, int approverId, DateTime neededDate, bool oversized, int shippingMethodId, string notes, bool attention)
        {
            /*
             * INSERT INTO dbo.PurchaseOrder (ClientID, AccountID, VendorID, CreatedDate, NeededDate, ApproverID, Oversized, ShippingMethodID, Notes, Attention, StatusID)
             * VALUES (@ClientID, @AccountID, @VendorID, @CreatedDate, @NeededDate, @ApproverID, @Oversized, @ShippingMethodID, @Notes, @Attention, 1)
             *
             * 1 = Status.Draft
             */

            var po = new Ordering.PurchaseOrder()
            {
                Client         = Require <Data.Client>(x => x.ClientID, clientId),
                AccountID      = accountId,
                Vendor         = Require <Ordering.Vendor>(x => x.VendorID, vendorId),
                CreatedDate    = DateTime.Now,
                NeededDate     = neededDate,
                Approver       = Require <Data.Client>(x => x.ClientID, approverId),
                Oversized      = oversized,
                ShippingMethod = Require <Ordering.ShippingMethod>(x => x.ShippingMethodID, shippingMethodId),
                Notes          = notes,
                Attention      = attention,
                Status         = GetStatus(OrderStatus.Draft),
                Details        = new List <Ordering.PurchaseOrderDetail>()
            };

            DataSession.Insert(po);

            Tracking.Track(TrackingCheckpoints.DraftCreated, po.CreateModel <IPurchaseOrder>(), clientId, new { VendorID = vendorId, AccountID = accountId, ApproverID = approverId });

            return(CreateOrder(po));
        }
        public ClientModel Post([FromUri] string option, [FromBody] ClientModel model, int id)
        {
            ClientModel result = null;

            switch (option)
            {
            case "current":
                ClientOrg co = DataSession.Single <ClientOrg>(id);
                ClientOrg mo = DataSession.Single <ClientOrg>(model.ClientOrgID);

                if (co != null && mo != null)
                {
                    var cm = DataSession.Query <ClientManager>().FirstOrDefault(x => x.ClientOrg == co && x.ManagerOrg == mo);

                    if (cm == null)
                    {
                        //no existing ClientManager record so create a new one
                        cm = new ClientManager()
                        {
                            ClientOrg  = co,
                            ManagerOrg = mo
                        };

                        DataSession.Insert(cm);
                    }

                    Provider.Data.ActiveLog.Enable(cm);

                    result = ApiUtility.CreateClientModel(cm.ManagerOrg.CreateModel <IClient>());
                }
                break;
            }

            return(result);
        }
示例#4
0
        public int AddCategory(int parentId, string categoryName, string categoryNumber)
        {
            var existing = DataSession.Query <Ordering.PurchaseOrderCategory>().FirstOrDefault(x => x.ParentID == parentId && x.CatNo == categoryNumber);

            if (existing != null)
            {
                if (existing.Active)
                {
                    throw new Exception($"A category already exists with category number: {categoryNumber}");
                }
                else
                {
                    existing.Active = true;
                    return(existing.CatID);
                }
            }

            var cat = new Ordering.PurchaseOrderCategory()
            {
                ParentID = parentId,
                CatName  = categoryName,
                CatNo    = categoryNumber,
                Active   = true
            };

            DataSession.Insert(cat);

            return(cat.CatID);
        }
示例#5
0
        public Purchaser AddOrUpdatePurchaser(int clientId, bool active)
        {
            var client = Require <Data.ClientInfo>(x => x.ClientID, clientId);

            var purch = DataSession.Query <Ordering.Purchaser>().FirstOrDefault(x => x.Client.ClientID == clientId);

            if (purch == null)
            {
                purch = new Ordering.Purchaser()
                {
                    Client  = Require <Data.Client>(x => x.ClientID, clientId),
                    Active  = active,
                    Deleted = false
                };

                DataSession.Insert(purch);
            }
            else
            {
                purch.Active  = active;
                purch.Deleted = false;
            }

            return(CreatePurchaser(purch, client));
        }
示例#6
0
 private Approver AddApprover(Ordering.Approver appr, bool isPrimary)
 {
     if (appr == null)
     {
         return(null);
     }
     appr.Active = true;
     DataSession.Insert(appr);
     SetPrimary(appr, isPrimary);
     return(CreateApprover(appr));
 }
示例#7
0
        public Vendor Add(int clientId, string vendorName, string address1, string address2, string address3, string contact, string phone, string fax, string url, string email)
        {
            var vend1 = new Ordering.Vendor()
            {
                ClientID   = clientId,
                VendorName = vendorName,
                Address1   = address1,
                Address2   = address2,
                Address3   = address3,
                Contact    = contact,
                Phone      = phone,
                Fax        = fax,
                URL        = url,
                Email      = email,
                Active     = true
            };

            DataSession.Insert(vend1);

            if (clientId > 0 && AutoAddStoreManagerVendor())
            {
                // check for a store manager vendor with same name
                var storeManagerVendor = DataSession.Query <Ordering.Vendor>().Where(x => x.VendorName == vendorName && x.ClientID == 0).FirstOrDefault();

                if (storeManagerVendor == null)
                {
                    // add a store manager vendor if the name was not found

                    var vend2 = new Ordering.Vendor()
                    {
                        ClientID   = 0,
                        VendorName = vendorName,
                        Address1   = address1,
                        Address2   = address2,
                        Address3   = address3,
                        Contact    = contact,
                        Phone      = phone,
                        Fax        = fax,
                        URL        = url,
                        Email      = email,
                        Active     = true
                    };

                    DataSession.Insert(vend2);
                }
            }

            return(CreateVendor(vend1));
        }
示例#8
0
        private Data.Client GetApproverForCopy(Ordering.PurchaseOrder po)
        {
            var currentClientId = Context.CurrentUser.ClientID;

            Data.Client approver;

            if (po.Client.ClientID != currentClientId)
            {
                // check if the current user has the same approver
                var app = DataSession.Query <Ordering.Approver>().FirstOrDefault(x => x.ApproverID == po.Approver.ClientID && x.ClientID == currentClientId);

                if (app == null)
                {
                    // use the current user's primary approver
                    var defapp = DataSession.Query <Ordering.Approver>().FirstOrDefault(x => x.Active && x.ClientID == currentClientId && x.IsPrimary);

                    if (defapp == null)
                    {
                        // fall-back: copy the po approver
                        app = new Ordering.Approver()
                        {
                            Active     = true,
                            ApproverID = po.Approver.ClientID,
                            ClientID   = currentClientId,
                            IsPrimary  = true //because the current user has no active primary at the moment
                        };

                        DataSession.Insert(app);

                        approver = po.Approver;
                    }
                    else
                    {
                        approver = Require <Data.Client>(x => x.ClientID, defapp.ApproverID);
                    }
                }
                else
                {
                    app.Active = true; //just in case
                    approver   = Require <Data.Client>(x => x.ClientID, app.ApproverID);
                }
            }
            else
            {
                approver = po.Approver;
            }

            return(approver);
        }
示例#9
0
        private Ordering.Vendor GetVendorForCopy(Ordering.PurchaseOrder po)
        {
            var currentClientId = Context.CurrentUser.ClientID;

            Ordering.Vendor vendor;

            if (po.Client.ClientID == currentClientId || po.Vendor.ClientID == 0)
            {
                // current user is copying own order, or store manager order
                vendor = po.Vendor;
            }
            else
            {
                // current user is copying another user's order

                // check for a vendor for the current user that has the same name as the po vendor
                vendor = DataSession.Query <Ordering.Vendor>().Where(x => x.ClientID == currentClientId).ToList().FirstOrDefault(x =>
                                                                                                                                 PurchaseOrderItems.CleanString(x.VendorName) == PurchaseOrderItems.CleanString(po.Vendor.VendorName));

                if (vendor == null)
                {
                    // make a copy
                    vendor = new Ordering.Vendor()
                    {
                        Active     = true,
                        Address1   = po.Vendor.Address1,
                        Address2   = po.Vendor.Address2,
                        Address3   = po.Vendor.Address3,
                        ClientID   = currentClientId,
                        Contact    = po.Vendor.Contact,
                        Email      = po.Vendor.Email,
                        Fax        = po.Vendor.Fax,
                        Items      = new List <Ordering.PurchaseOrderItem>(),
                        Phone      = po.Vendor.Phone,
                        URL        = po.Vendor.URL,
                        VendorName = po.Vendor.VendorName
                    };

                    DataSession.Insert(vendor);
                }
                else
                {
                    vendor.Active = true; //just in case
                }
            }

            return(vendor);
        }
示例#10
0
        public Item Add(string partNum, string description, double unitPrice, int vendorId, int?inventoryItemId)
        {
            var item = new Ordering.PurchaseOrderItem()
            {
                Active          = true,
                Description     = description,
                InventoryItemID = inventoryItemId,
                PartNum         = partNum,
                UnitPrice       = unitPrice,
                Vendor          = DataSession.Single <Ordering.Vendor>(vendorId)
            };

            DataSession.Insert(item);

            return(CreateItem(item));
        }
示例#11
0
        public Detail Add(int poid, int itemId, int catId, double qty, string unit, double unitPrice)
        {
            var item = Require <Ordering.PurchaseOrderItem>(x => x.ItemID, itemId);

            var pod = new Ordering.PurchaseOrderDetail()
            {
                PurchaseOrder         = Require <Ordering.PurchaseOrder>(x => x.POID, poid),
                Item                  = item,
                Category              = Require <Ordering.PurchaseOrderCategory>(x => x.CatID, catId),
                IsInventoryControlled = item.InventoryItemID.HasValue,
                Quantity              = qty,
                Unit                  = unit,
                UnitPrice             = unitPrice
            };

            DataSession.Insert(pod);

            return(CreateDetail(pod));
        }
示例#12
0
        public IEnumerable <Item> Copy(int toVendorId, int fromVendorId)
        {
            var toVendor   = Require <Ordering.Vendor>(x => x.VendorID, toVendorId);
            var fromVendor = Require <Ordering.Vendor>(x => x.VendorID, fromVendorId);

            var items = fromVendor.Items.Select(x => new Ordering.PurchaseOrderItem()
            {
                Active          = true,
                Description     = x.Description,
                InventoryItemID = x.InventoryItemID,
                PartNum         = x.PartNum,
                UnitPrice       = x.UnitPrice,
                Vendor          = toVendor
            }).ToList();

            DataSession.Insert(items);

            // return the new items
            return(CreateItems(items.AsQueryable()));
        }
示例#13
0
        public Order Copy(int poid, int?accountId = null)
        {
            //INSERT INTO dbo.PurchaseOrder (ClientID, AccountID, VendorID, CreatedDate, NeededDate, ApproverID, Oversized, ShippingMethodID, Notes, Attention, StatusID)
            //SELECT ClientID, AccountID, VendorID, GETDATE(), DATEADD(DAY, 7, GETDATE()), ApproverID, Oversized, ShippingMethodID, Notes, Attention, 1

            // get the po to be copied
            var po = Require <Ordering.PurchaseOrder>(x => x.POID, poid);

            // po may be for a different user than current, this will get the correct vendor and approver - making copies if necessary
            var vendor   = GetVendorForCopy(po);
            var approver = GetApproverForCopy(po);

            var copy = new Ordering.PurchaseOrder()
            {
                Client         = Require <Data.Client>(x => x.ClientID, Context.CurrentUser.ClientID),
                AccountID      = accountId ?? po.AccountID,
                Vendor         = vendor,
                CreatedDate    = DateTime.Now,
                NeededDate     = DateTime.Now.AddDays(7),
                Approver       = approver,
                Oversized      = po.Oversized,
                ShippingMethod = po.ShippingMethod,
                Notes          = po.Notes,
                Attention      = po.Attention,
                Status         = GetStatus(OrderStatus.Draft)
            };

            DataSession.Insert(copy);

            // po may be for a different user than current, this will get the correct details - making item copies if necessary
            var details = GetDetailsForCopy(po, copy, vendor);

            DataSession.Insert(details);

            copy.Details = details;

            Tracking.Track(TrackingCheckpoints.DraftCreated, copy.CreateModel <IPurchaseOrder>(), Context.CurrentUser.ClientID);

            return(CreateOrder(copy));
        }
示例#14
0
        public Account AddOrUpdate(int clientId, int accountId)
        {
            var acct = new Ordering.PurchaseOrderAccount {
                AccountID = accountId, ClientID = clientId
            };

            var existing = DataSession.Single <Ordering.PurchaseOrderAccount>(acct);

            if (existing == null)
            {
                //insert new
                acct.Active = true;
                DataSession.Insert(acct);
                return(CreateAccount(acct));
            }
            else
            {
                //update existing
                existing.Active = true;
                return(CreateAccount(existing));
            }
        }
示例#15
0
        public Vendor Copy(int toClientId, int fromVendorId)
        {
            var vend = Require <Ordering.Vendor>(x => x.VendorID, fromVendorId);

            var copy = new Ordering.Vendor()
            {
                ClientID   = toClientId,
                VendorName = vend.VendorName,
                Address1   = vend.Address1,
                Address2   = vend.Address2,
                Address3   = vend.Address3,
                Contact    = vend.Contact,
                Phone      = vend.Phone,
                Fax        = vend.Fax,
                URL        = vend.URL,
                Email      = vend.Email,
                Active     = vend.Active
            };

            DataSession.Insert(copy);

            return(CreateVendor(copy));
        }
示例#16
0
        public AccountModel Post([FromUri] string option, [FromBody] AccountModel model, int id)
        {
            AccountModel result = null;

            switch (option)
            {
            case "current":
                var ca = DataSession.Query <ClientAccount>().FirstOrDefault(x => x.ClientOrg.ClientOrgID == id && x.Account.AccountID == model.AccountID);

                if (ca == null)
                {
                    //no existing ClientAccount record so create a new one
                    ca = new ClientAccount()
                    {
                        ClientOrg = DataSession.Single <ClientOrg>(id),
                        Account   = DataSession.Single <Account>(model.AccountID),
                        IsDefault = false,
                        Manager   = false
                    };

                    DataSession.Insert(ca);
                }

                Provider.Data.ActiveLog.Enable(ca);

                //may need to restore physical access because there is now an active acct and other requirements are met
                string  alert;
                IClient c = DataSession.Single <ClientInfo>(ca.ClientOrg.Client.ClientID);
                Provider.Data.Client.UpdatePhysicalAccess(c, out alert);

                result = ApiUtility.CreateAccountModel(ca.Account);

                break;
            }

            return(result);
        }
示例#17
0
        public override bool Save()
        {
            int errors = 0;

            Message = string.Empty;

            if (string.IsNullOrEmpty(OrgName))
            {
                Message += GetAlert("Name is required.");
                errors++;
            }

            Org existingOrg = DataSession.Query <Org>().Where(x => x.OrgName == OrgName).FirstOrDefault();

            //three possibilities: 1) no org with this name exists, 2) existing is the same as this org, 3) existing is different
            if (existingOrg != null && existingOrg.OrgID != OrgID)
            {
                //there is an existing (and different) org with the same name
                Message += GetAlert("This name is already used by an {0} org.", existingOrg.Active ? "active" : "inactive");
                errors++;
            }

            OrgType orgType = null;

            if (OrgTypeID == 0)
            {
                Message += GetAlert("Type is required.");
                errors++;
            }
            else
            {
                orgType = DataSession.Single <OrgType>(OrgTypeID);
                if (orgType == null)
                {
                    Message += GetAlert("No record found for OrgTypeID {0}", OrgTypeID);
                    errors++;
                }
            }

            Org primary = null;

            if (CanEditPrimaryOrg())
            {
                primary = GetPrimaryOrg();
                if (PrimaryOrg)
                {
                    if (!Active)
                    {
                        Message += GetAlert("The primary org must be active.");
                        errors++;
                    }
                }
                else
                {
                    //make sure there is a primary org
                    if (primary == null)
                    {
                        Message += GetAlert("There must be at least one primary org.");
                        errors++;
                    }
                }
            }

            if (errors > 0)
            {
                return(false);
            }

            Org  org;
            bool originalActive = false;

            if (OrgID == 0)
            {
                //new record
                org = new Org()
                {
                    DefBillAddressID   = 0,
                    DefClientAddressID = 0,
                    DefShipAddressID   = 0,
                    NNINOrg            = NNINOrg,
                    OrgName            = OrgName,
                    OrgType            = orgType,
                    PrimaryOrg         = CanEditPrimaryOrg() && PrimaryOrg
                };

                DataSession.Insert(org); // gets a new OrgID
            }
            else
            {
                org = DataSession.Single <Org>(OrgID);

                if (org == null)
                {
                    Message += GetAlert("No record found for OrgID {0}", OrgID);
                    return(false);
                }

                originalActive = org.Active;

                org.NNINOrg    = NNINOrg;
                org.OrgName    = OrgName;
                org.OrgType    = orgType;
                org.PrimaryOrg = CanEditPrimaryOrg() ? PrimaryOrg : org.PrimaryOrg;
            }

            if (originalActive != Active)
            {
                if (Active)
                {
                    Provider.Data.ActiveLog.Enable(org);
                }
                else
                {
                    Provider.Data.ActiveLog.Disable(org);

                    //need to disable any clients where this was the only active org
                    var clientOrgs = Provider.Data.Client.GetClientOrgs(org.OrgID).Where(x => x.ClientActive);

                    foreach (var co in clientOrgs)
                    {
                        //does this ClientOrg have any other active associations?
                        bool hasAnotherActiveClientOrg = DataSession.Query <ClientOrg>().Any(x => x.Active && x.Client.ClientID == co.ClientID && x.Org.OrgID != co.OrgID);
                        if (!hasAnotherActiveClientOrg)
                        {
                            //no other active ClientOrgs so disable the Client record also
                            var c = DataSession.Single <Client>(co.ClientID);
                            Provider.Data.ActiveLog.Disable(c);
                        }
                    }
                }
            }

            if (CanEditPrimaryOrg() && PrimaryOrg && primary != null)
            {
                primary.PrimaryOrg = false;
                DataSession.SaveOrUpdate(primary);
            }

            OrgID = org.OrgID;

            return(true);
        }
示例#18
0
        public ActionResult Ajax(LNF.Impl.Repository.Data.GlobalSettings model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.SettingName))
                {
                    throw new Exception("Setting name is required.");
                }

                if (model.SettingID == 0)
                {
                    // create new
                    var existing = DataSession.Query <LNF.Impl.Repository.Data.GlobalSettings>().FirstOrDefault(x => x.SettingName == model.SettingName);

                    if (existing != null)
                    {
                        throw new Exception($"A setting with name '{model.SettingName}' already exists.");
                    }

                    DataSession.Insert(new LNF.Impl.Repository.Data.GlobalSettings
                    {
                        SettingName  = model.SettingName,
                        SettingValue = model.SettingValue
                    });
                }
                else
                {
                    var gs = DataSession.Single <LNF.Impl.Repository.Data.GlobalSettings>(model.SettingID);

                    if (gs == null)
                    {
                        throw new Exception($"Cannot find global setting with SettingID = {model.SettingID}");
                    }

                    var existing = DataSession.Query <LNF.Impl.Repository.Data.GlobalSettings>().FirstOrDefault(x => x.SettingName == model.SettingName && x.SettingID != model.SettingID);

                    if (existing != null)
                    {
                        throw new Exception($"A setting with name '{model.SettingName}' already exists.");
                    }

                    gs.SettingName  = model.SettingName;
                    gs.SettingValue = model.SettingValue;

                    DataSession.SaveOrUpdate(gs);
                }

                var items = DataSession.Query <LNF.Impl.Repository.Data.GlobalSettings>();

                return(Json(items));
            }
            catch (Exception ex)
            {
                Response.TrySkipIisCustomErrors = true;
                Response.StatusCode             = 500;
                return(Json(new
                {
                    Message = "An error has occurred.",
                    ExceptionMessage = ex.Message,
                    ExceptionType = ex.GetType().ToString(),
                    ex.StackTrace
                }));
            }
        }
示例#19
0
        private IList <Ordering.PurchaseOrderDetail> GetDetailsForCopy(Ordering.PurchaseOrder po, Ordering.PurchaseOrder copy, Ordering.Vendor vendor)
        {
            var currentClientId = Context.CurrentUser.ClientID;

            IList <Ordering.PurchaseOrderDetail> details;

            if (po.Client.ClientID == currentClientId || po.Vendor.ClientID == 0)
            {
                // current user is copying own order, or store manager vendor
                details = po.Details.Select(x => new Ordering.PurchaseOrderDetail()
                {
                    Category = x.Category,
                    IsInventoryControlled = x.IsInventoryControlled,
                    Item            = x.Item,
                    PurchaseOrder   = copy,
                    Quantity        = x.Quantity,
                    ToInventoryDate = null,
                    Unit            = x.Unit,
                    UnitPrice       = x.UnitPrice
                }).ToList();
            }
            else
            {
                // current user is copying another user's order

                details = new List <Ordering.PurchaseOrderDetail>();

                // check for items for the current user that have the same description and partnum as the po detail items
                foreach (var d in po.Details)
                {
                    var i = vendor.Items.FirstOrDefault(x =>
                                                        PurchaseOrderItems.CleanString(x.Description) == PurchaseOrderItems.CleanString(d.Item.Description) &&
                                                        PurchaseOrderItems.CleanString(x.PartNum) == PurchaseOrderItems.CleanString(d.Item.PartNum));

                    if (i == null)
                    {
                        // make a copy of the item
                        i = new Ordering.PurchaseOrderItem()
                        {
                            Active          = true,
                            Description     = d.Item.Description,
                            Details         = new List <Ordering.PurchaseOrderDetail>(),
                            InventoryItemID = d.Item.InventoryItemID,
                            PartNum         = d.Item.PartNum,
                            UnitPrice       = d.Item.UnitPrice,
                            Vendor          = vendor
                        };

                        DataSession.Insert(i);

                        vendor.Items.Add(i);
                    }
                    else
                    {
                        i.Active = true; //just in case
                    }

                    var detail = new Ordering.PurchaseOrderDetail()
                    {
                        Category = d.Category,
                        IsInventoryControlled = d.IsInventoryControlled,
                        Item            = i,
                        PurchaseOrder   = copy,
                        Quantity        = d.Quantity,
                        ToInventoryDate = null,
                        Unit            = d.Unit,
                        UnitPrice       = d.UnitPrice
                    };

                    details.Add(detail);
                }
            }

            return(details);
        }
示例#20
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 }));
        }
示例#21
0
        protected void SubmitButton_Command(object sender, CommandEventArgs e)
        {
            ShowErrorMessage(string.Empty);

            try
            {
                int             clientId;
                var             selectedAuthLevel = GetSelectedAuthLevel();
                var             resourceId        = GetCurrentResource().ResourceID;
                ClientAuthLevel refreshAuthLevel  = selectedAuthLevel;

                if (e.CommandName == "Authorize")
                {
                    clientId = int.Parse(ClientsDropDownList.SelectedValue);
                    var rc = new ResourceClient()
                    {
                        ResourceID             = resourceId,
                        ClientID               = clientId,
                        AuthLevel              = selectedAuthLevel,
                        Expiration             = null,
                        EmailNotify            = null,
                        PracticeResEmailNotify = null
                    };

                    SetExpiration(rc);

                    DataSession.Insert(rc);
                    ClearResourceClientsCache();

                    var lname    = string.Empty;
                    var fname    = string.Empty;
                    var splitter = ClientsDropDownList.SelectedItem.Text.Split(',');

                    if (splitter.Length > 0)
                    {
                        lname = splitter[0].Trim();
                    }

                    if (splitter.Length > 1)
                    {
                        fname = splitter[1].Trim();
                    }

                    CurrentClients.Add(new ResourceClientItem()
                    {
                        ResourceClientID = rc.ResourceClientID,
                        ClientID         = clientId,
                        AuthLevel        = selectedAuthLevel,
                        LName            = lname,
                        FName            = fname,
                        Expiration       = rc.Expiration,
                        ContactUrl       = GetContactUrl(clientId),
                        AuthDuration     = GetCurrentResource().AuthDuration,
                        Email            = GetEmailAddress(clientId)
                    });
                }
                else if (e.CommandName == "Modify")
                {
                    clientId = int.Parse(ClientIdHiddenField.Value);
                    var cc = CurrentClients.FirstOrDefault(x => x.ClientID == clientId);
                    if (cc != null)
                    {
                        refreshAuthLevel |= cc.AuthLevel;

                        var rc = DataSession.Single <ResourceClient>(cc.ResourceClientID);
                        rc.AuthLevel = selectedAuthLevel;
                        SetExpiration(rc);

                        DataSession.SaveOrUpdate(rc);
                        ClearResourceClientsCache();

                        cc.AuthLevel  = selectedAuthLevel;
                        cc.Expiration = rc.Expiration;

                        CancelEdit();
                    }
                }

                Fill(refreshAuthLevel);
                FillClients();
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex.Message);
            }
        }