File size: 4,653 Bytes
ce4b573 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | from collections import namedtuple
from dimagi.ext import jsonobject
from dimagi.utils import parsing as dateparse
from django.utils.translation import ugettext as _
from corehq.apps.commtrack import const
from corehq.apps.commtrack.const import StockActions
from corehq.apps.commtrack.exceptions import MissingProductId
from corehq.apps.commtrack.models import CommtrackActionConfig
from corehq.apps.commtrack.xmlutil import XML
from corehq.apps.products.models import Product
class UniqueLedgerReference(namedtuple('UniqueLedgerReference', ['case_id', 'section_id', 'entry_id'])):
def as_id(self):
return '/'.join(self)
@classmethod
def from_id(cls, id_string):
return UniqueLedgerReference(*id_string.split('/'))
class StockReportHelper(jsonobject.JsonObject):
"""
Intermediate class for dealing with stock XML
"""
domain = jsonobject.StringProperty()
form_id = jsonobject.StringProperty()
timestamp = jsonobject.DateTimeProperty()
tag = jsonobject.StringProperty()
transactions = jsonobject.ListProperty(lambda: StockTransactionHelper)
server_date = jsonobject.DateTimeProperty()
deprecated = jsonobject.BooleanProperty()
@property
def report_type(self):
# this is for callers to be able to use a less confusing name
return self.tag
@classmethod
def make_from_form(cls, form, timestamp, tag, transactions):
deprecated = form.is_deprecated
return cls(
domain=form.domain,
form_id=form.form_id if not deprecated else form.orig_id,
timestamp=timestamp,
tag=tag,
transactions=transactions,
server_date=form.received_on,
deprecated=deprecated,
)
def validate(self):
"""
Validates this object as best we can and raises Exceptions if we find anything invalid .
"""
if any(transaction_helper.product_id in ('', None) for transaction_helper in self.transactions):
raise MissingProductId(_('Product IDs must be set for all ledger updates!'))
class StockTransactionHelper(jsonobject.JsonObject):
"""
Helper class for transactions
"""
product_id = jsonobject.StringProperty()
action = jsonobject.StringProperty()
subaction = jsonobject.StringProperty()
domain = jsonobject.StringProperty()
quantity = jsonobject.DecimalProperty()
# todo: this field is never populated during normal form submissions, only on SMS submissions
location_id = jsonobject.StringProperty()
timestamp = jsonobject.DateTimeProperty()
case_id = jsonobject.StringProperty()
section_id = jsonobject.StringProperty()
@property
def ledger_reference(self):
return UniqueLedgerReference(
case_id=self.case_id, section_id=self.section_id, entry_id=self.product_id
)
@property
def relative_quantity(self):
"""
Gets the quantity of this transaction as a positive or negative number
depending on the action/context
"""
if self.action == const.StockActions.CONSUMPTION:
return -self.quantity
else:
return self.quantity
def action_config(self, commtrack_config):
action = CommtrackActionConfig(action=self.action,
subaction=self.subaction)
for a in commtrack_config.all_actions:
if a.name == action.name:
return a
return None
@property
def date(self):
if self.timestamp:
return dateparse.json_format_datetime(self.timestamp)
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),
)
@property
def category(self):
return 'stock'
def fragment(self):
"""
A short string representation of this to be used in sms correspondence
"""
if self.quantity is not None:
quant = self.quantity
else:
quant = ''
# FIXME product fetch here is inefficient
return '%s%s' % (Product.get(self.product_id).code.lower(), quant)
def __repr__(self):
return '{action} ({subaction}): {quantity} (loc: {location_id}, product: {product_id})'.format(
action=self.action,
subaction=self.subaction,
quantity=self.quantity,
location_id=self.location_id,
product_id=self.product_id,
)
|