Beispiel #1
0
        public override bool DeleteData(int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (ClientPaymentMngEntities context = CreateContext())
                {
                    ClientPayment dbItem = context.ClientPayment.FirstOrDefault(o => o.ClientPaymentID == id);
                    if (dbItem == null)
                    {
                        throw new Exception("Payment not found!");
                    }
                    if (dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed.Value)
                    {
                        throw new Exception("Can not delete the confirmed payment!");
                    }

                    context.ClientPayment.Remove(dbItem);
                    context.SaveChanges();
                    //// remove child
                    //foreach (ClientPaymentDetail dbDetail in dbItem.ClientPaymentDetail.ToArray())
                    //{
                    //    foreach (ClientPaymentDeduction dbDeduction in dbDetail.ClientPaymentDeduction.ToArray())
                    //    {
                    //        dbDetail.ClientPaymentDeduction.Remove(dbDeduction);
                    //    }
                    //    dbItem.ClientPaymentDetail.Remove(dbDetail);
                    //}
                    //foreach (ClientPaymentBallance dbBallance in dbItem.ClientPaymentBallance.ToArray())
                    //{
                    //    dbItem.ClientPaymentBallance.Remove(dbBallance);
                    //}
                    //context.ClientPaymentDeduction.Local.Where(o => o.ClientPaymentDetail == null).ToList().ForEach(o => context.ClientPaymentDeduction.Remove(o));
                    //context.ClientPaymentDetail.Local.Where(o => o.ClientPayment == null).ToList().ForEach(o => context.ClientPaymentDetail.Remove(o));
                    //context.ClientPaymentBallance.Local.Where(o => o.ClientPayment == null).ToList().ForEach(o => context.ClientPaymentBallance.Remove(o));
                    //context.ClientPayment.Remove(dbItem);
                    //context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        public void DTO2DB(DTO.ClientPayment dtoItem, ref ClientPayment dbItem)
        {
            // map fields
            AutoMapper.Mapper.Map <DTO.ClientPayment, ClientPayment>(dtoItem, dbItem);
            dbItem.UpdatedDate = DateTime.Now;
            if (DateTime.TryParse(dtoItem.PaymentDate, cInfo, System.Globalization.DateTimeStyles.None, out tmpDate))
            {
                dbItem.PaymentDate = tmpDate;
            }

            // map payment detail
            if (dtoItem.ClientPaymentDetails != null)
            {
                // check for child rows deleted
                foreach (ClientPaymentDetail dbDetail in dbItem.ClientPaymentDetail.ToArray())
                {
                    if (!dtoItem.ClientPaymentDetails.Select(o => o.ClientPaymentDetailID).Contains(dbDetail.ClientPaymentDetailID))
                    {
                        dbItem.ClientPaymentDetail.Remove(dbDetail);
                    }
                }

                // map child rows
                foreach (DTO.ClientPaymentDetail dtoDetail in dtoItem.ClientPaymentDetails)
                {
                    ClientPaymentDetail dbDetail;
                    if (dtoDetail.ClientPaymentDetailID <= 0)
                    {
                        dbDetail = new ClientPaymentDetail();
                        dbItem.ClientPaymentDetail.Add(dbDetail);
                    }
                    else
                    {
                        dbDetail = dbItem.ClientPaymentDetail.FirstOrDefault(o => o.ClientPaymentDetailID == dtoDetail.ClientPaymentDetailID);
                    }

                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.ClientPaymentDetail, ClientPaymentDetail>(dtoDetail, dbDetail);
                        if (DateTime.TryParse(dtoDetail.PaidDate, cInfo, System.Globalization.DateTimeStyles.None, out tmpDate))
                        {
                            dbDetail.PaidDate = tmpDate;
                        }

                        // map deduction
                        if (dtoDetail.ClientPaymentDeductions != null)
                        {
                            // check for child rows deleted
                            foreach (ClientPaymentDeduction dbDeduction in dbDetail.ClientPaymentDeduction.ToArray())
                            {
                                if (!dtoDetail.ClientPaymentDeductions.Select(o => o.ClientPaymentDeductionID).Contains(dbDeduction.ClientPaymentDeductionID))
                                {
                                    dbDetail.ClientPaymentDeduction.Remove(dbDeduction);
                                }
                            }

                            // map child row
                            foreach (DTO.ClientPaymentDeduction dtoDeduction in dtoDetail.ClientPaymentDeductions)
                            {
                                ClientPaymentDeduction dbDeduction;
                                if (dtoDeduction.ClientPaymentDeductionID <= 0)
                                {
                                    dbDeduction = new ClientPaymentDeduction();
                                    dbDetail.ClientPaymentDeduction.Add(dbDeduction);
                                }
                                else
                                {
                                    dbDeduction = dbDetail.ClientPaymentDeduction.FirstOrDefault(o => o.ClientPaymentDeductionID == dtoDeduction.ClientPaymentDeductionID);
                                }

                                if (dbDeduction != null)
                                {
                                    AutoMapper.Mapper.Map <DTO.ClientPaymentDeduction, ClientPaymentDeduction>(dtoDeduction, dbDeduction);
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #3
0
        public override bool Approve(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.ClientPayment dtoClientPayment = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.ClientPayment>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (ClientPaymentMngEntities context = CreateContext())
                {
                    ClientPayment dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new ClientPayment();
                        context.ClientPayment.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.ClientPayment.FirstOrDefault(o => o.ClientPaymentID == id);
                    }

                    if (dbItem == null)
                    {
                        throw new Exception("Payment not found!");
                    }
                    dbItem.IsConfirmed   = true;
                    dbItem.ConfirmedDate = DateTime.Now;
                    dbItem.ConfirmedBy   = userId;

                    // check if there's difference in total payment and detail
                    decimal totalPayment       = 0;
                    decimal totalDetailPayment = 0;
                    if (dbItem.Amount.HasValue)
                    {
                        totalPayment = dbItem.Amount.Value;
                    }
                    totalDetailPayment = dbItem.ClientPaymentDetail.Where(o => o.Amount.HasValue).Sum(o => o.Amount.Value);
                    if (totalPayment != totalDetailPayment)
                    {
                        ClientPaymentBallance dbBallance = dbItem.ClientPaymentBallance.FirstOrDefault();
                        if (dbBallance == null)
                        {
                            dbBallance = new ClientPaymentBallance();
                            dbBallance.ClientPayment = dbItem;
                            context.ClientPaymentBallance.Add(dbBallance);
                        }
                        dbBallance.Amount       = totalPayment - totalDetailPayment;
                        dbBallance.Currency     = dbItem.Currency;
                        dbBallance.BallanceDate = DateTime.Now;
                        dbBallance.UpdatedBy    = userId;
                        dbBallance.UpdatedDate  = DateTime.Now;
                        context.SaveChanges();

                        dbBallance.ClientPaymentBallanceUD = Library.Common.Helper.formatIndex(dbBallance.ClientPaymentBallanceID.ToString(), 8, "0");
                        context.SaveChanges();
                    }

                    context.SaveChanges();
                    dtoItem = GetData(string.Empty, 0, dbItem.ClientID.Value, dbItem.ClientPaymentID, out notification).Data;
                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }
Beispiel #4
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.ClientPayment dtoClientPayment = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.ClientPayment>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (ClientPaymentMngEntities context = CreateContext())
                {
                    ClientPayment dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new ClientPayment();
                        context.ClientPayment.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.ClientPayment.FirstOrDefault(o => o.ClientPaymentID == id);
                    }

                    if (dbItem == null)
                    {
                        throw new Exception("Payment not found!");
                    }
                    if (dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed.Value)
                    {
                        throw new Exception("Can not update the confirmed payment!");
                    }

                    using (DbContextTransaction scope = context.Database.BeginTransaction())
                    {
                        context.Database.ExecuteSqlCommand("SELECT * FROM ClientPayment WITH (TABLOCKX, HOLDLOCK)");

                        try
                        {
                            converter.DTO2DB(dtoClientPayment, ref dbItem);
                            // remove orphan
                            context.ClientPaymentDetail.Local.Where(o => o.ClientPayment == null).ToList().ForEach(o => context.ClientPaymentDetail.Remove(o));
                            context.ClientPaymentDeduction.Local.Where(o => o.ClientPaymentDetail == null).ToList().ForEach(o => context.ClientPaymentDeduction.Remove(o));

                            dbItem.UpdatedDate = DateTime.Now;
                            dbItem.UpdatedBy   = userId;
                            context.SaveChanges();
                            if (id == 0)
                            {
                                dbItem.ClientPaymentUD = Library.Common.Helper.formatIndex(dbItem.ClientPaymentID.ToString(), 8, "0");
                                context.SaveChanges();
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            scope.Commit();
                        }
                    }

                    // check if there's difference in total payment and detail
                    decimal totalPayment       = 0;
                    decimal totalDetailPayment = 0;
                    if (dbItem.Amount.HasValue)
                    {
                        totalPayment = dbItem.Amount.Value;
                    }
                    totalDetailPayment = dbItem.ClientPaymentDetail.Where(o => o.Amount.HasValue).Sum(o => o.Amount.Value);
                    if (totalPayment != totalDetailPayment)
                    {
                        ClientPaymentBallance dbBallance = dbItem.ClientPaymentBallance.FirstOrDefault();
                        if (dbBallance == null)
                        {
                            dbBallance = new ClientPaymentBallance();
                            dbBallance.ClientPayment = dbItem;
                            context.ClientPaymentBallance.Add(dbBallance);
                        }
                        dbBallance.Amount       = totalPayment - totalDetailPayment;
                        dbBallance.Currency     = dbItem.Currency;
                        dbBallance.BallanceDate = DateTime.Now;
                        dbBallance.UpdatedBy    = userId;
                        dbBallance.UpdatedDate  = DateTime.Now;
                        context.SaveChanges();

                        dbBallance.ClientPaymentBallanceUD = Library.Common.Helper.formatIndex(dbBallance.ClientPaymentBallanceID.ToString(), 8, "0");
                        context.SaveChanges();
                    }
                    else
                    {
                        ClientPaymentBallance dbBallance = dbItem.ClientPaymentBallance.FirstOrDefault();
                        context.ClientPaymentBallance.Remove(dbBallance);
                        context.SaveChanges();
                    }

                    dtoItem = GetData(string.Empty, 0, dbItem.ClientID.Value, dbItem.ClientPaymentID, out notification).Data;
                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }