forked from timschofield/webERP
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
StockUsageGraph.php: show zero counts within periods
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
Showing
4 changed files
with
168 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
INSERT INTO `config` (`confname`, `confvalue`) VALUES ('StockUsageShowZeroWithinPeriodRange', '0'); |
17afce4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
17afce4
There was a problem hiding this comment.
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)
17afce4
There was a problem hiding this comment.
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.
17afce4
There was a problem hiding this comment.
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!