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.

220 lines
7.9 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Copyright (C) 2015 Savoir-faire Linux. All Rights Reserved.
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as published
  8. # by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp import netsvc
  22. from datetime import datetime
  23. from openerp.tests import common
  24. from ..report.partner_aged_statement_report import PartnerAgedTrialReport
  25. from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
  26. from dateutil.relativedelta import relativedelta
  27. class test_aged_statement(common.TransactionCase):
  28. def create_fiscal_year(self, year):
  29. cr, uid, context = self.cr, self.uid, self.context
  30. fy_id = self.fiscal_year_model.create(cr, uid, {
  31. 'name': 'Test %s' % year,
  32. 'code': 'FY%s' % year,
  33. 'company_id': self.company_id,
  34. 'date_start': datetime(year, 1, 1),
  35. 'date_stop': datetime(year, 12, 31),
  36. }, context=context)
  37. fy = self.fiscal_year_model.browse(cr, uid, fy_id, context=context)
  38. fy.create_period()
  39. def setUp(self):
  40. super(test_aged_statement, self).setUp()
  41. self.user_model = self.registry("res.users")
  42. self.partner_model = self.registry("res.partner")
  43. self.invoice_model = self.registry("account.invoice")
  44. self.account_model = self.registry("account.account")
  45. self.journal_model = self.registry("account.journal")
  46. self.company_model = self.registry("res.company")
  47. self.fiscal_year_model = self.registry("account.fiscalyear")
  48. self.context = self.user_model.context_get(self.cr, self.uid)
  49. cr, uid, context = self.cr, self.uid, self.context
  50. self.company_id = self.user_model.browse(
  51. cr, uid, uid, context=context).company_id.id
  52. self.partner_id = self.partner_model.create(
  53. cr, uid, {'name': 'Test', 'customer': True}, context=context)
  54. self.account_id = self.account_model.search(
  55. cr, uid, [
  56. ('type', '=', 'receivable'),
  57. ('currency_id', '=', False),
  58. ], context=context)[0]
  59. self.account_2_id = self.account_model.search(
  60. cr, uid, [
  61. ('type', '=', 'other'),
  62. ('currency_id', '=', False),
  63. ], context=context)[0]
  64. self.journal_id = self.journal_model.search(
  65. cr, uid, [('type', '=', 'sale')], context=context)[0]
  66. self.today = datetime.now()
  67. self.create_fiscal_year(self.today.year - 1)
  68. self.date1 = self.today - relativedelta(weeks=2)
  69. self.date2 = self.today - relativedelta(months=1, weeks=1)
  70. self.date3 = self.today - relativedelta(months=2, weeks=1)
  71. self.date4 = self.today - relativedelta(months=3, weeks=1)
  72. self.date5 = self.today - relativedelta(months=4, weeks=1)
  73. self.invoice_ids = [
  74. self.invoice_model.create(
  75. cr, uid, {
  76. 'journal_id': self.journal_id,
  77. 'account_id': self.account_id,
  78. 'partner_id': self.partner_id,
  79. 'date_invoice': invoice[0],
  80. 'invoice_line': [(0, 0, {
  81. 'name': 'Test',
  82. 'account_id': self.account_2_id,
  83. 'price_unit': invoice[1],
  84. 'quantity': 1,
  85. })],
  86. 'type': invoice[2],
  87. }, context=context)
  88. for invoice in [
  89. (self.today, 300, 'out_invoice'),
  90. (self.date1, 100, 'out_invoice'),
  91. (self.date2, 150, 'out_invoice'),
  92. (self.date3, 200, 'out_invoice'),
  93. (self.date4, 250, 'out_invoice'),
  94. (self.date5, 500, 'out_invoice'),
  95. (self.date2, 600, 'out_refund'),
  96. (self.date4, 700, 'out_refund'),
  97. ]
  98. ]
  99. wf_service = netsvc.LocalService("workflow")
  100. for inv_id in self.invoice_ids:
  101. wf_service.trg_validate(
  102. uid, 'account.invoice', inv_id, 'invoice_open', cr)
  103. self.report = PartnerAgedTrialReport(
  104. cr, uid, 'webkit.partner_aged_statement_report',
  105. context=context)
  106. self.partner = self.partner_model.browse(
  107. cr, uid, self.partner_id, context=context)
  108. self.company = self.company_model.browse(
  109. cr, uid, self.company_id, context=context)
  110. def test_get_balance(self):
  111. balance = self.report._get_balance(self.partner, self.company)
  112. self.assertEqual(len(balance), 1)
  113. self.assertEqual(balance[0]['current'], 400)
  114. self.assertEqual(balance[0]['3160'], 150)
  115. self.assertEqual(balance[0]['6190'], 200)
  116. self.assertEqual(balance[0]['91120'], 250)
  117. self.assertEqual(balance[0]['121'], 500)
  118. self.assertEqual(balance[0]['total'], 400 + 150 + 200 + 250 + 500)
  119. def compare_vals(self, line, vals):
  120. self.assertEqual(
  121. line['date_original'], vals['date_original'].strftime(
  122. DEFAULT_SERVER_DATE_FORMAT))
  123. self.assertEqual(line['amount_original'], vals['amount_original'])
  124. self.assertEqual(
  125. line['amount_unreconciled'], vals['amount_unreconciled'])
  126. def test_line_get_current(self):
  127. lines = self.report._lines_get_current(self.partner, self.company)
  128. self.compare_vals(lines[0], {
  129. 'date_original': self.today,
  130. 'amount_original': 300,
  131. 'amount_unreconciled': 300,
  132. })
  133. self.compare_vals(lines[1], {
  134. 'date_original': self.date1,
  135. 'amount_original': 100,
  136. 'amount_unreconciled': 100,
  137. })
  138. def test_line_get_31_60(self):
  139. lines = self.report._lines_get_31_60(self.partner, self.company)
  140. self.compare_vals(lines[0], {
  141. 'date_original': self.date2,
  142. 'amount_original': 150,
  143. 'amount_unreconciled': 150,
  144. })
  145. def test_line_get_61(self):
  146. lines = sorted(
  147. self.report._lines_get_61(self.partner, self.company),
  148. key=lambda l: l['date_original'])
  149. self.compare_vals(lines[0], {
  150. 'date_original': self.date5,
  151. 'amount_original': 500,
  152. 'amount_unreconciled': 500,
  153. })
  154. self.compare_vals(lines[1], {
  155. 'date_original': self.date4,
  156. 'amount_original': 250,
  157. 'amount_unreconciled': 250,
  158. })
  159. self.compare_vals(lines[2], {
  160. 'date_original': self.date3,
  161. 'amount_original': 200,
  162. 'amount_unreconciled': 200,
  163. })
  164. def test_line_get_refunds(self):
  165. lines = sorted(
  166. self.report._lines_get_refunds(self.partner, self.company),
  167. key=lambda l: l['date_original'])
  168. self.compare_vals(lines[0], {
  169. 'date_original': self.date4,
  170. 'amount_original': 700,
  171. 'amount_unreconciled': 700,
  172. })
  173. self.compare_vals(lines[1], {
  174. 'date_original': self.date2,
  175. 'amount_original': 600,
  176. 'amount_unreconciled': 600,
  177. })
  178. def test_refunds_total(self):
  179. res = self.report._refunds_total(self.partner, self.company)
  180. self.assertEqual(res, 1300)