feat: add wartosc_zlecenia field to projects table and update related functionalities
This commit is contained in:
36
migrate-add-wartosc-zlecenia.mjs
Normal file
36
migrate-add-wartosc-zlecenia.mjs
Normal file
@@ -0,0 +1,36 @@
|
||||
import db from './src/lib/db.js';
|
||||
|
||||
console.log('Starting migration to add wartosc_zlecenia field to projects table...');
|
||||
|
||||
try {
|
||||
// Check if wartosc_zlecenia column already exists
|
||||
const schema = db.prepare("PRAGMA table_info(projects)").all();
|
||||
const hasWartoscZleceniaColumn = schema.some(column => column.name === 'wartosc_zlecenia');
|
||||
|
||||
if (hasWartoscZleceniaColumn) {
|
||||
console.log("✅ wartosc_zlecenia column already exists in projects table");
|
||||
} else {
|
||||
// Add the wartosc_zlecenia column
|
||||
db.prepare("ALTER TABLE projects ADD COLUMN wartosc_zlecenia REAL").run();
|
||||
console.log("✅ Added 'wartosc_zlecenia' column to projects table");
|
||||
}
|
||||
|
||||
// Verify the column was added
|
||||
const updatedSchema = db.prepare("PRAGMA table_info(projects)").all();
|
||||
const wartoscZleceniaColumn = updatedSchema.find(column => column.name === 'wartosc_zlecenia');
|
||||
|
||||
if (wartoscZleceniaColumn) {
|
||||
console.log("✅ Migration completed successfully");
|
||||
console.log(`Column details: ${JSON.stringify(wartoscZleceniaColumn, null, 2)}`);
|
||||
} else {
|
||||
console.error("❌ Migration failed - wartosc_zlecenia column not found");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
db.close();
|
||||
console.log("Database connection closed");
|
||||
|
||||
} catch (error) {
|
||||
console.error("❌ Migration failed:", error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -8,6 +8,7 @@ echo "🔄 Running database migrations..."
|
||||
# List of migration scripts to run (in order)
|
||||
MIGRATIONS=(
|
||||
"migrate-add-team-lead-role.mjs"
|
||||
"migrate-add-wartosc-zlecenia.mjs"
|
||||
)
|
||||
|
||||
for migration in "${MIGRATIONS[@]}"; do
|
||||
|
||||
@@ -421,6 +421,19 @@ export default function ProjectViewPage() {
|
||||
{project.investment_number || "N/A"}
|
||||
</p>
|
||||
</div>
|
||||
{session?.user?.role === 'team_lead' && project.wartosc_zlecenia && (
|
||||
<div>
|
||||
<span className="text-sm font-medium text-gray-500 block mb-1">
|
||||
Wartość zlecenia
|
||||
</span>
|
||||
<p className="text-gray-900 font-medium">
|
||||
{parseFloat(project.wartosc_zlecenia).toLocaleString('pl-PL', {
|
||||
style: 'currency',
|
||||
currency: 'PLN'
|
||||
})}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{project.contact && (
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import { useState, useEffect, forwardRef, useImperativeHandle } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useSession } from "next-auth/react";
|
||||
import { Card, CardHeader, CardContent } from "@/components/ui/Card";
|
||||
import Button from "@/components/ui/Button";
|
||||
import { Input } from "@/components/ui/Input";
|
||||
@@ -10,6 +11,7 @@ import { useTranslation } from "@/lib/i18n";
|
||||
|
||||
const ProjectForm = forwardRef(function ProjectForm({ initialData = null }, ref) {
|
||||
const { t } = useTranslation();
|
||||
const { data: session } = useSession();
|
||||
const [form, setForm] = useState({
|
||||
contract_id: "",
|
||||
project_name: "",
|
||||
@@ -24,6 +26,7 @@ const ProjectForm = forwardRef(function ProjectForm({ initialData = null }, ref)
|
||||
contact: "",
|
||||
notes: "",
|
||||
coordinates: "",
|
||||
wartosc_zlecenia: "",
|
||||
project_type: "design",
|
||||
assigned_to: "",
|
||||
});
|
||||
@@ -63,12 +66,14 @@ const ProjectForm = forwardRef(function ProjectForm({ initialData = null }, ref)
|
||||
contact: "",
|
||||
notes: "",
|
||||
coordinates: "",
|
||||
wartosc_zlecenia: "",
|
||||
project_type: "design",
|
||||
assigned_to: "",
|
||||
...initialData,
|
||||
// Ensure these defaults are preserved if not in initialData
|
||||
project_type: initialData.project_type || "design",
|
||||
assigned_to: initialData.assigned_to || "",
|
||||
wartosc_zlecenia: initialData.wartosc_zlecenia || "",
|
||||
// Format finish_date for input if it exists
|
||||
finish_date: initialData.finish_date
|
||||
? formatDateForInput(initialData.finish_date)
|
||||
@@ -325,6 +330,23 @@ const ProjectForm = forwardRef(function ProjectForm({ initialData = null }, ref)
|
||||
/>
|
||||
</div>
|
||||
|
||||
{session?.user?.role === 'team_lead' && (
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
Wartość zlecenia
|
||||
</label>
|
||||
<Input
|
||||
type="number"
|
||||
name="wartosc_zlecenia"
|
||||
value={form.wartosc_zlecenia || ""}
|
||||
onChange={handleChange}
|
||||
placeholder="0.00"
|
||||
step="0.01"
|
||||
min="0"
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="md:col-span-2">
|
||||
<label className="block text-sm font-medium text-gray-700 mb-2">
|
||||
{t('projects.contact')}
|
||||
|
||||
@@ -30,6 +30,7 @@ export default function initializeDatabase() {
|
||||
wp TEXT,
|
||||
contact TEXT,
|
||||
notes TEXT,
|
||||
wartosc_zlecenia REAL,
|
||||
project_type TEXT CHECK(project_type IN ('design', 'construction', 'design+construction')) DEFAULT 'design',
|
||||
project_status TEXT CHECK(project_status IN ('registered', 'in_progress_design', 'in_progress_construction', 'fulfilled', 'cancelled')) DEFAULT 'registered',
|
||||
FOREIGN KEY (contract_id) REFERENCES contracts(contract_id)
|
||||
|
||||
@@ -76,8 +76,8 @@ export function createProject(data, userId = null) {
|
||||
const stmt = db.prepare(`
|
||||
INSERT INTO projects (
|
||||
contract_id, project_name, project_number, address, plot, district, unit, city, investment_number, finish_date,
|
||||
wp, contact, notes, project_type, project_status, coordinates, created_by, assigned_to, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now', 'localtime'), datetime('now', 'localtime'))
|
||||
wp, contact, notes, wartosc_zlecenia, project_type, project_status, coordinates, created_by, assigned_to, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now', 'localtime'), datetime('now', 'localtime'))
|
||||
`);
|
||||
|
||||
const result = stmt.run(
|
||||
@@ -94,6 +94,7 @@ export function createProject(data, userId = null) {
|
||||
data.wp,
|
||||
data.contact,
|
||||
data.notes,
|
||||
data.wartosc_zlecenia || null,
|
||||
data.project_type || "design",
|
||||
data.project_status || "registered",
|
||||
data.coordinates || null,
|
||||
@@ -108,7 +109,7 @@ export function updateProject(id, data, userId = null) {
|
||||
const stmt = db.prepare(`
|
||||
UPDATE projects SET
|
||||
contract_id = ?, project_name = ?, project_number = ?, address = ?, plot = ?, district = ?, unit = ?, city = ?,
|
||||
investment_number = ?, finish_date = ?, wp = ?, contact = ?, notes = ?, project_type = ?, project_status = ?,
|
||||
investment_number = ?, finish_date = ?, wp = ?, contact = ?, notes = ?, wartosc_zlecenia = ?, project_type = ?, project_status = ?,
|
||||
coordinates = ?, assigned_to = ?, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE project_id = ?
|
||||
`);
|
||||
@@ -126,6 +127,7 @@ export function updateProject(id, data, userId = null) {
|
||||
data.wp,
|
||||
data.contact,
|
||||
data.notes,
|
||||
data.wartosc_zlecenia || null,
|
||||
data.project_type || "design",
|
||||
data.project_status || "registered",
|
||||
data.coordinates || null,
|
||||
|
||||
Reference in New Issue
Block a user