October 22, 2024
Chicago 12, Melborne City, USA
HTML

Why Isn't My Complete Case Working Anymore? It Used to Work


I’m stuck with an issue in my Laravel application. My form submission process used to work perfectly, but now it’s giving me issues

here is my controller for decline and approved cases

<?php

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use App\Models\declinecases;
use App\Models\pendingcases;
use App\Models\fullfillmentcases;
use App\Models\received;
use App\Models\RequestOrderModel;
use Illuminate\Support\Facades\Session;

class CaseController extends Controller
{
    public function fulfillment(Request $request) {

     

      \Log::info('Fulfillment route hit with data:', $request->all());
 
      $receivedQuantities = $request->input('receivedQuantities');
      
      
      foreach ($receivedQuantities as $receivedQuantity) {
        $family_id = $receivedQuantity['familyId'];

        $requestOrderId = $receivedQuantity['requestOrderId'];

        $requestedItem = $receivedQuantity['requestedItem'];

        $receivedQty = (int) ($receivedQuantity['receivedQuantity']);
    
      $received = new received();
        $received->family_id = $family_id;
        $received->requestorder_id = $requestOrderId;
        $received->reev = $receivedQty;
        $received->requestedItem = $requestedItem;
        $received->save();
    }



    // if the case will be approved then the the first step is to delete the case from pending case and transfer it to fullfill case table
  
   

    $caseID = $request ->input('caseID');

   
    

    // Retrieve the case from the pending cases table 
    $pendingCase = pendingcases::find($caseID);
   
    if($pendingCase) {
      $fulfillmentCase = new fullfillmentcases();

      // transfer data from pending cases to fulfillment table

      $fulfillmentCase -> family_id = $pendingCase ->family_id;
      $fulfillmentCase -> firstname = $pendingCase ->firstname;
      $fulfillmentCase -> lastname  = $pendingCase ->lastname;
      $fulfillmentCase -> phone     = $pendingCase ->phone;
      $fulfillmentCase -> address   = $pendingCase ->address;
      $fulfillmentCase -> receivedDate = $pendingCase -> receivedDate;
      $fulfillmentCase -> MoveInDate = $pendingCase -> MoveInDate;
      $fulfillmentCase -> daterequested = $pendingCase ->daterequested;

      $fulfillmentCase->approval_date = now(); // the date the case was approved 

       // Delete the case from the pending cases table
       $pendingCase->delete();

    
      $fulfillmentCase->save();
     
      return response() -> json([
        'success' => true,
        'message' => 'Case Approved'
    ]);

  } else {
    return response() -> json([
      'success' => false,
      'message' => 'An error occurred'
  ]);
  }

  }





    public function decline(Request $req) {

     $reason = $req->input('reason'); // reason why case is being declined 
     $receivedQuantities = $req->input('receivedQuantities');


     foreach ($receivedQuantities as $receivedQuantity) {
      $family_id = $receivedQuantity['familyId'];
     
      $requestOrderId = $receivedQuantity['requestOrderId'];
     
      $requestedItem = $receivedQuantity['requestedItem'];
      // ask Mrs susan if she wants to have an automatic 0 since is decline or ask her if she wants to put it manually
      $receivedQty = (int) ($receivedQuantity['receivedQuantity']);
     
      $received = new received();
      $received->family_id = $family_id;
      $received->requestorder_id = $requestOrderId;
      $received->reev = 0; // $receivedQty we put a 0 because we want it to automatically add 0 to any because it is a disqualified case 
      $received->requestedItem = $requestedItem;
      $received->save();
     }

    

    $caseID = $req->input('caseID');



    // Retrieve the case from the pending cases table 
     $pendingCase = pendingcases::find($caseID);

     if($pendingCase) {

      $decline = new declinecases();

      // transfer data from pending case to decline case

      $decline -> family_id = $pendingCase ->family_id;
      $decline -> firstname = $pendingCase ->firstname;
      $decline -> lastname  = $pendingCase ->lastname;
      $decline -> phone     = $pendingCase ->phone;
      $decline ->address    = $pendingCase ->address;
      $decline -> daterequested = $pendingCase ->daterequested;

      $decline -> datedeclined = now(); // the date the case was declined 
      $decline -> reason =  $reason;  // reason for decline 

      // delete case from the pending case table

      $pendingCase ->delete();

      //save the declined case in the declined case table

      $decline ->save();

      return response() -> json([

        'success' => true,
        'message' => 'Case Sucessfully Declined'
      ]);
     } else {
        return response() -> json([
          'sucess' => false,
          'message' => 'An error occured'
        ]);
     }
  }
  
    
    
}

HTML and submit button

   </tr>
        {{-- Loop through remaining categories --}}
        @foreach ($requestorders as $key => $ro)
            @if ($key > 0)
                <tr>
                    <td>{{ $ro->category }} @if($ro->substitute) <br> --SUBSTITUTED @endif</td>
                    <!-- <td>{{ $ro->categoryTotal }}</td> -->
                    <td>{{ $ro->requestedItem }}  @if ($ro->substitute_product) <br> (substituted: {{ $ro->substitute_product }}) @endif </td>
                    <td>{{ $ro->amountNeeded }}   @if (isset($ro->substitute_quantity)) <br> (substituted: {{ $ro->substitute_quantity }}) @endif</td>
                    <td><input type="number" name="reev[]" id="reev" data-request-order-id="{{ $ro->id }}" min="0" class="form-control"></td>
                    <td><button type="button" class="btn btn-primary" data-toggle="modal" data-target=".substitute-modal" data-category="{{ $ro ->category }}" data-product="{{ $ro->requestedItem }}" data-request-order-id="{{ $ro->id }}">Substitute</button></td> <!-- this doesnt work  -->
                </tr>
            @endif
        @endforeach
    </tbody>
                                    </table>
                                </div>

                            </div>
                            <button id="submitButton" class="btn btn-primary" data-case-id="{{$pendingcases->id}}"  type="submit" onclick="submitForm()" >Complete Order</button>

                        </div>
                     </div>

JS


<script>

    
function showNotificationModal(type, message) {
  const modal = $('#notificationModal');
  const notificationMessage = modal.find('.modal-body');

  // Set the message and color based on the notification type
  //notificationMessage.text(message);
  if (type === 'success') {
    notificationMessage.html('<div class="alert alert-success">' + message + '</div>');
  } else {
    notificationMessage.html('<div class="alert alert-danger">' + message + '</div>');
  } 

  // Show the modal
  modal.modal('show');
}




function submitForm() {
  $(document).ready(function() {
    $('#submitButton').click(function(e) {
      e.preventDefault();

      // Get the form values
      const decision = $('#decisionDropdown').val();
      const reason = $('#val-skill option:selected').text();
      const caseId = $(this).data('case-id');

      // Prepare the data to be sent
      const formData = {
        decision: decision,
        reason: reason,
        caseID: caseId,
        receivedQuantities: $('input[name="reev[]"]').map(function() {
          const familyId = document.getElementById('familyId').textContent;
          const requestOrderId = $(this).data('request-order-id');
          const receivedQuantity = $(this).val();
          const requestedItem = $(this).closest('tr').find('td:eq(3)').text().trim();
          return {
            familyId: familyId,
            requestOrderId: requestOrderId,
            receivedQuantity: receivedQuantity,
            requestedItem: requestedItem
          };
        }).get()
      };

      const csrfToken = $('meta[name="csrf-token"]').attr('content');
      const headers = {
        'X-CSRF-TOKEN': csrfToken
      };

      if (decision === 'APPROVE') {
        // Perform the AJAX request for fulfillment
        $.ajax({
          url: '/fulfillment',
          type: 'POST',
          data: formData,
          dataType: 'json',
          headers: headers,

          success: function(response) {
            console.log('Form submission successful:', response);
            if (response.success) {
              showNotificationModal('success', 'Case approved');
              // Additional logic for fulfilled cases
              window.location.href="/dashboard";
            } else {
              showNotificationModal('error', response.message);
            }
          },
          error: function(xhr,status, error) {
            console.error('Form submission failed RIGHT ERE:', xhr.responseText, status, error);
            showNotificationModal('error', 'An error occurred');
          }
        });
      } else if (decision === 'DECLINE') {
        // Perform the AJAX request for decline
        $.ajax({
          url: '/decline',
          type: 'POST',
          data: formData,
          dataType: 'json',
          headers: headers,

          success: function(response) {
            console.log('Decline request successful:', response);
            // Handle success response for decline request here
            window.location.href="/dashboard";
          },
          error: function(error) {
            console.error('Decline request failed:', error);
            // Handle error response for decline request here
          }
        });
      } else {
        // Handle cases where the decision is neither 'Approved' nor 'declined'
        console.error('Invalid decision:', decision);
        showNotificationModal('error', 'Invalid decision');
      }
    });
  });
 }




    // the following function toggles the decision and reason dropdown
    // if the user select decline then reason drop down will appear
    //if the user select approve the the reason dropdown will disappear
     function toggleReasonDropdown() {
      var decisionDropdown = document.getElementById("decisionDropdown");
      var reasonDropdown = document.getElementById("reasonDropdown");
    

      // Check if "DECLINE" is selected
      if (decisionDropdown.value === "DECLINE") {
        // Show the reason dropdown
        reasonDropdown.style.display = "block";

     
      } else {
        // Hide the reason dropdown
        reasonDropdown.style.display = "none";
     
      }
    }

</script>

case options

                                                     <div class="form-group row">
  <div class="col-lg-8">
    <div class="d-flex align-items-center">
      <h4 class="text-primary mr-2">DECISION:</h4>
      <select class="form-control" id="decisionDropdown" name="decisionDropdown" onchange="toggleReasonDropdown()" >
        <option value="OPTION">Please select</option>
        <option value="APPROVE">APPROVE</option>
        <option value="DECLINE">DECLINE</option>
      </select>
    </div>
  </div>
</div>

when I want to approved it just show as if a modal wants to appear but nothing and decline does nothing it used to work. could it be I used to many submit button

the above code used to work . I expect this code to send a case to decline table or fullfillment table based on the decision

I dont know if I may have to other submit button but they are unique



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video