
from django.db import migrations
from django.db.models import F


def calculate_taxrate(apps, schema_editor):
    '''
    Calculate the tax rate based on current totals for any InvoiceItem that
    does not currently have a tax rate, so that we can make taxRate non-nullable.
    '''
    InvoiceItem = apps.get_model("core", "InvoiceItem")
    db_alias = schema_editor.connection.alias

    to_update = InvoiceItem.objects.using(db_alias).filter(taxRate__isnull=True)
    to_update.update(taxRate=100 * (F('taxes') / F('total')))


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0040_invoiceitem_taxrate'),
    ]

    operations = [
        migrations.RunPython(calculate_taxrate, migrations.RunPython.noop),
    ]


from django.db import migrations
from django.db.models import F


def calculate_taxrate(apps, schema_editor):
    '''
    Calculate the tax rate based on current totals for any InvoiceItem that
    does not currently have a tax rate, so that we can make taxRate non-nullable.
    '''
    InvoiceItem = apps.get_model("core", "InvoiceItem")
    db_alias = schema_editor.connection.alias

    to_update = InvoiceItem.objects.using(db_alias).filter(taxRate__isnull=True).exclude(total=0)
    to_update.update(taxRate=100 * (F('taxes') / F('total')))
    to_update = InvoiceItem.objects.using(db_alias).filter(taxRate__isnull=True)
    to_update.update(taxRate=0)


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0040_invoiceitem_taxrate'),
    ]

    operations = [
        migrations.RunPython(calculate_taxrate, migrations.RunPython.noop),
    ]

Fix division by zero error when calculating tax rate on migration.
Fix division by zero error when calculating tax rate on migration.

  
  from django.db import migrations
  from django.db.models import F
  
  
  def calculate_taxrate(apps, schema_editor):
      '''
      Calculate the tax rate based on current totals for any InvoiceItem that
      does not currently have a tax rate, so that we can make taxRate non-nullable.
      '''
      InvoiceItem = apps.get_model("core", "InvoiceItem")
      db_alias = schema_editor.connection.alias
  
+     to_update = InvoiceItem.objects.using(db_alias).filter(taxRate__isnull=True).exclude(total=0)
+     to_update.update(taxRate=100 * (F('taxes') / F('total')))
      to_update = InvoiceItem.objects.using(db_alias).filter(taxRate__isnull=True)
-     to_update.update(taxRate=100 * (F('taxes') / F('total')))
+     to_update.update(taxRate=0)
  
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('core', '0040_invoiceitem_taxrate'),
      ]
  
      operations = [
          migrations.RunPython(calculate_taxrate, migrations.RunPython.noop),
      ]
  
Fix division by zero error when calculating tax rate on migration.
## Code Before:

from django.db import migrations
from django.db.models import F


def calculate_taxrate(apps, schema_editor):
    '''
    Calculate the tax rate based on current totals for any InvoiceItem that
    does not currently have a tax rate, so that we can make taxRate non-nullable.
    '''
    InvoiceItem = apps.get_model("core", "InvoiceItem")
    db_alias = schema_editor.connection.alias

    to_update = InvoiceItem.objects.using(db_alias).filter(taxRate__isnull=True)
    to_update.update(taxRate=100 * (F('taxes') / F('total')))


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0040_invoiceitem_taxrate'),
    ]

    operations = [
        migrations.RunPython(calculate_taxrate, migrations.RunPython.noop),
    ]

## Instruction:
Fix division by zero error when calculating tax rate on migration.
## Code After:

from django.db import migrations
from django.db.models import F


def calculate_taxrate(apps, schema_editor):
    '''
    Calculate the tax rate based on current totals for any InvoiceItem that
    does not currently have a tax rate, so that we can make taxRate non-nullable.
    '''
    InvoiceItem = apps.get_model("core", "InvoiceItem")
    db_alias = schema_editor.connection.alias

    to_update = InvoiceItem.objects.using(db_alias).filter(taxRate__isnull=True).exclude(total=0)
    to_update.update(taxRate=100 * (F('taxes') / F('total')))
    to_update = InvoiceItem.objects.using(db_alias).filter(taxRate__isnull=True)
    to_update.update(taxRate=0)


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0040_invoiceitem_taxrate'),
    ]

    operations = [
        migrations.RunPython(calculate_taxrate, migrations.RunPython.noop),
    ]
