Changeset 05481f7 in tailbone


Ignore:
Timestamp:
02/28/19 16:21:13 (5 months ago)
Author:
Lance Edgar <ledgar@…>
Branches:
master
Children:
a2965d8
Parents:
b1c77af
Message:

Add new "receive row" view for mobile receiving

this frees us up to dumb-down the "view row" which thus far has been tasked
with actual receiving

Location:
tailbone
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • tailbone/views/purchasing/receiving.py

    rb1c77af r05481f7  
    11161116        return "({}) {}".format(key, description)
    11171117
     1118    def make_mobile_row_grid_kwargs(self, **kwargs):
     1119        kwargs = super(ReceivingBatchView, self).make_mobile_row_grid_kwargs(**kwargs)
     1120
     1121        # use custom `receive_row` instead of `view_row`
     1122        # TODO: should still use `view_row` in some cases? e.g. executed batch
     1123        kwargs['url'] = lambda obj: self.get_row_action_url('receive', obj, mobile=True)
     1124
     1125        return kwargs
     1126
    11181127    def should_aggregate_products(self, batch):
    11191128        """
     
    13961405        return self.render_to_response('view_row', context, mobile=True)
    13971406
     1407    def mobile_receive_row(self):
     1408        """
     1409        Mobile view for row-level receiving.
     1410        """
     1411        self.mobile = True
     1412        self.viewing = True
     1413        row = self.get_row_instance()
     1414        batch = row.batch
     1415        permission_prefix = self.get_permission_prefix()
     1416        form = self.make_mobile_row_form(row)
     1417        context = {
     1418            'row': row,
     1419            'batch': batch,
     1420            'parent_instance': batch,
     1421            'instance': row,
     1422            'instance_title': self.get_row_instance_title(row),
     1423            'parent_model_title': self.get_model_title(),
     1424            'product_image_url': self.get_row_image_url(row),
     1425            'form': form,
     1426            'allow_expired': self.handler.allow_expired_credits(),
     1427            'allow_cases': self.handler.allow_cases(),
     1428            'quick_receive': False,
     1429            'quick_receive_all': False,
     1430        }
     1431
     1432        context['quick_receive'] = self.rattail_config.getbool('rattail.batch', 'purchase.mobile_quick_receive',
     1433                                                               default=True)
     1434        if batch.order_quantities_known:
     1435            context['quick_receive_all'] = self.rattail_config.getbool('rattail.batch', 'purchase.mobile_quick_receive_all',
     1436                                                                       default=False)
     1437
     1438        if self.request.has_perm('{}.create_row'.format(permission_prefix)):
     1439            schema = MobileReceivingForm().bind(session=self.Session())
     1440            update_form = forms.Form(schema=schema, request=self.request)
     1441            if update_form.validate(newstyle=True):
     1442                row = self.Session.query(model.PurchaseBatchRow).get(update_form.validated['row'])
     1443                mode = update_form.validated['mode']
     1444                cases = update_form.validated['cases']
     1445                units = update_form.validated['units']
     1446
     1447                # handler takes care of the row receiving logic for us
     1448                kwargs = dict(update_form.validated)
     1449                del kwargs['row']
     1450                self.handler.receive_row(row, **kwargs)
     1451
     1452                # keep track of last-used uom, although we just track
     1453                # whether or not it was 'CS' since the unit_uom can vary
     1454                sticky_case = None
     1455                if not update_form.validated['quick_receive']:
     1456                    if cases and not units:
     1457                        sticky_case = True
     1458                    elif units and not cases:
     1459                        sticky_case = False
     1460                if sticky_case is not None:
     1461                    self.request.session['tailbone.mobile.receiving.sticky_uom_is_case'] = sticky_case
     1462
     1463                return self.redirect(self.get_action_url('view', batch, mobile=True))
     1464
     1465        # unit_uom can vary by product
     1466        context['unit_uom'] = 'LB' if row.product and row.product.weighed else 'EA'
     1467
     1468        if context['quick_receive'] and context['quick_receive_all']:
     1469            if context['allow_cases']:
     1470                context['quick_receive_uom'] = 'CS'
     1471                raise NotImplementedError("TODO: add CS support for quick_receive_all")
     1472            else:
     1473                context['quick_receive_uom'] = context['unit_uom']
     1474                accounted_for = self.handler.get_units_accounted_for(row)
     1475                remainder = self.handler.get_units_ordered(row) - accounted_for
     1476
     1477                if accounted_for:
     1478                    # some product accounted for; button should receive "remainder" only
     1479                    if remainder:
     1480                        remainder = pretty_quantity(remainder)
     1481                        context['quick_receive_quantity'] = remainder
     1482                        context['quick_receive_text'] = "Receive Remainder ({} {})".format(remainder, context['unit_uom'])
     1483                    else:
     1484                        # unless there is no remainder, in which case disable it
     1485                        context['quick_receive'] = False
     1486
     1487                else: # nothing yet accounted for, button should receive "all"
     1488                    if not remainder:
     1489                        raise ValueError("why is remainder empty?")
     1490                    remainder = pretty_quantity(remainder)
     1491                    context['quick_receive_quantity'] = remainder
     1492                    context['quick_receive_text'] = "Receive ALL ({} {})".format(remainder, context['unit_uom'])
     1493
     1494        # effective uom can vary in a few ways...the basic default is 'CS' if
     1495        # self.default_uom_is_case is true, otherwise whatever unit_uom is.
     1496        sticky_case = self.request.session.get('tailbone.mobile.receiving.sticky_uom_is_case')
     1497        if sticky_case is None:
     1498            context['uom'] = 'CS' if self.default_uom_is_case else context['unit_uom']
     1499        elif sticky_case:
     1500            context['uom'] = 'CS'
     1501        else:
     1502            context['uom'] = context['unit_uom']
     1503        if context['uom'] == 'CS' and row.units_ordered and not row.cases_ordered:
     1504            context['uom'] = context['unit_uom']
     1505
     1506        if batch.order_quantities_known and not row.cases_ordered and not row.units_ordered:
     1507            warn = True
     1508            if batch.is_truck_dump_parent() and row.product:
     1509                uuids = [child.uuid for child in batch.truck_dump_children]
     1510                if uuids:
     1511                    count = self.Session.query(model.PurchaseBatchRow)\
     1512                                        .filter(model.PurchaseBatchRow.batch_uuid.in_(uuids))\
     1513                                        .filter(model.PurchaseBatchRow.product == row.product)\
     1514                                        .count()
     1515                    if count:
     1516                        warn = False
     1517            if warn:
     1518                self.request.session.flash("This item was NOT on the original purchase order.", 'receiving-warning')
     1519        return self.render_to_response('receive_row', context, mobile=True)
     1520
    13981521    def auto_receive(self):
    13991522        """
     
    14551578        model_key = cls.get_model_key()
    14561579        permission_prefix = cls.get_permission_prefix()
     1580
     1581        # row-level receiving
     1582        config.add_route('mobile.{}.receive_row'.format(route_prefix), '/mobile{}/{{uuid}}/rows/{{row_uuid}}/receive'.format(url_prefix))
     1583        config.add_view(cls, attr='mobile_receive_row', route_name='mobile.{}.receive_row'.format(route_prefix),
     1584                        permission='{}.edit_row'.format(permission_prefix))
    14571585
    14581586        if cls.allow_truck_dump:
Note: See TracChangeset for help on using the changeset viewer.