Commit 6458a236 authored by plb's avatar plb
Browse files

Allow to define sqlachemy class at runtime

Need to define the sqlalchemy according to the dolibarr version.
DolibarrSQLAlchemy embeds now the classes.
parent 50a2ce3e
This diff is collapsed.
......@@ -4,7 +4,7 @@ from __future__ import unicode_literals
import settings
import datetime
from himports.dolibarrAlchemy import *
from himports.dolibarrAlchemy import DolibarrSQLAlchemy
#
......@@ -21,14 +21,19 @@ class HledgerEntry(object):
tva_type = settings.get('TVA_TYPE')
# the sql class need to be defined in the derived classes
def _sql_class(self):
assert(False)
# the sql_class corresponding the hledger class
sql_class = None
sql_class = property(_sql_class)
# Date defining the current accounting year
k_accounting_date = None
def __init__(self, e):
def __init__(self, dolibarr_alchemy, e):
super(HledgerEntry, self).__init__()
self.dolibarr_alchemy = dolibarr_alchemy
self.e = e
self.accounting_date = e
for attr in self.k_accounting_date.split('.'):
......@@ -36,8 +41,8 @@ class HledgerEntry(object):
# Retrieve all entries corresponding to cls.sql_class
@classmethod
def get_entries(cls, session):
return [cls(i) for i in session.query(cls.sql_class).all()]
def get_entries(cls, dolibarr_alchemy):
return [cls(dolibarr_alchemy, i) for i in dolibarr_alchemy.session.query(cls.sql_class).all()]
# get_ledger : Print ledger output. Only defined in derived class
def get_ledger(self):
......@@ -73,8 +78,9 @@ class HledgerEntry(object):
# HledgerJournal: A complete Hledger journal. This is a base class.
#
class HledgerJournal(object):
def __init__(self, session, cls_entry):
self.entries = cls_entry.get_entries(session)
def __init__(self, dolibarr_alchemy, cls_entry):
self.dolibarr_alchemy = dolibarr_alchemy
self.entries = cls_entry.get_entries(dolibarr_alchemy)
# get_entries: return the journal entries
def get_entries(self):
......@@ -104,9 +110,18 @@ class HledgerJournal(object):
# HledgerBankEntry: a bank entry
#
class HledgerBankEntry(HledgerEntry):
sql_class = Bank
k_accounting_date = 'datev'
def _sql_class(self):
return self.dolibarr_alchemy.Bank
# get_entries: return the bank entries ordered by value date
@classmethod
def get_entries(cls, dolibarr_alchemy):
Bank = dolibarr_alchemy.Bank
entries = dolibarr_alchemy.session.query(Bank).order_by(Bank.datev, Bank.num_releve).all()
return [cls(dolibarr_alchemy, e) for e in entries]
# get_third_code: retrieve the third code corresponding to the entry. It could be
# a supplier payment
# a tax payment
......@@ -205,7 +220,7 @@ class HledgerBankEntry(HledgerEntry):
if e.url_payment_supplier:
for f in e.url_payment_supplier.payment_supplier.factures:
tvas = HledgerSupplierEntry.get_tva_payment_amounts(f.facture, journal="bank")
tvas = HledgerSupplierEntry.get_tva_payment_amounts(self.dolibarr_alchemy, f.facture, journal="bank")
for k in tvas:
s += " %(account_tva)s \t %(amount_tva)s\n" % {
'account_tva': settings.get_ledger_account(k),
......@@ -213,7 +228,7 @@ class HledgerBankEntry(HledgerEntry):
}
elif e.url_payment:
for f in e.url_payment.payment.factures:
tvas = HledgerSellEntry.get_tva_payment_amounts(f.facture, journal="bank")
tvas = HledgerSellEntry.get_tva_payment_amounts(self.dolibarr_alchemy, f.facture, journal="bank")
for k in tvas:
s += " %(account_tva)s \t %(amount_tva)s\n" % {
'account_tva': settings.get_ledger_account(k),
......@@ -224,22 +239,12 @@ class HledgerBankEntry(HledgerEntry):
return s
# get_entries: return the bank entries ordered by value date
@classmethod
def get_entries(cls, session):
return [cls(e) for e in session.query(cls.sql_class).order_by(Bank.datev, Bank.num_releve).all()]
#
# HledgerBillingEntry: An entry corresponding to a bill (Supplier or Client)
#
class HledgerBillingEntry(HledgerEntry):
# get_entries: return the bill entries ordered by billing date
@classmethod
def get_entries(cls, session):
return [cls(e) for e in session.query(cls.sql_class).order_by(cls.sql_class.datef).all()]
# is_tva_facture: return if the value added tax must be processed on the billing date
@classmethod
def is_tva_facture(cls, ed):
......@@ -252,13 +257,13 @@ class HledgerBillingEntry(HledgerEntry):
# get_tva_amounts: return the amount of value-added taxes.
@classmethod
def get_tva_amounts(cls, e, journal):
def get_tva_amounts(cls, dolibarr_alchemy, e, journal):
tvas = dict()
for ed in e.details:
if isinstance(e, Facture):
if isinstance(e, dolibarr_alchemy.Facture):
total_tva = -ed.total_tva
elif isinstance(e, FactureFourn):
elif isinstance(e, dolibarr_alchemy.FactureFourn):
total_tva = ed.tva
else:
raise Exception("Should be either Facture or FactureFourn")
......@@ -302,22 +307,31 @@ class HledgerBillingEntry(HledgerEntry):
# get_tva_billing_amounts: return the value-added tax amount to collect
@classmethod
def get_tva_billing_amounts(cls, e, journal):
return cls.get_tva_amounts(e, journal)
def get_tva_billing_amounts(cls, dolibarr_alchemy, e, journal):
return cls.get_tva_amounts(dolibarr_alchemy, e, journal)
# get_tva_payment_amounts: return value-added tax amount to deduce
@classmethod
def get_tva_payment_amounts(cls, e, journal):
return cls.get_tva_amounts(e, journal)
def get_tva_payment_amounts(cls, dolibarr_alchemy, e, journal):
return cls.get_tva_amounts(dolibarr_alchemy, e, journal)
#
# HledgerSupplierEntry: Billing entry corresponding to a supplier
#
class HledgerSupplierEntry(HledgerBillingEntry):
sql_class = FactureFourn
k_accounting_date = 'datef'
def _sql_class(self):
return self.dolibarr_alchemy.FactureFourn
# get_entries: return the bill entries ordered by billing date
@classmethod
def get_entries(cls, dolibarr_alchemy):
FactureFourn = dolibarr_alchemy.FactureFourn
return [cls(dolibarr_alchemy, e) for e in dolibarr_alchemy.session.query(FactureFourn).order_by(FactureFourn.datef).all()]
# check: check if the entry is coherent
def check(self):
e = self.e
......@@ -340,9 +354,10 @@ class HledgerSupplierEntry(HledgerBillingEntry):
# getMissingPC: retrieve missing accounts
def getMissingPC(self):
e = self.e
pc_missing = []
if e.societe.code_compta_fournisseur == "":
pc_missing.append("tiers:fournisseur: %s %s" % (e.societe.nom, s.societe.ape))
pc_missing.append("tiers:fournisseur: %s %s" % (e.societe.nom, e.societe.ape))
for ed in e.details:
if self.get_product_account_code(ed) == self.pc_default_expense:
......@@ -374,7 +389,7 @@ class HledgerSupplierEntry(HledgerBillingEntry):
'amount_ht': self._value(-ed.total_ht)
}
tvas = self.get_tva_billing_amounts(self.e, journal="supplier")
tvas = self.get_tva_billing_amounts(self.dolibarr_alchemy, self.e, journal="supplier")
for k in tvas:
s += " %(compte_tva)s \t %(amount_tva)s\n" % {
'compte_tva': settings.get_ledger_account(k),
......@@ -417,13 +432,22 @@ class HledgerSupplierEntry(HledgerBillingEntry):
s_code = cls.pc_default_supplier
return s_code
#
# HledgerSellEntry: The billing entry corresponding to a client
#
class HledgerSellEntry(HledgerBillingEntry):
sql_class = Facture
k_accounting_date = 'datef'
def _sql_class(self):
return self.dolibarr_alchemy.Facture
# get_entries: return the bill entries ordered by billing date
@classmethod
def get_entries(cls, dolibarr_alchemy):
Facture = dolibarr_alchemy.Facture
return [cls(dolibarr_alchemy, e) for e in dolibarr_alchemy.session.query(Facture).order_by(Facture.datef).all()]
# get_ledger: return the ledger corresping to this entry
def get_ledger(self):
e = self.e
......@@ -453,7 +477,7 @@ class HledgerSellEntry(HledgerBillingEntry):
}
# lignes pour la tva
tvas = self.get_tva_billing_amounts(self.e, journal="sell")
tvas = self.get_tva_billing_amounts(self.dolibarr_alchemy, self.e, journal="sell")
for k in tvas:
s += " %(compte_tva)s %(amount_tva)s\n" % {
'compte_tva': settings.get_ledger_account(k),
......@@ -474,7 +498,7 @@ class HledgerSellEntry(HledgerBillingEntry):
e = self.e
pc_missing = []
if e.societe.code_compta == "":
pc_missing.append("tiers: %s %s" % (e.societe.nom, s.societe.ape))
pc_missing.append("tiers: %s %s" % (e.societe.nom, e.societe.ape))
for ed in e.details:
if self.get_product_account_code(ed) == self.pc_default_income:
......@@ -530,13 +554,17 @@ class HledgerSellEntry(HledgerBillingEntry):
# HledgerSocialEntry: A ledger entry corresponding to a tax
#
class HledgerSocialEntry(HledgerEntry):
sql_class = CotisationsSociales
k_accounting_date = 'date_ech'
def _sql_class(self):
return self.dolibarr_alchemy.CotisationsSociales
# get_entries: retrieve all the entries for this entry type
@classmethod
def get_entries(cls, session):
return [cls(e) for e in session.query(cls.sql_class).order_by(CotisationsSociales.date_ech).all()]
def get_entries(cls, dolibarr_alchemy):
CotisationsSociales = dolibarr_alchemy.CotisationsSociales
entries = dolibarr_alchemy.session.query(CotisationsSociales).order_by(CotisationsSociales.date_ech).all()
return [cls(dolibarr_alchemy, e) for e in entries]
# get_third_code: return the third accounting code for this entry
@classmethod
......@@ -560,6 +588,7 @@ class HledgerSocialEntry(HledgerEntry):
# getMissingPC: return the missing accounting code for this entry
def getMissingPC(self):
e = self.e
pc_missing = []
if self.get_social_code(self.e) == self.pc_default_expense:
pc_missing.append("expenses: %s" % (e.libelle))
......@@ -605,13 +634,13 @@ class HledgerSocialEntry(HledgerEntry):
#
class HledgerDolibarrSQLAlchemy(DolibarrSQLAlchemy):
def get_bank_journal(self):
return HledgerJournal(self.session, HledgerBankEntry)
return HledgerJournal(self, HledgerBankEntry)
def get_supplier_journal(self):
return HledgerJournal(self.session, HledgerSupplierEntry)
return HledgerJournal(self, HledgerSupplierEntry)
def get_sell_journal(self):
return HledgerJournal(self.session, HledgerSellEntry)
return HledgerJournal(self, HledgerSellEntry)
def get_social_journal(self):
return HledgerJournal(self.session, HledgerSocialEntry)
return HledgerJournal(self, HledgerSocialEntry)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment