示例#1
0
        public async Task <Invoice> AddCharge(Charge charge, User user)
        {
            Currency?chargeCurrency = charge?.GetCurrency();
            int      month          = 0;
            int      year           = 0;

            if (!chargeCurrency.HasValue)
            {
                throw new InvalidEntityException(charge);
            }

            //Try to get month and year from charge event
            if (charge.Event != null && charge.Event.Date != default(DateTime))
            {
                month = charge.Event.Date.Month;
                year  = charge.Event.Date.Year;
            }
            Invoice foundInvoice = await _invoiceRepository.FindAsync(x =>
                                                                      x.User.Id == user.Id &&
                                                                      x.Month == month &&
                                                                      x.Year == year &&
                                                                      x.Currency == chargeCurrency);

            //If no invoice exists for this user/month/year combination
            //create a new one
            if (foundInvoice == null)
            {
                Invoice newInvoice = new Invoice(
                    month,
                    year,
                    chargeCurrency.Value,
                    user
                    );

                newInvoice.AddCharge(charge);

                if (newInvoice.IsValid())
                {
                    return(_invoiceRepository.Insert(newInvoice));
                }
                else
                {
                    throw new InvalidEntityException(newInvoice);
                }
            }
            else
            {
                //If invoice was found, add charge
                foundInvoice.AddCharge(charge);
                if (foundInvoice.IsValid())
                {
                    return(_invoiceRepository.Update(foundInvoice));
                }
                else
                {
                    throw new InvalidEntityException(foundInvoice);
                }
            }
        }