228 lines
8.2 KiB
PHP
228 lines
8.2 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_invoices.status, ip_invoices.e_invoice_number, ip_invoices.invoice_url, ip_clients.client_name,
|
|
ip_payment_methods.payment_method_name, ip_users.user_name,
|
|
ip_products.product_name, ip_invoice_items.item_price, ip_products.cgst, ip_products.sgst,
|
|
ip_invoice_items.item_quantity, ip_invoice_items.item_price, ip_invoice_items.item_name, ip_invoice_items.item_quantity,
|
|
ip_invoice_amounts.invoice_total, ip_invoice_amounts.invoice_item_subtotal, ip_invoice_amounts.invoice_item_tax_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();
|
|
// $results = $query->getResultArray();
|
|
return $result;
|
|
// $invoices = [];
|
|
|
|
// foreach ($results as $row) {
|
|
// $invoice_id = $row['invoice_id'];
|
|
|
|
// // If the invoice_id doesn't exist in the $invoices array, initialize it
|
|
// if (!isset($invoices[$invoice_id])) {
|
|
// $invoices[$invoice_id] = [
|
|
// 'invoice_id' => $row['invoice_id'],
|
|
// 'user_id' => $row['user_id'],
|
|
// 'client_id' => $row['client_id'],
|
|
// 'invoice_group_id' => $row['invoice_group_id'],
|
|
// 'invoice_status_id' => $row['invoice_status_id'],
|
|
// 'invoice_date_due' => $row['invoice_date_due'],
|
|
// 'invoice_date_created' => $row['invoice_date_created'],
|
|
// 'invoice_number' => $row['invoice_number'],
|
|
// 'invoice_terms' => $row['invoice_terms'],
|
|
// 'status' => $row['status'],
|
|
// 'e_invoice_number' => $row['e_invoice_number'],
|
|
// 'invoice_url' => $row['invoice_url'],
|
|
// 'client_name' => $row['client_name'],
|
|
// 'payment_method_name' => $row['payment_method_name'],
|
|
// 'user_name' => $row['user_name'],
|
|
// 'invoice_total' => $row['invoice_total'],
|
|
// 'invoice_item_subtotal' => $row['invoice_item_subtotal'],
|
|
// 'invoice_item_tax_total' => $row['invoice_item_tax_total'],
|
|
// 'products' => [] // Initialize the products array
|
|
// ];
|
|
// }
|
|
|
|
// // Add the product to the products array for the current invoice
|
|
// $invoices[$invoice_id]['products'][] = [
|
|
// 'product_name' => $row['product_name'],
|
|
// 'item_quantity' => $row['item_quantity'],
|
|
// 'item_price' => $row['item_price'],
|
|
// ];
|
|
// }
|
|
|
|
// // Return the grouped invoices
|
|
// return $invoices;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* 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();
|
|
}
|
|
|
|
|
|
} |