示例#1
0
        public void DTO2DB_CashBook(DTO.CashBookData dataItem, ref CashBook dbItem)
        {
            CashBookDocument dbDocument;

            foreach (CashBookDocument item in dbItem.CashBookDocument.ToArray())
            {
                if (!dataItem.CashBookDocuments.Select(s => s.CashBookDocumentID).Contains(item.CashBookDocumentID))
                {
                    dbItem.CashBookDocument.Remove(item);
                }
            }

            foreach (DTO.CashBookDocumentData dataDocument in dataItem.CashBookDocuments)
            {
                if (dataDocument.CashBookDocumentID <= 0)
                {
                    dbDocument = new CashBookDocument
                    {
                        FileUD = dataDocument.FileUD
                    };
                    dbItem.CashBookDocument.Add(dbDocument);
                }
                else
                {
                    dbDocument = dbItem.CashBookDocument.FirstOrDefault(o => o.CashBookDocumentID == dataDocument.CashBookDocumentID);
                }

                if (dbDocument != null)
                {
                    AutoMapper.Mapper.Map <DTO.CashBookDocumentData, CashBookDocument>(dataDocument, dbDocument);
                }
            }

            if (!string.IsNullOrEmpty(dataItem.BookDate))
            {
                if (DateTime.TryParse(dataItem.BookDate, nl, System.Globalization.DateTimeStyles.None, out DateTime tmpDate))
                {
                    dbItem.BookDate = tmpDate;
                }
            }

            AutoMapper.Mapper.Map <DTO.CashBookData, CashBook>(dataItem, dbItem);
        }
示例#2
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification)
        {
            CashBookData dataItem = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <CashBookData>();

            notification = new Notification()
            {
                Type = NotificationType.Success
            };

            try
            {
                using (var context = CreateContext())
                {
                    CashBook dbItem;

                    if (id > 0)
                    {
                        dbItem = context.CashBook.FirstOrDefault(o => o.CashBookID == id);

                        if (dbItem == null)
                        {
                            notification.Type    = NotificationType.Error;
                            notification.Message = "Can not found data!";

                            return(false);
                        }
                    }
                    else
                    {
                        dbItem = context.CashBook.FirstOrDefault(o => o.CashBookUD == dataItem.CashBookUD);

                        if (dbItem != null)
                        {
                            notification.Type    = NotificationType.Error;
                            notification.Message = "CashBook receipt is exist in database!";

                            return(false);
                        }

                        DateTime bookDate = new DateTime();

                        if (!string.IsNullOrEmpty(dataItem.BookDate))
                        {
                            if (DateTime.TryParse(dataItem.BookDate, nl, System.Globalization.DateTimeStyles.None, out DateTime tmpDate))
                            {
                                bookDate = tmpDate;
                            }
                        }

                        var cashBalance = context.CashBookBalanceMng_CashBookBalance_View.OrderByDescending(o => o.UpdatedDate).FirstOrDefault();

                        if (!IsValidCreateCashBook(bookDate, cashBalance, out string messageError, out NotificationType typeError))
                        {
                            notification.Type    = typeError;
                            notification.Message = messageError;

                            return(false);
                        }

                        dbItem = new CashBook();
                        context.CashBook.Add(dbItem);
                    }

                    foreach (CashBookDocumentData item in dataItem.CashBookDocuments)
                    {
                        Module.Framework.DAL.DataFactory factory = new Framework.DAL.DataFactory();

                        if (item.HasChange)
                        {
                            if (string.IsNullOrEmpty(item.NewFile))
                            {
                                factory.RemoveImageFile(item.FileUD);
                            }
                            else
                            {
                                item.FileUD = factory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", item.NewFile, item.FileUD);
                            }
                        }
                    }

                    converter.DTO2DB_CashBook(dataItem, ref dbItem);
                    dbItem.UpdatedBy   = userId;
                    dbItem.UpdatedDate = DateTime.Now;

                    context.CashBookDocument.Local.Where(o => o.CashBook == null).ToList().ForEach(o => context.CashBookDocument.Remove(o));

                    context.SaveChanges();

                    dtoItem = converter.DB2DTO_CashBook(context.CashBookMng_CashBook_View.FirstOrDefault(o => o.CashBookID == dbItem.CashBookID));
                }

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

                return(false);
            }
        }