PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/reports.php

https://bitbucket.org/rrobeson/ats_inventory_management_system
PHP | 322 lines | 243 code | 60 blank | 19 comment | 15 complexity | 5e1ca75c58f0d2178555679c88c78bf8 MD5 | raw file
  1. <?php
  2. class Reports_Controller extends Base_Controller {
  3. public $restful = true;
  4. public function __construct()
  5. {
  6. $this->filter('before', 'logged_in');
  7. $this->filter('before', 'read_only_admin_only')
  8. ->only(array(
  9. 'available_items'
  10. ));
  11. $this->filter('before', 'admin_only')
  12. ->except(array(
  13. 'available_items'
  14. ));
  15. }
  16. public function get_past_due_items()
  17. {
  18. if (!is_null(Item::past_due())) {
  19. $items = Item::past_due()->get();
  20. } else {
  21. $items = array();
  22. }
  23. return View::make('reports/_checked_out_items')
  24. ->with('title', 'Past Due Items')
  25. ->with('items', $items);
  26. }
  27. public function get_items_checked_out_today()
  28. {
  29. if (!is_null(Item::checked_out_today())) {
  30. $items = Item::checked_out_today()->get();
  31. } else {
  32. $items = array();
  33. }
  34. return View::make('reports/_checked_out_items')
  35. ->with('title', 'Items Checked Out Today')
  36. ->with('items', $items);
  37. }
  38. public function get_items_due_today()
  39. {
  40. if (!is_null(Item::due_today())) {
  41. $items = Item::due_today()->get();
  42. } else {
  43. $items = array();
  44. }
  45. return View::make('reports/_checked_out_items')
  46. ->with('title', 'Items Due Today')
  47. ->with('items', $items);
  48. }
  49. public function get_items_due_this_week()
  50. {
  51. if(!is_null(Item::due_this_week())) {
  52. $items = Item::due_this_week()->get();
  53. } else {
  54. $items = array();
  55. }
  56. return View::make('reports/_checked_out_items')
  57. ->with('title', 'Items Due This Week')
  58. ->with('items', $items);
  59. }
  60. public function get_current_holds()
  61. {
  62. $holds = Hold::current()
  63. ->order_by('end_date')
  64. ->get();
  65. return View::make('reports/current_holds')
  66. ->with('holds', $holds);
  67. }
  68. public function get_all_checkouts()
  69. {
  70. $transactions = CheckedOutTransaction::order_by('checked_out_date')
  71. ->order_by('renewal_due_date')
  72. ->order_by('due_date')
  73. ->get();
  74. return View::make('reports/_check_outs')
  75. ->with('title', 'All Checkouts')
  76. ->with('transactions', $transactions);
  77. }
  78. public function get_checkout_history()
  79. {
  80. $start_date = date('Y/m/d', strtotime('- 30 days'));
  81. $end_date = date('Y/m/d');
  82. return View::make('reports/checkout_history')
  83. ->with('start_date', $start_date)
  84. ->with('end_date', $end_date);
  85. }
  86. public function post_checkout_history()
  87. {
  88. $input = Input::get();
  89. $start_date = date('Y-m-d', strtotime($input['start_date']));
  90. $end_date = date('Y-m-d', strtotime($input['end_date']));
  91. $transactions = CheckedOutTransaction::order_by('checked_out_date')
  92. ->order_by('renewal_due_date')
  93. ->order_by('due_date')
  94. ->where('checked_out_date', '>=', $start_date)
  95. ->where('checked_out_date', '<=', $end_date)
  96. ->get();
  97. return View::make('reports/_check_outs')
  98. ->with('title', 'Checkouts between ' . $start_date . ' and ' . $end_date)
  99. ->with('transactions', $transactions);
  100. }
  101. public function get_items_checked_out_by_faculty()
  102. {
  103. $currently_checked_out = CheckedOutTransaction::still_checked_out()->get();
  104. $faculty_ids = [];
  105. // Only autocomplete for faculty that have items checked out.
  106. foreach($currently_checked_out as $c)
  107. {
  108. $f = $c->checked_out_to()->first();
  109. $faculty_ids[] = "&quot;" . $f->faculty_id . "&quot;";
  110. }
  111. $faculty_ids = array_unique($faculty_ids);
  112. if (count($faculty_ids) == 0) {
  113. Session::flash('success_message', 'There are currently no faculty with checked out items.');
  114. }
  115. return View::make('reports/items_checked_out_by_faculty')->with('faculty_ids', $faculty_ids);
  116. }
  117. public function post_items_checked_out_by_faculty()
  118. {
  119. $rules = array(
  120. 'faculty_id' => 'required|size:9|match:/^E.{8}$/|exists:faculty,faculty_id'
  121. );
  122. $input = Input::get();
  123. $validation = Validator::make($input, $rules);
  124. if($validation->fails()) {
  125. return Redirect::to_action('reports/items_checked_out_by_faculty')->with_input()->with_errors($validation);
  126. }
  127. $faculty = Faculty::where_faculty_id($input['faculty_id'])->first();
  128. $transactions = CheckedOutTransaction::still_checked_out()->where_checked_out_to($faculty->id)->order_by('due_date', 'asc')->get();
  129. if(count($transactions) == 0) {
  130. return Redirect::to_action('reports/items_checked_out_by_faculty')->with_input()->with('error_message', 'The selected faculty member currently has no checked out items.');
  131. }
  132. $items = [];
  133. foreach ($transactions as $t) {
  134. $dd = $t->renewal_due_date ? $t->renewal_due_date : $t->due_date;
  135. $dd = date('l F j, Y', strtotime($dd)) . ' at ' . date('h:ia', strtotime($dd));
  136. $items[] = array(
  137. 'item' => $t->item_checked_out()->first(),
  138. 'checked_out_to' => $t->checked_out_to()->first(),
  139. 'due_date' => $dd
  140. );
  141. }
  142. return View::make('reports/post_items_checked_out_by_faculty')->with('items', $items);
  143. }
  144. public function get_items_checked_out_per_day()
  145. {
  146. $counts = DB::query('SELECT Count(*) count, date_format(checked_out_date, \'%m-%d-%Y\') date FROM `checked_out_transactions` where date(checked_out_date) between curdate() - interval 30 day and curdate() group by date_format(checked_out_date, \'%m-%d-%Y\')');
  147. $values = [];
  148. /* Get the start and end date in the appropriate
  149. * format. This allows the inputs to display the
  150. * correct information.
  151. */
  152. if($counts) {
  153. $start_date = $counts[0]->date;
  154. $start_date = explode('-', $start_date);
  155. $start_date = $start_date[2] . '/' . $start_date[0] . '/' . $start_date[1];
  156. $end_date = $counts[count($counts) - 1]->date;
  157. $end_date = explode('-', $end_date);
  158. $end_date = $end_date[2] . '/' . $end_date[0] . '/' . $end_date[1];
  159. /* Store the counts and dates in a format
  160. * that nvd3 can use for graphing.
  161. */
  162. foreach($counts as $c)
  163. {
  164. $values[] = array('x' => $c->date , 'y' => $c->count);
  165. }
  166. } else {
  167. $start_date = date('Y-m-d');
  168. $end_date = date('Y-m-d');
  169. }
  170. $values = json_encode($values);
  171. return View::make('reports/items_checked_out_per_day')
  172. ->with('start_date', $start_date)
  173. ->with('end_date', $end_date)
  174. ->with('values', $values);
  175. }
  176. /*
  177. * This function is used for the AJAX request sent
  178. * by the HTTP GET version of this function. It returns
  179. * JSON with the values of the item counts between the
  180. * range of dates received in the request. The
  181. * graph is updated with this data
  182. */
  183. public function post_items_checked_out_per_day()
  184. {
  185. $input = Input::get();
  186. $start_date = date('Y-m-d', strtotime($input['start_date']));
  187. $end_date = date('Y-m-d', strtotime($input['end_date']));
  188. $counts = DB::query('SELECT Count(*) count, date_format(checked_out_date, \'%m-%d-%Y\') date FROM `checked_out_transactions` where date(checked_out_date) between ? and ? group by date_format(checked_out_date, \'%m-%d-%Y\')', array($start_date, $end_date));
  189. $values = [];
  190. foreach($counts as $c)
  191. {
  192. $values[] = array('x' => $c->date , 'y' => $c->count);
  193. }
  194. $values = json_encode($values);
  195. return $values;
  196. }
  197. public function get_number_of_items()
  198. {
  199. $query_items_by_category = <<<'EOQ'
  200. SELECT count(*) number_of_items, c.name FROM `items` i
  201. join item_categories c
  202. on i.category = c.id
  203. group by c.name
  204. EOQ;
  205. $query_items_by_status = <<<'EOQ'
  206. SELECT count(*) number_of_items, status FROM `items`
  207. group by status
  208. EOQ;
  209. $items_by_category = DB::query($query_items_by_category);
  210. $items_by_status = DB::query($query_items_by_status);
  211. $ibc_values = [];
  212. foreach($items_by_category as $ibc)
  213. {
  214. $ibc_values[] = array('key' => $ibc->name, 'y' => $ibc->number_of_items);
  215. }
  216. $ibs_values = [];
  217. foreach($items_by_status as $ibs)
  218. {
  219. $ibs_values[] = array('key' => $ibs->status, 'y' => $ibs->number_of_items);
  220. }
  221. $ibc_values = json_encode($ibc_values);
  222. $ibs_values = json_encode($ibs_values);
  223. return View::make('reports/number_of_items')
  224. ->with('ibc_values', $ibc_values)
  225. ->with('ibs_values', $ibs_values);
  226. }
  227. public function get_checked_out_items() {
  228. // Can't use Item::all_checked_out() here because
  229. // the ordering is not respected even if the order_by
  230. // is placed in the Item::all_checked_out() function
  231. // itself.
  232. $transactions = CheckedOutTransaction::still_checked_out()
  233. ->order_by('renewal_due_date')
  234. ->order_by('due_date')
  235. ->get();
  236. $items = [];
  237. foreach($transactions as $t) {
  238. $items[] = $t->item_checked_out()->first();
  239. }
  240. return View::make('reports/_checked_out_items')
  241. ->with('title', 'Items Currently Checked Out')
  242. ->with('items', $items);
  243. }
  244. public function get_available_items() {
  245. $items = Item::where_availability(true)->where('status', '=', 'good')->get();
  246. return View::make('reports/available_items')->with('items', $items);
  247. }
  248. public function get_damaged_lost_or_stolen() {
  249. $items = Item::where_status('damaged')
  250. ->or_where('status', '=', 'lost')
  251. ->or_where('status', '=', 'stolen')
  252. ->get();
  253. return View::make('reports/damaged_lost_or_stolen')
  254. ->with('items', $items);
  255. }
  256. public function get_surplus() {
  257. $items = Item::where_status('surplus')
  258. ->get();
  259. return View::make('reports/surplus')
  260. ->with('items', $items);
  261. }
  262. }