117 lines
4.8 KiB
Python
117 lines
4.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
from odoo import models, fields, api, _
|
|
from odoo.exceptions import UserError
|
|
|
|
class AnnualAddendum(models.Model):
|
|
_name = "odx.annual.addendum"
|
|
_description = "Annual Purchase Addendum"
|
|
_inherit = ['mail.thread', 'mail.activity.mixin']
|
|
_order = "create_date desc"
|
|
|
|
name = fields.Char(string="Reference", default=lambda self: self.env['ir.sequence'].next_by_code('odx.annual.addendum'), readonly=True, copy=False)
|
|
agreement_id = fields.Many2one('purchase.requisition', string="Agreement", required=True)
|
|
annual_request_id = fields.Many2one('odx.annual.request', string="Annual Request", index=True, ondelete='cascade')
|
|
vendor_id = fields.Many2one(related='annual_request_id.vendor_id', store=True, readonly=True)
|
|
|
|
department_id = fields.Many2one('hr.department', string="Department")
|
|
purpose = fields.Char(string="Purpose")
|
|
note = fields.Text(string="Notes")
|
|
state = fields.Selection([
|
|
('draft','Draft'),
|
|
('ssd','Shared Services Director'),
|
|
('gm','General Manager'),
|
|
('approved','Approved'),
|
|
('rejected','Rejected'),
|
|
('cancel','Cancelled'),
|
|
], default='draft', tracking=True)
|
|
line_ids = fields.One2many('odx.annual.addendum.line', 'addendum_id', string="Lines")
|
|
ssd_approve = fields.Boolean(string="SSD Approve", default=False)
|
|
seo_approve = fields.Boolean(string="SEO Approve", default=False)
|
|
recommendation = fields.Boolean(string="Recommendation", default=False)
|
|
|
|
def _check_lines(self):
|
|
for rec in self:
|
|
if not rec.line_ids:
|
|
raise UserError(_("Please add at least one line."))
|
|
|
|
def action_send(self):
|
|
self._check_lines()
|
|
self.write({'state':'ssd'})
|
|
|
|
def action_ssd_approve(self):
|
|
self.write({'state':'gm'})
|
|
|
|
def action_ssd_reject(self, reason=False):
|
|
self.message_post(body=_("Rejected by SSD: %s") % (reason or ''))
|
|
self.write({'state':'rejected'})
|
|
|
|
def action_gm_approve(self):
|
|
for rec in self:
|
|
for line in rec.line_ids:
|
|
self.env['purchase.requisition.line'].create({
|
|
'requisition_id': rec.agreement_id.id,
|
|
'product_id': line.product_id.id,
|
|
'product_uom_id': line.uom_id.id,
|
|
'product_qty': line.quantity,
|
|
'price_unit': line.price_unit or 0.0,
|
|
'name': line.description or line.product_id.display_name,
|
|
})
|
|
self.write({'state':'approved'})
|
|
self.message_post(body=_("Addendum lines added to Agreement."))
|
|
|
|
def action_gm_reject(self, reason=False):
|
|
self.message_post(body=_("Rejected by GM: %s") % (reason or ''))
|
|
self.write({'state':'rejected'})
|
|
|
|
def action_cancel(self):
|
|
self.write({'state': 'cancel'})
|
|
|
|
class AnnualAddendumLine(models.Model):
|
|
_name = "odx.annual.addendum.line"
|
|
_description = "Annual Purchase Addendum Line"
|
|
|
|
addendum_id = fields.Many2one('odx.annual.addendum', string="Addendum", required=True, ondelete='cascade')
|
|
product_id = fields.Many2one('product.product', string="Product", required=True, domain=[('purchase_ok','=',True)])
|
|
description = fields.Char(string="Description")
|
|
quantity = fields.Float(string="Quantity", default=1.0)
|
|
uom_id = fields.Many2one('uom.uom', string="UoM", related='product_id.uom_po_id')
|
|
price_unit = fields.Monetary(string="Unit Price", currency_field='currency_id')
|
|
currency_id = fields.Many2one(
|
|
'res.currency',
|
|
related='addendum_id.agreement_id.company_id.currency_id',
|
|
readonly=True, store=True
|
|
)
|
|
addendum_company_id = fields.Many2one(
|
|
'res.company',
|
|
related='addendum_id.agreement_id.company_id',
|
|
readonly=True, store=True
|
|
)
|
|
taxes_id = fields.Many2many(
|
|
'account.tax', string="Taxes",
|
|
domain="[('type_tax_use','=','purchase'), ('company_id','=', addendum_company_id)]"
|
|
)
|
|
price_subtotal = fields.Monetary(
|
|
string="Subtotal", currency_field='currency_id',
|
|
compute='_compute_amounts', store=True, readonly=True
|
|
)
|
|
|
|
@api.depends('quantity', 'price_unit', 'taxes_id', 'currency_id', 'product_id')
|
|
def _compute_amounts(self):
|
|
for rec in self:
|
|
qty = rec.quantity or 0.0
|
|
price_unit = rec.price_unit or 0.0
|
|
currency = rec.currency_id
|
|
|
|
taxes = rec.taxes_id
|
|
if rec.addendum_company_id:
|
|
taxes = taxes.filtered(lambda t: t.company_id == rec.addendum_company_id)
|
|
|
|
res = taxes.compute_all(
|
|
price_unit,
|
|
currency=currency,
|
|
quantity=qty,
|
|
product=rec.product_id,
|
|
partner=None
|
|
)
|
|
rec.price_subtotal = res['total_excluded']
|