source: rattail/rattail/db/model/batch/purchase.py @ aea69e7

Last change on this file since aea69e7 was aea69e7, checked in by Lance Edgar <lance@…>, 3 years ago

Add custom status for purchasing batches

for now, just want to indicate if batch contains any "unknown" products

  • Property mode set to 100644
File size: 5.8 KB
Line 
1# -*- coding: utf-8 -*-
2################################################################################
3#
4#  Rattail -- Retail Software Framework
5#  Copyright © 2010-2017 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"""
24Models for purchase order batches
25"""
26
27from __future__ import unicode_literals, absolute_import
28
29import sqlalchemy as sa
30from sqlalchemy import orm
31from sqlalchemy.ext.declarative import declared_attr
32
33from rattail.db.model import (Base, uuid_column, BatchMixin, BatchRowMixin,
34                              PurchaseBase, PurchaseItemBase, PurchaseCreditBase,
35                              Purchase, PurchaseItem)
36from rattail.util import pretty_quantity
37
38
39class PurchaseBatch(BatchMixin, PurchaseBase, Base):
40    """
41    Hopefully generic batch used for entering new purchases into the system, etc.?
42    """
43    batch_key = 'purchase'
44    __tablename__ = 'purchase_batch'
45    __batchrow_class__ = 'PurchaseBatchRow'
46    model_title = "Purchasing Batch"
47    model_title_plural = "Purchasing Batches"
48
49    @declared_attr
50    def __table_args__(cls):
51        return cls.__batch_table_args__() + cls.__purchase_table_args__() + (
52            sa.ForeignKeyConstraint(['purchase_uuid'], ['purchase.uuid'], name='purchase_batch_fk_purchase'),
53        )
54
55    STATUS_OK                   = 1
56    STATUS_UNKNOWN_PRODUCT      = 2
57
58    STATUS = {
59        STATUS_OK:              "ok",
60        STATUS_UNKNOWN_PRODUCT: "has unknown product(s)",
61    }
62
63    purchase_uuid = sa.Column(sa.String(length=32), nullable=True)
64
65    purchase = orm.relationship(
66        Purchase,
67        doc="""
68        Reference to the purchase with which the batch is associated.  May be
69        null, e.g. in the case of a "new purchase" batch.
70        """,
71        backref=orm.backref(
72            'batches',
73            order_by='PurchaseBatch.id',
74            doc="""
75            List of batches associated with the purchase.
76            """))
77
78    mode = sa.Column(sa.Integer(), nullable=False, doc="""
79    Numeric "mode" for the purchase batch, to indicate new/receiving etc.
80    """)
81
82
83class PurchaseBatchRow(BatchRowMixin, PurchaseItemBase, Base):
84    """
85    Row of data within a purchase batch.
86    """
87    __tablename__ = 'purchase_batch_row'
88    __batch_class__ = PurchaseBatch
89
90    @declared_attr
91    def __table_args__(cls):
92        return cls.__batchrow_table_args__() + cls.__purchaseitem_table_args__() + (
93            sa.ForeignKeyConstraint(['item_uuid'], ['purchase_item.uuid'], name='purchase_batch_row_fk_item'),
94        )
95
96    STATUS_OK                           = 1
97    STATUS_PRODUCT_NOT_FOUND            = 2
98    STATUS_COST_NOT_FOUND               = 3
99    STATUS_CASE_QUANTITY_UNKNOWN        = 4
100    STATUS_INCOMPLETE                   = 5
101    STATUS_ORDERED_RECEIVED_DIFFER      = 6
102
103    STATUS = {
104        STATUS_OK                       : "ok",
105        STATUS_PRODUCT_NOT_FOUND        : "product not found",
106        STATUS_COST_NOT_FOUND           : "product found but not cost",
107        STATUS_CASE_QUANTITY_UNKNOWN    : "case quantity not known",
108        STATUS_INCOMPLETE               : "incomplete",
109        STATUS_ORDERED_RECEIVED_DIFFER  : "ordered / received differ",
110    }
111
112    item_uuid = sa.Column(sa.String(length=32), nullable=True)
113
114    item = orm.relationship(
115        PurchaseItem,
116        doc="""
117        Reference to the purchase item with which the batch row is associated.
118        May be null, e.g. in the case of a "new purchase" batch.
119        """)
120
121
122class PurchaseBatchCredit(PurchaseCreditBase, Base):
123    """
124    Represents a working copy of purchase credit tied to a batch row.
125    """
126    __tablename__ = 'purchase_batch_credit'
127
128    @declared_attr
129    def __table_args__(cls):
130        return cls.__purchasecredit_table_args__() + (
131            sa.ForeignKeyConstraint(['row_uuid'], ['purchase_batch_row.uuid'], name='purchase_batch_credit_fk_row'),
132        )
133
134    uuid = uuid_column()
135
136    row_uuid = sa.Column(sa.String(length=32), nullable=True)
137
138    row = orm.relationship(
139        PurchaseBatchRow,
140        doc="""
141        Reference to the batch row with which the credit is associated.
142        """,
143        backref=orm.backref(
144            'credits',
145            doc="""
146            List of :class:`PurchaseBatchCredit` instances for the row.
147            """))
148
149    def __unicode__(self):
150        if self.cases_shorted is not None and self.units_shorted is not None:
151            qty = "{} cases, {} units".format(
152                pretty_quantity(self.cases_shorted),
153                pretty_quantity(self.units_shorted))
154        elif self.cases_shorted is not None:
155            qty = "{} cases".format(pretty_quantity(self.cases_shorted))
156        elif self.units_shorted is not None:
157            qty = "{} units".format(pretty_quantity(self.units_shorted))
158        else:
159            qty = "??"
160        qty += " {}".format(self.credit_type)
161        if self.credit_type == 'expired' and self.expiration_date:
162            qty += " ({})".format(self.expiration_date)
163        if self.credit_type == 'mispick' and self.mispick_product:
164            qty += " ({})".format(self.mispick_product)
165        if self.invoice_total:
166            return "{} = ${:0.2f}".format(qty, self.invoice_total)
167        return qty
Note: See TracBrowser for help on using the repository browser.