BBnepal-Accounts/account/application/models/MSales.php
2024-10-17 17:06:13 +05:45

67 lines
2.6 KiB
PHP

<?php
class MSales extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function getTotalSalesAmount($sales_id)
{
$SalesRecords = $this->db->where("status", 1)->where("sales_id", $sales_id)->get("tbl_salesdetails")->result();
$total = 0;
foreach ($SalesRecords as $SalesRecord) {
$total = $total + ($SalesRecord->qty) * ($SalesRecord->rate);
}
return $total;
}
public function getSales()
{
$Sales = $this->db->where("status", 1)->get("tbl_sales")->result();
foreach ($Sales as $Sale) {
$Sale->Details = $this->getSalesDetails($Sale->sales_id);
}
return $Sales;
}
public function getSalesDetails($sales_id = 0)
{
$this->db->where("status", 1);
if ($sales_id != 0) {
$this->db->where("sales_id", $sales_id);
}
$SalesRecords = $this->db->get("tbl_salesdetails")->result();
$total = 0;
foreach ($SalesRecords as $SalesRecord) {
$SalesRecord->Sales = $this->db->where("sales_id", $SalesRecord->sales_id)->get("tbl_sales")->row();
$SalesRecord->Account = $this->db->where("account_id", $SalesRecord->Sales->accounts_id)->get("tbl_accounts")->row();
$SalesRecord->Item = $this->db->where("item_id", $SalesRecord->items_id)->get("tbl_items")->row();
$total = $total + ($SalesRecord->qty) * ($SalesRecord->rate);
$SalesRecord->TotalSales = $total;
}
return $SalesRecords;
}
public function getSalesRecords($fromDate, $toDate)
{
$SalesRecords = $this->db->where("status", 1)->where("created_on between '$fromDate' AND '$toDate'")->get("tbl_sales")->result();
foreach ($SalesRecords as $SalesRecord) {
$SalesRecord->Details = $this->getSalesDetails($SalesRecord->sales_id);
$SalesRecord->totalsales = $this->getTotalSalesAmount($SalesRecord->sales_id);
}
return $SalesRecords;
}
public function getItemSales($sales_id = 0)
{
$this->db->select('items_id, AVG(rate) as average_rate, SUM(qty) as total_qty, SUM(qty * rate) as line_total');
$this->db->from('tbl_salesdetails');
if ($sales_id != 0) {
$this->db->where('sales_id', $sales_id);
}
$this->db->group_by('items_id');
$query = $this->db->get();
$SalesRecords = $query->result();
foreach ($SalesRecords as $SalesRecord) {
$SalesRecord->Item = $this->db->where("item_id", $SalesRecord->items_id)->get("tbl_items")->row();
}
return $SalesRecords;
}
}