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.

287 lines
15 KiB

11 years ago
11 years ago
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. #
  6. # Copyright (c) 2014 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, 12, 'text', _render("_('Journal')")],
  151. 'lines': [1, 0, 'text', _render("line.journal_id.code or ''")],
  152. 'totals': [1, 0, 'text', None]},
  153. 'company_currency': {
  154. 'header': [1, 10, 'text', _render("_('Comp. Curr.')")],
  155. 'lines': [1, 0, 'text', _render("line.company_id.currency_id.name or ''"), None, self.aml_cell_style_center],
  156. 'totals': [1, 0, 'text', None]},
  157. 'analytic_account': {
  158. 'header': [1, 36, 'text', _render("_('Analytic Account')")],
  159. 'lines': [1, 0, 'text', _render("line.analytic_account_id.code or ''")],
  160. 'totals': [1, 0, 'text', None]},
  161. 'product': {
  162. 'header': [1, 36, 'text', _render("_('Product')")],
  163. 'lines': [1, 0, 'text', _render("line.product_id.name or ''")],
  164. 'totals': [1, 0, 'text', None]},
  165. 'product_ref': {
  166. 'header': [1, 36, 'text', _render("_('Product Reference')")],
  167. 'lines': [1, 0, 'text', _render("line.product_id.default_code or ''")],
  168. 'totals': [1, 0, 'text', None]},
  169. 'product_uom': {
  170. 'header': [1, 20, 'text', _render("_('Unit of Measure')")],
  171. 'lines': [1, 0, 'text', _render("line.product_uom_id.name or ''")],
  172. 'totals': [1, 0, 'text', None]},
  173. 'quantity': {
  174. 'header': [1, 8, 'text', _render("_('Qty')"), None, self.rh_cell_style_right],
  175. 'lines': [1, 0, _render("line.quantity and 'number' or 'text'"),
  176. _render("line.quantity or None"), None, self.aml_cell_style_decimal],
  177. 'totals': [1, 0, 'text', None]},
  178. 'statement': {
  179. 'header': [1, 20, 'text', _render("_('Statement')")],
  180. 'lines': [1, 0, 'text', _render("line.statement_id and line.statement_id.name or ''")],
  181. 'totals': [1, 0, 'text', None]},
  182. 'invoice': {
  183. 'header': [1, 20, 'text', _render("_('Invoice')")],
  184. 'lines': [1, 0, 'text', _render("line.invoice and line.invoice.number or ''")],
  185. 'totals': [1, 0, 'text', None]},
  186. 'amount_residual': {
  187. 'header': [1, 18, 'text', _render("_('Residual Amount')"), None, self.rh_cell_style_right],
  188. 'lines': [1, 0, _render("line.amount_residual and 'number' or 'text'"),
  189. _render("line.amount_residual or None"),
  190. None, self.aml_cell_style_decimal],
  191. 'totals': [1, 0, 'text', None]},
  192. 'amount_residual_currency': {
  193. 'header': [1, 18, 'text', _render("_('Res. Am. in Curr.')"), None, self.rh_cell_style_right],
  194. 'lines': [1, 0, _render("line.amount_residual_currency and 'number' or 'text'"),
  195. _render("line.amount_residual_currency or None"),
  196. None, self.aml_cell_style_decimal],
  197. 'totals': [1, 0, 'text', None]},
  198. 'narration': {
  199. 'header': [1, 42, 'text', _render("_('Notes')")],
  200. 'lines': [1, 0, 'text', _render("line.move_id.narration or ''")],
  201. 'totals': [1, 0, 'text', None]},
  202. 'blocked': {
  203. 'header': [1, 4, 'text', _('Lit.'), None, self.rh_cell_style_right],
  204. 'lines': [1, 0, 'text', _render("line.blocked and 'x' or ''"), None, self.aml_cell_style_center],
  205. 'totals': [1, 0, 'text', None]},
  206. }
  207. def generate_xls_report(self, _p, _xs, data, objects, wb):
  208. wanted_list = _p.wanted_list
  209. self.col_specs_template.update(_p.template_changes)
  210. _ = _p._
  211. debit_pos = 'debit' in wanted_list and wanted_list.index('debit')
  212. credit_pos = 'credit' in wanted_list and wanted_list.index('credit')
  213. if not (credit_pos and debit_pos) and 'balance' in wanted_list:
  214. raise orm.except_orm(_('Customisation Error!'),
  215. _("The 'Balance' field is a calculated XLS field requiring the presence of the 'Debit' and 'Credit' fields !"))
  216. #report_name = objects[0]._description or objects[0]._name
  217. report_name = _("Journal Items")
  218. ws = wb.add_sheet(report_name[:31])
  219. ws.panes_frozen = True
  220. ws.remove_splits = True
  221. ws.portrait = 0 # Landscape
  222. ws.fit_width_to_pages = 1
  223. row_pos = 0
  224. # set print header/footer
  225. ws.header_str = self.xls_headers['standard']
  226. ws.footer_str = self.xls_footers['standard']
  227. # Title
  228. cell_style = xlwt.easyxf(_xs['xls_title'])
  229. c_specs = [
  230. ('report_name', 1, 0, 'text', report_name),
  231. ]
  232. row_data = self.xls_row_template(c_specs, ['report_name'])
  233. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style)
  234. row_pos += 1
  235. # Column headers
  236. c_specs = map(lambda x: self.render(x, self.col_specs_template, 'header', render_space={'_': _p._}), wanted_list)
  237. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  238. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.rh_cell_style, set_column_size=True)
  239. ws.set_horz_split_pos(row_pos)
  240. # account move lines
  241. for line in objects:
  242. debit_cell = rowcol_to_cell(row_pos, debit_pos)
  243. credit_cell = rowcol_to_cell(row_pos, credit_pos)
  244. bal_formula = debit_cell + '-' + credit_cell
  245. c_specs = map(lambda x: self.render(x, self.col_specs_template, 'lines'), wanted_list)
  246. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  247. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.aml_cell_style)
  248. # Totals
  249. aml_cnt = len(objects)
  250. debit_start = rowcol_to_cell(row_pos - aml_cnt, debit_pos)
  251. debit_stop = rowcol_to_cell(row_pos - 1, debit_pos)
  252. debit_formula = 'SUM(%s:%s)' % (debit_start, debit_stop)
  253. credit_start = rowcol_to_cell(row_pos - aml_cnt, credit_pos)
  254. credit_stop = rowcol_to_cell(row_pos - 1, credit_pos)
  255. credit_formula = 'SUM(%s:%s)' % (credit_start, credit_stop)
  256. debit_cell = rowcol_to_cell(row_pos, debit_pos)
  257. credit_cell = rowcol_to_cell(row_pos, credit_pos)
  258. bal_formula = debit_cell + '-' + credit_cell
  259. c_specs = map(lambda x: self.render(x, self.col_specs_template, 'totals'), wanted_list)
  260. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  261. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=self.rt_cell_style_right)
  262. move_line_xls('report.move.line.list.xls',
  263. 'account.move.line',
  264. parser=move_line_xls_parser)
  265. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: