# -*- coding: utf-8 -*- ################################################################################ # # Cybrosys Technologies Pvt. Ltd. # # Copyright (C) 2023-TODAY Cybrosys Technologies(). # Author: Ammu Raj (odoo@cybrosys.com) # # You can modify it under the terms of the GNU AFFERO # GENERAL PUBLIC LICENSE (AGPL v3), Version 3. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU AFFERO GENERAL PUBLIC LICENSE (AGPL v3) for more details. # # You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE # (AGPL v3) along with this program. # If not, see . # ################################################################################ from odoo import api, fields, models, _ class VendorRFQ(models.Model): """Vendor RFQ model""" _name = 'vendor.rfq' _inherit = ['mail.thread', 'mail.activity.mixin'] _description = 'Vendor RFQ' name = fields.Char(string='RFQ Reference', required=True, index=True, copy=False, default=lambda x: _('New'), help="Name of the Vendor RFQ") product_id = fields.Many2one('product.product', string='Product', help="Select the required product for quote") quantity = fields.Float(string="Quantity", help="Number of product quantity") uom_id = fields.Many2one('uom.uom', string='UoM', help="Unit of measure") estimated_quote = fields.Monetary(string="Estimated Quote", currency_field='currency_id', help="Estimated Quote Price") currency_id = fields.Many2one('res.currency', string='Currency', required=True, default=lambda self: self.env.user.company_id.currency_id, help="Current currency") notes = fields.Html(string='Notes', help="Additional notes") estimated_delivery_date = fields.Date(string="Delivery date", help="Vendor's delivery date") quote_date = fields.Datetime(default=fields.Datetime.now(), readonly=1, string='Quote Date', help="Date of the quote") closing_date = fields.Date(string="Closing date", help="Quotation closing date") vendor_ids = fields.Many2many('res.partner', string="Vendors", domain="[('is_registered', '=', True)]", help="Vendors you want to send quotations") vendor_quote_history_ids = fields.One2many('vendor.quote.history', 'quote_id', string="Vendors", help="The quote history of the" "vendors") user_id = fields.Many2one('res.users', string="Responsible", default=lambda self: self.env.user, help="Current user") approved_vendor_id = fields.Many2one('res.partner', string="Approved Vendors", help="The approved vendors") purchase_order_id = fields.Many2one('purchase.order', string="Purchase Order", help="All purchase orders") state = fields.Selection([ ('draft', 'Draft'), ('pending', 'Pending'), ('in_progress', 'In Progress'), ('done', 'Done'), ('cancel', 'Cancelled'), ('order', 'Purchase Order'), ], string="Status", default='draft', help="Status of the vendor rfq") company_id = fields.Many2one('res.company', string='Company', default=lambda self: self.env.company, help='Current company') @api.model def create(self, vals): """Create function for vendor rfq""" if vals.get('name', 'New') == 'New': vals['name'] = self.env['ir.sequence'].next_by_code( 'vendor.rfq') or '/' res = super(VendorRFQ, self).create(vals) return res def action_send_by_mail(self): """For sending email to vendors""" template = self.env.ref( 'vendor_portal_odoo.email_template_vendor_rfq_request').id for vendor in self.vendor_ids: context = { 'name': vendor.name, 'partner_to': vendor.id, 'lang': vendor.lang, } email_values = { 'email_to': vendor.email, 'email_from': self.env.user.partner_id.email, 'subject': 'RFQ Request', } self.env['mail.template'].browse(template).with_context( context).send_mail(self.id, email_values=email_values, force_send=True) self.state = 'in_progress' def action_pending(self): """For changing state to pending""" self.state = 'pending' def action_cancel(self): """For changing state to cancel""" self.state = 'cancel' def action_done(self): """For mark as done""" return { 'type': 'ir.actions.act_window', 'view_mode': 'form', 'res_model': 'rfq.done', 'target': 'new', 'views': [[False, 'form']], } def action_create_quotation(self): """For creating purchase RFQ from vendor quotations""" rfq_quote = self.env['vendor.quote.history'].search([ ('vendor_id', '=', self.approved_vendor_id.id), ('quote_id', '=', self.id)]) price = rfq_quote.quoted_price order = self.env['purchase.order'].sudo().create({ 'partner_id': self.approved_vendor_id.id, 'order_line': [ (0, 0, { 'name': self.product_id.name, 'product_id': self.product_id.id, 'product_qty': self.quantity, 'product_uom': self.product_id.uom_po_id.id, 'price_unit': price, 'date_planned': rfq_quote.estimate_date, 'taxes_id': [(6, 0, self.product_id.supplier_taxes_id.ids)], })], }) self.write({'state': 'order', 'purchase_order_id': order.id }) return { 'type': 'ir.actions.act_window', 'res_model': 'purchase.order', 'res_id': order.id, 'target': 'current', 'views': [(False, 'form')], } def set_rfq_done(self): """Set the RFQ as done""" quotes = self.search([('state', '=', 'in_progress'), ('vendor_quote_history_ids', '!=', False), ('closing_date', '=', fields.Date.today())]) if quotes: rfq_done_based_on = self.env['ir.config_parameter'].get_param( 'vendor_portal_odoo.rfq_done_based_on') for rec in quotes: order = 'quoted_price asc' if rfq_done_based_on == 'based_on_price' else 'estimate_date asc' vendor_quotes = rec.vendor_quote_history_ids.search([], limit=1, order=order) rec.write({'approved_vendor_id': vendor_quotes.vendor_id.id, 'state': 'done' }) def action_get_purchase_order(self): """Returns Purchase Order""" return { 'type': 'ir.actions.act_window', 'res_model': 'purchase.order', 'res_id': self.purchase_order_id.id, 'target': 'current', 'views': [(False, 'form')], }