Ejemplo n.º 1
0
        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);
            }
        }