ria/app/Models/Driver_model.php

159 lines
5.9 KiB
PHP
Executable File

<?php
namespace App\Models;
use CodeIgniter\Model;
class Driver_model extends Model
{
protected $table;
protected $primaryKey;
protected $allowedFields;
public function useDriverTable()
{
$this->table = 't_driver';
$this->primaryKey = 'driver_id';
$this->allowedFields = ['driver_id', 'driver_name', 'driver_mobile','created_on','created_by','updated_on','updated_by','isactive'];
return $this;
}
public function useDriverAttendanceTable()
{
$this->table = 't_driver_attendance';
$this->primaryKey = 'driver_attendance_id';
$this->allowedFields = ['driver_attendance_id','date','driver_id','invoice_number','vehicle_number','load_type','salary_amount','food_amount','created_on','created_by','updated_on','updated_by','isactive'];
return $this;
}
public function getDriverList()
{
if (empty($this->allowedFields)) { return []; }
$fields = implode(', ', array_map(function($field) {
return $this->table . '.' . $field;
}, $this->allowedFields));
$selectFields = "CONCAT_WS(' ', t_employee_details.FirstName, t_employee_details.LastName) AS FullName, t_employee_details.IsActive as employee_isactive, " . $fields;
return $this->select($selectFields)
->join('tbl_users', 'tbl_users.userId = ' . $this->table . '.created_by', 'left')
->join('t_employee_details', 'tbl_users.EmpID = t_employee_details.EmpID', 'left')
->findAll();
}
public function getDriverAttendanceList($driver_id,$from,$to){
if (empty($this->allowedFields)) { return []; }
$fields = implode(', ', array_map(function($field) {
return $this->table . '.' . $field;
}, $this->allowedFields));
$selectFields = "CONCAT_WS(' ', t_employee_details.FirstName, t_employee_details.LastName) AS FullName, t_employee_details.IsActive as employee_isactive, t_driver.driver_name,ip_clients.client_name as customer,ip_invoices.received_qty as quantity," . $fields;
return $this->select($selectFields)
->join('t_driver', 't_driver.driver_id = ' . $this->table . '.driver_id', 'left')
->join('tbl_users', 'tbl_users.userId = ' . $this->table . '.created_by', 'left')
->join('t_employee_details', 'tbl_users.EmpID = t_employee_details.EmpID', 'left')
->join('ip_invoices', 'ip_invoices.invoice_number = ' . $this->table . '.invoice_number', 'left')
->join('ip_clients', 'ip_clients.client_id = ip_invoices.client_id', 'left')
->where($this->table.'.date BETWEEN "'.$from.'" AND "'.$to.'"')
->where($this->table . '.driver_id', $driver_id)
->where($this->table . '.isactive', 1)
->findAll();
}
function getInvoiceInfo()
{
$builder = $this->db->table('ip_invoices')
->select('invoice_number,invoice_id');
$query = $builder->get();
return $query->getResult();
}
function getDriverName($id)
{
$builder = $this->db->table('t_driver')
->select('driver_name')
->where('driver_id', $id);
$query = $builder->get()->getRow();
$driver_name = $query ? $query->driver_name : null;
return $driver_name;
}
function gatheredCustAndQty($invoice){
$builder = $this->db->table('ip_invoices')
->select('invoice_number,received_qty as quantity,ip_clients.client_name as customer')
->join('ip_clients', 'ip_clients.client_id = ip_invoices.client_id', 'left')
->where('invoice_number', $invoice);
$query = $builder->get();
return $query->getRowArray() ?: [];
}
function driverAttendanceForMonthlyPayInputs($from,$to){
$builder = $this->db->table('t_driver_attendance')
->select('t_driver_attendance.driver_id,SUM(t_driver_attendance.salary_amount) as driver_monthly_salary_amount,SUM(t_driver_attendance.food_amount) as driver_monthly_food_amount,t_driver.driver_name,
DATE_FORMAT(date, "%M-%Y") AS month_year,SUM(salary_amount + food_amount) AS driver_earnings_amount,COUNT(t_driver_attendance.load_type) AS no_of_loads')
->join('t_driver', 't_driver.driver_id = t_driver_attendance.driver_id and t_driver.isactive = 1', 'left')
->where('t_driver_attendance.date BETWEEN "'.$from.'" AND "'.$to.'"')
->groupBy('t_driver.driver_id, DATE_FORMAT(t_driver_attendance.date, "%M-%Y")');
$query = $builder->get();
return $query->getResult();
}
function driverMonthlyPayInputs($monthYear){
$builder = $this->db->table('t_driver_monthly_pay_inputs');
$query = $builder->where('t_driver_monthly_pay_inputs.month_year', $monthYear)
->groupBy('t_driver_monthly_pay_inputs.driver_id, t_driver_monthly_pay_inputs.month_year')
->get();
$result = $query->getResult();
return $result;
}
function saveMonthlyData($data)
{
$month = $data['month_year'];
$drid = $data['driver_id'];
$count = 0;
$builder = $this->db->table('t_driver_monthly_pay_inputs')->select('count(*) as count')
->where('month_year', $month)
->where('driver_id', $drid);
$isExits = $builder->get();
$res = $isExits->getResultArray();
if (!empty($res)) {
$count = $res[0]['count'];
}
if ($count == 0) {
unset($data['updated_by']);
$builder = $this->db->table('t_driver_monthly_pay_inputs');
$builder->insert($data);
$i = $this->db->affectedRows();
return $i;
} elseif ($count == 1) {
unset($data['created_by']);
$this->db->table('t_driver_monthly_pay_inputs')
->where('driver_id', $data['driver_id'])
->where('month_year', $data['month_year'])
->update($data);
$i = $this->db->affectedRows();
return $i;
}
}
}
?>