isConfigurable = true; // dashlet is configurable
$this->hasScript = false; // dashlet has java ipt attached to it
if($current_user->id=='2e72f487-d92b-954e-f50c-528b10ce81c9'){
}
// if no custom title, use default
if(empty($def['title'])) $this->title = 'Raport stary crm ';
else $this->title = $def['title'];
}
function multisort($data,$keys){
if(count($data)>0){
foreach($data as $key => $row){
foreach($keys as $k){
$cols[$k['key']][$key] = $row[$k['key']];
}
}
$idkeys=@array_keys($data);
$i=0;
foreach($keys as $k){
if($i>0)$sort.=',';
$sort.='$cols['.$k['key'].']';
if($k['sort'])$sort.=',SORT_'.strtoupper($k['sort']);
if($k['type'])$sort.=',SORT_'.strtoupper($k['type']);
$i++;
}
$sort.=',$idkeys';
$sort='@array_multisort('.$sort.');';
eval($sort);
foreach($idkeys as $idkey){
$result[$idkey]=$data[$idkey];
}
return $result;
}
}
function salesPercentRange($date1,$date2){
$w=$GLOBALS['db']->query("select sum(value_wz) as swz,sum(value_sale) as ssale from ecmproductcategories_realisation where date>='".$date1."' and date<='".$date2."'");
$r=$GLOBALS['db']->fetchByAssoc($w);
if($r['ssale']>0)return @100*$r['swz']/$r['ssale'];
}
function salesPercent($date){
$w=$GLOBALS['db']->query("select * from ecmsales where delivery_date like '".$date."%' and deleted='0'");
while($r=$GLOBALS['db']->fetchByAssoc($w)){
$rsale=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select sum(quantity*price) as qty from ecmsaleitems where ecmsale_id='".$r['id']."'"));
$rwz=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select id from ecmstockdocouts where so_id='".$r['id']."'"));
$rwzi=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select sum(quantity*selling_price) as qty from ecmstockdocoutitems where ecmstockdocout_id='".$rwz['id']."'"));
$ecmstockdocout_id=$rwz['id'];
if($ecmstockdocout_id){
$sum_wz_qty+=$rwzi['qty'];
$sum_sale_qty+=$rsale['qty'];
}
$qty_diff=$rwzi['qty']-$rsale['qty'];
$diff_minus=$diff_plus=0;
$rst=$rsale['qty'];
$rwt=$rwzi['qty'];
if($ecmstockdocout_id){
$wsale=$GLOBALS['db']->query("select quantity,price,code,ecmproduct_id from ecmsaleitems where deleted='0' and ecmsale_id='".$r['id']."' order by code");
while($rsale=$GLOBALS['db']->fetchByAssoc($wsale)){
$qty=0;
$wwz=$GLOBALS['db']->query("select quantity,selling_price from ecmstockdocoutitems where deleted='0' and ecmproduct_id='".$rsale['ecmproduct_id']."' and ecmstockdocout_id='".$ecmstockdocout_id."'");
while($rwz=$GLOBALS['db']->fetchByAssoc($wwz)){
$qty+=$rwz['quantity']*$rwz['selling_price'];
}
if($rsale['quantity']*$rsale['price']>$qty)$diff_minus+=$qty-$rsale['quantity']*$rsale['price'];
if($rsale['quantity']*$rsale['price']<$qty)$diff_plus+=$qty-$rsale['quantity']*$rsale['price'];
$sum[$rsale['ecmproduct_id']]['wz']+=$qty;
$sum[$rsale['ecmproduct_id']]['sale']+=$rsale['quantity']*$rsale['price'];
}
}
}
if($sum_sale_qty)return @100*$sum_wz_qty/$sum_sale_qty;
}
function getPrediction($product_id,$date,$arr){
$mmm=(int)date("m")-1;
if($mmm==0)$mmm=1;
$e=explode("-",$date);
$w=$GLOBALS['db']->query("select value from ecmsalesreports_predictions_cat where type='sales' and year='".$e[0]."' and month='".(int)$e[1]."' and bean_id='".$product_id."'");
if($GLOBALS['db']->getRowCount($w)>0){
while($r=$GLOBALS['db']->fetchByAssoc($w)){
$sales+=$r['value'];
}
}
else{
$s=getSum($arr,$e[0],$product_id);
$sales+=$s['total']/$mmm;
}
return ($sales);
}
function getOrdersByDate($date){
if(!$no_inv)$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select sum(i.price*i.quantity) as s from ecmsaleitems as i inner join ecmsales as e on e.id=i.ecmsale_id inner join ecmstockdocouts as w on w.so_id=e.id inner join ecminvoiceouts as inv on inv.wz_id=w.id inner join ecmproducts as p on i.ecmproduct_id=p.id where e.deleted='0' and i.deleted='0' and inv.register_date like '".$date."%' and p.product_category_id!='' and p.product_category_id is not null and e.type='sales_order' and e.status!='s10'"));
return $r['s'];
}
function getOrdersBydateNoInv($date){
$w=$GLOBALS['db']->query("select i.price*i.quantity as s,e.id as id,e.register_date as d from ecmsaleitems as i inner join ecmsales as e on e.id=i.ecmsale_id inner join ecmproducts as p on i.ecmproduct_id=p.id where e.deleted='0' and i.deleted='0' and e.delivery_date like '".$date."%' and p.product_category_id!='' and p.product_category_id is not null and e.type='sales_order' and e.status!='s10'");
while($r=$GLOBALS['db']->fetchByAssoc($w)){
if($GLOBALS['db']->getRowCount($GLOBALS['db']->query("select id from ecmstockdocouts where so_id='".$r['id']."' and register_date>='".$r['d']."' and deleted='0'"))==0)$s+=$r['s'];
}
return $s;
}
function getOrdersBydateNoInv1($date){
$w=$GLOBALS['db']->query("select
sum(CASE WHEN w.id is null THEN i.price*i.quantity ELSE 0 END) as s
from ecmsaleitems as i
inner join ecmsales as e on e.id=i.ecmsale_id
inner join ecmproducts as p on i.ecmproduct_id=p.id
left join ecmstockdocouts as w on w.so_id=e.id
where
w.so_id is null and
(w.deleted='0' or w.deleted is null) and
e.deleted='0' and
i.deleted='0' and
e.delivery_date like '".$date."%' and
p.product_category_id!='' and
p.product_category_id is not null and
e.type='sales_order' and
e.status!='s10'");
$r=$GLOBALS['db']->fetchByAssoc($w);
return $r['s'];
}
function getSalesByDate1($date,$cor=0){
if($cor==1){
$w=$GLOBALS['db']->query("select i.ecminvoiceoutitem_id,i.price,i.quantity from ecminvoiceoutitems as i inner join ecminvoiceouts as e on e.id=i.ecminvoiceout_id where i.deleted='0' and e.deleted='0' and e.register_date like '".$date."%' and e.type='correct' and e.status='accepted'");
while($r=$GLOBALS['db']->fetchByAssoc($w)){
$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select price,quantity,purchase_price from ecminvoiceoutitems where id='".$r['ecminvoiceoutitem_id']."'"));
$value_sale+=$r['quantity']*$r['price']-$rr['quantity']*$rr['price'];
$value_pur+=$r['quantity']*$rr['purchase_price']-$rr['quantity']*$rr['purchase_price'];
}
}
else{
$w=$GLOBALS['db']->query("select sum(i.quantity*i.price) as sale,sum(i.quantity*i.purchase_price) as purchase from ecminvoiceoutitems as i inner join ecminvoiceouts as e on e.id=i.ecminvoiceout_id inner join ecmstockdocouts as w on w.id=e.wz_id inner join ecmsales as s on s.id=w.so_id where i.deleted='0' and e.deleted='0' and e.register_date like '".$date."%' and e.type!='correct' and e.status='accepted' and s.status!='s10' and w.status='accepted'");
$r=$GLOBALS['db']->fetchByAssoc($w);
$value_sale=$r['sale'];
$value_pur=$r['purchase'];
}
return array("sale"=>$value_sale,"purchase"=>$value_pur);
}
function getSalesByDate($date,$cor=0){
if($cor==1)$type="correct";
else $type="normal";
$w=$GLOBALS['db']->query("
select
sum(
CASE WHEN e.type!='correct'
THEN
CASE WHEN e.currency_value is null or e.currency_value=''
THEN
i.price*i.quantity
ELSE
i.price*i.quantity*e.currency_value
END
ELSE
CASE WHEN ci.price is null or ci.quantity is null
THEN
0
ELSE
i.price*i.quantity-ci.price*ci.quantity
END
END
) as sales,
sum(
CASE WHEN e.type!='correct'
THEN
i.purchase_price*i.quantity
ELSE
CASE WHEN ci.quantity is null
THEN
0
ELSE
ci.purchase_price*i.quantity-ci.purchase_price*ci.quantity
END
END
) as purchase
from ecminvoiceoutitems as i
left join ecminvoiceoutitems as ci on i.ecminvoiceoutitem_id=ci.id
inner join ecminvoiceouts as e on e.id=i.ecminvoiceout_id
left join ecmstockdocouts as w on e.wz_id=w.id
left join ecmproducts as p on p.id=i.ecmproduct_id
where i.deleted='0' and e.deleted='0' and e.register_date like '".$date."%' and e.type='".$type."'");
$r=$GLOBALS['db']->fetchByAssoc($w);
return array("sale"=>$r['sales'],"purchase"=>$r['purchase']);
}
function display() {
global $current_user;
$ss = new Sugar_Smarty();
$optionsArray = $this->loadOptions();
//$ss->assign('account_id',$optionsArray['account_id']);
$h=0;
$ss->assign('id', $this->id);
$ss->assign('id', $this->id);
$ss->assign('sales_year_title', $optionsArray['sales_year_title']);
$ss->assign('sales_title',$optionsArray['sales_title']);
$ss->assign('sales_year_title',$optionsArray['sales_year_title']);
$ss->assign('percent_title',$optionsArray['percent_title']);
$ss->assign('quotes_title',$optionsArray['quotes_title']);
$ss->assign('orders_title',$optionsArray['orders_title']);
$ss->assign('stocks_title',$optionsArray['stocks_title']);
$ss->assign('products_title',$optionsArray['products_title']);
$ss->assign('new_products_title',$optionsArray['new_products_title']);
$ss->assign('payments_title',$optionsArray['payments_title']);
$tds1='
Period: '.$optionsArray['month'].'.'.$optionsArray['year'].' '.$tbs;
$t.=$trs;
$t.=$tds1c;
$t.="Indeks";
$t.=$tde;
$t.=$tds1;
$t.="Nazwa";
$t.=$tde;
$t.=$tds1;
$t.="Sprzedana ilość";
$t.=$tde;
$t.=$tds1;
$t.="Sprzedaż PLN";
$t.=$tde;
$t.=$tds1;
$t.="Margines";
$t.=$tde;
$t.=$tds1;
$t.="Margines PLN";
$t.=$tde;
$t.=$tds1;
$t.="fak ilo";
$t.=$tde;
$t.=$tds1;
$t.="Zam ilo";
$t.=$tde;
$t.=$tds1;
$t.="Prognoza sprzedaży";
$t.=$tde;
$t.=$tre;
$w=$GLOBALS['db']->query("select e.currency_value,i.ecmproduct_id,i.price,i.quantity,i.purchase_price,e.status,e.type,e.ecminvoiceout_id,i.ecminvoiceoutitem_id as item from ecminvoiceoutitems as i inner join ecminvoiceouts as e on e.id=i.ecminvoiceout_id where e.register_date like '".$optionsArray['year'].'-'.$optionsArray['month']."%' and e.deleted='0' and e.status='accepted'");
$w=$GLOBALS['db']->query("
select
sum(
CASE WHEN e.type!='correct'
THEN
CASE WHEN e.currency_value is null or e.currency_value=''
THEN
i.price*i.quantity
ELSE
i.price*i.quantity*e.currency_value
END
ELSE
CASE WHEN ci.price is null or ci.quantity is null
THEN
0
ELSE
i.price*i.quantity-ci.price*ci.quantity
END
END
) as sales,
sum(
CASE WHEN e.type!='correct'
THEN
i.purchase_price*i.quantity
ELSE
CASE WHEN ci.quantity is null
THEN
0
ELSE
i.purchase_price*i.quantity-i.purchase_price*ci.quantity
END
END
) as purchase,
sum(
CASE WHEN e.type!='correct'
THEN
i.quantity
ELSE
CASE WHEN ci.quantity is null
THEN
0
ELSE
i.quantity-ci.quantity
END
END
) as quantity,
p.code,
p.name,
p.id,
p.ordered,
p.ems_qty_in_stock
from ecminvoiceoutitems as i
left join ecminvoiceoutitems as ci on i.ecminvoiceoutitem_id=ci.id
inner join ecminvoiceouts as e on e.id=i.ecminvoiceout_id
inner join ecmproducts as p on p.id=i.ecmproduct_id
where
i.deleted='0' and
(ci.deleted='0' or ci.deleted is null) and
e.deleted='0' and
e.register_date like '".$optionsArray['year'].'-'.$optionsArray['month']."%' and
e.status='accepted'
group by p.id");
echo mysql_error();
while($r=$GLOBALS['db']->fetchByAssoc($w)){
$arr[$r['id']]['code']=$r['code'];
$arr[$r['id']]['name']=$r['name'];
$arr[$r['id']]['ordered']=$r['ordered'];
$arr[$r['id']]['ems_qty_in_stock']=$r['ems_qty_in_stock'];
$arr[$r['id']]['id']=$r['id'];
$arr[$r['id']]['sales']+=$r['sales'];
$arr[$r['id']]['margin']+=$r['purchase'];
$arr[$r['id']]['quantity']+=$r['quantity'];
/*if($r['type']!="correct"){
if($_REQUEST['die']==1 && $p['code']=="RE01332")echo $r['price']." ".$r['quantity']." ".$r['purchase_price']." ";
$arr[$p['id']]['sales']+=$currency_value*$r['price']*$r['quantity'];
$arr[$p['id']]['margin']+=$r['purchase_price']*$r['quantity'];
$arr[$p['id']]['quantity']+=$r['quantity'];
}
else{
$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select price,quantity,purchase_price from ecminvoiceoutitems where id='".$r['item']."'"));
$arr[$p['id']]['sales']+=$currency_value*($r['price']*$r['quantity']-$rr['price']*$rr['quantity']);
$arr[$p['id']]['margin']+=$rr['purchase_price']*($r['quantity']-$rr['quantity']);
$arr[$p['id']]['quantity']+=$r['quantity']-$rr['quantity'];
}*/
}
$arr=$this->multisort($arr,array(array('key'=>'sales','sort'=>'desc')));
//echo '
';
$rst=0;
$rwt=0;
$ww=$GLOBALS['db']->query("select c.id,c.name,t.value_sale,t.value_wz from ecmproductcategories as c inner join ecmproductcategories_realisation as t on t.product_category_id=c.id where t.date='".$optionsArray['year'].'-'.$optionsArray['month']."' and c.deleted='0' order by c.name asc");
echo mysql_error();
while($rr=$GLOBALS['db']->fetchByAssoc($ww)){
$k=$rr['id'];
$rwt+=$rr['value_wz'];
$rst+=$rr['value_sale'];
if($rr['value_wz']<$rr['value_sale'])$color="red";
elseif($rr['value_wz']>$rr['value_sale'])$color="green";
else $color="black";
$ri=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select sum(s.quantity*s.price) as s from ecmstockstates as s inner join ecmproducts as p on p.id=s.product_id where p.product_category_id='".$rr['id']."' and p.deleted='0' and s.deleted='0'"));
$inv_value+=$ri['s'];
$t.='
';
$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select value_sale from ecmproductcategories_realisation where date='".$optionsArray['year'].'-'.$optionsArray['month']."' and product_category_id='diff_minus'"));
$t.='
'.number_format($r['value_sale'],2,",",".").'
';
$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select value_sale from ecmproductcategories_realisation where date='".$optionsArray['year'].'-'.$optionsArray['month']."' and product_category_id='diff_plus'"));
$t.='
'.number_format($r['value_sale'],2,",",".").'
';
$t.='
';
$as.=$t;
}
$a.=$percent;
if($optionsArray['quotes']){
$send=0;
$won=0;
$new_send=0;
$new_won=0;
$w=$GLOBALS['db']->query("select status,parent_id from ecmquotes where deleted='0' and register_date like '".$optionsArray['year'].'-'.$optionsArray['month']."%'");
while($r=$GLOBALS['db']->fetchByAssoc($w)){
if($r['status']=="s50" || $r['status']=="s30"){
$send++;
if($GLOBALS['db']->getRowCount($GLOBALS['db']->query("select id from ecmquotes where (status='s50' or status='s30') and parent_id='".$r['parent_id']."' and register_date<'".$optionsArray['year'].'-'.$optionsArray['month']."' and deleted='0'"))==0){
$new_send++;
}
}
if($r['status']=="s60"){
$won++;
if($GLOBALS['db']->getRowCount($GLOBALS['db']->query("select id from ecmquotes where status='s60' and parent_id='".$r['parent_id']."' and register_date<'".$optionsArray['year'].'-'.$optionsArray['month']."' and deleted='0'"))==0){
$new_won++;
}
}
}
$t='
'.$optionsArray['quotes_title'].'
Okres: '.$optionsArray['month'].'.'.$optionsArray['year'].' '.$tbs;
$t.=$trs;
$t.=$tds1;
$t.='Sent';
$t.=$tde;
$t.=$tds;
$t.=$send;
$t.=$tde;
$t.=$tre;
$t.=$trs;
$t.=$tds1;
$t.='New sent';
$t.=$tde;
$t.=$tds;
$t.=$new_send;
$t.=$tde;
$t.=$tre;
/*$t.=$trs;
$t.=$tds1;
$t.='Won';
$t.=$tde;
$t.=$tds;
$t.=$won;
$t.=$tde;
$t.=$tre;
$t.=$trs;
$t.=$tds1;
$t.='New won';
$t.=$tde;
$t.=$tds;
$t.=$new_won;
$t.=$tde;
$t.=$tre;
*/
$t.=$tbe;
$as.=$t;
}
if($optionsArray['orders']){
$date=date("Y-m-d",mktime()-3600*24);
if(date("l",strtotime($date))=="Sunday")$date=date("Y-m-d",mktime()-3*3600*24);
if(date("l",strtotime($date))=="Saturday")$date=date("Y-m-d",mktime()-2*3600*24);
if(date("l",strtotime(date("Y-m-d")))=="Friday")$today1=date("Y-m-d",mktime()+3*3600*24);
else $today1=date("Y-m-d",mktime()+3600*24);
$today=date("Y-m-d");
$nt=$GLOBALS['db']->getRowCount($GLOBALS['db']->query("select e.id from ecmsales as e inner join ecmstockdocouts as w on w.so_id=e.id inner join ecminvoiceouts as inv on inv.wz_id=w.id where e.deleted='0' and inv.register_date like '".$today."' and e.type='sales_order' and e.status!='s10'"));
$valuet=$this->getOrdersByDate($today);
$valuets=$this->getOrdersByDateNoInv($today1);
$n=$GLOBALS['db']->getRowCount($GLOBALS['db']->query("select e.id from ecmsales as e inner join ecmstockdocouts as w on w.so_id=e.id inner join ecminvoiceouts as inv on inv.wz_id=w.id where e.deleted='0' and inv.register_date like '".$date."' and e.type='sales_order' and e.status!='s10'"));
$value=$this->getOrdersByDate($date);
$values=$this->getOrdersByDateNoInv($date);
$ny=$GLOBALS['db']->getRowCount($GLOBALS['db']->query("select e.id from ecmsales as e inner join ecmstockdocouts as w on w.so_id=e.id inner join ecminvoiceouts as inv on inv.wz_id=w.id where e.deleted='0' and inv.register_date like '".date("Y")."%' and e.type='sales_order' and e.status!='s10'"));
$valuey=$this->getOrdersByDate(date("Y"));
//$valueys=$this->getOrdersByDateNoInv(date("Y"));
$nm=$GLOBALS['db']->getRowCount($GLOBALS['db']->query("select e.id from ecmsales as e inner join ecmstockdocouts as w on w.so_id=e.id inner join ecminvoiceouts as inv on inv.wz_id=w.id where e.deleted='0' and inv.register_date like '".date("Y-m")."%' and e.type='sales_order' and e.status!='s10'"));
$valuem=$this->getOrdersByDate(date("Y-m"));
$valuems=$this->getOrdersByDateNoInv(date("Y-m"));
$valuet_sale=$this->getSalesBydate($today);
$value_sale=$this->getSalesBydate($date);
$valuey_sale=$this->getSalesBydate(date("Y"));
$valuem_sale=$this->getSalesBydate(date("Y-m"));
$valuet_salec=$this->getSalesBydate(date("Y-m-d"),1);
$value_salec=$this->getSalesBydate($date,1);
$valuey_salec=$this->getSalesBydate(date("Y"),1);
$valuem_salec=$this->getSalesBydate(date("Y-m"),1);
$t='