public override bool DeleteData(int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };

            try
            {
                using (ForwarderInvoiceMngEntities context = CreateContext())
                {
                    ForwarderInvoice dbItem = context.ForwarderInvoices.FirstOrDefault(o => o.ForwarderInvoiceID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "Forwarder Invoice not found!";
                        return(false);
                    }
                    else
                    {
                        context.ForwarderInvoices.Remove(dbItem);
                        context.SaveChanges();

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }
        public override bool UpdateData(int id, ref DTO.ForwarderInvoiceMng.ForwarderInvoice dtoItem, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try {
                using (ForwarderInvoiceMngEntities context = CreateContext())
                {
                    ForwarderInvoice dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new ForwarderInvoice();
                        context.ForwarderInvoices.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.ForwarderInvoices.FirstOrDefault(o => o.ForwarderInvoiceID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "Forwarder Invoice not found";
                        return(false);
                    }
                    else
                    {
                        converter.DTO2BD(dtoItem, ref dbItem);
                        context.SaveChanges();

                        // processing file
                        if (dtoItem.PDFFileScan_HasChange)
                        {
                            dbItem.PDFFileScan = fwFactory.CreateNoneImageFilePointer(this._TempFolder, dtoItem.PDFFileScan_NewFile, dtoItem.PDFFileScan);
                        }
                        context.SaveChanges();

                        dtoItem = GetData(dbItem.ForwarderInvoiceID, out notification).Data;

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

                return(false);
            }
        }
Beispiel #3
0
        public void DTO2BD(DTO.ForwarderInvoiceMng.ForwarderInvoice dtoItem, ref ForwarderInvoice dbItem)
        {
            AutoMapper.Mapper.Map <DTO.ForwarderInvoiceMng.ForwarderInvoice, ForwarderInvoice>(dtoItem, dbItem);
            dbItem.UpdatedDate = DateTime.Now;
            if (!string.IsNullOrEmpty(dtoItem.InvoiceDate))
            {
                try
                {
                    dbItem.InvoiceDate = DateTime.ParseExact(dtoItem.InvoiceDate, "d", new System.Globalization.CultureInfo("vi-VN"));
                }
                catch
                {
                    dbItem.InvoiceDate = null;
                }
            }

            if (!string.IsNullOrEmpty(dtoItem.DueDate))
            {
                try
                {
                    dbItem.DueDate = DateTime.ParseExact(dtoItem.DueDate, "d", new System.Globalization.CultureInfo("vi-VN"));
                }
                catch
                {
                    dbItem.DueDate = null;
                }
            }

            //map pieces
            if (dtoItem.ForwarderInvoiceDetails != null)
            {
                // check for child rows deleted
                foreach (ForwarderInvoiceDetail dbDetail in dbItem.ForwarderInvoiceDetails.ToArray())
                {
                    if (!dtoItem.ForwarderInvoiceDetails.Select(o => o.ForwarderInvoiceDetailID).Contains(dbDetail.ForwarderInvoiceDetailID))
                    {
                        dbItem.ForwarderInvoiceDetails.Remove(dbDetail);
                    }
                }

                // map child rows
                foreach (DTO.ForwarderInvoiceMng.ForwarderInvoiceDetail dtoDetail in dtoItem.ForwarderInvoiceDetails)
                {
                    ForwarderInvoiceDetail dbDetail;
                    if (dtoDetail.ForwarderInvoiceDetailID <= 0)
                    {
                        dbDetail = new ForwarderInvoiceDetail();
                        dbItem.ForwarderInvoiceDetails.Add(dbDetail);
                    }
                    else
                    {
                        dbDetail = dbItem.ForwarderInvoiceDetails.FirstOrDefault(o => o.ForwarderInvoiceDetailID == dtoDetail.ForwarderInvoiceDetailID);
                    }

                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.ForwarderInvoiceMng.ForwarderInvoiceDetail, ForwarderInvoiceDetail>(dtoDetail, dbDetail);
                    }
                }
            }
        }