def test_save_provider_devolution_ready_then_date(self):
        """
        Does ready provider_devolution and then change to draft
        """
        test_account = Account.objects.create(
            name='Test Account',
            currency=CURRENCY_CUC)
        test_balance = test_account.balance

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

        provider_devolution = ProviderDevolution(
            provider=self.test_provider,
            date=test_date1,
            account=test_account,
            amount=test_amount,
            status=test_status)

        provider_devolution = FinanceServices.save_provider_devolution(
            user=self.test_user,
            provider_devolution=provider_devolution)

        test_name1 = provider_devolution.name

        # account balance incremented
        self.assertAccount(test_account=test_account, test_balance=test_balance + test_amount)

        # document data auto filled
        self.assertDocument(provider_devolution, DOC_TYPE_PROVIDER_DEVOLUTION, test_account.currency)

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

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

        # one accounting history created
        accountings = provider_devolution.accountingdocumenthistory_set
        self.assertEqual(accountings.count(), 1)

        # accounting history info
        accounting = accountings.first()
        operation = Operation.objects.get(pk=accounting.operation_id)

        # one movement created
        movements = operation.operationmovement_set
        self.assertEqual(movements.count(), 1)

        # movement info
        movement = movements.first()
        self.assertMovement(
            test_movement=movement, test_account=test_account,
            test_movement_type=MOVEMENT_TYPE_INPUT, test_amount=test_amount)

        test_date2 = test_date1 - timedelta(days=5)

        provider_devolution.date = test_date2

        provider_devolution = FinanceServices.save_provider_devolution(
            user=self.test_user,
            provider_devolution=provider_devolution)

        test_name2 = provider_devolution.name

        # name changed
        self.assertNotEqual(test_name1, test_name2)

        # account balance remains changed
        self.assertAccount(test_account=test_account, test_balance=test_balance + test_amount)

        # document data auto filled
        self.assertDocument(provider_devolution, DOC_TYPE_PROVIDER_DEVOLUTION, test_account.currency)

        # same finantial history
        finantials = provider_devolution.finantialdocumenthistory_set
        self.assertEqual(finantials.count(), 1)

        # same accounting history
        accountings = provider_devolution.accountingdocumenthistory_set
        self.assertEqual(accountings.count(), 1)
    def test_save_provider_devolution_draft_then_date(self):
        """
        Does draft provider_devolution and then change date
        """
        test_account = Account.objects.create(
            name='Test Account',
            currency=CURRENCY_CUC)
        test_balance = test_account.balance

        test_date1 = timezone.now()
        test_amount = 100
        test_status = STATUS_DRAFT

        provider_devolution = ProviderDevolution(
            provider=self.test_provider,
            date=test_date1,
            account=test_account,
            amount=test_amount,
            status=test_status)

        provider_devolution = FinanceServices.save_provider_devolution(
            user=self.test_user,
            provider_devolution=provider_devolution)

        test_name1 = provider_devolution.name

        # account balance unchanged
        self.assertAccount(test_account=test_account, test_balance=test_balance)

        # document data auto filled
        self.assertDocument(provider_devolution, DOC_TYPE_PROVIDER_DEVOLUTION, test_account.currency)

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

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

        # no accounting history created
        accountings = provider_devolution.accountingdocumenthistory_set
        self.assertEqual(accountings.count(), 0)

        test_date2 = test_date1 - timedelta(days=5)

        provider_devolution.date = test_date2

        provider_devolution = FinanceServices.save_provider_devolution(
            user=self.test_user,
            provider_devolution=provider_devolution)

        test_name2 = provider_devolution.name

        # name changed
        self.assertNotEqual(test_name1, test_name2)

        # account balance unchanged
        self.assertAccount(test_account=test_account, test_balance=test_balance)

        # document data auto filled
        self.assertDocument(provider_devolution, DOC_TYPE_PROVIDER_DEVOLUTION, test_account.currency)

        # no finantial history created
        finantials = provider_devolution.finantialdocumenthistory_set
        self.assertEqual(finantials.count(), 1)

        # no accounting history created
        accountings = provider_devolution.accountingdocumenthistory_set
        self.assertEqual(accountings.count(), 0)