Files
crm.e5.pl/modules/EcmReports/Forecast.php
2024-04-27 09:23:34 +02:00

305 lines
15 KiB
PHP

<?php
global $db;
if(!$_REQUEST['year']){
$_REQUEST['year']=date("Y");
$_GET['year']=date("Y");
}
if($_REQUEST['save']){
//print_r($_REQUEST);
$w=$db->query("select id from ecmproducts where product_active='1' and deleted='0' order by product_category_name asc,code asc");
while($r=$db->fetchByAssoc($w)){
$ww=$db->query("select id from accounts where deleted='0' and core_client='1' order by name asc");
while($rr=$db->fetchByAssoc($ww)){
$rrr=$db->fetchByAssoc($db->query("select count(*) as cnt from ecmforecasts where ecmproduct_id='".$r['id']."' and account_id='".$rr['id']."'"));
if($_REQUEST['pred'][$r['id']][$rr['id']]!=0){
$value=$_REQUEST['pred'][$r['id']][$rr['id']];
if($rrr['cnt']>0){
$db->query("update ecmforecasts set value='".$value."' where ecmproduct_id='".$r['id']."' and account_id='".$rr['id']."'");
}
else{
$db->query("insert into ecmforecasts set id='".create_guid()."',ecmproduct_id='".$r['id']."',account_id='".$rr['id']."',value='".$value."'");
}
}
}
$rrr=$db->fetchByAssoc($db->query("select count(*) as cnt from ecmforecasts where ecmproduct_id='".$r['id']."' and account_id='new'"));
if($_REQUEST['pred'][$r['id']]["new"]!=0){
$value=$_REQUEST['pred'][$r['id']]["new"];
if($rrr['cnt']>0){
$db->query("update ecmforecasts set value='".$value."' where ecmproduct_id='".$r['id']."' and account_id='new'");
}
else{
$db->query("insert into ecmforecasts set id='".create_guid()."',ecmproduct_id='".$r['id']."',account_id='new',value='".$value."'");
}
}
}
header("Location: index.php?module=EcmReports&action=Forecast&category=".$_REQUEST['category']);
}
?>
<table cellspacing="0" cellpadding="0" border="0"><tr><td><img src="themes/Sugar/images/EcmProducts.gif" style="margin-top: 3px; margin-right: 3px;" alt="EcmProducts" width="16" border="0" height="16"></td><td><h2>Charts: Products </h2></td></tr></table><br />
<ul class="tablist">
<li>
<a class="current" href="#">Basic Search</a>
</li>
</ul>
<form action="index.php" method="get" name="search_reports">
<input type="hidden" name="module" value="EcmReports" />
<input type="hidden" name="action" value="Forecast" />
<table style="border-top: 0px none; margin-bottom: 4px;width:100%" class="tabForm" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="dataLabel" width="20%" nowrap="nowrap">Year</td>
<td class="dataField" width="30%" nowrap="nowrap">
<select name="year" id="year">
<?
$ey=(int)date("Y");
$sy=2000;
for($i=$sy;$i<=$ey;$i++){
echo '<option value="'.$i.'"';
if($i==$_GET['year'])echo ' selected';
echo '>'.$i.'</option>';
}
?>
</select>
</td>
<td class="dataLabel" width="10%" nowrap="nowrap">Category</td>
<td class="dataField" width="90%" nowrap="nowrap">
<select name="category" id="category"><option value="">select</option>
<?
$w=mysql_query("select id,name from ecmproductcategories where deleted='0' order by name asc");
while($r=mysql_fetch_array($w)){
echo '<option value="'.$r['id'].'"';
if($_REQUEST['category']==$r['id'])echo ' selected';
echo '>'.$r['name'].'</option>';
}
?>
</select>
</td>
</tr>
</tbody>
</table>
<input class="button" name="submit" value="Search" type="submit">
<input class="button" name="xls" value="Create XLS" type="button" onclick="location.href='index.php?module=EcmReports&action=CreateXLSforecast&category=<?php echo $_REQUEST['category'];?>&year=<?php echo $_REQUEST['year'];?>';">
<br />
</form>
<?php
$trs='<tr>';
$tre='</tr>';
$tds='<td class="oddListRowS1" style="text-align:center">';
$tds_left='<td class="oddListRowS1">';
$tds_l='<td class="listViewThS1" style="width:40px;border: 1px solid #cccccc;text-align:center">';
$tds_cat='<td class="listViewThS1" style="width:80px;border: 1px solid #cccccc;text-align:center">';
$tds_inv='<td class="listViewThS1" colspan="3" style="width:80px;border: 1px solid #cccccc;text-align:center">';
$tds_name='<td class="listViewThS1" style="width:130px;border: 1px solid #cccccc;text-align:center">';
$tds_index='<td class="listViewThS1" style="width:50px;border: 1px solid #cccccc;text-align:center">';
$tds_total='<td class="listViewThS1" colspan="4" style="width:30px;border: 1px solid #cccccc;text-align:center">';
$tds_account='<td colspan="5" class="listViewThS1" style="width:200px;border: 1px solid #cccccc;text-align:center">';
$tds_account_new='<td colspan="3" class="listViewThS1" style="width:100px;border: 1px solid #cccccc;text-align:center">';
$tbs='<table cellpadding="0" cellspacing="0" border="0" class="ListView" width="100%">';
$tbe='</table>';
$tde='</td>';
$tdr='</tr>';
include_once("modules/EcmReports/class.sales.php");
$w=$db->query("select id from accounts where deleted='0' and core_client='1'");
if(!$_REQUEST['year'])$_REQUEST['year']=date("Y");
while($r=$db->fetchByAssoc($w)){
$s=new Sales();
$s->date_from=$_REQUEST['year']."-01-01";
$s->date_to=$_REQUEST['year']."-12-31";
$s->account_id=$r['id'];
$s->product_active="active";
$sdata[$r['id']]=$s->getSalesByProductForecast();
}
echo '<script language="javascript" src="modules/EcmProducts/helper.js"></script>';
echo '<script language="javascript" src="modules/EcmProducts/mintajax.js"></script>';
echo '<script>
function nf(nStr, inD, outD, sep)
{
nStr += \'\';
var dpos = nStr.indexOf(inD);
var nStrEnd = \'\';
if (dpos != -1) {
nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
nStr = nStr.substring(0, dpos);
}
var rgx = /(\d+)(\d{3})/;
while (rgx.test(nStr)) {
nStr = nStr.replace(rgx, \'$1\' + sep + \'$2\');
}
return nStr + nStrEnd;
}
function countTotals(){
var q;var s;var st=0;var qt=0;';
if($_REQUEST['category'])$wh_cat=" and product_category_id='".$_REQUEST['category']."'";
else $wh_cat="";
$w=$db->query("select ems_price,ems_qty_in_stock,id,name,code,product_category_name,product_category_id,srp_price from ecmproducts where product_active='1' and production!='1'".$wh_cat." and deleted='0' order by product_category_name asc,code asc");
while($r=$db->fetchByAssoc($w)){
echo 'q=0;s=0;';
$ww=$db->query("select name,id from accounts where deleted='0' and core_client='1' order by name asc");
while($rr=$db->fetchByAssoc($ww)){
echo 'q+=parseInt(document.getElementById("pred['.$r['id'].']['.$rr['id'].']").value);
s+=parseFloat(document.getElementById("avg_'.$r['id'].'_'.$rr['id'].'").value)*parseInt(document.getElementById("pred['.$r['id'].']['.$rr['id'].']").value);';
}
echo 'q+=parseInt(document.getElementById("pred['.$r['id'].'][new]").value);
s+=parseFloat(document.getElementById("avg_'.$r['id'].'_new").value)*parseInt(document.getElementById("pred['.$r['id'].'][new]").value);';
echo 'document.getElementById("total_pred_sales_'.$r['id'].'").innerHTML=nf(s.toFixed(2),\'.\',\',\',\'.\');document.getElementById("total_pred_qty_'.$r['id'].'").innerHTML=q;st+=s;qt+=q;';
}
echo 'document.getElementById("total_pred_sales").innerHTML=nf(st.toFixed(2),\'.\',\',\',\'.\');document.getElementById("total_pred_qty").innerHTML=qt;
}
function countPrediction(p,a){
var q=document.getElementById("pred["+p+"]["+a+"]").value;
var s=document.getElementById("avg_"+p+"_"+a).value;
q=q.replace(",",".");
q=parseFloat(q);
s=s.replace(",",".");
s=parseFloat(s);
if(s && q)document.getElementById("pred_"+p+"_"+a).innerHTML=nf(s*q,\'.\',\',\',\'.\');
countTotals();
}
</script>';
$t.='<br><form action="index.php?module=EcmReports&action=Forecast&category='.$_REQUEST['category'].'" method="post"><input type="submit" name="save" class="button" value="Save" /><br><br>';
$t.=$tbs;
$t.=$trs;
$t.=$tds_index."Index".$tde;
$t.=$tds_name."Name".$tde;
$t.=$tds_cat."Category".$tde;
$t.=$tds_inv."Inventory".$tde;
$t.=$tds_cat."SRP price".$tde;
$t.=$tds_total."Total".$tde;
$w=$db->query("select name from accounts where deleted='0' and core_client='1' order by name asc");
while($r=$db->fetchByAssoc($w)){
$t.=$tds_account.$r['name'].$tde;
}
$t.=$tds_account_new."New clients".$tde;
$t.=$tre;
$t.=$trs;
$t.=$tds_index."&nbsp;".$tde;
$t.=$tds_name."&nbsp;".$tde;
$t.=$tds_cat."&nbsp;".$tde;
$t.=$tds_cat."Qty".$tde;
$t.=$tds_cat."Price".$tde;
$t.=$tds_cat."Value".$tde;
$t.=$tds_cat."&nbsp;".$tde;
$t.=$tds_cat."Sales".$tde;
$t.=$tds_cat."Qty".$tde;
$t.=$tds_cat."Pred. sales".$tde;
$t.=$tds_cat."Pred. qty".$tde;
$w=$db->query("select name from accounts where deleted='0' and core_client='1' order by name asc");
while($r=$db->fetchByAssoc($w)){
$t.=$tds_l."Avg. price".$tde;
$t.=$tds_l."Qty".$tde;
$t.=$tds_l."Sales".$tde;
$t.=$tds_l."Pred. qty".$tde;
$t.=$tds_l."Pred. sales".$tde;
}
$t.=$tds_l."Avg. price".$tde;
$t.=$tds_l."Qty".$tde;
$t.=$tds_l."Prediction".$tde;
$t.=$tre;
function getTotalFromAccounts($d,$id,$type="sales"){
global $db;
$ww=$db->query("select name,id from accounts where deleted='0' and core_client='1' order by name asc");
while($rr=$db->fetchByAssoc($ww)){
$total+=$d[$rr['id']][$id][$type];
}
return $total;
}
if($_REQUEST['category'])$wh_cat=" and product_category_id='".$_REQUEST['category']."'";
else $wh_cat="";
$w=$db->query("select ems_price,ems_qty_in_stock,id,name,code,product_category_name,product_category_id,srp_price from ecmproducts where product_active='1' and production!='1'".$wh_cat." and deleted='0' order by product_category_name asc,code asc");
while($r=$db->fetchByAssoc($w)){
$m++;
$t.=$trs;
$t.=$tds_left.$r['code'].$tde;
$t.=$tds_left.'<a href="index.php?module=EcmProducts&action=DetailView&record='.$r['id'].'">'.$r['name'].'</a>'.$tde;
$t.=$tds.'<a href="index.php?modules=EcmProductCategories&action=DetailView&record='.$r['product_category_id'].'">'.$r['product_category_name'].'</a>'.$tde;
$t.=$tds.number_format($r['ems_qty_in_stock'],0,",",".").$tde;
$t.=$tds.number_format($r['ems_price'],2,",",".").$tde;
$t.=$tds.number_format($r['ems_price']*$r['ems_qty_in_stock'],2,",",".").$tde;
$t.=$tds.'<div id="srp_price_'.$r['id'].'">'.number_format($r['srp_price'],2,",",".").'</div>'.$tde;
$t.=$tds.number_format(getTotalFromAccounts($sdata,$r['id']),2,",",".").$tde;
$t.=$tds.number_format(getTotalFromAccounts($sdata,$r['id'],"quantity"),0,",",".").$tde;
$t.=$tds.'<div id="total_pred_sales_'.$r['id'].'"><div>'.$tde;
$t.=$tds.'<div id="total_pred_qty_'.$r['id'].'"><div>'.$tde;
$total_srp_price+=$r['srp_price'];
$total_ems_qty+=$r['ems_qty_in_stock'];
$total_ems_price+=$r['ems_qty_in_stock'];
$total_ems_value+=$r['ems_qty_in_stock']*$r['ems_price'];
$total_sales+=getTotalFromAccounts($sdata,$r['id']);
$total_qty+=getTotalFromAccounts($sdata,$r['id'],"quantity");
$ww=$db->query("select name,id from accounts where deleted='0' and core_client='1' order by name asc");
while($rr=$db->fetchByAssoc($ww)){
if($sdata[$rr['id']][$r['id']]['quantity']>0)$aprice=$sdata[$rr['id']][$r['id']]['sales']/$sdata[$rr['id']][$r['id']]['quantity'];
else $aprice=0;
if($aprice==0){
$rpb=$db->fetchByAssoc($db->query("select price from ecmpricebooks_ecmproducts where active=1 and account_id='".$rr['id']."' and ecmproduct_id='".$r['id']."' and deleted='0'"));
$aprice=$rpb['price'];
}
if($aprice==0){
$rpb=$db->fetchByAssoc($db->query("select price from ecmpricebooks_ecmproducts where ecmpricebook_id='7b840616-a226-4a15-a831-4d11f116995a' and ecmproduct_id='".$r['id']."' and deleted='0'"));
$aprice=$rpb['price'];
}
$t.=$tds.'<input size="2" type="hidden" name="avg_'.$r['id'].'_'.$rr['id'].'" id="avg_'.$r['id'].'_'.$rr['id'].'" value="'.round($aprice,2).'">'.number_format($aprice,2,",",".").$tde;
$t.=$tds.number_format($sdata[$rr['id']][$r['id']]['quantity'],0,",",".").$tde;
$t.=$tds.number_format($sdata[$rr['id']][$r['id']]['sales'],2,",",".").$tde;
$rrr=$db->fetchByAssoc($db->query("select value from ecmforecasts where ecmproduct_id='".$r['id']."' and account_id='".$rr['id']."'"));
$pred=(int)$rrr['value'];
$t.=$tds.'<input onclick="countPrediction(\''.$r['id'].'\',\''.$rr['id'].'\');" onblur="countPrediction(\''.$r['id'].'\',\''.$rr['id'].'\');" type="text" name="pred['.$r['id'].']['.$rr['id'].']" id="pred['.$r['id'].']['.$rr['id'].']" value="'.$pred.'" size="4">'.$tde;
$t.=$tds.'<div id="pred_'.$r['id'].'_'.$rr['id'].'" style="text-align:center">'.number_format($rrr['value']*round($aprice,2),2,",",".").'</div>'.$tde;
$totals_quantity[$rr['id']]+=$sdata[$rr['id']][$r['id']]['quantity'];
$totals_sales[$rr['id']]+=$sdata[$rr['id']][$r['id']]['sales'];
$totals_pred[$rr['id']]+=$rrr['value']*round($aprice,2);
$totals_pred_qty[$rr['id']]+=$rrr['value'];
}
if(getTotalFromAccounts($sdata,$r['id'],"quantity")>0)$avg_price=getTotalFromAccounts($sdata,$r['id'])/getTotalFromAccounts($sdata,$r['id'],"quantity");
else $avg_price=0;
if($avg_price==0){
$rpb=$db->fetchByAssoc($db->query("select price from ecmpricebooks_ecmproducts where ecmpricebook_id='7b840616-a226-4a15-a831-4d11f116995a' and ecmproduct_id='".$r['id']."' and deleted='0'"));
$avg_price=$rpb['price'];
}
$t.=$tds.'<input size="2" type="hidden" name="avg_'.$r['id'].'_new" id="avg_'.$r['id'].'_new" value="'.round($avg_price,2).'">'.number_format($avg_price,2,",",".").$tde;
$rrr=$db->fetchByAssoc($db->query("select value from ecmforecasts where ecmproduct_id='".$r['id']."' and account_id='new'"));
$pred=(int)$rrr['value'];
$t.=$tds.'<input onclick="countPrediction(\''.$r['id'].'\',\'new\');" onblur="countPrediction(\''.$r['id'].'\',\'new\');" type="text" name="pred['.$r['id'].'][new]" id="pred['.$r['id'].'][new]" value="'.$pred.'" size="4">'.$tde;
$t.=$tds.'<div id="pred_'.$r['id'].'_new" style="text-align:center">'.number_format($rrr['value']*round($avg_price,2),2,",",".").'</div>'.$tde;
$totals_pred["new"]+=$rrr['value']*round($avg_price,2);
$totals_pred_qty["new"]+=$rrr['value'];
$t.=$tre;
}
$t.='<tr style="font-weight:bold;">';
$t.=$tds_left."&nbsp;".$tde;
$t.=$tds_left."&nbsp;".$tde;
$t.=$tds."&nbsp;".$tde;
$t.=$tds.number_format($total_ems_qty,0,",",".").$tde;
$t.=$tds.number_format($total_ems_value/$total_ems_qty,2,",",".").$tde;
$t.=$tds.number_format($total_ems_value,2,",",".").$tde;
$t.=$tds.number_format($total_srp_price/$m,2,",",".").$tde;
$t.=$tds.number_format($total_sales,2,",",".").$tde;
$t.=$tds.number_format($total_qty,0,",",".").$tde;
$t.=$tds.'<div id="total_pred_sales"></div>'.$tde;
$t.=$tds.'<div id="total_pred_qty"></div>'.$tde;
$ww=$db->query("select name,id from accounts where deleted='0' and core_client='1' order by name asc");
while($rr=$db->fetchByAssoc($ww)){
$t.=$tds.@number_format($totals_sales[$rr['id']]/$totals_quantity[$rr['id']],2,",",".").$tde;
$t.=$tds.number_format($totals_quantity[$rr['id']],0,",",".").$tde;
$t.=$tds.number_format($totals_sales[$rr['id']],2,",",".").$tde;
$t.=$tds.'<div id="total_pred_qty_'.$rr['id'].'">'.number_format($totals_pred_qty[$rr['id']],0,",",".").'</div>'.$tde;
$t.=$tds.'<div id="total_pred_sales_'.$rr['id'].'">'.number_format($totals_pred[$rr['id']],2,",",".").'</div>'.$tde;
}
$t.=$tds.@number_format($totals_pred["new"]/$totals_pred_qty["new"],2,",",".").$tde;
$t.=$tds.number_format($totals_pred_qty["new"],0,",",".").$tde;
$t.=$tds.number_format($totals_pred["new"],2,",",".").$tde;
$t.=$tre;
$t.=$tbe;
$t.='</form><script>countTotals();</script>';
echo $t;
?>