An easy point of sale system with automatic inventory tracking. https://netsyms.biz/apps/nickelbox/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

action.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. */
  7. require_once __DIR__ . "/required.php";
  8. require_once __DIR__ . "/lib/item.php";
  9. switch ($VARS['action']) {
  10. case "addtocart":
  11. $item = $VARS['item'];
  12. $qty = $VARS['qty'];
  13. if (!$binstack->has('items', ['AND' => ['itemid' => $item, 'price[>]' => 0]])) {
  14. header('Location: ./?page=cart&msg=invaliditem');
  15. die("Invalid item");
  16. }
  17. $cart = [];
  18. if (!empty($_SESSION['cart'])) {
  19. $cart = $_SESSION['cart'];
  20. }
  21. if (empty($cart[$item])) {
  22. $cart[$item] = $qty;
  23. } else {
  24. $cart[$item] += $qty;
  25. }
  26. $_SESSION['cart'] = $cart;
  27. header('Location: ./?page=cart&msg=itemadded');
  28. die();
  29. break;
  30. case "updatecart":
  31. $item = $VARS['item'];
  32. $qty = $VARS['qty'];
  33. $cart = [];
  34. if (!empty($_SESSION['cart'])) {
  35. $cart = $_SESSION['cart'];
  36. }
  37. $cart[$item] = $qty;
  38. if ($qty <= 0) {
  39. unset($cart[$item]);
  40. }
  41. $_SESSION['cart'] = $cart;
  42. header('Location: ./?page=cart&msg=itemupdated');
  43. break;
  44. case "login":
  45. $email = $VARS['email'];
  46. $password = $VARS['password'];
  47. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  48. header('Location: ./?page=login&msg=invalidemail');
  49. die("Invalid email address.");
  50. }
  51. if ($database->has('customers', ['email' => $email])) {
  52. $hash = $database->get('customers', 'password', ['email' => $email]);
  53. if (password_verify($password, $hash)) {
  54. $_SESSION['shop_account'] = $database->get('customers', ['customerid (id)', 'name', 'password (hashed_password)', 'email'], ['email' => $email]);
  55. header('Location: ./?page=account');
  56. die();
  57. } else {
  58. header('Location: ./?page=login&msg=badlogin');
  59. die("Bad login.");
  60. }
  61. } else {
  62. header('Location: ./?page=login&msg=badlogin');
  63. die("Bad login.");
  64. }
  65. break;
  66. case "logout":
  67. $_SESSION['shop_account'] = null;
  68. header('Location: ./');
  69. break;
  70. case "signup":
  71. $name = $VARS['name'];
  72. $email = $VARS['email'];
  73. $password = $VARS['password'];
  74. $phone = $VARS['phone'];
  75. if (empty($name) || empty($email) || empty($password)) {
  76. header('Location: ./?page=signup&msg=missingdata');
  77. die("Missing required data.");
  78. }
  79. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  80. header('Location: ./?page=signup&msg=invalidemail');
  81. die("Invalid email address.");
  82. }
  83. if ($database->has('customers', ['OR' => ['name' => $name, 'email' => $email]])) {
  84. header('Location: ./?page=signup&msg=accountinuse');
  85. die("Name or email already in use.");
  86. }
  87. if (empty($phone)) {
  88. $phone = null;
  89. }
  90. $database->insert('customers', ['name' => $name, 'email' => $email, 'password' => password_hash($password, PASSWORD_BCRYPT), 'phone' => $phone]);
  91. $_SESSION['shop_account'] = $database->get('customers', ['name', 'password (hashed_password)', 'email'], ['email' => $email]);
  92. header('Location: ./?page=account');
  93. die();
  94. break;
  95. }