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.

627 lines
38 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. #
  6. # Copyright (c) 2013 Noviat nv/sa (www.noviat.com). All rights reserved.
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. import xlwt
  23. import time
  24. from datetime import datetime
  25. from openerp.report import report_sxw
  26. from openerp.addons.report_xls.report_xls import report_xls
  27. from openerp.addons.report_xls.utils import rowcol_to_cell
  28. from openerp.addons.account_financial_report_webkit.report.open_invoices import PartnersOpenInvoicesWebkit
  29. from openerp.tools.translate import _
  30. #import logging
  31. #_logger = logging.getLogger(__name__)
  32. class open_invoices_xls(report_xls):
  33. column_sizes = [12,12,20,15,30,30,14,14,14,14,14,14,10]
  34. def global_initializations(self, wb, _p, xlwt, _xs, objects, data):
  35. # this procedure will initialise variables and Excel cell styles and return them as global ones
  36. global ws
  37. ws = wb.add_sheet(_p.report_name[:31])
  38. ws.panes_frozen = True
  39. ws.remove_splits = True
  40. ws.portrait = 0 # Landscape
  41. ws.fit_width_to_pages = 1
  42. ws.header_str = self.xls_headers['standard']
  43. ws.footer_str = self.xls_footers['standard']
  44. #-------------------------------------------------------
  45. global nbr_columns #number of columns is 11 in case of normal report, 13 in case the option currency is selected and 12 in case of the regroup by currency option is checked
  46. group_lines = False
  47. for acc in objects: #search if the regroup option is selected by browsing the accounts defined in objects - see account_report_open_invoices.mako
  48. if hasattr(acc, 'grouped_ledger_lines'):
  49. group_lines = True
  50. if group_lines:
  51. nbr_columns = 12
  52. elif _p.amount_currency(data) and not group_lines:
  53. nbr_columns = 13
  54. else:
  55. nbr_columns = 11
  56. #-------------------------------------------------------
  57. global style_font12 #cell style for report title
  58. style_font12 = xlwt.easyxf(_xs['xls_title'])
  59. #-------------------------------------------------------
  60. global style_default
  61. style_default = xlwt.easyxf(_xs['borders_all'])
  62. #-------------------------------------------------------
  63. global style_default_italic
  64. style_default_italic = xlwt.easyxf(_xs['borders_all'] + _xs['italic'])
  65. #-------------------------------------------------------
  66. global style_bold
  67. style_bold = xlwt.easyxf(_xs['bold'] + _xs['borders_all'])
  68. #-------------------------------------------------------
  69. global style_bold_center
  70. style_bold_center = xlwt.easyxf(_xs['bold'] + _xs['borders_all'] + _xs['center'])
  71. #-------------------------------------------------------
  72. global style_bold_italic
  73. style_bold_italic = xlwt.easyxf(_xs['bold'] + _xs['borders_all'] + _xs['italic'])
  74. #-------------------------------------------------------
  75. global style_bold_italic_decimal
  76. style_bold_italic_decimal = xlwt.easyxf(_xs['bold'] + _xs['borders_all'] + _xs['italic'] + _xs['right'], num_format_str = report_xls.decimal_format)
  77. #-------------------------------------------------------
  78. global style_bold_blue
  79. style_bold_blue = xlwt.easyxf(_xs['bold'] + _xs['fill_blue'] + _xs['borders_all'] )
  80. #-------------------------------------------------------
  81. global style_bold_blue_italic_decimal
  82. style_bold_blue_italic_decimal = xlwt.easyxf(_xs['bold'] + _xs['fill_blue'] + _xs['borders_all'] + _xs['italic'], num_format_str = report_xls.decimal_format)
  83. #-------------------------------------------------------
  84. global style_bold_blue_center #cell style for header titles: 'Chart of accounts' - 'Fiscal year' ...
  85. style_bold_blue_center= xlwt.easyxf(_xs['bold'] + _xs['fill_blue'] + _xs['borders_all'] + _xs['center'])
  86. #-------------------------------------------------------
  87. global style_center #cell style for header data: 'Chart of accounts' - 'Fiscal year' ...
  88. style_center = xlwt.easyxf(_xs['borders_all'] + _xs['wrap'] + _xs['center'])
  89. #-------------------------------------------------------
  90. global style_yellow_bold #cell style for columns titles 'Date'- 'Period' - 'Entry'...
  91. style_yellow_bold = xlwt.easyxf(_xs['bold'] + _xs['fill'] + _xs['borders_all'])
  92. #-------------------------------------------------------
  93. global style_yellow_bold_right #cell style for columns titles 'Date'- 'Period' - 'Entry'...
  94. style_yellow_bold_right = xlwt.easyxf(_xs['bold'] + _xs['fill'] + _xs['borders_all'] + _xs['right'])
  95. #-------------------------------------------------------
  96. global style_right
  97. style_right = xlwt.easyxf(_xs['borders_all'] + _xs['right'])
  98. #-------------------------------------------------------
  99. global style_right_italic
  100. style_right_italic = xlwt.easyxf(_xs['borders_all'] + _xs['right'] + _xs['italic'])
  101. #-------------------------------------------------------
  102. global style_decimal
  103. style_decimal = xlwt.easyxf(_xs['borders_all'] + _xs['right'], num_format_str = report_xls.decimal_format)
  104. #-------------------------------------------------------
  105. global style_decimal_italic
  106. style_decimal_italic = xlwt.easyxf(_xs['borders_all'] + _xs['right'] + _xs['italic'], num_format_str = report_xls.decimal_format)
  107. #-------------------------------------------------------
  108. global style_date
  109. style_date = xlwt.easyxf(_xs['borders_all'] + _xs['left'], num_format_str = report_xls.date_format)
  110. #-------------------------------------------------------
  111. global style_date_italic
  112. style_date_italic = xlwt.easyxf(_xs['borders_all'] + _xs['left'] + _xs['italic'], num_format_str = report_xls.date_format)
  113. #-------------------------------------------------------
  114. global style_account_title, style_account_title_right, style_account_title_decimal
  115. cell_format = _xs['xls_title'] + _xs['bold'] + _xs['fill'] + _xs['borders_all']
  116. style_account_title = xlwt.easyxf(cell_format)
  117. style_account_title_right = xlwt.easyxf(cell_format + _xs['right'])
  118. style_account_title_decimal = xlwt.easyxf(cell_format + _xs['right'], num_format_str = report_xls.decimal_format)
  119. #-------------------------------------------------------
  120. global style_partner_row
  121. cell_format = _xs['bold']
  122. style_partner_row = xlwt.easyxf(cell_format)
  123. #-------------------------------------------------------
  124. global style_partner_cumul, style_partner_cumul_right, style_partner_cumul_center, style_partner_cumul_decimal
  125. cell_format = _xs['bold'] + _xs['fill'] + _xs['borders_all']
  126. style_partner_cumul = xlwt.easyxf(cell_format)
  127. style_partner_cumul_right = xlwt.easyxf(cell_format + _xs['right'])
  128. style_partner_cumul_center = xlwt.easyxf(cell_format + _xs['center'])
  129. style_partner_cumul_decimal = xlwt.easyxf(cell_format + _xs['right'], num_format_str = report_xls.decimal_format)
  130. def print_title(self, _p, row_position): # print the first line "OPEN INVOICE REPORT - db name - Currency
  131. report_name = ' - '.join([_p.report_name.upper(), _p.company.partner_id.name, _p.company.currency_id.name])
  132. c_specs = [('report_name', 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(ws, row_position, row_data, row_style=style_font12)
  135. return row_position
  136. def print_empty_row(self, row_position): #send an empty row to the Excel document
  137. c_sizes = self.column_sizes
  138. c_specs = [('empty%s'%i, 1, c_sizes[i], 'text', None) for i in range(0,len(c_sizes))]
  139. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  140. row_position = self.xls_write_row(ws, row_position, row_data, set_column_size=True)
  141. return row_position
  142. def print_header_titles(self, _p, data, row_position): #Fill in the titles of the header summary tables: Chart of account - Fiscal year - ...
  143. c_specs = [
  144. ('coa', 2, 0, 'text', _('Chart of Account'), None, style_bold_blue_center),
  145. ('fy', 2, 0, 'text', _('Fiscal Year'), None, style_bold_blue_center),
  146. ('df', 2, 0, 'text', _p.filter_form(data) == 'filter_date' and _('Dates Filter') or _('Periods Filter'), None, style_bold_blue_center),
  147. ('cd', 1 if nbr_columns == 11 else 2 , 0, 'text', _('Clearance Date'), None, style_bold_blue_center),
  148. ('af', 2, 0, 'text', _('Accounts Filter'), None, style_bold_blue_center),
  149. ('tm', 3 if nbr_columns == 13 else 2, 0, 'text', _('Target Moves'), None, style_bold_blue_center),
  150. ]
  151. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  152. row_position = self.xls_write_row(ws, row_position, row_data, row_style=style_bold_blue_center)
  153. return row_position
  154. def print_header_data(self, _p, data, row_position): #Fill in the data of the header summary tables: Chart of account - Fiscal year - ...
  155. c_specs = [
  156. ('coa', 2, 0, 'text', _p.chart_account.name, None, style_center),
  157. ('fy', 2, 0, 'text', _p.fiscalyear.name if _p.fiscalyear else '-', None, style_center),
  158. ]
  159. df = _('From') + ': '
  160. if _p.filter_form(data) == 'filter_date':
  161. df += _p.start_date if _p.start_date else u''
  162. else:
  163. df += _p.start_period.name if _p.start_period else u''
  164. df += ' ' + _('To') + ': '
  165. if _p.filter_form(data) == 'filter_date':
  166. df += _p.stop_date if _p.stop_date else u''
  167. else:
  168. df += _p.stop_period.name if _p.stop_period else u''
  169. c_specs += [
  170. ('df', 2, 0, 'text', df, None, style_center),
  171. ('cd', 1 if nbr_columns == 11 else 2, 0, 'text', _p.date_until, None, style_center), #clearance date
  172. ('af', 2, 0, 'text', _('Custom Filter') if _p.partner_ids else _p.display_partner_account(data), None, style_center),
  173. ('tm', 3 if nbr_columns == 13 else 2, 0, 'text', _p.display_target_move(data), None, style_center),
  174. ]
  175. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  176. row_position = self.xls_write_row(ws, row_position, row_data, row_style=style_center)
  177. return row_position
  178. def print_columns_title(self, _p, row_position, data, group_lines=False): # Fill in a row with the titles of the columns for the invoice lines: Date - Period - Entry -...
  179. c_specs = [
  180. ('date', 1, 0, 'text', _('Date'),None,style_yellow_bold),
  181. ('period', 1, 0, 'text', _('Period'),None,style_yellow_bold),
  182. ('entry', 1, 0, 'text', _('Entry'),None,style_yellow_bold),
  183. ('journal', 1, 0, 'text', _('Journal'),None,style_yellow_bold),
  184. ]
  185. if not group_lines:
  186. c_specs += [('partner', 1, 0, 'text', _('Partner'),None,style_yellow_bold),]
  187. c_specs += [
  188. ('label', 1, 0, 'text', _('Label'),None,style_yellow_bold),
  189. ('rec', 1, 0, 'text', _('Rec.'),None,style_yellow_bold),
  190. ('due_date', 1, 0, 'text', _('Due Date'),None,style_yellow_bold),
  191. ('debit', 1, 0, 'text', _('Debit'),None,style_yellow_bold_right),
  192. ('credit', 1, 0, 'text', _('Credit'),None,style_yellow_bold_right),
  193. ('cumul', 1, 0, 'text', _('Cumul. Bal.'),None,style_yellow_bold_right),
  194. ]
  195. if group_lines:
  196. c_specs += [
  197. ('currbal', 1, 0, 'text', _('Curr. Balance'),None,style_yellow_bold_right),
  198. ('curr', 1, 0, 'text', _('Curr.'),None,style_yellow_bold_right),
  199. ]
  200. else:
  201. if _p.amount_currency(data):
  202. c_specs += [
  203. ('currbal', 1, 0, 'text', _('Curr. Balance'),None,style_yellow_bold_right),
  204. ('curr', 1, 0, 'text', _('Curr.'), None, style_yellow_bold_right),
  205. ]
  206. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  207. row_position = self.xls_write_row(ws, row_position, row_data, row_style=style_yellow_bold)
  208. return row_position
  209. def print_row_code_account(self, regroupmode, account, row_position, partner_name): # Fill in a row with the code and the name of an account + the partner name in case of currency regrouping
  210. if regroupmode == "regroup":
  211. c_specs = [ ('acc_title', nbr_columns, 0, 'text', ' - '.join([account.code, account.name, partner_name or _('No partner')])), ]
  212. else:
  213. c_specs = [ ('acc_title', nbr_columns, 0, 'text', ' - '.join([account.code, account.name])), ]
  214. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  215. row_position = self.xls_write_row(ws, row_position, row_data, style_account_title)
  216. return row_position+1
  217. def print_row_partner(self, row_position, partner_name):
  218. c_specs = [ ('partner', nbr_columns, 0, 'text', partner_name or _('No partner')), ]
  219. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  220. row_position = self.xls_write_row(ws, row_position, row_data, style_partner_row)
  221. return row_position
  222. def print_group_currency(self, row_position, curr, _p):
  223. c_specs = [ ('curr', nbr_columns, 0, 'text', curr or _p.company.currency_id.name), ]
  224. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  225. row_position = self.xls_write_row(ws, row_position, row_data, style_bold)
  226. return row_position
  227. def print_lines(self, row_position, account, line,_p, data, line_number): # Fill in rows of invoice line
  228. label_elements = [line.get('lname') or '']
  229. if line.get('invoice_number'):
  230. label_elements.append("(%s)" % (line['invoice_number'],))
  231. label = ' '.join(label_elements)
  232. # Mako: <div class="act_as_row lines ${line.get('is_from_previous_periods') and 'open_invoice_previous_line' or ''} ${line.get('is_clearance_line') and 'clearance_line' or ''}">
  233. if line.get('is_from_previous_periods') or line.get('is_clearance_line'):
  234. style_line_default = style_default_italic
  235. style_line_right = style_right_italic
  236. style_line_date = style_date_italic
  237. style_line_decimal = style_decimal_italic
  238. else:
  239. style_line_default = style_default
  240. style_line_right = style_right
  241. style_line_date = style_date
  242. style_line_decimal = style_decimal
  243. if line.get('ldate'):
  244. c_specs = [('date', 1, 0, 'date', datetime.strptime(line['ldate'],'%Y-%m-%d'), None, style_line_date)]
  245. else:
  246. c_specs = [('date', 1, 0, 'text', None)]
  247. c_specs += [
  248. ('period_code', 1, 0, 'text', line.get('period_code') or '' ),
  249. ('entry', 1, 0, 'text', line.get('move_name') or '' ),
  250. ('journal', 1, 0, 'text', line.get('jcode') or '' ),
  251. ('partner', 1, 0, 'text', line.get('partner_name') or '' ),
  252. ('label', 1, 0, 'text', label ),
  253. ('rec', 1, 0, 'text', line.get('rec_name') or '' ),
  254. ]
  255. if line.get('date_maturity'):
  256. c_specs += [('datedue', 1, 0, 'date', datetime.strptime(line['date_maturity'],'%Y-%m-%d'), None, style_line_date)]
  257. else:
  258. c_specs += [('datedue', 1, 0, 'text', None)]
  259. c_specs += [
  260. ('debit', 1, 0, 'number', line.get('debit') or 0.0 , None, style_line_decimal),
  261. ('credit', 1, 0, 'number', line.get('credit') or 0.0 , None, style_line_decimal),
  262. ]
  263. #determine the formula of the cumulated balance
  264. debit_cell = rowcol_to_cell(row_position, 8)
  265. credit_cell = rowcol_to_cell(row_position, 9)
  266. previous_balance = rowcol_to_cell(row_position - 1, 10)
  267. if line_number == 1: #if it is the first line, the balance is only debit - credit
  268. cumul_balance = debit_cell + '-' + credit_cell
  269. else: # cumulate debit - credit and balance of previous line
  270. cumul_balance = debit_cell + '-' + credit_cell + '+' + previous_balance
  271. c_specs += [('cumul', 1, 0, 'number', None, cumul_balance, style_line_decimal)]
  272. if _p.amount_currency(data):
  273. if account.currency_id:
  274. c_specs += [
  275. ('curramount', 1, 0, 'number', line.get('amount_currency') or 0.0, None, style_line_decimal),
  276. ('currcode', 1, 0, 'text', line['currency_code'], None, style_line_right),
  277. ]
  278. else:
  279. c_specs += [
  280. ('curramount', 1, 0, 'text', '-', None, style_line_right),
  281. ('currcode', 1, 0, 'text', '', None, style_line_right),
  282. ]
  283. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  284. row_position = self.xls_write_row(ws, row_position, row_data, style_line_default)
  285. return row_position
  286. def print_group_lines(self, row_position, account, line,_p, line_number): # Fill in rows of invoice line when the option currency regroup is selected
  287. label_elements = [line.get('lname') or '']
  288. if line.get('invoice_number'):
  289. label_elements.append("(%s)" % (line['invoice_number'],))
  290. label = ' '.join(label_elements)
  291. # Mako: <div class="act_as_row lines ${line.get('is_from_previous_periods') and 'open_invoice_previous_line' or ''} ${line.get('is_clearance_line') and 'clearance_line' or ''}">
  292. if line.get('is_from_previous_periods') or line.get('is_clearance_line'):
  293. style_line_default = style_default_italic
  294. style_line_right = style_right_italic
  295. style_line_date = style_date_italic
  296. style_line_decimal = style_decimal_italic
  297. else:
  298. style_line_default = style_default
  299. style_line_right = style_right
  300. style_line_date = style_date
  301. style_line_decimal = style_decimal
  302. debit_cell = rowcol_to_cell(row_position, 7)
  303. credit_cell = rowcol_to_cell(row_position, 8)
  304. previous_balance = rowcol_to_cell(row_position - 1, 9)
  305. if line_number == 1: #if it is the first line, the balance is only debit - credit
  306. cumul_balance = debit_cell + '-' + credit_cell
  307. else: # cumulate devit - credit and balance of previous line
  308. cumul_balance = debit_cell + '-' + credit_cell + '+' + previous_balance
  309. if line.get('ldate'):
  310. c_specs = [('date', 1, 0, 'date', datetime.strptime(line['ldate'],'%Y-%m-%d'), None, style_line_date)]
  311. else:
  312. c_specs = [('date', 1, 0, 'text', None)]
  313. c_specs += [
  314. ('period_code', 1, 0, 'text', line.get('period_code') or '' ),
  315. ('entry', 1, 0, 'text', line.get('move_name') or '' ),
  316. ('journal', 1, 0, 'text', line.get('jcode') or '' ),
  317. ('label', 1, 0, 'text', label),
  318. ('rec', 1, 0, 'text', line.get('rec_name') or '' ),
  319. ]
  320. if line.get('date_maturity'):
  321. c_specs += [('datedue', 1, 0, 'date', datetime.strptime(line['date_maturity'],'%Y-%m-%d'), None, style_line_date)]
  322. else:
  323. c_specs += [('datedue', 1, 0, 'text', None)]
  324. c_specs += [
  325. ('debit', 1, 0, 'number', line.get('debit') or 0.0, None, style_line_decimal),
  326. ('credit', 1, 0, 'number', line.get('credit') or 0.0, None, style_line_decimal),
  327. ('cumul', 1, 0, 'number', None, cumul_balance, style_line_decimal),
  328. ]
  329. if account.currency_id:
  330. c_specs += [
  331. ('curramount', 1, 0, 'number', line.get('amount_currency') or 0.0, None, style_line_decimal),
  332. ('currcode', 1, 0, 'text', line.get('currency_code') or '', None, style_line_right),
  333. ]
  334. else:
  335. c_specs += [
  336. ('curramount', 1, 0, 'text', '-', None, style_line_right),
  337. ('currcode', 1, 0, 'text', '', None, style_line_right),
  338. ]
  339. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  340. row_position = self.xls_write_row(ws, row_position, row_data, style_line_default)
  341. return (row_position, cumul_balance)
  342. def print_cumul_partner(self, row_position, row_start_partner, account, _p, data): #print by partner the totals and cumulated balance (Excel formulas)
  343. start_col = 5 #the text "Cumulated Balance on Partner starts in column 4 when selecting the option regroup by currency, 5 in the other case
  344. debit_partner_start = rowcol_to_cell(row_start_partner, start_col + 3)
  345. debit_partner_end = rowcol_to_cell(row_position-1, start_col + 3)
  346. debit_partner_total = 'SUM(' + debit_partner_start + ':' + debit_partner_end + ')'
  347. credit_partner_start = rowcol_to_cell(row_start_partner, start_col + 4)
  348. credit_partner_end = rowcol_to_cell(row_position-1, start_col + 4)
  349. credit_partner_total = 'SUM(' + credit_partner_start + ':' + credit_partner_end + ')'
  350. bal_curr_start = rowcol_to_cell(row_start_partner, start_col + 6)
  351. bal_curr_end = rowcol_to_cell(row_position-1, start_col + 6)
  352. cumul_balance_curr = 'SUM(' + bal_curr_start + ':' + bal_curr_end + ')'
  353. bal_partner_debit = rowcol_to_cell(row_position, start_col + 3)
  354. bal_partner_credit = rowcol_to_cell(row_position, start_col + 4)
  355. bal_partner_total = bal_partner_debit + '-' + bal_partner_credit
  356. c_specs = [('empty%s' %x, 1, 0, 'text', None) for x in range(start_col)]
  357. c_specs += [
  358. ('init_bal', 1, 0, 'text', _('Cumulated Balance on Partner')),
  359. ('rec', 1, 0, 'text', None),
  360. ('empty5', 1, 0, 'text', None),
  361. ('debit', 1, 0, 'number', None, debit_partner_total, style_partner_cumul_decimal),
  362. ('credit', 1, 0, 'number', None, credit_partner_total, style_partner_cumul_decimal),
  363. ('cumul_bal', 1, 0, 'number', None, bal_partner_total, style_partner_cumul_decimal),
  364. ]
  365. if _p.amount_currency(data):
  366. if account.currency_id:
  367. c_specs += [('cumul_bal_curr', 1, 0, 'number', None, cumul_balance_curr, style_partner_cumul_decimal),
  368. ('curr_name', 1, 0, 'text', account.currency_id.name, None, style_partner_cumul_right),
  369. ]
  370. else:
  371. c_specs += [('cumul_bal_curr', 1, 0, 'text', '-', None, style_partner_cumul_right),
  372. ('curr_name', 1, 0, 'text', '', None, style_partner_cumul_right)
  373. ]
  374. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  375. row_position = self.xls_write_row(ws, row_position, row_data, style_partner_cumul)
  376. return row_position+1
  377. def print_group_cumul_partner(self,row_position, row_start_partner, account, _p, data): #print by partner the totals and cumulated balance (Excel formulas) when the option currency regroup is selected
  378. start_col = 4 #the text "Cumulated Balance on Partner starts in column 4 when selecting the option regroup by currency, 5 in the other case
  379. debit_partner_start = rowcol_to_cell(row_start_partner, start_col + 3)
  380. debit_partner_end = rowcol_to_cell(row_position-1, start_col + 3)
  381. debit_partner_total = 'SUM(' + debit_partner_start + ':' + debit_partner_end + ')'
  382. credit_partner_start = rowcol_to_cell(row_start_partner, start_col + 4)
  383. credit_partner_end = rowcol_to_cell(row_position-1, start_col + 4)
  384. credit_partner_total = 'SUM(' + credit_partner_start + ':' + credit_partner_end + ')'
  385. bal_curr_start = rowcol_to_cell(row_start_partner, start_col + 5)
  386. bal_curr_end = rowcol_to_cell(row_position-1, start_col + 5)
  387. cumul_balance_curr = 'SUM(' + bal_curr_start + ':' + bal_curr_end + ')'
  388. bal_partner_debit = rowcol_to_cell(row_position, start_col + 3)
  389. bal_partner_credit = rowcol_to_cell(row_position, start_col + 4)
  390. bal_partner_total = bal_partner_debit + '-' + bal_partner_credit
  391. c_specs = [('empty%s' %x, 1, 0, 'text', None) for x in range(start_col)]
  392. c_specs += [
  393. ('init_bal', 1, 0, 'text', _('Cumulated Balance on Partner')), #, style_bold_italic),
  394. ('rec', 1, 0, 'text', None),
  395. ('empty5', 1, 0, 'text', None),
  396. ('debit', 1, 0, 'number', None, debit_partner_total, style_partner_cumul_decimal),
  397. ('credit', 1, 0, 'number', None, credit_partner_total, style_partner_cumul_decimal),
  398. ('cumul_bal', 1, 0, 'number', None, bal_partner_total, style_partner_cumul_decimal),
  399. ]
  400. if account.currency_id:
  401. c_specs += [
  402. ('cumul_bal_curr', 1, 0, 'number', None, cumul_balance_curr, style_partner_cumul_decimal),
  403. ('curr_name', 1, 0, 'text', account.currency_id.name, None, style_partner_cumul_right),
  404. ]
  405. else:
  406. c_specs += [
  407. ('cumul_bal_curr', 1, 0, 'text', "-", None, style_partner_cumul_right),
  408. ('curr_name', 1, 0, 'text', "", None, style_partner_cumul_right),
  409. ]
  410. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  411. row_position = self.xls_write_row(ws, row_position, row_data, style_partner_cumul)
  412. return row_position+1
  413. def print_cumul_account(self, row_position, row_start_account, account, _p, data): #print by account the totals of the credit and debit + balance calculation
  414. #This procedure will create an Excel sumif function that will check in the column "label" for the "Cumulated Balance.." string and make a sum of the debit & credit data
  415. start_col = 5 #the text "Cumulated Balance on Partner starts in column 4 when selecting the option regroup by currency, 5 in the other case
  416. reference_start = rowcol_to_cell(row_start_account, start_col) #range in which we search for the text "Cumulated Balance on Partner"
  417. reference_stop = rowcol_to_cell(row_position -1 , start_col)
  418. range_debit_start = rowcol_to_cell(row_start_account, start_col + 3) #range in which we make the sum of all the cumulated balance lines (debit)
  419. range_debit_stop = rowcol_to_cell(row_position -1, start_col + 3)
  420. range_credit_start = rowcol_to_cell(row_start_account, start_col + 4) #range in which we make the sum of all the cumulated balance lines (crebit)
  421. range_credit_stop = rowcol_to_cell(row_position -1, start_col + 4)
  422. search_key = _('Cumulated Balance on Partner')
  423. total_debit_account = 'SUMIF(' + reference_start + ':' + reference_stop + ';"' + search_key + '";' + range_debit_start + ':' + range_debit_stop + ')'
  424. total_credit_account = 'SUMIF(' + reference_start + ':' + reference_stop + ';"' + search_key + '";' + range_credit_start + ':' + range_credit_stop + ')'
  425. bal_account_debit = rowcol_to_cell(row_position, start_col + 3)
  426. bal_account_credit = rowcol_to_cell(row_position, start_col + 4)
  427. bal_account_total = bal_account_debit + '-' + bal_account_credit
  428. bal_curr_start = rowcol_to_cell(row_start_account, start_col + 6)
  429. bal_curr_end = rowcol_to_cell(row_position-1, start_col + 6)
  430. cumul_balance_curr = 'SUMIF(' + reference_start + ':' + reference_stop + ';"' + search_key + '";' + bal_curr_start + ':' + bal_curr_end + ')'
  431. c_specs = [
  432. ('acc_title', start_col, 0, 'text', ' - '.join([account.code, account.name])),
  433. ('init_bal', 2, 0, 'text', _('Cumulated Balance on Account')),
  434. ('empty2', 1, 0, 'text', None),
  435. ('debit', 1, 0, 'number', None, total_debit_account, style_account_title_decimal),
  436. ('credit', 1, 0, 'number', None, total_credit_account, style_account_title_decimal),
  437. ('balance', 1, 0, 'number', None, bal_account_total, style_account_title_decimal),
  438. ]
  439. if _p.amount_currency(data):
  440. if account.currency_id:
  441. c_specs += [('cumul_bal_curr', 1, 0, 'number', None, cumul_balance_curr),
  442. ('curr_name', 1, 0, 'text', account.currency_id.name, None, style_account_title_right),
  443. ]
  444. else:
  445. c_specs += [('cumul_bal_curr', 1, 0, 'text', "-", None, style_account_title_right),
  446. ('curr_name', 1, 0, 'text', "", None, style_account_title_right)
  447. ]
  448. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  449. row_position = self.xls_write_row(ws, row_position, row_data, style_account_title)
  450. return row_position+1
  451. def print_group_cumul_account(self,row_position, row_start_account, account): #print by account the totals of the credit and debit + balance calculation
  452. #This procedure will create an Excel sumif function that will check in the column "label" for the "Cumulated Balance.." string and make a sum of the debit & credit data
  453. start_col = 4 #the text "Cumulated Balance on Partner starts in column 4 when selecting the option regroup by currency, 5 in the other case
  454. reference_start = rowcol_to_cell(row_start_account, start_col) #range in which we search for the text "Cumulated Balance on Partner"
  455. reference_stop = rowcol_to_cell(row_position -1 , start_col)
  456. range_debit_start = rowcol_to_cell(row_start_account, start_col + 3) #range in which we make the sum of all the cumulated balance lines (debit)
  457. range_debit_stop = rowcol_to_cell(row_position -1, start_col + 3)
  458. range_credit_start = rowcol_to_cell(row_start_account, start_col + 4) #range in which we make the sum of all the cumulated balance lines (crebit)
  459. range_credit_stop = rowcol_to_cell(row_position -1, start_col + 4)
  460. search_key = _('Cumulated Balance on Partner')
  461. total_debit_account = 'SUMIF(' + reference_start + ':' + reference_stop + ';"' + search_key + '";' + range_debit_start + ':' + range_debit_stop + ')'
  462. total_credit_account = 'SUMIF(' + reference_start + ':' + reference_stop + ';"' + search_key + '";' + range_credit_start + ':' + range_credit_stop + ')'
  463. bal_account_debit = rowcol_to_cell(row_position, start_col + 3)
  464. bal_account_credit = rowcol_to_cell(row_position, start_col + 4)
  465. bal_account_total = bal_account_debit + '-' + bal_account_credit
  466. bal_curr_start = rowcol_to_cell(row_start_account, start_col + 6)
  467. bal_curr_end = rowcol_to_cell(row_position-1, start_col + 6)
  468. cumul_balance_curr = 'SUMIF(' + reference_start + ':' + reference_stop + ';"' + search_key + '";' + bal_curr_start + ':' + bal_curr_end + ')'
  469. c_specs = [
  470. ('acc_title', start_col, 0, 'text', ' - '.join([account.code, account.name])),
  471. ('init_bal', 2, 0, 'text', _('Cumulated Balance on Account')),
  472. ('empty2', 1, 0, 'text', None),
  473. ('debit', 1, 0, 'number', None, total_debit_account, style_account_title_decimal),
  474. ('credit', 1, 0, 'number', None, total_credit_account, style_account_title_decimal),
  475. ('balance', 1, 0, 'number', None, bal_account_total, style_account_title_decimal),
  476. ]
  477. if account.currency_id:
  478. c_specs += [('cumul_bal_curr', 1, 0, 'number', None, cumul_balance_curr, style_account_title_decimal),
  479. ('curr_name', 1, 0, 'text', account.currency_id.name, None, style_account_title_decimal),
  480. ]
  481. else:
  482. c_specs += [('cumul_bal_curr', 1, 0, 'text', "-", None, style_account_title_right),
  483. ('curr_name', 1, 0, 'text', "", None, style_account_title_right)
  484. ]
  485. row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
  486. row_position = self.xls_write_row(ws, row_position, row_data, style_account_title)
  487. return row_position+1
  488. def print_grouped_line_report(self, row_pos, account, _xs, xlwt, _p, data): # export the invoice AR/AP lines when the option currency regroup is selected
  489. if account.grouped_ledger_lines and account.partners_order:
  490. row_start_account = row_pos
  491. for partner_name, p_id, p_ref, p_name in account.partners_order:
  492. row_pos = self.print_row_code_account("regroup", account,row_pos, partner_name)
  493. for curr, grouped_lines in account.grouped_ledger_lines.get(p_id, []):
  494. row_pos = self.print_group_currency(row_pos, curr, _p)
  495. # Print row: Titles "Date-Period-Entry-Journal..."
  496. row_pos = self.print_columns_title(_p, row_pos, data, group_lines=True)
  497. row_pos_start = row_pos
  498. line_number = 0
  499. for line in grouped_lines:
  500. line_number += 1
  501. row_pos, cumul_balance = self.print_group_lines(row_pos, account, line, _p, line_number)
  502. row_pos = self.print_group_cumul_partner(row_pos,row_pos_start, account, _p, data)
  503. row_pos = self.print_group_cumul_account(row_pos, row_start_account, account)
  504. return row_pos
  505. def print_ledger_lines(self, row_pos, account, _xs, xlwt, _p, data): # export the invoice AR/AP lines
  506. if account.ledger_lines and account.partners_order:
  507. row_start_account = row_pos
  508. #Print account line: code - account
  509. row_pos = self.print_row_code_account("noregroup",account,row_pos, "")
  510. for partner_name, p_id, p_ref, p_name in account.partners_order:
  511. #Print partner row
  512. row_pos = self.print_row_partner(row_pos, partner_name)
  513. # Print row: Titles "Date-Period-Entry-Journal..."
  514. row_pos = self.print_columns_title(_p, row_pos, data, group_lines=False)
  515. row_pos_start = row_pos
  516. line_number = 0
  517. for line in account.ledger_lines.get(p_id, []):
  518. line_number += 1
  519. # print ledger lines
  520. row_pos = self.print_lines(row_pos, account, line, _p, data, line_number)
  521. row_pos = self.print_cumul_partner(row_pos, row_pos_start, account, _p, data)
  522. row_pos = self.print_cumul_account(row_pos, row_start_account, account, _p, data)
  523. return row_pos
  524. def generate_xls_report(self, _p, _xs, data, objects, wb): # main function
  525. # Initializations
  526. self.global_initializations(wb,_p, xlwt, _xs, objects, data)
  527. row_pos = 0
  528. # Print Title
  529. row_pos = self.print_title(_p, row_pos)
  530. # Print empty row to define column sizes
  531. row_pos = self.print_empty_row(row_pos)
  532. # Print Header Table titles (Fiscal Year - Accounts Filter - Periods Filter...)
  533. row_pos = self.print_header_titles(_p, data, row_pos)
  534. # Print Header Table data
  535. row_pos = self.print_header_data(_p, data, row_pos)
  536. #Freeze the line
  537. ws.set_horz_split_pos(row_pos)
  538. # Print empty row
  539. row_pos = self.print_empty_row(row_pos)
  540. for acc in objects:
  541. if hasattr(acc, 'grouped_ledger_lines'):
  542. # call xls equivalent of "grouped_by_curr_open_invoices_inclusion.mako.html"
  543. row_pos = self.print_grouped_line_report(row_pos, acc, _xs, xlwt, _p, data)
  544. else:
  545. # call xls equivalent of "open_invoices_inclusion.mako.html"
  546. row_pos = self.print_ledger_lines(row_pos, acc, _xs, xlwt, _p, data)
  547. row_pos += 1
  548. open_invoices_xls('report.account.account_report_open_invoices_xls', 'account.account', parser=PartnersOpenInvoicesWebkit)