Skip to content

Commit

Permalink
StockUsageGraph.php: show zero counts within periods
Browse files Browse the repository at this point in the history
Forum query contribution by HDeriauFF
http://www.weberp.org/forum/showthread.php?tid=8418  (some minor adjustment applied to work with existing code/query handling)

Paul: Added a configurable system parameter to use the feature. (defaults to No/0) ... and apply a few static analysis item fixes reported using PHPStan.
  • Loading branch information
TurboPT committed Nov 15, 2020
1 parent e5816a6 commit 17afce4
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 88 deletions.
224 changes: 139 additions & 85 deletions StockUsageGraph.php
Original file line number Diff line number Diff line change
@@ -1,113 +1,167 @@
<?php

include('includes/session.php');
$result = DB_query("SELECT description FROM stockmaster WHERE stockid='" . trim(mb_strtoupper($_GET['StockID'])) . "'");
$myrow = DB_fetch_row($result);
$Result = DB_query("SELECT description FROM stockmaster WHERE stockid='" . trim(mb_strtoupper($_GET['StockID'])) . "'");
$MyRow = DB_fetch_row($Result);

include('includes/phplot/phplot.php');
$graph = new phplot(1000,500);
$graph->SetTitle($myrow[0] . ' ' . _('Usage'));
$graph = new PHPlot(1000, 500);
$graph->SetTitle($MyRow[0] . ' ' . _('Usage'));
$graph->SetXTitle(_('Month'));
$graph->SetYTitle(_('Quantity'));
$graph->SetBackgroundColor("wheat");
$graph->SetTitleColor("blue");
$graph->SetPlotType="bars";
$graph->SetPlotType('linepoints');
$graph->SetShading(5);
$graph->SetDrawYGrid(TRUE);
$graph->SetMarginsPixels(40,40,40,40);
$graph->SetMarginsPixels(40, 40, 40, 40);
$graph->SetDataType('text-data');

if($_GET['StockLocation']=='All'){
$sql = "SELECT periods.periodno,
periods.lastdate_in_period,
SUM(-stockmoves.qty) AS qtyused
FROM stockmoves INNER JOIN periods
ON stockmoves.prd=periods.periodno
INNER JOIN locationusers ON locationusers.loccode=stockmoves.loccode AND locationusers.userid='" . $_SESSION['UserID'] . "' AND locationusers.canview=1
WHERE (stockmoves.type=10 OR stockmoves.type=11 OR stockmoves.type=28)
AND stockmoves.hidemovt=0
AND stockmoves.stockid = '" . trim(mb_strtoupper($_GET['StockID'])) . "'
GROUP BY periods.periodno,
periods.lastdate_in_period
ORDER BY periodno LIMIT 24";
if($_GET['StockLocation'] == 'All') {
if (!empty($_SESSION['StockUsageShowZeroWithinPeriodRange'])) {
$CurrentPeriod = GetPeriod(Date($_SESSION['DefaultDateFormat']));

$SQL = "SELECT periods.periodno,
periods.lastdate_in_period,
SUM(CASE WHEN stockmoves.type IN (10, 11, 28)
AND stockmoves.hidemovt = 0
AND stockmoves.stockid = '" . $_GET['StockID'] . "'
THEN -stockmoves.qty ELSE 0 END) AS qtyused
FROM periods
LEFT JOIN stockmoves ON periods.periodno = stockmoves.prd
LEFT JOIN locationusers ON locationusers.loccode = stockmoves.loccode
AND locationusers.userid = '" . $_SESSION['UserID'] . "'
AND locationusers.canview = 1
WHERE periods.periodno > '" . ($CurrentPeriod - 24) . "'
GROUP BY periods.periodno,
periods.lastdate_in_period
ORDER BY periodno ASC
LIMIT 24";
} else {
$SQL = "SELECT periods.periodno,
periods.lastdate_in_period,
SUM(-stockmoves.qty) AS qtyused
FROM stockmoves
INNER JOIN periods ON stockmoves.prd = periods.periodno
INNER JOIN locationusers ON locationusers.loccode = stockmoves.loccode
AND locationusers.userid = '" . $_SESSION['UserID'] . "'
AND locationusers.canview = 1
WHERE stockmoves.type IN (10, 11, 28)
AND stockmoves.hidemovt = 0
AND stockmoves.stockid = '" . trim(mb_strtoupper($_GET['StockID'])) . "'
GROUP BY periods.periodno,
periods.lastdate_in_period
ORDER BY periodno
LIMIT 24";
}
} else {
$sql = "SELECT periods.periodno,
periods.lastdate_in_period,
SUM(-stockmoves.qty) AS qtyused
FROM stockmoves INNER JOIN periods
ON stockmoves.prd=periods.periodno
INNER JOIN locationusers ON locationusers.loccode=stockmoves.loccode AND locationusers.userid='" . $_SESSION['UserID'] . "' AND locationusers.canview=1
WHERE (stockmoves.type=10 Or stockmoves.type=11 OR stockmoves.type=28)
AND stockmoves.hidemovt=0
AND stockmoves.loccode='" . $_GET['StockLocation'] . "'
AND stockmoves.stockid = '" . trim(mb_strtoupper($_GET['StockID'])) . "'
GROUP BY periods.periodno,
periods.lastdate_in_period
ORDER BY periodno LIMIT 24";
if (!empty($_SESSION['StockUsageShowZeroWithinPeriodRange'])) {
$CurrentPeriod = GetPeriod(Date($_SESSION['DefaultDateFormat']));

$SQL = "SELECT periods.periodno,
periods.lastdate_in_period,
SUM(CASE WHEN stockmoves.type IN (10, 11, 28)
AND stockmoves.hidemovt = 0
AND stockmoves.loccode = '" . $_GET['StockLocation'] . "'
AND stockmoves.stockid = '" . $_GET['StockID'] . "'
THEN -stockmoves.qty ELSE 0 END) AS qtyused
FROM periods
LEFT JOIN stockmoves ON periods.periodno = stockmoves.prd
LEFT JOIN locationusers ON locationusers.loccode = stockmoves.loccode
AND locationusers.userid = '" . $_SESSION['UserID'] . "'
AND locationusers.canview = 1
WHERE periods.periodno > '" . ($CurrentPeriod - 24) . "'
GROUP BY periods.periodno,
periods.lastdate_in_period
ORDER BY periodno ASC
LIMIT 24";
} else {
$SQL = "SELECT periods.periodno,
periods.lastdate_in_period,
SUM(-stockmoves.qty) AS qtyused
FROM stockmoves
INNER JOIN periods ON stockmoves.prd = periods.periodno
INNER JOIN locationusers ON locationusers.loccode = stockmoves.loccode
AND locationusers.userid = '" . $_SESSION['UserID'] . "'
AND locationusers.canview = 1
WHERE stockmoves.type IN (10, 11, 28)
AND stockmoves.hidemovt = 0
AND stockmoves.loccode = '" . $_GET['StockLocation'] . "'
AND stockmoves.stockid = '" . trim(mb_strtoupper($_GET['StockID'])) . "'
GROUP BY periods.periodno,
periods.lastdate_in_period
ORDER BY periodno
LIMIT 24";
}
}
$MovtsResult = DB_query($sql);
if (DB_error_no() !=0) {
$Title = _('Stock Usage Graph Problem');
include ('includes/header.php');
echo _('The stock usage for the selected criteria could not be retrieved because') . ' - ' . DB_error_msg();
if ($debug==1){
echo '<br />' . _('The SQL that failed was') . $sql;
}
include('includes/footer.php');
exit;

$MovtsResult = DB_query($SQL);

if (DB_error_no() != 0) {
$Title = _('Stock Usage Graph Problem');
include ('includes/header.php');
echo _('The stock usage for the selected criteria could not be retrieved because') . ' - ' . DB_error_msg();
if ($debug == 1) {
echo '<br />' . _('The SQL that failed was') . $SQL;
}
include('includes/footer.php');
exit;
}
if (DB_num_rows($MovtsResult)==0){
$Title = _('Stock Usage Graph Problem');
include ('includes/header.php');
prnMsg(_('There are no movements of this item from the selected location to graph'),'info');
include('includes/footer.php');
exit;

if (DB_num_rows($MovtsResult) == 0) {
$Title = _('Stock Usage Graph Problem');
include ('includes/header.php');
prnMsg(_('There are no movements of this item from the selected location to graph'),'info');
include('includes/footer.php');
exit;
}

$UsageArray = array();
$NumberOfPeriodsUsage = DB_num_rows($MovtsResult);
if ($NumberOfPeriodsUsage!=24){
$graph->SetDataColors(
array("blue"), //Data Colors
array("black") //Border Colors
);
for ($i=1;$i++;$i<=$NumberOfPeriodsUsage){
$UsageRow = DB_fetch_array($MovtsResult);
if (!$UsageRow){
break;
} else {
$UsageArray[] = array(MonthAndYearFromSQLDate($UsageRow['lastdate_in_period']),$UsageRow['qtyused']);
}
}
}else {
$graph->SetDataColors(
array("blue","red"), //Data Colors
array("black") //Border Colors
);
for ($i=1;$i++;$i<=12){
$UsageRow = DB_fetch_array($MovtsResult);
if (!$UsageRow){
break;
}
$UsageArray[] = array(MonthAndYearFromSQLDate($UsageRow['lastdate_in_period']),$UsageRow['qtyused']);
}
for ($i=0,$i++;$i<=11;){
$UsageRow = DB_fetch_array($MovtsResult);
if (!$UsageRow){
break;
}
$UsageArray[$i][0] = MonthAndYearFromSQLDate($UsageRow['lastdate_in_period']);
$UsageArray[$i][2] = $UsageRow['qtyused'];
}

if ($NumberOfPeriodsUsage != 24) {
$graph->SetDataColors(
array("blue"), //Data Colors
array("black") //Border Colors
);

for ($i = 1; $i <= $NumberOfPeriodsUsage; $i++) {
$UsageRow = DB_fetch_array($MovtsResult);
if (!$UsageRow) {
break;
} else {
$UsageArray[] = array(MonthAndYearFromSQLDate($UsageRow['lastdate_in_period']),$UsageRow['qtyused']);
}
}
} else {
$graph->SetDataColors(
array("blue","red"), //Data Colors
array("black") //Border Colors
);

for ($i = 1; $i <= 12; $i++) {
$UsageRow = DB_fetch_array($MovtsResult);
if (!$UsageRow) {
break;
}
$UsageArray[] = array(MonthAndYearFromSQLDate($UsageRow['lastdate_in_period'], true), $UsageRow['qtyused']);
}

for ($i = 0; $i <= 11; $i++) {
$UsageRow = DB_fetch_array($MovtsResult);
if (!$UsageRow) {
break;
}
$UsageArray[$i][0] = MonthAndYearFromSQLDate($UsageRow['lastdate_in_period'], true);
$UsageArray[$i][2] = $UsageRow['qtyused'];
}
}
//$graph->SetDrawXGrid(TRUE);
$graph->SetDataValues($UsageArray);
$graph->SetDataColors(
array("blue","red"), //Data Colors
array("black") //Border Colors
array("blue","red"), //Data Colors
array("black") //Border Colors
);


//Draw it
$graph->DrawGraph();
?>
15 changes: 15 additions & 0 deletions SystemParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
$_POST['X_NumberOfPeriodsOfStockUsage'] < 1 OR $_POST['X_NumberOfPeriodsOfStockUsage'] > 12 ) {
$InputError = 1;
prnMsg(_('Financial period per year must be a number between 1 and 12'),'error');
} elseif (!in_array(intval($_POST['X_StockUsageShowZeroWithinPeriodRange']), [0, 1])) {
$InputError = 1;
prnMsg(_('Unexpected Show Zero Counts Within Stock Usage Graph Period Range value.'), 'error');
} elseif (mb_strlen($_POST['X_TaxAuthorityReferenceName']) >25) {
$InputError = 1;
prnMsg(_('The Tax Authority Reference Name must be 25 characters or less long'),'error');
Expand Down Expand Up @@ -201,6 +204,9 @@
if ($_SESSION['NumberOfPeriodsOfStockUsage'] != $_POST['X_NumberOfPeriodsOfStockUsage'] ) {
$sql[] = "UPDATE config SET confvalue = '".$_POST['X_NumberOfPeriodsOfStockUsage']."' WHERE confname = 'NumberOfPeriodsOfStockUsage'";
}
if ($_SESSION['StockUsageShowZeroWithinPeriodRange'] != $_POST['X_StockUsageShowZeroWithinPeriodRange']) {
$sql[] = "UPDATE config SET confvalue = '" . intval($_POST['X_StockUsageShowZeroWithinPeriodRange']) . "' WHERE confname = 'StockUsageShowZeroWithinPeriodRange'";
}
if ($_SESSION['Check_Qty_Charged_vs_Del_Qty'] != $_POST['X_Check_Qty_Charged_vs_Del_Qty'] ) {
$sql[] = "UPDATE config SET confvalue = '".$_POST['X_Check_Qty_Charged_vs_Del_Qty']."' WHERE confname = 'Check_Qty_Charged_vs_Del_Qty'";
}
Expand Down Expand Up @@ -775,6 +781,15 @@
echo '<option '.($_SESSION['NumberOfPeriodsOfStockUsage'] == $i?'selected="selected" ':'').'value="'.$i.'">' . $i . '</option>';
echo '</select></td><td>' . _('In stock usage inquiries this determines how many periods of stock usage to show. An average is calculated over this many periods') . '</td></tr>';

// StockUsageShowZeroWithinPeriodRange
echo '<tr style="outline: 1px solid"><td>' . _('Show Zero Counts Within Stock Usage Graph Period Range') . ':</td>
<td><select name="X_StockUsageShowZeroWithinPeriodRange">
<option ' . ($_SESSION['StockUsageShowZeroWithinPeriodRange'] ? 'selected="selected" ':'') . 'value="1">' . _('Yes') . '</option>
<option ' . (!$_SESSION['StockUsageShowZeroWithinPeriodRange'] ? 'selected="selected" ':'') . 'value="0">' . _('No') . '</option>
</select></td>
<td>' . _('Show periods having zero counts within Stock Usage Graph. Choosing yes may show a wider period range than expected.') . '</td>
</tr>';

//Show values on GRN
echo '<tr style="outline: 1px solid"><td>' . _('Show order values on GRN') . ':</td>
<td><select name="X_ShowValueOnGRN">
Expand Down
16 changes: 13 additions & 3 deletions includes/DateFunctions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function Is_date($DateEntry) {
} //end of Is_Date function


function MonthAndYearFromSQLDate($DateEntry) {
function MonthAndYearFromSQLDate($DateEntry, $useShortMonthAndYear = false) {

if (mb_strpos($DateEntry,'/')) {
$Date_Array = explode('/',$DateEntry);
Expand All @@ -95,9 +95,19 @@ function MonthAndYearFromSQLDate($DateEntry) {
$Date_Array[2]= mb_substr($Date_Array[2],0,2);
}

$MonthName = GetMonthText(date('n', mktime(0,0,0, (int)$Date_Array[1],(int)$Date_Array[2],(int)$Date_Array[0])));
return $MonthName . ' ' . date('Y', mktime(0,0,0, (int)$Date_Array[1],(int)$Date_Array[2],(int)$Date_Array[0]));
$MonthAndYear = '';
$timestamp = mktime(0,0,0, (int)$Date_Array[1], (int)$Date_Array[2], (int)$Date_Array[0]);

if ($useShortMonthAndYear) {
// 2-digit month and year: 04/20.
// Useful for Graphs with many plot references.
$MonthAndYear = date('m/y', $timestamp);
} else {
$MonthName = GetMonthText(date('n', $timestamp));
$MonthAndYear = $MonthName . ' ' . date('Y', $timestamp);
}

return $MonthAndYear;
}

function GetMonthText($MonthNumber){
Expand Down
1 change: 1 addition & 0 deletions sql/mysql/upgrade4.15.1-4.15.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO `config` (`confname`, `confvalue`) VALUES ('StockUsageShowZeroWithinPeriodRange', '0');

4 comments on commit 17afce4

@huguesderiau
Copy link

@huguesderiau huguesderiau commented on 17afce4 Nov 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StockUsageGraph-1 php
StockUsageGraph php

line 17, you might want to change
$graph->SetMarginsPixels(40, 40, 40, 40);
to
$graph->SetMarginsPixels(80, 40, 40, 40);

to give more room to y axis values and the word "quantity": y values are cut off afert 4 digits and the word "quantity" is not visible.
see my example with 40 and 80

@TurboPT
Copy link
Collaborator Author

@TurboPT TurboPT commented on 17afce4 Nov 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does it look with 60 as the first parameter, to have what looks like a little less whitespace towards the left margin?

Oh, and do you prefer the graph to be bars? ... in the forum posts, the graph images were linepoints.
Bars would not be a big deal, as it looked like that was the original intended value. The way it was previously assigned was wrong (line 14), so I matched the graph type based on your images. (or a newer PHPlot version changed how that was used, and simply defaulted to linepoints)

@huguesderiau
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

60 works, it leaves 3 pixels on the left of the dot of the "I" of quantity.

@TurboPT
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Margin adjusted, set the graph type back to the original bars value.

Thanks!

Please sign in to comment.