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

import logging
from openerp import fields
from openerp import models
from openerp.addons.connector.unit.mapper import (mapping,
                                                  ExportMapper,
                                                  none,
                                                  convert,
                                                  )
from ..unit.backend_adapter import CarepointCRUDAdapter
from ..unit.mapper import CarepointImportMapper
from ..backend import carepoint
from ..unit.import_synchronizer import (DelayedBatchImporter,
                                        CarepointImporter,
                                        )
from ..unit.export_synchronizer import (CarepointExporter)

_logger = logging.getLogger(__name__)


class CarepointMedicalPrescriptionOrder(models.Model):
    """ Binding Model for the Carepoint Prescription """
    _name = 'carepoint.medical.prescription.order'
    _inherit = 'carepoint.binding'
    _inherits = {'medical.prescription.order': 'odoo_id'}
    _description = 'Carepoint Prescription'
    _cp_lib = 'prescription'  # Name of model in Carepoint lib (snake_case)

    odoo_id = fields.Many2one(
        comodel_name='medical.prescription.order',
        string='Prescription Line',
        required=True,
        ondelete='cascade'
    )


class MedicalPrescriptionOrder(models.Model):
    """ Adds the ``one2many`` relation to the Carepoint bindings
    (``carepoint_bind_ids``)
    """
    _inherit = 'medical.prescription.order'

    carepoint_bind_ids = fields.One2many(
        comodel_name='carepoint.medical.prescription.order',
        inverse_name='odoo_id',
        string='Carepoint Bindings',
    )


@carepoint
class MedicalPrescriptionOrderAdapter(CarepointCRUDAdapter):
    """ Backend Adapter for the Carepoint Prescription """
    _model_name = 'carepoint.medical.prescription.order'


@carepoint
class MedicalPrescriptionOrderBatchImporter(DelayedBatchImporter):
    """ Import the Carepoint Prescriptions.
    For every prescription in the list, a delayed job is created.
    """
    _model_name = ['carepoint.medical.prescription.order']


@carepoint
class MedicalPrescriptionOrderImportMapper(CarepointImportMapper):
    _model_name = 'carepoint.medical.prescription.order'

    direct = [
        ('start_date', 'date_prescription'),
    ]

    @mapping
    def patient_id(self, record):
        binder = self.binder_for('carepoint.medical.patient')
        patient_id = binder.to_odoo(record['pat_id'])
        return {'patient_id': patient_id}

    @mapping
    def physician_id(self, record):
        binder = self.binder_for('carepoint.medical.physician')
        physician_id = binder.to_odoo(record['md_id'])
        return {'physician_id': physician_id}

    @mapping
    def partner_id(self, record):
        binder = self.binder_for('carepoint.carepoint.store')
        store = binder.to_odoo(record['store_id'], browse=True)
        return {'partner_id': store.pharmacy_id.id}

    @mapping
    def carepoint_id(self, record):
        return {'carepoint_id': record['rx_id']}


@carepoint
class MedicalPrescriptionOrderImporter(CarepointImporter):
    _model_name = ['carepoint.medical.prescription.order']
    _base_mapper = MedicalPrescriptionOrderImportMapper

    def _import_dependencies(self):
        """ Import depends for record """
        record = self.carepoint_record
        self._import_dependency(record['pat_id'],
                                'carepoint.medical.patient')
        self._import_dependency(record['md_id'],
                                'carepoint.medical.physician')


@carepoint
class MedicalPrescriptionOrderExportMapper(ExportMapper):
    _model_name = 'carepoint.medical.prescription.order'

    direct = [
        (convert('date_prescription', fields.Datetime.from_string),
         'start_date'),
        (none('qty'), 'written_qty'),
        (none('frequency'), 'freq_of_admin'),
        (none('quantity'), 'units_per_dose'),
        # Note that the col naming seems to be reversed *shrug*
        # ('refill_qty_original', 'refills_left'),
        # ('refill_qty_remain', 'refills_orig'),
    ]

    @mapping
    def pat_id(self, record):
        return {'pat_id': record.carepoint_id}


@carepoint
class MedicalPrescriptionOrderExporter(CarepointExporter):
    _model_name = ['carepoint.medical.prescription.order']
    _base_mapper = MedicalPrescriptionOrderExportMapper<commit_msg>[IMP] connector_carepoint: Void Rx handling * Add handling for voided prescriptions * Simplify medical.prescription.order mappers<commit_after># -*- coding: utf-8 -*-
# Copyright 2015-2016 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import logging
from openerp import fields
from openerp import models
from openerp.addons.connector.unit.mapper import (mapping,
                                                  ExportMapper,
                                                  none,
                                                  convert,
                                                  backend_to_m2o,
                                                  )
from ..unit.backend_adapter import CarepointCRUDAdapter
from ..unit.mapper import CarepointImportMapper
from ..backend import carepoint
from ..unit.import_synchronizer import (DelayedBatchImporter,
                                        CarepointImporter,
                                        )
from ..unit.export_synchronizer import (CarepointExporter)

_logger = logging.getLogger(__name__)


class CarepointMedicalPrescriptionOrder(models.Model):
    """ Binding Model for the Carepoint Prescription """
    _name = 'carepoint.medical.prescription.order'
    _inherit = 'carepoint.binding'
    _inherits = {'medical.prescription.order': 'odoo_id'}
    _description = 'Carepoint Prescription'
    _cp_lib = 'prescription'  # Name of model in Carepoint lib (snake_case)

    odoo_id = fields.Many2one(
        comodel_name='medical.prescription.order',
        string='Prescription Line',
        required=True,
        ondelete='cascade'
    )


class MedicalPrescriptionOrder(models.Model):
    """ Adds the ``one2many`` relation to the Carepoint bindings
    (``carepoint_bind_ids``)
    """
    _inherit = 'medical.prescription.order'

    carepoint_bind_ids = fields.One2many(
        comodel_name='carepoint.medical.prescription.order',
        inverse_name='odoo_id',
        string='Carepoint Bindings',
    )


@carepoint
class MedicalPrescriptionOrderAdapter(CarepointCRUDAdapter):
    """ Backend Adapter for the Carepoint Prescription """
    _model_name = 'carepoint.medical.prescription.order'


@carepoint
class MedicalPrescriptionOrderBatchImporter(DelayedBatchImporter):
    """ Import the Carepoint Prescriptions.
    For every prescription in the list, a delayed job is created.
    """
    _model_name = ['carepoint.medical.prescription.order']


@carepoint
class MedicalPrescriptionOrderImportMapper(CarepointImportMapper):
    _model_name = 'carepoint.medical.prescription.order'

    direct = [
        ('start_date', 'date_prescription'),
        ('rx_id', 'carepoint_id'),
        (backend_to_m2o('pat_id', binding='carepoint.medical.patient'),
         'patient_id'),
        (backend_to_m2o('md_id', binding='carepoint.medical.physician'),
         'physician_id'),
    ]

    @mapping
    def active(self, record):
        return {'active': record['status_cn'] == 3}

    @mapping
    def partner_id(self, record):
        binder = self.binder_for('carepoint.carepoint.store')
        store = binder.to_odoo(record['store_id'], browse=True)
        return {'partner_id': store.pharmacy_id.id}


@carepoint
class MedicalPrescriptionOrderImporter(CarepointImporter):
    _model_name = ['carepoint.medical.prescription.order']
    _base_mapper = MedicalPrescriptionOrderImportMapper

    def _import_dependencies(self):
        """ Import depends for record """
        record = self.carepoint_record
        self._import_dependency(record['pat_id'],
                                'carepoint.medical.patient')
        self._import_dependency(record['md_id'],
                                'carepoint.medical.physician')


@carepoint
class MedicalPrescriptionOrderExportMapper(ExportMapper):
    _model_name = 'carepoint.medical.prescription.order'

    direct = [
        (convert('date_prescription', fields.Datetime.from_string),
         'start_date'),
        ('carepoint_id', 'pat_id'),
        (none('qty'), 'written_qty'),
        (none('frequency'), 'freq_of_admin'),
        (none('quantity'), 'units_per_dose'),
        # Note that the col naming seems to be reversed *shrug*
        # ('refill_qty_original', 'refills_left'),
        # ('refill_qty_remain', 'refills_orig'),
    ]

    @mapping
    def status_cn(self, record):
        if not record.active:
            return {'status_cn': 3}


@carepoint
class MedicalPrescriptionOrderExporter(CarepointExporter):
    _model_name = ['carepoint.medical.prescription.order']
    _base_mapper = MedicalPrescriptionOrderExportMapper
{'fraction_non_alphanumeric': 0.1025222874537943, 'fraction_numerical': 0.003587736464448793, 'mean_word_length': 2.815429282455413, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '413209', 'n_tokens_mistral': 2644, 'n_tokens_neox': 2501, 'n_words': 498}