Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imp/purchase requisition full bid order generator #1353

Open
wants to merge 3 commits into
base: 8.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion purchase_requisition_full_bid_order_generator/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"Oihane Crucelaegui <[email protected]>"],
"depends": ["base", "purchase", "purchase_requisition"],
"data": [
"views/bids_ext_view.xml"],
"views/bids_ext_view.xml",
"views/purchase_config_setting_view.xml",],
"installable": True
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
##############################################################################

from . import bids
from . import purchase_config_setting
from . import res_company
79 changes: 65 additions & 14 deletions purchase_requisition_full_bid_order_generator/models/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,76 @@
# For copyright and license notices, see __openerp__.py file in root directory
##############################################################################

from openerp import models, api
from openerp import models, api, fields


class PurchaseRequisition(models.Model):
_inherit = 'purchase.requisition'

@api.multi
def _prepare_requisition_purchase_order_line(self, line, product, product_qty, purchase_id, supplier):
po_line_obj = self.env['purchase.order.line']
product_uom = self.env['product.uom']
default_uom_po_id = product.uom_po_id.id
date_order = self.ordering_date or fields.Datetime.now()
qty = product_uom.browse(product.uom_id.id)._compute_qty(default_uom_po_id, product_qty)
supplier_pricelist = supplier.property_product_pricelist_purchase and supplier.property_product_pricelist_purchase.id or False
vals = po_line_obj.onchange_product_id(
supplier_pricelist, product.id, qty, default_uom_po_id,
supplier.id, date_order=date_order,
fiscal_position_id=supplier.property_account_position.id,
date_planned=line.schedule_date,
name=False, price_unit=False, state='draft')['value']
vals.update({
'order_id': purchase_id,
'product_id': product.id,
'account_analytic_id': line.account_analytic_id.id,
'taxes_id': [(6, 0, vals.get('taxes_id', []))],
})
return vals

@api.multi
def create_purchase_order(self):
supplier_obj = self.env['res.partner']
supplierinfo_obj = self.env['product.supplierinfo']
# Start with all suppliers
suppliers = supplier_obj.search([('supplier', '=', True)])
for line in self.line_ids:
sinfos = supplierinfo_obj.search(
[('product_tmpl_id', '=', line.product_id.product_tmpl_id.id)])
suppliers &= sinfos.mapped('name')
# Stop condition to avoid the full loop if we don't have suppliers
if not suppliers:
break
# Create RFQ to all suppliers with products they have from Requisition line
if self.company_id.rfq_to_suppliers == 's':
suppliers_dict = {}
for line in self.line_ids:
for supplier in line.product_id.seller_ids:
supplier_id = supplier.name.id
if supplier_id not in suppliers_dict.keys():
suppliers_dict[supplier_id] = [{line.product_id: line.product_qty}]
else:
if suppliers_dict[supplier_id][0].has_key(line.product_id):
suppliers_dict[supplier_id][0][line.product_id] = suppliers_dict[supplier_id][0][
line.product_id] + line.product_qty
else:
suppliers_dict[supplier_id].append({line.product_id: line.product_qty})

for supplier, product_list in suppliers_dict.iteritems():
res_partner = self.env['res.partner']
supplier = res_partner.browse(supplier)
# create purcahse order
purchase = self.env['purchase.order'].create(self._prepare_purchase_order(self, supplier))
for product_dict in product_list:
for product, qty in product_dict.iteritems():
# create purchase order line
line = self.env['purchase.requisition.line'].search(
[('product_id', '=', product.id), ('requisition_id', '=', self.id)], limit=1)
self.env['purchase.order.line'].create(
self._prepare_requisition_purchase_order_line(line, product, qty, purchase.id, supplier))
else:
# Create RFQ to the supplier which has all the products
supplier_obj = self.env['res.partner']
supplierinfo_obj = self.env['product.supplierinfo']
# Start with all suppliers
suppliers = supplier_obj.search([('supplier', '=', True)])
for line in self.line_ids:
sinfos = supplierinfo_obj.search(
[('product_tmpl_id', '=', line.product_id.product_tmpl_id.id)])
suppliers &= sinfos.mapped('name')
# Stop condition to avoid the full loop if we don't have suppliers
if not suppliers:
break

for supplier in suppliers:
self.make_purchase_order(supplier.id)
for supplier in suppliers:
self.make_purchase_order(supplier.id)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
##############################################################################
# For copyright and license notices, see __openerp__.py file in root directory
##############################################################################

from openerp import models, api, fields


class purchase_config_settings(models.Model):
_inherit = 'purchase.config.settings'

rfq_to_suppliers = fields.Selection([('a', 'Create RFQ to only suppliers, with all the products'),
('s', 'Create RFQ to all the suppliers with available products')],
string='RFQ from Bids')

@api.multi
def get_default_rfq_to_suppliers(self):
return {'rfq_to_suppliers': self.env.user.company_id.rfq_to_suppliers}

@api.one
def set_default_rfq_to_suppliers(self):
self.env.user.company_id.write({'rfq_to_suppliers': self.rfq_to_suppliers})
return True
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
##############################################################################
# For copyright and license notices, see __openerp__.py file in root directory
##############################################################################

from openerp import models, api, fields


class res_company(models.Model):
_inherit = 'res.company'

rfq_to_suppliers = fields.Selection([('a', 'To All Products'), ('s', 'To All Suppliers')], default='a',
required=True, string='RFQ from Bids')
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<field name="type">form</field>
<field name="arch" type="xml">
<button string="Request a Quotation" position="after">
<button name="create_purchase_order" type="object" string="Request a quotation to suppliers of all products"
<button name="create_purchase_order" type="object" string="Request Quotations"
attrs="{'invisible': ['|', ('line_ids','=',[]),('state', 'in', ('draft' ,'open','done'))]}"/>
</button>
</field>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>

<record id="view_purchase_configuration" model="ir.ui.view">
<field name="name">purchase settings</field>
<field name="model">purchase.config.settings</field>
<field name="inherit_id" ref="purchase.view_purchase_configuration"/>
<field name="arch" type="xml">
<separator string="Location &amp; Warehouse" position="before">
<separator string="Purchase Requisition"/>
<group>
<field name="rfq_to_suppliers" widget="radio"/>
</group>
</separator>
</field>
</record>

</data>
</openerp>