Ejemplo n.º 1
0
        public async Task <ItemWiseReOrderLevelReport> GetReOrderLevelReport(long?supplierId)
        {
            Dal.Entities.Supplier supplier = null;

            if (supplierId.HasValue)
            {
                supplier = await _supplierService.GetSupplierById(supplierId.Value);
            }

            var query = _unitOfWork.ProductRepository.GetAll();

            if (supplierId.HasValue)
            {
                query = query.Where(p => p.SupplierId == supplierId.Value);
            }

            query = query.Where(p => p.ReorderLevel >= p.Stocks.Sum(s => s.Quantity));

            return(new ItemWiseReOrderLevelReport()
            {
                SupplierCode = supplier?.Code,
                SupplierName = supplier?.SupplierName,
                ItemWiseReOrderLevelDetails = query.Select(item => new ItemWiseReOrderLevelDetailReport()
                {
                    Code = item.Code,
                    Name = item.Name,
                    UnitOfMeasure = item.UnitOfMeasure.Code,
                    ReOrderLevel = item.ReorderLevel,
                    StockInHand = item.Stocks.Sum(s => s.Quantity)
                }).ToList()
            });
        }
        public async Task <Dal.Entities.Supplier> UpdateSupplier(Dal.Entities.Supplier supplier)
        {
            Guard.Argument(supplier, nameof(supplier)).NotNull();

            await ValidateSupplier(supplier);

            await using var transaction = await _unitOfWork.GetTransaction();

            try
            {
                var currentSupplier = await GetSupplierById(supplier.Id);

                supplier.TrackerCode = currentSupplier.TrackerCode;

                var mapper = ServiceMapper.GetMapper();
                mapper.Map(supplier, currentSupplier);

                await _unitOfWork.SaveChanges();

                var savedSupplier = await GetSupplierById(supplier.Id);

                await _trackerApiService.UpdateCustomerStatus(new UpdateCustomerRequest()
                {
                    Active       = supplier.Active ? "1" : "0",
                    CustomerCode = savedSupplier.TrackerCode,
                    StatusDate   = DateTime.Now,
                    StatusReason = "Update the supplier"
                });

                await transaction.CommitAsync();
            }
            catch (TrackingApiException ex)
            {
                await transaction.RollbackAsync();

                throw new ServiceException(new ErrorMessage[]
                {
                    new ErrorMessage()
                    {
                        Code    = string.Empty,
                        Message = $"Unable to create a supplier in tracker side"
                    }
                });
            }
            catch
            {
                await transaction.RollbackAsync();

                throw;
            }

            return(await GetSupplierById(supplier.Id));
        }
        private async Task ValidateSupplier(Dal.Entities.Supplier supplier)
        {
            var supplierValidator = new SupplierValidator();
            var validateResult    = await supplierValidator.ValidateAsync(supplier);

            if (validateResult.IsValid)
            {
                return;
            }

            throw new ServiceException(validateResult.Errors.Select(e => new ErrorMessage()
            {
                Code = ErrorCodes.Model_Validation_Error_Code,
                Meta = new
                {
                    e.ErrorCode,
                    e.ErrorMessage,
                    e.PropertyName
                },
                Message = e.ErrorMessage
            }).ToArray());
        }
        public async Task <Dal.Entities.Supplier> AddSupplier(Dal.Entities.Supplier supplier)
        {
            Guard.Argument(supplier, nameof(supplier)).NotNull();

            await ValidateSupplier(supplier);

            await using var transaction = await _unitOfWork.GetTransaction();

            try
            {
                _unitOfWork.SupplierRepository.Insert(supplier);
                await _unitOfWork.SaveChanges();

                var savedSupplier = await GetSupplierById(supplier.Id);

                var response = await _trackerApiService.CreateCustomer(new CreateCustomerRequest()
                {
                    CustomerName    = savedSupplier.SupplierName,
                    CustomerAddress =
                        $"{savedSupplier.Address.AddressLine1} {savedSupplier.Address.AddressLine2} {savedSupplier.Address.City.CityName} {savedSupplier.Address.PostalCode}",
                    ContactNumber   = savedSupplier.Contact.Phone,
                    EmailAddress    = savedSupplier.Contact.Email,
                    NicNo           = string.Empty,
                    ContactPerson   = savedSupplier.Contact.ContactPerson,
                    TaxRegNo        = savedSupplier.VatNumber,
                    Vat             = string.IsNullOrEmpty(savedSupplier.VatNumber) ? "0" : "1",
                    Remarks         = string.Empty,
                    CreatedDate     = System.DateTime.Now,
                    TplSupplierCode = savedSupplier.Code
                });

                if (response.IsSuccess != "1")
                {
                    throw new ServiceException(new ErrorMessage[]
                    {
                        new ErrorMessage()
                        {
                            Code    = string.Empty,
                            Message = $"Unable to create a supplier in tracker side"
                        }
                    });
                }

                savedSupplier.TrackerCode = response.Result.CustomerCode;
                await _unitOfWork.SaveChanges();

                await transaction.CommitAsync();
            }
            catch (TrackingApiException ex)
            {
                await transaction.RollbackAsync();

                throw new ServiceException(new ErrorMessage[]
                {
                    new ErrorMessage()
                    {
                        Code    = string.Empty,
                        Message = $"Unable to create a supplier in tracker side"
                    }
                });
            }
            catch
            {
                await transaction.RollbackAsync();

                throw;
            }

            return(await GetSupplierById(supplier.Id));
        }
 public bool IsActiveSupplier(Dal.Entities.Supplier supplier)
 {
     return(supplier.Active);
 }