diff --git a/api/laboratory/views.py b/api/laboratory/views.py index 2e95ef6458..f7ef1bf04b 100644 --- a/api/laboratory/views.py +++ b/api/laboratory/views.py @@ -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, diff --git a/directions/models.py b/directions/models.py index 7b12ec74fa..a62e0be3d8 100644 --- a/directions/models.py +++ b/directions/models.py @@ -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 @@ -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() @@ -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: diff --git a/ftp_orders/main.py b/ftp_orders/main.py index 897aa5fbda..c7a6a52cb9 100644 --- a/ftp_orders/main.py +++ b/ftp_orders/main.py @@ -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 @@ -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 @@ -345,7 +343,6 @@ def push_order(self, direction: Napravleniya): { "org": self.hospital.safe_short_title, "content": content, - "orderNumber": order_number, }, ) @@ -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]}") diff --git a/ftp_orders/sql_func.py b/ftp_orders/sql_func.py new file mode 100644 index 0000000000..b9b2b030ca --- /dev/null +++ b/ftp_orders/sql_func.py @@ -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 \ No newline at end of file diff --git a/laboratory/settings.py b/laboratory/settings.py index 991fd670bf..37aadb7fe6 100644 --- a/laboratory/settings.py +++ b/laboratory/settings.py @@ -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 diff --git a/statistic/report/custom_research.py b/statistic/report/custom_research.py index 0f8e4a3045..a814a6da55 100644 --- a/statistic/report/custom_research.py +++ b/statistic/report/custom_research.py @@ -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]