diff --git a/app/views.py b/app/views.py index 4e29fda..e1df595 100644 --- a/app/views.py +++ b/app/views.py @@ -797,6 +797,69 @@ class HatchingsViewSet(viewsets.ModelViewSet): 'ک��ب': 'کاب' } if int(request.data['GoodSum']) > 0: + poultry_data = {} + + same_keys = [ + 'UnitName', 'SystemCode', 'EpidemiologicCode', 'LocationNameProvince', + 'LocationNameCity', 'RegDate', 'RegDateShamsi', 'RegDateShamsiWithTime', + 'RegDateShamsiOnlyTime', 'StringId', 'ModalCss', 'GridContainerParametersModel', + 'MenuUserAccess', 'LogTableName', 'LogTableAlias', 'PageTitle', + 'PartIdCode', 'UnitTypeName' + ] + + for key in same_keys: + if key in request.data and request.data[key] is not None: + poultry_data[key] = request.data[key] + + bool_keys = ['IsPersisted', 'AllowInsert', 'AllowUpdate', 'UnitIsActive'] + for key in bool_keys: + if key in request.data: + poultry_data[key] = bool(request.data[key]) + + if 'MenuUserAccessId' in request.data: + try: + poultry_data['MenuUserAccessId'] = int(request.data['MenuUserAccessId']) + except (ValueError, TypeError): + poultry_data['MenuUserAccessId'] = -1 + + if 'UnitPostalCode' in request.data: + poultry_data['PostalCode'] = request.data['UnitPostalCode'] + + if 'ProvinceName' in request.data: + poultry_data['Province'] = request.data['ProvinceName'] + + if 'CityName' in request.data: + poultry_data['City'] = request.data['CityName'] + + if 'PersonFullName' in request.data and 'UserName' not in poultry_data: + 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(): + if hasattr(poultry, key): + current_value = getattr(poultry, key) + if current_value != value: + setattr(poultry, key, value) + update_fields.append(key) + + if update_fields: + try: + poultry.save(update_fields=update_fields) + except : + pass + + poultry = Poultry.objects.filter(SystemCode=system_code).first() bars = request.data.get('Transports', []) if 'Transports' in request.data: request.data.pop('Transports') @@ -809,10 +872,12 @@ class HatchingsViewSet(viewsets.ModelViewSet): if hatching: for key, value in request.data.items(): setattr(hatching, key, value) + if hatching.poultry is None: + hatching.poultry=poultry hatching.save() else: - poultry = Poultry.objects.filter(SystemCode=request.data['SystemCode']).first() + hatchings = Hatching.objects.filter(poultry=poultry).order_by('id').last() period = hatchings.Period + 1 if hatchings else 1 request.data['Period'] = period