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.

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