Add basic support for "eligible purchases" for receiving batch API

This commit is contained in:
Lance Edgar 2019-11-12 17:46:18 -06:00
parent 3514c4050e
commit a9b740dcaa
3 changed files with 64 additions and 9 deletions

View file

@ -117,9 +117,12 @@ class APIBatchView(APIBatchMixin, APIMasterView):
Here we'll invoke the handler for actual batch creation, instead of
typical logic used for simple records.
"""
user = self.request.user
kwargs = dict(data)
kwargs['user'] = self.request.user
kwargs['user'] = user
batch = self.handler.make_batch(self.Session(), **kwargs)
if self.handler.should_populate(batch):
self.handler.do_populate(batch, user)
return batch
def update_object(self, batch, data):

View file

@ -50,9 +50,18 @@ class ReceivingBatchViews(APIBatchView):
return data
def get_purchase(self, uuid):
return self.Session.query(model.Purchase).get(uuid)
def create_object(self, data):
data = dict(data)
data['mode'] = self.enum.PURCHASE_BATCH_MODE_RECEIVING
# if 'purchase_key' in data:
# purchase = self.get_purchase(data['purchase_key'])
# data['purchase'] = purchase
batch = super(ReceivingBatchViews, self).create_object(data)
return batch
@ -65,6 +74,54 @@ class ReceivingBatchViews(APIBatchView):
def get(self):
return self._get()
def eligible_purchases(self):
uuid = self.request.params.get('vendor_uuid')
vendor = self.Session.query(model.Vendor).get(uuid) if uuid else None
if not vendor:
return {'error': "Vendor not found"}
purchases = self.handler.get_eligible_purchases(
vendor, self.enum.PURCHASE_BATCH_MODE_RECEIVING)
purchases = [self.normalize_eligible_purchase(p)
for p in purchases]
return {'purchases': purchases}
def normalize_eligible_purchase(self, purchase):
return {
'key': purchase.uuid,
'department_uuid': purchase.department_uuid,
'display': self.render_eligible_purchase(purchase),
}
def render_eligible_purchase(self, purchase):
if purchase.status == self.enum.PURCHASE_STATUS_ORDERED:
date = purchase.date_ordered
total = purchase.po_total
elif purchase.status == self.enum.PURCHASE_STATUS_RECEIVED:
date = purchase.date_received
total = purchase.invoice_total
return '{} for ${:0,.2f} ({})'.format(date, total, purchase.department or purchase.buyer)
@classmethod
def defaults(cls, config):
cls._batch_defaults(config)
cls._receiving_batch_defaults(config)
@classmethod
def _receiving_batch_defaults(cls, config):
route_prefix = cls.get_route_prefix()
permission_prefix = cls.get_permission_prefix()
collection_url_prefix = cls.get_collection_url_prefix()
# eligible purchases
config.add_route('{}.eligible_purchases'.format(route_prefix), '{}/eligible-purchases'.format(collection_url_prefix),
request_method='GET')
config.add_view(cls, attr='eligible_purchases', route_name='{}.eligible_purchases'.format(route_prefix),
permission='{}.create'.format(permission_prefix),
renderer='json')
class ReceivingBatchRowViews(APIBatchRowView):

View file

@ -503,15 +503,10 @@ class PurchasingBatchView(BatchMasterView):
if not mode or mode not in self.enum.PURCHASE_BATCH_MODE:
return {'error': "Unknown mode: {}".format(mode)}
purchases = self.Session.query(model.Purchase)\
.filter(model.Purchase.vendor == vendor)
if mode == self.enum.PURCHASE_BATCH_MODE_RECEIVING:
purchases = purchases.filter(model.Purchase.status == self.enum.PURCHASE_STATUS_ORDERED)\
.order_by(model.Purchase.date_ordered, model.Purchase.created)
elif mode == self.enum.PURCHASE_BATCH_MODE_COSTING:
purchases = purchases.filter(model.Purchase.status == self.enum.PURCHASE_STATUS_RECEIVED)\
.order_by(model.Purchase.date_received, model.Purchase.created)
purchases = self.handler.get_eligible_purchases(vendor, mode)
return self.get_eligible_purchases_data(purchases)
def get_eligible_purchases_data(self, purchases):
return {'purchases': [{'key': p.uuid,
'department_uuid': p.department_uuid or '',
'display': self.render_eligible_purchase(p)}