"""add_tax_units_table Revision ID: caf3f4ef4b76 Revises: 20250929_000501_add_products_and_pricing Create Date: 2025-09-30 14:46:58.614162 """ from alembic import op import sqlalchemy as sa from sqlalchemy.dialects import mysql from sqlalchemy import inspect # revision identifiers, used by Alembic. revision = 'caf3f4ef4b76' down_revision = '20250929_000501_add_products_and_pricing' branch_labels = None depends_on = None def upgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### bind = op.get_bind() inspector = inspect(bind) # Check if persons table exists and has the code column if 'persons' in inspector.get_table_names(): cols = {c['name'] for c in inspector.get_columns('persons')} # Only alter code column if it exists if 'code' in cols: op.alter_column('persons', 'code', existing_type=mysql.INTEGER(), comment='کد یکتا در هر کسب و کار', existing_nullable=True) # Only alter person_type column if it exists if 'person_type' in cols: op.alter_column('persons', 'person_type', existing_type=mysql.ENUM('مشتری', 'بازاریاب', 'کارمند', 'تامین\u200cکننده', 'همکار', 'فروشنده', 'سهامدار', collation='utf8mb4_general_ci'), comment='نوع شخص', existing_nullable=False) # Only alter person_types column if it exists if 'person_types' in cols: op.alter_column('persons', 'person_types', existing_type=mysql.TEXT(collation='utf8mb4_general_ci'), comment='لیست انواع شخص به صورت JSON', existing_nullable=True) # Only alter commission columns if they exist if 'commission_sale_percent' in cols: op.alter_column('persons', 'commission_sale_percent', existing_type=mysql.DECIMAL(precision=5, scale=2), comment='درصد پورسانت از فروش', existing_nullable=True) if 'commission_sales_return_percent' in cols: op.alter_column('persons', 'commission_sales_return_percent', existing_type=mysql.DECIMAL(precision=5, scale=2), comment='درصد پورسانت از برگشت از فروش', existing_nullable=True) if 'commission_sales_amount' in cols: op.alter_column('persons', 'commission_sales_amount', existing_type=mysql.DECIMAL(precision=12, scale=2), comment='مبلغ فروش مبنا برای پورسانت', existing_nullable=True) if 'commission_sales_return_amount' in cols: op.alter_column('persons', 'commission_sales_return_amount', existing_type=mysql.DECIMAL(precision=12, scale=2), comment='مبلغ برگشت از فروش مبنا برای پورسانت', existing_nullable=True) if 'commission_exclude_discounts' in cols: op.alter_column('persons', 'commission_exclude_discounts', existing_type=mysql.TINYINT(display_width=1), comment='عدم محاسبه تخفیف در پورسانت', existing_nullable=False, existing_server_default=sa.text("'0'")) if 'commission_exclude_additions_deductions' in cols: op.alter_column('persons', 'commission_exclude_additions_deductions', existing_type=mysql.TINYINT(display_width=1), comment='عدم محاسبه اضافات و کسورات فاکتور در پورسانت', existing_nullable=False, existing_server_default=sa.text("'0'")) if 'commission_post_in_invoice_document' in cols: op.alter_column('persons', 'commission_post_in_invoice_document', existing_type=mysql.TINYINT(display_width=1), comment='ثبت پورسانت در سند حسابداری فاکتور', existing_nullable=False, existing_server_default=sa.text("'0'")) # Continue with other operations op.alter_column('price_items', 'tier_name', existing_type=mysql.VARCHAR(length=64), comment='نام پله قیمت (تکی/عمده/همکار/...)', existing_nullable=False) op.create_index(op.f('ix_price_items_currency_id'), 'price_items', ['currency_id'], unique=False) op.create_index(op.f('ix_price_items_unit_id'), 'price_items', ['unit_id'], unique=False) op.create_index(op.f('ix_price_lists_currency_id'), 'price_lists', ['currency_id'], unique=False) op.create_index(op.f('ix_price_lists_default_unit_id'), 'price_lists', ['default_unit_id'], unique=False) op.create_index(op.f('ix_price_lists_name'), 'price_lists', ['name'], unique=False) op.alter_column('products', 'item_type', existing_type=mysql.ENUM('کالا', 'خدمت'), comment='نوع آیتم (کالا/خدمت)', existing_nullable=False) op.alter_column('products', 'code', existing_type=mysql.VARCHAR(length=64), comment='کد یکتا در هر کسب\u200cوکار', existing_nullable=False) op.create_index(op.f('ix_products_category_id'), 'products', ['category_id'], unique=False) op.create_index(op.f('ix_products_main_unit_id'), 'products', ['main_unit_id'], unique=False) op.create_index(op.f('ix_products_secondary_unit_id'), 'products', ['secondary_unit_id'], unique=False) op.create_index(op.f('ix_products_tax_type_id'), 'products', ['tax_type_id'], unique=False) op.create_index(op.f('ix_products_tax_unit_id'), 'products', ['tax_unit_id'], unique=False) # ### end Alembic commands ### def downgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### op.drop_index(op.f('ix_products_tax_unit_id'), table_name='products') op.drop_index(op.f('ix_products_tax_type_id'), table_name='products') op.drop_index(op.f('ix_products_secondary_unit_id'), table_name='products') op.drop_index(op.f('ix_products_main_unit_id'), table_name='products') op.drop_index(op.f('ix_products_category_id'), table_name='products') op.alter_column('products', 'code', existing_type=mysql.VARCHAR(length=64), comment=None, existing_comment='کد یکتا در هر کسب\u200cوکار', existing_nullable=False) op.alter_column('products', 'item_type', existing_type=mysql.ENUM('کالا', 'خدمت'), comment=None, existing_comment='نوع آیتم (کالا/خدمت)', existing_nullable=False) op.drop_index(op.f('ix_price_lists_name'), table_name='price_lists') op.drop_index(op.f('ix_price_lists_default_unit_id'), table_name='price_lists') op.drop_index(op.f('ix_price_lists_currency_id'), table_name='price_lists') op.drop_index(op.f('ix_price_items_unit_id'), table_name='price_items') op.drop_index(op.f('ix_price_items_currency_id'), table_name='price_items') op.alter_column('price_items', 'tier_name', existing_type=mysql.VARCHAR(length=64), comment=None, existing_comment='نام پله قیمت (تکی/عمده/همکار/...)', existing_nullable=False) op.alter_column('persons', 'commission_post_in_invoice_document', existing_type=mysql.TINYINT(display_width=1), comment=None, existing_comment='ثبت پورسانت در سند حسابداری فاکتور', existing_nullable=False, existing_server_default=sa.text("'0'")) op.alter_column('persons', 'commission_exclude_additions_deductions', existing_type=mysql.TINYINT(display_width=1), comment=None, existing_comment='عدم محاسبه اضافات و کسورات فاکتور در پورسانت', existing_nullable=False, existing_server_default=sa.text("'0'")) op.alter_column('persons', 'commission_exclude_discounts', existing_type=mysql.TINYINT(display_width=1), comment=None, existing_comment='عدم محاسبه تخفیف در پورسانت', existing_nullable=False, existing_server_default=sa.text("'0'")) op.alter_column('persons', 'commission_sales_return_amount', existing_type=mysql.DECIMAL(precision=12, scale=2), comment=None, existing_comment='مبلغ برگشت از فروش مبنا برای پورسانت', existing_nullable=True) op.alter_column('persons', 'commission_sales_amount', existing_type=mysql.DECIMAL(precision=12, scale=2), comment=None, existing_comment='مبلغ فروش مبنا برای پورسانت', existing_nullable=True) op.alter_column('persons', 'commission_sales_return_percent', existing_type=mysql.DECIMAL(precision=5, scale=2), comment=None, existing_comment='درصد پورسانت از برگشت از فروش', existing_nullable=True) op.alter_column('persons', 'commission_sale_percent', existing_type=mysql.DECIMAL(precision=5, scale=2), comment=None, existing_comment='درصد پورسانت از فروش', existing_nullable=True) op.alter_column('persons', 'person_types', existing_type=mysql.TEXT(collation='utf8mb4_general_ci'), comment=None, existing_comment='لیست انواع شخص به صورت JSON', existing_nullable=True) op.alter_column('persons', 'person_type', existing_type=mysql.ENUM('مشتری', 'بازاریاب', 'کارمند', 'تامین\u200cکننده', 'همکار', 'فروشنده', 'سهامدار', collation='utf8mb4_general_ci'), comment=None, existing_comment='نوع شخص', existing_nullable=False) op.alter_column('persons', 'code', existing_type=mysql.INTEGER(), comment=None, existing_comment='کد یکتا در هر کسب و کار', existing_nullable=True) # ### end Alembic commands ###