# Generated by Django 5.2.7 on 2025-12-01 06:34

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('basic_info', '0016_merchantsetting_type'),
        ('business', '0012_customerbalance_supplierbalance'),
    ]

    operations = [
        migrations.CreateModel(
            name='BalanceChangeRecord',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='更新时间')),
                ('target_type', models.IntegerField(choices=[(1, '供应商'), (2, '客户')], verbose_name='业务主体类型')),
                ('source_type', models.IntegerField(choices=[(1, '采购单'), (2, '销售单'), (3, '付款单'), (4, '收款单')], verbose_name='来源业务类型')),
                ('source_id', models.BigIntegerField(verbose_name='来源业务ID')),
                ('delta', models.DecimalField(decimal_places=2, max_digits=15, verbose_name='变动金额')),
                ('direction', models.IntegerField(choices=[(1, '增加'), (2, '减少')], verbose_name='方向')),
                ('balance_before', models.DecimalField(decimal_places=2, max_digits=15, verbose_name='变动前余额')),
                ('balance_after', models.DecimalField(decimal_places=2, max_digits=15, verbose_name='变动后余额')),
                ('request_id', models.CharField(blank=True, max_length=64, null=True, verbose_name='幂等请求ID')),
                ('remarks', models.TextField(blank=True, null=True, verbose_name='备注')),
                ('extra_meta', models.JSONField(blank=True, default=dict, verbose_name='扩展信息')),
                ('offset_to', models.BigIntegerField(blank=True, null=True, verbose_name='冲抵目标ID')),
                ('offset_at', models.DateTimeField(blank=True, null=True, verbose_name='冲抵时间')),
                ('offset_id', models.BigIntegerField(blank=True, null=True, verbose_name='冲抵来源ID')),
                ('cancelled', models.BooleanField(default=False, verbose_name='已被冲抵')),
                ('cancelled_at', models.DateTimeField(blank=True, null=True, verbose_name='被冲抵时间')),
                ('customer', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='balance_change_records', to='basic_info.customer', verbose_name='客户')),
                ('merchant', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='balance_change_records', to='basic_info.merchant', verbose_name='所属商户')),
                ('supplier', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='balance_change_records', to='basic_info.supplier', verbose_name='供应商')),
            ],
            options={
                'verbose_name': '余额变动记录',
                'verbose_name_plural': '余额变动记录',
                'indexes': [models.Index(fields=['merchant', 'source_type', 'source_id'], name='balance_change_source_idx')],
                'constraints': [models.CheckConstraint(condition=models.Q(models.Q(('customer__isnull', True), ('supplier__isnull', False)), models.Q(('customer__isnull', False), ('supplier__isnull', True)), _connector='OR'), name='balance_change_single_counterparty')],
            },
        ),
    ]
