public static void Edit(Bill bill, BillEditModel model, string userId) { bill.VendorId = model.VendorId; bill.BillNumber = model.BillNumber; bill.Tax = model.Tax; bill.Discount = model.Discount; bill.TotalAmount = model.TotalAmount; bill.Remark = model.Remark; bill.DueDate = model.DueDate; bill.UpdatedBy = userId ?? "0"; bill.UpdatedOn = Utility.GetDateTime(); bill.BillDate = model.BillDate; bill.StrBillDate = model.BillDate.ToString("dd/MM/yyyy"); bill.StrDueDate = model.DueDate.Value.ToString("dd/MM/yyyy"); bill.PoSoNumber = model.PoSoNumber; bill.Notes = model.Notes; bill.SubTotal = model.SubTotal; bill.LineAmountSubTotal = model.LineAmountSubTotal; bill.RefrenceNumber = model.RefrenceNumber; ArrayList tempArr = new ArrayList(); foreach (var item in model.Items) { tempArr.Add(item.ItemId); var alreadyExistServices = bill.Items.Where(x => item.ItemId == x.ItemId).FirstOrDefault(); if (alreadyExistServices != null) { alreadyExistServices.Price = item.Price; alreadyExistServices.TaxId = item.TaxId; alreadyExistServices.TaxPercentage = item.TaxPercentage; alreadyExistServices.Rate = item.Rate; alreadyExistServices.Quantity = item.Quantity; alreadyExistServices.TaxPrice = item.TaxPrice; alreadyExistServices.LineAmount = item.LineAmount; bill.Items.Add(alreadyExistServices); } } var deletedServices = bill.Items.Where(x => !tempArr.Contains(x.ItemId)).ToList(); //var alreadyExistServices = entity.Services.Where(x => tempArr.Contains(x.ServiceId)).ToList(); //var resultAll = items.Where(i => filter.All(x => i.Features.Any(f => x == f.Id))); foreach (var deletedService in deletedServices) { bill.Items.Remove(deletedService); } var addedServices = model.Items .Where(x => !bill.Items.Select(y => y.ItemId).Contains(x.ItemId)) .ToList(); foreach (var service in addedServices) { bill.Items.Add(new BillItem { Id = Guid.NewGuid(), ItemId = service.ItemId, Rate = service.Rate, TaxId = service.TaxId, Price = service.Price, Quantity = service.Quantity, TaxPercentage = service.TaxPercentage, TaxPrice = service.TaxPrice, LineAmount = service.LineAmount }); } if (model.Attachments == null || !model.Attachments.Any()) { return; } var deletedAttachemntFiles = bill.Attachments.Select(x => x.FileName) .Except(model.Attachments.Select(y => y.FileName)).ToList(); foreach (var deletedAttachemntFile in deletedAttachemntFiles) { var attachemnt = bill.Attachments.Single(x => x.FileName.Equals(deletedAttachemntFile)); bill.Attachments.Remove(attachemnt); } foreach (var attachment in model.Attachments) { var billAttachment = bill.Attachments.SingleOrDefault(x => x.FileName.Equals(attachment.FileName)); if (billAttachment == null) { billAttachment = new BillAttachment { Title = attachment.Title, FileName = attachment.FileName, OriginalFileName = attachment.OriginalFileName, CreatedBy = userId ?? "0", CreatedOn = Utility.GetDateTime() }; } else { billAttachment.Title = attachment.Title; billAttachment.FileName = attachment.FileName; billAttachment.OriginalFileName = attachment.OriginalFileName; } bill.Attachments.Add(billAttachment); } }
public IHttpActionResult UpdateBill(int billId) { try { using (MaxMasterDbEntities db = new MaxMasterDbEntities()) { var form = HttpContext.Current.Request.Form; var BilledItemsList = JsonConvert.DeserializeObject <List <BilledItemsModel> >(form.Get("items")); Bill bill = db.Bills.Find(billId); bill.BillDate = Convert.ToDateTime(form.Get("billDate")); bill.BillNumber = form.Get("billNum"); bill.PoNumber = form.Get("poNum"); if (form.Get("paymentTerms") != null) { bill.PaymentTerms = form.Get("paymentTerms"); } bill.Notes = form.Get("notes"); bill.SupplierId = (form.Get("supplierId")); if (form.Get("dueDate") != "") { bill.DueDate = Convert.ToDateTime(form.Get("dueDate")); } bill.Subtotal = Convert.ToDecimal(form.Get("subtotal")); var tds = form.Get("tdsId"); if (tds != null) { bill.TDSAmount = Convert.ToDecimal(form.Get("tdsAmount")); bill.TDSId = Convert.ToInt32(form.Get("tdsId")); } else { bill.TDSId = null; bill.TDSAmount = null; } bill.Total = Convert.ToDecimal(form.Get("total")); //if (form.Get("taxAmount") != "") //{ // bill.TotalTaxAmount = Convert.ToDecimal(form.Get("taxAmount")); //} //else //{ // bill.TotalTaxAmount = null; //} if (form.Get("discountAmount") != "") { bill.Discount = Convert.ToDecimal(form.Get("discountAmount")); } else { bill.Discount = null; } if (form.Get("discountType") != null) { bill.DiscountType = form.Get("discountType"); } else { bill.DiscountType = null; } if (form.Get("courierCharges") != "") { bill.CourierCharges = Convert.ToDecimal(form.Get("courierCharges")); } else { bill.CourierCharges = null; } if (form.Get("roundOffNumber") != "") { bill.RoundOffNumber = Convert.ToDecimal(form.Get("roundOffNumber")); } else { bill.RoundOffNumber = null; } bill.BalanceDue = Convert.ToDecimal(form.Get("total")); if (DateTime.Now < bill.DueDate) { bill.Status = "Due"; } else { bill.Status = "OverDue"; } var files = HttpContext.Current.Request.Files; var fileAttachments = new List <HttpPostedFile>(); if (files.Count > 0) { for (int i = 0; i < files.Count; i++) { fileAttachments.Add(files[i]); } foreach (var file in fileAttachments) { var fileDirecory = HttpContext.Current.Server.MapPath("~/BillAttachments"); if (!Directory.Exists(fileDirecory)) { Directory.CreateDirectory(fileDirecory); } var fileName = file.FileName; var filePath = Path.Combine(fileDirecory, fileName); file.SaveAs(filePath); BillAttachment Billatt = new BillAttachment(); Billatt.FileName = Path.GetFileNameWithoutExtension(file.FileName); Billatt.AttachmentUrl = Path.Combine(ConfigurationManager.AppSettings["ApiUrl"], "BillAttachments", fileName); bill.BillAttachments.Add(Billatt); } } // update items list var previouslyBilledItems = db.ItemsBillMappings.Where(x => x.BillId == bill.Id).ToList(); for (int i = 0; i < previouslyBilledItems.Count(); i++) { ItemsBillMapping item = db.ItemsBillMappings.Find(previouslyBilledItems[i].Id); db.ItemsBillMappings.Remove(item); // db.SaveChanges(); } for (int i = 0; i < BilledItemsList.Count(); i++) { ItemsBillMapping ibm = new ItemsBillMapping(); ibm.ModelId = BilledItemsList[i].ModelId; ibm.PricePerUnit = Convert.ToDecimal(BilledItemsList[i].Price); ibm.Quantity = BilledItemsList[i].Quantity; ibm.GST = Convert.ToDecimal(BilledItemsList[i].Tax) / 2; ibm.CGST = Convert.ToDecimal(BilledItemsList[i].Tax) / 2; ibm.Description = BilledItemsList[i].Description; bill.ItemsBillMappings.Add(ibm); // db.ItemsBillMappings.Add(ibm); } db.Entry(bill).State = EntityState.Modified; db.SaveChanges(); return(Ok()); } } catch (Exception ex) { new Error().logAPIError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString(), ex.StackTrace); return(Content(HttpStatusCode.InternalServerError, "An error occured, please try again later")); } }