2023-07-14

Search through the entire columns in datatable

I've tried several times but it seems like there is an issue with the code, I have a table and I've applied Datatable plugin by using Server Side processing, when I search through the entire table there is no problem except the idCustomer column, for example if I search for a name which is David, there is no result but if I search the id of David for example 10 there is no problem, It seems like the problem is with the idCustomer column from the below code since idCustomer is used as a value in order to retrieve the name of customers through another table, So I want also to be able to search through their names.

Here is my serverside processing code:

<?php
include "../views/lang/config.php";   
require_once "../controllers/customers.controller.php";
require_once "../models/customers.model.php";
 
// DB table to use
$table = 'sales';
 
// Table's primary key
$primaryKey = 'id';
 

$condition = "paymentMethod <> 'loaned'";                 
    if (isset($_GET['initialDate']) && isset($_GET['finalDate'])) { 
        $initialDate = $_GET['initialDate'];
        $finalDate = $_GET['finalDate'];


        if ($initialDate == $finalDate ) {
            $condition .= " AND (DATE(saledate) = '$initialDate' OR DATE(saledate) = DATE('$initialDate')) ";

        }else{

        $actualDate = new DateTime();                  
        $actualDate ->add(new DateInterval("P1D"));     
        $actualDatePlusOne = $actualDate->format("Y-m-d");

        $finalDate2 = new DateTime($finalDate);          
        $finalDate2 ->add(new DateInterval("P1D"));
        $finalDatePlusOne = $finalDate2->format("Y-m-d");

        if($finalDatePlusOne == $actualDatePlusOne){

            $condition .= " AND saledate BETWEEN '$initialDate' AND '$finalDatePlusOne'";

        }else{

            $condition .= " AND saledate BETWEEN '$initialDate' AND '$finalDate'";  

        }


        }



    }




// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array( 'db' => 'id', 'dt' => 0, 'formatter' => function ($d, $row) {
        $code = $row['code'];
        return '<span data-search="'.$code.'"></span>';
    }),  

    array( 'db' => 'jobOrder', 'dt' => 1,
           'formatter' => function($d, $row){
            $jobOrder = $d;
            $remainingPaymentUSD = $row['remainingPaymentUSD'];
            $remainingPaymentIQD = $row['remainingPaymentIQD'];
            $totalPriceUSD = $row['totalPriceUSD'];
            $totalPriceIQD = $row['totalPriceIQD'];

            // check if the customer paid the total money or there is some money is remaining
            if (($remainingPaymentUSD == 0 && $remainingPaymentIQD == 0) && ($totalPriceUSD > 0 || $totalPriceIQD > 0) ) {
                $jobOrderStatus = '<button class="btn btn-success">'.$jobOrder.'</button>';
            }else{
                $jobOrderStatus = '<button class="btn btn-danger" style="background-color: #dc3545;">'.$jobOrder.'</button>';
            }
            return $jobOrderStatus;
    }),

    array( 'db' => 'code', 'dt' => 2 ),
    array( 'db' => 'idCustomer', 'dt' => 3, 
            'formatter' => function ($d) use ($lang){       // use ($lang) allowing it to access the $lang array
                $itemCustomer = "id";
                $valueCustomer = $d;
                $customerAnswer = ControllerCustomers::ctrShowCustomers($itemCustomer, $valueCustomer);
                if (empty($customerAnswer)) {
                    // Set default value here
                    $customerAnswer['name']= '<button class="btn btn-danger btn-xs">'.  $lang['Customer_is_not_exist']. '</button>';        
                }else{
                    $customerAnswer['name'] = '<a href="index.php?route=customers-profile&IdCustomer='.$valueCustomer.'">'.$customerAnswer['name'].'</a>';
                }
        
        return $customerAnswer['name'];                
    } ),

    array( 'db' => 'totalPriceUSD', 'dt' => 4, 'formatter' => function($d){
            $totalPriceUSD = number_format($d, 2);
            return $totalPriceUSD;
    } ),

    array( 'db' => 'totalPriceIQD', 'dt' => 5, 'formatter' => function($d){
            $totalPriceIQD = number_format($d, 2);
            return $totalPriceIQD;
    } ),

    array( 'db' => 'cashPaymentUSD', 'dt' => 6, 'formatter' => function($d){
            $cashPaymentUSD = number_format($d, 2);
            return $cashPaymentUSD;
    } ),

    array( 'db' => 'cashPaymentIQD', 'dt' => 7, 'formatter' => function($d){
            $cashPaymentIQD = number_format($d, 2);
            return $cashPaymentIQD;
    } ),

    array( 'db' => 'remainingPaymentUSD', 'dt' => 8, 'formatter' => function($d){
            $remainingPaymentUSD = number_format($d, 2);
            return $remainingPaymentUSD;
    } ),

    array( 'db' => 'remainingPaymentIQD', 'dt' => 9, 'formatter' => function($d){
            $remainingPaymentIQD = number_format($d, 2);
            return $remainingPaymentIQD;
    } ),

    array( 'db' => 'saledate', 'dt' => 10 ),
    array( 
        'db' => '',  
        'dt' => 11,
        'formatter' => function($row) {
            $idSale = $row['id'];
            $code = $row['code'];
            $buttonHtml = "<div class='btn-group'>
            <button class='btn btn-success btnPrintBill' saleCode='".$code."'>
            <i class='fa fa-print'></i></button>
            <button class='btn btn-info btnEditSale' idSale='".$idSale."'><i class='fa fa-eye'></i></button>
            <button class='btn btn-danger btnDeleteSale' idSale='".$idSale."'><i class='fa fa-times'></i></button>
            </div>";
            return $buttonHtml;
        }        
         ),



);

 
// SQL server connection information
$sql_details = array(
    'user' => 'aaaaa',
    'pass' => '',
    'db'   => 'bbbbb',
    'host' => 'localhost'
    ,'charset' => 'utf8' // Depending on your PHP and MySQL config, you may need this
);
 
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */
 
require( 'ssp.class.php' );
 

echo json_encode(
    SSP::complex($_GET, $sql_details, $table, $primaryKey, $columns, null, $condition) 
);



$(document).ready(function() {

  tableSales = $('#manage_sales_table').DataTable({
    processing: true,
    serverSide: true,
    ajax: 'serverside/sales_server_processing.php',
    language: {
      processing: '<div><div></div><div></div><div></div><div></div></div>'
    }
  });
});

I apperciate your help.



No comments:

Post a Comment