Beispiel #1
0
        public ActionResult AddNewCompany(CompanyViewModel model)
        {
            var status          = true;
            var message         = "Company added";
            var data            = model;
            var newCompanyModel = new CompanyViewModel();

            if (!ModelState.IsValid)
            {
                return(Json("Something went wrong"));
            }
            else
            {
                if (model.IsShipSame)
                {
                    var physicalAddress = new Address
                    {
                        CountryId = 1,
                        CreatedBy = User.Identity.Name,
                        CreatedOn = DateTime.Now,
                        UpdatedBy = User.Identity.Name,
                        UpdatedOn = DateTime.Now,
                        Number    = model.PhysicalAddress.Number,
                        Street    = model.PhysicalAddress.Street,
                        City      = model.PhysicalAddress.City,
                        Suburb    = model.PhysicalAddress.Suburb,
                        PostCode  = model.PhysicalAddress.PostCode
                    };
                    db.Address.Add(physicalAddress);
                    db.SaveChanges();
                    var company = new Company()
                    {
                        IsActive          = true,
                        Name              = model.Name,
                        Website           = model.Website,
                        PhoneNumber       = model.PhoneNumber,
                        UpdatedBy         = User.Identity.Name,
                        CreatedOn         = DateTime.Now,
                        CreatedBy         = User.Identity.Name,
                        UpdatedOn         = DateTime.Now,
                        PhysicalAddressId = physicalAddress.AddressId,
                        BillingAddressId  = physicalAddress.AddressId
                    };
                    newCompanyModel.PhysicalAddress = new AddressViewModel
                    {
                        AddressId = physicalAddress.AddressId,
                        CountryId = physicalAddress.CountryId,
                        Number    = physicalAddress.Number,
                        Street    = physicalAddress.Street,
                        City      = physicalAddress.City,
                        Suburb    = physicalAddress.Suburb,
                        PostCode  = physicalAddress.PostCode
                    };
                    newCompanyModel.BillingAddress = new AddressViewModel
                    {
                        AddressId = physicalAddress.AddressId,
                        CountryId = physicalAddress.CountryId,
                        Number    = physicalAddress.Number,
                        Street    = physicalAddress.Street,
                        City      = physicalAddress.City,
                        Suburb    = physicalAddress.Suburb,
                        PostCode  = physicalAddress.PostCode
                    };
                    db.Company.Add(company);
                    db.SaveChanges();
                    newCompanyModel.Id       = company.Id;
                    newCompanyModel.IsActive = company.IsActive;
                    newCompanyModel.Name     = company.Name;
                    newCompanyModel.Website  = company.Website;
                    data = newCompanyModel;
                }
                else
                {
                    var physicalAddress = new Address
                    {
                        CountryId = 1,
                        CreatedBy = User.Identity.Name,
                        CreatedOn = DateTime.Now,
                        UpdatedBy = User.Identity.Name,
                        UpdatedOn = DateTime.Now,
                        Number    = model.PhysicalAddress.Number,
                        Street    = model.PhysicalAddress.Street,
                        City      = model.PhysicalAddress.City,
                        Suburb    = model.PhysicalAddress.Suburb,
                        PostCode  = model.PhysicalAddress.PostCode
                    };
                    db.Address.Add(physicalAddress);
                    db.SaveChanges();

                    var billingAddress = new Address
                    {
                        CountryId = 1,
                        CreatedBy = User.Identity.Name,
                        CreatedOn = DateTime.Now,
                        UpdatedBy = User.Identity.Name,
                        UpdatedOn = DateTime.Now,
                        Number    = model.BillingAddress.Number,
                        Street    = model.BillingAddress.Street,
                        City      = model.BillingAddress.City,
                        Suburb    = model.BillingAddress.Suburb,
                        PostCode  = model.BillingAddress.PostCode
                    };
                    db.Address.Add(billingAddress);
                    db.SaveChanges();
                    var company = new Company()
                    {
                        IsActive          = true,
                        Name              = model.Name,
                        Website           = model.Website,
                        PhoneNumber       = model.PhoneNumber,
                        UpdatedBy         = User.Identity.Name,
                        CreatedOn         = DateTime.Now,
                        CreatedBy         = User.Identity.Name,
                        UpdatedOn         = DateTime.Now,
                        PhysicalAddressId = physicalAddress.AddressId,
                        BillingAddressId  = billingAddress.AddressId
                    };
                    newCompanyModel.PhysicalAddress = new AddressViewModel
                    {
                        AddressId = physicalAddress.AddressId,
                        CountryId = physicalAddress.CountryId,
                        Number    = physicalAddress.Number,
                        Street    = physicalAddress.Street,
                        City      = physicalAddress.City,
                        Suburb    = physicalAddress.Suburb,
                        PostCode  = physicalAddress.PostCode
                    };
                    newCompanyModel.BillingAddress = new AddressViewModel
                    {
                        AddressId = billingAddress.AddressId,
                        CountryId = billingAddress.CountryId,
                        Number    = billingAddress.Number,
                        Street    = billingAddress.Street,
                        City      = billingAddress.City,
                        Suburb    = billingAddress.Suburb,
                        PostCode  = billingAddress.PostCode
                    };
                    db.Company.Add(company);
                    db.SaveChanges();

                    newCompanyModel.Id       = company.Id;
                    newCompanyModel.IsActive = company.IsActive;
                    newCompanyModel.Name     = company.Name;
                    newCompanyModel.Website  = company.Website;

                    data = newCompanyModel;
                }
            }
            return(Json(new
            {
                success = status,
                message = message,
                data = data
            }));
        }
Beispiel #2
0
        public ActionResult EditCompany(CompanyViewModel model)
        {
            var foundCompany1 = db.Company.FirstOrDefault(x => x.Id == model.Id);

            if (foundCompany1 != null)
            {
                if (ModelState.IsValid)
                {
                    foundCompany1.Id          = model.Id;
                    foundCompany1.Name        = model.Name;
                    foundCompany1.Website     = model.Website;
                    foundCompany1.PhoneNumber = model.PhoneNumber;
                    foundCompany1.UpdatedBy   = User.Identity.Name;
                    foundCompany1.UpdatedOn   = Convert.ToDateTime(DateTime.Now);

                    if (foundCompany1.PhysicalAddressId == foundCompany1.BillingAddressId)
                    {
                        if (model.IsShipSame) //check box is checked
                        {                     //updating physical address only if intially same and current physical addresses are same
                            foundCompany1.Address1.Number   = model.PhysicalAddress.Number;
                            foundCompany1.Address1.Street   = model.PhysicalAddress.Street;
                            foundCompany1.Address1.City     = model.PhysicalAddress.City;
                            foundCompany1.Address1.Suburb   = model.PhysicalAddress.Suburb;
                            foundCompany1.Address1.PostCode = model.PhysicalAddress.PostCode;

                            db.SaveChanges();
                        }
                        else //Not same
                        {
                            if (model.PhysicalAddress == model.BillingAddress)
                            {
                                model.IsShipSame = false;
                            }
                            else
                            {
                                // Physical address updating
                                foundCompany1.Address1.Number   = model.PhysicalAddress.Number;
                                foundCompany1.Address1.Street   = model.PhysicalAddress.Street;
                                foundCompany1.Address1.City     = model.PhysicalAddress.City;
                                foundCompany1.Address1.Suburb   = model.PhysicalAddress.Suburb;
                                foundCompany1.Address1.PostCode = model.PhysicalAddress.PostCode;

                                //Generating new billing address
                                Address billingAddress = new Address
                                {
                                    CountryId = 1,
                                    Number    = model.BillingAddress.Number,
                                    Street    = model.BillingAddress.Street,
                                    Suburb    = model.BillingAddress.Suburb,
                                    City      = model.BillingAddress.City,
                                    PostCode  = model.BillingAddress.PostCode,
                                };
                                db.Address.Add(billingAddress);
                                foundCompany1.BillingAddressId = billingAddress.AddressId;
                                db.SaveChanges();
                            }
                        }
                    }
                    else
                    {
                        if (!model.IsShipSame) //is not checked
                        {
                            //Physical address updated
                            foundCompany1.Address1.Number   = model.PhysicalAddress.Number;
                            foundCompany1.Address1.Street   = model.PhysicalAddress.Street;
                            foundCompany1.Address1.City     = model.PhysicalAddress.City;
                            foundCompany1.Address1.Suburb   = model.PhysicalAddress.Suburb;
                            foundCompany1.Address1.PostCode = model.PhysicalAddress.PostCode;

                            //Billing Address updated
                            foundCompany1.Address.Number   = model.BillingAddress.Number;
                            foundCompany1.Address.Street   = model.BillingAddress.Street;
                            foundCompany1.Address.Suburb   = model.BillingAddress.Suburb;
                            foundCompany1.Address.City     = model.BillingAddress.City;
                            foundCompany1.Address.PostCode = model.BillingAddress.PostCode;
                            db.SaveChanges();
                        }
                        else
                        {
                            //physical address updating
                            var bill = db.Address.Where(p => p.AddressId == foundCompany1.BillingAddressId).First();
                            foundCompany1.Address1.Number   = model.PhysicalAddress.Number;
                            foundCompany1.Address1.Street   = model.PhysicalAddress.Street;
                            foundCompany1.Address1.City     = model.PhysicalAddress.City;
                            foundCompany1.Address1.Suburb   = model.PhysicalAddress.Suburb;
                            foundCompany1.Address1.PostCode = model.PhysicalAddress.PostCode;
                            //billing address and physical adress are same
                            foundCompany1.BillingAddressId = foundCompany1.PhysicalAddressId;
                            db.Address.Remove(bill);
                            db.SaveChanges();
                        }
                    }
                    if (model.PhotoProfile == null)
                    {
                        foundCompany1.ProfilePhoto = model.PhotoProfile;
                        db.SaveChanges();
                    }
                }
                else
                {
                    return(Json(new { Success = false, Message = "Model State isnt valid!" }));
                }
            }
            else
            {
                return(Json(new { Success = false, Message = "Company not found!" }));
            }
            return(Json(new { Success = true, Message = "Company details has been updated successfully!" }));
        }