示例#1
0
 def _create_stock_state(self, product, consumption):
     xform = XFormInstance.get('test-xform')
     loc = Location.by_site_code(TEST_DOMAIN, 'garms')
     now = datetime.datetime.utcnow()
     report = StockReport(form_id=xform._id,
                          date=(now - datetime.timedelta(days=10)).replace(
                              second=0, microsecond=0),
                          type='balance',
                          domain=TEST_DOMAIN)
     report.save()
     stock_transaction = StockTransaction(
         case_id=loc.linked_supply_point().get_id,
         product_id=product.get_id,
         sql_product=SQLProduct.objects.get(product_id=product.get_id),
         section_id='stock',
         type='stockonhand',
         stock_on_hand=2 * consumption,
         report=report)
     stock_transaction.save()
     report = StockReport(form_id=xform._id,
                          date=now.replace(second=0, microsecond=0),
                          type='balance',
                          domain=TEST_DOMAIN)
     report.save()
     stock_transaction = StockTransaction(
         case_id=loc.linked_supply_point().get_id,
         product_id=product.get_id,
         sql_product=SQLProduct.objects.get(product_id=product.get_id),
         section_id='stock',
         type='stockonhand',
         stock_on_hand=consumption,
         report=report)
     stock_transaction.save()
    def create_transactions(self, domain=None):
        report = StockReport.objects.create(form_id=uuid.uuid4().hex,
                                            date=ago(2),
                                            type=const.REPORT_TYPE_BALANCE,
                                            domain=domain)

        txn = StockTransaction(
            report=report,
            section_id=const.SECTION_TYPE_STOCK,
            type=const.TRANSACTION_TYPE_STOCKONHAND,
            case_id=self.case_id,
            product_id=self.product_id,
            stock_on_hand=Decimal(10),
        )
        txn.save()

        report2 = StockReport.objects.create(form_id=uuid.uuid4().hex,
                                             date=ago(1),
                                             type=const.REPORT_TYPE_BALANCE,
                                             domain=domain)

        txn2 = StockTransaction(
            report=report2,
            section_id=const.SECTION_TYPE_STOCK,
            type=const.TRANSACTION_TYPE_STOCKONHAND,
            case_id=self.case_id,
            product_id=self.product_id,
            stock_on_hand=Decimal(30),
        )
        txn2.save()
    def _test_subtype(self, initial, final):
        case_id = uuid.uuid4().hex
        CommCareCase(
            _id=case_id,
            domain='fakedomain',
        ).save()

        product_id = uuid.uuid4().hex
        SQLProduct(product_id=product_id, domain='fakedomain').save()
        report = StockReport.objects.create(form_id=uuid.uuid4().hex,
                                            date=ago(1),
                                            server_date=datetime.utcnow(),
                                            type=const.REPORT_TYPE_BALANCE)

        txn = StockTransaction(
            report=report,
            section_id=const.SECTION_TYPE_STOCK,
            type=const.TRANSACTION_TYPE_STOCKONHAND,
            subtype=initial,
            case_id=case_id,
            product_id=product_id,
            stock_on_hand=Decimal(10),
        )
        txn.save()

        saved = StockTransaction.objects.get(id=txn.id)
        self.assertEqual(final, saved.subtype)
示例#4
0
def _create_model_for_stock_transaction(report, transaction_helper):
    assert report.type in stockconst.VALID_REPORT_TYPES
    txn = StockTransaction(
        report=report,
        case_id=transaction_helper.case_id,
        section_id=transaction_helper.section_id,
        product_id=transaction_helper.product_id,
        type=transaction_helper.action,
        subtype=transaction_helper.subaction,
    )

    def lazy_original_balance():
        previous_transaction = txn.get_previous_transaction()
        if previous_transaction:
            return previous_transaction.stock_on_hand
        else:
            return None

    new_ledger_values = compute_ledger_values(
        lazy_original_balance, report.type,
        transaction_helper.relative_quantity)

    txn.stock_on_hand = new_ledger_values.balance
    txn.quantity = new_ledger_values.delta

    if report.domain:
        # set this as a shortcut for post save signal receivers
        txn.domain = report.domain
    txn.save()
    return txn
示例#5
0
def _get_model_for_stock_transaction(report, transaction_helper, ledger_db):
    assert report.type in const.VALID_REPORT_TYPES
    txn = StockTransaction(
        report=report,
        case_id=transaction_helper.case_id,
        section_id=transaction_helper.section_id,
        product_id=transaction_helper.product_id,
        type=transaction_helper.action,
        subtype=transaction_helper.subaction,
    )

    def lazy_original_balance():
        return ledger_db.get_current_ledger_value(txn.ledger_reference)

    new_ledger_values = compute_ledger_values(
        lazy_original_balance, report.type,
        transaction_helper.relative_quantity)

    txn.stock_on_hand = new_ledger_values.balance
    txn.quantity = new_ledger_values.delta

    if report.domain:
        # set this as a shortcut for post save signal receivers
        txn.__domain = report.domain

    # update the ledger DB in case later transactions reference the same ledger item
    ledger_db.set_ledger(txn)
    return txn
示例#6
0
def _receipt_report(case_id, product_id, amount, days_ago):
    report = StockReport.objects.create(form_id=uuid.uuid4().hex, date=ago(days_ago),
                                        type=const.REPORT_TYPE_TRANSFER)
    txn = StockTransaction(
        report=report,
        section_id=const.SECTION_TYPE_STOCK,
        type=const.TRANSACTION_TYPE_RECEIPTS,
        case_id=case_id,
        product_id=product_id,
        quantity=amount,
    )
    previous_transaction = txn.get_previous_transaction()
    txn.stock_on_hand = (previous_transaction.stock_on_hand if previous_transaction else 0) + txn.quantity
    txn.save()
示例#7
0
def _stock_report(case_id, product_id, amount, days_ago):
    report = StockReport.objects.create(form_id=uuid.uuid4().hex, date=ago(days_ago),
                                        type=const.REPORT_TYPE_BALANCE)
    txn = StockTransaction(
        report=report,
        section_id=const.SECTION_TYPE_STOCK,
        type=const.TRANSACTION_TYPE_STOCKONHAND,
        case_id=case_id,
        product_id=product_id,
        stock_on_hand=Decimal(amount),
    )
    txn._test_config = ConsumptionConfiguration.test_config()
    txn.quantity = 0
    txn.save()
示例#8
0
def create_stock_report(location, products_quantities, date=None):
    date = date or datetime.utcnow()
    sql_location = location.sql_location
    report = StockReport.objects.create(form_id='ews-reminders-test',
                                        domain=sql_location.domain,
                                        type='balance',
                                        date=date,
                                        server_date=date)
    for product_code, quantity in products_quantities.iteritems():
        StockTransaction(stock_on_hand=Decimal(quantity),
                         report=report,
                         type='stockonhand',
                         section_id='stock',
                         case_id=sql_location.supply_point_id,
                         product_id=SQLProduct.objects.get(
                             domain=sql_location.domain,
                             code=product_code).product_id).save()
示例#9
0
    section_id = 'stock'
    params = dict(
        form_id='logistics-xform',
        date=force_to_datetime(stocktransaction.date),
        type='balance',
        domain=domain
    )
    try:
        report, _ = StockReport.objects.get_or_create(**params)
    except StockReport.MultipleObjectsReturned:
        # legacy
        report = StockReport.objects.filter(**params)[0]

    sql_product = SQLProduct.objects.get(code=stocktransaction.product, domain=domain)
    if stocktransaction.report_type.lower() == 'stock received':
        transactions.append(StockTransaction(
            case_id=case.get_id,
            product_id=sql_product.product_id,
            sql_product=sql_product,
            section_id=section_id,
            type='receipts',
            stock_on_hand=Decimal(stocktransaction.ending_balance),
            quantity=Decimal(stocktransaction.quantity),
            report=report
        ))
    elif stocktransaction.report_type.lower() == 'stock on hand':
        if stocktransaction.quantity < 0 and bulk:
            transactions.append(StockTransaction(
                case_id=case.get_id,
                product_id=sql_product.product_id,
                sql_product=sql_product,
                section_id=section_id,