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

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

Add complete flag to all batches

you don't have to use it, but it's there if you want it

  • Property mode set to 100644
File size: 5.6 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    purchase_uuid = sa.Column(sa.String(length=32), nullable=True)
56
57    purchase = orm.relationship(
58        Purchase,
59        doc="""
60        Reference to the purchase with which the batch is associated.  May be
61        null, e.g. in the case of a "new purchase" batch.
62        """,
63        backref=orm.backref(
64            'batches',
65            order_by='PurchaseBatch.id',
66            doc="""
67            List of batches associated with the purchase.
68            """))
69
70    mode = sa.Column(sa.Integer(), nullable=False, doc="""
71    Numeric "mode" for the purchase batch, to indicate new/receiving etc.
72    """)
73
74
75class PurchaseBatchRow(BatchRowMixin, PurchaseItemBase, Base):
76    """
77    Row of data within a purchase batch.
78    """
79    __tablename__ = 'purchase_batch_row'
80    __batch_class__ = PurchaseBatch
81
82    @declared_attr
83    def __table_args__(cls):
84        return cls.__batchrow_table_args__() + cls.__purchaseitem_table_args__() + (
85            sa.ForeignKeyConstraint(['item_uuid'], ['purchase_item.uuid'], name='purchase_batch_row_fk_item'),
86        )
87
88    STATUS_OK                           = 1
89    STATUS_PRODUCT_NOT_FOUND            = 2
90    STATUS_COST_NOT_FOUND               = 3
91    STATUS_CASE_QUANTITY_UNKNOWN        = 4
92    STATUS_INCOMPLETE                   = 5
93    STATUS_ORDERED_RECEIVED_DIFFER      = 6
94
95    STATUS = {
96        STATUS_OK                       : "ok",
97        STATUS_PRODUCT_NOT_FOUND        : "product not found",
98        STATUS_COST_NOT_FOUND           : "product found but not cost",
99        STATUS_CASE_QUANTITY_UNKNOWN    : "case quantity not known",
100        STATUS_INCOMPLETE               : "incomplete",
101        STATUS_ORDERED_RECEIVED_DIFFER  : "ordered / received differ",
102    }
103
104    item_uuid = sa.Column(sa.String(length=32), nullable=True)
105
106    item = orm.relationship(
107        PurchaseItem,
108        doc="""
109        Reference to the purchase item with which the batch row is associated.
110        May be null, e.g. in the case of a "new purchase" batch.
111        """)
112
113
114class PurchaseBatchCredit(PurchaseCreditBase, Base):
115    """
116    Represents a working copy of purchase credit tied to a batch row.
117    """
118    __tablename__ = 'purchase_batch_credit'
119
120    @declared_attr
121    def __table_args__(cls):
122        return cls.__purchasecredit_table_args__() + (
123            sa.ForeignKeyConstraint(['row_uuid'], ['purchase_batch_row.uuid'], name='purchase_batch_credit_fk_row'),
124        )
125
126    uuid = uuid_column()
127
128    row_uuid = sa.Column(sa.String(length=32), nullable=True)
129
130    row = orm.relationship(
131        PurchaseBatchRow,
132        doc="""
133        Reference to the batch row with which the credit is associated.
134        """,
135        backref=orm.backref(
136            'credits',
137            doc="""
138            List of :class:`PurchaseBatchCredit` instances for the row.
139            """))
140
141    def __unicode__(self):
142        if self.cases_shorted is not None and self.units_shorted is not None:
143            qty = "{} cases, {} units".format(
144                pretty_quantity(self.cases_shorted),
145                pretty_quantity(self.units_shorted))
146        elif self.cases_shorted is not None:
147            qty = "{} cases".format(pretty_quantity(self.cases_shorted))
148        elif self.units_shorted is not None:
149            qty = "{} units".format(pretty_quantity(self.units_shorted))
150        else:
151            qty = "??"
152        qty += " {}".format(self.credit_type)
153        if self.credit_type == 'expired' and self.expiration_date:
154            qty += " ({})".format(self.expiration_date)
155        if self.credit_type == 'mispick' and self.mispick_product:
156            qty += " ({})".format(self.mispick_product)
157        if self.invoice_total:
158            return "{} = ${:0.2f}".format(qty, self.invoice_total)
159        return qty
Note: See TracBrowser for help on using the repository browser.