odex30_standard/odex30_base/branch/models/purchase_order.py

108 lines
3.3 KiB
Python

# Part of BrowseInfo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError
class PurchaseOrder(models.Model):
_inherit = 'purchase.order'
branch_id = fields.Many2one('res.branch', string='Branch')
@api.model
def default_get(self,fields):
res = super(PurchaseOrder, self).default_get(fields)
branch_id = picking_type_id = False
if self.env.user.branch_id:
branch_id = self.env.user.branch_id.id
if branch_id:
branched_warehouse = self.env['stock.warehouse'].search([('branch_id','=',branch_id)])
if branched_warehouse:
picking_type_id = branched_warehouse[0].in_type_id.id
else:
picking_type_id = self._default_picking_type().id
res.update({
'branch_id' : branch_id,
'picking_type_id' : picking_type_id
})
return res
def _prepare_picking(self):
res = super(PurchaseOrder, self)._prepare_picking()
branch_id = False
if self.branch_id:
branch_id = self.branch_id.id
elif self.env.user.branch_id:
branch_id = self.env.user.branch_id.id
res.update({
'branch_id' : branch_id
})
return res
def _prepare_invoice(self):
result = super(PurchaseOrder, self)._prepare_invoice()
branch_id = False
if self.branch_id:
branch_id = self.branch_id.id
elif self.env.user.branch_id:
branch_id = self.env.user.branch_id.id
result.update({
'branch_id' : branch_id
})
return result
@api.onchange('branch_id')
def _onchange_branch_id(self):
if self.branch_id:
user_branch = self.env.user.sudo().branch_id
if user_branch and user_branch.id != self.branch_id.id:
raise ValidationError("Please select active branch only. Other may create the Multi branch issue. \n\ne.g: If you wish to add other branch then Switch branch from the header and set that.")
class purchase_order(models.Model):
_inherit = 'purchase.order.line'
branch_id = fields.Many2one('res.branch', string="Branch", related="order_id.branch_id", store=True)
def _prepare_account_move_line(self, move=False):
result = super(purchase_order, self)._prepare_account_move_line(move)
result.update({
'branch_id' : self.order_id.branch_id.id,
})
return result
@api.model
def default_get(self, default_fields):
res = super(purchase_order, self).default_get(default_fields)
branch_id = False
if self._context.get('branch_id'):
branch_id = self._context.get('branch_id')
elif self.env.user.branch_id:
branch_id = self.env.user.branch_id.id
res.update({'branch_id' : branch_id})
return res
def _prepare_stock_moves(self, picking):
result = super(purchase_order, self)._prepare_stock_moves(picking)
branch_id = False
if self.branch_id:
branch_id = self.branch_id.id
elif self.env.user.branch_id:
branch_id = self.env.user.branch_id.id
for res in result:
res.update({'branch_id' : branch_id})
return result