Sélectionner une révision Git
acc_repartition_keys_wizard.py 4,54 Kio
# Copyright 2021- Le Filament (https://le-filament.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)
import base64
from datetime import datetime
from odoo import _, fields, models
from odoo.exceptions import UserError, ValidationError
from ..tools.key_file import RepartitionKeyEntryFile
class AccRepartitionKeysWizard(models.TransientModel):
_name = "acc.repartition.keys.wizard"
_description = "clés de répartition"
# ------------------------------------------------------
# Default methods
# ------------------------------------------------------
def _default_operation_id(self):
return self.env.context.get("active_id")
# ------------------------------------------------------
# Fields declaration
# ------------------------------------------------------
csv_file = fields.Binary("Contenu du fichier CSV")
filename = fields.Char("Nom du fichier")
date_send = fields.Date(default=None)
operation_id = fields.Many2one(
"acc.operation", "Opération", default=_default_operation_id
)
# ------------------------------------------------------
# SQL Constraints
# ------------------------------------------------------
# ------------------------------------------------------
# Computed fields / Search Fields
# ------------------------------------------------------
# ------------------------------------------------------
# Onchange / Constraints
# ------------------------------------------------------
# ------------------------------------------------------
# CRUD methods (ORM overrides)
# ------------------------------------------------------
def ignore(self):
return {"type": "ir.actions.act_window_close"}
# ------------------------------------------------------
# Actions
# ------------------------------------------------------
def send_imported_file(self):
"""
testing a file, check that all prm id exist, are in operation and all
operation prm are in file
:return:
"""
if self.csv_file:
try:
file = (
base64.b64decode(self.csv_file)
.decode("utf-8")
.replace("\r", "")
.split("\n")
)
except UnicodeDecodeError as exc:
raise UserError(_("Fichier de répartitions au mauvais format")) from exc
try:
entry_file_handler = RepartitionKeyEntryFile(data=file)
except (ValueError, IndexError) as exc:
raise UserError(_("Fichier de répartitions au mauvais format")) from exc
entry_file_handler.operation_counter_list = self.env[
"acc.counter.period"
]._get_periods_from_interval(
[("acc_operation_id", "=", self.operation_id.id), ("prm_type", "=", "delivery")],
datetime.strptime(
entry_file_handler.get_min_date(), "%d-%m-%Y %H:%M"
).date(),
datetime.strptime(
entry_file_handler.get_max_date(), "%d-%m-%Y %H:%M"
).date(),
).mapped("acc_counter_id.name")
try:
file_check_result = entry_file_handler.check()
except (ValueError, IndexError) as exc:
raise UserError(_("Fichier de répartitions au mauvais format")) from exc
if not file_check_result.get("check"):
raise UserError(_(file_check_result.get("message")))
data_to_send = entry_file_handler.data_to_send(send_empty_key=False)
for key in data_to_send:
job_description = f"Send repartition key at {key.get('timestamp')}"
try:
self.operation_id.with_delay(
description=job_description
).send_repartition_key(key=key)
except ValidationError as exc:
raise UserError(_(str(exc))) from exc
self.env["acc.repartition.keys"].create(
{
"csv_file": self.csv_file,
"filename": self.filename,
"date_send": fields.Date.context_today(self),
"operation_id": self.operation_id.id,
}
)
else:
raise UserError(_("Fichier de répartition non chargé"))
# ------------------------------------------------------
# Business methods
# ------------------------------------------------------