public bool DeleteColliPiece(int id, out Library.DTO.Notification notification)
 {
     notification = new Library.DTO.Notification()
     {
         Type = Library.DTO.NotificationType.Success, Message = "Delete set piece success"
     };
     try
     {
         using (ProductMngEntities context = CreateContext())
         {
             var x = context.ProductColliPiece.Where(o => o.ProductColliPieceID == id).FirstOrDefault();
             context.ProductColliPiece.Remove(x);
             context.ProductColliPiece.Local.Where(o => o.ProductColli == null).ToList().ForEach(o => context.ProductColliPiece.Remove(o));
             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 DTO.ProductMng.ProductColliPiece UpdateColliPiece(DTO.ProductMng.ProductColliPiece dtoItem, out Library.DTO.Notification notification)
 {
     notification = new Library.DTO.Notification()
     {
         Type = Library.DTO.NotificationType.Success, Message = "Update colli piece success"
     };
     try
     {
         using (ProductMngEntities context = CreateContext())
         {
             var dbColliPiece = context.ProductColliPiece.Where(o => o.ProductColliPieceID == dtoItem.ProductColliPieceID).FirstOrDefault();
             dbColliPiece.PieceID = dtoItem.PieceID;
             dbColliPiece.Colli   = dtoItem.Colli;
             dbColliPiece.Pcs     = dtoItem.Pcs;
             dbColliPiece.ColliPieceDescription = dtoItem.ColliPieceDescription;
             context.SaveChanges();
         }
         return(dtoItem);
     }
     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(dtoItem);
     }
 }
 public override bool Reset(int id, ref DTO.ProductMng.Product dtoItem, out Library.DTO.Notification notification)
 {
     notification = new Library.DTO.Notification()
     {
         Type = Library.DTO.NotificationType.Success
     };
     try
     {
         using (ProductMngEntities context = CreateContext())
         {
             Product product = context.Product.FirstOrDefault(o => o.ProductID == id);
             product.IsConfirmed   = false;
             product.ConfirmedDate = null;
             product.ConfirmedBy   = null;
             context.SaveChanges();
             return(true);
         }
     }
     catch (Exception ex)
     {
         notification.Type    = Library.DTO.NotificationType.Error;
         notification.Message = ex.Message;
         return(false);
     }
 }
        public override bool Approve(int id, ref DTO.ProductMng.Product dtoItem, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (ProductMngEntities context = CreateContext())
                {
                    Product product = context.Product.FirstOrDefault(o => o.ProductID == id);

                    if (!product.MaterialID.HasValue || !product.ModelID.HasValue || !product.MaterialTypeID.HasValue || !product.MaterialColorID.HasValue || !product.ManufacturerCountryID.HasValue || !product.FrameMaterialID.HasValue || !product.FrameMaterialColorID.HasValue || !product.SubMaterialColorID.HasValue || !product.SubMaterialID.HasValue || !product.CushionColorID.HasValue || !product.CushionID.HasValue)
                    {
                        throw new Exception("Please fill in all the required data for current product - Model, Material, Cushion, Frame and Manufacturer Country is required");
                    }

                    product.IsConfirmed   = true;
                    product.ConfirmedDate = DateTime.Now;
                    product.ConfirmedBy   = dtoItem.UpdatedBy;
                    context.SaveChanges();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }
        public DTO.ProductMng.ProductColli CreateColli(int productSetEANCodeID, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success, Message = "Create set ean code success"
            };
            DTO.ProductMng.ProductColli dtoColli = new DTO.ProductMng.ProductColli();
            dtoColli.ProductColliID = -1;
            try
            {
                using (ProductMngEntities context = CreateContext())
                {
                    var          dbSetEanCode = context.ProductSetEANCode.Where(o => o.ProductSetEANCodeID == productSetEANCodeID).FirstOrDefault();
                    ProductColli dbColli      = new ProductColli();
                    dbSetEanCode.ProductColli.Add(dbColli);

                    //finding lasted eancode
                    var x = dbSetEanCode.ProductColli.OrderByDescending(o => o.ColliIndex).First();
                    int i = (x.ColliIndex.HasValue ? x.ColliIndex.Value : 0) + 1;
                    dbColli.ColliIndex = i;
                    dbColli.EANCode    = dbSetEanCode.EANCode + "-" + i.ToString().PadLeft(2, '0');
                    context.SaveChanges();
                    dtoColli.ProductColliID = dbColli.ProductColliID;
                    dtoColli.EANCode        = dbColli.EANCode;
                    dtoColli.ColliIndex     = dbColli.ColliIndex;

                    if (i == 1)
                    {
                        var dbProduct = context.ProductMng_Product_View.FirstOrDefault(o => o.ProductID == dbSetEanCode.ProductID);

                        if (dbProduct != null)
                        {
                            dtoColli.PackagingMethodID = dbProduct.PackagingMethodID;
                            dtoColli.CartonBoxDimH     = dbProduct.CartonBoxDimH;
                            dtoColli.CartonBoxDimL     = dbProduct.CartonBoxDimL;
                            dtoColli.CartonBoxDimW     = dbProduct.CartonBoxDimW;
                            dtoColli.NetWeight         = dbProduct.NetWeight2;
                            dtoColli.GrossWeight       = dbProduct.GrossWeight2;
                        }
                    }
                }

                return(dtoColli);
            }
            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(dtoColli);
            }
        }
        public DTO.ProductMng.ProductColliPiece CreateColliPiece(int productColliID, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success, Message = "Create set ean code success"
            };
            DTO.ProductMng.ProductColliPiece dtoColliPiece = new DTO.ProductMng.ProductColliPiece();
            dtoColliPiece.ProductColliPieceID = -1;
            try
            {
                using (ProductMngEntities context = CreateContext())
                {
                    var dbColli = context.ProductColli.Where(o => o.ProductColliID == productColliID).FirstOrDefault();
                    ProductColliPiece dbColliPiece = new ProductColliPiece();
                    dbColli.ProductColliPiece.Add(dbColliPiece);
                    dbColliPiece.Pcs   = 1;
                    dbColliPiece.Colli = 1;

                    //if product is not SET then PieceID is same ProductID
                    int?productID = dbColli.ProductSetEANCode.ProductID;
                    var x         = context.ProductMng_Product_View.Where(o => o.ProductID == productID).FirstOrDefault();
                    if (x == null)
                    {
                        throw new Exception("Could not find product type of product");
                    }
                    string productTypeNM = x.ProductTypeNM;
                    if (productTypeNM != "SET")
                    {
                        dbColliPiece.PieceID = productID;
                    }
                    //save data
                    context.SaveChanges();

                    //reload data to dto
                    dtoColliPiece.ProductColliPieceID = dbColliPiece.ProductColliPieceID;
                    dtoColliPiece.Pcs             = dbColliPiece.Pcs;
                    dtoColliPiece.Colli           = dbColliPiece.Colli;
                    dtoColliPiece.PieceID         = dbColliPiece.PieceID;
                    dtoColliPiece.PiceDescription = context.ProductMng_ProductColliPiece_View.Where(o => o.ProductColliPieceID == dtoColliPiece.ProductColliPieceID).FirstOrDefault().PiceDescription;
                }
                return(dtoColliPiece);
            }
            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(dtoColliPiece);
            }
        }
        //ean code new feature

        public DTO.ProductMng.ProductSetEANCode CreateSetEanCode(int productID, string eanCode, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success, Message = "Create set ean code success"
            };
            DTO.ProductMng.ProductSetEANCode dtoSetEanCode = new DTO.ProductMng.ProductSetEANCode();
            dtoSetEanCode.ProductSetEANCodeID = -1;
            try
            {
                using (ProductMngEntities context = CreateContext())
                {
                    var p = context.Product.Where(o => o.ProductID == productID).FirstOrDefault();
                    ProductSetEANCode dbSetEanCode = new ProductSetEANCode();
                    p.ProductSetEANCode.Add(dbSetEanCode);

                    //if (string.IsNullOrEmpty(eanCodeIndex))
                    //{
                    //    //finding lasted eancode
                    //    var x = context.ProductSetEANCode.OrderByDescending(o => o.EANCodeIndex).FirstOrDefault();
                    //    int i = (x == null ? 1 : (x.EANCodeIndex.HasValue ? x.EANCodeIndex.Value : 0) + 1);
                    //    dbSetEanCode.EANCodeIndex = i;
                    //    dbSetEanCode.EANCode = "871969928" + i.ToString().PadLeft(4, '0');
                    //}
                    //else
                    //{
                    //    dbSetEanCode.EANCode = "871969928" + eanCodeIndex;
                    //    dbSetEanCode.EANCodeIndex = Convert.ToInt32(eanCodeIndex);
                    //}

                    dbSetEanCode.EANCode      = eanCode;
                    dbSetEanCode.EANCodeIndex = Convert.ToInt32(eanCode.Substring(9, 4));
                    context.SaveChanges();
                    //create dto to return
                    dtoSetEanCode = new DTO.ProductMng.ProductSetEANCode();
                    dtoSetEanCode.ProductSetEANCodeID = dbSetEanCode.ProductSetEANCodeID;
                    dtoSetEanCode.EANCode             = dbSetEanCode.EANCode;
                    dtoSetEanCode.EANCodeIndex        = dbSetEanCode.EANCodeIndex;
                }
                return(dtoSetEanCode);
            }
            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(dtoSetEanCode);
            }
        }
        public override bool DeleteData(int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (ProductMngEntities context = CreateContext())
                {
                    Product dbItem = context.Product.FirstOrDefault(o => o.ProductID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "Product not found!";
                        return(false);
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(dbItem.ImageFile))
                        {
                            // remove image
                            fwFactory.RemoveImageFile(dbItem.ImageFile);
                        }

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

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message
                };
                return(false);
            }
        }
        public override bool UpdateData(int id, ref DTO.ProductMng.Product dtoItem, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (ProductMngEntities context = CreateContext())
                {
                    Product dbItem = null;
                    if (id == 0)
                    {
                        dbItem             = new Product();
                        dbItem.CreatedBy   = dtoItem.CreatedBy;
                        dbItem.CreatedDate = DateTime.Now;
                        context.Product.Add(dbItem);
                    }
                    else
                    {
                        dbItem             = context.Product.FirstOrDefault(o => o.ProductID == id);
                        dbItem.UpdatedBy   = dtoItem.UpdatedBy;
                        dbItem.UpdatedDate = DateTime.Now;
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "Product 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(dtoItem, ref dbItem);

                        // processing image
                        if (dtoItem.ImageFile_HasChange)
                        {
                            dbItem.ImageFile      = fwFactory.CreateFilePointer(this._TempFolder, dtoItem.ImageFile_NewFile, dtoItem.ImageFile);
                            dbItem.IsMatchedImage = true;
                        }

                        context.ProductPiece.Local.Where(o => o.Product == null).ToList().ForEach(o => context.ProductPiece.Remove(o));
                        context.SaveChanges();

                        dtoItem = GetData(dbItem.ProductID, 0, out notification).Data;
                        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);
            }
        }