
<?php
// Checked for PHP5.6 / 8.x compatibility
// Initialize arrays to prevent undefined variable warnings
$invoicecommittedon = array();
$orderpricelist = array();
$displayline = array();
$companyidlist = array();
$customerdisplaylineqty = array();
$customerdisplaylinevalue = array();
$customerdisplayline = array();
$total = 0;
$currencypricelist = array(); // Assuming this is a global or needs initialization

include("../../operainfo/getq2.php");
$x=getsecurity();
$stock_ref=safetext2(isset($_GET['stk']) ? $_GET['stk'] : '');
$hostcompanyid=safetext2(isset($_GET['hostcompanyid']) ? $_GET['hostcompanyid'] : '');
$monthyearonly=safetext2(isset($_GET['monthyearonly']) ? $_GET['monthyearonly'] : '');
$currency=safetext2(isset($_GET['crrency']) ? $_GET['crrency'] : '');
print "<button onclick=\"get_newsalesreport('$stock_ref','$hostcompanyid')\">Overall Sales Screen</button>";
print "<button onclick=\"get_newsalesreportmonth('$stock_ref','$hostcompanyid','$monthyearonly','$currency')\">Refresh</button>";
print "<button onclick=\"get_stockdeletedorders('$stock_ref','$hostcompanyid','$monthyearonly','$currency')\">Cancelled Orders</button>";
$month=substr($monthyearonly,0,2);
$year=substr($monthyearonly,2,4);
//print "<h1>$stock_ref - $monthyearonly $hostcompanyid $currency $month $year</h1>";
$sql="
SELECT
viamedquote_orders_invoices.invoiceref,
viamedquote_orders_invoices.invoicecommitedon
FROM  viamedquote_orders_invoices
group by
viamedquote_orders_invoices.invoiceref";
$stmt = pdoquery($sql);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
	$invoiceref=$row['invoiceref'];
	$invoicecommittedon[$invoiceref]=$row['invoicecommitedon'];
}
$sql="SELECT * FROM viamedquote_orders_details where viamedquote_orders_details.stockref=? group by orderid";
$stmt = pdoquery($sql, array($stock_ref));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
	$orderid=$row['orderid'];
	$orderpricelist[$orderid]=$row['pricelist'];
	//print "$orderid $row[pricelist]<br>";
}
$sql="SELECT
count(stockref) as totalcount ,
viamedquote_orders_invoices_details.invoiceref ,
viamedquote_orders_invoices_details.deliveryid ,
viamedquote_orders_invoices_details.goodstotal ,
viamedquote_orders_invoices_details.orderid,
viamedquote_orders.company_id
FROM viamedquote_orders,viamedquote_orders_invoices_details
WHERE
viamedquote_orders_invoices_details.stockref=?  and
viamedquote_orders.hostcompany=? and
viamedquote_orders_invoices_details.orderid=viamedquote_orders.id
group by
viamedquote_orders_invoices_details.invoiceref,
viamedquote_orders_invoices_details.stockref,
viamedquote_orders_invoices_details.deliveryid
order by invoiceref
";
//print "$sql<br>";
$lowestdate=$nowis;
$highestdate=0;
$stmt = pdoquery($sql, array($stock_ref, $hostcompanyid));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$currenctcodelist['ALL']='ALL';
$headerdisplay .= "<table class=CSSTable_n2><tr><td>Invoice Reference</td><td>Date</td><td>Account</td><td>Company</td><td>Qty</td><td>Raw Value</td><td>Currency</td><td>Pricelist</td></tr>";
foreach ($rows as $row) {
	$invoiceref=$row['invoiceref'];
	$invcommitdate=$invoicecommittedon[$invoiceref];
	$orderid=$row['orderid'];
	$pricelist=$orderpricelist[$orderid];
	$deliveryid=$row['deliveryid'];
	$currenctcode=isset($currencypricelist[$pricelist]) ? $currencypricelist[$pricelist] : '';
	$companyid=$row['company_id'];
	//print "$invoiceref $invoicecommittedon[$invoiceref]<br>";

	// is POS FLow or Negative Flow
	if ($deliveryid >0) {
		// Invoice So Positive
		$mod=1;
	} else {
		$mod=-1;
		// Credit so Negative Flow
	}

	$qtyinvoiced=$row['totalcount']*$mod;
	$goodstotal=$row['goodstotal']*$mod;
	$datemonthlink=date("mY",$invcommitdate);
	$datemonthpricelistlink=date("mY",$invcommitdate).$pricelist;
	$prndate=date("d M Y",$invcommitdate);
	$float = 0; // Assuming float is 0
	$id = ''; // Assuming id is empty string
	if ($monthyearonly==$datemonthlink) {
		$displayline[$prndate] .= "<tr><Td>".operadoclinks($x,$invoiceref,$float)."$invoiceref</td><td>$prndate</td><td>CID$companyid</td><td>";
		$displayline[$prndate] .= "<button onclick=\"customerstockreport('$companyid','$stock_ref' , '$hostcompanyid')\">Zoom</button>";
		$displayline[$prndate] .= linkcrm($x,$companyid,$id).getcompanyname($companyid)."</td>
		<td>$qtyinvoiced</td><td>$goodstotal</td><td>$currenctcode</td></tr>";
		$total=$total+$qtyinvoiced;
	}

  	$companyidlist[$companyid]=$companyid;
	$datemonthcustomerlink=date("mY",$invcommitdate).$companyid;
	$customerdisplaylineqty[$datemonthcustomerlink] = (isset($customerdisplaylineqty[$datemonthcustomerlink]) ? $customerdisplaylineqty[$datemonthcustomerlink] : 0) + $qtyinvoiced;
	$customerdisplaylinevalue[$datemonthcustomerlink] = (isset($customerdisplaylinevalue[$datemonthcustomerlink]) ? $customerdisplaylinevalue[$datemonthcustomerlink] : 0) + $goodstotal;


}
ksort($displayline);
foreach ($displayline as $line) {
	$headerdisplay .=$line;
}
$headerdisplay .= "<tr><td></td><td></td><td></td><Td>TOTAL</td><td>$total</td></tr>";
$headerdisplay .= "</table>";
$lowestdateyear=date("Y",$lowestdate);
$highestdateyear=date("Y",$highestdate);
print "$headerdisplay";
?>
<br><br><br>