<?php

if (!defined('sugarEntry') || !sugarEntry)
    die('Not A Valid Entry Point');

/**
 * 
 * @param array $where Tablica typu: klucz=> wartosc gdzie klucz to 
 */
function AnalysisProductSale($where) {
    global $db;
    $query = "SELECT p.quantity_corrected, p.total_brutto_corrected, p.total_netto_corrected ,p.ecminvoiceout_id, p.ecmproduct_id, p.code, p.name, p.total_netto, p.total_brutto, p.price_purchase, p.quantity , i.register_date, i.type, i.currency_value, i.currency_value_nbp, i.currency_id FROM ecminvoiceoutitems p, ecminvoiceouts i WHERE p.ecminvoiceout_id = i.id";
    //die(1);
    if (isset($where) && is_array($where) && count($where) > 0) {

        $wherereturn = array();
        foreach ($where as $key => $value) {
            switch ($key) {
                case 'register_date_from':
                    $wherereturn[] = "i.register_date>='" . $value . "'";
                    break;
                case 'register_date_to':
                    $wherereturn[] = "i.register_date<='" . $value . "'";
                    break;
                case 'accountName':
                    $wherereturn[] = "i.parent_name LIKE '%" . trim($value) . "%'";
                    $wherereturn[] = "i.parent_type ='Accounts'";
                    break;
                case 'productName':
                    $wherereturn[] = "p.name LIKE '%" . trim($value) . "%'";
                    break;
				case 'productId':
                    $wherereturn[] = "p.ecmproduct_id = '" . trim($value) . "'";
                    break;
				case 'accountId':
					$wherereturn[] = "i.parent_id = '" . trim($value) . "'";
					break;
                default:
                    $wherereturn[] = "i." . $key . " = '" . $value . "'";
                    break;
            }
        }
        if (count($wherereturn) > 0) {
            $query .= " AND " . implode(" AND ", $wherereturn);
        }
    }
    $query .= " ORDER BY i.register_date";
	//echo $query;
    $result = $db->query($query);
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
			$currency = 1;
			if($row['type']!='K'){
				if($row['currency_value']!=''&& $row['currency_value']!='0'){
					$currency = $row['currency_value'];
				}elseif($row['currency_value_nbp']!=''){
					$currency = $row['currency_value_nbp'];
				}
			}
			if($row['type']=='normal'){
				$return[substr($row['register_date'], 0, 7)]['total_purchase'] += round($row['price_purchase']* $row['quantity'],2);
				$return[substr($row['register_date'], 0, 7)]['total_netto'] += $row['total_netto']* $currency;
			//	$return[substr($row['register_date'], 0, 7)]['total_brutto'] += $row['total_brutto']* $currency;
			}else{
				$return[substr($row['register_date'], 0, 7)]['total_purchase'] += round($row['price_purchase'] * $row['quantity_corrected'] ,2);
				$return[substr($row['register_date'], 0, 7)]['total_netto'] += $row['total_netto_corrected']* $currency;
			//	$return[substr($row['register_date'], 0, 7)]['total_brutto'] += $row['total_brutto_corrected']* $currency;
			}
        }
    } else {
        $return = NULL;
    }
    return $return;
}

function getProductCategoryList() {
    global $db;
    $resultArray = NULL;
    $query = "SELECT id, name FROM ecmproductcategories WHERE deleted=0 ORDER BY name, date_entered";
    $result = $db->query($query);
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $resultArray[$row['id']] = $row['name'];
        };
    }
    return $resultArray;
}
?>
