inventory.php 4.96 KB
Newer Older
1
2
3
<?php
//    Pastèque Web back office, Stocks module
//
4
//    Copyright (C) 2013-2016 Scil (http://scil.coop)
5
//        Cédric Houbart, Philippe Pary philippe@scil.coop
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//
//    This file is part of Pastèque.
//
//    Pastèque is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Pastèque 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 General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Pastèque.  If not, see <http://www.gnu.org/licenses/>.

namespace BaseStocks;

24
25
26
27
$modules = \Pasteque\get_loaded_modules(\Pasteque\get_user_id());
$multilocations = false;
$defaultLocationId = null;
if (in_array("stock_multilocations", $modules)) {
28
	$multilocations = true;
29
30
}

31
32
33
34
35
$locSrv = new \Pasteque\LocationsService();
$locations = $locSrv->getAll();
$locNames = array();
$locIds = array();
foreach ($locations as $location) {
36
37
	$locNames[] = $location->label;
	$locIds[] = $location->id;
38
39
40
}
$currLocation = null;
if (isset($_POST['location'])) {
41
	$currLocation = $_POST['location'];
42
} else {
43
	$currLocation = $locations[0]->id;
44
}
45
$products = \Pasteque\ProductsService::getAll();
46
47
48
49
$categories = \Pasteque\CategoriesService::getAll();
$prdCat = array();
// Link products to categories and don't track compositions
foreach ($products as $product) {
50
51
52
	if ($product->categoryId !== \Pasteque\CompositionsService::CAT_ID) {
		$prdCat[$product->categoryId][] = $product;
	}
53
}
54
$levels = \Pasteque\StocksService::getLevels($currLocation);
55
56
$prdLevel = array();
foreach ($levels as $level) {
57
	$prdLevel[$level->productId] = $level;
58
}
59

60
61
62
63
64
65
66
//Title
echo \Pasteque\row(\Pasteque\mainTitle(\i18n("Inventory", PLUGIN_NAME)));
//Buttons
$buttons = \Pasteque\exportButton(\i18n("Export inventory", PLUGIN_NAME), \Pasteque\get_report_url(PLUGIN_NAME, "inventory"));
echo \Pasteque\row(\Pasteque\buttonGroup($buttons));
//Information
\Pasteque\tpl_msg_box($message, $error);
Philippe Pary's avatar
Philippe Pary committed
67

68
69
70
71
72
if ($multilocations) {
	// Location picker
	$content = \Pasteque\row(\Pasteque\form_select("location", \i18n("Location"), $locIds, $locNames, $currLocation));
	$content .= \Pasteque\row(\Pasteque\form_send());
	echo \Pasteque\row(\Pasteque\form_generate(\Pasteque\get_current_url(), "post", $content));
73
}
74

75
unset($content);
76
foreach ($categories as $category) {
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
	if (isset($prdCat[$category->id])) {
		// Category header
		echo \Pasteque\row(\Pasteque\secondaryTitle($category->label));
		$content[0][0] = "";
		$content[0][1] = \i18n("Product.reference");
		$content[0][2] = \i18n("Product.label");
		$content[0][3] = \i18n("Quantity");
		$content[0][4] = \i18n("Stock.SellValue");
		$content[0][5] = \i18n("Stock.BuyValue");
		$content[0][6] = \i18n("QuantityMin");
		$content[0][7] = \i18n("QuantityMax");
		$i = 1;
		foreach ($prdCat[$category->id] as $product) {
			if (!isset($prdLevel[$product->id])) {
				continue;
			}
			// Level lines
			$prdRef = "";
			$prdLabel = "";
			$imgSrc = "";
			$prdSellPrice = 0;
			$prdBuyPrice = 0;
			$level = $prdLevel[$product->id];
			if ($product->hasImage) {
				$imgSrc = \Pasteque\PT::URL_ACTION_PARAM . "=img&w=product&id=" . $product->id;
			} else {
				$imgSrc = \Pasteque\PT::URL_ACTION_PARAM . "=img&w=product";
			}
			$prdLabel = $product->label;
			$prdRef = $product->reference;
			$prdSellPrice = $product->priceSell !== null ? $product->priceSell : 0;
			$prdBuyPrice = $product->priceBuy !== null ? $product->priceBuy : 0;
			$security = $level->security;
			$max = $level->max;
			$qty = $level->qty !== null ? $level->qty : 0;
			$class = "";
			$help = "";
			if ($security !== null && $qty < $security) {
				$class=" warn-level";
				$help = ' title="' . \Pasteque\esc_attr(\i18n("Stock is below security level!", PLUGIN_NAME)) . '"';
			}
			if ($qty < 0) {
				$class=" alert-level";
				$help = ' title="' . \Pasteque\esc_attr(\i18n("Stock is negative!", PLUGIN_NAME)) . '"';
			} else if ($max !== NULL && $qty > $max) {
				$class=" alert-level";
				$help = ' title="' . \Pasteque\esc_attr(\i18n("Overstock!", PLUGIN_NAME)) . '"';
			}
			if (!isset($security)) {
				$security = \i18n("Undefined");
			}
			if (!isset($max)) {
				$max = \i18n("Undefined");
			}
			$content[$i][0] = "<img class=\"thumbnail\" src=\"?" . \Pasteque\esc_attr($imgSrc) . "\">";
			$content[$i][1] = \Pasteque\esc_html($prdRef);
			$content[$i][2] = \Pasteque\esc_html($prdLabel);
			$content[$i][3] = \Pasteque\esc_html($qty);
			$content[$i][4] = \Pasteque\esc_html(\i18nCurr($prdSellPrice*$qty));
			$content[$i][5] = \Pasteque\esc_html(\i18nCurr($prdBuyPrice*$qty));
			$content[$i][6] = \Pasteque\esc_html($security);
			$content[$i][7] = \Pasteque\esc_html($max);
			$i++;
		}
		echo \Pasteque\row(\Pasteque\standardTable($content));
	}
143
}