Debian Codegraph @

addons/l10n_fr_pos_cert/models/res_company.py

raw
1 # -*- coding: utf-8 -*-
2 # Part of Odoo. See LICENSE file for full copyright and licensing details.
3 from odoo import models, api, fields, _
4 from odoo.exceptions import UserError
5 from datetime import datetime
6 from odoo.fields import Datetime, Date
7 from odoo.tools.misc import format_date
8 import pytz
9
10
11 def ctx_tz(record, field):
12 res_lang = None
13 ctx = record.env.context
14 timestamp = Datetime.from_string(record[field])
15 if ctx.get('lang'):
16 res_lang = record.env['res.lang']._get_data(code=ctx['lang'])
17 if res_lang:
18 timestamp = pytz.utc.localize(timestamp, is_dst=False)
19 tz = record.env.tz
20 return datetime.strftime(timestamp.astimezone(tz), res_lang.date_format + ' ' + res_lang.time_format)
21 return Datetime.context_timestamp(record, timestamp)
22
23
24 class ResCompany(models.Model):
25 _inherit = 'res.company'
26
27 l10n_fr_pos_cert_sequence_id = fields.Many2one('ir.sequence')
28
29 @api.model_create_multi
30 def create(self, vals_list):
31 companies = super().create(vals_list)
32 for company in companies:
33 #when creating a new french company, create the securisation sequence as well
34 if company._is_accounting_unalterable():
35 sequence_fields = ['l10n_fr_pos_cert_sequence_id']
36 company._create_secure_sequence(sequence_fields)
37 return companies
38
39 def write(self, vals):
40 res = super(ResCompany, self).write(vals)
41 #if country changed to fr, create the securisation sequence
42 for company in self:
43 if company._is_accounting_unalterable():
44 sequence_fields = ['l10n_fr_pos_cert_sequence_id']
45 company._create_secure_sequence(sequence_fields)
46 return res
47
48 def _action_check_pos_hash_integrity(self):
49 return self.env.ref('l10n_fr_pos_cert.action_report_pos_hash_integrity').report_action(self.id)
50
51 def _check_pos_hash_integrity(self):
52 """Checks that all posted or invoiced pos orders have still the same data as when they were posted
53 and raises an error with the result.
54 """
55 def build_order_info(order):
56 entry_reference = _('(Receipt ref.: %s)')
57 order_reference_string = order.pos_reference and entry_reference % order.pos_reference or ''
58 return [ctx_tz(order, 'date_order'), order.l10n_fr_hash, order.name, order_reference_string, ctx_tz(order, 'write_date')]
59
60 msg_alert = ''
61 report_dict = {}
62 if self._is_accounting_unalterable():
63 orders = self.with_context(prefetch_fields=False).env['pos.order'].search([('state', 'in', ['paid', 'done']), ('company_id', '=', self.id),
64 ('l10n_fr_secure_sequence_number', '!=', 0)], order="l10n_fr_secure_sequence_number ASC")
65
66 if not orders:
67 msg_alert = (_('There isn\'t any order flagged for data inalterability yet for the company %s. This mechanism only runs for point of sale orders generated after the installation of the module France - Certification CGI 286 I-3 bis. - POS', self.env.company.name))
68 raise UserError(msg_alert)
69
70 previous_hash = u''
71 corrupted_orders = []
72 for order in orders:
73 if order.l10n_fr_hash != order._compute_hash(previous_hash=previous_hash):
74 corrupted_orders.append(order.name)
75 msg_alert = (_('Corrupted data on point of sale order with id %s.', order.id))
76 previous_hash = order.l10n_fr_hash
77 orders.invalidate_recordset()
78
79 orders_sorted_date = orders.sorted(lambda o: o.date_order)
80 start_order_info = build_order_info(orders_sorted_date[0])
81 end_order_info = build_order_info(orders_sorted_date[-1])
82
83 report_dict.update({
84 'first_order_name': start_order_info[2],
85 'first_order_hash': start_order_info[1],
86 'first_order_date': start_order_info[0],
87 'last_order_name': end_order_info[2],
88 'last_order_hash': end_order_info[1],
89 'last_order_date': end_order_info[0],
90 })
91 corrupted_orders = ', '.join([o for o in corrupted_orders])
92 return {
93 'result': report_dict or 'None',
94 'msg_alert': msg_alert or 'None',
95 'printing_date': format_date(self.env, Date.to_string( Date.today())),
96 'corrupted_orders': corrupted_orders or 'None'
97 }
98 else:
99 raise UserError(_('Accounting is not unalterable for the company %s. This mechanism is designed for companies where accounting is unalterable.', self.env.company.name))