|
@@ -15,6 +15,8 @@ class HledgerEntry(object):
|
|
|
pc_default_charge = settings.get('PC_REFS')['default_charge']
|
|
|
pc_default_bank = settings.get('PC_REFS')['default_bank']
|
|
|
|
|
|
+ tva_type = settings.get('TVA_TYPE')
|
|
|
+
|
|
|
sql_class = None
|
|
|
|
|
|
# Date permettant de déterminer dans quel exercice comptable
|
|
@@ -82,17 +84,18 @@ class HledgerJournal(object):
|
|
|
class HledgerBankEntry(HledgerEntry):
|
|
|
sql_class = Bank
|
|
|
k_accounting_date = 'datev'
|
|
|
-
|
|
|
- def get_third_code(self):
|
|
|
- e = self.e
|
|
|
-
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_third_code(cls, e):
|
|
|
third_code = ""
|
|
|
if e.url_payment_supplier:
|
|
|
if e.url_company:
|
|
|
third_code = e.url_company.societe.code_compta_fournisseur
|
|
|
|
|
|
if e.url_payment_sc:
|
|
|
- third_code = e.url_payment_sc.payment_sc.cotisation_sociale.type.accountancy_code
|
|
|
+ code = e.url_payment_sc.payment_sc.cotisation_sociale.type.code
|
|
|
+ if code in settings.get('SOCIAL_REFS'):
|
|
|
+ third_code = settings.get('SOCIAL_REFS')[code]
|
|
|
|
|
|
if e.url_payment:
|
|
|
if e.url_company:
|
|
@@ -100,12 +103,15 @@ class HledgerBankEntry(HledgerEntry):
|
|
|
|
|
|
if third_code == "":
|
|
|
fn = settings.get('PC_REFS')['fn_custom_code']
|
|
|
- third_code = fn(self.e)
|
|
|
+ third_code = fn(e)
|
|
|
+
|
|
|
+ if third_code == "":
|
|
|
+ third_code = cls.pc_default_tiers
|
|
|
|
|
|
return third_code
|
|
|
|
|
|
- def get_description(self):
|
|
|
- e = self.e
|
|
|
+ @classmethod
|
|
|
+ def get_description(self, e):
|
|
|
s_nom = ""
|
|
|
s_description = ""
|
|
|
|
|
@@ -134,21 +140,14 @@ class HledgerBankEntry(HledgerEntry):
|
|
|
e = self.e
|
|
|
s = ""
|
|
|
|
|
|
- s_description = self.get_description()
|
|
|
+ s_description = self.get_description(self.e)
|
|
|
|
|
|
s += "%(date)s %(description)s\n" % {
|
|
|
'date': e.datev.strftime("%Y/%m/%d"),
|
|
|
'description': s_description
|
|
|
}
|
|
|
|
|
|
- third_code = self.get_third_code()
|
|
|
- if third_code == "":
|
|
|
- third_code = self.pc_default_tiers
|
|
|
-
|
|
|
- s += " %(account)s \t %(amount)s\n" % {
|
|
|
- 'account': settings.get_ledger_account(third_code),
|
|
|
- 'amount': e.amount
|
|
|
- }
|
|
|
+ third_code = self.get_third_code(self.e)
|
|
|
|
|
|
ba_code = e.account.account_number
|
|
|
if ba_code == "":
|
|
@@ -158,20 +157,108 @@ class HledgerBankEntry(HledgerEntry):
|
|
|
'account': settings.get_ledger_account(ba_code),
|
|
|
'amount': -e.amount
|
|
|
}
|
|
|
+ s += " %(account)s \t %(amount)s\n" % {
|
|
|
+ 'account': settings.get_ledger_account(third_code),
|
|
|
+ 'amount': e.amount
|
|
|
+ }
|
|
|
+ if e.url_payment_supplier:
|
|
|
+ for f in e.url_payment_supplier.payment_supplier.factures:
|
|
|
+ tvas = HledgerSupplierEntry.get_tva_paiement_amounts(f.facture, journal="bank")
|
|
|
+ for k in tvas:
|
|
|
+ s += " %(account_tva)s \t %(amount_tva)s\n" % {
|
|
|
+ 'account_tva': settings.get_ledger_account(k),
|
|
|
+ 'amount_tva': tvas[k]
|
|
|
+ }
|
|
|
+ elif e.url_payment:
|
|
|
+ for f in e.url_payment.payment.factures:
|
|
|
+ tvas = HledgerSellEntry.get_tva_paiement_amounts(f.facture, journal="bank")
|
|
|
+ for k in tvas:
|
|
|
+ s += " %(account_tva)s \t %(amount_tva)s\n" % {
|
|
|
+ 'account_tva': settings.get_ledger_account(k),
|
|
|
+ 'amount_tva': tvas[k]
|
|
|
+ }
|
|
|
+ else:
|
|
|
+ pass
|
|
|
+
|
|
|
return s
|
|
|
|
|
|
@classmethod
|
|
|
def get_entries(cls, session):
|
|
|
- return [cls(e) for e in session.query(cls.sql_class).order_by(Bank.datev, Bank.num_releve).filter(Bank.num_releve != "").all()]
|
|
|
+ return [cls(e) for e in session.query(cls.sql_class).order_by(Bank.datev, Bank.num_releve).all()]
|
|
|
|
|
|
|
|
|
-class HledgerSupplierEntry(HledgerEntry):
|
|
|
- sql_class = FactureFourn
|
|
|
- k_accounting_date = 'datef'
|
|
|
+class HledgerFactureEntry(HledgerEntry):
|
|
|
|
|
|
@classmethod
|
|
|
def get_entries(cls, session):
|
|
|
- return [cls(e) for e in session.query(cls.sql_class).order_by(FactureFourn.datef).all()]
|
|
|
+ return [cls(e) for e in session.query(cls.sql_class).order_by(cls.sql_class.datef).all()]
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def is_tva_facture(cls, ed):
|
|
|
+ return ed.productcls.tva_type == 'service_sur_debit' and ed.product_type == 1
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def is_tva_paiement(cls, ed):
|
|
|
+ return cls.tva_type != 'service_sur_debit' or ed.product_type != 1
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_tva_amounts(cls, e, journal):
|
|
|
+
|
|
|
+ tvas = dict()
|
|
|
+ for ed in e.details:
|
|
|
+ if isinstance(e, Facture):
|
|
|
+ total_tva = -ed.total_tva
|
|
|
+ elif isinstance(e, FactureFourn):
|
|
|
+ total_tva = ed.tva
|
|
|
+ else:
|
|
|
+ raise Exception("Should be either Facture or FactureFourn")
|
|
|
+
|
|
|
+ if total_tva == 0:
|
|
|
+ continue
|
|
|
+
|
|
|
+ tva_account = cls.get_tva_account(ed)
|
|
|
+ tva_regul = cls.get_tva_regul_account(ed)
|
|
|
+
|
|
|
+ if journal == "bank":
|
|
|
+ if ed.product_type == 1 and cls.tva_type == 'standard':
|
|
|
+ if tva_regul not in tvas:
|
|
|
+ tvas[tva_regul] = 0
|
|
|
+ if tva_account not in tvas:
|
|
|
+ tvas[tva_account] = 0
|
|
|
+ tvas[tva_account] += -total_tva
|
|
|
+ tvas[tva_regul] += total_tva
|
|
|
+
|
|
|
+ elif journal == "sell" or journal == "supplier":
|
|
|
+ if ed.product_type == 1 and cls.tva_type == 'standard':
|
|
|
+ if tva_regul not in tvas:
|
|
|
+ tvas[tva_regul] = 0
|
|
|
+ tvas[tva_regul] += -total_tva
|
|
|
+ else:
|
|
|
+ if tva_account not in tvas:
|
|
|
+ tvas[tva_account] = 0
|
|
|
+ tvas[tva_account] += -total_tva
|
|
|
+
|
|
|
+ return tvas
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_tva_regul_account(cls, ed):
|
|
|
+ return settings.get('PC_REFS')['tva_regul']
|
|
|
+
|
|
|
+ # Calcul de la tva à décaisser à paiement de la facture
|
|
|
+ #
|
|
|
+ # Cela du type de produit (bien ou service) et du type de tva
|
|
|
+ @classmethod
|
|
|
+ def get_tva_facture_amounts(cls, e, journal):
|
|
|
+ return cls.get_tva_amounts(e, journal)
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_tva_paiement_amounts(cls, e, journal):
|
|
|
+ return cls.get_tva_amounts(e, journal)
|
|
|
+
|
|
|
+
|
|
|
+class HledgerSupplierEntry(HledgerFactureEntry):
|
|
|
+ sql_class = FactureFourn
|
|
|
+ k_accounting_date = 'datef'
|
|
|
|
|
|
def check(self):
|
|
|
e = self.e
|
|
@@ -213,87 +300,100 @@ class HledgerSupplierEntry(HledgerEntry):
|
|
|
'description': e.ref_supplier + " - " + e.societe.nom,
|
|
|
}
|
|
|
|
|
|
- s_code = self.get_supplier_code()
|
|
|
+ s_code = self.get_supplier_code(self.e)
|
|
|
s += " %(compte_tiers)s %(amount_ttc)s\n" % {
|
|
|
'compte_tiers': settings.get_ledger_account(s_code),
|
|
|
'amount_ttc': e.total_ttc,
|
|
|
}
|
|
|
|
|
|
- if e.total_tva != 0:
|
|
|
- if s_code.startswith("2"):
|
|
|
- tva_account = settings.get('PC_REFS')['tva_deductible']
|
|
|
- else:
|
|
|
- tva_account = settings.get('PC_REFS')['tva_deductible_immo']
|
|
|
- s += " %(compte_tva)s %(amount_tva)s\n" % {
|
|
|
- 'compte_tva': settings.get_ledger_account(tva_account),
|
|
|
- 'amount_tva': -e.total_tva,
|
|
|
- }
|
|
|
-
|
|
|
for ed in e.details:
|
|
|
p_code = self.get_product_account_code(ed)
|
|
|
s += " %(compte_produit)s %(amount_ht)s\n" % {
|
|
|
'compte_produit': settings.get_ledger_account(p_code),
|
|
|
'amount_ht': -ed.total_ht
|
|
|
}
|
|
|
+
|
|
|
+ tvas = self.get_tva_facture_amounts(self.e, journal="supplier")
|
|
|
+ for k in tvas:
|
|
|
+ s += " %(compte_tva)s %(amount_tva)s\n" % {
|
|
|
+ 'compte_tva': settings.get_ledger_account(k),
|
|
|
+ 'amount_tva': tvas[k],
|
|
|
+ }
|
|
|
+
|
|
|
return s
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_tva_account(cls, ed):
|
|
|
+ p_code = cls.get_product_account_code(ed)
|
|
|
+ if p_code.startswith("2"):
|
|
|
+ tva_account = settings.get('PC_REFS')['tva_deductible']
|
|
|
+ else:
|
|
|
+ tva_account = settings.get('PC_REFS')['tva_deductible_immo']
|
|
|
+ return tva_account
|
|
|
|
|
|
- def get_product_account_code(self, ed):
|
|
|
+ @classmethod
|
|
|
+ def get_product_account_code(cls, ed):
|
|
|
p_code = ""
|
|
|
if ed.accounting_account:
|
|
|
p_code = ed.accounting_account.account_number
|
|
|
elif ed.product:
|
|
|
p_code = ed.product.accountancy_code_buy
|
|
|
else:
|
|
|
- p_code = self.pc_default_charge
|
|
|
+ p_code = cls.pc_default_charge
|
|
|
return p_code
|
|
|
-
|
|
|
- def get_supplier_code(self):
|
|
|
- e = self.e
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_supplier_code(cls, e):
|
|
|
s_code = e.societe.code_compta_fournisseur
|
|
|
if s_code == "":
|
|
|
- s_code = self.pc_default_supplier
|
|
|
+ s_code = cls.pc_default_supplier
|
|
|
return s_code
|
|
|
|
|
|
|
|
|
-
|
|
|
-class HledgerSellEntry(HledgerEntry):
|
|
|
+class HledgerSellEntry(HledgerFactureEntry):
|
|
|
sql_class = Facture
|
|
|
k_accounting_date = 'datef'
|
|
|
|
|
|
- @classmethod
|
|
|
- def get_entries(cls, session):
|
|
|
- return [cls(e) for e in session.query(cls.sql_class).order_by(Facture.datef).all()]
|
|
|
-
|
|
|
def get_ledger(self):
|
|
|
e = self.e
|
|
|
self.check()
|
|
|
s = ""
|
|
|
+
|
|
|
+ # Date et description
|
|
|
s += "%(date)s %(description)s\n" % {
|
|
|
'date': e.datef.strftime("%Y/%m/%d"),
|
|
|
'description': e.facnumber + " - " + e.societe.nom,
|
|
|
}
|
|
|
|
|
|
- s_code = self.get_client_code()
|
|
|
+ # ligne pour compte client
|
|
|
+ s_code = self.get_client_code(self.e)
|
|
|
s += " %(compte_tiers)s %(amount_ttc)s\n" % {
|
|
|
'compte_tiers': settings.get_ledger_account(s_code),
|
|
|
'amount_ttc': -e.total_ttc,
|
|
|
|
|
|
}
|
|
|
-
|
|
|
- if float(e.tva) != 0:
|
|
|
- tva_account = settings.get('PC_REFS')['tva_collecte']
|
|
|
- s += " %(compte_tva_collecte)s %(amount_tva)s\n" % {
|
|
|
- 'compte_tva_collecte': settings.get_ledger_account(tva_account),
|
|
|
- 'amount_tva': e.tva,
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
+ # lignes pour compte de produits
|
|
|
for ed in e.details:
|
|
|
p_code = self.get_product_account_code(ed)
|
|
|
s += " %(compte_produit)s %(amount_ht)s\n" % {
|
|
|
'compte_produit': settings.get_ledger_account(p_code),
|
|
|
'amount_ht': ed.total_ht
|
|
|
}
|
|
|
+
|
|
|
+ # lignes pour la tva
|
|
|
+ tvas = self.get_tva_facture_amounts(self.e, journal="sell")
|
|
|
+ for k in tvas:
|
|
|
+ s += " %(compte_tva)s %(amount_tva)s\n" % {
|
|
|
+ 'compte_tva': settings.get_ledger_account(k),
|
|
|
+ 'amount_tva': tvas[k],
|
|
|
+ }
|
|
|
+
|
|
|
return s
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def get_tva_account(cls, ed):
|
|
|
+ return settings.get('PC_REFS')['tva_collecte']
|
|
|
|
|
|
def getMissingPC(self):
|
|
|
e = self.e
|
|
@@ -311,21 +411,22 @@ class HledgerSellEntry(HledgerEntry):
|
|
|
|
|
|
return pc_missing
|
|
|
|
|
|
- def get_product_account_code(self, ed):
|
|
|
+ @classmethod
|
|
|
+ def get_product_account_code(cls, ed):
|
|
|
p_code = ""
|
|
|
if ed.accounting_account:
|
|
|
p_code = ed.accounting_account.account_number
|
|
|
elif ed.product:
|
|
|
p_code = ed.product.accountancy_code_sell
|
|
|
else:
|
|
|
- p_code = self.pc_default_produit
|
|
|
+ p_code = cls.pc_default_produit
|
|
|
return p_code
|
|
|
|
|
|
- def get_client_code(self):
|
|
|
- e = self.e
|
|
|
+ @classmethod
|
|
|
+ def get_client_code(cls, e):
|
|
|
s_code = e.societe.code_compta
|
|
|
if s_code == "":
|
|
|
- s_code = self.pc_default_client
|
|
|
+ s_code = cls.pc_default_client
|
|
|
return s_code
|
|
|
|
|
|
def check(self):
|
|
@@ -355,30 +456,30 @@ class HledgerSocialEntry(HledgerEntry):
|
|
|
def get_entries(cls, session):
|
|
|
return [cls(e) for e in session.query(cls.sql_class).order_by(CotisationsSociales.date_ech).all()]
|
|
|
|
|
|
- def get_third_code(self):
|
|
|
- e = self.e
|
|
|
+ @classmethod
|
|
|
+ def get_third_code(cls, e):
|
|
|
third_code = ""
|
|
|
if e.type.code in settings.get('SOCIAL_REFS'):
|
|
|
third_code = settings.get('SOCIAL_REFS')[e.type.code]
|
|
|
if third_code == "":
|
|
|
- third_code = self.pc_default_supplier
|
|
|
+ third_code = cls.pc_default_supplier
|
|
|
return third_code
|
|
|
|
|
|
- def get_social_code(self):
|
|
|
- e = self.e
|
|
|
+ @classmethod
|
|
|
+ def get_social_code(cls, e):
|
|
|
s_code = ""
|
|
|
if e.type:
|
|
|
s_code = e.type.accountancy_code
|
|
|
if s_code == "":
|
|
|
- s_code = self.pc_default_charge
|
|
|
+ s_code = cls.pc_default_charge
|
|
|
return s_code
|
|
|
|
|
|
def getMissingPC(self):
|
|
|
pc_missing = []
|
|
|
- if self.get_social_code() == self.pc_default_charge:
|
|
|
+ if self.get_social_code(self.e) == self.pc_default_charge:
|
|
|
pc_missing.append("charges: %s" % (e.libelle))
|
|
|
|
|
|
- if self.get_third_code() == self.pc_default_supplier:
|
|
|
+ if self.get_third_code(self.e) == self.pc_default_supplier:
|
|
|
pc_missing.append("tiers: %s (%s)" % (e.libelle, e.type.code))
|
|
|
|
|
|
return pc_missing
|
|
@@ -393,8 +494,8 @@ class HledgerSocialEntry(HledgerEntry):
|
|
|
'description': e.libelle + " - " + e.type.libelle
|
|
|
}
|
|
|
|
|
|
- third_code = self.get_third_code()
|
|
|
- s_code = self.get_social_code()
|
|
|
+ third_code = self.get_third_code(self.e)
|
|
|
+ s_code = self.get_social_code(self.e)
|
|
|
|
|
|
s += " %(account)s \t %(amount)s\n" % {
|
|
|
'account': settings.get_ledger_account(third_code),
|