# Generated by Django 5.2.17 on 2026-09-01 07:06

import datetime
import django.db.models.deletion
from decimal import Decimal
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('catalog_app', '0030_publisher_book_publisher'),
        ('inventory', '0007_reportshub_and_more'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='BookstoreListing',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('agreed_unit_price', models.DecimalField(decimal_places=2, help_text="The price the payee's earnings are computed from — independent of what the book actually sells for at this bookstore.", max_digits=10)),
                ('bookstore_cut_percent', models.DecimalField(decimal_places=2, help_text='% of the actual sale amount the bookstore keeps.', max_digits=5)),
                ('payee_cut_percent', models.DecimalField(decimal_places=2, help_text='% of the agreed price paid to the author/publisher. Tai AfroPress keeps the remainder, including any markup above the agreed price.', max_digits=5)),
                ('effective_from', models.DateField(default=datetime.date.today, help_text="Terms apply to sales on or after this date, until a newer listing's effective_from.")),
                ('notes', models.TextField(blank=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
            ],
            options={
                'verbose_name': 'Bookstore Listing',
                'verbose_name_plural': 'Bookstore Listings',
                'ordering': ['book', 'bookstore', '-effective_from'],
            },
        ),
        migrations.AddField(
            model_name='authorpayout',
            name='publisher',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='payouts', to='catalog_app.publisher'),
        ),
        migrations.AddField(
            model_name='sale',
            name='total_bookstore_cut',
            field=models.DecimalField(decimal_places=2, default=Decimal('0'), help_text="Sum of external bookstores' cut, for distribution-channel sales only.", max_digits=12),
        ),
        migrations.AddField(
            model_name='saleitem',
            name='bookstore_cut_amount',
            field=models.DecimalField(blank=True, decimal_places=2, max_digits=12, null=True),
        ),
        migrations.AddField(
            model_name='saleitem',
            name='bookstore_cut_percent',
            field=models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True),
        ),
        migrations.AlterField(
            model_name='authorpayout',
            name='author',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='payouts', to='catalog_app.author'),
        ),
        migrations.AddIndex(
            model_name='authorpayout',
            index=models.Index(fields=['publisher', 'status'], name='inventory_a_publish_05ca47_idx'),
        ),
        migrations.AddConstraint(
            model_name='authorpayout',
            constraint=models.CheckConstraint(condition=models.Q(models.Q(('author__isnull', False), ('publisher__isnull', True)), models.Q(('author__isnull', True), ('publisher__isnull', False)), _connector='OR'), name='authorpayout_exactly_one_payee'),
        ),
        migrations.AddField(
            model_name='bookstorelisting',
            name='book',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bookstore_listings', to='catalog_app.book'),
        ),
        migrations.AddField(
            model_name='bookstorelisting',
            name='bookstore',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='listings', to='inventory.bookstore'),
        ),
        migrations.AddField(
            model_name='bookstorelisting',
            name='created_by',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='bookstore_listings_created', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='saleitem',
            name='bookstore_listing',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='sale_items', to='inventory.bookstorelisting'),
        ),
        migrations.AddIndex(
            model_name='bookstorelisting',
            index=models.Index(fields=['book', 'bookstore', 'effective_from'], name='inventory_b_book_id_8b722b_idx'),
        ),
    ]
