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()
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), )