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.

322 lines
10 KiB

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