|
| 1 | +# Copyright 2019 Elico Corp, Dominique K. <dominique.k@elico-corp.com.sg> |
| 2 | +# Copyright 2019 Ecosoft Co., Ltd., Kitti U. <kittiu@ecosoft.co.th> |
| 3 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
| 4 | + |
| 5 | +from odoo import fields |
| 6 | +from odoo.tests.common import TransactionCase |
| 7 | +from odoo.exceptions import UserError |
| 8 | +from odoo.tests.common import Form |
| 9 | + |
| 10 | + |
| 11 | +class TestPurchaseDeposit(TransactionCase): |
| 12 | + |
| 13 | + def setUp(self): |
| 14 | + super(TestPurchaseDeposit, self).setUp() |
| 15 | + self.product_model = self.env['product.product'] |
| 16 | + self.account_model = self.env['account.account'] |
| 17 | + self.invoice_model = self.env['account.invoice'] |
| 18 | + self.default_model = self.env['ir.default'] |
| 19 | + |
| 20 | + # Create Deposit Account |
| 21 | + self.account_deposit = self.account_model.create({ |
| 22 | + 'name': 'Purchase Deposit', |
| 23 | + 'code': '11620', |
| 24 | + 'user_type_id': self.env.ref( |
| 25 | + 'account.data_account_type_current_assets').id, |
| 26 | + }) |
| 27 | + # Create products: |
| 28 | + p1 = self.product1 = self.product_model.create({ |
| 29 | + 'name': 'Test Product 1', |
| 30 | + 'type': 'service', |
| 31 | + 'default_code': 'PROD1', |
| 32 | + 'purchase_method': 'purchase', |
| 33 | + }) |
| 34 | + |
| 35 | + self.po = self.env['purchase.order'].create({ |
| 36 | + 'partner_id': self.ref('base.res_partner_3'), |
| 37 | + 'order_line': [ |
| 38 | + (0, 0, {'product_id': p1.id, |
| 39 | + 'product_uom': p1.uom_id.id, |
| 40 | + 'name': p1.name, |
| 41 | + 'price_unit': 100.0, |
| 42 | + 'date_planned': fields.Datetime.now(), |
| 43 | + 'product_qty': 42.0})]}) |
| 44 | + |
| 45 | + def test_create_deposit_invoice(self): |
| 46 | + self.assertEqual(len(self.po.order_line), 1) |
| 47 | + # We create invoice from expense |
| 48 | + ctx = {'active_id': self.po.id, |
| 49 | + 'active_ids': [self.po.id], |
| 50 | + 'active_model': 'purchase.order'} |
| 51 | + CreateDeposit = self.env['purchase.advance.payment.inv'] |
| 52 | + self.po.button_confirm() |
| 53 | + with Form(CreateDeposit.with_context(ctx)) as f: |
| 54 | + f.advance_payment_method = 'percentage' |
| 55 | + f.deposit_account_id = self.account_deposit |
| 56 | + wizard = f.save() |
| 57 | + wizard.amount = 10.0 # 10% |
| 58 | + wizard.create_invoices() |
| 59 | + # New Purchase Deposit is created automatically |
| 60 | + deposit_id = self.default_model.get('purchase.advance.payment.inv', |
| 61 | + 'purchase_deposit_product_id') |
| 62 | + deposit = self.product_model.browse(deposit_id) |
| 63 | + self.assertEqual(deposit.name, 'Purchase Deposit') |
| 64 | + # 1 Deposit Invoice is created |
| 65 | + self.assertRecordValues(self.po.invoice_ids.invoice_line_ids, [ |
| 66 | + {'product_id': deposit.id, |
| 67 | + 'price_unit': 420.0, 'name': 'Deposit Payment', }, |
| 68 | + ]) |
| 69 | + # On Purchase Order, there will be new deposit line create |
| 70 | + self.assertRecordValues(self.po.order_line, [ |
| 71 | + {'product_id': self.product1.id, |
| 72 | + 'price_unit': 100.0, 'is_deposit': False}, |
| 73 | + {'product_id': deposit.id, |
| 74 | + 'price_unit': 420.0, 'is_deposit': True}, |
| 75 | + ]) |
| 76 | + # On Purchase Order, create normal billing |
| 77 | + res = self.po.with_context(create_bill=True).action_view_invoice() |
| 78 | + ctx = res.get('context') |
| 79 | + f = Form(self.invoice_model.with_context(ctx), |
| 80 | + view='account.invoice_supplier_form') |
| 81 | + invoice = f.save() |
| 82 | + self.assertRecordValues(invoice.invoice_line_ids, [ |
| 83 | + {'product_id': self.product1.id, |
| 84 | + 'price_unit': 100.0, 'quantity': 42}, |
| 85 | + {'product_id': deposit.id, |
| 86 | + 'price_unit': 420.0, 'quantity': -1}, |
| 87 | + ]) |
| 88 | + |
| 89 | + def test_create_deposit_invoice_exception(self): |
| 90 | + """This test focus on exception cases, when create deposit invoice, |
| 91 | + 1. This action is allowed only in Purchase Order sate |
| 92 | + 2. The value of the deposit must be positive |
| 93 | + 3. For type percentage, The percentage of the deposit must <= 100 |
| 94 | + 4. Purchase Deposit Product's purchase_method != purchase |
| 95 | + 5. Purchase Deposit Product's type != service |
| 96 | + """ |
| 97 | + self.assertEqual(len(self.po.order_line), 1) |
| 98 | + # We create invoice from expense |
| 99 | + ctx = {'active_id': self.po.id, |
| 100 | + 'active_ids': [self.po.id], |
| 101 | + 'active_model': 'purchase.order'} |
| 102 | + CreateDeposit = self.env['purchase.advance.payment.inv'] |
| 103 | + # 1. This action is allowed only in Purchase Order sate |
| 104 | + with self.assertRaises(UserError): |
| 105 | + Form(CreateDeposit.with_context(ctx)) # Initi wizard |
| 106 | + self.po.button_confirm() |
| 107 | + self.assertEqual(self.po.state, 'purchase') |
| 108 | + # 2. The value of the deposit must be positive |
| 109 | + f = Form(CreateDeposit.with_context(ctx)) |
| 110 | + f.advance_payment_method = 'fixed' |
| 111 | + f.amount = 0.0 |
| 112 | + f.deposit_account_id = self.account_deposit |
| 113 | + wizard = f.save() |
| 114 | + with self.assertRaises(UserError): |
| 115 | + wizard.create_invoices() |
| 116 | + # 3. For type percentage, The percentage of the deposit must <= 100 |
| 117 | + wizard.advance_payment_method = 'percentage' |
| 118 | + wizard.amount = 101.0 |
| 119 | + with self.assertRaises(UserError): |
| 120 | + wizard.create_invoices() |
| 121 | + wizard.amount = 10 |
| 122 | + # 4. Purchase Deposit Product's purchase_method != purchase |
| 123 | + deposit_id = self.default_model.get('purchase.advance.payment.inv', |
| 124 | + 'purchase_deposit_product_id') |
| 125 | + deposit = self.product_model.browse(deposit_id) |
| 126 | + deposit.purchase_method = 'receive' |
| 127 | + wizard.purchase_deposit_product_id = deposit |
| 128 | + with self.assertRaises(UserError): |
| 129 | + wizard.create_invoices() |
| 130 | + deposit.purchase_method = 'purchase' |
| 131 | + # 5. Purchase Deposit Product's type != service |
| 132 | + deposit.type = 'consu' |
| 133 | + with self.assertRaises(UserError): |
| 134 | + wizard.create_invoices() |
0 commit comments