Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
20 kB
17
Indexable
Never
public class MD_BorrowFormViewModel : FormsLookupNavigationAware
{
    private readonly IRegionManager regionManager;
    private readonly IFormPrintOutTemplateService formPrintOutTemplateService;
    private readonly IDialogService dialogService;
    private readonly IReferenceNoProcessor referenceNoProcessor;
    private readonly IEventAggregator ea;
    private readonly BorrowFormServices borrowFormServices;
    LoadingScreen loading = new LoadingScreen();

    public DelegateCommand PrintListCommand { get; private set; }
    public DelegateCommand LoadDataToFormCommand { get; private set; }
    public DelegateCommand RefreshListCommand { get; private set; }
    public DelegateCommand ReturnDeviceCommand { get; private set; }
    public DelegateCommand GenerateReferenceNoCommand { get; private set; }
    public DelegateCommand ViewIMTELogsCommand { get; private set; }

    private string _title = $"Borrow Device";
    public string Title
    {
        get { return _title; }
        set { SetProperty(ref _title, value); }
    }

    private bool _isDataSaving;
    public bool IsDataSaving
    {
        get { return _isDataSaving; }
        set { SetProperty(ref _isDataSaving, value); }
    }

    private string _filterText;
    public string FilterText
    {
        get { return _filterText; }
        set
        {
            if (value != null)
            {
                LoadDataToList(value, OrderText);
                SetProperty(ref _filterText, value);
            }
        }
    }

    private string _orderText;
    public string OrderText
    {
        get { return _orderText; }
        set
        {
            if (value != null)
            {
                LoadDataToList(FilterText, value);
                SetProperty(ref _orderText, value);
            }
        }
    }

    private EmployeeShiftGroupDTO _selectedIssuedBy = new EmployeeShiftGroupDTO();
    public EmployeeShiftGroupDTO SelectedIssuedBy
    {
        get { return _selectedIssuedBy; }
        set
        {
            if (value != null)
            {
                MainDeviceLedger.IssuedByEmployee = value.Employee;
                SetProperty(ref _selectedIssuedBy, value);
            }
        }
    }

    private EmployeeShiftGroupDTO _selectedIssuedTo = new EmployeeShiftGroupDTO();
    public EmployeeShiftGroupDTO SelectedIssuedTo
    {
        get { return _selectedIssuedTo; }
        set
        {
            if (value != null)
            {
                MainDeviceLedger.IssuedToEmployee = value.Employee;
                MainDeviceLedger.TransferToDepartment = value.Employee.PrimaryDepartment;
                SetProperty(ref _selectedIssuedTo, value);
            }
        }
    }

    public MD_BorrowFormViewModel(
        IRegionManager regionManager,
        IFormPrintOutTemplateService formPrintOutTemplateService,
        IDialogService dialogService,
        IReferenceNoProcessor referenceNoProcessor,
        IEventAggregator ea,
        BorrowFormServices borrowFormServices) : base(ea)
    {
        this.regionManager = regionManager;
        this.formPrintOutTemplateService = formPrintOutTemplateService;
        this.dialogService = dialogService;
        this.referenceNoProcessor = referenceNoProcessor;
        this.ea = ea;
        this.borrowFormServices = borrowFormServices;

        PrintListCommand = new DelegateCommand(PrintList);
        LoadDataToFormCommand = new DelegateCommand(LoadDataToForm);
        RefreshListCommand = new DelegateCommand(RefreshList);
        ReturnDeviceCommand = new DelegateCommand(ReturnDevice);
        GenerateReferenceNoCommand = new DelegateCommand(GenerateReferenceNo);
        ViewIMTELogsCommand = new DelegateCommand(ViewIMTELogs);

        SaveCommand = new DelegateCommand(SaveData);

        LoadData();
    }

    private void ViewIMTELogs()
    {
        var dialogParameter = new DialogParameters();
        dialogParameter.Add("deviceId", MeasuringDevice.Id);

        dialogService.ShowDialog("IMTELogs", dialogParameter, null);
    }

    private async void GenerateReferenceNo()
    {
        loading.Show();

        try
        {
            var jsonString = await referenceNoProcessor.RetrieveReferenceNo();
            JObject jsonObject = JObject.Parse(jsonString);

            MainDeviceLedger.RefNo = jsonObject["Result"].ToString();
        }
        catch (Exception ex)
        {
            loading.Close();
            MessageBoxDialog.ShowExceptionDialog(dialogService, ex);
        }
        finally
        {
            loading.Close();
        }
    }

    private void ReturnDevice()
    {
        var parameter = new DialogParameters();
        parameter.Add("DeviceBorrowedData", MeasuringDeviceLedgers[0]);
        parameter.Add("MeasuringDeviceObj", MeasuringDevice);

        dialogService.ShowDialog("ReturnDialog", parameter, r =>
        {
            LoadDataToList();

            if (MeasuringDeviceLedgers != null && MeasuringDeviceLedgers.Count > 0 && MeasuringDeviceLedgers[0].DeviceStatus.Id == 7)
                CanReturnDevice = true;
            else
                CanReturnDevice = false;
        });
    }

    private void RefreshList()
    {
        MeasuringDeviceLedgers?.Clear();
        LoadDataToList();
    }

    private void LoadDataToForm()
    {
        if (MeasuringDevicesHelper.IsSelectedItemValid<MeasuringDeviceLedgerDTO>(SelectedLedgerData, dialogService))
        {
            MainDeviceLedger = new MeasuringDeviceLedgerDTO
            {
                TransactionDate = SelectedLedgerData.TransactionDate,
                IssuedByEmployee = SelectedLedgerData.IssuedByEmployee,
                IssuedToEmployee = SelectedLedgerData.IssuedToEmployee,
                Plant = SelectedLedgerData.Plant,
                TransferToDepartment = SelectedLedgerData.TransferToDepartment,
                Remarks = SelectedLedgerData.Remarks,
            };
        }
    }

    private async void PrintList()
    {
        try
        {
            var data = new
            {
                MeasuringDevice = MeasuringDevice,
                MeasuringDeviceLedgerList = MeasuringDeviceLedgers,
                CompanyName = GlobalValues.SelectedCompany.CompanyName,
                DateExported = DateTime.Now
            };
            await FileProcessorHelper.ExportFile(formPrintOutTemplateService, dialogService, ReportFileType.Ledger, data);
        }
        catch (Exception ex)
        {
            MessageBoxDialog.ShowExceptionDialog(dialogService, ex);
        }
    }

    private async void LoadDataToList()
    {
        try
        {
            MeasuringDeviceLedgers.Clear();

            var ledgers = await borrowFormServices.MeasuringDeviceLedgerService.GetMDLedgerByMDId(MeasuringDevice.Id);
            MeasuringDeviceLedgers = new AsyncObservableCollection<MeasuringDeviceLedgerDTO>(ledgers);
        }
        catch (Exception ex)
        {
            MessageBoxDialog.ShowExceptionDialog(dialogService, ex);
        }
    }

    private async void LoadDataToList(string condition, string sortCondition)
    {
        try
        {
            MeasuringDeviceLedgers.Clear();

            var ledgers = await borrowFormServices.MeasuringDeviceLedgerService.GetMDLedgerByMDId(MeasuringDevice.Id, condition, sortCondition);
            MeasuringDeviceLedgers = new AsyncObservableCollection<MeasuringDeviceLedgerDTO>(ledgers);
        }
        catch (Exception ex)
        {
            MessageBoxDialog.ShowExceptionDialog(dialogService, ex);
        }
    }

    private async void LoadData()
    {
        try
        {
            var issuedByEmployees = await borrowFormServices.EmployeeService.GetEmployeesAsync();
            if (IssuedBys != null) IssuedBys.Clear();
            IssuedBys = new AsyncObservableCollection<EmployeeShiftGroupDTO>(issuedByEmployees);

            var issuedToEmployees = await borrowFormServices.EmployeeService.GetEmployeeShiftGroups();
            if (IssuedTos != null) IssuedTos.Clear();
            IssuedTos = new AsyncObservableCollection<EmployeeShiftGroupDTO>(issuedToEmployees);

            var plants = await borrowFormServices.PlantService.GetPlants();
            if (Plants != null) Plants.Clear();
            Plants = new AsyncObservableCollection<PlantDTO>(plants);

            var departments = await borrowFormServices.DepartmentService.GetDepartmentsAsync();
            if (Departments != null) Departments.Clear();
            Departments = new AsyncObservableCollection<DepartmentDTO>(departments);

            var workOrders = await borrowFormServices.WorkOrderService.GetWorkOrders();
            if (WorkOrder != null) WorkOrder.Clear();
            WorkOrder = new AsyncObservableCollection<WorkOrderDTO>(workOrders);

            var deviceStatuses = await borrowFormServices.DeviceStatusService.GetDeviceStatusesAsync();
            if (DeviceStatuses != null) DeviceStatuses.Clear();
            DeviceStatuses = new AsyncObservableCollection<DeviceStatusDTO>(deviceStatuses);

            MainDeviceLedger.DeviceStatus = DeviceStatuses.FirstOrDefault(x => x.Status.ToLower() == "borrow") ?? new DeviceStatusDTO();
            SelectedIssuedBy = IssuedBys.FirstOrDefault(x => x.Employee.Id == GlobalValues.EmployeeLoggedIn.Id) ?? new EmployeeShiftGroupDTO();

            LoadDataToList();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.StackTrace, e.Message + " " + e.Source);
        }
    }

    #region INavigationAware

    public override void OnNavigatedTo(NavigationContext navigationContext)
    {
        var ledgers = navigationContext.Parameters["measuringDeviceLedgerObj"] as AsyncObservableCollection<MeasuringDeviceLedgerDTO> ?? new AsyncObservableCollection<MeasuringDeviceLedgerDTO>();
        var deviceData = navigationContext.Parameters["measuringDeviceObj"] as MeasuringDeviceDTO ?? new MeasuringDeviceDTO();

        MeasuringDeviceLedgers = new AsyncObservableCollection<MeasuringDeviceLedgerDTO>(ledgers);
        MeasuringDevice = new MeasuringDeviceDTO(deviceData);


        //IsDataSaving = (bool)navigationContext.Parameters["IsDataSaving"];

        if (MeasuringDeviceLedgers != null && MeasuringDeviceLedgers.Count > 0 && MeasuringDeviceLedgers[0].DeviceStatus.Status.ToLower() == "borrow")
            CanReturnDevice = true;
        else
            CanReturnDevice = false;

        ButtonControlLogic(true, false, false, false, false);
    }

    public override bool IsNavigationTarget(NavigationContext navigationContext) => true;

    #endregion

    #region Full Property

    private MeasuringDeviceDTO _measuringDevice = new MeasuringDeviceDTO();
    public MeasuringDeviceDTO MeasuringDevice
    {
        get { return _measuringDevice; }
        set { SetProperty(ref _measuringDevice, value); }
    }

    private MeasuringDeviceLedgerDTO _mainDeviceLedger = new MeasuringDeviceLedgerDTO();
    public MeasuringDeviceLedgerDTO MainDeviceLedger
    {
        get { return _mainDeviceLedger; }
        set { SetProperty(ref _mainDeviceLedger, value); }
    }

    private MeasuringDeviceLedgerDTO _selectedLedgerData = new MeasuringDeviceLedgerDTO();
    public MeasuringDeviceLedgerDTO SelectedLedgerData
    {
        get { return _selectedLedgerData; }
        set { SetProperty(ref _selectedLedgerData, value); }
    }

    private bool _isIssuedToEmployee;
    public bool IsIssuedToEmployee
    {
        get { return _isIssuedToEmployee; }
        set { SetProperty(ref _isIssuedToEmployee, value); }
    }

    private bool _isIssuedByEmployee;
    public bool IsIssuedByEmployee
    {
        get { return _isIssuedByEmployee; }
        set { SetProperty(ref _isIssuedByEmployee, value); }
    }

    private bool _canReturnDevice;
    public bool CanReturnDevice
    {
        get { return _canReturnDevice; }
        set { SetProperty(ref _canReturnDevice, value); }
    }

    #endregion

    #region Observable Collections

    private AsyncObservableCollection<EmployeeShiftGroupDTO> _issuedBys;
    public AsyncObservableCollection<EmployeeShiftGroupDTO> IssuedBys
    {
        get { return _issuedBys; }
        set { SetProperty(ref _issuedBys, value); }
    }

    private AsyncObservableCollection<EmployeeShiftGroupDTO> _issuedTos;
    public AsyncObservableCollection<EmployeeShiftGroupDTO> IssuedTos
    {
        get { return _issuedTos; }
        set { SetProperty(ref _issuedTos, value); }
    }

    private AsyncObservableCollection<PlantDTO> _plants;
    public AsyncObservableCollection<PlantDTO> Plants
    {
        get { return _plants; }
        set { SetProperty(ref _plants, value); }
    }

    private AsyncObservableCollection<DepartmentDTO> _departments;
    public AsyncObservableCollection<DepartmentDTO> Departments
    {
        get { return _departments; }
        set { SetProperty(ref _departments, value); }
    }

    private AsyncObservableCollection<DeviceStatusDTO> _deviceStatuses;
    public AsyncObservableCollection<DeviceStatusDTO> DeviceStatuses
    {
        get { return _deviceStatuses; }
        set { SetProperty(ref _deviceStatuses, value); }
    }

    private AsyncObservableCollection<WorkOrderDTO> _workOrder;
    public AsyncObservableCollection<WorkOrderDTO> WorkOrder
    {
        get { return _workOrder; }
        set { SetProperty(ref _workOrder, value); }
    }

    private AsyncObservableCollection<MeasuringDeviceLedgerDTO> _measuringDeviceLedgers;
    public AsyncObservableCollection<MeasuringDeviceLedgerDTO> MeasuringDeviceLedgers
    {
        get { return _measuringDeviceLedgers; }
        set { SetProperty(ref _measuringDeviceLedgers, value); }
    }


    #endregion

    public override void SetWorkOrderLookup(WorkOrderDTO obj)
    {
        MainDeviceLedger.WorkOrder = obj;
    }

    public override void SetPlantFromLookup(PlantDTO obj)
    {
        MainDeviceLedger.Plant = obj;
    }

    public override void SetDepartmentFromLookup(DepartmentDTO obj)
    {
        MainDeviceLedger.TransferToDepartment = obj;
    }

    public override void SetIssuedEmployeeLookup(EmployeeDTO obj)
    {
        if (IsIssuedToEmployee)
            SelectedIssuedTo = IssuedTos.FirstOrDefault(x => x.Employee.Id == obj.Id) ?? new EmployeeShiftGroupDTO();

        if (IsIssuedByEmployee)
            SelectedIssuedBy = IssuedBys.FirstOrDefault(x => x.Employee.Id == obj.Id) ?? new EmployeeShiftGroupDTO();
    }

    public override void OpenWorkOrderLookup()
    {
        dialogService.ShowDialog("WorkOrderConfig");
    }

    public override void OpenPlantLookup()
    {
        dialogService.ShowDialog("PlantConfig");
    }

    public override void OpenDepartmentLookup()
    {
        dialogService.ShowDialog("DeptConfig");
    }

    public override void OpenIssuedToEmployeeLookup()
    {
        IsIssuedToEmployee = true;
        IsIssuedByEmployee = false;

        var parameter = new DialogParameters();
        parameter.Add("IsIssuedTo", true);

        dialogService.ShowDialog("EmpConfig", parameter, null);
    }

    public override void OpenIssuedByEmployeeLookup()
    {
        IsIssuedToEmployee = false;
        IsIssuedByEmployee = true;

        var parameter = new DialogParameters();
        parameter.Add("IsIssuedTo", false);

        dialogService.ShowDialog("EmpConfig", parameter, null);
    }

    public override void CreateNew()
    {
        try
        {
            if (MeasuringDevicesHelper.IsDeviceCanBorrow(dialogService, MeasuringDevice))
            {
                IsDataSaving = true;

                MainDeviceLedger = new MeasuringDeviceLedgerDTO();
                ButtonControlLogic(true, false, true, true, false);

                MainDeviceLedger.DeviceStatus = DeviceStatuses.FirstOrDefault(x => x.Status.ToLower() == "borrow") ?? new DeviceStatusDTO();
                SelectedIssuedBy = IssuedBys.FirstOrDefault(x => x.Employee.Id == GlobalValues.EmployeeLoggedIn.Id) ?? new EmployeeShiftGroupDTO();
            }
        }
        catch (Exception ex)
        {
            MessageBoxDialog.ShowExceptionDialog(dialogService, ex);
        }
    }

    public override void Cancel()
    {
        IsDataSaving = false;
        ButtonControlLogic(true, false, false, false, false);
    }

    public override void Delete()
    {
        // No Functionality for delete
    }

    public override void Update()
    {
        ButtonControlLogic(true, false, true, true, false);
    }

    public async override void SaveData()
    {
        try
        {
            // will trigger if the selected device latest ledger is currently borrow
            if (MeasuringDeviceLedgers[0].DeviceStatus.Status.ToLower() == "borrow")
            {
                MessageBoxDialog.ShowDialog(dialogService, "Selected device currently borrowed", MessageBoxDialog.DialogIcon.ExclamationCircleSolid);
            }
            else
            {
                MainDeviceLedger.MeasuringDevice = MeasuringDevice;
                MainDeviceLedger.MeasuringDevice.Plant = MainDeviceLedger.Plant;
                MainDeviceLedger.MeasuringDevice.Department = MainDeviceLedger.TransferToDepartment;
                MainDeviceLedger.MeasuringDevice.DeviceStatus = MainDeviceLedger.DeviceStatus;

                await borrowFormServices.MeasuringDeviceLedgerService.ExecuteBorrowMeasuringDeviceTransaction(MainDeviceLedger);
                MessageBoxDialog.ShowDialog(dialogService, "Information Saved", MessageBoxDialog.DialogIcon.ThumbsUpSolid);
                LoadDataToList();
                IsDataSaving = false;
                CanReturnDevice = true;
            }
        }
        catch (Exception ex)
        {
            MessageBoxDialog.ShowExceptionDialog(dialogService, ex);
        }
    }

    public override void UpdateData()
    {
        IsDataSaving = true;
        ButtonControlLogic(true, false, true, true, false);
    }

    public override void Dispose()
    {
        base.Dispose();

        foreach (var ob in MeasuringDeviceLedgers)
        {
            ob.Dispose();
        }

        foreach (var item in Departments)
        {
            item.Dispose();
        }

        foreach (var item in IssuedBys)
        {
            item.Dispose();
        }

        foreach (var item in IssuedTos)
        {
            item.Dispose();
        }

        WorkOrder?.Clear();
        WorkOrder = null;

        Plants?.Clear();
        Plants = null;

        IssuedTos.Clear();
        IssuedTos = null;

        IssuedBys.Clear();
        IssuedBys = null;

        Departments?.Clear();
        Departments = null;

        DeviceStatuses?.Clear();
        DeviceStatuses = null;

        MeasuringDeviceLedgers?.Clear();
        MeasuringDeviceLedgers = null;

        SelectedIssuedTo?.Dispose();
        SelectedIssuedTo = null;

        SelectedIssuedBy?.Dispose();
        SelectedIssuedBy = null;

        SelectedLedgerData?.Dispose();
        SelectedLedgerData = null;

        MainDeviceLedger?.Dispose();
        MainDeviceLedger = null;

        MeasuringDevice?.Dispose();
        MeasuringDevice = null;
    }
}
Leave a Comment