Example #1
0
        //
        // CUSTOM FUNCTIONS
        //
        public bool ChangeStatus(int id, int statusId, ref DTO.WarehouseExportMng.WarehouseExport dtoItem, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };

            // only update the current data if the status is different than CONFIRMED
            if (dtoItem.ProcessingStatusID == 1)
            {
                // update current data
                if (!UpdateData(id, ref dtoItem, out notification))
                {
                    return(false);
                }
            }

            try
            {
                using (WarehouseExportMngEntities context = CreateContext())
                {
                    WarehouseExport dbItem = context.WarehouseExport.FirstOrDefault(o => o.WarehouseExportID == id);
                    if (dbItem != null)
                    {
                        dbItem.ProcessingStatusID = statusId;
                        dbItem.StatusUpdatedBy    = dtoItem.UpdatedBy;
                        dbItem.StatusUpdatedDate  = DateTime.Now;
                        context.SaveChanges();

                        dtoItem = GetData(id, out notification).Data;

                        return(true);
                    }
                    else
                    {
                        throw new Exception("Export receipt not found!");
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }
Example #2
0
        public void DTO2DB(DTO.WarehouseExportMng.WarehouseExport dtoItem, ref WarehouseExport dbItem)
        {
            // map main object
            AutoMapper.Mapper.Map <DTO.WarehouseExportMng.WarehouseExport, WarehouseExport>(dtoItem, dbItem);

            // map child collection
            // delete case
            List <int> toBeDeletedDetailIDs = new List <int>();

            foreach (WarehouseExportProductDetail toBeDeletedDetail in dbItem.WarehouseExportProductDetail)
            {
                if (!dtoItem.Details.Select(o => o.WarehouseExportProductDetailID).Contains(toBeDeletedDetail.WarehouseExportProductDetailID))
                {
                    toBeDeletedDetailIDs.Add(toBeDeletedDetail.WarehouseExportProductDetailID);
                }
            }
            foreach (int toBeDeletedDetailID in toBeDeletedDetailIDs)
            {
                dbItem.WarehouseExportProductDetail.Remove(dbItem.WarehouseExportProductDetail.FirstOrDefault(o => o.WarehouseExportProductDetailID == toBeDeletedDetailID));
            }

            foreach (DTO.WarehouseExportMng.WarehouseExportProductDetail dtoDetail in dtoItem.Details)
            {
                // add new case
                WarehouseExportProductDetail dbDetail = null;
                if (dtoDetail.WarehouseExportProductDetailID <= 0)
                {
                    dbDetail = new WarehouseExportProductDetail();
                    dbDetail.WarehouseExport = dbItem;
                    dbItem.WarehouseExportProductDetail.Add(dbDetail);
                }
                else
                {
                    dbDetail = dbItem.WarehouseExportProductDetail.FirstOrDefault(o => o.WarehouseExportProductDetailID == dtoDetail.WarehouseExportProductDetailID);
                }

                if (dbDetail != null)
                {
                    AutoMapper.Mapper.Map <DTO.WarehouseExportMng.WarehouseExportProductDetail, WarehouseExportProductDetail>(dtoDetail, dbDetail);
                }
            }
        }
Example #3
0
        public override bool DeleteData(int id, out Library.DTO.Notification notification)
        {
            bool result = false;

            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (WarehouseExportMngEntities context = CreateContext())
                {
                    WarehouseExport dbItem = context.WarehouseExport.FirstOrDefault(o => o.WarehouseExportID == id);
                    if (dbItem == null)
                    {
                        throw new Exception("Export receipt: " + dbItem.ReceiptNo + " not found!");
                    }
                    else
                    {
                        if (dbItem.ProcessingStatusID == 1)
                        {
                            context.WarehouseExport.Remove(dbItem);
                            context.SaveChanges();
                            result = true;
                        }
                        else
                        {
                            throw new Exception("Can not delete the approved/canceled export receipt");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
            }

            return(result);
        }
Example #4
0
        public override bool UpdateData(int id, ref DTO.WarehouseExportMng.WarehouseExport dtoItem, out Library.DTO.Notification notification)
        {
            bool result = false;

            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (WarehouseExportMngEntities context = CreateContext())
                {
                    WarehouseExport dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new WarehouseExport();
                        context.WarehouseExport.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.WarehouseExport.FirstOrDefault(o => o.WarehouseExportID == id);
                    }

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

                        // check if status = CONFIRMED
                        if (dbItem.ProcessingStatusID > 1)
                        {
                            throw new Exception(DALBase.Helper.TEXT_CONFIRMED_CONFLICT);
                        }

                        DateTime dateValue;
                        if (DateTime.TryParse(dtoItem.ExportedDate, new System.Globalization.CultureInfo("vi-VN"), System.Globalization.DateTimeStyles.None, out dateValue))
                        {
                            dbItem.ExportedDate = dateValue;
                        }

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

                        // remove orphans
                        context.WarehouseExportProductDetail.Local.Where(o => o.WarehouseExport == null).ToList().ForEach(o => context.WarehouseExportProductDetail.Remove(o));
                        context.SaveChanges();

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

                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
            }

            return(result);
        }