Stock.php 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php
// Dobrado Content Management System
// Copyright (C) 2012 Malcolm Blaney
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

class Stock extends Base {

  public function Add($id) {

  }

  public function Callback() {
    $object = array();
26
    if (!$this->user->canEditPage) {
27
      $object["error"] = "You don't have permission to access stock.";
28 29 30 31 32 33 34 35 36 37 38
      return $object;
    }

    $mysqli = connect_db();
    $action = $mysqli->escape_string($_POST["action"]);

    if ($action == "list") {
      $object = $this->Products();
    }
    else if ($action == "edit") {
      $name = $mysqli->escape_string($_POST["name"]);
39
      $user = $mysqli->escape_string($_POST["supplier"]);
40 41 42 43 44
      $unit = $mysqli->escape_string($_POST["unit"]);
      $price = $mysqli->escape_string($_POST["price"]);
      $category = $mysqli->escape_string($_POST["category"]);
      $available = $mysqli->escape_string($_POST["available"]);

45
      if ($name === "") {
46 47
        $object["error"] = "No product name given";
      }
48
      else if ($user === "") {
49 50
        $object["error"] = "No supplier given";
      }
51
      else if ($unit === "") {
52 53
        $object["error"] = "No units given";
      }
54
      else if ($price === "") {
55 56 57 58 59 60 61 62
        $object["error"] = "No price given";
      }
      else {
        $query = 'INSERT INTO stock VALUES ("'.$name.'","'.$user.'","'.$unit.
          '","'.$price.'","'.$category.'","'.$available.'") '.
          'ON DUPLICATE KEY UPDATE user="'.$user.'", unit="'.$unit.'", price="'.
          $price.'", category="'.$category.'", available="'.$available.'"';
        if (!$mysqli->query($query)) {
63 64 65 66 67 68 69
          $this->Log('Stock->Callback 1: '.$mysqli->error);
        }
        $query = 'INSERT INTO stock_history VALUES ("'.$name.'","'.$user.'","'.
          $unit.'","'.$price.'","'.$category.'","'.$available.'", "edit", "'.
          $this->user->name.'","'.time().'")';
        if (!$mysqli->query($query)) {
          $this->Log('Stock->Callback 2: '.$mysqli->error);
70 71 72 73 74 75
        }
        $object["done"] = true;
      }
    }
    else if ($action == "remove") {
      $name = $mysqli->escape_string($_POST["name"]);
76 77 78 79
      if ($name === "") {
        $object["error"] = "No product name given";
      }
      else {
80 81
        $query = 'DELETE FROM stock WHERE name="'.$name.'"';
        if (!$mysqli->query($query)) {
82 83 84 85 86 87 88
          $this->Log('Stock->Callback 3: '.$mysqli->error);
        }
        $query = 'INSERT INTO stock_history VALUES ("'.$name.'","'.$user.'","'.
          $unit.'","'.$price.'","'.$category.'","'.$available.'", "remove", "'.
          $this->user->name.'","'.time().'")';
        if (!$mysqli->query($query)) {
          $this->Log('Stock->Callback 4: '.$mysqli->error);
89 90 91 92 93 94 95 96 97 98
        }
        $object = $this->Products();
      }
    }
    $mysqli->close();
    return $object;
  }

  public function CanAdd($page) {
    // Need admin privileges to add the stock module.
99
    if (!$this->user->canEditSite) return false;
100 101 102 103 104 105 106 107 108 109 110 111 112 113
    // Can only have one stock module on a page.
    return !$this->AlreadyOnPage("stock", $page);
  }

  public function CanEdit($id) {
    return false;
  }

  public function CanRemove($id) {
    return true;
  }

  public function Content($id) {
    return '<form id="stock-form">'.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
      '<div class="form-spacing">'.
        '<label for="stock-name-input">Product:</label>'.
        '<input id="stock-name-input" type="text" name="stock-name" size="15" '.
          'maxlength="100">'.
      '</div>'.
      '<div class="form-spacing">'.
        '<label for="stock-user-input">Supplier:</label>'.
        '<input id="stock-user-input" type="text" name="stock-user" size="15" '.
          'maxlength="50">'.
      '</div>'.
      '<div class="form-spacing">'.
        '<label for="stock-unit-select">Units:</label>'.
        '<select id="stock-unit-select" name="stock-unit">'.
          '<option value="each">each</option>'.
          '<option value="kg">kg</option>'.
          '<option value="g">g</option>'.
        '</select>'.
      '</div>'.
      '<div class="form-spacing">'.
        '<label for="stock-price-input">Price:</label>'.
        '<input id="stock-price-input" type="text" name="stock-price" '.
          'size="10" maxlength="12">'.
      '</div>'.
      '<div class="form-spacing">'.
        '<label for="stock-category-input">Category:</label>'.
        '<input id="stock-category-input" type="text" name="stock-category" '.
          'size="15" maxlength="100">'.
      '</div>'.
      '<div class="form-spacing">'.
        '<label for="stock-available-input">Available:</label>'.
        '<input id="stock-available-input" type="checkbox" '.
          'name="stock-available">'.
      '</div>'.
147 148 149 150 151 152 153 154 155
      '<button class="submit">submit</button>'.
      '<button class="remove">remove</button>'.
      '</form>';
  }

  public function Copy($id, $old_owner, $old_id) {

  }

156
  public function Factory($fn) {
157 158 159 160 161 162
    if (is_array($fn)) {
      $name = $fn[0];
      if ($name == "Supplier" && count($fn) == 2) {
        $user = $fn[1];
        return $this->Supplier($user);
      }
163 164 165 166
      if ($name == "AvailableProducts" && count($fn) == 2) {
        $array = $fn[1];
        return $this->AvailableProducts($array);
      }
167 168
      return;
    }
169
    if ($fn == "AvailableProducts") return $this->AvailableProducts();
170
    if ($fn == "LastUpdate") return $this->LastUpdate();
171 172
    if ($fn == "AllSuppliers") return $this->AllSuppliers();
    if ($fn == "Supplier") return $this->Supplier();
173 174
  }

175 176 177 178 179 180 181 182 183 184
  public function Group() {

  }

  public function IncludeScript() {
    return true;
  }

  public function Install($path) {
    // Append dobrado.stock.js to the existing dobrado.js file.
185
    // Note that the module is only available when logged in.
186 187 188 189 190 191 192 193 194 195 196 197
    $this->AppendScript($path, "dobrado.stock.js", false);
    $mysqli = connect_db();
    $query = 'CREATE TABLE IF NOT EXISTS stock ('.
      'name VARCHAR(100) NOT NULL,'.
      'user VARCHAR(50) NOT NULL,'.
      'unit ENUM("each", "g", "kg") NOT NULL,'.
      'price DECIMAL(8,2) NOT NULL,'.
      'category VARCHAR(100),'.
      'available TINYINT(1),'.
      'PRIMARY KEY(name)'.
      ') ENGINE=MyISAM';
    if (!$mysqli->query($query)) {
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
      $this->Log('Stock->Install 1: '.$mysqli->error);
    }
    $query = 'CREATE TABLE IF NOT EXISTS stock_history ('.
      'name VARCHAR(100) NOT NULL,'.
      'user VARCHAR(50) NOT NULL,'.
      'unit ENUM("each", "g", "kg") NOT NULL,'.
      'price DECIMAL(8,2) NOT NULL,'.
      'category VARCHAR(100),'.
      'available TINYINT(1),'.
      'action ENUM("edit", "remove"),'.
      'modified_by VARCHAR(50),'.
      'timestamp INT(10) UNSIGNED NOT NULL,'.
      'PRIMARY KEY(name, timestamp)'.
      ') ENGINE=MyISAM';
    if (!$mysqli->query($query)) {
      $this->Log('Stock->Install 2: '.$mysqli->error);
214
    }
215

216 217 218 219 220
    $mysqli->close();
    $site_style = array('"","#stock-form","background-color","#eeeeee"',
                        '"","#stock-form","border","1px solid #aaaaaa"',
                        '"","#stock-form","border-radius","2px"',
                        '"","#stock-form","padding","5px"',
221
                        '"","#stock-form label","width","5em"',
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
                        '"","#stock-form .submit","float","right"');
    $this->AddSiteStyle($site_style);
  }

  public function Placement() {
    return "middle";
  }

  public function Remove($id) {

  }

  public function SetContent($id, $us_content) {

  }

  public function Update() {
    // This is called when the version of the module is updated,
    // to provide a way to update or modify tables etc..
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    $mysqli = connect_db();
    $query = 'CREATE TABLE IF NOT EXISTS stock_history ('.
      'name VARCHAR(100) NOT NULL,'.
      'user VARCHAR(50) NOT NULL,'.
      'unit ENUM("each", "g", "kg") NOT NULL,'.
      'price DECIMAL(8,2) NOT NULL,'.
      'category VARCHAR(100),'.
      'available TINYINT(1),'.
      'action ENUM("edit", "remove"),'.
      'modified_by VARCHAR(50),'.
      'timestamp INT(10) UNSIGNED NOT NULL,'.
      'PRIMARY KEY(name, timestamp)'.
      ') ENGINE=MyISAM';
    if (!$mysqli->query($query)) {
      $this->Log('Stock->Update: '.$mysqli->error);
    }
    $mysqli->close();
258 259 260 261 262 263
  }

  public function UpdateScript($path) {
    $this->AppendScript($path, "dobrado.stock.js", false);
  }

264 265
  // Private functions below here ////////////////////////////////////////////

266
  private function AvailableProducts($array = true) {
267 268 269 270 271 272
    $object = array();
    $mysqli = connect_db();
    $query = 'SELECT name, user, unit, price, category FROM stock '.
      'WHERE available="1" ORDER BY name';
    if ($result = $mysqli->query($query)) {
      while ($stock = $result->fetch_assoc()) {
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
        // The grid module requires this data as an indexed array, but it's
        // also useful to have an associative array keyed on product names.
        if ($array) {
          $object[] = array("name" => $stock["name"],
                            "user" => $stock["user"],
                            "unit" => $stock["unit"],
                            "price" => (float)$stock["price"],
                            "category" => $stock["category"]);
        }
        else {
          $object[$stock["name"]] = array("user" => $stock["user"],
                                          "unit" => $stock["unit"],
                                          "price" => (float)$stock["price"],
                                          "category" => $stock["category"]);
        }
288 289 290 291
      }
      $result->close();
    }
    else {
292
      $this->Log('Stock->AvailableProducts: '.$mysqli->error);
293 294 295 296 297
    }
    $mysqli->close();
    return $object;
  }

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
  private function LastUpdate() {
    $mysqli = connect_db();
    $timestamp = "";
    $query = 'SELECT MAX(timestamp) AS timestamp FROM stock_history';
    if ($result = $mysqli->query($query)) {
      if ($stock_history = $result->fetch_assoc()) {
        $timestamp = $stock_history["timestamp"];
      }
      $result->close();
    }
    else {
      $this->Log('Stock->LastUpdate: '.$mysqli->error);
    }
    $mysqli->close();
    return $timestamp;
  }

315 316 317
  private function Products() {
    $object = array();
    $mysqli = connect_db();
318 319
    $query = 'SELECT name, user, unit, price, category, available FROM stock '.
      'ORDER BY name';
320 321
    if ($result = $mysqli->query($query)) {
      while ($stock = $result->fetch_assoc()) {
322 323
        $price = (float)$stock["price"];
        $price = number_format($price, 2, ".", "");
324 325 326
        $object[] = array("name" => $stock["name"],
                          "user" => $stock["user"],
                          "unit" => $stock["unit"],
327
                          "price" => $price,
328 329 330 331 332 333
                          "category" => $stock["category"],
                          "available" => (int)$stock["available"]);
      }
      $result->close();
    }
    else {
334
      $this->Log('Stock->Products: '.$mysqli->error);
335 336 337 338 339
    }
    $mysqli->close();
    return $object;
  }

340
  private function Supplier($user = "") {
341
    $object = array();
342 343 344
    if ($user === "") {
      $user = $this->user->name;
    }
345
    $mysqli = connect_db();
346
    $query = 'SELECT name FROM stock WHERE user="'.$user.'"';
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    if ($result = $mysqli->query($query)) {
      while ($stock = $result->fetch_assoc()) {
        $object[] = $stock["name"];
      }
      $result->close();
    }
    else {
      $this->Log('Stock->Supplier: '.$mysqli->error);
    }
    $mysqli->close();
    return $object;
  }

  private function AllSuppliers() {
    $object = array();
    $mysqli = connect_db();
    $query = 'SELECT user, name FROM stock ORDER BY user';
    if ($result = $mysqli->query($query)) {
      while ($stock = $result->fetch_assoc()) {
        // Initialise a new array for each supplier.
        if (!isset($object[$stock["user"]])) {
          $object[$stock["user"]] = array();
        }
        $object[$stock["user"]][] = $stock["name"];
      }
      $result->close();
    }
    else {
      $this->Log('Stock->Supplier: '.$mysqli->error);
    }
    $mysqli->close();
    return $object;
  }

381 382 383
}

?>