From 0a595cdbb35e56b2f8a3cfc6ae39b9ff851772c7 Mon Sep 17 00:00:00 2001 From: Peter Alabaster Date: Tue, 13 Mar 2018 23:57:55 +0000 Subject: [PATCH 01/11] [META][WIP] Batch allocation prototype/work in progress --- .../src/js/form_tree_selected_widget.js | 25 + nh_clinical/views/static_resources.xml | 1 + nh_clinical/wizard/user_allocation.py | 603 ++++++++---------- nh_clinical/wizard/user_allocation_view.xml | 18 +- 4 files changed, 293 insertions(+), 354 deletions(-) create mode 100644 nh_clinical/static/src/js/form_tree_selected_widget.js diff --git a/nh_clinical/static/src/js/form_tree_selected_widget.js b/nh_clinical/static/src/js/form_tree_selected_widget.js new file mode 100644 index 00000000..81c33073 --- /dev/null +++ b/nh_clinical/static/src/js/form_tree_selected_widget.js @@ -0,0 +1,25 @@ +openerp.nh_clinical = function(instance, local) { + + local.FormTreeSelectedWidget = instance.web.form.FieldBoolean.extend({ + start: function() { + // TODO: Fix this widget so that it actually runs when it's used + // i.e on the 'selected' field on the nh allocation wizard. + // I'm not entirely sure how to declare a field widget so may need to look at other extensions + // where it has been done on Odoo 8.0 + + + // TODO: Add an onclick handler for each widget which makes an + // rpc call to `nh.clinical.allocating`.write() to write the value + // of the 'selected' field for the record whos row the widget is sat on. + + // This means that when we call batch_allocate, the rows which need to be + // actioned will easily be visible + this._super(); + }, + }); + + instance.web.form.widgets.add('form_tree_selected_widget', 'instance.nh_clinical.FormTreeSelectedWidget'); + + + +} diff --git a/nh_clinical/views/static_resources.xml b/nh_clinical/views/static_resources.xml index 8ffe084e..bbd75d4d 100644 --- a/nh_clinical/views/static_resources.xml +++ b/nh_clinical/views/static_resources.xml @@ -3,6 +3,7 @@ diff --git a/nh_clinical/wizard/user_allocation.py b/nh_clinical/wizard/user_allocation.py index c4edb271..6b2ae5fa 100644 --- a/nh_clinical/wizard/user_allocation.py +++ b/nh_clinical/wizard/user_allocation.py @@ -1,6 +1,6 @@ # Part of NHClinical. See LICENSE file for full copyright and licensing details # -*- coding: utf-8 -*- -from openerp.osv import osv, fields +from openerp import models, fields, api def list_diff(a, b): @@ -13,85 +13,63 @@ def list_intersect(a, b): return [aa for aa in a if aa in b] -class AllocationWizards(osv.AbstractModel): +class AllocationWizards(models.AbstractModel): _name = 'nh.clinical.allocation' - _columns = { - 'create_uid': fields.many2one( - 'res.users', - 'User Executing the Wizard'), - 'create_date': fields.datetime('Create Date') - } + create_uid = fields.Many2one( + 'res.users', + 'User Executing the Wizard') + create_date = fields.Datetime('Create Date') - def responsibility_allocation_activity(self, cr, uid, user_id, - location_ids, context=None): + def responsibility_allocation_activity(self, user_id, location_ids): """ Create and complete a responsibility allocation activity for location :param location_ids: Ward ID - :param context: A context :return: True """ - activity_pool = self.pool['nh.activity'] - respallocation_pool = self.pool[ - 'nh.clinical.user.responsibility.allocation' - ] - activity_id = respallocation_pool.create_activity( - cr, uid, {}, { + NhActivity = self.env['nh.activity'] + NhClinicalUserResponsibilityAllocation = self.env['nh.clinical.user.responsibility.allocation'] + activity_id = NhClinicalUserResponsibilityAllocation.create_activity({}, { 'responsible_user_id': user_id, 'location_ids': [[6, 0, location_ids]] - }, context=context) - activity_pool.complete(cr, uid, activity_id, context=context) + }) + NhActivity.complete(activity_id) return True - def unfollow_patients_in_locations(self, cr, uid, location_ids, - context=None): + def unfollow_patients_in_locations(self, location_ids): """ Unfollow any patients in the locations currently being reallocated :param location_ids: List of location ids - :param context: context for odoo :return: True """ - activity_pool = self.pool['nh.activity'] - patient_pool = self.pool['nh.clinical.patient'] - unfollow_pool = self.pool['nh.clinical.patient.unfollow'] - patient_ids = patient_pool.search( - cr, uid, [['current_location_id', 'in', location_ids]], - context=context) + NhActivity = self.env['nh.activity'] + NhClinicalPatient = self.env['nh.clinical.patient'] + NhClinicalPatientUnfollow = self.env['nh.clinical.patient.unfollow'] + patient_ids = NhClinicalPatient.search([['current_location_id', 'in', location_ids]]).ids if patient_ids: - unfollow_activity_id = unfollow_pool.create_activity(cr, uid, {}, { - 'patient_ids': [[6, 0, patient_ids]]}, context=context) - activity_pool.complete(cr, uid, unfollow_activity_id, - context=context) + unfollow_activity = NhClinicalPatientUnfollow.create_activity({}, { + 'patient_ids': [[6, 0, patient_ids]]}) + NhActivity.complete(unfollow_activity) return True - def complete(self, cr, uid, ids, context=None): - if isinstance(ids, list): - ids = ids[0] - if not isinstance(ids, int): - raise ValueError('Invalid ID passed to complete') - allocating_pool = self.pool['nh.clinical.allocating'] - wizard = self.browse(cr, uid, ids, context=context) + def complete(self): + NhClinicalAllocating = self.env['nh.clinical.allocating'] allocation = {u.id: [l.id for l in u.location_ids] for u in - wizard.user_ids} - for allocating in allocating_pool.browse( - cr, uid, [a.id for a in wizard.allocating_ids], - context=context): + self.user_ids} + for allocating in self.allocating_ids: if allocating.nurse_id: - allocation[allocating.nurse_id.id].append( - allocating.location_id.id) + allocation[allocating.nurse_id.id].append(allocating.location_id.id) if allocating.nurse_id.id == uid: - allocation[allocating.nurse_id.id].append( - wizard.ward_id.id) + allocation[allocating.nurse_id.id].append(self.ward_id.id) for hca in allocating.hca_ids: allocation[hca.id].append(allocating.location_id.id) for key, value in allocation.iteritems(): - self.responsibility_allocation_activity(cr, uid, key, value, - context=context) + self.responsibility_allocation_activity(key, value) return {'type': 'ir.actions.act_window_close'} -class StaffAllocationWizard(osv.TransientModel): +class StaffAllocationWizard(models.TransientModel): _name = 'nh.clinical.staff.allocation' _inherit = 'nh.clinical.allocation' _rec_name = 'create_uid' @@ -99,65 +77,61 @@ class StaffAllocationWizard(osv.TransientModel): _stages = [['wards', 'My Ward'], ['review', 'De-allocate'], ['users', 'Roll Call'], ['allocation', 'Allocation']] - _columns = { - 'stage': fields.selection(_stages, string='Stage'), - 'ward_id': fields.many2one('nh.clinical.location', - string='Ward', - domain=[['usage', '=', 'ward']]), - 'location_ids': fields.many2many('nh.clinical.location', - 'alloc_loc_rel', 'allocation_id', - 'location_id', - string='Locations'), - 'user_ids': fields.many2many('res.users', 'alloc_user_rel', - 'allocation_id', 'user_id', - string='Users', - domain=[ - ['groups_id.name', 'in', - ['NH Clinical HCA Group', - 'NH Clinical Nurse Group']] - ]), - 'allocating_ids': fields.many2many('nh.clinical.allocating', - 'alloc_allocating_rel', - 'allocation_id', - 'allocating_id', - string='Allocating Locations') - } + + stage = fields.Selection(_stages, string='Stage') + ward_id = fields.Many2one('nh.clinical.location', + string='Ward', + domain=[['usage', '=', 'ward']]) + location_ids = fields.Many2many('nh.clinical.location', + 'alloc_loc_rel', 'allocation_id', + 'location_id', + string='Locations') + user_ids = fields.Many2many('res.users', 'alloc_user_rel', + 'allocation_id', 'user_id', + string='Users', + domain=[ + ['groups_id.name', 'in', + ['NH Clinical HCA Group', + 'NH Clinical Nurse Group']] + ]) + allocating_ids = fields.Many2many('nh.clinical.allocating', + 'alloc_allocating_rel', + 'allocation_id', + 'allocating_id', + string='Allocating Locations') + _defaults = { 'stage': 'wards' } - def submit_ward(self, cr, uid, ids, context=None): - if isinstance(ids, list): - ids = ids[0] - if not isinstance(ids, int): - raise ValueError('Invalid ID passed to submit_wards') - wiz = self.browse(cr, uid, ids, context=context) - ward_ids = [wiz.ward_id.id] - location_pool = self.pool['nh.clinical.location'] - location_ids = location_pool.search( - cr, uid, [['id', 'child_of', ward_ids]], context=context) - self.write(cr, uid, ids, - {'stage': 'review', 'location_ids': [[6, 0, location_ids]]}, - context=context) + @api.multi + def batch_allocate(self): + # TODO: Make this function return a 'special' action which performs + # a batch allocation. + selected = [(x.id, x.selected) for x in self.allocating_ids] + import pdb;pdb.set_trace() + + @api.multi + def submit_ward(self): + ward_ids = [self.ward_id.id] + NhClinicalLocation = self.env['nh.clinical.location'] + location_ids = NhClinicalLocation.search([['id', 'child_of', ward_ids]]).ids + self.write({'stage': 'review', 'location_ids': [[6, 0, location_ids]]}) return { 'type': 'ir.actions.act_window', 'name': 'Nursing Shift Change', 'res_model': 'nh.clinical.staff.allocation', - 'res_id': ids, + 'res_id': self.id, 'view_mode': 'form', 'target': 'new', } - def deallocate(self, cr, uid, ids, context=None): - if isinstance(ids, list): - ids = ids[0] - if not isinstance(ids, int): - raise ValueError('Invalid ID passed to deallocate') - user_pool = self.pool['res.users'] - allocating_pool = self.pool['nh.clinical.allocating'] - wiz = self.browse(cr, uid, ids, context=context) - location_ids = [location.id for location in wiz.location_ids] - user_ids = user_pool.search(cr, uid, [ + @api.multi + def deallocate(self): + ResUsers = self.env['res.users'] + NhClinicalAllocating = self.env['nh.clinical.allocating'] + location_ids = [location.id for location in self.location_ids] + user_ids = ResUsers.search([ ['groups_id.name', 'in', [ 'NH Clinical HCA Group', @@ -165,52 +139,42 @@ def deallocate(self, cr, uid, ids, context=None): 'NH Clinical Shift Coordinator Group' ]], ['location_ids', 'in', location_ids] - ], context=context) + ]) for location_id in location_ids: - user_pool.write(cr, uid, user_ids, - {'location_ids': [[3, location_id]]}, - context=context) - self.responsibility_allocation_activity(cr, uid, uid, [wiz.ward_id.id], - context=context) - self.unfollow_patients_in_locations(cr, uid, location_ids, - context=context) + user_ids.write({'location_ids': [[3, location_id]]}) + self.responsibility_allocation_activity(self.env.uid, [self.ward_id.id]) + self.unfollow_patients_in_locations(location_ids) allocating_ids = [] - for location in wiz.location_ids: + for location in self.location_ids: if location.usage == 'bed': - allocating_ids.append( - allocating_pool.create(cr, uid, { - 'location_id': location.id}, context=context)) - self.write(cr, uid, ids, - { - 'allocating_ids': [[6, 0, allocating_ids]], - 'stage': 'users' - }, context=context) + allocating_ids.append(NhClinicalAllocating.create({'location_id': location.id}).id) + self.write({ + 'allocating_ids': [[6, 0, allocating_ids]], + 'stage': 'users' + }) return { 'type': 'ir.actions.act_window', 'name': 'Nursing Shift Change', 'res_model': 'nh.clinical.staff.allocation', - 'res_id': ids, + 'res_id': self.id, 'view_mode': 'form', 'target': 'new', } - def submit_users(self, cr, uid, ids, context=None): - if isinstance(ids, list): - ids = ids[0] - if not isinstance(ids, int): - raise ValueError('Invalid ID passed to submit_users') - self.write(cr, uid, ids, {'stage': 'allocation'}, context=context) + @api.multi + def submit_users(self): + self.write({'stage': 'allocation'}) return { 'type': 'ir.actions.act_window', 'name': 'Nursing Shift Change', 'res_model': 'nh.clinical.staff.allocation', - 'res_id': ids, + 'res_id': self.id, 'view_mode': 'form', 'target': 'new', } -class StaffReallocationWizard(osv.TransientModel): +class StaffReallocationWizard(models.TransientModel): _name = 'nh.clinical.staff.reallocation' _inherit = 'nh.clinical.allocation' _rec_name = 'create_uid' @@ -218,42 +182,36 @@ class StaffReallocationWizard(osv.TransientModel): _nursing_groups = ['NH Clinical Nurse Group', 'NH Clinical HCA Group'] _stages = [['users', 'Current Roll Call'], ['allocation', 'Allocation']] - def _get_default_ward(self, cr, uid, context=None): - location_pool = self.pool['nh.clinical.location'] - ward_ids = location_pool.search( - cr, uid, [['usage', '=', 'ward'], ['user_ids', 'in', [uid]]], - context=context) + def _get_default_ward(self): + NhClinicalLocation = self.env['nh.clinical.location'] + ward_ids = NhClinicalLocation.search([['usage', '=', 'ward'], ['user_ids', 'in', [self.env.uid]]]) if not ward_ids: raise osv.except_osv( 'Shift Management Error!', 'You must be in charge of a ward to do this task!') return ward_ids[0] - def get_users_for_locations(self, cr, uid, location_ids, context=None): - user_pool = self.pool['res.users'] - return user_pool.search( - cr, uid, [['groups_id.name', 'in', self._nursing_groups], - ['location_ids', 'in', location_ids]], context=context) - - def _get_default_users(self, cr, uid, context=None): - location_ids = self._get_default_locations(cr, uid, context=context) - return self.get_users_for_locations(cr, uid, location_ids, - context=context) - - def _get_default_locations(self, cr, uid, context=None): - location_pool = self.pool['nh.clinical.location'] - ward_id = self._get_default_ward(cr, uid, context=context) - location_ids = location_pool.search(cr, uid, - [['id', 'child_of', ward_id]], - context=context) - return location_ids + def get_users_for_locations(self, locations): + ResUsers = self.env['res.users'] + return ResUsers.search([ + ['groups_id.name', 'in', self._nursing_groups], + ['location_ids', 'in', location_ids.ids]] + ) + + def _get_default_users(self): + locations = self._get_default_locations() + return self.get_users_for_locations(locations) + + def _get_default_locations(self): + NhClinicalLocation = self.env['nh.clinical.location'] + ward_id = self._get_default_ward() + locations = NhClinicalLocation.search([['id', 'child_of', ward_id]]) + return locations def _get_default_allocatings(self, cr, uid, context=None): - location_pool = self.pool['nh.clinical.location'] - allocating_pool = self.pool['nh.clinical.allocating'] - location_ids = self._get_default_locations(cr, uid, context=context) - locations = location_pool.browse(cr, uid, location_ids, - context=context) + NhClinicalLocation = self.env['nh.clinical.location'] + NhClinicalAllocating = self.env['nh.clinical.allocating'] + locations = self._get_default_locations() allocating_ids = [] for l in locations: if l.usage != 'bed': @@ -270,36 +228,35 @@ def _get_default_allocatings(self, cr, uid, context=None): if 'NH Clinical Shift Coordinator Group' in groups \ and not nurse_id: nurse_id = u.id - allocating_ids.append(allocating_pool.create(cr, uid, { + allocating_ids.append(NhClinicalAllocating.create({ 'location_id': l.id, 'nurse_id': nurse_id, 'hca_ids': [[6, 0, hca_ids]] - }, context=context)) + }).id) return allocating_ids - _columns = { - 'stage': fields.selection(_stages, string='Stage'), - 'ward_id': fields.many2one('nh.clinical.location', - string='Ward', - domain=[['usage', '=', 'ward']]), - 'location_ids': fields.many2many('nh.clinical.location', - 'realloc_loc_rel', 'reallocation_id', - 'location_id', - string='Locations'), - 'user_ids': fields.many2many('res.users', 'realloc_user_rel', - 'allocation_id', 'user_id', - string='Users', - domain=[ - ['groups_id.name', 'in', - ['NH Clinical HCA Group', - 'NH Clinical Nurse Group']] - ]), - 'allocating_ids': fields.many2many('nh.clinical.allocating', - 'real_allocating_rel', - 'reallocation_id', - 'allocating_id', - string='Allocating Locations') - } + stage = fields.Selection(_stages, string='Stage') + ward_id = fields.Many2one('nh.clinical.location', + string='Ward', + domain=[['usage', '=', 'ward']]) + location_ids = fields.Many2many('nh.clinical.location', + 'realloc_loc_rel', 'reallocation_id', + 'location_id', + string='Locations') + user_ids = fields.Many2many('res.users', 'realloc_user_rel', + 'allocation_id', 'user_id', + string='Users', + domain=[ + ['groups_id.name', 'in', + ['NH Clinical HCA Group', + 'NH Clinical Nurse Group']] + ]) + allocating_ids = fields.Many2many('nh.clinical.allocating', + 'real_allocating_rel', + 'reallocation_id', + 'allocating_id', + string='Allocating Locations') + _defaults = { 'stage': 'users', 'ward_id': _get_default_ward, @@ -308,80 +265,70 @@ def _get_default_allocatings(self, cr, uid, context=None): 'allocating_ids': _get_default_allocatings } - def reallocate(self, cr, uid, ids, context=None): - if isinstance(ids, list): - ids = ids[0] - if not isinstance(ids, int): - raise ValueError('reallocate expected integer') - user_pool = self.pool['res.users'] - wiz = self.read( - cr, uid, ids, ['location_ids', 'user_ids'], context=context) + @api.multi + def batch_allocate(self): + # TODO: Make this function return a 'special' action which performs + # a batch allocation. + selected = [(x.id, x.selected) for x in self.allocating_ids] + import pdb;pdb.set_trace() + + + @api.multi + def reallocate(self): + ResUsers = self.env['res.users'] + wiz = self.read(['location_ids', 'user_ids']) location_ids = wiz.get('location_ids') - loc_user_ids = self.get_users_for_locations( - cr, uid, location_ids, context=context) + loc_user_ids = self.get_users_for_locations(location_ids) user_ids = wiz.get('user_ids') recompute = False for u_id in loc_user_ids: if u_id not in user_ids and u_id != uid: recompute = True - user = user_pool.read( - cr, uid, u_id, ['location_ids'], context=context) + user = u_id.read(['location_ids']) uloc_ids = user.get('location_ids') loc_ids = list_diff(uloc_ids, location_ids) - self.responsibility_allocation_activity( - cr, uid, u_id, loc_ids, context=context) + self.responsibility_allocation_activity(u_id, loc_ids) # Remove patient followers loc_ids = list_intersect(uloc_ids, location_ids) - self.unfollow_patients_in_locations( - cr, uid, loc_ids, context=context) - self.write(cr, uid, ids, {'stage': 'allocation'}, context=context) + self.unfollow_patients_in_locations(loc_ids) + self.write({'stage': 'allocation'}) if recompute: - allocating_ids = self._get_default_allocatings(cr, uid, - context=context) - self.write(cr, uid, ids, - {'allocating_ids': [[6, 0, allocating_ids]]}, - context=context) + allocating_ids = self._get_default_allocatings() + self.write({'allocating_ids': [[6, 0, allocating_ids]]}) return { 'type': 'ir.actions.act_window', 'name': 'Nursing Re-Allocation', 'res_model': 'nh.clinical.staff.reallocation', - 'res_id': ids, + 'res_id': self.id, 'view_mode': 'form', 'target': 'new', } - def complete(self, cr, uid, ids, context=None): - if isinstance(ids, list): - ids = ids[0] - if not isinstance(ids, int): - raise ValueError('Invalid ID passed to complete') - allocating_pool = self.pool['nh.clinical.allocating'] - wizard = self.browse(cr, uid, ids, context=context) + + @api.multi + def complete(self): allocation = { u.id: [l.id for l in u.location_ids if l.id not - in wizard.location_ids.ids] for u in wizard.user_ids} - for allocating in allocating_pool.browse( - cr, uid, [a.id for a in wizard.allocating_ids], - context=context): + in self.location_ids.ids] for u in self.user_ids} + for allocating in self.allocating_ids: if allocating.nurse_id: allocation[allocating.nurse_id.id].append( allocating.location_id.id) if allocating.nurse_id.id == uid: allocation[allocating.nurse_id.id].append( - wizard.ward_id.id) + self.ward_id.id) for hca in allocating.hca_ids: allocation[hca.id].append(allocating.location_id.id) if uid not in allocation: - allocation[uid] = [wizard.ward_id.id] - elif wizard.ward_id.id not in allocation.get(uid): - allocation[uid].append(wizard.ward_id.id) + allocation[uid] = [self.ward_id.id] + elif self.ward_id.id not in allocation.get(uid): + allocation[uid].append(self.ward_id.id) for key, value in allocation.iteritems(): - self.responsibility_allocation_activity(cr, uid, key, value, - context=context) + self.responsibility_allocation_activity(key, value) return {'type': 'ir.actions.act_window_close'} -class doctor_allocation_wizard(osv.TransientModel): +class doctor_allocation_wizard(models.TransientModel): _name = 'nh.clinical.doctor.allocation' _rec_name = 'create_uid' @@ -391,62 +338,53 @@ class doctor_allocation_wizard(osv.TransientModel): 'NH Clinical Consultant Group', 'NH Clinical Registrar Group'] - def _get_default_ward(self, cr, uid, context=None): - location_pool = self.pool['nh.clinical.location'] - ward_ids = location_pool.search(cr, uid, [['usage', '=', 'ward'], - ['user_ids', 'in', [uid]]], - context=context) + def _get_default_ward(self): + NhClinicalLocation = self.env['nh.clinical.location'] + ward_ids = NhClinicalLocation.search([['usage', '=', 'ward'], ['user_ids', 'in', [uid]]]) if not ward_ids: raise osv.except_osv( 'Shift Management Error!', 'You must be in charge of a ward to do this task!') return ward_ids[0] - def _get_default_locations(self, cr, uid, context=None): - location_pool = self.pool['nh.clinical.location'] - ward_ids = location_pool.search( - cr, uid, [['usage', '=', 'ward'], ['user_ids', 'in', [uid]]], - context=context) + def _get_default_locations(self): + NhClinicalLocation = self.env['nh.clinical.location'] + ward_ids = NhClinicalLocation.search([['usage', '=', 'ward'], ['user_ids', 'in', [uid]]]) if not ward_ids: raise osv.except_osv( 'Shift Management Error!', 'You must be in charge of a ward to do this task!') - location_ids = location_pool.search( - cr, uid, [['id', 'child_of', ward_ids[0]]], context=context) + location_ids = NhClinicalLocation.search([['id', 'child_of', ward_ids.ids]]) return location_ids - def _get_current_doctors(self, cr, uid, context=None): - location_pool = self.pool['nh.clinical.location'] - user_pool = self.pool['res.users'] - ward_ids = location_pool.search(cr, uid, [['usage', '=', 'ward'], - ['user_ids', 'in', [uid]]], - context=context) + def _get_current_doctors(self): + NhClinicalLocation = self.env['nh.clinical.location'] + ResUsers = self.env['res.users'] + ward_ids = NhClinicalLocation.search([['usage', '=', 'ward'], ['user_ids', 'in', [uid]]]) if not ward_ids: raise osv.except_osv( 'Shift Management Error!', 'You must be in charge of a ward to do this task!') - doctor_ids = user_pool.search( - cr, uid, [['groups_id.name', 'in', self._doctor_groups], - ['location_ids', 'in', ward_ids]], context=context) + doctor_ids = ResUsers.search([['groups_id.name', 'in', self._doctor_groups], ['location_ids', 'in', ward_ids.ids]]) return doctor_ids - _columns = { - 'create_uid': fields.many2one('res.users', - 'User Executing the Wizard'), - 'create_date': fields.datetime('Create Date'), - 'stage': fields.selection(_stages, string='Stage'), - 'ward_id': fields.many2one('nh.clinical.location', string='Ward', - domain=[['usage', '=', 'ward']]), - 'doctor_ids': fields.many2many('res.users', 'docalloc_doc_rel', - 'allocation_id', 'user_id', - string='Current Doctors'), - 'location_ids': fields.many2many('nh.clinical.location', - 'docalloc_loc_rel', 'allocation_id', - 'location_id', string='Locations'), - 'user_ids': fields.many2many( - 'res.users', 'docalloc_user_rel', 'allocation_id', 'user_id', - string='Users', domain=[['groups_id.name', 'in', _doctor_groups]]) - } + + create_uid = fields.Many2one('res.users', + 'User Executing the Wizard') + create_date = fields.Datetime('Create Date') + stage = fields.Selection(_stages, string='Stage') + ward_id = fields.Many2one('nh.clinical.location', string='Ward', + domain=[['usage', '=', 'ward']]) + doctor_ids = fields.Many2many('res.users', 'docalloc_doc_rel', + 'allocation_id', 'user_id', + string='Current Doctors') + location_ids = fields.Many2many('nh.clinical.location', + 'docalloc_loc_rel', 'allocation_id', + 'location_id', string='Locations') + user_ids = fields.Many2many( + 'res.users', 'docalloc_user_rel', 'allocation_id', 'user_id', + string='Users', domain=[['groups_id.name', 'in', _doctor_groups]]) + _defaults = { 'stage': 'review', 'ward_id': _get_default_ward, @@ -454,105 +392,76 @@ def _get_current_doctors(self, cr, uid, context=None): 'doctor_ids': _get_current_doctors } - def deallocate(self, cr, uid, ids, context=None): - if not isinstance(ids, list): - ids = [ids] - wiz = self.browse(cr, uid, ids[0], context=context) - deallocate_location_ids = \ - [location.id for location in wiz.location_ids] - - user_pool = self.pool['res.users'] - all_doctor_ids = user_pool.search( - cr, uid, [['groups_id.name', 'in', self._doctor_groups]], - context=context - ) + def deallocate(self): + deallocate_location_ids = self.location_ids.ids - for doctor_id in all_doctor_ids: - doctor_current_location_ids = user_pool.read( - cr, uid, doctor_id, fields=['location_ids'] - )['location_ids'] + ResUsers = self.env['res.users'] + all_doctors = ResUsers.search([['groups_id.name', 'in', self._doctor_groups]]) + + for doctor in all_doctors: + doctor_current_location_ids = doctor.location_ids.ids doctor_new_location_ids = \ set(doctor_current_location_ids) - set(deallocate_location_ids) - user_pool.write( - cr, uid, doctor_id, - {'location_ids': [(6, 0, doctor_new_location_ids)]}, - context=context - ) + doctor.write({'location_ids': [(6, 0, doctor_new_location_ids)]}) - self.write(cr, uid, ids, {'stage': 'users'}, context=context) + self.write({'stage': 'users'}) return { 'type': 'ir.actions.act_window', 'name': 'Medical Shift Change', 'res_model': 'nh.clinical.doctor.allocation', - 'res_id': ids[0], + 'res_id': self.id, 'view_mode': 'form', 'target': 'new', } - def submit_users(self, cr, uid, ids, context=None): - if not isinstance(ids, list): - ids = [ids] - wiz = self.browse(cr, uid, ids[0], context=context) - respallocation_pool = self.pool[ + def submit_users(self): + NhClinicalUserResponsibilityAllocation = self.env[ 'nh.clinical.user.responsibility.allocation' ] - activity_pool = self.pool['nh.activity'] - for doctor in wiz.user_ids: - activity_id = respallocation_pool.create_activity( - cr, uid, {}, { - 'responsible_user_id': doctor.id, - 'location_ids': [[6, 0, [wiz.ward_id.id]]] - }, context=context) - activity_pool.complete(cr, uid, activity_id, context=context) + NhActivity = self.env['nh.activity'] + for doctor in self.user_ids: + activity = NhClinicalUserResponsibilityAllocation.create_activity({}, { + 'responsible_user_id': doctor.id, + 'location_ids': [[6, 0, [self.ward_id.id]]] + }) + activity.complete() return {'type': 'ir.actions.act_window_close'} -class allocating_user(osv.TransientModel): +class allocating_user(models.TransientModel): _name = 'nh.clinical.allocating' _rec_name = 'location_id' - _columns = { - 'location_id': fields.many2one('nh.clinical.location', 'Location', - required=1), - 'patient_ids': fields.related('location_id', 'patient_ids', - type='many2many', - relation='nh.clinical.patient', - string='Patient'), - 'nurse_id': fields.many2one( - 'res.users', 'Responsible Nurse', - domain=[['groups_id.name', 'in', ['NH Clinical Nurse Group']]]), - 'hca_ids': fields.many2many( - 'res.users', 'allocating_hca_rel', 'allocating_id', 'hca_id', - string='Responsible HCAs', - domain=[['groups_id.name', 'in', ['NH Clinical HCA Group']]]), - 'nurse_name': fields.related('nurse_id', 'name', type='char', size=100, - string='Responsible Nurse') - } - - def fields_view_get(self, cr, uid, view_id=None, view_type='form', - context=None, toolbar=False, submenu=False): - res = super(allocating_user, self).fields_view_get(cr, uid, view_id, - view_type, context, + selected = fields.Boolean(string="Batch") + location_id = fields.Many2one('nh.clinical.location', 'Location', + required=1) + patient_ids = fields.Many2many(related='location_id.patient_ids', + relation='nh.clinical.patient', + string='Patient') + nurse_id = fields.Many2one( + 'res.users', 'Responsible Nurse', + domain=[['groups_id.name', 'in', ['NH Clinical Nurse Group']]]) + hca_ids = fields.Many2many( + 'res.users', 'allocating_hca_rel', 'allocating_id', 'hca_id', + string='Responsible HCAs', + domain=[['groups_id.name', 'in', ['NH Clinical HCA Group']]]) + nurse_name = fields.Char(related='nurse_id.name', size=100, string='Responsible Nurse') + + def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False): + res = super(allocating_user, self).fields_view_get(view_id, view_type, toolbar, submenu) - allocation_pool = self.pool['nh.clinical.staff.allocation'] - reallocation_pool = self.pool['nh.clinical.staff.reallocation'] - al_id = allocation_pool.search(cr, uid, [['create_uid', '=', uid]], - order='id desc') - real_id = reallocation_pool.search(cr, uid, [['create_uid', '=', uid]], - order='id desc') - allocation = True if al_id else False - if al_id and real_id: - al = allocation_pool.browse(cr, uid, al_id[0], context=context) - real = reallocation_pool.browse(cr, uid, real_id[0], - context=context) + NhClinicalStaffAllocation = self.env['nh.clinical.staff.allocation'] + NhClinicalStaffReallocation = self.env['nh.clinical.staff.reallocation'] + al = NhClinicalStaffAllocation.search([['create_uid', '=', self.env.uid]], order='id desc') + real = NhClinicalStaffReallocation.search([['create_uid', '=', self.env.uid]], order='id desc') + allocation = True if al else False + if al and real: allocation = True if al.create_date > real.create_date else False - if not (al_id or real_id) or view_type != 'form': + if not (al or real) or view_type != 'form': return res else: if allocation: - allocation = allocation_pool.browse(cr, uid, al_id[0], - context=context) - user_ids = [u.id for u in allocation.user_ids] + user_ids = [u.id for u in al.user_ids] res['fields']['nurse_id']['domain'] = [ ['id', 'in', user_ids], ['groups_id.name', 'in', ['NH Clinical Nurse Group']] @@ -562,9 +471,7 @@ def fields_view_get(self, cr, uid, view_id=None, view_type='form', ['groups_id.name', 'in', ['NH Clinical HCA Group']] ] else: - reallocation = reallocation_pool.browse(cr, uid, real_id[0], - context=context) - user_ids = [u.id for u in reallocation.user_ids] + user_ids = [u.id for u in real.user_ids] res['fields']['nurse_id']['domain'] = [ ['id', 'in', user_ids], ['groups_id.name', 'in', ['NH Clinical Nurse Group']] @@ -575,28 +482,26 @@ def fields_view_get(self, cr, uid, view_id=None, view_type='form', ] return res - -class user_allocation_wizard(osv.TransientModel): +class user_allocation_wizard(models.TransientModel): _name = 'nh.clinical.user.allocation' _stages = [['wards', 'Select Wards'], ['users', 'Select Users'], ['allocation', 'Allocation']] - _columns = { - 'create_uid': fields.many2one('res.users', - 'User Executing the Wizard'), - 'stage': fields.selection(_stages, string='Stage'), - 'ward_ids': fields.many2many('nh.clinical.location', - 'allocation_ward_rel', 'allocation_id', - 'location_id', string='Wards', - domain=[['usage', '=', 'ward']]), - 'user_ids': fields.many2many('res.users', 'allocation_user_rel', - 'allocation_id', 'user_id', - string='Users'), - 'allocating_user_ids': fields.many2many( - 'nh.clinical.allocating', 'allocating_allocation_rel', - 'allocation_id', 'allocating_user_id', string='Allocating Users') - } + create_uid = fields.Many2one('res.users', + 'User Executing the Wizard') + stage = fields.Selection(_stages, string='Stage') + ward_ids = fields.Many2many('nh.clinical.location', + 'allocation_ward_rel', 'allocation_id', + 'location_id', string='Wards', + domain=[['usage', '=', 'ward']]) + user_ids = fields.Many2many('res.users', 'allocation_user_rel', + 'allocation_id', 'user_id', + string='Users') + allocating_user_ids = fields.Many2many( + 'nh.clinical.allocating', 'allocating_allocation_rel', + 'allocation_id', 'allocating_user_id', string='Allocating Users') + _defaults = { 'stage': 'users' } diff --git a/nh_clinical/wizard/user_allocation_view.xml b/nh_clinical/wizard/user_allocation_view.xml index a4f2cc1c..086659cd 100644 --- a/nh_clinical/wizard/user_allocation_view.xml +++ b/nh_clinical/wizard/user_allocation_view.xml @@ -5,7 +5,7 @@ User Allocation Wizard Form nh.clinical.user.allocation -
+ Allocating Form nh.clinical.allocating - + @@ -83,7 +83,7 @@ Staff Allocation Wizard Form nh.clinical.staff.allocation - +
@@ -118,6 +120,8 @@ + + @@ -138,13 +142,15 @@ Staff Re-Allocation Wizard Form nh.clinical.staff.reallocation - +
@@ -160,6 +166,8 @@ + + @@ -183,7 +191,7 @@ Doctor Allocation Wizard Form nh.clinical.doctor.allocation - +
+ + @@ -151,9 +155,13 @@ attrs="{'invisible': [['stage','!=','allocation']]}"/>