419 lines
13 KiB
PHP
419 lines
13 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Projects;
|
|
|
|
use App\Controllers\BaseController;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
|
|
class ProjectsController extends BaseController
|
|
{
|
|
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
|
|
if (!defined('AUTHENTICATED') || AUTHENTICATED === false) {
|
|
die('NO ACCESS');
|
|
}
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
// Default action
|
|
}
|
|
|
|
public function getAllProjects(): ResponseInterface
|
|
{
|
|
$archive = $this->cleanInput('archive');
|
|
$search = $this->cleanInput('search');
|
|
$order = $this->cleanInput('order');
|
|
$direction = $this->cleanInput('direction');
|
|
|
|
$projects = \Project::getAllProjects($archive, $search, $order, $direction);
|
|
|
|
return $this->json($projects);
|
|
}
|
|
|
|
public function getProject(int $projectID): ResponseInterface
|
|
{
|
|
$project = \Project::getProject($projectID);
|
|
|
|
// Get invoice details using the Invoices_model equivalent
|
|
$db = \Config\Database::connect();
|
|
$builder = $db->table('invoices');
|
|
$project->invoiceDetails = $builder
|
|
->selectSum('invoiceamount', 'invoiceTotal')
|
|
->selectSum('forecastamount', 'forecastTotal')
|
|
// Join VAT to calculate totals including VAT per invoice
|
|
->join('vats', 'vats.id = invoices.vat_id', 'left')
|
|
// Total invoiced including VAT based on each invoice's VAT rate
|
|
->select('SUM(ROUND(invoices.invoiceamount * (100 + COALESCE(vats.vatrate, 0)) / 100)) AS totalinvoiced', false)
|
|
->where('project_id', $project->id)
|
|
->get()
|
|
->getRow();
|
|
|
|
// Add total payments (VAT included amounts stored in payments table)
|
|
$paymentsBuilder = $db->table('payments');
|
|
$payments = $paymentsBuilder
|
|
->select('COALESCE(ROUND(SUM(payments.paymentamount), 0), 0) AS payed', false)
|
|
->join('invoices', 'invoices.id = payments.invoice_id', 'inner')
|
|
->where('invoices.project_id', $project->id)
|
|
->get()
|
|
->getRow();
|
|
|
|
// Ensure invoiceDetails exists and attach payed sum
|
|
if (!isset($project->invoiceDetails) || !$project->invoiceDetails) {
|
|
$project->invoiceDetails = (object) [];
|
|
}
|
|
$project->invoiceDetails->payed = $payments->payed ?? 0;
|
|
|
|
$project->hourTotals = \Projecthour::getTotalHoursPerProject($project->id);
|
|
|
|
return $this->json($project);
|
|
}
|
|
|
|
public function getAvailableEmployees(int $projectID): ResponseInterface
|
|
{
|
|
$availableEmployees = \Projectemployee::getAvailableEmployees($projectID);
|
|
return $this->json($availableEmployees);
|
|
}
|
|
|
|
public function addProject(): ResponseInterface
|
|
{
|
|
$projectID = \Project::createProject();
|
|
return $this->json($projectID);
|
|
}
|
|
|
|
public function creatProjectFromOffer(int $offerID): ResponseInterface
|
|
{
|
|
$offer = \Offer::find($offerID);
|
|
|
|
$offernumber = $offer->offernumber;
|
|
$projectnumber = 'P' . substr($offernumber, 1);
|
|
|
|
$data = [
|
|
'projectnumber' => $projectnumber,
|
|
'projectname' => $offer->offername,
|
|
'projectleader_id' => $offer->offerleader_id,
|
|
'startdate' => $offer->accepteddate,
|
|
'customer_id' => $offer->customer_id
|
|
];
|
|
|
|
$project = \Project::create($data);
|
|
|
|
\Offer::whereId($offerID)->update(['project_id' => $project->id]);
|
|
|
|
\Projectemployee::firstOrCreateProjectemployee($offer->offerleader_id, $project->id);
|
|
|
|
$offerteHours = \Employeehour::whereOfferId($offerID)->get();
|
|
foreach ($offerteHours as $offerteHour) {
|
|
$projectemployee = \Projectemployee::firstOrCreateProjectemployee($offerteHour->employee_id, $project->id);
|
|
|
|
$data = [
|
|
'projectemployee_id' => $projectemployee->id,
|
|
'hourtype_id' => 10,
|
|
'hourdate' => $offerteHour->hourdate,
|
|
'hours' => $offerteHour->hours
|
|
];
|
|
|
|
\Projecthour::create($data);
|
|
\Employeehour::whereId($offerteHour->id)->delete();
|
|
}
|
|
|
|
return $this->json(['success' => true, 'projectnumber' => $projectnumber]);
|
|
}
|
|
|
|
public function addProjectEmployee(int $projectID): ResponseInterface
|
|
{
|
|
\Projectemployee::createProjectEmployee($projectID);
|
|
return $this->json(['success' => true]);
|
|
}
|
|
|
|
public function saveProject(): ResponseInterface
|
|
{
|
|
$projectValidation = new \ProjectValidation();
|
|
|
|
if (!$projectValidation->getValidationResult()) {
|
|
return $this->json([
|
|
'success' => false,
|
|
'messages' => $projectValidation->getErrorMessages()
|
|
]);
|
|
}
|
|
|
|
$project = $this->request->getPost();
|
|
|
|
$projectID = $project['id'];
|
|
unset($project['id']);
|
|
|
|
$project['startdate'] = date2mysql($project['startdate']);
|
|
$project['enddate'] = date2mysql($project['enddate']);
|
|
|
|
$projectID = \Project::saveProject($projectID, $project);
|
|
|
|
\Offer::syncProjectname($projectID);
|
|
|
|
return $this->json(['success' => true, 'projectID' => $projectID]);
|
|
}
|
|
|
|
public function saveProjectemployee(): ResponseInterface
|
|
{
|
|
$projectemployeeValidation = new \ProjectemployeeValidation();
|
|
|
|
if (!$projectemployeeValidation->getValidationResult()) {
|
|
return $this->json([
|
|
'success' => false,
|
|
'messages' => $projectemployeeValidation->getErrorMessages()
|
|
]);
|
|
}
|
|
|
|
$projectemployee = $this->request->getPost();
|
|
|
|
$projectemployeeID = $projectemployee['id'];
|
|
unset($projectemployee['id']);
|
|
|
|
\Projectemployee::saveProjectemployee($projectemployeeID, $projectemployee);
|
|
|
|
return $this->json(['success' => true]);
|
|
}
|
|
|
|
public function saveInvoice(): ResponseInterface
|
|
{
|
|
$invoiceValidation = new \InvoiceValidation();
|
|
|
|
if (!$invoiceValidation->getValidationResult()) {
|
|
return $this->json([
|
|
'success' => false,
|
|
'messages' => $invoiceValidation->getErrorMessages()
|
|
]);
|
|
}
|
|
|
|
$invoice = $this->request->getPost();
|
|
|
|
$invoiceID = $invoice['id'];
|
|
unset($invoice['id']);
|
|
|
|
$invoice['invoicedate'] = date2mysql($invoice['invoicedate']);
|
|
$invoice['forecastdate'] = date2mysql($invoice['forecastdate']);
|
|
$invoice['invoiceamount'] = str_replace(',', '.', $invoice['invoiceamount'] ?? '');
|
|
$invoice['forecastamount'] = str_replace(',', '.', $invoice['forecastamount'] ?? '');
|
|
|
|
$invoiceID = \Invoice::saveInvoice($invoiceID, $invoice);
|
|
|
|
return $this->json(['success' => true, 'invoiceID' => $invoiceID]);
|
|
}
|
|
|
|
public function savePayment(): ResponseInterface
|
|
{
|
|
$paymentValidation = new \PaymentValidation();
|
|
|
|
if (!$paymentValidation->getValidationResult()) {
|
|
return $this->json([
|
|
'success' => false,
|
|
'messages' => $paymentValidation->getErrorMessages()
|
|
]);
|
|
}
|
|
|
|
$payment = $this->request->getPost();
|
|
|
|
$paymentID = $payment['id'];
|
|
unset($payment['id']);
|
|
|
|
$payment['paymentdate'] = date2mysql($payment['paymentdate']);
|
|
$payment['paymentamount'] = str_replace(',', '.', $payment['paymentamount'] ?? '');
|
|
|
|
$paymentID = \Payment::savePayment($paymentID, $payment);
|
|
|
|
return $this->json(['success' => true, 'paymentID' => $paymentID]);
|
|
}
|
|
|
|
public function saveProjectcost(): ResponseInterface
|
|
{
|
|
$projectcostValidation = new \ProjectcostValidation();
|
|
|
|
if (!$projectcostValidation->getValidationResult()) {
|
|
return $this->json([
|
|
'success' => false,
|
|
'messages' => $projectcostValidation->getErrorMessages()
|
|
]);
|
|
}
|
|
|
|
$projectcost = $this->request->getPost();
|
|
|
|
$projectcostID = $projectcost['id'];
|
|
unset($projectcost['id']);
|
|
|
|
$projectcost['date'] = date2mysql($projectcost['date']);
|
|
|
|
$projectcostID = \Projectcost::saveProjectcost($projectcostID, $projectcost);
|
|
|
|
return $this->json(['success' => true, 'projectcostID' => $projectcostID]);
|
|
}
|
|
|
|
public function saveProjecthour(): ResponseInterface
|
|
{
|
|
$projecthourValidation = new \ProjecthourValidation();
|
|
|
|
if (!$projecthourValidation->getValidationResult()) {
|
|
return $this->json([
|
|
'success' => false,
|
|
'messages' => $projecthourValidation->getErrorMessages()
|
|
]);
|
|
}
|
|
|
|
$projecthour = $this->request->getPost();
|
|
|
|
$projecthourID = $projecthour['id'];
|
|
unset($projecthour['id']);
|
|
|
|
$projecthour['hourdate'] = date2mysql($projecthour['hourdate']);
|
|
|
|
$paymentID = \Projecthour::saveProjecthour($projecthourID, $projecthour);
|
|
|
|
return $this->json(['success' => true, 'projecthourID' => $paymentID]);
|
|
}
|
|
|
|
public function saveForecast(): ResponseInterface
|
|
{
|
|
$forecastValidation = new \ForecastValidation();
|
|
|
|
if (!$forecastValidation->getValidationResult()) {
|
|
return $this->json([
|
|
'success' => false,
|
|
'messages' => $forecastValidation->getErrorMessages()
|
|
]);
|
|
}
|
|
|
|
$forecast = $this->request->getPost();
|
|
|
|
$forecastID = $forecast['id'];
|
|
unset($forecast['id']);
|
|
|
|
$forecastID = \Forecast::saveForecast($forecastID, $forecast);
|
|
|
|
return $this->json(['success' => true, 'forecastID' => $forecastID]);
|
|
}
|
|
|
|
public function saveReservation(): ResponseInterface
|
|
{
|
|
$reservationValidation = new \ReservationValidation();
|
|
|
|
if (!$reservationValidation->getValidationResult()) {
|
|
return $this->json([
|
|
'success' => false,
|
|
'messages' => $reservationValidation->getErrorMessages()
|
|
]);
|
|
}
|
|
|
|
$reservation = $this->request->getPost();
|
|
|
|
$reservationID = $reservation['id'];
|
|
unset($reservation['id']);
|
|
|
|
$reservationID = \Reservation::saveReservation($reservationID, $reservation);
|
|
|
|
return $this->json(['success' => true, 'reservationID' => $reservationID]);
|
|
}
|
|
|
|
public function archiveProject(int $projectID, string $status): ResponseInterface
|
|
{
|
|
\Project::whereId($projectID)->update(['archive' => $status]);
|
|
return $this->json(['success' => true]);
|
|
}
|
|
|
|
public function removeProjectemployee(int $projectemployeeID): ResponseInterface
|
|
{
|
|
$removeProjectemployee = new \RemoveProjectemployee($projectemployeeID);
|
|
|
|
if ($removeProjectemployee->getRemoveAllowed() === false) {
|
|
return $this->json(['result' => false]);
|
|
}
|
|
|
|
\Projectemployee::removeProjectemployee($projectemployeeID);
|
|
|
|
return $this->json(['result' => true]);
|
|
}
|
|
|
|
public function removeInvoice(int $invoiceID): ResponseInterface
|
|
{
|
|
$removeInvoice = new \RemoveInvoice($invoiceID);
|
|
|
|
if ($removeInvoice->getRemoveAllowed() === false) {
|
|
return $this->json(['result' => false]);
|
|
}
|
|
|
|
\Invoice::whereId($invoiceID)->delete();
|
|
|
|
return $this->json(['result' => true]);
|
|
}
|
|
|
|
public function removeIncomingInvoice(int $invoiceID): ResponseInterface
|
|
{
|
|
\Projectcost::whereId($invoiceID)->delete();
|
|
return $this->json(['result' => true]);
|
|
}
|
|
|
|
public function removePayment(int $paymentID): ResponseInterface
|
|
{
|
|
$removePayment = new \RemovePayment($paymentID);
|
|
|
|
if ($removePayment->getAllowDelete() === false) {
|
|
return $this->json(['result' => false]);
|
|
}
|
|
|
|
\Payment::removePayment($paymentID);
|
|
|
|
return $this->json(['result' => true]);
|
|
}
|
|
|
|
public function removeProjecthour(int $projecthourID): ResponseInterface
|
|
{
|
|
$removeProjecthour = new \RemoveProjecthour($projecthourID);
|
|
|
|
if ($removeProjecthour->getAllowDelete() === false) {
|
|
return $this->json(['result' => false]);
|
|
}
|
|
|
|
\Projecthour::removeProjecthour($projecthourID);
|
|
|
|
return $this->json(['result' => true]);
|
|
}
|
|
|
|
public function removeForecast(int $forecastID): ResponseInterface
|
|
{
|
|
$removeForecast = new \RemoveForecast($forecastID);
|
|
|
|
if ($removeForecast->getAllowDelete() === false) {
|
|
return $this->json(['result' => false]);
|
|
}
|
|
|
|
\Forecast::removeForecast($forecastID);
|
|
|
|
return $this->json(['result' => true]);
|
|
}
|
|
|
|
public function removeReservation(int $reservationID): ResponseInterface
|
|
{
|
|
$removeReservation = new \RemoveReservation($reservationID);
|
|
|
|
if ($removeReservation->getAllowDelete() === false) {
|
|
return $this->json(['result' => false]);
|
|
}
|
|
|
|
\Reservation::removeReservation($reservationID);
|
|
|
|
return $this->json(['result' => true]);
|
|
}
|
|
|
|
public function removeProject(int $projectID): ResponseInterface
|
|
{
|
|
$removeProject = new \RemoveProject($projectID);
|
|
|
|
if ($removeProject->getRemoveAllowed() === false) {
|
|
return $this->json(['result' => false]);
|
|
}
|
|
|
|
\Project::removeProject($projectID);
|
|
|
|
return $this->json(['result' => true]);
|
|
}
|
|
}
|