PageRenderTime 370ms CodeModel.GetById 130ms app.highlight 76ms RepoModel.GetById 159ms app.codeStats 0ms

/controllers/stock.php

https://bitbucket.org/cpoppema/stockpiler
PHP | 423 lines | 365 code | 41 blank | 17 comment | 54 complexity | d1f9edd06a0c52fff43b4a3fc919993d MD5 | raw file
  1<?php
  2
  3    const MSG_SUCCESS_DELIVERY = 'Your products have been added to stock successfully!';
  4    const MSG_SUCCESS_PICKUP = 'Your products have been removed from stock successfully!';
  5    const MSG_SUCCESS_EDIT_STOCK = 'Your %s was updated successfully!';
  6
  7    const MSG_UNAUTHORIZED_DELIVERY = 'You are not authorized to make a delivery!';
  8    const MSG_UNAUTHORIZED_PICKUP = 'You are not authorized to do a pickup!';
  9    const MSG_UNAUTHORIZED_EDIT_STOCK = 'You are not authorized to edit deliveries and pickups!';
 10
 11
 12    function stock_history()
 13    {
 14        Security_Authorize();
 15
 16        $stock_changes = R::findAll('stock', 'order by id desc');
 17        $index = R::$adapter->getAffectedRows();
 18
 19        R::preload($stock_changes, array('product'));
 20
 21        foreach($stock_changes as $stock)
 22        {
 23            $css_class = ($stock['type'] == 'delivery') ? 'success' : 'error';
 24            if ($stock['iscanceled'] == 1)
 25            {
 26                $css_class .= ' warning';
 27            }
 28
 29            if ($stock['iscanceled'] == 1 && $_SESSION['CurrentUser_HideInacitve'])
 30            {
 31                $css_class .= ' hide';
 32            }
 33
 34            $body .= "<tr class='" . $css_class . "'>\n";
 35
 36            $body .= "<th>\n";
 37            $body .= $index;
 38            $body .= "</th>\n";
 39            $body .= "<td class='name'>\n";
 40            $body .= $stock['product']['name'];
 41            $body .= "</td>\n";
 42            $body .= "<td class='code'>\n";
 43            $body .= $stock['product']['code'];
 44            $body .= "</td>\n";
 45            $body .= "<td>\n";
 46            $body .= (($stock['type'] == 'delivery') ? '&plus;' : '&minus;') . '&nbsp;' . abs($stock['amount']);
 47            $body .= "</td>\n";
 48			if ($_SESSION["CurrentUser_IsReadOnly"] != "1")
 49			{
 50				$body .= "<td>\n";
 51				$body .= "<a href='" . option('base_uri') . "stock/" . $stock['id'] . "'>Edit</a>\n";
 52                if (isset($stock['order_id']))
 53                {
 54                    $body .= "| <a href='" . option('base_uri') . "orders/" . $stock['order_id'] . "'>View order</a>\n";
 55                }
 56				$body .= "</td>\n";
 57            } else {
 58                $body .= "<td>\n";
 59                if (isset($stock['order_id']))
 60                {
 61                    $body .= "<a href='" . option('base_uri') . "stock/" . $stock['order_id'] . "'>View order</a>\n";
 62                }
 63                $body .= "</td>\n";
 64            }
 65
 66            $body .= "</tr>\n";
 67
 68            $index--;
 69        }
 70
 71        if (R::$adapter->getAffectedRows() < 1)
 72        {
 73            $body .= "<tr>\n";
 74            $body .= "<td colspan='3'>You're a stock.</td>\n";
 75            $body .= "</tr>\n";
 76        }
 77
 78        // Get typeahead data
 79        $names = array();
 80        $codes = array();
 81        $products = R::findAll('product', 'order by name asc, code asc');
 82        foreach ($products as $product) {
 83            $names[] = $product['name'];
 84            $codes[] = $product['code'];
 85        }
 86        set("names", $names);
 87        set("codes", $codes);
 88        set("title", "Stock History");
 89        set("body", $body);
 90        return html("stock/history.php");
 91    }
 92
 93    function stock_delivery()
 94    {
 95        Security_Authorize();
 96
 97        if ($_SESSION['CurrentUser_IsReadOnly'] == "1")
 98        {
 99            header("Location: " . option('base_uri') . "&error=" . MSG_UNAUTHORIZED_DELIVERY);
100            exit;
101        }
102
103        // Get typeahead data
104        $names = array();
105        $codes = array();
106        $products = R::find('product', 'isdeleted = 0 or isdeleted is null order by name asc, code asc');
107        foreach ($products as $product) {
108            $names[] = array('value' => $product['id'], 'label' => $product['name']);  // nested array to allow exact match
109            $codes[] = $product['code'];
110        }
111        set("names", $names);
112        set("codes", $codes);
113        set("title", "Stock Delivery");
114        return html("stock/delivery.php");
115    }
116
117    function stock_delivery_post()
118    {
119        Security_Authorize();
120
121        if ($_SESSION['CurrentUser_IsReadOnly'] == "1")
122        {
123            header("Location: " . option('base_uri') . "&error=" . MSG_UNAUTHORIZED_DELIVERY);
124            exit;
125        }
126
127        if ($_POST['manualmode'] == '1')
128        {
129
130            if (empty($_POST['id']) || strlen(trim($_POST['id'])) == 0)
131            {
132                $_GET['error'] = "You need to provide at least a product!";
133            }
134            if ( !isset($_POST['amount']) || $_POST['amount'] <= 0)
135            {
136                $_GET['error'] = "Care to tell us how much you really want to add to stock?!";
137            } else {
138                $product = R::findOne('product', 'id = ? and (isdeleted = 0 or isdeleted is null)', array($_POST['id']));
139                if (R::$adapter->getAffectedRows() < 1)
140                {
141                    $_GET['error'] = "The product you entered was not found! Did you scan a previous deleted product perhaps?";
142                } else {
143                    // Add Delivery
144                    $stock = R::dispense('stock');
145                    $stock->product = $product;
146                    $stock->type = 'delivery';
147                    $stock->amount =  $_POST['amount'];
148                    $stock->reason = null;
149                    $stock->iscanceled = false;
150                    $id = R::store($stock);
151
152                    // Log adding delivery
153                    $entry = R::dispense('log');
154                    $entry->action = 'created';
155                    $entry->object = $stock->getID();
156                    $entry->object_type = $stock->getMeta('type');
157                    $entry->user_id = $_SESSION['CurrentUser_ID'];
158                    $entry->date = R::isoDateTime();
159                    R::store($entry);
160
161                    // Delivery added, go to list
162                    header("Location: " . option('base_uri') . "stock&success=" . MSG_SUCCESS_DELIVERY);
163                    exit;
164                }
165            }
166        } else {
167            if (empty($_POST['code']) || strlen(trim($_POST['code'])) == 0)
168            {
169                $_GET['error'] = "You need to provide at least the product's code!";
170            }
171            if ( !isset($_POST['amount']) || $_POST['amount'] <= 0)
172            {
173                $_GET['error'] = "Care to tell us how much you really want to add to stock?!";
174            } else {
175                $product = R::findOne('product', 'code = ? and (isdeleted = 0 or isdeleted is null)', array($_POST['code']));
176                if (R::$adapter->getAffectedRows() < 1)
177                {
178                    $_GET['error'] = "The code you entered was not found! Did you scan a previous deleted product perhaps?";
179                } else {
180                    // Add Delivery
181                    $stock = R::dispense('stock');
182                    $stock->product = $product;
183                    $stock->type = 'delivery';
184                    $stock->amount =  $_POST['amount'];
185                    $stock->reason = null;
186                    $stock->iscanceled = false;
187                    $id = R::store($stock);
188
189                    // Log adding delivery
190                    $entry = R::dispense('log');
191                    $entry->action = 'created';
192                    $entry->object = $stock->getID();
193                    $entry->object_type = $stock->getMeta('type');
194                    $entry->user_id = $_SESSION['CurrentUser_ID'];
195                    $entry->date = R::isoDateTime();
196                    R::store($entry);
197
198                    // Delivery added, go to list
199                    header("Location: " . option('base_uri') . "stock&success=" . MSG_SUCCESS_DELIVERY);
200                    exit;
201                }
202            }
203        }
204
205        // Get typeahead data
206        $names = array();
207        $codes = array();
208        $products = R::find('product', 'isdeleted = 0 or isdeleted is null order by name asc, code asc');
209        foreach ($products as $product) {
210            $names[] = array('value' => $product['id'], 'label' => $product['name']);  // nested array to allow exact match
211            $codes[] = $product['code'];
212        }
213        set("names", $names);
214        set("codes", $codes);
215
216        // Errors, go to form
217        set("title", "Stock Delivery");
218        return html("stock/delivery.php");
219    }
220
221    function stock_pickup()
222    {
223        Security_Authorize();
224
225        if ($_SESSION['CurrentUser_IsReadOnly'] == "1")
226        {
227            header("Location: " . option('base_uri') . "&error=" . MSG_UNAUTHORIZED_PICKUP);
228            exit;
229        }
230
231        // Get typeahead data
232        $names = array();
233        $codes = array();
234        $products = R::find('product', 'isdeleted = 0 or isdeleted is null order by name asc, code asc');
235        foreach ($products as $product) {
236            $names[] = array('value' => $product['id'], 'label' => $product['name']);  // nested array to allow exact match
237            $codes[] = $product['code'];
238        }
239        set("names", $names);
240        set("codes", $codes);
241        set("title", "Stock Pickup");
242        return html("stock/pickup.php");
243    }
244
245    function stock_pickup_post()
246    {
247        Security_Authorize();
248
249        if ($_SESSION['CurrentUser_IsReadOnly'] == "1")
250        {
251            header("Location: " . option('base_uri') . "&error=" . MSG_UNAUTHORIZED_PICKUP);
252            exit;
253        }
254
255        if ($_POST['manualmode'] == '1')
256        {
257
258            if (empty($_POST['id']) || strlen(trim($_POST['id'])) == 0)
259            {
260                $_GET['error'] = "You need to provide at least a product!";
261            }
262            if ( !isset($_POST['amount']) || $_POST['amount'] <= 0)
263            {
264                $_GET['error'] = "Care to tell us how much you really want to remove from stock?!";
265            } else {
266                $product = R::findOne('product', 'id = ? and (isdeleted = 0 or isdeleted is null)', array($_POST['id']));
267                if (R::$adapter->getAffectedRows() < 1)
268                {
269                    $_GET['error'] = "The product you entered was not found! Did you scan a previous deleted product perhaps?";
270                } else {
271                    // Add Pickup
272                    $stock = R::dispense('stock');
273                    $stock->product = $product;
274                    $stock->type = 'pickup';
275                    $stock->amount =  $_POST['amount'] * -1;
276                    $stock->reason = null;
277                    $stock->iscanceled = false;
278                    $id = R::store($stock);
279
280                    // Log adding pickup
281                    $entry = R::dispense('log');
282                    $entry->action = 'created';
283                    $entry->object = $stock->getID();
284                    $entry->object_type = $stock->getMeta('type');
285                    $entry->user_id = $_SESSION['CurrentUser_ID'];
286                    $entry->date = R::isoDateTime();
287                    R::store($entry);
288
289                    // Delivery added, go to list
290                    header("Location: " . option('base_uri') . "stock&success=" . MSG_SUCCESS_PICKUP);
291                    exit;
292                }
293            }
294        } else {
295            if (empty($_POST['code']) || strlen(trim($_POST['code'])) == 0)
296            {
297                $_GET['error'] = "You need to provide at least the code!";
298            }
299            if ( !isset($_POST['amount']) || $_POST['amount'] <= 0)
300            {
301                $_GET['error'] = "Care to tell us how much you really want to remove from stock?!";
302            } else {
303                $product = R::findOne('product', 'code = ? and (isdeleted = 0 or isdeleted is null)', array($_POST['code']));
304                if (R::$adapter->getAffectedRows() < 1)
305                {
306                    $_GET['error'] = "The code you entered was not found! Did you scan a previous deleted product perhaps?";
307                } else {
308                    // Add Pickup
309                    $stock = R::dispense('stock');
310                    $stock->product = $product;
311                    $stock->type = 'pickup';
312                    $stock->amount =  $_POST['amount'] * -1;
313                    $stock->reason = null;
314                    $stock->iscanceled = false;
315                    $id = R::store($stock);
316
317                    // Log adding pickup
318                    $entry = R::dispense('log');
319                    $entry->action = 'created';
320                    $entry->object = $stock->getID();
321                    $entry->object_type = $stock->getMeta('type');
322                    $entry->user_id = $_SESSION['CurrentUser_ID'];
323                    $entry->date = R::isoDateTime();
324                    R::store($entry);
325
326                    // Pickup added, go to list
327                    header("Location: " . option('base_uri') . "stock&success=" . MSG_SUCCESS_PICKUP);
328                    exit;
329                }
330            }
331        }
332
333        // Get typeahead data
334        $names = array();
335        $codes = array();
336        $products = R::find('product', 'isdeleted = 0 or isdeleted is null order by name asc, code asc');
337        foreach ($products as $product) {
338            $names[] = array('value' => $product['id'], 'label' => $product['name']);  // nested array to allow exact match
339            $codes[] = $product['code'];
340        }
341        set("names", $names);
342        set("codes", $codes);
343
344        // Errors, go to form
345        set("title", "Stock Pickup");
346        return html("stock/pickup.php");
347    }
348
349    function stock_edit()
350    {
351        Security_Authorize();
352
353        if ($_SESSION['CurrentUser_IsReadOnly'] == "1")
354        {
355            header("Location: " . option('base_uri') . "&error=" . MSG_UNAUTHORIZED_EDIT_STOCK);
356            exit;
357        }
358
359        $stock = R::load('stock', params('id'));
360        if (!$stock->id)
361        {
362            set("title", "Not Found");
363            set("type", "");
364            return html("error/notfound.php");
365        }
366
367        R::preload($stock, array('product'));
368        $stock['amount'] = abs($stock['amount']);
369        set("title", "Edit " . ucfirst($stock['type']));
370        set("stock", $stock);
371        return html("stock/edit.php");
372    }
373
374    function stock_edit_post()
375    {
376        Security_Authorize();
377
378        if ($_SESSION['CurrentUser_IsReadOnly'] == "1")
379        {
380            header("Location: " . option('base_uri') . "&error=" . MSG_UNAUTHORIZED_EDIT_STOCK);
381            exit;
382        }
383
384        $stock = R::load('stock', params('id'));
385        if (!$stock->id)
386        {
387            set("title", "Not Found");
388            set("type", "");
389            return html("error/notfound.php");
390        }
391
392        // Log editing delivery/pickup
393        $entry = R::dispense('log');
394        $entry->action = 'modified';
395        if ($_POST['iscanceled'] == 1)
396        {
397            if ($stock->iscanceled != 1)
398            {
399                $entry->action = 'canceled';
400            } else {
401                $entry->action = 'modified when canceled';
402            }
403        }
404        elseif ($stock->iscanceled == 1)
405        {
406            $entry->action = 'uncanceled';
407        }
408
409        // Edit delivery/pickup
410        $stock->amount = ($stock->type == 'delivery') ? $_POST['amount'] : $_POST['amount'] * -1;
411        $stock->iscanceled = ((isset($_POST['iscanceled'])) ? true : false);
412        R::store($stock);
413
414        // Continue log entry editing delivery/pickup
415        $entry->object = $stock->getID();
416        $entry->object_type = $stock->getMeta('type');
417        $entry->user_id = $_SESSION['CurrentUser_ID'];
418        $entry->date = R::isoDateTime();
419        R::store($entry);
420
421        header("Location: " . option('base_uri') . "stock&success=" . sprintf(MSG_SUCCESS_EDIT_STOCK, $stock['type']));
422        exit;
423    }