update HatchingsViewSet
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
314
app/views.py
314
app/views.py
@@ -796,180 +796,180 @@ class HatchingsViewSet(viewsets.ModelViewSet):
|
|||||||
'ایند<EFBFBD><EFBFBD>ن ریور': 'ایندین ریور',
|
'ایند<EFBFBD><EFBFBD>ن ریور': 'ایندین ریور',
|
||||||
'ک<EFBFBD><EFBFBD>ب': 'کاب'
|
'ک<EFBFBD><EFBFBD>ب': 'کاب'
|
||||||
}
|
}
|
||||||
if int(request.data['GoodSum']) > 0:
|
# if int(request.data['GoodSum']) > 0:
|
||||||
poultry_data = {}
|
poultry_data = {}
|
||||||
|
|
||||||
same_keys = [
|
same_keys = [
|
||||||
'UnitName', 'SystemCode', 'EpidemiologicCode', 'LocationNameProvince',
|
'UnitName', 'SystemCode', 'EpidemiologicCode', 'LocationNameProvince',
|
||||||
'LocationNameCity', 'RegDate', 'RegDateShamsi', 'RegDateShamsiWithTime',
|
'LocationNameCity', 'RegDate', 'RegDateShamsi', 'RegDateShamsiWithTime',
|
||||||
'RegDateShamsiOnlyTime', 'StringId', 'ModalCss', 'GridContainerParametersModel',
|
'RegDateShamsiOnlyTime', 'StringId', 'ModalCss', 'GridContainerParametersModel',
|
||||||
'MenuUserAccess', 'LogTableName', 'LogTableAlias', 'PageTitle',
|
'MenuUserAccess', 'LogTableName', 'LogTableAlias', 'PageTitle',
|
||||||
'PartIdCode', 'UnitTypeName'
|
'PartIdCode', 'UnitTypeName'
|
||||||
]
|
]
|
||||||
|
|
||||||
for key in same_keys:
|
for key in same_keys:
|
||||||
if key in request.data and request.data[key] is not None:
|
if key in request.data and request.data[key] is not None:
|
||||||
poultry_data[key] = request.data[key]
|
poultry_data[key] = request.data[key]
|
||||||
|
|
||||||
bool_keys = ['IsPersisted', 'AllowInsert', 'AllowUpdate', 'UnitIsActive']
|
bool_keys = ['IsPersisted', 'AllowInsert', 'AllowUpdate', 'UnitIsActive']
|
||||||
for key in bool_keys:
|
for key in bool_keys:
|
||||||
if key in request.data:
|
if key in request.data:
|
||||||
poultry_data[key] = bool(request.data[key])
|
poultry_data[key] = bool(request.data[key])
|
||||||
|
|
||||||
if 'MenuUserAccessId' in request.data:
|
if 'MenuUserAccessId' in request.data:
|
||||||
try:
|
try:
|
||||||
poultry_data['MenuUserAccessId'] = int(request.data['MenuUserAccessId'])
|
poultry_data['MenuUserAccessId'] = int(request.data['MenuUserAccessId'])
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
poultry_data['MenuUserAccessId'] = -1
|
poultry_data['MenuUserAccessId'] = -1
|
||||||
|
|
||||||
if 'UnitPostalCode' in request.data:
|
if 'UnitPostalCode' in request.data:
|
||||||
poultry_data['PostalCode'] = request.data['UnitPostalCode']
|
poultry_data['PostalCode'] = request.data['UnitPostalCode']
|
||||||
|
|
||||||
if 'ProvinceName' in request.data:
|
if 'ProvinceName' in request.data:
|
||||||
poultry_data['Province'] = request.data['ProvinceName']
|
poultry_data['Province'] = request.data['ProvinceName']
|
||||||
|
|
||||||
if 'CityName' in request.data:
|
if 'CityName' in request.data:
|
||||||
poultry_data['City'] = request.data['CityName']
|
poultry_data['City'] = request.data['CityName']
|
||||||
|
|
||||||
if 'PersonFullName' in request.data and 'UserName' not in poultry_data:
|
if 'PersonFullName' in request.data and 'UserName' not in poultry_data:
|
||||||
poultry_data['UserName'] = request.data['PersonFullName']
|
poultry_data['UserName'] = request.data['PersonFullName']
|
||||||
|
|
||||||
system_code = request.data.get('SystemCode')
|
|
||||||
if system_code:
|
|
||||||
poultry = Poultry.objects.filter(SystemCode=system_code).first()
|
|
||||||
|
|
||||||
if not poultry:
|
|
||||||
try:
|
|
||||||
poultry = Poultry.objects.create(**poultry_data)
|
|
||||||
poultry.save()
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error creating poultry: {e}")
|
|
||||||
else:
|
|
||||||
update_fields = []
|
|
||||||
for key, value in poultry_data.items():
|
|
||||||
setattr(poultry, key, value)
|
|
||||||
poultry.save()
|
|
||||||
|
|
||||||
# if update_fields:
|
|
||||||
# try:
|
|
||||||
# poultry.save(update_fields=update_fields)
|
|
||||||
# except :
|
|
||||||
# pass
|
|
||||||
|
|
||||||
|
system_code = request.data.get('SystemCode')
|
||||||
|
if system_code:
|
||||||
poultry = Poultry.objects.filter(SystemCode=system_code).first()
|
poultry = Poultry.objects.filter(SystemCode=system_code).first()
|
||||||
bars = request.data.get('Transports', [])
|
|
||||||
if 'Transports' in request.data:
|
|
||||||
request.data.pop('Transports')
|
|
||||||
evacuation_details = request.data.get('EvacuationDetail', [])
|
|
||||||
if 'EvacuationDetail' in request.data:
|
|
||||||
request.data.pop('EvacuationDetail')
|
|
||||||
|
|
||||||
hatching = Hatching.objects.filter(RequestCode=request.data['RequestCode']).first()
|
|
||||||
print({'1': request.data['ProvinceName']})
|
|
||||||
if hatching:
|
|
||||||
for key, value in request.data.items():
|
|
||||||
setattr(hatching, key, value)
|
|
||||||
if hatching.poultry is None:
|
|
||||||
hatching.poultry=poultry
|
|
||||||
hatching.save()
|
|
||||||
|
|
||||||
|
if not poultry:
|
||||||
|
try:
|
||||||
|
poultry = Poultry.objects.create(**poultry_data)
|
||||||
|
poultry.save()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error creating poultry: {e}")
|
||||||
else:
|
else:
|
||||||
|
update_fields = []
|
||||||
|
for key, value in poultry_data.items():
|
||||||
|
setattr(poultry, key, value)
|
||||||
|
poultry.save()
|
||||||
|
|
||||||
hatchings = Hatching.objects.filter(poultry=poultry).order_by('id').last()
|
# if update_fields:
|
||||||
period = hatchings.Period + 1 if hatchings else 1
|
# try:
|
||||||
request.data['Period'] = period
|
# poultry.save(update_fields=update_fields)
|
||||||
hatching = Hatching.objects.create(**request.data)
|
# except :
|
||||||
hatching.ArchiveDate = hatching.Date + datetime.timedelta(days=76)
|
# pass
|
||||||
|
|
||||||
if poultry:
|
poultry = Poultry.objects.filter(SystemCode=system_code).first()
|
||||||
hatching.poultry = poultry
|
bars = request.data.get('Transports', [])
|
||||||
hatching.save()
|
if 'Transports' in request.data:
|
||||||
allowed_evacuation_fields = {
|
request.data.pop('Transports')
|
||||||
'PartIdCode', 'RequestId', 'MoReportId', 'ReportType', 'ReportTypeString',
|
evacuation_details = request.data.get('EvacuationDetail', [])
|
||||||
'ReportDate', 'ReportDateShamsi', 'MoReason', 'MoDate', 'MoDateShamsi',
|
if 'EvacuationDetail' in request.data:
|
||||||
'MoStartDay', 'MoEndDay', 'MoReportSubId', 'ReportStatus', 'GoodCount',
|
request.data.pop('EvacuationDetail')
|
||||||
'Message', 'ErrorCode', 'IsDeleted', 'RegDate', 'RegDateShamsi',
|
|
||||||
'RegDateShamsiWithTime', 'RegDateShamsiOnlyTime', 'ExternalId', 'StringId',
|
|
||||||
'IsPersisted', 'AllowInsert', 'AllowUpdate', 'ModalCss',
|
|
||||||
'GridContainerParametersModel', 'MenuUserAccess', 'MenuUserAccessId',
|
|
||||||
'LogTableName', 'LogTableAlias', 'PageTitle'
|
|
||||||
}
|
|
||||||
|
|
||||||
if evacuation_details:
|
hatching = Hatching.objects.filter(RequestCode=request.data['RequestCode']).first()
|
||||||
cleaned_payload = []
|
print({'1': request.data['ProvinceName']})
|
||||||
external_ids = []
|
if hatching:
|
||||||
for evacuation_data in evacuation_details:
|
for key, value in request.data.items():
|
||||||
clean_data = {
|
setattr(hatching, key, value)
|
||||||
k: v for k, v in evacuation_data.items()
|
if hatching.poultry is None:
|
||||||
if k in allowed_evacuation_fields or k == 'Id'
|
hatching.poultry=poultry
|
||||||
}
|
|
||||||
external_id = clean_data.pop('Id', None)
|
|
||||||
if external_id is not None:
|
|
||||||
clean_data['ExternalId'] = external_id
|
|
||||||
external_ids.append(external_id)
|
|
||||||
cleaned_payload.append((external_id, clean_data))
|
|
||||||
|
|
||||||
existing_map = {}
|
|
||||||
if external_ids:
|
|
||||||
existing_qs = EvacuationDetail.objects.filter(
|
|
||||||
ExternalId__in=external_ids,
|
|
||||||
trash=False
|
|
||||||
)
|
|
||||||
existing_map = {ev.ExternalId: ev for ev in existing_qs}
|
|
||||||
|
|
||||||
bulk_create = []
|
|
||||||
for external_id, clean_data in cleaned_payload:
|
|
||||||
if external_id:
|
|
||||||
evacuation = existing_map.get(external_id)
|
|
||||||
if evacuation and 'GoodCount' in clean_data:
|
|
||||||
evacuation.GoodCount = clean_data['GoodCount']
|
|
||||||
evacuation.hatching = hatching
|
|
||||||
evacuation.save(update_fields=['GoodCount', 'hatching'])
|
|
||||||
continue
|
|
||||||
clean_data['hatching'] = hatching
|
|
||||||
bulk_create.append(EvacuationDetail(**clean_data))
|
|
||||||
|
|
||||||
if bulk_create:
|
|
||||||
EvacuationDetail.objects.bulk_create(bulk_create)
|
|
||||||
|
|
||||||
if bars:
|
|
||||||
for transport_data in bars:
|
|
||||||
transport = TransportingDetail.objects.filter(
|
|
||||||
TrackingCode=transport_data['TrackingCode']).first()
|
|
||||||
if transport:
|
|
||||||
for key, value in transport_data.items():
|
|
||||||
setattr(transport, key, value)
|
|
||||||
transport.save()
|
|
||||||
|
|
||||||
else:
|
|
||||||
transport = TransportingDetail.objects.create(**transport_data)
|
|
||||||
kill_house = KillHouse.objects.filter(PartIdCode=transport.DesPartIdCode, trash=False).first()
|
|
||||||
transport.hatching = hatching
|
|
||||||
if kill_house:
|
|
||||||
transport.Province = kill_house.Province
|
|
||||||
transport.City = kill_house.City
|
|
||||||
if hatching.poultry.LocationIdProvince != kill_house.ProvinceId:
|
|
||||||
transport.Out = True
|
|
||||||
transport.save()
|
|
||||||
transport.Age = (transport.Date.date() - transport.hatching.Date.date()).days + 1
|
|
||||||
transport.save()
|
|
||||||
bars = TransportingDetail.objects.filter(trash=False, hatching=hatching)
|
|
||||||
bars_quantity = bars.filter(TrackingStatusDescription='تایید تخلیه').aggregate(total=Sum('GoodAmount'))[
|
|
||||||
'total'] or 0
|
|
||||||
ave_age = int(bars.aggregate(avg_age=Avg('Age'))[
|
|
||||||
'avg_age'] or 0)
|
|
||||||
hatching.LeftOver = hatching.ChickCountSum - (hatching.Evacuation + bars_quantity) if (
|
|
||||||
hatching.ChickCountSum - (
|
|
||||||
hatching.Evacuation + bars_quantity)) > 0 else 0
|
|
||||||
print({'2': hatching.ProvinceName})
|
|
||||||
if hatching.PedigreeName in BREED_STANDARDIZATION:
|
|
||||||
hatching.PedigreeName = BREED_STANDARDIZATION[hatching.PedigreeName]
|
|
||||||
hatching.KillingAve = ave_age
|
|
||||||
hatching.samasat_discharge_percentage = int(
|
|
||||||
((hatching.Evacuation + bars_quantity) / hatching.ChickCountSum) * 100)
|
|
||||||
hatching.save()
|
hatching.save()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print('تعداد حمل صفر است')
|
|
||||||
|
hatchings = Hatching.objects.filter(poultry=poultry).order_by('id').last()
|
||||||
|
period = hatchings.Period + 1 if hatchings else 1
|
||||||
|
request.data['Period'] = period
|
||||||
|
hatching = Hatching.objects.create(**request.data)
|
||||||
|
hatching.ArchiveDate = hatching.Date + datetime.timedelta(days=76)
|
||||||
|
|
||||||
|
if poultry:
|
||||||
|
hatching.poultry = poultry
|
||||||
|
hatching.save()
|
||||||
|
allowed_evacuation_fields = {
|
||||||
|
'PartIdCode', 'RequestId', 'MoReportId', 'ReportType', 'ReportTypeString',
|
||||||
|
'ReportDate', 'ReportDateShamsi', 'MoReason', 'MoDate', 'MoDateShamsi',
|
||||||
|
'MoStartDay', 'MoEndDay', 'MoReportSubId', 'ReportStatus', 'GoodCount',
|
||||||
|
'Message', 'ErrorCode', 'IsDeleted', 'RegDate', 'RegDateShamsi',
|
||||||
|
'RegDateShamsiWithTime', 'RegDateShamsiOnlyTime', 'ExternalId', 'StringId',
|
||||||
|
'IsPersisted', 'AllowInsert', 'AllowUpdate', 'ModalCss',
|
||||||
|
'GridContainerParametersModel', 'MenuUserAccess', 'MenuUserAccessId',
|
||||||
|
'LogTableName', 'LogTableAlias', 'PageTitle'
|
||||||
|
}
|
||||||
|
|
||||||
|
if evacuation_details:
|
||||||
|
cleaned_payload = []
|
||||||
|
external_ids = []
|
||||||
|
for evacuation_data in evacuation_details:
|
||||||
|
clean_data = {
|
||||||
|
k: v for k, v in evacuation_data.items()
|
||||||
|
if k in allowed_evacuation_fields or k == 'Id'
|
||||||
|
}
|
||||||
|
external_id = clean_data.pop('Id', None)
|
||||||
|
if external_id is not None:
|
||||||
|
clean_data['ExternalId'] = external_id
|
||||||
|
external_ids.append(external_id)
|
||||||
|
cleaned_payload.append((external_id, clean_data))
|
||||||
|
|
||||||
|
existing_map = {}
|
||||||
|
if external_ids:
|
||||||
|
existing_qs = EvacuationDetail.objects.filter(
|
||||||
|
ExternalId__in=external_ids,
|
||||||
|
trash=False
|
||||||
|
)
|
||||||
|
existing_map = {ev.ExternalId: ev for ev in existing_qs}
|
||||||
|
|
||||||
|
bulk_create = []
|
||||||
|
for external_id, clean_data in cleaned_payload:
|
||||||
|
if external_id:
|
||||||
|
evacuation = existing_map.get(external_id)
|
||||||
|
if evacuation and 'GoodCount' in clean_data:
|
||||||
|
evacuation.GoodCount = clean_data['GoodCount']
|
||||||
|
evacuation.hatching = hatching
|
||||||
|
evacuation.save(update_fields=['GoodCount', 'hatching'])
|
||||||
|
continue
|
||||||
|
clean_data['hatching'] = hatching
|
||||||
|
bulk_create.append(EvacuationDetail(**clean_data))
|
||||||
|
|
||||||
|
if bulk_create:
|
||||||
|
EvacuationDetail.objects.bulk_create(bulk_create)
|
||||||
|
|
||||||
|
if bars:
|
||||||
|
for transport_data in bars:
|
||||||
|
transport = TransportingDetail.objects.filter(
|
||||||
|
TrackingCode=transport_data['TrackingCode']).first()
|
||||||
|
if transport:
|
||||||
|
for key, value in transport_data.items():
|
||||||
|
setattr(transport, key, value)
|
||||||
|
transport.save()
|
||||||
|
|
||||||
|
else:
|
||||||
|
transport = TransportingDetail.objects.create(**transport_data)
|
||||||
|
kill_house = KillHouse.objects.filter(PartIdCode=transport.DesPartIdCode, trash=False).first()
|
||||||
|
transport.hatching = hatching
|
||||||
|
if kill_house:
|
||||||
|
transport.Province = kill_house.Province
|
||||||
|
transport.City = kill_house.City
|
||||||
|
if hatching.poultry.LocationIdProvince != kill_house.ProvinceId:
|
||||||
|
transport.Out = True
|
||||||
|
transport.save()
|
||||||
|
transport.Age = (transport.Date.date() - transport.hatching.Date.date()).days + 1
|
||||||
|
transport.save()
|
||||||
|
bars = TransportingDetail.objects.filter(trash=False, hatching=hatching)
|
||||||
|
bars_quantity = bars.filter(TrackingStatusDescription='تایید تخلیه').aggregate(total=Sum('GoodAmount'))[
|
||||||
|
'total'] or 0
|
||||||
|
ave_age = int(bars.aggregate(avg_age=Avg('Age'))[
|
||||||
|
'avg_age'] or 0)
|
||||||
|
hatching.LeftOver = hatching.ChickCountSum - (hatching.Evacuation + bars_quantity) if (
|
||||||
|
hatching.ChickCountSum - (
|
||||||
|
hatching.Evacuation + bars_quantity)) > 0 else 0
|
||||||
|
print({'2': hatching.ProvinceName})
|
||||||
|
if hatching.PedigreeName in BREED_STANDARDIZATION:
|
||||||
|
hatching.PedigreeName = BREED_STANDARDIZATION[hatching.PedigreeName]
|
||||||
|
hatching.KillingAve = ave_age
|
||||||
|
hatching.samasat_discharge_percentage = int(
|
||||||
|
((hatching.Evacuation + bars_quantity) / hatching.ChickCountSum) * 100)
|
||||||
|
hatching.save()
|
||||||
|
# else:
|
||||||
|
# print('تعداد حمل صفر است')
|
||||||
return Response({"result": "با موفقیت ثبت شد"}, status=status.HTTP_201_CREATED)
|
return Response({"result": "با موفقیت ثبت شد"}, status=status.HTTP_201_CREATED)
|
||||||
|
|
||||||
def list(self, request, *args, **kwargs):
|
def list(self, request, *args, **kwargs):
|
||||||
|
|||||||
Reference in New Issue
Block a user