Commit 1f0e8b68 authored by Cédric Houbart's avatar Cédric Houbart
Browse files

Fix stocks module (removed thresholds, waiting for attributes)

parent be908e1c
......@@ -268,6 +268,8 @@ class ProductsService {
$stmtcat->execute(array(":id" => $id));
$stmtstk = $pdo->prepare("DELETE FROM STOCKLEVEL WHERE PRODUCT = :id");
$stmtstk->execute(array(":id" => $id));
$stmtstk2 = $pdo->prepare("DELETE FROM STOCKCURRENT WHERE PRODUCT = :id");
$stmtstk2->execute(array(":id" => $id));
// Update reference with garbage to break unicity constraint
$garbage = "_deleted_" . \md5(\time());
$stmt = $pdo->prepare("UPDATE PRODUCTS SET DELETED = " . $db->true()
......
......@@ -62,9 +62,10 @@ class StocksService {
. "WHERE LOCATION = :loc";
$stmtQty = $pdo->prepare($sqlQty);
$stmtQty->bindParam(':loc', $locationId);
$stmtQty->execute();
while ($row = $stmtQty->fetch()) {
$prdId = $row['PRODUCT'];
if (!isset($qties[$row['PRODUCT']])) {
if (!isset($qties[$prdId])) {
$qties[$prdId] = array();
}
$qties[$prdId][$row['ATTR']] = $row['UNITS'];
......
......@@ -26,9 +26,6 @@ $stocks = FALSE;
$discounts = FALSE;
$attributes = FALSE;
$modules = \Pasteque\get_loaded_modules(\Pasteque\get_user_id());
if (in_array("base_stocks", $modules)) {
$stocks = TRUE;
}
if (in_array("product_discounts", $modules)) {
$discounts = TRUE;
}
......@@ -38,30 +35,6 @@ if (in_array("product_attributes", $modules)) {
$message = NULL;
$error = NULL;
function saveStock($newId = NULL) {
$level = new \stdClass();
if (isset($_POST['stockId'])) {
$level->id = $_POST['stockId'];
}
if ($newId === NULL) {
$level->product = $_POST['id'];
} else {
$level->product = $newId;
}
$level->security = NULL;
if (isset($_POST['security']) && $_POST['security'] != "") {
$level->security = $_POST['security'];
}
$level->max = NULL;
if (isset($_POST['max']) && $_POST['max'] != "") {
$level->max = $_POST['max'];
}
if (isset($_POST['stockId'])) {
\Pasteque\StocksService::updateLevel($level);
} else {
\Pasteque\StocksService::createLevel($level);
}
}
if (isset($_POST['id'])) {
if (isset($_POST['reference']) && isset($_POST['label'])
......@@ -94,7 +67,6 @@ if (isset($_POST['id'])) {
$taxCatId, $visible, $scaled, $_POST['priceBuy'], $attr,
$_POST['barcode'], $img != null,
$discount_enabled, $discount_rate);
if ($stocks) { saveStock(); }
if (\Pasteque\ProductsService::update($prd, $img)) {
$message = \i18n("Changes saved");
} else {
......@@ -131,7 +103,6 @@ if (isset($_POST['id'])) {
$img !== null, $discount_enabled, $discount_rate);
$id = \Pasteque\ProductsService::create($prd, $img);
if ($id !== FALSE) {
if ($stocks) { saveStock($id); }
$message = \i18n("Product saved. <a href=\"%s\">Go to the product page</a>.", PLUGIN_NAME, \Pasteque\get_module_url_action(PLUGIN_NAME, 'product_edit', array('id' => $id)));
} else {
$error = \i18n("Unable to save changes");
......@@ -222,20 +193,6 @@ if ($stocks === TRUE && $product != NULL) {
<?php } ?>
</fieldset>
<?php if ($stocks) { ?>
<fieldset>
<legend><?php \pi18n("Stock", "base_stocks"); ?></legend>
<?php if ($level != NULL) { ?><input type="hidden" name="stockId" value="<?php echo $level->id; ?>" /><?php } ?>
<div class="row">
<label for="security"><?php \pi18n("Security threshold", "base_stocks"); ?></label>
<input id="security" type="numeric" name="security" <?php if ($level !== NULL && $level->security !== NULL) echo 'value="' . $level->security . '"'; ?> />
</div>
<div class="row">
<label for="max-stock"><?php \pi18n("Maximum", "base_stocks"); ?></label>
<input id="max-stock" type="numeric" name="max" <?php if ($level !== NULL && $level->max !== NULL) echo 'value="' . $level->max . '"'; ?> />
</div>
</fieldset>
<?php } ?>
<div class="row actions">
<?php \Pasteque\form_save(); ?>
</div>
......
......@@ -31,11 +31,11 @@ foreach ($locations as $location) {
$currLocation = null;
if (isset($_POST['location'])) {
$currLocation = $_POST['location'];
} else {
$currLocation = $locations[0]->id;
}
$products = \Pasteque\ProductsService::getAll();
$stocks = \Pasteque\StocksService::getQties($currLocation);
$products = \Pasteque\ProductsService::getAll(true);
$levels = \Pasteque\StocksService::getLevels($currLocation);
?>
<h1><?php \pi18n("Inventory", PLUGIN_NAME); ?></h1>
......@@ -57,18 +57,25 @@ $levels = \Pasteque\StocksService::getLevels($currLocation);
<th><?php \pi18n("Product.reference"); ?></th>
<th><?php \pi18n("Product.label"); ?></th>
<th><?php \pi18n("Quantity"); ?></th>
<th><?php \pi18n("Security threshold", PLUGIN_NAME); ?></th>
<th><?php \pi18n("Maximum", PLUGIN_NAME); ?></th>
</tr>
</thead>
<tbody>
<?php
$par = FALSE;
foreach ($products as $product) {
foreach ($levels as $level) {
$par = !$par;
$qty = isset($stocks[$product->id]) ? $stocks[$product->id] : 0;
$security = isset($levels[$product->id]) ? $levels[$product->id]->security : NULL;
$max = isset($levels[$product->id]) ? $levels[$product->id]->max : NULL;
$prdRef = "";
$prdLabel = "";
foreach ($products as $product) {
if ($product->id == $level->productId) {
$prdLabel = $product->label;
$prdRef = $product->reference;
break;
}
}
$security = $level->security;
$max = $level->max;
$qty = $level->qty !== null ? $level->qty : 0;
$class = "";
$help = "";
if ($security !== NULL && $qty < $security) {
......@@ -84,12 +91,10 @@ if ($qty < 0) {
}
?>
<tr class="row-<?php echo $par ? 'par' : 'odd'; ?>">
<td><img class="thumbnail" src="?<?php echo \Pasteque\URL_ACTION_PARAM; ?>=img&w=product&id=<?php echo $product->id; ?>" />
<td><?php echo $product->reference; ?></td>
<td><?php echo $product->label; ?></td>
<td><img class="thumbnail" src="?<?php echo \Pasteque\URL_ACTION_PARAM; ?>=img&w=product&id=<?php echo $level->productId; ?>" />
<td><?php echo $prdRef; ?></td>
<td><?php echo $prdLabel; ?></td>
<td class="numeric<?php echo $class; ?>"<?php echo $help; ?>><?php echo $qty; ?></td>
<td class="numeric"><?php echo $security === NULL ? \i18n("Undefined") : $security; ?></td>
<td class="numeric"><?php echo $max === NULL ? \i18n("Undefined") : $max; ?></td>
</tr>
<?php
}
......
......@@ -20,21 +20,35 @@
namespace BaseStocks;
$message = NULL;
$error = NULL;
$message = null;
$error = null;
$dateStr = isset($_POST['date']) ? $_POST['date'] : \i18nDate(time());
$time = \i18nRevDate($dateStr);
$date = \Pasteque\stdstrftime($time);
if (isset($_POST['reason'])) {
$reason = $_POST['reason'];
$locationId = $_POST['location'];
foreach ($_POST as $key => $value) {
if (strpos($key, "qty-") === 0) {
$product_id = substr($key, 4);
$productId = substr($key, 4);
$product = \Pasteque\ProductsService::get($productId);
switch ($reason) {
case \Pasteque\StockMove::REASON_OUT_SELL:
$price = $product->priceSell;
break;
case \Pasteque\StockMove::REASON_IN_BUY:
case \Pasteque\StockMove::REASON_OUT_BACK:
default:
if ($product->priceBuy !== null) {
$price = $product->priceBuy;
} else {
$price = 0.0;
}
break;
}
$qty = $value;
$move = new \Pasteque\StockMove($date, $reason, $locationId,
$product_id, $qty);
$move = new \Pasteque\StockMove($time, $reason, $productId,
$locationId, null, $qty, $price);
if (\Pasteque\StocksService::addMove($move)) {
$message = \i18n("Changes saved");
} else {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment