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.

286 lines
15 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.report import report_sxw
  26. from openerp.addons.report_xls.report_xls import report_xls
  27. from openerp.addons.report_xls.utils import rowcol_to_cell
  28. from openerp.addons.account_financial_report_webkit.report.general_ledger import GeneralLedgerWebkit
  29. from openerp.tools.translate import _
  30. #import logging
  31. #_logger = logging.getLogger(__name__)
  32. _column_sizes = [
  33. ('date', 12),
  34. ('period', 12),
  35. ('move', 20),
  36. ('journal', 12),
  37. ('account_code', 12),
  38. ('partner', 30),
  39. ('label', 45),
  40. ('counterpart', 30),
  41. ('debit', 15),
  42. ('credit', 15),
  43. ('cumul_bal', 15),
  44. ('curr_bal', 15),
  45. ('curr_code', 7),
  46. ]
  47. class general_ledger_xls(report_xls):
  48. column_sizes = [x[1] for x in _column_sizes]
  49. def generate_xls_report(self, _p, _xs, data, objects, wb):
  50. ws = wb.add_sheet(_p.report_name[:31])
  51. ws.panes_frozen = True
  52. ws.remove_splits = True
  53. ws.portrait = 0 # Landscape
  54. ws.fit_width_to_pages = 1
  55. row_pos = 0
  56. # set print header/footer
  57. ws.header_str = self.xls_headers['standard']
  58. ws.footer_str = self.xls_footers['standard']
  59. # cf. account_report_general_ledger.mako
  60. initial_balance_text = {'initial_balance': _('Computed'), 'opening_balance': _('Opening Entries'), False: _('No')}
  61. # Title
  62. cell_style = xlwt.easyxf(_xs['xls_title'])
  63. report_name = ' - '.join([_p.report_name.upper(), _p.company.partner_id.name, _p.company.currency_id.name])
  64. c_specs = [
  65. ('report_name', 1, 0, 'text', report_name),
  66. ]
  67. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  68. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style)
  69. # write empty row to define column sizes
  70. c_sizes = self.column_sizes
  71. c_specs = [('empty%s'%i, 1, c_sizes[i], 'text', None) for i in range(0,len(c_sizes))]
  72. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  73. row_pos = self.xls_write_row(ws, row_pos, row_data, set_column_size=True)
  74. # Header Table
  75. cell_format = _xs['bold'] + _xs['fill_blue'] + _xs['borders_all']
  76. cell_style = xlwt.easyxf(cell_format)
  77. cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
  78. c_specs = [
  79. ('coa', 2, 0, 'text', _('Chart of Account')),
  80. ('fy', 1, 0, 'text', _('Fiscal Year')),
  81. ('df', 3, 0, 'text', _p.filter_form(data) == 'filter_date' and _('Dates Filter') or _('Periods Filter')),
  82. ('af', 1, 0, 'text', _('Accounts Filter')),
  83. ('tm', 2, 0, 'text', _('Target Moves')),
  84. ('ib', 2, 0, 'text', _('Initial Balance')),
  85. ]
  86. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  87. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style_center)
  88. cell_format = _xs['borders_all']
  89. cell_style = xlwt.easyxf(cell_format)
  90. cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
  91. c_specs = [
  92. ('coa', 2, 0, 'text', _p.chart_account.name),
  93. ('fy', 1, 0, 'text', _p.fiscalyear.name if _p.fiscalyear else '-'),
  94. ]
  95. df = _('From') + ': '
  96. if _p.filter_form(data) == 'filter_date':
  97. df += _p.start_date if _p.start_date else u''
  98. else:
  99. df += _p.start_period.name if _p.start_period else u''
  100. df += ' ' + _('To') + ': '
  101. if _p.filter_form(data) == 'filter_date':
  102. df += _p.stop_date if _p.stop_date else u''
  103. else:
  104. df += _p.stop_period.name if _p.stop_period else u''
  105. c_specs += [
  106. ('df', 3, 0, 'text', df),
  107. ('af', 1, 0, 'text', _p.accounts(data) and ', '.join([account.code for account in _p.accounts(data)]) or _('All')),
  108. ('tm', 2, 0, 'text', _p.display_target_move(data)),
  109. ('ib', 2, 0, 'text', initial_balance_text[_p.initial_balance_mode]),
  110. ]
  111. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  112. row_pos = self.xls_write_row(ws, row_pos, row_data, row_style=cell_style_center)
  113. ws.set_horz_split_pos(row_pos)
  114. row_pos += 1
  115. # Column Title Row
  116. cell_format = _xs['bold']
  117. c_title_cell_style = xlwt.easyxf(cell_format)
  118. # Column Header Row
  119. cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  120. c_hdr_cell_style = xlwt.easyxf(cell_format)
  121. c_hdr_cell_style_right = xlwt.easyxf(cell_format + _xs['right'])
  122. c_hdr_cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
  123. c_hdr_cell_style_decimal = xlwt.easyxf(cell_format + _xs['right'], num_format_str = report_xls.decimal_format)
  124. # Column Initial Balance Row
  125. cell_format = _xs['italic'] + _xs['borders_all']
  126. c_init_cell_style = xlwt.easyxf(cell_format)
  127. c_init_cell_style_right = xlwt.easyxf(cell_format + _xs['right'])
  128. c_init_cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
  129. c_init_cell_style_decimal = xlwt.easyxf(cell_format + _xs['right'], num_format_str = report_xls.decimal_format)
  130. c_specs = [
  131. ('date', 1, 0, 'text', _('Date'), None, c_hdr_cell_style),
  132. ('period', 1, 0, 'text', _('Period'), None, c_hdr_cell_style),
  133. ('move', 1, 0, 'text', _('Entry'), None, c_hdr_cell_style),
  134. ('journal', 1, 0, 'text', _('Journal'), None, c_hdr_cell_style),
  135. ('account_code', 1, 0, 'text', _('Account'), None, c_hdr_cell_style),
  136. ('partner', 1, 0, 'text', _('Partner'), None, c_hdr_cell_style),
  137. ('label', 1, 0, 'text', _('Label'), None, c_hdr_cell_style),
  138. ('counterpart', 1, 0, 'text', _('Counterpart'), None, c_hdr_cell_style),
  139. ('debit', 1, 0, 'text', _('Debit'), None, c_hdr_cell_style_right),
  140. ('credit', 1, 0, 'text', _('Credit'), None, c_hdr_cell_style_right),
  141. ('cumul_bal', 1, 0, 'text', _('Cumul. Bal.'), None, c_hdr_cell_style_right),
  142. ]
  143. if _p.amount_currency(data):
  144. c_specs += [
  145. ('curr_bal', 1, 0, 'text', _('Curr. Bal.'), None, c_hdr_cell_style_right),
  146. ('curr_code', 1, 0, 'text', _('Curr.'), None, c_hdr_cell_style_center),
  147. ]
  148. c_hdr_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  149. # cell styles for ledger lines
  150. ll_cell_format = _xs['borders_all']
  151. ll_cell_style = xlwt.easyxf(ll_cell_format)
  152. ll_cell_style_right = xlwt.easyxf(ll_cell_format + _xs['right'])
  153. ll_cell_style_center = xlwt.easyxf(ll_cell_format + _xs['center'])
  154. ll_cell_style_date = xlwt.easyxf(ll_cell_format + _xs['left'], num_format_str = report_xls.date_format)
  155. ll_cell_style_decimal = xlwt.easyxf(ll_cell_format + _xs['right'], num_format_str = report_xls.decimal_format)
  156. cnt = 0
  157. for account in objects:
  158. display_initial_balance = account.init_balance and (account.init_balance.get('debit', 0.0) != 0.0 or account.init_balance.get('credit', 0.0) != 0.0)
  159. display_ledger_lines = account.ledger_lines
  160. if _p.display_account_raw(data) == 'all' or (display_ledger_lines or display_initial_balance):
  161. #TO DO : replace cumul amounts by xls formulas
  162. cnt += 1
  163. cumul_debit = 0.0
  164. cumul_credit = 0.0
  165. cumul_balance = 0.0
  166. cumul_balance_curr = 0.0
  167. c_specs = [
  168. ('acc_title', 11, 0, 'text', ' - '.join([account.code, account.name])),
  169. ]
  170. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  171. row_pos = self.xls_write_row(ws, row_pos, row_data, c_title_cell_style)
  172. row_pos = self.xls_write_row(ws, row_pos, c_hdr_data)
  173. row_start = row_pos
  174. if display_initial_balance:
  175. cumul_debit = account.init_balance.get('debit') or 0.0
  176. cumul_credit = account.init_balance.get('credit') or 0.0
  177. cumul_balance = account.init_balance.get('init_balance') or 0.0
  178. cumul_balance_curr = account.init_balance.get('init_balance_currency') or 0.0
  179. debit_cell = rowcol_to_cell(row_pos, 8)
  180. credit_cell = rowcol_to_cell(row_pos, 9)
  181. bal_formula = debit_cell + '-' + credit_cell
  182. c_specs = [('empty%s' %x, 1, 0, 'text', None) for x in range(6)]
  183. c_specs += [
  184. ('init_bal', 1, 0, 'text', _('Initial Balance')),
  185. ('counterpart', 1, 0, 'text', None),
  186. ('debit', 1, 0, 'number', cumul_debit, None, c_init_cell_style_decimal),
  187. ('credit', 1, 0, 'number', cumul_credit, None, c_init_cell_style_decimal),
  188. ('cumul_bal', 1, 0, 'number', cumul_balance, None, c_init_cell_style_decimal),
  189. ]
  190. if _p.amount_currency(data):
  191. c_specs += [
  192. ('curr_bal', 1, 0, 'number', cumul_balance_curr, None, c_init_cell_style_decimal),
  193. ('curr_code', 1, 0, 'text', None),
  194. ]
  195. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  196. row_pos = self.xls_write_row(ws, row_pos, row_data, c_init_cell_style)
  197. for line in account.ledger_lines:
  198. cumul_debit += line.get('debit') or 0.0
  199. cumul_credit += line.get('credit') or 0.0
  200. cumul_balance_curr += line.get('amount_currency') or 0.0
  201. cumul_balance += line.get('balance') or 0.0
  202. label_elements = [line.get('lname') or '']
  203. if line.get('invoice_number'):
  204. label_elements.append("(%s)" % (line['invoice_number'],))
  205. label = ' '.join(label_elements)
  206. if line.get('ldate'):
  207. c_specs = [
  208. ('ldate', 1, 0, 'date', datetime.strptime(line['ldate'],'%Y-%m-%d'), None, ll_cell_style_date),
  209. ]
  210. else:
  211. c_specs = [
  212. ('ldate', 1, 0, 'text', None),
  213. ]
  214. c_specs += [
  215. ('period', 1, 0, 'text', line.get('period_code') or ''),
  216. ('move', 1, 0, 'text', line.get('move_name') or ''),
  217. ('journal', 1, 0, 'text', line.get('jcode') or ''),
  218. ('account_code', 1, 0, 'text', account.code),
  219. ('partner', 1, 0, 'text', line.get('partner_name') or ''),
  220. ('label', 1, 0, 'text', label),
  221. ('counterpart', 1, 0, 'text', line.get('counterparts') or ''),
  222. ('debit', 1, 0, 'number', line.get('debit', 0.0), None, ll_cell_style_decimal),
  223. ('credit', 1, 0, 'number', line.get('credit', 0.0), None, ll_cell_style_decimal),
  224. ('cumul_bal', 1, 0, 'number', cumul_balance, None, ll_cell_style_decimal),
  225. ]
  226. if _p.amount_currency(data):
  227. c_specs += [
  228. ('curr_bal', 1, 0, 'number', line.get('amount_currency') or 0.0, None, ll_cell_style_decimal),
  229. ('curr_code', 1, 0, 'text', line.get('currency_code') or '', None, ll_cell_style_center),
  230. ]
  231. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  232. row_pos = self.xls_write_row(ws, row_pos, row_data, ll_cell_style)
  233. debit_start = rowcol_to_cell(row_start, 8)
  234. debit_end = rowcol_to_cell(row_pos-1, 8)
  235. debit_formula = 'SUM(' + debit_start + ':' + debit_end + ')'
  236. credit_start = rowcol_to_cell(row_start, 9)
  237. credit_end = rowcol_to_cell(row_pos-1, 9)
  238. credit_formula = 'SUM(' + credit_start + ':' + credit_end + ')'
  239. balance_debit = rowcol_to_cell(row_pos, 8)
  240. balance_credit = rowcol_to_cell(row_pos, 9)
  241. balance_formula = balance_debit + '-' + balance_credit
  242. c_specs = [
  243. ('acc_title', 7, 0, 'text', ' - '.join([account.code, account.name])),
  244. ('cum_bal', 1, 0, 'text', _('Cumulated Balance on Account'), None, c_hdr_cell_style_right),
  245. ('debit', 1, 0, 'number', None, debit_formula, c_hdr_cell_style_decimal),
  246. ('credit', 1, 0, 'number', None, credit_formula, c_hdr_cell_style_decimal),
  247. ('balance', 1, 0, 'number', None, balance_formula, c_hdr_cell_style_decimal),
  248. ]
  249. if _p.amount_currency(data):
  250. if account.currency_id:
  251. c_specs += [('curr_bal', 1, 0, 'number', cumul_balance_curr, None, c_hdr_cell_style_decimal)]
  252. else:
  253. c_specs += [('curr_bal', 1, 0, 'text', None)]
  254. c_specs += [('curr_code', 1, 0, 'text', None)]
  255. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  256. row_pos = self.xls_write_row(ws, row_pos, row_data, c_hdr_cell_style)
  257. row_pos += 1
  258. general_ledger_xls('report.account.account_report_general_ledger_xls', 'account.account',
  259. parser=GeneralLedgerWebkit)
  260. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: