vendor_batch_payment_merge/doc/technical_guide.md
2025-09-20 12:02:42 +07:00

344 lines
10 KiB
Markdown

# Vendor Batch Payment Merge - Technical Guide
## Overview
This document provides technical details about the Vendor Batch Payment Merge addon implementation. It covers the models, views, and business logic used to extend Odoo's batch payment functionality.
## Architecture
### Models
#### account.batch.payment (Inherited)
Extended to include direct payment lines functionality:
- **Field**: `direct_payment_line_ids` (One2many)
- Links to `account.batch.payment.line` records
- Allows creation of payment templates directly in the batch form
- **Method**: `generate_payments_from_lines()`
- Creates actual payment records from direct payment lines
- Posts the payments
- Adds them to the batch
- Links each line to its generated payment
#### account.batch.payment.line (New)
Represents a direct payment line template:
- **Fields**:
- `batch_payment_id` (Many2one): Link to the parent batch payment
- `partner_id` (Many2one): The partner (vendor/customer)
- `amount` (Monetary): Payment amount
- `currency_id` (Many2one): Payment currency (related to batch)
- `expense_account_id` (Many2one): Expense/Income account for journal entry
- `memo` (Char): Payment memo
- `date` (Date): Payment date
- `payment_id` (Many2one): Link to generated payment (readonly)
- **Methods**:
- `_onchange_partner_id()`: Placeholder for future enhancements
#### account.payment (Inherited)
Extended to support expense account functionality:
- **Field**: `expense_account_id` (Many2one)
- Specifies the account to use in journal entries instead of default payable/receivable
- **Method**: `_compute_destination_account_id()`
- Overrides the default destination account computation
- Uses the expense account if specified
- **Method**: `_prepare_move_line_default_vals()`
- Overrides journal entry line creation
- Uses the expense account in the counterpart line
### Views
#### account_batch_payment_views.xml
- **Inherited View**: `account_batch_payment.view_batch_payment_form`
- **Additions**:
- New "Direct Payment Lines" tab with editable tree view
- "Generate Payments" button in the header
- Restored smart partner filtering based on batch type:
- For outbound payments: Shows partners with supplier_rank > 0
- For inbound payments: Shows partners with customer_rank > 0
#### account_payment_views.xml
- **Inherited View**: `account.view_account_payment_form`
- **Additions**:
- Expense account field in the main form
- Visibility condition to show only for relevant payment types
### Data
#### account_payment_method_data.xml
- Creates two payment methods with code `direct_batch`:
- One for inbound payments
- One for outbound payments
## Business Logic
### Payment Generation Flow
1. User creates direct payment lines in batch payment form
2. User clicks "Generate Payments"
3. System validates that all lines have a partner selected
4. For each line:
- Determine payment type and partner type based on batch type
- Create payment record with specified details
- Post the payment
- Link the line to the generated payment
5. Add all generated payments to the batch
### Journal Entry Creation
When a payment with an expense account is posted:
1. Standard liquidity line is created (bank account)
2. Instead of using the partner's payable/receivable account, the expense account is used
3. This allows for more flexible accounting without affecting partner balances
## Implementation Details
### Field Domains
- **Partner Fields**:
- Restored smart filtering based on batch type:
- For outbound payments: `supplier_rank > 0`
- For inbound payments: `customer_rank > 0`
- Added `no_create` option to prevent creating new partners from the payment line
- **Expense Account Fields**:
- `account_type not in ('asset_receivable', 'liability_payable')`
### Payment Method Handling
The addon creates payment methods with code `direct_batch` and now properly handles payment method lines:
- Searches for existing payment method lines with the correct code and journal
- Automatically creates payment method lines if they don't exist
- Properly links payments to payment method lines
### Error Handling
- Validates that a direct batch payment method line exists for the journal or can be created
- Validates that all payment lines have a partner selected
- Handles both inbound and outbound payment types appropriately
- Links direct payment lines to generated payments for traceability
## Testing
### Test Cases
1. **test_create_vendor_batch_payment_with_direct_lines**
- Creates a vendor batch payment with direct lines
- Verifies payment generation and linking
2. **test_create_customer_batch_payment_with_direct_lines**
- Creates a customer batch payment with direct lines
- Verifies payment generation and linking
3. **test_expense_account_in_journal_entry**
- Verifies that the expense account is used in journal entries
- Checks journal entry line balances
4. **test_direct_batch_payment_method**
- Verifies that the payment methods are created
- Checks for payment method lines (note: existence not asserted due to Odoo's automatic creation)
### Test Data
- Demo vendor and customer partners
- Demo expense and income accounts
- Bank journal (created if not exists)
## Security
### Access Rights
- Standard Odoo accounting security model applies
- New model `account.batch.payment.line` has full access rights
- No additional security groups required
### Record Rules
- Inherits standard Odoo record rules
- No additional record rules implemented
## Extensibility
### Hooks for Customization
1. **account.batch.payment**
- `generate_payments_from_lines()` can be extended
- Additional fields can be added to direct payment lines
2. **account.payment**
- `_compute_destination_account_id()` can be further customized
- `_prepare_move_line_default_vals()` can be extended for additional modifications
### Integration Points
1. **Payment Methods**: Additional payment methods can be added
2. **Account Types**: Additional account types can be supported
3. **Journal Types**: Extension to other journal types is possible
## Performance Considerations
### Batch Operations
- Payment generation is done in batch for efficiency
- Database operations are minimized through proper ORM usage
- Large batch payments should be handled efficiently
### Computed Fields
- Properly decorated computed fields with dependencies
- Efficient search and filter operations
- Minimal impact on form loading times
## Known Limitations
1. **Payment Method Lines**:
- Payment method lines are created automatically by Odoo
- Manual creation may be needed in some configurations
2. **Multi-Currency**:
- Standard Odoo multi-currency handling applies
- No additional currency-specific logic implemented
3. **Workflow Integration**:
- Standard Odoo payment workflow is used
- No additional workflow states or transitions
## Dependencies
### Odoo Modules
- `account`: Core accounting functionality
- `account_batch_payment`: Base batch payment functionality
### Python Libraries
- Standard Odoo Python environment
- No additional Python dependencies required
## Installation
### File Structure
```
vendor_batch_payment_merge/
├── __init__.py
├── __manifest__.py
├── data/
│ ├── __init__.py
│ └── account_payment_method_data.xml
├── demo/
│ └── demo_data.xml
├── doc/
│ ├── __init__.py
│ ├── user_guide.md
│ └── technical_guide.md
├── models/
│ ├── __init__.py
│ ├── account_batch_payment.py
│ └── account_payment.py
├── security/
│ └── ir.model.access.csv
├── tests/
│ ├── __init__.py
│ └── test_vendor_batch_payment_merge.py
└── views/
├── __init__.py
├── account_batch_payment_views.xml
└── account_payment_views.xml
```
### Installation Process
1. Place addon in Odoo addons directory
2. Update Apps list
3. Install "Vendor Batch Payment Merge" addon
4. No additional configuration required
## Upgrade Considerations
### Version Compatibility
- Designed for Odoo 17
- May require adjustments for other versions
- Follows standard Odoo inheritance patterns
### Data Migration
- No special migration needed for new installations
- Existing batch payments unaffected
- Direct payment lines are new records with no impact on existing data
## Recent Fixes
### Partner Field Issues
- Removed `required=True` constraint on partner_id field to avoid constraint issues
- Fixed incorrect reference to `property_account_expense` on partner model
- Added validation in payment generation to ensure all lines have partners
- Restored smart partner filtering based on batch type
### Payment Method Line Issues
- Fixed search for payment method lines to properly look for `code` field
- Added automatic creation of payment method lines when they don't exist
- Improved error handling for missing payment methods
### Domain Context Issues
- Fixed domain context for proper partner filtering in payment lines
- Used `parent.batch_type` instead of `context.get('default_batch_type')` for accurate filtering
### View Improvements
- Restored smart partner filtering based on batch type
- Improved error handling for missing partners
- Added `no_create` option to prevent accidental partner creation
## Troubleshooting
### Common Issues
1. **Payment Method Not Found**
- Verify addon installation
- Check payment method data
- Ensure journal configuration
- The addon now automatically creates payment method lines if they don't exist
2. **Missing Partner Error**
- Ensure all payment lines have a partner selected
- Check that the partner field is not empty for any line
3. **Journal Entry Account Issues**
- Verify expense account specification
- Check payment posting status
- Confirm account types
4. **View Errors**
- Verify XML view definitions
- Check field references
- Confirm model inheritance
### Debugging Tips
1. Enable debug mode in Odoo
2. Check server logs for error messages
3. Use Odoo's developer tools to inspect views
4. Verify model and field definitions
5. Test with minimal data sets
## Support
For technical issues or questions, consult the Odoo community or contact the addon developer.