more inventory management work
This commit is contained in:
81
app/contexts/inventory_item.py
Normal file
81
app/contexts/inventory_item.py
Normal file
@ -0,0 +1,81 @@
|
||||
from app.services.base_service import BaseService
|
||||
from app.models.inventory_management import InventoryItem
|
||||
from app.models.tcgplayer_product import TCGPlayerProduct
|
||||
from app.contexts.inventory_product import InventoryProductContext
|
||||
from sqlalchemy.orm import Session
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class InventoryItemContext:
|
||||
def __init__(self, item: InventoryItem, db: Session):
|
||||
self.item = item
|
||||
self.physical_item = item.physical_item
|
||||
self.marketplace_listing = item.marketplace_listing
|
||||
self.parent = item.parent
|
||||
self.children = item.children
|
||||
self.product = item.physical_item.product
|
||||
self.db = db
|
||||
|
||||
@property
|
||||
def cost_basis(self) -> float:
|
||||
return self.item.cost_basis
|
||||
|
||||
@property
|
||||
def product_id(self) -> int:
|
||||
return self.physical_item.product_id
|
||||
|
||||
@property
|
||||
def product_name(self) -> str:
|
||||
return self.physical_item.product_name
|
||||
|
||||
@property
|
||||
def item_type(self) -> str:
|
||||
return self.physical_item.item_type
|
||||
|
||||
@property
|
||||
def market_price(self) -> float:
|
||||
return self.product.most_recent_tcgplayer_price.market_price
|
||||
|
||||
@property
|
||||
def tcg_low_price(self) -> float:
|
||||
return self.product.most_recent_tcgplayer_price.low_price
|
||||
|
||||
@property
|
||||
def listed_price(self) -> float:
|
||||
return self.marketplace_listing.listed_price
|
||||
|
||||
def top_level_parent(self) -> "InventoryItemContext":
|
||||
if self.parent:
|
||||
return self.parent.top_level_parent()
|
||||
else:
|
||||
return self
|
||||
|
||||
def box_expected_value(self) -> float:
|
||||
top_level_parent_item = self.top_level_parent_item()
|
||||
if 'case' in top_level_parent_item.item_type:
|
||||
return top_level_parent_item.open_event.sealed_case.expected_value
|
||||
elif 'box' in top_level_parent_item.item_type:
|
||||
return top_level_parent_item.open_event.sealed_box.expected_value
|
||||
else:
|
||||
raise ValueError("Unknown top level parent item type")
|
||||
|
||||
def box_acquisition_cost(self) -> float:
|
||||
if self.physical_item.transaction_item:
|
||||
return self.physical_item.transaction_item.unit_price
|
||||
elif self.parent:
|
||||
return self.parent.box_acquisition_cost()
|
||||
else:
|
||||
raise ValueError("Cannot find transaction unit price for this item")
|
||||
|
||||
def age_on_marketplace(self) -> int:
|
||||
return (datetime.now() - self.marketplace_listing.listing_date).days
|
||||
|
||||
class InventoryItemContextFactory:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
|
||||
def get_context(self, item: InventoryItem) -> InventoryItemContext:
|
||||
return InventoryItemContext(item, self.db)
|
||||
|
||||
def get_context_for_product(self, product: TCGPlayerProduct) -> InventoryProductContext:
|
||||
return InventoryProductContext(product, self.db)
|
Reference in New Issue
Block a user