Skip to content

Commit

Permalink
[FIX] connector_carepoint: Sync issues
Browse files Browse the repository at this point in the history
* Fix dates on prescription and prescription line export
* Fix date on prescription import
* Fix address and phone syncing
* Add missing binders
* Add missing consumers
* Handle blank zips on import/export
* Account for +1 variance on Carepoint Refills Left
* Support multiple types of phone numbers
* Upgrade for support w/ Python-Carepoint v.0.1.5
* Add method on backend to force sync of one record
  • Loading branch information
lasley committed Oct 28, 2016
1 parent 9b92c9b commit e6add7e
Show file tree
Hide file tree
Showing 23 changed files with 494 additions and 159 deletions.
14 changes: 14 additions & 0 deletions connector_carepoint/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ def delay_export(session, model_name, record_id, vals):
@on_record_write(model_names=['medical.prescription.order.line',
'medical.patient',
'carepoint.address',
'carepoint.phone',
'carepoint.address.patient',
'carepoint.phone.patient',
'carepoint.organization',
'carepoint.address.organization',
'carepoint.phone.organization',
'medical.physician',
'carepoint.address.physician',
'carepoint.phone.physician',
])
def delay_export_all_bindings(session, model_name, record_id, vals):
""" Delay a job which export all the bindings of a record.
Expand All @@ -57,12 +61,16 @@ def delay_export_all_bindings(session, model_name, record_id, vals):
@on_record_create(model_names=['medical.prescription.order.line',
'medical.patient',
'carepoint.address',
'carepoint.phone',
'carepoint.address.patient',
'carepoint.phone.patient',
'carepoint.organization',
'carepoint.address.organization',
'carepoint.phone.organization',
'carepoint.account',
'medical.physician',
'carepoint.address.physician',
'carepoint.phone.physician',
])
def delay_create(session, model_name, record_id, vals):
""" Create a new binding record, then trigger delayed export
Expand Down Expand Up @@ -93,3 +101,9 @@ def delay_create(session, model_name, record_id, vals):
# if carepoint_id:
# export_delete_record.delay(session, model_name,
# record.backend_id.id, carepoint_id)


@on_record_write(model_names=['carepoint.phone'])
def sync_phone_to_partner(session, model_name, record_id, vals):
""" Triggers phone sync to partner when written """
session.env[model_name].browse(record_id)._sync_partner()
11 changes: 11 additions & 0 deletions connector_carepoint/data/ir_cron_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,16 @@
<field name="function">cron_import_address</field>
<field name="args">()</field>
</record>

<record id="ir_cron_connector_carepoint_address" model="ir.cron">
<field name="name">Fetch Updated CarePoint Phones</field>
<field name="interval_number">10</field>
<field name="interval_type">minutes</field>
<field name="numbercall">-1</field>
<field name="doall" eval="False"/>
<field name="model">carepoint.backend</field>
<field name="function">cron_import_phone</field>
<field name="args">()</field>
</record>

</odoo>
4 changes: 2 additions & 2 deletions connector_carepoint/models/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ class CarepointAddressImportMapper(CarepointImportMapper):

@mapping
def zip(self, record):
zip_plus4 = record['zip_plus4'].strip()
_zip = record['zip'].strip()
zip_plus4 = (record['zip_plus4'] or '').strip()
_zip = (record['zip'] or '').strip()
if zip_plus4:
_zip = '%s-%s' % (_zip, zip_plus4)
return {'zip': _zip}
Expand Down
16 changes: 12 additions & 4 deletions connector_carepoint/models/address_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@

from ..connector import add_checkpoint


_logger = logging.getLogger(__name__)

try:
from carepoint.models.address_mixin import EnumAddressType
except ImportError:
_logger.warning('Cannot import EnumPhoneType from carepoint')


class CarepointAddressAbstract(models.AbstractModel):
""" Adds the ``one2many`` relation to the Carepoint bindings
Expand Down Expand Up @@ -231,12 +235,16 @@ def addr_id(self, binding):
return {'addr_id': rec_id}

@mapping
def static_defaults(self, binding):
return {
def static_defaults(self, binding, addr_type='business'):
res = {
'priority': 1,
'addr_type_cn': 1,
'app_flags': 0,
}
try:
res['addr_type_cn'] = EnumAddressType[addr_type].value
except KeyError:
pass
return res


@carepoint
Expand Down
5 changes: 5 additions & 0 deletions connector_carepoint/models/address_patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ class CarepointAddressPatientExportMapper(
):
_model_name = 'carepoint.carepoint.address.patient'

@mapping
def static_defaults(self, binding):
sup = super(CarepointAddressPatientExportMapper, self)
return sup.static_defaults(binding, 'home')

@mapping
def pat_id(self, binding):
binder = self.binder_for('carepoint.medical.patient')
Expand Down
31 changes: 26 additions & 5 deletions connector_carepoint/models/carepoint_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class CarepointBackend(models.Model):
import_prescriptions_from_date = fields.Datetime()
import_sales_from_date = fields.Datetime()
import_addresses_from_date = fields.Datetime()
import_phones_from_date = fields.Datetime()
import_pickings_from_date = fields.Datetime()
import_invoices_from_date = fields.Datetime()
company_id = fields.Many2one(
Expand Down Expand Up @@ -218,7 +219,7 @@ def _check_default_for_company(self):
))

@api.model
def __get_session(self):
def __get_model_session(self):
return ConnectorSession(
self.env.cr, self.env.uid, context=self.env.context
)
Expand All @@ -240,7 +241,7 @@ def check_carepoint_structure(self):

@api.multi
def synchronize_metadata(self):
session = self.__get_session()
session = self.__get_model_session()
for backend in self:
for model in ('carepoint.carepoint.store',
# 'carepoint.res.users',
Expand All @@ -253,15 +254,15 @@ def synchronize_metadata(self):

@api.multi
def _import_all(self, model):
session = self.__get_session()
session = self.__get_model_session()
for backend in self:
backend.check_carepoint_structure()
import_batch.delay(session, model, backend.id)

@api.multi
def _import_from_date(self, model, from_date_field,
chg_date_field='chg_date'):
session = self.__get_session()
session = self.__get_model_session()
import_start_time = datetime.now()
for backend in self:
backend.check_carepoint_structure()
Expand Down Expand Up @@ -290,7 +291,7 @@ def _import_from_date(self, model, from_date_field,
@api.model
def resync_all(self, binding_model):
""" Resync all bindings for model """
session = self.__get_session()
session = self.__get_model_session()
for record_id in self.env[binding_model].search([]):
for binding_id in record_id.carepoint_bind_ids:
import_record.delay(session,
Expand All @@ -300,6 +301,17 @@ def resync_all(self, binding_model):
force=True,
)

@api.model
def force_sync(self, binding_model, remote_pk, backend_id):
""" Force sycronization based on model and primary key """
session = self.__get_model_session()
import_record.delay(session,
binding_model,
backend_id,
remote_pk,
force=True,
)

@api.multi
def import_carepoint_item(self):
self._import_from_date('carepoint.carepoint.item',
Expand Down Expand Up @@ -365,6 +377,15 @@ def import_address(self):
def cron_import_address(self):
self.search([]).import_address()

@api.multi
def import_phone(self):
self._import_from_date('carepoint.carepoint.phone',
'import_phones_from_date')

@api.model
def cron_import_phone(self):
self.search([]).import_phone()

@api.multi
def import_fdb(self):
# self._import_all('carepoint.fdb.img.mfg')
Expand Down
9 changes: 6 additions & 3 deletions connector_carepoint/models/medical_patient_disease.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class MedicalPatientDiseaseImportMapper(PartnerImportMapper):

@mapping
def pathology_id(self, record):
if not record['icd9']:
return
pathology_id = self.env['medical.pathology'].search([
('code', '=', record['icd9'].strip()),
('code_type_id.name', '=ilike', 'ICD9%'),
Expand Down Expand Up @@ -129,9 +131,10 @@ def _import_dependencies(self):
'carepoint.medical.patient')
self._import_dependency(record['caring_md_id'],
'carepoint.medical.physician')
pathology = self.unit_for(MedicalPathologyUnit,
'carepoint.medical.pathology')
pathology._import_by_code(record['icd9'].strip())
if record['icd9']:
pathology = self.unit_for(MedicalPathologyUnit,
'carepoint.medical.pathology')
pathology._import_by_code(record['icd9'].strip())

def _create(self, data): # pragma: no cover
binding = super(MedicalPatientDiseaseImporter, self)._create(data)
Expand Down
13 changes: 8 additions & 5 deletions connector_carepoint/models/medical_prescription_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from openerp.addons.connector.unit.mapper import (mapping,
ExportMapper,
none,
convert,
)
from ..unit.backend_adapter import CarepointCRUDAdapter
from ..unit.mapper import CarepointImportMapper
Expand Down Expand Up @@ -67,7 +68,9 @@ class MedicalPrescriptionOrderBatchImporter(DelayedBatchImporter):
class MedicalPrescriptionOrderImportMapper(CarepointImportMapper):
_model_name = 'carepoint.medical.prescription.order'

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

@mapping
def patient_id(self, record):
Expand All @@ -84,8 +87,8 @@ def physician_id(self, record):
@mapping
def partner_id(self, record):
binder = self.binder_for('carepoint.carepoint.store')
pharmacy_id = binder.to_odoo(record['store_id'])
return {'partner_id': pharmacy_id}
store = binder.to_odoo(record['store_id'], browse=True)
return {'partner_id': store.pharmacy_id.id}

@mapping
def carepoint_id(self, record):
Expand All @@ -111,8 +114,8 @@ class MedicalPrescriptionOrderExportMapper(ExportMapper):
_model_name = 'carepoint.medical.prescription.order'

direct = [
(none('date_start_treatment'), 'start_date'),
(none('date_stop_treatment'), 'expire_date'),
(convert('date_prescription', fields.Datetime.from_string),
'start_date'),
(none('qty'), 'written_qty'),
(none('frequency'), 'freq_of_admin'),
(none('quantity'), 'units_per_dose'),
Expand Down
16 changes: 10 additions & 6 deletions connector_carepoint/models/medical_prescription_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
only_create,
changed_by,
ExportMapper,
convert,
)
from ..unit.backend_adapter import CarepointCRUDAdapter
from ..unit.mapper import CarepointImportMapper
from ..unit.mapper import CarepointImportMapper, add_to
from ..backend import carepoint
from ..unit.import_synchronizer import (DelayedBatchImporter,
CarepointImporter,
Expand Down Expand Up @@ -118,7 +119,7 @@ class MedicalPrescriptionOrderLineImportMapper(CarepointImportMapper):
('written_qty', 'qty'),
('freq_of_admin', 'frequency'),
('units_entered', 'quantity'),
('refills_left', 'refill_qty_remain'),
(add_to('refills_left', -1), 'refill_qty_remain'),
('refills_orig', 'refill_qty_original')
]

Expand Down Expand Up @@ -239,14 +240,17 @@ class MedicalPrescriptionOrderLineExportMapper(ExportMapper):
_model_name = 'carepoint.rx.ord.ln'

direct = [
('date_start_treatment', 'start_date'),
('date_stop_treatment', 'expire_date'),
('date_stop_treatment', 'expire_date'),
(convert('date_start_treatment', fields.Datetime.from_string),
'start_date'),
(convert('date_stop_treatment', fields.Datetime.from_string),
'expire_date'),
(convert('date_stop_treatment', fields.Datetime.from_string),
'expire_date'),
('qty', 'written_qty'),
('frequency', 'freq_of_admin'),
('quantity', 'units_entered'),
('refill_qty_original', 'refills_orig'),
('refill_qty_remain', 'refills_left'),
(add_to('refill_qty_remain', 1), 'refills_left'),
]

@mapping
Expand Down
Loading

0 comments on commit e6add7e

Please sign in to comment.