processPostWeighingDetails method

Future<void> processPostWeighingDetails()

Implementation

Future<void> processPostWeighingDetails() async {
  try {
    // Copy data from fullTicketWeighingTO if available
    if (WBAutoWeighingTO.rstNo != null &&
        WBAutoWeighingTO.commodity != null) {
      tblTRLoadingWeighingTO.rstNo = WBAutoWeighingTO.rstNo;
      tblTRLoadingWeighingTO.commodity = WBAutoWeighingTO.commodity;
      tblTRLoadingWeighingTO..loadOrUnload = WBAutoWeighingTO.loadOrUnload;
      tblTRLoadingWeighingTO.flag = WBAutoWeighingTO.flag;
      tblTRLoadingWeighingTO.material = WBAutoWeighingTO.material;
      tblTRLoadingWeighingTO.weighingDate = WBAutoWeighingTO.weighingDate;
      tblTRLoadingWeighingTO.weighingTime = WBAutoWeighingTO.weighingTime;
      tblTRLoadingWeighingTO.wbVehicleNo = WBAutoWeighingTO.wbVehicleNo;
    }

    // Copy comments if they exist
    if (wtMismatchCmt.isNotEmpty) {
      tblTRLoadingWeighingTO.wtMismatchCmt = wtMismatchCmt;
    }
    if (wtFtNotFoundCmt.isNotEmpty) {
      tblTRLoadingWeighingTO.wtFtNotFoundCmt = wtFtNotFoundCmt;
    }
    if (wtVehicleNoMismatchCmt.isNotEmpty) {
      tblTRLoadingWeighingTO.wtVehicleNoMismatchCmt = wtVehicleNoMismatchCmt;
    }
    if (manualGateOpenTO.wbid != null)
      tblTRLoadingWeighingTO.manualGateOpenTO = manualGateOpenTO;

    // API call
    // *****************************************************************************
    tblTRLoadingWeighingTO.loadingId = selectVehicleNo.value?.value;
    tblTRLoadingWeighingTO.actualWeight =
        int.tryParse(measurementController.value.text) ?? 0;
    tblTRLoadingWeighingTO.weighingMachineId = selectKata.value?.value;
    tblTRLoadingWeighingTO.machineName = selectKata.value?.text;
    tblTRLoadingWeighingTO.vehicleNo = selectVehicleNo.value?.text;
    tblTRLoadingWeighingTO.loadingTypeId = loadingTypeId.value;
    // *****************************************************************************

    final response = await _apiService.postData(AppUrls.postWeighingDetails,
        data: tblTRLoadingWeighingTO);

    if (response.statusCode == 200) {
      final finalResult = response
          .data['data']; // Assuming response.data contains the result object
      print('finalResult: $finalResult');
      if (finalResult['Result'] == 1) {
        if (tblTRLoadingWeighingTO.isWeighingCompleted == 1) {
          AppUtils.showSnackBar('Weighing Completed Successfully');

          if (loadingTypeId.value == 2) {
            tempTblTRLoadingWeighingTOList.value =
                TblTRLoadingWeighingTO.fromJsonList(finalResult['data']);
            isConfirmed.value = true;
            prepareWeighingDiffScreen();
          } else {
            resetData();
          }
        } else {
          AppUtils.showSnackBar('Weighing Added Successfully');
          tblTRLoadingWeighingTOList.value =
              TblTRLoadingWeighingTO.fromJsonList(finalResult['data']);
          isConfirmed.value = true;
          tblTRLoadingWeighingTO.actualWeight = 0;
          measurementController.value.clear();
          setWeighingData();
          manualGateOpenTO = ManualGateOpenTO();
          narrationGateController.value.clear();
          toggleGate(nextEqCode);
        }
        if (tblTRLoadingWeighingTOList.length > 1) {
          isEnableCompleteWeighingBtn.value = true;
        } else {
          isEnableCompleteWeighingBtn.value = false;
        }
      } else {
        AppUtils.showSnackBar(finalResult['Text']);
      }

      if (tblTRLoadingTO.value?.statusId ==
              LoadingStatusInternalTransferE
                  .rejectVehicleAfterTareWeight.value &&
          loadingTypeId == 1) {
        resetData();
      }
      isConfirmButtonEnabled.value = false;
      isPlayButtonEnabled.value = false;
      tblTRLoadingWeighingTO.isWeighingCompleted = 0;
      retryPressCount = 0;
    } else {
      AppUtils.showSnackBar('Server Error - Failed to Add Weighing');
    }
  } catch (error) {
    AppUtils.showSnackBar('Server Error - Failed to Add Weighing');
  } finally {
    isLoading(false);
  }
}