<commit_before>"""
byceps.services.shop.order.models.order_item
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2018 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from collections import namedtuple
from decimal import Decimal

from .....database import db, generate_uuid

from ...article.models.article import Article

from .order import Order


OrderItemTuple = namedtuple('OrderItemTuple', [
    'order_number',
    'article_number',
    'description',
    'unit_price',
    'tax_rate',
    'quantity',
    'line_price',
])


class OrderItem(db.Model):
    """An item that belongs to an order."""
    __tablename__ = 'shop_order_items'

    id = db.Column(db.Uuid, default=generate_uuid, primary_key=True)
    order_number = db.Column(db.Unicode(13), db.ForeignKey('shop_orders.order_number'), index=True, nullable=False)
    order = db.relationship(Order, backref='items')
    article_number = db.Column(db.Unicode(20), db.ForeignKey('shop_articles.item_number'), index=True, nullable=False)
    article = db.relationship(Article, backref='order_items')
    description = db.Column(db.Unicode(80), nullable=False)
    price = db.Column(db.Numeric(6, 2), nullable=False)
    tax_rate = db.Column(db.Numeric(3, 3), nullable=False)
    quantity = db.Column(db.Integer, db.CheckConstraint('quantity > 0'), nullable=False)
    shipping_required = db.Column(db.Boolean, nullable=False)

    def __init__(self, order: Order, article: Article, quantity: int) -> None:
        self.order = order
        self.article = article
        self.description = article.description
        self.price = article.price
        self.tax_rate = article.tax_rate
        self.quantity = quantity
        self.shipping_required = article.shipping_required

    @property
    def unit_price(self) -> Decimal:
        return self.price

    @property
    def line_price(self) -> Decimal:
        return self.unit_price * self.quantity

    def to_tuple(self) -> OrderItemTuple:
        """Return a tuple representation of (parts of) this entity."""
        return OrderItemTuple(
            self.order_number,
            self.article_number,
            self.description,<commit_msg>Document why the order entity rather than just the order number must be passed on order item creation<commit_after>"""
byceps.services.shop.order.models.order_item
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2018 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from collections import namedtuple
from decimal import Decimal

from .....database import db, generate_uuid

from ...article.models.article import Article

from .order import Order


OrderItemTuple = namedtuple('OrderItemTuple', [
    'order_number',
    'article_number',
    'description',
    'unit_price',
    'tax_rate',
    'quantity',
    'line_price',
])


class OrderItem(db.Model):
    """An item that belongs to an order."""
    __tablename__ = 'shop_order_items'

    id = db.Column(db.Uuid, default=generate_uuid, primary_key=True)
    order_number = db.Column(db.Unicode(13), db.ForeignKey('shop_orders.order_number'), index=True, nullable=False)
    order = db.relationship(Order, backref='items')
    article_number = db.Column(db.Unicode(20), db.ForeignKey('shop_articles.item_number'), index=True, nullable=False)
    article = db.relationship(Article, backref='order_items')
    description = db.Column(db.Unicode(80), nullable=False)
    price = db.Column(db.Numeric(6, 2), nullable=False)
    tax_rate = db.Column(db.Numeric(3, 3), nullable=False)
    quantity = db.Column(db.Integer, db.CheckConstraint('quantity > 0'), nullable=False)
    shipping_required = db.Column(db.Boolean, nullable=False)

    def __init__(self, order: Order, article: Article, quantity: int) -> None:
        # Require order instance rather than order number as argument
        # because order item are created together with the order and
        # until the order is created, there is no order number assigned.
        self.order = order
        self.article = article
        self.description = article.description
        self.price = article.price
        self.tax_rate = article.tax_rate
        self.quantity = quantity
        self.shipping_required = article.shipping_required

    @property
    def unit_price(self) -> Decimal:
        return self.price

    @property
    def line_price(self) -> Decimal:
        return self.unit_price * self.quantity

    def to_tuple(self) -> OrderItemTuple:
        """Return a tuple representation of (parts of) this entity."""
        return OrderItemTuple(
            self.order_number,
            self.article_number,
            self.description,
{'fraction_non_alphanumeric': 0.12868117797695264, 'fraction_numerical': 0.00810926163038839, 'mean_word_length': 3.559338521400778, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1452558', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1344, 'n_words': 336}