def test_agency_match_then_change_agency(self): """ Does match between agency_payment and agency_invoice then change agency """ test_account = Account.objects.create(name='Test Account', currency=CURRENCY_CUC, balance=1000) test_agency1 = Agency.objects.create(name='Test Agency1') test_date = timezone.now() test_amount = 100 test_status = STATUS_READY agency_payment = AgencyPayment(date=test_date, account=test_account, amount=test_amount, agency=test_agency1, status=test_status) agency_payment = FinanceService.save_agency_payment( user=self.test_user, agency_payment=agency_payment) agency_invoice = AgencyInvoice(date=test_date, amount=test_amount, agency=test_agency1, status=test_status) agency_invoice = FinanceService.save_agency_invoice( user=self.test_user, agency_invoice=agency_invoice) agency_match = AgencyDocumentMatch(credit_document=agency_payment, debit_document=agency_invoice, matched_amount=test_amount) agency_match = FinanceService.save_agency_match(agency_match) agency_payment.refresh_from_db() agency_invoice.refresh_from_db() self.assertEqual(agency_payment.matched_amount, test_amount) self.assertEqual(agency_invoice.matched_amount, test_amount) test_agency2 = Agency.objects.create(name='Test Agency2') agency_invoice.agency = test_agency2 with self.assertRaisesMessage(ValidationError, ERROR_MATCH_AGENCY): agency_invoice = FinanceService.save_agency_invoice( user=self.test_user, agency_invoice=agency_invoice)
def test_agency_match_then_increase_amount(self): """ Does match between agency_payment and agency_invoice then increase amount """ test_account = Account.objects.create(name='Test Account', currency=CURRENCY_CUC, balance=1000) test_agency = Agency.objects.create(name='Test Agency') test_date = timezone.now() test_amount = 100 test_status = STATUS_READY agency_payment = AgencyPayment(date=test_date, account=test_account, amount=test_amount, agency=test_agency, status=test_status) agency_payment = FinanceService.save_agency_payment( user=self.test_user, agency_payment=agency_payment) agency_invoice = AgencyInvoice(date=test_date, amount=test_amount, agency=test_agency, status=test_status) agency_invoice = FinanceService.save_agency_invoice( user=self.test_user, agency_invoice=agency_invoice) agency_match = AgencyDocumentMatch(credit_document=agency_payment, debit_document=agency_invoice, matched_amount=test_amount) agency_match = FinanceService.save_agency_match(agency_match) agency_payment.refresh_from_db() agency_invoice.refresh_from_db() self.assertEqual(agency_payment.matched_amount, test_amount) self.assertEqual(agency_invoice.matched_amount, test_amount) agency_invoice.amount = agency_invoice.amount + 20 agency_invoice = FinanceService.save_agency_invoice( user=self.test_user, agency_invoice=agency_invoice) self.assertEqual(agency_invoice.matched_amount, test_amount)
def test_save_agency_payment_status_ready(self): """ Does ready agency_payment """ test_account = Account.objects.create(name='Test Account', currency=CURRENCY_CUC) test_balance = test_account.balance test_date = timezone.now() test_amount = 100 test_status = STATUS_READY agency_payment = AgencyPayment(agency=self.test_agency, date=test_date, account=test_account, amount=test_amount, status=test_status) agency_payment = FinanceService.save_agency_payment( user=self.test_user, agency_payment=agency_payment) # account balance incremented self.assertAccount(test_account=test_account, test_balance=test_balance + test_amount) # document data auto filled self.assertDocument(agency_payment, DOC_TYPE_AGENCY_PAYMENT, test_account.currency) # one finantial history created finantials = agency_payment.finantialdocumenthistory_set self.assertEqual(finantials.count(), 1) # finantial history info finantial = finantials.first() self.assertFinantialHistory(test_finantial_history=finantial, test_document=agency_payment, test_user=self.test_user, test_old_status=None, test_new_status=test_status) # one accounting history created accountings = agency_payment.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)
def test_save_agency_payment_status_draft(self): """ Does draft agency_payment """ test_account = Account.objects.create(name='Test Account', currency=CURRENCY_CUC) test_balance = test_account.balance test_date = timezone.now() test_amount = 100 test_status = STATUS_DRAFT agency_payment = AgencyPayment(agency=self.test_agency, date=test_date, account=test_account, amount=test_amount, status=test_status) agency_payment = FinanceService.save_agency_payment( user=self.test_user, agency_payment=agency_payment) # account balance unchanged self.assertAccount(test_account=test_account, test_balance=test_balance) # document data auto filled self.assertDocument(agency_payment, DOC_TYPE_AGENCY_PAYMENT, test_account.currency) # one finantial history created finantials = agency_payment.finantialdocumenthistory_set self.assertEqual(finantials.count(), 1) # finantial history info finantial = finantials.first() self.assertFinantialHistory(test_finantial_history=finantial, test_document=agency_payment, test_user=self.test_user, test_old_status=None, test_new_status=test_status) # no accounting history created accountings = agency_payment.accountingdocumenthistory_set self.assertEqual(accountings.count(), 0)
def test_agency_match_different_agency(self): """ Does match between agency_payment and agency_invoice with different loan_entities """ test_account = Account.objects.create(name='Test Account', currency=CURRENCY_CUC, balance=1000) test_agency1 = Agency.objects.create(name='Test Agency1') test_date = timezone.now() test_amount = 100 test_status = STATUS_READY agency_payment = AgencyPayment(date=test_date, account=test_account, amount=test_amount, agency=test_agency1, status=test_status) agency_payment = FinanceService.save_agency_payment( user=self.test_user, agency_payment=agency_payment) test_agency2 = Agency.objects.create(name='Test Agency2') agency_invoice = AgencyInvoice(date=test_date, amount=test_amount, agency=test_agency2, status=test_status) agency_invoice = FinanceService.save_agency_invoice( user=self.test_user, agency_invoice=agency_invoice) agency_match = AgencyDocumentMatch(credit_document=agency_payment, debit_document=agency_invoice, matched_amount=test_amount) with self.assertRaises(ValidationError): agency_match = FinanceService.save_agency_match(agency_match)
def test_save_agency_payment_ready_then_amount_account(self): """ Does ready agency_payment and then change amount and account """ test_account1 = Account.objects.create(name='Test Account1', currency=CURRENCY_CUC) test_balance1 = test_account1.balance test_date = timezone.now() test_amount1 = 100 test_status = STATUS_READY agency_payment = AgencyPayment(agency=self.test_agency, date=test_date, account=test_account1, amount=test_amount1, status=test_status) agency_payment = FinanceService.save_agency_payment( user=self.test_user, agency_payment=agency_payment) # account balance incremented self.assertAccount(test_account=test_account1, test_balance=test_balance1 + test_amount1) # document data auto filled self.assertDocument(agency_payment, DOC_TYPE_AGENCY_PAYMENT, test_account1.currency) # one finantial history created finantials = agency_payment.finantialdocumenthistory_set self.assertEqual(finantials.count(), 1) # finantial history info finantial = finantials.first() self.assertFinantialHistory(test_finantial_history=finantial, test_document=agency_payment, test_user=self.test_user, test_old_status=None, test_new_status=test_status) # one accounting history created accountings = agency_payment.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_account1, test_movement_type=MOVEMENT_TYPE_INPUT, test_amount=test_amount1) test_account2 = Account.objects.create(name='Test Account2', currency=CURRENCY_USD) test_balance2 = test_account2.balance test_amount2 = 50 agency_payment.account = test_account2 agency_payment.amount = test_amount2 agency_payment = FinanceService.save_agency_payment( user=self.test_user, agency_payment=agency_payment) # account balance updated self.assertAccount(test_account=test_account1, test_balance=test_balance1) self.assertAccount(test_account=test_account2, test_balance=test_balance2 + test_amount2) # document data auto filled self.assertDocument(agency_payment, DOC_TYPE_AGENCY_PAYMENT, test_account2.currency) # no aditional finantial history finantials = agency_payment.finantialdocumenthistory_set self.assertEqual(finantials.count(), 1) # two accounting history created accountings = agency_payment.accountingdocumenthistory_set self.assertEqual(accountings.count(), 3) elements = accountings.all() # accounting history info accounting = elements[1] 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_account2, test_movement_type=MOVEMENT_TYPE_INPUT, test_amount=test_amount2) # accounting history info accounting = elements[2] 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_account1, test_movement_type=MOVEMENT_TYPE_OUTPUT, test_amount=test_amount1)