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.

239 lines
13 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. #
  6. # Copyright (c) 2013 Noviat nv/sa (www.noviat.com). All rights reserved.
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. import xlwt
  23. import time
  24. from datetime import datetime
  25. from openerp.osv import orm
  26. from openerp.report import report_sxw
  27. from openerp.addons.report_xls.report_xls import report_xls
  28. from openerp.addons.report_xls.utils import rowcol_to_cell, _render
  29. from openerp.tools.translate import translate, _
  30. from openerp import pooler
  31. import logging
  32. _logger = logging.getLogger(__name__)
  33. _ir_translation_name = 'move.line.list.xls'
  34. class move_line_xls_parser(report_sxw.rml_parse):
  35. def __init__(self, cr, uid, name, context):
  36. super(move_line_xls_parser, self).__init__(cr, uid, name, context=context)
  37. move_obj = self.pool.get('account.move.line')
  38. self.context = context
  39. wanted_list = move_obj._report_xls_fields(cr, uid, context)
  40. template_changes = move_obj._report_xls_template(cr, uid, context)
  41. self.localcontext.update({
  42. 'datetime': datetime,
  43. 'wanted_list': wanted_list,
  44. 'template_changes': template_changes,
  45. '_': self._,
  46. })
  47. def _(self, src):
  48. lang = self.context.get('lang', 'en_US')
  49. return translate(self.cr, _ir_translation_name, 'report', lang, src) or src
  50. class move_line_xls(report_xls):
  51. def __init__(self, name, table, rml=False, parser=False, header=True, store=False):
  52. super(move_line_xls, self).__init__(name, table, rml, parser, header, store)
  53. # Cell Styles
  54. _xs = self.xls_styles
  55. # header
  56. rh_cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  57. self.rh_cell_style = xlwt.easyxf(rh_cell_format)
  58. self.rh_cell_style_center = xlwt.easyxf(rh_cell_format + _xs['center'])
  59. self.rh_cell_style_right = xlwt.easyxf(rh_cell_format + _xs['right'])
  60. # lines
  61. aml_cell_format = _xs['borders_all']
  62. self.aml_cell_style = xlwt.easyxf(aml_cell_format)
  63. self.aml_cell_style_center = xlwt.easyxf(aml_cell_format + _xs['center'])
  64. self.aml_cell_style_date = xlwt.easyxf(aml_cell_format + _xs['left'], num_format_str = report_xls.date_format)
  65. self.aml_cell_style_decimal = xlwt.easyxf(aml_cell_format + _xs['right'], num_format_str = report_xls.decimal_format)
  66. # totals
  67. rt_cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  68. self.rt_cell_style = xlwt.easyxf(rt_cell_format)
  69. self.rt_cell_style_right = xlwt.easyxf(rt_cell_format + _xs['right'])
  70. self.rt_cell_style_decimal = xlwt.easyxf(rt_cell_format + _xs['right'], num_format_str = report_xls.decimal_format)
  71. # XLS Template
  72. self.col_specs_template = {
  73. 'move':{
  74. 'header': [1, 20, 'text', _render("_('Entry')")],
  75. 'lines': [1, 0, 'text', _render("line.move_id.name or ''")],
  76. 'totals': [1, 0, 'text', None]},
  77. 'name':{
  78. 'header': [1, 42, 'text', _render("_('Name')")],
  79. 'lines': [1, 0, 'text', _render("line.name or ''")],
  80. 'totals': [1, 0, 'text', None]},
  81. 'ref':{
  82. 'header': [1, 42, 'text', _render("_('Reference')")],
  83. 'lines': [1, 0, 'text', _render("line.ref or ''")],
  84. 'totals': [1, 0, 'text', None]},
  85. 'date':{
  86. 'header': [1, 13, 'text', _render("_('Effective Date')")],
  87. 'lines': [1, 0, 'date', _render("datetime.strptime(line.date,'%Y-%m-%d')"), None, self.aml_cell_style_date],
  88. 'totals': [1, 0, 'text', None]},
  89. 'period':{
  90. 'header': [1, 12, 'text', _render("_('Period')")],
  91. 'lines': [1, 0, 'text', _render("line.period_id.code or line.period_id.name")],
  92. 'totals': [1, 0, 'text', None]},
  93. 'partner':{
  94. 'header': [1, 36, 'text', _render("_('Partner')")],
  95. 'lines': [1, 0, 'text', _render("line.partner_id and line.partner_id.name or ''")],
  96. 'totals': [1, 0, 'text', None]},
  97. 'partner_ref':{
  98. 'header': [1, 36, 'text', _render("_('Partner Reference')")],
  99. 'lines': [1, 0, 'text', _render("line.partner_id and line.partner_id.ref or ''")],
  100. 'totals': [1, 0, 'text', None]},
  101. 'account':{
  102. 'header': [1, 12, 'text', _render("_('Account')")],
  103. 'lines': [1, 0, 'text', _render("line.account_id.code")],
  104. 'totals': [1, 0, 'text', None]},
  105. 'date_maturity':{
  106. 'header': [1, 13, 'text', _render("_('Maturity Date')")],
  107. 'lines': [1, 0, _render("line.date_maturity.val and 'date' or 'text'"),
  108. _render("line.date_maturity.val and datetime.strptime(line.date_maturity,'%Y-%m-%d') or None"),
  109. None, self.aml_cell_style_date],
  110. 'totals': [1, 0, 'text', None]},
  111. 'debit':{
  112. 'header': [1, 18, 'text', _render("_('Debit')"), None, self.rh_cell_style_right],
  113. 'lines': [1, 0, 'number', _render("line.debit"), None, self.aml_cell_style_decimal],
  114. 'totals': [1, 0, 'number', None, _render("debit_formula"), self.rt_cell_style_decimal]},
  115. 'credit':{
  116. 'header': [1, 18, 'text', _render("_('Credit')"), None, self.rh_cell_style_right],
  117. 'lines': [1, 0, 'number', _render("line.credit"), None, self.aml_cell_style_decimal],
  118. 'totals': [1, 0, 'number', None, _render("credit_formula"), self.rt_cell_style_decimal]},
  119. 'balance':{
  120. 'header': [1, 18, 'text', _render("_('Balance')"), None, self.rh_cell_style_right],
  121. 'lines': [1, 0, 'number', None, _render("bal_formula"), self.aml_cell_style_decimal],
  122. 'totals': [1, 0, 'number', None, _render("bal_formula"), self.rt_cell_style_decimal]},
  123. 'reconcile':{
  124. 'header': [1, 12, 'text', _render("_('Rec.')"), None, self.rh_cell_style_center],
  125. 'lines': [1, 0, 'text', _render("line.reconcile_id.name or ''"), None, self.aml_cell_style_center],
  126. 'totals': [1, 0, 'text', None]},
  127. 'reconcile_partial':{
  128. 'header': [1, 12, 'text', _render("_('Part. Rec.')"), None, self.rh_cell_style_center],
  129. 'lines': [1, 0, 'text', _render("line.reconcile_partial_id.name or ''"), None, self.aml_cell_style_center],
  130. 'totals': [1, 0, 'text', None]},
  131. 'tax_code':{
  132. 'header': [1, 12, 'text', _render("_('Tax Code')"), None, self.rh_cell_style_center],
  133. 'lines': [1, 0, 'text', _render("line.tax_code_id.code or ''"), None, self.aml_cell_style_center],
  134. 'totals': [1, 0, 'text', None]},
  135. 'tax_amount':{
  136. 'header': [1, 18, 'text', _render("_('Tax/Base Amount')"), None, self.rh_cell_style_right],
  137. 'lines': [1, 0, 'number', _render("line.tax_amount"), None, self.aml_cell_style_decimal],
  138. 'totals': [1, 0, 'text', None]},
  139. 'amount_currency':{
  140. 'header': [1, 18, 'text', _render("_('Am. Currency')"), None, self.rh_cell_style_right],
  141. 'lines': [1, 0, _render("line.amount_currency and 'number' or 'text'"),
  142. _render("line.amount_currency or None"),
  143. None, self.aml_cell_style_decimal],
  144. 'totals': [1, 0, 'text', None]},
  145. 'currency_name':{
  146. 'header': [1, 6, 'text', _render("_('Curr.')"), None, self.rh_cell_style_center],
  147. 'lines': [1, 0, 'text', _render("line.currency_id and line.currency_id.name or ''"), None, self.aml_cell_style_center],
  148. 'totals': [1, 0, 'text', None]},
  149. 'journal': {
  150. 'header': [1, 6, 'text', _('Journal')],
  151. 'lines': [1, 0, 'text', _render("line.journal_id.code or ''")],
  152. 'totals': [1, 0, 'text', None]},
  153. 'company_currency': {
  154. 'header': [1, 6, 'text', _('Company currency')],
  155. 'lines': [1, 0, 'text', _render("line.company_id.currency_id.name or ''")],
  156. 'totals': [1, 0, 'text', None]},
  157. 'analytic_account': {
  158. 'header': [1, 6, 'text', _('Analytic Account')],
  159. 'lines': [1, 0, 'text', _render("line.analytic_account_id.code or ''")],
  160. 'totals': [1, 0, 'text', None]},
  161. }
  162. def generate_xls_report(self, _p, _xs, data, objects, wb):
  163. wanted_list = _p.wanted_list
  164. self.col_specs_template.update(_p.template_changes)
  165. _ = _p._
  166. debit_pos = 'debit' in wanted_list and wanted_list.index('debit')
  167. credit_pos = 'credit' in wanted_list and wanted_list.index('credit')
  168. if not (credit_pos and debit_pos) and 'balance' in wanted_list:
  169. raise orm.except_orm(_('Customisation Error!'),
  170. _("The 'Balance' field is a calculated XLS field requiring the presence of the 'Debit' and 'Credit' fields !"))
  171. #report_name = objects[0]._description or objects[0]._name
  172. report_name = _("Journal Items")
  173. ws = wb.add_sheet(report_name[:31])
  174. ws.panes_frozen = True
  175. ws.remove_splits = True
  176. ws.portrait = 0 # Landscape
  177. ws.fit_width_to_pages = 1
  178. row_pos = 0
  179. # set print header/footer
  180. ws.header_str = self.xls_headers['standard']
  181. ws.footer_str = self.xls_footers['standard']
  182. # Title
  183. cell_style = xlwt.easyxf(_xs['xls_title'])
  184. c_specs = [
  185. ('report_name', 1, 0, 'text', report_name),
  186. ]
  187. row_data = self.xls_row_template(c_specs, ['report_name'])
  188. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style)
  189. row_pos += 1
  190. # Column headers
  191. c_specs = map(lambda x: self.render(x, self.col_specs_template, 'header', render_space={'_': _p._}), wanted_list)
  192. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  193. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.rh_cell_style, set_column_size=True)
  194. ws.set_horz_split_pos(row_pos)
  195. # account move lines
  196. for line in objects:
  197. debit_cell = rowcol_to_cell(row_pos, debit_pos)
  198. credit_cell = rowcol_to_cell(row_pos, credit_pos)
  199. bal_formula = debit_cell + '-' + credit_cell
  200. c_specs = map(lambda x: self.render(x, self.col_specs_template, 'lines'), wanted_list)
  201. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  202. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.aml_cell_style)
  203. # Totals
  204. aml_cnt = len(objects)
  205. debit_start = rowcol_to_cell(row_pos - aml_cnt, debit_pos)
  206. debit_stop = rowcol_to_cell(row_pos - 1, debit_pos)
  207. debit_formula = 'SUM(%s:%s)' %(debit_start, debit_stop)
  208. credit_start = rowcol_to_cell(row_pos - aml_cnt, credit_pos)
  209. credit_stop = rowcol_to_cell(row_pos - 1, credit_pos)
  210. credit_formula = 'SUM(%s:%s)' %(credit_start, credit_stop)
  211. debit_cell = rowcol_to_cell(row_pos, debit_pos)
  212. credit_cell = rowcol_to_cell(row_pos, credit_pos)
  213. bal_formula = debit_cell + '-' + credit_cell
  214. c_specs = map(lambda x: self.render(x, self.col_specs_template, 'totals'), wanted_list)
  215. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  216. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.rt_cell_style_right)
  217. move_line_xls('report.move.line.list.xls',
  218. 'account.move.line',
  219. parser=move_line_xls_parser)
  220. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: