예제 #1
0
    def to_xml(self, E=None, **kwargs):
        if not E:
            E = XML()

        tx_type = 'balance' if self.action in (
            StockActions.STOCKONHAND,
            StockActions.STOCKOUT,
        ) else 'transfer'

        attr = {}
        if self.timestamp:
            attr['date'] = dateparse.json_format_datetime(self.timestamp)

        attr['section-id'] = 'stock'
        if tx_type == 'balance':
            attr['entity-id'] = self.case_id
        elif tx_type == 'transfer':
            here, there = ('dest', 'src') if self.action == StockActions.RECEIPTS else ('src', 'dest')
            attr[here] = self.case_id
            # no 'there' for now
            if self.subaction:
                attr['type'] = self.subaction

        return getattr(E, tx_type)(
            E.entry(
                id=self.product_id,
                quantity=str(self.quantity if self.action != StockActions.STOCKOUT else 0),
            ),
            **attr
        )
예제 #2
0
    def to_xml(self, E=None, **kwargs):
        if not E:
            E = XML()

        return E.entry(
            id=self.product_id,
            quantity=str(self.quantity if self.action != StockActions.STOCKOUT else 0),
        )
예제 #3
0
    def to_xml(self, E=None, **kwargs):
        if not E:
            E = XML()

        return E.entry(
            id=self.product_id,
            quantity=str(self.quantity if self.action != StockActions.STOCKOUT
                         else 0),
        )
예제 #4
0
def to_instance(data):
    """convert the parsed sms stock report into an instance like what would be
    submitted from a commcare phone"""
    E = XML()
    M = XML(const.META_XMLNS, 'jrm')

    deviceID = get_device_id(data)
    timestamp = json_format_datetime(data['timestamp'])

    transactions = data['transactions']
    category = set(tx.category for tx in transactions).pop()
    stock_blocks = convert_transactions_to_blocks(E, transactions)

    if category == 'stock':
        root = E.stock_report(
            M.meta(
                M.userID(data['user']._id),
                M.deviceID(deviceID),
                M.timeStart(timestamp),
                M.timeEnd(timestamp)
            ),
            E.location(data['location']._id),
            *stock_blocks
        )
        return etree.tostring(root, encoding='utf-8', pretty_print=True)
    else:
        return requisition_case_xml(data, stock_blocks)
예제 #5
0
def to_instance(data):
    """convert the parsed sms stock report into an instance like what would be
    submitted from a commcare phone"""
    E = XML()
    M = XML(const.META_XMLNS, 'jrm')

    deviceID = ''
    if data.get('phone'):
        deviceID = 'sms:%s' % data['phone']
    timestamp = json_format_datetime(data['timestamp'])

    transactions = data['transactions']
    category = set(tx.category for tx in transactions).pop()
    factory = {
        'stock': E.stock_report,
        'requisition': E.requisition,
    }[category]

    root = factory(
        M.meta(
            M.userID(data['user']._id),
            M.deviceID(deviceID),
            M.timeStart(timestamp),
            M.timeEnd(timestamp)
        ),
        E.location(data['location']._id),
        *[tx.to_xml() for tx in transactions]
    )

    return root
예제 #6
0
파일: sms.py 프로젝트: xbryanc/commcare-hq
def to_instance(data):
    """convert the parsed sms stock report into an instance like what would be
    submitted from a commcare phone"""
    E = XML()
    M = XML(const.META_XMLNS, 'jrm')

    deviceID = get_device_id(data)
    timestamp = json_format_datetime(data['timestamp'])

    transactions = data['transactions']
    category = set(tx.category for tx in transactions).pop()
    stock_blocks = convert_transactions_to_blocks(E, transactions)

    if category == 'stock':
        root = E.stock_report(
            M.meta(M.userID(data['user']._id), M.deviceID(deviceID),
                   M.timeStart(timestamp), M.timeEnd(timestamp)),
            E.location(data['location']._id), *stock_blocks)
        return etree.tostring(root, encoding='utf-8', pretty_print=True)
    else:
        raise RequisitionsHaveBeenRemoved()