Files
crm.twinpol.com/modules/EcmCharts/Dashlets/MyChartsAgreementsDashlet/ChartsAgreementsDashlet.php
2025-05-12 15:44:39 +00:00

151 lines
4.1 KiB
PHP
Executable File

<?php
class ChartsAgreementsDashlet
{
private $outputOptions = null;
private $options = null;
private $db = null;
private $monthName = null;
private $comparativeData = null;
private $months;
public function getTable($db, $date_from)
{
$Calendar_daFormat = str_replace("d","%d",str_replace("m","%m",str_replace("Y","%Y",$GLOBALS['timedate']->get_date_format())));
// Default value from date input
$dateq=date("Y-m-d",strtotime($date_from));
$total=0;
$total_o=0;
$total_p=0;
$total_r=0;
$q=$db->query("select payment_date,(interest_rate) as rate,sum(rate_of_commission) as rate2,sum(principal_value) as rate3,sum(rate_of_commission+principal_value+interest_rate) as total from ecmagreementitems
where payment_date>='".$dateq."'
group by payment_date asc");
$table=array();
$chart=array();
while($row=$db->fetchByAssoc($q)){
$row['payment_date']=$this->dateV('Y F',strtotime($row['payment_date']));
$total+=$row['total'];
$total_o+=$row['rate'];
$total_p+=$row['rate2'];
$total_r+=$row['rate3'];
$table[]=$row;
}
return $table;
}
function dateV($format,$timestamp=null){
$to_convert = array(
'l'=>array('dat'=>'N','str'=>array('Poniedziałek','Wtorek','Środa','Czwartek','Piątek','Sobota','Niedziela')),
'F'=>array('dat'=>'n','str'=>array('styczeń','luty','marzec','kwiecień','maj','czerwiec','lipiec','sierpień','wrzesień','październik','listopad','grudzień')),
'f'=>array('dat'=>'n','str'=>array('stycznia','lutego','marca','kwietnia','maja','czerwca','lipca','sierpnia','września','października','listopada','grudnia'))
);
if ($pieces = preg_split('#[:/.\-, ]#', $format)){
if ($timestamp === null) { $timestamp = time(); }
foreach ($pieces as $datepart){
if (array_key_exists($datepart,$to_convert)){
$replace[] = $to_convert[$datepart]['str'][(date($to_convert[$datepart]['dat'],$timestamp)-1)];
}else{
$replace[] = date($datepart,$timestamp);
}
}
$result = strtr($format,array_combine($pieces,$replace));
return $result;
}
}
// Zwraca ustawienia Google Charts
public function getChartOptions()
{
$months = $this->getMonths();
$data = "";
for( $i = 0; $i < count($months); $i++ )
{
if( $months[$i] != 0 && $months[$i] != null)
{
$value = sprintf("%.2f", (float)$months[$i]);
$data .= "['".$this->monthName[$i]."', ". $value;
if( $this->options["comparativeData"] == "enabled" )
$data .= ", ".$this->comparativeData[$i].'0';
$data .= "],";
}
}
$data = rtrim($data, ",");
return $data;
}
// Pobiera tablice z wartościami sum netto dla każdego roku
private function getMonths()
{
$months = array();
$thisYear = date("Y");
$thisMonth = date("m");
for( $i = 1; $i <= 12; $i++ )
{
$result = $this->getSubtotalByMonth(($i <= 9 ? '0'.$i : $i), $this->options['year']);
array_push($months, $result);
}
$this->months = $months;
return $months;
}
// Metoda zwraca cenę netto dla danego miesiąca w danym roku
private function getSubtotalByMonth($month, $year)
{
$type = $this->options["type"];
$query = "
SELECT
SUM(CASE WHEN e.currency_value = '0' OR e.currency_value = '' OR e.currency_value=0 OR e.currency_value is null
THEN
e.total_netto
ELSE
e.total_netto*e.currency_value
END
) as 'subtotal'
FROM
ecminvoiceouts e
WHERE e.canceled = '0'
AND e.deleted = '0'
AND e.type LIKE '$type'
AND e.register_date LIKE '$year-$month-%'
";
//echo $query."<br><br>";
if( $this->options["contractor_id"] && $this->options["contractor_name"])
{
$query .= "AND e.parent_id = '".$this->options["contractor_id"]."'";
}
$results = $this->db->query($query);
$result = $this->db->fetchByAssoc( $results );
return $result['subtotal'];
}
public function getMonthsArray()
{
return $this->months;
}
public function haveComparativeData()
{
if( $this->comparativeData != null )
return true;
else
return false;
}
}
?>