Files
@ 180c9314d848
Branch filter:
Location: rattail-project/rattail/rattail/db/model/vendors.py
180c9314d848
5.8 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 | # -*- 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 Models for Vendors
"""
from __future__ import unicode_literals, absolute_import
import six
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.orm import relationship
from sqlalchemy.ext.orderinglist import ordering_list
from sqlalchemy.ext.associationproxy import association_proxy
from .core import Base, uuid_column, getset_factory
from .contact import PhoneNumber, EmailAddress
from .people import Person
@six.python_2_unicode_compatible
class Vendor(Base):
"""
Represents a vendor from which products are purchased by the store.
"""
__tablename__ = 'vendor'
__table_args__ = (
sa.UniqueConstraint('id', name='vendor_uq_id'),
)
__versioned__ = {}
uuid = uuid_column()
id = sa.Column(sa.String(length=15))
name = sa.Column(sa.String(length=50), nullable=True)
special_discount = sa.Column(sa.Numeric(precision=5, scale=3))
lead_time_days = sa.Column(sa.Numeric(precision=5, scale=1), nullable=True, doc="""
The number of days expected to elapse between the order generation and receipt
of goods. (This description is borrowed from the SIL standard.)
""")
order_interval_days = sa.Column(sa.Numeric(precision=5, scale=1), nullable=True, doc="""
The number of days expected to elapse between repeated order placement. (This
description is borrowed from the SIL standard.)
""")
def __str__(self):
return self.name or ''
@property
def fax_number(self):
for phone in self.phones:
if 'fax' in (phone.type or '').lower():
return phone.number
def add_email_address(self, address, type='Info'):
email = VendorEmailAddress(address=address, type=type)
self.emails.append(email)
def add_phone_number(self, number, type='Voice'):
phone = VendorPhoneNumber(number=number, type=type)
self.phones.append(phone)
class VendorPhoneNumber(PhoneNumber):
"""
Represents a phone (or fax) number associated with a vendor.
"""
__mapper_args__ = {'polymorphic_identity': 'Vendor'}
Vendor.phones = relationship(
VendorPhoneNumber,
backref='vendor',
primaryjoin=VendorPhoneNumber.parent_uuid == Vendor.uuid,
foreign_keys=[VendorPhoneNumber.parent_uuid],
collection_class=ordering_list('preference', count_from=1),
order_by=VendorPhoneNumber.preference,
cascade='save-update, merge, delete, delete-orphan')
Vendor.phone = relationship(
VendorPhoneNumber,
primaryjoin=sa.and_(
VendorPhoneNumber.parent_uuid == Vendor.uuid,
VendorPhoneNumber.preference == 1),
foreign_keys=[VendorPhoneNumber.parent_uuid],
uselist=False,
viewonly=True)
class VendorEmailAddress(EmailAddress):
"""
Represents an email address associated with a vendor.
"""
__mapper_args__ = {'polymorphic_identity': 'Vendor'}
Vendor.emails = relationship(
VendorEmailAddress,
backref='vendor',
primaryjoin=VendorEmailAddress.parent_uuid == Vendor.uuid,
foreign_keys=[VendorEmailAddress.parent_uuid],
collection_class=ordering_list('preference', count_from=1),
order_by=VendorEmailAddress.preference,
cascade='save-update, merge, delete, delete-orphan')
Vendor.email = relationship(
VendorEmailAddress,
primaryjoin=sa.and_(
VendorEmailAddress.parent_uuid == Vendor.uuid,
VendorEmailAddress.preference == 1),
foreign_keys=[VendorEmailAddress.parent_uuid],
uselist=False,
viewonly=True)
@six.python_2_unicode_compatible
class VendorContact(Base):
"""
Represents a point of contact (e.g. salesperson) for a vendor.
"""
__tablename__ = 'vendor_contact'
__table_args__ = (
sa.ForeignKeyConstraint(['vendor_uuid'], ['vendor.uuid'], name='vendor_contact_fk_vendor'),
sa.ForeignKeyConstraint(['person_uuid'], ['person.uuid'], name='vendor_contact_fk_person'),
)
__versioned__ = {}
uuid = uuid_column()
vendor_uuid = sa.Column(sa.String(length=32), nullable=False)
person_uuid = sa.Column(sa.String(length=32), nullable=False)
preference = sa.Column(sa.Integer(), nullable=False)
person = orm.relationship(
Person,
backref=orm.backref(
'_vendor_contacts',
cascade='all, delete-orphan'))
def __str__(self):
return str(self.person)
Vendor._contacts = relationship(
VendorContact, backref='vendor',
collection_class=ordering_list('preference', count_from=1),
order_by=VendorContact.preference,
cascade='save-update, merge, delete, delete-orphan')
Vendor.contacts = association_proxy(
'_contacts', 'person',
getset_factory=getset_factory,
creator=lambda p: VendorContact(person=p))
Vendor._contact = relationship(
VendorContact,
primaryjoin=sa.and_(
VendorContact.vendor_uuid == Vendor.uuid,
VendorContact.preference == 1),
uselist=False,
viewonly=True)
Vendor.contact = association_proxy(
'_contact', 'person',
getset_factory=getset_factory)
|