[ADD]import_chart_of_accounts

This commit is contained in:
2024-06-24 13:56:24 +02:00
parent 851e71e5a6
commit b9ddf46922
8 changed files with 392 additions and 0 deletions

View File

@@ -0,0 +1 @@
from . import import_coa_wizard

View File

@@ -0,0 +1,50 @@
from odoo import models, fields, api, exceptions, _
import csv
import base64
from io import StringIO
from odoo.exceptions import UserError
class ImportCoaWizard(models.TransientModel):
_name = 'import.coa.wizard'
coa_file = fields.Binary(string='CSV File', required=True)
filename = fields.Char(string='Filename')
def import_plan_comptable(self):
self.ensure_one()
if not self.coa_file:
raise UserError("Please upload a file.")
if self.filename and not self.filename.lower().endswith('.csv'):
raise UserError("Import COA Wizard only supports CSV")
file_content = base64.b64decode(self.coa_file).decode('utf-8')
csv_file = StringIO(file_content)
csv_reader = csv.DictReader(csv_file)
required_columns = ['code', 'name']
if not all(column in csv_reader.fieldnames for column in required_columns):
raise UserError(f"The CSV file must contain the following columns: {', '.join(required_columns)}")
data_to_import = [row for row in csv_reader]
for record_data in data_to_import:
# the account is already existing in Odoo if the 6 first digits are identicales
existing_line = self.find_account_with_same_firsts_digits(record_data['code'][:6])
if existing_line:
existing_line.write(record_data)
else:
# the closest account already existing in Odoo has the first tree digits identicales
closest_account = self.find_account_with_same_firsts_digits(record_data['code'][:3])
if closest_account :
record_data['account_type'] = closest_account.account_type
record_data['reconcile'] = closest_account.reconcile
new_account = self.env['account.account'].create(record_data)
return {
"type": "ir.actions.act_window",
"res_model": "account.account",
"view_mode": "list"
}
def find_account_with_same_firsts_digits(self, code_prefix):
return self.env['account.account'].search([('code', '=like', f'{code_prefix}%')],limit=1)

View File

@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="import_coa_wizard_view" model="ir.ui.view">
<field name="name">import.coa.wizard.view</field>
<field name="model">import.coa.wizard</field>
<field name="arch" type="xml">
<form string="Accounts chart CSV Import Wizard">
<group name="help-import" colspan="1" col="1">
<div colspan="2">
<p>Pour importer le plan comptable :
<ul>
<li>Le fichier téléversé doit être au <strong>format CSV</strong></li>
<li>Il ne doit comporter que <strong>deux colonnes</strong></li>
<li>La première colonne doit s'appeler <strong>code</strong> pour le numéro de compte comptable</li>
<li>La deuxième colonne doit s'appeler <strong>name</strong> pour le nom du compte comptable</li>
</ul>
</p>
<br/>
<p>Pendant l'import, chaque ligne est comparée aux comptes comptables présents dans Odoo
<ul>
<li>Si le compte comptable existe déjà dans bdd, seul le nom du compte comptable est mis à jour</li>
<li>Un compte comptable est déjà existant si les 6 premiers chiffres du code comptable sont identique (par ex: le compte 120000 et 12000000 sont les mêmes comptes)</li>
<li>Si le compte comptable n'existe pas dans bdd, il est créé. Puis un compte similaire est recherché dans la bdd,
<br/>le compte comptable nouvellement créé aura le même type et la même autorisation de lettrage.</li>
</ul>
</p>
<br/>
<p><strong>Exemple :</strong>
<ul>
<li>J'importe le compte comptable <strong>607730 Epicerie divers</strong></li>
<li>Ce compte comptable n'existe pas déjà dans Odoo, il est créé.</li>
<li>Pour le configurer automatiquement, on se fonde sur le compte le plus proche dans Odoo</li>
<li>Ici il s'agit de <strong>607000 Achats de marchandise</strong>:<br/>
Il a pour <strong>Type</strong> Charges car c'est une compte de charges et <strong>Autoriser le lettrage</strong> est à False.<br/>
Le compte 607730 Epicerie divers sera donc enregistré dans Odoo avec le même paramétrage,<br/>
c'est-à-dire avec un type Charge et une non autorisation du lettrage</li>
</ul>
</p>
</div>
</group>
<group name="import">
<field name="coa_file" filename="filename" />
<field name="filename" invisible="1"/>
</group>
<footer>
<button name="import_plan_comptable" type="object"
class="btn-primary" string="Import"/>
<button special="cancel" string="Cancel" class="btn-default"/>
</footer>
</form>
</field>
</record>
<record id="action_import_coa_wizard" model="ir.actions.act_window">
<field name="name">Upload Chart Of Accounts</field>
<field name="res_model">import.coa.wizard</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
<menuitem
id="import_coa_menu"
action="action_import_coa_wizard"
sequence="1"
parent="account.account_account_menu"
/>
</odoo>