public override bool Reset(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.FactoryPayment dtoFactoryCreditNote = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryPayment>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (FactoryPayment2MngEntities context = CreateContext()) { FactoryPayment2 dbItem = context.FactoryPayment2.FirstOrDefault(o => o.FactoryPayment2ID == id); if (dbItem == null) { throw new Exception("Factory payment not found!"); } dbItem.IsConfirmed = null; dbItem.ConfirmedDate = null; dbItem.ConfirmedBy = null; dbItem.ConfirmedRemark = null; dbItem.ConfirmedAmount = null; context.SaveChanges(); dtoItem = GetData(userId, dbItem.FactoryPayment2ID, -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 Approve(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.FactoryPayment dtoFactoryPayment = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryPayment>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; try { using (FactoryPayment2MngEntities context = CreateContext()) { FactoryPayment2 dbItem = context.FactoryPayment2.FirstOrDefault(o => o.FactoryPayment2ID == id); if (dbItem == null) { throw new Exception("Factory payment not found!"); } // validate before approve if (!dbItem.PaymentDate.HasValue) { throw new Exception("Payment date is required!"); } if (dbItem.ConfirmedAmount.HasValue && dbItem.TotalAmount.HasValue && dbItem.TotalAmount < dbItem.ConfirmedAmount) { throw new Exception("Confirmed received amount greater than total paid amount!"); } if (fwFactory.IsDPBalanceClosed(userId, dbItem.SupplierID.Value, dbItem.Season)) { throw new Exception("Balance for season: " + dbItem.Season + " is closed or you dont have access permission for the balance data!"); } dbItem.IsConfirmed = true; dbItem.ConfirmedDate = DateTime.Now; dbItem.ConfirmedBy = userId; dbItem.ConfirmedRemark = dtoFactoryPayment.ConfirmedRemark; dbItem.ConfirmedAmount = dtoFactoryPayment.ConfirmedAmount; context.SaveChanges(); dtoItem = GetData(userId, dbItem.FactoryPayment2ID, -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.CheckFactoryPaymentPermission(userId, id) == 0) { throw new Exception("Current user don't have access permission for the selected payment data"); } using (FactoryPayment2MngEntities context = CreateContext()) { FactoryPayment2 dbItem = context.FactoryPayment2.FirstOrDefault(o => o.FactoryPayment2ID == id); if (dbItem == null) { throw new Exception("Payment not found!"); } // check if invoice already confirmed if (dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed.Value) { throw new Exception("Can not delete the confirmed payment!"); } // everything ok, delete the invoice context.FactoryPayment2.Remove(dbItem); context.SaveChanges(); } } catch (Exception ex) { notification.Type = Library.DTO.NotificationType.Error; notification.Message = ex.Message; return(false); } return(true); }
public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification) { DTO.FactoryPayment dtoFactoryPayment = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryPayment>(); notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success }; int number; string indexName; try { // check permission if (fwFactory.CheckSupplierPermission(userId, dtoFactoryPayment.SupplierID.Value) == 0) { throw new Exception("Current user don't have access permission for the selected supplier data"); } if (id > 0 && fwFactory.CheckFactoryPaymentPermission(userId, id) == 0) { throw new Exception("Current user don't have access permission for the selected factory payment data"); } using (FactoryPayment2MngEntities context = CreateContext()) { FactoryPayment2 dbItem = null; if (id == 0) { dbItem = new FactoryPayment2(); context.FactoryPayment2.Add(dbItem); } else { dbItem = context.FactoryPayment2.FirstOrDefault(o => o.FactoryPayment2ID == id); } if (dbItem == null) { notification.Message = "Factory payment not found!"; return(false); } else { // check if payment is confirmed if (dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed.Value) { throw new Exception("Can not edit the confirmed payment!"); } // check concurrency if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoFactoryPayment.ConcurrencyFlag))) { throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT); } // validata data if (dbItem.ConfirmedAmount.HasValue && dbItem.TotalAmount.HasValue && dbItem.TotalAmount < dbItem.ConfirmedAmount) { throw new Exception("Confirmed received amount greater than total paid amount!"); } if (fwFactory.IsDPBalanceClosed(userId, dtoFactoryPayment.SupplierID.Value, dtoFactoryPayment.Season)) { throw new Exception("Balance for season: " + dbItem.Season + " is closed or you dont have access permission for the balance data!"); } using (DbContextTransaction scope = context.Database.BeginTransaction()) { context.Database.ExecuteSqlCommand("SELECT * FROM FactoryPayment2 WITH (TABLOCKX, HOLDLOCK); SELECT * FROM FactoryPayment2Balance WITH (TABLOCKX, HOLDLOCK); SELECT * FROM FactoryPayment2Detail WITH (TABLOCKX, HOLDLOCK);"); try { converter.DTO2DB(dtoFactoryPayment, ref dbItem); decimal remainDPAmount = context.FactoryPayment2Mng_function_GetRemainDPAmount(dbItem.Season, dbItem.SupplierID).FirstOrDefault().Value; decimal totalDPDeductedAmount = dbItem.FactoryPayment2Detail.Where(o => o.DPDeductedAmount.HasValue).Sum(o => o.DPDeductedAmount.Value); if (totalDPDeductedAmount > 0 && totalDPDeductedAmount > remainDPAmount) { throw new Exception("DP deducted amount (" + totalDPDeductedAmount.ToString() + ") larger than remain DP amount (" + remainDPAmount.ToString() + ")!"); } dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; // remove orphan context.FactoryPayment2Detail.Local.Where(o => o.FactoryPayment2 == null).ToList().ForEach(o => context.FactoryPayment2Detail.Remove(o)); context.SaveChanges(); dbItem.ReceiptNo = Library.Common.Helper.formatIndex(dbItem.FactoryPayment2ID.ToString(), 8, "0"); context.SaveChanges(); } catch (Exception ex) { throw ex; } finally { scope.Commit(); } } } dtoItem = GetData(userId, dbItem.FactoryPayment2ID, -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 "ReceipNoUnique": notification.Message = "Duplicate payment receipt number (payment receipt number 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.FactoryPayment dtoItem, ref FactoryPayment2 dbItem) { decimal subtotal = 0; // map fields AutoMapper.Mapper.Map <DTO.FactoryPayment, FactoryPayment2>(dtoItem, dbItem); if (!string.IsNullOrEmpty(dtoItem.PaymentDate)) { if (DateTime.TryParse(dtoItem.PaymentDate, nl, System.Globalization.DateTimeStyles.None, out tmpDate)) { dbItem.PaymentDate = tmpDate; } } // map detail if (dtoItem.FactoryPaymentDetails != null) { // check for child rows deleted foreach (FactoryPayment2Detail dbDetail in dbItem.FactoryPayment2Detail.ToArray()) { if (!dtoItem.FactoryPaymentDetails.Select(o => o.FactoryPayment2DetailID).Contains(dbDetail.FactoryPayment2DetailID)) { dbItem.FactoryPayment2Detail.Remove(dbDetail); } } // map child rows foreach (DTO.FactoryPaymentDetail dtoDetail in dtoItem.FactoryPaymentDetails) { FactoryPayment2Detail dbDetail; if (dtoDetail.FactoryPayment2DetailID <= 0) { dbDetail = new FactoryPayment2Detail(); dbItem.FactoryPayment2Detail.Add(dbDetail); } else { dbDetail = dbItem.FactoryPayment2Detail.FirstOrDefault(o => o.FactoryPayment2DetailID == dtoDetail.FactoryPayment2DetailID); } if (dbDetail != null) { AutoMapper.Mapper.Map <DTO.FactoryPaymentDetail, FactoryPayment2Detail>(dtoDetail, dbDetail); if (dtoDetail.PaidAmount.HasValue) { subtotal += Math.Round(dtoDetail.PaidAmount.Value, 2, MidpointRounding.AwayFromZero); } else { dbDetail.PaidAmount = 0; } if (dtoDetail.PaidAmount.HasValue && dtoDetail.DPDeductedAmount.HasValue && dtoDetail.TotalAmount.HasValue && (dtoDetail.PaidAmount.Value + dtoDetail.DPDeductedAmount.Value) > dtoDetail.TotalAmount.Value) { throw new Exception("DP amount + paid amount (" + (dtoDetail.PaidAmount.Value + dtoDetail.DPDeductedAmount.Value).ToString() + ") for invoice " + dtoDetail.InvoiceNo + " larger than to be paid amount (" + dtoDetail.TotalAmount.Value.ToString() + ")"); } } } } if (dbItem.DPAmount.HasValue) { dbItem.TotalAmount = subtotal + Math.Round(dbItem.DPAmount.Value, 2, MidpointRounding.AwayFromZero); } else { dbItem.TotalAmount = subtotal; dbItem.DPAmount = 0; } }