public bool SynInvoiceClientToServer(string LoginName, string Password, int StoreID, NailSyn.DataAccess.Model.InvoiceClient dataInvoice) { try { using (var db = new NailShopEntities()) { using (TransactionScope transcope = new TransactionScope()) { try { NailShop.DataAccess.Invoice _invoice; foreach (NailSyn.DataAccess.SynInvoiceClient row in dataInvoice.ListInvoice) { var select = from c in db.Invoices where c.StoreID == StoreID && c.LocalID == row.InvoiceID select c; if (select.Count() == 0) { if (row.RecordState == (int)Enum.RecordState.AddNew) { _invoice = new Invoice(); _invoice.LocalID = row.InvoiceID; _invoice.StoreID = StoreID; _invoice.IsPaid = row.IsPaid; _invoice.IsPending = row.IsPending; _invoice.InvoiceDate = row.InvoiceDate; _invoice.InvoiceNo = row.InvoiceNo; _invoice.BusinessName = row.BusinessName; _invoice.Address = row.Address; _invoice.City = row.City; _invoice.State = row.City; _invoice.ZipCode = row.ZipCode; _invoice.AddressAddr1 = row.AddressAddr1; _invoice.AddressAddr2 = row.AddressAddr2; _invoice.AddressAddr3 = row.AddressAddr3; _invoice.AddressAddr4 = row.AddressAddr4; _invoice.AddressAddr5 = row.AddressAddr5; _invoice.AddressAddr1S = row.AddressAddr1S; _invoice.AddressAddr2S = row.AddressAddr2S; _invoice.AddressAddr3S = row.AddressAddr3S; _invoice.AddressAddr4S = row.AddressAddr4S; _invoice.AddressAddr5S = row.AddressAddr5S; _invoice.SkipTax = row.SkipTax; _invoice.SubTotal = row.SubTotal; _invoice.SaleTax = row.SaleTax; _invoice.Deposit = row.Deposit; _invoice.AmtPaid = row.AmtPaid; _invoice.Discount = row.Discount; _invoice.AmtCredit = row.AmtCredit; _invoice.Shipping = row.Shipping; _invoice.Balance = row.Balance; _invoice.Total = row.Total; _invoice.Voided = row.Voided; _invoice.Notes = row.Notes; _invoice.Memo = row.Memo; _invoice.RecordState = (int)Enum.RecordState.Unchange; _invoice.InvoiceStatus = row.InvoiceStatus; if(row.CustomerID!=null) { var customer = from c in db.Customers where c.StoreID == StoreID && c.LocalID == row.CustomerID select c; _invoice.CustomerID = customer.First().CustomerID; } db.Invoices.Add(_invoice); db.SaveChanges(); //Insert OrdItem OrdItem _OrdItem; foreach (NailSyn.DataAccess.SynOrdItemClient rowDetail in dataInvoice.ListOrdItem) { if (rowDetail.InvoiceID == row.InvoiceID) { if (rowDetail.RecordState != (int)Enum.RecordState.Delete) { _OrdItem = new OrdItem(); _OrdItem.InvoiceID = _invoice.InvoiceID; _OrdItem.ProductName = rowDetail.ProductName; _OrdItem.Description = rowDetail.Description; _OrdItem.ProdType = rowDetail.ProdType; _OrdItem.NoTax = rowDetail.NoTax; _OrdItem.Qty = rowDetail.Qty; _OrdItem.Price = rowDetail.Price; _OrdItem.Discount = rowDetail.Discount; _OrdItem.Total = rowDetail.Total; _OrdItem.RecordState = (int)Enum.RecordState.Unchange; var selectProduct = from c in db.Products where c.LocalID == rowDetail.ProductID && c.StoreID == StoreID select c; _OrdItem.ProductID = selectProduct.First().ProductID; db.OrdItems.Add(_OrdItem); } } } } } else { if (select.Count() == 1) { long InvoiceID = select.First().InvoiceID; var selectInvoice = from c in db.Invoices where c.InvoiceID == InvoiceID select c; if (row.RecordState == (int)Enum.RecordState.Modify) { if (row.CustomerID != null) { var customer = from c in db.Customers where c.StoreID == StoreID && c.LocalID == row.CustomerID select c; selectInvoice.First().CustomerID = customer.First().CustomerID; } selectInvoice.First().IsPaid = row.IsPaid; selectInvoice.First().IsPending = row.IsPending; selectInvoice.First().InvoiceDate = row.InvoiceDate; selectInvoice.First().InvoiceNo = row.InvoiceNo; selectInvoice.First().BusinessName = row.BusinessName; selectInvoice.First().Address = row.Address; selectInvoice.First().City = row.City; selectInvoice.First().State = row.City; selectInvoice.First().ZipCode = row.ZipCode; selectInvoice.First().AddressAddr1 = row.AddressAddr1; selectInvoice.First().AddressAddr2 = row.AddressAddr2; selectInvoice.First().AddressAddr3 = row.AddressAddr3; selectInvoice.First().AddressAddr4 = row.AddressAddr4; selectInvoice.First().AddressAddr5 = row.AddressAddr5; selectInvoice.First().AddressAddr1S = row.AddressAddr1S; selectInvoice.First().AddressAddr2S = row.AddressAddr2S; selectInvoice.First().AddressAddr3S = row.AddressAddr3S; selectInvoice.First().AddressAddr4S = row.AddressAddr4S; selectInvoice.First().AddressAddr5S = row.AddressAddr5S; selectInvoice.First().SkipTax = row.SkipTax; selectInvoice.First().SubTotal = row.SubTotal; selectInvoice.First().SaleTax = row.SaleTax; selectInvoice.First().Deposit = row.Deposit; selectInvoice.First().AmtPaid = row.AmtPaid; selectInvoice.First().Discount = row.Discount; selectInvoice.First().AmtCredit = row.AmtCredit; selectInvoice.First().Shipping = row.Shipping; selectInvoice.First().Balance = row.Balance; selectInvoice.First().Total = row.Total; selectInvoice.First().Voided = row.Voided; selectInvoice.First().Notes = row.Notes; selectInvoice.First().Memo = row.Memo; selectInvoice.First().RecordState = (int)Enum.RecordState.Unchange; selectInvoice.First().InvoiceStatus = row.InvoiceStatus; db.Entry(select.First()).State = System.Data.Entity.EntityState.Modified; //Clear Detail & insert var selectDetail = from c in db.OrdItems where c.InvoiceID == InvoiceID select c; db.OrdItems.RemoveRange(selectDetail.ToList()); db.SaveChanges(); //Insert OrdItem OrdItem _OrdItem; foreach (NailSyn.DataAccess.SynOrdItemClient rowDetail in dataInvoice.ListOrdItem) { if (rowDetail.InvoiceID == row.InvoiceID) { if (rowDetail.RecordState != (int)Enum.RecordState.Delete) { _OrdItem = new OrdItem(); _OrdItem.InvoiceID = InvoiceID; _OrdItem.ProductName = rowDetail.ProductName; _OrdItem.Description = rowDetail.Description; _OrdItem.ProdType = rowDetail.ProdType; _OrdItem.NoTax = rowDetail.NoTax; _OrdItem.Qty = rowDetail.Qty; _OrdItem.Price = rowDetail.Price; _OrdItem.Discount = rowDetail.Discount; _OrdItem.Total = rowDetail.Total; _OrdItem.RecordState = (int)Enum.RecordState.Unchange; var selectProduct = from c in db.Products where c.LocalID == rowDetail.ProductID && c.StoreID == StoreID select c; _OrdItem.ProductID = selectProduct.First().ProductID; db.OrdItems.Add(_OrdItem); } } } db.SaveChanges(); } else { //Delete Detail var selectDetail = from c in db.OrdItems where c.InvoiceID == InvoiceID select c; db.OrdItems.RemoveRange(selectDetail.ToList()); db.Entry(select.First()).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); } } } } db.SaveChanges(); transcope.Complete(); return true; } catch { transcope.Dispose(); return false; } } } } catch { return false; } }
public JsonResult SaveData(Invoice invoice, List<GetOrdItem_Result> ordItems) { try { invoice.StoreID = _session.StoreID; invoice.CustomerID = _session.CustomerID; List<OrdItem> ordDtail = new List<OrdItem>(); OrdItem mRow; foreach(GetOrdItem_Result row in ordItems) { if (row.Qty > 0) { mRow = new OrdItem(); mRow.InvoiceID = invoice.InvoiceID; mRow.ProductID = row.ProductID ?? -1; mRow.ProductName = row.ProductName; mRow.Description = row.Description; mRow.ProdType = row.ProdType; mRow.NoTax = row.NoTax; mRow.Qty = row.Qty; mRow.Price = row.Price; mRow.Discount = row.Discount; mRow.Total = row.Total; if (invoice.InvoiceID == -1) mRow.RecordState = (int)NailShop.Business.Enum.RecordState.AddNew; else mRow.RecordState = (int)NailShop.Business.Enum.RecordState.Modify; ordDtail.Add(mRow); } } IOrder _cls = new OrderBO(); if(_cls.Save(invoice, ordDtail)) return Json(new { IsOk = true }, JsonRequestBehavior.AllowGet); return Json(new { IsOk = false }, JsonRequestBehavior.AllowGet); } catch { return Json(new { IsOk = false }, JsonRequestBehavior.AllowGet); } }