328 lines
13 KiB
PHP
328 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\SurfaceAreaConstant;
|
|
use App\Models\PaintFollowUp;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use DB;
|
|
|
|
class SurfaceAreaController extends Controller
|
|
{
|
|
/**
|
|
* Get distinct element types for a given standard
|
|
*/
|
|
public function getElements(Request $request)
|
|
{
|
|
$standard = $request->get('standard', 'ASME');
|
|
|
|
$elements = SurfaceAreaConstant::where('standard_type', $standard)
|
|
->where('is_active', true)
|
|
->select('element_type_en', 'element_type_ru')
|
|
->distinct()
|
|
->orderBy('element_type_en')
|
|
->get();
|
|
|
|
return response()->json($elements);
|
|
}
|
|
|
|
/**
|
|
* Get diameter options for a given standard and element
|
|
*/
|
|
public function getDiameters(Request $request)
|
|
{
|
|
$standard = $request->get('standard', 'ASME');
|
|
$element = $request->get('element');
|
|
|
|
// Element can be either EN or RU, so we need to check both
|
|
$diameters = SurfaceAreaConstant::where('standard_type', $standard)
|
|
->where(function($query) use ($element) {
|
|
$query->where('element_type_en', $element)
|
|
->orWhere('element_type_ru', $element);
|
|
})
|
|
->where('is_active', true)
|
|
->select('diameter_inch', 'diameter_mm', 'surface_area_m2', 'manufacturing_standard', 'element_type_en', 'element_type_ru', 'general_name')
|
|
->orderBy('diameter_mm')
|
|
->get();
|
|
|
|
return response()->json($diameters);
|
|
}
|
|
|
|
/**
|
|
* Get MTO total for a given iso_drawings
|
|
*/
|
|
public function getMtoTotal(Request $request)
|
|
{
|
|
$isoDrawings = $request->get('iso_drawings');
|
|
|
|
if (!$isoDrawings) {
|
|
return response()->json(['error' => 'iso_drawings parameter is required'], 400);
|
|
}
|
|
|
|
// MTO tablosundan pipe verilerini hesapla
|
|
// Formula: SUM(quantity * PI * (odmm_1/2000)^2)
|
|
$mtoTotal = DB::table('m_t_o_s')
|
|
->where('description_en', 'like', '%pipe%')
|
|
->where('line', $isoDrawings)
|
|
->selectRaw('SUM(quantity * POWER(odmm_1/2000, 2) * PI()) AS total')
|
|
->first()->total;
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'iso_drawings' => $isoDrawings,
|
|
'mto_total' => $mtoTotal ?? 0
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get saved surface area items for a construction paint log
|
|
*/
|
|
public function getSavedItems(Request $request)
|
|
{
|
|
$constructionPaintLogId = $request->get('construction_paint_log_id');
|
|
|
|
if (!$constructionPaintLogId) {
|
|
return response()->json(['error' => 'construction_paint_log_id parameter is required'], 400);
|
|
}
|
|
|
|
$paintLog = DB::table('construction_paint_logs')
|
|
->where('id', $constructionPaintLogId)
|
|
->select('surface_area_items', 'fittings_m2', 'pipe_m2', 'total_m2')
|
|
->first();
|
|
|
|
if (!$paintLog) {
|
|
return response()->json(['error' => 'Construction Paint Log not found'], 404);
|
|
}
|
|
|
|
$items = [];
|
|
if ($paintLog->surface_area_items) {
|
|
$items = json_decode($paintLog->surface_area_items, true) ?: [];
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'items' => $items,
|
|
'fittings_m2' => $paintLog->fittings_m2,
|
|
'pipe_m2' => $paintLog->pipe_m2,
|
|
'total_m2' => $paintLog->total_m2
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Save surface area calculation to construction_paint_logs and paint_follow_ups
|
|
*/
|
|
public function save(Request $request)
|
|
{
|
|
$constructionPaintLogId = $request->get('construction_paint_log_id');
|
|
$fittingsTotal = $request->get('fittings_total');
|
|
$pipeTotal = $request->get('pipe_total');
|
|
$grandTotal = $request->get('grand_total');
|
|
$cuttingMm = $request->get('cutting_mm');
|
|
$items = json_decode($request->get('items', '[]'), true);
|
|
$dn1 = $request->get('dn_1');
|
|
$dn2 = $request->get('dn_2');
|
|
$linePipeMm1 = $request->get('line_pipe_mm_1');
|
|
$linePipeMm2 = $request->get('line_pipe_mm_2');
|
|
$totalLineMm = $request->get('total_line_mm');
|
|
|
|
// Calculate total_line_mm if not provided (sum of line_pipe_mm_1 and line_pipe_mm_2)
|
|
if ($totalLineMm === null || $totalLineMm === '') {
|
|
$linePipeMm1Val = (float)($linePipeMm1 ?? 0);
|
|
$linePipeMm2Val = (float)($linePipeMm2 ?? 0);
|
|
$totalLineMm = $linePipeMm1Val + $linePipeMm2Val;
|
|
}
|
|
|
|
// Get construction paint log record
|
|
$paintLog = DB::table('construction_paint_logs')->find($constructionPaintLogId);
|
|
|
|
if (!$paintLog) {
|
|
return response()->json(['error' => 'Construction Paint Log not found'], 404);
|
|
}
|
|
|
|
// Process items to calculate fitting quantities and areas
|
|
$elbowPcs = 0;
|
|
$elbow45Pcs = 0;
|
|
$elbowM2 = 0;
|
|
$teePcs = 0;
|
|
$teeM2 = 0;
|
|
$reductionsPcs = 0;
|
|
$reductionsM2 = 0;
|
|
$flangePcs = 0;
|
|
$flangeM2 = 0;
|
|
$ventPcs = 0;
|
|
$ventM2 = 0;
|
|
$capPcs = 0;
|
|
$capM2 = 0;
|
|
|
|
foreach ($items as $item) {
|
|
$elementEn = $item['element_en'] ?? '';
|
|
$elementRu = $item['element_ru'] ?? '';
|
|
$quantity = (int)($item['quantity'] ?? 0);
|
|
$total = (float)($item['total'] ?? 0);
|
|
|
|
// Check both EN and RU element names (use EN first, fallback to RU)
|
|
$elementName = trim($elementEn ?: $elementRu);
|
|
$elementNameLower = strtolower($elementName);
|
|
|
|
// Elbow 90° 1.5D
|
|
if (stripos($elementNameLower, 'elbow') !== false &&
|
|
(stripos($elementNameLower, '90') !== false || stripos($elementName, '90°') !== false)) {
|
|
$elbowPcs += $quantity;
|
|
$elbowM2 += $total;
|
|
}
|
|
// Elbow 45° 1.5D
|
|
elseif (stripos($elementNameLower, 'elbow') !== false &&
|
|
(stripos($elementNameLower, '45') !== false || stripos($elementName, '45°') !== false)) {
|
|
$elbow45Pcs += $quantity;
|
|
$elbowM2 += $total;
|
|
}
|
|
// Reductions (Concentric, Eccentric, any reducer)
|
|
elseif (stripos($elementNameLower, 'reducer') !== false ||
|
|
stripos($elementNameLower, 'reduction') !== false) {
|
|
$reductionsPcs += $quantity;
|
|
$reductionsM2 += $total;
|
|
}
|
|
// Slip On Flange (Class 150)
|
|
elseif (stripos($elementNameLower, 'slip on flange') !== false ||
|
|
(stripos($elementNameLower, 'slip') !== false && stripos($elementNameLower, 'flange') !== false)) {
|
|
$flangePcs += $quantity;
|
|
$flangeM2 += $total;
|
|
}
|
|
// Weldneck Flange (Class 150)
|
|
elseif (stripos($elementNameLower, 'weldneck flange') !== false ||
|
|
(stripos($elementNameLower, 'weldneck') !== false && stripos($elementNameLower, 'flange') !== false)) {
|
|
$flangePcs += $quantity;
|
|
$flangeM2 += $total;
|
|
}
|
|
// Tee (Equal Tee, Reducing Tee, Cross Tee - all types)
|
|
elseif (stripos($elementNameLower, 'tee') !== false ||
|
|
stripos($elementNameLower, 'cross') !== false) {
|
|
$teePcs += $quantity;
|
|
$teeM2 += $total;
|
|
}
|
|
// Nipple/Olet (replaces Vent)
|
|
elseif (stripos($elementNameLower, 'nipple') !== false ||
|
|
stripos($elementNameLower, 'olet') !== false ||
|
|
stripos($elementNameLower, 'nipel') !== false ||
|
|
stripos($elementNameLower, 'nippel') !== false) {
|
|
$ventPcs += $quantity;
|
|
$ventM2 += $total;
|
|
}
|
|
// Cap
|
|
elseif (stripos($elementNameLower, 'cap') !== false) {
|
|
$capPcs += $quantity;
|
|
$capM2 += $total;
|
|
}
|
|
}
|
|
|
|
// Prepare update data
|
|
$updateData = [
|
|
'fittings_m2' => $fittingsTotal,
|
|
'pipe_m2' => $pipeTotal,
|
|
'total_m2' => $grandTotal,
|
|
'cutting_mm' => $cuttingMm,
|
|
'surface_area_items' => json_encode($items),
|
|
'elbow_pcs' => $elbowPcs,
|
|
'elbow_45_pcs' => $elbow45Pcs,
|
|
'elbow_m2' => round($elbowM2, 2),
|
|
'tee_pcs' => $teePcs,
|
|
'tee_m2' => round($teeM2, 2),
|
|
'reductions_pcs' => $reductionsPcs,
|
|
'reductions_m2' => round($reductionsM2, 2),
|
|
'flange_pcs' => $flangePcs,
|
|
'flange_m2' => round($flangeM2, 2),
|
|
'vent_pcs' => $ventPcs,
|
|
'vent_m2' => round($ventM2, 2),
|
|
'cap_pcs' => $capPcs,
|
|
'cap_m2' => round($capM2, 2),
|
|
'updated_at' => now()
|
|
];
|
|
|
|
// Add pipe dimensions if provided (only if not empty and not zero)
|
|
if ($dn1 !== null && $dn1 !== '') {
|
|
$updateData['dn_1'] = $dn1;
|
|
}
|
|
if ($dn2 !== null && $dn2 !== '') {
|
|
$updateData['dn_2'] = $dn2;
|
|
}
|
|
|
|
// Check if columns exist before updating line_pipe_mm fields
|
|
$hasLinePipeMm1 = Schema::hasColumn('construction_paint_logs', 'line_pipe_mm_1');
|
|
$hasLinePipeMm2 = Schema::hasColumn('construction_paint_logs', 'line_pipe_mm_2');
|
|
$hasTotalLineMm = Schema::hasColumn('construction_paint_logs', 'total_line_mm');
|
|
|
|
if ($hasLinePipeMm1 && $linePipeMm1 !== null && $linePipeMm1 !== '' && $linePipeMm1 != 0) {
|
|
$updateData['line_pipe_mm_1'] = (int)$linePipeMm1;
|
|
}
|
|
if ($hasLinePipeMm2 && $linePipeMm2 !== null && $linePipeMm2 !== '' && $linePipeMm2 != 0) {
|
|
$updateData['line_pipe_mm_2'] = (int)$linePipeMm2;
|
|
}
|
|
// Add total_line_mm (sum of line_pipe_mm_1 and line_pipe_mm_2)
|
|
// Only update if total is greater than 0 and column exists
|
|
if ($hasTotalLineMm && $totalLineMm !== null && $totalLineMm > 0) {
|
|
$updateData['total_line_mm'] = (float)$totalLineMm;
|
|
}
|
|
|
|
// Update construction paint log with surface area values and fitting details
|
|
DB::table('construction_paint_logs')
|
|
->where('id', $constructionPaintLogId)
|
|
->update($updateData);
|
|
|
|
// Find or create paint follow up record based on spool/line
|
|
$paintFollowUp = PaintFollowUp::where('spool_no_joint_no', $paintLog->spool)
|
|
->where('line', $paintLog->line)
|
|
->first();
|
|
|
|
$paintFollowUpAction = '';
|
|
$paintFollowUpId = null;
|
|
|
|
if ($paintFollowUp) {
|
|
$paintFollowUp->update([
|
|
'volume_1' => $grandTotal,
|
|
'volume_2' => $grandTotal,
|
|
'volume_3' => $grandTotal,
|
|
'total_volume' => $grandTotal * 3,
|
|
]);
|
|
$paintFollowUpAction = 'updated';
|
|
$paintFollowUpId = $paintFollowUp->id;
|
|
} else {
|
|
$newRecord = PaintFollowUp::create([
|
|
'line' => $paintLog->line,
|
|
'iso_number' => $paintLog->iso_drawings,
|
|
'spool_no_joint_no' => $paintLog->spool,
|
|
'volume_1' => $grandTotal,
|
|
'volume_2' => $grandTotal,
|
|
'volume_3' => $grandTotal,
|
|
'total_volume' => $grandTotal * 3,
|
|
]);
|
|
$paintFollowUpAction = 'created';
|
|
$paintFollowUpId = $newRecord->id;
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Surface area saved successfully',
|
|
'construction_paint_log' => [
|
|
'id' => $constructionPaintLogId,
|
|
'spool' => $paintLog->spool,
|
|
'line' => $paintLog->line,
|
|
'fittings_m2' => $fittingsTotal,
|
|
'pipe_m2' => $pipeTotal,
|
|
'total_m2' => $grandTotal
|
|
],
|
|
'paint_follow_up' => [
|
|
'action' => $paintFollowUpAction,
|
|
'id' => $paintFollowUpId,
|
|
'spool_no_joint_no' => $paintLog->spool,
|
|
'line' => $paintLog->line,
|
|
'volume_1' => $grandTotal,
|
|
'volume_2' => $grandTotal,
|
|
'volume_3' => $grandTotal,
|
|
'total_volume' => $grandTotal * 3
|
|
]
|
|
]);
|
|
}
|
|
}
|