Esempio n. 1
0
 def _to_fields(transaction):
     ret = {'requisition_status': RequisitionStatus.by_action_type(transaction.action_config.action_type)}
     if transaction.action_config.action_type == RequisitionActions.REQUEST:
         ret.update({
             'create': True,
             'owner_id': get_owner_id(product_stock_case) or user_id,
             'amount_requested': transaction.value,
             'product_id': product_stock_case.product,
             'requested_by': user_id,
             'requested_on': datetime.utcnow(),
         })
     elif transaction.action_config.action_type == RequisitionActions.APPROVAL:
         ret.update({
             'amount_approved': transaction.value,
             'approved_by': user_id,
             'approved_on': datetime.utcnow(),
         })
     elif transaction.action_config.action_type == RequisitionActions.PACK:
         ret.update({
             'amount_packed': transaction.value,
             'packed_by': user_id,
             'packed_on': datetime.utcnow(),
         })
     elif transaction.action_config.action_type == RequisitionActions.RECEIPTS:
         ret.update({
             'amount_received': transaction.value,
             'received_by': user_id,
             'received_on': datetime.utcnow(),
             'close': True,
         })
     else:
         raise ValueError("the type %s isn't yet supported." % transaction.action_config.action_type)
     return ret
Esempio n. 2
0
 def _to_fields(transaction):
     ret = {"requisition_status": RequisitionStatus.by_action_type(transaction.action_config.action_type)}
     if transaction.action_config.action_type == RequisitionActions.REQUEST:
         ret.update(
             {
                 "create": True,
                 "owner_id": get_owner_id(product_stock_case) or user_id,
                 "amount_requested": transaction.value,
                 "product_id": product_stock_case.product,
                 "requested_by": user_id,
                 "requested_on": datetime.utcnow(),
             }
         )
     elif transaction.action_config.action_type == RequisitionActions.APPROVAL:
         ret.update(
             {"amount_approved": transaction.value, "approved_by": user_id, "approved_on": datetime.utcnow()}
         )
     elif transaction.action_config.action_type == RequisitionActions.PACK:
         ret.update({"amount_packed": transaction.value, "packed_by": user_id, "packed_on": datetime.utcnow()})
     elif transaction.action_config.action_type == RequisitionActions.RECEIPTS:
         ret.update(
             {
                 "amount_received": transaction.value,
                 "received_by": user_id,
                 "received_on": datetime.utcnow(),
                 "close": True,
             }
         )
     else:
         raise ValueError("the type %s isn't yet supported." % transaction.action_config.action_type)
     return ret
Esempio n. 3
0
 def _to_fields(transaction):
     ret = {
         'requisition_status':
         RequisitionStatus.by_action_type(
             transaction.action_config.action_type)
     }
     if transaction.action_config.action_type == RequisitionActions.REQUEST:
         ret.update({
             'create': True,
             'owner_id': get_owner_id(product_stock_case) or user_id,
             'amount_requested': transaction.value,
             'product_id': product_stock_case.product,
             'requested_by': user_id,
             'requested_on': datetime.utcnow(),
         })
     elif transaction.action_config.action_type == RequisitionActions.APPROVAL:
         ret.update({
             'amount_approved': transaction.value,
             'approved_by': user_id,
             'approved_on': datetime.utcnow(),
         })
     elif transaction.action_config.action_type == RequisitionActions.PACK:
         ret.update({
             'amount_packed': transaction.value,
             'packed_by': user_id,
             'packed_on': datetime.utcnow(),
         })
     elif transaction.action_config.action_type == RequisitionActions.RECEIPTS:
         ret.update({
             'amount_received': transaction.value,
             'received_by': user_id,
             'received_on': datetime.utcnow(),
             'close': True,
         })
     else:
         raise ValueError("the type %s isn't yet supported." %
                          transaction.action_config.action_type)
     return ret
Esempio n. 4
0
def send_notifications(xform, cases):
    # TODO: fix circular imports
    from corehq.apps.commtrack.requisitions import get_notification_recipients
    from corehq.apps.commtrack.requisitions import get_notification_message

    # for now the only notifications are for requisitions that were touched.
    # todo: if we wanted to include previously requested items we could do so
    # by either polling for other open requisitions here, or by ensuring that
    # they get touched by the commtrack case processing.
    requisitions = [RequisitionCase.wrap(case._doc) for case in cases if case.type == const.REQUISITION_CASE_TYPE]

    if requisitions:
        by_status = defaultdict(list)
        for r in requisitions:
            by_status[r.requisition_status].append(r)

        req_config = CommtrackConfig.for_domain(requisitions[0].domain).requisition_config
        # since each state transition might trigger a different person to be notified
        for s, reqs in by_status.items():
            next_action = req_config.get_next_action(RequisitionStatus.to_action_type(s))

            if next_action:
                # we could make this even more customizable by specifying it per requisition
                # but that would get even messier in terms of constructing the messages
                # so we'll just compose one message per status type now, and then send
                # it to everyone who should be notified.
                to_notify = filter(
                    create_unique_filter(lambda u: u._id),
                    itertools.chain(*(get_notification_recipients(next_action, r) for r in reqs)),
                )

                msg = get_notification_message(next_action, reqs)
                for u in to_notify:
                    phone = u.get_verified_number()
                    if phone:
                        send_sms_to_verified_number(phone, msg)
Esempio n. 5
0
 def get_next_action(self):
     req_config = CommtrackConfig.for_domain(self.domain).requisition_config
     return req_config.get_next_action(
         RequisitionStatus.to_action_type(self.requisition_status)
     )
Esempio n. 6
0
 def get_next_action(self):
     req_config = CommtrackConfig.for_domain(self.domain).requisition_config
     return req_config.get_next_action(
         RequisitionStatus.to_action_type(self.requisition_status))
Esempio n. 7
0
    requisitions = [
        RequisitionCase.wrap(case._doc) for case in cases
        if case.type == const.REQUISITION_CASE_TYPE
    ]

    if requisitions:
        by_status = defaultdict(list)
        for r in requisitions:
            by_status[r.requisition_status].append(r)

        req_config = CommtrackConfig.for_domain(
            requisitions[0].domain).requisition_config
        # since each state transition might trigger a different person to be notified
        for s, reqs in by_status.items():
            next_action = req_config.get_next_action(
                RequisitionStatus.to_action_type(s))

            if next_action:
                # we could make this even more customizable by specifying it per requisition
                # but that would get even messier in terms of constructing the messages
                # so we'll just compose one message per status type now, and then send
                # it to everyone who should be notified.
                to_notify = filter(
                    create_unique_filter(lambda u: u._id),
                    itertools.chain(
                        *(get_notification_recipients(next_action, r)
                          for r in reqs)))

                msg = get_notification_message(next_action, reqs)
                for u in to_notify:
                    phone = u.get_verified_number()