odoo18/custom_addons/grn_purchase/models/grn_purchase.py

58 lines
2.3 KiB
Python

# -*- coding: utf-8 -*-
from odoo import _,api, fields, models
class StockPicking(models.Model):
_inherit = 'stock.picking'
def _action_done(self):
res = super(StockPicking, self)._action_done()
for rec in self:
if rec.picking_type_id.code == 'incoming':
grn = self.env['grn'].search([('picking_id', '=', rec.id)])
if not grn and rec.purchase_id:
grn_data = {
'vendor_id':rec.partner_id.id,
'date':fields.Datetime.now(),
'location_id':rec.location_dest_id.id,
'note':rec.purchase_id.name,
'grn_line_ids':[]
}
for line in rec.move_ids.filtered(lambda x:x.product_id.type == 'consu'):
grn_data['grn_line_ids'].append((0,0,{
'product_id':line.product_id.id,
'quantity':line.quantity,
'product_uom_id':line.product_id.uom_id.id,
'price':line.price_unit,
}))
new_grn = self.env['grn'].create(grn_data)
new_grn.name = self.env['ir.sequence'].next_by_code('grn') or _('New')
new_grn.picking_id = rec
new_grn.state = 'done'
rec.purchase_id.grn_ids |= new_grn
if grn and grn.state != 'done':
for line in rec.move_ids:
if line.quantity != grn.grn_line_ids.filtered(lambda x:x.product_id == line.product_id).quantity:
grn.grn_line_ids.filtered(lambda x: x.product_id == line.product_id).quantity = line.quantity
grn.state = 'done'
return res
class PurchaseOrder(models.Model):
_inherit = "purchase.order"
grn_ids = fields.Many2many('grn', string="GRN")
grn_count = fields.Integer('GRN Count', compute='_compute_grn_count')
@api.depends('grn_ids')
def _compute_grn_count(self):
self.grn_count = len(self.grn_ids)
def action_show_grn_ids(self):
action = self.env['ir.actions.actions']._for_xml_id('grn.grn_action')
action['domain'] = [('id', 'in', self.grn_ids.ids)]
action['context'] = {}
return action