本文整理汇总了Python中casexml.apps.stock.models.StockTransaction类的典型用法代码示例。如果您正苦于以下问题:Python StockTransaction类的具体用法?Python StockTransaction怎么用?Python StockTransaction使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StockTransaction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_model_for_stock_transaction
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_balance(_stock_transaction_to_unique_ledger_reference(txn))
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_current_balance(_stock_transaction_to_unique_ledger_reference(txn), txn.stock_on_hand)
return txn
示例2: _test_subtype
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)
示例3: _stock_report
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()
示例4: _receipt_report
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()
示例5: get_current_ledger_state
def get_current_ledger_state(case_ids, ensure_form_id=False):
"""
Given a list of cases returns a dict of all current ledger data of the following format:
{
"case_id": {
"section_id": {
"product_id": StockState,
"product_id": StockState,
...
},
...
},
...
}
:param ensure_form_id: Set to True to make sure return StockState
have the ``last_modified_form_id`` field populated
"""
from corehq.apps.commtrack.models import StockState
if not case_ids:
return {}
states = StockState.objects.filter(
case_id__in=case_ids
)
ret = {case_id: {} for case_id in case_ids}
for state in states:
sections = ret[state.case_id].setdefault(state.section_id, {})
sections[state.product_id] = state
if ensure_form_id and not state.last_modified_form_id:
transaction = StockTransaction.latest(state.case_id, state.section_id, state.product_id)
if transaction is not None:
state.last_modified_form_id = transaction.report.form_id
state.save()
return ret
示例6: get_valid_reports
def get_valid_reports(self, data):
filtered_transactions = []
excluded_products = []
for product_id, transactions in get_transactions_by_product(data["transactions"]).iteritems():
begin_soh = None
end_soh = None
receipt = 0
for transaction in transactions:
if begin_soh is None:
sql_location = SQLLocation.objects.get(location_id=transaction.location_id)
latest = StockTransaction.latest(
sql_location.supply_point_id, SECTION_TYPE_STOCK, transaction.product_id
)
begin_soh = 0
if latest:
begin_soh = float(latest.stock_on_hand)
if transaction.action == "receipts":
receipt += float(transaction.quantity)
elif not end_soh:
end_soh = float(transaction.quantity)
if end_soh > begin_soh + receipt:
excluded_products.append(transaction.product_id)
else:
filtered_transactions.append(transaction)
if excluded_products:
message = ERROR_MESSAGE.format(
products_list=", ".join(
[SQLProduct.objects.get(product_id=product_id).code for product_id in set(excluded_products)]
)
)
self.respond(message)
return filtered_transactions
示例7: get_data
def get_data(self):
sp_ids = get_relevant_supply_point_ids(
self.domain,
self.active_location
)
products = Product.by_domain(self.domain)
if self.program_id:
products = filter(
lambda product: product.program_id == self.program_id, products
)
for sp_id in sp_ids:
for product in products:
loc = SupplyPointCase.get(sp_id).location
last_transaction = StockTransaction.latest(
sp_id,
STOCK_SECTION_TYPE,
product._id
)
yield {
'loc_id': loc._id,
'loc_path': loc.path,
'name': loc.name,
'type': loc.location_type,
'reporting_status': reporting_status(
last_transaction,
self.start_date,
self.end_date
),
'geo': loc._geopoint,
}
示例8: get_current_ledger_value
def get_current_ledger_value(self, unique_ledger_reference):
latest_txn = StockTransaction.latest(
case_id=unique_ledger_reference.case_id,
section_id=unique_ledger_reference.section_id,
product_id=unique_ledger_reference.entry_id,
)
return latest_txn.stock_on_hand if latest_txn else 0
示例9: get_stock_state_for_transaction
def get_stock_state_for_transaction(transaction):
from corehq.apps.commtrack.models import StockState
from corehq.apps.locations.models import SQLLocation
from corehq.apps.products.models import SQLProduct
# todo: in the worst case, this function makes
# - three calls to couch (for the case, domain, and commtrack config)
# - four postgres queries (transacitons, product, location, and state)
# - one postgres write (to save the state)
# and that doesn't even include the consumption calc, which can do a whole
# bunch more work and hit the database.
sql_product = SQLProduct.objects.get(product_id=transaction.product_id)
try:
domain_name = transaction.__domain
except AttributeError:
domain_name = sql_product.domain
try:
sql_location = SQLLocation.objects.get(supply_point_id=transaction.case_id)
except SQLLocation.DoesNotExist:
sql_location = None
try:
state = StockState.include_archived.get(
section_id=transaction.section_id,
case_id=transaction.case_id,
product_id=transaction.product_id,
)
except StockState.DoesNotExist:
state = StockState(
section_id=transaction.section_id,
case_id=transaction.case_id,
product_id=transaction.product_id,
sql_product=sql_product,
sql_location=sql_location,
)
# we may not be saving the latest transaction so make sure we use that
# todo: this should change to server date
latest_transaction = StockTransaction.latest(
case_id=transaction.case_id,
section_id=transaction.section_id,
product_id=transaction.product_id
)
if latest_transaction != transaction:
logging.warning(
'Just fired signal for a stale stock transaction. Domain: {}, instance: {},latest was {}'.format(
domain_name, transaction, latest_transaction
)
)
transaction = latest_transaction
state.last_modified_date = transaction.report.server_date
state.last_modified_form_id = transaction.report.form_id
state.stock_on_hand = transaction.stock_on_hand
# so you don't have to look it up again in the signal receivers
if domain_name:
state.__domain = domain_name
return state
示例10: _get_ledger
def _get_ledger(self, unique_ledger_reference):
try:
return StockTransaction.latest(
case_id=unique_ledger_reference.case_id,
section_id=unique_ledger_reference.section_id,
product_id=unique_ledger_reference.entry_id,
)
except StockTransaction.DoesNotExist:
return None
示例11: check_stock_models
def check_stock_models(self, case, product_id, expected_soh, expected_qty, section_id):
if not isinstance(expected_qty, Decimal):
expected_qty = Decimal(str(expected_qty))
if not isinstance(expected_soh, Decimal):
expected_soh = Decimal(str(expected_soh))
latest_trans = StockTransaction.latest(case._id, section_id, product_id)
self.assertIsNotNone(latest_trans)
self.assertEqual(section_id, latest_trans.section_id)
self.assertEqual(expected_soh, latest_trans.stock_on_hand)
self.assertEqual(expected_qty, latest_trans.quantity)
示例12: sync_stock_transactions_for_facility
def sync_stock_transactions_for_facility(domain, endpoint, facility, checkpoint,
date, limit=1000, offset=0):
"""
Syncs stock data from StockTransaction objects in ILSGateway to StockTransaction objects in HQ
"""
has_next = True
next_url = ""
section_id = 'stock'
supply_point = facility
case = get_supply_point_case_in_domain_by_id(domain, supply_point)
if not case:
return
location_id = case.location_id
save_stock_data_checkpoint(checkpoint, 'stock_transaction', limit, offset, date, location_id, True)
products_saved = set()
while has_next:
meta, stocktransactions = endpoint.get_stocktransactions(
next_url_params=next_url,
limit=limit,
offset=offset,
filters={
'supply_point': supply_point,
'date__gte': date,
'date__lte': checkpoint.start_date
}
)
# set the checkpoint right before the data we are about to process
meta_limit = meta.get('limit') or limit
meta_offset = meta.get('offset') or offset
save_stock_data_checkpoint(
checkpoint, 'stock_transaction', meta_limit, meta_offset, date, location_id, True
)
transactions_to_add = []
with transaction.atomic():
for stocktransaction in stocktransactions:
transactions = sync_stock_transaction(stocktransaction, domain, case, bulk=True)
transactions_to_add.extend(transactions)
products_saved.update(map(lambda x: x.product_id, transactions))
if transactions_to_add:
# Doesn't send signal
StockTransaction.objects.bulk_create(transactions_to_add)
if not meta.get('next', False):
has_next = False
else:
next_url = meta['next'].split('?')[1]
for product in products_saved:
# if we saved anything rebuild the stock state object by firing the signal
# on the last transaction for each product
last_st = StockTransaction.latest(case.get_id, section_id, product)
update_stock_state_for_transaction(last_st)
示例13: _create_stock_state
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()
示例14: _create_model_for_stock_transaction
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
示例15: create_transactions
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()