Files
@ 180c9314d848
Branch filter:
Location: rattail-project/rattail/rattail/db/model/purchase.py
180c9314d848
17.6 KiB
text/x-python
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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | # -*- coding: utf-8 -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2017 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 model for purchase orders
"""
from __future__ import unicode_literals, absolute_import
import datetime
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.declarative import declared_attr
from rattail.db.model import Base, uuid_column, Store, Department, Vendor, Employee, User, Product
from rattail.db.types import GPCType
class PurchaseBase(object):
"""
Base class for purchases; defines common fields.
"""
@declared_attr
def __table_args__(cls):
return cls.__purchase_table_args__()
@classmethod
def __purchase_table_args__(cls):
return (
sa.ForeignKeyConstraint(['store_uuid'], ['store.uuid'], name='{}_fk_store'.format(cls.__tablename__)),
sa.ForeignKeyConstraint(['vendor_uuid'], ['vendor.uuid'], name='{}_fk_vendor'.format(cls.__tablename__)),
sa.ForeignKeyConstraint(['department_uuid'], ['department.uuid'], name='{}_fk_department'.format(cls.__tablename__)),
sa.ForeignKeyConstraint(['buyer_uuid'], ['employee.uuid'], name='{}_purchase_fk_buyer'.format(cls.__tablename__)),
)
store_uuid = sa.Column(sa.String(length=32), nullable=False)
@declared_attr
def store(cls):
return orm.relationship(
Store,
doc="""
Reference to the :class:`Store` for which the purchase was made.
""")
vendor_uuid = sa.Column(sa.String(length=32), nullable=False)
@declared_attr
def vendor(cls):
return orm.relationship(
Vendor,
doc="""
Reference to the :class:`Vendor` to which the purchase was made.
""")
department_uuid = sa.Column(sa.String(length=32), nullable=True)
@declared_attr
def department(cls):
return orm.relationship(
Department,
doc="""
Reference to the primary :class:`Department` for which the purchase was made.
""")
buyer_uuid = sa.Column(sa.String(length=32), nullable=False)
@declared_attr
def buyer(cls):
return orm.relationship(
Employee,
doc="""
Reference to the :class:`Employee` who placed the order with the vendor.
""")
po_number = sa.Column(sa.String(length=20), nullable=True, doc="""
Purchase order number, e.g. for cross-reference with another system.
""")
po_total = sa.Column(sa.Numeric(precision=8, scale=2), nullable=True, doc="""
Purchase order total, i.e. expected cost as of order placement.
""")
date_ordered = sa.Column(sa.Date(), nullable=True, doc="""
Date on which the purchase order was first submitted to the vendor.
""")
date_shipped = sa.Column(sa.Date(), nullable=True, doc="""
Date on which the order was shipped from the vendor.
""")
date_received = sa.Column(sa.Date(), nullable=True, doc="""
Date on which the order was received at the store.
""")
invoice_number = sa.Column(sa.String(length=20), nullable=True, doc="""
Invoice number, e.g. for cross-reference with another system.
""")
invoice_date = sa.Column(sa.Date(), nullable=True, doc="""
Invoice date, if applicable.
""")
invoice_total = sa.Column(sa.Numeric(precision=8, scale=2), nullable=True, doc="""
Invoice total, if applicable.
""")
def __unicode__(self):
if self.vendor and self.date_ordered:
return "{} ({})".format(self.vendor, self.date_ordered.strftime('%Y-%m-%d'))
if self.vendor:
return unicode(self.vendor)
return ''
class PurchaseItemBase(object):
"""
Base class for purchase line items.
"""
@declared_attr
def __table_args__(cls):
return cls.__purchaseitem_table_args__()
@classmethod
def __purchaseitem_table_args__(cls):
return (
sa.ForeignKeyConstraint(['product_uuid'], ['product.uuid'], name='{}_fk_product'.format(cls.__tablename__)),
)
sequence = sa.Column(sa.Integer(), nullable=True, doc="""
Effectively the (internal) line number for the purchase line item, using 1-based indexing.
""")
vendor_code = sa.Column(sa.String(length=20), nullable=True, doc="""
Vendor item code for the product.
""")
product_uuid = sa.Column(sa.String(length=32), nullable=True)
@declared_attr
def product(cls):
return orm.relationship(
Product,
doc="""
Reference to the :class:`Product` which the line item contains / represents.
""")
upc = sa.Column(GPCType(), nullable=True, doc="""
Product UPC for the line item.
""")
item_id = sa.Column(sa.String(length=20), nullable=True, doc="""
Generic ID string for the item.
""")
brand_name = sa.Column(sa.String(length=100), nullable=True, doc="""
Brand name for the line item.
""")
description = sa.Column(sa.String(length=60), nullable=False, default='', doc="""
Product description for the line item.
""")
size = sa.Column(sa.String(length=255), nullable=True, doc="""
Product size for the line item.
""")
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=8, scale=2), nullable=True, doc="""
Number of units in a single case of product.
""")
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.
""")
po_line_number = sa.Column(sa.Integer(), nullable=True, doc="""
Line number from the PO if known, for cross-reference.
""")
po_unit_cost = sa.Column(sa.Numeric(precision=7, scale=3), nullable=True, doc="""
Expected cost per single unit of product, as of initial order placement.
""")
po_total = sa.Column(sa.Numeric(precision=7, scale=2), nullable=True, doc="""
Expected total cost for line item, as of initial order placement.
""")
cases_received = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
Number of cases of product which were ultimately received.
""")
units_received = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
Number of units of product which were ultimately received.
""")
invoice_line_number = sa.Column(sa.Integer(), nullable=True, doc="""
Line number from the invoice if known, for cross-reference.
""")
invoice_case_cost = sa.Column(sa.Numeric(precision=7, scale=3), nullable=True, doc="""
Actual cost per case of product, per invoice.
""")
invoice_unit_cost = sa.Column(sa.Numeric(precision=7, scale=3), nullable=True, doc="""
Actual cost per single unit of product, per invoice.
""")
invoice_total = sa.Column(sa.Numeric(precision=7, scale=2), nullable=True, doc="""
Actual total cost for line item, per invoice.
""")
cases_damaged = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
Number of cases of product which were shipped damaged.
""")
units_damaged = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
Number of units of product which were shipped damaged.
""")
cases_expired = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
Number of cases of product which were shipped expired.
""")
units_expired = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
Number of units of product which were shipped expired.
""")
cases_mispick = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
Number of cases of product for which mispick was shipped.
""")
units_mispick = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
Number of units of product for which mispick was shipped.
""")
class PurchaseCreditBase(object):
"""
Base class for purchase credits.
"""
@declared_attr
def __table_args__(cls):
return cls.__purchasecredit_table_args__()
@classmethod
def __purchasecredit_table_args__(cls):
return (
sa.ForeignKeyConstraint(['store_uuid'], ['store.uuid'], name='{}_fk_store'.format(cls.__tablename__)),
sa.ForeignKeyConstraint(['vendor_uuid'], ['vendor.uuid'], name='{}_fk_vendor'.format(cls.__tablename__)),
sa.ForeignKeyConstraint(['product_uuid'], ['product.uuid'], name='{}_fk_product'.format(cls.__tablename__)),
sa.ForeignKeyConstraint(['mispick_product_uuid'], ['product.uuid'], name='{}_fk_mispick_product'.format(cls.__tablename__)),
)
store_uuid = sa.Column(sa.String(length=32), nullable=False)
@declared_attr
def store(cls):
return orm.relationship(
Store,
doc="""
Reference to the :class:`Store` for which the purchase was made.
""")
vendor_uuid = sa.Column(sa.String(length=32), nullable=False)
@declared_attr
def vendor(cls):
return orm.relationship(
Vendor,
doc="""
Reference to the :class:`Vendor` to which the purchase was made.
""")
date_ordered = sa.Column(sa.Date(), nullable=True, doc="""
Date on which the purchase order was first submitted to the vendor.
""")
date_shipped = sa.Column(sa.Date(), nullable=True, doc="""
Date on which the order was shipped from the vendor.
""")
date_received = sa.Column(sa.Date(), nullable=True, doc="""
Date on which the order was received at the store.
""")
invoice_number = sa.Column(sa.String(length=20), nullable=True, doc="""
Invoice number, e.g. for cross-reference with another system.
""")
invoice_date = sa.Column(sa.Date(), nullable=True, doc="""
Invoice date, if applicable.
""")
credit_type = sa.Column(sa.String(length=20), nullable=False, doc="""
Type of the credit, i.e. damaged/expired/mispick
""")
product_uuid = sa.Column(sa.String(length=32), nullable=True)
@declared_attr
def product(cls):
return orm.relationship(
Product,
primaryjoin='Product.uuid == {}.product_uuid'.format(cls.__name__),
doc="""
Reference to the :class:`Product` with which the credit is associated.
""")
upc = sa.Column(GPCType(), nullable=True, doc="""
Product UPC for the credit item.
""")
brand_name = sa.Column(sa.String(length=100), nullable=True, doc="""
Brand name for the credit item.
""")
description = sa.Column(sa.String(length=60), nullable=False, default='', doc="""
Product description for the credit item.
""")
size = sa.Column(sa.String(length=255), nullable=True, doc="""
Product size for the credit item.
""")
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=8, scale=2), nullable=True, doc="""
Number of units in a single case of product.
""")
cases_shorted = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
Number of cases of product which were ordered but not received.
""")
units_shorted = sa.Column(sa.Numeric(precision=10, scale=4), nullable=True, doc="""
Number of cases of product which were ordered but not received.
""")
product_discarded = sa.Column(sa.Boolean(), nullable=True, doc="""
Indicates the associated product was discarded, and cannot be returned to vendor.
""")
expiration_date = sa.Column(sa.Date(), nullable=True, doc="""
Expiration date marked on expired product, if applicable.
""")
invoice_line_number = sa.Column(sa.Integer(), nullable=True, doc="""
Line number from the invoice if known, for cross-reference.
""")
invoice_case_cost = sa.Column(sa.Numeric(precision=7, scale=3), nullable=True, doc="""
Actual cost per case of product, per invoice.
""")
invoice_unit_cost = sa.Column(sa.Numeric(precision=7, scale=3), nullable=True, doc="""
Actual cost per single unit of product, per invoice.
""")
invoice_total = sa.Column(sa.Numeric(precision=7, scale=2), nullable=True, doc="""
Actual total cost for line item, per invoice.
""")
mispick_product_uuid = sa.Column(sa.String(length=32), nullable=True)
@declared_attr
def mispick_product(cls):
return orm.relationship(
Product,
primaryjoin='Product.uuid == {}.mispick_product_uuid'.format(cls.__name__),
doc="""
Reference to the :class:`Product` which was shipped in place of the
one which was ordered.
""")
mispick_upc = sa.Column(GPCType(), nullable=True, doc="""
Product UPC for the mispick item.
""")
mispick_brand_name = sa.Column(sa.String(length=100), nullable=True, doc="""
Brand name for the mispick item.
""")
mispick_description = sa.Column(sa.String(length=60), nullable=True, default='', doc="""
Product description for the mispick item.
""")
mispick_size = sa.Column(sa.String(length=255), nullable=True, doc="""
Product size for the mispick item.
""")
class Purchase(PurchaseBase, Base):
"""
Represents a purchase made by a store.
"""
__tablename__ = 'purchase'
@declared_attr
def __table_args__(cls):
return cls.__purchase_table_args__() + (
sa.ForeignKeyConstraint(['created_by_uuid'], ['user.uuid'], name='purchase_fk_created_by'),
)
uuid = uuid_column()
status = sa.Column(sa.Integer(), nullable=False, doc="""
Numeric code used to signify current status for the purchase, e.g. placed
or paid etc.
""")
created = sa.Column(sa.DateTime(), nullable=False, default=datetime.datetime.utcnow, doc="""
Timestamp when the purchase was first created within this system.
""")
created_by_uuid = sa.Column(sa.String(length=32), nullable=False)
created_by = orm.relationship(
User,
primaryjoin=User.uuid == created_by_uuid,
foreign_keys=[created_by_uuid],
doc="""
Reference to the :class:`User` who first created the purchase within
this system.
""")
class PurchaseItem(PurchaseItemBase, Base):
"""
Represents a line item on a purchase.
"""
__tablename__ = 'purchase_item'
@declared_attr
def __table_args__(cls):
return cls.__purchaseitem_table_args__() + (
sa.ForeignKeyConstraint(['purchase_uuid'], ['purchase.uuid'], name='purchase_item_fk_purchase'),
)
uuid = uuid_column()
purchase_uuid = sa.Column(sa.String(length=32), nullable=False)
purchase = orm.relationship(
Purchase,
doc="""
Reference to the :class:`Purchase` to which the item belongs.
""",
backref=orm.backref(
'items',
cascade='all',
doc="""
List of :class:`PurchaseItem` instances for the purchase.
"""))
status = sa.Column(sa.Integer(), nullable=True, doc="""
Numeric code used to signify current status for the line item, e.g. for
highlighting rows when invoice cost differed from expected/PO cost (?)
""")
class PurchaseCredit(PurchaseCreditBase, Base):
"""
Represents a purchase credit item.
"""
__tablename__ = 'purchase_credit'
@declared_attr
def __table_args__(cls):
return cls.__purchasecredit_table_args__() + (
sa.ForeignKeyConstraint(['purchase_uuid'], ['purchase.uuid'], name='purchase_credit_fk_purchase'),
sa.ForeignKeyConstraint(['item_uuid'], ['purchase_item.uuid'], name='purchase_credit_fk_item'),
)
uuid = uuid_column()
purchase_uuid = sa.Column(sa.String(length=32), nullable=True)
purchase = orm.relationship(
Purchase,
doc="""
Reference to the :class:`Purchase` to which the credit applies.
""",
backref=orm.backref(
'credits',
doc="""
List of :class:`PurchaseCredit` instances for the purchase.
"""))
item_uuid = sa.Column(sa.String(length=32), nullable=True)
item = orm.relationship(
PurchaseItem,
doc="""
Reference to the purchase item with which the credit is associated.
""")
status = sa.Column(sa.Integer(), nullable=True, doc="""
Numeric code used to signify current status for the credit.
""")
|