public void TestUpdateEquipment()
        {
            //Arrange
            CS_Equipment equipment = new CS_Equipment()
                                         {
                                             Active = true,
                                             ID = 1,
                                             Name = "Xyz",
                                             Status = "Up",
                                             HeavyEquipment = false
                                         };

            FakeObjectSet<CS_Equipment> fakeEquipment = new FakeObjectSet<CS_Equipment>();
            fakeEquipment.AddObject(equipment);

            Mock<IUnitOfWork> mockUnitWork = new Mock<IUnitOfWork>();
            mockUnitWork.Setup(w => w.CreateObjectSet<CS_Equipment>()).Returns(fakeEquipment);

            EquipmentModel model = new EquipmentModel(mockUnitWork.Object);

            //Act
            model.UpdateMaintenanceEquipment(equipment);
            CS_Equipment csEquipment = model.GetEquipment(1);

            //Assert

            Assert.AreEqual(Globals.EquipmentMaintenance.Status.Up.ToString(), csEquipment.Status, "Error on Status Field");
            Assert.AreEqual(false, csEquipment.HeavyEquipment, "Error on heavyequipment field.");
        }
        /// <summary>
        /// Set values get from page to the entity CS_Equipment
        /// </summary>
        /// <returns></returns>
        public CS_Equipment SetEquipment()
        {
            CS_Equipment equipment = new CS_Equipment();

            equipment.ID = _view.EquipmentID;
            equipment.ModificationDate = DateTime.Now;
            equipment.ModifiedBy = _view.Username;
            equipment.Status = _view.EquipmentStatus;

            return equipment;
        }
        /// <summary>
        /// Update the maintenance equipment
        /// </summary>
        /// <param name="equipment">entity CS_Equipment</param>
        public void UpdateMaintenanceEquipment(CS_Equipment equipment)
        {
            if (null == equipment)
                throw new Exception("The Equipment object is null!");

            try
            {
                _equipmentRepository.Update(equipment);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while trying to update the maintenance equipment.", ex);
            }
        }
        public void UpdateEquipmentDown(CS_EquipmentDownHistory equipmentDownHistory, CS_Equipment equipment)
        {
            CS_EquipmentDownHistory eDownHistory = GetEquipmentDownHistory(equipment.ID);

            if (equipment.Status == Globals.EquipmentMaintenance.Status.Down.ToString())
            {
                if (null != equipmentDownHistory)
                {
                    if (null != eDownHistory)
                    {
                        eDownHistory.DownHistoryStartDate = equipmentDownHistory.DownHistoryStartDate;
                        eDownHistory.Duration = equipmentDownHistory.Duration;
                        eDownHistory.ModificationDate = DateTime.Now;
                        eDownHistory.ModifiedBy = equipmentDownHistory.ModifiedBy;

                        //Update the equipmentdownhistory
                        UpdateEquipmentDownHistory(eDownHistory);
                    }
                    else
                    {
                        //Save the equipmentdownhistory
                        equipmentDownHistory.Active = true;
                        equipmentDownHistory.CreationDate = DateTime.Now;
                        equipmentDownHistory.CreatedBy = equipmentDownHistory.ModifiedBy;
                        equipmentDownHistory.DownHistoryEndDate = null;

                        SaveEquipmentDownHistory(equipmentDownHistory);
                    }
                }
            }
            else
            {
                if (null != eDownHistory)
                {
                    eDownHistory.Active = false;
                    eDownHistory.DownHistoryEndDate = equipmentDownHistory.DownHistoryEndDate;
                    eDownHistory.ModifiedBy = equipment.ModifiedBy;

                    //Update the maintenance equipmentdownhistory
                    UpdateEquipmentDownHistory(eDownHistory);
                }
            }
        }
        public void SaveEquipment(CS_Equipment equipment, CS_EquipmentCoverage equipmentCoverage, CS_EquipmentWhiteLight equipmentWhiteLight, IList<CS_EquipmentWhiteLight> comboEquipmentsWhiteLight, CS_EquipmentDownHistory equipmentDownHistory, IList<CS_EquipmentDownHistory> equipmentComboDownHistory, bool isHeavyEquipment, bool isSeasonal, bool displayInResourceAllocation, bool replicateToCombo, bool IsEquipmentCoverage, bool IsEquipmentWhiteLight, string username)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                #region [ Equipment/EquipmentDown ]
                SaveEquipment(equipment, equipmentDownHistory, equipmentComboDownHistory, isHeavyEquipment, isSeasonal, displayInResourceAllocation, replicateToCombo);
                #endregion

                #region [ EquipmentCoverage ]

                CS_EquipmentCoverage eCoverage = GetEquipmentCoverage(equipment.ID);

                if (IsEquipmentCoverage)
                {
                    if (null != eCoverage)
                    {
                        eCoverage.CoverageStartDate = equipmentCoverage.CoverageStartDate;
                        eCoverage.DivisionID = equipmentCoverage.DivisionID;
                        eCoverage.Duration = equipmentCoverage.Duration;
                        eCoverage.ModifiedBy = equipmentCoverage.ModifiedBy;
                        eCoverage.ModificationDate = equipmentCoverage.ModificationDate;

                        //Update equipmentcoverage
                        UpdateEquipmentCoverage(eCoverage);
                    }
                    else
                    {
                        //Save equipmentcoverage
                        SaveEquipmentCoverage(equipmentCoverage);

                        using (_callLogModel = new CallLogModel())
                        {
                            _callLogModel.CreateCoverageCallLogs(equipment.ID, equipmentCoverage, username);
                        }
                    }
                }
                else
                {
                    if (null != eCoverage)
                    {
                        eCoverage.Active = false;
                        eCoverage.CoverageStartDate = equipmentCoverage.CoverageStartDate;
                        eCoverage.DivisionID = equipmentCoverage.DivisionID;
                        eCoverage.Duration = equipmentCoverage.Duration;
                        if (equipmentCoverage.CoverageEndDate.HasValue)
                        {
                            eCoverage.CoverageEndDate = equipmentCoverage.CoverageEndDate.Value;
                            eCoverage.ModificationDate = equipmentCoverage.CoverageEndDate.Value;
                        }
                        eCoverage.ModifiedBy = equipmentCoverage.ModifiedBy;

                        //Update equipmentcoverage when user uncheck the coverage checkbox
                        UpdateEquipmentCoverage(eCoverage);
                    }
                }

                #endregion

                #region [ WhiteLight ]

                CS_EquipmentWhiteLight eWhiteLight = GetEquipmentWhiteLight(equipment.ID);

                if (null != eWhiteLight)
                {
                    if (!IsEquipmentWhiteLight)
                    {
                        equipmentWhiteLight.ID = eWhiteLight.ID;
                        equipmentWhiteLight.WhiteLightEndDate = DateTime.Now;
                        equipmentWhiteLight.Active = false;
                        equipmentWhiteLight.ModificationDate = DateTime.Now;
                        equipmentWhiteLight.ModifiedBy = equipment.ModifiedBy;
                        if (!string.IsNullOrEmpty(equipmentWhiteLight.Notes))
                            equipmentWhiteLight.Notes = eWhiteLight.Notes + "\n" + equipmentWhiteLight.Notes;

                        UpdateEquipmentWhiteLight(equipmentWhiteLight);
                        if (replicateToCombo)
                        {
                            for (int i = 0; i < comboEquipmentsWhiteLight.Count; i++)
                            {
                                CS_EquipmentWhiteLight eWhiteLightCombo = GetEquipmentWhiteLight(comboEquipmentsWhiteLight[i].EquipmentID);
                                comboEquipmentsWhiteLight[i].ID = eWhiteLightCombo.ID;
                                comboEquipmentsWhiteLight[i].WhiteLightEndDate = DateTime.Now;
                                comboEquipmentsWhiteLight[i].Active = false;
                                comboEquipmentsWhiteLight[i].ModificationDate = DateTime.Now;
                                comboEquipmentsWhiteLight[i].ModifiedBy = equipment.ModifiedBy;
                                if (!string.IsNullOrEmpty(comboEquipmentsWhiteLight[i].Notes))
                                    comboEquipmentsWhiteLight[i].Notes = eWhiteLight.Notes + "\r\n" + comboEquipmentsWhiteLight[i].Notes;
                                UpdateEquipmentWhiteLight(comboEquipmentsWhiteLight[i]);
                            }
                        }
                    }
                }
                else
                {
                    if (IsEquipmentWhiteLight)
                    {
                        SaveEquipmentWhiteLight(equipmentWhiteLight);
                        if (replicateToCombo)
                        {
                            for (int i = 0; i < comboEquipmentsWhiteLight.Count; i++)
                            {
                                SaveEquipmentWhiteLight(comboEquipmentsWhiteLight[i]);
                            }
                        }
                    }
                }
                #endregion

                scope.Complete();
            }
        }
        public void SaveEquipment(CS_Equipment equipment, CS_EquipmentDownHistory equipmentDownHistory, IList<CS_EquipmentDownHistory> equipmentComboDownHistory, bool? isHeavyEquipment, bool? isSeasonal, bool? displayInResourceAllocation, bool? replicateToCombo)
        {
            CS_Equipment equip = GetEquipment(equipment.ID);

            equip.ModificationDate = equipment.ModificationDate;
            equip.ModifiedBy = equipment.ModifiedBy;

            if (isSeasonal.HasValue)
                equip.Seasonal = isSeasonal.Value;

            if (isHeavyEquipment.HasValue)
                equip.HeavyEquipment = isHeavyEquipment.Value;

            if (displayInResourceAllocation.HasValue)
                equip.DisplayInResourceAllocation = displayInResourceAllocation.Value;

            if (equipment.Status == Globals.EquipmentMaintenance.Status.Down.ToString())
            {
                equip.Status = Globals.EquipmentMaintenance.Status.Down.ToString();
            }
            else
            {
                equip.Status = Globals.EquipmentMaintenance.Status.Up.ToString();
            }

            //Update the maintenance equipment
            UpdateMaintenanceEquipment(equip);

            //Update the equipmentdown
            UpdateEquipmentDown(equipmentDownHistory, equip);

            if (replicateToCombo.HasValue && replicateToCombo.Value)
            {
                CS_View_EquipmentInfo eqInfo = _equipmentInfoRepository.Get(e => e.EquipmentID == equipment.ID);
                if (eqInfo.IsPrimary == 1 && eqInfo.ComboID.HasValue)
                {
                    IList<CS_Equipment> equips = ListEquipmentsFromPrimaryEquipment(eqInfo.EquipmentID, eqInfo.ComboID.Value);
                    for (int j = 0; j < equips.Count; j++)
                    {
                        if (equipment.ID != equips[j].ID)
                        {
                            equips[j].ModificationDate = equipment.ModificationDate;
                            equips[j].ModifiedBy = equipment.ModifiedBy;

                            if (isHeavyEquipment.HasValue)
                                equips[j].HeavyEquipment = isHeavyEquipment.Value;

                            if (displayInResourceAllocation.HasValue)
                                equips[j].DisplayInResourceAllocation = displayInResourceAllocation.Value;

                            if (equipment.Status == Globals.EquipmentMaintenance.Status.Down.ToString())
                            {
                                equips[j].Status = Globals.EquipmentMaintenance.Status.Down.ToString();
                            }
                            else
                            {
                                equips[j].Status = Globals.EquipmentMaintenance.Status.Up.ToString();
                            }

                            //Update the maintenance equipment
                            UpdateMaintenanceEquipment(equips[j]);

                            CS_EquipmentDownHistory hist = equipmentComboDownHistory.Where(e => e.EquipmentID == equips[j].ID).First();

                            //Update the equipmentdown
                            UpdateEquipmentDown(hist, equips[j]);
                        }
                    }
                }
            }
        }
        public void SaveEquipment(CS_Equipment equipment, CS_EquipmentDownHistory equipmentDownHistory, bool? isHeavyEquipment)
        {
            CS_Equipment equip = GetEquipment(equipment.ID);

            equip.ModificationDate = equipment.ModificationDate;
            equip.ModifiedBy = equipment.ModifiedBy;

            if (isHeavyEquipment.HasValue)
                equip.HeavyEquipment = isHeavyEquipment.Value;

            if (equipment.Status == Globals.EquipmentMaintenance.Status.Down.ToString())
            {
                equip.Status = Globals.EquipmentMaintenance.Status.Down.ToString();
            }
            else
            {
                equip.Status = Globals.EquipmentMaintenance.Status.Up.ToString();
            }

            //Update the maintenance equipment
            UpdateMaintenanceEquipment(equip);

            //Update the equipmentdown
            UpdateEquipmentDown(equipmentDownHistory, equip);
        }