<commit_before># -*- coding: utf-8 -*-
# © 2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import fields, models, api, _
from openerp.exceptions import ValidationError
import logging


_logger = logging.getLogger(__name__)


class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    dispense_qty = fields.Float(
        default=0.0,
        readonly=True,
        compute='_compute_dispense_qty',
    )

    @api.multi
    @api.depends('product_uom',
                 'prescription_order_line_id.dispense_uom_id',
                 )
    def _compute_dispense_qty(self):
        for rec_id in self:
            rx_line = rec_id.prescription_order_line_id
            if rec_id.product_uom == rx_line.dispense_uom_id:
                rec_id.dispense_qty = rec_id.product_uom_qty
            else:
                rec_id.dispense_qty = self.env['product.uom']._compute_qty_obj(
                    rec_id.product_uom,
                    rec_id.product_uom_qty,
                    rx_line.dispense_uom_id,
                )

    @api.multi
    @api.constrains('product_id', 'prescription_order_line_id')
    def _check_product(self):
        for rec_id in self:
            if not rec_id.prescription_order_line_id:
                continue
            rx_line = rec_id.prescription_order_line_id
            if rx_line.medicament_id.product_id != rec_id.product_id:
                if not rx_line.is_substitutable:
                    raise ValidationError(_(
                        'Products must be same on Order and Rx lines. '
                        'Got %s on order line %s, expected %s from %r' % (
                            rec_id.product_id.name, rec_id.name,
                            rx_line.medicament_id.product_id.name, rx_line,
                        ),
                    ))
                else:
                    # @TODO: implement determination for what drugs can be
                    #        substituted
                    pass

    @api.multi
    @api.constrains('patient_id', 'prescription_order_line_id')
    def _check_patient(self):
        for rec_id in self:
            if not rec_id.prescription_order_line_id:
                continue
            rx_line = rec_id.prescription_order_line_id
            if rec_id.patient_id != rx_line.patient_id:
                raise ValidationError(_(
                    'Patients must be same on Order and Rx lines. '
                    'Got %s on order line %d, expected %s from rx line %d' % (
                        rec_id.patient_id.name, rec_id.id,
                        rx_line.patient_id.name, rx_line.id,
                    ),
                ))

    @api.multi
    @api.constrains('dispense_qty', 'prescription_order_line_id')
    def _check_can_dispense(self):
        for rec_id in self:
            if not rec_id.prescription_order_line_id:
                continue
            rx_line = rec_id.prescription_order_line_id
            if not rx_line.can_dispense:
                raise ValidationError(_(
                    'Cannot dispense because there are related, '
                    'pending order(s). \n'
                    'Currently %.2f processed %.2f pending %.2f exception' % (
                        rx_line.dispensed_qty,
                        rx_line.pending_dispense_qty,
                        rx_line.exception_dispense_qty,
                    )
                ))
            if rec_id.dispense_qty > rx_line.can_dispense_qty:
                raise ValidationError(_(
                    'Cannot dispense - %s goes over Rx qty by %d' % (
                        rec_id.name,
                        rec_id.dispense_qty - rx_line.can_dispense_qty
                    )
                ))

    @api.multi
    def _prepare_order_line_procurement(self, group_id=False):
        self.ensure_one()
        res = super(SaleOrderLine, self)._prepare_order_line_procurement(
            group_id=group_id
        )
        if self.product_id.is_medicament:
            medicament_id = self.env['medical.medicament'].get_by_product(
                self.product_id,
            )
            warehouse_id = self.order_id.warehouse_id
            if medicament_id.is_prescription:
                res['route_ids'] = [
                    (6, 0, [warehouse_id.prescription_route_id.id])
                ]
            else:
                res['route_ids'] = [
                    (6, 0, [warehouse_id.otc_route_id.id])
                ]
        return res
<commit_msg>Add ability to override rx constraints w/ context in medical_prescription_sale_stock<commit_after># -*- coding: utf-8 -*-
# © 2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from openerp import fields, models, api, _
from openerp.exceptions import ValidationError
import logging


_logger = logging.getLogger(__name__)


class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    dispense_qty = fields.Float(
        default=0.0,
        readonly=True,
        compute='_compute_dispense_qty',
    )

    @api.multi
    @api.depends('product_uom',
                 'prescription_order_line_id.dispense_uom_id',
                 )
    def _compute_dispense_qty(self):
        for rec_id in self:
            rx_line = rec_id.prescription_order_line_id
            if rec_id.product_uom == rx_line.dispense_uom_id:
                rec_id.dispense_qty = rec_id.product_uom_qty
            else:
                rec_id.dispense_qty = self.env['product.uom']._compute_qty_obj(
                    rec_id.product_uom,
                    rec_id.product_uom_qty,
                    rx_line.dispense_uom_id,
                )

    @api.multi
    @api.constrains('product_id', 'prescription_order_line_id')
    def _check_product(self):
        if self.env.context.get('__rx_force__'):
            return True
        for rec_id in self:
            if not rec_id.prescription_order_line_id:
                continue
            rx_line = rec_id.prescription_order_line_id
            if rx_line.medicament_id.product_id != rec_id.product_id:
                if not rx_line.is_substitutable:
                    raise ValidationError(_(
                        'Products must be same on Order and Rx lines. '
                        'Got %s on order line %s, expected %s from %r' % (
                            rec_id.product_id.name, rec_id.name,
                            rx_line.medicament_id.product_id.name, rx_line,
                        ),
                    ))
                else:
                    # @TODO: implement determination for what drugs can be
                    #        substituted
                    pass

    @api.multi
    @api.constrains('patient_id', 'prescription_order_line_id')
    def _check_patient(self):
        if self.env.context.get('__rx_force__'):
            return True
        for rec_id in self:
            if not rec_id.prescription_order_line_id:
                continue
            rx_line = rec_id.prescription_order_line_id
            if rec_id.patient_id != rx_line.patient_id:
                raise ValidationError(_(
                    'Patients must be same on Order and Rx lines. '
                    'Got %s on order line %d, expected %s from rx line %d' % (
                        rec_id.patient_id.name, rec_id.id,
                        rx_line.patient_id.name, rx_line.id,
                    ),
                ))

    @api.multi
    @api.constrains('dispense_qty', 'prescription_order_line_id')
    def _check_can_dispense(self):
        if self.env.context.get('__rx_force__'):
            return True
        for rec_id in self:
            if not rec_id.prescription_order_line_id:
                continue
            rx_line = rec_id.prescription_order_line_id
            if not rx_line.can_dispense:
                raise ValidationError(_(
                    'Cannot dispense %s because there are related, '
                    'pending order(s). \n'
                    'Currently %.2f processed %.2f pending %.2f exception' % (
                        rec_id.dispense_qty,
                        rx_line.dispensed_qty,
                        rx_line.pending_dispense_qty,
                        rx_line.exception_dispense_qty,
                    )
                ))
            if rec_id.dispense_qty > rx_line.can_dispense_qty:
                raise ValidationError(_(
                    'Cannot dispense - %s goes over Rx qty by %d' % (
                        rec_id.name,
                        rec_id.dispense_qty - rx_line.can_dispense_qty
                    )
                ))

    @api.multi
    def _prepare_order_line_procurement(self, group_id=False):
        self.ensure_one()
        res = super(SaleOrderLine, self)._prepare_order_line_procurement(
            group_id=group_id
        )
        if self.product_id.is_medicament:
            medicament_id = self.env['medical.medicament'].get_by_product(
                self.product_id,
            )
            warehouse_id = self.order_id.warehouse_id
            if medicament_id.is_prescription:
                res['route_ids'] = [
                    (6, 0, [warehouse_id.prescription_route_id.id])
                ]
            else:
                res['route_ids'] = [
                    (6, 0, [warehouse_id.otc_route_id.id])
                ]
        return res

{'fraction_non_alphanumeric': 0.07866836301950805, 'fraction_numerical': 0.0033927056827820186, 'mean_word_length': 1.5027858848500928, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2366304', 'n_tokens_mistral': 2877, 'n_tokens_neox': 2706, 'n_words': 515}