public ActionResult ClientProfile()
 {
     ClientViewModel clientview = new ClientViewModel();
     ActiveUser AU = (ActiveUser)Session["ActiveUser"];
     Client client = (from c in db.Clients
                      where c.Id == AU.ClientId
                      select c).FirstOrDefault();
     clientview.Id = client.Id;
     clientview.Description = client.Description;
     clientview.Clientname = client.Clientname;
     clientview.CageCode = client.CageCode;
     clientview.SICCode = client.SICCode;
     clientview.SalesTaxRate =(decimal) client.SalesTaxRate;
     clientview.CompanyType = (Business_Types) client.CompanyType;
     Option opt = client.Options.FirstOrDefault();
     clientview.Purchasing = (bool)opt.Purchasing;
     clientview.Sales = (bool)opt.Sales;
     clientview.Warehouse = (bool)opt.Warehouse;
     Address addr = client.Addresses.FirstOrDefault();
     Common.CopyProperties(addr, clientview, false);
     return View(clientview);
 }
 public ActionResult ClientProfile(ClientViewModel clientview)
 {
     return View(clientview);
 }
        public ActionResult RegisterView(ClientViewModel clientview)
        {
            if (ModelState.IsValid)
            {
                int id = clientview.Id;
                Session["ParentId"] = id.ToString();
                Update_Client(clientview);

                if (clientview.Id == 0)
                    return RedirectToAction("UserView");
            }
            return View(clientview);
        }
        public void Update_Client(ClientViewModel clientview)
        {
            Client client;
            client = (clientview.Id > 0) ? db.Clients.Find(clientview.Id) :  client = new Client();

            client.Clientname = clientview.Clientname;
            client.Description = clientview.Description;
            client.CageCode = clientview.CageCode;
            client.SICCode = clientview.SICCode;
            client.CompanyType = clientview.CompanyType;
            client.SalesTaxRate = clientview.SalesTaxRate;
            if (client.Status == null)
                client.Status = "DEMO";
            client.ExpDate = DateTime.Now.AddMonths(1);
            Address address;

            address = (clientview.Id > 0) ? client.Addresses.FirstOrDefault() : new Address();

            address.Address1 = clientview.Address1;
            address.Address2 = clientview.Address2;
            address.City = clientview.City;
            address.State = clientview.State;
            address.PostalCode = clientview.PostalCode;
            address.Province = clientview.Province;
            address.Country = clientview.Country;
            address.AddressType = Address_Types.Corporate;
            Option opts;
            opts = (clientview.Id > 0) ? client.Options.FirstOrDefault() : new Option();
            opts.Item = true;
            opts.Purchasing = true;
            opts.Sales = true;
            opts.Warehouse = false;

            if (clientview.Id > 0)
            {
                db.Entry(address).State = EntityState.Modified;
                db.Entry(client).State = EntityState.Modified;
                db.Entry(opts).State = EntityState.Modified;
             }
            else
            {
                License lic =  new License();
                lic.Maximum_Installs = 1;
                lic.Current_Installs = 0;
                lic.Cost = 0;
                lic.Period = 0;
                lic.DueDate = DateTime.Now.AddMonths(1);
                lic.ProductKey = Guid.NewGuid().ToString();
                lic.SystemName = "BLSInventory";
                lic.SubSystem = "Item";
                client.Licenses.Add(lic);
                //if (clientview.Purchasing)
                //{
                    License plic = new License();
                    lic.CopyPropertiesTo<License, License>(plic);
                    plic.SubSystem = "Purchasing";
                    client.Licenses.Add(plic);
                //}
                //if (clientview.Sales)
                //{
                    License slic = new License();
                    lic.CopyPropertiesTo<License, License>(slic);
                    slic.SubSystem = "Sales";
                    client.Licenses.Add(slic);
                //}
                if (clientview.Warehouse)
                {
                    License wlic = new License();
                    lic.CopyPropertiesTo<License, License>(wlic);
                    wlic.SubSystem = "Warehouse";
                    client.Licenses.Add(wlic);
                }
                client.Addresses.Add(address);
                opts.Demo = true;
                opts.ExpDate = DateTime.Now.AddMonths(1);
                client.Options.Add(opts);
                db.Clients.Add(client);
            }

            try
            {
                db.SaveChanges();
                db.Entry(client).GetDatabaseValues();
                Session["ParentId"] = client.Id.ToString();
            }
            catch (DbEntityValidationException e)
            {
                return;
            }
        }
        //--------------------------------------------------------------------------------------------------
        public ActionResult RegisterView(int? Id)
        {
            ClientViewModel clientview = new ClientViewModel();
            var comptypes = from Business_Types c in Enum.GetValues(typeof(Business_Types))
                            select new { ID = (int)c, Name = c.ToString() };

               if (Id != null)
            {
                Client client = db.Clients.Find(Id);

                if (client != null)
                {
                    clientview.Clientname = client.Clientname;
                    clientview.Description = client.Description;
                    clientview.CageCode = client.CageCode;
                    clientview.SICCode = client.SICCode;
                    clientview.InActive = (client.InActive == false) ? false : (bool) client.InActive;
                    Address address = client.Addresses.FirstOrDefault();
                    if (address != null)
                    {
                        clientview.Address1 = address.Address1;
                        clientview.Address2 = address.Address2;
                        clientview.City = address.City;
                        clientview.State = address.State;
                        clientview.PostalCode = address.PostalCode;
                        clientview.Province = address.Province;
                        clientview.Country = address.Country;
                    }
                }
                ViewBag.Title = "Edit Client Information";

                ViewBag.CompanyType = new SelectList(comptypes,"ID", "Name", (int)client.CompanyType.Value);
            }
               else
               {
               ViewBag.Title = "Register New Client";
               ViewBag.CompanyType = new SelectList(comptypes, "ID", "Name");
               clientview.Purchasing = true;
               clientview.Sales = true;
               }
            return View(clientview);
        }