dolibarrAlchemy.py 13 KB

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