dolibarrAlchemy.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from sqlalchemy import Column, Integer, ForeignKey, UniqueConstraint
  4. from sqlalchemy.orm import relationship, sessionmaker
  5. from sqlalchemy.ext.declarative import declarative_base, DeferredReflection
  6. from sqlalchemy import create_engine, MetaData
  7. from distutils.version import LooseVersion
  8. class DolibarrSQLAlchemy(object):
  9. def __init__(
  10. self,
  11. mysql_host,
  12. mysql_port,
  13. mysql_database,
  14. mysql_user,
  15. mysql_password,
  16. echo=False):
  17. self.mysql_database = mysql_database
  18. self.mysql_host = mysql_host
  19. self.mysql_password = mysql_password
  20. self.mysql_user = mysql_user
  21. self.mysql_port = mysql_port
  22. self.echo = echo
  23. def connect(self):
  24. engine_str = "mysql://%s:%s@%s:%s/%s" % (
  25. self.mysql_user,
  26. self.mysql_password,
  27. self.mysql_host,
  28. self.mysql_port,
  29. self.mysql_database
  30. )
  31. self.engine = create_engine(engine_str, echo=self.echo, encoding=str("iso8859-1"), convert_unicode=True)
  32. self.prepare()
  33. # create a configured "Session" class
  34. Session = sessionmaker(bind=self.engine)
  35. # create a Session
  36. self.session = Session(autocommit=True)
  37. def prepare(self):
  38. version = self.engine.execute("SELECT value FROM llx_const WHERE name='MAIN_VERSION_LAST_UPGRADE'").fetchone()[0]
  39. self.metadata = MetaData(bind=self.engine, info={'dolibarr_version': version})
  40. Base = declarative_base(bind=self.engine, metadata=self.metadata)
  41. class DolibarrBase(DeferredReflection, Base):
  42. __abstract__ = True
  43. @classmethod
  44. def after40(cls):
  45. return LooseVersion(cls.metadata.info['dolibarr_version']) > LooseVersion("4.0")
  46. class Bank(DolibarrBase):
  47. __tablename__ = "llx_bank"
  48. id = Column("rowid", Integer, primary_key=True)
  49. fk_account = Column('fk_account', Integer, ForeignKey('llx_bank_account.rowid'))
  50. account = relationship("BankAccount", backref="entries", lazy="subquery")
  51. bankclass = relationship(
  52. "BankClass", backref="bank", uselist=False,
  53. lazy="subquery"
  54. )
  55. url_company = relationship(
  56. "BankUrl", uselist=False, backref="bank_company", innerjoin=True,
  57. primaryjoin="and_(Bank.id==BankUrl.fk_bank, " "BankUrl.type=='company')",
  58. lazy="subquery",
  59. )
  60. url_payment = relationship(
  61. "BankUrl", uselist=False, backref="bank_payement", innerjoin=True,
  62. primaryjoin="and_(Bank.id==BankUrl.fk_bank, " "BankUrl.type=='payment')",
  63. lazy="subquery",
  64. )
  65. url_payment_sc = relationship(
  66. "BankUrl", uselist=False, backref="bank_payement_sc", innerjoin=True,
  67. primaryjoin="and_(Bank.id==BankUrl.fk_bank, " "BankUrl.type=='payment_sc')",
  68. lazy="subquery",
  69. )
  70. url_payment_supplier = relationship(
  71. "BankUrl", uselist=False, backref="bank_payement_supplier", innerjoin=True,
  72. primaryjoin="and_(Bank.id==BankUrl.fk_bank, " "BankUrl.type=='payment_supplier')",
  73. lazy="subquery",
  74. )
  75. payment_tva = relationship(
  76. "TVA", backref="bank",
  77. lazy="subquery"
  78. )
  79. class BankAccount(DolibarrBase):
  80. __tablename__ = "llx_bank_account"
  81. id = Column("rowid", Integer, primary_key=True)
  82. class BankCateg(DolibarrBase):
  83. __tablename__ = "llx_bank_categ"
  84. id = Column("rowid", Integer, primary_key=True)
  85. class BankClass(DolibarrBase):
  86. __tablename__ = "llx_bank_class"
  87. id = Column('lineid', Integer, ForeignKey('llx_bank.rowid'), primary_key=True)
  88. UniqueConstraint('lineid', 'fk_categ')
  89. categ_id = Column('fk_categ', Integer, ForeignKey('llx_bank_categ.rowid'))
  90. categ = relationship(
  91. "BankCateg", backref="classes",
  92. lazy="subquery",
  93. )
  94. class BankUrl(DolibarrBase):
  95. __tablename__ = "llx_bank_url"
  96. id = Column("rowid", Integer, primary_key=True)
  97. fk_bank = Column('fk_bank', Integer, ForeignKey('llx_bank.rowid'))
  98. url_id = Column('url_id', Integer)
  99. societe = relationship(
  100. "Societe", backref="bank_urls", uselist=False,
  101. primaryjoin="and_(BankUrl.url_id==Societe.id, " "BankUrl.type=='company')",
  102. foreign_keys=url_id,
  103. lazy="subquery",
  104. )
  105. payment = relationship(
  106. "Paiement", backref="bank_urls", uselist=False,
  107. primaryjoin="and_(BankUrl.url_id==Paiement.id, " "BankUrl.type=='payment')",
  108. foreign_keys=url_id,
  109. lazy="subquery",
  110. order_by="Paiement.datep,Paiement.id",
  111. )
  112. payment_sc = relationship(
  113. "PaiementCharge", backref="bank_urls", uselist=False,
  114. primaryjoin="and_(BankUrl.url_id==PaiementCharge.id, " "BankUrl.type=='payment_sc')",
  115. foreign_keys=url_id,
  116. lazy="subquery",
  117. order_by="PaiementCharge.datep,PaiementCharge.id",
  118. )
  119. payment_supplier = relationship(
  120. "PaiementFourn", backref="bank_urls", uselist=False,
  121. primaryjoin="and_(BankUrl.url_id==PaiementFourn.id, " "BankUrl.type=='payment_supplier')",
  122. foreign_keys=url_id,
  123. lazy="subquery",
  124. order_by="PaiementFourn.datep,PaiementFourn.id",
  125. )
  126. class TVA(DolibarrBase):
  127. __tablename__ = "llx_tva"
  128. id = Column("rowid", Integer, primary_key=True)
  129. fk_bank = Column('fk_bank', Integer, ForeignKey('llx_bank.rowid'))
  130. class CotisationsSociales(DolibarrBase):
  131. __tablename__ = "llx_chargesociales"
  132. id = Column("rowid", Integer, primary_key=True)
  133. fk_type = Column('fk_type', Integer, ForeignKey('llx_c_chargesociales.id'))
  134. type = relationship(
  135. "CCotisationsSociales", backref="cotisations_sociales",
  136. lazy="subquery",
  137. )
  138. class CCotisationsSociales(DolibarrBase):
  139. __tablename__ = "llx_c_chargesociales"
  140. id = Column("id", Integer, primary_key=True)
  141. class Commande(DolibarrBase):
  142. __tablename__ = "llx_commande"
  143. id = Column("rowid", Integer, primary_key=True)
  144. class CommandeDet(DolibarrBase):
  145. __tablename__ = "llx_commandedet"
  146. id = Column("rowid", Integer, primary_key=True)
  147. fk_commande = Column("fk_commande", Integer, ForeignKey('llx_commande.rowid'))
  148. commande = relationship(
  149. "Commande", backref="details",
  150. lazy="subquery",
  151. )
  152. fk_product = Column("fk_product", Integer, ForeignKey('llx_product.rowid'))
  153. product = relationship(
  154. "Product", backref="commande_details",
  155. lazy="subquery",
  156. )
  157. class Societe(DolibarrBase):
  158. __tablename__ = "llx_societe"
  159. id = Column("rowid", Integer, primary_key=True)
  160. class PaiementFacture(DolibarrBase):
  161. __tablename__ = "llx_paiement_facture"
  162. fk_paiement = Column(Integer, ForeignKey('llx_paiement.rowid'), primary_key=True)
  163. fk_facture = Column(Integer, ForeignKey('llx_facture.rowid'), primary_key=True)
  164. facture = relationship(
  165. "Facture",
  166. lazy="subquery",
  167. order_by="Facture.facnumber"
  168. )
  169. paiement = relationship(
  170. "Paiement",
  171. lazy="subquery",
  172. order_by="Paiement.datep,Paiement.id",
  173. )
  174. class Paiement(DolibarrBase):
  175. __tablename__ = "llx_paiement"
  176. id = Column("rowid", Integer, primary_key=True)
  177. factures = relationship(
  178. "PaiementFacture",
  179. lazy="subquery",
  180. order_by="PaiementFacture.fk_facture,PaiementFacture.rowid",
  181. )
  182. class PaiementFournFactureFourn(DolibarrBase):
  183. __tablename__ = "llx_paiementfourn_facturefourn"
  184. fk_paiementfourn = Column(Integer, ForeignKey('llx_paiementfourn.rowid'), primary_key=True)
  185. fk_facturefourn = Column(Integer, ForeignKey('llx_facture_fourn.rowid'), primary_key=True)
  186. facture = relationship(
  187. "FactureFourn",
  188. lazy="subquery",
  189. )
  190. paiement = relationship(
  191. "PaiementFourn",
  192. lazy="subquery",
  193. order_by="PaiementFourn.datep,PaiementFourn.id",
  194. )
  195. class PaiementFourn(DolibarrBase):
  196. __tablename__ = "llx_paiementfourn"
  197. id = Column("rowid", Integer, primary_key=True)
  198. factures = relationship(
  199. "PaiementFournFactureFourn",
  200. lazy="subquery",
  201. )
  202. class PaiementCharge(DolibarrBase):
  203. __tablename__ = "llx_paiementcharge"
  204. id = Column("rowid", Integer, primary_key=True)
  205. fk_bank = Column("fk_bank", Integer, ForeignKey('llx_bank.rowid'))
  206. bank = relationship(
  207. "Bank", backref="paiementcharges",
  208. lazy="subquery",
  209. )
  210. fk_charge = Column(Integer, ForeignKey('llx_chargesociales.rowid'))
  211. cotisation_sociale = relationship(
  212. "CotisationsSociales", backref="paiement",
  213. lazy="subquery"
  214. )
  215. class Product(DolibarrBase):
  216. __tablename__ = "llx_product"
  217. id = Column("rowid", Integer, primary_key=True)
  218. class FactureFourn(DolibarrBase):
  219. __tablename__ = "llx_facture_fourn"
  220. id = Column("rowid", Integer, primary_key=True)
  221. fk_soc = Column(Integer, ForeignKey('llx_societe.rowid'))
  222. societe = relationship(
  223. 'Societe',
  224. backref="factures_fournisseurs",
  225. lazy="subquery",
  226. )
  227. details = relationship(
  228. 'FactureFournDet',
  229. lazy="subquery",
  230. )
  231. class Facture(DolibarrBase):
  232. __tablename__ = "llx_facture"
  233. id = Column("rowid", Integer, primary_key=True)
  234. fk_soc = Column(Integer, ForeignKey('llx_societe.rowid'))
  235. societe = relationship(
  236. 'Societe',
  237. backref="factures",
  238. lazy="joined",
  239. )
  240. details = relationship(
  241. 'FactureDet',
  242. lazy="subquery",
  243. )
  244. class FactureDet(DolibarrBase):
  245. __tablename__ = "llx_facturedet"
  246. id = Column("rowid", Integer, primary_key=True)
  247. fk_facture = Column(Integer, ForeignKey('llx_facture.rowid'))
  248. facture = relationship(
  249. 'Facture',
  250. lazy="subquery",
  251. )
  252. fk_product = Column(Integer, ForeignKey('llx_product.rowid'))
  253. product = relationship(
  254. 'Product',
  255. backref="facture_det",
  256. lazy="subquery",
  257. )
  258. fk_code_ventilation = Column(Integer, ForeignKey('llx_accountingaccount.rowid'))
  259. accounting_account = relationship(
  260. 'AccountingAccount',
  261. backref="facture_det",
  262. lazy="subquery",
  263. )
  264. if DolibarrBase.after40():
  265. fk_code_ventilation = Column(Integer, ForeignKey('llx_accounting_account.rowid'))
  266. class FactureFournDet(DolibarrBase):
  267. __tablename__ = "llx_facture_fourn_det"
  268. id = Column("rowid", Integer, primary_key=True)
  269. fk_facture_fourn = Column(Integer, ForeignKey('llx_facture_fourn.rowid'))
  270. facture = relationship(
  271. 'FactureFourn',
  272. lazy="subquery",
  273. )
  274. fk_product = Column(Integer, ForeignKey('llx_product.rowid'))
  275. product = relationship(
  276. 'Product',
  277. backref="facture_fourn_det",
  278. lazy="subquery",
  279. )
  280. fk_code_ventilation = Column(Integer, ForeignKey('llx_accountingaccount.rowid'))
  281. accounting_account = relationship(
  282. 'AccountingAccount',
  283. backref="facture_fourn_det",
  284. lazy="subquery",
  285. )
  286. if DolibarrBase.after40():
  287. fk_code_ventilation = Column(Integer, ForeignKey('llx_accounting_account.rowid'))
  288. class AccountingAccount(DolibarrBase):
  289. __tablename__ = "llx_accountingaccount"
  290. id = Column("rowid", Integer, primary_key=True)
  291. if DolibarrBase.after40():
  292. __tablename__ = "llx_accounting_account"
  293. self.Bank = Bank
  294. self.Facture = Facture
  295. self.FactureFourn = FactureFourn
  296. self.CotisationsSociales = CotisationsSociales
  297. DolibarrBase.prepare(self.engine)
  298. def disconnect(self):
  299. self.session.close()