120 lines
3.2 KiB
PHP
120 lines
3.2 KiB
PHP
<?php
|
|
|
|
$main_class = 'Locations';
|
|
|
|
class Locations extends Window
|
|
{
|
|
protected $tableName = 'warehouse_locations';
|
|
|
|
protected $nameField = 'code';
|
|
|
|
public function handleUpdate()
|
|
{
|
|
$update = parent::handleUpdate();
|
|
|
|
$acn = $this->getAction();
|
|
|
|
if ($acn == 'add') {
|
|
$data = $this->getData();
|
|
|
|
$this->generatePositions($data['code'], [
|
|
$this->emptyToNull($data['A']),
|
|
$this->emptyToNull($data['B']),
|
|
$this->emptyToNull($data['C']),
|
|
$this->emptyToNull($data['D']),
|
|
]);
|
|
}
|
|
|
|
return $update;
|
|
}
|
|
|
|
private function generatePositions($code, $maxNumbers)
|
|
{
|
|
$numbers = [];
|
|
|
|
foreach ($maxNumbers as $maxNumber) {
|
|
if ($maxNumber === null) {
|
|
break;
|
|
}
|
|
$numbers[] = $this->getAllNumbers($maxNumber);
|
|
}
|
|
|
|
$combinations = $this->generateCombinations($numbers);
|
|
foreach ($combinations as $combination) {
|
|
array_unshift($combination, $code);
|
|
$this->insertSQL('warehouse_positions',
|
|
[
|
|
'code' => join('-', $combination),
|
|
'id_location' => $this->getID(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function generateCombinations(array $data, array &$all = [], array $group = [], $value = null, $i = 0)
|
|
{
|
|
$keys = array_keys($data);
|
|
if (isset($value) === true) {
|
|
array_push($group, $value);
|
|
}
|
|
|
|
if ($i >= count($data)) {
|
|
array_push($all, $group);
|
|
} else {
|
|
$currentKey = $keys[$i];
|
|
$currentElement = $data[$currentKey];
|
|
foreach ($currentElement as $val) {
|
|
$this->generateCombinations($data, $all, $group, $val, $i + 1);
|
|
}
|
|
}
|
|
|
|
return $all;
|
|
}
|
|
|
|
private function getAllNumbers($max)
|
|
{
|
|
$return = [];
|
|
for ($i = 1; $i <= $max; $i++) {
|
|
$return[] = $i;
|
|
}
|
|
|
|
return $return;
|
|
}
|
|
|
|
private function emptyToNull($value)
|
|
{
|
|
return empty($value) ? null : $value;
|
|
}
|
|
|
|
public function handleDelete()
|
|
{
|
|
if (!findRight('LOC_DEL')) {
|
|
$this->returnError('Na mazání nemáte práva');
|
|
}
|
|
|
|
if (sqlQueryBuilder()
|
|
->select('*')
|
|
->from('warehouse_products', 'wp')
|
|
->leftJoin('wp', 'warehouse_positions', 'wpos', 'wpos.id=wp.id_position')
|
|
->leftJoin('wpos', 'warehouse_locations', 'wl', 'wl.id=wpos.id_location')
|
|
->where(\Query\Operator::equals(['wl.id' => $this->getID()]))
|
|
->execute()
|
|
->fetchAll()) {
|
|
$this->returnError('V lokaci jsou pozice s produkty! Nejprve je nutne preskladnit produkty ze vsech pozici teto lokace!');
|
|
}
|
|
|
|
parent::handleDelete();
|
|
}
|
|
|
|
public function hasRights($name = null)
|
|
{
|
|
switch ($name) {
|
|
case Window::RIGHT_DELETE:
|
|
return findRight('LOC_DEL');
|
|
case Window::RIGHT_DUPLICATE:
|
|
return false;
|
|
default:
|
|
return parent::hasRights($name);
|
|
}
|
|
}
|
|
}
|