2024-12-14 06:53:23 -05:00
|
|
|
from odoo import models, fields, api
|
|
|
|
|
|
2025-01-02 08:06:56 -05:00
|
|
|
|
2024-12-14 06:53:23 -05:00
|
|
|
class SaleOrderLine(models.Model):
|
2025-01-02 08:06:56 -05:00
|
|
|
"""
|
|
|
|
|
Extends sale order line to add itch cycle tracking.
|
|
|
|
|
"""
|
2024-12-14 06:53:23 -05:00
|
|
|
_inherit = 'sale.order.line'
|
|
|
|
|
|
|
|
|
|
itch_cycle_id = fields.Many2one(
|
|
|
|
|
comodel_name='itch.cycle.product.partner',
|
2025-01-02 08:06:56 -05:00
|
|
|
string="Cycle")
|
2024-12-14 06:53:23 -05:00
|
|
|
|
2024-12-17 15:40:14 -05:00
|
|
|
stock_move_ids = fields.One2many(
|
|
|
|
|
comodel_name='stock.move',
|
|
|
|
|
inverse_name='sale_line_id',
|
2025-01-02 08:06:56 -05:00
|
|
|
string="Movements")
|
2024-12-17 15:40:14 -05:00
|
|
|
|
|
|
|
|
sale_date = fields.Datetime(
|
2025-01-02 08:06:56 -05:00
|
|
|
string="Date",
|
2024-12-17 15:40:14 -05:00
|
|
|
related='order_id.date_order',
|
2025-01-02 08:06:56 -05:00
|
|
|
store=True)
|
2024-12-17 15:40:14 -05:00
|
|
|
|
2024-12-17 12:51:45 -05:00
|
|
|
@api.model_create_multi
|
|
|
|
|
def create(self, vals_list):
|
2025-01-02 08:06:56 -05:00
|
|
|
"""
|
|
|
|
|
Create sale order lines and associate them with their itch cycle.
|
|
|
|
|
|
|
|
|
|
If the product is cycle tracked and no existing cycle exists for the
|
|
|
|
|
product/partner combination, a new cycle is created.
|
|
|
|
|
"""
|
2024-12-17 12:51:45 -05:00
|
|
|
lines = super().create(vals_list)
|
|
|
|
|
|
|
|
|
|
for line in lines:
|
2025-01-02 08:06:56 -05:00
|
|
|
if (line.product_id and line.order_id and
|
|
|
|
|
line.product_id.categ_id.is_cycle_tracked):
|
2024-12-17 12:51:45 -05:00
|
|
|
partner_id = line.order_id.partner_id.id
|
|
|
|
|
itch_cycle = self.env['itch.cycle.product.partner'].search([
|
|
|
|
|
('partner_id', '=', partner_id),
|
|
|
|
|
('product_id', '=', line.product_id.id)
|
|
|
|
|
], limit=1)
|
|
|
|
|
if not itch_cycle:
|
2025-01-02 08:06:56 -05:00
|
|
|
cycle_vals = {
|
2024-12-17 12:51:45 -05:00
|
|
|
'partner_id': partner_id,
|
|
|
|
|
'product_id': line.product_id.id,
|
2025-01-02 08:06:56 -05:00
|
|
|
}
|
|
|
|
|
itch_cycle = self.env['itch.cycle.product.partner'].create(
|
|
|
|
|
cycle_vals)
|
2024-12-17 12:51:45 -05:00
|
|
|
line.itch_cycle_id = itch_cycle.id
|
|
|
|
|
|
2025-01-02 08:06:56 -05:00
|
|
|
return lines
|