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.

430 lines
20 KiB

11 years ago
11 years ago
  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. from openerp.addons.report_xls.report_xls import report_xls
  24. from openerp.addons.report_xls.utils import rowcol_to_cell
  25. from openerp.addons.account_financial_report_webkit.report.partner_balance \
  26. import PartnerBalanceWebkit
  27. from openerp.tools.translate import _
  28. # import logging
  29. # _logger = logging.getLogger(__name__)
  30. def display_line(all_comparison_lines):
  31. return any([line.get('balance') for line in all_comparison_lines])
  32. class partners_balance_xls(report_xls):
  33. column_sizes = [12, 40, 25, 17, 17, 17, 17, 17]
  34. def print_title(self, ws, _p, row_position, xlwt, _xs):
  35. cell_style = xlwt.easyxf(_xs['xls_title'])
  36. report_name = ' - '.join([_p.report_name.upper(),
  37. _p.company.partner_id.name,
  38. _p.company.currency_id.name])
  39. c_specs = [
  40. ('report_name', 1, 0, 'text', report_name),
  41. ]
  42. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  43. row_position = self.xls_write_row(
  44. ws, row_position, row_data, row_style=cell_style)
  45. return row_position
  46. def print_empty_row(self, ws, row_position):
  47. c_sizes = self.column_sizes
  48. c_specs = [('empty%s' % i, 1, c_sizes[i], 'text', None)
  49. for i in range(0, len(c_sizes))]
  50. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  51. row_position = self.xls_write_row(
  52. ws, row_position, row_data, set_column_size=True)
  53. return row_position
  54. def print_header_titles(self, ws, _p, data, row_position, xlwt, _xs):
  55. cell_format = _xs['bold'] + _xs['fill_blue'] + _xs['borders_all']
  56. cell_style = xlwt.easyxf(cell_format)
  57. cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
  58. c_specs = [
  59. ('fy', 1, 0, 'text', _('Fiscal Year'), None, cell_style_center),
  60. ('af', 1, 0, 'text', _('Accounts Filter'),
  61. None, cell_style_center),
  62. ('df', 1, 0, 'text', _p.filter_form(data) == 'filter_date' and _(
  63. 'Dates Filter') or _('Periods Filter'), None,
  64. cell_style_center),
  65. ('pf', 1, 0, 'text', _('Partners Filter'),
  66. None, cell_style_center),
  67. ('tm', 1, 0, 'text', _('Target Moves'), None, cell_style_center),
  68. ('ib', 1, 0, 'text', _('Initial Balance'),
  69. None, cell_style_center),
  70. ('coa', 1, 0, 'text', _('Chart of Account'),
  71. None, cell_style_center),
  72. ]
  73. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  74. row_position = self.xls_write_row(
  75. ws, row_position, row_data, row_style=cell_style)
  76. return row_position
  77. def print_header_data(self, ws, _p, data, row_position, xlwt, _xs,
  78. initial_balance_text):
  79. cell_format = _xs['borders_all'] + _xs['wrap'] + _xs['top']
  80. cell_style = xlwt.easyxf(cell_format)
  81. cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
  82. c_specs = [
  83. ('fy', 1, 0, 'text', _p.fiscalyear.name if _p.fiscalyear else '-',
  84. None, cell_style_center),
  85. ('af', 1, 0, 'text', _p.accounts(data) and ', '.join(
  86. [account.code for account in _p.accounts(data)]) or _('All'),
  87. None, cell_style_center),
  88. ]
  89. df = _('From') + ': '
  90. if _p.filter_form(data) == 'filter_date':
  91. df += _p.start_date if _p.start_date else u''
  92. else:
  93. df += _p.start_period.name if _p.start_period else u''
  94. df += ' ' + _('\nTo') + ': '
  95. if _p.filter_form(data) == 'filter_date':
  96. df += _p.stop_date if _p.stop_date else u''
  97. else:
  98. df += _p.stop_period.name if _p.stop_period else u''
  99. c_specs += [
  100. ('df', 1, 0, 'text', df, None, cell_style_center),
  101. ('tm', 1, 0, 'text', _p.display_partner_account(
  102. data), None, cell_style_center),
  103. ('pf', 1, 0, 'text', _p.display_target_move(
  104. data), None, cell_style_center),
  105. ('ib', 1, 0, 'text', initial_balance_text[
  106. _p.initial_balance_mode], None, cell_style_center),
  107. ('coa', 1, 0, 'text', _p.chart_account.name,
  108. None, cell_style_center),
  109. ]
  110. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  111. row_position = self.xls_write_row(
  112. ws, row_position, row_data, row_style=cell_style)
  113. return row_position
  114. def print_comparison_header(self, _xs, xlwt, row_position, _p, ws,
  115. initial_balance_text):
  116. cell_format_ct = _xs['bold'] + _xs['fill_blue'] + _xs['borders_all']
  117. cell_style_ct = xlwt.easyxf(cell_format_ct)
  118. c_specs = [('ct', 7, 0, 'text', _('Comparisons'))]
  119. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  120. row_position = self.xls_write_row(
  121. ws, row_position, row_data, row_style=cell_style_ct)
  122. cell_format = _xs['borders_all'] + _xs['wrap'] + _xs['top']
  123. cell_style_center = xlwt.easyxf(cell_format)
  124. for index, params in enumerate(_p.comp_params):
  125. c_specs = [
  126. ('c', 2, 0, 'text', _('Comparison') + str(index + 1) +
  127. ' (C' + str(index + 1) + ')')]
  128. if params['comparison_filter'] == 'filter_date':
  129. c_specs += [('f', 2, 0, 'text', _('Dates Filter') + ': ' +
  130. _p.formatLang(params['start'], date=True) + ' - '
  131. + _p.formatLang(params['stop'], date=True))]
  132. elif params['comparison_filter'] == 'filter_period':
  133. c_specs += [('f', 2, 0, 'text', _('Periods Filter') +
  134. ': ' + params['start'].name + ' - ' +
  135. params['stop'].name)]
  136. else:
  137. c_specs += [('f', 2, 0, 'text', _('Fiscal Year') +
  138. ': ' + params['fiscalyear'].name)]
  139. c_specs += [('ib', 2, 0, 'text', _('Initial Balance') +
  140. ': ' +
  141. initial_balance_text[params['initial_balance_mode']])]
  142. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  143. row_position = self.xls_write_row(
  144. ws, row_position, row_data, row_style=cell_style_center)
  145. return row_position
  146. def print_account_header(self, ws, _p, _xs, xlwt, row_position):
  147. cell_format = _xs['bold'] + _xs['fill'] + \
  148. _xs['borders_all'] + _xs['wrap'] + _xs['top']
  149. cell_style = xlwt.easyxf(cell_format)
  150. cell_style_right = xlwt.easyxf(cell_format + _xs['right'])
  151. cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
  152. if len(_p.comp_params) == 2:
  153. account_span = 3
  154. else:
  155. account_span = _p.initial_balance_mode and 2 or 3
  156. c_specs = [
  157. ('account', account_span, 0, 'text', _('Account / Partner Name')),
  158. ('code', 1, 0, 'text', _('Code / Ref')),
  159. ]
  160. if _p.comparison_mode == 'no_comparison':
  161. if _p.initial_balance_mode:
  162. c_specs += [('init_bal', 1, 0, 'text',
  163. _('Initial Balance'), None, cell_style_right)]
  164. c_specs += [
  165. ('debit', 1, 0, 'text', _('Debit'), None, cell_style_right),
  166. ('credit', 1, 0, 'text', _('Credit'), None, cell_style_right),
  167. ]
  168. if _p.comparison_mode == 'no_comparison' or not _p.fiscalyear:
  169. c_specs += [('balance', 1, 0, 'text',
  170. _('Balance'), None, cell_style_right)]
  171. else:
  172. c_specs += [('balance_fy', 1, 0, 'text', _('Balance %s') %
  173. _p.fiscalyear.name, None, cell_style_right)]
  174. if _p.comparison_mode in ('single', 'multiple'):
  175. for index in range(_p.nb_comparison):
  176. if _p.comp_params[index][
  177. 'comparison_filter'] == 'filter_year' \
  178. and _p.comp_params[index].get('fiscalyear', False):
  179. c_specs += [('balance_%s' % index, 1, 0, 'text',
  180. _('Balance %s') %
  181. _p.comp_params[index]['fiscalyear'].name,
  182. None, cell_style_right)]
  183. else:
  184. c_specs += [('balance_%s' % index, 1, 0, 'text',
  185. _('Balance C%s') % (index + 1), None,
  186. cell_style_right)]
  187. if _p.comparison_mode == 'single':
  188. c_specs += [
  189. ('diff', 1, 0, 'text', _('Difference'),
  190. None, cell_style_right),
  191. ('diff_percent', 1, 0, 'text',
  192. _('% Difference'), None, cell_style_center),
  193. ]
  194. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  195. row_position = self.xls_write_row(
  196. ws, row_position, row_data, row_style=cell_style)
  197. return row_position
  198. def print_row_code_account(self, ws, current_account, row_position, _xs,
  199. xlwt):
  200. cell_format = _xs['xls_title'] + _xs['bold'] + \
  201. _xs['fill'] + _xs['borders_all']
  202. cell_style = xlwt.easyxf(cell_format)
  203. c_specs = [
  204. ('acc_title', 7, 0, 'text', ' - '.join([current_account.code,
  205. current_account.name])), ]
  206. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  207. row_position = self.xls_write_row(
  208. ws, row_position, row_data, cell_style)
  209. return row_position
  210. def print_account_totals(self, _xs, xlwt, ws, row_start_account,
  211. row_position, current_account, _p):
  212. cell_format = _xs['bold'] + _xs['fill'] + \
  213. _xs['borders_all'] + _xs['wrap'] + _xs['top']
  214. cell_style = xlwt.easyxf(cell_format)
  215. cell_style_decimal = xlwt.easyxf(
  216. cell_format + _xs['right'],
  217. num_format_str=report_xls.decimal_format)
  218. c_specs = [
  219. ('acc_title', 2, 0, 'text', current_account.name),
  220. ('code', 1, 0, 'text', current_account.code),
  221. ]
  222. for column in range(3, 7):
  223. # in case of one single comparison, the column 6 will contain
  224. # percentages
  225. if (_p.comparison_mode == 'single' and column == 6):
  226. total_diff = rowcol_to_cell(row_position, column - 1)
  227. total_balance = rowcol_to_cell(row_position, column - 2)
  228. account_formula = 'Round(' + total_diff + \
  229. '/' + total_balance + '*100;0)'
  230. else:
  231. account_start = rowcol_to_cell(row_start_account, column)
  232. account_end = rowcol_to_cell(row_position - 1, column)
  233. account_formula = 'Round(SUM(' + \
  234. account_start + ':' + account_end + ');2)'
  235. c_specs += [('total%s' % column, 1, 0, 'text', None,
  236. account_formula, None, cell_style_decimal)]
  237. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  238. row_position = self.xls_write_row(
  239. ws, row_position, row_data, cell_style)
  240. return row_position + 1
  241. def generate_xls_report(self, _p, _xs, data, objects, wb):
  242. # Initialisations
  243. ws = wb.add_sheet(_p.report_name[:31])
  244. ws.panes_frozen = True
  245. ws.remove_splits = True
  246. ws.portrait = 0 # Landscape
  247. ws.fit_width_to_pages = 1
  248. row_pos = 0
  249. ws.header_str = self.xls_headers['standard']
  250. ws.footer_str = self.xls_footers['standard']
  251. # Print Title
  252. row_pos = self.print_title(ws, _p, row_pos, xlwt, _xs)
  253. # Print empty row to define column sizes
  254. row_pos = self.print_empty_row(ws, row_pos)
  255. # Print Header Table titles (Fiscal Year - Accounts Filter - Periods
  256. # Filter...)
  257. row_pos = self.print_header_titles(ws, _p, data, row_pos, xlwt, _xs)
  258. initial_balance_text = {
  259. 'initial_balance': _('Computed'),
  260. 'opening_balance': _('Opening Entries'),
  261. False: _('No')} # cf. account_report_partner_balance.mako
  262. # Print Header Table data
  263. row_pos = self.print_header_data(
  264. ws, _p, data, row_pos, xlwt, _xs, initial_balance_text)
  265. # Print comparison header table
  266. if _p.comparison_mode in ('single', 'multiple'):
  267. row_pos += 1
  268. row_pos = self.print_comparison_header(
  269. _xs, xlwt, row_pos, _p, ws, initial_balance_text)
  270. # Freeze the line
  271. ws.set_horz_split_pos(row_pos)
  272. # cell styles for account data
  273. regular_cell_format = _xs['borders_all']
  274. regular_cell_style = xlwt.easyxf(regular_cell_format)
  275. regular_cell_style_decimal = xlwt.easyxf(
  276. regular_cell_format + _xs['right'],
  277. num_format_str=report_xls.decimal_format)
  278. row_pos += 1
  279. for current_account in objects:
  280. partners_order = _p['partners_order_accounts']\
  281. .get(current_account.id, False)
  282. # do not display accounts without partners
  283. if not partners_order:
  284. continue
  285. comparisons = _p['comparisons_accounts']\
  286. .get(current_account.id, False)
  287. # in multiple columns mode, we do not want to print accounts
  288. # without any rows
  289. if _p.comparison_mode in ('single', 'multiple'):
  290. all_comparison_lines = [comp['partners_amounts'][partner_id[1]]
  291. for partner_id in partners_order
  292. for comp in comparisons]
  293. if not display_line(all_comparison_lines):
  294. continue
  295. current_partner_amounts = _p['partners_amounts_accounts']\
  296. .get(current_account.id, False)
  297. if _p.comparison_mode in ('single', 'multiple'):
  298. comparison_total = {}
  299. for i, comp in enumerate(comparisons):
  300. comparison_total[i] = {'balance': 0.0}
  301. # print row: Code - Account name
  302. row_pos = self.print_row_code_account(
  303. ws, current_account, row_pos, _xs, xlwt)
  304. row_account_start = row_pos
  305. # Print row: Titles "Account/Partner Name-Code/ref-Initial
  306. # Balance-Debit-Credit-Balance" or "Account/Partner
  307. # Name-Code/ref-Balance Year-Balance Year2-Balance C2-Balance C3"
  308. row_pos = self.print_account_header(ws, _p, _xs, xlwt, row_pos)
  309. for (partner_code_name, partner_id, partner_ref, partner_name) \
  310. in partners_order:
  311. partner = current_partner_amounts.get(partner_id, {})
  312. # in single mode, we have to display all the partners even if
  313. # their balance is 0.0 because the initial balance should match
  314. # with the previous year closings
  315. # in multiple columns mode, we do not want to print partners
  316. # which have a balance at 0.0 in each comparison column
  317. if _p.comparison_mode in ('single', 'multiple'):
  318. all_comparison_lines = [comp['partners_amounts']
  319. [partner_id]
  320. for comp in comparisons
  321. if comp['partners_amounts'].
  322. get(partner_id)]
  323. if not display_line(all_comparison_lines):
  324. continue
  325. # display data row
  326. if len(_p.comp_params) == 2:
  327. account_span = 3
  328. else:
  329. account_span = _p.initial_balance_mode and 2 or 3
  330. c_specs = [('acc_title', account_span, 0, 'text',
  331. partner_name if partner_name else
  332. _('Unallocated'))]
  333. c_specs += [('partner_ref', 1, 0, 'text',
  334. partner_ref if partner_ref else '')]
  335. if _p.comparison_mode == 'no_comparison':
  336. bal_formula = ''
  337. if _p.initial_balance_mode:
  338. init_bal_cell = rowcol_to_cell(row_pos, 3)
  339. bal_formula = init_bal_cell + '+'
  340. debit_col = 4
  341. c_specs += [
  342. ('init_bal', 1, 0, 'number', partner.get(
  343. 'init_balance', 0.0), None,
  344. regular_cell_style_decimal),
  345. ]
  346. else:
  347. debit_col = 3
  348. c_specs += [
  349. ('debit', 1, 0, 'number', partner.get('debit', 0.0),
  350. None, regular_cell_style_decimal),
  351. ('credit', 1, 0, 'number', partner.get('credit', 0.0),
  352. None, regular_cell_style_decimal),
  353. ]
  354. debit_cell = rowcol_to_cell(row_pos, debit_col)
  355. credit_cell = rowcol_to_cell(row_pos, debit_col + 1)
  356. bal_formula += debit_cell + '-' + credit_cell
  357. c_specs += [('bal', 1, 0, 'number', None,
  358. bal_formula, regular_cell_style_decimal), ]
  359. else:
  360. c_specs += [('bal', 1, 0, 'number', partner.get('balance',
  361. 0.0),
  362. None, regular_cell_style_decimal), ]
  363. if _p.comparison_mode in ('single', 'multiple'):
  364. for i, comp in enumerate(comparisons):
  365. comp_partners = comp['partners_amounts']
  366. balance = diff = percent_diff = 0
  367. if comp_partners.get(partner_id):
  368. balance = comp_partners[partner_id]['balance']
  369. diff = comp_partners[partner_id]['diff']
  370. percent_diff = comp_partners[
  371. partner_id]['percent_diff']
  372. comparison_total[i]['balance'] += balance
  373. c_specs += [('balance_%s' % i, 1, 0, 'number',
  374. balance, None,
  375. regular_cell_style_decimal), ]
  376. # no diff in multiple comparisons because it shows too much
  377. # data
  378. if _p.comparison_mode == 'single':
  379. c_specs += [('balance_diff', 1, 0, 'number',
  380. diff, None, regular_cell_style_decimal), ]
  381. if percent_diff is False:
  382. c_specs += [('balance', 1, 0, 'number',
  383. diff, None, regular_cell_style_decimal), ]
  384. else:
  385. c_specs += [('perc_diff', 1, 0, 'number',
  386. int(round(percent_diff))), ]
  387. row_data = self.xls_row_template(
  388. c_specs, [x[0] for x in c_specs])
  389. row_pos = self.xls_write_row(
  390. ws, row_pos, row_data, regular_cell_style)
  391. row_pos = self.print_account_totals(
  392. _xs, xlwt, ws, row_account_start, row_pos, current_account, _p)
  393. partners_balance_xls('report.account.account_report_partner_balance_xls',
  394. 'account.account',
  395. parser=PartnerBalanceWebkit)