|
| 1 | +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from odoo.tests.common import TransactionCase |
| 6 | + |
| 7 | +_logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +class TestCommon(TransactionCase): |
| 11 | + """Base test class with tracking disabled.""" |
| 12 | + |
| 13 | + @classmethod |
| 14 | + def setUpClass(cls): |
| 15 | + super().setUpClass() |
| 16 | + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) |
| 17 | + |
| 18 | + |
| 19 | +class TestInvoiceSendOnReconcileMixin: |
| 20 | + """Mixin with helper methods for invoice send on reconcile tests.""" |
| 21 | + |
| 22 | + def create_partner( self, name="Test Partner", email="[email protected]"): |
| 23 | + """Create a test partner with email.""" |
| 24 | + return self.env["res.partner"].create( |
| 25 | + { |
| 26 | + "name": name, |
| 27 | + "email": email, |
| 28 | + } |
| 29 | + ) |
| 30 | + |
| 31 | + def create_product(self, name="Test Product", price=100.0): |
| 32 | + """Create a test product.""" |
| 33 | + return self.env["product.product"].create( |
| 34 | + { |
| 35 | + "name": name, |
| 36 | + "list_price": price, |
| 37 | + "type": "service", |
| 38 | + } |
| 39 | + ) |
| 40 | + |
| 41 | + def create_invoice(self, partner, product, amount=100.0, move_type="out_invoice"): |
| 42 | + """Create a customer invoice. |
| 43 | +
|
| 44 | + Args: |
| 45 | + partner: res.partner record |
| 46 | + product: product.product record |
| 47 | + amount: invoice amount |
| 48 | + move_type: 'out_invoice', 'out_refund', 'in_invoice', etc. |
| 49 | +
|
| 50 | + Returns: |
| 51 | + account.move record |
| 52 | + """ |
| 53 | + invoice = self.env["account.move"].create( |
| 54 | + { |
| 55 | + "partner_id": partner.id, |
| 56 | + "move_type": move_type, |
| 57 | + "invoice_line_ids": [ |
| 58 | + ( |
| 59 | + 0, |
| 60 | + 0, |
| 61 | + { |
| 62 | + "product_id": product.id, |
| 63 | + "name": product.name, |
| 64 | + "quantity": 1, |
| 65 | + "price_unit": amount, |
| 66 | + }, |
| 67 | + ) |
| 68 | + ], |
| 69 | + } |
| 70 | + ) |
| 71 | + return invoice |
| 72 | + |
| 73 | + def create_bank_journal(self, name="Bank", code="BNK1"): |
| 74 | + """Create a bank journal for payments.""" |
| 75 | + return self.env["account.journal"].create( |
| 76 | + { |
| 77 | + "name": name, |
| 78 | + "code": code, |
| 79 | + "type": "bank", |
| 80 | + } |
| 81 | + ) |
| 82 | + |
| 83 | + def reconcile_invoice_with_payment(self, invoice, journal=None): |
| 84 | + """Reconcile invoice with a payment using account.payment.register wizard. |
| 85 | +
|
| 86 | + Args: |
| 87 | + invoice: account.move record (must be posted) |
| 88 | + journal: account.journal record (optional, uses first bank journal if not provided) |
| 89 | +
|
| 90 | + Returns: |
| 91 | + dict: Result from _reconcile_payments |
| 92 | + """ |
| 93 | + if journal is None: |
| 94 | + journal = self.env["account.journal"].search( |
| 95 | + [("type", "=", "bank")], limit=1 |
| 96 | + ) |
| 97 | + if not journal: |
| 98 | + journal = self.create_bank_journal() |
| 99 | + |
| 100 | + # Create payment register wizard |
| 101 | + ctx = { |
| 102 | + "active_model": "account.move", |
| 103 | + "active_ids": invoice.ids, |
| 104 | + } |
| 105 | + |
| 106 | + payment_register = ( |
| 107 | + self.env["account.payment.register"] |
| 108 | + .with_context(**ctx) |
| 109 | + .create( |
| 110 | + { |
| 111 | + "journal_id": journal.id, |
| 112 | + } |
| 113 | + ) |
| 114 | + ) |
| 115 | + |
| 116 | + # Process the payment (this calls _reconcile_payments internally) |
| 117 | + result = payment_register.action_create_payments() |
| 118 | + |
| 119 | + return result |
| 120 | + |
| 121 | + def get_sent_mail_count(self, invoice): |
| 122 | + """Get count of sent mails for an invoice. |
| 123 | +
|
| 124 | + Args: |
| 125 | + invoice: account.move record |
| 126 | +
|
| 127 | + Returns: |
| 128 | + int: Number of mail.mail records for this invoice |
| 129 | + """ |
| 130 | + return self.env["mail.mail"].search_count( |
| 131 | + [ |
| 132 | + ("model", "=", "account.move"), |
| 133 | + ("res_id", "=", invoice.id), |
| 134 | + ] |
| 135 | + ) |
| 136 | + |
| 137 | + def create_payment_acquirer(self, provider="paypal"): |
| 138 | + """Create a payment acquirer for testing online payments. |
| 139 | +
|
| 140 | + Args: |
| 141 | + provider: Provider name (paypal, stripe, etc.) |
| 142 | +
|
| 143 | + Returns: |
| 144 | + payment.acquirer record |
| 145 | + """ |
| 146 | + vals = { |
| 147 | + "name": f"Test {provider.title()}", |
| 148 | + "provider": provider, |
| 149 | + "state": "test", |
| 150 | + } |
| 151 | + |
| 152 | + # Add provider-specific required fields |
| 153 | + if provider == "paypal": |
| 154 | + vals[ "paypal_email_account"] = "[email protected]" |
| 155 | + |
| 156 | + return self.env["payment.acquirer"].create(vals) |
0 commit comments