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.

828 lines
38 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 datetime import datetime
  24. from openerp.addons.report_xls.report_xls import report_xls
  25. from openerp.addons.report_xls.utils import rowcol_to_cell
  26. from openerp.addons.account_financial_report_webkit.report.open_invoices \
  27. import PartnersOpenInvoicesWebkit
  28. from openerp.tools.translate import _
  29. # import logging
  30. # _logger = logging.getLogger(__name__)
  31. class open_invoices_xls(report_xls):
  32. column_sizes = [12, 12, 20, 15, 30, 30, 14, 14, 14, 14, 14, 14, 10]
  33. def global_initializations(self, wb, _p, xlwt, _xs, objects, data):
  34. # this procedure will initialise variables and Excel cell styles and
  35. # return them as global ones
  36. self.ws = wb.add_sheet(_p.report_name[:31])
  37. self.ws.panes_frozen = True
  38. self.ws.remove_splits = True
  39. self.ws.portrait = 0 # Landscape
  40. self.ws.fit_width_to_pages = 1
  41. self.ws.header_str = self.xls_headers['standard']
  42. self.ws.footer_str = self.xls_footers['standard']
  43. # -------------------------------------------------------
  44. # number of columns is 11 in case of normal report, 13 in case the
  45. # option currency is selected and 12 in case of the regroup by currency
  46. # option is checked
  47. group_lines = False
  48. # search if the regroup option is selected by browsing the accounts
  49. # defined in objects - see account_report_open_invoices.mako
  50. for acc in objects:
  51. if hasattr(acc, 'grouped_ledger_lines'):
  52. group_lines = True
  53. if group_lines:
  54. self.nbr_columns = 12
  55. elif _p.amount_currency(data) and not group_lines:
  56. self.nbr_columns = 13
  57. else:
  58. self.nbr_columns = 11
  59. # -------------------------------------------------------
  60. # cell style for report title
  61. self.style_font12 = xlwt.easyxf(_xs['xls_title'])
  62. # -------------------------------------------------------
  63. self.style_default = xlwt.easyxf(_xs['borders_all'])
  64. # -------------------------------------------------------
  65. self.style_default_italic = xlwt.easyxf(
  66. _xs['borders_all'] + _xs['italic'])
  67. # -------------------------------------------------------
  68. self.style_bold = xlwt.easyxf(_xs['bold'] + _xs['borders_all'])
  69. # -------------------------------------------------------
  70. # cell style for header titles: 'Chart of accounts' - 'Fiscal year' ...
  71. self.style_bold_blue_center = xlwt.easyxf(
  72. _xs['bold'] + _xs['fill_blue'] + _xs['borders_all'] +
  73. _xs['center'])
  74. # -------------------------------------------------------
  75. # cell style for header data: 'Chart of accounts' - 'Fiscal year' ...
  76. self.style_center = xlwt.easyxf(
  77. _xs['borders_all'] + _xs['wrap'] + _xs['center'])
  78. # -------------------------------------------------------
  79. # cell style for columns titles 'Date'- 'Period' - 'Entry'...
  80. self.style_yellow_bold = xlwt.easyxf(
  81. _xs['bold'] + _xs['fill'] + _xs['borders_all'])
  82. # -------------------------------------------------------
  83. # cell style for columns titles 'Date'- 'Period' - 'Entry'...
  84. self.style_yellow_bold_right = xlwt.easyxf(
  85. _xs['bold'] + _xs['fill'] + _xs['borders_all'] + _xs['right'])
  86. # -------------------------------------------------------
  87. self.style_right = xlwt.easyxf(_xs['borders_all'] + _xs['right'])
  88. # -------------------------------------------------------
  89. self.style_right_italic = xlwt.easyxf(
  90. _xs['borders_all'] + _xs['right'] + _xs['italic'])
  91. # -------------------------------------------------------
  92. self.style_decimal = xlwt.easyxf(
  93. _xs['borders_all'] + _xs['right'],
  94. num_format_str=report_xls.decimal_format)
  95. # -------------------------------------------------------
  96. self.style_decimal_italic = xlwt.easyxf(
  97. _xs['borders_all'] + _xs['right'] + _xs['italic'],
  98. num_format_str=report_xls.decimal_format)
  99. # -------------------------------------------------------
  100. self.style_date = xlwt.easyxf(
  101. _xs['borders_all'] + _xs['left'],
  102. num_format_str=report_xls.date_format)
  103. # -------------------------------------------------------
  104. self.style_date_italic = xlwt.easyxf(
  105. _xs['borders_all'] + _xs['left'] + _xs['italic'],
  106. num_format_str=report_xls.date_format)
  107. # -------------------------------------------------------
  108. cell_format = _xs['xls_title'] + _xs['bold'] + \
  109. _xs['fill'] + _xs['borders_all']
  110. self.style_account_title = xlwt.easyxf(cell_format)
  111. self.style_account_title_right = xlwt.easyxf(
  112. cell_format + _xs['right'])
  113. self.style_account_title_decimal = xlwt.easyxf(
  114. cell_format + _xs['right'],
  115. num_format_str=report_xls.decimal_format)
  116. # -------------------------------------------------------
  117. cell_format = _xs['bold']
  118. self.style_partner_row = xlwt.easyxf(cell_format)
  119. # -------------------------------------------------------
  120. cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  121. self.style_partner_cumul = xlwt.easyxf(cell_format)
  122. self.style_partner_cumul_right = xlwt.easyxf(
  123. cell_format + _xs['right'])
  124. self.style_partner_cumul_decimal = xlwt.easyxf(
  125. cell_format + _xs['right'],
  126. num_format_str=report_xls.decimal_format)
  127. # print the first line "OPEN INVOICE REPORT - db name - Currency
  128. def print_title(self, _p, row_position):
  129. report_name = ' - '.join([_p.report_name.upper(),
  130. _p.company.partner_id.name,
  131. _p.company.currency_id.name])
  132. c_specs = [('report_name', self.nbr_columns, 0, 'text', report_name), ]
  133. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  134. row_position = self.xls_write_row(
  135. self.ws, row_position, row_data, row_style=self.style_font12)
  136. return row_position
  137. # send an empty row to the Excel document
  138. def print_empty_row(self, row_position):
  139. c_sizes = self.column_sizes
  140. c_specs = [('empty%s' % i, 1, c_sizes[i], 'text', None)
  141. for i in range(0, len(c_sizes))]
  142. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  143. row_position = self.xls_write_row(
  144. self.ws, row_position, row_data, set_column_size=True)
  145. return row_position
  146. # Fill in the titles of the header summary tables: Chart of account -
  147. # Fiscal year - ...
  148. def print_header_titles(self, _p, data, row_position):
  149. c_specs = [
  150. ('coa', 2, 0, 'text', _('Chart of Account'),
  151. None, self.style_bold_blue_center),
  152. ('fy', 2, 0, 'text', _('Fiscal Year'),
  153. None, self.style_bold_blue_center),
  154. ('df', 2, 0, 'text', _p.filter_form(data) == 'filter_date' and _(
  155. 'Dates Filter') or _('Periods Filter'), None,
  156. self.style_bold_blue_center),
  157. ('cd', 1 if self.nbr_columns == 11 else 2, 0, 'text',
  158. _('Clearance Date'), None, self.style_bold_blue_center),
  159. ('af', 2, 0, 'text', _('Accounts Filter'),
  160. None, self.style_bold_blue_center),
  161. ('tm', 3 if self.nbr_columns == 13 else 2, 0, 'text',
  162. _('Target Moves'), None, self.style_bold_blue_center),
  163. ]
  164. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  165. row_position = self.xls_write_row(
  166. self.ws, row_position, row_data,
  167. row_style=self.style_bold_blue_center)
  168. return row_position
  169. # Fill in the data of the header summary tables: Chart of account - Fiscal
  170. # year - ...
  171. def print_header_data(self, _p, data, row_position):
  172. c_specs = [
  173. ('coa', 2, 0, 'text', _p.chart_account.name,
  174. None, self.style_center),
  175. ('fy', 2, 0, 'text', _p.fiscalyear.name if _p.fiscalyear else '-',
  176. None, self.style_center),
  177. ]
  178. df = _('From') + ': '
  179. if _p.filter_form(data) == 'filter_date':
  180. df += _p.start_date if _p.start_date else u''
  181. else:
  182. df += _p.start_period.name if _p.start_period else u''
  183. df += ' ' + _('To') + ': '
  184. if _p.filter_form(data) == 'filter_date':
  185. df += _p.stop_date if _p.stop_date else u''
  186. else:
  187. df += _p.stop_period.name if _p.stop_period else u''
  188. c_specs += [
  189. ('df', 2, 0, 'text', df, None, self.style_center),
  190. ('cd', 1 if self.nbr_columns == 11 else 2, 0, 'text',
  191. _p.date_until, None, self.style_center), # clearance date
  192. ('af', 2, 0, 'text', _('Custom Filter')
  193. if _p.partner_ids else _p.display_partner_account(data), None,
  194. self.style_center),
  195. ('tm', 3 if self.nbr_columns == 13 else 2, 0, 'text',
  196. _p.display_target_move(data), None, self.style_center),
  197. ]
  198. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  199. row_position = self.xls_write_row(
  200. self.ws, row_position, row_data, row_style=self.style_center)
  201. return row_position
  202. # Fill in a row with the titles of the columns for the invoice lines: Date
  203. # - Period - Entry -...
  204. def print_columns_title(self, _p, row_position, data, group_lines=False):
  205. c_specs = [
  206. ('date', 1, 0, 'text', _('Date'), None, self.style_yellow_bold),
  207. ('period', 1, 0, 'text', _('Period'),
  208. None, self.style_yellow_bold),
  209. ('entry', 1, 0, 'text', _('Entry'), None, self.style_yellow_bold),
  210. ('journal', 1, 0, 'text', _('Journal'),
  211. None, self.style_yellow_bold),
  212. ]
  213. if not group_lines:
  214. c_specs += [('partner', 1, 0, 'text', _('Partner'),
  215. None, self.style_yellow_bold), ]
  216. c_specs += [
  217. ('label', 1, 0, 'text', _('Label'), None, self.style_yellow_bold),
  218. ('rec', 1, 0, 'text', _('Rec.'), None, self.style_yellow_bold),
  219. ('due_date', 1, 0, 'text', _('Due Date'),
  220. None, self.style_yellow_bold),
  221. ('debit', 1, 0, 'text', _('Debit'),
  222. None, self.style_yellow_bold_right),
  223. ('credit', 1, 0, 'text', _('Credit'),
  224. None, self.style_yellow_bold_right),
  225. ('cumul', 1, 0, 'text', _('Cumul. Bal.'),
  226. None, self.style_yellow_bold_right),
  227. ]
  228. if group_lines:
  229. c_specs += [
  230. ('currbal', 1, 0, 'text', _('Curr. Balance'),
  231. None, self.style_yellow_bold_right),
  232. ('curr', 1, 0, 'text', _('Curr.'),
  233. None, self.style_yellow_bold_right),
  234. ]
  235. else:
  236. if _p.amount_currency(data):
  237. c_specs += [
  238. ('currbal', 1, 0, 'text', _('Curr. Balance'),
  239. None, self.style_yellow_bold_right),
  240. ('curr', 1, 0, 'text', _('Curr.'),
  241. None, self.style_yellow_bold_right),
  242. ]
  243. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  244. row_position = self.xls_write_row(
  245. self.ws, row_position, row_data, row_style=self.style_yellow_bold)
  246. return row_position
  247. # Fill in a row with the code and the name of an account + the partner
  248. # name in case of currency regrouping
  249. def print_row_code_account(self, regroupmode, account, row_position,
  250. partner_name):
  251. if regroupmode == "regroup":
  252. c_specs = [('acc_title', self.nbr_columns, 0, 'text',
  253. ' - '.join([account.code,
  254. account.name,
  255. partner_name or _('No partner')])), ]
  256. else:
  257. c_specs = [
  258. ('acc_title', self.nbr_columns, 0, 'text', ' - '.
  259. join([account.code, account.name])), ]
  260. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  261. row_position = self.xls_write_row(
  262. self.ws, row_position, row_data, self.style_account_title)
  263. return row_position + 1
  264. def print_row_partner(self, row_position, partner_name):
  265. c_specs = [
  266. ('partner', self.nbr_columns, 0, 'text',
  267. partner_name or _('No partner')), ]
  268. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  269. row_position = self.xls_write_row(
  270. self.ws, row_position, row_data, self.style_partner_row)
  271. return row_position
  272. def print_group_currency(self, row_position, curr, _p):
  273. c_specs = [
  274. ('curr', self.nbr_columns, 0, 'text',
  275. curr or _p.company.currency_id.name), ]
  276. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  277. row_position = self.xls_write_row(
  278. self.ws, row_position, row_data, self.style_bold)
  279. return row_position
  280. # Fill in rows of invoice line
  281. def print_lines(self, row_position, account, line, _p, data, line_number):
  282. label_elements = [line.get('lname') or '']
  283. if line.get('invoice_number'):
  284. label_elements.append("(%s)" % (line['invoice_number'],))
  285. label = ' '.join(label_elements)
  286. # Mako: <div class="act_as_row lines
  287. # ${line.get('is_from_previous_periods') and
  288. # 'open_invoice_previous_line' or ''} ${line.get('is_clearance_line')
  289. # and 'clearance_line' or ''}">
  290. if line.get('is_from_previous_periods') \
  291. or line.get('is_clearance_line'):
  292. style_line_default = self.style_default_italic
  293. style_line_right = self.style_right_italic
  294. style_line_date = self.style_date_italic
  295. style_line_decimal = self.style_decimal_italic
  296. else:
  297. style_line_default = self.style_default
  298. style_line_right = self.style_right
  299. style_line_date = self.style_date
  300. style_line_decimal = self.style_decimal
  301. if line.get('ldate'):
  302. c_specs = [('date', 1, 0, 'date', datetime.strptime(
  303. line['ldate'], '%Y-%m-%d'), None, style_line_date)]
  304. else:
  305. c_specs = [('date', 1, 0, 'text', None)]
  306. c_specs += [
  307. ('period_code', 1, 0, 'text', line.get('period_code') or ''),
  308. ('entry', 1, 0, 'text', line.get('move_name') or ''),
  309. ('journal', 1, 0, 'text', line.get('jcode') or ''),
  310. ('partner', 1, 0, 'text', line.get('partner_name') or ''),
  311. ('label', 1, 0, 'text', label),
  312. ('rec', 1, 0, 'text', line.get('rec_name') or ''),
  313. ]
  314. if line.get('date_maturity'):
  315. c_specs += [('datedue', 1, 0, 'date',
  316. datetime.strptime(line['date_maturity'], '%Y-%m-%d'),
  317. None, style_line_date)]
  318. else:
  319. c_specs += [('datedue', 1, 0, 'text', None)]
  320. c_specs += [
  321. ('debit', 1, 0, 'number', line.get('debit') or 0.0, None,
  322. style_line_decimal),
  323. ('credit', 1, 0, 'number', line.get('credit') or 0.0, None,
  324. style_line_decimal),
  325. ]
  326. # determine the formula of the cumulated balance
  327. debit_cell = rowcol_to_cell(row_position, 8)
  328. credit_cell = rowcol_to_cell(row_position, 9)
  329. previous_balance = rowcol_to_cell(row_position - 1, 10)
  330. # if it is the first line, the balance is only debit - credit
  331. if line_number == 1:
  332. cumul_balance = debit_cell + '-' + credit_cell
  333. # cumulate debit - credit and balance of previous line
  334. else:
  335. cumul_balance = debit_cell + '-' + \
  336. credit_cell + '+' + previous_balance
  337. c_specs += [('cumul', 1, 0, 'number', None,
  338. cumul_balance, style_line_decimal)]
  339. if _p.amount_currency(data):
  340. if account.currency_id:
  341. c_specs += [
  342. ('curramount', 1, 0, 'number',
  343. line.get('amount_currency') or 0.0, None,
  344. style_line_decimal),
  345. ('currcode', 1, 0, 'text', line[
  346. 'currency_code'], None, style_line_right),
  347. ]
  348. else:
  349. c_specs += [
  350. ('curramount', 1, 0, 'text', '-', None, style_line_right),
  351. ('currcode', 1, 0, 'text', '', None, style_line_right),
  352. ]
  353. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  354. row_position = self.xls_write_row(
  355. self.ws, row_position, row_data, style_line_default)
  356. return row_position
  357. # Fill in rows of invoice line when the option currency regroup is
  358. # selecself.wsd
  359. def print_group_lines(self, row_position, account, line, _p, line_number):
  360. label_elements = [line.get('lname') or '']
  361. if line.get('invoice_number'):
  362. label_elements.append("(%s)" % (line['invoice_number'],))
  363. label = ' '.join(label_elements)
  364. # Mako: <div class="act_as_row lines
  365. # ${line.get('is_from_previous_periods') and
  366. # 'open_invoice_previous_line' or ''} ${line.get('is_clearance_line')
  367. # and 'clearance_line' or ''}">
  368. if line.get('is_from_previous_periods') or \
  369. line.get('is_clearance_line'):
  370. style_line_default = self.style_default_italic
  371. style_line_right = self.style_right_italic
  372. style_line_date = self.style_date_italic
  373. style_line_decimal = self.style_decimal_italic
  374. else:
  375. style_line_default = self.style_default
  376. style_line_right = self.style_right
  377. style_line_date = self.style_date
  378. style_line_decimal = self.style_decimal
  379. debit_cell = rowcol_to_cell(row_position, 7)
  380. credit_cell = rowcol_to_cell(row_position, 8)
  381. previous_balance = rowcol_to_cell(row_position - 1, 9)
  382. # if it is the first line, the balance is only debit - credit
  383. if line_number == 1:
  384. cumul_balance = debit_cell + '-' + credit_cell
  385. # cumulate devit - credit and balance of previous line
  386. else:
  387. cumul_balance = debit_cell + '-' + \
  388. credit_cell + '+' + previous_balance
  389. if line.get('ldate'):
  390. c_specs = [('date', 1, 0, 'date', datetime.strptime(
  391. line['ldate'], '%Y-%m-%d'), None, style_line_date)]
  392. else:
  393. c_specs = [('date', 1, 0, 'text', None)]
  394. c_specs += [
  395. ('period_code', 1, 0, 'text', line.get('period_code') or ''),
  396. ('entry', 1, 0, 'text', line.get('move_name') or ''),
  397. ('journal', 1, 0, 'text', line.get('jcode') or ''),
  398. ('label', 1, 0, 'text', label),
  399. ('rec', 1, 0, 'text', line.get('rec_name') or ''),
  400. ]
  401. if line.get('date_maturity'):
  402. c_specs += [('datedue', 1, 0, 'date',
  403. datetime.strptime(line['date_maturity'], '%Y-%m-%d'),
  404. None, style_line_date)]
  405. else:
  406. c_specs += [('datedue', 1, 0, 'text', None)]
  407. c_specs += [
  408. ('debit', 1, 0, 'number', line.get('debit') or 0.0, None,
  409. style_line_decimal),
  410. ('credit', 1, 0, 'number', line.get('credit') or 0.0, None,
  411. style_line_decimal),
  412. ('cumul', 1, 0, 'number', None, cumul_balance, style_line_decimal),
  413. ]
  414. if account.currency_id:
  415. c_specs += [
  416. ('curramount', 1, 0, 'number',
  417. line.get('amount_currency') or 0.0, None, style_line_decimal),
  418. ('currcode', 1, 0, 'text',
  419. line.get('currency_code') or '', None, style_line_right),
  420. ]
  421. else:
  422. c_specs += [
  423. ('curramount', 1, 0, 'text', '-', None, style_line_right),
  424. ('currcode', 1, 0, 'text', '', None, style_line_right),
  425. ]
  426. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  427. row_position = self.xls_write_row(
  428. self.ws, row_position, row_data, style_line_default)
  429. return (row_position, cumul_balance)
  430. # print by partner the totals and cumulated balance (Excel formulas)
  431. def print_cumul_partner(self, row_position, row_start_partner, account, _p,
  432. data):
  433. # the text "Cumulated Balance on Partner starts in column 4 when
  434. # selecting the option regroup by currency, 5 in the other case
  435. start_col = 5
  436. debit_partner_start = rowcol_to_cell(row_start_partner, start_col + 3)
  437. debit_partner_end = rowcol_to_cell(row_position - 1, start_col + 3)
  438. debit_partner_total = 'SUM(' + debit_partner_start + \
  439. ':' + debit_partner_end + ')'
  440. credit_partner_start = rowcol_to_cell(row_start_partner, start_col + 4)
  441. credit_partner_end = rowcol_to_cell(row_position - 1, start_col + 4)
  442. credit_partner_total = 'SUM(' + credit_partner_start + \
  443. ':' + credit_partner_end + ')'
  444. bal_curr_start = rowcol_to_cell(row_start_partner, start_col + 6)
  445. bal_curr_end = rowcol_to_cell(row_position - 1, start_col + 6)
  446. cumul_balance_curr = 'SUM(' + bal_curr_start + ':' + bal_curr_end + ')'
  447. bal_partner_debit = rowcol_to_cell(row_position, start_col + 3)
  448. bal_partner_credit = rowcol_to_cell(row_position, start_col + 4)
  449. bal_partner_total = bal_partner_debit + '-' + bal_partner_credit
  450. c_specs = [('empty%s' % x, 1, 0, 'text', None)
  451. for x in range(start_col)]
  452. c_specs += [
  453. ('init_bal', 1, 0, 'text', _('Cumulated Balance on Partner')),
  454. ('rec', 1, 0, 'text', None),
  455. ('empty5', 1, 0, 'text', None),
  456. ('debit', 1, 0, 'number', None,
  457. debit_partner_total, self.style_partner_cumul_decimal),
  458. ('credit', 1, 0, 'number', None,
  459. credit_partner_total, self.style_partner_cumul_decimal),
  460. ('cumul_bal', 1, 0, 'number', None,
  461. bal_partner_total, self.style_partner_cumul_decimal),
  462. ]
  463. if _p.amount_currency(data):
  464. if account.currency_id:
  465. c_specs += [('cumul_bal_curr', 1, 0, 'number', None,
  466. cumul_balance_curr,
  467. self.style_partner_cumul_decimal),
  468. ('curr_name', 1, 0, 'text',
  469. account.currency_id.name,
  470. None, self.style_partner_cumul_right),
  471. ]
  472. else:
  473. c_specs += [('cumul_bal_curr', 1, 0, 'text', '-', None,
  474. self.style_partner_cumul_right),
  475. ('curr_name', 1, 0, 'text', '',
  476. None, self.style_partner_cumul_right)
  477. ]
  478. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  479. row_position = self.xls_write_row(
  480. self.ws, row_position, row_data, self.style_partner_cumul)
  481. return row_position + 1
  482. # print by partner the totals and cumulated balance (Excel formulas) when
  483. # the option currency regroup is selected
  484. def print_group_cumul_partner(self, row_position, row_start_partner,
  485. account, _p, data):
  486. # the text "Cumulated Balance on Partner starts in column 4 when
  487. # selecting the option regroup by currency, 5 in the other case
  488. start_col = 4
  489. debit_partner_start = rowcol_to_cell(row_start_partner, start_col + 3)
  490. debit_partner_end = rowcol_to_cell(row_position - 1, start_col + 3)
  491. debit_partner_total = 'SUM(' + debit_partner_start + \
  492. ':' + debit_partner_end + ')'
  493. credit_partner_start = rowcol_to_cell(row_start_partner, start_col + 4)
  494. credit_partner_end = rowcol_to_cell(row_position - 1, start_col + 4)
  495. credit_partner_total = 'SUM(' + credit_partner_start + \
  496. ':' + credit_partner_end + ')'
  497. bal_curr_start = rowcol_to_cell(row_start_partner, start_col + 5)
  498. bal_curr_end = rowcol_to_cell(row_position - 1, start_col + 5)
  499. cumul_balance_curr = 'SUM(' + bal_curr_start + ':' + bal_curr_end + ')'
  500. bal_partner_debit = rowcol_to_cell(row_position, start_col + 3)
  501. bal_partner_credit = rowcol_to_cell(row_position, start_col + 4)
  502. bal_partner_total = bal_partner_debit + '-' + bal_partner_credit
  503. c_specs = [('empty%s' % x, 1, 0, 'text', None)
  504. for x in range(start_col)]
  505. c_specs += [
  506. # , style_bold_italic),
  507. ('init_bal', 1, 0, 'text', _('Cumulated Balance on Partner')),
  508. ('rec', 1, 0, 'text', None),
  509. ('empty5', 1, 0, 'text', None),
  510. ('debit', 1, 0, 'number', None,
  511. debit_partner_total, self.style_partner_cumul_decimal),
  512. ('credit', 1, 0, 'number', None,
  513. credit_partner_total, self.style_partner_cumul_decimal),
  514. ('cumul_bal', 1, 0, 'number', None,
  515. bal_partner_total, self.style_partner_cumul_decimal),
  516. ]
  517. if account.currency_id:
  518. c_specs += [
  519. ('cumul_bal_curr', 1, 0, 'number', None,
  520. cumul_balance_curr, self.style_partner_cumul_decimal),
  521. ('curr_name', 1, 0, 'text', account.currency_id.name,
  522. None, self.style_partner_cumul_right),
  523. ]
  524. else:
  525. c_specs += [
  526. ('cumul_bal_curr', 1, 0, 'text', "-",
  527. None, self.style_partner_cumul_right),
  528. ('curr_name', 1, 0, 'text', "",
  529. None, self.style_partner_cumul_right),
  530. ]
  531. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  532. row_position = self.xls_write_row(
  533. self.ws, row_position, row_data, self.style_partner_cumul)
  534. return row_position + 1
  535. # print by account the totals of the credit and debit + balance calculation
  536. def print_cumul_account(self, row_position, row_start_account, account, _p,
  537. data):
  538. # This procedure will create an Excel sumif function that will check
  539. # in the column "label" for the "Cumulated Balance.." string and make a
  540. # sum of the debit & credit data
  541. # the text "Cumulated Balance on Partner starts in column 4 when
  542. # selecting the option regroup by currency, 5 in the other case
  543. start_col = 5
  544. # range in which we search for the text "Cumulated Balance on Partner"
  545. reference_start = rowcol_to_cell(row_start_account, start_col)
  546. reference_stop = rowcol_to_cell(row_position - 1, start_col)
  547. # range in which we make the sum of all the cumulated balance lines
  548. # (debit)
  549. range_debit_start = rowcol_to_cell(row_start_account, start_col + 3)
  550. range_debit_stop = rowcol_to_cell(row_position - 1, start_col + 3)
  551. # range in which we make the sum of all the cumulated balance lines
  552. # (crebit)
  553. range_credit_start = rowcol_to_cell(row_start_account, start_col + 4)
  554. range_credit_stop = rowcol_to_cell(row_position - 1, start_col + 4)
  555. search_key = _('Cumulated Balance on Partner')
  556. total_debit_account = 'SUMIF(' + reference_start + ':' + \
  557. reference_stop + ';"' + search_key + '";' + range_debit_start + \
  558. ':' + range_debit_stop + ')'
  559. total_credit_account = 'SUMIF(' + reference_start + ':' + \
  560. reference_stop + ';"' + search_key + '";' + range_credit_start + \
  561. ':' + range_credit_stop + ')'
  562. bal_account_debit = rowcol_to_cell(row_position, start_col + 3)
  563. bal_account_credit = rowcol_to_cell(row_position, start_col + 4)
  564. bal_account_total = bal_account_debit + '-' + bal_account_credit
  565. bal_curr_start = rowcol_to_cell(row_start_account, start_col + 6)
  566. bal_curr_end = rowcol_to_cell(row_position - 1, start_col + 6)
  567. cumul_balance_curr = 'SUMIF(' + reference_start + ':' + \
  568. reference_stop + ';"' + search_key + '";' + \
  569. bal_curr_start + ':' + bal_curr_end + ')'
  570. c_specs = [
  571. ('acc_title', start_col, 0, 'text',
  572. ' - '.join([account.code, account.name])),
  573. ('init_bal', 2, 0, 'text',
  574. _('Cumulated Balance on Account')),
  575. ('empty2', 1, 0, 'text', None),
  576. ('debit', 1, 0, 'number', None,
  577. total_debit_account, self.style_account_title_decimal),
  578. ('credit', 1, 0, 'number', None,
  579. total_credit_account, self.style_account_title_decimal),
  580. ('balance', 1, 0, 'number', None,
  581. bal_account_total, self.style_account_title_decimal),
  582. ]
  583. if _p.amount_currency(data):
  584. if account.currency_id:
  585. c_specs += [('cumul_bal_curr', 1, 0, 'number', None,
  586. cumul_balance_curr),
  587. ('curr_name', 1, 0, 'text',
  588. account.currency_id.name,
  589. None, self.style_account_title_right),
  590. ]
  591. else:
  592. c_specs += [('cumul_bal_curr', 1, 0, 'text', "-", None,
  593. self.style_account_title_right),
  594. ('curr_name', 1, 0, 'text', "",
  595. None, self.style_account_title_right)
  596. ]
  597. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  598. row_position = self.xls_write_row(
  599. self.ws, row_position, row_data, self.style_account_title)
  600. return row_position + 1
  601. # print by account the totals of the credit and debit + balance calculation
  602. def print_group_cumul_account(self, row_position, row_start_account,
  603. account):
  604. # This procedure will create an Excel sumif function that will check
  605. # in the column "label" for the "Cumulated Balance.." string and make a
  606. # sum of the debit & credit data
  607. # the text "Cumulated Balance on Partner starts in column 4 when
  608. # selecting the option regroup by currency, 5 in the other case
  609. start_col = 4
  610. # range in which we search for the text "Cumulated Balance on Partner"
  611. reference_start = rowcol_to_cell(row_start_account, start_col)
  612. reference_stop = rowcol_to_cell(row_position - 1, start_col)
  613. # range in which we make the sum of all the cumulated balance lines
  614. # (debit)
  615. range_debit_start = rowcol_to_cell(row_start_account, start_col + 3)
  616. range_debit_stop = rowcol_to_cell(row_position - 1, start_col + 3)
  617. # range in which we make the sum of all the cumulated balance lines
  618. # (crebit)
  619. range_credit_start = rowcol_to_cell(row_start_account, start_col + 4)
  620. range_credit_stop = rowcol_to_cell(row_position - 1, start_col + 4)
  621. search_key = _('Cumulated Balance on Partner')
  622. total_debit_account = 'SUMIF(' + reference_start + ':' + \
  623. reference_stop + ';"' + search_key + '";' + range_debit_start + \
  624. ':' + range_debit_stop + ')'
  625. total_credit_account = 'SUMIF(' + reference_start + ':' + \
  626. reference_stop + ';"' + search_key + '";' + range_credit_start + \
  627. ':' + range_credit_stop + ')'
  628. bal_account_debit = rowcol_to_cell(row_position, start_col + 3)
  629. bal_account_credit = rowcol_to_cell(row_position, start_col + 4)
  630. bal_account_total = bal_account_debit + '-' + bal_account_credit
  631. bal_curr_start = rowcol_to_cell(row_start_account, start_col + 6)
  632. bal_curr_end = rowcol_to_cell(row_position - 1, start_col + 6)
  633. cumul_balance_curr = 'SUMIF(' + reference_start + ':' + \
  634. reference_stop + ';"' + search_key + '";' + \
  635. bal_curr_start + ':' + bal_curr_end + ')'
  636. c_specs = [
  637. ('acc_title', start_col, 0, 'text',
  638. ' - '.join([account.code, account.name])),
  639. ('init_bal', 2, 0, 'text',
  640. _('Cumulated Balance on Account')),
  641. ('empty2', 1, 0, 'text', None),
  642. ('debit', 1, 0, 'number', None,
  643. total_debit_account, self.style_account_title_decimal),
  644. ('credit', 1, 0, 'number', None,
  645. total_credit_account, self.style_account_title_decimal),
  646. ('balance', 1, 0, 'number', None,
  647. bal_account_total, self.style_account_title_decimal),
  648. ]
  649. if account.currency_id:
  650. c_specs += [('cumul_bal_curr', 1, 0, 'number', None,
  651. cumul_balance_curr, self.style_account_title_decimal),
  652. ('curr_name', 1, 0, 'text', account.currency_id.name,
  653. None, self.style_account_title_decimal),
  654. ]
  655. else:
  656. c_specs += [('cumul_bal_curr', 1, 0, 'text', "-", None,
  657. self.style_account_title_right),
  658. ('curr_name', 1, 0, 'text', "",
  659. None, self.style_account_title_right)
  660. ]
  661. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  662. row_position = self.xls_write_row(
  663. self.ws, row_position, row_data, self.style_account_title)
  664. return row_position + 1
  665. # export the invoice AR/AP lines when the option currency regroup is
  666. # selected
  667. def print_grouped_line_report(self, row_pos, account, _xs, xlwt, _p, data):
  668. if account.grouped_ledger_lines and \
  669. _p['partners_order'][account.id]:
  670. row_start_account = row_pos
  671. for partner_name, p_id, p_ref, p_name \
  672. in _p['partners_order'][account.id]:
  673. row_pos = self.print_row_code_account(
  674. "regroup", account, row_pos, partner_name)
  675. for curr, grouped_lines in account.grouped_ledger_lines.\
  676. get(p_id, []):
  677. row_pos = self.print_group_currency(row_pos, curr, _p)
  678. # Print row: Titles "Date-Period-Entry-Journal..."
  679. row_pos = self.print_columns_title(
  680. _p, row_pos, data, group_lines=True)
  681. row_pos_start = row_pos
  682. line_number = 0
  683. for line in grouped_lines:
  684. line_number += 1
  685. row_pos, cumul_balance = self.print_group_lines(
  686. row_pos, account, line, _p, line_number)
  687. row_pos = self.print_group_cumul_partner(
  688. row_pos, row_pos_start, account, _p, data)
  689. row_pos = self.print_group_cumul_account(
  690. row_pos, row_start_account, account)
  691. return row_pos
  692. # export the invoice AR/AP lines
  693. def print_ledger_lines(self, row_pos, account, _xs, xlwt, _p, data):
  694. if _p['ledger_lines'][account.id] \
  695. and _p['partners_order'][account.id]:
  696. row_start_account = row_pos
  697. # Print account line: code - account
  698. row_pos = self.print_row_code_account(
  699. "noregroup", account, row_pos, "")
  700. for partner_name, p_id, p_ref, p_name \
  701. in _p['partners_order'][account.id]:
  702. # Print partner row
  703. row_pos = self.print_row_partner(row_pos, partner_name)
  704. # Print row: Titles "Date-Period-Entry-Journal..."
  705. row_pos = self.print_columns_title(
  706. _p, row_pos, data, group_lines=False)
  707. row_pos_start = row_pos
  708. line_number = 0
  709. for line in _p['ledger_lines'][account.id].get(p_id, []):
  710. line_number += 1
  711. # print ledger lines
  712. row_pos = self.print_lines(
  713. row_pos, account, line, _p, data, line_number)
  714. row_pos = self.print_cumul_partner(
  715. row_pos, row_pos_start, account, _p, data)
  716. row_pos = self.print_cumul_account(
  717. row_pos, row_start_account, account, _p, data)
  718. return row_pos
  719. def generate_xls_report(self, _p, _xs, data, objects, wb): # main function
  720. # Initializations
  721. self.global_initializations(wb, _p, xlwt, _xs, objects, data)
  722. row_pos = 0
  723. # Print Title
  724. row_pos = self.print_title(_p, row_pos)
  725. # Print empty row to define column sizes
  726. row_pos = self.print_empty_row(row_pos)
  727. # Print Header Table titles (Fiscal Year - Accounts Filter - Periods
  728. # Filter...)
  729. row_pos = self.print_header_titles(_p, data, row_pos)
  730. # Print Header Table data
  731. row_pos = self.print_header_data(_p, data, row_pos)
  732. # Freeze the line
  733. self.ws.set_horz_split_pos(row_pos)
  734. # Print empty row
  735. row_pos = self.print_empty_row(row_pos)
  736. for acc in objects:
  737. if hasattr(acc, 'grouped_ledger_lines'):
  738. # call xls equivalent of
  739. # "grouped_by_curr_open_invoices_inclusion.mako.html"
  740. row_pos = self.print_grouped_line_report(
  741. row_pos, acc, _xs, xlwt, _p, data)
  742. else:
  743. # call xls equivalent of "open_invoices_inclusion.mako.html"
  744. row_pos = self.print_ledger_lines(
  745. row_pos, acc, _xs, xlwt, _p, data)
  746. row_pos += 1
  747. open_invoices_xls('report.account.account_report_open_invoices_xls',
  748. 'account.account', parser=PartnersOpenInvoicesWebkit)