source: tailbone/tailbone/views/purchases/credits.py @ da10c65

Last change on this file since da10c65 was da10c65, checked in by Lance Edgar <lance@…>, 18 months ago

Add support for new credit_total field for purchase credits

  • Property mode set to 100644
File size: 5.4 KB
Line 
1# -*- coding: utf-8; -*-
2################################################################################
3#
4#  Rattail -- Retail Software Framework
5#  Copyright © 2010-2018 Lance Edgar
6#
7#  This file is part of Rattail.
8#
9#  Rattail is free software: you can redistribute it and/or modify it under the
10#  terms of the GNU General Public License as published by the Free Software
11#  Foundation, either version 3 of the License, or (at your option) any later
12#  version.
13#
14#  Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
15#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16#  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17#  details.
18#
19#  You should have received a copy of the GNU General Public License along with
20#  Rattail.  If not, see <http://www.gnu.org/licenses/>.
21#
22################################################################################
23"""
24Views for "true" purchase credits
25"""
26
27from __future__ import unicode_literals, absolute_import
28
29import six
30
31from rattail.db import model
32
33from webhelpers2.html import tags
34
35from tailbone import grids
36from tailbone.views import MasterView
37
38
39class PurchaseCreditView(MasterView):
40    """
41    Master view for purchase credits
42    """
43    model_class = model.PurchaseCredit
44    route_prefix = 'purchases.credits'
45    url_prefix = '/purchases/credits'
46    creatable = False
47    editable = False
48    checkboxes = True
49
50    grid_columns = [
51        'vendor',
52        'invoice_number',
53        'invoice_date',
54        'upc',
55        'vendor_item_code',
56        'brand_name',
57        'description',
58        'size',
59        'cases_shorted',
60        'units_shorted',
61        'credit_total',
62        'credit_type',
63        'mispick_upc',
64        'date_received',
65        'status',
66    ]
67
68    def configure_grid(self, g):
69        super(PurchaseCreditView, self).configure_grid(g)
70
71        g.set_joiner('vendor', lambda q: q.outerjoin(model.Vendor))
72        g.set_sorter('vendor', model.Vendor.name)
73
74        g.set_sort_defaults('date_received', 'desc')
75
76        g.set_enum('status', self.enum.PURCHASE_CREDIT_STATUS)
77        g.filters['status'].set_value_renderer(grids.filters.EnumValueRenderer(self.enum.PURCHASE_CREDIT_STATUS))
78        g.filters['status'].default_active = True
79        g.filters['status'].default_verb = 'not_equal'
80        # TODO: should not have to convert value to string!
81        g.filters['status'].default_value = six.text_type(self.enum.PURCHASE_CREDIT_STATUS_SATISFIED)
82
83        # g.set_type('upc', 'gpc')
84        g.set_type('cases_shorted', 'quantity')
85        g.set_type('units_shorted', 'quantity')
86        g.set_type('credit_total', 'currency')
87
88        g.set_label('invoice_number', "Invoice No.")
89        g.set_label('upc', "UPC")
90        g.set_label('vendor_item_code', "Item Code")
91        g.set_label('brand_name', "Brand")
92        g.set_label('cases_shorted', "Cases")
93        g.set_label('units_shorted', "Units")
94        g.set_label('credit_type', "Type")
95        g.set_label('mispick_upc', "Mispick UPC")
96        g.set_label('date_received', "Date")
97
98    def change_status(self):
99        if self.request.method != 'POST':
100            self.request.session.flash("Sorry, you must POST to change credit status", 'error')
101            return self.redirect(self.get_index_url())
102
103        status = self.request.POST.get('status', '')
104        if status.isdigit():
105            status = int(status)
106        else:
107            self.request.session.flash("Received invalid status: {}".format(status), 'error')
108            return self.redirect(self.get_index_url())
109
110        credits_ = []
111        for uuid in self.request.POST.get('uuids', '').split(','):
112            uuid = uuid.strip()
113            if uuid:
114                credit = self.Session.query(model.PurchaseCredit).get(uuid)
115                if credit:
116                    credits_.append(credit)
117        if not credits_:
118            self.request.session.flash("Received zero valid credits", 'error')
119            return self.redirect(self.get_index_url())
120
121        # okay, really change status
122        for credit in credits_:
123            credit.status = status
124
125        self.request.session.flash("Changed status for {} credits".format(len(credits_)))
126        return self.redirect(self.get_index_url())
127
128    def template_kwargs_index(self, **kwargs):
129        kwargs['status_options'] = self.status_options()
130        return kwargs
131
132    def status_options(self):
133        options = []
134        for value in sorted(self.enum.PURCHASE_CREDIT_STATUS):
135            options.append(tags.Option(self.enum.PURCHASE_CREDIT_STATUS[value], value))
136        return options
137
138    @classmethod
139    def defaults(cls, config):
140        route_prefix = cls.get_route_prefix()
141        url_prefix = cls.get_url_prefix()
142        permission_prefix = cls.get_permission_prefix()
143        model_title_plural = cls.get_model_title_plural()
144
145        # change status
146        config.add_tailbone_permission(permission_prefix, '{}.change_status'.format(permission_prefix),
147                                       "Change status for {}".format(model_title_plural))
148        config.add_route('{}.change_status'.format(route_prefix), '{}/change-status'.format(url_prefix))
149        config.add_view(cls, attr='change_status', route_name='{}.change_status'.format(route_prefix),
150                        permission='{}.change_status'.format(permission_prefix))
151
152        cls._defaults(config)
153
154
155def includeme(config):
156    PurchaseCreditView.defaults(config)
Note: See TracBrowser for help on using the repository browser.