generate pdf with state and station ID

This commit is contained in:
misya 2025-04-17 10:29:35 +08:00
parent 58434ccc1d
commit 7f4d39d2dd
5 changed files with 372 additions and 288 deletions

View File

@ -6,29 +6,27 @@ using PSTW_CentralSystem.Models;
using PSTW_CentralSystem.Areas.MMS;
using System.Linq;
using QuestPDF.Fluent;
using PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator;
namespace PSTW_CentralSystem.Areas.MMS.Controllers
{
[Area("MMS")]
//[Authorize(Policy = "RoleModulePolicy")]
public class MarineController : Controller
public class MarineController(MMSSystemContext context) : Controller
{
private readonly MMSSystemContext _context;
public MarineController(MMSSystemContext context)
{
_context = context;
}
private readonly MMSSystemContext _context = context;
public IActionResult Index()
{
return View(); // This will look for Index.cshtml in Areas/MMS/Views/Marine
}
public IActionResult TarBallForm()
{
var marineTarballs = _context.MarineTarballs
.Select(t => new
{
t.Id, //Include Id property
t.Id, // Include Id property
Date = t.DateSample,
Station = t.StationID
})
@ -46,26 +44,34 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
public IActionResult GenerateReport(int id)
{
Console.WriteLine($"Requested ID in GenerateReport: {id}"); // Log the ID
try
{
// Retrieve the specific record based on the id
var tarballData = _context.MarineTarballs
.Where(t => t.Id == id)
.Select(t => new
// Retrieve the specific record based on the id, including the related StateID
var tarballData = (from marine in _context.MarineTarballs
join station in _context.TarballStations
on marine.StationID equals station.StationID
join state in _context.States
on station.StateID equals state.StateID
where marine.Id == id
select new
{
t.StationID,
t.DateSample
})
.FirstOrDefault();
marine.StationID,
marine.DateSample,
state.StateName // Get the full state name
}).FirstOrDefault();
if (tarballData == null)
{
return NotFound("The specified record was not found.");
}
Console.WriteLine($"Found Record: StationID = {tarballData.StationID}, StateName = {tarballData.StateName}");
// Generate the PDF
var document = new TarBallPDF(); // Customize the report if needed
var pdf = document.GeneratePdf();
var pdfdocument = new TarBallPDF(tarballData.StationID, tarballData.StateName); // Pass StateID to the PDF generator
var pdf = pdfdocument.GeneratePdf();
// Construct the filename
var formattedDate = tarballData.DateSample.ToString("ddMMyyyy");
@ -83,28 +89,39 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
public IActionResult ViewPDF(int id)
{
Console.WriteLine("testing"); //TESTING DEBUGGING
Console.WriteLine($"Requested ID in ViewReport: {id}"); // Log the ID
try
{
// Retrieve the specific record based on the id
var tarballData = _context.MarineTarballs
.Where(t => t.Id == id)
.Select(t => new
// Retrieve the specific record based on the id, including the related StateID
var tarballData = (from marine in _context.MarineTarballs
join station in _context.TarballStations
on marine.StationID equals station.StationID into StationGroup
from station in StationGroup.DefaultIfEmpty() //Handle missing
join state in _context.States
on station.StateID equals state.StateID
where marine.Id == id
select new
{
t.StationID,
t.DateSample
})
.FirstOrDefault();
marine.StationID,
marine.DateSample,
StationExists = station != null,
state.StateName // Get the full state name
}).FirstOrDefault();
if (tarballData == null)
{
return NotFound("The specified record was not found.");
}
Console.WriteLine($"Found Record: StationID = {tarballData.StationID}, StateName = {tarballData.StateName}");
// Generate the PDF
var document = new TarBallPDF(); // Customize the PDF if needed
var document = new TarBallPDF(tarballData.StationID, tarballData.StateName); // Pass StateID to the PDF generator
var pdf = document.GeneratePdf();
//For filename
// Construct the filename
var formattedDate = tarballData.DateSample.ToString("ddMMyyyy");
var fileName = $"TbReport_{tarballData.StationID}_{formattedDate}.pdf";
@ -117,6 +134,5 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers
return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while viewing the PDF. " + ex.Message);
}
}
}
}

View File

@ -1,17 +1,30 @@
using QuestPDF.Fluent;
using QuestPDF.Infrastructure;
using QuestPDF.Helpers;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
public class TarBallPDF : IDocument
namespace PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator
{
public DocumentMetadata GetMetadata() => new DocumentMetadata
public class TarBallPDF : IDocument
{
private readonly string _stationId;
private readonly string _stateName;
// Constructor
public TarBallPDF(string stationId, string stateName)
{
_stationId = stationId;
_stateName = stateName;
}
// Metadata for the PDF document
public DocumentMetadata GetMetadata() => new()
{
Title = "TarBall Sampling Form",
Author = "PAKAR SCIENO TW Integrated Environmental Solutions",
Subject = "Environmental Survey and Observations"
};
// Compose the PDF content
public void Compose(IDocumentContainer container)
{
container.Page(page =>
@ -27,23 +40,22 @@ public class TarBallPDF : IDocument
row.RelativeItem(1).Element(CellStyle).Column(column =>
{
column.Item()
.AlignMiddle() // Ensures vertical centering
.AlignCenter() // Ensures horizontal centering
.Text("Logo Placeholder"); // Placeholder for the logo
.AlignMiddle()
.AlignCenter()
.Text("Logo Placeholder");
});
row.RelativeItem(1).Element(CellStyle)
.AlignMiddle() // Ensures vertical centering
.AlignCenter() // Ensures horizontal centering
.AlignMiddle()
.AlignCenter()
.Text("TARBALL SAMPLING FORM")
.FontSize(16)
.FontColor("#4B0082");
row.RelativeItem(1).Column(column =>
{
column.Spacing(0); // Adds consistent spacing between rows
column.Spacing(0);
// Document Row
column.Item().Row(innerRow =>
{
innerRow.RelativeItem(1).Element(CellStyle).Text("Document:")
@ -65,12 +77,10 @@ public class TarBallPDF : IDocument
innerRow.RelativeItem(1).Element(CellStyle).Text("02")
.AlignLeft();
});
});
// Define styles
static IContainer CellStyle(IContainer container)
=> container.Border(0.5f).Padding(5); // Retains padding for text
=> container.Border(0.5f).Padding(5);
});
// Content Section
@ -80,7 +90,7 @@ public class TarBallPDF : IDocument
column.Item().Element(container =>
{
container
.PaddingTop(20) // Adds space above the text
.PaddingTop(20)
.PaddingBottom(10)
.Text("Please be informed that we have observed the following conditions:");
});
@ -90,42 +100,41 @@ public class TarBallPDF : IDocument
table.ColumnsDefinition(columns =>
{
columns.RelativeColumn(3); // Label/Header column
columns.RelativeColumn(3); // Data Entry column
columns.RelativeColumn(3);
columns.RelativeColumn(3);
});
// Table Rows
table.Cell()
.Background(Colors.Grey.Lighten2).Element(CellStyle).Text("STATE")
.Bold();
table.Cell().Element(CellStyle).Text(""); // Empty cell for data entry
table.Cell().Element(CellStyle).Text(_stateName);
table.Cell()
.Background(Colors.Grey.Lighten2).Element(CellStyle).Text("STATION ID")
.Bold();
table.Cell().Element(CellStyle).Text(""); // Empty cell for data entry
table.Cell().Element(CellStyle).Text(_stationId);
table.Cell()
.Background(Colors.Grey.Lighten2).Element(CellStyle).Text("LOCATION")
.Bold();
table.Cell().Element(CellStyle).Text(""); // Empty cell for data entry
table.Cell().Element(CellStyle).Text("");
table.Cell()
.Background(Colors.Grey.Lighten2).Element(CellStyle).Text("TARBALL SURVEY LOCATION LONGITUDE & LATITUDE")
.Bold();
table.Cell().Element(CellStyle).Text(""); // Empty cell for data entry
table.Cell().Element(CellStyle).Text("");
table.Cell()
.Background(Colors.Grey.Lighten2).Element(CellStyle).Text("DATE / TIME")
.Bold();
table.Cell().Element(CellStyle).Text(""); // Empty cell for data entry
table.Cell().Element(CellStyle).Text("");
});
column.Spacing(3);
// Survey Findings
column.Item()
.PaddingTop(10) // Adds space above the text
.PaddingTop(10)
.PaddingBottom(10)
.Text("SURVEY FINDING:")
.Bold().FontSize(12);
@ -146,22 +155,21 @@ public class TarBallPDF : IDocument
column.Item()
.PaddingBottom(5)
.Text("PHOTOGRAPHS OF SAMPLING")
.AlignCenter() // Ensures horizontal centering
.AlignCenter()
.Bold()
.FontSize(14);
// Table for Photos with Existing Heights
// Table for Photos
column.Item().Table(table =>
{
column.Spacing(0); // No extra spacing between rows
column.Spacing(0);
table.ColumnsDefinition(columns =>
{
columns.RelativeColumn(1); // First column
columns.RelativeColumn(1); // Second column
columns.RelativeColumn(1);
columns.RelativeColumn(1);
});
// For pictures
table.Cell().Element(CellStyle).Height(150);
table.Cell().Element(CellStyle).Height(150);
@ -179,34 +187,25 @@ public class TarBallPDF : IDocument
column.Item().PageBreak();
// Additional Photos Section
column.Item()
.PaddingBottom(5)
.AlignCenter(); // Ensures horizontal centering
// Table for Additional Photos
column.Item().Table(table =>
{
column.Spacing(0); // No extra spacing between rows
column.Spacing(0);
table.ColumnsDefinition(columns =>
{
columns.RelativeColumn(1); // First column
columns.RelativeColumn(1); // Second column
columns.RelativeColumn(1);
columns.RelativeColumn(1);
});
// Row 1: Empty spaces for pictures
table.Cell().Element(CellStyle).Height(150);
table.Cell().Element(CellStyle).Height(150);
// Row 2: Labels for the pictures
table.Cell().Element(CellStyle).Text("Figure 5:").FontSize(12).AlignLeft();
table.Cell().Element(CellStyle).Text("Figure 6:").FontSize(12).AlignLeft();
// Row 3: Empty spaces for pictures
table.Cell().Element(CellStyle).Height(150);
table.Cell().Element(CellStyle).Height(150);
// Row 4: Labels for the pictures
table.Cell().Element(CellStyle).Text("Figure 7:").FontSize(12).AlignLeft();
table.Cell().Element(CellStyle).Text("Figure 8:").FontSize(12).AlignLeft();
});
@ -222,43 +221,41 @@ public class TarBallPDF : IDocument
// Signature Section
column.Item().Table(table =>
{
//define how many columns the table has
table.ColumnsDefinition(columns =>
{
columns.RelativeColumn(2); // Label column
columns.RelativeColumn(1); // Signature column
columns.RelativeColumn(2); //empty
columns.RelativeColumn(1); // Date column
columns.RelativeColumn(1); //empty
columns.RelativeColumn(2);
columns.RelativeColumn(1);
columns.RelativeColumn(2);
columns.RelativeColumn(1);
columns.RelativeColumn(1);
});
// Signature Rows
table.Cell().RowSpan(2).Element(CellStyle).Text("REPORTED BY :").Bold().FontSize(12);
table.Cell().Element(CellStyle).Text("Signature").FontSize(12);
table.Cell().Element(CellStyle).Text("").FontSize(12);
table.Cell().Element(CellStyle).Text("");
table.Cell().Element(CellStyle).Text("Date").FontSize(12);
table.Cell().Element(CellStyle).Text("").FontSize(12);
table.Cell().Element(CellStyle).Text("");
table.Cell().Element(CellStyle).Text("Designation").FontSize(12);
table.Cell().ColumnSpan(3).Element(CellStyle).Text("").FontSize(12);
table.Cell().ColumnSpan(3).Element(CellStyle).Text("");
table.Cell().RowSpan(2).Element(CellStyle).Text("CHECKED BY :").Bold().FontSize(12);
table.Cell().Element(CellStyle).Text("Signature").FontSize(12);
table.Cell().Element(CellStyle).Text("").FontSize(12);
table.Cell().Element(CellStyle).Text("");
table.Cell().Element(CellStyle).Text("Date").FontSize(12);
table.Cell().Element(CellStyle).Text("").FontSize(12);
table.Cell().Element(CellStyle).Text("");
table.Cell().Element(CellStyle).Text("Designation").FontSize(12);
table.Cell().ColumnSpan(3).Element(CellStyle).Text("").FontSize(12);
table.Cell().ColumnSpan(3).Element(CellStyle).Text("");
table.Cell().RowSpan(2).Element(CellStyle).Text("VERIFIED BY :").Bold().FontSize(12);
table.Cell().Element(CellStyle).Text("Signature").FontSize(12);
table.Cell().Element(CellStyle).Text("").FontSize(12);
table.Cell().Element(CellStyle).Text("");
table.Cell().Element(CellStyle).Text("Date").FontSize(12);
table.Cell().Element(CellStyle).Text("").FontSize(12);
table.Cell().Element(CellStyle).Text("");
table.Cell().Element(CellStyle).Text("Designation").FontSize(12);
table.Cell().ColumnSpan(3).Element(CellStyle).Text("").FontSize(12);
table.Cell().ColumnSpan(3).Element(CellStyle).Text("");
});
});
@ -270,8 +267,9 @@ public class TarBallPDF : IDocument
text.Span(" of ");
text.TotalPages();
});
static IContainer CellStyle(IContainer container) => container.Border(0.5f)
.Padding(5);
static IContainer CellStyle(IContainer container) => container.Border(0.5f).Padding(5);
});
}
}
}

View File

@ -86,14 +86,15 @@
<th>No.</th>
<th v-on:click="sortBy('date')">
Date
<span class="sort-arrow" :class="{ active: sortKey === 'date' && sortOrder === 'asc' }">▲</span>
<span class="sort-arrow" :class="{ active: sortKey === 'date' && sortOrder === 'desc' }">▼</span>
<span :class="['sort-arrow', { active: sortKey === 'date' && sortOrder === 'asc' }]">▲</span>
<span :class="['sort-arrow', { active: sortKey === 'date' && sortOrder === 'desc' }]">▼</span>
</th>
<th v-on:click="sortBy('station')">
Station
<span class="sort-arrow" :class="{ active: sortKey === 'station' && sortOrder === 'asc' }">▲</span>
<span class="sort-arrow" :class="{ active: sortKey === 'station' && sortOrder === 'desc' }">▼</span>
<span :class="['sort-arrow', { active: sortKey === 'station' && sortOrder === 'asc' }]">▲</span>
<span :class="['sort-arrow', { active: sortKey === 'station' && sortOrder === 'desc' }]">▼</span>
</th>
<th>Status</th>
<th>PDF</th>
</tr>
@ -133,7 +134,7 @@
'June', 'July', 'August', 'September',
'October', 'November', 'December'
],
dataFromServer: @Html.Raw(Json.Serialize(Model))
dataFromServer: @Html.Raw(Json.Serialize(Model ?? new List<object>()))
},
computed: {
years() {

View File

@ -11,6 +11,9 @@ namespace PSTW_CentralSystem.DBContext
// DbSet for tbl_marine_tarball
public DbSet<MarineTarball> MarineTarballs { get; set; }
public DbSet<TarballStation> TarballStations { get; set; }
public DbSet<State> States { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
@ -36,7 +39,46 @@ namespace PSTW_CentralSystem.DBContext
entity.Property(e => e.GetLatitude).HasColumnName("getLatitude");
entity.Property(e => e.GetLongitude).HasColumnName("getLongitude");
entity.Property(e => e.Timestamp).HasColumnName("timestamp");
//Configure relationship with TarballStation
entity.HasOne(m => m.TarballStation)
.WithMany()
.HasForeignKey(m => m.StationID)
.HasPrincipalKey(t => t.StationID);
});
// Map TarballStation to tbl_tarball_station
modelBuilder.Entity<TarballStation>().ToTable("tbl_tarball_station");
modelBuilder.Entity<TarballStation>(entity =>
{
entity.HasKey(e => e.Id); // Primary key
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.StationID).HasColumnName("stationID").HasMaxLength(20);
entity.Property(e => e.StateID).HasColumnName("stateID").HasMaxLength(10);
entity.Property(e => e.CategoryID).HasColumnName("categoryID").HasMaxLength(10);
entity.Property(e => e.LocationName).HasColumnName("locationName").HasMaxLength(50);
entity.Property(e => e.Longitude).HasColumnName("longitude").HasColumnType("decimal(10,5)");
entity.Property(e => e.Latitude).HasColumnName("latitude").HasColumnType("decimal(10,5)");
// Configure relationship with State
entity.HasOne(t => t.State)
.WithMany()
.HasForeignKey(t => t.StateID)
.HasPrincipalKey(s => s.StateID);
});
// Map State to tbl_state
modelBuilder.Entity<State>().ToTable("tbl_state");
modelBuilder.Entity<State>(entity =>
{
entity.HasKey(e => e.Id); // Primary key
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.StateID).HasColumnName("stateID").HasMaxLength(20);
entity.Property(e => e.StateName).HasColumnName("stateName").HasMaxLength(50);
});
}
}
}

View File

@ -1,21 +1,48 @@
using System;
using System.ComponentModel.DataAnnotations.Schema;
namespace PSTW_CentralSystem.Models
{
public class MarineTarball
{
public int Id { get; set; } // Maps to 'id'
public string ReportID { get; set; } // Maps to 'reportID'
public string FirstSampler { get; set; } // Maps to 'firstSampler'
public string SecondSampler { get; set; } // Maps to 'secondSampler'
public required string ReportID { get; set; } // Maps to 'reportID'
public required string FirstSampler { get; set; } // Maps to 'firstSampler'
public required string SecondSampler { get; set; } // Maps to 'secondSampler'
public DateTime DateSample { get; set; } // Maps to 'dateSample'
public TimeSpan TimeSample { get; set; } // Maps to 'timeSample'
public string StationID { get; set; } // Maps to 'stationID'
public string ClassifyID { get; set; } // Maps to 'classifyID'
public required string StationID { get; set; } // Maps to 'stationID'
public required string ClassifyID { get; set; } // Maps to 'classifyID'
public double Latitude { get; set; } // Maps to 'latitude'
public double Longitude { get; set; } // Maps to 'longitude'
public double GetLatitude { get; set; } // Maps to 'getLatitude'
public double GetLongitude { get; set; } // Maps to 'getLongitude'
public DateTime Timestamp { get; set; } // Maps to 'timestamp'
[ForeignKey("StationID")]
public required TarballStation TarballStation { get; set; }
}
public class TarballStation
{
public int Id { get; set; } // Maps to 'id'
public required string StationID { get; set; } // Maps to 'stationID'
public required string StateID { get; set; } // Maps to 'stateID'
public required string CategoryID { get; set; } // Maps to 'categoryID'
public required string LocationName { get; set; } // Maps to 'locationName'
public decimal Longitude { get; set; } // Maps to 'longitude'
public decimal Latitude { get; set; } // Maps to 'latitude'
[ForeignKey("StateID")]
public required State State { get; set; }
}
public class State
{
public int Id { get; set; } // Maps to 'id'
public required string StateID { get; set; } // Maps to 'stateID'
public required string StateName { get; set; } // Maps to 'stateName'
}
}