dolibarrAlchemy.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. Base = declarative_base(cls=DeferredReflection)
  8. class Bank(Base):
  9. __tablename__ = "llx_bank"
  10. id = Column("rowid", Integer, primary_key=True)
  11. fk_account = Column('fk_account', Integer, ForeignKey('llx_bank_account.rowid'))
  12. account = relationship("BankAccount", backref="entries", lazy="subquery")
  13. bankclass = relationship(
  14. "BankClass", backref="bank", uselist=False,
  15. lazy="subquery"
  16. )
  17. url_company = relationship(
  18. "BankUrl", uselist=False, backref="bank_company", innerjoin=True,
  19. primaryjoin="and_(Bank.id==BankUrl.fk_bank, " "BankUrl.type=='company')",
  20. lazy="subquery",
  21. )
  22. url_payment = relationship(
  23. "BankUrl", uselist=False, backref="bank_payement", innerjoin=True,
  24. primaryjoin="and_(Bank.id==BankUrl.fk_bank, " "BankUrl.type=='payment')",
  25. lazy="subquery",
  26. )
  27. url_payment_sc = relationship(
  28. "BankUrl", uselist=False, backref="bank_payement_sc", innerjoin=True,
  29. primaryjoin="and_(Bank.id==BankUrl.fk_bank, " "BankUrl.type=='payment_sc')",
  30. lazy="subquery",
  31. )
  32. url_payment_supplier = relationship(
  33. "BankUrl", uselist=False, backref="bank_payement_supplier", innerjoin=True,
  34. primaryjoin="and_(Bank.id==BankUrl.fk_bank, " "BankUrl.type=='payment_supplier')",
  35. lazy="subquery",
  36. )
  37. payment_tva = relationship(
  38. "TVA", backref="bank",
  39. lazy="subquery"
  40. )
  41. class BankAccount(Base):
  42. __tablename__ = "llx_bank_account"
  43. id = Column("rowid", Integer, primary_key=True)
  44. class BankCateg(Base):
  45. __tablename__ = "llx_bank_categ"
  46. id = Column("rowid", Integer, primary_key=True)
  47. class BankClass(Base):
  48. __tablename__ = "llx_bank_class"
  49. id = Column('lineid', Integer, ForeignKey('llx_bank.rowid'), primary_key=True)
  50. UniqueConstraint('lineid', 'fk_categ')
  51. categ_id = Column('fk_categ', Integer, ForeignKey('llx_bank_categ.rowid'))
  52. categ = relationship(
  53. "BankCateg", backref="classes",
  54. lazy="subquery",
  55. )
  56. class BankUrl(Base):
  57. __tablename__ = "llx_bank_url"
  58. id = Column("rowid", Integer, primary_key=True)
  59. fk_bank = Column('fk_bank', Integer, ForeignKey('llx_bank.rowid'))
  60. url_id = Column('url_id', Integer)
  61. societe = relationship(
  62. "Societe", backref="bank_urls", uselist=False,
  63. primaryjoin="and_(BankUrl.url_id==Societe.id, " "BankUrl.type=='company')",
  64. foreign_keys=url_id,
  65. lazy="subquery",
  66. )
  67. payment = relationship(
  68. "Paiement", backref="bank_urls", uselist=False,
  69. primaryjoin="and_(BankUrl.url_id==Paiement.id, " "BankUrl.type=='payment')",
  70. foreign_keys=url_id,
  71. lazy="subquery",
  72. )
  73. payment_sc = relationship(
  74. "PaiementCharge", backref="bank_urls", uselist=False,
  75. primaryjoin="and_(BankUrl.url_id==PaiementCharge.id, " "BankUrl.type=='payment_sc')",
  76. foreign_keys=url_id,
  77. lazy="subquery",
  78. )
  79. payment_supplier = relationship(
  80. "PaiementFourn", backref="bank_urls", uselist=False,
  81. primaryjoin="and_(BankUrl.url_id==PaiementFourn.id, " "BankUrl.type=='payment_supplier')",
  82. foreign_keys=url_id,
  83. lazy="subquery",
  84. )
  85. class TVA(Base):
  86. __tablename__ = "llx_tva"
  87. id = Column("rowid", Integer, primary_key=True)
  88. fk_bank = Column('fk_bank', Integer, ForeignKey('llx_bank.rowid'))
  89. class CotisationsSociales(Base):
  90. __tablename__ = "llx_chargesociales"
  91. id = Column("rowid", Integer, primary_key=True)
  92. fk_type = Column('fk_type', Integer, ForeignKey('llx_c_chargesociales.id'))
  93. type = relationship(
  94. "CCotisationsSociales", backref="cotisations_sociales",
  95. lazy="subquery",
  96. )
  97. class CCotisationsSociales(Base):
  98. __tablename__ = "llx_c_chargesociales"
  99. id = Column("id", Integer, primary_key=True)
  100. class Commande(Base):
  101. __tablename__ = "llx_commande"
  102. id = Column("rowid", Integer, primary_key=True)
  103. class CommandeDet(Base):
  104. __tablename__ = "llx_commandedet"
  105. id = Column("rowid", Integer, primary_key=True)
  106. fk_commande = Column("fk_commande", Integer, ForeignKey('llx_commande.rowid'))
  107. commande = relationship(
  108. "Commande", backref="details",
  109. lazy="subquery",
  110. )
  111. fk_product = Column("fk_product", Integer, ForeignKey('llx_product.rowid'))
  112. product = relationship(
  113. "Product", backref="commande_details",
  114. lazy="subquery",
  115. )
  116. class Societe(Base):
  117. __tablename__ = "llx_societe"
  118. id = Column("rowid", Integer, primary_key=True)
  119. class PaiementFacture(Base):
  120. __tablename__ = "llx_paiement_facture"
  121. fk_paiement = Column(Integer, ForeignKey('llx_paiement.rowid'), primary_key=True)
  122. fk_facture = Column(Integer, ForeignKey('llx_facture.rowid'), primary_key=True)
  123. facture = relationship(
  124. "Facture",
  125. lazy="subquery",
  126. )
  127. paiement = relationship(
  128. "Paiement",
  129. lazy="subquery",
  130. order_by="Paiement.datep,Paiement.id",
  131. )
  132. class Paiement(Base):
  133. __tablename__ = "llx_paiement"
  134. id = Column("rowid", Integer, primary_key=True)
  135. factures = relationship(
  136. "PaiementFacture",
  137. lazy="subquery",
  138. )
  139. class PaiementFournFactureFourn(Base):
  140. __tablename__ = "llx_paiementfourn_facturefourn"
  141. fk_paiementfourn = Column(Integer, ForeignKey('llx_paiementfourn.rowid'), primary_key=True)
  142. fk_facturefourn = Column(Integer, ForeignKey('llx_facture_fourn.rowid'), primary_key=True)
  143. facture = relationship(
  144. "FactureFourn",
  145. lazy="subquery",
  146. )
  147. paiement = relationship(
  148. "PaiementFourn",
  149. lazy="subquery",
  150. order_by="PaiementFourn.datep,PaiementFourn.id",
  151. )
  152. class PaiementFourn(Base):
  153. __tablename__ = "llx_paiementfourn"
  154. id = Column("rowid", Integer, primary_key=True)
  155. factures = relationship(
  156. "PaiementFournFactureFourn",
  157. lazy="subquery",
  158. )
  159. class PaiementCharge(Base):
  160. __tablename__ = "llx_paiementcharge"
  161. id = Column("rowid", Integer, primary_key=True)
  162. fk_bank = Column("fk_bank", Integer, ForeignKey('llx_bank.rowid'))
  163. bank = relationship(
  164. "Bank", backref="paiementcharges",
  165. lazy="subquery",
  166. )
  167. fk_charge = Column(Integer, ForeignKey('llx_chargesociales.rowid'))
  168. cotisation_sociale = relationship(
  169. "CotisationsSociales", backref="paiement",
  170. lazy="subquery"
  171. )
  172. class Product(Base):
  173. __tablename__ = "llx_product"
  174. id = Column("rowid", Integer, primary_key=True)
  175. class FactureFourn(Base):
  176. __tablename__ = "llx_facture_fourn"
  177. id = Column("rowid", Integer, primary_key=True)
  178. fk_soc = Column(Integer, ForeignKey('llx_societe.rowid'))
  179. societe = relationship(
  180. 'Societe',
  181. backref="factures_fournisseurs",
  182. lazy="subquery",
  183. )
  184. details = relationship(
  185. 'FactureFournDet',
  186. lazy="subquery",
  187. )
  188. class Facture(Base):
  189. __tablename__ = "llx_facture"
  190. id = Column("rowid", Integer, primary_key=True)
  191. fk_soc = Column(Integer, ForeignKey('llx_societe.rowid'))
  192. societe = relationship(
  193. 'Societe',
  194. backref="factures",
  195. lazy="joined",
  196. )
  197. details = relationship(
  198. 'FactureDet',
  199. lazy="subquery",
  200. )
  201. class FactureDet(Base):
  202. __tablename__ = "llx_facturedet"
  203. id = Column("rowid", Integer, primary_key=True)
  204. fk_facture = Column(Integer, ForeignKey('llx_facture.rowid'))
  205. facture = relationship(
  206. 'Facture',
  207. lazy="subquery",
  208. )
  209. fk_product = Column(Integer, ForeignKey('llx_product.rowid'))
  210. product = relationship(
  211. 'Product',
  212. backref="facture_det",
  213. lazy="subquery",
  214. )
  215. fk_code_ventilation = Column(Integer, ForeignKey('llx_accountingaccount.rowid'))
  216. accounting_account = relationship(
  217. 'AccountingAccount',
  218. backref="facture_det",
  219. lazy="subquery",
  220. )
  221. class FactureFournDet(Base):
  222. __tablename__ = "llx_facture_fourn_det"
  223. id = Column("rowid", Integer, primary_key=True)
  224. fk_facture_fourn = Column(Integer, ForeignKey('llx_facture_fourn.rowid'))
  225. facture = relationship(
  226. 'FactureFourn',
  227. lazy="subquery",
  228. )
  229. fk_product = Column(Integer, ForeignKey('llx_product.rowid'))
  230. product = relationship(
  231. 'Product',
  232. backref="facture_fourn_det",
  233. lazy="subquery",
  234. )
  235. fk_code_ventilation = Column(Integer, ForeignKey('llx_accountingaccount.rowid'))
  236. accounting_account = relationship(
  237. 'AccountingAccount',
  238. backref="facture_fourn_det",
  239. lazy="subquery",
  240. )
  241. class AccountingAccount(Base):
  242. __tablename__ = "llx_accountingaccount"
  243. id = Column("rowid", Integer, primary_key=True)
  244. MYSQL_QUERIES = {
  245. "bank": """SELECT DISTINCT b.rowid as b_rowid,
  246. ba.ref as ba_ref,
  247. ba.label as ba_label,
  248. ba.account_number as ba_account_number,
  249. b.datev as b_datev,
  250. b.dateo as b_dateo,
  251. b.label as b_label,
  252. b.num_chq as b_num_chq,
  253. -b.amount as _b_amount,
  254. b.amount as b_amount,
  255. b.num_releve as b_num_releve,
  256. b.datec as b_datec,
  257. bu.url_id as bu_url_id,
  258. s.nom as s_nom,
  259. s.code_compta as s_code_compta,
  260. s.code_compta_fournisseur as s_code_compta_fournisseur,
  261. bca.label as bca_label,
  262. bca.rowid as bca_rowid,
  263. bcl.lineid as bcl_lineid,
  264. ccs.code as ccs_code,
  265. ccs.libelle as ccs_label
  266. FROM (llx_bank_account as ba, llx_bank as b)
  267. LEFT JOIN llx_bank_url as bu ON (bu.fk_bank = b.rowid AND bu.type = 'company')
  268. LEFT JOIN llx_societe as s ON bu.url_id = s.rowid
  269. LEFT JOIN llx_bank_class as bcl ON bcl.lineid = b.rowid
  270. LEFT JOIN llx_bank_categ as bca ON bca.rowid = bcl.fk_categ
  271. LEFT JOIN llx_paiementcharge as p ON p.fk_bank = b.rowid
  272. LEFT JOIN llx_chargesociales as cs ON cs.rowid = p.fk_charge
  273. LEFT JOIN llx_c_chargesociales as ccs ON cs.fk_type = ccs.id
  274. WHERE ba.rowid = b.fk_account AND ba.entity = 1 and b.num_releve <> ''
  275. ORDER BY b.datev, b.num_releve;""",
  276. "sells": """SELECT DISTINCT s.rowid as s_rowid,
  277. s.nom as s_nom,
  278. s.address as s_address,
  279. s.zip as s_zip,
  280. s.town as s_town,
  281. c.code as c_code,
  282. s.phone as s_phone,
  283. s.siren as s_siren,
  284. s.siret as s_siret,
  285. s.ape as s_ape,
  286. s.idprof4 as s_idprof4,
  287. s.code_compta as s_code_compta,
  288. s.code_compta_fournisseur as s_code_compta_fournisseur,
  289. s.tva_intra as s_tva_intra,
  290. f.rowid as f_rowid,
  291. f.facnumber as f_facnumber,
  292. f.datec as f_datec,
  293. f.datef as f_datef,
  294. f.date_lim_reglement as f_date_lim_reglement,
  295. f.total as f_total,
  296. f.total_ttc as f_total_ttc,
  297. f.tva as f_tva,
  298. f.paye as f_paye,
  299. f.fk_statut as f_fk_statut,
  300. f.note_private as f_note_private,
  301. f.note_public as f_note_public,
  302. fd.rowid as fd_rowid,
  303. fd.label as fd_label,
  304. fd.description as fd_description,
  305. fd.subprice as fd_subprice,
  306. fd.tva_tx as fd_tva_tx,
  307. fd.qty as fd_qty,
  308. fd.total_ht as fd_total_ht,
  309. fd.total_tva as fd_total_tva,
  310. fd.total_ttc as fd_total_ttc,
  311. fd.date_start as fd_date_start,
  312. fd.date_end as fd_date_end,
  313. fd.special_code as fd_special_code,
  314. fd.product_type as fd_product_type,
  315. fd.fk_product as fd_fk_product,
  316. p.ref as p_ref,
  317. p.label as p_label,
  318. p.accountancy_code_sell as p_accountancy_code_sell,
  319. a.account_number as a_account_number
  320. FROM llx_societe as s
  321. LEFT JOIN llx_c_pays as c on s.fk_pays = c.rowid, llx_facture as f
  322. LEFT JOIN llx_facture_extrafields as extra ON f.rowid = extra.fk_object , llx_facturedet as fd
  323. LEFT JOIN llx_product as p on (fd.fk_product = p.rowid)
  324. LEFT JOIN llx_accountingaccount as a ON fd.fk_code_ventilation = a.rowid
  325. WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_facture AND f.entity = 1""",
  326. "suppliers": """SELECT DISTINCT s.rowid as s_rowid,
  327. s.nom as s_nom,
  328. s.address as s_address,
  329. s.zip as s_zip,
  330. s.town as s_town,
  331. s.code_compta_fournisseur as s_code_supplier,
  332. c.code as c_code,
  333. s.phone as s_phone,
  334. s.siren as s_siren,
  335. s.siret as s_siret,
  336. s.ape as s_ape,
  337. s.idprof4 as s_idprof4,
  338. s.idprof5 as s_idprof5,
  339. s.idprof6 as s_idprof6,
  340. s.tva_intra as s_tva_intra,
  341. f.rowid as f_rowid,
  342. f.ref as f_ref,
  343. f.ref_supplier as f_ref_supplier,
  344. f.datec as f_datec,
  345. f.datef as f_datef,
  346. f.total_ht as f_total_ht,
  347. f.total_ttc as f_total_ttc,
  348. f.total_tva as f_total_tva,
  349. f.paye as f_paye,
  350. f.fk_statut as f_fk_statut,
  351. f.note_public as f_note_public,
  352. fd.rowid as fd_rowid,
  353. fd.description as fd_description,
  354. fd.tva_tx as fd_tva_tx,
  355. fd.qty as fd_qty,
  356. fd.remise_percent as fd_remise_percent,
  357. fd.total_ht as fd_total_ht,
  358. fd.total_ttc as fd_total_ttc,
  359. fd.tva as fd_tva,
  360. fd.product_type as fd_product_type,
  361. fd.fk_product as fd_fk_product,
  362. p.ref as p_ref,
  363. p.label as p_label,
  364. p.accountancy_code_buy as p_accountancy_code_buy,
  365. a.account_number as a_account_number FROM llx_societe as s
  366. LEFT JOIN llx_c_pays as c
  367. ON s.fk_pays = c.rowid, llx_facture_fourn as f
  368. LEFT JOIN llx_facture_fourn_extrafields as extra
  369. ON f.rowid = extra.fk_object, llx_facture_fourn_det as fd
  370. LEFT JOIN llx_product as p on (fd.fk_product = p.rowid)
  371. LEFT JOIN llx_accountingaccount as a
  372. ON fd.fk_code_ventilation = a.rowid
  373. WHERE f.fk_soc = s.rowid AND f.rowid = fd.fk_facture_fourn AND f.entity = 1""",
  374. "social": """SELECT DISTINCT cc.libelle as cc_libelle,
  375. c.rowid as c_rowid,
  376. c.libelle as c_libelle,
  377. c.date_ech as c_date_ech,
  378. c.periode as c_periode,
  379. c.amount as c_amount,
  380. c.paye as c_paye,
  381. p.rowid as p_rowid,
  382. p.datep as p_datep,
  383. p.amount as p_amount,
  384. p.num_paiement as p_num_paiement,
  385. cc.accountancy_code as cc_acc_code,
  386. cc.code as cc_code FROM llx_c_chargesociales as cc,
  387. llx_chargesociales as c
  388. LEFT JOIN llx_paiementcharge as p
  389. ON p.fk_charge = c.rowid
  390. WHERE c.fk_type = cc.id AND c.entity = 1""",
  391. }
  392. class DolibarrSQLAlchemy(object):
  393. def __init__(
  394. self,
  395. mysql_host,
  396. mysql_port,
  397. mysql_database,
  398. mysql_user,
  399. mysql_password,
  400. echo=False):
  401. self.mysql_database = mysql_database
  402. self.mysql_host = mysql_host
  403. self.mysql_password = mysql_password
  404. self.mysql_user = mysql_user
  405. self.mysql_port = mysql_port
  406. self.echo = echo
  407. def connect(self):
  408. engine_str = "mysql://%s:%s@%s:%s/%s" % (
  409. self.mysql_user,
  410. self.mysql_password,
  411. self.mysql_host,
  412. self.mysql_port,
  413. self.mysql_database
  414. )
  415. print engine_str
  416. self.engine = create_engine(engine_str, echo=self.echo, encoding=str("iso8859-1"), convert_unicode=True)
  417. self.metadata = MetaData(bind=self.engine)
  418. Base.prepare(self.engine)
  419. # create a configured "Session" class
  420. Session = sessionmaker(bind=self.engine)
  421. # create a Session
  422. self.session = Session(autocommit=True)
  423. def disconnect(self):
  424. self.session.close()