Skip to content

Commit

Permalink
Merge pull request #20 from openimis/develop
Browse files Browse the repository at this point in the history
MERGING develop into release/24.04
  • Loading branch information
delcroip committed Mar 28, 2024
2 parents b96481a + 844bc34 commit e4e3469
Show file tree
Hide file tree
Showing 16 changed files with 495 additions and 269 deletions.
4 changes: 2 additions & 2 deletions payment_cycle/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'gql_create_payment_cycle_perms': ['200002'],
'gql_update_payment_cycle_perms': ['200003'],
'gql_delete_payment_cycle_perms': ['200004'],
'gql_mutation_process_payment_cycle_perms': ['200005']
'gql_check_payment_cycle': True,
}


Expand All @@ -19,7 +19,7 @@ class PaymentCycleConfig(AppConfig):
gql_create_payment_cycle_perms = None
gql_update_payment_cycle_perms = None
gql_delete_payment_cycle_perms = None
gql_mutation_process_payment_cycle_perms = None
gql_check_payment_cycle = None

def ready(self):
from core.models import ModuleConfiguration
Expand Down
87 changes: 78 additions & 9 deletions payment_cycle/gql_mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,91 @@
from django.core.exceptions import PermissionDenied
from django.utils.translation import gettext as _

from core.gql.gql_mutations.base_mutation import BaseHistoryModelCreateMutationMixin, BaseMutation, \
BaseHistoryModelUpdateMutationMixin
from core.schema import OpenIMISMutation
from payment_cycle.apps import PaymentCycleConfig
from payment_cycle.services import BenefitPlanPaymentCycleService
from payment_cycle.models import PaymentCycle, PaymentCycleMutation
from payment_cycle.services import PaymentCycleService


class ProcessBenefitPlanPaymentCycleMutation(OpenIMISMutation):
class CreatePaymentCycleInput(OpenIMISMutation.Input):
class PaymentCycleEnum(graphene.Enum):
PENDING = PaymentCycle.PaymentCycleStatus.PENDING
ACTIVE = PaymentCycle.PaymentCycleStatus.ACTIVE
SUSPENDED = PaymentCycle.PaymentCycleStatus.SUSPENDED

code = graphene.String(required=True)
start_date = graphene.Date(required=True)
end_date = graphene.Date(required=True)
status = graphene.Field(PaymentCycleEnum, required=True)


class UpdatePaymentCycleInput(CreatePaymentCycleInput):
id = graphene.UUID(required=True)


class CreatePaymentCycleMutation(BaseHistoryModelCreateMutationMixin, BaseMutation):
_mutation_module = "payment_cycle"
_mutation_class = "ProcessBenefitPlanPaymentCycleMutation"
_mutation_class = "CreatePaymentCycleMutation"

class Input(OpenIMISMutation.Input):
year = graphene.Int()
month = graphene.Int()
@classmethod
def _validate_mutation(cls, user, **data):
super()._validate_mutation(user, **data)
if not user.has_perms(PaymentCycleConfig.gql_create_payment_cycle_perms):
raise PermissionDenied(_("unauthorized"))

@classmethod
def async_mutate(cls, user, **data):
if not user.has_perms(PaymentCycleConfig.gql_mutation_process_payment_cycle_perms):
def _mutate(cls, user, **data):
client_mutation_id = data.pop('client_mutation_id', None)
data.pop('client_mutation_label', None)

service = PaymentCycleService(user)
if (PaymentCycleConfig.gql_check_payment_cycle and
data["status"] == PaymentCycle.PaymentCycleStatus.ACTIVE):
res = service.create_create_task(data)
else:
res = service.create(data)
if client_mutation_id and res['success']:
payment_cycle_id = res['data']['id']
payment_cycle = PaymentCycle.objects.get(id=payment_cycle_id)
PaymentCycleMutation.object_mutated(
user, client_mutation_id=client_mutation_id, payment_cycle=payment_cycle
)
return res if not res['success'] else None

class Input(CreatePaymentCycleInput):
pass


class UpdatePaymentCycleMutation(BaseHistoryModelUpdateMutationMixin, BaseMutation):
_mutation_module = "payment_cycle"
_mutation_class = "PaymentCycleMutation"
_model = PaymentCycle

@classmethod
def _validate_mutation(cls, user, **data):
super()._validate_mutation(user, **data)
if not user.has_perms(PaymentCycleConfig.gql_update_payment_cycle_perms):
raise PermissionDenied(_("unauthorized"))
res = BenefitPlanPaymentCycleService(user).process(**data)

@classmethod
def _mutate(cls, user, **data):
client_mutation_id = data.pop('client_mutation_id', None)
data.pop('client_mutation_label', None)

service = PaymentCycleService(user)
if PaymentCycleConfig.gql_check_payment_cycle:
res = service.create_update_task(data)
else:
res = service.update(data)
if client_mutation_id and res['success']:
payment_cycle_id = data["id"]
payment_cycle = PaymentCycle.objects.get(id=payment_cycle_id)
PaymentCycleMutation.object_mutated(
user, client_mutation_id=client_mutation_id, payment_cycle=payment_cycle
)
return res if not res['success'] else None

class Input(UpdatePaymentCycleInput):
pass
6 changes: 4 additions & 2 deletions payment_cycle/gql_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class Meta:
interfaces = (graphene.relay.Node,)
filter_fields = {
"id": ["exact"],
"run_year": ["exact"],
"run_month": ["exact"],
"code": ["exact", "istartswith", "icontains", "iexact"],
"start_date": ["exact", "lt", "lte", "gt", "gte"],
"end_date": ["exact", "lt", "lte", "gt", "gte"],
"status": ["exact", "istartswith", "icontains", "iexact"],
"type_id": ["exact"],

"date_created": ["exact", "lt", "lte", "gt", "gte"],
Expand Down
123 changes: 123 additions & 0 deletions payment_cycle/migrations/0004_add_start_and_end_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
from django.db import migrations, models
import string
import random
import core.fields
from django.utils.translation import gettext_lazy as _
from datetime import date
from calendar import monthrange, month_name


def generate_random_code():
characters = string.ascii_letters + string.digits
return ''.join(random.choice(characters) for _ in range(8))


def get_default_date():
return date(1999, 6, 28)


def get_date(year, month):
first_day = 1
_, last_day = monthrange(year, month)
return date(year, month, first_day), date(year, month, last_day)


def create_code_from_date(apps, schema_editor):
PaymentCycle = apps.get_model('payment_cycle', 'PaymentCycle')
for payment_cycle in PaymentCycle.objects.all():
month_name_string = month_name[payment_cycle.run_month]
year_month_code = f"{payment_cycle.run_year}-{month_name_string}"

new_code = year_month_code
while PaymentCycle.objects.filter(code=new_code).exists():
new_code = f"{year_month_code}-{generate_random_code()}"

payment_cycle.code = new_code
payment_cycle.save()


def set_default_start_end_date_and_status(apps, schema_editor):
PaymentCycle = apps.get_model('payment_cycle', 'PaymentCycle')

for payment_cycle in PaymentCycle.objects.all():
start_date, end_date = get_date(payment_cycle.run_year, payment_cycle.run_month)
payment_cycle.start_date = start_date
payment_cycle.end_date = end_date
payment_cycle.save()


def create_unique_code_historical(apps, schema_editor):
PaymentCycle = apps.get_model('payment_cycle', 'PaymentCycle')
HistoricalPaymentCycle = apps.get_model('payment_cycle', 'HistoricalPaymentCycle')

for payment_cycle in PaymentCycle.objects.all():
historical_payment_cycles = HistoricalPaymentCycle.objects.filter(id=payment_cycle.id)
for historical in historical_payment_cycles:
historical.code = payment_cycle.code
historical.save()


def set_default_start_end_date_and_status_historical(apps, schema_editor):
HistoricalPaymentCycle = apps.get_model('payment_cycle', 'HistoricalPaymentCycle')

for payment_cycle in HistoricalPaymentCycle.objects.all():
start_date, end_date = get_date(payment_cycle.run_year, payment_cycle.run_month)
payment_cycle.start_date = start_date
payment_cycle.end_date = end_date
payment_cycle.save()


class Migration(migrations.Migration):
dependencies = [
('payment_cycle', '0003_alter_historicalpaymentcycle_date_created_and_more'),
]

operations = [
migrations.AddField(
model_name='paymentcycle',
name='code',
field=models.CharField(max_length=255, default=generate_random_code()),
),
migrations.AddField(
model_name='paymentcycle',
name='start_date',
field=core.fields.DateField(default=get_default_date()),
),
migrations.AddField(
model_name='paymentcycle',
name='end_date',
field=core.fields.DateField(default=get_default_date()),
),
migrations.AddField(
model_name='paymentcycle',
name='status',
field=models.CharField(max_length=255, choices=[('PENDING', _('PENDING')), ('ACTIVE', _('ACTIVE')),
('SUSPENDED', _('SUSPENDED'))], default='PENDING'),
),
migrations.RunPython(set_default_start_end_date_and_status),
migrations.RunPython(create_code_from_date),
# historical
migrations.AddField(
model_name='historicalpaymentcycle',
name='code',
field=models.CharField(max_length=255, default=generate_random_code()),
),
migrations.AddField(
model_name='historicalpaymentcycle',
name='start_date',
field=core.fields.DateField(default=get_default_date()),
),
migrations.AddField(
model_name='historicalpaymentcycle',
name='end_date',
field=core.fields.DateField(default=get_default_date()),
),
migrations.AddField(
model_name='historicalpaymentcycle',
name='status',
field=models.CharField(max_length=255, choices=[('PENDING', _('PENDING')), ('ACTIVE', _('ACTIVE')),
('SUSPENDED', _('SUSPENDED'))], default='PENDING'),
),
migrations.RunPython(set_default_start_end_date_and_status_historical),
migrations.RunPython(create_unique_code_historical),
]
29 changes: 29 additions & 0 deletions payment_cycle/migrations/0005_remove_run_month.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.2.24 on 2024-03-11 13:53

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('payment_cycle', '0004_add_start_and_end_date'),
]

operations = [
migrations.RemoveField(
model_name='historicalpaymentcycle',
name='run_year',
),
migrations.RemoveField(
model_name='historicalpaymentcycle',
name='run_month',
),
migrations.RemoveField(
model_name='paymentcycle',
name='run_year',
),
migrations.RemoveField(
model_name='paymentcycle',
name='run_month',
),
]
44 changes: 44 additions & 0 deletions payment_cycle/migrations/0006_auto_20240315_0943.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 3.2.24 on 2024-03-15 09:43

import core.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('payment_cycle', '0005_remove_run_month'),
]

operations = [
migrations.AlterField(
model_name='historicalpaymentcycle',
name='code',
field=models.CharField(max_length=255),
),
migrations.AlterField(
model_name='historicalpaymentcycle',
name='end_date',
field=core.fields.DateField(),
),
migrations.AlterField(
model_name='historicalpaymentcycle',
name='start_date',
field=core.fields.DateField(),
),
migrations.AlterField(
model_name='paymentcycle',
name='code',
field=models.CharField(max_length=255),
),
migrations.AlterField(
model_name='paymentcycle',
name='end_date',
field=core.fields.DateField(),
),
migrations.AlterField(
model_name='paymentcycle',
name='start_date',
field=core.fields.DateField(),
),
]
29 changes: 29 additions & 0 deletions payment_cycle/migrations/0007_paymentcyclemutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.2.24 on 2024-03-20 12:33

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


class Migration(migrations.Migration):

dependencies = [
('core', '0027_alter_interactiveuser_last_login_and_more'),
('payment_cycle', '0006_auto_20240315_0943'),
]

operations = [
migrations.CreateModel(
name='PaymentCycleMutation',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('mutation', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, related_name='payment_cycle', to='core.mutationlog')),
('payment_cycle', models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, related_name='mutations', to='payment_cycle.paymentcycle')),
],
options={
'abstract': False,
},
bases=(models.Model, core.models.ObjectMutation),
),
]
24 changes: 20 additions & 4 deletions payment_cycle/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
from django.contrib.contenttypes.models import ContentType
from django.db import models

from core.models import HistoryModel
from gettext import gettext as _

from core.models import HistoryModel, ObjectMutation, UUIDModel, MutationLog
from core.fields import DateField


class PaymentCycle(HistoryModel):
run_year = models.IntegerField()
run_month = models.SmallIntegerField()
type = models.ForeignKey(ContentType, on_delete=models.DO_NOTHING, blank=True, null=True, unique=False)
class PaymentCycleStatus(models.TextChoices):
PENDING = 'PENDING', _('PENDING')
ACTIVE = 'ACTIVE', _('ACTIVE')
SUSPENDED = 'SUSPENDED', _('SUSPENDED')

code = models.CharField(max_length=255, blank=False, null=False)
start_date = DateField(blank=False, null=False)
end_date = DateField(blank=False, null=False)
status = models.CharField(max_length=255, choices=PaymentCycleStatus.choices, default=PaymentCycleStatus.PENDING)
type = models.ForeignKey(ContentType, on_delete=models.DO_NOTHING, blank=True, null=True, unique=False)


class PaymentCycleMutation(UUIDModel, ObjectMutation):
payment_cycle = models.ForeignKey(PaymentCycle, models.DO_NOTHING, related_name='mutations')
mutation = models.ForeignKey(
MutationLog, models.DO_NOTHING, related_name='payment_cycle')
Loading

0 comments on commit e4e3469

Please sign in to comment.