Cikkszám:
Mennyiség:
Raktárhely:
:
Cikkszám:
:
"A cikkszámhoz nem lett megadva doboz méret!"]); exit(); } } else { echo json_encode(["result" => "Nem létező cikkszámot adott meg!"]); exit(); } /* -------------------- Raktár struktúrák --------------------*/ $warehouse_config = array(); $query = "SELECT * FROM warehouse_structure"; if ($result = $conn->query($query)) { while ($wh_structure = $result->fetch_assoc()) { $warehouse_config[$wh_structure["warehouse_id"]] = [ "capacity" => $wh_structure["capacity"], "location" => $wh_structure["location"], "code" => $wh_structure["code"], "row" => $wh_structure["row"], "columns" => $wh_structure["columns"], "status" => $wh_structure["status"], "size" => $wh_structure["size"], "name" => $wh_structure["name"], "multiitem_row" => $wh_structure["multiitem_row"], "SizeJSON" => json_decode(base64_decode($wh_structure["size"]), true) ]; } } /* -------------------- Ha van olyan polc (cella) ahol az adott cikkszám megtalálható és nincsen tele --------------------*/ $FindedWarehouseElement = array(); $query = "SELECT * FROM warehouse WHERE item_id = '$item_id' and amount != 0"; if ($result = $conn->query($query)) { while ($warehouse = $result->fetch_assoc()) { $warehouse_id = $warehouse['warehouse_id']; $CanSave = true; /* Kapacitás számolása */ $position = explode(":", $warehouse['position']); //($row + 1).':'.($column + 1); $size_key = (intval($position[1]) - 1).':'.(intval($position[0]) - 1); // $column + $row $capacity_left = $warehouse_config[$warehouse_id]["SizeJSON"]['globalCapacity']; if (isset($warehouse_config[$warehouse_id]["SizeJSON"]['customCapacity'][$size_key])) { $capacity_left = $warehouse_config[$warehouse_id]["SizeJSON"]['customCapacity'][$size_key]; } $position = $warehouse['position']; $sql = mysqli_query($conn,"SELECT SUM(amount) FROM warehouse WHERE position = '$position' and item_id != '$item_id' and amount != 0 and warehouse_id = '$warehouse_id'"); $otherElement = mysqli_fetch_array($sql); if ($otherElement != null) { $capacity_left = $capacity_left - $otherElement[0] - $warehouse['amount']; } else { $capacity_left = $capacity_left - $warehouse['amount']; } if ($capacity_left <= 0) { $CanSave = false; } // Ellenőrzés: Doboz mérete $slot_size = $warehouse_config[$warehouse_id]["SizeJSON"]['customSizes'][$size_key] ?? $warehouse_config[$warehouse_id]["SizeJSON"]['globalSize']; if (!in_array($slot_size, $item_id_config["size"])) $CanSave = false; /* Raktár hely ellenőrzése */ if ($warehouse_config[$warehouse_id]["location"] != $location && $location != "" && $location != "-1") { $CanSave = false; } else if ($warehouse_config[$warehouse_id]["status"] != 1) { $CanSave = false; } /* Elérhető opció */ if ($CanSave) { $FindedWarehouseElement[] = [ 'wid' => $warehouse['wid'], 'warehouse_id' => $warehouse_id, 'position' => $warehouse['position'], 'amount' => $warehouse['amount'], 'capacity_left' => $capacity_left ]; } } } usort($FindedWarehouseElement, function($a, $b) use ($amount_left) { $a_enough = $a['capacity_left'] >= $amount_left; $b_enough = $b['capacity_left'] >= $amount_left; if ($a_enough && !$b_enough) return -1; if (!$a_enough && $b_enough) return 1; if ($a_enough && $b_enough) { return $a['capacity_left'] <=> $b['capacity_left']; } return $b['capacity_left'] <=> $a['capacity_left']; }); foreach ($FindedWarehouseElement as $slot) { if ($amount_left <= 0) break; $can_place = min($slot['capacity_left'], $amount_left); if ($can_place > 0) { $WeHaveToPlaceHere[] = [ 'warehouse_id' => $slot['warehouse_id'], 'position' => $slot['position'], 'placed' => $can_place, 'size' => $item_id_config["size"] ]; $amount_left -= $can_place; } } /* -------------------- Megkeresi a legközelebbi üres polcot, és oda ajánlja --------------------*/ $existing_positions = []; $warehouse_counts = []; foreach ($FindedWarehouseElement as $slot) { $wid = $slot['warehouse_id']; $existing_positions[$wid][] = $slot['position']; if (!isset($warehouse_counts[$wid])) $warehouse_counts[$wid] = 0; $warehouse_counts[$wid]++; } // Súlyozás: leggyakoribb warehouse_id előre arsort($warehouse_counts); $sorted_warehouses = array_keys($warehouse_counts); foreach (array_keys($warehouse_config) as $wid) { if (!in_array($wid, $sorted_warehouses)) { $sorted_warehouses[] = $wid; } } if ($amount_left > 0) { foreach ($sorted_warehouses as $wid) { if (isset($warehouse_config[$wid]["location"]) && $warehouse_config[$wid]["location"] != $location && $location != "" && $location != "-1") continue; if (isset($warehouse_config[$wid]["status"]) && $warehouse_config[$wid]["status"] != 1) continue; if (!isset($warehouse_config[$wid])) continue; if ($amount_left <= 0) break; $row = $warehouse_config[$wid]['row']; $cols = $warehouse_config[$wid]['columns']; $max_cap = $warehouse_config[$wid]['capacity']; $GlobalSize = $warehouse_config[$wid]["SizeJSON"]['globalSize']; $candidates = []; for ($i = 1; $i <= $row; $i++) { for ($j = 1; $j <= $cols; $j++) { $pos = "$i:$j"; // Ellenőrzés: foglalt pozíció? $safe_pos = mysqli_real_escape_string($conn, $pos); $sql = mysqli_query($conn, "SELECT wid FROM warehouse WHERE position = '$safe_pos' AND warehouse_id = '$wid' AND amount != 0"); if (mysqli_fetch_array($sql) != null) continue; // Ellenőrzés: Doboz mérete $size_key = ($j - 1) . ':' . ($i - 1); $slot_size = $warehouse_config[$wid]["SizeJSON"]['customSizes'][$size_key] ?? $GlobalSize; if (!in_array($slot_size, $item_id_config["size"])) continue; // Ellenőrzés: kapacitás $cap_left = $warehouse_config[$wid]["SizeJSON"]['customCapacity'][$size_key] ?? $warehouse_config[$wid]["SizeJSON"]['globalCapacity']; foreach ($FindedWarehouseElement as $slot) { if ($slot['warehouse_id'] === $wid && $slot['position'] === $pos) { $cap_left = (int)$slot['capacity_left']; break; } } if ($cap_left <= 0) continue; $dist = isset($existing_positions[$wid]) ? min_distance($i, $j, $existing_positions[$wid]) : 0; $candidates[] = [ 'warehouse_id' => $wid, 'position' => $pos, 'capacity_left' => $cap_left, 'distance' => $dist, 'size' => $slot_size ]; } } // Közelebbi pozíciók előnyben, raktárpolc pazarlás számolás usort($candidates, function($a, $b) use ($amount_left) { $a_enough = $a['capacity_left'] >= $amount_left; $b_enough = $b['capacity_left'] >= $amount_left; if ($a_enough && !$b_enough) return -1; if (!$a_enough && $b_enough) return 1; $distCmp = $a['distance'] <=> $b['distance']; if ($distCmp !== 0) return $distCmp; if ($a_enough && $b_enough) { return $a['capacity_left'] <=> $b['capacity_left']; } return $b['capacity_left'] <=> $a['capacity_left']; }); foreach ($candidates as $c) { if ($amount_left <= 0) break; $exists = false; foreach ($WeHaveToPlaceHere as $existing) { if ($existing['warehouse_id'] == $c['warehouse_id'] && $existing['position'] == $c['position']) { $exists = true; break; } } if ($exists) continue; $can_place = min($amount_left, $c['capacity_left']); $WeHaveToPlaceHere[] = [ 'warehouse_id' => $c['warehouse_id'], 'position' => $c['position'], 'placed' => $can_place, 'size' => $c['size'] ]; $amount_left -= $can_place; } } } /* -------------------- A legközelebbi még nem teli polcra ajánlja --------------------*/ if ($amount_left > 0) { function candidate_exists($candidates, $warehouse_id, $position) { foreach ($candidates as $c) { if ($c['warehouse_id'] === $warehouse_id && $c['position'] === $position) { return true; } } return false; } foreach ($sorted_warehouses as $wid) { if (isset($warehouse_config[$wid]["location"]) && $warehouse_config[$wid]["location"] != $location && $location != "" && $location != "-1") continue; if (isset($warehouse_config[$wid]["status"]) && $warehouse_config[$wid]["status"] != 1) continue; if (!isset($warehouse_config[$wid])) continue; if ($amount_left <= 0) break; $row = $warehouse_config[$wid]['row']; $cols = $warehouse_config[$wid]['columns']; $max_cap = $warehouse_config[$wid]['capacity']; $GlobalSize = $warehouse_config[$wid]["SizeJSON"]['globalSize']; $candidates = []; for ($i = intval($warehouse_config[$wid]["multiitem_row"]); $i <= $row; $i++) { for ($j = 1; $j <= $cols; $j++) { $pos = "$i:$j"; //Ellenőrzés: Tettünk e már ide if (candidate_exists($WeHaveToPlaceHere, $wid, $pos)) continue; // Ellenőrzés: Doboz mérete $size_key = ($j - 1) . ':' . ($i - 1); $slot_size = $warehouse_config[$wid]["SizeJSON"]['customSizes'][$size_key] ?? $GlobalSize; if (!in_array($slot_size, $item_id_config["size"])) continue; // Ellenőrzés: 1 termék van a polcon? $safe_pos = mysqli_real_escape_string($conn, $pos); $sql = mysqli_query($conn, "SELECT COUNT(wid), SUM(amount) FROM warehouse WHERE position = '$safe_pos' AND warehouse_id = '$wid' AND amount != 0"); $sqlres = mysqli_fetch_array($sql); if ($sqlres[0] == 0) continue; // Ellenőrzés: kapacitás $cap_left = intval($warehouse_config[$wid]["SizeJSON"]['customCapacity'][$size_key] ?? $warehouse_config[$wid]["SizeJSON"]['globalCapacity']) - $sqlres[1]; if ($cap_left <= 0) continue; $dist = isset($existing_positions[$wid]) ? min_distance($i, $j, $existing_positions[$wid]) : 0; $candidates[] = [ 'warehouse_id' => $wid, 'position' => $pos, 'capacity_left' => $cap_left, 'distance' => $dist, 'wid_count' => $sqlres[0], 'size' => $slot_size ]; } } usort($candidates, function($a, $b) use ($amount_left) { if ($a['wid_count'] !== $b['wid_count']) { return $a['wid_count'] <=> $b['wid_count']; } $a_enough = $a['capacity_left'] >= $amount_left; $b_enough = $b['capacity_left'] >= $amount_left; if ($a_enough && !$b_enough) return -1; if (!$a_enough && $b_enough) return 1; $distCmp = $a['distance'] <=> $b['distance']; if ($distCmp !== 0) return $distCmp; if ($a_enough && $b_enough) { return $a['capacity_left'] <=> $b['capacity_left']; } return $b['capacity_left'] <=> $a['capacity_left']; }); foreach ($candidates as $c) { if ($amount_left <= 0) break; $exists = false; foreach ($WeHaveToPlaceHere as $existing) { if ($existing['warehouse_id'] == $c['warehouse_id'] && $existing['position'] == $c['position']) { $exists = true; break; } } if ($exists) continue; $can_place = min($amount_left, $c['capacity_left']); $WeHaveToPlaceHere[] = [ 'warehouse_id' => $c['warehouse_id'], 'position' => $c['position'], 'placed' => $can_place, 'size' => $c['size'] ]; $amount_left -= $can_place; } } } /* -------------------- Végső simítások --------------------*/ $toplace = array(); foreach ($WeHaveToPlaceHere as $place) { $warehouse_id = $place["warehouse_id"]; $toplace[] = [ "warehouse_loc" => $warehouse_config[$place["warehouse_id"]]["location"], "warehouse_code" => $warehouse_config[$place["warehouse_id"]]["code"], "warehouse_name" => $warehouse_config[$place["warehouse_id"]]["name"], "warehouse_id" => $place["warehouse_id"], "position" => $place["position"], "placed" => $place["placed"], "size" => $place["size"] ]; } usort($toplace, function ($a, $b) { $loc_cmp = strcmp($a['warehouse_loc'], $b['warehouse_loc']); if ($loc_cmp !== 0) return $loc_cmp; $name_cmp = strcmp($a['warehouse_name'], $b['warehouse_name']); if ($name_cmp !== 0) return $name_cmp; list($a_row, $a_col) = explode(":", $a['position']); list($b_row, $b_col) = explode(":", $b['position']); if ((int)$a_col === (int)$b_col) { return (int)$a_row - (int)$b_row; } return (int)$a_col - (int)$b_col; }); echo json_encode(["result" => "ok", "toplace" => $toplace, "amount_left" => $amount_left, "item_id" => $item_id]); } else if (htmlspecialchars($_POST["func"]) == "PlaceIntoWarehouse") { $item_id = htmlspecialchars(str_replace(' ', '+', $_POST['item_id'])); $placed = htmlspecialchars($_POST["placed"]); $warehouse_id = htmlspecialchars($_POST["warehouse_id"]); $position = htmlspecialchars($_POST["position"]); $reason = htmlspecialchars($_POST["reason"]); $result = array(); $sql = mysqli_query($conn, "SELECT * FROM warehouse_structure WHERE warehouse_id = '$warehouse_id'"); $WarehouseData = mysqli_fetch_array($sql); $sql = mysqli_query($conn, "SELECT sum(amount) as amount FROM warehouse WHERE position = '$position' AND warehouse_id = '$warehouse_id'"); $intheloc = mysqli_fetch_array($sql); $sizeJSON = json_decode(base64_decode($WarehouseData["size"]), true); $capacity = intval($sizeJSON['globalCapacity']); if (isset($sizeJSON['customCapacity'][intval(explode(':', $position)[1])-1 . ':' . intval(explode(':', $position)[0])-1])) { $capacity = intval($sizeJSON['customCapacity'][intval(explode(':', $position)[1])-1 . ':' . intval(explode(':', $position)[0])-1]); } if ($intheloc != null && $intheloc["amount"] != null) { $freespace = $capacity - $intheloc["amount"]; } else { $freespace = $capacity; } $sql = mysqli_query($conn, "SELECT size, item_id FROM pr_warehouse_parameters WHERE item_id = '$item_id'"); $ItemSize = mysqli_fetch_array($sql); $LocSize = ''; if (isset($sizeJSON['customSizes'][intval(explode(':', $position)[1])-1 . ':' . intval(explode(':', $position)[0])-1])) { $LocSize = $sizeJSON['customSizes'][intval(explode(':', $position)[1])-1 . ':' . intval(explode(':', $position)[0])-1]; } else { $LocSize = $sizeJSON['globalSize']; } if ($WarehouseData == null || $WarehouseData["status"] != 1) { $result['result'] = "Nem létező, vagy inaktív raktárba szeretne betenni elemet!"; } else if ($ItemSize == null || $ItemSize[0] == "") { $result['result'] = "A megadott terméknek nem lett megadva dobozméret!"; } else if ($freespace < $placed) { $result['result'] = "A kijelölt helyen nincsen elegendő hely!"; } else { $item_id = $ItemSize['item_id']; $sql = mysqli_query($conn, "SELECT wid, amount FROM warehouse WHERE item_id = '$item_id' and warehouse_id = '$warehouse_id' and position = '$position'"); $IsThereProduct = mysqli_fetch_array($sql); if ($IsThereProduct != null) { $newamount = $placed + $IsThereProduct[1]; $wid = $IsThereProduct[0]; $sql = mysqli_query($conn, "UPDATE warehouse SET amount = '$newamount' WHERE wid = '$wid'"); } else { $sql = mysqli_query($conn, "SELECT wid FROM warehouse WHERE amount = 0 LIMIT 1"); $WeCanUpdateOld = mysqli_fetch_array($sql); if ($WeCanUpdateOld != null) { $wid = $WeCanUpdateOld[0]; $sql = mysqli_query($conn, "UPDATE warehouse SET item_id = '$item_id', warehouse_id = '$warehouse_id', position = '$position', amount = '$placed' WHERE wid = '$wid'"); } else { $sql = mysqli_query($conn, "INSERT INTO warehouse(item_id, warehouse_id, position, amount) VALUES ('$item_id', '$warehouse_id', '$position', '$placed')"); } } $loggerclass->writeLogWarehouse(['reason' => 'Doboz elhelyezés', 'reason_code' => 1, 'item_id' => $item_id, 'from_place' => $reason, 'to_place' => $WarehouseData['code'] . chr(64 + intval(explode(':', $position)[0])) . explode(':', $position)[1], 'amount_left' => $placed, 'amount_right' => $placed ]); $result['result'] = "ok"; if ($reason != null && $reason != "") { $reasonData = explode("#", $reason); if ($reasonData[0] == "Production") { $validCategories = [ 'classic' => 'production_classic', 'injmold' => 'production_injmold', 'sporty' => 'production_sporty', 'boxing' => 'production_boxing' ]; if (!array_key_exists($reasonData[1], $validCategories)) { $result['result'] = "A termék elhelyezve a raktárban, de az indoklás nem került elmentésre!"; } else { $table = $validCategories[$reasonData[1]]; $pr_id = htmlspecialchars($reasonData[2]); $sql = mysqli_query($conn, "SELECT db_revenue FROM $table WHERE pr_id = $pr_id"); $db_revenue = mysqli_fetch_array($sql); if ($db_revenue != null) { $new_db_revenue = intval($placed) + intval($db_revenue[0]); $sql = mysqli_query($conn, "UPDATE $table SET db_revenue = $new_db_revenue WHERE pr_id = $pr_id"); } } } } } echo json_encode($result); } exit(); } $WarehouseLocSelect = ''; $query = "SELECT DISTINCT location FROM warehouse_structure ORDER BY location ASC"; if ($result = $conn->query($query)) { while ($warehouse = $result->fetch_assoc()) { $WarehouseLocSelect = $WarehouseLocSelect.''; } } ?>

Cikkszám:
Mennyiség:
Raktárhely:
:
Cikkszám:
: