Create new services: inventory, recipes, suppliers
This commit is contained in:
93
services/suppliers/migrations/alembic.ini
Normal file
93
services/suppliers/migrations/alembic.ini
Normal file
@@ -0,0 +1,93 @@
|
||||
# A generic, single database configuration.
|
||||
|
||||
[alembic]
|
||||
# path to migration scripts
|
||||
script_location = migrations
|
||||
|
||||
# template used to generate migration files
|
||||
# file_template = %%(rev)s_%%(slug)s
|
||||
|
||||
# sys.path path, will be prepended to sys.path if present.
|
||||
# defaults to the current working directory.
|
||||
prepend_sys_path = .
|
||||
|
||||
# timezone to use when rendering the date within the migration file
|
||||
# as well as the filename.
|
||||
# If specified, requires the python-dateutil library that can be
|
||||
# installed by adding `alembic[tz]` to the pip requirements
|
||||
# string value is passed to dateutil.tz.gettz()
|
||||
# leave blank for localtime
|
||||
# timezone =
|
||||
|
||||
# max length of characters to apply to the
|
||||
# "slug" field
|
||||
# truncate_slug_length = 40
|
||||
|
||||
# set to 'true' to run the environment during
|
||||
# the 'revision' command, regardless of autogenerate
|
||||
# revision_environment = false
|
||||
|
||||
# set to 'true' to allow .pyc and .pyo files without
|
||||
# a source .py file to be detected as revisions in the
|
||||
# versions/ directory
|
||||
# sourceless = false
|
||||
|
||||
# version number format
|
||||
# Uses Alembic datetime format
|
||||
version_num_format = %%(year)d%%(month).2d%%(day).2d_%%(hour).2d%%(minute).2d_%%(second).2d
|
||||
|
||||
# version name format
|
||||
version_path_separator = /
|
||||
|
||||
# the output encoding used when revision files
|
||||
# are written from script.py.mako
|
||||
# output_encoding = utf-8
|
||||
|
||||
sqlalchemy.url = postgresql+asyncpg://suppliers_user:suppliers_pass123@suppliers-db:5432/suppliers_db
|
||||
|
||||
|
||||
[post_write_hooks]
|
||||
# post_write_hooks defines scripts or Python functions that are run
|
||||
# on newly generated revision scripts. See the documentation for further
|
||||
# detail and examples
|
||||
|
||||
# format using "black" - use the console_scripts runner, against the "black" entrypoint
|
||||
# hooks = black
|
||||
# black.type = console_scripts
|
||||
# black.entrypoint = black
|
||||
# black.options = -l 79 REVISION_SCRIPT_FILENAME
|
||||
|
||||
# Logging configuration
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARN
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
||||
109
services/suppliers/migrations/env.py
Normal file
109
services/suppliers/migrations/env.py
Normal file
@@ -0,0 +1,109 @@
|
||||
"""
|
||||
Alembic environment configuration for Suppliers Service
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from logging.config import fileConfig
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from sqlalchemy import pool
|
||||
from sqlalchemy.engine import Connection
|
||||
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||||
|
||||
from alembic import context
|
||||
|
||||
# Add the app directory to the path
|
||||
sys.path.insert(0, str(Path(__file__).parent.parent))
|
||||
|
||||
# Import models to ensure they're registered
|
||||
from app.models.suppliers import * # noqa
|
||||
from shared.database.base import Base
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# Set the SQLAlchemy URL from environment variable if available
|
||||
database_url = os.getenv('SUPPLIERS_DATABASE_URL')
|
||||
if database_url:
|
||||
config.set_main_option('sqlalchemy.url', database_url)
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
target_metadata = Base.metadata
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
compare_type=True,
|
||||
compare_server_default=True,
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def do_run_migrations(connection: Connection) -> None:
|
||||
"""Run migrations with database connection"""
|
||||
context.configure(
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
compare_type=True,
|
||||
compare_server_default=True,
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
async def run_async_migrations() -> None:
|
||||
"""Run migrations in async mode"""
|
||||
connectable = async_engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
async with connectable.connect() as connection:
|
||||
await connection.run_sync(do_run_migrations)
|
||||
|
||||
await connectable.dispose()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Run migrations in 'online' mode."""
|
||||
asyncio.run(run_async_migrations())
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
||||
24
services/suppliers/migrations/script.py.mako
Normal file
24
services/suppliers/migrations/script.py.mako
Normal file
@@ -0,0 +1,24 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = ${repr(up_revision)}
|
||||
down_revision = ${repr(down_revision)}
|
||||
branch_labels = ${repr(branch_labels)}
|
||||
depends_on = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
||||
@@ -0,0 +1,404 @@
|
||||
"""Initial supplier and procurement tables
|
||||
|
||||
Revision ID: 001_initial_supplier_tables
|
||||
Revises:
|
||||
Create Date: 2024-01-15 10:00:00.000000
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects.postgresql import UUID, JSONB
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '001_initial_supplier_tables'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# Create suppliers table
|
||||
op.create_table('suppliers',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False, primary_key=True),
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('name', sa.String(255), nullable=False),
|
||||
sa.Column('supplier_code', sa.String(50), nullable=True),
|
||||
sa.Column('tax_id', sa.String(50), nullable=True),
|
||||
sa.Column('registration_number', sa.String(100), nullable=True),
|
||||
sa.Column('supplier_type', sa.Enum('INGREDIENTS', 'PACKAGING', 'EQUIPMENT', 'SERVICES', 'UTILITIES', 'MULTI', name='suppliertype'), nullable=False),
|
||||
sa.Column('status', sa.Enum('ACTIVE', 'INACTIVE', 'PENDING_APPROVAL', 'SUSPENDED', 'BLACKLISTED', name='supplierstatus'), nullable=False, default='PENDING_APPROVAL'),
|
||||
sa.Column('contact_person', sa.String(200), nullable=True),
|
||||
sa.Column('email', sa.String(254), nullable=True),
|
||||
sa.Column('phone', sa.String(30), nullable=True),
|
||||
sa.Column('mobile', sa.String(30), nullable=True),
|
||||
sa.Column('website', sa.String(255), nullable=True),
|
||||
sa.Column('address_line1', sa.String(255), nullable=True),
|
||||
sa.Column('address_line2', sa.String(255), nullable=True),
|
||||
sa.Column('city', sa.String(100), nullable=True),
|
||||
sa.Column('state_province', sa.String(100), nullable=True),
|
||||
sa.Column('postal_code', sa.String(20), nullable=True),
|
||||
sa.Column('country', sa.String(100), nullable=True),
|
||||
sa.Column('payment_terms', sa.Enum('CASH_ON_DELIVERY', 'NET_15', 'NET_30', 'NET_45', 'NET_60', 'PREPAID', 'CREDIT_TERMS', name='paymentterms'), nullable=False, default='NET_30'),
|
||||
sa.Column('credit_limit', sa.Numeric(12, 2), nullable=True),
|
||||
sa.Column('currency', sa.String(3), nullable=False, default='EUR'),
|
||||
sa.Column('standard_lead_time', sa.Integer(), nullable=False, default=3),
|
||||
sa.Column('minimum_order_amount', sa.Numeric(10, 2), nullable=True),
|
||||
sa.Column('delivery_area', sa.String(255), nullable=True),
|
||||
sa.Column('quality_rating', sa.Float(), nullable=True, default=0.0),
|
||||
sa.Column('delivery_rating', sa.Float(), nullable=True, default=0.0),
|
||||
sa.Column('total_orders', sa.Integer(), nullable=False, default=0),
|
||||
sa.Column('total_amount', sa.Numeric(12, 2), nullable=False, default=0.0),
|
||||
sa.Column('approved_by', UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('approved_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('rejection_reason', sa.Text(), nullable=True),
|
||||
sa.Column('notes', sa.Text(), nullable=True),
|
||||
sa.Column('certifications', JSONB, nullable=True),
|
||||
sa.Column('business_hours', JSONB, nullable=True),
|
||||
sa.Column('specializations', JSONB, nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('created_by', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('updated_by', UUID(as_uuid=True), nullable=False)
|
||||
)
|
||||
|
||||
# Create supplier_price_lists table
|
||||
op.create_table('supplier_price_lists',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False, primary_key=True),
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('supplier_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('ingredient_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('product_code', sa.String(100), nullable=True),
|
||||
sa.Column('product_name', sa.String(255), nullable=False),
|
||||
sa.Column('unit_price', sa.Numeric(10, 4), nullable=False),
|
||||
sa.Column('unit_of_measure', sa.String(20), nullable=False),
|
||||
sa.Column('minimum_order_quantity', sa.Integer(), nullable=True, default=1),
|
||||
sa.Column('price_per_unit', sa.Numeric(10, 4), nullable=False),
|
||||
sa.Column('tier_pricing', JSONB, nullable=True),
|
||||
sa.Column('effective_date', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('expiry_date', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=False, default=True),
|
||||
sa.Column('brand', sa.String(100), nullable=True),
|
||||
sa.Column('packaging_size', sa.String(50), nullable=True),
|
||||
sa.Column('origin_country', sa.String(100), nullable=True),
|
||||
sa.Column('shelf_life_days', sa.Integer(), nullable=True),
|
||||
sa.Column('storage_requirements', sa.Text(), nullable=True),
|
||||
sa.Column('quality_specs', JSONB, nullable=True),
|
||||
sa.Column('allergens', JSONB, nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('created_by', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('updated_by', UUID(as_uuid=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['supplier_id'], ['suppliers.id'])
|
||||
)
|
||||
|
||||
# Create purchase_orders table
|
||||
op.create_table('purchase_orders',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False, primary_key=True),
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('supplier_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('po_number', sa.String(50), nullable=False),
|
||||
sa.Column('reference_number', sa.String(100), nullable=True),
|
||||
sa.Column('status', sa.Enum('DRAFT', 'PENDING_APPROVAL', 'APPROVED', 'SENT_TO_SUPPLIER', 'CONFIRMED', 'PARTIALLY_RECEIVED', 'COMPLETED', 'CANCELLED', 'DISPUTED', name='purchaseorderstatus'), nullable=False, default='DRAFT'),
|
||||
sa.Column('priority', sa.String(20), nullable=False, default='normal'),
|
||||
sa.Column('order_date', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('required_delivery_date', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('estimated_delivery_date', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('subtotal', sa.Numeric(12, 2), nullable=False, default=0.0),
|
||||
sa.Column('tax_amount', sa.Numeric(12, 2), nullable=False, default=0.0),
|
||||
sa.Column('shipping_cost', sa.Numeric(10, 2), nullable=False, default=0.0),
|
||||
sa.Column('discount_amount', sa.Numeric(10, 2), nullable=False, default=0.0),
|
||||
sa.Column('total_amount', sa.Numeric(12, 2), nullable=False, default=0.0),
|
||||
sa.Column('currency', sa.String(3), nullable=False, default='EUR'),
|
||||
sa.Column('delivery_address', sa.Text(), nullable=True),
|
||||
sa.Column('delivery_instructions', sa.Text(), nullable=True),
|
||||
sa.Column('delivery_contact', sa.String(200), nullable=True),
|
||||
sa.Column('delivery_phone', sa.String(30), nullable=True),
|
||||
sa.Column('requires_approval', sa.Boolean(), nullable=False, default=False),
|
||||
sa.Column('approved_by', UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('approved_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('rejection_reason', sa.Text(), nullable=True),
|
||||
sa.Column('sent_to_supplier_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('supplier_confirmation_date', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('supplier_reference', sa.String(100), nullable=True),
|
||||
sa.Column('notes', sa.Text(), nullable=True),
|
||||
sa.Column('internal_notes', sa.Text(), nullable=True),
|
||||
sa.Column('terms_and_conditions', sa.Text(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('created_by', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('updated_by', UUID(as_uuid=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['supplier_id'], ['suppliers.id'])
|
||||
)
|
||||
|
||||
# Create purchase_order_items table
|
||||
op.create_table('purchase_order_items',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False, primary_key=True),
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('purchase_order_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('price_list_item_id', UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('ingredient_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('product_code', sa.String(100), nullable=True),
|
||||
sa.Column('product_name', sa.String(255), nullable=False),
|
||||
sa.Column('ordered_quantity', sa.Integer(), nullable=False),
|
||||
sa.Column('unit_of_measure', sa.String(20), nullable=False),
|
||||
sa.Column('unit_price', sa.Numeric(10, 4), nullable=False),
|
||||
sa.Column('line_total', sa.Numeric(12, 2), nullable=False),
|
||||
sa.Column('received_quantity', sa.Integer(), nullable=False, default=0),
|
||||
sa.Column('remaining_quantity', sa.Integer(), nullable=False, default=0),
|
||||
sa.Column('quality_requirements', sa.Text(), nullable=True),
|
||||
sa.Column('item_notes', sa.Text(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.ForeignKeyConstraint(['purchase_order_id'], ['purchase_orders.id']),
|
||||
sa.ForeignKeyConstraint(['price_list_item_id'], ['supplier_price_lists.id'])
|
||||
)
|
||||
|
||||
# Create deliveries table
|
||||
op.create_table('deliveries',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False, primary_key=True),
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('purchase_order_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('supplier_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('delivery_number', sa.String(50), nullable=False),
|
||||
sa.Column('supplier_delivery_note', sa.String(100), nullable=True),
|
||||
sa.Column('status', sa.Enum('SCHEDULED', 'IN_TRANSIT', 'OUT_FOR_DELIVERY', 'DELIVERED', 'PARTIALLY_DELIVERED', 'FAILED_DELIVERY', 'RETURNED', name='deliverystatus'), nullable=False, default='SCHEDULED'),
|
||||
sa.Column('scheduled_date', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('estimated_arrival', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('actual_arrival', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('completed_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('delivery_address', sa.Text(), nullable=True),
|
||||
sa.Column('delivery_contact', sa.String(200), nullable=True),
|
||||
sa.Column('delivery_phone', sa.String(30), nullable=True),
|
||||
sa.Column('carrier_name', sa.String(200), nullable=True),
|
||||
sa.Column('tracking_number', sa.String(100), nullable=True),
|
||||
sa.Column('inspection_passed', sa.Boolean(), nullable=True),
|
||||
sa.Column('inspection_notes', sa.Text(), nullable=True),
|
||||
sa.Column('quality_issues', JSONB, nullable=True),
|
||||
sa.Column('received_by', UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('received_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('notes', sa.Text(), nullable=True),
|
||||
sa.Column('photos', JSONB, nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('created_by', UUID(as_uuid=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['purchase_order_id'], ['purchase_orders.id']),
|
||||
sa.ForeignKeyConstraint(['supplier_id'], ['suppliers.id'])
|
||||
)
|
||||
|
||||
# Create delivery_items table
|
||||
op.create_table('delivery_items',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False, primary_key=True),
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('delivery_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('purchase_order_item_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('ingredient_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('product_name', sa.String(255), nullable=False),
|
||||
sa.Column('ordered_quantity', sa.Integer(), nullable=False),
|
||||
sa.Column('delivered_quantity', sa.Integer(), nullable=False),
|
||||
sa.Column('accepted_quantity', sa.Integer(), nullable=False),
|
||||
sa.Column('rejected_quantity', sa.Integer(), nullable=False, default=0),
|
||||
sa.Column('batch_lot_number', sa.String(100), nullable=True),
|
||||
sa.Column('expiry_date', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('quality_grade', sa.String(20), nullable=True),
|
||||
sa.Column('quality_issues', sa.Text(), nullable=True),
|
||||
sa.Column('rejection_reason', sa.Text(), nullable=True),
|
||||
sa.Column('item_notes', sa.Text(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.ForeignKeyConstraint(['delivery_id'], ['deliveries.id']),
|
||||
sa.ForeignKeyConstraint(['purchase_order_item_id'], ['purchase_order_items.id'])
|
||||
)
|
||||
|
||||
# Create supplier_quality_reviews table
|
||||
op.create_table('supplier_quality_reviews',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False, primary_key=True),
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('supplier_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('purchase_order_id', UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('delivery_id', UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('review_date', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('review_type', sa.String(50), nullable=False),
|
||||
sa.Column('quality_rating', sa.Enum('EXCELLENT', 'GOOD', 'AVERAGE', 'POOR', 'VERY_POOR', name='qualityrating'), nullable=False),
|
||||
sa.Column('delivery_rating', sa.Enum('EXCELLENT', 'GOOD', 'AVERAGE', 'POOR', 'VERY_POOR', name='deliveryrating'), nullable=False),
|
||||
sa.Column('communication_rating', sa.Integer(), nullable=False),
|
||||
sa.Column('overall_rating', sa.Float(), nullable=False),
|
||||
sa.Column('quality_comments', sa.Text(), nullable=True),
|
||||
sa.Column('delivery_comments', sa.Text(), nullable=True),
|
||||
sa.Column('communication_comments', sa.Text(), nullable=True),
|
||||
sa.Column('improvement_suggestions', sa.Text(), nullable=True),
|
||||
sa.Column('quality_issues', JSONB, nullable=True),
|
||||
sa.Column('corrective_actions', sa.Text(), nullable=True),
|
||||
sa.Column('follow_up_required', sa.Boolean(), nullable=False, default=False),
|
||||
sa.Column('follow_up_date', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('is_final', sa.Boolean(), nullable=False, default=True),
|
||||
sa.Column('approved_by', UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('reviewed_by', UUID(as_uuid=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['supplier_id'], ['suppliers.id']),
|
||||
sa.ForeignKeyConstraint(['purchase_order_id'], ['purchase_orders.id']),
|
||||
sa.ForeignKeyConstraint(['delivery_id'], ['deliveries.id'])
|
||||
)
|
||||
|
||||
# Create supplier_invoices table
|
||||
op.create_table('supplier_invoices',
|
||||
sa.Column('id', UUID(as_uuid=True), nullable=False, primary_key=True),
|
||||
sa.Column('tenant_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('supplier_id', UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('purchase_order_id', UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('invoice_number', sa.String(50), nullable=False),
|
||||
sa.Column('supplier_invoice_number', sa.String(100), nullable=False),
|
||||
sa.Column('status', sa.Enum('PENDING', 'APPROVED', 'PAID', 'OVERDUE', 'DISPUTED', 'CANCELLED', name='invoicestatus'), nullable=False, default='PENDING'),
|
||||
sa.Column('invoice_date', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('due_date', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('received_date', sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column('subtotal', sa.Numeric(12, 2), nullable=False),
|
||||
sa.Column('tax_amount', sa.Numeric(12, 2), nullable=False, default=0.0),
|
||||
sa.Column('shipping_cost', sa.Numeric(10, 2), nullable=False, default=0.0),
|
||||
sa.Column('discount_amount', sa.Numeric(10, 2), nullable=False, default=0.0),
|
||||
sa.Column('total_amount', sa.Numeric(12, 2), nullable=False),
|
||||
sa.Column('currency', sa.String(3), nullable=False, default='EUR'),
|
||||
sa.Column('paid_amount', sa.Numeric(12, 2), nullable=False, default=0.0),
|
||||
sa.Column('payment_date', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('payment_reference', sa.String(100), nullable=True),
|
||||
sa.Column('approved_by', UUID(as_uuid=True), nullable=True),
|
||||
sa.Column('approved_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('rejection_reason', sa.Text(), nullable=True),
|
||||
sa.Column('notes', sa.Text(), nullable=True),
|
||||
sa.Column('invoice_document_url', sa.String(500), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('created_by', UUID(as_uuid=True), nullable=False),
|
||||
sa.ForeignKeyConstraint(['supplier_id'], ['suppliers.id']),
|
||||
sa.ForeignKeyConstraint(['purchase_order_id'], ['purchase_orders.id'])
|
||||
)
|
||||
|
||||
# Create indexes
|
||||
op.create_index('ix_suppliers_tenant_id', 'suppliers', ['tenant_id'])
|
||||
op.create_index('ix_suppliers_name', 'suppliers', ['name'])
|
||||
op.create_index('ix_suppliers_tenant_name', 'suppliers', ['tenant_id', 'name'])
|
||||
op.create_index('ix_suppliers_tenant_status', 'suppliers', ['tenant_id', 'status'])
|
||||
op.create_index('ix_suppliers_tenant_type', 'suppliers', ['tenant_id', 'supplier_type'])
|
||||
op.create_index('ix_suppliers_quality_rating', 'suppliers', ['quality_rating'])
|
||||
op.create_index('ix_suppliers_status', 'suppliers', ['status'])
|
||||
op.create_index('ix_suppliers_supplier_type', 'suppliers', ['supplier_type'])
|
||||
|
||||
op.create_index('ix_price_lists_tenant_id', 'supplier_price_lists', ['tenant_id'])
|
||||
op.create_index('ix_price_lists_supplier_id', 'supplier_price_lists', ['supplier_id'])
|
||||
op.create_index('ix_price_lists_tenant_supplier', 'supplier_price_lists', ['tenant_id', 'supplier_id'])
|
||||
op.create_index('ix_price_lists_ingredient', 'supplier_price_lists', ['ingredient_id'])
|
||||
op.create_index('ix_price_lists_active', 'supplier_price_lists', ['is_active'])
|
||||
op.create_index('ix_price_lists_effective_date', 'supplier_price_lists', ['effective_date'])
|
||||
|
||||
op.create_index('ix_purchase_orders_tenant_id', 'purchase_orders', ['tenant_id'])
|
||||
op.create_index('ix_purchase_orders_supplier_id', 'purchase_orders', ['supplier_id'])
|
||||
op.create_index('ix_purchase_orders_tenant_supplier', 'purchase_orders', ['tenant_id', 'supplier_id'])
|
||||
op.create_index('ix_purchase_orders_tenant_status', 'purchase_orders', ['tenant_id', 'status'])
|
||||
op.create_index('ix_purchase_orders_po_number', 'purchase_orders', ['po_number'])
|
||||
op.create_index('ix_purchase_orders_order_date', 'purchase_orders', ['order_date'])
|
||||
op.create_index('ix_purchase_orders_delivery_date', 'purchase_orders', ['required_delivery_date'])
|
||||
op.create_index('ix_purchase_orders_status', 'purchase_orders', ['status'])
|
||||
|
||||
op.create_index('ix_po_items_tenant_id', 'purchase_order_items', ['tenant_id'])
|
||||
op.create_index('ix_po_items_purchase_order_id', 'purchase_order_items', ['purchase_order_id'])
|
||||
op.create_index('ix_po_items_tenant_po', 'purchase_order_items', ['tenant_id', 'purchase_order_id'])
|
||||
op.create_index('ix_po_items_ingredient', 'purchase_order_items', ['ingredient_id'])
|
||||
|
||||
op.create_index('ix_deliveries_tenant_id', 'deliveries', ['tenant_id'])
|
||||
op.create_index('ix_deliveries_tenant_status', 'deliveries', ['tenant_id', 'status'])
|
||||
op.create_index('ix_deliveries_scheduled_date', 'deliveries', ['scheduled_date'])
|
||||
op.create_index('ix_deliveries_delivery_number', 'deliveries', ['delivery_number'])
|
||||
|
||||
op.create_index('ix_delivery_items_tenant_id', 'delivery_items', ['tenant_id'])
|
||||
op.create_index('ix_delivery_items_delivery_id', 'delivery_items', ['delivery_id'])
|
||||
op.create_index('ix_delivery_items_tenant_delivery', 'delivery_items', ['tenant_id', 'delivery_id'])
|
||||
op.create_index('ix_delivery_items_ingredient', 'delivery_items', ['ingredient_id'])
|
||||
|
||||
op.create_index('ix_quality_reviews_tenant_id', 'supplier_quality_reviews', ['tenant_id'])
|
||||
op.create_index('ix_quality_reviews_supplier_id', 'supplier_quality_reviews', ['supplier_id'])
|
||||
op.create_index('ix_quality_reviews_tenant_supplier', 'supplier_quality_reviews', ['tenant_id', 'supplier_id'])
|
||||
op.create_index('ix_quality_reviews_date', 'supplier_quality_reviews', ['review_date'])
|
||||
op.create_index('ix_quality_reviews_overall_rating', 'supplier_quality_reviews', ['overall_rating'])
|
||||
|
||||
op.create_index('ix_invoices_tenant_id', 'supplier_invoices', ['tenant_id'])
|
||||
op.create_index('ix_invoices_supplier_id', 'supplier_invoices', ['supplier_id'])
|
||||
op.create_index('ix_invoices_tenant_supplier', 'supplier_invoices', ['tenant_id', 'supplier_id'])
|
||||
op.create_index('ix_invoices_tenant_status', 'supplier_invoices', ['tenant_id', 'status'])
|
||||
op.create_index('ix_invoices_due_date', 'supplier_invoices', ['due_date'])
|
||||
op.create_index('ix_invoices_invoice_number', 'supplier_invoices', ['invoice_number'])
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# Drop indexes
|
||||
op.drop_index('ix_invoices_invoice_number', 'supplier_invoices')
|
||||
op.drop_index('ix_invoices_due_date', 'supplier_invoices')
|
||||
op.drop_index('ix_invoices_tenant_status', 'supplier_invoices')
|
||||
op.drop_index('ix_invoices_tenant_supplier', 'supplier_invoices')
|
||||
op.drop_index('ix_invoices_supplier_id', 'supplier_invoices')
|
||||
op.drop_index('ix_invoices_tenant_id', 'supplier_invoices')
|
||||
|
||||
op.drop_index('ix_quality_reviews_overall_rating', 'supplier_quality_reviews')
|
||||
op.drop_index('ix_quality_reviews_date', 'supplier_quality_reviews')
|
||||
op.drop_index('ix_quality_reviews_tenant_supplier', 'supplier_quality_reviews')
|
||||
op.drop_index('ix_quality_reviews_supplier_id', 'supplier_quality_reviews')
|
||||
op.drop_index('ix_quality_reviews_tenant_id', 'supplier_quality_reviews')
|
||||
|
||||
op.drop_index('ix_delivery_items_ingredient', 'delivery_items')
|
||||
op.drop_index('ix_delivery_items_tenant_delivery', 'delivery_items')
|
||||
op.drop_index('ix_delivery_items_delivery_id', 'delivery_items')
|
||||
op.drop_index('ix_delivery_items_tenant_id', 'delivery_items')
|
||||
|
||||
op.drop_index('ix_deliveries_delivery_number', 'deliveries')
|
||||
op.drop_index('ix_deliveries_scheduled_date', 'deliveries')
|
||||
op.drop_index('ix_deliveries_tenant_status', 'deliveries')
|
||||
op.drop_index('ix_deliveries_tenant_id', 'deliveries')
|
||||
|
||||
op.drop_index('ix_po_items_ingredient', 'purchase_order_items')
|
||||
op.drop_index('ix_po_items_tenant_po', 'purchase_order_items')
|
||||
op.drop_index('ix_po_items_purchase_order_id', 'purchase_order_items')
|
||||
op.drop_index('ix_po_items_tenant_id', 'purchase_order_items')
|
||||
|
||||
op.drop_index('ix_purchase_orders_status', 'purchase_orders')
|
||||
op.drop_index('ix_purchase_orders_delivery_date', 'purchase_orders')
|
||||
op.drop_index('ix_purchase_orders_order_date', 'purchase_orders')
|
||||
op.drop_index('ix_purchase_orders_po_number', 'purchase_orders')
|
||||
op.drop_index('ix_purchase_orders_tenant_status', 'purchase_orders')
|
||||
op.drop_index('ix_purchase_orders_tenant_supplier', 'purchase_orders')
|
||||
op.drop_index('ix_purchase_orders_supplier_id', 'purchase_orders')
|
||||
op.drop_index('ix_purchase_orders_tenant_id', 'purchase_orders')
|
||||
|
||||
op.drop_index('ix_price_lists_effective_date', 'supplier_price_lists')
|
||||
op.drop_index('ix_price_lists_active', 'supplier_price_lists')
|
||||
op.drop_index('ix_price_lists_ingredient', 'supplier_price_lists')
|
||||
op.drop_index('ix_price_lists_tenant_supplier', 'supplier_price_lists')
|
||||
op.drop_index('ix_price_lists_supplier_id', 'supplier_price_lists')
|
||||
op.drop_index('ix_price_lists_tenant_id', 'supplier_price_lists')
|
||||
|
||||
op.drop_index('ix_suppliers_supplier_type', 'suppliers')
|
||||
op.drop_index('ix_suppliers_status', 'suppliers')
|
||||
op.drop_index('ix_suppliers_quality_rating', 'suppliers')
|
||||
op.drop_index('ix_suppliers_tenant_type', 'suppliers')
|
||||
op.drop_index('ix_suppliers_tenant_status', 'suppliers')
|
||||
op.drop_index('ix_suppliers_tenant_name', 'suppliers')
|
||||
op.drop_index('ix_suppliers_name', 'suppliers')
|
||||
op.drop_index('ix_suppliers_tenant_id', 'suppliers')
|
||||
|
||||
# Drop tables
|
||||
op.drop_table('supplier_invoices')
|
||||
op.drop_table('supplier_quality_reviews')
|
||||
op.drop_table('delivery_items')
|
||||
op.drop_table('deliveries')
|
||||
op.drop_table('purchase_order_items')
|
||||
op.drop_table('purchase_orders')
|
||||
op.drop_table('supplier_price_lists')
|
||||
op.drop_table('suppliers')
|
||||
|
||||
# Drop enums
|
||||
op.execute('DROP TYPE IF EXISTS invoicestatus')
|
||||
op.execute('DROP TYPE IF EXISTS deliveryrating')
|
||||
op.execute('DROP TYPE IF EXISTS qualityrating')
|
||||
op.execute('DROP TYPE IF EXISTS deliverystatus')
|
||||
op.execute('DROP TYPE IF EXISTS purchaseorderstatus')
|
||||
op.execute('DROP TYPE IF EXISTS paymentterms')
|
||||
op.execute('DROP TYPE IF EXISTS supplierstatus')
|
||||
op.execute('DROP TYPE IF EXISTS suppliertype')
|
||||
Reference in New Issue
Block a user