test_purchase_discount.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. # (c) 2015 Pedro M. Baeza
  3. # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
  4. import openerp.tests.common as common
  5. from openerp import workflow, fields
  6. class TestPurchaseOrder(common.TransactionCase):
  7. def setUp(self):
  8. super(TestPurchaseOrder, self).setUp()
  9. self.product_1 = self.env.ref('product.product_product_4')
  10. self.product_2 = self.env.ref('product.product_product_5b')
  11. po_model = self.env['purchase.order.line']
  12. self.purchase_order = self.env['purchase.order'].create(
  13. {'partner_id': self.env.ref('base.res_partner_3').id,
  14. 'pricelist_id': self.env.ref('purchase.list0').id,
  15. 'location_id': self.env.ref('stock.stock_location_stock').id})
  16. self.po_line_1 = po_model.create(
  17. {'order_id': self.purchase_order.id,
  18. 'product_id': self.product_1.id,
  19. 'date_planned': fields.Datetime.now(),
  20. 'name': 'Test',
  21. 'product_qty': 1.0,
  22. 'discount': 50.0,
  23. 'price_unit': 10.0})
  24. self.tax = self.env['account.tax'].create(
  25. {'name': 'Sample tax 15%',
  26. 'type': 'percent',
  27. 'amount': 0.15})
  28. self.po_line_2 = po_model.create(
  29. {'order_id': self.purchase_order.id,
  30. 'product_id': self.product_2.id,
  31. 'date_planned': fields.Datetime.now(),
  32. 'name': 'Test',
  33. 'product_qty': 10.0,
  34. 'discount': 30,
  35. 'taxes_id': [(6, 0, [self.tax.id])],
  36. 'price_unit': 230.0})
  37. def test_purchase_order_vals(self):
  38. self.assertEqual(self.po_line_1.price_subtotal, 5.0)
  39. self.assertEqual(self.po_line_2.price_subtotal, 1610.0)
  40. self.assertEqual(self.purchase_order.amount_untaxed, 1615.0)
  41. self.assertEqual(self.purchase_order.amount_tax, 241.5)
  42. def test_make_invoice_draft_invoice(self):
  43. self.purchase_order.invoice_method = 'order'
  44. workflow.trg_validate(
  45. self.uid, 'purchase.order', self.purchase_order.id,
  46. 'purchase_confirm', self.cr)
  47. self.assertEqual(self.po_line_1.invoice_lines.discount, 50)
  48. self.assertEqual(self.po_line_2.invoice_lines.discount, 30)
  49. def test_make_invoice_from_picking(self):
  50. self.purchase_order.invoice_method = 'picking'
  51. workflow.trg_validate(
  52. self.uid, 'purchase.order', self.purchase_order.id,
  53. 'purchase_confirm', self.cr)
  54. invoice_ids = self.purchase_order.picking_ids.action_invoice_create(
  55. self.env.ref('account.expenses_journal').id, type='in_invoice')
  56. invoice = self.env['account.invoice'].browse(invoice_ids[0])
  57. self.assertEqual(invoice.invoice_line[0].discount, 50)
  58. self.assertEqual(invoice.invoice_line[1].discount, 30)
  59. def test_make_invoice_from_returned_picking(self):
  60. self.purchase_order.invoice_method = 'picking'
  61. workflow.trg_validate(
  62. self.uid, 'purchase.order', self.purchase_order.id,
  63. 'purchase_confirm', self.cr)
  64. self.purchase_order.picking_ids.do_transfer()
  65. picking = self.purchase_order.picking_ids[0]
  66. move = picking.move_lines[0]
  67. wiz = self.env['stock.return.picking'].with_context(
  68. active_id=picking.id).create(
  69. {'invoice_state': '2binvoiced'})
  70. wiz.with_context(active_id=picking.id).create_returns()
  71. returned_picking = self.env['stock.move'].search(
  72. [('origin_returned_move_id', '=', move.id)])[0].picking_id
  73. invoice_ids = returned_picking.action_invoice_create(
  74. self.env.ref('account.expenses_journal').id, type='in_invoice')
  75. invoice = self.env['account.invoice'].browse(invoice_ids[0])
  76. self.assertEqual(invoice.invoice_line[0].discount, 50)
  77. self.assertEqual(invoice.invoice_line[1].discount, 30)