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

325 lines
12 KiB
PHP

<?php
class RaportSprzedazy {
public $db;
public $date_start;
public $date_end;
public $c_date_start;
public $c_date_end;
public $category;
public $productGroup;
public $noWz;
public function __construct($date_start, $date_end, $status,$category,$noWz,$productGroup) {
$this->date_start = DateTime::createFromFormat ( 'd.m.Y', $date_start );
$this->date_end = DateTime::createFromFormat ( 'd.m.Y', $date_end );
$this->c_date_start = DateTime::createFromFormat ( 'd.m.Y', $date_start );
$this->c_date_end = DateTime::createFromFormat ( 'd.m.Y', $date_end );
$this->status = $status;
$this->products=[];
$this->category=$category;
$this->productGroup=$productGroup;
$this->tablica=[];
$this->total=0;
$this->noWz=$noWz;
$this->db = $GLOBALS ['db'];
}
public function resetDate() {
$this->date_start = $this->c_date_start;
$this->date_end = $this->c_date_end;
}
public function getDataForNormalType() {
if (count ( $this->status ) > 0) {
$status = " and s.status in ('" . implode ( "','", $this->status ) . "')";
} else {
$status = "";
}
if($this->category!=""){
$category_inner=" inner join ecmproductcategories_bean c on i.ecmproduct_id = c.bean_id";
$category_where= " and c.ecmproductcategory_id='".$this->category."'";
} else {
$category="";
$category_where= "";
}
if($this->productGroup!=""){
$product_group_where= " and p.group_id='".$this->productGroup."'";
} else {
$product_group_where= "";
}
if($this->noWz==true){
$no_wz=' and s.id not in (select ecmsale_id from ecmstockdocouts where ecmsale_id=s.id)';
} else {
$no_wz='';
}
$query = "select p.name,p.code,i.ecmproduct_id, sum(i.quantity) as quantity,s.delivery_date from ecmsaleitems i
inner join ecmsales s on s.id = i.ecmsale_id
inner join ecmproducts p on p.id=i.ecmproduct_id
" . $category_inner. "
where
s.delivery_date >= '" . $this->date_start->format ( "Y-m-d" ) . "' and s.type!='interval_order' and s.delivery_date <= '" . $this->date_end->format ( "Y-m-d" ) . "' " . $status . " " . $category_where. "
".$no_wz." ".$product_group_where."
group by i.ecmproduct_id,s.delivery_date order by p.code ASC";
$res = $this->db->query ( $query );
while ( $dane = $this->db->fetchByAssoc ( $res ) ) {
$this->tablica [$dane ['ecmproduct_id']] ['code'] = $dane ['code'];
$this->tablica [$dane ['ecmproduct_id']] ['name'] = $dane ['name'];
$this->tablica [$dane ['ecmproduct_id']] ['id'] = $dane ['ecmproduct_id'];
$ecmStockOperations = new EcmStockOperation();
$this->tablica [$dane ['ecmproduct_id']] ['state']=($ecmStockOperations->getRealStock($dane ['ecmproduct_id']));
if($this->tablica [$dane ['ecmproduct_id']] ['total']==""){
$this->tablica [$dane ['ecmproduct_id']] ['total']=$dane['quantity'];
} else {
$this->tablica [$dane ['ecmproduct_id']] ['total']=$this->tablica [$dane ['ecmproduct_id']] ['total']+$dane['quantity'];
}
$this->total+=$dane ['quantity'];
$this->tablica [$dane ['ecmproduct_id']] ['array'] [$dane ['delivery_date']] = $dane ['quantity'];
$this->products [] = $dane ['ecmproduct_id'];
}
$this->products = array_unique ( $this->products );
while ( $this->date_start <= $this->date_end ) {
foreach ( $this->products as $product ) {
if ($this->tablica [$product] ['array'] [$this->date_start->format ( "Y-m-d" )] == null) {
$this->tablica [$product] ['array'] [$this->date_start->format ( "Y-m-d" )] = 0;
}
}
$this->date_start->modify ( '+1 day' );
}
return $this->tablica;
}
public function getDataForIntervalType() {
if (count ( $this->status ) > 0) {
$status = " and s.status in ('" . implode ( "','", $this->status ) . "')";
} else {
$status = "";
}
if($this->category!=""){
$category_inner=" inner join ecmproductcategories_bean c on i.ecmproduct_id = c.bean_id";
$category_where= " and c.ecmproductcategory_id='".$this->category."'";
} else {
$category="";
$category_where= "";
}
if($this->productGroup!=""){
$product_group_where= " and p.group_id='".$this->productGroup."'";
} else {
$product_group_where= "";
}
$query = "select p.name,p.code,i.ecmproduct_id,s.type, sum(i.quantity) as quantity,s.sale_date_interval,
s.delivery_date from ecmsaleitems i
inner join ecmsales s on s.id = i.ecmsale_id
inner join ecmproducts p on p.id=i.ecmproduct_id
" . $category_inner. "
where
s.type='interval_order' " . $status . " " . $category_where. " " . $product_group_where. " group by i.ecmproduct_id,s.delivery_date order by i.name ASC";
$res = $this->db->query ( $query );
$this->resetDate ();
while ( $dane = $this->db->fetchByAssoc ( $res ) ) {
$delivery_date = DateTime::createFromFormat ( 'Y-m-d', $dane ['delivery_date'] );
if ($delivery_date < $this->date_start) {
while ( ($delivery_date < $this->date_start || $delivery_date < $this->date_end) ) {
$delivery_date->modify ( '+' . $dane ['sale_date_interval'] . ' month' );
if ($delivery_date <= $this->date_end && $delivery_date >= $this->date_start) {
$this->tablica [$dane ['ecmproduct_id']] ['code'] = $dane ['code'];
$this->tablica [$dane ['ecmproduct_id']] ['name'] = $dane ['name'];
$this->tablica [$dane ['ecmproduct_id']] ['id'] = $dane ['ecmproduct_id'];
if($this->tablica [$dane ['ecmproduct_id']] ['total']==""){
$this->tablica [$dane ['ecmproduct_id']] ['total']=$dane['quantity'];
} else {
$this->tablica [$dane ['ecmproduct_id']] ['total']=$this->tablica [$dane ['ecmproduct_id']] ['total']+$dane['quantity'];
}
if($this->tablica [$dane ['ecmproduct_id']] ['array'] [$delivery_date->format ( "Y-m-d" )] !=""){
$this->tablica [$dane ['ecmproduct_id']] ['array'] [$delivery_date->format ( "Y-m-d" )] = $this->tablica [$dane ['ecmproduct_id']] ['array'] [$delivery_date->format ( "Y-m-d" )]+ $dane ['quantity'];
} else {
$this->tablica [$dane ['ecmproduct_id']] ['array'] [$delivery_date->format ( "Y-m-d" )] = $dane ['quantity'];
}
$this->total+=$dane ['quantity'];
$this->products [] = $dane ['ecmproduct_id'];
}
}
}
if ($delivery_date >= $this->date_start && $delivery_date <= $this->date_end) {
if ($delivery_date <= $this->date_end && $delivery_date >= $this->date_start) {
$this->tablica [$dane ['ecmproduct_id']] ['code'] = $dane ['code'];
$this->tablica [$dane ['ecmproduct_id']] ['name'] = $dane ['name'];
$this->tablica [$dane ['ecmproduct_id']] ['id'] = $dane ['ecmproduct_id'];
if($this->tablica [$dane ['ecmproduct_id']] ['total']==""){
$this->tablica [$dane ['ecmproduct_id']] ['total']=$dane['quantity'];
} else {
$this->tablica [$dane ['ecmproduct_id']] ['total']=$this->tablica [$dane ['ecmproduct_id']] ['total']+$dane['quantity'];
}
if($this->tablica [$dane ['ecmproduct_id']] ['array'] [$delivery_date->format ( "Y-m-d" )] !=""){
$this->tablica [$dane ['ecmproduct_id']] ['array'] [$delivery_date->format ( "Y-m-d" )] = $this->tablica [$dane ['ecmproduct_id']] ['array'] [$delivery_date->format ( "Y-m-d" )]+ $dane ['quantity'];
} else {
$this->tablica [$dane ['ecmproduct_id']] ['array'] [$delivery_date->format ( "Y-m-d" )] = $dane ['quantity'];
}
$this->total+=$dane ['quantity'];
$this->products [] = $dane ['ecmproduct_id'];
}
while ( $delivery_date < $this->date_end ) {
$delivery_date->modify ( '+' . $dane ['sale_date_interval'] . ' month' );
if ($delivery_date <= $this->date_end && $delivery_date >= $this->date_start) {
$this->tablica [$dane ['ecmproduct_id']] ['code'] = $dane ['code'];
$this->tablica [$dane ['ecmproduct_id']] ['name'] = $dane ['name'];
$this->tablica [$dane ['ecmproduct_id']] ['id'] = $dane ['ecmproduct_id'];
if($this->tablica [$dane ['ecmproduct_id']] ['total']==""){
$this->tablica [$dane ['ecmproduct_id']] ['total']=$dane['quantity'];
} else {
$this->tablica [$dane ['ecmproduct_id']] ['total']=$this->tablica [$dane ['ecmproduct_id']] ['total']+$dane['quantity'];
}
if($this->tablica [$dane ['ecmproduct_id']] ['array'] [$delivery_date->format ( "Y-m-d" )] !=""){
$this->tablica [$dane ['ecmproduct_id']] ['array'] [$delivery_date->format ( "Y-m-d" )] = $this->tablica [$dane ['ecmproduct_id']] ['array'] [$delivery_date->format ( "Y-m-d" )]+ $dane ['quantity'];
} else {
$this->tablica [$dane ['ecmproduct_id']] ['array'] [$delivery_date->format ( "Y-m-d" )] = $dane ['quantity'];
}
$this->total+=$dane ['quantity'];
$this->products [] = $dane ['ecmproduct_id'];
}
}
}
}
return $this->tablica;
}
public static function increaseData($delivery_date, $month) {
$delivery_date->modify ( '+' . $month . ' month' );
}
public function buildDateRangeArray($date_start, $date_end) {
$array = [ ];
$this->date_start = DateTime::createFromFormat ( 'd.m.Y', $date_start );
$this->date_end = DateTime::createFromFormat ( 'd.m.Y', $date_end );
$i = 1;
while ( $this->date_start <= $this->date_end ) {
$array [$i] = $this->date_start->format ( "d.m" );
$this->date_start->modify ( '+1 day' );
$i ++;
}
return $array;
}
public static function getStatusArray() {
global $app_list_strings;
return $app_list_strings ['ecmsales_status_dom'];
}
public function getData(){
$this->products = array_unique ( $this->products );
while ( $this->date_start <= $this->date_end ) {
foreach ( $this->products as $product ) {
if ($this->tablica [$product] ['array'] [$this->date_start->format ( "Y-m-d" )] == null) {
$this->tablica [$product] ['array'] [$this->date_start->format ( "Y-m-d" )] = 0;
}
}
$this->date_start->modify ( '+1 day' );
}
foreach ( $this->tablica as $key => $produkt ) {
ksort ( $produkt ['array'] );
$this->tablica [$key] ['array'] = $produkt ['array'];
}
return $this->tablica;
}
}
if($_REQUEST['no_wz']){
$checked=true;
} else {
$checked=false;
}
global $mod_strings;
if ($_REQUEST ['date_from'] == "") {
$checked=true;
$_REQUEST ['date_from'] = date ( "d.m.Y" );
$date_from = $_REQUEST ['date_from'];
} else {
$date_from = $_REQUEST ['date_from'];
}
if ($_REQUEST ['date_to'] == "") {
$_REQUEST ['date_to'] = date ( "t.m.Y" );
$date_to = $_REQUEST ['date_to'];
} else {
$date_to = $_REQUEST ['date_to'];
}
if (isset ( $_REQUEST ['submit'] )) {
$c = new RaportSprzedazy ( $date_from, $date_to, $_REQUEST ['status'] ,$_REQUEST['product_category_id_basic'],$checked,$_REQUEST['product_group_id_basic']);
$data=null;
if($_REQUEST['order_type']==''){
$c->getDataForNormalType ();
$c->getDataForIntervalType ();
}
if($_REQUEST['order_type']=='E'){
$c->getDataForNormalType ();
}
if($_REQUEST['order_type']=='K'){
$c->getDataForIntervalType ();
}
$data=$c->getData();
$date_range = $c->buildDateRangeArray ( $date_from, $date_to );
}
$smarty = new Sugar_Smarty ();
$smarty->assign ( "DATE_RANGE", $date_range );
$smarty->assign ( "date_from", $date_from );
$smarty->assign ( "STATUS_ARRAY", RaportSprzedazy::getStatusArray () );
$smarty->assign ( "date_to", $date_to );
$smarty->assign ( "checked", $checked);
$smarty->assign ( "total", $c->total);
$smarty->assign ("product_category_name_basic",$_REQUEST['product_category_name_basic']);
$smarty->assign ("product_category_id_basic",$_REQUEST['product_category_id_basic']);
$smarty->assign ("product_group_name_basic",$_REQUEST['product_group_name_basic']);
$smarty->assign ("product_group_id_basic",$_REQUEST['product_group_id_basic']);
$smarty->assign ( "status_selected", $_REQUEST ['status'] );
$smarty->assign ( "dateFormat", "%d.%m.%Y" );
$smarty->assign ( "MOD", $mod_strings );
$smarty->assign ( "order_type",$_REQUEST['order_type']);
$smarty->assign ( "DATA", $data );
if($_REQUEST['to_pdf']=='1'){
include_once ("include/MPDF57/mpdf.php");
$p = new mPDF ( '', 'A4', null, 'helvetica', 10, 10, 5, 5, 5, 5 );
$content= $smarty->fetch ( 'modules/EcmReports/tpls/PDF/RaportZamowien2.html' );
$p->WriteHTML ( $content );
//echo $content;
// draw PDF
$p->Output ();
} else {
echo $smarty->display ( 'modules/EcmReports/tpls/RaportZamowien2.html' );
}
?>