def transfer_clinic_chw(self, message, receiver, code, quantity): """ Transfer Drug from Clinic to CHW or CHW to Clinic DIST @mdiallo #001 10""" sender = StoreProvider.cls().objects.get(id=message.persistant_connection.reporter.id) receiver = StoreProvider.cls().objects.get(alias=receiver.lower()) item = Item.by_code(code) if item == None or sender == None or receiver == None: message.respond(_(u"Distribution request failed. Either Item ID or CHW alias is wrong.")) return True try: return self.do_transfer_drug(message, sender, receiver, item, quantity) except ItemNotInStore: message.respond(_(u"Distribution request failed. You do not have %(med)s") % {"med": item}) return True except NotEnoughItemInStock: message.respond( _( u"Distribution request failed. You can't transfer %(q)s %(it)s to %(rec)s because you only have %(stk)s." ) % { "q": quantity, "it": item.name, "rec": receiver.display_full(), "stk": StockItem.objects.get(peer=sender, item=item).quantity, } ) return True
def dispense_drug_patient (self, message, first, last, gender, age, code, quantity): age = Patient.age_from_str(age) gender = Patient.SEXE_MALE if gender.upper() == 'M' else Patient.SEXE_FEMALE receiver= Patient(first_name=first, last_name=last, sexe=gender,age=age) receiver.save() sender = StoreProvider.cls().by_mobile(message.peer) item = Item.by_code(code) if item == None or sender == None or receiver == None: message.respond(_(u"Dispense request failed. Either Item ID or Patient datas are wrong.")) return True try: log = transfer_item(sender=sender, receiver=receiver, item=item, quantity=int(quantity)) except ItemNotInStore: message.respond(_(u"Dispense request failed. You do not have %(med)s") % {'med': item}) return True except NotEnoughItemInStock: message.respond(_(u"Dispense request failed. You can't dispense %(q)s %(it)s to %(rec)s because you only have %(stk)s.") % {'q': quantity, 'it': item.name, 'rec': receiver.display_full(), 'stk': StockItem.objects.get(peer=sender, item=item).quantity}) return True message.respond("CONFIRMATION #%(d)s-%(sid)s-%(rid)s-%(lid)s You have dispensed %(quantity)s %(item)s to %(receiver)s. If not correct please reply: CANCEL %(lid)s" % { 'quantity': quantity, 'item': item.name, 'receiver': receiver.display_full(), 'd': log.date.strftime("%d%m%y"), 'sid': sender.id, 'rid': receiver.id, 'lid': log.id }) return True
def add_stock (self, message, code, quantity, note): ''' Add stock for item. Used by main drug distribution point''' sender = StoreProvider.cls().by_mobile(message.peer) receiver = sender #receiver = StoreProvider.cls().by_alias(receiver) item = Item.by_code(code) if item == None or sender == None or receiver == None: message.respond(_(u"Distribution request failed. Either Item ID or CHW alias is wrong.")) return True try: log = add_stock_for_item(receiver=receiver, item=item, quantity=int(quantity)) message.respond("CONFIRMATION #%(d)s-%(sid)s-%(lid)s You have added %(quantity)s %(item)s to your stock. If not correct please reply: CANCEL %(lid)s" % { 'quantity': quantity, 'item': item.name, 'receiver': receiver.display_full(), 'd': log.date.strftime("%d%m%y"), 'sid': sender.id, 'rid': receiver.id, 'lid': log.id }) except: pass return True
def bulk_transfer_clinic_chw (self, message, receiver, sku_quantities): ''' Transfer Multiple Drugs from Clinic to CHW CDIST @mdiallo #001 10 #004 45 #007 32''' sender = StoreProvider.cls().by_mobile(message.peer) receiver = StoreProvider.cls().by_alias(receiver) if sku_quantities == None or sender == None or receiver == None: message.respond(_(u"Distribution request failed. Either Item IDs or CHW alias is wrong.")) return True try: sq = self.parse_sku_quantities(sku_quantities) except MalformedRequest: message.respond(_(u"Distribution failed. Syntax error in drugs/quantities statement.")) return True success = [] failures= [] for code in sq.itervalues(): try: #print u"%(q)s %(c)s" % {'q':code['quantity'], 'c':code['item']} self.do_transfer_drug(message, sender, receiver, code['item'], code['quantity']) success.append(code) except (NotEnoughItemInStock, ItemNotInStore, Exception): failures.append(code) continue if failures.__len__() == 0: message.respond(_(u"SUMMARY: Multiple Drugs Distribution went through successfuly.")) return True if success.__len__() == 0: message.respond(_(u"SUMMARY: complete FAILURE. Multiple Drugs Distribution went wrong on all items.")) return True # some failed, some went trough details = u"" for fail in failures: details += u"%s, " % fail['item'].name details = details[:-2] message.respond(_(u"SUMMARY: Some items couldn't be transfered: %(detail)s") % {'detail': details}) return True
def transfer_clinic_chw (self, message, receiver, code, quantity): ''' Transfer Drug from Clinic to CHW or CHW to Clinic DIST @mdiallo #001 10''' sender = StoreProvider.cls().by_mobile(message.peer) receiver = StoreProvider.cls().by_alias(receiver) item = Item.by_code(code) if item == None or sender == None or receiver == None: message.respond(_(u"Distribution request failed. Either Item ID or CHW alias is wrong.")) return True try: return self.do_transfer_drug(message, sender, receiver, item, quantity) except ItemNotInStore: message.respond(_(u"Distribution request failed. You do not have %(med)s") % {'med': item}) return True except NotEnoughItemInStock: message.respond(_(u"Distribution request failed. You can't transfer %(q)s %(it)s to %(rec)s because you only have %(stk)s.") % {'q': quantity, 'it': item.name, 'rec': receiver.display_full(), 'stk': StockItem.objects.get(peer=sender, item=item).quantity}) return True
def bulk_transfer_clinic_chw(self, message, receiver, sku_quantities): """ Transfer Multiple Drugs from Clinic to CHW CDIST @mdiallo #001 10 #004 45 #007 32""" sender = StoreProvider.cls().objects.get(id=message.persistant_connection.reporter.id) receiver = StoreProvider.cls().objects.get(alias=receiver.lower()) if sku_quantities == None or sender == None or receiver == None: message.respond(_(u"Distribution request failed. Either Item IDs or CHW alias is wrong.")) return True try: sq = self.parse_sku_quantities(sku_quantities) except MalformedRequest: message.respond(_(u"Distribution failed. Syntax error in drugs/quantities statement.")) return True success = [] failures = [] for code in sq.itervalues(): try: self.do_transfer_drug(message, sender, receiver, code["item"], code["quantity"]) success.append(code) except (NotEnoughItemInStock, ItemNotInStore, Exception): failures.append(code) continue if failures.__len__() == 0: message.respond(_(u"SUMMARY: Multiple Drugs Distribution went through successfuly.")) return True if success.__len__() == 0: message.respond(_(u"SUMMARY: complete FAILURE. Multiple Drugs Distribution went wrong on all items.")) return True # some failed, some went trough details = u"" for fail in failures: details += u"%s, " % fail["item"].name details = details[:-2] message.respond(_(u"SUMMARY: Some items couldn't be transfered: %(detail)s") % {"detail": details}) return True
def add_stock(self, message, code, quantity, note): """ Add stock for item. Used by main drug distribution point""" sender = StoreProvider.cls().objects.get(id=message.persistant_connection.reporter.id) # only PHA can add drugs try: no_pha = not sender.direct().role == Role.objects.get(code="pha") except: no_pha = True if no_pha: message.respond(_(u"Addition request failed. Only PHA can perform such action.")) return True receiver = sender item = Item.by_code(code) if item == None or sender == None or receiver == None: message.respond(_(u"Addition request failed. Either Item ID or CHW alias is wrong.")) return True try: log = add_stock_for_item(receiver=receiver, item=item, quantity=int(quantity)) message.respond( "CONFIRMATION #%(d)s-%(sid)s-%(lid)s You have added %(quantity)s %(item)s to your stock. If not correct please reply: CANCEL %(lid)s" % { "quantity": quantity, "item": item.name, "receiver": receiver.display_full(), "d": log.date.strftime("%d%m%y"), "sid": sender.id, "rid": receiver.id, "lid": log.id, } ) except: pass return True
if self.alias: return self.alias if self.connection(): return self.connection().identity else: return str(self.id) def display_full(self): if not self.location or self.role != Role.objects.get(code='pha'): return self.display_name() return _(u"%(n)s at %(p)s") % {'n': self.display_name(), 'p': self.location.name} def __unicode__(self): return self.display_name() StoreProvider.set_class(Provider, 'provider') class Patient(StoreProvider): class Meta: app_label = "drugtrack" SEXE_MALE = 1 SEXE_FEMALE = 2 SEXE_CHOICES = ( (SEXE_MALE, _('Male')), (SEXE_FEMALE, _('Female')), ) first_name = models.CharField(max_length=32, null=True, blank=True)
def request_self_stock (self, message): ''' Get stock status for a store STOCK''' provider = StoreProvider.cls().by_mobile(message.peer) return self.stock_for(message, provider)
def request_stock (self, message, target): ''' Get stock status for someone STOCK @mdiallo''' provider = StoreProvider.cls().by_alias(target) return self.stock_for(message, provider)