public override bool Reset(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.FactoryInvoice dtoFactoryInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryInvoice>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (FactoryInvoiceMngEntities context = CreateContext()) { FactoryInvoice dbItem = context.FactoryInvoice.FirstOrDefault(o => o.FactoryInvoiceID == id); if (dbItem == null) { throw new Exception("Invoice not found!"); } dbItem.IsConfirmed = null; dbItem.ConfirmedDate = null; dbItem.ConfirmedBy = null; context.SaveChanges(); dtoItem = GetData(userId, dbItem.FactoryInvoiceID, -1, -1, string.Empty, out notification).Data; return(true); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }
public bool DeleteData(int userId, int id, out Library.DTO.Notification notification) { notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { // check permission if (id > 0 && fwFactory.CheckFactoryInvoicePermission(userId, id) == 0) { throw new Exception("Current user don't have access permission for the selected invoice data"); } using (FactoryInvoiceMngEntities context = CreateContext()) { FactoryInvoice dbItem = context.FactoryInvoice.FirstOrDefault(o => o.FactoryInvoiceID == id); if (dbItem == null) { throw new Exception("Invoice not found!"); } // check if invoice already confirmed if (dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed.Value) { throw new Exception("Can not delete the confirmed invoice!"); } // everything ok, delete the invoice if (dbItem.ScanFile != string.Empty) { fwFactory.RemoveImageFile(dbItem.ScanFile); } context.FactoryInvoice.Remove(dbItem); context.SaveChanges(); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } return(true); }
public override bool Approve(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.FactoryInvoice dtoFactoryInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryInvoice>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (FactoryInvoiceMngEntities context = CreateContext()) { FactoryInvoice dbItem = context.FactoryInvoice.FirstOrDefault(o => o.FactoryInvoiceID == id); if (dbItem == null) { throw new Exception("Invoice not found!"); } // validate before approve if (string.IsNullOrEmpty(dbItem.ScanFile)) { throw new Exception("Scan file is required!"); } if (!dbItem.InvoiceDate.HasValue) { throw new Exception("Invoice date is required!"); } if (string.IsNullOrEmpty(dbItem.InvoiceNo)) { throw new Exception("Invoice number is required!"); } dbItem.IsConfirmed = true; dbItem.ConfirmedDate = DateTime.Now; dbItem.ConfirmedBy = userId; context.SaveChanges(); dtoItem = GetData(userId, dbItem.FactoryInvoiceID, -1, -1, string.Empty, out notification).Data; return(true); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.FactoryInvoice dtoFactoryInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryInvoice>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; int number; string indexName; try { // check permission if (dtoFactoryInvoice.BookingID.HasValue && dtoFactoryInvoice.BookingID.Value > 0 && fwFactory.CheckBookingPermission(userId, dtoFactoryInvoice.BookingID.Value) == 0) { throw new Exception("Current user don't have access permission for the selected booking data"); } if (fwFactory.CheckSupplierPermission(userId, dtoFactoryInvoice.SupplierID.Value) == 0) { throw new Exception("Current user don't have access permission for the selected supplier data"); } if (id > 0 && fwFactory.CheckFactoryInvoicePermission(userId, id) == 0) { throw new Exception("Current user don't have access permission for the selected invoice data"); } using (FactoryInvoiceMngEntities context = CreateContext()) { FactoryInvoice dbItem = null; if (id == 0) { dbItem = new FactoryInvoice(); context.FactoryInvoice.Add(dbItem); } else { dbItem = context.FactoryInvoice.FirstOrDefault(o => o.FactoryInvoiceID == id); } if (dbItem == null) { notification.Message = "Factory invoice not found!"; return(false); } else { // check if invoice is confirmed if (dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed.Value) { throw new Exception("Can not edit the confirmed invoice!"); } // check concurrency if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoFactoryInvoice.ConcurrencyFlag))) { throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT); } // check remain quantity if (dtoFactoryInvoice.BookingID.HasValue) { int bookingID = dtoFactoryInvoice.BookingID.Value; int supplierID = dtoFactoryInvoice.SupplierID.Value; foreach (DTO.FactoryInvoiceDetail dtoDetail in dtoFactoryInvoice.FactoryInvoiceDetails) { int factoryOrderDetailID = dtoDetail.FactoryOrderDetailID.Value; // check total order FactoryInvoiceMng_FactoryOrderDetailSearchResult_View dtoDetailOrder = context.FactoryInvoiceMng_FactoryOrderDetailSearchResult_View.FirstOrDefault(o => o.FactoryOrderDetailID == factoryOrderDetailID && o.BookingID == bookingID && o.SupplierID == supplierID); if (dtoDetailOrder == null || !dtoDetailOrder.TotalQuantity.HasValue) { throw new Exception("Booking: " + dtoFactoryInvoice.BookingUD + " not contain item: " + dtoDetail.Description); } FactoryInvoiceMng_IssuedFactoryOrderDetail_View dtoDetailQnt = context.FactoryInvoiceMng_function_CheckOrderDetailQuantity(dtoDetail.FactoryOrderDetailID.Value, dtoFactoryInvoice.BookingID.Value, dtoFactoryInvoice.SupplierID.Value, dtoFactoryInvoice.FactoryInvoiceID).FirstOrDefault(); if (dtoDetailQnt != null && dtoDetailQnt.TotalQuantity.HasValue) { if (dtoDetailOrder.TotalQuantity.Value < dtoDetailQnt.TotalQuantity.Value + dtoDetail.Quantity.Value) { throw new Exception("Quantity exceed shipped quantity<br/><u>Product</u>: " + dtoDetail.Description + "<br/><u>Total shipped</u>: " + dtoDetailOrder.TotalQuantity.Value.ToString() + "<br/><u>Total issued</u>: " + dtoDetailQnt.TotalQuantity.Value.ToString() + "<br/><u>Previous invoice</u>: " + dtoDetailQnt.InvoiceNo + "<br/><u>To be issued in this invoice</u>: " + dtoDetail.Quantity.Value.ToString()); } } } foreach (DTO.FactoryInvoiceSparepartDetail dtoSparepartDetail in dtoFactoryInvoice.FactoryInvoiceSparepartDetails) { int factoryOrderSparepartDetailID = dtoSparepartDetail.FactoryOrderSparepartDetailID.Value; // check total order FactoryInvoiceMng_FactoryOrderSparepartDetailSearchResult_View dtoSparepartDetailOrder = context.FactoryInvoiceMng_FactoryOrderSparepartDetailSearchResult_View.FirstOrDefault(o => o.FactoryOrderSparepartDetailID == factoryOrderSparepartDetailID && o.BookingID == bookingID && o.SupplierID == supplierID); if (dtoSparepartDetailOrder == null || !dtoSparepartDetailOrder.TotalQuantity.HasValue) { throw new Exception("Booking: " + dtoFactoryInvoice.BookingUD + " not contain sparepart: " + dtoSparepartDetailOrder.Description); } FactoryInvoiceMng_IssuedFactoryOrderSparepartDetail_View dtoSparepartDetailQnt = context.FactoryInvoiceMng_function_CheckOrderSparepartDetailQuantity(dtoSparepartDetail.FactoryOrderSparepartDetailID.Value, dtoFactoryInvoice.BookingID.Value, dtoFactoryInvoice.SupplierID.Value, dtoFactoryInvoice.FactoryInvoiceID).FirstOrDefault(); if (dtoSparepartDetailQnt != null && dtoSparepartDetailQnt.TotalQuantity.HasValue) { if (dtoSparepartDetailOrder.TotalQuantity.Value < dtoSparepartDetailQnt.TotalQuantity.Value + dtoSparepartDetail.Quantity.Value) { throw new Exception("Quantity exceed shipped quantity<br/><u>Sparepart</u>: " + dtoSparepartDetail.Description + "<br/><u>Total shipped</u>: " + dtoSparepartDetailOrder.TotalQuantity.Value.ToString() + "<br/><u>Total issued</u>: " + dtoSparepartDetailQnt.TotalQuantity.Value.ToString() + "<br/><u>Previous invoice</u>: " + dtoSparepartDetailQnt.InvoiceNo + "<br/><u>To be issued in this invoice</u>: " + dtoSparepartDetail.Quantity.Value.ToString()); } } } } converter.DTO2DB(dtoFactoryInvoice, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\"); dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; // remove orphan context.FactoryInvoiceDetail.Local.Where(o => o.FactoryInvoice == null).ToList().ForEach(o => context.FactoryInvoiceDetail.Remove(o)); context.FactoryInvoiceSparepartDetail.Local.Where(o => o.FactoryInvoice == null).ToList().ForEach(o => context.FactoryInvoiceSparepartDetail.Remove(o)); context.FactoryInvoiceExtra.Local.Where(o => o.FactoryInvoice == null).ToList().ForEach(o => context.FactoryInvoiceExtra.Remove(o)); context.SaveChanges(); } dtoItem = GetData(userId, dbItem.FactoryInvoiceID, -1, -1, string.Empty, out notification).Data; return(true); } } catch (System.Data.DataException dEx) { notification.Type = Library.DTO.NotificationType.Error; Library.ErrorHelper.DataExceptionParser(dEx, out number, out indexName); if (number == 2601 && !string.IsNullOrEmpty(indexName)) { switch (indexName) { case "InvoiceNoUnique": notification.Message = "Duplicate invoice number (invoice no must be unique)!"; break; default: notification.Message = dEx.Message; break; } } else { notification.Message = dEx.Message; } return(false); } catch (Exception ex) { notification = new Library.DTO.Notification() { Message = ex.Message, Type = Library.DTO.NotificationType.Error }; return(false); } }
public void DTO2DB(DTO.FactoryInvoice dtoItem, ref FactoryInvoice dbItem, string _tempFolder) { // map fields AutoMapper.Mapper.Map <DTO.FactoryInvoice, FactoryInvoice>(dtoItem, dbItem); // insert file Module.Framework.DAL.DataFactory fwFactory = new Module.Framework.DAL.DataFactory(); if (dtoItem.ScanFile_HasChange) { dbItem.ScanFile = fwFactory.CreateNoneImageFilePointer(_tempFolder, dtoItem.ScanFile_NewFile, dtoItem.ScanFile); } if (!string.IsNullOrEmpty(dtoItem.InvoiceDate)) { if (DateTime.TryParse(dtoItem.InvoiceDate, nl, System.Globalization.DateTimeStyles.None, out tmpDate)) { dbItem.InvoiceDate = tmpDate; } } decimal subtotal = 0; // map detail if (dtoItem.FactoryInvoiceDetails != null) { // check for child rows deleted foreach (FactoryInvoiceDetail dbDetail in dbItem.FactoryInvoiceDetail.ToArray()) { if (!dtoItem.FactoryInvoiceDetails.Select(o => o.FactoryInvoiceDetailID).Contains(dbDetail.FactoryInvoiceDetailID)) { dbItem.FactoryInvoiceDetail.Remove(dbDetail); } } // map child rows foreach (DTO.FactoryInvoiceDetail dtoDetail in dtoItem.FactoryInvoiceDetails) { FactoryInvoiceDetail dbDetail; if (dtoDetail.FactoryInvoiceDetailID <= 0) { dbDetail = new FactoryInvoiceDetail(); dbItem.FactoryInvoiceDetail.Add(dbDetail); } else { dbDetail = dbItem.FactoryInvoiceDetail.FirstOrDefault(o => o.FactoryInvoiceDetailID == dtoDetail.FactoryInvoiceDetailID); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.FactoryInvoiceDetail, FactoryInvoiceDetail>(dtoDetail, dbDetail); if (dtoDetail.UnitPrice.HasValue && dtoDetail.Quantity.HasValue) { dbDetail.SubTotal = Math.Round(dtoDetail.UnitPrice.Value * dtoDetail.Quantity.Value, 2, MidpointRounding.AwayFromZero); subtotal += Math.Round(dtoDetail.UnitPrice.Value * dtoDetail.Quantity.Value, 2, MidpointRounding.AwayFromZero); } } } } // map sparepart detail if (dtoItem.FactoryInvoiceSparepartDetails != null) { // check for child rows deleted foreach (FactoryInvoiceSparepartDetail dbSparepartDetail in dbItem.FactoryInvoiceSparepartDetail.ToArray()) { if (!dtoItem.FactoryInvoiceSparepartDetails.Select(o => o.FactoryInvoiceSparepartDetailID).Contains(dbSparepartDetail.FactoryInvoiceSparepartDetailID)) { dbItem.FactoryInvoiceSparepartDetail.Remove(dbSparepartDetail); } } // map child rows foreach (DTO.FactoryInvoiceSparepartDetail dtoSparepartDetail in dtoItem.FactoryInvoiceSparepartDetails) { FactoryInvoiceSparepartDetail dbSparepartDetail; if (dtoSparepartDetail.FactoryInvoiceSparepartDetailID <= 0) { dbSparepartDetail = new FactoryInvoiceSparepartDetail(); dbItem.FactoryInvoiceSparepartDetail.Add(dbSparepartDetail); } else { dbSparepartDetail = dbItem.FactoryInvoiceSparepartDetail.FirstOrDefault(o => o.FactoryInvoiceSparepartDetailID == dtoSparepartDetail.FactoryInvoiceSparepartDetailID); } if (dbSparepartDetail != null) { AutoMapper.Mapper.Map <DTO.FactoryInvoiceSparepartDetail, FactoryInvoiceSparepartDetail>(dtoSparepartDetail, dbSparepartDetail); if (dtoSparepartDetail.UnitPrice.HasValue && dtoSparepartDetail.Quantity.HasValue) { dbSparepartDetail.SubTotal = Math.Round(dtoSparepartDetail.UnitPrice.Value * dtoSparepartDetail.Quantity.Value, 2, MidpointRounding.AwayFromZero); subtotal += Math.Round(dtoSparepartDetail.UnitPrice.Value * dtoSparepartDetail.Quantity.Value, 2, MidpointRounding.AwayFromZero); } } } } // map extra if (dtoItem.FactoryInvoiceExtras != null) { // check for child rows deleted foreach (FactoryInvoiceExtra dbExtra in dbItem.FactoryInvoiceExtra.ToArray()) { if (!dtoItem.FactoryInvoiceExtras.Select(o => o.FactoryInvoiceExtraID).Contains(dbExtra.FactoryInvoiceExtraID)) { dbItem.FactoryInvoiceExtra.Remove(dbExtra); } } // map child rows foreach (DTO.FactoryInvoiceExtra dtoExtra in dtoItem.FactoryInvoiceExtras) { FactoryInvoiceExtra dbExtra; if (dtoExtra.FactoryInvoiceExtraID <= 0) { dbExtra = new FactoryInvoiceExtra(); dbItem.FactoryInvoiceExtra.Add(dbExtra); } else { dbExtra = dbItem.FactoryInvoiceExtra.FirstOrDefault(o => o.FactoryInvoiceExtraID == dtoExtra.FactoryInvoiceExtraID); } if (dbExtra != null) { AutoMapper.Mapper.Map <DTO.FactoryInvoiceExtra, FactoryInvoiceExtra>(dtoExtra, dbExtra); if (dtoExtra.UnitPrice.HasValue && dtoExtra.Quantity.HasValue) { dbExtra.SubTotal = Math.Round(dtoExtra.UnitPrice.Value * dtoExtra.Quantity.Value, 2, MidpointRounding.AwayFromZero); subtotal += Math.Round(dtoExtra.UnitPrice.Value * dtoExtra.Quantity.Value, 2, MidpointRounding.AwayFromZero); } } } } dbItem.SubTotalAmount = subtotal; if (dbItem.DeductedAmount.HasValue) { dbItem.TotalAmount = dbItem.SubTotalAmount.Value - dbItem.DeductedAmount.Value; } else { dbItem.TotalAmount = dbItem.SubTotalAmount; } }