You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
11 KiB

  1. # -*- encoding: utf-8 -*-
  2. from openerp import fields, models, api
  3. import time
  4. from cStringIO import StringIO
  5. import base64
  6. import xlsxwriter
  7. HEADER = [
  8. 'Num. Coop',
  9. 'Numero de registre national',
  10. 'Nom',
  11. 'Email',
  12. 'Banque',
  13. 'Mobile',
  14. 'Adresse',
  15. 'Rue',
  16. 'Code Postal',
  17. 'Ville',
  18. 'Pays',
  19. 'Nombre de part total',
  20. 'Montant total des parts',
  21. 'Demande de liberation de capital',
  22. 'Communication',
  23. 'Nombre de part',
  24. 'Montant',
  25. 'Reception du paiement',
  26. 'Date de la souscription'
  27. ]
  28. HEADER2 = [
  29. 'Date de la souscription',
  30. 'Nom',
  31. 'Type',
  32. 'Nombre de part',
  33. 'Montant',
  34. 'Statut',
  35. 'Numero de registre national',
  36. 'Email',
  37. 'Mobile',
  38. 'Adresse',
  39. 'Code Postal',
  40. 'Ville',
  41. 'Pays',
  42. ]
  43. class export_global_report(models.TransientModel):
  44. _name = 'export.global.report'
  45. name = fields.Char('Name')
  46. def write_header(self, worksheet, headers):
  47. i = 0
  48. for header in headers:
  49. worksheet.write(0, i, header)
  50. i += 1
  51. return True
  52. @api.multi
  53. def export_global_report_xlsx(self):
  54. partner_obj = self.env['res.partner']
  55. invoice_obj = self.env['account.invoice']
  56. subscription_obj = self.env['subscription.request']
  57. file_data = StringIO()
  58. workbook = xlsxwriter.Workbook(file_data)
  59. worksheet1 = workbook.add_worksheet()
  60. self.write_header(worksheet1, HEADER)
  61. cooperators = partner_obj.search([('cooperator', '=', True),
  62. ('member', '=', True)])
  63. j = 1
  64. for coop in cooperators:
  65. i = 0
  66. worksheet1.write(j, i, coop.cooperator_register_number)
  67. i += 1
  68. worksheet1.write(j, i, coop.national_register_number)
  69. i += 1
  70. worksheet1.write(j, i, coop.name)
  71. i += 1
  72. worksheet1.write(j, i, coop.email)
  73. i += 1
  74. acc_number = ""
  75. if coop.bank_ids:
  76. acc_number = coop.bank_ids[0].acc_number
  77. worksheet1.write(j, i, acc_number)
  78. i += 1
  79. worksheet1.write(j, i, coop.phone)
  80. i += 1
  81. address = coop.street + ' ' + coop.zip + ' ' \
  82. + coop.city + ' ' + coop.country_id.name
  83. worksheet1.write(j, i, address)
  84. i += 1
  85. worksheet1.write(j, i, coop.street)
  86. i += 1
  87. worksheet1.write(j, i, int(coop.zip))
  88. i += 1
  89. worksheet1.write(j, i, coop.city)
  90. i += 1
  91. worksheet1.write(j, i, coop.country_id.name)
  92. i += 1
  93. worksheet1.write(j, i, coop.number_of_share)
  94. i += 1
  95. worksheet1.write(j, i, coop.total_value)
  96. invoice_ids = invoice_obj.search([('release_capital_request', '=', True),
  97. ('partner_id', '=', coop.id)])
  98. j += 1
  99. for invoice in invoice_ids:
  100. i = 11
  101. worksheet1.write(j, i, invoice.number)
  102. i += 1
  103. worksheet1.write(j, i, invoice.state)
  104. i += 1
  105. worksheet1.write(j, i, invoice.date_invoice)
  106. i += 1
  107. worksheet1.write(j, i, invoice.reference)
  108. i += 1
  109. for line in invoice.invoice_line_ids:
  110. worksheet1.write(j, i, line.quantity)
  111. i += 1
  112. worksheet1.write(j, i, line.price_subtotal)
  113. i += 1
  114. if invoice.payment_move_line_ids:
  115. worksheet1.write(j, i, invoice.payment_move_line_ids[0].date)
  116. i += 1
  117. if invoice.subscription_request:
  118. ind = len(invoice.subscription_request)-1
  119. worksheet1.write(j, i, invoice.subscription_request[ind].date)
  120. j += 1
  121. sub_requests = subscription_obj.search([('state', 'in',
  122. ['draft', 'waiting']),
  123. ('partner_id', '=', coop.id)
  124. ])
  125. for sub_request in sub_requests:
  126. i = 11
  127. worksheet1.write(j, i, dict(subscription_obj._columns['type'].selection).get(sub_request.type,False))
  128. i += 1
  129. worksheet1.write(j, i, sub_request.state)
  130. i += 3
  131. quantity = int(sub_request.ordered_parts)
  132. worksheet1.write(j, i, quantity)
  133. i += 1
  134. amount = quantity * sub_request.share_unit_price
  135. worksheet1.write(j, i, amount)
  136. i += 2
  137. worksheet1.write(j, i, sub_request.date)
  138. j += 1
  139. worksheet1bis = workbook.add_worksheet()
  140. self.write_header(worksheet1bis, HEADER)
  141. cooperators = partner_obj.search([('cooperator', '=', True),
  142. ('member', '=', False)])
  143. j = 1
  144. for coop in cooperators:
  145. i = 0
  146. worksheet1bis.write(j, i, coop.cooperator_register_number)
  147. i += 1
  148. worksheet1bis.write(j, i, coop.national_register_number)
  149. i += 1
  150. worksheet1bis.write(j, i, coop.name)
  151. i += 1
  152. worksheet1bis.write(j, i, coop.email)
  153. i += 1
  154. worksheet1bis.write(j, i, coop.phone)
  155. i += 1
  156. worksheet1bis.write(j, i, coop.street)
  157. i += 1
  158. worksheet1bis.write(j, i, int(coop.zip))
  159. i += 1
  160. worksheet1bis.write(j, i, coop.city)
  161. i += 1
  162. worksheet1bis.write(j, i, coop.country_id.name)
  163. i += 1
  164. worksheet1bis.write(j, i, coop.number_of_share)
  165. i += 1
  166. worksheet1bis.write(j, i, coop.total_value)
  167. invoice_ids = invoice_obj.search([('release_capital_request', '=', True),
  168. ('partner_id', '=', coop.id)])
  169. j += 1
  170. for invoice in invoice_ids:
  171. i = 11
  172. worksheet1bis.write(j, i, invoice.number)
  173. i += 1
  174. worksheet1bis.write(j, i, invoice.state)
  175. i += 1
  176. worksheet1bis.write(j, i, invoice.date_invoice)
  177. i += 1
  178. worksheet1bis.write(j, i, invoice.reference)
  179. i += 1
  180. for line in invoice.invoice_line_ids:
  181. worksheet1bis.write(j, i, line.quantity)
  182. i += 1
  183. worksheet1bis.write(j, i, line.price_subtotal)
  184. i += 1
  185. if invoice.payment_ids:
  186. worksheet1bis.write(j, i, invoice.payment_ids[0].date)
  187. i += 1
  188. if invoice.subscription_request:
  189. ind = len(invoice.subscription_request)-1
  190. worksheet1bis.write(j, i, invoice.subscription_request[ind].date)
  191. j += 1
  192. sub_requests = subscription_obj.search([('state', 'in',
  193. ['draft', 'waiting']),
  194. ('partner_id', '=', coop.id)
  195. ])
  196. for sub_request in sub_requests:
  197. i = 11
  198. worksheet1bis.write(j, i, dict(subscription_obj._columns['type'].selection).get(sub_request.type,False))
  199. i += 1
  200. worksheet1bis.write(j, i, sub_request.state)
  201. i += 3
  202. quantity = int(sub_request.ordered_parts)
  203. worksheet1bis.write(j, i, quantity)
  204. i += 1
  205. amount = quantity * sub_request.share_unit_price
  206. worksheet1bis.write(j, i, amount)
  207. i += 2
  208. worksheet1bis.write(j, i, sub_request.date)
  209. j += 1
  210. worksheet2 = workbook.add_worksheet()
  211. self.write_header(worksheet2, HEADER2)
  212. sub_requests = subscription_obj.search([('state', 'in',
  213. ['draft', 'waiting']),
  214. ])
  215. j = 1
  216. for sub_request in sub_requests:
  217. i = 0
  218. worksheet2.write(j, i, sub_request.date)
  219. i += 1
  220. worksheet2.write(j, i, sub_request.name)
  221. i += 1
  222. sub_type_sel = subscription_obj._columns['type'].selection
  223. worksheet2.write(j, i, dict(sub_type_sel).get(sub_request.type, False))
  224. i += 1
  225. quantity = int(sub_request.ordered_parts)
  226. worksheet2.write(j, i, quantity)
  227. i += 1
  228. amount = quantity * sub_request.share_unit_price
  229. worksheet2.write(j, i, amount)
  230. i += 1
  231. worksheet2.write(j, i, sub_request.state)
  232. i += 1
  233. worksheet2.write(j, i, sub_request.no_registre)
  234. i += 1
  235. worksheet2.write(j, i, sub_request.email)
  236. i += 1
  237. worksheet2.write(j, i, sub_request.phone)
  238. i += 1
  239. worksheet2.write(j, i, sub_request.address)
  240. i += 1
  241. worksheet2.write(j, i, sub_request.city)
  242. i += 1
  243. worksheet2.write(j, i, int(sub_request.zip_code))
  244. i += 1
  245. worksheet2.write(j, i, sub_request.country_id.name)
  246. j += 1
  247. workbook.close()
  248. file_data.seek(0)
  249. data = base64.encodestring(file_data.read())
  250. attachment_id = self.env['ir.attachment'].create({
  251. 'name': "Global export" + time.strftime('%Y-%m-%d %H:%M')
  252. + ".xlsx",
  253. 'datas': data,
  254. 'datas_fname': 'Global_export.xlsx',
  255. 'res_model': 'export.global.report',
  256. },)
  257. # Prepare your download URL
  258. download_url = '/web/content/' + str(attachment_id.id) + '?download=True'
  259. base_url = self.env['ir.config_parameter'].get_param('web.base.url')
  260. return {
  261. "type": "ir.actions.act_url",
  262. "url": str(base_url) + str(download_url),
  263. "target": "new",
  264. }