Skip to content

Instantly share code, notes, and snippets.

@rvalyi
Created May 9, 2020 16:04
Show Gist options
  • Save rvalyi/7f632fb488007a49588fa5ceb8684587 to your computer and use it in GitHub Desktop.
Save rvalyi/7f632fb488007a49588fa5ceb8684587 to your computer and use it in GitHub Desktop.
from odoo import api, fields, models
from odoo.tools import float_compare
class SaleOrderLine(models.Model):
_inherit = 'sale.order.line'
# here we let subcontracted services create purchase orders via
# the subcontracted_service module inside the same
# procurement group
# ver https://github.com/OCA/purchase-workflow/blob/12.0/subcontracted_service/tests/test_subcontracted_service.py#L49
def _action_launch_stock_rule(self):
"""
Launch procurement group run method with required/custom fields genrated by a
sale order line. procurement group will launch '_run_pull', '_run_buy' or '_run_manufacture'
depending on the sale order line product rule.
"""
precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
errors = []
for line in self:
if line.state != 'sale'\
or not line.product_id.property_subcontracted_service:
# dealt in super later when a product or consumable
continue
qty = line._get_qty_procurement()
if float_compare(qty, line.product_uom_qty, precision_digits=precision) >= 0:
continue
group_id = line.order_id.warehouse_id.subcontracting_service_proc_rule_id.group_id
values = line._prepare_procurement_values(group_id=group_id)
product_qty = line.product_uom_qty - qty
procurement_uom = line.product_uom
try:
self.env['procurement.group'].run(line.product_id, product_qty, procurement_uom, line.order_id.partner_shipping_id.property_stock_customer, line.name, line.order_id.name, values)
except UserError as error:
errors.append(error.name)
if errors:
raise UserError('\n'.join(errors))
return super(SaleOrderLine, self)._action_launch_stock_rule()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment