ria/app/Models/Ipinvoice_model.php
2024-08-16 10:55:42 +00:00

184 lines
5.8 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class Ipinvoice_model extends Model
{
/**
* This function is used to get the user listing count
* @param string $searchText : This is optional search text
* @param number $page : This is pagination offset
* @param number $segment : This is pagination limit
* @return array $result : This is result
*/
function saleInvoiceListing()
{
$builder = $this->db->table('ip_invoices ')
->select('ip_invoices.invoice_id, ip_invoices.user_id, ip_invoices.client_id,
ip_invoices.invoice_group_id, ip_invoices.invoice_status_id, ip_invoices.invoice_date_due, ip_invoices.invoice_date_created,
ip_invoices.invoice_number, ip_invoices.invoice_terms, ip_clients.client_name,
ip_payment_methods.payment_method_name, ip_users.user_name, ip_products.product_name,
ip_invoice_amounts.invoice_total')
->join('ip_payment_methods', 'ip_invoices.payment_method = ip_payment_methods.payment_method_id', 'left')
->join('ip_clients', 'ip_invoices.client_id = ip_clients.client_id', 'left')
->join('ip_users', 'ip_invoices.user_id = ip_users.user_id', 'left')
->join('ip_invoice_items', 'ip_invoices.invoice_id = ip_invoice_items.invoice_id', 'left')
->join('ip_products', 'ip_invoice_items.item_product_id = ip_products.product_id', 'left')
->join('ip_invoice_amounts', 'ip_invoices.invoice_id = ip_invoice_amounts.invoice_id', 'left')
->orderBy('ip_invoices.invoice_number', 'DESC');
$query = $builder->get();
$result = $query->getResult();
return $result;
}
/**
* This function is used to add new user to system
* @return number $insert_id : This is last inserted id
*/
function addNewUser($userInfo)
{
$this->db->transStart();
$builder = $this->db->table('tbl_users');
$builder->insert($userInfo);
$insert_id = $this->db->affectedRows();
$this->db->transComplete();
return $insert_id;
}
/**
* This function used to get user information by id
* @param number $userId : This is user id
* @return array $result : This is user information
*/
function getInvoice($invoiceId)
{
$builder = $this->db->table('ip_invoices')
// ->select('')
->where('invoice_id', $invoiceId);
$query = $builder->get();
return $query->getResult();
}
/**
* This function used to get user information by id
* @param number $userId : This is user id
* @return array $result : This is user information
*/
function getinvoiceAttachment($invoiceId)
{
$builder = $this->db->table('ip_invoice_attachment')
// ->select('')
->where('invoice_id', $invoiceId);
$query = $builder->get();
return $query->getResult();
}
// Ipinvoice_model
public function deleteAttachment($invoice_attachment_id) {
// Get the attachment details to get the file path
$builder = $this->db->table('ip_invoice_attachment');
$builder->where('invoice_attachment_id', $invoice_attachment_id);
$query = $builder->get();
$attachment = $query->getRowArray(); // Get single row as an associative array
if ($attachment) {
// Construct the file path
$fileName = $attachment['attachment_name']; // Make sure this is the correct field name
$filePath = './public/uploads/images/invoice_files/' . $fileName;
// Debug: Log the file path
log_message('error', 'Attempting to delete file: ' . $filePath);
// Check if the path is actually a file
if (file_exists($filePath) && !is_dir($filePath)) {
unlink($filePath); // Delete the file
} else {
log_message('error', 'File does not exist or is a directory: ' . $filePath);
}
// Delete the record from the database
$builder->where('invoice_attachment_id', $invoice_attachment_id);
$builder->delete();
return $this->db->affectedRows() > 0;
}
return false;
}
/**
* This function is used to update the user information
* @param array $userInfo : This is users updated information
* @param number $userId : This is user id
*/
function editUser($userInfo, $userId)
{
$this->db->table('tbl_users')
->where('EmpID', $userId)
->update($userInfo);
return TRUE;
}
/**
* This function is used to delete the user information
* @param number $userId : This is user id
* @return boolean $result : TRUE / FALSE
*/
function deleteUser($userId, $userInfo)
{
$this->db->table('tbl_users')
->where('userId', $userId)
->update($userInfo);
return $this->db->affectedRows();
}
/**
* This function is used to get the All employees
* @return array $result : This is result of the query
*/
function getAllEmployees()
{
/*->select('EmpID,FirstName,LastName');
$builder = $this->db->table('t_employee_details');
$query = $builder->get();
return $query->getResult(); */
$builder = $this->db->table('t_employee_details EMP')
->select('EMP.EmpID,EMP.FirstName,EMP.LastName,EMP.Designation,EMP.EmailId,EMP.ContactNumber,DEPT.DEPCode,DEPT.DepartmentName')
->join('t_departmentdetails DEPT', 'EMP.Departmentcode = DEPT.DEPCode')
->where('EMP.IsActive ', 1);
$query = $builder->get();
return $query->getResult();
}
}