Changeset ebb395a in rattail for rattail/db/model/batch/purchase.py


Ignore:
Timestamp:
12/10/16 09:05:21 (3 years ago)
Author:
Lance Edgar <lance@…>
Branches:
custorders, master
Children:
b5b9feed
Parents:
ad98038
Message:

Add PurchaseCredit and friends to schema

Also allow missing cost when refreshing purchase batch row

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rattail/db/model/batch/purchase.py

    rad98038 rebb395a  
    3131from sqlalchemy.ext.declarative import declared_attr
    3232
    33 from rattail.db.model import (Base, PurchaseBase, PurchaseItemBase,
    34                               Purchase, PurchaseItem,
    35                               BatchMixin, BatchRowMixin)
     33from rattail.db.model import (Base, uuid_column, BatchMixin, BatchRowMixin,
     34                              PurchaseBase, PurchaseItemBase, PurchaseCreditBase,
     35                              Purchase, PurchaseItem)
     36from rattail.util import pretty_quantity
    3637
    3738
     
    114115        May be null, e.g. in the case of a "new purchase" batch.
    115116        """)
     117
     118
     119class PurchaseBatchCredit(PurchaseCreditBase, Base):
     120    """
     121    Represents a working copy of purchase credit tied to a batch row.
     122    """
     123    __tablename__ = 'purchase_batch_credit'
     124
     125    @declared_attr
     126    def __table_args__(cls):
     127        return cls.__purchasecredit_table_args__() + (
     128            sa.ForeignKeyConstraint(['row_uuid'], ['purchase_batch_row.uuid'], name='purchase_batch_credit_fk_row'),
     129        )
     130
     131    uuid = uuid_column()
     132
     133    row_uuid = sa.Column(sa.String(length=32), nullable=True)
     134
     135    row = orm.relationship(
     136        PurchaseBatchRow,
     137        doc="""
     138        Reference to the batch row with which the credit is associated.
     139        """,
     140        backref=orm.backref(
     141            'credits',
     142            doc="""
     143            List of :class:`PurchaseBatchCredit` instances for the row.
     144            """))
     145
     146    def __unicode__(self):
     147        if self.cases_shorted is not None and self.units_shorted is not None:
     148            qty = "{} cases, {} units".format(
     149                pretty_quantity(self.cases_shorted),
     150                pretty_quantity(self.units_shorted))
     151        elif self.cases_shorted is not None:
     152            qty = "{} cases".format(pretty_quantity(self.cases_shorted))
     153        elif self.units_shorted is not None:
     154            qty = "{} units".format(pretty_quantity(self.units_shorted))
     155        else:
     156            qty = "??"
     157        qty += " {}".format(self.credit_type)
     158        if self.credit_type == 'expired' and self.expiration_date:
     159            qty += " ({})".format(self.expiration_date)
     160        if self.credit_type == 'mispick' and self.mispick_product:
     161            qty += " ({})".format(self.mispick_product)
     162        if self.invoice_total:
     163            return "{} = ${:0.2f}".format(qty, self.invoice_total)
     164        return qty
Note: See TracChangeset for help on using the changeset viewer.