public IHttpActionResult Update(int id, DTO.LoadingPlanMng.LoadingPlan dtoItem)
        {
            Library.DTO.Notification notification;

            // authentication
            Module.Framework.BLL fwBll = new Module.Framework.BLL();
            if (id > 0 && !fwBll.CanPerformAction(ControllerContext.GetAuthUserId(), moduleCode, Library.DTO.ModuleAction.CanUpdate))
            {
                // edit case
                return(InternalServerError(new Exception(Properties.Resources.NOT_AUTHORIZED)));
            }
            else if (id == 0 && !fwBll.CanPerformAction(ControllerContext.GetAuthUserId(), moduleCode, Library.DTO.ModuleAction.CanCreate))
            {
                // create new case
                return(InternalServerError(new Exception(Properties.Resources.NOT_AUTHORIZED)));
            }

            // validation
            if (!Helper.CommonHelper.ValidateDTO <DTO.LoadingPlanMng.LoadingPlan>(dtoItem, out notification))
            {
                return(Ok(new Library.DTO.ReturnData <DTO.LoadingPlanMng.LoadingPlan>()
                {
                    Data = dtoItem, Message = notification
                }));
            }

            // continue processing
            BLL.LoadingPlanMng bll = new BLL.LoadingPlanMng(Helper.AuthHelper.GetCurrentUserFolder(ControllerContext));
            bll.UpdateData(id, ref dtoItem, ControllerContext.GetAuthUserId(), out notification);

            return(Ok(new Library.DTO.ReturnData <DTO.LoadingPlanMng.LoadingPlan>()
            {
                Data = dtoItem, Message = notification
            }));
        }
        public void DTO2DB(DTO.LoadingPlanMng.LoadingPlan dtoItem, ref LoadingPlan dbItem)
        {
            // map parent
            AutoMapper.Mapper.Map <DTO.LoadingPlanMng.LoadingPlan, LoadingPlan>(dtoItem, dbItem);
            dbItem.LoadingDate  = dtoItem.LoadingDate.ConvertStringToDateTime();
            dbItem.SentDate     = dtoItem.SentDate.ConvertStringToDateTime();
            dbItem.ShipmentDate = dtoItem.ShipmentDate.ConvertStringToDateTime();
            dbItem.ShippedDate  = dtoItem.ShippedDate.ConvertStringToDateTime();
            dbItem.UpdatedDate  = DateTime.Now;

            // map child - detail
            if (dtoItem.Details != null && dtoItem.Details.Count > 0)
            {
                foreach (LoadingPlanDetail dbDetailToBeDeleted in dbItem.LoadingPlanDetail.ToArray())
                {
                    if (!dtoItem.Details.Select(o => o.LoadingPlanDetailID).Contains(dbDetailToBeDeleted.LoadingPlanDetailID))
                    {
                        dbItem.LoadingPlanDetail.Remove(dbDetailToBeDeleted);
                    }
                }

                // create + update
                foreach (DTO.LoadingPlanMng.LoadingPlanDetail dtoDetail in dtoItem.Details)
                {
                    LoadingPlanDetail dbDetail;
                    if (dtoDetail.LoadingPlanDetailID <= 0)
                    {
                        //create new loading plandetail
                        dbDetail             = new LoadingPlanDetail();
                        dbDetail.LoadingPlan = dbItem;
                        dbItem.LoadingPlanDetail.Add(dbDetail);
                    }
                    else
                    {
                        //get loadingplandetail to updated
                        dbDetail = dbItem.LoadingPlanDetail.FirstOrDefault(o => o.LoadingPlanDetailID == dtoDetail.LoadingPlanDetailID);
                    }
                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.LoadingPlanMng.LoadingPlanDetail, LoadingPlanDetail>(dtoDetail, dbDetail);
                    }
                }
            }

            // map child - sparepart
            if (dtoItem.Spareparts != null && dtoItem.Spareparts.Count > 0)
            {
                // delete case
                foreach (LoadingPlanSparepartDetail dbDetailToBeDeleted in dbItem.LoadingPlanSparepartDetail.ToArray())
                {
                    if (!dtoItem.Spareparts.Select(o => o.LoadingPlanSparepartDetailID).Contains(dbDetailToBeDeleted.LoadingPlanSparepartDetailID))
                    {
                        dbItem.LoadingPlanSparepartDetail.Remove(dbDetailToBeDeleted);
                    }
                }

                // create + update
                foreach (DTO.LoadingPlanMng.LoadingPlanSparepartDetail dtoDetail in dtoItem.Spareparts)
                {
                    LoadingPlanSparepartDetail dbDetail;
                    if (dtoDetail.LoadingPlanSparepartDetailID <= 0)
                    {
                        dbDetail             = new LoadingPlanSparepartDetail();
                        dbDetail.LoadingPlan = dbItem;
                        dbItem.LoadingPlanSparepartDetail.Add(dbDetail);
                    }
                    else
                    {
                        //get loadingplan sparepart detail to updated
                        dbDetail = dbItem.LoadingPlanSparepartDetail.FirstOrDefault(o => o.LoadingPlanSparepartDetailID == dtoDetail.LoadingPlanSparepartDetailID);
                    }

                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.LoadingPlanMng.LoadingPlanSparepartDetail, LoadingPlanSparepartDetail>(dtoDetail, dbDetail);
                    }
                }
            }

            // map child - sample
            if (dtoItem.SampleProducts != null && dtoItem.SampleProducts.Count > 0)
            {
                foreach (LoadingPlanSampleDetail dbDetailToBeDeleted in dbItem.LoadingPlanSampleDetail.ToArray())
                {
                    if (!dtoItem.SampleProducts.Select(o => o.LoadingPlanSampleDetailID).Contains(dbDetailToBeDeleted.LoadingPlanSampleDetailID))
                    {
                        dbItem.LoadingPlanSampleDetail.Remove(dbDetailToBeDeleted);
                    }
                }

                // create + update
                foreach (DTO.LoadingPlanMng.LoadingPlanSampleProductDetail dtoDetail in dtoItem.SampleProducts)
                {
                    LoadingPlanSampleDetail dbDetail;
                    if (dtoDetail.LoadingPlanSampleDetailID <= 0)
                    {
                        //create new loading plandetail
                        dbDetail             = new LoadingPlanSampleDetail();
                        dbDetail.LoadingPlan = dbItem;
                        dbItem.LoadingPlanSampleDetail.Add(dbDetail);
                    }
                    else
                    {
                        //get loadingplandetail to updated
                        dbDetail = dbItem.LoadingPlanSampleDetail.FirstOrDefault(o => o.LoadingPlanSampleDetailID == dtoDetail.LoadingPlanSampleDetailID);
                    }
                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.LoadingPlanMng.LoadingPlanSampleProductDetail, LoadingPlanSampleDetail>(dtoDetail, dbDetail);
                    }
                }
            }
        }
 public DTO.LoadingPlanMng.LoadingPlan DB2DTO_LoadingPlan(LoadingPlanMng_LoadingPlan_View dbItem)
 {
     DTO.LoadingPlanMng.LoadingPlan dtoItem = Mapper.Map <LoadingPlanMng_LoadingPlan_View, DTO.LoadingPlanMng.LoadingPlan>(dbItem);
     dtoItem.ConcurrencyFlag_String = Convert.ToBase64String(dtoItem.ConcurrencyFlag);
     return(dtoItem);
 }