public async Task <OperationResult> Update(RentableItemModel rentableItemPnUpdateModel)
        {
            try
            {
                RentableItem rentableItem = await _dbContext.RentableItem.SingleOrDefaultAsync(x => x.Id == rentableItemPnUpdateModel.Id);

                if (rentableItem != null)
                {
                    rentableItem.Brand            = rentableItemPnUpdateModel.Brand;
                    rentableItem.ModelName        = rentableItemPnUpdateModel.ModelName;
                    rentableItem.RegistrationDate = rentableItemPnUpdateModel.RegistrationDate;
                    rentableItem.SerialNumber     = rentableItemPnUpdateModel.SerialNumber;
                    rentableItem.VinNumber        = rentableItemPnUpdateModel.VinNumber;
                    rentableItem.PlateNumber      = rentableItemPnUpdateModel.PlateNumber;
                    rentableItem.eFormId          = rentableItemPnUpdateModel.EFormId;

                    await rentableItem.Update(_dbContext);
                }
                return(new OperationDataResult <RentableItemsModel>(true,
                                                                    _rentableItemsLocalizationService.GetString("RentableItemUpdatedSuccessfully")));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                _logger.LogError(e.Message);
                return(new OperationDataResult <RentableItemsModel>(false,
                                                                    _rentableItemsLocalizationService.GetString("ErrorWhileUpdatingRentableItemInfo")));
            }
        }
        public async Task <OperationResult> Create(RentableItemModel rentableItemPnCreateModel)
        {
            try
            {
                RentableItem rentableItem = new RentableItem
                {
                    Brand            = rentableItemPnCreateModel.Brand,
                    ModelName        = rentableItemPnCreateModel.ModelName,
                    RegistrationDate = rentableItemPnCreateModel.RegistrationDate,
                    VinNumber        = rentableItemPnCreateModel.VinNumber,
                    SerialNumber     = rentableItemPnCreateModel.SerialNumber,
                    PlateNumber      = rentableItemPnCreateModel.PlateNumber,
                    eFormId          = rentableItemPnCreateModel.EFormId
                };

                await rentableItem.Create(_dbContext);

                //_dbContext.RentableItem.Add(rentableItemPn);
                //_dbContext.SaveChanges();
                return(new OperationResult(true,
                                           _rentableItemsLocalizationService.GetString("RentableItemCreated", rentableItemPnCreateModel.Brand,
                                                                                       rentableItemPnCreateModel.ModelName)));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                _logger.LogError(e.Message);
                return(new OperationResult(true, _rentableItemsLocalizationService.GetString("ErrorWhileCreatingRentableItem")));
            }
        }
        public async Task <OperationDataResult <RentableItemsModel> > Index(int contractId)
        {
            try
            {
                RentableItemsModel          rentableItemsModel = new RentableItemsModel();
                List <ContractRentableItem> itemContractsQuery = await
                                                                 _dbContext.ContractRentableItem.Where(x => x.ContractId == contractId && x.WorkflowState == Constants.WorkflowStates.Created).ToListAsync();

                foreach (var rentableItemContract in itemContractsQuery)
                {
                    RentableItemModel rentableItemModel = new RentableItemModel();
                    RentableItem      rentableItem      = await
                                                          _dbContext.RentableItem.FirstOrDefaultAsync(y => y.Id == rentableItemContract.RentableItemId);

                    rentableItemModel.Brand            = rentableItem.Brand;
                    rentableItemModel.VinNumber        = rentableItem.VinNumber;
                    rentableItemModel.RegistrationDate = rentableItem.RegistrationDate;
                    rentableItemModel.PlateNumber      = rentableItem.PlateNumber;
                    rentableItemModel.ModelName        = rentableItem.ModelName;
                    rentableItemModel.SerialNumber     = rentableItem.SerialNumber;
                    rentableItemModel.Id = rentableItem.Id;
                    rentableItemsModel.RentableItems.Add(rentableItemModel);
                    rentableItemsModel.RentableItemIds.Add(rentableItemContract.RentableItemId);
                }
                return(new OperationDataResult <RentableItemsModel>(true, rentableItemsModel));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                _logger.LogError(e.Message);
                return(new OperationDataResult <RentableItemsModel>(false, "Could not find any rentable items"));
            }
        }
        public static RentableItemModel ComposeValues(RentableItemModel rentableItem, JToken headers,
                                                      JToken rentableObj)
        {
            if (int.TryParse(headers[2]["headerValue"].ToString(), out var brandId))
            {
                rentableItem.Brand = rentableObj[brandId].ToString(); // Brand
            }

            if (int.TryParse(headers[3]["headerValue"].ToString(), out var modelId))
            {
                rentableItem.ModelName = rentableObj[modelId].ToString(); // Model Name
            }

            if (int.TryParse(headers[4]["headerValue"].ToString(), out var regDateId))
            {
                rentableItem.RegistrationDate = DateTime.Parse(rentableObj[regDateId].ToString()); // Model Name
            }

            if (int.TryParse(headers[5]["headerValue"].ToString(), out var vinNumberId))
            {
                rentableItem.VinNumber = rentableObj[vinNumberId].ToString(); // Vin Number
            }

            return(rentableItem);
        }
        public async Task <OperationDataResult <RentableItemModel> > Read(int id)
        {
            RentableItem rentableItem = await _dbContext.RentableItem.SingleOrDefaultAsync(x => x.Id == id);

            if (rentableItem != null)
            {
                RentableItemModel rentableItemModel = new RentableItemModel
                {
                    Id               = rentableItem.Id,
                    Brand            = rentableItem.Brand,
                    ModelName        = rentableItem.ModelName,
                    VinNumber        = rentableItem.VinNumber,
                    SerialNumber     = rentableItem.SerialNumber,
                    PlateNumber      = rentableItem.PlateNumber,
                    RegistrationDate = rentableItem.RegistrationDate,
                    FullName         = $"{rentableItem.Brand} - {rentableItem.ModelName} - {rentableItem.SerialNumber} - {rentableItem.VinNumber} - {rentableItem.PlateNumber}"
                };
                return(new OperationDataResult <RentableItemModel>(true, rentableItemModel));
            }
            return(new OperationDataResult <RentableItemModel>(false, _rentableItemsLocalizationService.GetString("ErrorWhileReadingRentableItemInfo")));
        }
        public async Task <OperationDataResult <ContractInspectionsModel> > Index(ContractInspectionsRequestModel contractInspectionsPnRequestModel)
        {
            try
            {
                ContractInspectionsModel contractInspectionsModel = new ContractInspectionsModel();
                Core _core = await _coreHelper.GetCore();

                IQueryable <ContractInspection> contractInspectionsQuery = _dbContext.ContractInspection.
                                                                           Where(x => x.WorkflowState != Constants.WorkflowStates.Removed).AsQueryable();
                if (!string.IsNullOrEmpty(contractInspectionsPnRequestModel.SortColumnName) &&
                    contractInspectionsPnRequestModel.SortColumnName != "")
                {
                    if (contractInspectionsPnRequestModel.IsSortDsc)
                    {
                        contractInspectionsQuery = contractInspectionsQuery.CustomOrderByDescending(contractInspectionsPnRequestModel.SortColumnName);
                    }
                    else
                    {
                        contractInspectionsQuery = contractInspectionsQuery.CustomOrderBy(contractInspectionsPnRequestModel.SortColumnName);
                    }
                }
                contractInspectionsModel.Total = await contractInspectionsQuery.CountAsync(x => x.WorkflowState != Constants.WorkflowStates.Removed);

                contractInspectionsQuery
                    = contractInspectionsQuery
                      .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                      .Skip(contractInspectionsPnRequestModel.Offset)
                      .Take(contractInspectionsPnRequestModel.PageSize);
                List <ContractInspection> contractInspections = await contractInspectionsQuery.ToListAsync();

                contractInspections.ForEach(contractInspection =>
                {
                    ContractInspectionItem contractInspectionItem =
                        _dbContext.ContractInspectionItem.FirstOrDefault(x =>
                                                                         x.ContractInspectionId == contractInspection.Id);

                    Contract contract = _dbContext.Contract.Single(x => x.Id == contractInspection.ContractId);

                    Customer customer =
                        _customerDbContext.Customers.Single(x => x.Id == contract.CustomerId);
                    RentableItemCustomerModel rentableItemCustomerModel = new RentableItemCustomerModel
                    {
                        Id              = customer.Id,
                        CustomerNo      = customer.CustomerNo,
                        CompanyName     = customer.CompanyName,
                        ContactPerson   = customer.ContactPerson,
                        CompanyAddress  = customer.CompanyAddress,
                        CompanyAddress2 = customer.CompanyAddress2,
                        CityName        = customer.CityName,
                        ZipCode         = customer.ZipCode,
                        CountryCode     = customer.CountryCode,
                        EanCode         = customer.EanCode,
                        VatNumber       = customer.VatNumber,
                        Email           = customer.Email,
                        Phone           = customer.Phone,
                        Description     = customer.Description
                    };
                    List <RentableItemModel> rentableItemModels = new List <RentableItemModel>();
                    foreach (ContractRentableItem contractRentableItem in _dbContext.ContractRentableItem.Where(x =>
                                                                                                                x.ContractId == contract.Id && x.WorkflowState == Constants.WorkflowStates.Created).ToList())
                    {
                        RentableItem rentableItem           = _dbContext.RentableItem.Single(x => x.Id == contractRentableItem.RentableItemId);
                        RentableItemModel rentableItemModel = new RentableItemModel
                        {
                            Id               = rentableItem.Id,
                            Brand            = rentableItem.Brand,
                            ModelName        = rentableItem.ModelName,
                            PlateNumber      = rentableItem.PlateNumber,
                            VinNumber        = rentableItem.VinNumber,
                            SerialNumber     = rentableItem.SerialNumber,
                            RegistrationDate = rentableItem.RegistrationDate,
                            EFormId          = rentableItem.eFormId
                        };
                        rentableItemModels.Add(rentableItemModel);
                    }

                    using (var dbContext = _core.DbContextHelper.GetDbContext())
                    {
                        if (contractInspectionItem != null)
                        {
                            contractInspectionsModel.ContractInspections.Add(new ContractInspectionModel
                            {
                                SdkCaseApiId = contractInspectionItem.SDKCaseId,
                                SdkCaseId    = dbContext.Cases
                                               .Single(x => x.MicrotingUid == contractInspectionItem.SDKCaseId).Id,
                                eFormId              = rentableItemModels.First().EFormId,
                                ContractId           = contractInspection.ContractId,
                                ContractStart        = contract.ContractStart,
                                ContractEnd          = contract.ContractEnd,
                                DoneAt               = contractInspection.DoneAt,
                                Id                   = contractInspection.Id,
                                Status               = contractInspectionItem.Status,
                                RentableItemCustomer = rentableItemCustomerModel,
                                RentableItems        = rentableItemModels
                            });
                        }
                    }
                });
                return(new OperationDataResult <ContractInspectionsModel>(true, contractInspectionsModel));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                _logger.LogError(e.Message);
                return(new OperationDataResult <ContractInspectionsModel>(false,
                                                                          _rentableItemsLocalizationService.GetString("ErrorObtainingContractInspectionInfo")));
            }
        }
예제 #7
0
 public Task <OperationResult> Update([FromBody] RentableItemModel rentableItemUpdateModel)
 {
     return(_rentableItemsService.Update(rentableItemUpdateModel));
 }
예제 #8
0
        public async Task <OperationDataResult <ContractsModel> > Index(ContractsRequestModel contractsPnRequestModel)
        {
            try
            {
                ContractsModel        contractsModel = new ContractsModel();
                IQueryable <Contract> contractsQuery = _dbContext.Contract.
                                                       Where(x => x.WorkflowState != Constants.WorkflowStates.Removed).AsQueryable();
                if (!string.IsNullOrEmpty(contractsPnRequestModel.SortColumnName))
                {
                    if (contractsPnRequestModel.IsSortDsc)
                    {
                        contractsQuery = contractsQuery.CustomOrderByDescending(contractsPnRequestModel.SortColumnName);
                    }
                    else
                    {
                        contractsQuery = contractsQuery.CustomOrderBy(contractsPnRequestModel.SortColumnName);
                    }
                }
                contractsModel.Total = contractsQuery.Count();
                contractsQuery
                    = contractsQuery
                      .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed)
                      .Skip(contractsPnRequestModel.Offset)
                      .Take(contractsPnRequestModel.PageSize);
                List <Contract> contracts = await contractsQuery.ToListAsync();

                contracts.ForEach(contract =>
                {
                    var customer =
                        _customerDbContext.Customers.Single(x => x.Id == contract.CustomerId);
                    RentableItemCustomerModel rentableItemCustomerModel = new RentableItemCustomerModel
                    {
                        Id              = customer.Id,
                        CustomerNo      = customer.CustomerNo,
                        CompanyName     = customer.CompanyName,
                        ContactPerson   = customer.ContactPerson,
                        CompanyAddress  = customer.CompanyAddress,
                        CompanyAddress2 = customer.CompanyAddress2,
                        CityName        = customer.CityName,
                        ZipCode         = customer.ZipCode,
                        CountryCode     = customer.CountryCode,
                        EanCode         = customer.EanCode,
                        VatNumber       = customer.VatNumber,
                        Email           = customer.Email,
                        Phone           = customer.Phone,
                        Description     = customer.Description
                    };

                    List <RentableItemModel> rentableItemModels = new List <RentableItemModel>();
                    foreach (ContractRentableItem contractRentableItem in _dbContext.ContractRentableItem.Where(x => x.ContractId == contract.Id && x.WorkflowState == Constants.WorkflowStates.Created).ToList())
                    {
                        RentableItem rentableItem           = _dbContext.RentableItem.Single(x => x.Id == contractRentableItem.RentableItemId);
                        RentableItemModel rentableItemModel = new RentableItemModel
                        {
                            Id               = rentableItem.Id,
                            Brand            = rentableItem.Brand,
                            ModelName        = rentableItem.ModelName,
                            PlateNumber      = rentableItem.PlateNumber,
                            VinNumber        = rentableItem.VinNumber,
                            SerialNumber     = rentableItem.SerialNumber,
                            RegistrationDate = rentableItem.RegistrationDate
                        };
                        rentableItemModels.Add(rentableItemModel);
                    }

                    contractsModel.Contracts.Add(new ContractModel
                    {
                        ContractEnd          = contract.ContractEnd,
                        ContractNr           = contract.ContractNr,
                        ContractStart        = contract.ContractStart,
                        CustomerId           = contract.CustomerId,
                        RentableItemCustomer = rentableItemCustomerModel,
                        RentableItems        = rentableItemModels,
                        Id = contract.Id,
                    });
                });
                return(new OperationDataResult <ContractsModel>(true, contractsModel));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                _logger.LogError(e.Message);
                return(new OperationDataResult <ContractsModel>(false,
                                                                _rentableItemsLocalizationService.GetString("ErrorObtainingContracts")));
            }
        }
        public async Task <OperationResult> Import(ImportModel importModel)
        {
            try
            {
                JToken rawJson        = JToken.Parse(importModel.ImportList);
                int    eFormId        = importModel.eFormId;
                JToken rawHeadersJson = JToken.Parse(importModel.Headers);

                JToken headers = rawHeadersJson;
                IEnumerable <JToken> rentableObjects = rawJson.Skip(2);

                Core core = await _coreHelper.GetCore();

                foreach (JToken rentableObject in rentableObjects)
                {
                    Customer customer          = null;
                    bool     companyNameExists = int.TryParse(headers[1]["headerValue"].ToString(), out int companyNameColumn);

                    if (companyNameExists)
                    {
                        customer = await FindCustomer(companyNameExists, companyNameColumn, headers, rentableObject);

                        if (customer == null)
                        {
                            CustomerFullModel customerModel =
                                CustomerHelper.ComposeValues(new CustomerFullModel(), headers, rentableObject);

                            customer = new Customer
                            {
                                CompanyName = customerModel.CompanyName
                            };
                            await customer.Create(_customerDbContext);
                        }
                    }

                    RentableItem rentableItem           = null;
                    bool         brandExists            = int.TryParse(headers[2]["headerValue"].ToString(), out int brandColumn);
                    bool         modelExists            = int.TryParse(headers[3]["headerValue"].ToString(), out int modelColumn);
                    bool         registrationDateExists =
                        int.TryParse(headers[4]["headerValue"].ToString(), out int registrationDateColumn);
                    bool vinNumberExists = int.TryParse(headers[5]["headerValue"].ToString(), out int vinNumberColumn);

                    if (brandExists &&
                        modelExists &&
                        registrationDateExists &&
                        vinNumberExists)
                    {
                        rentableItem = await FindRentableItem(brandExists, brandColumn, modelExists, modelColumn,
                                                              registrationDateExists, registrationDateColumn, vinNumberExists, vinNumberColumn, headers,
                                                              rentableObject);

                        if (rentableItem == null)
                        {
                            RentableItemModel rentableItemModel =
                                RentableItemHelper.ComposeValues(new RentableItemModel(), headers, rentableObject);

                            rentableItem = new RentableItem
                            {
                                Brand            = rentableItemModel.Brand,
                                ModelName        = rentableItemModel.ModelName,
                                RegistrationDate = rentableItemModel.RegistrationDate,
                                VinNumber        = rentableItemModel.VinNumber,
                                eFormId          = eFormId
                            };
                            await rentableItem.Create(_dbContext);
                        }
                    }

                    Contract contract             = null;
                    bool     contractNumberExists =
                        int.TryParse(headers[0]["headerValue"].ToString(), out int contractNumberColumn);
                    bool contractStartExists =
                        int.TryParse(headers[6]["headerValue"].ToString(), out int contractStartColumn);
                    bool contractEndExists = int.TryParse(headers[7]["headerValue"].ToString(), out int contractEndColumn);
                    int  customerID        = customer.Id;
                    if (contractNumberExists &&
                        contractStartExists &&
                        contractEndExists &&
                        customerID != null)
                    {
                        contract = await FindContract(contractNumberExists, contractNumberColumn, contractStartExists,
                                                      contractStartColumn, contractEndExists, contractEndColumn, customerID, headers, rentableObject);

                        if (contract == null)
                        {
                            ContractModel contractModel =
                                ContractsHelper.ComposeValues(new ContractModel(), headers, rentableObject);

                            contract = new Contract
                            {
                                CustomerId    = customerID,
                                ContractEnd   = contractModel.ContractEnd,
                                ContractStart = contractModel.ContractStart,
                                ContractNr    = contractModel.ContractNr
                            };
                            await contract.Create(_dbContext);
                        }
                    }

                    ContractRentableItem contractRentableItem = null;
                    if (contract.Id != null && rentableItem.Id != null)
                    {
                        contractRentableItem = await FindtContractRentableItem(contract.Id, rentableItem.Id);

                        if (contractRentableItem == null)
                        {
                            contractRentableItem = new ContractRentableItem
                            {
                                ContractId     = contract.Id,
                                RentableItemId = rentableItem.Id
                            };
                            await contractRentableItem.Create(_dbContext);
                        }
                    }
                }
                return(new OperationResult(true, "Import Successful"));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                _logger.LogError(e.Message);
                return(new OperationResult(false, "You goofed up"));
            }
        }