test_account_payment_register.py 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. # -*- coding: utf-8 -*-
  2. from odoo.addons.account.tests.common import AccountTestInvoicingCommon
  3. from odoo.exceptions import UserError
  4. from odoo.tests import tagged, Form
  5. from odoo import Command
  6. @tagged('post_install', '-at_install')
  7. class TestAccountPaymentRegister(AccountTestInvoicingCommon):
  8. @classmethod
  9. def setUpClass(cls, chart_template_ref=None):
  10. super().setUpClass(chart_template_ref=chart_template_ref)
  11. cls.currency_data_3 = cls.setup_multi_currency_data({
  12. 'name': "Umbrella",
  13. 'symbol': '☂',
  14. 'currency_unit_label': "Umbrella",
  15. 'currency_subunit_label': "Broken Umbrella",
  16. }, rate2017=0.01)
  17. cls.payment_debit_account_id = cls.company_data['default_journal_bank'].company_id.account_journal_payment_debit_account_id.copy()
  18. cls.payment_credit_account_id = cls.company_data['default_journal_bank'].company_id.account_journal_payment_credit_account_id.copy()
  19. cls.bank_journal_1 = cls.company_data['default_journal_bank']
  20. cls.bank_journal_2 = cls.company_data['default_journal_bank'].copy()
  21. cls.partner_bank_account1 = cls.env['res.partner.bank'].create({
  22. 'acc_number': "0123456789",
  23. 'partner_id': cls.partner_a.id,
  24. 'acc_type': 'bank',
  25. })
  26. cls.partner_bank_account2 = cls.env['res.partner.bank'].create({
  27. 'acc_number': "9876543210",
  28. 'partner_id': cls.partner_a.id,
  29. 'acc_type': 'bank',
  30. })
  31. cls.comp_bank_account1 = cls.env['res.partner.bank'].create({
  32. 'acc_number': "985632147",
  33. 'partner_id': cls.env.company.partner_id.id,
  34. 'acc_type': 'bank',
  35. })
  36. cls.comp_bank_account2 = cls.env['res.partner.bank'].create({
  37. 'acc_number': "741258963",
  38. 'partner_id': cls.env.company.partner_id.id,
  39. 'acc_type': 'bank',
  40. })
  41. # Customer invoices sharing the same batch.
  42. cls.out_invoice_1 = cls.env['account.move'].create({
  43. 'move_type': 'out_invoice',
  44. 'date': '2017-01-01',
  45. 'invoice_date': '2017-01-01',
  46. 'partner_id': cls.partner_a.id,
  47. 'currency_id': cls.currency_data['currency'].id,
  48. 'invoice_line_ids': [(0, 0, {'product_id': cls.product_a.id, 'price_unit': 1000.0, 'tax_ids': []})],
  49. })
  50. cls.out_invoice_2 = cls.env['account.move'].create({
  51. 'move_type': 'out_invoice',
  52. 'date': '2017-01-01',
  53. 'invoice_date': '2017-01-01',
  54. 'partner_id': cls.partner_a.id,
  55. 'currency_id': cls.currency_data['currency'].id,
  56. 'invoice_line_ids': [(0, 0, {'product_id': cls.product_a.id, 'price_unit': 2000.0, 'tax_ids': []})],
  57. })
  58. cls.out_invoice_3 = cls.env['account.move'].create({
  59. 'move_type': 'out_invoice',
  60. 'date': '2017-01-01',
  61. 'invoice_date': '2017-01-01',
  62. 'partner_id': cls.partner_a.id,
  63. 'currency_id': cls.currency_data['currency'].id,
  64. 'invoice_line_ids': [(0, 0, {'product_id': cls.product_a.id, 'price_unit': 24.02, 'tax_ids': []})],
  65. })
  66. cls.out_invoice_4 = cls.env['account.move'].create({
  67. 'move_type': 'out_invoice',
  68. 'date': '2017-01-01',
  69. 'invoice_date': '2017-01-01',
  70. 'partner_id': cls.partner_a.id,
  71. 'currency_id': cls.currency_data['currency'].id,
  72. 'invoice_line_ids': [(0, 0, {'product_id': cls.product_a.id, 'price_unit': 23.98, 'tax_ids': []})],
  73. })
  74. (cls.out_invoice_1 + cls.out_invoice_2 + cls.out_invoice_3 + cls.out_invoice_4).action_post()
  75. # Vendor bills, in_invoice_1 + in_invoice_2 are sharing the same batch but not in_invoice_3.
  76. cls.in_invoice_1 = cls.env['account.move'].create({
  77. 'move_type': 'in_invoice',
  78. 'date': '2017-01-01',
  79. 'invoice_date': '2017-01-01',
  80. 'partner_id': cls.partner_a.id,
  81. 'invoice_line_ids': [(0, 0, {'product_id': cls.product_a.id, 'price_unit': 1000.0, 'tax_ids': []})],
  82. })
  83. cls.in_invoice_2 = cls.env['account.move'].create({
  84. 'move_type': 'in_invoice',
  85. 'date': '2017-01-01',
  86. 'invoice_date': '2017-01-01',
  87. 'partner_id': cls.partner_a.id,
  88. 'invoice_line_ids': [(0, 0, {'product_id': cls.product_a.id, 'price_unit': 2000.0, 'tax_ids': []})],
  89. })
  90. cls.in_invoice_3 = cls.env['account.move'].create({
  91. 'move_type': 'in_invoice',
  92. 'date': '2017-01-01',
  93. 'invoice_date': '2017-01-01',
  94. 'partner_id': cls.partner_b.id,
  95. 'invoice_payment_term_id': False,
  96. 'currency_id': cls.currency_data['currency'].id,
  97. 'invoice_line_ids': [(0, 0, {'product_id': cls.product_a.id, 'price_unit': 3000.0, 'tax_ids': []})],
  98. })
  99. (cls.in_invoice_1 + cls.in_invoice_2 + cls.in_invoice_3).action_post()
  100. # Credit note
  101. cls.in_refund_1 = cls.env['account.move'].create({
  102. 'move_type': 'in_refund',
  103. 'date': '2017-01-01',
  104. 'invoice_date': '2017-01-01',
  105. 'partner_id': cls.partner_a.id,
  106. 'invoice_line_ids': [(0, 0, {'product_id': cls.product_a.id, 'price_unit': 1600.0, 'tax_ids': []})],
  107. })
  108. cls.in_refund_1.action_post()
  109. def test_register_payment_single_batch_grouped_keep_open_lower_amount(self):
  110. ''' Pay 800.0 with 'open' as payment difference handling on two customer invoices (1000 + 2000). '''
  111. active_ids = (self.out_invoice_1 + self.out_invoice_2).ids
  112. payments = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids).create({
  113. 'amount': 800.0,
  114. 'group_payment': True,
  115. 'payment_difference_handling': 'open',
  116. 'currency_id': self.currency_data['currency'].id,
  117. 'payment_method_line_id': self.inbound_payment_method_line.id,
  118. })._create_payments()
  119. self.assertRecordValues(payments, [{
  120. 'ref': 'INV/2017/00001 INV/2017/00002',
  121. 'payment_method_line_id': self.inbound_payment_method_line.id,
  122. }])
  123. self.assertRecordValues(payments.line_ids.sorted('balance'), [
  124. # Receivable line:
  125. {
  126. 'debit': 0.0,
  127. 'credit': 400.0,
  128. 'currency_id': self.currency_data['currency'].id,
  129. 'amount_currency': -800.0,
  130. 'reconciled': True,
  131. },
  132. # Liquidity line:
  133. {
  134. 'debit': 400.0,
  135. 'credit': 0.0,
  136. 'currency_id': self.currency_data['currency'].id,
  137. 'amount_currency': 800.0,
  138. 'reconciled': False,
  139. },
  140. ])
  141. def test_register_payment_single_batch_grouped_keep_open_higher_amount(self):
  142. ''' Pay 3100.0 with 'open' as payment difference handling on two customer invoices (1000 + 2000). '''
  143. active_ids = (self.out_invoice_1 + self.out_invoice_2).ids
  144. payments = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids).create({
  145. 'amount': 3100.0,
  146. 'group_payment': True,
  147. 'payment_difference_handling': 'open',
  148. 'currency_id': self.currency_data['currency'].id,
  149. 'payment_method_line_id': self.inbound_payment_method_line.id,
  150. })._create_payments()
  151. self.assertRecordValues(payments, [{
  152. 'ref': 'INV/2017/00001 INV/2017/00002',
  153. 'payment_method_line_id': self.inbound_payment_method_line.id,
  154. }])
  155. self.assertRecordValues(payments.line_ids.sorted('balance'), [
  156. # Receivable line:
  157. {
  158. 'debit': 0.0,
  159. 'credit': 1550.0,
  160. 'currency_id': self.currency_data['currency'].id,
  161. 'amount_currency': -3100.0,
  162. 'reconciled': False,
  163. },
  164. # Liquidity line:
  165. {
  166. 'debit': 1550.0,
  167. 'credit': 0.0,
  168. 'currency_id': self.currency_data['currency'].id,
  169. 'amount_currency': 3100.0,
  170. 'reconciled': False,
  171. },
  172. ])
  173. def test_register_payment_single_batch_grouped_writeoff_lower_amount_debit(self):
  174. ''' Pay 800.0 with 'reconcile' as payment difference handling on two customer invoices (1000 + 2000). '''
  175. active_ids = (self.out_invoice_1 + self.out_invoice_2).ids
  176. payments = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids).create({
  177. 'amount': 800.0,
  178. 'group_payment': True,
  179. 'payment_difference_handling': 'reconcile',
  180. 'writeoff_account_id': self.company_data['default_account_revenue'].id,
  181. 'writeoff_label': 'writeoff',
  182. 'payment_method_line_id': self.inbound_payment_method_line.id,
  183. })._create_payments()
  184. self.assertRecordValues(payments, [{
  185. 'ref': 'INV/2017/00001 INV/2017/00002',
  186. 'payment_method_line_id': self.inbound_payment_method_line.id,
  187. }])
  188. self.assertRecordValues(payments.line_ids.sorted('balance'), [
  189. # Receivable line:
  190. {
  191. 'debit': 0.0,
  192. 'credit': 1500.0,
  193. 'currency_id': self.currency_data['currency'].id,
  194. 'amount_currency': -3000.0,
  195. 'reconciled': True,
  196. },
  197. # Liquidity line:
  198. {
  199. 'debit': 400.0,
  200. 'credit': 0.0,
  201. 'currency_id': self.currency_data['currency'].id,
  202. 'amount_currency': 800.0,
  203. 'reconciled': False,
  204. },
  205. # Writeoff line:
  206. {
  207. 'debit': 1100.0,
  208. 'credit': 0.0,
  209. 'currency_id': self.currency_data['currency'].id,
  210. 'amount_currency': 2200.0,
  211. 'reconciled': False,
  212. },
  213. ])
  214. def test_register_payment_single_batch_grouped_writeoff_higher_amount_debit(self):
  215. ''' Pay 3100.0 with 'reconcile' as payment difference handling on two customer invoices (1000 + 2000). '''
  216. active_ids = (self.out_invoice_1 + self.out_invoice_2).ids
  217. payments = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids).create({
  218. 'amount': 3100.0,
  219. 'group_payment': True,
  220. 'payment_difference_handling': 'reconcile',
  221. 'writeoff_account_id': self.company_data['default_account_revenue'].id,
  222. 'writeoff_label': 'writeoff',
  223. 'payment_method_line_id': self.inbound_payment_method_line.id,
  224. })._create_payments()
  225. self.assertRecordValues(payments, [{
  226. 'ref': 'INV/2017/00001 INV/2017/00002',
  227. 'payment_method_line_id': self.inbound_payment_method_line.id,
  228. }])
  229. self.assertRecordValues(payments.line_ids.sorted('balance'), [
  230. # Receivable line:
  231. {
  232. 'debit': 0.0,
  233. 'credit': 1500.0,
  234. 'currency_id': self.currency_data['currency'].id,
  235. 'amount_currency': -3000.0,
  236. 'reconciled': True,
  237. },
  238. # Writeoff line:
  239. {
  240. 'debit': 0.0,
  241. 'credit': 50.0,
  242. 'currency_id': self.currency_data['currency'].id,
  243. 'amount_currency': -100.0,
  244. 'reconciled': False,
  245. },
  246. # Liquidity line:
  247. {
  248. 'debit': 1550.0,
  249. 'credit': 0.0,
  250. 'currency_id': self.currency_data['currency'].id,
  251. 'amount_currency': 3100.0,
  252. 'reconciled': False,
  253. },
  254. ])
  255. def test_register_payment_single_batch_grouped_writeoff_lower_amount_credit(self):
  256. ''' Pay 800.0 with 'reconcile' as payment difference handling on two vendor billes (1000 + 2000). '''
  257. active_ids = (self.in_invoice_1 + self.in_invoice_2).ids
  258. payments = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids).create({
  259. 'amount': 800.0,
  260. 'group_payment': True,
  261. 'payment_difference_handling': 'reconcile',
  262. 'writeoff_account_id': self.company_data['default_account_revenue'].id,
  263. 'writeoff_label': 'writeoff',
  264. 'payment_method_line_id': self.inbound_payment_method_line.id,
  265. })._create_payments()
  266. self.assertRecordValues(payments, [{
  267. 'ref': 'BILL/2017/01/0001 BILL/2017/01/0002',
  268. 'payment_method_line_id': self.inbound_payment_method_line.id,
  269. }])
  270. self.assertRecordValues(payments.line_ids.sorted('balance'), [
  271. # Writeoff line:
  272. {
  273. 'debit': 0.0,
  274. 'credit': 2200.0,
  275. 'currency_id': self.company_data['currency'].id,
  276. 'amount_currency': -2200.0,
  277. 'reconciled': False,
  278. },
  279. # Liquidity line:
  280. {
  281. 'debit': 0.0,
  282. 'credit': 800.0,
  283. 'currency_id': self.company_data['currency'].id,
  284. 'amount_currency': -800.0,
  285. 'reconciled': False,
  286. },
  287. # Payable line:
  288. {
  289. 'debit': 3000.0,
  290. 'credit': 0.0,
  291. 'currency_id': self.company_data['currency'].id,
  292. 'amount_currency': 3000.0,
  293. 'reconciled': True,
  294. },
  295. ])
  296. def test_register_payment_single_batch_grouped_writeoff_higher_amount_credit(self):
  297. ''' Pay 3100.0 with 'reconcile' as payment difference handling on two vendor billes (1000 + 2000). '''
  298. active_ids = (self.in_invoice_1 + self.in_invoice_2).ids
  299. payments = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids).create({
  300. 'amount': 3100.0,
  301. 'group_payment': True,
  302. 'payment_difference_handling': 'reconcile',
  303. 'writeoff_account_id': self.company_data['default_account_revenue'].id,
  304. 'writeoff_label': 'writeoff',
  305. 'payment_method_line_id': self.inbound_payment_method_line.id,
  306. })._create_payments()
  307. self.assertRecordValues(payments, [{
  308. 'ref': 'BILL/2017/01/0001 BILL/2017/01/0002',
  309. 'payment_method_line_id': self.inbound_payment_method_line.id,
  310. }])
  311. self.assertRecordValues(payments.line_ids.sorted('balance'), [
  312. # Liquidity line:
  313. {
  314. 'debit': 0.0,
  315. 'credit': 3100.0,
  316. 'currency_id': self.company_data['currency'].id,
  317. 'amount_currency': -3100.0,
  318. 'reconciled': False,
  319. },
  320. # Writeoff line:
  321. {
  322. 'debit': 100.0,
  323. 'credit': 0.0,
  324. 'currency_id': self.company_data['currency'].id,
  325. 'amount_currency': 100.0,
  326. 'reconciled': False,
  327. },
  328. # Payable line:
  329. {
  330. 'debit': 3000.0,
  331. 'credit': 0.0,
  332. 'currency_id': self.company_data['currency'].id,
  333. 'amount_currency': 3000.0,
  334. 'reconciled': True,
  335. },
  336. ])
  337. def test_register_payment_single_batch_not_grouped(self):
  338. ''' Choose to pay two customer invoices with separated payments (1000 + 2000). '''
  339. active_ids = (self.out_invoice_1 + self.out_invoice_2).ids
  340. payment_register = self.env['account.payment.register']\
  341. .with_context(active_model='account.move', active_ids=active_ids)\
  342. .create({
  343. 'group_payment': False,
  344. 'amount': 1200.0
  345. })
  346. self.assertRecordValues(payment_register, [{'payment_difference': 1800.0}])
  347. payments = payment_register._create_payments()
  348. self.assertRecordValues(payments, [
  349. {
  350. 'ref': 'INV/2017/00001',
  351. 'payment_method_line_id': self.inbound_payment_method_line.id,
  352. },
  353. {
  354. 'ref': 'INV/2017/00002',
  355. 'payment_method_line_id': self.inbound_payment_method_line.id,
  356. },
  357. ])
  358. self.assertRecordValues(payments[0].line_ids.sorted('balance') + payments[1].line_ids.sorted('balance'), [
  359. # == Payment 1: to pay out_invoice_1 ==
  360. # Receivable line:
  361. {
  362. 'debit': 0.0,
  363. 'credit': 500.0,
  364. 'currency_id': self.currency_data['currency'].id,
  365. 'amount_currency': -1000.0,
  366. 'reconciled': True,
  367. },
  368. # Liquidity line:
  369. {
  370. 'debit': 500.0,
  371. 'credit': 0.0,
  372. 'currency_id': self.currency_data['currency'].id,
  373. 'amount_currency': 1000.0,
  374. 'reconciled': False,
  375. },
  376. # == Payment 2: to pay out_invoice_2 ==
  377. # Receivable line:
  378. {
  379. 'debit': 0.0,
  380. 'credit': 1000.0,
  381. 'currency_id': self.currency_data['currency'].id,
  382. 'amount_currency': -2000.0,
  383. 'reconciled': True,
  384. },
  385. # Liquidity line:
  386. {
  387. 'debit': 1000.0,
  388. 'credit': 0.0,
  389. 'currency_id': self.currency_data['currency'].id,
  390. 'amount_currency': 2000.0,
  391. 'reconciled': False,
  392. },
  393. ])
  394. def test_register_payment_different_type_single_batch_not_grouped(self):
  395. """ Choose to pay a bill and a refund with separated payments (1000 + -2000)."""
  396. active_ids = (self.in_invoice_1 + self.in_refund_1).ids
  397. payments = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids).create({
  398. 'group_payment': False,
  399. })._create_payments()
  400. self.assertRecordValues(payments[0], [
  401. {
  402. 'ref': 'BILL/2017/01/0001',
  403. 'payment_type': 'outbound',
  404. }
  405. ])
  406. self.assertRecordValues(payments[1], [
  407. {
  408. 'ref': 'RBILL/2017/01/0001',
  409. 'payment_type': 'inbound',
  410. },
  411. ])
  412. self.assertRecordValues(payments[0].line_ids.sorted('balance'), [
  413. # == Payment 1: to pay in_invoice_1 ==
  414. # Liquidity line:
  415. {
  416. 'debit': 0.0,
  417. 'credit': 1000.0,
  418. 'currency_id': self.company_data['currency'].id,
  419. 'amount_currency': -1000.0,
  420. 'reconciled': False,
  421. },
  422. # Payable line:
  423. {
  424. 'debit': 1000.0,
  425. 'credit': 0.0,
  426. 'currency_id': self.company_data['currency'].id,
  427. 'amount_currency': 1000.0,
  428. 'reconciled': True,
  429. },
  430. ])
  431. self.assertRecordValues(payments[1].line_ids.sorted('balance'), [
  432. # == Payment 2: to pay in_refund_1 ==
  433. # Payable line:
  434. {
  435. 'debit': 0.0,
  436. 'credit': 1600.0,
  437. 'currency_id': self.company_data['currency'].id,
  438. 'amount_currency': -1600.0,
  439. 'reconciled': True,
  440. },
  441. # Liquidity line:
  442. {
  443. 'debit': 1600.0,
  444. 'credit': 0.0,
  445. 'currency_id': self.company_data['currency'].id,
  446. 'amount_currency': 1600.0,
  447. 'reconciled': False,
  448. },
  449. ])
  450. def test_register_payment_single_batch_grouped_with_credit_note(self):
  451. ''' Pay 1400.0 on two vendor bills (1000.0 + 2000.0) and one credit note (1600.0). '''
  452. active_ids = (self.in_invoice_1 + self.in_invoice_2 + self.in_refund_1).ids
  453. payments = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids).create({
  454. 'group_payment': True,
  455. })._create_payments()
  456. self.assertRecordValues(payments, [
  457. {
  458. 'ref': 'BILL/2017/01/0001 BILL/2017/01/0002 RBILL/2017/01/0001',
  459. 'payment_method_line_id': self.outbound_payment_method_line.id,
  460. },
  461. ])
  462. self.assertRecordValues(payments[0].line_ids.sorted('balance'), [
  463. # Liquidity line:
  464. {
  465. 'debit': 0.0,
  466. 'credit': 1400.0,
  467. 'currency_id': self.company_data['currency'].id,
  468. 'amount_currency': -1400.0,
  469. 'reconciled': False,
  470. },
  471. # Payable line:
  472. {
  473. 'debit': 1400.0,
  474. 'credit': 0.0,
  475. 'currency_id': self.company_data['currency'].id,
  476. 'amount_currency': 1400.0,
  477. 'reconciled': True,
  478. },
  479. ])
  480. def test_register_payment_multiple_batch_grouped_with_credit_note(self):
  481. ''' Do not batch payments if multiple partner_bank_id '''
  482. bank1 = self.env['res.partner.bank'].create({
  483. 'acc_number': 'BE43798822936101',
  484. 'partner_id': self.partner_a.id,
  485. })
  486. bank2 = self.env['res.partner.bank'].create({
  487. 'acc_number': 'BE85812541345906',
  488. 'partner_id': self.partner_a.id,
  489. })
  490. self.in_invoice_1.partner_bank_id = bank1
  491. self.in_invoice_2.partner_bank_id = bank2
  492. active_ids = (self.in_invoice_1 + self.in_invoice_2 + self.in_refund_1).ids
  493. payments = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids).create({
  494. 'group_payment': True,
  495. })._create_payments()
  496. self.assertRecordValues(payments, [
  497. {
  498. 'ref': 'BILL/2017/01/0001',
  499. 'payment_method_line_id': self.outbound_payment_method_line.id,
  500. },
  501. {
  502. 'ref': 'BILL/2017/01/0002',
  503. 'payment_method_line_id': self.outbound_payment_method_line.id,
  504. },
  505. {
  506. 'ref': 'RBILL/2017/01/0001',
  507. 'payment_method_line_id': self.inbound_payment_method_line.id,
  508. },
  509. ])
  510. self.assertRecordValues(payments[0].line_ids.sorted('balance') + payments[1].line_ids.sorted('balance') + payments[2].line_ids.sorted('balance'), [
  511. # Liquidity line:
  512. {
  513. 'debit': 0.0,
  514. 'credit': 1000.0,
  515. 'currency_id': self.company_data['currency'].id,
  516. 'amount_currency': -1000.0,
  517. 'reconciled': False,
  518. },
  519. # Payable line:
  520. {
  521. 'debit': 1000.0,
  522. 'credit': 0.0,
  523. 'currency_id': self.company_data['currency'].id,
  524. 'amount_currency': 1000.0,
  525. 'reconciled': True,
  526. },
  527. # Liquidity line:
  528. {
  529. 'debit': 0.0,
  530. 'credit': 2000.0,
  531. 'currency_id': self.company_data['currency'].id,
  532. 'amount_currency': -2000.0,
  533. 'reconciled': False,
  534. },
  535. # Payable line:
  536. {
  537. 'debit': 2000.0,
  538. 'credit': 0.0,
  539. 'currency_id': self.company_data['currency'].id,
  540. 'amount_currency': 2000.0,
  541. 'reconciled': True,
  542. },
  543. # Receivable line:
  544. {
  545. 'debit': 0.0,
  546. 'credit': 1600.0,
  547. 'currency_id': self.company_data['currency'].id,
  548. 'amount_currency': -1600.0,
  549. 'reconciled': True,
  550. },
  551. # Liquidity line:
  552. {
  553. 'debit': 1600.0,
  554. 'credit': 0.0,
  555. 'currency_id': self.company_data['currency'].id,
  556. 'amount_currency': 1600.0,
  557. 'reconciled': False,
  558. },
  559. ])
  560. def test_register_payment_multi_batches_grouped(self):
  561. ''' Choose to pay multiple batches, one with two customer invoices (1000 + 2000)
  562. and one with a vendor bill of 600, by grouping payments.
  563. '''
  564. active_ids = (self.in_invoice_1 + self.in_invoice_2 + self.in_invoice_3).ids
  565. payment_register = self.env['account.payment.register']\
  566. .with_context(active_model='account.move', active_ids=active_ids)\
  567. .create({
  568. 'group_payment': True,
  569. 'amount': 1000.0
  570. # Test _compute_payment_difference. Since the partners_ids are not the same, this should be without effect.
  571. })
  572. payments = payment_register._create_payments()
  573. self.assertRecordValues(payment_register, [{
  574. 'payment_difference': 0.0
  575. }])
  576. self.assertRecordValues(payments, [
  577. {
  578. 'ref': 'BILL/2017/01/0001 BILL/2017/01/0002',
  579. 'payment_method_line_id': self.outbound_payment_method_line.id,
  580. },
  581. {
  582. 'ref': 'BILL/2017/01/0003',
  583. 'payment_method_line_id': self.outbound_payment_method_line.id,
  584. },
  585. ])
  586. self.assertRecordValues(payments[0].line_ids.sorted('balance') + payments[1].line_ids.sorted('balance'), [
  587. # == Payment 1: to pay in_invoice_1 & in_invoice_2 ==
  588. # Liquidity line:
  589. {
  590. 'debit': 0.0,
  591. 'credit': 3000.0,
  592. 'currency_id': self.company_data['currency'].id,
  593. 'amount_currency': -3000.0,
  594. 'reconciled': False,
  595. },
  596. # Payable line:
  597. {
  598. 'debit': 3000.0,
  599. 'credit': 0.0,
  600. 'currency_id': self.company_data['currency'].id,
  601. 'amount_currency': 3000.0,
  602. 'reconciled': True,
  603. },
  604. # == Payment 2: to pay in_invoice_3 ==
  605. # Liquidity line:
  606. {
  607. 'debit': 0.0,
  608. 'credit': 1500.0,
  609. 'currency_id': self.currency_data['currency'].id,
  610. 'amount_currency': -3000.0,
  611. 'reconciled': False,
  612. },
  613. # Payable line:
  614. {
  615. 'debit': 1500.0,
  616. 'credit': 0.0,
  617. 'currency_id': self.currency_data['currency'].id,
  618. 'amount_currency': 3000.0,
  619. 'reconciled': True,
  620. },
  621. ])
  622. def test_register_payment_multi_batches_not_grouped(self):
  623. ''' Choose to pay multiple batches, one with two customer invoices (1000 + 2000)
  624. and one with a vendor bill of 600, by splitting payments.
  625. '''
  626. self.in_invoice_1.partner_bank_id = self.partner_bank_account1
  627. self.in_invoice_2.partner_bank_id = self.partner_bank_account2
  628. active_ids = (self.in_invoice_1 + self.in_invoice_2 + self.in_invoice_3).ids
  629. payments = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids).create({
  630. 'group_payment': False,
  631. })._create_payments()
  632. self.assertRecordValues(payments, [
  633. {
  634. 'journal_id': self.bank_journal_1.id,
  635. 'ref': 'BILL/2017/01/0001',
  636. 'payment_method_line_id': self.outbound_payment_method_line.id,
  637. 'partner_bank_id': self.partner_bank_account1.id,
  638. },
  639. {
  640. 'journal_id': self.bank_journal_1.id,
  641. 'ref': 'BILL/2017/01/0002',
  642. 'payment_method_line_id': self.outbound_payment_method_line.id,
  643. 'partner_bank_id': self.partner_bank_account2.id,
  644. },
  645. {
  646. 'journal_id': self.bank_journal_1.id,
  647. 'ref': 'BILL/2017/01/0003',
  648. 'payment_method_line_id': self.outbound_payment_method_line.id,
  649. 'partner_bank_id': False,
  650. },
  651. ])
  652. self.assertRecordValues(payments[0].line_ids.sorted('balance') + payments[1].line_ids.sorted('balance') + payments[2].line_ids.sorted('balance'), [
  653. # == Payment 1: to pay in_invoice_1 ==
  654. # Liquidity line:
  655. {
  656. 'debit': 0.0,
  657. 'credit': 1000.0,
  658. 'currency_id': self.company_data['currency'].id,
  659. 'amount_currency': -1000.0,
  660. 'reconciled': False,
  661. },
  662. # Payable line:
  663. {
  664. 'debit': 1000.0,
  665. 'credit': 0.0,
  666. 'currency_id': self.company_data['currency'].id,
  667. 'amount_currency': 1000.0,
  668. 'reconciled': True,
  669. },
  670. # == Payment 2: to pay in_invoice_2 ==
  671. # Liquidity line:
  672. {
  673. 'debit': 0.0,
  674. 'credit': 2000.0,
  675. 'currency_id': self.company_data['currency'].id,
  676. 'amount_currency': -2000.0,
  677. 'reconciled': False,
  678. },
  679. # Payable line:
  680. {
  681. 'debit': 2000.0,
  682. 'credit': 0.0,
  683. 'currency_id': self.company_data['currency'].id,
  684. 'amount_currency': 2000.0,
  685. 'reconciled': True,
  686. },
  687. # == Payment 3: to pay in_invoice_3 ==
  688. # Liquidity line:
  689. {
  690. 'debit': 0.0,
  691. 'credit': 1500.0,
  692. 'currency_id': self.currency_data['currency'].id,
  693. 'amount_currency': -3000.0,
  694. 'reconciled': False,
  695. },
  696. # Payable line:
  697. {
  698. 'debit': 1500.0,
  699. 'credit': 0.0,
  700. 'currency_id': self.currency_data['currency'].id,
  701. 'amount_currency': 3000.0,
  702. 'reconciled': True,
  703. },
  704. ])
  705. def test_register_payment_constraints(self):
  706. # Test to register a payment for a draft journal entry.
  707. self.out_invoice_1.button_draft()
  708. with self.assertRaises(UserError), self.cr.savepoint():
  709. self.env['account.payment.register']\
  710. .with_context(active_model='account.move', active_ids=self.out_invoice_1.ids)\
  711. .create({})
  712. # Test to register a payment for an already fully reconciled journal entry.
  713. self.env['account.payment.register']\
  714. .with_context(active_model='account.move', active_ids=self.out_invoice_2.ids)\
  715. .create({})\
  716. ._create_payments()
  717. with self.assertRaises(UserError), self.cr.savepoint():
  718. self.env['account.payment.register']\
  719. .with_context(active_model='account.move', active_ids=self.out_invoice_2.ids)\
  720. .create({})
  721. def test_register_payment_multi_currency_rounding_issue_positive_delta(self):
  722. ''' When registering a payment using a different currency than the invoice one, the invoice must be fully paid
  723. at the end whatever the currency rate.
  724. '''
  725. payment = self.env['account.payment.register']\
  726. .with_context(active_model='account.move', active_ids=self.out_invoice_3.ids)\
  727. .create({
  728. 'currency_id': self.currency_data_3['currency'].id,
  729. 'amount': 0.12,
  730. })\
  731. ._create_payments()
  732. self.assertRecordValues(payment.line_ids.sorted('balance'), [
  733. # Receivable line:
  734. {
  735. 'debit': 0.0,
  736. 'credit': 12.01,
  737. 'currency_id': self.currency_data_3['currency'].id,
  738. 'amount_currency': -0.12,
  739. 'reconciled': True,
  740. },
  741. # Liquidity line:
  742. {
  743. 'debit': 12.01,
  744. 'credit': 0.0,
  745. 'currency_id': self.currency_data_3['currency'].id,
  746. 'amount_currency': 0.12,
  747. 'reconciled': False,
  748. },
  749. ])
  750. def test_register_payment_multi_currency_rounding_issue_negative_delta(self):
  751. ''' When registering a payment using a different currency than the invoice one, the invoice must be fully paid
  752. at the end whatever the currency rate.
  753. '''
  754. payment = self.env['account.payment.register']\
  755. .with_context(active_model='account.move', active_ids=self.out_invoice_4.ids)\
  756. .create({
  757. 'currency_id': self.currency_data_3['currency'].id,
  758. 'amount': 0.12,
  759. })\
  760. ._create_payments()
  761. self.assertRecordValues(payment.line_ids.sorted('balance'), [
  762. # Receivable line:
  763. {
  764. 'debit': 0.0,
  765. 'credit': 11.99,
  766. 'currency_id': self.currency_data_3['currency'].id,
  767. 'amount_currency': -0.12,
  768. 'reconciled': True,
  769. },
  770. # Liquidity line:
  771. {
  772. 'debit': 11.99,
  773. 'credit': 0.0,
  774. 'currency_id': self.currency_data_3['currency'].id,
  775. 'amount_currency': 0.12,
  776. 'reconciled': False,
  777. },
  778. ])
  779. def test_register_payment_multi_currency_rounding_issue_writeoff_lower_amount_keep_open(self):
  780. payment = self.env['account.payment.register']\
  781. .with_context(active_model='account.move', active_ids=self.out_invoice_3.ids)\
  782. .create({
  783. 'currency_id': self.currency_data_3['currency'].id,
  784. 'amount': 0.08,
  785. 'payment_difference_handling': 'open',
  786. })\
  787. ._create_payments()
  788. self.assertRecordValues(payment.line_ids.sorted('balance'), [
  789. # Receivable line:
  790. {
  791. 'debit': 0.0,
  792. 'credit': 8.0,
  793. 'currency_id': self.currency_data_3['currency'].id,
  794. 'amount_currency': -0.08,
  795. 'reconciled': True,
  796. },
  797. # Liquidity line:
  798. {
  799. 'debit': 8.0,
  800. 'credit': 0.0,
  801. 'currency_id': self.currency_data_3['currency'].id,
  802. 'amount_currency': 0.08,
  803. 'reconciled': False,
  804. },
  805. ])
  806. def test_register_payment_multi_currency_rounding_issue_writeoff_lower_amount_reconcile_positive_delta(self):
  807. payment = self.env['account.payment.register']\
  808. .with_context(active_model='account.move', active_ids=self.out_invoice_3.ids)\
  809. .create({
  810. 'currency_id': self.currency_data_3['currency'].id,
  811. 'amount': 0.08,
  812. 'payment_difference_handling': 'reconcile',
  813. 'writeoff_account_id': self.company_data['default_account_revenue'].id,
  814. 'writeoff_label': 'writeoff',
  815. })\
  816. ._create_payments()
  817. self.assertRecordValues(payment.line_ids.sorted('balance'), [
  818. # Receivable line:
  819. {
  820. 'debit': 0.0,
  821. 'credit': 12.01,
  822. 'currency_id': self.currency_data_3['currency'].id,
  823. 'amount_currency': -0.12,
  824. 'reconciled': True,
  825. },
  826. # Write-off line:
  827. {
  828. 'debit': 4.0,
  829. 'credit': 0.0,
  830. 'currency_id': self.currency_data_3['currency'].id,
  831. 'amount_currency': 0.04,
  832. 'reconciled': False,
  833. },
  834. # Liquidity line:
  835. {
  836. 'debit': 8.01,
  837. 'credit': 0.0,
  838. 'currency_id': self.currency_data_3['currency'].id,
  839. 'amount_currency': 0.08,
  840. 'reconciled': False,
  841. },
  842. ])
  843. def test_register_payment_multi_currency_rounding_issue_writeoff_lower_amount_reconcile_negative_delta(self):
  844. payment = self.env['account.payment.register']\
  845. .with_context(active_model='account.move', active_ids=self.out_invoice_4.ids)\
  846. .create({
  847. 'currency_id': self.currency_data_3['currency'].id,
  848. 'amount': 0.08,
  849. 'payment_difference_handling': 'reconcile',
  850. 'writeoff_account_id': self.company_data['default_account_revenue'].id,
  851. 'writeoff_label': 'writeoff',
  852. })\
  853. ._create_payments()
  854. self.assertRecordValues(payment.line_ids.sorted('balance'), [
  855. # Receivable line:
  856. {
  857. 'debit': 0.0,
  858. 'credit': 11.99,
  859. 'currency_id': self.currency_data_3['currency'].id,
  860. 'amount_currency': -0.12,
  861. 'reconciled': True,
  862. },
  863. # Write-off line:
  864. {
  865. 'debit': 4.0,
  866. 'credit': 0.0,
  867. 'currency_id': self.currency_data_3['currency'].id,
  868. 'amount_currency': 0.04,
  869. 'reconciled': False,
  870. },
  871. # Liquidity line:
  872. {
  873. 'debit': 7.99,
  874. 'credit': 0.0,
  875. 'currency_id': self.currency_data_3['currency'].id,
  876. 'amount_currency': 0.08,
  877. 'reconciled': False,
  878. },
  879. ])
  880. def test_register_payment_multi_currency_rounding_issue_writeoff_higher_amount_reconcile_positive_delta(self):
  881. payment = self.env['account.payment.register']\
  882. .with_context(active_model='account.move', active_ids=self.out_invoice_3.ids)\
  883. .create({
  884. 'currency_id': self.currency_data_3['currency'].id,
  885. 'amount': 0.16,
  886. 'payment_difference_handling': 'reconcile',
  887. 'writeoff_account_id': self.company_data['default_account_revenue'].id,
  888. 'writeoff_label': 'writeoff',
  889. })\
  890. ._create_payments()
  891. self.assertRecordValues(payment.line_ids.sorted('balance'), [
  892. # Receivable line:
  893. {
  894. 'debit': 0.0,
  895. 'credit': 12.01,
  896. 'currency_id': self.currency_data_3['currency'].id,
  897. 'amount_currency': -0.12,
  898. 'reconciled': True,
  899. },
  900. # Write-off line:
  901. {
  902. 'debit': 0.0,
  903. 'credit': 4.0,
  904. 'currency_id': self.currency_data_3['currency'].id,
  905. 'amount_currency': -0.04,
  906. 'reconciled': False,
  907. },
  908. # Liquidity line:
  909. {
  910. 'debit': 16.01,
  911. 'credit': 0.0,
  912. 'currency_id': self.currency_data_3['currency'].id,
  913. 'amount_currency': 0.16,
  914. 'reconciled': False,
  915. },
  916. ])
  917. def test_register_payment_multi_currency_rounding_issue_writeoff_higher_amount_reconcile_negative_delta(self):
  918. payment = self.env['account.payment.register']\
  919. .with_context(active_model='account.move', active_ids=self.out_invoice_4.ids)\
  920. .create({
  921. 'currency_id': self.currency_data_3['currency'].id,
  922. 'amount': 0.16,
  923. 'payment_difference_handling': 'reconcile',
  924. 'writeoff_account_id': self.company_data['default_account_revenue'].id,
  925. 'writeoff_label': 'writeoff',
  926. })\
  927. ._create_payments()
  928. self.assertRecordValues(payment.line_ids.sorted('balance'), [
  929. # Receivable line:
  930. {
  931. 'debit': 0.0,
  932. 'credit': 11.99,
  933. 'currency_id': self.currency_data_3['currency'].id,
  934. 'amount_currency': -0.12,
  935. 'reconciled': True,
  936. },
  937. # Write-off line:
  938. {
  939. 'debit': 0.0,
  940. 'credit': 4.0,
  941. 'currency_id': self.currency_data_3['currency'].id,
  942. 'amount_currency': -0.04,
  943. 'reconciled': False,
  944. },
  945. # Liquidity line:
  946. {
  947. 'debit': 15.99,
  948. 'credit': 0.0,
  949. 'currency_id': self.currency_data_3['currency'].id,
  950. 'amount_currency': 0.16,
  951. 'reconciled': False,
  952. },
  953. ])
  954. def test_suggested_default_partner_bank_inbound_payment(self):
  955. """ Test the suggested bank account on the wizard for inbound payment. """
  956. self.out_invoice_1.partner_bank_id = False
  957. ctx = {'active_model': 'account.move', 'active_ids': self.out_invoice_1.ids}
  958. wizard = self.env['account.payment.register'].with_context(**ctx).create({})
  959. self.assertRecordValues(wizard, [{
  960. 'journal_id': self.bank_journal_1.id,
  961. 'available_partner_bank_ids': [],
  962. 'partner_bank_id': False,
  963. }])
  964. self.bank_journal_2.bank_account_id = self.out_invoice_1.partner_bank_id = self.comp_bank_account2
  965. wizard = self.env['account.payment.register'].with_context(**ctx).create({})
  966. self.assertRecordValues(wizard, [{
  967. 'journal_id': self.bank_journal_2.id,
  968. 'available_partner_bank_ids': self.comp_bank_account2.ids,
  969. 'partner_bank_id': self.comp_bank_account2.id,
  970. }])
  971. wizard.journal_id = self.bank_journal_1
  972. self.assertRecordValues(wizard, [{
  973. 'journal_id': self.bank_journal_1.id,
  974. 'available_partner_bank_ids': [],
  975. 'partner_bank_id': False,
  976. }])
  977. def test_suggested_default_partner_bank_outbound_payment(self):
  978. """ Test the suggested bank account on the wizard for outbound payment. """
  979. self.in_invoice_1.partner_bank_id = False
  980. ctx = {'active_model': 'account.move', 'active_ids': self.in_invoice_1.ids}
  981. wizard = self.env['account.payment.register'].with_context(**ctx).create({})
  982. self.assertRecordValues(wizard, [{
  983. 'journal_id': self.bank_journal_1.id,
  984. 'available_partner_bank_ids': self.partner_a.bank_ids.ids,
  985. 'partner_bank_id': self.partner_bank_account1.id,
  986. }])
  987. self.in_invoice_1.partner_bank_id = self.partner_bank_account2
  988. wizard = self.env['account.payment.register'].with_context(**ctx).create({})
  989. self.assertRecordValues(wizard, [{
  990. 'journal_id': self.bank_journal_1.id,
  991. 'available_partner_bank_ids': self.partner_a.bank_ids.ids,
  992. 'partner_bank_id': self.partner_bank_account2.id,
  993. }])
  994. wizard.journal_id = self.bank_journal_2
  995. self.assertRecordValues(wizard, [{
  996. 'journal_id': self.bank_journal_2.id,
  997. 'available_partner_bank_ids': self.partner_a.bank_ids.ids,
  998. 'partner_bank_id': self.partner_bank_account2.id,
  999. }])
  1000. def test_register_payment_inbound_multiple_bank_account(self):
  1001. """ Pay customer invoices with different bank accounts. """
  1002. self.out_invoice_1.partner_bank_id = self.comp_bank_account1
  1003. self.out_invoice_2.partner_bank_id = self.comp_bank_account2
  1004. self.bank_journal_2.bank_account_id = self.comp_bank_account2
  1005. ctx = {'active_model': 'account.move', 'active_ids': (self.out_invoice_1 + self.out_invoice_2).ids}
  1006. wizard = self.env['account.payment.register'].with_context(**ctx).create({'journal_id': self.bank_journal_2.id})
  1007. payments = wizard._create_payments()
  1008. self.assertRecordValues(payments, [
  1009. {
  1010. 'journal_id': self.bank_journal_2.id,
  1011. 'ref': 'INV/2017/00001',
  1012. 'partner_bank_id': self.comp_bank_account2.id,
  1013. },
  1014. {
  1015. 'journal_id': self.bank_journal_2.id,
  1016. 'ref': 'INV/2017/00002',
  1017. 'partner_bank_id': self.comp_bank_account2.id,
  1018. },
  1019. ])
  1020. def test_register_payment_invoice_foreign_curr_payment_comp_curr(self):
  1021. # Invoice 1200 Gol = 400 USD
  1022. invoice = self.env['account.move'].create({
  1023. 'move_type': 'out_invoice',
  1024. 'date': '2016-01-01',
  1025. 'invoice_date': '2016-01-01',
  1026. 'partner_id': self.partner_a.id,
  1027. 'currency_id': self.currency_data['currency'].id,
  1028. 'invoice_line_ids': [Command.create(
  1029. {'product_id': self.product_a.id,
  1030. 'price_unit': 1200.0,
  1031. 'tax_ids': [],
  1032. })],
  1033. })
  1034. invoice.action_post()
  1035. # Payment of 600 USD (equivalent to 1200 Gol in 2017).
  1036. # 600.0 USD should be computed correctly to fully paid the invoices.
  1037. wizard = self.env['account.payment.register']\
  1038. .with_context(active_model='account.move', active_ids=invoice.ids)\
  1039. .create({
  1040. 'currency_id': self.company_data['currency'].id,
  1041. 'payment_date': '2017-01-01',
  1042. })
  1043. self.assertRecordValues(wizard, [{
  1044. 'amount': 600.0,
  1045. 'payment_difference': 0.0,
  1046. 'currency_id': self.company_data['currency'].id,
  1047. }])
  1048. payment = wizard._create_payments()
  1049. lines = (invoice + payment.move_id).line_ids.filtered(lambda x: x.account_type == 'asset_receivable')
  1050. self.assertRecordValues(lines, [
  1051. {'amount_residual': 0.0, 'amount_residual_currency': 0.0, 'currency_id': self.currency_data['currency'].id, 'reconciled': True},
  1052. {'amount_residual': 0.0, 'amount_residual_currency': 0.0, 'currency_id': self.company_data['currency'].id, 'reconciled': True},
  1053. ])
  1054. def test_register_payment_invoice_comp_curr_payment_foreign_curr(self):
  1055. # Invoice of 600 USD (equivalent to 1200 Gol in 2017).
  1056. invoice = self.env['account.move'].create({
  1057. 'move_type': 'out_invoice',
  1058. 'date': '2016-01-01',
  1059. 'invoice_date': '2016-01-01',
  1060. 'partner_id': self.partner_a.id,
  1061. 'currency_id': self.company_data['currency'].id,
  1062. 'invoice_line_ids': [Command.create({
  1063. 'product_id': self.product_a.id,
  1064. 'price_unit': 600.0,
  1065. 'tax_ids': [],
  1066. })],
  1067. })
  1068. invoice.action_post()
  1069. # Payment of 600 USD = 1200 Gol.
  1070. # 1200.0 Gol should be computed correctly to fully paid the invoices.
  1071. wizard = self.env['account.payment.register']\
  1072. .with_context(active_model='account.move', active_ids=invoice.ids)\
  1073. .create({
  1074. 'currency_id': self.currency_data['currency'].id,
  1075. 'payment_date': '2017-01-01',
  1076. })
  1077. self.assertRecordValues(wizard, [{
  1078. 'amount': 1200.0,
  1079. 'payment_difference': 0.0,
  1080. 'currency_id': self.currency_data['currency'].id,
  1081. }])
  1082. payment = wizard._create_payments()
  1083. lines = (invoice + payment.move_id).line_ids.filtered(lambda x: x.account_type == 'asset_receivable')
  1084. self.assertRecordValues(lines, [
  1085. {'amount_residual': 0.0, 'amount_residual_currency': 0.0, 'currency_id': self.company_data['currency'].id, 'reconciled': True},
  1086. {'amount_residual': 0.0, 'amount_residual_currency': 0.0, 'currency_id': self.currency_data['currency'].id, 'reconciled': True},
  1087. ])
  1088. def test_payment_method_different_type_single_batch_not_grouped(self):
  1089. """ Test payment methods when paying a bill and a refund with separated payments (1000 + -2000)."""
  1090. invoice_1 = self.in_invoice_1
  1091. invoice_2 = invoice_1.copy({'invoice_date': invoice_1.invoice_date, 'partner_id': self.partner_b.id})
  1092. refund_1, refund_2 = self.env['account.move'].create([
  1093. {
  1094. 'move_type': 'in_refund',
  1095. 'date': '2017-01-01',
  1096. 'invoice_date': '2017-01-01',
  1097. 'partner_id': self.partner_a.id,
  1098. 'invoice_line_ids': [(0, 0, {'product_id': self.product_a.id, 'price_unit': 1600.0, 'tax_ids': False})],
  1099. },
  1100. {
  1101. 'move_type': 'in_refund',
  1102. 'date': '2017-01-01',
  1103. 'invoice_date': '2017-01-01',
  1104. 'partner_id': self.partner_b.copy({'property_account_position_id': False}).id,
  1105. 'invoice_line_ids': [(0, 0, {'product_id': self.product_a.id, 'price_unit': 1600.0, 'tax_ids': False})],
  1106. },
  1107. ])
  1108. (invoice_2 + refund_1 + refund_2).action_post()
  1109. for moves in ((invoice_1 + invoice_2), (refund_1 + refund_2)):
  1110. wizard = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=moves.ids).create({
  1111. 'group_payment': False,
  1112. })
  1113. expected_available_payment_method_lines = wizard.journal_id.inbound_payment_method_line_ids if moves[0].move_type == 'in_refund' else wizard.journal_id.outbound_payment_method_line_ids
  1114. self.assertRecordValues(wizard, [
  1115. {
  1116. 'available_payment_method_line_ids': expected_available_payment_method_lines.ids,
  1117. 'payment_method_line_id': expected_available_payment_method_lines[:1].id,
  1118. }
  1119. ])
  1120. active_ids = (invoice_1 + invoice_2 + refund_1 + refund_2).ids
  1121. payments = self.env['account.payment.register'].with_context(active_model='account.move', active_ids=active_ids).create({
  1122. 'group_payment': False,
  1123. })._create_payments()
  1124. self.assertRecordValues(payments[0], [
  1125. {
  1126. 'ref': 'BILL/2017/01/0001',
  1127. 'payment_method_line_id': self.bank_journal_1.outbound_payment_method_line_ids[0].id,
  1128. 'payment_type': 'outbound',
  1129. }
  1130. ])
  1131. self.assertRecordValues(payments[1], [
  1132. {
  1133. 'ref': 'BILL/2017/01/0004',
  1134. 'payment_method_line_id': self.bank_journal_1.outbound_payment_method_line_ids[0].id,
  1135. 'payment_type': 'outbound',
  1136. }
  1137. ])
  1138. self.assertRecordValues(payments[2], [
  1139. {
  1140. 'ref': 'RBILL/2017/01/0002',
  1141. 'payment_method_line_id': self.bank_journal_1.inbound_payment_method_line_ids[0].id,
  1142. 'payment_type': 'inbound',
  1143. },
  1144. ])
  1145. self.assertRecordValues(payments[3], [
  1146. {
  1147. 'ref': 'RBILL/2017/01/0003',
  1148. 'payment_method_line_id': self.bank_journal_1.inbound_payment_method_line_ids[0].id,
  1149. 'payment_type': 'inbound',
  1150. },
  1151. ])
  1152. self.assertRecordValues(payments[0].line_ids.sorted('balance'), [
  1153. # == Payment 1: to pay invoice_1 ==
  1154. # Liquidity line:
  1155. {
  1156. 'debit': 0.0,
  1157. 'credit': 1000.0,
  1158. 'currency_id': self.company_data['currency'].id,
  1159. 'amount_currency': -1000.0,
  1160. 'reconciled': False,
  1161. },
  1162. # Payable line:
  1163. {
  1164. 'debit': 1000.0,
  1165. 'credit': 0.0,
  1166. 'currency_id': self.company_data['currency'].id,
  1167. 'amount_currency': 1000.0,
  1168. 'reconciled': True,
  1169. },
  1170. ])
  1171. self.assertRecordValues(payments[1].line_ids.sorted('balance'), [
  1172. # == Payment 2: to pay invoice_2 ==
  1173. # Payable line:
  1174. {
  1175. 'debit': 0.0,
  1176. 'credit': 1000.0,
  1177. 'currency_id': self.company_data['currency'].id,
  1178. 'amount_currency': -1000.0,
  1179. 'reconciled': False,
  1180. },
  1181. # Liquidity line:
  1182. {
  1183. 'debit': 1000.0,
  1184. 'credit': 0.0,
  1185. 'currency_id': self.company_data['currency'].id,
  1186. 'amount_currency': 1000.0,
  1187. 'reconciled': True,
  1188. },
  1189. ])
  1190. self.assertRecordValues(payments[2].line_ids.sorted('balance'), [
  1191. # == Payment 3: to pay refund_1 ==
  1192. # Liquidity line:
  1193. {
  1194. 'debit': 0.0,
  1195. 'credit': 1600.0,
  1196. 'currency_id': self.company_data['currency'].id,
  1197. 'amount_currency': -1600.0,
  1198. 'reconciled': True,
  1199. },
  1200. # Payable line:
  1201. {
  1202. 'debit': 1600.0,
  1203. 'credit': 0.0,
  1204. 'currency_id': self.company_data['currency'].id,
  1205. 'amount_currency': 1600.0,
  1206. 'reconciled': False,
  1207. },
  1208. ])
  1209. self.assertRecordValues(payments[3].line_ids.sorted('balance'), [
  1210. # == Payment 4: to pay refund_2 ==
  1211. # Payable line:
  1212. {
  1213. 'debit': 0.0,
  1214. 'credit': 1600.0,
  1215. 'currency_id': self.company_data['currency'].id,
  1216. 'amount_currency': -1600.0,
  1217. 'reconciled': True,
  1218. },
  1219. # Liquidity line:
  1220. {
  1221. 'debit': 1600.0,
  1222. 'credit': 0.0,
  1223. 'currency_id': self.company_data['currency'].id,
  1224. 'amount_currency': 1600.0,
  1225. 'reconciled': False,
  1226. },
  1227. ])