1<?php
2
3include('includes/session.php');
4$Result = DB_query("SELECT description FROM stockmaster WHERE stockid='" . trim(mb_strtoupper($_GET['StockID'])) . "'");
5$MyRow = DB_fetch_row($Result);
6
7include('includes/phplot/phplot.php');
8$graph = new PHPlot(1000, 500);
9$graph->SetTitle($MyRow[0] . ' ' . _('Usage'));
10$graph->SetXTitle(_('Month'));
11$graph->SetYTitle(_('Quantity'));
12$graph->SetBackgroundColor("wheat");
13$graph->SetTitleColor("blue");
14$graph->SetPlotType('bars');
15$graph->SetShading(5);
16$graph->SetDrawYGrid(TRUE);
17$graph->SetMarginsPixels(60, 40, 40, 40);
18$graph->SetDataType('text-data');
19
20if($_GET['StockLocation'] == 'All') {
21    if (!empty($_SESSION['StockUsageShowZeroWithinPeriodRange'])) {
22        $CurrentPeriod = GetPeriod(Date($_SESSION['DefaultDateFormat']));
23
24        $SQL = "SELECT periods.periodno,
25                       periods.lastdate_in_period,
26                       SUM(CASE WHEN stockmoves.type IN (10, 11, 28)
27                                 AND stockmoves.hidemovt = 0
28                                 AND stockmoves.stockid = '" . $_GET['StockID'] . "'
29                                THEN -stockmoves.qty ELSE 0 END) AS qtyused
30                  FROM periods
31             LEFT JOIN stockmoves ON periods.periodno = stockmoves.prd
32             LEFT JOIN locationusers ON locationusers.loccode = stockmoves.loccode
33                                    AND locationusers.userid = '" . $_SESSION['UserID'] . "'
34                                    AND locationusers.canview = 1
35                 WHERE periods.periodno > '" . ($CurrentPeriod - 24) . "'
36              GROUP BY periods.periodno,
37                       periods.lastdate_in_period
38              ORDER BY periodno ASC
39                 LIMIT 24";
40    } else {
41        $SQL = "SELECT periods.periodno,
42                       periods.lastdate_in_period,
43                       SUM(-stockmoves.qty) AS qtyused
44                  FROM stockmoves
45            INNER JOIN periods ON stockmoves.prd = periods.periodno
46            INNER JOIN locationusers ON locationusers.loccode = stockmoves.loccode
47                                        AND locationusers.userid = '" . $_SESSION['UserID'] . "'
48                                        AND locationusers.canview = 1
49                 WHERE stockmoves.type IN (10, 11, 28)
50                   AND stockmoves.hidemovt = 0
51                   AND stockmoves.stockid = '" . trim(mb_strtoupper($_GET['StockID'])) . "'
52              GROUP BY periods.periodno,
53                       periods.lastdate_in_period
54              ORDER BY periodno
55                 LIMIT 24";
56    }
57} else {
58    if (!empty($_SESSION['StockUsageShowZeroWithinPeriodRange'])) {
59        $CurrentPeriod = GetPeriod(Date($_SESSION['DefaultDateFormat']));
60
61        $SQL = "SELECT periods.periodno,
62                       periods.lastdate_in_period,
63                       SUM(CASE WHEN stockmoves.type IN (10, 11, 28)
64                                 AND stockmoves.hidemovt = 0
65                                 AND stockmoves.loccode = '" . $_GET['StockLocation'] . "'
66                                 AND stockmoves.stockid = '" . $_GET['StockID'] . "'
67                                THEN -stockmoves.qty ELSE 0 END) AS qtyused
68                  FROM periods
69             LEFT JOIN stockmoves ON periods.periodno = stockmoves.prd
70             LEFT JOIN locationusers ON locationusers.loccode = stockmoves.loccode
71                                    AND locationusers.userid = '" . $_SESSION['UserID'] . "'
72                                    AND locationusers.canview = 1
73                 WHERE periods.periodno > '" . ($CurrentPeriod - 24) . "'
74              GROUP BY periods.periodno,
75                       periods.lastdate_in_period
76              ORDER BY periodno ASC
77                 LIMIT 24";
78    } else {
79        $SQL = "SELECT periods.periodno,
80                       periods.lastdate_in_period,
81                       SUM(-stockmoves.qty) AS qtyused
82                  FROM stockmoves
83            INNER JOIN periods ON stockmoves.prd = periods.periodno
84            INNER JOIN locationusers ON locationusers.loccode = stockmoves.loccode
85                                    AND locationusers.userid = '" . $_SESSION['UserID'] . "'
86                                    AND locationusers.canview = 1
87                 WHERE stockmoves.type IN (10, 11, 28)
88                   AND stockmoves.hidemovt = 0
89                   AND stockmoves.loccode = '" . $_GET['StockLocation'] . "'
90                   AND stockmoves.stockid = '" . trim(mb_strtoupper($_GET['StockID'])) . "'
91              GROUP BY periods.periodno,
92                       periods.lastdate_in_period
93              ORDER BY periodno
94                 LIMIT 24";
95    }
96}
97
98$MovtsResult = DB_query($SQL);
99
100if (DB_error_no() != 0) {
101    $Title = _('Stock Usage Graph Problem');
102    include ('includes/header.php');
103    echo _('The stock usage for the selected criteria could not be retrieved because') . ' - ' . DB_error_msg();
104    if ($debug == 1) {
105        echo '<br />' . _('The SQL that failed was') . $SQL;
106    }
107    include('includes/footer.php');
108    exit;
109}
110
111if (DB_num_rows($MovtsResult) == 0) {
112    $Title = _('Stock Usage Graph Problem');
113    include ('includes/header.php');
114    prnMsg(_('There are no movements of this item from the selected location to graph'),'info');
115    include('includes/footer.php');
116    exit;
117}
118
119$UsageArray = array();
120$NumberOfPeriodsUsage = DB_num_rows($MovtsResult);
121
122if ($NumberOfPeriodsUsage != 24) {
123    $graph->SetDataColors(
124        array("blue"),  //Data Colors
125        array("black")  //Border Colors
126    );
127
128    for ($i = 1; $i <= $NumberOfPeriodsUsage; $i++) {
129        $UsageRow = DB_fetch_array($MovtsResult);
130        if (!$UsageRow) {
131            break;
132        } else {
133            $UsageArray[] = array(MonthAndYearFromSQLDate($UsageRow['lastdate_in_period']),$UsageRow['qtyused']);
134        }
135    }
136} else {
137    $graph->SetDataColors(
138        array("blue","red"),  //Data Colors
139        array("black")    //Border Colors
140    );
141
142    for ($i = 1; $i <= 12; $i++) {
143        $UsageRow = DB_fetch_array($MovtsResult);
144        if (!$UsageRow) {
145            break;
146        }
147        $UsageArray[] = array(MonthAndYearFromSQLDate($UsageRow['lastdate_in_period'], true), $UsageRow['qtyused']);
148    }
149
150    for ($i = 0; $i <= 11; $i++) {
151        $UsageRow = DB_fetch_array($MovtsResult);
152        if (!$UsageRow) {
153            break;
154        }
155        $UsageArray[$i][0] = MonthAndYearFromSQLDate($UsageRow['lastdate_in_period'], true);
156        $UsageArray[$i][2] = $UsageRow['qtyused'];
157    }
158}
159//$graph->SetDrawXGrid(TRUE);
160$graph->SetDataValues($UsageArray);
161$graph->SetDataColors(
162    array("blue","red"),  //Data Colors
163    array("black")    //Border Colors
164);
165
166//Draw it
167$graph->DrawGraph();
168