diff --git a/easy_my_coop_export_xlsx/__init__.py b/easy_my_coop_export_xlsx/__init__.py new file mode 100644 index 0000000..863921d --- /dev/null +++ b/easy_my_coop_export_xlsx/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf8 -*- +from . import wizard diff --git a/easy_my_coop_export_xlsx/__openerp__.py b/easy_my_coop_export_xlsx/__openerp__.py new file mode 100644 index 0000000..73e2c77 --- /dev/null +++ b/easy_my_coop_export_xlsx/__openerp__.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- + +# Copyright (C) 2018- Coop IT Easy SCRLfs. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +############################################################################## + + +{ + 'name': 'Easy My Coop Export XLSX', + + 'summary': """ + Generate a xlsx file with information on current state of subscription + request, cooperators and capital release request. + """, + 'author': 'Houssine BAKKALI, ', + 'license': 'AGPL-3', + 'version': '9.0.1.0', + 'website': "www.coopiteasy.be", + + 'category': 'Cooperative Management', + + 'depends': [ + 'easy_my_coop', + ], + 'external_dependencies': { + 'python': ['xlsxwriter'], + }, + 'data': [ + # 'security/ir.model.access.csv', + 'wizard/export_global_wizard.xml', + ] +} diff --git a/easy_my_coop_export_xlsx/wizard/__init__.py b/easy_my_coop_export_xlsx/wizard/__init__.py new file mode 100644 index 0000000..ac23f7b --- /dev/null +++ b/easy_my_coop_export_xlsx/wizard/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf8 -*- +from . import export_global_wizard diff --git a/easy_my_coop_export_xlsx/wizard/export_global_wizard.py b/easy_my_coop_export_xlsx/wizard/export_global_wizard.py new file mode 100644 index 0000000..c8c4d0b --- /dev/null +++ b/easy_my_coop_export_xlsx/wizard/export_global_wizard.py @@ -0,0 +1,291 @@ +# -*- encoding: utf-8 -*- + +from openerp import fields, models, api + +import time +from cStringIO import StringIO +import base64 + +import xlsxwriter + +HEADER = [ + 'Num. Coop', + 'Numero de registre national', + 'Nom', + 'Email', + 'Banque', + 'Mobile', + 'Adresse', + 'Rue', + 'Code Postal', + 'Ville', + 'Pays', + 'Nombre de part total', + 'Montant total des parts', + 'Numero de demande', + 'Statut', + 'Demande de liberation de capital', + 'Communication', + 'Nombre de part', + 'Montant', + 'Reception du paiement', + 'Date de la souscription' + ] +HEADER2 = [ + 'Date de la souscription', + 'Nom', + 'Type', + 'Nombre de part', + 'Montant', + 'Statut', + 'Numero de registre national', + 'Email', + 'Mobile', + 'Adresse', + 'Code Postal', + 'Ville', + 'Pays', + ] + + +class export_global_report(models.TransientModel): + _name = 'export.global.report' + + name = fields.Char('Name') + + def write_header(self, worksheet, headers): + i = 0 + for header in headers: + worksheet.write(0, i, header) + i += 1 + return True + + @api.multi + def export_global_report_xlsx(self): + partner_obj = self.env['res.partner'] + invoice_obj = self.env['account.invoice'] + subscription_obj = self.env['subscription.request'] + + file_data = StringIO() + workbook = xlsxwriter.Workbook(file_data) + worksheet1 = workbook.add_worksheet() + + self.write_header(worksheet1, HEADER) + cooperators = partner_obj.search([('cooperator', '=', True), + ('member', '=', True)]) + + j = 1 + for coop in cooperators: + i = 0 + worksheet1.write(j, i, coop.cooperator_register_number) + i += 1 + worksheet1.write(j, i, coop.national_register_number) + i += 1 + worksheet1.write(j, i, coop.name) + i += 1 + worksheet1.write(j, i, coop.email) + i += 1 + acc_number = "" + if coop.bank_ids: + acc_number = coop.bank_ids[0].acc_number + worksheet1.write(j, i, acc_number) + i += 1 + worksheet1.write(j, i, coop.phone) + i += 1 + address = coop.street + ' ' + coop.zip + ' ' \ + + coop.city + ' ' + coop.country_id.name + worksheet1.write(j, i, address) + i += 1 + worksheet1.write(j, i, coop.street) + i += 1 + worksheet1.write(j, i, int(coop.zip)) + i += 1 + worksheet1.write(j, i, coop.city) + i += 1 + worksheet1.write(j, i, coop.country_id.name) + i += 1 + worksheet1.write(j, i, coop.number_of_share) + i += 1 + worksheet1.write(j, i, coop.total_value) + + invoice_ids = invoice_obj.search([('release_capital_request', '=', True), + ('partner_id', '=', coop.id)]) + j += 1 + for invoice in invoice_ids: + i = 11 + worksheet1.write(j, i, invoice.number) + i += 1 + worksheet1.write(j, i, invoice.state) + i += 1 + worksheet1.write(j, i, invoice.date_invoice) + i += 1 + worksheet1.write(j, i, invoice.reference) + i += 1 + for line in invoice.invoice_line_ids: + worksheet1.write(j, i, line.quantity) + i += 1 + worksheet1.write(j, i, line.price_subtotal) + i += 1 + if invoice.payment_ids: + worksheet1.write(j, i, invoice.payment_ids[0].date) + i += 1 + if invoice.subscription_request: + ind = len(invoice.subscription_request)-1 + worksheet1.write(j, i, invoice.subscription_request[ind].date) + j += 1 + + sub_requests = subscription_obj.search([('state', 'in', + ['draft', 'waiting']), + ('partner_id', '=', coop.id) + ]) + for sub_request in sub_requests: + i = 11 + worksheet1.write(j, i, dict(subscription_obj._columns['type'].selection).get(sub_request.type,False)) + i += 1 + worksheet1.write(j, i, sub_request.state) + i += 3 + quantity = int(sub_request.ordered_parts) + worksheet1.write(j, i, quantity) + i += 1 + amount = quantity * 250 + worksheet1.write(j, i, amount) + i += 2 + worksheet1.write(j, i, sub_request.date) + j += 1 + + worksheet1bis = workbook.add_worksheet() + self.write_header(worksheet1bis, HEADER) + cooperators = partner_obj.search([('cooperator', '=', True), + ('member', '=', False)]) + + j = 1 + for coop in cooperators: + i = 0 + worksheet1bis.write(j, i, coop.cooperator_register_number) + i += 1 + worksheet1bis.write(j, i, coop.national_register_number) + i += 1 + worksheet1bis.write(j, i, coop.name) + i += 1 + worksheet1bis.write(j, i, coop.email) + i += 1 + worksheet1bis.write(j, i, coop.phone) + i += 1 + worksheet1bis.write(j, i, coop.street) + i += 1 + worksheet1bis.write(j, i, int(coop.zip)) + i += 1 + worksheet1bis.write(j, i, coop.city) + i += 1 + worksheet1bis.write(j, i, coop.country_id.name) + i += 1 + worksheet1bis.write(j, i, coop.number_of_share) + i += 1 + worksheet1bis.write(j, i, coop.total_value) + + invoice_ids = invoice_obj.search([('release_capital_request', '=', True), + ('partner_id', '=', coop.id)]) + j += 1 + for invoice in invoice_ids: + i = 11 + worksheet1bis.write(j, i, invoice.number) + i += 1 + worksheet1bis.write(j, i, invoice.state) + i += 1 + worksheet1bis.write(j, i, invoice.date_invoice) + i += 1 + worksheet1bis.write(j, i, invoice.reference) + i += 1 + for line in invoice.invoice_line_ids: + worksheet1bis.write(j, i, line.quantity) + i += 1 + worksheet1bis.write(j, i, line.price_subtotal) + i += 1 + if invoice.payment_ids: + worksheet1bis.write(j, i, invoice.payment_ids[0].date) + i += 1 + if invoice.subscription_request: + ind = len(invoice.subscription_request)-1 + worksheet1bis.write(j, i, invoice.subscription_request[ind].date) + j += 1 + + sub_requests = subscription_obj.search([('state', 'in', + ['draft', 'waiting']), + ('partner_id', '=', coop.id) + ]) + for sub_request in sub_requests: + i = 11 + worksheet1bis.write(j, i, dict(subscription_obj._columns['type'].selection).get(sub_request.type,False)) + i += 1 + worksheet1bis.write(j, i, sub_request.state) + i += 3 + quantity = int(sub_request.ordered_parts) + worksheet1bis.write(j, i, quantity) + i += 1 + amount = quantity * 250 + worksheet1bis.write(j, i, amount) + i += 2 + worksheet1bis.write(j, i, sub_request.sync_date) + j += 1 + + worksheet2 = workbook.add_worksheet() + self.write_header(worksheet2, HEADER2) + sub_requests = subscription_obj.search([('state', 'in', + ['draft', 'waiting']), + ]) + + j = 1 + for sub_request in sub_requests: + i = 0 + worksheet2.write(j, i, sub_request.date) + i += 1 + worksheet2.write(j, i, sub_request.name) + i += 1 + sub_type_sel = subscription_obj._columns['type'].selection + worksheet2.write(j, i, dict(sub_type_sel).get(sub_request.type, False)) + i += 1 + quantity = int(sub_request.ordered_parts) + worksheet2.write(j, i, quantity) + i += 1 + amount = quantity * 250 + worksheet2.write(j, i, amount) + i += 1 + worksheet2.write(j, i, sub_request.state) + i += 1 + worksheet2.write(j, i, sub_request.no_registre) + i += 1 + worksheet2.write(j, i, sub_request.email) + i += 1 + worksheet2.write(j, i, sub_request.phone) + i += 1 + worksheet2.write(j, i, sub_request.address) + i += 1 + worksheet2.write(j, i, sub_request.city) + i += 1 + worksheet2.write(j, i, int(sub_request.zip_code)) + i += 1 + worksheet2.write(j, i, sub_request.country_id.name) + j += 1 + + workbook.close() + file_data.seek(0) + + data = base64.encodestring(file_data.read()) + + attachment_id = self.env['ir.attachment'].create({ + 'name': "Global export" + time.strftime('%Y-%m-%d %H:%M') + + ".xlsx", + 'datas': data, + 'datas_fname': 'Global_export.xlsx', + 'res_model': 'export.global.report', + },) + + # Prepare your download URL + download_url = '/web/content/' + str(attachment_id.id) + '?download=True' + base_url = self.env['ir.config_parameter'].get_param('web.base.url') + + return { + "type": "ir.actions.act_url", + "url": str(base_url) + str(download_url), + "target": "new", + } diff --git a/easy_my_coop_export_xlsx/wizard/export_global_wizard.xml b/easy_my_coop_export_xlsx/wizard/export_global_wizard.xml new file mode 100644 index 0000000..cd5a64d --- /dev/null +++ b/easy_my_coop_export_xlsx/wizard/export_global_wizard.xml @@ -0,0 +1,33 @@ + + + + + + export.global.report.wizard.form + export.global.report + +
+ +
+
+ +
+
+ + + Export gloabal report + ir.actions.act_window + export.global.report + form + form + + new + True + + + +
+