export stock addresses

This commit is contained in:
Michał Zieliński
2025-11-08 12:23:19 +01:00
parent ef6b3e0f7c
commit a39ebfd84c
3 changed files with 291 additions and 187 deletions

View File

@@ -134,6 +134,23 @@ function isProductValid(id) {
return false;
}
}
function exportFile() {
$("#loader").show();
$.ajax({
url: "index.php?module=EcmProducts&action=&action=stockAddress&ajax=export&to_pdf=1",
}).done(function (data) {
$("#loader").hide();
if (data.status === 'Error') {
alert('Błąd: ' + data.msg);
} else {
console.log(data);
downloadFile(data.fileContent, data.fileName, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
}
}).fail(() => {
$("#loader").hide();
alert('Błąd serwera.');
});
}
$(document).ready(function () {
$("#address").mask("AB.CD.E.F", {
placeholder: "__.__._._",
@@ -189,12 +206,49 @@ $(document).ready(function () {
$("#searchByIndexForm").submit();
}
});
$("#exportTrigger").click(() => {
exportFile();
});
$('[name^="showAddress').click((event) => {
$("#address").val(event.target.name.split('-')[1]);
if (isAddressValid()) {
$("#searchByAddressForm").submit();
}
});
//bind buttons
});
});
function downloadFile(base64Data, fileName, mimeType) {
// Stwórz blob z danych base64
const byteCharacters = atob(base64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += 512) {
const slice = byteCharacters.slice(offset, offset + 512);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, {type: mimeType});
// Stwórz link do pobrania
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
// Dodaj link do dokumentu i kliknij go
document.body.appendChild(link);
link.click();
// Usuń link z dokumentu
setTimeout(function () {
document.body.removeChild(link);
window.URL.revokeObjectURL(link.href);
}, 100);
}

View File

@@ -1,151 +1,186 @@
<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
$db = $GLOBALS['db'];
if ($_GET['ajax']) {
switch ($_GET['ajax']) {
case 'addAddress':
$productId = $_GET['productId'];
$address = $_GET['address'];
$isNotFull = $_GET['isNotFull'];
// check if product exists
$productId = $db->fetchByAssoc($db->query("SELECT id FROM ecmproducts WHERE id = '$productId'"))['id'];
if ($productId == null) {
echo json_encode(array('status' => 'Error', 'msg' => 'Nie znalaziono produktu w bazie: '.$index ));
}
// check if location is empty
$location = $db->fetchByAssoc($db->query("SELECT stock_address FROM ecmproducts_stock_addresses WHERE stock_address = '$address'"));
if ($location != null) {
echo json_encode(array('status' => 'Error', 'msg' => 'Wskazana lokalizacja jest zajęta: '.$address ));
return;
}
$db->query("INSERT INTO ecmproducts_stock_addresses (ecmproduct_id, stock_address, is_not_full) VALUES ('$productId','$address', $isNotFull)");
echo json_encode(array('status' => 'Success', 'msg' => 'addAddress' ));
break;
case 'removeAddress':
$address = $_GET['address'];
$db->query("DELETE FROM ecmproducts_stock_addresses WHERE stock_address='$address'");
echo json_encode(array('status' => 'Success', 'msg' => 'removeAddress' ));
break;
case 'setIsNotFull':
$address = $_GET['address'];
$isNotFull = $_GET['isNotFull'] == 'true' ? 1 : 0;
$db->query("UPDATE ecmproducts_stock_addresses SET is_not_full=$isNotFull WHERE stock_address='$address'");
echo json_encode(array('status' => 'Success', 'msg' => 'setIsNotFull'));
break;
}
} else {
$pRes = $db->query("SELECT DISTINCT id, code FROM ecmproducts WHERE deleted=0;");
$codes = array();
while ($p = $db -> fetchByAssoc($pRes)) {
$index = str_replace("&#039;", "'", $p['code']);
array_push($codes, ['id' => $p['id'], 'code' => $index]);
}
$smarty = new Sugar_Smarty ();
if (strlen($_GET['searchType']) > 0 && $_GET['searchType'] == 'address')
{
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
$db = $GLOBALS['db'];
if ($_GET['ajax']) {
switch ($_GET['ajax']) {
case 'addAddress':
$productId = $_GET['productId'];
$address = $_GET['address'];
$product = $db->fetchByAssoc($db->query("
$isNotFull = $_GET['isNotFull'];
// check if product exists
$productId = $db->fetchByAssoc($db->query("SELECT id FROM ecmproducts WHERE id = '$productId'"))['id'];
if ($productId == null) {
echo json_encode(array('status' => 'Error', 'msg' => 'Nie znalaziono produktu w bazie: ' . $index));
}
// check if location is empty
$location = $db->fetchByAssoc($db->query("SELECT stock_address FROM ecmproducts_stock_addresses WHERE stock_address = '$address'"));
if ($location != null) {
echo json_encode(array('status' => 'Error', 'msg' => 'Wskazana lokalizacja jest zajęta: ' . $address));
return;
}
$db->query("INSERT INTO ecmproducts_stock_addresses (ecmproduct_id, stock_address, is_not_full) VALUES ('$productId','$address', $isNotFull)");
echo json_encode(array('status' => 'Success', 'msg' => 'addAddress'));
break;
case 'removeAddress':
$address = $_GET['address'];
$db->query("DELETE FROM ecmproducts_stock_addresses WHERE stock_address='$address'");
echo json_encode(array('status' => 'Success', 'msg' => 'removeAddress'));
break;
case 'setIsNotFull':
$address = $_GET['address'];
$isNotFull = $_GET['isNotFull'] == 'true' ? 1 : 0;
$db->query("UPDATE ecmproducts_stock_addresses SET is_not_full=$isNotFull WHERE stock_address='$address'");
echo json_encode(array('status' => 'Success', 'msg' => 'setIsNotFull'));
break;
case 'export':
exportExcel();
break;
}
} else {
$pRes = $db->query("SELECT DISTINCT id, code FROM ecmproducts WHERE deleted=0;");
$codes = array();
while ($p = $db->fetchByAssoc($pRes)) {
$index = str_replace("&#039;", "'", $p['code']);
array_push($codes, ['id' => $p['id'], 'code' => $index]);
}
$smarty = new Sugar_Smarty ();
if (strlen($_GET['searchType']) > 0 && $_GET['searchType'] == 'address') {
$address = $_GET['address'];
$product = $db->fetchByAssoc($db->query("
SELECT a.ecmproduct_id, p.name, p.code, p.ems_qty_in_stock AS stock_state, a.is_not_full FROM ecmproducts_stock_addresses AS a
INNER JOIN ecmproducts as p ON p.id=a.ecmproduct_id
WHERE stock_address='$address';"));
$state = $db->fetchByAssoc($db->query("SELECT SUM(quantity) as qty FROM ecmstockstates WHERE product_id='".$product['ecmproduct_id']."'"));
$product['stock_state'] = $state['qty'];
$smarty->assign("PROCESS", "ADDRESS");
$smarty->assign("PRODUCT", $product);
}
if (strlen($_GET['searchType']) > 0 && $_GET['searchType'] == 'index')
{
$id = $_GET['productId'];
$res = $db->query("
$state = $db->fetchByAssoc($db->query("SELECT SUM(quantity) as qty FROM ecmstockstates WHERE product_id='" . $product['ecmproduct_id'] . "'"));
$product['stock_state'] = $state['qty'];
$smarty->assign("PROCESS", "ADDRESS");
$smarty->assign("PRODUCT", $product);
}
if (strlen($_GET['searchType']) > 0 && $_GET['searchType'] == 'index') {
$id = $_GET['productId'];
$res = $db->query("
SELECT a.ecmproduct_id, a.stock_address, a.is_not_full, p.name, p.code FROM ecmproducts_stock_addresses AS a
INNER JOIN ecmproducts as p ON p.id=a.ecmproduct_id
WHERE p.id='$id' AND p.deleted=0;");
$addresses = array();
while ($r = $db -> fetchByAssoc($res)) {
array_push($addresses, $r);
}
$product = $db->fetchByAssoc($db->query("SELECT id, name, code, ems_qty_in_stock FROM ecmproducts WHERE id = '$id';"));
$state = $db->fetchByAssoc($db->query("SELECT SUM(quantity) as qty FROM ecmstockstates WHERE product_id='$id'"));
$product['ems_qty_in_stock'] = $state['qty'];
$smarty->assign("PROCESS", "PRODUCT");
$smarty->assign("PRODUCT", $product);
$smarty->assign("ADDRESSES", $addresses);
$addresses = array();
while ($r = $db->fetchByAssoc($res)) {
array_push($addresses, $r);
}
if (strlen($_GET['searchType']) > 0 && $_GET['searchType'] == 'allAddresses')
{
if ($_GET['onlyFull'] == "on") {
$allAddresses = getAllAddresses(true);
} else {
$allAddresses = getAllAddresses(false);
}
$res = $db->query("
SELECT a.ecmproduct_id, a.stock_address, a.is_not_full, p.name, p.code, p.ems_qty_in_stock FROM ecmproducts_stock_addresses AS a
INNER JOIN ecmproducts as p ON p.id=a.ecmproduct_id");
$used = array();
while ($r = $db -> fetchByAssoc($res)) {
array_push($used, $r);
}
$addresses = array();
$sum = 0;
foreach ($allAddresses as $a ) {
$i = array_search($a, array_column($used, 'stock_address'));
$element = ($i !== false ? $used[$i] : null);
$row = array();
$row['stock_address'] = $a;
$row['is_not_full'] = $element['is_not_full'];
if ($element) {
$row['name'] = $element['name'];
$row['code'] = $element['code'];
$row['ecmproduct_id'] = $element['ecmproduct_id'];
$state = $db->fetchByAssoc($db->query("SELECT SUM(quantity) as qty FROM ecmstockstates WHERE product_id='".$element['ecmproduct_id']."'"));
$row['stock_state'] = $state['qty'];
}
if ($_GET['allType'] == 'used' && $element) {
array_push($addresses, $row);
$sum++;
} else if ($_GET['allType'] == 'empty' && !$element) {
array_push($addresses, $row);
$sum++;
} else if ($_GET['allType'] == 'all') {
array_push($addresses, $row);
$sum++;
}
}
$smarty->assign("SUM", $sum);
$smarty->assign("ALL_TYPE", $_GET['allType']);
$smarty->assign("ONLY_FULL", $_GET['onlyFull']);
$smarty->assign("PROCESS", "ALL_ADDRESSES");
$smarty->assign("ADDRESSES", $addresses);
}
$smarty->assign("CODES", json_encode($codes));
$smarty->assign("CURRENT_ADDRESS", $_GET['address']);
$smarty->assign("CURRENT_INDEX", $_GET['index']);
echo $smarty->display ( 'modules/EcmProducts/tpls/stockAddress.tpl' );
$product = $db->fetchByAssoc($db->query("SELECT id, name, code, ems_qty_in_stock FROM ecmproducts WHERE id = '$id';"));
$state = $db->fetchByAssoc($db->query("SELECT SUM(quantity) as qty FROM ecmstockstates WHERE product_id='$id'"));
$product['ems_qty_in_stock'] = $state['qty'];
$smarty->assign("PROCESS", "PRODUCT");
$smarty->assign("PRODUCT", $product);
$smarty->assign("ADDRESSES", $addresses);
}
function getAllAddresses($onlyFull) {
if (strlen($_GET['searchType']) > 0 && $_GET['searchType'] == 'allAddresses') {
if ($_GET['onlyFull'] == "on") {
$allAddresses = getAllAddresses(true);
} else {
$allAddresses = getAllAddresses(false);
}
$res = $db->query("
SELECT a.ecmproduct_id, a.stock_address, a.is_not_full, p.name, p.code, p.ems_qty_in_stock FROM ecmproducts_stock_addresses AS a
INNER JOIN ecmproducts as p ON p.id=a.ecmproduct_id");
$used = array();
while ($r = $db->fetchByAssoc($res)) {
array_push($used, $r);
}
$addresses = array();
for ($i = 1; $i <= 17; $i++) {
$s1 = (strlen($i) == 1) ? '0'.$i : $i;
for ($j = 1; $j <= 15; $j++) {
$s2 = (strlen($j) == 1) ? '0'.$j : $j;
for ($k = 1; $k <=3; $k++) {
if ($onlyFull) {
array_push($addresses, $s1.'.'.$s2.'.'.$k.'.0');
} else {
for ($l = 0; $l <=2; $l++) {
array_push($addresses, $s1.'.'.$s2.'.'.$k.'.'.$l);
}
}
$sum = 0;
foreach ($allAddresses as $a) {
$i = array_search($a, array_column($used, 'stock_address'));
$element = ($i !== false ? $used[$i] : null);
$row = array();
$row['stock_address'] = $a;
$row['is_not_full'] = $element['is_not_full'];
if ($element) {
$row['name'] = $element['name'];
$row['code'] = $element['code'];
$row['ecmproduct_id'] = $element['ecmproduct_id'];
$state = $db->fetchByAssoc($db->query("SELECT SUM(quantity) as qty FROM ecmstockstates WHERE product_id='" . $element['ecmproduct_id'] . "'"));
$row['stock_state'] = $state['qty'];
}
if ($_GET['allType'] == 'used' && $element) {
array_push($addresses, $row);
$sum++;
} else if ($_GET['allType'] == 'empty' && !$element) {
array_push($addresses, $row);
$sum++;
} else if ($_GET['allType'] == 'all') {
array_push($addresses, $row);
$sum++;
}
}
$smarty->assign("SUM", $sum);
$smarty->assign("ALL_TYPE", $_GET['allType']);
$smarty->assign("ONLY_FULL", $_GET['onlyFull']);
$smarty->assign("PROCESS", "ALL_ADDRESSES");
$smarty->assign("ADDRESSES", $addresses);
}
$smarty->assign("CODES", json_encode($codes));
$smarty->assign("CURRENT_ADDRESS", $_GET['address']);
$smarty->assign("CURRENT_INDEX", $_GET['index']);
echo $smarty->display('modules/EcmProducts/tpls/stockAddress.tpl');
}
function getAllAddresses($onlyFull)
{
$addresses = array();
for ($i = 1; $i <= 17; $i++) {
$s1 = (strlen($i) == 1) ? '0' . $i : $i;
for ($j = 1; $j <= 15; $j++) {
$s2 = (strlen($j) == 1) ? '0' . $j : $j;
for ($k = 1; $k <= 3; $k++) {
if ($onlyFull) {
array_push($addresses, $s1 . '.' . $s2 . '.' . $k . '.0');
} else {
for ($l = 0; $l <= 2; $l++) {
array_push($addresses, $s1 . '.' . $s2 . '.' . $k . '.' . $l);
}
}
}
}
return $addresses;
}
}
return $addresses;
}
function exportExcel()
{
$db = $GLOBALS['db'];
$address = $_GET['address'];
$res = $db->query("SELECT p.code, s.stock_address
FROM ecmproducts_stock_addresses AS s
INNER JOIN ecmproducts AS p
ON p.id = s.ecmproduct_id
ORDER BY s.stock_address;");
$result = array();
while ($row = $db->fetchByAssoc($res)) {
array_push($result, $row);
}
$headers = [
'Indeks', 'Adres'
];
$sheetData = array_merge([$headers], $result);
require_once 'modules/EcmReports/BimIT-Reports/lib/xlsxGenerator.php';
$xlsx = Shuchkin\SimpleXLSXGen::fromArray($sheetData);
$xlsx_content = (string)$xlsx;
header('Content-Type: application/json');
echo json_encode([
'success' => true,
'fileContent' => base64_encode($xlsx_content),
'fileName' => 'adresy_magazynowe.xlsx'
]);
}

View File

@@ -1,5 +1,5 @@
<div class="loader" id="loader">
<img src="themes/default/images/loading.gif" />
<img src="themes/default/images/loading.gif"/>
</div>
<script>
var CODES = {$CODES};
@@ -17,10 +17,10 @@
<input type="text" name="address" id="address" size="30" value="{$CURRENT_ADDRESS}">
</td>
<td>
<input type="hidden" name="module" value="EcmProducts" />
<input type="hidden" name="action" value="stockAddress" />
<input type="hidden" name="searchType" value="address" />
<input type="button" id="searchByAddressTrigger" value="Szukaj adresu&nbsp;" class="button" />
<input type="hidden" name="module" value="EcmProducts"/>
<input type="hidden" name="action" value="stockAddress"/>
<input type="hidden" name="searchType" value="address"/>
<input type="button" id="searchByAddressTrigger" value="Szukaj adresu&nbsp;" class="button"/>
<span style="color: red;" id="addressError"></span>
</td>
</tr>
@@ -34,11 +34,11 @@
<input type='text' name="index" id="index" size="30" value="{$CURRENT_INDEX}">
</td>
<td>
<input type="hidden" name="module" value="EcmProducts" />
<input type="hidden" name="action" value="stockAddress" />
<input type="hidden" name="searchType" value="index" />
<input type="hidden" name="productId" id="productId" value="" />
<input type="button" id="searchByIndexTrigger" value="Szukaj indeksu" class="button" />
<input type="hidden" name="module" value="EcmProducts"/>
<input type="hidden" name="action" value="stockAddress"/>
<input type="hidden" name="searchType" value="index"/>
<input type="hidden" name="productId" id="productId" value=""/>
<input type="button" id="searchByIndexTrigger" value="Szukaj indeksu" class="button"/>
<span style="color: red;" id="indexError"></span>
</td>
</tr>
@@ -57,13 +57,23 @@
</select>
</td>
<td>
<input type="hidden" name="module" value="EcmProducts" />
<input type="hidden" name="action" value="stockAddress" />
<input type="hidden" name="searchType" value="allAddresses" />
<input type="submit" id="searchByIndexTrigger" value="Pokaż adresy" class="button" />
<input type="hidden" name="module" value="EcmProducts"/>
<input type="hidden" name="action" value="stockAddress"/>
<input type="hidden" name="searchType" value="allAddresses"/>
<input type="submit" id="searchByIndexTrigger" value="Pokaż adresy" class="button"/>
</td>
</tr>
</form>
<tr>
<td scope="row" nowrap="nowrap" width="1%">
<input type="hidden" name="module" value="EcmProducts"/>
<input type="hidden" name="action" value="stockAddress"/>
<input type="hidden" name="searchType" value="export"/>
<input type="button" id="exportTrigger" value="Eksport Excel" class="button"/>
</td>
<td nowrap="nowrap" width="1%">&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</div>
{if $PROCESS && $PROCESS=='ADDRESS'}
@@ -92,10 +102,11 @@
<td valign="top" class="oddListRowS1"
style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;">
<input type="button" value="Karta produktu" class="button"
onclick="openProductCard('{$PRODUCT.ecmproduct_id}');" />&nbsp;&nbsp;
onclick="openProductCard('{$PRODUCT.ecmproduct_id}');"/>&nbsp;&nbsp;
<input type="button" value="Wyszukaj wszystkie adresy" class="button"
onclick="searchByIndex('{$PRODUCT.code}')" />&nbsp;&nbsp;
<input type="button" value="Usuń produkt z adresu" class="button" onclick="removeProductFromAddress()" />
onclick="searchByIndex('{$PRODUCT.code}')"/>&nbsp;&nbsp;
<input type="button" value="Usuń produkt z adresu" class="button"
onclick="removeProductFromAddress()"/>
</td>
</tr>
</table>
@@ -118,7 +129,7 @@
</td>
<td valign="top" class="oddListRowS1"
style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;">
<input type="button" value="Dodaj produkt" class="button" onclick="addProductToAddress()" />
<input type="button" value="Dodaj produkt" class="button" onclick="addProductToAddress()"/>
</td>
</tr>
</table>
@@ -126,7 +137,10 @@
{/if}
{if $PROCESS && $PROCESS=='PRODUCT'}
<h2>{$PRODUCT.code} {$PRODUCT.name} ( Stan: {$PRODUCT.ems_qty_in_stock} )&nbsp;<input type="button"
value="Karta produktu" class="button" onclick="openProductCard('{$PRODUCT.id}');" /></h2>
value="Karta produktu"
class="button"
onclick="openProductCard('{$PRODUCT.id}');"/>
</h2>
<table cellpadding="0" cellspacing="0" class="list view">
<tr class="oddListRowS1">
<th style="width: 15%;"><b>Adres</b></td>
@@ -141,29 +155,29 @@
</td>
<td valign="top" class="oddListRowS1"
style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;">
<input type="checkbox" {if $item.is_not_full}checked{/if}
onclick="setAddressIsNotFull('{$item.stock_address}', this)" />
<input type="checkbox" {if $item.is_not_full}checked{/if}
onclick="setAddressIsNotFull('{$item.stock_address}', this)"/>
</td>
<td valign="top" class="oddListRowS1"
style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;">
<input type="button" value="Usuń lokalizację" class="button"
onclick="removeAddressFromProduct('{$item.stock_address}')" />
onclick="removeAddressFromProduct('{$item.stock_address}')"/>
</td>
</tr>
{/foreach}
<tr class="oddListRowS1">
<td valign="top" class="oddListRowS1"
style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;">
<input type="text" size="10" id="newAddress" />
<input type="text" size="10" id="newAddress"/>
<span style="color: red;" id="newAddressError"></span>
</td>
<td valign="top" class="oddListRowS1"
style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;">
<input type="checkbox" id="isNotFull" />
<input type="checkbox" id="isNotFull"/>
</td>
<td valign="top" class="oddListRowS1"
style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;">
<input type="button" value="Dodaj lokalizację" class="button" onclick="addAddressToProduct();" />
<input type="button" value="Dodaj lokalizację" class="button" onclick="addAddressToProduct();"/>
</td>
</tr>
</table>
@@ -186,11 +200,11 @@
</td>
<td valign="top" class="oddListRowS1"
style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;">
<input type="checkbox" {if $item.is_not_full}checked{/if} disabled />
<input type="checkbox" {if $item.is_not_full}checked{/if} disabled/>
</td>
<td valign="top" class="oddListRowS1"
style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;">
<input type="button" value="Pokaż adres" class="button" name="showAddress-{$item.stock_address}" />
<input type="button" value="Pokaż adres" class="button" name="showAddress-{$item.stock_address}"/>
</td>
<td valign="top" class="oddListRowS1"
style="padding:5px !important;border-bottom:1px solid #cccccc;vertical-align:top;">
@@ -212,31 +226,32 @@
{/if}
<style>
{literal}
.ui-autocomplete {
max-height: 200px;
overflow-y: auto;
overflow-x: hidden;
padding-right: 20px;
border-radius: 0;
}
.ui-autocomplete {
max-height: 200px;
overflow-y: auto;
overflow-x: hidden;
padding-right: 20px;
border-radius: 0;
}
.ui-corner-all {
border-radius: 0;
}
.ui-corner-all {
border-radius: 0;
}
.loader {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
padding-top: 50vh;
text-align: center;
background-color: rgba(16, 16, 16, 0.5);
}
.loader {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
padding-top: 50vh;
text-align: center;
background-color: rgba(16, 16, 16, 0.5);
}
.loader>img {}
.loader > img {
}
{/literal}
</style>