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

66 lines
2.8 KiB
PHP

<?php
class MPurchases extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function getTotalPurchaseAmount($purchase_id)
{
$PurchaseRecords = $this->db->where("status", 1)->where("purchases_id", $purchase_id)->get("tbl_purchasedetails")->result();
$total = 0;
foreach ($PurchaseRecords as $PurchaseRecord) {
$total = $total + ($PurchaseRecord->qty) * ($PurchaseRecord->rate);
}
return $total;
}
public function getPurchase()
{
$Purchase = $this->db->where("status", 1)->get("tbl_purchases")->result();
foreach ($Purchase as $Sale) {
$Sale->Details = $this->getPurchaseDetails($Sale->purchase_id);
}
return $Purchase;
}
public function getPurchaseDetails($purchase_id = 0)
{
$this->db->where("status", 1);
if ($purchase_id != 0) {
$this->db->where("purchases_id", $purchase_id);
}
$PurchaseRecords = $this->db->get("tbl_purchasedetails")->result();
$total = 0;
foreach ($PurchaseRecords as $PurchaseRecord) {
$PurchaseRecord->Purchase = $this->db->where("purchase_id", $PurchaseRecord->purchases_id)->get("tbl_purchases")->row();
$PurchaseRecord->Item = $this->db->where("item_id", $PurchaseRecord->items_id)->get("tbl_items")->row();
$PurchaseRecord->Account = $this->db->where("account_id", $PurchaseRecord->Purchase->accounts_id)->get("tbl_accounts")->row();
$total = $total + ($PurchaseRecord->qty) * ($PurchaseRecord->rate);
$PurchaseRecord->TotalPurchase = $total;
}
return $PurchaseRecords;
}
public function getPurchaseRecords($fromDate, $toDate)
{
$PurchaseRecords = $this->db->where("status", 1)->where("created_on between '$fromDate' AND '$toDate'")->get("tbl_purchases")->result();
foreach ($PurchaseRecords as $PurchaseRecord) {
$PurchaseRecord->Details = $this->getPurchaseDetails($PurchaseRecord->purchase_id);
$PurchaseRecord->TotalPurchase = $this->getTotalPurchaseAmount($PurchaseRecord->purchase_id);
}
return $PurchaseRecords;
}
public function getItemPurchases($item_id = 0)
{
$this->db->where("status", 1);
if ($item_id != 0) {
$this->db->where("items_id", $item_id);
}
$this->db->select('items_id, AVG(rate) as average_rate, SUM(qty) as total_qty, SUM(rate * qty) as line_total');
$this->db->group_by('items_id');
$PurchaseRecords = $this->db->get('tbl_purchasedetails')->result();
foreach ($PurchaseRecords as $PurchaseRecord) {
$PurchaseRecord->Item = $this->db->where("item_id", $PurchaseRecord->items_id)->get("tbl_items")->row();
}
return $PurchaseRecords;
}
}