add new feature to automate filling purchase order line and filtering out subcontract move that have return movement

This commit is contained in:
admin.suherdy 2025-11-24 13:27:57 +07:00
parent c92a3b0291
commit f3d01a7de5
10 changed files with 214 additions and 2 deletions

63
CHANGELOG.md Normal file
View File

@ -0,0 +1,63 @@
# Changelog - Subcontracting Inventory Move First Module
## Version 18.0.1.1.0 - Enhancement
### New Features
#### 1. Automatic Purchase Order Line Creation
When a user selects a subcontracting move to link to a purchase order, the system now automatically creates a purchase order line with:
- Product from the subcontracting move
- Quantity from the move's demand quantity (product_uom_qty)
- Unit of measure from the move
- Price unit from the product's standard price
- Planned date from the move date
- Product description as the line name
**Implementation:**
- Modified `link_selected_subcontracting_move()` method in `purchase_order.py`
- Creates `purchase.order.line` record automatically when linking moves
#### 2. Filter Out Moves with Returns
The module now filters out subcontracting moves that have return movements, ensuring only valid subcontracting moves (without returns) can be linked to purchase orders.
**Implementation:**
- Added `has_return` computed field to `stock.move` model
- Field checks if any return moves reference the current move via `origin_returned_move_id`
- Updated domain in `action_link_subcontracting_moves()` to exclude moves where `has_return = True`
- Added `has_return` field to tree views for visibility
### Modified Files
1. **models/purchase_order.py**
- Enhanced `link_selected_subcontracting_move()` to create purchase order lines
- Updated `action_link_subcontracting_moves()` domain to filter out moves with returns
2. **models/stock_move.py**
- Added `has_return` computed field
- Added `_compute_has_return()` method to detect return movements
3. **views/stock_move_link_views.xml**
- Added `has_return` field to link tree view (hidden but available for filtering)
4. **views/stock_move_views.xml**
- Added `has_return` field to stock move tree view (optional/hide by default)
5. **README.md**
- Updated features list
- Updated usage instructions
- Updated technical details
### Benefits
- **Improved Efficiency**: No need to manually create purchase order lines after linking moves
- **Data Accuracy**: Product and quantity are automatically copied from the subcontracting move
- **Better Data Integrity**: Prevents linking of invalid moves (those with returns)
- **Cleaner Workflow**: Only valid subcontracting moves are shown in the selection list
### Upgrade Notes
After upgrading to this version:
1. Update the module in Odoo
2. The `has_return` field will be computed for all existing stock moves
3. Existing linked moves are not affected
4. New links will automatically create purchase order lines

View File

@ -6,6 +6,8 @@ This module extends the standard Odoo subcontracting workflow to allow creating
## Features
- Create "Resupply Subcontract" inventory moves directly in the inventory module
- Link these moves to purchase orders after creation
- Automatically create purchase order lines with product and demand quantity when linking moves
- Filter out subcontracting moves with returns - only valid moves (without returns) can be linked
- Smart button on purchase orders to view linked inventory moves
- Maintain data consistency between moves and purchase orders
- Update source document field in inventory moves and related stock pickings to show purchase order number
@ -21,9 +23,10 @@ This module extends the standard Odoo subcontracting workflow to allow creating
### Linking to Purchase Orders
1. Go to the purchase order you want to link
2. Click the "Link Subcontracting Moves" button
3. Select the appropriate inventory moves from the list
3. Select the appropriate inventory moves from the list (only moves without returns will be shown)
4. Click the "Link to Purchase Order" button at the top of the list view to confirm linking
5. The moves will be linked to the purchase order and source document fields updated
6. Purchase order lines will be automatically created with the product and demand quantity from the selected moves
### Viewing Linked Moves
1. On any purchase order form, the smart button "Subcontracting Moves" will show the count of linked moves
@ -32,9 +35,12 @@ This module extends the standard Odoo subcontracting workflow to allow creating
## Technical Details
- The module adds a `purchase_order_id` field to `stock.move` to track links to purchase orders
- The module adds a `subcontracting_move_ids` field to `purchase.order` to track linked moves
- The module adds a `has_return` computed field to `stock.move` to identify moves with return movements
- Business logic ensures data consistency when linking/unlinking moves and orders
- When linking moves to purchase orders, the source document field is updated to show the purchase order number
- When linking moves to purchase orders, the source document field in related stock pickings is also updated
- When linking moves to purchase orders, purchase order lines are automatically created with product and quantity from the move
- Only subcontracting moves without returns can be linked to purchase orders (moves with returns are filtered out)
- Proper handling of purchase order cancellation to unlink moves without errors
## Compatibility

110
UPGRADE_GUIDE.md Normal file
View File

@ -0,0 +1,110 @@
# Upgrade Guide - Version 18.0.1.1.0
## Overview
This upgrade adds two major enhancements to the Subcontracting Inventory Move First module:
1. Automatic creation of purchase order lines when linking subcontracting moves
2. Filtering out subcontracting moves that have returns
## Installation Steps
### 1. Update Module Files
Ensure all module files are updated to version 18.0.1.1.0
### 2. Upgrade Module in Odoo
```bash
# Option 1: Via Odoo UI
1. Go to Apps menu
2. Remove "Apps" filter
3. Search for "Subcontracting Inventory Move First"
4. Click "Upgrade" button
# Option 2: Via Command Line
./odoo-bin -u subcontracting_inventory_move_first -d your_database_name
```
### 3. Verify Installation
After upgrade, verify:
- The `has_return` field is computed for all stock moves
- The link dialog only shows moves without returns
- Linking a move creates a purchase order line automatically
## What's New
### Feature 1: Auto-Create Purchase Order Lines
**Before:** When linking a subcontracting move to a purchase order, you had to manually create the purchase order line.
**After:** The system automatically creates a purchase order line with:
- Product from the move
- Quantity from the move's demand quantity
- Unit of measure from the move
- Price from product's standard price
**Example:**
```
Subcontracting Move:
- Product: Component A
- Quantity: 100 Units
After Linking → Purchase Order Line Created:
- Product: Component A
- Quantity: 100
- UoM: Units
- Price: $10.00 (from product standard price)
```
### Feature 2: Filter Moves with Returns
**Before:** All subcontracting moves were shown in the link dialog, including those with returns.
**After:** Only valid moves (without returns) are shown in the link dialog.
**How it works:**
- The system checks if any return move references the original move
- Moves with returns are automatically filtered out
- The `has_return` field is available in tree views for manual filtering
## Testing the Upgrade
### Test Case 1: Auto-Create Purchase Order Line
1. Create a subcontracting move with:
- Product: Test Product
- Quantity: 50
2. Create a purchase order
3. Click "Link Subcontracting Moves"
4. Select the move and click "Link to Purchase Order"
5. **Expected Result:** A purchase order line is automatically created with the product and quantity
### Test Case 2: Filter Moves with Returns
1. Create a subcontracting move and mark it as done
2. Create a return for this move
3. Create a new purchase order
4. Click "Link Subcontracting Moves"
5. **Expected Result:** The move with return should NOT appear in the list
## Rollback Instructions
If you need to rollback:
1. Restore the previous version files
2. Downgrade the module:
```bash
./odoo-bin -u subcontracting_inventory_move_first -d your_database_name
```
## Support
For issues or questions:
- Check the CHANGELOG.md for detailed changes
- Review the README.md for usage instructions
- Contact: Suherdy Yacob
## Database Changes
### New Fields Added:
- `stock.move.has_return` (Boolean, Computed, Stored)
### Modified Methods:
- `purchase.order.link_selected_subcontracting_move()` - Now creates PO lines
- `purchase.order.action_link_subcontracting_moves()` - Updated domain filter
- `stock.move._compute_has_return()` - New compute method
### No Data Migration Required
This upgrade does not require data migration. All changes are backward compatible.

View File

@ -1,6 +1,6 @@
{
'name': 'Subcontracting Inventory Move First',
'version': '18.0.1.0.0',
'version': '18.0.1.1.0',
'category': 'Manufacturing/Manufacturing',
'summary': 'Allow creating inventory moves first and then linking to purchase orders in subcontracting',
'author' : "Suherdy Yacob",

View File

@ -40,6 +40,7 @@ class PurchaseOrder(models.Model):
('location_dest_id.name', '=', 'Subcontracting Location'),
('purchase_order_id', '=', False),
('state', 'in', ['done']),
('has_return', '=', False), # Only show moves without returns
],
'target': 'new',
'context': {
@ -85,6 +86,17 @@ class PurchaseOrder(models.Model):
# Note: stock.picking doesn't have a purchase_order_id field, so we can't link it directly
})
# Create purchase order line with product and demand quantity from the move
self.env['purchase.order.line'].create({
'order_id': self.id,
'product_id': move.product_id.id,
'product_qty': move.product_uom_qty,
'product_uom': move.product_uom.id,
'price_unit': move.product_id.standard_price or 0.0,
'date_planned': move.date or fields.Datetime.now(),
'name': move.product_id.display_name or move.name,
})
# Also add to the Many2many field
current_moves = self.subcontracting_move_ids.ids
current_moves.append(move.id)

View File

@ -20,11 +20,30 @@ class StockMove(models.Model):
string='Linked Purchase Orders Count',
compute='_compute_linked_purchase_order_count'
)
# Computed field to check if move has returns
has_return = fields.Boolean(
string='Has Return',
compute='_compute_has_return',
store=True,
help='Indicates if this move has any return movements'
)
@api.depends('purchase_order_id')
def _compute_linked_purchase_order_count(self):
for move in self:
move.linked_purchase_order_count = 1 if move.purchase_order_id else 0
@api.depends('origin_returned_move_id', 'move_dest_ids')
def _compute_has_return(self):
"""Check if this move has any return movements"""
for move in self:
# Check if this move has been returned
# A return is identified by moves that reference this move as origin_returned_move_id
returned_moves = self.env['stock.move'].search([
('origin_returned_move_id', '=', move.id)
], limit=1)
move.has_return = bool(returned_moves)
def action_view_linked_purchase_order(self):
"""Action to view the linked purchase order from the inventory move"""

View File

@ -39,6 +39,7 @@ action = {'type': 'ir.actions.act_window_close'}
<field name="state"/>
<field name="date"/>
<field name="location_dest_id"/>
<field name="has_return" column_invisible="1"/>
</list>
</field>
</record>

View File

@ -26,6 +26,7 @@
<field name="arch" type="xml">
<xpath expr="//field[@name='product_uom_qty']" position="after">
<field name="purchase_order_id" invisible="not is_subcontract"/>
<field name="has_return" optional="hide"/>
</xpath>
</field>
</record>