Beispiel #1
0
        public async Task <ResultModel> Add(CreditCard card)
        {
            var result          = new ResultModel();
            var validatorResult = new CreditCardValidator(_creditCardRepository, _userRepository).Validate(card);

            if (validatorResult.IsValid)
            {
                await _creditCardRepository.Add(card);
            }
            else
            {
                result.AddNotification(validatorResult.Errors);
            }

            return(result);
        }
        public ActionResult Add([FromBody] CreditCardDTO creditCardDTO)
        {
            var newCreditCard = new CreditCard
            {
                Number       = creditCardDTO.Number,
                Holder       = creditCardDTO.Holder,
                SecurityCode = creditCardDTO.SecurityCode,
                Flag         = creditCardDTO.Flag,
                Limit        = creditCardDTO.Limit,
                Purchases    = creditCardDTO.Purchases,
                UserId       = _userManager.GetUserAsync(HttpContext.User).Result.Id
            };

            _creditCardRepository.Add(newCreditCard);
            return(Created("", newCreditCard));
        }
Beispiel #3
0
        public bool CreateCreditCard(CreaditCardCreateViewModel model)
        {
            StripeConfiguration.SetApiKey(SETTING.Value.SecretStripe);


            var customerTMP = _customerRepository.Get(x => x.Deleted == false && x.Id == model.CustomerId);

            if (customerTMP.StripeId == null || customerTMP.StripeId == "")
            {
                var options = new CustomerCreateOptions
                {
                    Email       = customerTMP.Email,
                    Description = "Customer for " + customerTMP.FullName + " " + customerTMP.Email,
                    SourceToken = model.CardId
                };
                var             service  = new Stripe.CustomerService();
                Stripe.Customer customer = service.Create(options);
                customerTMP.StripeId = customer.Id;
                _customerRepository.Update(customerTMP);
                model.CardId    = customer.DefaultSourceId;
                model.Isdefault = true;
            }
            else
            {
                var optionsCard = new CardCreateOptions
                {
                    SourceToken = model.CardId
                };
                var serviceCard = new Stripe.CardService();
                var card        = serviceCard.Create(customerTMP.StripeId, optionsCard);
                model.CardId = card.Id;
            }
            model.Last4DigitsHash = encrypt(model.Last4DigitsHash);
            var creditCard = _mapper.Map <CreaditCardCreateViewModel, CreditCard>(model);

            _creditCardRepository.Add(creditCard);
            _unitOfWork.CommitChanges();
            return(true);
        }
Beispiel #4
0
        public async Task <SaleDto> Create(Guid userId, SaleForCreationDto saletForCreationDto)
        {
            try
            {
                Client client = null;
                if (saletForCreationDto.ClientId.HasValue)
                {
                    client = await _clientRepository.GetById(saletForCreationDto.ClientId.Value);

                    if (client == null)
                    {
                        throw new KeyNotFoundException($"Client with id: {saletForCreationDto.ClientId} not found.");
                    }
                }

                var sale = new Sale()
                {
                    ClientId    = saletForCreationDto.ClientId,
                    ClientName  = client != null ? client.Name + " " + client.Lastname : saletForCreationDto.ClientName,
                    Date        = saletForCreationDto.Date.ToLocalTime(),
                    PaymentType = saletForCreationDto.PaymentType,
                    CreatedBy   = userId
                };

                decimal total = 0;
                foreach (var detailFC in saletForCreationDto.Details)
                {
                    var product = await _productRepository.GetById(detailFC.ProductId);

                    if (product == null)
                    {
                        throw new KeyNotFoundException($"Product with id: {detailFC.ProductId} not found.");
                    }

                    var price = (await _priceRepository.GetAll())
                                .OrderByDescending(x => x.DateTime)
                                .FirstOrDefault(
                        x => x.ProductId == detailFC.ProductId &&
                        x.DateTime.ToLocalTime() <= saletForCreationDto.Date.ToLocalTime() &&
                        x.PriceType == ePriceTypes.SalePrice &&
                        !x.IsDeleted
                        );

                    FeeRule feeRule = null;
                    if (sale.PaymentType == ePaymentTypes.OwnFees)
                    {
                        feeRule = (await _feeRuleRepository.Find(x => x.ProductId == detailFC.ProductId))
                                  .OrderByDescending(x => x.Date).ThenBy(x => x.FeesAmountTo)
                                  .FirstOrDefault(x => x.Date <= sale.Date && x.FeesAmountTo >= saletForCreationDto.OwnFees.Quantity);
                        if (feeRule == null)
                        {
                            throw new KeyNotFoundException($"Fee Rule not found.");
                        }

                        decimal percentage = feeRule.Percentage * saletForCreationDto.OwnFees.Quantity / 100;
                        total += price.Value * detailFC.Quantity * (1 + percentage);
                    }
                    else
                    {
                        total += price.Value * detailFC.Quantity;
                    }

                    var detail = new Detail()
                    {
                        SaleId    = sale.Id,
                        ProductId = detailFC.ProductId,
                        Quantity  = detailFC.Quantity,
                        UnitPrice = price.Value,
                        FeeRuleId = feeRule?.Id,
                        CreatedBy = userId
                    };
                    product.Stock -= detailFC.Quantity;

                    detail = await _detailRepository.Add(detail);

                    await _productRepository.Update(product);
                }

                Payment payment = null;
                switch (saletForCreationDto.PaymentType)
                {
                case Util.Enums.ePaymentTypes.Cash:
                    var cashDto = (CashForCreationDto)saletForCreationDto.Cash;
                    payment = new Cash(total, cashDto.Discount)
                    {
                        SaleId    = sale.Id,
                        CreatedBy = userId
                    };

                    await _cashRepository.Add((Cash)payment);

                    break;

                case Util.Enums.ePaymentTypes.OwnFees:
                    var ownFeesDto = (OwnFeesForCreationDto)saletForCreationDto.OwnFees;
                    payment = new OwnFees(ownFeesDto.ExpirationDate, total, ownFeesDto.Quantity, userId)
                    {
                        SaleId = sale.Id,
                    };

                    payment = await _ownFeesRepository.Add((OwnFees)payment);

                    foreach (var fee in ((OwnFees)payment).FeeList)
                    {
                        await _feeRepository.Add(fee);
                    }
                    break;

                case Util.Enums.ePaymentTypes.CreditCard:
                    var creditCardDto = (CreditCardForCreationDto)saletForCreationDto.CreditCard;
                    payment = new CreditCard(total, creditCardDto.Discount, creditCardDto.Surcharge)
                    {
                        SaleId    = sale.Id,
                        CardType  = creditCardDto.CardType,
                        Bank      = creditCardDto.Bank,
                        CreatedBy = userId
                    };

                    await _creditCardRepository.Add((CreditCard)payment);

                    break;

                case Util.Enums.ePaymentTypes.DebitCard:
                    var debitCardDto = (DebitCardForCreationDto)saletForCreationDto.DebitCard;
                    payment = new DebitCard(total, debitCardDto.Discount, debitCardDto.Surcharge)
                    {
                        SaleId    = sale.Id,
                        CardType  = debitCardDto.CardType,
                        Bank      = debitCardDto.Bank,
                        CreatedBy = userId
                    };

                    await _debitCardRepository.Add((DebitCard)payment);

                    break;

                case Util.Enums.ePaymentTypes.Cheques:
                    var chequesDto = (ChequesPaymentForCreationDto)saletForCreationDto.Cheques;

                    if (chequesDto.ListOfCheques.Sum(x => x.Value) != total)
                    {
                        throw new InvalidOperationException("The sum of cheques list is different of amount.");
                    }

                    payment = new ChequesPayment()
                    {
                        SaleId    = sale.Id,
                        Amount    = Math.Ceiling(total * 100) / 100,
                        CreatedBy = userId
                    };

                    await _chequesPaymentRepository.Add((ChequesPayment)payment);

                    foreach (var c in chequesDto.ListOfCheques)
                    {
                        var cheque = new Cheque()
                        {
                            ChequesPaymentId = payment.Id,
                            Bank             = c.Bank,
                            Nro       = c.Nro,
                            Value     = c.Value,
                            CreatedBy = userId
                        };

                        await _chequeRepository.Add(cheque);
                    }
                    break;

                default:
                    break;
                }

                sale.PaymentId = payment.Id;
                sale           = await _saleRepository.Add(sale);

                await _saleRepository.CommitAsync();

                return(_mapper.Map <Sale, SaleDto>(sale));
            }
            catch (Exception e)
            {
                throw e;
            }
        }