Files
crm.twinpol.com/modules/Home/StanyXls.php

171 lines
6.4 KiB
PHP
Raw Permalink Normal View History

2025-05-12 15:44:39 +00:00
<?php
if (!$_REQUEST['settings'] || $_REQUEST['settings']=="")
die('Błąd parametrów');
$tmp = base64_decode($_REQUEST['settings']);;
$settings = objectToArray(json_decode($tmp));
$module = $settings['module'];
$fields = $settings['fields'];
$table = $settings['table'];
$where = base64_decode($settings['where']);
;
$where = str_replace("like '", "like '%", $where);
$where = str_replace("(ecmstockstates.empty = '0' OR ecmstockstates.empty IS NULL)", "", $where);
$where = str_replace("AND ( ecmstockstates.empty = '0' OR ecmstockstates.empty IS NULL)", "", $where);
//vd($settings, true);
if($where!=''){
$where.=' and '.strtolower($module).'.deleted=0 ';
} else {
$where.=' '.strtolower($module).'.deleted=0 ';
}
if( $_SESSION[$module.'_order']!=''){
$where.=' order by '.$_SESSION[$module.'_order'];
}
$l = return_module_language('pl_pl', $module);
global $app_strings;
$l = array_merge($l, $app_strings);
$db=$GLOBALS['db'];
$query="select * from ".$table." where ".$where."";
function vd($v, $die = false) {
echo '<pre>'; var_dump($v); echo '</pre>';
if ($die == true) die();
}
function objectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}
$z=$db->query($query);
$txt="";
while($dane=$db->fetchByAssoc($z)){
$p = new EcmProduct();
$p->retrieve($dane['product_id']);
getConsignmentsDocs($dane['product_id'],$dane['stock_id'],$p->unit_id,$txt);
}
$fp = fopen('stanymagazynowe.csv', 'w');
echo $txt;
fwrite($fp, $txt);
fclose($fp);
function getConsignmentsDocs($prod_id, $stock_id, $jm,&$txt) {
$documents_map = array (
'EcmStockDocIns' => 'PZ',
'EcmStockDocCorrects' => 'KS',
'EcmStockDocMoves' => 'MM',
'EcmStockDocInsideIns' => 'PW'
);
global $db,$app_list_strings;
$query = "
select i.id, i.parent_id, i.parent_type,i.product_code,i.product_name, i.parent_name, i.price, i.quantity as in_qty, i.part_no, ifnull(sum(o.quantity),0) as out_qty, p.unit_id
from ecmstockoperations as i
left join ecmstockoperations as o
on i.id = o.in_id
inner join ecmproducts as p
on i.product_id = p.id
where
i.product_id='$prod_id' and
i.stock_id='$stock_id' and
i.used = '0' and
i.type='0'
group by i.id
order by i.date_entered;";
$res = $db->query ( $query );
$result = array ();
global $app_list_strings, $sugar_config;
while ( $row = $db->fetchByAssoc ( $res ) ) {
$availability = (floatval ( $row ['in_qty'] ) - floatval ( $row ['out_qty'] ));
if ($availability <= 0)
continue;
$tmp = array ();
if($row ['parent_type'] == 'EcmStockDocInsideIns'){
$accountQuery = "SELECT name FROM ecmstockdocinsideins WHERE id='" . $row['parent_id'] . "'";
$accountResult = $db->query($accountQuery);
if($accountResult->num_rows > 0){
$accountRow = $db->fetchByAssoc ( $accountResult );
$tmp ['account_name'] = $accountRow ['name'];
}else{
$tmp ['account_name'] = '';
}
}else{
$accountQuery = "SELECT parent_id, parent_name FROM " . strtolower($row ['parent_type']) ." WHERE id='" . $row['parent_id'] . "'";
$accountResult = $db->query($accountQuery);
if($accountResult->num_rows > 0){
$accountRow = $db->fetchByAssoc ( $accountResult );
$tmp ['account_name'] = html_entity_decode($accountRow['parent_name']);
}else{
$tmp ['account_name'] = '';
}
}
$tmp ['operation_id'] = $row ['id'];
$tmp ['parent_name'] =$documents_map [$row ['parent_type']] . ' ' . $row ['parent_name'];
$tmp ['part_no'] = $row['part_no'];
$tmp ['availability'] = $availability;
$tmp ['parent_id'] = $row ['parent_id'];
$tmp ['parent_type'] = $row ['parent_type'];
$tmp ['product_code'] = $row ['product_code'];
$tmp ['product_name'] = $row ['product_name'];
$tmp ['price'] = $row ['price'];
$tmp ['unit'] = $app_list_strings ['ecmproducts_unit_dom'] [$row ['unit_id']];
$tmp ['currency'] = $sugar_config ['default_currency_iso4217'];
$result [] = $tmp;
}
// number_format($tmp, 2, ",", ".")
$html = '
<div style="text-align: center; display: none"><font size="4">Struktura zapasu</font><br>
<table name="Pztki">
<tr>
<td style="text-align: center"><b>Dokument</b></td>
<td style="text-align: center"><b>Dostawca/Nazwa dokumentu</b></td>
<td style="text-align: center; width:10%"><b>Partia</b></td>
<td style="text-align: center; width:10%"><b>Ilość</b></td>
<td style="text-align: center; width:10%"><b>Cena</b></td>
<td style="text-align: center; width:10%"><b>Wartość</b></td>
</tr>';
$s = new EcmStock();
$s->retrieve($stock_id);
foreach($result as $key => $value){
$tmp = $value['availability']*$value['price'];
$txt.='"=""'.iconv('UTF-8//IGNORE','windows-1250',preg_replace("/[^A-ZłŁąĄęĘżŻ,:@.źŹ%ćĆńŃśŚóÓa-z0-9\-\/]/",' ',preg_replace("/\r\n|\r|\n/",' ',$value['product_code'])) ).'""";';
$txt.= '"=""'.iconv('UTF-8//IGNORE','windows-1250',preg_replace("/[^A-ZłŁąĄęĘżŻ,:@.źŹ%ćĆńŃśŚóÓa-z0-9\-\/]/",' ',preg_replace("/\r\n|\r|\n/",' ',$value['product_name'] ))).'""";';
$txt.='"=""'.iconv('UTF-8//IGNORE','windows-1250',preg_replace("/[^A-ZłŁąĄęĘżŻ,:@.źŹ%ćĆńŃśŚóÓa-z0-9\-\/]/",' ',preg_replace("/\r\n|\r|\n/",' ',$value['parent_name']))) .'""";';
$txt.= '"=""'.iconv('UTF-8//IGNORE','windows-1250',preg_replace("/[^A-ZłŁąĄęĘżŻ,:@.źŹ%ćĆńŃśŚóÓa-z0-9\-\/]/",' ',preg_replace("/\r\n|\r|\n/",' ',$value['account_name']))) .'""";';
$txt.= '"=""'.$value['part_no'] .'""";';
$txt.= '"=""'.number_format($value['availability'], $app_list_strings['ecmproducts_unit_dom_precision'][$jm], ",", " ") .'""";'; //FormatNumber(c.availability) + ' ' + c.unit
$txt.='"=""'.iconv('UTF-8//IGNORE','windows-1250',$value['unit']).'""";';
$txt.= '"=""'.number_format($value['price'], 2, ",", " ").'""";'; //FormatNumber(c.price) + ' ' + c.currency + ' / ' + c.unit
$txt.= '"=""'.number_format($tmp, 2, ",", " ") .'""";';
$txt.='"=""'.iconv('UTF-8//IGNORE','windows-1250',preg_replace("/[^A-ZłŁąĄęĘżŻ,:@.źŹ%ćĆńŃśŚóÓa-z0-9\-\/]/",' ',preg_replace("/\r\n|\r|\n/",' ',$s->name))).'""";'.PHP_EOL;
}
}
header("Location: stanymagazynowe.csv");
?>