def test_generate_payments_values(self): # Test 1 values = generate_payments_values(Decimal(101), 3) expected = [Decimal('33.67'), Decimal('33.67'), Decimal('33.66')] self.assertEqual(values, expected) self.assertEqual(len(values), 3) self.assertEqual(sum(values), Decimal(101)) # Test 2 values = generate_payments_values(Decimal('10.5'), 5, Decimal('1')) expected = [ Decimal('2.12'), Decimal('2.12'), Decimal('2.12'), Decimal('2.12'), Decimal('2.12') ] self.assertEqual(values, expected) self.assertEqual(len(values), 5) self.assertEqual(sum(values), (Decimal('10.5') + Decimal('0.10'))) # Test 3 self.assertRaises(AssertionError, generate_payments_values, Decimal('2'), 0)
def test_generate_payments_values(self): # Test 1 values = generate_payments_values(Decimal(101), 3) expected = [Decimal("33.67"), Decimal("33.67"), Decimal("33.66")] self.assertEqual(values, expected) self.assertEqual(len(values), 3) self.assertEqual(sum(values), Decimal(101)) # Test 2 self.assertRaises(ValueError, generate_payments_values, Decimal("2"), 0)
def test_generate_payments_values(self): # Test 1 values = generate_payments_values(Decimal(101), 3) expected = [Decimal('33.67'), Decimal('33.67'), Decimal('33.66')] self.assertEqual(values, expected) self.assertEqual(len(values), 3) self.assertEqual(sum(values), Decimal(101)) # Test 2 self.assertRaises(ValueError, generate_payments_values, Decimal('2'), 0)
def test_generate_payments_values(self): # Test 1 values = generate_payments_values(Decimal(101), 3) expected = [Decimal('33.67'), Decimal('33.67'), Decimal('33.66')] self.assertEqual(values, expected) self.assertEqual(len(values), 3) self.assertEqual(sum(values), Decimal(101)) # Test 2 values = generate_payments_values(Decimal('10.5'), 5, Decimal('1')) expected = [Decimal('2.12'), Decimal('2.12'), Decimal('2.12'), Decimal('2.12'), Decimal('2.12')] self.assertEqual(values, expected) self.assertEqual(len(values), 5) self.assertEqual(sum(values), (Decimal('10.5') + Decimal('0.10'))) # Test 3 self.assertRaises(AssertionError, generate_payments_values, Decimal('2'), 0)
def create_payments(self, branch, station: BranchStation, payment_type, group, value, due_dates): """Creates new payments The values of the individual payments are calculated by taking the value and dividing it by the number of payments. The number of payments is determined by the length of the due_dates sequence. :param payment_type: the kind of payment, in or out :param payment_group: a |paymentgroup| :param branch: the |branch| associated with the payments, for incoming payments this is the branch receiving the payment and for outgoing payments this is the branch sending the payment. :param value: total value of all payments :param due_dates: a list of datetime objects :returns: a list of |payments| """ installments = len(due_dates) if not installments: raise ValueError(_('Need at least one installment')) if payment_type == Payment.TYPE_IN: if installments > self.max_installments: raise ValueError( _('The number of installments can not be greater than %d ' 'for payment method %s') % (self.max_installments, self.method_name)) # Create the requested payments with the right: # - due_date # - description for the specific group # - normalized value payments = [] normalized_values = generate_payments_values(value, installments) for (i, due_date), normalized_value in zip(enumerate(due_dates), normalized_values): description = self.describe_payment(group, i + 1, installments) payment = self.create_payment(station=station, payment_type=payment_type, payment_group=group, branch=branch, value=normalized_value, due_date=due_date, description=description) payments.append(payment) return payments
def create_payments(self, payment_type, group, branch, value, due_dates): """Creates new payments The values of the individual payments are calculated by taking the value and dividing it by the number of payments. The number of payments is determined by the length of the due_dates sequence. :param payment_type: the kind of payment, in or out :param payment_group: a |paymentgroup| :param branch: the |branch| associated with the payments, for incoming payments this is the branch receiving the payment and for outgoing payments this is the branch sending the payment. :param value: total value of all payments :param due_dates: a list of datetime objects :returns: a list of |payments| """ installments = len(due_dates) if not installments: raise ValueError(_('Need at least one installment')) if payment_type == Payment.TYPE_IN: if installments > self.max_installments: raise ValueError( _('The number of installments can not be greater than %d ' 'for payment method %s') % (self.max_installments, self.method_name)) # Create the requested payments with the right: # - due_date # - description for the specific group # - normalized value payments = [] normalized_values = generate_payments_values(value, installments) for (i, due_date), normalized_value in zip(enumerate(due_dates), normalized_values): description = self.describe_payment(group, i + 1, installments) payment = self.create_payment( payment_type=payment_type, payment_group=group, branch=branch, value=normalized_value, due_date=due_date, description=description) payments.append(payment) return payments
def add_payments(self, installments_number, start_date, interval, interval_type): values = generate_payments_values(self.total_value, installments_number) due_dates = create_date_interval(interval_type=interval_type, interval=interval, count=installments_number, start_date=start_date) bank_account = None self.clear_payments() for i in range(installments_number): description = self.method.describe_payment(self.group, i + 1, installments_number) if self._has_bank_account(): bank_account = _TemporaryBankData() self.add_payment(description, currency(values[i]), due_dates[i], None, bank_account, False) self.update_view()