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.

826 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, account.name, partner_name
  254. or _('No partner')])), ]
  255. else:
  256. c_specs = [
  257. ('acc_title', self.nbr_columns, 0, 'text', ' - '.
  258. join([account.code, account.name])), ]
  259. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  260. row_position = self.xls_write_row(
  261. self.ws, row_position, row_data, self.style_account_title)
  262. return row_position + 1
  263. def print_row_partner(self, row_position, partner_name):
  264. c_specs = [
  265. ('partner', self.nbr_columns, 0, 'text',
  266. partner_name or _('No partner')), ]
  267. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  268. row_position = self.xls_write_row(
  269. self.ws, row_position, row_data, self.style_partner_row)
  270. return row_position
  271. def print_group_currency(self, row_position, curr, _p):
  272. c_specs = [
  273. ('curr', self.nbr_columns, 0, 'text',
  274. curr or _p.company.currency_id.name), ]
  275. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  276. row_position = self.xls_write_row(
  277. self.ws, row_position, row_data, self.style_bold)
  278. return row_position
  279. # Fill in rows of invoice line
  280. def print_lines(self, row_position, account, line, _p, data, line_number):
  281. label_elements = [line.get('lname') or '']
  282. if line.get('invoice_number'):
  283. label_elements.append("(%s)" % (line['invoice_number'],))
  284. label = ' '.join(label_elements)
  285. # Mako: <div class="act_as_row lines
  286. # ${line.get('is_from_previous_periods') and
  287. # 'open_invoice_previous_line' or ''} ${line.get('is_clearance_line')
  288. # and 'clearance_line' or ''}">
  289. if line.get('is_from_previous_periods') \
  290. or line.get('is_clearance_line'):
  291. style_line_default = self.style_default_italic
  292. style_line_right = self.style_right_italic
  293. style_line_date = self.style_date_italic
  294. style_line_decimal = self.style_decimal_italic
  295. else:
  296. style_line_default = self.style_default
  297. style_line_right = self.style_right
  298. style_line_date = self.style_date
  299. style_line_decimal = self.style_decimal
  300. if line.get('ldate'):
  301. c_specs = [('date', 1, 0, 'date', datetime.strptime(
  302. line['ldate'], '%Y-%m-%d'), None, style_line_date)]
  303. else:
  304. c_specs = [('date', 1, 0, 'text', None)]
  305. c_specs += [
  306. ('period_code', 1, 0, 'text', line.get('period_code') or ''),
  307. ('entry', 1, 0, 'text', line.get('move_name') or ''),
  308. ('journal', 1, 0, 'text', line.get('jcode') or ''),
  309. ('partner', 1, 0, 'text', line.get('partner_name') or ''),
  310. ('label', 1, 0, 'text', label),
  311. ('rec', 1, 0, 'text', line.get('rec_name') or ''),
  312. ]
  313. if line.get('date_maturity'):
  314. c_specs += [('datedue', 1, 0, 'date',
  315. datetime.strptime(line['date_maturity'], '%Y-%m-%d'),
  316. None, style_line_date)]
  317. else:
  318. c_specs += [('datedue', 1, 0, 'text', None)]
  319. c_specs += [
  320. ('debit', 1, 0, 'number', line.get('debit')
  321. or 0.0, None, style_line_decimal),
  322. ('credit', 1, 0, 'number', line.get('credit')
  323. or 0.0, None, style_line_decimal),
  324. ]
  325. # determine the formula of the cumulated balance
  326. debit_cell = rowcol_to_cell(row_position, 8)
  327. credit_cell = rowcol_to_cell(row_position, 9)
  328. previous_balance = rowcol_to_cell(row_position - 1, 10)
  329. # if it is the first line, the balance is only debit - credit
  330. if line_number == 1:
  331. cumul_balance = debit_cell + '-' + credit_cell
  332. # cumulate debit - credit and balance of previous line
  333. else:
  334. cumul_balance = debit_cell + '-' + \
  335. credit_cell + '+' + previous_balance
  336. c_specs += [('cumul', 1, 0, 'number', None,
  337. cumul_balance, style_line_decimal)]
  338. if _p.amount_currency(data):
  339. if account.currency_id:
  340. c_specs += [
  341. ('curramount', 1, 0, 'number', line.get('amount_currency')
  342. or 0.0, None, style_line_decimal),
  343. ('currcode', 1, 0, 'text', line[
  344. 'currency_code'], None, style_line_right),
  345. ]
  346. else:
  347. c_specs += [
  348. ('curramount', 1, 0, 'text', '-', None, style_line_right),
  349. ('currcode', 1, 0, 'text', '', None, style_line_right),
  350. ]
  351. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  352. row_position = self.xls_write_row(
  353. self.ws, row_position, row_data, style_line_default)
  354. return row_position
  355. # Fill in rows of invoice line when the option currency regroup is
  356. # selecself.wsd
  357. def print_group_lines(self, row_position, account, line, _p, line_number):
  358. label_elements = [line.get('lname') or '']
  359. if line.get('invoice_number'):
  360. label_elements.append("(%s)" % (line['invoice_number'],))
  361. label = ' '.join(label_elements)
  362. # Mako: <div class="act_as_row lines
  363. # ${line.get('is_from_previous_periods') and
  364. # 'open_invoice_previous_line' or ''} ${line.get('is_clearance_line')
  365. # and 'clearance_line' or ''}">
  366. if line.get('is_from_previous_periods') or \
  367. line.get('is_clearance_line'):
  368. style_line_default = self.style_default_italic
  369. style_line_right = self.style_right_italic
  370. style_line_date = self.style_date_italic
  371. style_line_decimal = self.style_decimal_italic
  372. else:
  373. style_line_default = self.style_default
  374. style_line_right = self.style_right
  375. style_line_date = self.style_date
  376. style_line_decimal = self.style_decimal
  377. debit_cell = rowcol_to_cell(row_position, 7)
  378. credit_cell = rowcol_to_cell(row_position, 8)
  379. previous_balance = rowcol_to_cell(row_position - 1, 9)
  380. # if it is the first line, the balance is only debit - credit
  381. if line_number == 1:
  382. cumul_balance = debit_cell + '-' + credit_cell
  383. # cumulate devit - credit and balance of previous line
  384. else:
  385. cumul_balance = debit_cell + '-' + \
  386. credit_cell + '+' + previous_balance
  387. if line.get('ldate'):
  388. c_specs = [('date', 1, 0, 'date', datetime.strptime(
  389. line['ldate'], '%Y-%m-%d'), None, style_line_date)]
  390. else:
  391. c_specs = [('date', 1, 0, 'text', None)]
  392. c_specs += [
  393. ('period_code', 1, 0, 'text', line.get('period_code') or ''),
  394. ('entry', 1, 0, 'text', line.get('move_name') or ''),
  395. ('journal', 1, 0, 'text', line.get('jcode') or ''),
  396. ('label', 1, 0, 'text', label),
  397. ('rec', 1, 0, 'text', line.get('rec_name') or ''),
  398. ]
  399. if line.get('date_maturity'):
  400. c_specs += [('datedue', 1, 0, 'date',
  401. datetime.strptime(line['date_maturity'], '%Y-%m-%d'),
  402. None, style_line_date)]
  403. else:
  404. c_specs += [('datedue', 1, 0, 'text', None)]
  405. c_specs += [
  406. ('debit', 1, 0, 'number', line.get('debit')
  407. or 0.0, None, style_line_decimal),
  408. ('credit', 1, 0, 'number', line.get('credit')
  409. or 0.0, None, style_line_decimal),
  410. ('cumul', 1, 0, 'number', None, cumul_balance, style_line_decimal),
  411. ]
  412. if account.currency_id:
  413. c_specs += [
  414. ('curramount', 1, 0, 'number', line.get('amount_currency')
  415. or 0.0, None, style_line_decimal),
  416. ('currcode', 1, 0, 'text', line.get('currency_code')
  417. or '', None, style_line_right),
  418. ]
  419. else:
  420. c_specs += [
  421. ('curramount', 1, 0, 'text', '-', None, style_line_right),
  422. ('currcode', 1, 0, 'text', '', None, style_line_right),
  423. ]
  424. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  425. row_position = self.xls_write_row(
  426. self.ws, row_position, row_data, style_line_default)
  427. return (row_position, cumul_balance)
  428. # print by partner the totals and cumulated balance (Excel formulas)
  429. def print_cumul_partner(self, row_position, row_start_partner, account, _p,
  430. data):
  431. # the text "Cumulated Balance on Partner starts in column 4 when
  432. # selecting the option regroup by currency, 5 in the other case
  433. start_col = 5
  434. debit_partner_start = rowcol_to_cell(row_start_partner, start_col + 3)
  435. debit_partner_end = rowcol_to_cell(row_position - 1, start_col + 3)
  436. debit_partner_total = 'SUM(' + debit_partner_start + \
  437. ':' + debit_partner_end + ')'
  438. credit_partner_start = rowcol_to_cell(row_start_partner, start_col + 4)
  439. credit_partner_end = rowcol_to_cell(row_position - 1, start_col + 4)
  440. credit_partner_total = 'SUM(' + credit_partner_start + \
  441. ':' + credit_partner_end + ')'
  442. bal_curr_start = rowcol_to_cell(row_start_partner, start_col + 6)
  443. bal_curr_end = rowcol_to_cell(row_position - 1, start_col + 6)
  444. cumul_balance_curr = 'SUM(' + bal_curr_start + ':' + bal_curr_end + ')'
  445. bal_partner_debit = rowcol_to_cell(row_position, start_col + 3)
  446. bal_partner_credit = rowcol_to_cell(row_position, start_col + 4)
  447. bal_partner_total = bal_partner_debit + '-' + bal_partner_credit
  448. c_specs = [('empty%s' % x, 1, 0, 'text', None)
  449. for x in range(start_col)]
  450. c_specs += [
  451. ('init_bal', 1, 0, 'text', _('Cumulated Balance on Partner')),
  452. ('rec', 1, 0, 'text', None),
  453. ('empty5', 1, 0, 'text', None),
  454. ('debit', 1, 0, 'number', None,
  455. debit_partner_total, self.style_partner_cumul_decimal),
  456. ('credit', 1, 0, 'number', None,
  457. credit_partner_total, self.style_partner_cumul_decimal),
  458. ('cumul_bal', 1, 0, 'number', None,
  459. bal_partner_total, self.style_partner_cumul_decimal),
  460. ]
  461. if _p.amount_currency(data):
  462. if account.currency_id:
  463. c_specs += [('cumul_bal_curr', 1, 0, 'number', None,
  464. cumul_balance_curr,
  465. self.style_partner_cumul_decimal),
  466. ('curr_name', 1, 0, 'text',
  467. account.currency_id.name,
  468. None, self.style_partner_cumul_right),
  469. ]
  470. else:
  471. c_specs += [('cumul_bal_curr', 1, 0, 'text', '-', None,
  472. self.style_partner_cumul_right),
  473. ('curr_name', 1, 0, 'text', '',
  474. None, self.style_partner_cumul_right)
  475. ]
  476. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  477. row_position = self.xls_write_row(
  478. self.ws, row_position, row_data, self.style_partner_cumul)
  479. return row_position + 1
  480. # print by partner the totals and cumulated balance (Excel formulas) when
  481. # the option currency regroup is selected
  482. def print_group_cumul_partner(self, row_position, row_start_partner,
  483. account, _p, data):
  484. # the text "Cumulated Balance on Partner starts in column 4 when
  485. # selecting the option regroup by currency, 5 in the other case
  486. start_col = 4
  487. debit_partner_start = rowcol_to_cell(row_start_partner, start_col + 3)
  488. debit_partner_end = rowcol_to_cell(row_position - 1, start_col + 3)
  489. debit_partner_total = 'SUM(' + debit_partner_start + \
  490. ':' + debit_partner_end + ')'
  491. credit_partner_start = rowcol_to_cell(row_start_partner, start_col + 4)
  492. credit_partner_end = rowcol_to_cell(row_position - 1, start_col + 4)
  493. credit_partner_total = 'SUM(' + credit_partner_start + \
  494. ':' + credit_partner_end + ')'
  495. bal_curr_start = rowcol_to_cell(row_start_partner, start_col + 5)
  496. bal_curr_end = rowcol_to_cell(row_position - 1, start_col + 5)
  497. cumul_balance_curr = 'SUM(' + bal_curr_start + ':' + bal_curr_end + ')'
  498. bal_partner_debit = rowcol_to_cell(row_position, start_col + 3)
  499. bal_partner_credit = rowcol_to_cell(row_position, start_col + 4)
  500. bal_partner_total = bal_partner_debit + '-' + bal_partner_credit
  501. c_specs = [('empty%s' % x, 1, 0, 'text', None)
  502. for x in range(start_col)]
  503. c_specs += [
  504. # , style_bold_italic),
  505. ('init_bal', 1, 0, 'text', _('Cumulated Balance on Partner')),
  506. ('rec', 1, 0, 'text', None),
  507. ('empty5', 1, 0, 'text', None),
  508. ('debit', 1, 0, 'number', None,
  509. debit_partner_total, self.style_partner_cumul_decimal),
  510. ('credit', 1, 0, 'number', None,
  511. credit_partner_total, self.style_partner_cumul_decimal),
  512. ('cumul_bal', 1, 0, 'number', None,
  513. bal_partner_total, self.style_partner_cumul_decimal),
  514. ]
  515. if account.currency_id:
  516. c_specs += [
  517. ('cumul_bal_curr', 1, 0, 'number', None,
  518. cumul_balance_curr, self.style_partner_cumul_decimal),
  519. ('curr_name', 1, 0, 'text', account.currency_id.name,
  520. None, self.style_partner_cumul_right),
  521. ]
  522. else:
  523. c_specs += [
  524. ('cumul_bal_curr', 1, 0, 'text', "-",
  525. None, self.style_partner_cumul_right),
  526. ('curr_name', 1, 0, 'text', "",
  527. None, self.style_partner_cumul_right),
  528. ]
  529. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  530. row_position = self.xls_write_row(
  531. self.ws, row_position, row_data, self.style_partner_cumul)
  532. return row_position + 1
  533. # print by account the totals of the credit and debit + balance calculation
  534. def print_cumul_account(self, row_position, row_start_account, account, _p,
  535. data):
  536. # This procedure will create an Excel sumif function that will check
  537. # in the column "label" for the "Cumulated Balance.." string and make a
  538. # sum of the debit & credit data
  539. # the text "Cumulated Balance on Partner starts in column 4 when
  540. # selecting the option regroup by currency, 5 in the other case
  541. start_col = 5
  542. # range in which we search for the text "Cumulated Balance on Partner"
  543. reference_start = rowcol_to_cell(row_start_account, start_col)
  544. reference_stop = rowcol_to_cell(row_position - 1, start_col)
  545. # range in which we make the sum of all the cumulated balance lines
  546. # (debit)
  547. range_debit_start = rowcol_to_cell(row_start_account, start_col + 3)
  548. range_debit_stop = rowcol_to_cell(row_position - 1, start_col + 3)
  549. # range in which we make the sum of all the cumulated balance lines
  550. # (crebit)
  551. range_credit_start = rowcol_to_cell(row_start_account, start_col + 4)
  552. range_credit_stop = rowcol_to_cell(row_position - 1, start_col + 4)
  553. search_key = _('Cumulated Balance on Partner')
  554. total_debit_account = 'SUMIF(' + reference_start + ':' + \
  555. reference_stop + ';"' + search_key + '";' + range_debit_start + \
  556. ':' + range_debit_stop + ')'
  557. total_credit_account = 'SUMIF(' + reference_start + ':' + \
  558. reference_stop + ';"' + search_key + '";' + range_credit_start + \
  559. ':' + range_credit_stop + ')'
  560. bal_account_debit = rowcol_to_cell(row_position, start_col + 3)
  561. bal_account_credit = rowcol_to_cell(row_position, start_col + 4)
  562. bal_account_total = bal_account_debit + '-' + bal_account_credit
  563. bal_curr_start = rowcol_to_cell(row_start_account, start_col + 6)
  564. bal_curr_end = rowcol_to_cell(row_position - 1, start_col + 6)
  565. cumul_balance_curr = 'SUMIF(' + reference_start + ':' + \
  566. reference_stop + ';"' + search_key + '";' + \
  567. bal_curr_start + ':' + bal_curr_end + ')'
  568. c_specs = [
  569. ('acc_title', start_col, 0, 'text',
  570. ' - '.join([account.code, account.name])),
  571. ('init_bal', 2, 0, 'text',
  572. _('Cumulated Balance on Account')),
  573. ('empty2', 1, 0, 'text', None),
  574. ('debit', 1, 0, 'number', None,
  575. total_debit_account, self.style_account_title_decimal),
  576. ('credit', 1, 0, 'number', None,
  577. total_credit_account, self.style_account_title_decimal),
  578. ('balance', 1, 0, 'number', None,
  579. bal_account_total, self.style_account_title_decimal),
  580. ]
  581. if _p.amount_currency(data):
  582. if account.currency_id:
  583. c_specs += [('cumul_bal_curr', 1, 0, 'number', None,
  584. cumul_balance_curr),
  585. ('curr_name', 1, 0, 'text',
  586. account.currency_id.name,
  587. None, self.style_account_title_right),
  588. ]
  589. else:
  590. c_specs += [('cumul_bal_curr', 1, 0, 'text', "-", None,
  591. self.style_account_title_right),
  592. ('curr_name', 1, 0, 'text', "",
  593. None, self.style_account_title_right)
  594. ]
  595. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  596. row_position = self.xls_write_row(
  597. self.ws, row_position, row_data, self.style_account_title)
  598. return row_position + 1
  599. # print by account the totals of the credit and debit + balance calculation
  600. def print_group_cumul_account(self, row_position, row_start_account,
  601. account):
  602. # This procedure will create an Excel sumif function that will check
  603. # in the column "label" for the "Cumulated Balance.." string and make a
  604. # sum of the debit & credit data
  605. # the text "Cumulated Balance on Partner starts in column 4 when
  606. # selecting the option regroup by currency, 5 in the other case
  607. start_col = 4
  608. # range in which we search for the text "Cumulated Balance on Partner"
  609. reference_start = rowcol_to_cell(row_start_account, start_col)
  610. reference_stop = rowcol_to_cell(row_position - 1, start_col)
  611. # range in which we make the sum of all the cumulated balance lines
  612. # (debit)
  613. range_debit_start = rowcol_to_cell(row_start_account, start_col + 3)
  614. range_debit_stop = rowcol_to_cell(row_position - 1, start_col + 3)
  615. # range in which we make the sum of all the cumulated balance lines
  616. # (crebit)
  617. range_credit_start = rowcol_to_cell(row_start_account, start_col + 4)
  618. range_credit_stop = rowcol_to_cell(row_position - 1, start_col + 4)
  619. search_key = _('Cumulated Balance on Partner')
  620. total_debit_account = 'SUMIF(' + reference_start + ':' + \
  621. reference_stop + ';"' + search_key + '";' + range_debit_start + \
  622. ':' + range_debit_stop + ')'
  623. total_credit_account = 'SUMIF(' + reference_start + ':' + \
  624. reference_stop + ';"' + search_key + '";' + range_credit_start + \
  625. ':' + range_credit_stop + ')'
  626. bal_account_debit = rowcol_to_cell(row_position, start_col + 3)
  627. bal_account_credit = rowcol_to_cell(row_position, start_col + 4)
  628. bal_account_total = bal_account_debit + '-' + bal_account_credit
  629. bal_curr_start = rowcol_to_cell(row_start_account, start_col + 6)
  630. bal_curr_end = rowcol_to_cell(row_position - 1, start_col + 6)
  631. cumul_balance_curr = 'SUMIF(' + reference_start + ':' + \
  632. reference_stop + ';"' + search_key + '";' + \
  633. bal_curr_start + ':' + bal_curr_end + ')'
  634. c_specs = [
  635. ('acc_title', start_col, 0, 'text',
  636. ' - '.join([account.code, account.name])),
  637. ('init_bal', 2, 0, 'text',
  638. _('Cumulated Balance on Account')),
  639. ('empty2', 1, 0, 'text', None),
  640. ('debit', 1, 0, 'number', None,
  641. total_debit_account, self.style_account_title_decimal),
  642. ('credit', 1, 0, 'number', None,
  643. total_credit_account, self.style_account_title_decimal),
  644. ('balance', 1, 0, 'number', None,
  645. bal_account_total, self.style_account_title_decimal),
  646. ]
  647. if account.currency_id:
  648. c_specs += [('cumul_bal_curr', 1, 0, 'number', None,
  649. cumul_balance_curr, self.style_account_title_decimal),
  650. ('curr_name', 1, 0, 'text', account.currency_id.name,
  651. None, self.style_account_title_decimal),
  652. ]
  653. else:
  654. c_specs += [('cumul_bal_curr', 1, 0, 'text', "-", None,
  655. self.style_account_title_right),
  656. ('curr_name', 1, 0, 'text', "",
  657. None, self.style_account_title_right)
  658. ]
  659. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  660. row_position = self.xls_write_row(
  661. self.ws, row_position, row_data, self.style_account_title)
  662. return row_position + 1
  663. # export the invoice AR/AP lines when the option currency regroup is
  664. # selected
  665. def print_grouped_line_report(self, row_pos, account, _xs, xlwt, _p, data):
  666. if account.grouped_ledger_lines and \
  667. _p['partners_order'][account.id]:
  668. row_start_account = row_pos
  669. for partner_name, p_id, p_ref, p_name \
  670. in _p['partners_order'][account.id]:
  671. row_pos = self.print_row_code_account(
  672. "regroup", account, row_pos, partner_name)
  673. for curr, grouped_lines in account.grouped_ledger_lines.\
  674. get(p_id, []):
  675. row_pos = self.print_group_currency(row_pos, curr, _p)
  676. # Print row: Titles "Date-Period-Entry-Journal..."
  677. row_pos = self.print_columns_title(
  678. _p, row_pos, data, group_lines=True)
  679. row_pos_start = row_pos
  680. line_number = 0
  681. for line in grouped_lines:
  682. line_number += 1
  683. row_pos, cumul_balance = self.print_group_lines(
  684. row_pos, account, line, _p, line_number)
  685. row_pos = self.print_group_cumul_partner(
  686. row_pos, row_pos_start, account, _p, data)
  687. row_pos = self.print_group_cumul_account(
  688. row_pos, row_start_account, account)
  689. return row_pos
  690. # export the invoice AR/AP lines
  691. def print_ledger_lines(self, row_pos, account, _xs, xlwt, _p, data):
  692. if _p['ledger_lines'][account.id] \
  693. and _p['partners_order'][account.id]:
  694. row_start_account = row_pos
  695. # Print account line: code - account
  696. row_pos = self.print_row_code_account(
  697. "noregroup", account, row_pos, "")
  698. for partner_name, p_id, p_ref, p_name \
  699. in _p['partners_order'][account.id]:
  700. # Print partner row
  701. row_pos = self.print_row_partner(row_pos, partner_name)
  702. # Print row: Titles "Date-Period-Entry-Journal..."
  703. row_pos = self.print_columns_title(
  704. _p, row_pos, data, group_lines=False)
  705. row_pos_start = row_pos
  706. line_number = 0
  707. for line in _p['ledger_lines'][account.id].get(p_id, []):
  708. line_number += 1
  709. # print ledger lines
  710. row_pos = self.print_lines(
  711. row_pos, account, line, _p, data, line_number)
  712. row_pos = self.print_cumul_partner(
  713. row_pos, row_pos_start, account, _p, data)
  714. row_pos = self.print_cumul_account(
  715. row_pos, row_start_account, account, _p, data)
  716. return row_pos
  717. def generate_xls_report(self, _p, _xs, data, objects, wb): # main function
  718. # Initializations
  719. self.global_initializations(wb, _p, xlwt, _xs, objects, data)
  720. row_pos = 0
  721. # Print Title
  722. row_pos = self.print_title(_p, row_pos)
  723. # Print empty row to define column sizes
  724. row_pos = self.print_empty_row(row_pos)
  725. # Print Header Table titles (Fiscal Year - Accounts Filter - Periods
  726. # Filter...)
  727. row_pos = self.print_header_titles(_p, data, row_pos)
  728. # Print Header Table data
  729. row_pos = self.print_header_data(_p, data, row_pos)
  730. # Freeze the line
  731. self.ws.set_horz_split_pos(row_pos)
  732. # Print empty row
  733. row_pos = self.print_empty_row(row_pos)
  734. for acc in objects:
  735. if hasattr(acc, 'grouped_ledger_lines'):
  736. # call xls equivalent of
  737. # "grouped_by_curr_open_invoices_inclusion.mako.html"
  738. row_pos = self.print_grouped_line_report(
  739. row_pos, acc, _xs, xlwt, _p, data)
  740. else:
  741. # call xls equivalent of "open_invoices_inclusion.mako.html"
  742. row_pos = self.print_ledger_lines(
  743. row_pos, acc, _xs, xlwt, _p, data)
  744. row_pos += 1
  745. open_invoices_xls('report.account.account_report_open_invoices_xls',
  746. 'account.account', parser=PartnersOpenInvoicesWebkit)