Skip to content

Commit

Permalink
Merge pull request #2947 from moodpulse/tranferLabToDefault
Browse files Browse the repository at this point in the history
is_default_hosp need transfer order to ftp for self execute
  • Loading branch information
mikhailprivalov authored Jul 13, 2023
2 parents 7da8edc + a48a7f7 commit 6adef52
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 13 deletions.
1 change: 0 additions & 1 deletion api/laboratory/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,6 @@ def receive_history(request):
external_order_organization = first_iss.napravleniye.external_order.organization.safe_short_title
else:
external_order_organization = None

result["rows"].append(
{
"pk": row.number,
Expand Down
6 changes: 6 additions & 0 deletions directions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
EXCLUDE_DOCTOR_PROFILE_PKS_ANKETA_NEED,
RESEARCHES_EXCLUDE_AUTO_MEDICAL_EXAMINATION,
AUTO_PRINT_RESEARCH_DIRECTION,
NEED_ORDER_DIRECTION_FOR_DEFAULT_HOSPITAL,
)
from laboratory.celery import app as celeryapp
from odii.integration import add_task_request, add_task_result
Expand Down Expand Up @@ -543,6 +544,7 @@ class Napravleniya(models.Model):
external_executor_hospital = models.ForeignKey(
Hospitals, related_name='external_executor_hospital', default=None, blank=True, null=True, on_delete=models.PROTECT, help_text='Внешняя организация-исполнитель'
)
time_send_hl7 = models.DateTimeField(help_text='Дата и время отправки заказа', db_index=True, blank=True, default=None, null=True)

def sync_confirmed_fields(self):
has_confirmed_iss = Issledovaniya.objects.filter(napravleniye=self, time_confirmation__isnull=False).exists()
Expand Down Expand Up @@ -1579,6 +1581,10 @@ def gen_napravleniya_by_issledovaniya(
directions_for_researches[dir_group].need_order_redirection = True
directions_for_researches[dir_group].external_executor_hospital = research.plan_external_performing_organization
directions_for_researches[dir_group].save(update_fields=["need_order_redirection", "external_executor_hospital"])
elif not directions_for_researches[dir_group].need_order_redirection and NEED_ORDER_DIRECTION_FOR_DEFAULT_HOSPITAL:
directions_for_researches[dir_group].need_order_redirection = True
directions_for_researches[dir_group].external_executor_hospital = Hospitals.objects.filter(is_default=True).first()
directions_for_researches[dir_group].save(update_fields=["need_order_redirection", "external_executor_hospital"])

loc = ""
if str(research.pk) in localizations:
Expand Down
27 changes: 15 additions & 12 deletions ftp_orders/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
from hl7apy.parser import parse_message

from clients.models import Individual
from directions.models import Napravleniya, RegisteredOrders, NumberGenerator
from directions.models import Napravleniya, RegisteredOrders, NumberGenerator, TubesRegistration
from ftp_orders.sql_func import get_tubesregistration_id_by_iss
from hospitals.models import Hospitals
from directory.models import Researches
from laboratory.utils import current_time
from slog.models import Log
from users.models import DoctorProfile

Expand Down Expand Up @@ -312,22 +314,18 @@ def push_order(self, direction: Napravleniya):
ordd = hl7.ORM_O01_ORDER.add_group("ORM_O01_ORDER_DETAIL")

with transaction.atomic():
gen: NumberGenerator = NumberGenerator.objects.select_for_update().filter(key='externalOrderNumber', hospital=self.hospital, is_active=True).first()

if not gen and self.hospital.strict_external_numbers:
raise NoValidNumberGeneratorException(f"No number generator found for hospital {self.hospital}")

order_number = str(gen.get_next_value() if gen else direction.pk)
direction.order_redirection_number = order_number
direction.need_order_redirection = False
direction.save(update_fields=['order_redirection_number', 'need_order_redirection'])
direction.time_send_hl7 = current_time()
direction.save(update_fields=['time_send_hl7', 'need_order_redirection'])

n = 0

for iss in direction.issledovaniya_set.all():
n += 1
obr = ordd.add_segment("OBR")
obr.obr_1 = str(n)
obr.obr_3.value = order_number
tube_data = [i.tube_number for i in get_tubesregistration_id_by_iss(iss.pk)]
obr.obr_3.value = str(tube_data[0])
obr.obr_4.obr_4_4.value = iss.research.internal_code
obr.obr_4.obr_4_5.value = iss.research.title.replace(" ", "_")
obr.obr_7.value = created_at
Expand All @@ -345,7 +343,6 @@ def push_order(self, direction: Napravleniya):
{
"org": self.hospital.safe_short_title,
"content": content,
"orderNumber": order_number,
},
)

Expand Down Expand Up @@ -434,7 +431,13 @@ def process_push_orders():
directions_to_sync = []

for direction in Napravleniya.objects.filter(external_executor_hospital=ftp_connection.hospital, need_order_redirection=True)[:10]:
directions_to_sync.append(direction)
is_recieve = False
for tube in TubesRegistration.objects.filter(issledovaniya__napravleniye=direction).distinct():
is_recieve = True
if tube.time_recive is None:
is_recieve = False
if is_recieve:
directions_to_sync.append(direction)

ftp_connection.log(f"Directions to sync: {[d.pk for d in directions_to_sync]}")

Expand Down
18 changes: 18 additions & 0 deletions ftp_orders/sql_func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from utils.db import namedtuplefetchall
from django.db import connection


def get_tubesregistration_id_by_iss(iss_id):
with connection.cursor() as cursor:
cursor.execute(
"""
SELECT dt.number as tube_number
FROM directions_issledovaniya_tubes
LEFT JOIN directions_tubesregistration dt on directions_issledovaniya_tubes.tubesregistration_id = dt.id
where issledovaniya_id = %(iss_id)s
LIMIT 1
""",
params={'iss_id': iss_id},
)
rows = namedtuplefetchall(cursor)
return rows
1 change: 1 addition & 0 deletions laboratory/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ def __getitem__(self, item):
AUTO_PRINT_RESEARCH_DIRECTION = {} # {perid_month_ago: "10", "researches": [research_pk, research_pk, research_pk]}
ECP_SEARCH_PATIENT = {}
DAYS_AGO_SEARCH_RESULT = {} # {"isLab: 90", "isInstrument: 365"}
NEED_ORDER_DIRECTION_FOR_DEFAULT_HOSPITAL = False

try:
from laboratory.local_settings import * # noqa: F403,F401
Expand Down
1 change: 1 addition & 0 deletions statistic/report/custom_research.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def custom_research_fill_data(ws1, result_query, row=6):
for i in result_query["result"]:
column = 0
table_data = {}
r += 1
for k in range(len_fields):
column += 1
title = result_query["fields"][k]
Expand Down

0 comments on commit 6adef52

Please sign in to comment.