Files
crm.e5.pl/modules/EcmReports/BimIT-Reports/productionSchedule/productionSchedule.php
2025-07-06 08:39:16 +00:00

248 lines
10 KiB
PHP

<?php
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'])) {
$dateFrom = date("Y-m-d", strtotime($_GET['date_from']));
} else {
$dateFrom = date("Y-m-d");
}
if (isset($_GET['date_to'])) {
$dateTo = date("Y-m-d", strtotime($_GET['date_to']));
} else {
$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";
//echo $query;
$rows = $db->query($query);
$data = array();
$i = 0;
while ($r = $db->fetchByAssoc($rows)) {
$row = array();
$i++;
$row['position'] = $i;
$row['id'] = $r['id'];
$row['productName'] = strlen($r['name']) > 55 ? substr($r['name'], 0, 55) . "..." : $r['name'];
$row['productFullName'] = $r['name'];
$row['productCode'] = strlen($r['code']) > 20 ? substr($r['code'], 0, 20) . "..." : $r['code'];
$row['productFullCode'] = $r['code'];
$row['productId'] = $r['ecmproduct_id'];
$row['productQty'] = $r['quantity'];
$row['qty'] = $r['scheduledQuantity'];
$row['productKind'] = $r['kind'];
$row['productSize'] = $r['size'];
$row['productShape'] = $app_list_strings['ecmproducts_shape_dom'][$r['shape']];
$row['productBrand'] = $app_list_strings['ecmproducts_brand_dom'][$r['brand']];
$row['orderNo'] = $r['document_no'];
$row['orderId'] = $r['orderId'];
$row['orderStatus'] = $app_list_strings['ecmsales_status_dom'][$r['status']];
$row['orderType'] = $app_list_strings['ecmsales_type_dom'][$r['type']];
$row['orderParent'] = $r['parent_name'];
$row['orderParentId'] = $r['parent_id'];
$row['orderSendDate'] = $r['send_date'];
$row['orderItemId'] = $r['item_id'];
$row['description'] = strlen($r['description']) > 0 ? substr($r['description'], 0, 30) : '';
$row['fullDescription'] = $r['description'];
$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;
$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;
}
}
function getRawMaterials($ids) {
$response = array();
$idsString = join("','", $ids);
$db = $GLOBALS['db'];
$res = $db->query("SELECT ecmproduct_id, SUM(quantity) AS quantity FROM productionScheduler WHERE id IN ('$idsString') GROUP BY ecmproduct_id");
$rawMaterials = array();
while ($p = $db->fetchByAssoc($res)) {
$rawMaterials = array_merge($rawMaterials, getProductRawMaterials($p['ecmproduct_id'], $p['quantity']));
}
$groupedRawMaterials = [];
foreach ($rawMaterials as $item) {
$productId = $item['ecmproduct_id'];
if (!isset($groupedRawMaterials[$productId])) {
$groupedRawMaterials[$productId] = 0;
}
$groupedRawMaterials[$productId] += $item['quantity'];
}
$rawMaterials = [];
foreach ($groupedRawMaterials as $productId => $quantity) {
$rawMaterials[] = ['ecmproduct_id' => $productId, 'quantity' => $quantity];
}
global $app_list_strings;
foreach ($rawMaterials as $raw) {
$product = $db->fetchByAssoc($db->query("
SELECT p.id, p.code, p.name, p.group_ks, p.unit_id, ss.quantity as stockState,
GROUP_CONCAT(s.stock_address SEPARATOR ', ') AS stock_addresses
FROM ecmproducts AS p
LEFT JOIN ecmstockstates AS ss
ON ss.product_id = p.id AND ss.stock_id = '368479db-22c5-0220-3a14-4bc426b1c709'
LEFT JOIN ecmproducts_stock_addresses s ON p.id = s.ecmproduct_id
WHERE p.id='" . $raw['ecmproduct_id'] . "'
GROUP BY p.id
"));
$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['fullCode'] = $product['code'];
$result['quantity'] = $raw['quantity'];
$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'] : "";
$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'";
$crows = $db->query($componentsQuery);
if ($crows->num_rows == 0) {
return array(
array(
'ecmproduct_id' => $productId,
'quantity' => $quantity
)
);
} else {
while ($c = $db->fetchByAssoc($crows)) {
$response = array_merge($response, getProductRawMaterials($c['ecmcomponent_id'], $quantity * $c['quantity']));
}
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'];
global $current_user;
$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');",
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'];
global $current_user;
$ids = explode("|", $ids);
$query = sprintf("UPDATE productionScheduler SET deleted=1, modified_user_id='%s', date_modified=NOW() WHERE id IN ('%s')",
$current_user->id, join("','", $ids));
$db->query($query);
}
function saveProductionDate($id, $date)
{
$db = $GLOBALS['db'];
$date = date("Y-m-d", strtotime($date));
if ($date == '1970-01-01') {
$db->query("UPDATE productionScheduler SET production_date=NULL WHERE id='$id'");
} else {
$db->query("UPDATE productionScheduler SET production_date='$date' WHERE id='$id'");
}
}
function saveProductDescription($id, $description)
{
$db = $GLOBALS['db'];
$description = mysql_escape_string($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']
];
echo 'a';
$xlsx = Shuchkin\SimpleXLSXGen::fromArray( $books );
echo json_encode($xlsx);
$xlsx->saveAs('books.xlsx');
echo 'done';
}
function generateUuidV4()
{
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}