Esempio n. 1
0
 def create_report(self, transactions=None, tag=None, date=None):
     form = get_simple_wrapped_form(uuid.uuid4().hex, metadata=TestFormMetadata(domain=self.domain_name))
     report = StockReportHelper.make_from_form(
         form,
         date or datetime.utcnow(),
         tag or REPORT_TYPE_BALANCE,
         transactions or [],
     )
     return report, form
 def create_report(self, transactions=None, tag=None, date=None):
     form = get_simple_wrapped_form(uuid.uuid4().hex, metadata=TestFormMetadata(domain=self.domain))
     report = StockReportHelper.make_from_form(
         form,
         date or datetime.utcnow(),
         tag or REPORT_TYPE_BALANCE,
         transactions or [],
     )
     return report, form
Esempio n. 3
0
 def create_report(self, transactions=None, tag=None, date=None):
     form = XFormInstance(domain=self.domain)
     form.save()
     report = StockReportHelper.make_from_form(
         form,
         date or datetime.utcnow(),
         tag or REPORT_TYPE_BALANCE,
         transactions or [],
     )
     return report, form
Esempio n. 4
0
def _ledger_json_to_stock_report_helper(form, report_type, ledger_json):
    domain = form.domain
    # figure out what kind of block we're dealing with
    if ledger_json.get('@section-id'):
        ledger_format = LedgerFormat.INDIVIDUAL
    else:
        ledger_format = LedgerFormat.PER_ENTRY

    # details of transaction generation
    # depend on whether it's a balance or a transfer
    if report_type not in (stockconst.REPORT_TYPE_BALANCE,
                           stockconst.REPORT_TYPE_TRANSFER):
        raise ValueError(_(
            'Invalid stock report type {}!'.format(report_type)))

    timestamp = _get_and_validate_date(ledger_json, form)
    common_attributes = {
        'domain': domain,
        'date': timestamp,
    }
    if ledger_format == LedgerFormat.INDIVIDUAL:
        ledger_instructions = _get_ledger_instructions_from_individual_format(
            ledger_json, common_attributes)
    else:
        ledger_instructions = _get_ledger_instructions_from_per_entry_format(
            ledger_json, common_attributes)

    # filter out ones where quantity is None
    # todo: is this really the behavior we want when quantity=""?
    ledger_instructions = [
        ledger_instruction for ledger_instruction in ledger_instructions
        if ledger_instruction.quantity is not None
    ]

    helper_generator_fn = {
        stockconst.REPORT_TYPE_BALANCE:
        _get_transaction_helpers_from_balance_instruction,
        stockconst.REPORT_TYPE_TRANSFER:
        _get_transaction_helpers_from_transfer_instruction,
    }[report_type]
    transaction_helpers = [
        transaction_helper for ledger_instruction in ledger_instructions
        for transaction_helper in helper_generator_fn(ledger_instruction)
    ]
    return StockReportHelper.make_from_form(form, timestamp, report_type,
                                            transaction_helpers)
Esempio n. 5
0
def _ledger_json_to_stock_report_helper(form, report_type, ledger_json):
    domain = form.domain
    # figure out what kind of block we're dealing with
    if ledger_json.get('@section-id'):
        ledger_format = LedgerFormat.INDIVIDUAL
    else:
        ledger_format = LedgerFormat.PER_ENTRY

    # details of transaction generation
    # depend on whether it's a balance or a transfer
    if report_type not in (stockconst.REPORT_TYPE_BALANCE, stockconst.REPORT_TYPE_TRANSFER):
        raise ValueError(_('Invalid stock report type {}!'.format(report_type)))

    timestamp = _get_and_validate_date(ledger_json, form)
    common_attributes = {
        'domain': domain,
        'date': timestamp,
    }
    if ledger_format == LedgerFormat.INDIVIDUAL:
        ledger_instructions = _get_ledger_instructions_from_individual_format(ledger_json, common_attributes)
    else:
        ledger_instructions = _get_ledger_instructions_from_per_entry_format(ledger_json, common_attributes)

    # filter out ones where quantity is None
    # todo: is this really the behavior we want when quantity=""?
    ledger_instructions = [
        ledger_instruction
        for ledger_instruction in ledger_instructions
        if ledger_instruction.quantity is not None
    ]

    helper_generator_fn = {
        stockconst.REPORT_TYPE_BALANCE: _get_transaction_helpers_from_balance_instruction,
        stockconst.REPORT_TYPE_TRANSFER: _get_transaction_helpers_from_transfer_instruction,
    }[report_type]
    transaction_helpers = [
        transaction_helper
        for ledger_instruction in ledger_instructions
        for transaction_helper in helper_generator_fn(ledger_instruction)
    ]
    return StockReportHelper.make_from_form(form, timestamp, report_type, transaction_helpers)