diff --git a/apps/transactions/migrations/0003_auto_20210511_0703.py b/apps/transactions/migrations/0003_auto_20210511_0703.py new file mode 100644 index 0000000..5bad782 --- /dev/null +++ b/apps/transactions/migrations/0003_auto_20210511_0703.py @@ -0,0 +1,38 @@ +# Generated by Django 3.2.2 on 2021-05-11 05:28 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('transactions', '0002_transaction_trade_id'), + ] + + operations = [ + migrations.AddField( + model_name='transaction', + name='crypto_amount', + field=models.DecimalField(blank=True, decimal_places=10, max_digits=24, null=True), + ), + migrations.AddField( + model_name='transaction', + name='crypto_fee', + field=models.DecimalField(blank=True, decimal_places=10, max_digits=24, null=True), + ), + migrations.AddField( + model_name='transaction', + name='fiat_amount', + field=models.DecimalField(blank=True, decimal_places=10, max_digits=24, null=True), + ), + migrations.AddField( + model_name='transaction', + name='fiat_fee', + field=models.DecimalField(blank=True, decimal_places=10, max_digits=24, null=True), + ), + migrations.AddField( + model_name='transaction', + name='transaction_type', + field=models.CharField(choices=[('B', 'Buy Crypto'), ('S', 'Sell Crypto'), ('T', 'Transfer Crypto')], default='B', max_length=1), + ), + ] diff --git a/apps/transactions/migrations/0004_auto_20210511_0703.py b/apps/transactions/migrations/0004_auto_20210511_0703.py new file mode 100644 index 0000000..120608f --- /dev/null +++ b/apps/transactions/migrations/0004_auto_20210511_0703.py @@ -0,0 +1,35 @@ +# Generated by Django 3.2 on 2021-05-11 05:03 + +from django.db import migrations + +def separate_fees(apps, schema_editor): + + Person = apps.get_model('transactions', 'Transaction') + for tr in Transaction.objects.all(): + if tr.amount_before_fee > 0: + tr.crypto_fee = tr.amount_before_fee - tr.amount_after_fee + tr.crypto_amount = tr.amount_before_fee + tr.transaction_type = 'B' + elif tr.amount_before_fee < 0: + tr.crypto_fee = -1 * tr.amount_before_fee + tr.amount_after_fee + tr.crypto_amount = -1 * tr.amount_before_fee + tr.transaction_type = 'S' + else: + tr.crypto_fee = 0 + tr.crypto_amount = 0 + tr.transaction_type = 'T' + + tr.fiat_amount = tr.amount + tr.fiat_fee = 0 + + tr.save() + +class Migration(migrations.Migration): + + dependencies = [ + ('transactions', '0003_auto_20210511_0703'), + ] + + operations = [ + migrations.RunPython(separate_fees), + ] diff --git a/apps/transactions/models.py b/apps/transactions/models.py index d4a82d5..872bb33 100644 --- a/apps/transactions/models.py +++ b/apps/transactions/models.py @@ -3,14 +3,26 @@ from django.contrib.auth.models import User # Create your models here. class Transaction(models.Model): + TRANSACTION_TYPES = ( + ('B', 'Buy Crypto'), + ('S', 'Sell Crypto'), + ('T', 'Transfer Crypto'), + ) + trade_id = models.CharField(max_length=20, default='MANUAL') - crypto_currency = models.CharField(max_length=10) owner = models.ForeignKey(User, on_delete=models.CASCADE) + crypto_currency = models.CharField(max_length=10) + crypto_amount = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True) + crypto_fee = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True) amount_before_fee = models.DecimalField(max_digits=24, decimal_places=10) amount_after_fee = models.DecimalField(max_digits=24, decimal_places=10) exchange_rate = models.DecimalField(max_digits=24, decimal_places=10) fiat_currency = models.CharField(max_length=10) + fiat_amount = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True) + fiat_fee = models.DecimalField(max_digits=24, decimal_places=10, null=True, blank=True) amount = models.DecimalField(max_digits=24, decimal_places=10) date_valid = models.DateTimeField('date transfered') + transaction_type = models.CharField(max_length=1, choices=TRANSACTION_TYPES, default='B') + class Meta: db_table = "transaction"