Ejemplo n.º 1
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.FactoryCreditNote dtoFactoryCreditNote = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryCreditNote>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            int    number;
            string indexName;

            try
            {
                // check permission
                if (fwFactory.CheckSupplierPermission(userId, dtoFactoryCreditNote.SupplierID.Value) == 0)
                {
                    throw new Exception("Current user don't have access permission for the selected supplier data");
                }
                if (id > 0 && fwFactory.CheckFactoryCreditNotePermission(userId, id) == 0)
                {
                    throw new Exception("Current user don't have access permission for the selected credit note data");
                }

                using (FactoryCreditNoteMngEntities context = CreateContext())
                {
                    FactoryCreditNote dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new FactoryCreditNote();
                        context.FactoryCreditNote.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.FactoryCreditNote.FirstOrDefault(o => o.FactoryCreditNoteID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "Factory credit note not found!";
                        return(false);
                    }
                    else
                    {
                        // check if credit note is confirmed
                        if (dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed.Value)
                        {
                            throw new Exception("Can not edit the confirmed credit note!");
                        }

                        // check concurrency
                        if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoFactoryCreditNote.ConcurrencyFlag)))
                        {
                            throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT);
                        }

                        converter.DTO2DB(dtoFactoryCreditNote, ref dbItem);
                        dbItem.UpdatedBy   = userId;
                        dbItem.UpdatedDate = DateTime.Now;

                        // remove orphan
                        context.FactoryCreditNoteDetail.Local.Where(o => o.FactoryCreditNote == null).ToList().ForEach(o => context.FactoryCreditNoteDetail.Remove(o));

                        context.SaveChanges();
                    }
                    dtoItem = GetData(userId, dbItem.FactoryCreditNoteID, -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 "CreditNoteNoUnique":
                        notification.Message = "Duplicate credit note number (credit note 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);
            }
        }