source: rattail/rattail/db/model/batch/purchase.py @ 4db05e9

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

Refactor truck dump "claiming" a bit, add "case quantity differs" status

  • Property mode set to 100644
File size: 10.7 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"""
24Models for purchase order batches
25"""
26
27from __future__ import unicode_literals, absolute_import
28
29import six
30import sqlalchemy as sa
31from sqlalchemy import orm
32from sqlalchemy.ext.declarative import declared_attr
33
34from rattail.db.model import (Base, uuid_column, BatchMixin, BatchRowMixin,
35                              PurchaseBase, PurchaseItemBase, PurchaseCreditBase,
36                              Purchase, PurchaseItem)
37from rattail.db.model.batch import filename_column
38from rattail.util import pretty_quantity
39
40
41class PurchaseBatch(BatchMixin, PurchaseBase, Base):
42    """
43    Hopefully generic batch used for entering new purchases into the system, etc.?
44    """
45    batch_key = 'purchase'
46    __tablename__ = 'purchase_batch'
47    __batchrow_class__ = 'PurchaseBatchRow'
48    model_title = "Purchasing Batch"
49    model_title_plural = "Purchasing Batches"
50
51    @declared_attr
52    def __table_args__(cls):
53        return cls.__batch_table_args__() + cls.__purchase_table_args__() + (
54            sa.ForeignKeyConstraint(['purchase_uuid'], ['purchase.uuid'], name='purchase_batch_fk_purchase'),
55            sa.ForeignKeyConstraint(['truck_dump_batch_uuid'], ['purchase_batch.uuid'], name='purchase_batch_fk_truck_dump_batch', use_alter=True),
56        )
57
58    STATUS_OK                   = 1
59    STATUS_UNKNOWN_PRODUCT      = 2
60    STATUS_TRUCKDUMP_UNCLAIMED  = 3
61    STATUS_TRUCKDUMP_CLAIMED    = 4
62
63    STATUS = {
64        STATUS_OK                       : "ok",
65        STATUS_UNKNOWN_PRODUCT          : "has unknown product(s)",
66        STATUS_TRUCKDUMP_UNCLAIMED      : "not yet fully claimed",
67        STATUS_TRUCKDUMP_CLAIMED        : "fully claimed by child(ren)",
68    }
69
70    purchase_uuid = sa.Column(sa.String(length=32), nullable=True)
71
72    purchase = orm.relationship(
73        Purchase,
74        doc="""
75        Reference to the purchase with which the batch is associated.  May be
76        null, e.g. in the case of a "new purchase" batch.
77        """,
78        backref=orm.backref(
79            'batches',
80            order_by='PurchaseBatch.id',
81            doc="""
82            List of batches associated with the purchase.
83            """))
84
85    mode = sa.Column(sa.Integer(), nullable=False, doc="""
86    Numeric "mode" for the purchase batch, to indicate new/receiving etc.
87    """)
88
89    invoice_file = filename_column(doc="Base name for the associated invoice file, if any.")
90
91    invoice_parser_key = sa.Column(sa.String(length=100), nullable=True, doc="""
92    The key of the parser used to read the contents of the invoice file.
93    """)
94
95    truck_dump = sa.Column(sa.Boolean(), nullable=True, default=False, doc="""
96    Flag indicating whether a "receiving" batch is of the "truck dump"
97    persuasion, i.e.  it does not correspond to a single purchase order but
98    rather is assumed to represent multiple orders.
99    """)
100
101    truck_dump_batch_uuid = sa.Column(sa.String(length=32), nullable=True)
102    truck_dump_batch = orm.relationship(
103        'PurchaseBatch',
104        remote_side='PurchaseBatch.uuid',
105        doc="""
106        Reference to the "truck dump" receiving batch, for which the current
107        batch represents a single invoice which partially "consumes" the truck
108        dump.
109        """,
110        backref=orm.backref(
111            'truck_dump_children',
112            order_by='PurchaseBatch.id',
113            doc="""
114            List of batches which are "children" of the current batch, which is
115            assumed to be a truck dump.
116            """))
117
118
119class PurchaseBatchRow(BatchRowMixin, PurchaseItemBase, Base):
120    """
121    Row of data within a purchase batch.
122    """
123    __tablename__ = 'purchase_batch_row'
124    __batch_class__ = PurchaseBatch
125
126    @declared_attr
127    def __table_args__(cls):
128        return cls.__batchrow_table_args__() + cls.__purchaseitem_table_args__() + (
129            sa.ForeignKeyConstraint(['item_uuid'], ['purchase_item.uuid'], name='purchase_batch_row_fk_item'),
130        )
131
132    STATUS_OK                           = 1
133    STATUS_PRODUCT_NOT_FOUND            = 2
134    STATUS_COST_NOT_FOUND               = 3
135    STATUS_CASE_QUANTITY_UNKNOWN        = 4
136    STATUS_INCOMPLETE                   = 5
137    STATUS_ORDERED_RECEIVED_DIFFER      = 6
138    STATUS_TRUCKDUMP_UNCLAIMED          = 7
139    STATUS_TRUCKDUMP_CLAIMED            = 8
140    STATUS_TRUCKDUMP_OVERCLAIMED        = 9
141    STATUS_CASE_QUANTITY_DIFFERS        = 10
142
143    STATUS = {
144        STATUS_OK                       : "ok",
145        STATUS_PRODUCT_NOT_FOUND        : "product not found",
146        STATUS_COST_NOT_FOUND           : "product found but not cost",
147        STATUS_CASE_QUANTITY_UNKNOWN    : "case quantity not known",
148        STATUS_CASE_QUANTITY_DIFFERS    : "case quantity differs",
149        STATUS_INCOMPLETE               : "incomplete",
150        STATUS_ORDERED_RECEIVED_DIFFER  : "ordered / received differ",
151        STATUS_TRUCKDUMP_UNCLAIMED      : "not yet fully claimed",
152        STATUS_TRUCKDUMP_CLAIMED        : "fully claimed by child(ren)",
153        STATUS_TRUCKDUMP_OVERCLAIMED    : "OVER claimed by child(ren)",
154    }
155
156    item_uuid = sa.Column(sa.String(length=32), nullable=True)
157
158    item = orm.relationship(
159        PurchaseItem,
160        doc="""
161        Reference to the purchase item with which the batch row is associated.
162        May be null, e.g. in the case of a "new purchase" batch.
163        """)
164
165
166class PurchaseBatchRowClaim(Base):
167    """
168    Represents the connection between a row(s) from a truck dump batch, and the
169    corresponding "child" batch row which claims it, as well as the claimed
170    quantities etc.
171    """
172    __tablename__ = 'purchase_batch_row_claim'
173    __table_args__ = (
174        sa.ForeignKeyConstraint(['claiming_row_uuid'], ['purchase_batch_row.uuid'], name='purchase_batch_row_claim_fk_claiming_row'),
175        sa.ForeignKeyConstraint(['claimed_row_uuid'], ['purchase_batch_row.uuid'], name='purchase_batch_row_claim_fk_claimed_row'),
176    )
177
178    uuid = uuid_column()
179
180    claiming_row_uuid = sa.Column(sa.String(length=32), nullable=False)
181    claiming_row = orm.relationship(
182        PurchaseBatchRow,
183        foreign_keys='PurchaseBatchRowClaim.claiming_row_uuid',
184        doc="""
185        Reference to the "child" row which is claiming some row from a truck
186        dump batch.
187        """,
188        backref=orm.backref(
189            'truck_dump_claims',
190            cascade='all, delete-orphan',
191            doc="""
192            List of claims which this "child" row makes against rows within a
193            truck dump batch.
194            """))
195
196    claimed_row_uuid = sa.Column(sa.String(length=32), nullable=False)
197    claimed_row = orm.relationship(
198        PurchaseBatchRow,
199        foreign_keys='PurchaseBatchRowClaim.claimed_row_uuid',
200        doc="""
201        Reference to the truck dump batch row which is claimed by the "child" row.
202        """,
203        backref=orm.backref(
204            'claims',
205            # cascade='all, delete-orphan',
206            doc="""
207            List of claims made by "child" rows against this truck dump batch row.
208            """))
209
210    cases_received = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
211    Number of cases of product which were ultimately received, and are involved in the claim.
212    """)
213
214    units_received = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
215    Number of units of product which were ultimately received, and are involved in the claim.
216    """)
217
218    cases_damaged = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
219    Number of cases of product which were shipped damaged, and are involved in the claim.
220    """)
221
222    units_damaged = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
223    Number of units of product which were shipped damaged, and are involved in the claim.
224    """)
225
226    cases_expired = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
227    Number of cases of product which were shipped expired, and are involved in the claim.
228    """)
229
230    units_expired = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
231    Number of units of product which were shipped expired, and are involved in the claim.
232    """)
233
234
235@six.python_2_unicode_compatible
236class PurchaseBatchCredit(PurchaseCreditBase, Base):
237    """
238    Represents a working copy of purchase credit tied to a batch row.
239    """
240    __tablename__ = 'purchase_batch_credit'
241
242    @declared_attr
243    def __table_args__(cls):
244        return cls.__purchasecredit_table_args__() + (
245            sa.ForeignKeyConstraint(['row_uuid'], ['purchase_batch_row.uuid'], name='purchase_batch_credit_fk_row'),
246        )
247
248    uuid = uuid_column()
249
250    row_uuid = sa.Column(sa.String(length=32), nullable=True)
251
252    row = orm.relationship(
253        PurchaseBatchRow,
254        doc="""
255        Reference to the batch row with which the credit is associated.
256        """,
257        backref=orm.backref(
258            'credits',
259            doc="""
260            List of :class:`PurchaseBatchCredit` instances for the row.
261            """))
262
263    def __str__(self):
264        if self.cases_shorted is not None and self.units_shorted is not None:
265            qty = "{} cases, {} units".format(
266                pretty_quantity(self.cases_shorted),
267                pretty_quantity(self.units_shorted))
268        elif self.cases_shorted is not None:
269            qty = "{} cases".format(pretty_quantity(self.cases_shorted))
270        elif self.units_shorted is not None:
271            qty = "{} units".format(pretty_quantity(self.units_shorted))
272        else:
273            qty = "??"
274        qty += " {}".format(self.credit_type)
275        if self.credit_type == 'expired' and self.expiration_date:
276            qty += " ({})".format(self.expiration_date)
277        if self.credit_type == 'mispick' and self.mispick_product:
278            qty += " ({})".format(self.mispick_product)
279        if self.invoice_total:
280            return "{} = ${:0.2f}".format(qty, self.invoice_total)
281        return qty
Note: See TracBrowser for help on using the repository browser.