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.

432 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 += [
  130. ('f', 2, 0, 'text',
  131. _('Dates Filter') + ': ' +
  132. _p.formatLang(params['start'], date=True) + ' - ' +
  133. _p.formatLang(params['stop'], date=True))]
  134. elif params['comparison_filter'] == 'filter_period':
  135. c_specs += [('f', 2, 0, 'text', _('Periods Filter') +
  136. ': ' + params['start'].name + ' - ' +
  137. params['stop'].name)]
  138. else:
  139. c_specs += [('f', 2, 0, 'text', _('Fiscal Year') +
  140. ': ' + params['fiscalyear'].name)]
  141. c_specs += [('ib', 2, 0, 'text', _('Initial Balance') +
  142. ': ' +
  143. initial_balance_text[params['initial_balance_mode']])]
  144. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  145. row_position = self.xls_write_row(
  146. ws, row_position, row_data, row_style=cell_style_center)
  147. return row_position
  148. def print_account_header(self, ws, _p, _xs, xlwt, row_position):
  149. cell_format = _xs['bold'] + _xs['fill'] + \
  150. _xs['borders_all'] + _xs['wrap'] + _xs['top']
  151. cell_style = xlwt.easyxf(cell_format)
  152. cell_style_right = xlwt.easyxf(cell_format + _xs['right'])
  153. cell_style_center = xlwt.easyxf(cell_format + _xs['center'])
  154. if len(_p.comp_params) == 2:
  155. account_span = 3
  156. else:
  157. account_span = _p.initial_balance_mode and 2 or 3
  158. c_specs = [
  159. ('account', account_span, 0, 'text', _('Account / Partner Name')),
  160. ('code', 1, 0, 'text', _('Code / Ref')),
  161. ]
  162. if _p.comparison_mode == 'no_comparison':
  163. if _p.initial_balance_mode:
  164. c_specs += [('init_bal', 1, 0, 'text',
  165. _('Initial Balance'), None, cell_style_right)]
  166. c_specs += [
  167. ('debit', 1, 0, 'text', _('Debit'), None, cell_style_right),
  168. ('credit', 1, 0, 'text', _('Credit'), None, cell_style_right),
  169. ]
  170. if _p.comparison_mode == 'no_comparison' or not _p.fiscalyear:
  171. c_specs += [('balance', 1, 0, 'text',
  172. _('Balance'), None, cell_style_right)]
  173. else:
  174. c_specs += [('balance_fy', 1, 0, 'text', _('Balance %s') %
  175. _p.fiscalyear.name, None, cell_style_right)]
  176. if _p.comparison_mode in ('single', 'multiple'):
  177. for index in range(_p.nb_comparison):
  178. if _p.comp_params[index][
  179. 'comparison_filter'] == 'filter_year' \
  180. and _p.comp_params[index].get('fiscalyear', False):
  181. c_specs += [('balance_%s' % index, 1, 0, 'text',
  182. _('Balance %s') %
  183. _p.comp_params[index]['fiscalyear'].name,
  184. None, cell_style_right)]
  185. else:
  186. c_specs += [('balance_%s' % index, 1, 0, 'text',
  187. _('Balance C%s') % (index + 1), None,
  188. cell_style_right)]
  189. if _p.comparison_mode == 'single':
  190. c_specs += [
  191. ('diff', 1, 0, 'text', _('Difference'),
  192. None, cell_style_right),
  193. ('diff_percent', 1, 0, 'text',
  194. _('% Difference'), None, cell_style_center),
  195. ]
  196. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  197. row_position = self.xls_write_row(
  198. ws, row_position, row_data, row_style=cell_style)
  199. return row_position
  200. def print_row_code_account(self, ws, current_account, row_position, _xs,
  201. xlwt):
  202. cell_format = _xs['xls_title'] + _xs['bold'] + \
  203. _xs['fill'] + _xs['borders_all']
  204. cell_style = xlwt.easyxf(cell_format)
  205. c_specs = [
  206. ('acc_title', 7, 0, 'text', ' - '.join([current_account.code,
  207. current_account.name])), ]
  208. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  209. row_position = self.xls_write_row(
  210. ws, row_position, row_data, cell_style)
  211. return row_position
  212. def print_account_totals(self, _xs, xlwt, ws, row_start_account,
  213. row_position, current_account, _p):
  214. cell_format = _xs['bold'] + _xs['fill'] + \
  215. _xs['borders_all'] + _xs['wrap'] + _xs['top']
  216. cell_style = xlwt.easyxf(cell_format)
  217. cell_style_decimal = xlwt.easyxf(
  218. cell_format + _xs['right'],
  219. num_format_str=report_xls.decimal_format)
  220. c_specs = [
  221. ('acc_title', 2, 0, 'text', current_account.name),
  222. ('code', 1, 0, 'text', current_account.code),
  223. ]
  224. for column in range(3, 7):
  225. # in case of one single comparison, the column 6 will contain
  226. # percentages
  227. if (_p.comparison_mode == 'single' and column == 6):
  228. total_diff = rowcol_to_cell(row_position, column - 1)
  229. total_balance = rowcol_to_cell(row_position, column - 2)
  230. account_formula = 'Round(' + total_diff + \
  231. '/' + total_balance + '*100;0)'
  232. else:
  233. account_start = rowcol_to_cell(row_start_account, column)
  234. account_end = rowcol_to_cell(row_position - 1, column)
  235. account_formula = 'Round(SUM(' + \
  236. account_start + ':' + account_end + ');2)'
  237. c_specs += [('total%s' % column, 1, 0, 'text', None,
  238. account_formula, None, cell_style_decimal)]
  239. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  240. row_position = self.xls_write_row(
  241. ws, row_position, row_data, cell_style)
  242. return row_position + 1
  243. def generate_xls_report(self, _p, _xs, data, objects, wb):
  244. # Initialisations
  245. ws = wb.add_sheet(_p.report_name[:31])
  246. ws.panes_frozen = True
  247. ws.remove_splits = True
  248. ws.portrait = 0 # Landscape
  249. ws.fit_width_to_pages = 1
  250. row_pos = 0
  251. ws.header_str = self.xls_headers['standard']
  252. ws.footer_str = self.xls_footers['standard']
  253. # Print Title
  254. row_pos = self.print_title(ws, _p, row_pos, xlwt, _xs)
  255. # Print empty row to define column sizes
  256. row_pos = self.print_empty_row(ws, row_pos)
  257. # Print Header Table titles (Fiscal Year - Accounts Filter - Periods
  258. # Filter...)
  259. row_pos = self.print_header_titles(ws, _p, data, row_pos, xlwt, _xs)
  260. initial_balance_text = {
  261. 'initial_balance': _('Computed'),
  262. 'opening_balance': _('Opening Entries'),
  263. False: _('No')} # cf. account_report_partner_balance.mako
  264. # Print Header Table data
  265. row_pos = self.print_header_data(
  266. ws, _p, data, row_pos, xlwt, _xs, initial_balance_text)
  267. # Print comparison header table
  268. if _p.comparison_mode in ('single', 'multiple'):
  269. row_pos += 1
  270. row_pos = self.print_comparison_header(
  271. _xs, xlwt, row_pos, _p, ws, initial_balance_text)
  272. # Freeze the line
  273. ws.set_horz_split_pos(row_pos)
  274. # cell styles for account data
  275. regular_cell_format = _xs['borders_all']
  276. regular_cell_style = xlwt.easyxf(regular_cell_format)
  277. regular_cell_style_decimal = xlwt.easyxf(
  278. regular_cell_format + _xs['right'],
  279. num_format_str=report_xls.decimal_format)
  280. row_pos += 1
  281. for current_account in objects:
  282. partners_order = _p['partners_order_accounts']\
  283. .get(current_account.id, False)
  284. # do not display accounts without partners
  285. if not partners_order:
  286. continue
  287. comparisons = _p['comparisons_accounts']\
  288. .get(current_account.id, False)
  289. # in multiple columns mode, we do not want to print accounts
  290. # without any rows
  291. if _p.comparison_mode in ('single', 'multiple'):
  292. all_comparison_lines = [comp['partners_amounts'][partner_id[1]]
  293. for partner_id in partners_order
  294. for comp in comparisons]
  295. if not display_line(all_comparison_lines):
  296. continue
  297. current_partner_amounts = _p['partners_amounts_accounts']\
  298. .get(current_account.id, False)
  299. if _p.comparison_mode in ('single', 'multiple'):
  300. comparison_total = {}
  301. for i, comp in enumerate(comparisons):
  302. comparison_total[i] = {'balance': 0.0}
  303. # print row: Code - Account name
  304. row_pos = self.print_row_code_account(
  305. ws, current_account, row_pos, _xs, xlwt)
  306. row_account_start = row_pos
  307. # Print row: Titles "Account/Partner Name-Code/ref-Initial
  308. # Balance-Debit-Credit-Balance" or "Account/Partner
  309. # Name-Code/ref-Balance Year-Balance Year2-Balance C2-Balance C3"
  310. row_pos = self.print_account_header(ws, _p, _xs, xlwt, row_pos)
  311. for (partner_code_name, partner_id, partner_ref, partner_name) \
  312. in partners_order:
  313. partner = current_partner_amounts.get(partner_id, {})
  314. # in single mode, we have to display all the partners even if
  315. # their balance is 0.0 because the initial balance should match
  316. # with the previous year closings
  317. # in multiple columns mode, we do not want to print partners
  318. # which have a balance at 0.0 in each comparison column
  319. if _p.comparison_mode in ('single', 'multiple'):
  320. all_comparison_lines = [comp['partners_amounts']
  321. [partner_id]
  322. for comp in comparisons
  323. if comp['partners_amounts'].
  324. get(partner_id)]
  325. if not display_line(all_comparison_lines):
  326. continue
  327. # display data row
  328. if len(_p.comp_params) == 2:
  329. account_span = 3
  330. else:
  331. account_span = _p.initial_balance_mode and 2 or 3
  332. c_specs = [('acc_title', account_span, 0, 'text',
  333. partner_name if partner_name else
  334. _('Unallocated'))]
  335. c_specs += [('partner_ref', 1, 0, 'text',
  336. partner_ref if partner_ref else '')]
  337. if _p.comparison_mode == 'no_comparison':
  338. bal_formula = ''
  339. if _p.initial_balance_mode:
  340. init_bal_cell = rowcol_to_cell(row_pos, 3)
  341. bal_formula = init_bal_cell + '+'
  342. debit_col = 4
  343. c_specs += [
  344. ('init_bal', 1, 0, 'number', partner.get(
  345. 'init_balance', 0.0), None,
  346. regular_cell_style_decimal),
  347. ]
  348. else:
  349. debit_col = 3
  350. c_specs += [
  351. ('debit', 1, 0, 'number', partner.get('debit', 0.0),
  352. None, regular_cell_style_decimal),
  353. ('credit', 1, 0, 'number', partner.get('credit', 0.0),
  354. None, regular_cell_style_decimal),
  355. ]
  356. debit_cell = rowcol_to_cell(row_pos, debit_col)
  357. credit_cell = rowcol_to_cell(row_pos, debit_col + 1)
  358. bal_formula += debit_cell + '-' + credit_cell
  359. c_specs += [('bal', 1, 0, 'number', None,
  360. bal_formula, regular_cell_style_decimal), ]
  361. else:
  362. c_specs += [('bal', 1, 0, 'number', partner.get('balance',
  363. 0.0),
  364. None, regular_cell_style_decimal), ]
  365. if _p.comparison_mode in ('single', 'multiple'):
  366. for i, comp in enumerate(comparisons):
  367. comp_partners = comp['partners_amounts']
  368. balance = diff = percent_diff = 0
  369. if comp_partners.get(partner_id):
  370. balance = comp_partners[partner_id]['balance']
  371. diff = comp_partners[partner_id]['diff']
  372. percent_diff = comp_partners[
  373. partner_id]['percent_diff']
  374. comparison_total[i]['balance'] += balance
  375. c_specs += [('balance_%s' % i, 1, 0, 'number',
  376. balance, None,
  377. regular_cell_style_decimal), ]
  378. # no diff in multiple comparisons because it shows too much
  379. # data
  380. if _p.comparison_mode == 'single':
  381. c_specs += [('balance_diff', 1, 0, 'number',
  382. diff, None, regular_cell_style_decimal), ]
  383. if percent_diff is False:
  384. c_specs += [('balance', 1, 0, 'number',
  385. diff, None, regular_cell_style_decimal), ]
  386. else:
  387. c_specs += [('perc_diff', 1, 0, 'number',
  388. int(round(percent_diff))), ]
  389. row_data = self.xls_row_template(
  390. c_specs, [x[0] for x in c_specs])
  391. row_pos = self.xls_write_row(
  392. ws, row_pos, row_data, regular_cell_style)
  393. row_pos = self.print_account_totals(
  394. _xs, xlwt, ws, row_account_start, row_pos, current_account, _p)
  395. partners_balance_xls('report.account.account_report_partner_balance_xls',
  396. 'account.account',
  397. parser=PartnerBalanceWebkit)