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.

291 lines
10 KiB

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