Skip to content

Commit

Permalink
[WIP] l10n_do_accounting: migration to v17
Browse files Browse the repository at this point in the history
  • Loading branch information
jose-pcg committed Feb 8, 2024
1 parent 634a78b commit 4acb9f8
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 189 deletions.
1 change: 1 addition & 0 deletions l10n_do_accounting/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"license": "LGPL-3",
"website": "https://github.com/odoo-dominicana",
"version": "17.0.1.0.0",
"countries": ["do"],
# any module necessary for this one to work correctly
"depends": ["l10n_latam_invoice_document", "l10n_do"],
# always loaded
Expand Down
46 changes: 23 additions & 23 deletions l10n_do_accounting/data/l10n_latam.document.type.csv
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
id,sequence,code,name,report_name,internal_type,l10n_do_ncf_type,doc_code_prefix,country_id/id,is_vat_required,active
ncf_fiscal_client,10,B,Crédito Fiscal,Factura de Crédito Fiscal,invoice,01,B01,base.do,True,TRUE
ncf_consumer_supplier,30,B,Consumo,Factura de Consumo,invoice,02,B02,base.do,False,TRUE
ncf_debit_note_client,40,B,Nota de Débito,Nota de Débito,debit_note,03,B03,base.do,True,TRUE
ncf_credit_note_client,60,B,Nota de Crédito,Nota de Crédito,credit_note,04,B04,base.do,False,TRUE
ncf_informal_supplier,80,B,Comprobante de Compra,Factura de Compra,invoice,11,B11,base.do,True,TRUE
ncf_unique_client,90,B,Único Ingreso,Factura de Único Ingreso,invoice,12,B12,base.do,False,TRUE
ncf_minor_supplier,100,B,Gasto Menor,Gasto Menor,invoice,13,B13,base.do,False,TRUE
ncf_special_client,110,B,Régimen Especial,Factura de Régimen Especial,invoice,14,B14,base.do,True,TRUE
ncf_gov_client,120,B,Factura Gubernamental,Factura Gubernamental,invoice,15,B15,base.do,True,TRUE
ncf_export_client,130,B,Exportación,Factura de Exportación,invoice,16,B16,base.do,True,TRUE
ncf_exterior_supplier,140,B,Pago al Exterior,Factura de Pago al Exterior,invoice,17,B17,base.do,False,TRUE
non_fiscal_import_supplier,150,N,Importación,Factura de Importación,invoice,,IMP,base.do,False,FALSE
ecf_fiscal_client,160,E,Crédito Fiscal Electrónica,Factura de Crédito Fiscal Electrónica,invoice,31,E31,base.do,True,TRUE
ecf_consumer_supplier,170,E,Consumo Electrónica,Factura de Consumo Electrónica,invoice,32,E32,base.do,False,TRUE
ecf_debit_note_client,180,E,Nota de Débito Electrónica,Nota de Débito Electrónica,debit_note,33,E33,base.do,True,TRUE
ecf_credit_note_client,190,E,Nota de Crédito Electrónica,Nota de Crédito Electrónica,credit_note,34,E34,base.do,True,TRUE
ecf_informal_supplier,200,E,Compras Electrónica,Factura de Compra Electrónica,invoice,41,E41,base.do,True,TRUE
ecf_minor_supplier,220,E,Gasto Menor Electrónica,Factura Gasto Menor Electrónica,invoice,43,E43,base.do,False,TRUE
ecf_special_client,230,E,Régimen Especial Electrónica,Factura de Régimen Especial Electrónica,invoice,44,E44,base.do,True,TRUE
ecf_gov_client,240,E,Factura Gubernamental Electrónica,Factura Gubernamental Electrónica,invoice,45,E45,base.do,True,TRUE
ecf_export_client,250,E,Exportación Electrónica,Factura de Exportación Electrónica,invoice,46,E46,base.do,True,TRUE
ecf_exterior_supplier,260,E,Pago al Exterior Electrónica,Factura de Pago al Exterior Electrónica,invoice,47,E47,base.do,False,TRUE
id,sequence,code,name@es,name,report_name,internal_type,l10n_do_ncf_type,doc_code_prefix,country_id/id,is_vat_required,active
ncf_fiscal_client,10,B,Crédito Fiscal,Fiscal Credit,Factura de Crédito Fiscal,invoice,1,B01,base.do,TRUE,TRUE
ncf_consumer_supplier,30,B,Consumo,Consumer,Factura de Consumo,invoice,2,B02,base.do,FALSE,TRUE
ncf_debit_note_client,40,B,Nota de Débito,Debit Note,Nota de Débito,debit_note,3,B03,base.do,TRUE,TRUE
ncf_credit_note_client,60,B,Nota de Crédito,Credit Note,Nota de Crédito,credit_note,4,B04,base.do,FALSE,TRUE
ncf_informal_supplier,80,B,Comprobante de Compra,Purchase receipt,Factura de Compra,invoice,11,B11,base.do,TRUE,TRUE
ncf_unique_client,90,B,Único Ingreso,Single Income,Factura de Único Ingreso,invoice,12,B12,base.do,FALSE,TRUE
ncf_minor_supplier,100,B,Gasto Menor,Minor Expense,Gasto Menor,invoice,13,B13,base.do,FALSE,TRUE
ncf_special_client,110,B,Régimen Especial,Special Regime,Factura de Régimen Especial,invoice,14,B14,base.do,TRUE,TRUE
ncf_gov_client,120,B,Factura Gubernamental,Government Invoice,Factura Gubernamental,invoice,15,B15,base.do,TRUE,TRUE
ncf_export_client,130,B,Exportación,Export,Factura de Exportación,invoice,16,B16,base.do,TRUE,TRUE
ncf_exterior_supplier,140,B,Pago al Exterior,Foreign Payment,Factura de Pago al Exterior,invoice,17,B17,base.do,FALSE,TRUE
non_fiscal_import_supplier,150,N,Importación,Import,Factura de Importación,invoice,,IMP,base.do,FALSE,FALSE
ecf_fiscal_client,160,E,Crédito Fiscal Electrónica,Electronic Fiscal Credit,Factura de Crédito Fiscal Electrónica,invoice,31,E31,base.do,TRUE,TRUE
ecf_consumer_supplier,170,E,Consumo Electrónica,Electronic Consumer,Factura de Consumo Electrónica,invoice,32,E32,base.do,FALSE,TRUE
ecf_debit_note_client,180,E,Nota de Débito Electrónica,Electronic Debit Note,Nota de Débito Electrónica,debit_note,33,E33,base.do,TRUE,TRUE
ecf_credit_note_client,190,E,Nota de Crédito Electrónica,Electronic Credit Note,Nota de Crédito Electrónica,credit_note,34,E34,base.do,TRUE,TRUE
ecf_informal_supplier,200,E,Compras Electrónica,Electronic Purchase,Factura de Compra Electrónica,invoice,41,E41,base.do,TRUE,TRUE
ecf_minor_supplier,220,E,Gasto Menor Electrónica,Electronic Minor Expense,Factura Gasto Menor Electrónica,invoice,43,E43,base.do,FALSE,TRUE
ecf_special_client,230,E,Régimen Especial Electrónica,Electronic Special Regime,Factura de Régimen Especial Electrónica,invoice,44,E44,base.do,TRUE,TRUE
ecf_gov_client,240,E,Factura Gubernamental Electrónica,Electronic Government Invoice,Factura Gubernamental Electrónica,invoice,45,E45,base.do,TRUE,TRUE
ecf_export_client,250,E,Exportación Electrónica,Electronic Export,Factura de Exportación Electrónica,invoice,46,E46,base.do,TRUE,TRUE
ecf_exterior_supplier,260,E,Pago al Exterior Electrónica,Electronic Foreign Payment,Factura de Pago al Exterior Electrónica,invoice,47,E47,base.do,FALSE,TRUE
2 changes: 1 addition & 1 deletion l10n_do_accounting/models/account_journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def create(self, vals_list):

def write(self, values):
to_check = {"type", "l10n_latam_use_documents"}
res = super().write(values)
res = super(AccountJournal, self).write(values)
if to_check.intersection(set(values.keys())):
for rec in self:
rec._l10n_do_create_document_types()
Expand Down
202 changes: 125 additions & 77 deletions l10n_do_accounting/models/account_move.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import re
from psycopg2 import sql
from werkzeug import urls

from odoo import models, fields, api, _
from odoo.osv import expression
from odoo.exceptions import ValidationError, UserError, AccessError
from odoo.tools.sql import column_exists, create_column, drop_index, index_exists


class AccountMove(models.Model):
Expand Down Expand Up @@ -124,36 +124,79 @@ def _get_l10n_do_income_type(self):
"manually because a new expiration date was set on journal",
)

def init(self):
super(AccountMove, self).init()

if not self._abstract and self._sequence_index:
index_name = self._table + "_l10n_do_sequence_index"
_sql_constraints = [
(
"unique_l10n_do_fiscal_number_sales",
"",
"Another document with the same fiscal number already exists.",
),
(
"unique_l10n_do_fiscal_number_purchase_manual",
"",
"Another document for the same partner with the same fiscal number already exists.",
),
(
"unique_l10n_do_fiscal_number_purchase_internal",
"",
"Another document for the same partner with the same fiscal number already exists.",
),
]

# def init(self):
# super(AccountMove, self).init()
#
# if not self._abstract and self._sequence_index:
# index_name = self._table + "_l10n_do_sequence_index"
# self.env.cr.execute(
# "SELECT indexname FROM pg_indexes WHERE indexname = %s", (index_name,)
# )
# if not self.env.cr.fetchone():
# self.env.cr.execute(
# sql.SQL(
# """
# CREATE INDEX {index_name} ON {table}
# ({sequence_index},
# l10n_do_sequence_prefix desc,
# l10n_do_sequence_number desc,
# {field});
# CREATE INDEX {index2_name} ON {table}
# ({sequence_index},
# id desc,
# l10n_do_sequence_prefix);
# """
# ).format(
# sequence_index=sql.Identifier(self._sequence_index),
# index_name=sql.Identifier(index_name),
# index2_name=sql.Identifier(index_name + "2"),
# table=sql.Identifier(self._table),
# field=sql.Identifier(self._l10n_do_sequence_field),
# )
# )

def _auto_init(self):
if not index_exists(self.env.cr, "account_move_unique_l10n_do_fiscal_number_sales"):
drop_index(self.env.cr, "unique_l10n_do_fiscal_number_purchase_manual", self._table)
drop_index(self.env.cr, "unique_l10n_do_fiscal_number_purchase_internal", self._table)
self.env.cr.execute(
"SELECT indexname FROM pg_indexes WHERE indexname = %s", (index_name,)
"""
CREATE UNIQUE INDEX account_move_unique_l10n_do_fiscal_number_sales
ON account_move(l10n_do_fiscal_number, company_id)
WHERE (state = 'posted'
AND (l10n_latam_document_type_id IS NOT NULL

Check warning on line 185 in l10n_do_accounting/models/account_move.py

View workflow job for this annotation

GitHub Actions / flake8

trailing whitespace

Check warning on line 185 in l10n_do_accounting/models/account_move.py

View workflow job for this annotation

GitHub Actions / flake8

trailing whitespace
AND move_type NOT IN ('in_invoice', 'in_refund', 'in_receipt')));
CREATE UNIQUE INDEX unique_l10n_do_fiscal_number_purchase_manual
ON account_move(l10n_do_fiscal_number, commercial_partner_id, company_id)
WHERE (state = 'posted'
AND (l10n_latam_document_type_id IS NOT NULL AND move_type IN ('in_invoice', 'in_refund', 'in_receipt')

Check warning on line 190 in l10n_do_accounting/models/account_move.py

View workflow job for this annotation

GitHub Actions / flake8

trailing whitespace

Check warning on line 190 in l10n_do_accounting/models/account_move.py

View workflow job for this annotation

GitHub Actions / flake8

trailing whitespace
AND l10n_latam_manual_document_number = 't'));
CREATE UNIQUE INDEX unique_l10n_do_fiscal_number_purchase_internal
ON account_move(l10n_do_fiscal_number, company_id)
WHERE (state = 'posted'
AND (l10n_latam_document_type_id IS NOT NULL AND move_type IN ('in_invoice', 'in_refund', 'in_receipt')

Check warning on line 195 in l10n_do_accounting/models/account_move.py

View workflow job for this annotation

GitHub Actions / flake8

trailing whitespace

Check warning on line 195 in l10n_do_accounting/models/account_move.py

View workflow job for this annotation

GitHub Actions / flake8

trailing whitespace
AND l10n_latam_manual_document_number = 'f'));
"""
)
if not self.env.cr.fetchone():
self.env.cr.execute(
sql.SQL(
"""
CREATE INDEX {index_name} ON {table}
({sequence_index},
l10n_do_sequence_prefix desc,
l10n_do_sequence_number desc,
{field});
CREATE INDEX {index2_name} ON {table}
({sequence_index},
id desc,
l10n_do_sequence_prefix);
"""
).format(
sequence_index=sql.Identifier(self._sequence_index),
index_name=sql.Identifier(index_name),
index2_name=sql.Identifier(index_name + "2"),
table=sql.Identifier(self._table),
field=sql.Identifier(self._l10n_do_sequence_field),
)
)
return super()._auto_init()

@api.model
def _name_search(
Expand Down Expand Up @@ -204,7 +247,9 @@ def _compute_l10n_do_show_expiration_date_msg(self):
and not inv.l10n_latam_manual_document_number
)
for invoice in l10n_do_internal_invoices:
invoice.l10n_do_show_expiration_date_msg = invoice._l10n_do_is_new_expiration_date()
invoice.l10n_do_show_expiration_date_msg = (
invoice._l10n_do_is_new_expiration_date()
)

(self - l10n_do_internal_invoices).l10n_do_show_expiration_date_msg = False

Expand All @@ -226,21 +271,24 @@ def _compute_l10n_do_enable_first_sequence(self):
and not inv.l10n_latam_manual_document_number
)
for invoice in l10n_do_internal_invoices:
invoice.l10n_do_enable_first_sequence = not bool(
self.search_count(
[
("company_id", "=", invoice.company_id.id),
("move_type", "=", invoice.move_type),
(
"l10n_latam_document_type_id",
"=",
invoice.l10n_latam_document_type_id.id,
),
("posted_before", "=", True),
("id", "!=", invoice.id or invoice._origin.id),
],
invoice.l10n_do_enable_first_sequence = (
not bool(
self.search_count(
[
("company_id", "=", invoice.company_id.id),
("move_type", "=", invoice.move_type),
(
"l10n_latam_document_type_id",
"=",
invoice.l10n_latam_document_type_id.id,
),
("posted_before", "=", True),
("id", "!=", invoice.id or invoice._origin.id),
],
)
)
) or invoice.l10n_do_show_expiration_date_msg
or invoice.l10n_do_show_expiration_date_msg
)

(self - l10n_do_internal_invoices).l10n_do_enable_first_sequence = False

Expand Down Expand Up @@ -350,39 +398,39 @@ def _compute_l10n_do_electronic_stamp(self):

(self - l10n_do_ecf_invoice).l10n_do_electronic_stamp = False

@api.constrains("name", "journal_id", "state", "l10n_do_fiscal_number")
def _check_unique_sequence_number(self):
l10n_do_invoices = self.filtered(
lambda inv: inv.l10n_latam_use_documents
and inv.country_code == "DO"
and inv.is_sale_document()
and inv.state == "posted"
)
if l10n_do_invoices:
self.flush_model(
["name", "journal_id", "move_type", "state", "l10n_do_fiscal_number"]
)
self._cr.execute(
"""
SELECT move2.id, move2.l10n_do_fiscal_number
FROM account_move move
INNER JOIN account_move move2 ON
move2.l10n_do_fiscal_number = move.l10n_do_fiscal_number
AND move2.journal_id = move.journal_id
AND move2.move_type = move.move_type
AND move2.id != move.id
WHERE move.id IN %s AND move2.state = 'posted'
""",
[tuple(l10n_do_invoices.ids)],
)
res = self._cr.fetchone()
if res:
raise ValidationError(
_("There is already a sale invoice with fiscal number %s")
% self.l10n_do_fiscal_number
)

super(AccountMove, (self - l10n_do_invoices))._check_unique_sequence_number()
# @api.constrains("name", "journal_id", "state", "l10n_do_fiscal_number")
# def _check_unique_sequence_number(self):
# l10n_do_invoices = self.filtered(
# lambda inv: inv.l10n_latam_use_documents
# and inv.country_code == "DO"
# and inv.is_sale_document()
# and inv.state == "posted"
# )
# if l10n_do_invoices:
# self.flush_model(
# ["name", "journal_id", "move_type", "state", "l10n_do_fiscal_number"]
# )
# self._cr.execute(
# """
# SELECT move2.id, move2.l10n_do_fiscal_number
# FROM account_move move
# INNER JOIN account_move move2 ON
# move2.l10n_do_fiscal_number = move.l10n_do_fiscal_number
# AND move2.journal_id = move.journal_id
# AND move2.move_type = move.move_type
# AND move2.id != move.id
# WHERE move.id IN %s AND move2.state = 'posted'
# """,
# [tuple(l10n_do_invoices.ids)],
# )
# res = self._cr.fetchone()
# if res:
# raise ValidationError(
# _("There is already a sale invoice with fiscal number %s")
# % self.l10n_do_fiscal_number
# )
#
# super(AccountMove, (self - l10n_do_invoices))._check_unique_sequence_number()

@api.constrains(
"l10n_do_fiscal_number", "partner_id", "company_id", "posted_before"
Expand Down
2 changes: 1 addition & 1 deletion l10n_do_accounting/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AccountMoveLine(models.Model):

@api.depends("quantity", "discount", "price_unit", "tax_ids", "currency_id")
def _compute_totals(self):
super()._compute_totals()
super(AccountMoveLine, self)._compute_totals()
for line in self:
if line.display_type != "product":
line.l10n_do_itbis_amount = False
Expand Down
Loading

0 comments on commit 4acb9f8

Please sign in to comment.