Exemple #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 (CostInvoiceMngEntities context = CreateContext())
                {
                    CostInvoice dbItem = context.CostInvoice.FirstOrDefault(o => o.CostInvoiceID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "Invoice not found!";
                        return(false);
                    }
                    else
                    {
                        context.CostInvoice.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);
            }
        }
        public void DTO2DB_CostInvoice(DTO.CostInvoiceMng.CostInvoice dtoItem, ref CostInvoice dbItem)
        {
            List <CostInvoiceDetail>       itemNeedDelete_Details = new List <CostInvoiceDetail>();
            List <CostInvoiceDetailExtend> itemNeedDelete_DetailExtends;

            if (dtoItem.CostInvoiceDetails != null)
            {
                //CHECK
                foreach (CostInvoiceDetail dbDetail in dbItem.CostInvoiceDetail)
                {
                    //DB NOT EXIST IN DTO
                    if (!dtoItem.CostInvoiceDetails.Select(s => s.CostInvoiceDetailID).Contains(dbDetail.CostInvoiceDetailID))
                    {
                        itemNeedDelete_Details.Add(dbDetail);
                    }
                    else //DB IS EXIST IN DB
                    {
                        itemNeedDelete_DetailExtends = new List <CostInvoiceDetailExtend>();
                        foreach (CostInvoiceDetailExtend dbDetailExt in dbDetail.CostInvoiceDetailExtend)
                        {
                            if (!dtoItem.CostInvoiceDetails.Where(o => o.CostInvoiceDetailID == dbDetail.CostInvoiceDetailID).FirstOrDefault().CostInvoiceDetailExtends.Select(x => x.CostInvoiceDetailExtendID).Contains(dbDetailExt.CostInvoiceDetailExtendID))
                            {
                                itemNeedDelete_DetailExtends.Add(dbDetailExt);
                            }
                        }
                        foreach (CostInvoiceDetailExtend dbDetailExt in itemNeedDelete_DetailExtends)
                        {
                            dbDetail.CostInvoiceDetailExtend.Remove(dbDetailExt);
                        }
                    }
                }

                foreach (CostInvoiceDetail dbDetail in itemNeedDelete_Details)
                {
                    List <CostInvoiceDetailExtend> item_deleteds = new List <CostInvoiceDetailExtend>();
                    foreach (CostInvoiceDetailExtend dbDetailExtend in dbDetail.CostInvoiceDetailExtend)
                    {
                        item_deleteds.Add(dbDetailExtend);
                    }
                    foreach (CostInvoiceDetailExtend item in item_deleteds)
                    {
                        dbItem.CostInvoiceDetail.Where(o => o.CostInvoiceDetailID == dbDetail.CostInvoiceDetailID).FirstOrDefault().CostInvoiceDetailExtend.Remove(item);
                    }
                    dbItem.CostInvoiceDetail.Remove(dbDetail);
                }

                //MAP
                CostInvoiceDetail       _dbDetail;
                CostInvoiceDetailExtend _dbDetailExtend;
                foreach (DTO.CostInvoiceMng.CostInvoiceDetail dtoDetail in dtoItem.CostInvoiceDetails)
                {
                    dtoDetail.CostType = dtoItem.CostType;
                    dtoDetail.ClientID = dtoItem.ClientID;

                    if (dtoDetail.CostInvoiceDetailID < 0)
                    {
                        _dbDetail = new CostInvoiceDetail();

                        if (dtoDetail.CostInvoiceDetailExtends != null)
                        {
                            foreach (DTO.CostInvoiceMng.CostInvoiceDetailExtend dtoDetailExtend in dtoDetail.CostInvoiceDetailExtends)
                            {
                                _dbDetailExtend = new CostInvoiceDetailExtend();
                                _dbDetail.CostInvoiceDetailExtend.Add(_dbDetailExtend);
                                AutoMapper.Mapper.Map <DTO.CostInvoiceMng.CostInvoiceDetailExtend, CostInvoiceDetailExtend>(dtoDetailExtend, _dbDetailExtend);
                            }
                        }

                        dbItem.CostInvoiceDetail.Add(_dbDetail);
                    }
                    else
                    {
                        _dbDetail = dbItem.CostInvoiceDetail.FirstOrDefault(o => o.CostInvoiceDetailID == dtoDetail.CostInvoiceDetailID);
                        if (_dbDetail != null && dtoDetail.CostInvoiceDetailExtends != null)
                        {
                            foreach (DTO.CostInvoiceMng.CostInvoiceDetailExtend dtoDetailExtend in dtoDetail.CostInvoiceDetailExtends)
                            {
                                if (dtoDetailExtend.CostInvoiceDetailExtendID < 0)
                                {
                                    _dbDetailExtend = new CostInvoiceDetailExtend();
                                    _dbDetail.CostInvoiceDetailExtend.Add(_dbDetailExtend);
                                }
                                else
                                {
                                    _dbDetailExtend = _dbDetail.CostInvoiceDetailExtend.FirstOrDefault(o => o.CostInvoiceDetailExtendID == dtoDetailExtend.CostInvoiceDetailExtendID);
                                }
                                if (_dbDetailExtend != null)
                                {
                                    AutoMapper.Mapper.Map <DTO.CostInvoiceMng.CostInvoiceDetailExtend, CostInvoiceDetailExtend>(dtoDetailExtend, _dbDetailExtend);
                                }
                            }
                        }
                    }

                    if (_dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.CostInvoiceMng.CostInvoiceDetail, CostInvoiceDetail>(dtoDetail, _dbDetail);
                    }
                }
            }

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

            if (dtoItem.CostInvoiceID > 0)
            {
                dbItem.UpdatedBy   = dtoItem.UpdatedBy;
                dbItem.UpdatedDate = DateTime.Now;
            }
            else
            {
                dbItem.CreatedBy   = dtoItem.UpdatedBy;
                dbItem.CreatedDate = DateTime.Now;
            }
            dbItem.InvoiceDate = dtoItem.InvoiceDateFormated.ConvertStringToDateTime();
        }
Exemple #3
0
        public override bool UpdateData(int id, ref DTO.CostInvoiceMng.CostInvoice dtoItem, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (CostInvoiceMngEntities context = CreateContext())
                {
                    CostInvoice dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new CostInvoice();
                        context.CostInvoice.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.CostInvoice.FirstOrDefault(o => o.CostInvoiceID == id);
                    }

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

                        converter.DTO2DB_CostInvoice(dtoItem, ref dbItem);

                        //remove orphan item
                        context.CostInvoiceDetailExtend.Local.Where(o => o.CostInvoiceDetail == null).ToList().ForEach(o => context.CostInvoiceDetailExtend.Remove(o));
                        context.CostInvoiceDetail.Local.Where(o => o.CostInvoice == null).ToList().ForEach(o => context.CostInvoiceDetail.Remove(o));

                        //save data
                        context.SaveChanges();

                        //reload data
                        dtoItem = GetData(dbItem.CostInvoiceID, out notification);

                        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);
            }
        }