Skip to content

Commit

Permalink
Merge PR #156 into 15.0
Browse files Browse the repository at this point in the history
Signed-off-by tv-openbig
  • Loading branch information
OCA-git-bot committed Apr 10, 2024
2 parents 059d610 + 316c6ce commit 2a977da
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 8 deletions.
1 change: 1 addition & 0 deletions datev_export_xml/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class AccountMove(models.Model):
help="When finishing a datev export the processed invoices are marked as exported.\n"
"If you need to export the invoices again, set this field to False.",
)
datev_validation = fields.Text()

def datev_format_total(self, value, prec=2):
self.ensure_one()
Expand Down
45 changes: 42 additions & 3 deletions datev_export_xml/models/datev_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ def name_get(self):
compute="_compute_datev_filesize",
)

problematic_invoices_count = fields.Integer(
compute="_compute_problematic_invoices_count"
)
invoice_ids = fields.Many2many(comodel_name="account.move", string="Invoices")
invoices_count = fields.Integer(compute="_compute_invoices_count", store=True)

Expand All @@ -147,6 +150,13 @@ def _compute_datev_filesize(self):
for r in self.with_context(bin_size=True):
r.datev_filesize = r.datev_file

@api.depends("invoice_ids")
def _compute_problematic_invoices_count(self):
for r in self:
r.problematic_invoices_count = len(
r.invoice_ids.filtered("datev_validation")
)

@api.depends("invoice_ids")
def _compute_invoices_count(self):
for r in self:
Expand Down Expand Up @@ -247,6 +257,8 @@ def get_zip(self):
self.write({"exception_info": msg, "state": "failed"})
_logger.exception(e)

self._compute_problematic_invoices_count()

@api.model
def cron_run_pending_export(self):
"""
Expand Down Expand Up @@ -342,6 +354,22 @@ def _create_activity(self):
}
)

def action_validate(self):
generator = self.env["datev.xml.generator"]
for invoice in self.invoice_ids:
try:
generator._check_invoices(invoice)
except ValueError as e:
invoice.datev_validation = str(e)
continue

try:
generator.generate_xml_invoice(invoice)
except UserError:
continue

self._compute_problematic_invoices_count()

def action_done(self):
self.filtered(lambda r: r.state in ["running", "failed"]).write(
{
Expand Down Expand Up @@ -382,10 +410,21 @@ def action_draft(self):
)
r.write({"state": "draft"})

def action_show_invalid_invoices_view(self):
tree_view = self.env.ref("datev_export_xml.view_move_datev_validation")
return {
"type": "ir.actions.act_window",
"view_mode": "tree,form",
"views": [[tree_view.id, "tree"], [False, "form"]],
"res_model": "account.move",
"target": "current",
"name": _("Problematic Invoices"),
"domain": [("id", "in", self.invoice_ids.filtered("datev_validation").ids)],
}

def action_show_related_invoices_view(self):
return {
"type": "ir.actions.act_window",
"view_type": "form",
"view_mode": "tree,kanban,form",
"res_model": "account.move",
"target": "current",
Expand All @@ -400,7 +439,7 @@ def unlink(self):
return res

def write(self, vals):
super().write(vals)
res = super().write(vals)
if any(
r in vals
for r in [
Expand All @@ -417,7 +456,7 @@ def write(self, vals):
super(DatevExport, r).write(
{"invoice_ids": [(6, 0, r.get_invoices().ids)]}
)
return True
return res

@api.model
def create(self, vals):
Expand Down
10 changes: 9 additions & 1 deletion datev_export_xml/models/datev_xml_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ class DatevXmlGenerator(models.AbstractModel):
_description = "DATEV XML Generator"

@api.model
def check_xml_file(self, doc_name, root, xsd=None):
def check_xml_file(self, doc_name, root, xsd=None, invoice=None):
if not xsd:
xsd = "Document_v050.xsd"

schema = tools.file_open(f"datev_export_xml/xsd_files/{xsd}")
try:
if invoice:
invoice.datev_validation = False

schema = etree.XMLSchema(etree.parse(schema))
schema.assertValid(root)
except (etree.DocumentInvalid, etree.XMLSyntaxError) as e:
_logger.warning(etree.tostring(root))

if invoice:
invoice.datev_validation = str(e)

raise UserError(
_(
"Wrong Data in XML file!\nTry to solve the problem with "
Expand Down Expand Up @@ -92,6 +99,7 @@ def generate_xml_invoice(self, invoice, check_xsd=True):
doc_name,
root,
"Belegverwaltung_online_invoice_v050.xsd",
invoice=invoice,
)

return doc_name, etree.tostring(root)
12 changes: 12 additions & 0 deletions datev_export_xml/views/account_invoice_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
</field>
</record>

<record id="view_move_datev_validation" model="ir.ui.view">
<field name="model">account.move</field>
<field name="mode">primary</field>
<field name="priority">800</field>
<field name="arch" type="xml">
<tree>
<field name="name" decoration-bf="1" />
<field name="datev_validation" />
</tree>
</field>
</record>

<record id="view_account_invoice_filter" model="ir.ui.view">
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_account_invoice_filter" />
Expand Down
20 changes: 20 additions & 0 deletions datev_export_xml/views/datev_export_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
attrs="{'invisible': ['|', ('state', 'not in', ['done', 'pending', 'failed']), ('manually_document_selection', '=', True)]}"
confirm="Do you really want to set the export to draft?"
/>
<button
name="action_validate"
type="object"
string="Validate"
attrs="{'invisible': [('state', '=', 'done')]}"
/>
<button
name="action_pending"
type="object"
Expand Down Expand Up @@ -147,6 +153,20 @@
<span class="o_stat_text">Invoices</span>
</div>
</button>
<button
class="oe_stat_button oe_read_only"
name="action_show_invalid_invoices_view"
icon="fa-exclamation"
type="object"
attrs="{'invisible': [('problematic_invoices_count', '=', 0)]}"
>
<div class="o_form_field o_stat_info">
<span class="o_stat_value">
<field name="problematic_invoices_count" />
</span>
<span class="o_stat_text">Problems</span>
</div>
</button>
</div>
<field name="manually_document_selection" invisible="1" />
<group>
Expand Down
28 changes: 24 additions & 4 deletions datev_export_xml/views/templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<template id="export_party">
<address
t-att-name="(partner.display_name or '')[:50]"
t-att-street="(partner.street or '')[:40]"
t-att-street="' '.join([partner.street or '', partner.street2 or ''])[:40]"
t-att-zip="partner.zip or ''"
t-att-city="partner.city or ''"
t-att-country="partner.country_id.code or ''"
Expand All @@ -24,11 +24,17 @@

<template id="export_invoice_line_item">
<price_line_amount
t-if="prices['total_included']"
t-att-currency="line.currency_id.name"
t-att-gross_price_line_amount="doc.datev_format_total(prices['total_included'])"
t-att-net_price_line_amount="doc.datev_format_total(prices['total_excluded'])"
t-att-tax="doc.datev_format_total(line.tax_ids.amount)"
/>
<price_line_amount
t-else=""
t-att-currency="line.currency_id.name"
t-att-tax="doc.datev_format_total(line.tax_ids.amount)"
/>

<accounting_info
t-att-account_no="line.account_id.code or ''"
Expand All @@ -42,7 +48,7 @@
<t t-set="prices" t-value="line.datev_price_information()" />

<invoice_item_list
t-if="line.product_id"
t-if="line.product_id and prices['total_excluded']"
t-call="datev_export_xml.export_invoice_line_item"
t-att-product_id="line.product_id.default_code"
t-att-description_short="(line.name or line.product_id.name)[:40]"
Expand All @@ -51,12 +57,26 @@
t-att-quantity="'%.02f' % line.quantity"
/>
<invoice_item_list
t-elif="not line.display_type"
t-if="line.product_id"
t-call="datev_export_xml.export_invoice_line_item"
t-att-description_short="line.name or ''"
t-att-product_id="line.product_id.default_code"
t-att-description_short="(line.name or line.product_id.name)[:40]"
t-att-order_unit="line.product_uom_id.name"
t-att-quantity="'%.02f' % line.quantity"
/>
<invoice_item_list
t-elif="not line.display_type and prices['total_excluded']"
t-call="datev_export_xml.export_invoice_line_item"
t-att-description_short="(line.name or '')[:40]"
t-att-net_product_price="doc.datev_format_total(prices['total_excluded'], 3)"
t-att-quantity="'%.02f' % line.quantity"
/>
<invoice_item_list
t-elif="not line.display_type"
t-call="datev_export_xml.export_invoice_line_item"
t-att-description_short="(line.name or '')[:40]"
t-att-quantity="'%.02f' % line.quantity"
/>
</template>

<template id="export_invoice">
Expand Down

0 comments on commit 2a977da

Please sign in to comment.