/// <summary>
 /// Processes an add transaction command
 /// </summary>
 /// <param name="cmd"></param>
 /// <param name="cargoDTO"></param>
 /// <returns></returns>
 public int ProccessTransaction(AddTransactionCommand cmd, CargoTypeDetailedDTO cargoDTO)
 {
     if (_transactionHandlers.ContainsKey(cmd.TransactionType))
     {
         _transactionHandlers[cmd.TransactionType](cmd, cargoDTO);
         return(0);
     }
     return(-1);
 }
Exemple #2
0
        /// <summary>
        /// Sell cargo
        /// </summary>
        /// <remarks>
        /// -Amount Source Organization (Warehouse)
        /// </remarks>
        /// <param name="command"></param>
        /// <param name="cargo"></param>
        public void SellCargo(AddTransactionCommand command, CargoTypeDetailedDTO cargo)
        {
            if (!command.SourceOrganizationId.HasValue)
            {
                throw new ArgumentException("NO_SOURCE_ORGANIZATION_IN_REQUEST");
            }

            var sourceOrganizationResponse =
                _organizationService.GetOrganizationById(new GetOrganizationByIdRequest
            {
                Id = command.SourceOrganizationId.Value
            });

            if (sourceOrganizationResponse.ServiceStatus != ServiceStatus.Success)
            {
                throw new ServiceErrorException(sourceOrganizationResponse.ErrorMessage, "OrganizationService");
            }
            if (!sourceOrganizationResponse.OrganizationDetailed.OrganizationType.Equals("warehouse", StringComparison.CurrentCultureIgnoreCase))
            {
                throw new ArgumentException("TYPE_OF_TARGET_ORGANIZATION_IS_NOT_WAREHOUSE");
            }

            var sourceLedger = _unitOfWork.LedgerTransactionRepository.Add(new LedgerTransaction()
            {
                Amount         = -command.Amount,
                OrganizationId = sourceOrganizationResponse.OrganizationDetailed.Id,
                TotalPrice     = command.Amount * cargo.Price
            });

            _unitOfWork.Commit();
            _eventBus.Publish(GetLedgerTransactionEvent(command, sourceLedger, cargo, sourceOrganizationResponse.OrganizationDetailed));

            var balanceSourse = AddOrUpdateMinusBalance(command.Amount,
                                                        sourceOrganizationResponse.OrganizationDetailed.Id, cargo.Id, sourceLedger.Id);

            // Add transaction
            _unitOfWork.TransactionRepository.Add(new Transaction()
            {
                Amount             = command.Amount,
                CargoId            = cargo.Id,
                Comments           = command.Comments,
                LedgerTransactions = new List <LedgerTransaction>()
                {
                    sourceLedger
                },
                SourceOrganizationId = sourceOrganizationResponse.OrganizationDetailed.Id,
                Timestamp            = DateTime.Now,
                TransactionType      = command.TransactionType,
                UserId = "test"
            });
            _unitOfWork.Commit();
            _eventBus.Publish(GetBalanceUpdatedEvent(command, balanceSourse, cargo, sourceOrganizationResponse.OrganizationDetailed));
        }
Exemple #3
0
 /// <summary>
 /// Get balance updated event
 /// </summary>
 /// <param name="cmd"></param>
 /// <param name="balance"></param>
 /// <param name="cargo"></param>
 /// <param name="organizationDetailedDto"></param>
 /// <returns></returns>
 private BalanceUpdatedEvent GetBalanceUpdatedEvent(AddTransactionCommand cmd, Balance balance,
                                                    CargoTypeDetailedDTO cargo, OrganizationDetailedDTO organizationDetailedDto)
 {
     return(new BalanceUpdatedEvent()
     {
         Id = balance.Id,
         Amount = balance.CargoBalance,
         OrganizationId = balance.OrganizationId,
         Description = cmd.Comments,
         Ean = cargo.Ean,
         CargoId = cmd.CargoId,
         Kardex = organizationDetailedDto.Kardex
     });
 }
Exemple #4
0
 /// <summary>
 /// Get leget transaction event
 /// </summary>
 /// <param name="cmd"></param>
 /// <param name="ledgerTransaction"></param>
 /// <param name="cargo"></param>
 /// <param name="organizationDetailedDto"></param>
 /// <returns></returns>
 private LedgerTransactionAddedEvent GetLedgerTransactionEvent(AddTransactionCommand cmd,
                                                               LedgerTransaction ledgerTransaction, CargoTypeDetailedDTO cargo, OrganizationDetailedDTO organizationDetailedDto)
 {
     return(new LedgerTransactionAddedEvent
     {
         Id = ledgerTransaction.Id,
         Amount = ledgerTransaction.Amount,
         OrganizationId = ledgerTransaction.OrganizationId,
         TotalPrice = ledgerTransaction.TotalPrice,
         TransactionType = cmd.TransactionType,
         CargoId = cmd.CargoId,
         Comments = cmd.Comments,
         Timestamp = DateTime.Now,
         Ean = cargo.Ean,
         Kardex = organizationDetailedDto.Kardex
     });
 }
Exemple #5
0
        /// <summary>
        ///  Manual transfer of cargo
        /// </summary>
        /// <remarks>
        ///  +Amount Source Organization (Any type - Negative amount allowed)
        ///  -Amount Target Organization (Any type - Negative amount allowed)
        /// </remarks>
        /// <param name="command"></param>
        /// <param name="cargo"></param>
        public void ManualTransferOfCargo(AddTransactionCommand command, CargoTypeDetailedDTO cargo)
        {
            if (!command.SourceOrganizationId.HasValue | !command.TargetOrganizationId.HasValue)
            {
                throw new ArgumentException("NO_SOURCE_OR_TARGET_ORGANIZATION_IN_REQUEST");
            }

            var sourceOrganizationResponse =
                _organizationService.GetOrganizationById(new GetOrganizationByIdRequest
            {
                Id = command.SourceOrganizationId.Value
            });

            if (sourceOrganizationResponse.ServiceStatus != ServiceStatus.Success)
            {
                throw new ServiceErrorException(sourceOrganizationResponse.ErrorMessage, "OrganizationService");
            }

            var targetOrganizationResponse =
                _organizationService.GetOrganizationById(new GetOrganizationByIdRequest
            {
                Id = command.TargetOrganizationId.Value
            });

            if (targetOrganizationResponse.ServiceStatus != ServiceStatus.Success)
            {
                throw new ServiceErrorException(targetOrganizationResponse.ErrorMessage, "OrganizationService");
            }

            var sourceLedger = _unitOfWork.LedgerTransactionRepository.Add(new LedgerTransaction()
            {
                Amount         = command.Amount,
                OrganizationId = sourceOrganizationResponse.OrganizationDetailed.Id,
                TotalPrice     = command.Amount * cargo.Price
            });

            var targetLedger = _unitOfWork.LedgerTransactionRepository.Add(new LedgerTransaction()
            {
                Amount         = -command.Amount,
                OrganizationId = targetOrganizationResponse.OrganizationDetailed.Id,
                TotalPrice     = command.Amount * cargo.Price
            });

            _unitOfWork.Commit();
            _eventBus.Publish(GetLedgerTransactionEvent(command, sourceLedger, cargo, sourceOrganizationResponse.OrganizationDetailed));
            _eventBus.Publish(GetLedgerTransactionEvent(command, targetLedger, cargo, targetOrganizationResponse.OrganizationDetailed));

            var balanceSourse = AddOrUpdatePlusBalance(command.Amount, sourceOrganizationResponse.OrganizationDetailed.Id, cargo.Id, sourceLedger.Id);
            var balanceTarget = AddOrUpdateMinusBalance(command.Amount,
                                                        targetOrganizationResponse.OrganizationDetailed.Id, cargo.Id, targetLedger.Id);

            // Add transaction
            _unitOfWork.TransactionRepository.Add(new Transaction()
            {
                Amount             = command.Amount,
                CargoId            = cargo.Id,
                Comments           = command.Comments,
                LedgerTransactions = new List <LedgerTransaction>()
                {
                    sourceLedger, targetLedger
                },
                SourceOrganizationId = sourceOrganizationResponse.OrganizationDetailed.Id,
                TargetOrganizationId = targetOrganizationResponse.OrganizationDetailed.Id,
                Timestamp            = DateTime.Now,
                TransactionType      = command.TransactionType,
                UserId = "test"
            });

            _unitOfWork.Commit();
            _eventBus.Publish(GetBalanceUpdatedEvent(command, balanceSourse, cargo, sourceOrganizationResponse.OrganizationDetailed));
            _eventBus.Publish(GetBalanceUpdatedEvent(command, balanceTarget, cargo, targetOrganizationResponse.OrganizationDetailed));
        }
Exemple #6
0
 public GetCargoByIdResponse(CargoTypeDetailedDTO cargoTypeDetailed, ServiceStatus serviceStatus, string errorMessage) : base(serviceStatus, errorMessage)
 {
     CargoTypeDetailed = cargoTypeDetailed;
     ServiceStatus     = serviceStatus;
     ErrorMessage      = errorMessage;
 }