Production changes

This commit is contained in:
Michał Zieliński
2025-07-09 19:31:37 +02:00
parent a3408d62f6
commit c493147113
242 changed files with 73354 additions and 102 deletions

View File

@@ -3,9 +3,6 @@ ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
$db = $GLOBALS['db'];
global $app_list_strings;
if (!isset($_GET['ajaxAction'])) {
if (isset($_GET['date_from'])) {
@@ -20,27 +17,80 @@ if (!isset($_GET['ajaxAction'])) {
$dateTo = date("Y-m-d", strtotime("+ 1 month"));
}
$query = "SELECT s.id as orderId, s.document_no, s.status, s.parent_name, s.parent_id, s.send_date, s.type,
p.code, p.name, ps.ecmproduct_id, ps.description,
si.quantity, p.vendor_part_no as kind, p.brand, p.part_no as size, p.shape,
ss.quantity as stockState, si.id as item_id, s.shipping_address_name, ps.production_date,
ps.quantity as scheduledQuantity, ps.id as id
FROM productionScheduler AS ps
LEFT JOIN ecmsaleitems AS si
ON ps.ecmsaleitem_id = si.id
LEFT JOIN ecmsales AS s
ON ps.ecmsale_id = s.id
INNER JOIN ecmproducts AS p
ON ps.ecmproduct_id = p.id
LEFT JOIN ecmstockstates AS ss
ON ss.product_id = ps.ecmproduct_id AND ss.stock_id = 'c7afd71a-4c3a-bde4-138d-4acaee1644e4'
WHERE ps.deleted = 0 AND
(
(ps.production_date >= '$dateFrom' AND ps.production_date <= '$dateTo')
OR ps.production_date IS NULL OR ps.production_date = '0000-00-00')
ORDER BY s.delivery_date, s.register_date, s.document_no";
$data = loadSchedulers($dateFrom, $dateTo);
//echo $query;
$smarty = new Sugar_Smarty();
$smarty->assign("data", $data);
$smarty->assign("dateFrom", $GLOBALS['timedate']->to_display_date($dateFrom));
$smarty->assign("dateTo", $GLOBALS['timedate']->to_display_date($dateTo));
echo $smarty->display('modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.tpl');
} else {
switch ($_GET['ajaxAction']) {
case 'saveQty':
saveQty($_GET['id'], $_GET['qty']);
break;
case 'duplicatePositions':
duplicatePositions($_GET['ids']);
break;
case 'removePositions':
removePositions($_GET['ids']);
break;
case 'saveProductionDate':
saveProductionDate($_GET['id'], $_GET['date']);
break;
case 'saveProductDescription':
saveProductDescription($_GET['id'], $_GET['description']);
break;
case 'exportExcel':
exportExcel($_GET['ids']);
break;
case 'exportPDF':
exportPDF($_GET['ids']);
break;
case 'getRawMaterials':
getRawMaterials($_POST['ids']);
break;
}
}
function loadSchedulers($dateFrom = null, $dateTo = null, $ids = null)
{
$db = $GLOBALS['db'];
global $app_list_strings;
$query = "SELECT s.id as orderId, s.document_no, s.status, s.parent_name, s.parent_id, s.send_date, s.type,
p.code, p.name, ps.ecmproduct_id, ps.description,
si.quantity, p.vendor_part_no as kind, p.brand, p.part_no as size, p.shape,
ss.quantity as stockState, si.id as item_id, s.shipping_address_name, ps.production_date,
ps.quantity as scheduledQuantity, ps.id as id,
ps.main_id,
ps_main.ecmproduct_id as main_product_id,
p_main.code as main_product_code
FROM productionScheduler AS ps
LEFT JOIN ecmsaleitems AS si
ON ps.ecmsaleitem_id = si.id
LEFT JOIN ecmsales AS s
ON ps.ecmsale_id = s.id
INNER JOIN ecmproducts AS p
ON ps.ecmproduct_id = p.id
LEFT JOIN ecmstockstates AS ss
ON ss.product_id = ps.ecmproduct_id AND ss.stock_id = 'c7afd71a-4c3a-bde4-138d-4acaee1644e4'
LEFT JOIN productionScheduler AS ps_main
ON ps.main_id = ps_main.id
LEFT JOIN ecmproducts AS p_main
ON ps_main.ecmproduct_id = p_main.id
WHERE ps.deleted = 0 ";
if ($dateFrom != null && $dateTo != null) {
$query .= " AND (
(ps.production_date >= '$dateFrom' AND ps.production_date <= '$dateTo')
OR ps.production_date IS NULL OR ps.production_date = '0000-00-00')";
}
if ($ids != null) {
$query .= " AND ps.id IN ('" . join("','", $ids) . "')";
}
$query .= "ORDER BY s.delivery_date, s.register_date, s.document_no";
$rows = $db->query($query);
$data = array();
@@ -74,41 +124,16 @@ if (!isset($_GET['ajaxAction'])) {
$row['shippingTo'] = ($r['shipping_address_name'] == 'Adres korespondencyjny' ? '' : $r['shipping_address_name']);
$row['productionDate'] = $r['production_date'] == '0000-00-00' ? '' : $r['production_date'];
$row['productStockState'] = $r['stockState'] | 0;
$row['mainProductId'] = $r['main_product_id'];
$row['mainProductCode'] = $r['main_product_code'];
$data[] = $row;
}
$smarty = new Sugar_Smarty();
$smarty->assign("data", $data);
$smarty->assign("dateFrom", $GLOBALS['timedate']->to_display_date($dateFrom));
$smarty->assign("dateTo", $GLOBALS['timedate']->to_display_date($dateTo));
echo $smarty->display('modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.tpl');
} else {
switch ($_GET['ajaxAction']) {
case 'saveQty':
saveQty($_GET['id'], $_GET['qty']);
break;
case 'duplicatePositions':
duplicatePositions($_GET['ids']);
break;
case 'removePositions':
removePositions($_GET['ids']);
break;
case 'saveProductionDate':
saveProductionDate($_GET['id'], $_GET['date']);
break;
case 'saveProductDescription':
saveProductDescription($_GET['id'], $_GET['description']);
break;
case 'exportExcel':
exportExcel();
break;
case 'getRawMaterials':
getRawMaterials($_POST['ids']);
break;
}
return $data;
}
function getRawMaterials($ids) {
function getRawMaterials($ids)
{
$response = array();
$idsString = join("','", $ids);
$db = $GLOBALS['db'];
@@ -122,16 +147,24 @@ function getRawMaterials($ids) {
foreach ($rawMaterials as $item) {
$productId = $item['ecmproduct_id'];
if (!isset($groupedRawMaterials[$productId])) {
$groupedRawMaterials[$productId] = 0;
$groupedRawMaterials[$productId] = [
'quantity' => 0,
'isComponent' => $item['isComponent']
];
}
$groupedRawMaterials[$productId] += $item['quantity'];
$groupedRawMaterials[$productId]['quantity'] += $item['quantity'];
}
$rawMaterials = [];
foreach ($groupedRawMaterials as $productId => $quantity) {
$rawMaterials[] = ['ecmproduct_id' => $productId, 'quantity' => $quantity];
foreach ($groupedRawMaterials as $productId => $data) {
$rawMaterials[] = [
'ecmproduct_id' => $productId,
'quantity' => $data['quantity'],
'isComponent' => $data['isComponent'] // Include isComponent in the final array
];
}
global $app_list_strings;
foreach ($rawMaterials as $raw) {
$product = $db->fetchByAssoc($db->query("
@@ -147,44 +180,54 @@ function getRawMaterials($ids) {
$result['id'] = $product['id'];
$result['name'] = strlen($product['name']) > 55 ? substr($product['name'], 0, 55) . "..." : $product['name'];
$result['fullName'] = $product['name'];
$result['code'] = strlen($product['code']) > 20 ? substr($product['code'], 0, 20) . "..." : $product['code'];
$result['code'] = strlen($product['code']) > 55 ? substr($product['code'], 0, 20) . "..." : $product['code'];
$result['fullCode'] = $product['code'];
$result['quantity'] = $raw['quantity'];
$result['isComponent'] = $raw['isComponent'];
$result['unit'] = $app_list_strings['ecmproducts_unit_dom'][$product['unit_id']];
$result['stockState'] = (!empty($product['stockState'])) ? $product['stockState'] : 0;
$result['stockAddress'] = (!empty($product['stock_addresses'])) ? $product['stock_addresses'] : "";
$result['stockAddress'] = (!empty($product['stock_addresses'])) ? $product['stock_addresses'] : "";
$response[] = $result;
}
echo json_encode($response);
}
function getProductRawMaterials($productId, $quantity)
{
$db = $GLOBALS['db'];
$response = array();
$componentsQuery = "SELECT c.ecmcomponent_id, c.quantity
FROM ecmproductcomponents as c
WHERE c.ecmproduct_id = '$productId'";
$componentsQuery = "SELECT
c.ecmcomponent_id,
c.quantity,
(
SELECT COUNT(*)
FROM ecmproductcomponents as nested
WHERE nested.ecmproduct_id = c.ecmcomponent_id
) as nested_components_count
FROM
ecmproductcomponents as c
WHERE
c.ecmproduct_id = '$productId'";
$crows = $db->query($componentsQuery);
if ($crows->num_rows == 0) {
return array(
array(
'ecmproduct_id' => $productId,
'quantity' => $quantity
)
while ($c = $db->fetchByAssoc($crows)) {
$response[] = array(
'ecmproduct_id' => $c['ecmcomponent_id'],
'quantity' => $quantity * $c['quantity'],
'isComponent' => $c['nested_components_count'] > 0
);
} else {
while ($c = $db->fetchByAssoc($crows)) {
$response = array_merge($response, getProductRawMaterials($c['ecmcomponent_id'], $quantity * $c['quantity']));
}
return $response;
}
return $response;
}
function saveQty($id, $qty)
{
$db = $GLOBALS['db'];
$query = sprintf("UPDATE productionScheduler SET quantity='%d' WHERE id='%s'", $qty, $id);
$db->query($query);
}
function duplicatePositions($ids)
{
$db = $GLOBALS['db'];
@@ -192,12 +235,13 @@ function duplicatePositions($ids)
$ids = explode("|", $ids);
foreach ($ids as $id) {
$res = $db->fetchByAssoc($db->query("SELECT * FROM productionScheduler WHERE id ='$id'"));
$query = sprintf("INSERT INTO productionScheduler VALUES ('%s', '%s', '%s', '%d', NOW(), NOW(), '%s', '%s', 0, '%s', '%d', '%s', '%s');",
$query = sprintf("INSERT INTO productionScheduler VALUES ('%s', null, '%s', '%s', '%d', NOW(), NOW(), '%s', '%s', 0, '%s', '%d', '%s', '%s');",
generateUuidV4(), $res['ecmsaleitem_id'], $res['ecmsale_id'], $res['quantity'], $current_user->id, $current_user->id, $res['ecmproduct_id'],
$res['is_component'], $res['production_date'], $res['description']);
$db->query($query);
}
}
function removePositions($ids)
{
$db = $GLOBALS['db'];
@@ -207,6 +251,7 @@ function removePositions($ids)
$current_user->id, join("','", $ids));
$db->query($query);
}
function saveProductionDate($id, $date)
{
$db = $GLOBALS['db'];
@@ -217,6 +262,7 @@ function saveProductionDate($id, $date)
$db->query("UPDATE productionScheduler SET production_date='$date' WHERE id='$id'");
}
}
function saveProductDescription($id, $description)
{
$db = $GLOBALS['db'];
@@ -224,21 +270,166 @@ function saveProductDescription($id, $description)
$db->query("UPDATE productionScheduler SET description='$description' WHERE id='$id'");
echo "UPDATE productionScheduler SET description='$description' WHERE id='$id'";
}
function exportExcel() {
echo 'bhhh';
require_once 'modules/EcmReports/BimIT-Reports/lib/xlsxGenerator.php';
echo 'qqqqqq';
$books = [
['ISBN', 'title', 'author', 'publisher', 'ctry' ],
[618260307, 'The Hobbit', 'J. R. R. Tolkien', 'Houghton Mifflin', 'USA'],
[908606664, 'Slinky Malinki', 'Lynley Dodd', 'Mallinson Rendel', 'NZ']
function exportExcel($ids)
{
$headers = [
'Indeks', 'Nazwa', 'Data produkcji', 'Ilość', 'Ilość ZS', 'Stan', 'ZS E5', 'Typ',
'Status', 'Produkt główny', 'Odbiorca', 'Forma', 'Marka', 'Data wysyłki', 'Uwagi'
];
echo 'a';
$xlsx = Shuchkin\SimpleXLSXGen::fromArray( $books );
echo json_encode($xlsx);
$xlsx->saveAs('books.xlsx');
echo 'done';
$sheetData = [$headers];
$ids = explode("|", $ids);
$data = loadSchedulers(null, null, $ids);
foreach ($data as $row) {
$sheetRow = [];
$sheetRow[] = $row['productFullCode'];
$sheetRow[] = $row['productFullName'];
$sheetRow[] = $row['productionDate'];
$sheetRow[] = $row['qty'];
$sheetRow[] = $row['productQty'];
$sheetRow[] = $row['productStockState'];
$sheetRow[] = $row['orderNo'];
$sheetRow[] = $row['orderType'];
$sheetRow[] = $row['orderStatus'];
$sheetRow[] = $row['mainProductCode'];
$sheetRow[] = $row['shippingTo'];
$sheetRow[] = $row['productShape'];
$sheetRow[] = $row['productBrand'];
$sheetRow[] = $row['orderSendDate'];
$sheetRow[] = $row['fullDescription'];
$sheetData[] = $sheetRow;
}
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' => 'harmonogram_produkcji.xlsx'
]);
}
function exportPDF($ids)
{
require_once 'modules/EcmReports/BimIT-Reports/lib/tcpdf/tcpdf.php';
$pdf = new TCPDF('L', 'mm', 'A4', true, 'UTF-8');
$pdf->SetCreator('CRM E5');
$pdf->SetAuthor('System E5');
$pdf->SetTitle('Harmonogram produkcji');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();
$pdf->SetFont('dejavusans', '', 5);
$headers = [
'Indeks', 'Nazwa', 'Data produkcji', 'Ilość', 'Ilość ZS', 'Stan', 'ZS E5', 'Typ',
'Status', 'Produkt główny', 'Odbiorca', 'Forma', 'Marka', 'Data wysyłki', 'Uwagi'
];
$ids = explode("|", $ids);
$data = loadSchedulers(null, null, $ids);
$widths = [20, 30, 20, 15, 15, 15, 15, 15, 20, 20, 25, 15, 15, 20, 20];
$pdf->SetFillColor(224, 224, 224);
$pdf->SetFont('dejavusans', 'B', 5);
$pdf->Cell($widths[0], 10, $headers[0], 1, 0, 'C', 1);
$pdf->Cell($widths[1], 10, $headers[1], 1, 0, 'C', 1);
$pdf->Cell($widths[2], 10, $headers[2], 1, 0, 'C', 1);
$pdf->Cell($widths[3], 10, $headers[3], 1, 0, 'C', 1);
$pdf->Cell($widths[4], 10, $headers[4], 1, 0, 'C', 1);
$pdf->Cell($widths[5], 10, $headers[5], 1, 0, 'C', 1);
$pdf->Cell($widths[6], 10, $headers[6], 1, 0, 'C', 1);
$pdf->Cell($widths[7], 10, $headers[7], 1, 0, 'C', 1);
$pdf->Cell($widths[8], 10, $headers[8], 1, 0, 'C', 1);
$pdf->Cell($widths[9], 10, $headers[9], 1, 0, 'C', 1);
$pdf->Cell($widths[10], 10, $headers[10], 1, 0, 'C', 1);
$pdf->Cell($widths[11], 10, $headers[11], 1, 0, 'C', 1);
$pdf->Cell($widths[12], 10, $headers[12], 1, 0, 'C', 1);
$pdf->Cell($widths[13], 10, $headers[13], 1, 0, 'C', 1);
$pdf->Cell($widths[14], 10, $headers[14], 1, 1, 'C', 1);
$pdf->SetFont('dejavusans', '', 5);
$pdf->SetFillColor(255, 255, 255);
foreach ($data as $row) {
if ($pdf->getY() > 250) {
$pdf->AddPage();
$pdf->SetFont('dejavusans', 'B', 10);
$pdf->SetFillColor(224, 224, 224);
$pdf->Cell($widths[0], 10, $headers[0], 1, 0, 'C', 1);
$pdf->Cell($widths[1], 10, $headers[1], 1, 0, 'C', 1);
$pdf->Cell($widths[2], 10, $headers[2], 1, 0, 'C', 1);
$pdf->Cell($widths[3], 10, $headers[3], 1, 0, 'C', 1);
$pdf->Cell($widths[4], 10, $headers[4], 1, 0, 'C', 1);
$pdf->Cell($widths[5], 10, $headers[5], 1, 0, 'C', 1);
$pdf->Cell($widths[6], 10, $headers[6], 1, 0, 'C', 1);
$pdf->Cell($widths[7], 10, $headers[7], 1, 0, 'C', 1);
$pdf->Cell($widths[8], 10, $headers[8], 1, 0, 'C', 1);
$pdf->Cell($widths[9], 10, $headers[9], 1, 0, 'C', 1);
$pdf->Cell($widths[10], 10, $headers[10], 1, 0, 'C', 1);
$pdf->Cell($widths[11], 10, $headers[11], 1, 0, 'C', 1);
$pdf->Cell($widths[12], 10, $headers[12], 1, 0, 'C', 1);
$pdf->Cell($widths[13], 10, $headers[13], 1, 0, 'C', 1);
$pdf->Cell($widths[14], 10, $headers[14], 1, 1, 'C', 1);
$pdf->SetFont('dejavusans', '', 9);
$pdf->SetFillColor(255, 255, 255);
}
$startY = $pdf->GetY();
$currentPage = $pdf->getPage();
$pdf->MultiCell($widths[0], 10, $row['productFullCode'], 1, 'L', 1, 0);
if ($currentPage != $pdf->getPage()) {
$startY = $pdf->GetY();
$currentPage = $pdf->getPage();
}
$pdf->SetXY($pdf->GetX(), $startY);
$pdf->MultiCell($widths[1], 10, $row['productFullName'], 1, 'L', 1, 0);
$pdf->SetXY($pdf->GetX(), $startY);
$pdf->Cell($widths[2], 10, $row['productionDate'], 1, 0, 'L', 1);
$pdf->Cell($widths[3], 10, $row['qty'], 1, 0, 'R', 1);
$pdf->Cell($widths[4], 10, $row['productQty'], 1, 0, 'R', 1);
$pdf->Cell($widths[5], 10, $row['productStockState'], 1, 0, 'R', 1);
$pdf->Cell($widths[6], 10, $row['orderNo'], 1, 0, 'L', 1);
$pdf->Cell($widths[7], 10, $row['orderType'], 1, 0, 'L', 1);
$pdf->Cell($widths[8], 10, $row['orderStatus'], 1, 0, 'L', 1);
$pdf->Cell($widths[9], 10, $row['mainProductCode'], 1, 0, 'L', 1);
$pdf->Cell($widths[10], 10, $row['shippingTo'], 1, 0, 'L', 1);
$pdf->Cell($widths[11], 10, $row['productShape'], 1, 0, 'L', 1);
$pdf->Cell($widths[12], 10, $row['productBrand'], 1, 0, 'L', 1);
$pdf->Cell($widths[13], 10, $row['orderSendDate'], 1, 0, 'L', 1);
$pdf->Cell($widths[14], 10, $row['fullDescription'], 1, 1, 'L', 1);
}
$pdf_content = $pdf->Output('', 'S');
header('Content-Type: application/json');
echo json_encode([
'success' => true,
'fileContent' => base64_encode($pdf_content),
'fileName' => 'harmonogram_produkcji.pdf'
]);
}
function generateUuidV4()
{
$data = openssl_random_pseudo_bytes(16);