public void DeleteSparepart(SparepartViewModel sparepart, int userId)
        {
            DateTime serverTime = DateTime.Now;
            List<SparepartDetail> details = _sparepartDetailRepository.GetMany(spd => spd.SparepartId == sparepart.Id).ToList();
            foreach (var iDetails in details)
            {
                iDetails.Status = (int)DbConstant.DefaultDataStatus.Deleted;
                iDetails.ModifyDate = serverTime;
                iDetails.ModifyUserId = userId;
                _sparepartDetailRepository.AttachNavigation(iDetails.CreateUser);
                _sparepartDetailRepository.AttachNavigation(iDetails.ModifyUser);
                _sparepartDetailRepository.AttachNavigation(iDetails.PurchasingDetail);
                _sparepartDetailRepository.AttachNavigation(iDetails.Sparepart);
                _sparepartDetailRepository.AttachNavigation(iDetails.SparepartManualTransaction);
                _sparepartDetailRepository.Update(iDetails);
            }

            sparepart.Status = (int)DbConstant.DefaultDataStatus.Deleted;
            sparepart.ModifyDate = serverTime;
            sparepart.ModifyUserId = userId;
            Sparepart entity = _sparepartRepository.GetById(sparepart.Id);
            Map(sparepart, entity);
            _sparepartRepository.AttachNavigation(entity.CreateUser);
            _sparepartRepository.AttachNavigation(entity.ModifyUser);
            _sparepartRepository.AttachNavigation(entity.CategoryReference);
            _sparepartRepository.AttachNavigation(entity.UnitReference);
            _sparepartRepository.Update(entity);

            _unitOfWork.SaveChanges();
        }
 public void UpdateSparepart(SparepartViewModel sparepart, int userId)
 {
     DateTime serverTime = DateTime.Now;
     sparepart.ModifyDate = serverTime;
     sparepart.ModifyUserId = userId;
     Sparepart entity = _sparepartRepository.GetById(sparepart.Id);
     Map(sparepart, entity);
     _sparepartRepository.AttachNavigation(entity.CreateUser);
     _sparepartRepository.AttachNavigation(entity.ModifyUser);
     _sparepartRepository.AttachNavigation(entity.CategoryReference);
     _sparepartRepository.AttachNavigation(entity.UnitReference);
     _sparepartRepository.Update(entity);
     _unitOfWork.SaveChanges();
 }
 public void InsertSparepart(SparepartViewModel sparepart, int userId)
 {
     DateTime serverTime = DateTime.Now;
     sparepart.CreateDate = serverTime;
     sparepart.CreateUserId = userId;
     sparepart.ModifyDate = serverTime;
     sparepart.ModifyUserId = userId;
     sparepart.Status = (int)DbConstant.DefaultDataStatus.Active;
     Sparepart entity = new Sparepart();
     Map(sparepart, entity);
     _sparepartRepository.AttachNavigation(entity.CreateUser);
     _sparepartRepository.AttachNavigation(entity.ModifyUser);
     _sparepartRepository.AttachNavigation(entity.CategoryReference);
     _sparepartRepository.AttachNavigation(entity.UnitReference);
     _sparepartRepository.Add(entity);
     _unitOfWork.SaveChanges();
 }
        public bool ApproveSPK(SPKViewModel spk, List<SPKDetailSparepartViewModel> spkSparepartList, List<SPKDetailSparepartDetailViewModel> spkSparepartDetailList, int userId, bool isApproved, out List<SparepartViewModel> warningList)
        {
            bool result = false;
            bool hasParent = false;

            warningList = new List<SparepartViewModel>();

            DateTime serverTime = DateTime.Now;
            SPK spkParent = _SPKRepository.GetById(spk.SPKParentId);

            if (spkParent != null)
            {
                hasParent = true;
            }

            if (isApproved)
            {
                SPK entity = _SPKRepository.GetById(spk.Id);
                entity.StatusApprovalId = (int)DbConstant.ApprovalStatus.Approved;
                entity.StatusPrintId = (int)DbConstant.SPKPrintStatus.Ready;
                entity.ModifyDate = serverTime;
                entity.ModifyUserId = userId;

                _SPKRepository.Update(entity);
                _unitOfWork.SaveChanges();

                foreach (var item in spkSparepartList)
                {
                    Sparepart sparepart = _sparepartRepository.GetById(item.Sparepart.Id);
                    sparepart.StockQty = sparepart.StockQty - item.TotalQuantity;
                    sparepart.ModifyDate = serverTime;
                    sparepart.ModifyUserId = userId;

                    _sparepartRepository.Update(sparepart);

                    if (sparepart.StockQty <= GetStockThreshold())
                    {
                        SparepartViewModel viewModel = new SparepartViewModel();
                        Map(sparepart, viewModel);
                        warningList.Add(viewModel);
                    }
                }

                _unitOfWork.SaveChanges();

                foreach (var item in spkSparepartDetailList)
                {
                    SparepartDetail sparepartDetail = _sparepartDetailRepository.GetById(item.SparepartDetail.Id);
                    sparepartDetail.ModifyDate = serverTime;
                    sparepartDetail.ModifyUserId = userId;
                    if (item.SPKDetailSparepart.SPK.CategoryReference.Id == 22)
                    {
                        sparepartDetail.Status = (int)DbConstant.SparepartDetailDataStatus.OutPurchase;
                    }
                    else
                    {
                        sparepartDetail.Status = (int)DbConstant.SparepartDetailDataStatus.OutService;
                    }

                    _sparepartDetailRepository.Update(sparepartDetail);
                }

                _unitOfWork.SaveChanges();

                result = true;
            }
            else
            {

                SPK entity = _SPKRepository.GetById(spk.Id);
                entity.StatusApprovalId = (int)DbConstant.ApprovalStatus.Approved;
                entity.StatusPrintId = (int)DbConstant.SPKPrintStatus.Ready;
                entity.ModifyDate = serverTime;
                entity.ModifyUserId = userId;

                if (spk.SPKParent == null)
                {
                    entity.SPKParent = null;
                }
                _SPKRepository.Update(entity);

                _unitOfWork.SaveChanges();

                if (hasParent)
                {
                    spkParent.StatusApprovalId = (int)DbConstant.ApprovalStatus.Approved;
                    spkParent.StatusCompletedId = (int)DbConstant.SPKCompletionStatus.InProgress;
                    spkParent.Status = (int)DbConstant.DefaultDataStatus.Active;

                    spkParent.ModifyDate = serverTime;
                    spkParent.ModifyUserId = userId;

                    _SPKRepository.Update(spkParent);

                    _unitOfWork.SaveChanges();
                }
                result = true;
            }

            return result;
        }
 public override void RefreshDataView()
 {
     if (!bgwMain.IsBusy)
     {
         MethodBase.GetCurrentMethod().Info("Fecthing sparepart data...");
         _selectedSparepart = null;
         FormHelpers.CurrentMainForm.UpdateStatusInformation("Memuat data sparepart...", false);
         bgwMain.RunWorkerAsync();
     }
 }