Skip to content

Commit

Permalink
Merging development into delete_users_view
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelagz committed May 13, 2024
2 parents 670c425 + 7872338 commit 552c2ac
Show file tree
Hide file tree
Showing 17 changed files with 470 additions and 154 deletions.
3 changes: 3 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@

13/10/2023
- python manage.py set_mediafiles_names

29/04/2024
- python manage.py precursor_report
22 changes: 21 additions & 1 deletion src/laboratory/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ class LaboratoryAdmin(admin.ModelAdmin):
search_fields = ['name']
list_filter = ['organization']

class PrecursorReportValuesInline(admin.TabularInline):
model = models.PrecursorReportValues
fields = ["object", "measurement_unit", "previous_balance", "new_income", "stock", "month_expense", "final_balance"]

class PrecursorReportAdmin(admin.ModelAdmin):
search_fields = ["laboratory__name","month","year"]
list_filter = ["laboratory__name","month","year"]
list_display = ["consecutive","laboratory","month","year"]
inlines = (PrecursorReportValuesInline, )

class PrecursorReportValuesAdmin(admin.ModelAdmin):
search_fields = ["precursor_report__laboratory__name","object__code", "object__name"]

list_display = ["precursor_report","object","measurement_unit", "final_balance"]

class BaseUnittAdmin(admin.ModelAdmin):
list_display = ["measurement_unit","si_value"]

admin.site.register(models.Laboratory, LaboratoryAdmin)
admin.site.register(models.Protocol)
Expand All @@ -74,12 +91,15 @@ class LaboratoryAdmin(admin.ModelAdmin):
admin.site.register(models.Provider)
admin.site.register(models.ObjectLogChange)
admin.site.register(models.TranferObject)
admin.site.register(models.PrecursorReport)
admin.site.register(models.PrecursorReport,PrecursorReportAdmin)
admin.site.register(models.RegisterUserQR)
admin.site.register(models.OrganizationStructure, OrganizationStrutureAdmin)
admin.site.register(models.UserOrganization)
admin.site.register(models.InformScheduler, InformSchedulerAdmin)
admin.site.register(models.ShelfObjectObservation)
admin.site.register(models.BaseUnitValues,BaseUnittAdmin)
admin.site.register(models.PrecursorReportValues,PrecursorReportValuesAdmin)



admin.site.site_header = _('Organilab Administration site')
3 changes: 3 additions & 0 deletions src/laboratory/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,6 @@ class InstrumentalFamilyDataTableSerializer(serializers.Serializer):
draw = serializers.IntegerField(required=True)
recordsFiltered = serializers.IntegerField(required=True)
recordsTotal = serializers.IntegerField(required=True)

class PrecursorSerializer(serializers.Serializer):
pk = serializers.IntegerField()
12 changes: 6 additions & 6 deletions src/laboratory/api/shelfobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def create_reactive(self, serializer, limits_serializer):
laboratory_id)

log_object_change(created_by, laboratory_id, shelfobject, 0,
shelfobject.quantity, '', 0, "Create", create=True)
shelfobject.quantity, '', ADDITION, "Create", create=True)
utils.organilab_logentry(created_by, shelfobject, ADDITION,
changed_data=['object', 'shelf', 'status', 'quantity',
'measurement_unit', 'limit_quantity',
Expand Down Expand Up @@ -214,7 +214,7 @@ def create_refuse_reactive(self, serializer, limits_serializer, equiment_seriali

log_object_change(created_by, laboratory_id, shelfobject, 0,
shelfobject.quantity, '',
0, "Create", create=True, organization=organization_id)
ADDITION, "Create", create=True, organization=organization_id)
utils.organilab_logentry(created_by, shelfobject, ADDITION,
changed_data=['object', 'shelf', 'status', 'quantity',
'measurement_unit', 'marked_as_discard',
Expand Down Expand Up @@ -251,7 +251,7 @@ def create_material(self, serializer, limits_serializer):

log_object_change(created_by, laboratory_id, shelfobject, 0,
shelfobject.quantity, '',
0, "Create", create=True, organization=organization_id)
ADDITION, "Create", create=True, organization=organization_id)
utils.organilab_logentry(created_by, shelfobject, ADDITION,
changed_data=['object', 'shelf', 'status', 'quantity',
'limit_quantity', 'measurement_unit',
Expand Down Expand Up @@ -288,7 +288,7 @@ def create_refuse_material(self, serializer, limits_serializer):

log_object_change(created_by, laboratory_id, shelfobject, 0,
shelfobject.quantity, '',
0, "Create", create=True, organization=organization_id)
ADDITION, "Create", create=True, organization=organization_id)
utils.organilab_logentry(created_by, shelfobject, ADDITION,
changed_data=['object', 'shelf', 'status', 'quantity',
'limit_quantity', 'measurement_unit',
Expand Down Expand Up @@ -335,7 +335,7 @@ def create_equipment(self, serializer, limits_serializer,equipment_serializer =

log_object_change(created_by, laboratory_id, shelfobject, 0,
shelfobject.quantity, '',
0, "Create", create=True, organization=organization_id)
ADDITION, "Create", create=True, organization=organization_id)
utils.organilab_logentry(created_by, shelfobject, ADDITION,
changed_data=['object', 'shelf', 'status', 'quantity',
'limit_quantity', 'measurement_unit',
Expand Down Expand Up @@ -379,7 +379,7 @@ def create_refuse_equipment(self, serializer, limits_serializer, equipment_seria

log_object_change(created_by, laboratory_id, shelfobject, 0,
shelfobject.quantity, '',
0, "Create", create=True, organization=organization_id)
ADDITION, "Create", create=True, organization=organization_id)
utils.organilab_logentry(created_by, shelfobject, ADDITION,
changed_data=['object', 'shelf', 'status', 'quantity',
'limit_quantity', 'measurement_unit',
Expand Down
2 changes: 2 additions & 0 deletions src/laboratory/logsustances.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.conf import settings
from django.contrib.admin.models import CHANGE, ADDITION
from django.shortcuts import get_object_or_404

from laboratory.models import ObjectLogChange, OrganizationStructure
Expand Down Expand Up @@ -36,6 +37,7 @@ def log_object_add_change(user, laboratory, shelfobject, old, new, msg, provider
precursor=shelfobject.object.is_precursor,
measurement_unit=shelfobject.measurement_unit,
subject=msg,
type_action=ADDITION,
provider=provider,
bill=bill,
note='',
Expand Down
52 changes: 52 additions & 0 deletions src/laboratory/management/commands/precursor_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from dateutil.relativedelta import relativedelta
from django.contrib.admin.models import CHANGE, ADDITION
from django.core.management.base import BaseCommand
from django.db.models import Count, Min
from django.utils.timezone import now

from laboratory.models import ObjectLogChange, PrecursorReport, \
Laboratory
from laboratory.task_utils import save_object_report_precursor, \
build_precursor_report_from_reports
from laboratory.tasks import add_consecutive


class Command(BaseCommand):

help = 'Create the report or precursors'

def get_change_log(self):
PrecursorReport.objects.all().delete()

actual_date = now()
ObjectLogChange.objects.filter(subject="Update", diff_value__lte=0).update(type_action=CHANGE)
ObjectLogChange.objects.filter(subject="Update", diff_value__gte=0).update(type_action=ADDITION)
labs = Laboratory.objects.annotate(changelog_count=Count('objectlogchange'),
update_time_min=Min('objectlogchange__update_time'),
).filter(
changelog_count__gt=0)

for lab in labs:
current_time=lab.update_time_min
previos_report=None
while current_time<actual_date:
current_time=current_time+relativedelta(months=+1)
print("Running on %s for %d of %d" % (
str(lab),
current_time.year,
current_time.month
))
report = PrecursorReport.objects.create(
month=current_time.month,
year=current_time.year,
laboratory=lab,
consecutive=add_consecutive(lab)
)
save_object_report_precursor(report)
build_precursor_report_from_reports(report, previos_report)
previos_report=report



def handle(self, *args, **options):
self.get_change_log()
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 4.1.13 on 2024-04-15 17:55

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


class Migration(migrations.Migration):

dependencies = [
('laboratory', '0138_alter_shelfobjectequipmentcharacteristics_available_to_use_and_more'),
]

operations = [
migrations.CreateModel(
name='PrecursorReportValues',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('quantity', models.FloatField(default=0.0)),
('previous_balance', models.CharField(blank=True, max_length=200)),
('new_income', models.FloatField(default=0.0)),
('bills', models.CharField(blank=True, max_length=200)),
('providers', models.CharField(blank=True, max_length=200)),
('stock', models.FloatField(default=0.0)),
('month_expense', models.FloatField(default=0.0)),
('final_balance', models.FloatField(default=0.0)),
('reason_to_spend', models.TextField(blank=True)),
('measurement_unit', laboratory.catalog.GTForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='laboratory.catalog', verbose_name='measurement unit')),
('object', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='laboratory.object')),
('precursor_report', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='laboratory.precursorreport')),
],
),
migrations.AddField(
model_name='precursorreport',
name='report_values',
field=models.ManyToManyField(through='laboratory.PrecursorReportValues', to='laboratory.object'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.1.13 on 2024-04-22 17:32

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('laboratory', '0139_precursorreportvalues_precursorreport_report_values'),
]

operations = [
migrations.AlterField(
model_name='precursorreportvalues',
name='previous_balance',
field=models.FloatField(default=0.0),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Generated by Django 4.1.13 on 2024-05-07 23:00

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


class Migration(migrations.Migration):

dependencies = [
('laboratory', '0140_alter_precursorreportvalues_previous_balance'),
]

operations = [
migrations.AlterField(
model_name='precursorreportvalues',
name='bills',
field=models.CharField(blank=True, max_length=200, verbose_name='Bills'),
),
migrations.AlterField(
model_name='precursorreportvalues',
name='final_balance',
field=models.FloatField(default=0.0, verbose_name='Final balance'),
),
migrations.AlterField(
model_name='precursorreportvalues',
name='measurement_unit',
field=laboratory.catalog.GTForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='laboratory.catalog', verbose_name='Measurement unit'),
),
migrations.AlterField(
model_name='precursorreportvalues',
name='month_expense',
field=models.FloatField(default=0.0, verbose_name='Month expense'),
),
migrations.AlterField(
model_name='precursorreportvalues',
name='new_income',
field=models.FloatField(default=0.0, verbose_name='New income'),
),
migrations.AlterField(
model_name='precursorreportvalues',
name='object',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='laboratory.object', verbose_name='Object'),
),
migrations.AlterField(
model_name='precursorreportvalues',
name='precursor_report',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='laboratory.precursorreport', verbose_name='Report'),
),
migrations.AlterField(
model_name='precursorreportvalues',
name='previous_balance',
field=models.FloatField(default=0.0, verbose_name='Previous balance'),
),
migrations.AlterField(
model_name='precursorreportvalues',
name='providers',
field=models.CharField(blank=True, max_length=200, verbose_name='Providers'),
),
migrations.AlterField(
model_name='precursorreportvalues',
name='quantity',
field=models.FloatField(default=0.0, verbose_name='Quantity'),
),
migrations.AlterField(
model_name='precursorreportvalues',
name='reason_to_spend',
field=models.TextField(blank=True, verbose_name='Reason to spend'),
),
migrations.AlterField(
model_name='precursorreportvalues',
name='stock',
field=models.FloatField(default=0.0, verbose_name='Stock'),
),
]
18 changes: 18 additions & 0 deletions src/laboratory/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,13 +939,31 @@ def get_object_detail(self):
(12, _('December')),
)

class PrecursorReportValues(models.Model):
precursor_report= models.ForeignKey("PrecursorReport", on_delete= models.CASCADE, verbose_name=_("Report"))
object = models.ForeignKey(Object, on_delete=models.CASCADE, verbose_name=_("Object"))
measurement_unit = catalog.GTForeignKey(Catalog,
on_delete=models.DO_NOTHING,
verbose_name=_('Measurement unit'),
key_name="key", key_value='units')
quantity = models.FloatField(default=0.0, verbose_name=_("Quantity"))
previous_balance = models.FloatField(default=0.0, verbose_name=_("Previous balance"))
new_income = models.FloatField(default=0.0, verbose_name=_("New income"))
bills = models.CharField(max_length=200, blank=True, verbose_name=_("Bills"))
providers = models.CharField(max_length=200, blank=True, verbose_name=_("Providers"))
stock = models.FloatField(default=0.0, verbose_name=_("Stock"))
month_expense = models.FloatField(default=0.0, verbose_name=_("Month expense"))
final_balance = models.FloatField(default=0.0, verbose_name=_("Final balance"))
reason_to_spend = models.TextField(blank=True, verbose_name=_("Reason to spend"))

class PrecursorReport(models.Model):
month = models.IntegerField(choices=MONTHS)
year = models.IntegerField()
laboratory = models.ForeignKey(Laboratory, on_delete=models.CASCADE,
verbose_name=_('Laboratory'))
consecutive = models.IntegerField(default=1)
report_values = models.ManyToManyField(Object,through=PrecursorReportValues)



STATUS_CHOICES = (
Expand Down
Loading

0 comments on commit 552c2ac

Please sign in to comment.