"""Peewee migrations -- 199_MAIN.py.

Tax document rounding: ``Setup.taxdocument_rounding_policy`` + ``TaxDocument.rounding_policy``.

The with-VAT total of a tax document can be rounded up to whole currency units (Czech
cash practice; the rounding item sits outside the VAT base). Whether that happens is a
Setup choice - never / cash payments only / always - defaulting to never, so deploying
this changes no plant's documents until someone opts in.

The chosen mode is snapshotted onto every new document, exactly like ``vat_rate`` and
``currency`` already are, so changing the setting later cannot move the amount printed on
an already issued document. Existing documents keep a NULL mode: they were issued before
rounding existed and printed the exact total, and ``TaxDocument.rounds_to_whole`` reads
NULL as "do not round", so a reprint still matches the paper in the customer's folder.
"""

import peewee as pw
from peewee_migrate import Migrator


def migrate(migrator: Migrator, database: pw.Database, *, fake=False):
    """Write your migrations here."""

    migrator.add_fields(
        'taxdocument',

        rounding_policy=pw.IntegerField(null=True))

    migrator.add_fields(
        'setup',

        taxdocument_rounding_policy=pw.IntegerField(default=0))


def rollback(migrator: Migrator, database: pw.Database, *, fake=False):
    """Write your rollback migrations here."""

    migrator.remove_fields('setup', 'taxdocument_rounding_policy')

    migrator.remove_fields('taxdocument', 'rounding_policy')
