0byt3m1n1
Path:
C:
/
wamp64
/
www
/
cpms_new
/
ACCOUNTS
/
[
Home
]
File: ac_models.php
<?php require_once "../lib/config.php"; require_once BASE_DIR."/lib/db/Connection.php"; require_once("../model/defaults_mdl.php"); class Ac_Models { protected $db; public function __construct() { $dbHost = "localhost"; $dbUser = "root"; $dbPassword = ""; $dbName = "cpms"; $this->db = new mysqli($dbHost, $dbUser, $dbPassword, $dbName); if ($this->db->connect_error) { die("Database connection failed: " . $this->db->connect_error); } } public static function getBudgetForm($project_id, $from, $to, $status) { $arr = array(); $status_clause = (!empty($status) && $status != "All") ? "bf.status = '$status'" : "bf.status <> 'DELETED'"; $project_clause = (!empty($project_id) && $project_id != "All") ? "P.project_id='$project_id' AND " : ""; try { $mysqli = new mysqli("localhost", "root", "", "cpms"); if ($mysqli->connect_error) { die("Connection failed: " . $mysqli->connect_error); } $u_id = @$_SESSION['user_id']; $sql = "SELECT *, bh.id AS id, bf.name AS sup_name, bf.amount, bfm.name AS debit_head, LOWER(bf.status) AS status, DATE_FORMAT(bf.date_current, '%d-%b-%Y') AS created_on, bf.id AS id, inst.name AS installment, IF('0000-00-00' = bf.invoice_date OR '1970-01-01' = bf.invoice_date, ' ', bf.invoice_date) AS invoice_date, DATE_FORMAT(bf.date_current, '%d-%b-%Y') AS created_on, bf.remarks, UU.name AS created_by FROM tab_budget_form bf JOIN tab_budget_head bh ON bf.debit_head = bh.id JOIN addproject P ON p.project_id = bf.project_id LEFT OUTER JOIN tab_budget_head_master bfm ON bh.budget_head_id = bfm.id LEFT OUTER JOIN tab_project_installment inst ON inst.id = bf.installment_id JOIN user UU ON UU.user_id = bf.created_by WHERE $project_clause"; if (!empty($from) && !empty($to)) { $sql .= " bf.date_current >= '$from' AND bf.date_current <= '$to' AND"; } $sql .= "$status_clause AND bf.status <> 'DELETED' AND FIND_IN_SET(P.fundtype, (SELECT accounts_type FROM user WHERE user_id = $u_id)) GROUP BY bf.id ORDER BY bf.id DESC"; $result = $mysqli->query($sql); if ($result) { while ($row = $result->fetch_assoc()) { $arr[] = $row; } $result->free(); } else { echo "Error: " . $mysqli->error; } $mysqli->close(); } catch (Exception $ex) { // Handle exception if needed } return $arr; } public static function getInstallmentDetails($project_id){ $arr = array(); try{ $mysqli = new mysqli("localhost", "root", "", "cpms"); $u_id=@$_SESSION['user_id']; $sql="select id,name from tab_project_installment " ." where project_id='$project_id' AND created_by = $u_id Order by id desc limit 1"; $result = $mysqli->query($sql); if ($result) { while ($row = $result->fetch_assoc()) { $arr[] = $row; } $result->free(); } else { echo "Error: " . $mysqli->error; } $mysqli->close(); } catch (Exception $ex) { // Handle exception if needed } return $arr; } public static function getBudgetHead($project_id){ $arr = array(); try{ $mysqli = new mysqli("localhost", "root", "", "cpms"); $u_id=@$_SESSION['user_id']; $debit_heads=@$_SESSION['debit_heads']; $disabled_debit_heads=@$_SESSION['disabled_debit_heads']; $sql="select a.id as id,mas.name as name,mas.type as master_debit from tab_budget_head a" ." JOIN tab_budget_head_master mas ON a.budget_head_id = mas.id" ." where project_id='$project_id' "; if(!empty($debit_heads)) { $sql.=" AND a.budget_head_id IN ($debit_heads)"; } if(!empty($disabled_debit_heads)) { $sql.=" AND a.budget_head_id NOT IN ($disabled_debit_heads)"; } // echo "$sql"; $result = $mysqli->query($sql); if ($result) { while ($row = $result->fetch_assoc()) { $arr[] = $row; } $result->free(); } else { echo "Error: " . $mysqli->error; } $mysqli->close(); } catch (Exception $ex) { // Handle exception if needed } return $arr; } public static function addBudgetHead($data){ if(!self::insert("tab_budget_form",$data)){ return FALSE; }else{ return TRUE; } } public function insert($table, $data){ try { $mysqli = new mysqli("localhost", "root", "", "cpms"); $key = array_keys($data); $value = array_values($data); $query ="INSERT INTO $table ( ". implode(',' , $key) .") VALUES('". implode("','" , $value) ."')"; $result = $mysqli->query($sql); if ($result) { while ($row = $result->fetch_assoc()) { $arr[] = $row; } $result->free(); } else { echo "Error: " . $mysqli->error; } $mysqli->close(); } catch (Exception $ex) { // Handle exception if needed } return $arr; } public static function getPendingVouchers($user=""){ $arr = array(); $us = ""; if(!empty($user)){ if($user =="pi"){ $us = " AND FIND_IN_SET(P.fundtype,(SELECT accounts_type FROM user WHERE user_id =".$_SESSION['user_id'].")) AND UU.is_admin <>2 AND bf.created_by <> ".$_SESSION['user_id']; }else{ $us = "AND bf.created_by = ".$_SESSION['user_id']; } } try{ $mysqli = new mysqli("localhost", "root", "", "cpms"); $u_id=@$_SESSION['user_id']; $sql="select *,bh.id as id,bf.name sup_name,bf.amount,bfm.name as debit_head,LOWER(bf.status) as status, " . " DATE_FORMAT(bf.date_current ,'%d-%b-%Y') as created_on,bf.id AS id,inst.name as installment," . " IF( '0000-00-00' = bf.invoice_date OR '1970-01-01'=bf.invoice_date , ' ',bf.invoice_date ) as invoice_date " ." , GREATEST(DATE_FORMAT(bf.date_current ,'%d-%b-%Y'), COALESCE(DATE_FORMAT(bf.modified_on ,'%d-%b-%Y'), 0), COALESCE(" ." DATE_FORMAT(bf.modified_date_accounts ,'%d-%b-%Y'), 0)) as created_on " . ",UU.name as user_name from tab_budget_form bf " . " JOIN tab_budget_head bh ON bf.debit_head = bh.id " . " JOIN addproject P ON p.project_id = bf.project_id" . " LEFT OUTER JOIN tab_budget_head_master bfm ON bh.budget_head_id = bfm.id " . " LEFT OUTER JOIN tab_project_installment inst ON inst.id= bf.installment_id" . " JOIN user UU ON UU.user_id =bf.created_by " . " where "; $user_debit_condition = ""; $debit_heads=@$_SESSION['debit_heads']; $disabled_debit_heads=@$_SESSION['disabled_debit_heads']; if(!empty($debit_heads)) { $user_debit_condition=" AND bh.budget_head_id IN ($debit_heads)"; } if(!empty($disabled_debit_heads)) { $user_debit_condition.=" AND bh.budget_head_id NOT IN ($disabled_debit_heads)"; } $sql.= "bf.status = 'VERIFIED' $us $user_debit_condition ORDER BY bf.id DESC"; // ec $result = $mysqli->query($sql); if ($result) { while ($row = $result->fetch_assoc()) { $arr[] = $row; } $result->free(); } else { echo "Error: " . $mysqli->error; } $mysqli->close(); } catch (Exception $ex) { // Handle exception if needed } return $arr; } public function getBudgetFormById($id){ $arr = array(); try{ $u_id=@$_SESSION['user_id']; $sql="select *,bh.budget_head_id as bh_head_id,a.amount as amount,IF( '0000-00-00' = a.invoice_date OR '1970-01-01'=a.invoice_date , '',a.invoice_date ) as invoice_date, a.name as name from tab_budget_form a " ." JOIN tab_budget_head bh ON bh.id = a.debit_head " ." where a.id = '$id' AND a.status ='VERIFIED' "; $result = $mysqli->query($sql); if ($result) { while ($row = $result->fetch_assoc()) { $arr[] = $row; } $result->free(); } else { echo "Error: " . $mysqli->error; } $mysqli->close(); } catch (Exception $ex) { // Handle exception if needed } return $arr; } function update($table, $data, $where){ try{ $cols = array(); foreach($data as $key=>$val) { $cols[] = "$key = '$val'"; } $sql = "UPDATE $table SET " . implode(', ', $cols) . " WHERE $where"; // echo $sql; $result = $mysqli->query($sql); if ($result) { while ($row = $result->fetch_assoc()) { $arr[] = $row; } $result->free(); } else { echo "Error: " . $mysqli->error; } $mysqli->close(); } catch (Exception $ex) { // Handle exception if needed } return $arr; } public function editBudgetHead($id,$post){ if(!self::update("tab_budget_form",$post,"id = '$id' ")){ return FALSE; }else{ return TRUE; } } public function update_voucher_status($budget_id,$type,$remarks){ $status = ($type =="accept") ? "ACCEPTED" : "DECLINED"; $status = ($type =="self_declined") ? "VERIFIED" : $status; $post = array("status" =>$status,"remarks" =>$remarks,"modified_on" => date("Y-m-d H:i:s" )); $post['date_current'] = date("Y-m-d"); if(!self::update("tab_budget_form",$post,"id IN ($budget_id) ")){ return FALSE; }else{ return TRUE; } } public function getPendingVouchersCount($user=""){ $arr = array(); $us = ""; if(!empty($user)){ if($user =="pi"){ $us = " AND FIND_IN_SET(P.fundtype,(SELECT accounts_type FROM user WHERE user_id =".$_SESSION['user_id'].")) AND UU.is_admin <>2 AND bf.created_by <> ".$_SESSION['user_id']; }else{ $us = "AND bf.created_by = ".$_SESSION['user_id']; } } try{ $mysqli = new mysqli("localhost", "root", "", "cpms"); $sql="select count(*) as cnt from tab_budget_form bf " . " JOIN tab_budget_head bh ON bf.debit_head = bh.id " . " JOIN addproject P ON p.project_id = bf.project_id" . " LEFT OUTER JOIN tab_budget_head_master bfm ON bh.budget_head_id = bfm.id " . " LEFT OUTER JOIN tab_project_installment inst ON inst.id= bf.installment_id" . " JOIN user UU ON UU.user_id = bf.created_by" . " where "; $user_debit_condition = ""; $debit_heads=@$_SESSION['debit_heads']; $disabled_debit_heads=@$_SESSION['disabled_debit_heads']; if(!empty($debit_heads)) { $user_debit_condition=" AND bh.budget_head_id IN ($debit_heads)"; } if(!empty($disabled_debit_heads)) { $user_debit_condition.=" AND bh.budget_head_id NOT IN ($disabled_debit_heads)"; } $sql.= "bf.status = 'VERIFIED' $us $user_debit_condition ORDER BY bf.id DESC"; $result = $mysqli->query($sql); if ($result) { while ($row = $result->fetch_assoc()) { $arr[] = $row; } $result->free(); } else { echo "Error: " . $mysqli->error; } $mysqli->close(); } catch (Exception $ex) { // Handle exception if needed } return $arr; } public function getBudgetSummary($project_id,$start_date,$end_date){ $summary = array(); $summary['installment_details'] = self::getProjectInstallmentTotal($project_id,$start_date,$end_date); $summary['received_funds'] = self::getInstallmentTotal($project_id,$start_date,$end_date); $summary['voucher_details'] = self::getVouchers($project_id,$start_date,$end_date); $summary['project_details'] = self::getProjectDetails($project_id); $summary['project_outlay'] = self::getProjectOutlayDetails($project_id); return $summary; } public function getProjectOutlayDetails($project_id){ $arr = array(); try{ $u_id=@$_SESSION['user_id']; $sql="SELECT * from project_total_outlay WHERE project_id =$project_id "; $sql = mysql_query($sql); if (mysql_num_rows($sql)>0){ while($row=mysql_fetch_assoc($sql)){ $arr[] = $row; } } }catch( Exception $ex){ } return $arr; } public function getProjectInstallmentTotal($project_id,$start_date,$end_date){ $arr = array(); try{ $u_id=@$_SESSION['user_id']; $sql="select I.id,I.project_id,I.name AS installment_name,BHM.name debit_name,SUM(II.amount) as amount,BHM.id as bhm_id from tab_project_installment I " ." JOIN tab_project_installment_details II ON II.installment_id = I.id" ." JOIN tab_budget_head BH ON BH.id = II.budget_head_id " ." JOIN tab_budget_head_master BHM ON BHM.id = BH.budget_head_id " ." where I.project_id='$project_id' " ." AND DATE(I.created_on) BETWEEN '$start_date' AND '$end_date' " ." group by II.budget_head_id ORDER BY BHM.id"; // echo "$sql"; $result = $mysqli->query($sql); if ($result) { while ($row = $result->fetch_assoc()) { $arr[] = $row; } $result->free(); } else { echo "Error: " . $mysqli->error; } $mysqli->close(); } catch (Exception $ex) { // Handle exception if needed } return $arr; } public function getInstallmentTotal($project_id,$start_date,$end_date){ $arr = array(); try{ $u_id=@$_SESSION['user_id']; $sql="select I.id,I.project_id,I.name AS installment_name,sum(II.amount) as amount , case when DATE(I.created_on) BETWEEN '$start_date' AND '$end_date' then 1 else 0 end as is_show from tab_project_installment I " ." JOIN tab_project_installment_details II ON II.installment_id = I.id" ." JOIN tab_budget_head BH ON BH.id = II.budget_head_id " ." where I.project_id='$project_id' group by II.installment_id"; // echo "$sql"; $result = $mysqli->query($sql); if ($result) { while ($row = $result->fetch_assoc()) { $arr[] = $row; } $result->free(); } else { echo "Error: " . $mysqli->error; } $mysqli->close(); } catch (Exception $ex) { // Handle exception if needed } return $arr; } public function getVouchers($project_id,$start_date,$end_date){ $arr = array(); try{ $u_id=@$_SESSION['user_id']; $sql="Select sum(TBF.amount) as amount,Bh.id as debit_head, BHM.name as debit_name,BHM.id as bhm_id from tab_budget_form TBF " ." JOIN tab_budget_head BH ON BH.id = TBF.debit_head" ." JOIN tab_budget_head_master BHM ON BHM.id = BH.budget_head_id" ." WHERE TBF.status ='ACCEPTED' AND " ." DATE(TBF.date_current) BETWEEN '$start_date' AND '$end_date' AND TBF.project_id = $project_id group by TBF.debit_head ORDER BY BHM.id"; // echo $sql; $result = $mysqli->query($sql); if ($result) { while ($row = $result->fetch_assoc()) { $arr[] = $row; } $result->free(); } else { echo "Error: " . $mysqli->error; } $mysqli->close(); } catch (Exception $ex) { // Handle exception if needed } return $arr; } public function getProjectDetails($project_id){ $arr = array(); try{ $u_id=@$_SESSION['user_id']; $Columns = " p.project_code,MFT.name as fundtype, DATE_FORMAT(p.startdate ,'%d-%b-%Y') as startdate, DATE_FORMAT(p.end_date ,'%d-%b-%Y') as end_date,p.project_outlay,p.duration , p.project_id,p.project_title,p.duration,p.fundagency,P.investigators,CONCAT(U.salutation,' ',U.name) as name, (SELECT GROUP_CONCAT(' ', CONCAT(UU.salutation,' ',UU.name)) from user as UU WHERE FIND_IN_SET(UU.user_id,(P.co_pi)) ) as co_pi,p.fundtype as fund_type_id" ; $sql="Select $Columns from addproject P " ." JOIN tab_master_fund_type MFT ON p.fundtype = MFT.id" ." JOIN user U ON p.user_id = U.user_id" ." where P.project_id='$project_id' GROUP BY P.project_id "; // echo $sql; $result = $mysqli->query($sql); if ($result) { while ($row = $result->fetch_assoc()) { $arr[] = $row; } $result->free(); } else { echo "Error: " . $mysqli->error; } $mysqli->close(); } catch (Exception $ex) { // Handle exception if needed } return $arr; } public function getAllMasterBudgetHeads(){ $arr = array(); try{ $mysqli = new mysqli("localhost", "root", "", "cpms"); $u_id=@$_SESSION['user_id']; $sql="Select *,DATE_FORMAT(created_on ,'%d-%b-%Y') as created_on from tab_budget_head_master LIMIT 50" ; $result = $mysqli->query($sql); if ($result) { while ($row = $result->fetch_assoc()) { $arr[] = $row; } $result->free(); } else { echo "Error: " . $mysqli->error; } $mysqli->close(); } catch (Exception $ex) { // Handle exception if needed } return $arr; } public function getBudgetFormWithBudgetHead($from,$to,$budget_head){ $arr = array(); try{ $u_id=@$_SESSION['user_id']; $sql="select *,bh.id as id,bf.name sup_name,bf.amount,bfm.name as debit_head,LOWER(bf.status) as status, " . " DATE_FORMAT(bf.date_current ,'%d-%b-%Y') as created_on,bf.id AS id,inst.name as installment," . " IF( '0000-00-00' = bf.invoice_date OR '1970-01-01'=bf.invoice_date , ' ',bf.invoice_date ) as invoice_date " ." , DATE_FORMAT(bf.date_current ,'%d-%b-%Y') as created_on " . " ,bf.remarks,UU.name as created_by from tab_budget_form bf " . " JOIN tab_budget_head bh ON bf.debit_head = bh.id " . " JOIN addproject P ON p.project_id = bf.project_id" . " LEFT OUTER JOIN tab_budget_head_master bfm ON bh.budget_head_id = bfm.id " . " LEFT OUTER JOIN tab_project_installment inst ON inst.id= bf.installment_id" . " JOIN user UU ON UU.user_id= bf.created_by" . " where "; if(!empty($from) && !empty($to)){ $sql.= " bf.date_current >='$from' AND bf.date_current <='$to' AND "; } $sql.= " bfm.id=$budget_head AND bf.status = 'ACCEPTED' AND FIND_IN_SET(P.fundtype,(SELECT accounts_type FROM user WHERE user_id =$u_id) ) GROUP BY bf.id ORDER BY bf.id DESC"; // echo $sql; $result = $mysqli->query($sql); if ($result) { while ($row = $result->fetch_assoc()) { $arr[] = $row; } $result->free(); } else { echo "Error: " . $mysqli->error; } $mysqli->close(); } catch (Exception $ex) { // Handle exception if needed } return $arr; } } ?>