public void AddNewCustomer(CustomerM customer) { using (PhoneShopdbEntities e = new PhoneShopdbEntities()) { try { Customer c = new Customer(); c.Name = customer.Name; c.NIC = customer.NIC; c.Email = customer.Email; e.Customers.Add(c); e.SaveChanges(); } catch { } } }
public bool AddNewJobDetailed(DetailedJobModelM job) { using (PhoneShopdbEntities ctx = new PhoneShopdbEntities()) { Customer customer = ctx.Customers.Where(x => x.NIC == job.Customer.NIC).FirstOrDefault(); if (customer == null) { customer = new Customer(); customer.Name = job.Customer.Name; customer.NIC = job.Customer.NIC; customer.Email = job.Customer.Email; ctx.Customers.Add(customer); } else { customer = GetCustomerByNIC(job.Customer.NIC); customer.Name = job.Customer.Name; customer.Email = job.Customer.Email; } var telNums = ctx.TelephoneNumbers.Where(x => x.Customer_Id == customer.Id).ToList(); TelephoneNumber home = null; TelephoneNumber mobile = null; foreach (var item in job.Customer.TelephoneNumbers) { if (item.IsHome) { home = telNums.Where(x => x.IsHome == true).FirstOrDefault(); if (home == null) { home = new TelephoneNumber(); home.Customer_Id = customer.Id; home.IsHome = item.IsHome; home.TelNo = item.TelNo; ctx.TelephoneNumbers.Add(home); } else { home.TelNo = item.TelNo; } } else { mobile = telNums.Where(x => x.IsHome == false).FirstOrDefault(); if (mobile == null) { mobile = new TelephoneNumber(); mobile.Customer_Id = customer.Id; mobile.IsHome = item.IsHome; mobile.TelNo = item.TelNo; ctx.TelephoneNumbers.Add(mobile); } else { mobile.TelNo = item.TelNo; } } } PhoneBrand brand = ctx.PhoneBrands.Where(x => x.BrandName == job.PhoneModel.BrandName).FirstOrDefault(); PhoneModel model = null; if (brand == null) { brand = new PhoneBrand(); brand.BrandName = job.PhoneModel.BrandName; ctx.PhoneBrands.Add(brand); model = new PhoneModel(); model.Brand_Id = brand.Id; model.ModelNumber = job.PhoneModel.SelectedPhoneModel.ModelNumber; model.ImageURI = job.PhoneModel.SelectedPhoneModel.ImageURI; ctx.PhoneModels.Add(model); } else { model = ctx.PhoneModels.Where(x => x.ModelNumber == job.PhoneModel.SelectedPhoneModel.ModelNumber).FirstOrDefault(); if (model == null) { model = new PhoneModel(); model.Brand_Id = (int)brand.Id; model.ModelNumber = job.PhoneModel.SelectedPhoneModel.ModelNumber; model.ImageURI = job.PhoneModel.SelectedPhoneModel.ImageURI; ctx.PhoneModels.Add(model); } else { model.ImageURI = job.PhoneModel.SelectedPhoneModel.ImageURI; } //model.ImageURI = job.PhoneModel.SelectedPhoneModel.ImageURI; } Job newJob = new Job(); newJob.Customer_Id = customer.Id; newJob.IsCompleted = false; newJob.IsDevice = job.DeviceOrAccaccessories.Count == 0 ? false : true; newJob.JobDiscription = job.Job.JobDiscription; newJob.JobNo = job.Job.JobNumber; newJob.OtherDiscription = job.Job.OtherDiscription; newJob.PhoneModel_Id = model.Id; //advanced pavement ctx.Jobs.Add(newJob); foreach (var item in job.DeviceOrAccaccessories) { Device d = new Device(); d.Job_Id = newJob.Id; if (item.IsPhone) { d.IsPhone = true; d.EmiNumber = item.EmiNumber; } d.Discription = item.Discription; ctx.Devices.Add(d); } ctx.SaveChanges(); } return false; }
public Customer GetCustomerByNIC(string NIC) { Customer customer = new Customer(); using (PhoneShopdbEntities e = new PhoneShopdbEntities()) { customer = e.Customers.Where(x => x.NIC == NIC).FirstOrDefault(); //if (c != null) //{ // customer.Id = c.Id; // customer.Name = c.Name; // customer.NIC = c.NIC; // customer.Email = c.Email; //} } return customer; }