donation/app/Models/InvoiceModel.php

304 lines
12 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class InvoiceModel extends Model
{
protected $table = 'receipt';
protected $primaryKey = 'receipt_id';
// protected $allowedFields = ['invoice_id','invoice_number','doner_id','invoice_date','event_id','business_id','created_on','business_id'];
protected $allowedFields = ['receipt_id', 'receipt_number', 'doner_id','notes','amount', 'payment_mode', 'business_id', 'created_on', 'created_by', 'updated_on', 'updated_by','isactive', 'payment_ref_no', 'receipt_date','causes_id'];
public function saveInvoiceItemDetails($data){
$i = 0;
$statement = [];
foreach ($data as $row) {
$id = isset($row['invoice_child_id']) ? $row['invoice_child_id'] : '';
if($id != ''){
unset($row['invoice_child_id']); // Remove the id from the data to avoid updating it
unset($row['created_by']); // bcoz here data are Updating here.
$this->db->table('invoiceitems')->where('invoice_child_id', $id)->update($row);// Update the row with the specified id
$affectedRows = $this->db->affectedRows();
$statement[$i] = "Invoice Item - ".$id." ".$affectedRows ? " Updated":" Not Updated";
}else{
unset($row['updated_by']); // bcoz here data are inserting here.
$this->db->table('invoiceitems')->insert($row);
$insertID = $this->db->insertID();
$statement[$i] = "Invoice Item - ".$insertID." Inserted";
}
}
return $statement;
}
public function inactiveMissingInvoiceItemDetails($where,$missingValues){
$dataToUpdate = ['isactive' => 0];
$this->db->table('invoiceitems')->where($where)->whereIn('invoice_child_id',$missingValues)->update($dataToUpdate);
}
public function updateData($table, $data, $where)
{
$this->db->table($table)->update($data, $where);
$affected_rows = $this->db->affectedRows();
return $affected_rows;
}
public function getSubscriptionInvoiceDetail($where)
{
$result = $this->getJoinedData($where);
// print_r($result);die;
if(!empty($result)){
foreach ($result as $i => $item) {
$invoiceId = $item['invoice_id'];
$scheme_name = [];
$from_subscription = [];
$to_subscription = [];
$invoiceItems = $this->getInvoiceItems($invoiceId,'');
foreach ($invoiceItems as $j => $child) {
if ($child->category_name == 'Membership') {
$scheme_name[] = $child->title;
$from_subscription[] = ($j == 0 && !empty($child->from_subscription) && $child->from_subscription !== null) ? date('d/m/Y', strtotime($child->from_subscription)) : "";
$to_subscription[] = ($j == 0 && !empty($child->to_subscription) && $child->from_subscription !== null ) ? date('d/m/Y', strtotime($child->to_subscription)) : "";
}
}
$result[$i]['scheme_name'] = !empty($scheme_name) ? implode(", ", $scheme_name) : '';
$result[$i]['from_subscription'] = !empty($from_subscription) ? implode(" ", $from_subscription) : '';
$result[$i]['to_subscription'] = !empty($to_subscription) ? implode(" ", $to_subscription) : '';
$result[$i]['invoice_items_details'] = $invoiceItems;
}
}
return $result;
}
public function getJoinedData($where)
{
return $this->db->table($this->table.' as I' )
->join('customers as C', 'C.doner_id = I.doner_id', 'left')
->join('Campaign as E', 'E.Campaign_id = I.event_id', 'left')
->join('business as B', 'B.business_id = I.business_id', 'left')
->join('users as U1', 'U1.user_id = I.created_by', 'left')
->join('users as U2', 'U2.user_id = I.updated_by', 'left')
->select('I.invoice_id, (C.first_name," ",C.last_name) as customer_name , I.business_id, I.created_on, I.created_by,concat(U1.first_name," ",U1.last_name) as created_by_name, I.updated_on, I.updated_by,concat(U2.first_name," ",U2.last_name) as updated_by_name, I.isactive,C.email as customer_email,C.mobile_no as customer_mobile')
->where($where)
->orderBy('invoice_id', 'DESC')
->get()
->getResultArray();
// I.shipping_address_id, I.billing_address_id,
}
public function getDetailForApproveNotifications($where)
{
$result = $this->getJoinedData($where);
if(!empty($result)){
foreach ($result as $object) {
$invoiceId = $object['invoice_id'];
$items = $this->getInvoiceItems($invoiceId,'');
}
}else{
$items = [];
}
$data['invoice'] = $result;
$data['item'] = $items;
return $data;
}
public function getData($table, $where = null)
{
$query = $this->db->table($table);
if ($where) {
$query->where($where);
}
return $query->get()->getResult();
}
public function getInvoiceData($id)
{
// Fetch invoice data
return $this->db->table('receipt')
->where('receipt_id', $id)
->join('customers as C','C.doner_id= receipt.doner_id','left')
->join('business as B','B.business_id = receipt.business_id','left')
->select('receipt.*,concat(C.first_name," ",C.last_name) as customer_name,C.address, C.postal_code,C.city, C.state,C.mobile_no as customer_mobile, C.pan_no as cust_pan_no')
->select('B.title ,B.business_logo,B.terms,B.address,B.city,B.state,B.postal_code,B.email,B.mobile_no, B.pan_no, B.org_reg_no')
->get()
->getResult();
}
// // this function Also Used For Approve Notification.
// public function getInvoiceItems($id,$stringflag)
// {
// if ($stringflag == 'groupby') {
// $select = 'invoiceitems.*, B.*, GROUP_CONCAT(C.name SEPARATOR \',\') as name';
// $group_by = 'invoiceitems.invoice_child_id';
// } else {
// $select = 'invoiceitems.*, B.*, C.name as category_name';
// $group_by = "";
// }
// $data = $this->db->table('invoiceitems')
// ->where(['invoice_id' => $id, 'invoiceitems.isactive' => 1])
// ->join('causes as B', 'B.causes_id = invoiceitems.product', 'left')
// ->select($select)
// ->groupBy($group_by) // Fixed the variable name here
// ->get()
// ->getResult();
// //->orderBy("book_img_id", "asc") // Order by book_img_id in ascending order
// //->limit(1,0)
// // print_r($this->db->getLastQuery());die;
// // print_r($data);die("ends -- here");
// return $data;
// }
// public function getMembershipListForCustomer($category, $id)
// {
// $data = $this->db->table('subscription as S')
// ->join('customers as C', 'C.doner_id = S.doner_id', 'left')
// ->join('category as CM', 'CM.id = S.scheme_id', 'left')
// ->join('book_categories as BC', 'BC.category_id = S.scheme_id', 'left')
// ->join('causes as BK', 'BC.causes_id = BK.causes_id', 'left')
// ->select('S.doner_id, CONCAT(C.first_name, " ", C.last_name) as customer_name, S.sub_id, S.scheme_id, DATE_FORMAT(S.from_subscription, "%d/%m/%Y") AS formatted_from_subscription, DATE_FORMAT(S.to_subscription, "%d/%m/%Y") AS formatted_to_subscription, S.from_subscription, S.to_subscription, S.created_on, CM.name, BK.title, BK.causes_id')
// ->where('CM.name', strtolower($category))
// ->where('S.doner_id', $id)
// ->groupBy('S.sub_id')
// ->orderBy('S.created_on', 'ASC')
// ->get()
// ->getResultArray();
// return $data;
// }
public function getProductImgs($productId)
{
$data = $this->db->table('book_images')
->where(['causes_id' => $productId])
// ->join('causes as B', 'B.causes_id = invoiceitems.product', 'left')
// ->join('book_images as BI', 'BI.causes_id = invoiceitems.product', 'left')
->select('book_images.*')
//->orderBy("book_img_id", "asc") // Order by book_img_id in ascending order
//->limit(1,0)
->get()
->getResult();
// print_r($this->db->getLastQuery());
return $data;
}
public function getCategoryBooks()
{
return $this->db->table('causes')
->join('book_categories', 'book_categories.causes_id= causes.causes_id', 'left')
->join('category','category.id=book_categories.category_id')
->where('category.name', 'membership')
->get()
->getResult();
}
public function getNonMembershipCategoryBooks()
{
$data = $this->db->table('causes')
->select('causes.*')
->join('book_categories', 'book_categories.causes_id= causes.causes_id', 'left')
->join('category','category.id=book_categories.category_id')
->where('category.name !=', 'membership')
->groupBy('causes.causes_id')
->get()
->getResult();
return $data;
}
public function insertSubscriptionData($data)
{
return $this->db->table('subscription')->insert($data);
}
// ***********************REPORT**********************
public function get_general_invoice_data($f_date = null, $t_date = null)
{
// Fetch invoice data
$query = $this->db->table('invoice')
->select('invoice.*, customers.*, COUNT(invoiceitems.product) as item_count')
->where('invoice.isactive', 1)
->where('invoice.invoice_type', 1)
->where('invoiceitems.isactive', 1);
// Add date range filter if provided
if ($f_date !== null && $t_date !== null) {
$query->where('invoice.invoice_date >=', $f_date)
->where('invoice.invoice_date <=', $t_date);
}
$result = $query->join('customers', 'customers.doner_id = invoice.doner_id', 'left')
->join('invoiceitems', 'invoiceitems.invoice_id = invoice.invoice_id', 'left')
->join('causes', 'causes.causes_id = invoiceitems.product', 'left')
->groupBy('invoice.invoice_id') // Assuming invoice_id is the primary key of the invoice table
->get()
->getResult();
return $result;
}
public function get_mem_invoice_data($f_date = null, $t_date = null)
{
// Fetch invoice data
$query = $this->db->table('invoice')
->select('invoice.*, customers.*, COUNT(invoiceitems.product) as item_count')
->where('invoice.isactive', 1)
->where('invoice.invoice_type', 2)
->where('invoiceitems.isactive', 1);
// Add date range filter if provided
if ($f_date !== null && $t_date !== null) {
$query->where('invoice.invoice_date >=', $f_date)
->where('invoice.invoice_date <=', $t_date);
}
$result = $query->join('customers', 'customers.doner_id = invoice.doner_id', 'left')
->join('invoiceitems', 'invoiceitems.invoice_id = invoice.invoice_id', 'left')
->join('causes', 'causes.causes_id = invoiceitems.product', 'left')
->groupBy('invoice.invoice_id') // Assuming invoice_id is the primary key of the invoice table
->get()
->getResult();
return $result;
}
public function itemwise_report_data($f_date = null, $t_date = null)
{
// Fetch invoice data
$query = $this->db->table('invoice')
->select('invoice.*, causes.* , COUNT(invoiceitems.product) as item_count , sum(invoiceitems.product * invoiceitems.unit_price) as item_cost')
->where('invoice.isactive', 1)
->where('causes.isactive', 1);
// Add date range filter if provided
if ($f_date !== null && $t_date !== null) {
$query->where('invoice.invoice_date >=', $f_date)
->where('invoice.invoice_date <=', $t_date);
}
$result = $query->join('invoiceitems', 'invoiceitems.invoice_id = invoice.invoice_id', 'left')
->join('causes', 'causes.causes_id = invoiceitems.product', 'left')
->groupBy('causes.causes_id')
->get()
->getResult();
return $result;
}
}