Exemplo n.º 1
0
 def handle(self, text):
     # at the end of your receipt message you can write:
     # 'from xxx' to indicate the source of the supplies.
     # this is used in the stock transfer workflow
     # TODO: less brittle parsing
     if "from" in text.lower().split():
         splittext = text.lower().split()
         text = " ".join(splittext[:splittext.index("from")])
         supplier = " ".join(splittext[splittext.index("from") + 1:])
     else:
         supplier = None
     
     # parse the report and save as normal receipt
     stock_report = ProductReportsHelper(self.msg.logistics_contact.supply_point, 
                                         Reports.REC, self.msg.logger_msg)
     stock_report.parse(text)
     stock_report.save()
     
     # Close pending requests. This logic only applies if you are using the 
     # StockRequest workflow, but should not break anything if you are not
     StockRequest.close_pending_from_receipt_report(stock_report, self.msg.logistics_contact)
     
     # fill in transfers, if there were any
     if supplier is not None:
         StockTransfer.create_from_receipt_report(stock_report, supplier)
         self.respond(_(config.Messages.RECEIPT_FROM_CONFIRM), products=" ".join(stock_report.reported_products()).strip(),
                      supplier=supplier)
     else:
         self.respond(_(config.Messages.RECEIPT_CONFIRM), products=" ".join(stock_report.reported_products()).strip())
Exemplo n.º 2
0
 def _process_give(self):
     words = self.report_data.split(" ")
     # TODO: this is too much copy-paste from the transfer handler
     if len(words) < 3: 
         return self.help()
     hsa_id = words[0]
     remainder = " ".join(words[1:])
     hsa = util.get_hsa(hsa_id)
     if hsa is None:
         self.respond(config.Messages.UNKNOWN_HSA, hsa_id=hsa_id)
     else:
         stock_report = create_stock_report(Reports.GIVE,  
                                            self.hsa.supply_point,
                                            remainder, 
                                            self.msg.logger_msg)
         transfers = StockTransfer.create_from_transfer_report(stock_report, hsa.supply_point)
         send_transfer_responses(self.msg, stock_report, transfers, self.hsa, hsa)
Exemplo n.º 3
0
 def handle(self, text):
     words = text.split(" ")
     # need at least a keyword and 1 product + amount
     if len(words) < 3: 
         return self.help()
     hsa_id = words[0]
     remainder = " ".join(words[1:])
     hsa = util.get_hsa(hsa_id)
     if hsa is None:
         self.respond(config.Messages.UNKNOWN_HSA, hsa_id=hsa_id)
     else:
         stock_report = create_stock_report(Reports.GIVE,  
                                            self.msg.logistics_contact.supply_point,
                                            remainder, 
                                            self.msg.logger_msg)
         transfers = StockTransfer.create_from_transfer_report(stock_report, hsa.supply_point)
         send_transfer_responses(self.msg, stock_report, transfers, self.msg.logistics_contact, hsa)
Exemplo n.º 4
0
 def handle(self, text):
     words = text.split(" ")
     # need at least a keyword and 1 product + amount
     if len(words) < 3:
         return self.help()
     hsa_id = words[0]
     remainder = " ".join(words[1:])
     hsa = util.get_hsa(hsa_id)
     if hsa is None:
         self.respond(config.Messages.UNKNOWN_HSA, hsa_id=hsa_id)
     else:
         stock_report = create_stock_report(
             Reports.GIVE, self.msg.logistics_contact.supply_point,
             remainder, self.msg.logger_msg)
         transfers = StockTransfer.create_from_transfer_report(
             stock_report, hsa.supply_point)
         send_transfer_responses(self.msg, stock_report, transfers,
                                 self.msg.logistics_contact, hsa)
Exemplo n.º 5
0
 def _process_give(self):
     words = self.report_data.split(" ")
     # TODO: this is too much copy-paste from the transfer handler
     if len(words) < 3:
         return self.help()
     hsa_id = words[0]
     remainder = " ".join(words[1:])
     hsa = util.get_hsa(hsa_id)
     if hsa is None:
         self.respond(config.Messages.UNKNOWN_HSA, hsa_id=hsa_id)
     else:
         stock_report = create_stock_report(Reports.GIVE,
                                            self.hsa.supply_point,
                                            remainder, self.msg.logger_msg)
         transfers = StockTransfer.create_from_transfer_report(
             stock_report, hsa.supply_point)
         send_transfer_responses(self.msg, stock_report, transfers,
                                 self.hsa, hsa)
Exemplo n.º 6
0
    def handle(self, text):
        pending = StockTransfer.pending_transfers().filter\
            (receiver=self.msg.logistics_contact.supply_point).all()
        if len(pending) == 0:
            self.respond(config.Messages.NO_PENDING_TRANSFERS)
        else:
            # the easiest way to mark these in the database,
            # is to make a fake stock report
            # of the pending transfers, considering them as receipts
            stock_report = ProductReportsHelper(
                self.msg.logistics_contact.supply_point, Reports.REC)
            stock_report.parse(" ".join([p.sms_format() for p in pending]))
            stock_report.save()

            # close the pending transfers
            now = datetime.utcnow()
            for p in pending:
                p.confirm(now)
            self.respond(config.Messages.CONFIRM_RESPONSE,
                         receiver=self.msg.logistics_contact.name,
                         products=", ".join([p.sms_format() for p in pending]))
Exemplo n.º 7
0
 def handle(self, text):
     pending = StockTransfer.pending_transfers().filter\
         (receiver=self.msg.logistics_contact.supply_point).all()
     if len(pending) == 0:
         self.respond(config.Messages.NO_PENDING_TRANSFERS)
     else:
         # the easiest way to mark these in the database, 
         # is to make a fake stock report
         # of the pending transfers, considering them as receipts
         stock_report = ProductReportsHelper(self.msg.logistics_contact.supply_point, 
                                             Reports.REC)
         stock_report.parse(" ".join([p.sms_format() for p in pending]))
         stock_report.save()
         
         # close the pending transfers
         now = datetime.utcnow()
         for p in pending:
             p.confirm(now)
         self.respond(config.Messages.CONFIRM_RESPONSE, 
                      receiver=self.msg.logistics_contact.name,
                      products=", ".join([p.sms_format() for p in pending]))
         
Exemplo n.º 8
0
class ReceiptHandler(KeywordHandler, TaggingHandler):
    """
    Allows SMS reporters to send in "rec jd 10 mc 30" to report 10 jadelle and 30 male condoms received
    """

    keyword = logistics_keyword("rec|receipts|received")

    def help(self):
        self.respond(
            _("Please send in information about your receipts in the format 'rec <product> <amount> <product> <amount>...'"
              ))

    @logistics_contact_and_permission_required(config.Operations.REPORT_RECEIPT
                                               )
    def handle(self, text):
        dupes = Message.objects.filter(
            direction="I",
            connection=self.msg.connection,
            text__iexact=self.msg.raw_text).exclude(pk=self.msg.logger_msg.pk)
        if settings.LOGISTICS_IGNORE_DUPE_RECEIPTS_WITHIN:
            dupe_ignore_threshold = datetime.utcnow() - timedelta(
                seconds=settings.LOGISTICS_IGNORE_DUPE_RECEIPTS_WITHIN)
            ignore = dupes.filter(date__gte=dupe_ignore_threshold)
            if ignore.count() and ProductReport.objects.filter(
                    message__in=dupes).count():
                return True

        # at the end of your receipt message you can write:
        # 'from xxx' to indicate the source of the supplies.
        # this is used in the stock transfer workflow
        # TODO: less brittle parsing
        if "from" in text.lower().split():
            splittext = text.lower().split()
            text = " ".join(splittext[:splittext.index("from")])
            supplier = " ".join(splittext[splittext.index("from") + 1:])
        else:
            supplier = None

        # parse the report and save as normal receipt
        stock_report = ProductReportsHelper(
            self.msg.logistics_contact.supply_point, Reports.REC,
            self.msg.logger_msg)
        stock_report.parse(text)
        # check max stock levels
        max_level_function = get_max_level_function()
        if max_level_function:

            try:
                max_level_function(stock_report)
            except TooMuchStockError, e:
                # bit of a hack, also check if there was a recent message
                # that matched this and if so force it through
                override_threshold = datetime.utcnow() - timedelta(seconds=60 *
                                                                   60 * 4)
                override = dupes.filter(date__gte=override_threshold)
                if override.count() == 0:
                    self.respond(
                        config.Messages.TOO_MUCH_STOCK % {
                            'keyword': self.msg.text.split()[0],
                            'req': e.amount,
                            'prod': e.product,
                            'max': e.max,
                        })
                    return True

        stock_report.save()

        # Close pending requests. This logic only applies if you are using the
        # StockRequest workflow, but should not break anything if you are not
        StockRequest.close_pending_from_receipt_report(
            stock_report, self.msg.logistics_contact)

        # fill in transfers, if there were any
        if supplier is not None:
            StockTransfer.create_from_receipt_report(stock_report, supplier)
            self.respond(_(config.Messages.RECEIPT_FROM_CONFIRM),
                         products=" ".join(
                             stock_report.reported_products()).strip(),
                         supplier=supplier)
        else:
            self.respond(_(config.Messages.RECEIPT_CONFIRM),
                         products=" ".join(
                             stock_report.reported_products()).strip())