public void InsertAutoPart(AutoPartColumnModel model)
        {
            try
            {
                using (this.unitOfWork)
                {
                    AutoPart item = FetchAutoPartById(model.PartId);
                    if (item != null)
                    {
                        item.PartName = model.PartName;
                    }
                    else
                    {
                        item = new AutoPart()
                        {
                            PartName  = model.PartName,
                            IsDeleted = false
                        };

                        db.AddToAutoPart(item);
                    }

                    //Add details
                    if (model.AutoPartDetail != null)
                    {
                        AutoPartDetailColumnModel detail = model.AutoPartDetail;
                        AutoPartDetail            d      = new AutoPartDetail()
                        {
                            Make          = detail.Make,
                            Model         = detail.Model,
                            BrandId       = detail.BrandId,
                            IsDeleted     = false,
                            PartNumber    = detail.PartNumber,
                            Quantity      = detail.Quantity,
                            SellingPrice1 = detail.SellingPrice,
                            SellingPrice2 = detail.SellingPrice2,
                            Unit          = detail.Unit,
                            ReorderLimit  = detail.ReorderLimit,
                            BuyingPrice   = detail.PurchasePrice,
                            Size          = detail.Size,
                            Picture       = !string.IsNullOrWhiteSpace(detail.Picture) ? Path.GetFileName(detail.Picture) : string.Empty,
                            Description   = detail.Description
                        };

                        if (model.AutoPartDetail.AltPartNumbers.Count > 0)
                        {
                            foreach (string n in model.AutoPartDetail.AltPartNumbers)
                            {
                                AutoPartAltPartNumber altNumber = new AutoPartAltPartNumber()
                                {
                                    AltPartNumber = n
                                };
                                d.AutoPartAltPartNumber.Add(altNumber);
                            }
                        }

                        if (!string.IsNullOrWhiteSpace(model.AutoPartDetail.Picture))
                        {
                            SavePictureFile(model.AutoPartDetail.Picture);
                        }

                        item.AutoPartDetail.Add(d);
                    }

                    string action = string.Format("Added new Auto Part - {0} {1}", model.PartName, model.AutoPartDetail.PartNumber);
                    this.actionLogController.AddToLog(action, UserInfo.UserId);

                    this.unitOfWork.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void UpdateAutoPart(AutoPartColumnModel model)
        {
            try
            {
                using (this.unitOfWork)
                {
                    var item = FetchAutoPartDetailById(model.Id);
                    if (item != null)
                    {
                        //Auto Part
                        if (item.AutoPartId != null & item.AutoPartId.Value != model.PartId)
                        {
                            AutoPart part = FetchAutoPartById(model.PartId);
                            if (part != null)
                            {
                                item.AutoPartId = model.PartId;
                            }
                            else
                            {
                                AutoPart newPart = new AutoPart();
                                newPart.PartName  = model.PartName;
                                newPart.IsDeleted = false;

                                item.AutoPart = newPart;
                            }
                        }

                        if (item.AutoPartAltPartNumber.Any())
                        {
                            item.AutoPartAltPartNumber.Clear();
                        }

                        if (model.AutoPartDetail.AltPartNumbers.Any())
                        {
                            foreach (var alt in model.AutoPartDetail.AltPartNumbers)
                            {
                                if (!item.AutoPartAltPartNumber.Any(a => a.AltPartNumber == alt))
                                {
                                    item.AutoPartAltPartNumber.Add(
                                        new AutoPartAltPartNumber()
                                    {
                                        AltPartNumber = alt
                                    });
                                }
                            }
                        }

                        //Auto Part Detail
                        if (model.AutoPartDetail != null)
                        {
                            item.Make       = model.AutoPartDetail.Make;
                            item.Model      = model.AutoPartDetail.Model;
                            item.BrandId    = model.AutoPartDetail.BrandId;
                            item.PartNumber = model.AutoPartDetail.PartNumber;
                            //item.AltPartNumber = model.AutoPartDetail.AltPartNumber;
                            item.SellingPrice1 = model.AutoPartDetail.SellingPrice;
                            item.SellingPrice2 = model.AutoPartDetail.SellingPrice2;
                            item.Unit          = model.AutoPartDetail.Unit;
                            item.ReorderLimit  = model.AutoPartDetail.ReorderLimit;
                            item.BuyingPrice   = model.AutoPartDetail.PurchasePrice;
                            item.Size          = model.AutoPartDetail.Size;
                            item.Description   = model.AutoPartDetail.Description;
                            item.Quantity      = model.AutoPartDetail.Quantity;

                            if (!string.IsNullOrWhiteSpace(item.Picture))
                            {
                                RemovePictureFile(item.Picture);
                            }

                            if (!string.IsNullOrWhiteSpace(model.AutoPartDetail.Picture))
                            {
                                if (string.Compare(item.Picture, model.AutoPartDetail.Picture, true) != 0)
                                {
                                    item.Picture = Path.GetFileName(model.AutoPartDetail.Picture);
                                    SavePictureFile(model.AutoPartDetail.Picture);
                                }
                            }
                            else
                            {
                                item.Picture = string.Empty;
                            }
                        }
                    }

                    string action = string.Format("Updated Auto Part - {0}", model.PartName);
                    this.actionLogController.AddToLog(action, UserInfo.UserId);

                    this.unitOfWork.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }