Debian Codegraph @

addons/l10n_id/models/qris_transaction.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2 from datetime import timedelta
3 from odoo import api, fields, models, _
4 from odoo.exceptions import ValidationError
5
6
7 class L10n_IdQrisTransaction(models.Model):
8 """QRIS Transaction
9
10 General table to store a certian unique transaction with QRIS details attached
11 """
12 _name = 'l10n_id.qris.transaction'
13 _description = "Record of QRIS transactions"
14
15 model = fields.Char(string="Model") # payment in respond to which model
16 model_id = fields.Char(string="Model ID") # id/uuid
17
18 # Fields that store the QRIS details coming from API request
19 qris_invoice_id = fields.Char(readonly=True)
20 qris_amount = fields.Integer(readonly=True)
21 qris_content = fields.Char(readonly=True)
22 qris_creation_datetime = fields.Datetime(readonly=True)
23
24 bank_id = fields.Many2one("res.partner.bank", help="Bank used to generate the current QRIS transaction")
25 paid = fields.Boolean(help="Payment Status of QRIS")
26
27 def _get_supported_models(self):
28 return ['account.move']
29
30 @api.constrains('model')
31 def _constraint_model(self):
32 # only allow supported models
33 if self.model not in self._get_supported_models():
34 raise ValidationError(_("QRIS capability is not extended to model %s yet!", self.model))
35
36 def _get_record(self):
37 """ Get the backend invoice record that the qris transaction is handling
38 To be overriden in other modules"""
39 self.ensure_one()
40 if self.model != 'account.move':
41 return
42 return self.env['account.move'].browse(int(self.model_id)).exists()
43
44 @api.model
45 def _get_latest_transaction(self, model, model_id):
46 """ Find latest transaction associated to the model and model_id """
47 return self.search([('model', '=', model), ('model_id', '=', model_id)], order='qris_creation_datetime desc', limit=1)
48
49 def _l10n_id_get_qris_qr_statuses(self):
50 """ Fetch the result of the transaction
51
52 :param invoice_bank_id (Model <res.partner.bank>): bank (with QRIS configuration)
53 :returns tuple(bool, dict): paid/unpaid status and status_response from QRIS
54 """
55 # storing all failure transactions in case final result is unpaid
56 unpaid_status_data = []
57
58 # Looping to make requests is far from ideal, but we have no choices as they don't allow getting multiple QR result at once.
59 # Ensure to loop in reverse and check from the most recent QR code.
60 for transaction in self.sorted(lambda t: t.qris_creation_datetime):
61 status_response = self.sudo().bank_id._l10n_id_qris_fetch_status(transaction)
62 if status_response['data'].get('qris_status') == 'paid':
63 transaction.paid = True
64 return {
65 'paid': True,
66 'qr_statuses': [status_response['data']]
67 }
68 else:
69 unpaid_status_data.append(status_response['data'])
70
71 return {
72 'paid': False,
73 'qr_statuses': unpaid_status_data
74 }
75
76 @api.autovacuum
77 def _gc_remove_pointless_qris_transactions(self):
78 """ Removes unpaid transactions that have been for more than 35 minutes.
79 These can no longer be paid and status will no longer change
80 """
81 time_limit = fields.Datetime.now() - timedelta(seconds=2100)
82 transactions = self.env['l10n_id.qris.transaction'].search([('qris_creation_datetime', '<=', time_limit), ('paid', '=', False)])
83 transactions.unlink()