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.

355 lines
16 KiB

11 years ago
10 years ago
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2009-2016 Noviat
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import xlwt
  5. from datetime import datetime
  6. from openerp.osv import orm
  7. from openerp.report import report_sxw
  8. from openerp.addons.report_xls.report_xls import report_xls
  9. from openerp.addons.report_xls.utils import rowcol_to_cell, _render
  10. from openerp.tools.translate import translate, _
  11. import logging
  12. _logger = logging.getLogger(__name__)
  13. _ir_translation_name = 'move.line.list.xls'
  14. class move_line_xls_parser(report_sxw.rml_parse):
  15. def __init__(self, cr, uid, name, context):
  16. super(move_line_xls_parser, self).__init__(
  17. cr, uid, name, context=context)
  18. move_obj = self.pool.get('account.move.line')
  19. self.context = context
  20. wanted_list = move_obj._report_xls_fields(cr, uid, context)
  21. template_changes = move_obj._report_xls_template(cr, uid, context)
  22. self.localcontext.update({
  23. 'datetime': datetime,
  24. 'wanted_list': wanted_list,
  25. 'template_changes': template_changes,
  26. '_': self._,
  27. })
  28. def _(self, src):
  29. lang = self.context.get('lang', 'en_US')
  30. return translate(self.cr, _ir_translation_name, 'report', lang, src) \
  31. or src
  32. class move_line_xls(report_xls):
  33. def __init__(self, name, table, rml=False, parser=False, header=True,
  34. store=False):
  35. super(move_line_xls, self).__init__(
  36. name, table, rml, parser, header, store)
  37. # Cell Styles
  38. _xs = self.xls_styles
  39. # header
  40. rh_cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  41. self.rh_cell_style = xlwt.easyxf(rh_cell_format)
  42. self.rh_cell_style_center = xlwt.easyxf(rh_cell_format + _xs['center'])
  43. self.rh_cell_style_right = xlwt.easyxf(rh_cell_format + _xs['right'])
  44. # lines
  45. aml_cell_format = _xs['borders_all']
  46. self.aml_cell_style = xlwt.easyxf(aml_cell_format)
  47. self.aml_cell_style_center = xlwt.easyxf(
  48. aml_cell_format + _xs['center'])
  49. self.aml_cell_style_date = xlwt.easyxf(
  50. aml_cell_format + _xs['left'],
  51. num_format_str=report_xls.date_format)
  52. self.aml_cell_style_decimal = xlwt.easyxf(
  53. aml_cell_format + _xs['right'],
  54. num_format_str=report_xls.decimal_format)
  55. # totals
  56. rt_cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  57. self.rt_cell_style = xlwt.easyxf(rt_cell_format)
  58. self.rt_cell_style_right = xlwt.easyxf(rt_cell_format + _xs['right'])
  59. self.rt_cell_style_decimal = xlwt.easyxf(
  60. rt_cell_format + _xs['right'],
  61. num_format_str=report_xls.decimal_format)
  62. # XLS Template
  63. self.col_specs_template = {
  64. 'move': {
  65. 'header': [1, 20, 'text', _render("_('Entry')")],
  66. 'lines': [1, 0, 'text', _render("line.move_id.name or ''")],
  67. 'totals': [1, 0, 'text', None]},
  68. 'name': {
  69. 'header': [1, 42, 'text', _render("_('Name')")],
  70. 'lines': [1, 0, 'text', _render("line.name or ''")],
  71. 'totals': [1, 0, 'text', None]},
  72. 'ref': {
  73. 'header': [1, 42, 'text', _render("_('Reference')")],
  74. 'lines': [1, 0, 'text', _render("line.ref or ''")],
  75. 'totals': [1, 0, 'text', None]},
  76. 'date': {
  77. 'header': [1, 13, 'text', _render("_('Effective Date')")],
  78. 'lines': [1, 0, 'date',
  79. _render("datetime.strptime(line.date,'%Y-%m-%d')"),
  80. None, self.aml_cell_style_date],
  81. 'totals': [1, 0, 'text', None]},
  82. 'period': {
  83. 'header': [1, 12, 'text', _render("_('Period')")],
  84. 'lines':
  85. [1, 0, 'text',
  86. _render("line.period_id.code or line.period_id.name")],
  87. 'totals': [1, 0, 'text', None]},
  88. 'partner': {
  89. 'header': [1, 36, 'text', _render("_('Partner')")],
  90. 'lines':
  91. [1, 0, 'text',
  92. _render("line.partner_id and line.partner_id.name or ''")],
  93. 'totals': [1, 0, 'text', None]},
  94. 'partner_ref': {
  95. 'header': [1, 36, 'text', _render("_('Partner Reference')")],
  96. 'lines':
  97. [1, 0, 'text',
  98. _render("line.partner_id and line.partner_id.ref or ''")],
  99. 'totals': [1, 0, 'text', None]},
  100. 'account': {
  101. 'header': [1, 12, 'text', _render("_('Account')")],
  102. 'lines': [1, 0, 'text', _render("line.account_id.code")],
  103. 'totals': [1, 0, 'text', None]},
  104. 'date_maturity': {
  105. 'header': [1, 13, 'text', _render("_('Maturity Date')")],
  106. 'lines':
  107. [1, 0,
  108. _render("line.date_maturity and 'date' or 'text'"),
  109. _render(
  110. "line.date_maturity"
  111. " and datetime.strptime(line.date_maturity,'%Y-%m-%d')"
  112. " or None"),
  113. None, self.aml_cell_style_date],
  114. 'totals': [1, 0, 'text', None]},
  115. 'debit': {
  116. 'header': [1, 18, 'text', _render("_('Debit')"), None,
  117. self.rh_cell_style_right],
  118. 'lines': [1, 0, 'number', _render("line.debit"), None,
  119. self.aml_cell_style_decimal],
  120. 'totals': [1, 0, 'number', None, _render("debit_formula"),
  121. self.rt_cell_style_decimal]},
  122. 'credit': {
  123. 'header': [1, 18, 'text', _render("_('Credit')"), None,
  124. self.rh_cell_style_right],
  125. 'lines': [1, 0, 'number', _render("line.credit"), None,
  126. self.aml_cell_style_decimal],
  127. 'totals': [1, 0, 'number', None, _render("credit_formula"),
  128. self.rt_cell_style_decimal]},
  129. 'balance': {
  130. 'header': [1, 18, 'text', _render("_('Balance')"), None,
  131. self.rh_cell_style_right],
  132. 'lines': [1, 0, 'number', None, _render("bal_formula"),
  133. self.aml_cell_style_decimal],
  134. 'totals': [1, 0, 'number', None, _render("bal_formula"),
  135. self.rt_cell_style_decimal]},
  136. 'reconcile': {
  137. 'header': [1, 12, 'text', _render("_('Rec.')"), None,
  138. self.rh_cell_style_center],
  139. 'lines': [1, 0, 'text',
  140. _render("line.reconcile_id.name or ''"), None,
  141. self.aml_cell_style_center],
  142. 'totals': [1, 0, 'text', None]},
  143. 'reconcile_partial': {
  144. 'header': [1, 12, 'text', _render("_('Part. Rec.')"), None,
  145. self.rh_cell_style_center],
  146. 'lines': [1, 0, 'text',
  147. _render("line.reconcile_partial_id.name or ''"),
  148. None, self.aml_cell_style_center],
  149. 'totals': [1, 0, 'text', None]},
  150. 'tax_code': {
  151. 'header': [1, 12, 'text', _render("_('Tax Code')"), None,
  152. self.rh_cell_style_center],
  153. 'lines': [1, 0, 'text', _render("line.tax_code_id.code or ''"),
  154. None, self.aml_cell_style_center],
  155. 'totals': [1, 0, 'text', None]},
  156. 'tax_amount': {
  157. 'header': [1, 18, 'text', _render("_('Tax/Base Amount')"),
  158. None, self.rh_cell_style_right],
  159. 'lines': [1, 0, 'number', _render("line.tax_amount"), None,
  160. self.aml_cell_style_decimal],
  161. 'totals': [1, 0, 'text', None]},
  162. 'amount_currency': {
  163. 'header': [1, 18, 'text', _render("_('Am. Currency')"), None,
  164. self.rh_cell_style_right],
  165. 'lines':
  166. [1, 0,
  167. _render("line.amount_currency and 'number' or 'text'"),
  168. _render("line.amount_currency or None"),
  169. None, self.aml_cell_style_decimal],
  170. 'totals': [1, 0, 'text', None]},
  171. 'currency_name': {
  172. 'header': [1, 6, 'text', _render("_('Curr.')"), None,
  173. self.rh_cell_style_center],
  174. 'lines':
  175. [1, 0, 'text',
  176. _render("line.currency_id and line.currency_id.name or ''"),
  177. None, self.aml_cell_style_center],
  178. 'totals': [1, 0, 'text', None]},
  179. 'journal': {
  180. 'header': [1, 12, 'text', _render("_('Journal')")],
  181. 'lines': [1, 0, 'text', _render("line.journal_id.code or ''")],
  182. 'totals': [1, 0, 'text', None]},
  183. 'company_currency': {
  184. 'header': [1, 10, 'text', _render("_('Comp. Curr.')")],
  185. 'lines': [1, 0, 'text',
  186. _render("line.company_id.currency_id.name or ''"),
  187. None, self.aml_cell_style_center],
  188. 'totals': [1, 0, 'text', None]},
  189. 'analytic_account': {
  190. 'header': [1, 36, 'text', _render("_('Analytic Account')")],
  191. 'lines': [1, 0, 'text',
  192. _render("line.analytic_account_id.code or ''")],
  193. 'totals': [1, 0, 'text', None]},
  194. 'product': {
  195. 'header': [1, 36, 'text', _render("_('Product')")],
  196. 'lines': [1, 0, 'text', _render("line.product_id.name or ''")],
  197. 'totals': [1, 0, 'text', None]},
  198. 'product_ref': {
  199. 'header': [1, 36, 'text', _render("_('Product Reference')")],
  200. 'lines': [1, 0, 'text',
  201. _render("line.product_id.default_code or ''")],
  202. 'totals': [1, 0, 'text', None]},
  203. 'product_uom': {
  204. 'header': [1, 20, 'text', _render("_('Unit of Measure')")],
  205. 'lines': [1, 0, 'text',
  206. _render("line.product_uom_id.name or ''")],
  207. 'totals': [1, 0, 'text', None]},
  208. 'quantity': {
  209. 'header': [1, 8, 'text', _render("_('Qty')"), None,
  210. self.rh_cell_style_right],
  211. 'lines': [1, 0,
  212. _render("line.quantity and 'number' or 'text'"),
  213. _render("line.quantity or None"), None,
  214. self.aml_cell_style_decimal],
  215. 'totals': [1, 0, 'text', None]},
  216. 'statement': {
  217. 'header': [1, 20, 'text', _render("_('Statement')")],
  218. 'lines':
  219. [1, 0, 'text',
  220. _render("line.statement_id and line.statement_id.name or ''")
  221. ],
  222. 'totals': [1, 0, 'text', None]},
  223. 'invoice': {
  224. 'header': [1, 20, 'text', _render("_('Invoice')")],
  225. 'lines':
  226. [1, 0, 'text',
  227. _render("line.invoice and line.invoice.number or ''")],
  228. 'totals': [1, 0, 'text', None]},
  229. 'amount_residual': {
  230. 'header': [1, 18, 'text', _render("_('Residual Amount')"),
  231. None, self.rh_cell_style_right],
  232. 'lines':
  233. [1, 0,
  234. _render("line.amount_residual and 'number' or 'text'"),
  235. _render("line.amount_residual or None"),
  236. None, self.aml_cell_style_decimal],
  237. 'totals': [1, 0, 'text', None]},
  238. 'amount_residual_currency': {
  239. 'header': [1, 18, 'text', _render("_('Res. Am. in Curr.')"),
  240. None, self.rh_cell_style_right],
  241. 'lines':
  242. [1, 0,
  243. _render(
  244. "line.amount_residual_currency and 'number' or 'text'"),
  245. _render("line.amount_residual_currency or None"),
  246. None, self.aml_cell_style_decimal],
  247. 'totals': [1, 0, 'text', None]},
  248. 'narration': {
  249. 'header': [1, 42, 'text', _render("_('Notes')")],
  250. 'lines': [1, 0, 'text',
  251. _render("line.move_id.narration or ''")],
  252. 'totals': [1, 0, 'text', None]},
  253. 'blocked': {
  254. 'header': [1, 4, 'text', _('Lit.'),
  255. None, self.rh_cell_style_right],
  256. 'lines': [1, 0, 'text', _render("line.blocked and 'x' or ''"),
  257. None, self.aml_cell_style_center],
  258. 'totals': [1, 0, 'text', None]},
  259. }
  260. def generate_xls_report(self, _p, _xs, data, objects, wb):
  261. wanted_list = _p.wanted_list
  262. self.col_specs_template.update(_p.template_changes)
  263. _ = _p._
  264. debit_pos = 'debit' in wanted_list and wanted_list.index('debit')
  265. credit_pos = 'credit' in wanted_list and wanted_list.index('credit')
  266. if not (credit_pos and debit_pos) and 'balance' in wanted_list:
  267. raise orm.except_orm(
  268. _('Customisation Error!'),
  269. _("The 'Balance' field is a calculated XLS field requiring \
  270. the presence of the 'Debit' and 'Credit' fields !"))
  271. # report_name = objects[0]._description or objects[0]._name
  272. report_name = _("Journal Items")
  273. ws = wb.add_sheet(report_name[:31])
  274. ws.panes_frozen = True
  275. ws.remove_splits = True
  276. ws.portrait = 0 # Landscape
  277. ws.fit_width_to_pages = 1
  278. row_pos = 0
  279. # set print header/footer
  280. ws.header_str = self.xls_headers['standard']
  281. ws.footer_str = self.xls_footers['standard']
  282. # Title
  283. cell_style = xlwt.easyxf(_xs['xls_title'])
  284. c_specs = [
  285. ('report_name', 1, 0, 'text', report_name),
  286. ]
  287. row_data = self.xls_row_template(c_specs, ['report_name'])
  288. row_pos = self.xls_write_row(
  289. ws, row_pos, row_data, row_style=cell_style)
  290. row_pos += 1
  291. # Column headers
  292. c_specs = map(lambda x: self.render(
  293. x, self.col_specs_template, 'header', render_space={'_': _p._}),
  294. wanted_list)
  295. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  296. row_pos = self.xls_write_row(
  297. ws, row_pos, row_data, row_style=self.rh_cell_style,
  298. set_column_size=True)
  299. ws.set_horz_split_pos(row_pos)
  300. # account move lines
  301. for line in objects:
  302. debit_cell = rowcol_to_cell(row_pos, debit_pos)
  303. credit_cell = rowcol_to_cell(row_pos, credit_pos)
  304. bal_formula = debit_cell + '-' + credit_cell
  305. _logger.debug('dummy call - %s', bal_formula)
  306. c_specs = map(
  307. lambda x: self.render(x, self.col_specs_template, 'lines'),
  308. wanted_list)
  309. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  310. row_pos = self.xls_write_row(
  311. ws, row_pos, row_data, row_style=self.aml_cell_style)
  312. # Totals
  313. aml_cnt = len(objects)
  314. debit_start = rowcol_to_cell(row_pos - aml_cnt, debit_pos)
  315. debit_stop = rowcol_to_cell(row_pos - 1, debit_pos)
  316. debit_formula = 'SUM(%s:%s)' % (debit_start, debit_stop)
  317. _logger.debug('dummy call - %s', debit_formula)
  318. credit_start = rowcol_to_cell(row_pos - aml_cnt, credit_pos)
  319. credit_stop = rowcol_to_cell(row_pos - 1, credit_pos)
  320. credit_formula = 'SUM(%s:%s)' % (credit_start, credit_stop)
  321. _logger.debug('dummy call - %s', credit_formula)
  322. debit_cell = rowcol_to_cell(row_pos, debit_pos)
  323. credit_cell = rowcol_to_cell(row_pos, credit_pos)
  324. bal_formula = debit_cell + '-' + credit_cell
  325. _logger.debug('dummy call - %s', bal_formula)
  326. c_specs = map(
  327. lambda x: self.render(x, self.col_specs_template, 'totals'),
  328. wanted_list)
  329. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  330. row_pos = self.xls_write_row(
  331. ws, row_pos, row_data, row_style=self.rt_cell_style_right)
  332. move_line_xls('report.move.line.list.xls',
  333. 'account.move.line',
  334. parser=move_line_xls_parser)