| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* This Source Code Form is subject to the terms of the Mozilla Public | 
					
						
							|  |  |  |  * License, v. 2.0. If a copy of the MPL was not distributed with this | 
					
						
							|  |  |  |  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Detect if loaded by the user or by PHP
 | 
					
						
							|  |  |  | if (count(get_included_files()) == 1) { | 
					
						
							|  |  |  |     define("LOADED", true); | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  |     define("LOADED", false); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require_once __DIR__ . "/../required.php"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Allow access with a download code, for mobile app and stuff
 | 
					
						
							|  |  |  | $date = date("Y-m-d H:i:s"); | 
					
						
							|  |  |  | if (isset($VARS['code']) && LOADED) { | 
					
						
							|  |  |  |     if (!$database->has('report_access_codes', ["AND" => ['code' => $VARS['code'], 'expires[>]' => $date]])) { | 
					
						
							|  |  |  |         dieifnotloggedin(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  |     dieifnotloggedin(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Delete old DB entries
 | 
					
						
							|  |  |  | $database->delete('report_access_codes', ['expires[<=]' => $date]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (LOADED) { | 
					
						
							|  |  |  |     if (isset($VARS['type']) && isset($VARS['format'])) { | 
					
						
							|  |  |  |         generateReport($VARS['type'], $VARS['format']); | 
					
						
							|  |  |  |         die(); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2018-09-07 18:29:25 -06:00
										 |  |  |         $Strings->get("invalid parameters"); | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |         die(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Get a 2d array of the items in the database. | 
					
						
							|  |  |  |  * @global type $database | 
					
						
							|  |  |  |  * @param array $filter Medoo WHERE clause. | 
					
						
							|  |  |  |  * @return string | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  | function getItemReport($filter = []): Report { | 
					
						
							| 
									
										
										
										
											2018-09-07 18:29:25 -06:00
										 |  |  |     global $database, $Strings; | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |     $items = $database->select( | 
					
						
							|  |  |  |             "items", [ | 
					
						
							|  |  |  |         "[>]locations" => ["locid"], | 
					
						
							|  |  |  |         "[>]categories" => ["catid"] | 
					
						
							|  |  |  |             ], [ | 
					
						
							|  |  |  |         "itemid", | 
					
						
							|  |  |  |         "name", | 
					
						
							|  |  |  |         "catname", | 
					
						
							|  |  |  |         "locname", | 
					
						
							|  |  |  |         "code1", | 
					
						
							|  |  |  |         "code2", | 
					
						
							|  |  |  |         "qty", | 
					
						
							|  |  |  |         "want", | 
					
						
							|  |  |  |         "userid", | 
					
						
							|  |  |  |         "text1", | 
					
						
							|  |  |  |         "text2", | 
					
						
							| 
									
										
										
										
											2018-04-10 01:25:07 -06:00
										 |  |  |         "text3", | 
					
						
							|  |  |  |         "cost", | 
					
						
							|  |  |  |         "price" | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |             ], $filter | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |     $report = new Report($Strings->get("Items", false)); | 
					
						
							|  |  |  |     $report->setHeader([ | 
					
						
							| 
									
										
										
										
											2018-09-07 18:29:25 -06:00
										 |  |  |         $Strings->get("itemid", false), | 
					
						
							|  |  |  |         $Strings->get("name", false), | 
					
						
							|  |  |  |         $Strings->get("category", false), | 
					
						
							|  |  |  |         $Strings->get("location", false), | 
					
						
							|  |  |  |         $Strings->get("code 1", false), | 
					
						
							|  |  |  |         $Strings->get("code 2", false), | 
					
						
							|  |  |  |         $Strings->get("quantity", false), | 
					
						
							|  |  |  |         $Strings->get("want", false), | 
					
						
							|  |  |  |         $Strings->get("Cost", false), | 
					
						
							|  |  |  |         $Strings->get("Price", false), | 
					
						
							|  |  |  |         $Strings->get("assigned to", false), | 
					
						
							|  |  |  |         $Strings->get("Description", false), | 
					
						
							|  |  |  |         $Strings->get("Notes", false), | 
					
						
							|  |  |  |         $Strings->get("Comments", false) | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |     ]); | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |     for ($i = 0; $i < count($items); $i++) { | 
					
						
							|  |  |  |         $user = ""; | 
					
						
							|  |  |  |         if (!is_null($items[$i]["userid"])) { | 
					
						
							| 
									
										
										
										
											2018-09-07 18:29:25 -06:00
										 |  |  |             $u = new User($items[$i]["userid"]); | 
					
						
							|  |  |  |             $user = $u->getName() . " (" . $u->getUsername() . ')'; | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |         $report->addDataRow([ | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |             $items[$i]["itemid"], | 
					
						
							|  |  |  |             $items[$i]["name"], | 
					
						
							|  |  |  |             $items[$i]["catname"], | 
					
						
							|  |  |  |             $items[$i]["locname"], | 
					
						
							|  |  |  |             $items[$i]["code1"], | 
					
						
							|  |  |  |             $items[$i]["code2"], | 
					
						
							|  |  |  |             $items[$i]["qty"], | 
					
						
							|  |  |  |             $items[$i]["want"], | 
					
						
							| 
									
										
										
										
											2018-04-10 01:25:07 -06:00
										 |  |  |             $items[$i]["cost"], | 
					
						
							|  |  |  |             $items[$i]["price"], | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |             $user, | 
					
						
							|  |  |  |             $items[$i]["text1"], | 
					
						
							|  |  |  |             $items[$i]["text2"], | 
					
						
							|  |  |  |             $items[$i]["text3"] | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |     return $report; | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  | function getCategoryReport(): Report { | 
					
						
							| 
									
										
										
										
											2018-09-07 18:29:25 -06:00
										 |  |  |     global $database, $Strings; | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |     $cats = $database->select('categories', [ | 
					
						
							|  |  |  |         'catid', | 
					
						
							|  |  |  |         'catname' | 
					
						
							|  |  |  |     ]); | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |     $report = new Report($Strings->get("Categories", false)); | 
					
						
							|  |  |  |     $report->setHeader([$Strings->get("id", false), $Strings->get("category", false), $Strings->get("item count", false)]); | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |     for ($i = 0; $i < count($cats); $i++) { | 
					
						
							|  |  |  |         $itemcount = $database->count('items', ['catid' => $cats[$i]['catid']]); | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |         $report->addDataRow([ | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |             $cats[$i]["catid"], | 
					
						
							|  |  |  |             $cats[$i]["catname"], | 
					
						
							|  |  |  |             $itemcount . "" | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |     return $report; | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  | function getLocationReport(): Report { | 
					
						
							| 
									
										
										
										
											2018-09-07 18:29:25 -06:00
										 |  |  |     global $database, $Strings; | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |     $locs = $database->select('locations', [ | 
					
						
							|  |  |  |         'locid', | 
					
						
							|  |  |  |         'locname', | 
					
						
							| 
									
										
										
										
											2017-12-26 17:01:52 -07:00
										 |  |  |         'loccode', | 
					
						
							|  |  |  |         'locinfo' | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |     ]); | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |     $report = new Report($Strings->get("Locations", false)); | 
					
						
							|  |  |  |     $report->setHeader([$Strings->get("id", false), $Strings->get("location", false), $Strings->get("code", false), $Strings->get("item count", false), $Strings->get("Description", false)]); | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |     for ($i = 0; $i < count($locs); $i++) { | 
					
						
							|  |  |  |         $itemcount = $database->count('items', ['locid' => $locs[$i]['locid']]); | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |         $report->addDataRow([ | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |             $locs[$i]["locid"], | 
					
						
							|  |  |  |             $locs[$i]["locname"], | 
					
						
							|  |  |  |             $locs[$i]["loccode"], | 
					
						
							| 
									
										
										
										
											2017-12-26 17:01:52 -07:00
										 |  |  |             $itemcount . "", | 
					
						
							|  |  |  |             $locs[$i]["locinfo"] | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |     return $report; | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  | function getReport($type): Report { | 
					
						
							|  |  |  |         switch ($type) { | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |         case "item": | 
					
						
							|  |  |  |             return getItemReport(); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case "category": | 
					
						
							|  |  |  |             return getCategoryReport(); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case "location": | 
					
						
							|  |  |  |             return getLocationReport(); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case "itemstock": | 
					
						
							|  |  |  |             return getItemReport(["AND" => ["qty[<]want", "want[>]" => 0]]); | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         default: | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |             return new Report("error", ["ERROR"], ["Invalid report type."]); | 
					
						
							| 
									
										
										
										
											2017-12-26 16:54:20 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function generateReport($type, $format) { | 
					
						
							| 
									
										
										
										
											2018-09-21 17:36:16 -06:00
										 |  |  |     $report = getReport($type); | 
					
						
							|  |  |  |     $report->output($format); | 
					
						
							|  |  |  | } |