Ejemplo n.º 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 (AVFPurchasingInvoiceMngEntities context = CreateContext())
                {
                    AVFPurchasingInvoice dbItem = context.AVFPurchasingInvoice.FirstOrDefault(o => o.AVFPurchasingInvoiceID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "Invoice not found!";
                        return(false);
                    }
                    else
                    {
                        foreach (AVFPurchasingInvoiceDetail detail in dbItem.AVFPurchasingInvoiceDetail.ToArray())
                        {
                            context.AVFPurchasingInvoiceDetail.Remove(detail);
                        }

                        context.AVFPurchasingInvoice.Remove(dbItem);
                        context.SaveChanges();

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                notification.DetailMessage.Add(ex.Message);
                if (ex.GetBaseException() != null)
                {
                    notification.DetailMessage.Add(ex.GetBaseException().Message);
                }
                return(false);
            }
        }
Ejemplo n.º 2
0
        public void DTO2BD(DTO.AVFPurchasingInvoice dtoItem, ref AVFPurchasingInvoice dbItem)
        {
            dbItem.InvoiceDate  = dtoItem.InvoiceDate.ConvertStringToDateTime();
            dbItem.DocumentDate = dtoItem.DocumentDate.ConvertStringToDateTime();

            AutoMapper.Mapper.Map <DTO.AVFPurchasingInvoice, AVFPurchasingInvoice>(dtoItem, dbItem);

            if (dtoItem.AVFPurchasingInvoiceDetails != null)
            {
                //check for child rows deleted
                foreach (AVFPurchasingInvoiceDetail dbDetail in dbItem.AVFPurchasingInvoiceDetail.ToArray())
                {
                    if (!dtoItem.AVFPurchasingInvoiceDetails.Select(o => o.AVFPurchasingInvoiceDetailID).Contains(dbDetail.AVFPurchasingInvoiceDetailID))
                    {
                        dbItem.AVFPurchasingInvoiceDetail.Remove(dbDetail);
                    }
                }

                //map child row
                foreach (DTO.AVFPurchasingInvoiceDetail dtoDetail in dtoItem.AVFPurchasingInvoiceDetails)
                {
                    AVFPurchasingInvoiceDetail dbDetail;
                    if (dtoDetail.AVFPurchasingInvoiceDetailID <= 0)
                    {
                        dbDetail = new AVFPurchasingInvoiceDetail();
                        dbItem.AVFPurchasingInvoiceDetail.Add(dbDetail);
                    }
                    else
                    {
                        dbDetail = dbItem.AVFPurchasingInvoiceDetail.FirstOrDefault(o => o.AVFPurchasingInvoiceDetailID == dtoDetail.AVFPurchasingInvoiceDetailID);
                    }

                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.AVFPurchasingInvoiceDetail, AVFPurchasingInvoiceDetail>(dtoDetail, dbDetail);
                    }
                }
                AutoMapper.Mapper.Map <DTO.AVFPurchasingInvoice, AVFPurchasingInvoice>(dtoItem, dbItem);
            }
        }
Ejemplo n.º 3
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.AVFPurchasingInvoice dtoAVFPurchasingInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.AVFPurchasingInvoice>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (AVFPurchasingInvoiceMngEntities context = CreateContext())
                {
                    AVFPurchasingInvoice dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new AVFPurchasingInvoice();
                        context.AVFPurchasingInvoice.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.AVFPurchasingInvoice.FirstOrDefault(o => o.AVFPurchasingInvoiceID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "Invoice not found!";
                        return(false);
                    }
                    else
                    {
                        // check concurrency
                        if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoAVFPurchasingInvoice.ConcurrencyFlag_String)))
                        {
                            throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT);
                        }

                        //convert dto to db
                        converter.DTO2BD(dtoAVFPurchasingInvoice, ref dbItem);
                        //remove orphan item
                        context.AVFPurchasingInvoiceDetail.Local.Where(o => o.AVFPurchasingInvoice == null).ToList().ForEach(o => context.AVFPurchasingInvoiceDetail.Remove(o));

                        // processing file
                        if (dtoAVFPurchasingInvoice.PDFFileScan_HasChange)
                        {
                            dbItem.PDFFileScan = fwFactory.CreateNoneImageFilePointer(this._tempFolder, dtoAVFPurchasingInvoice.PDFFileScan_NewFile, dtoAVFPurchasingInvoice.PDFFileScan);
                        }

                        dbItem.UpdatedDate = DateTime.Now;
                        dbItem.UpdatedBy   = userId;

                        context.SaveChanges();
                        dtoItem = GetData(dbItem.AVFPurchasingInvoiceID, out notification).Data;

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }