| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import logging |
| 5 |
|
| 6 |
from odoo import api, fields, models |
| 7 |
|
| 8 |
_logger = logging.getLogger(__name__) |
| 9 |
|
| 10 |
try: |
| 11 |
from num2words import num2words |
| 12 |
except ImportError: |
| 13 |
_logger.warning("The num2words python library is not installed, amount-to-text features won't be fully available.") |
| 14 |
num2words = None |
| 15 |
|
| 16 |
|
| 17 |
class AccountMove(models.Model): |
| 18 |
_inherit = 'account.move' |
| 19 |
|
| 20 |
narration = fields.Html(translate=True) |
| 21 |
|
| 22 |
def _get_name_invoice_report(self): |
| 23 |
self.ensure_one() |
| 24 |
if self.company_id.country_id and 'GCC' in self.company_id.country_id.country_group_codes: |
| 25 |
return 'l10n_gcc_invoice.l10n_gcc_report_invoice_document' |
| 26 |
return super()._get_name_invoice_report() |
| 27 |
|
| 28 |
def _l10n_gcc_get_invoice_title(self): |
| 29 |
"""To be overriden by inheriting modules implementing a custom invoice title""" |
| 30 |
self.ensure_one() |
| 31 |
return False |
| 32 |
|
| 33 |
def _num2words(self, number, lang): |
| 34 |
if num2words is None: |
| 35 |
_logger.warning("The library 'num2words' is missing, cannot render textual amounts.") |
| 36 |
return "" |
| 37 |
|
| 38 |
return num2words(number, lang=lang).title() |
| 39 |
|
| 40 |
def _load_narration_translation(self): |
| 41 |
|
| 42 |
|
| 43 |
if not self: |
| 44 |
return |
| 45 |
moves_to_fix = self.env['account.move'] |
| 46 |
for move in self.filtered(lambda m: m.narration and m.is_sale_document(include_receipts=True) and m.company_id.country_id and 'GCC' in m.company_id.country_id.country_group_codes): |
| 47 |
lang = move.partner_id.lang or self.env.user.lang |
| 48 |
if move.company_id.terms_type == 'html' or move.narration != move.company_id.with_context(lang=lang).invoice_terms: |
| 49 |
continue |
| 50 |
moves_to_fix |= move |
| 51 |
if not moves_to_fix: |
| 52 |
return |
| 53 |
self.env['res.company'].flush_model(['invoice_terms']) |
| 54 |
self.env.cr.execute('SELECT "id","invoice_terms" FROM "res_company" WHERE id = any(%s)', [moves_to_fix.company_id.ids]) |
| 55 |
translation_by_company_id = {company_id: narration for company_id, narration in self.env.cr.fetchall()} |
| 56 |
self.env.cache.update_raw(moves_to_fix, self._fields['narration'], [ |
| 57 |
translation_by_company_id[move.company_id.id] |
| 58 |
for move in moves_to_fix |
| 59 |
], dirty=True) |
| 60 |
moves_to_fix.modified(['narration']) |
| 61 |
|
| 62 |
@api.model_create_multi |
| 63 |
def create(self, vals_list): |
| 64 |
moves = super().create(vals_list) |
| 65 |
moves._load_narration_translation() |
| 66 |
return moves |
| 67 |
|
| 68 |
def _compute_narration(self): |
| 69 |
super()._compute_narration() |
| 70 |
|
| 71 |
self.filtered('id')._load_narration_translation() |
| 72 |
|
| 73 |
|
| 74 |
class AccountMoveLine(models.Model): |
| 75 |
_inherit = 'account.move.line' |
| 76 |
|
| 77 |
l10n_gcc_invoice_tax_amount = fields.Float(string='Tax Amount', compute='_compute_tax_amount', min_display_digits='Product Price') |
| 78 |
l10n_gcc_line_name = fields.Char(compute='_compute_l10n_gcc_line_name') |
| 79 |
|
| 80 |
@api.depends('price_subtotal', 'price_total') |
| 81 |
def _compute_tax_amount(self): |
| 82 |
for record in self: |
| 83 |
record.l10n_gcc_invoice_tax_amount = record.price_total - record.price_subtotal |
| 84 |
|
| 85 |
@api.depends('name') |
| 86 |
def _compute_l10n_gcc_line_name(self): |
| 87 |
def lang_product_name(line, lang): |
| 88 |
return line.with_context(lang=lang).product_id.display_name |
| 89 |
for line in self: |
| 90 |
if line.product_id and line.name in [lang_product_name(line, lang) for lang in ('ar_001', 'en_US')]: |
| 91 |
line.l10n_gcc_line_name = lang_product_name(line, line.move_id.partner_id.lang) |
| 92 |
else: |
| 93 |
line.l10n_gcc_line_name = line.name |
| 94 |
|
| 95 |
def _get_child_lines(self): |
| 96 |
|
| 97 |
self.ensure_one() |
| 98 |
res = super()._get_child_lines() |
| 99 |
|
| 100 |
for line in res: |
| 101 |
line['l10n_gcc_invoice_tax_amount'] = line['price_total'] - line['price_subtotal'] |
| 102 |
|
| 103 |
return res |
| 104 |
|
| 105 |
def _l10n_gcc_get_section_total(self): |
| 106 |
section_lines = self._get_section_lines() |
| 107 |
return sum(section_lines.mapped('price_total')) |
| 108 |
|
| 109 |
def _l10n_gcc_get_section_tax_amount(self): |
| 110 |
section_lines = self._get_section_lines() |
| 111 |
return sum(section_lines.mapped('l10n_gcc_invoice_tax_amount')) |