def test_save_provider_invoice_ready_then_amount_currency(self):
        """
        Does ready provider_invoice and then change amount and currency
        """
        test_currency1 = CURRENCY_CUC
        test_date = timezone.now()
        test_amount1 = 100
        test_status = STATUS_READY

        provider_invoice = ProviderInvoice(provider=self.test_provider,
                                           date=test_date,
                                           currency=test_currency1,
                                           amount=test_amount1,
                                           status=test_status)

        provider_invoice = FinanceService.save_provider_invoice(
            user=self.test_user, provider_invoice=provider_invoice)

        # document data auto filled
        self.assertDocument(provider_invoice, DOC_TYPE_PROVIDER_INVOICE,
                            test_currency1)

        # one finantial history created
        finantials = provider_invoice.finantialdocumenthistory_set
        self.assertEqual(finantials.count(), 1)

        # finantial history info
        finantial = finantials.first()
        self.assertFinantialHistory(test_finantial_history=finantial,
                                    test_document=provider_invoice,
                                    test_user=self.test_user,
                                    test_old_status=None,
                                    test_new_status=test_status)

        test_currency2 = CURRENCY_USD
        test_amount2 = 50

        provider_invoice.currency = test_currency2
        provider_invoice.amount = test_amount2

        provider_invoice = FinanceService.save_provider_invoice(
            user=self.test_user, provider_invoice=provider_invoice)

        # document data auto filled
        self.assertDocument(provider_invoice, DOC_TYPE_PROVIDER_INVOICE,
                            test_currency2)

        # no aditional finantial history
        finantials = provider_invoice.finantialdocumenthistory_set
        self.assertEqual(finantials.count(), 1)
    def test_provider_match_then_increase_amount(self):
        """
        Does match between provider_payment and provider_invoice then increase amount
        """
        test_account = Account.objects.create(name='Test Account',
                                              currency=CURRENCY_CUC,
                                              balance=1000)

        test_provider = Provider.objects.create(name='Test Provider')

        test_date = timezone.now()
        test_amount = 100
        test_status = STATUS_READY

        provider_payment = ProviderPayment(date=test_date,
                                           account=test_account,
                                           amount=test_amount,
                                           provider=test_provider,
                                           status=test_status)

        provider_payment = FinanceService.save_provider_payment(
            user=self.test_user, provider_payment=provider_payment)

        provider_invoice = ProviderInvoice(date=test_date,
                                           amount=test_amount,
                                           provider=test_provider,
                                           status=test_status)

        provider_invoice = FinanceService.save_provider_invoice(
            user=self.test_user, provider_invoice=provider_invoice)

        provider_match = ProviderDocumentMatch(
            credit_document=provider_payment,
            debit_document=provider_invoice,
            matched_amount=test_amount)

        provider_match = FinanceService.save_provider_match(provider_match)

        provider_payment.refresh_from_db()
        provider_invoice.refresh_from_db()

        self.assertEqual(provider_payment.matched_amount, test_amount)
        self.assertEqual(provider_invoice.matched_amount, test_amount)

        provider_invoice.amount = provider_invoice.amount + 20

        provider_invoice = FinanceService.save_provider_invoice(
            user=self.test_user, provider_invoice=provider_invoice)

        self.assertEqual(provider_invoice.matched_amount, test_amount)