<commit_before># -*- coding: utf-8 -*-
# Copyright 2016 Eficent Business and IT Consulting Services S.L.
# Copyright 2016 <NAME>/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0).

from datetime import datetime

from dateutil.relativedelta import relativedelta

from odoo import _, api, exceptions, fields, models


class PurchaseRequestLine(models.Model):

    _inherit = "purchase.request.line"

    @api.multi
    @api.depends('purchase_lines')
    def _compute_is_editable(self):
        super(PurchaseRequestLine, self)._compute_is_editable()
        self.filtered(lambda p: p in self and p.purchase_lines).update(
            {'is_editable': False})

    @api.multi
    def _compute_purchased_qty(self):
        for rec in self:
            rec.purchased_qty = 0.0
            for line in rec.purchase_lines.filtered(
                    lambda x: x.state != 'cancel'):
                if rec.product_uom_id and\
                        line.product_uom != rec.product_uom_id:
                    rec.purchased_qty += line.product_uom._compute_quantity(
                        line.product_qty, rec.product_uom_id)
                else:
                    rec.purchased_qty += line.product_qty

    @api.multi
    @api.depends('purchase_lines.state', 'purchase_lines.order_id.state')
    def _compute_purchase_state(self):
        for rec in self:
            temp_purchase_state = False
            if rec.purchase_lines:
                if any([po_line.state == 'done' for po_line in
                        rec.purchase_lines]):
                    temp_purchase_state = 'done'
                elif all([po_line.state == 'cancel' for po_line in
                          rec.purchase_lines]):
                    temp_purchase_state = 'cancel'
                elif any([po_line.state == 'purchase' for po_line in
                          rec.purchase_lines]):
                    temp_purchase_state = 'purchase'
                elif any([po_line.state == 'to approve' for po_line in
                          rec.purchase_lines]):
                    temp_purchase_state = 'to approve'
                elif any([po_line.state == 'sent' for po_line in
                          rec.purchase_lines]):
                    temp_purchase_state = 'sent'
                elif all([po_line.state in ('draft', 'cancel') for po_line in
                          rec.purchase_lines]):
                    temp_purchase_state = 'draft'
            rec.purchase_state = temp_purchase_state

    purchased_qty = fields.Float(string='Quantity in RFQ or PO',
                                 compute="_compute_purchased_qty")
    purchase_lines = fields.Many2many(
        'purchase.order.line', 'purchase_request_purchase_order_line_rel',
        'purchase_request_line_id',
        'purchase_order_line_id', 'Purchase Order Lines',
        readonly=True, copy=False)
    purchase_state = fields.Selection(
        compute="_compute_purchase_state",
        string="Purchase Status",
        selection=lambda self:
        self.env['purchase.order']._fields['state'].selection,
        stored=True,
    )

    @api.model
    def _planned_date(self, request_line, delay=0.0):
        company = request_line.company_id
        date_planned = datetime.strptime(
            request_line.date_required, '%Y-%m-%d') - \
            relativedelta(days=company.po_lead)
        if delay:
            date_planned -= relativedelta(days=delay)
        return date_planned and date_planned.strftime('%Y-%m-%d') \
            or False

    @api.model
    def _calc_new_qty(self, request_line, po_line=None, cancel=False):
        uom = request_line.product_uom_id
        qty = uom._compute_quantity(request_line.product_qty,
                                    request_line.product_id.uom_po_id)
        # Make sure we use the minimum quantity of the partner corresponding
        # to the PO. This does not apply in case of dropshipping
        supplierinfo_min_qty = 0.0
        if not po_line.order_id.dest_address_id:
            if po_line.product_id.seller_ids and \
                po_line.product_id.seller_ids[0].id == \
                    po_line.order_id.partner_id.id:
                supplierinfo_min_qty = po_line.product_id.seller_ids[0].min_qty
            else:
                supplierinfo_obj = self.env['product.supplierinfo']
                supplierinfos = supplierinfo_obj.search(
                    [('name', '=', po_line.order_id.partner_id.id),
                     ('product_tmpl_id', '=',
                      po_line.product_id.product_tmpl_id.id)])
                if supplierinfos:
                    supplierinfo_min_qty = supplierinfos[0].min_qty

        if not supplierinfo_min_qty:
            qty += po_line.product_qty
        else:
            # Recompute quantity by adding existing running procurements.
            for rl in po_line.purchase_request_lines:
                qty += uom._compute_quantity(rl.product_qty,
                                             rl.product_id.uom_po_id)
            qty = max(qty, supplierinfo_min_qty) if qty > 0.0 else 0.0
        return qty

    @api.multi
    def unlink(self):
        if self.mapped('purchase_lines'):
            raise exceptions.Warning(
                _('You cannot delete a record that refers to purchase '
                  'lines!'))
        return super(PurchaseRequestLine, self).unlink()<commit_msg>[FIX] Use UOM of current request line at total quantity recomputation<commit_after># -*- coding: utf-8 -*-
# Copyright 2016 Eficent Business and IT Consulting Services S.L.
# Copyright 2016 Acsone SA/NV
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0).

from datetime import datetime

from dateutil.relativedelta import relativedelta

from odoo import _, api, exceptions, fields, models


class PurchaseRequestLine(models.Model):

    _inherit = "purchase.request.line"

    @api.multi
    @api.depends('purchase_lines')
    def _compute_is_editable(self):
        super(PurchaseRequestLine, self)._compute_is_editable()
        self.filtered(lambda p: p in self and p.purchase_lines).update(
            {'is_editable': False})

    @api.multi
    def _compute_purchased_qty(self):
        for rec in self:
            rec.purchased_qty = 0.0
            for line in rec.purchase_lines.filtered(
                    lambda x: x.state != 'cancel'):
                if rec.product_uom_id and\
                        line.product_uom != rec.product_uom_id:
                    rec.purchased_qty += line.product_uom._compute_quantity(
                        line.product_qty, rec.product_uom_id)
                else:
                    rec.purchased_qty += line.product_qty

    @api.multi
    @api.depends('purchase_lines.state', 'purchase_lines.order_id.state')
    def _compute_purchase_state(self):
        for rec in self:
            temp_purchase_state = False
            if rec.purchase_lines:
                if any([po_line.state == 'done' for po_line in
                        rec.purchase_lines]):
                    temp_purchase_state = 'done'
                elif all([po_line.state == 'cancel' for po_line in
                          rec.purchase_lines]):
                    temp_purchase_state = 'cancel'
                elif any([po_line.state == 'purchase' for po_line in
                          rec.purchase_lines]):
                    temp_purchase_state = 'purchase'
                elif any([po_line.state == 'to approve' for po_line in
                          rec.purchase_lines]):
                    temp_purchase_state = 'to approve'
                elif any([po_line.state == 'sent' for po_line in
                          rec.purchase_lines]):
                    temp_purchase_state = 'sent'
                elif all([po_line.state in ('draft', 'cancel') for po_line in
                          rec.purchase_lines]):
                    temp_purchase_state = 'draft'
            rec.purchase_state = temp_purchase_state

    purchased_qty = fields.Float(string='Quantity in RFQ or PO',
                                 compute="_compute_purchased_qty")
    purchase_lines = fields.Many2many(
        'purchase.order.line', 'purchase_request_purchase_order_line_rel',
        'purchase_request_line_id',
        'purchase_order_line_id', 'Purchase Order Lines',
        readonly=True, copy=False)
    purchase_state = fields.Selection(
        compute="_compute_purchase_state",
        string="Purchase Status",
        selection=lambda self:
        self.env['purchase.order']._fields['state'].selection,
        stored=True,
    )

    @api.model
    def _planned_date(self, request_line, delay=0.0):
        company = request_line.company_id
        date_planned = datetime.strptime(
            request_line.date_required, '%Y-%m-%d') - \
            relativedelta(days=company.po_lead)
        if delay:
            date_planned -= relativedelta(days=delay)
        return date_planned and date_planned.strftime('%Y-%m-%d') \
            or False

    @api.model
    def _calc_new_qty(self, request_line, po_line=None, cancel=False):
        uom = request_line.product_uom_id
        qty = uom._compute_quantity(request_line.product_qty,
                                    request_line.product_id.uom_po_id)
        # Make sure we use the minimum quantity of the partner corresponding
        # to the PO. This does not apply in case of dropshipping
        supplierinfo_min_qty = 0.0
        if not po_line.order_id.dest_address_id:
            if po_line.product_id.seller_ids and \
                po_line.product_id.seller_ids[0].id == \
                    po_line.order_id.partner_id.id:
                supplierinfo_min_qty = po_line.product_id.seller_ids[0].min_qty
            else:
                supplierinfo_obj = self.env['product.supplierinfo']
                supplierinfos = supplierinfo_obj.search(
                    [('name', '=', po_line.order_id.partner_id.id),
                     ('product_tmpl_id', '=',
                      po_line.product_id.product_tmpl_id.id)])
                if supplierinfos:
                    supplierinfo_min_qty = supplierinfos[0].min_qty

        if not supplierinfo_min_qty:
            qty += po_line.product_qty
        else:
            # Recompute quantity by adding existing running procurements.
            for rl in po_line.purchase_request_lines:
                qty += rl.product_uom_id._compute_quantity(
                    rl.product_qty, rl.product_id.uom_po_id)
            qty = max(qty, supplierinfo_min_qty) if qty > 0.0 else 0.0
        return qty

    @api.multi
    def unlink(self):
        if self.mapped('purchase_lines'):
            raise exceptions.Warning(
                _('You cannot delete a record that refers to purchase '
                  'lines!'))
        return super(PurchaseRequestLine, self).unlink()
{'fraction_non_alphanumeric': 0.08937264627537429, 'fraction_numerical': 0.004960044089280794, 'mean_word_length': 1.9088966069997328, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '365988', 'n_tokens_mistral': 3312, 'n_tokens_neox': 3169, 'n_words': 639}