Esempio n. 1
0
def get_ledgers(request, domain):
    """
    Returns ledgers associated with a case in the format:
    {
        "section_id": {
            "product_id": amount,
            "product_id": amount,
            ...
        },
        ...
    }
    """
    case_id = request.REQUEST.get('case_id')
    if not case_id:
        return json_response(
            {'message': 'You must specify a case id to make this query.'},
            status_code=400
        )
    case = get_document_or_404(CommCareCase, domain, case_id)
    ledger_map = get_current_ledger_transactions(case._id)
    def custom_json_handler(obj):
        if isinstance(obj, StockTransaction):
            return obj.stock_on_hand
        return json_handler(obj)

    return json_response(
        {
            'entity_id': case_id,
            'ledger': ledger_map,
        },
        default=custom_json_handler,
    )
Esempio n. 2
0
    def test_get_current_ledger_transactions(self):
        def test_transactions(expected):
            for case in self.case_ids:
                transactions = get_current_ledger_transactions(case)
                self._validate_case_data(transactions, expected[case])

        self._test_get_current_ledger_transactions(test_transactions)

        self.assertEqual({}, get_current_ledger_transactions('non-existent'))
Esempio n. 3
0
    def test_get_current_ledger_transactions(self):
        def test_transactions(expected):
            for case in self.case_ids:
                transactions = get_current_ledger_transactions(case)
                self._validate_case_data(transactions, expected[case])

        self._test_get_current_ledger_transactions(test_transactions)

        self.assertEqual({}, get_current_ledger_transactions('non-existent'))
Esempio n. 4
0
def render_case(case, options):
    """
    Uses options since Django 1.3 doesn't seem to support templatetag kwargs.
    Change to kwargs when we're on a version of Django that does.
    """
    from corehq.apps.hqwebapp.templatetags.proptable_tags import get_tables_as_rows, get_definition
    case = wrapped_case(case)
    timezone = options.get('timezone', pytz.utc)
    timezone = timezone.localize(datetime.datetime.now()).tzinfo
    _get_tables_as_rows = partial(get_tables_as_rows, timezone=timezone)
    display = options.get('display') or case.get_display_config()
    show_transaction_export = options.get('show_transaction_export') or False
    get_case_url = options['get_case_url']

    data = copy.deepcopy(case.to_full_dict())

    default_properties = _get_tables_as_rows(data, display)

    # pop seen properties off of remaining case properties
    dynamic_data = dict(case.dynamic_case_properties())
    # hack - as of commcare 2.0, external id is basically a dynamic property
    # so also check and add it here
    if case.external_id:
        dynamic_data['external_id'] = case.external_id

    for section in display:
        for row in section['layout']:
            for item in row:
                dynamic_data.pop(item.get("expr"), None)

    if dynamic_data:
        dynamic_keys = sorted(dynamic_data.keys())
        definition = get_definition(
                dynamic_keys, num_columns=DYNAMIC_CASE_PROPERTIES_COLUMNS)

        dynamic_properties = _get_tables_as_rows(dynamic_data, definition)
    else:
        dynamic_properties = None

    actions = case.to_json()['actions']
    actions.reverse()

    the_time_is_now = datetime.datetime.now()
    tz_offset_ms = int(timezone.utcoffset(the_time_is_now).total_seconds()) * 1000
    tz_abbrev = timezone.localize(the_time_is_now).tzname()

    # ledgers
    def _product_name(product_id):
        try:
            return SQLProduct.objects.get(product_id=product_id).name
        except SQLProduct.DoesNotExist:
            return (_('Unknown Product ("{}")').format(product_id))

    ledgers = get_current_ledger_transactions(case._id)
    for section, product_map in ledgers.items():
        product_tuples = sorted(
            (_product_name(product_id), product_map[product_id]) for product_id in product_map
        )
        ledgers[section] = product_tuples

    return render_to_string("case/partials/single_case.html", {
        "default_properties": default_properties,
        "default_properties_options": {
            "style": "table"
        },
        "dynamic_properties": dynamic_properties,
        "dynamic_properties_options": {
            "style": "table"
        },
        "case": case,
        "case_actions": mark_safe(simplejson.dumps(actions)),
        "timezone": timezone,
        "tz_abbrev": tz_abbrev,
        "case_hierarchy_options": {
            "show_view_buttons": True,
            "get_case_url": get_case_url,
            "timezone": timezone
        },
        "ledgers": ledgers,
        "timezone_offset": tz_offset_ms,
        "show_transaction_export": show_transaction_export,
    })
Esempio n. 5
0
 def test_get_current_ledger_transactions(self):
     for case in self.case_ids:
         transactions = get_current_ledger_transactions(case)
         for section, products in transactions.items():
             for product, trans in products.items():
                 self.assertEqual(trans.stock_on_hand, self.transactions[case][section][product])
Esempio n. 6
0
 def test_transactions(expected):
     for case in self.case_ids:
         transactions = get_current_ledger_transactions(case)
         self._validate_case_data(transactions, expected[case])