def testReceipts(self):
        # this tests the requisition specific receipt keyword. not to be confused
        # with the standard stock receipt keyword
        self.testRequisition()

        reqs = RequisitionCase.open_for_location(self.domain.name, self.loc._id)
        self.assertEqual(3, len(reqs))
        req_ids_by_product_code = dict(((RequisitionCase.get(id).get_product().code, id) for id in reqs))

        rec_amounts = {
            'pp': 30,
            'pq': 20,
            'pr': 10,
        }
        # rec loc1 pp 10 pq 20...
        handled = handle(self.user.get_verified_number(), 'rec {loc} {report}'.format(
            loc='loc1',
            report=' '.join('%s %s' % (k, v) for k, v in rec_amounts.items())
        ))
        self.assertTrue(handled)

        # we should have closed the requisitions
        self.assertEqual(0, len(RequisitionCase.open_for_location(self.domain.name, self.loc._id)))

        forms = list(self.get_commtrack_forms())
        self.assertEqual(2, len(forms))

        self.assertEqual(self.sp.location_, forms[1].location_)
        # check updated status
        for code, amt in rec_amounts.items():
            req_case = RequisitionCase.get(req_ids_by_product_code[code])
            self.assertTrue(req_case.closed)
            self.assertEqual(str(amt), req_case.amount_received)
            self.assertEqual(self.user._id, req_case.received_by)
            self.assertTrue(req_case._id in reqs, 'requisition %s should be in %s' % (req_case._id, reqs))
    def testRequisition(self):
        self.assertEqual(0, len(RequisitionCase.open_for_location(self.domain.name, self.loc._id)))
        self.assertEqual(0, len(self.get_commtrack_forms()))

        amounts = {
            'pp': 10,
            'pq': 20,
            'pr': 30,
        }
        # req loc1 pp 10 pq 20...
        handled = handle(self.user.get_verified_number(), 'req {loc} {report}'.format(
            loc='loc1',
            report=' '.join('%s %s' % (k, v) for k, v in amounts.items())
        ))
        self.assertTrue(handled)

        # make sure we got the updated requisitions
        reqs = RequisitionCase.open_for_location(self.domain.name, self.loc._id)
        self.assertEqual(3, len(reqs))

        forms = list(self.get_commtrack_forms())
        self.assertEqual(1, len(forms))

        self.assertEqual(self.sp.location_, forms[0].location_)
        # check updated status
        for code, amt in amounts.items():
            spp = CommCareCase.get(self.spps[code]._id)
            # make sure the index was created
            [req_ref] = spp.reverse_indices
            req_case = RequisitionCase.get(req_ref.referenced_id)
            self.assertEqual(str(amt), req_case.amount_requested)
            self.assertEqual(self.user._id, req_case.requested_by)
            self.assertEqual(req_case.location_, self.sp.location_)
            self.assertTrue(req_case._id in reqs)
            self.assertEqual(spp._id, req_case.get_product_case()._id)
Exemplo n.º 3
0
    def testReceiptsWithNoOpenRequisition(self):
        # make sure we don't have any open requisitions
        self.assertEqual(
            0,
            len(
                RequisitionCase.open_for_location(self.domain.name,
                                                  self.loc._id)))

        rec_amounts = {
            'pp': 30,
            'pq': 20,
            'pr': 10,
        }
        # rec loc1 pp 10 pq 20...
        handled = handle(
            self.verified_number, 'rec {loc} {report}'.format(
                loc='loc1',
                report=' '.join('%s %s' % (k, v)
                                for k, v in rec_amounts.items())))
        self.assertTrue(handled)

        # should still be no open requisitions
        self.assertEqual(
            0,
            len(
                RequisitionCase.open_for_location(self.domain.name,
                                                  self.loc._id)))
    def testRequisition(self):
        # confirm we have a clean start
        self.assertEqual(0, len(RequisitionCase.open_for_location(self.domain.name, self.loc._id)))
        self.assertEqual(0, len(self.get_commtrack_forms(self.domain.name)))

        amounts = {
            'pp': 10,
            'pq': 20,
            'pr': 30,
        }
        # req loc1 pp 10 pq 20...
        handled = handle(self.users[0].get_verified_number(), 'req {loc} {report}'.format(
            loc='loc1',
            report=' '.join('%s %s' % (k, v) for k, v in amounts.items())
        ))

        self.assertTrue(handled)

        # make sure we got the updated requisitions
        reqs = RequisitionCase.open_for_location(self.domain.name, self.loc._id)
        self.assertEqual(1, len(reqs))

        req = RequisitionCase.get(reqs[0])
        [index] = req.indices

        self.assertEqual(req.requisition_status, 'requested')
        self.assertEqual(const.SUPPLY_POINT_CASE_TYPE, index.referenced_type)
        self.assertEqual(self.sp._id, index.referenced_id)
        self.assertEqual('parent_id', index.identifier)

        # check updated status
        for code, amt in amounts.items():
            self.check_stock(code, amt, req._id, 'ct-requested')
            self.check_stock(code, 0, req._id, 'stock')
    def testReceipt(self):
        amounts = {
            'pp': 10,
            'pq': 20,
            'pr': 30,
        }

        # start with an open request
        handle(
            self.users[0].get_verified_number(),
            'req {loc} {report}'.format(
                loc='loc1',
                report=' '.join('%s %s' % (k, v) for k, v in amounts.items())
            )
        )

        # fulfill it
        handle(
            self.users[0].get_verified_number(),
            'fulfill {loc} {report}'.format(
                loc='loc1',
                report=' '.join('%s %s' % (k, v) for k, v in amounts.items())
            )
        )

        # grab this first because we are about to close it
        req_id = RequisitionCase.open_for_location(self.domain.name, self.loc._id)[0]

        # mark it received
        handle(
            self.users[0].get_verified_number(),
            'rec {loc} {report}'.format(
                loc='loc1',
                report=' '.join('%s %s' % (k, v) for k, v in amounts.items())
            )
        )

        reqs = RequisitionCase.open_for_location(self.domain.name, self.loc._id)

        # receiving by sms closes the req
        self.assertEqual(0, len(reqs))

        req = RequisitionCase.get(req_id)
        [index] = req.indices

        self.assertEqual(req.requisition_status, 'received')

        # check updated status
        for code, amt in amounts.items():
            self.check_stock(code, 0, req._id, 'stock')
            self.check_stock(code, amt, self.sp._id, 'stock')
Exemplo n.º 6
0
 def get_case_ids(self):
     # todo: too many couch requests
     for c in filter(
         create_unique_filter(lambda id: RequisitionCase.get(id).get_product_case_id()),
         RequisitionCase.open_for_location(self.domain, self.location_id),
     ):
         yield c
Exemplo n.º 7
0
 def get_req_id(self):
     reqs = RequisitionCase.open_for_location(self.location["location"].domain, self.location["location"]._id)
     if reqs:
         # only support one open requisition per location
         assert len(reqs) == 1
         return reqs[0]
     else:
         return uuid.uuid4().hex
Exemplo n.º 8
0
 def get_case_ids(self):
     # todo: too many couch requests
     for c in filter(
             create_unique_filter(
                 lambda id: RequisitionCase.get(id).get_product_case_id()),
             RequisitionCase.open_for_location(self.domain,
                                               self.location_id)):
         yield c
Exemplo n.º 9
0
 def get_req_id(self):
     reqs = RequisitionCase.open_for_location(
         self.location['location'].domain, self.location['location']._id)
     if reqs:
         # only support one open requisition per location
         assert (len(reqs) == 1)
         return reqs[0]
     else:
         return uuid.uuid4().hex
Exemplo n.º 10
0
    def testReceiptsWithNoOpenRequisition(self):
        # make sure we don't have any open requisitions
        self.assertEqual(0, len(RequisitionCase.open_for_location(self.domain.name, self.loc._id)))

        rec_amounts = {
            'pp': 30,
            'pq': 20,
            'pr': 10,
        }
        # rec loc1 pp 10 pq 20...
        handled = handle(self.user.get_verified_number(), 'rec {loc} {report}'.format(
            loc='loc1',
            report=' '.join('%s %s' % (k, v) for k, v in rec_amounts.items())
        ))
        self.assertTrue(handled)

        # should still be no open requisitions
        self.assertEqual(0, len(RequisitionCase.open_for_location(self.domain.name, self.loc._id)))
Exemplo n.º 11
0
    def testReceipts(self):
        # this tests the requisition specific receipt keyword. not to be confused
        # with the standard stock receipt keyword
        self.testRequisition()

        reqs = RequisitionCase.open_for_location(self.domain.name,
                                                 self.loc._id)
        self.assertEqual(3, len(reqs))
        req_ids_by_product_code = dict(
            ((RequisitionCase.get(id).get_product().code, id) for id in reqs))

        rec_amounts = {
            'pp': 30,
            'pq': 20,
            'pr': 10,
        }
        # rec loc1 pp 10 pq 20...
        handled = handle(
            self.verified_number, 'rec {loc} {report}'.format(
                loc='loc1',
                report=' '.join('%s %s' % (k, v)
                                for k, v in rec_amounts.items())))
        self.assertTrue(handled)

        # we should have closed the requisitions
        self.assertEqual(
            0,
            len(
                RequisitionCase.open_for_location(self.domain.name,
                                                  self.loc._id)))

        forms = list(self.get_commtrack_forms())
        self.assertEqual(2, len(forms))

        self.assertEqual(self.sp.location_, forms[1].location_)
        # check updated status
        for code, amt in rec_amounts.items():
            req_case = RequisitionCase.get(req_ids_by_product_code[code])
            self.assertTrue(req_case.closed)
            self.assertEqual(str(amt), req_case.amount_received)
            self.assertEqual(self.user._id, req_case.received_by)
            self.assertTrue(
                req_case._id in reqs,
                'requisition %s should be in %s' % (req_case._id, reqs))
Exemplo n.º 12
0
    def testRequisition(self):
        self.assertEqual(
            0,
            len(
                RequisitionCase.open_for_location(self.domain.name,
                                                  self.loc._id)))
        self.assertEqual(0, len(self.get_commtrack_forms()))

        amounts = {
            'pp': 10,
            'pq': 20,
            'pr': 30,
        }
        # req loc1 pp 10 pq 20...
        handled = handle(
            self.verified_number, 'req {loc} {report}'.format(
                loc='loc1',
                report=' '.join('%s %s' % (k, v) for k, v in amounts.items())))
        self.assertTrue(handled)

        # make sure we got the updated requisitions
        reqs = RequisitionCase.open_for_location(self.domain.name,
                                                 self.loc._id)
        self.assertEqual(3, len(reqs))

        forms = list(self.get_commtrack_forms())
        self.assertEqual(1, len(forms))

        self.assertEqual(self.sp.location_, forms[0].location_)
        # check updated status
        for code, amt in amounts.items():
            spp = CommCareCase.get(self.spps[code]._id)
            # make sure the index was created
            [req_ref] = spp.reverse_indices
            req_case = RequisitionCase.get(req_ref.referenced_id)
            self.assertEqual(str(amt), req_case.amount_requested)
            self.assertEqual(self.user._id, req_case.requested_by)
            self.assertEqual(req_case.location_, self.sp.location_)
            self.assertTrue(req_case._id in reqs)
            self.assertEqual(spp._id, req_case.get_product_case()._id)
Exemplo n.º 13
0
    def inactive_testSimpleApproval(self):
        self.testRequisition()

        # approve loc1
        handled = handle(self.user.get_verified_number(), 'approve {loc}'.format(
            loc='loc1',
            ))
        self.assertTrue(handled)
        reqs = RequisitionCase.open_for_location(self.domain.name, self.loc._id)
        self.assertEqual(3, len(reqs))

        for req_id in reqs:
            req_case = RequisitionCase.get(req_id)
            self.assertEqual(RequisitionStatus.APPROVED, req_case.requisition_status)
            self.assertEqual(req_case.amount_requested, req_case.amount_approved)
            self.assertEqual(self.user._id, req_case.approved_by)
            self.assertIsNotNone(req_case.approved_on)
            self.assertTrue(isinstance(req_case.approved_on, datetime))
Exemplo n.º 14
0
    def testSimplePack(self):
        self.testRequisition()

        # pack loc1
        handled = handle(self.user.get_verified_number(), 'pack {loc}'.format(
            loc='loc1',
        ))
        self.assertTrue(handled)
        reqs = RequisitionCase.open_for_location(self.domain.name, self.loc._id)
        self.assertEqual(3, len(reqs))

        for req_id in reqs:
            req_case = RequisitionCase.get(req_id)
            self.assertEqual(RequisitionStatus.PACKED, req_case.requisition_status)
            self.assertEqual(req_case.amount_requested, req_case.amount_packed)
            self.assertEqual(self.user._id, req_case.packed_by)
            self.assertIsNotNone(req_case.packed_on)
            self.assertTrue(isinstance(req_case.packed_on, datetime))
            self.assertEqual(req_case.product_id, req_case.get_product_case().product)
Exemplo n.º 15
0
    def testSimplePack(self):
        self.testRequisition()

        # pack loc1
        handled = handle(self.verified_number,
                         'pack {loc}'.format(loc='loc1', ))
        self.assertTrue(handled)
        reqs = RequisitionCase.open_for_location(self.domain.name,
                                                 self.loc._id)
        self.assertEqual(3, len(reqs))

        for req_id in reqs:
            req_case = RequisitionCase.get(req_id)
            self.assertEqual(RequisitionStatus.PACKED,
                             req_case.requisition_status)
            self.assertEqual(req_case.amount_requested, req_case.amount_packed)
            self.assertEqual(self.user._id, req_case.packed_by)
            self.assertIsNotNone(req_case.packed_on)
            self.assertTrue(isinstance(req_case.packed_on, datetime))
            self.assertEqual(req_case.product_id,
                             req_case.get_product_case().product)
Exemplo n.º 16
0
    def testSimpleFulfill(self):
        amounts = {
            'pp': 10,
            'pq': 20,
            'pr': 30,
        }

        # start with an open request
        handle(
            self.users[0].get_verified_number(),
            'req {loc} {report}'.format(
                loc='loc1',
                report=' '.join('%s %s' % (k, v) for k, v in amounts.items())
            )
        )

        # fulfill loc1
        handled = handle(
            self.users[0].get_verified_number(),
            'fulfill {loc} {report}'.format(
                loc='loc1',
                report=' '.join('%s %s' % (k, v) for k, v in amounts.items())
            )
        )
        self.assertTrue(handled)

        reqs = RequisitionCase.open_for_location(self.domain.name, self.loc._id)

        # should not have created a new req
        self.assertEqual(1, len(reqs))

        req = RequisitionCase.get(reqs[0])
        [index] = req.indices

        self.assertEqual(req.requisition_status, 'fulfilled')

        for code, amt in amounts.items():
            self.check_stock(code, amt, req._id, 'stock')
            self.check_stock(code, amt, req._id, 'ct-fulfilled')
Exemplo n.º 17
0
    def test_create_fulfill_and_receive_requisition(self):
        amounts = [(p._id, 50.0 + float(i*10)) for i, p in enumerate(self.products)]

        # ----------------
        # Create a request
        # ----------------

        self.submit_xml_form(create_requisition_xml(amounts))
        req_cases = list(get_cases_in_domain(self.domain.name, type=const.REQUISITION_CASE_TYPE))
        self.assertEqual(1, len(req_cases))
        req = RequisitionCase.get(req_cases[0]._id)
        [index] = req.indices

        self.assertEqual(req.requisition_status, 'requested')
        self.assertEqual(const.SUPPLY_POINT_CASE_TYPE, index.referenced_type)
        self.assertEqual(self.sp._id, index.referenced_id)
        self.assertEqual('parent_id', index.identifier)
        # TODO: these types of tests probably belong elsewhere
        self.assertEqual(req.get_next_action().keyword, 'fulfill')
        self.assertEqual(req.get_location()._id, self.sp.location._id)
        self.assertEqual(len(RequisitionCase.open_for_location(
            self.domain.name,
            self.sp.location._id
        )), 1)
        self.assertEqual(
            get_notification_message(
                req.get_next_action(),
                [req]
            ),
            self.expected_notification_message(req, amounts)
        )

        for product, amt in amounts:
            self.check_stock_models(req, product, amt, 0, 'ct-requested')

        # ----------------
        # Mark it fulfilled
        # -----------------

        self.submit_xml_form(create_fulfillment_xml(req, amounts))

        req = RequisitionCase.get(req._id)

        self.assertEqual(req.requisition_status, 'fulfilled')
        self.assertEqual(req.get_next_action().keyword, 'rec')
        self.assertEqual(
            get_notification_message(
                req.get_next_action(),
                [req]
            ),
            self.expected_notification_message(req, amounts)
        )

        for product, amt in amounts:
            # we are expecting two separate blocks to have come with the same
            # values
            self.check_stock_models(req, product, amt, amt, 'stock')
            self.check_stock_models(req, product, amt, 0, 'ct-fulfilled')

        # ----------------
        # Mark it received
        # ----------------

        self.submit_xml_form(create_received_xml(req, amounts))

        req = RequisitionCase.get(req._id)

        self.assertEqual(req.requisition_status, 'received')
        self.assertIsNone(req.get_next_action())
        self.assertEqual(len(RequisitionCase.open_for_location(
            self.domain.name,
            self.sp.location._id
        )), 0)

        for product, amt in amounts:
            self.check_stock_models(req, product, 0, -amt, 'stock')
            self.check_stock_models(self.sp, product, amt, amt, 'stock')
Exemplo n.º 18
0
    def test_create_fulfill_and_receive_requisition(self):
        amounts = [(p._id, 50.0 + float(i * 10))
                   for i, p in enumerate(self.products)]

        # ----------------
        # Create a request
        # ----------------

        self.submit_xml_form(create_requisition_xml(amounts))
        req_cases = list(
            get_cases_in_domain(self.domain.name,
                                type=const.REQUISITION_CASE_TYPE))
        self.assertEqual(1, len(req_cases))
        req = RequisitionCase.get(req_cases[0]._id)
        [index] = req.indices

        self.assertEqual(req.requisition_status, 'requested')
        self.assertEqual(const.SUPPLY_POINT_CASE_TYPE, index.referenced_type)
        self.assertEqual(self.sp._id, index.referenced_id)
        self.assertEqual('parent_id', index.identifier)
        # TODO: these types of tests probably belong elsewhere
        self.assertEqual(req.get_next_action().keyword, 'fulfill')
        self.assertEqual(req.get_location()._id, self.sp.location._id)
        self.assertEqual(
            len(
                RequisitionCase.open_for_location(self.domain.name,
                                                  self.sp.location._id)), 1)
        self.assertEqual(
            get_notification_message(req.get_next_action(), [req]),
            self.expected_notification_message(req, amounts))

        for product, amt in amounts:
            self.check_stock_models(req, product, amt, 0, 'ct-requested')

        # ----------------
        # Mark it fulfilled
        # -----------------

        self.submit_xml_form(create_fulfillment_xml(req, amounts))

        req = RequisitionCase.get(req._id)

        self.assertEqual(req.requisition_status, 'fulfilled')
        self.assertEqual(req.get_next_action().keyword, 'rec')
        self.assertEqual(
            get_notification_message(req.get_next_action(), [req]),
            self.expected_notification_message(req, amounts))

        for product, amt in amounts:
            # we are expecting two separate blocks to have come with the same
            # values
            self.check_stock_models(req, product, amt, amt, 'stock')
            self.check_stock_models(req, product, amt, 0, 'ct-fulfilled')

        # ----------------
        # Mark it received
        # ----------------

        self.submit_xml_form(create_received_xml(req, amounts))

        req = RequisitionCase.get(req._id)

        self.assertEqual(req.requisition_status, 'received')
        self.assertIsNone(req.get_next_action())
        self.assertEqual(
            len(
                RequisitionCase.open_for_location(self.domain.name,
                                                  self.sp.location._id)), 0)

        for product, amt in amounts:
            self.check_stock_models(req, product, 0, -amt, 'stock')
            self.check_stock_models(self.sp, product, amt, amt, 'stock')