dolibarrAlchemy.py 17 KB

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