Skip to content

Commit

Permalink
add required values
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkhao committed May 2, 2024
1 parent 59f6ff7 commit 7b0ef08
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 48 deletions.
56 changes: 35 additions & 21 deletions export_invoice_edi_auchan/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

from odoo import fields, models

from ..schema.base import SegmentInterfaceExc
from ..schema.invoice_footer import PIESegment
from ..schema.invoice_header import ENTSegment
from ..schema.invoice_line import LIGSegment
from ..schema.invoice_taxes import TVASegment
from ..schema.partner import PARSegment

_logger = logging.getLogger()

Expand All @@ -28,56 +28,70 @@ def _find_bl_info(self):
"""Find entête "Numéro de BL" and date"""
raise NotImplementedError

def _render_segment(self, segment, vals):
try:
res = segment(**vals).render()
except SegmentInterfaceExc as e:
self.env.context["export_auchan_errors"] += str(e)
else:
return res

def _prepare_export_data(self, idx):
self.ensure_one()
res = []
source_orders = self.line_ids.sale_line_ids.order_id
bl_nbr, bl_date = self._find_bl_info()
self = self.with_context(export_auchan_errors="")
# Segment Entete facture
res.append(
ENTSegment(
**{
self._render_segment(
ENTSegment,
{
"invoice": self,
"source_orders": source_orders,
"bl_nbr": bl_nbr,
"bl_date": bl_date,
}
).render()
},
)
)
# segment partner
res.append(
PARSegment(
**{
self._render_segment(
ENTSegment,
{
"invoice": self,
}
).render()
},
)
)
# segment ligne de fatcure
for idx, line in enumerate(self.invoice_line_ids, start=0):
res.append(
LIGSegment(
**{
self._render_segment(
LIGSegment,
{
"line": line,
"line_num": idx,
}
).render()
},
)
)
# Segment pied facture
res.append(
PIESegment(
**{
self._render_segment(
PIESegment,
{
"invoice": self,
}
).render()
},
)
)
# segment ligne de TVA (détail des TVA)
for tax_line in self.line_ids.filtered(lambda x: x.tax_line_id):
res.append(
TVASegment(
**{
self._render_segment(
TVASegment,
{
"tax_line": tax_line,
}
).render()
},
)
)
# Segment END
res.append("END")
Expand Down
23 changes: 19 additions & 4 deletions export_invoice_edi_auchan/schema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
from odoo.tools import float_compare


class SegmentInterfaceExc(Exception):
pass


class SegmentInterface:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)

def _format_values(self, size, value="", ctx=False):
def _format_values(self, size, value="", required=True, ctx=False):
if required and not value:
raise ValueError()
if not ctx:
ctx = {}
if not value:
Expand Down Expand Up @@ -49,9 +55,18 @@ def _format_values(self, size, value="", ctx=False):

def render(self):
res = ""
for fmt_data in self.get_values():
fmt_val = self._format_values(*fmt_data)
res += fmt_val + ";"
errors = []
for idx, fmt_data in enumerate(self.get_values(), start=1):
try:
fmt_val = self._format_values(*fmt_data)
res += fmt_val + ";"
except ValueError:
errors += [(self.__name__, idx)]
if errors:
errstr = ""
for el in errors:
errstr += f"Segment {el[0]}: missing value on line {el[1]}\n"
raise SegmentInterfaceExc(errstr)
return res[:-1]

def get_values(self):
Expand Down
33 changes: 19 additions & 14 deletions export_invoice_edi_auchan/schema/invoice_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def get_values(self):
10,
self.source_orders and self.source_orders[0].date_order or "",
), # Date de commande JJ/MM/AAAA
(5, ""), # Heure de commande HH:MN opt
(10, ""), # date du message opt
(5, ""), # Heure du message opt
(5, "", False), # Heure de commande HH:MN opt
(10, "", False), # date du message opt
(5, "", False), # Heure du message opt
(
10,
self.bl_date,
Expand All @@ -25,10 +25,10 @@ def get_values(self):
35,
self.bl_nbr,
), # num du BL JJ/MM/AAAA
(10, ""), # Date avis d'expédition JJ/MM/AAAA opt
(35, ""), # Numéro de l'avis d'expédition opt
(10, ""), # Date d'enlèvement JJ/MM/AAAA opt
(5, ""), # Heure d'enlèvement HH:MN opt
(10, "", False), # Date avis d'expédition JJ/MM/AAAA opt
(35, "", False), # Numéro de l'avis d'expédition opt
(10, "", False), # Date d'enlèvement JJ/MM/AAAA opt
(5, "", False), # Heure d'enlèvement HH:MN opt
(35, self.invoice.name), # Numéro de document
(
16,
Expand All @@ -44,17 +44,22 @@ def get_values(self):
), # Type de document (Facture/Avoir)
# depend on 'move_type', 'in', ('out_invoice', 'out_refund')
(3, self.invoice.currency_id.name), # Code monnaie (EUR pour Euro)
(10, ""), # Date d'échéance pour l'escompte JJ/MM/AAAA opt
(10, "", False), # Date d'échéance pour l'escompte JJ/MM/AAAA opt
(
10,
"",
False,
), # Montant de l'escompte (le pourcentage de l'escompte est préconisé) opt
(35, ""), # Numéro de facture en référence (obligatoire si avoir) opt
(10, ""), # Date de facture en référence (obligatoire si avoir) opt
(6, ""), # Pourcentage de l'escompte opt
(3, ""), # Nb de jour de l'escompte opt
(6, ""), # Pourcentage de pénalité opt
(3, ""), # Nb de jour de pénalité opt
(
35,
"",
False,
), # Numéro de facture en référence (obligatoire si avoir) opt
(10, "", False), # Date de facture en référence (obligatoire si avoir) opt
(6, "", False), # Pourcentage de l'escompte opt
(3, "", False), # Nb de jour de l'escompte opt
(6, "", False), # Pourcentage de pénalité opt
(3, "", False), # Nb de jour de pénalité opt
(
1,
self.invoice.env.context.get("test_mode") and "1" or "0",
Expand Down
1 change: 0 additions & 1 deletion export_invoice_edi_auchan/schema/invoice_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class LIGSegment(SegmentInterface):
def get_values(self):

uom = (
self.line.product_uom_id.name == "kg"
and "KGM"
Expand Down
1 change: 0 additions & 1 deletion export_invoice_edi_auchan/schema/invoice_taxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class TVASegment(SegmentInterface):
def get_values(self):

return [
(3, "TVA"), # Étiquette de segment "TVA"
(5, self.tax_line.tax_line_id.amount or 0.0),
Expand Down
14 changes: 7 additions & 7 deletions export_invoice_edi_auchan/schema/partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,25 @@

class PARSegment(SegmentInterface):
def get_values(self):

return [
(3, "PAR"),
(13, self.invoice.partner_id.barcode), # Code EAN client
(35, self.invoice.partner_id.name), # Libellé client
(35, self.invoice.partner_id.name, False), # Libellé client
(
13,
self.invoice.company_id.partner_id.barcode,
), # Code EAN Fournisseur (vendeur)
(
35,
self.invoice.company_id.partner_id.name,
False,
), # Libellé Fournisseur (vendeur)
(13, self.invoice.partner_shipping_id.barcode), # Code EAN client livré
(35, self.invoice.partner_shipping_id.name), # Libellé client livré
(35, self.invoice.partner_shipping_id.name, False), # Libellé client livré
(13, self.invoice.partner_id.barcode), # Code EAN client facturé à
(35, self.invoice.partner_id.name), # Libellé client facturé à
(10,), # Code EAN factor (obligatoire si factor)
(10,), # Libellé alias factor (obligatoire si factor)
(35, self.invoice.partner_id.name, False), # Libellé client facturé à
(10, "", False), # Code EAN factor (obligatoire si factor)
(10, "", False), # Libellé alias factor (obligatoire si factor)
(13, self.invoice.company_id.partner_id.barcode), # Code EAN régler à
(35, self.invoice.company_id.partner_id.name), # Libellé régler à
(35, self.invoice.company_id.partner_id.name, False), # Libellé régler à
]

0 comments on commit 7b0ef08

Please sign in to comment.