vb_book/app/Models/InvoiceModel.php
bitbucket c5efb0d181 ss
2023-11-30 10:34:55 +05:30

311 lines
14 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class InvoiceModel extends Model
{
protected $table = 'invoice';
protected $primaryKey = 'invoice_id';
// protected $allowedFields = ['invoice_id','invoice_number','customer_id','invoice_date','event_id','business_id','created_on','business_id'];
protected $allowedFields = ['invoice_id', 'invoice_number', 'customer_id','notes', 'invoice_date', 'due_date', 'subtotal', 'tax', 'discount', 'shipping_charge','shipping_label','total_amount', 'payment_status', 'order_number','invoice_type', 'payment_method', 'event_id', 'business_id', 'shipping_address_id', 'billing_address_id', 'created_on', 'created_by', 'updated_on', 'updated_by','category','isactive','billing_address','shipping_address','status'];
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.customer_id = I.customer_id', 'left')
->join('events as E', 'E.event_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, I.invoice_number, I.customer_id,concat(C.first_name," ",C.last_name) as customer_name , I.invoice_date, I.due_date, I.subtotal, I.tax, I.discount, I.total_amount,I.status, I.payment_status, I.order_number, I.payment_method,I.invoice_type, I.event_id,E.event_name, I.business_id,B.title as business_name, 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,I.shipping_address_id,I.shipping_address,I.billing_address_id,I.billing_address,B.address as business_address,B.city as business_city,B.state as business_state,B.postal_code as business_postal_code,B.email as business_email,B.mobile_no as business_mobile_no')
->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('invoice')
->where('invoice_id', $id)
->join('customers as C','C.customer_id= invoice.customer_id','left')
->join('business as B','B.business_id = invoice.business_id','left')
->join('customer_addresses as A','A.customer_id=invoice.customer_id and A.address_type=1','left')
->join('states', 'states.state_short_name = A.state AND A.country = "IN"', 'left')
->join('countries', 'countries.country_short_name = A.country', 'left')
->select('invoice.*,concat(C.first_name," ",C.last_name) as customer_name,A.address_1, A.address_2,A.postal_code,A.city, A.state,C.mobile_no as customer_mobile')
->select('B.title as ,B.business_logo,B.terms,B.address,B.city,B.state,B.postal_code,B.email,B.mobile_no')
->select('COALESCE(NULLIF(states.state_name, ""), A.state) AS customer_bill_state')
->select('concat(A.address_1," ", A.address_2) as customer_bill_address,A.postal_code as customer_bill_postal_code ,A.city as customer_bill_city, countries.country_name as customer_bill_country')
->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('books as B', 'B.book_id = invoiceitems.product', 'left')
->join('book_categories as BC', 'BC.book_id = invoiceitems.product', 'left')
->join('category as C', 'C.id = BC.category_id', '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.customer_id = S.customer_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('books as BK', 'BC.book_id = BK.book_id', 'left')
->select('S.customer_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.book_id')
->where('CM.name', strtolower($category))
->where('S.customer_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(['book_id' => $productId])
// ->join('books as B', 'B.book_id = invoiceitems.product', 'left')
// ->join('book_images as BI', 'BI.book_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('books')
->join('book_categories', 'book_categories.book_id= books.book_id', 'left')
->join('category','category.id=book_categories.category_id')
->where('category.name', 'membership')
->get()
->getResult();
}
public function getNonMembershipCategoryBooks()
{
$data = $this->db->table('books')
->select('books.*')
->join('book_categories', 'book_categories.book_id= books.book_id', 'left')
->join('category','category.id=book_categories.category_id')
->where('category.name !=', 'membership')
->groupBy('books.book_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.customer_id = invoice.customer_id', 'left')
->join('invoiceitems', 'invoiceitems.invoice_id = invoice.invoice_id', 'left')
->join('books', 'books.book_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.customer_id = invoice.customer_id', 'left')
->join('invoiceitems', 'invoiceitems.invoice_id = invoice.invoice_id', 'left')
->join('books', 'books.book_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.*, books.* , COUNT(invoiceitems.product) as item_count , sum(invoiceitems.product * invoiceitems.unit_price) as item_cost')
->where('invoice.isactive', 1)
->where('books.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('books', 'books.book_id = invoiceitems.product', 'left')
->groupBy('books.book_id')
->get()
->getResult();
return $result;
}
}