Files
@ 750c5fb7b739
Branch filter:
Location: rattail-project/rattail/rattail/db/model/custorders.py
750c5fb7b739
11.7 KiB
text/x-python
Flush after deleting batch rows
not doing so is *believed* to have caused an error once...
not doing so is *believed* to have caused an error once...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | # -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2021 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Data Models for Customer Orders
"""
from __future__ import unicode_literals, absolute_import
import datetime
import six
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.orderinglist import ordering_list
from sqlalchemy.ext.declarative import declared_attr
from rattail.db.model import Base, uuid_column
from rattail.db.model import Store, Customer, Person, Product, User
from rattail.db.types import GPCType
class CustomerOrderBase(object):
"""
Base class for customer orders; defines common fields.
"""
@declared_attr
def __table_args__(cls):
return cls.__customer_order_table_args__()
@classmethod
def __customer_order_table_args__(cls):
table_name = cls.__tablename__
return (
sa.ForeignKeyConstraint(['store_uuid'], ['store.uuid'],
name='{}_fk_store'.format(table_name)),
sa.ForeignKeyConstraint(['customer_uuid'], ['customer.uuid'],
name='{}_fk_customer'.format(table_name)),
sa.ForeignKeyConstraint(['person_uuid'], ['person.uuid'],
name='{}_fk_person'.format(table_name)),
)
store_uuid = sa.Column(sa.String(length=32), nullable=True)
@declared_attr
def store(cls):
return orm.relationship(
Store,
doc="""
Reference to the store to which the order applies.
""")
customer_uuid = sa.Column(sa.String(length=32), nullable=True)
@declared_attr
def customer(cls):
return orm.relationship(
Customer,
doc="""
Reference to the customer account for which the order exists.
""")
person_uuid = sa.Column(sa.String(length=32), nullable=True)
@declared_attr
def person(cls):
return orm.relationship(
Person,
doc="""
Reference to the person to whom the order applies.
""")
phone_number = sa.Column(sa.String(length=20), nullable=True, doc="""
Customer contact phone number for sake of this order.
""")
email_address = sa.Column(sa.String(length=255), nullable=True, doc="""
Customer contact email address for sake of this order.
""")
total_price = sa.Column(sa.Numeric(precision=10, scale=3), nullable=True, doc="""
Full price (not including tax etc.) for all items on the order.
""")
@six.python_2_unicode_compatible
class CustomerOrder(CustomerOrderBase, Base):
"""
Represents an order placed by the customer.
"""
__tablename__ = 'custorder'
@declared_attr
def __table_args__(cls):
return cls.__customer_order_table_args__() + (
sa.ForeignKeyConstraint(['created_by_uuid'], ['user.uuid'],
name='custorder_fk_created_by'),
)
uuid = uuid_column()
id = sa.Column(sa.Integer(), doc="""
Numeric, auto-increment ID for the order.
""")
created = sa.Column(sa.DateTime(), nullable=False, default=datetime.datetime.utcnow, doc="""
Date and time when the order/batch was first created.
""")
created_by_uuid = sa.Column(sa.String(length=32), nullable=True)
created_by = orm.relationship(
User,
doc="""
Reference to the user who initially created the order/batch.
""")
status_code = sa.Column(sa.Integer(), nullable=False)
items = orm.relationship('CustomerOrderItem', back_populates='order',
collection_class=ordering_list('sequence', count_from=1), doc="""
Sequence of :class:`CustomerOrderItem` instances which belong to the order.
""")
def __str__(self):
if self.id:
return "#{}".format(self.id)
return "(pending)"
@six.python_2_unicode_compatible
class CustomerOrderItemBase(object):
"""
Base class for customer order line items.
"""
@declared_attr
def __table_args__(cls):
return cls.__customer_order_item_table_args__()
@classmethod
def __customer_order_item_table_args__(cls):
table_name = cls.__tablename__
return (
sa.ForeignKeyConstraint(['product_uuid'], ['product.uuid'],
name='{}_fk_product'.format(table_name)),
)
product_uuid = sa.Column(sa.String(length=32), nullable=True)
@declared_attr
def product(cls):
return orm.relationship(
Product,
doc="""
Reference to the master product record for the line item.
""")
product_upc = sa.Column(GPCType(), nullable=True, doc="""
UPC for the product associated with the row.
""")
product_brand = sa.Column(sa.String(length=100), nullable=True, doc="""
Brand name for the product being ordered. This should be a cache of the
relevant :attr:`Brand.name`.
""")
product_description = sa.Column(sa.String(length=60), nullable=True, doc="""
Primary description for the product being ordered. This should be a cache
of :attr:`Product.description`.
""")
product_size = sa.Column(sa.String(length=30), nullable=True, doc="""
Size of the product being ordered. This should be a cache of
:attr:`Product.size`.
""")
product_weighed = sa.Column(sa.String(length=4), nullable=True, doc="""
Flag indicating whether the product is sold by weight. This should be a
cache of :attr:`Product.weighed`.
""")
# TODO: probably should get rid of this, i can't think of why it's needed.
# for now we just make sure it is nullable, since that wasn't the case.
product_unit_of_measure = sa.Column(sa.String(length=4), nullable=True, doc="""
Code indicating the unit of measure for the product. This should be a
cache of :attr:`Product.unit_of_measure`.
""")
department_number = sa.Column(sa.Integer(), nullable=True, doc="""
Number of the department to which the product belongs.
""")
department_name = sa.Column(sa.String(length=30), nullable=True, doc="""
Name of the department to which the product belongs.
""")
case_quantity = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
Case pack count for the product being ordered. This should be a cache of
:attr:`Product.case_size`.
""")
# TODO: i now think that cases_ordered and units_ordered should go away.
# but will wait until that idea has proven itself before removing. am
# pretty sure they are obviated by order_quantity and order_uom.
cases_ordered = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
Number of cases of product which were initially ordered.
""")
units_ordered = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
Number of units of product which were initially ordered.
""")
order_quantity = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
Quantity being ordered by the customer.
""")
order_uom = sa.Column(sa.String(length=4), nullable=True, doc="""
Code indicating the unit of measure for the order itself. Does not
directly reflect the :attr:`~rattail.db.model.Product.unit_of_measure`.
""")
product_unit_cost = sa.Column(sa.Numeric(precision=9, scale=5), nullable=True, doc="""
Unit cost of the product being ordered. This should be a cache of the
relevant :attr:`rattail.db.model.ProductCost.unit_cost`.
""")
unit_price = sa.Column(sa.Numeric(precision=8, scale=3), nullable=True, doc="""
Unit price for the product being ordered. This is the price which is
quoted to the customer and/or charged to the customer, but for a unit only
and *before* any discounts are applied. It generally will be a cache of
the relevant :attr:`ProductPrice.price`.
""")
discount_percent = sa.Column(sa.Numeric(precision=5, scale=3), nullable=False, default=0, doc="""
Discount percentage which will be applied to the product's price as part of
calculating the :attr:`total_price` for the item.
""")
total_price = sa.Column(sa.Numeric(precision=8, scale=3), nullable=True, doc="""
Full price (not including tax etc.) which the customer is asked to pay for the item.
""")
paid_amount = sa.Column(sa.Numeric(precision=8, scale=3), nullable=False, default=0, doc="""
Amount which the customer has paid toward the :attr:`total_price` of theitem.
""")
payment_transaction_number = sa.Column(sa.String(length=8), nullable=True, doc="""
Transaction number in which payment for the order was taken, if applicable.
""")
def __str__(self):
return str(self.product or "(no product)")
class CustomerOrderItem(CustomerOrderItemBase, Base):
"""
Represents a particular line item (product) within a customer order.
"""
__tablename__ = 'custorder_item'
@declared_attr
def __table_args__(cls):
return cls.__customer_order_item_table_args__() + (
sa.ForeignKeyConstraint(['order_uuid'], ['custorder.uuid'],
name='custorder_item_fk_order'),
)
uuid = uuid_column()
order_uuid = sa.Column(sa.String(length=32), nullable=False)
order = orm.relationship(CustomerOrder, back_populates='items', doc="""
Reference to the :class:`CustomerOrder` instance to which the item belongs.
""")
sequence = sa.Column(sa.Integer(), nullable=False, doc="""
Numeric sequence for the item, i.e. its "line number". These values should
obviously increment in sequence and be unique within the context of a
single order.
""")
status_code = sa.Column(sa.Integer(), nullable=False)
class CustomerOrderItemEvent(Base):
"""
An event in the life of a customer order item
"""
__tablename__ = 'custorder_item_event'
__table_args__ = (
sa.ForeignKeyConstraint(['item_uuid'], ['custorder_item.uuid'], name='custorder_item_event_fk_item'),
sa.ForeignKeyConstraint(['user_uuid'], ['user.uuid'], name='custorder_item_event_fk_user'),
)
uuid = uuid_column()
item_uuid = sa.Column(sa.String(length=32), nullable=False)
item = orm.relationship(
CustomerOrderItem,
doc="""
Reference to the :class:`CustomerOrder` instance to which the item belongs.
""",
backref=orm.backref(
'events',
order_by='CustomerOrderItemEvent.occurred'))
type_code = sa.Column(sa.Integer, nullable=False)
occurred = sa.Column(sa.DateTime(), nullable=False, default=datetime.datetime.utcnow, doc="""
Date and time when the event occurred.
""")
user_uuid = sa.Column(sa.String(length=32), nullable=False)
user = orm.relationship(
User,
doc="""
User who was the "actor" for the event.
""")
note = sa.Column(sa.Text(), nullable=True, doc="""
Optional note recorded for the event.
""")
|