2025-11-15 11:14:31 +00:00
using DevExtreme.AspNet.Data ;
using DevExtreme.AspNet.Mvc ;
using Newtonsoft.Json ;
using Newtonsoft.Json.Linq ;
using OSS.Models ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Net ;
using System.Net.Http ;
using System.Net.Http.Formatting ;
using System.Web.Http ;
namespace OSS.Controllers
{
[Authorize]
[Route("api/CompanyProfile/{action}", Name = "CompanyProfile")]
public class CompanyProfileExternalApiController : ApiController
{
private OSSDBContext myContext = new OSSDBContext ( ) ;
[HttpGet]
public HttpResponseMessage GetCompanyProfile ( DataSourceLoadOptions loadOptions )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . CompanyProfileExternal , loadOptions ) ) ;
}
[HttpGet]
public HttpResponseMessage GetApplicationAttachments ( DataSourceLoadOptions loadOptions , string ApplicationCode )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . AttachmentsList . Where ( t = > t . ProjectCode = = ApplicationCode ) , loadOptions ) ) ;
}
[HttpGet]
public HttpResponseMessage GetCompanyProfileByTIN ( DataSourceLoadOptions loadOptions , string Username )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . CompanyProfileExternal . Where ( t = > t . AddedBy = = Username ) , loadOptions ) ) ;
}
[HttpGet]
public HttpResponseMessage GetIncompleteApp ( DataSourceLoadOptions loadOptions , string Username )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . StepsManagements . Where ( t = > t . CompanyEmail = = Username & & t . SubmittedStatus = = "Not Submitted" & & t . ApplicationType = = "New" ) , loadOptions ) ) ;
}
[HttpGet]
public HttpResponseMessage GetIncompleteAppExtension ( DataSourceLoadOptions loadOptions , string Username )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . StepsManagements . Where ( t = > t . CompanyEmail = = Username & & t . SubmittedStatus = = "Not Submitted" & & t . ApplicationType = = "Extension" ) , loadOptions ) ) ;
}
[HttpGet]
public HttpResponseMessage GetIncompleteAppAmendment ( DataSourceLoadOptions loadOptions , string Username )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . StepsManagements . Where ( t = > t . CompanyEmail = = Username & & t . SubmittedStatus = = "Not Submitted" & & t . ApplicationType = = "Amendment" ) , loadOptions ) ) ;
}
[HttpGet]
public HttpResponseMessage GetIncompleteAppExpansion ( DataSourceLoadOptions loadOptions , string Username )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . StepsManagements . Where ( t = > t . CompanyEmail = = Username & & t . SubmittedStatus = = "Not Submitted" & & t . ApplicationType = = "Expansion" ) , loadOptions ) ) ;
}
[HttpDelete]
public void Delete ( FormDataCollection form )
{
var key = Convert . ToInt32 ( form . Get ( "key" ) ) ;
var model = myContext . AttachmentsList . FirstOrDefault ( item = > item . AttachmentID = = key ) ;
myContext . AttachmentsList . Remove ( model ) ;
myContext . SaveChanges ( ) ;
}
[HttpGet]
public HttpResponseMessage GetProjectsByUserAmendment ( DataSourceLoadOptions loadOptions , string Username )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . ApplicationManagers . Where ( t = > t . CompanyEmail = = Username & & t . ServiceName = = "Amendment" ) . OrderByDescending ( t = > t . CreatedDate ) , loadOptions ) ) ;
}
[HttpGet]
public HttpResponseMessage GetProjectsByUserExtension ( DataSourceLoadOptions loadOptions , string Username )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . ApplicationManagers . Where ( t = > t . CompanyEmail = = Username & & t . ServiceName = = "Extension" ) . OrderByDescending ( t = > t . CreatedDate ) , loadOptions ) ) ;
}
[HttpGet]
public HttpResponseMessage GetProjectsByUser ( DataSourceLoadOptions loadOptions , string Username )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . ApplicationManagers . Where ( t = > t . CompanyEmail = = Username & & t . ServiceName = = "New" ) . OrderByDescending ( t = > t . CreatedDate ) , loadOptions ) ) ;
}
[HttpGet]
public HttpResponseMessage GetInvoiceByUser ( DataSourceLoadOptions loadOptions , string ProjectCode )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . ApplicationManagers . Where ( t = > t . ProjectCode = = ProjectCode ) . OrderByDescending ( t = > t . ApplicationID ) , loadOptions ) ) ;
}
[HttpGet]
public HttpResponseMessage GetInvoiceByUserAll ( DataSourceLoadOptions loadOptions , string ProjectCode )
{
2025-12-09 07:53:28 +00:00
// Get invoices from ApplicationManagers
var appManagerInvoices = myContext . ApplicationManagers
. Where ( t = > t . ProjectCode = = ProjectCode & & ( t . ServiceName = = "New" | | t . ServiceName = = "Expansion" | | t . ServiceName = = "Amendment" | | t . ServiceName = = "Extension" ) )
. OrderByDescending ( t = > t . CreatedDate )
. ToList ( ) ;
// Get additional payment invoices from tblInvoice
var additionalInvoices = myContext . Database . SqlQuery < InvoiceDisplay > (
"SELECT InvoiceID, Amount, Currency, ApplicationCode, ApplicationID, SubServiceName, FullName, PhoneNo, GFSCode, BillItemRefNo, StartDate, Expiredate, ServiceTypeID, PaymentStatus, ControlNo FROM tblInvoice WHERE ApplicationCode = @p0 AND SubServiceName LIKE 'Additional_Amount%' ORDER BY InvoiceID DESC" ,
new System . Data . SqlClient . SqlParameter ( "@p0" , ProjectCode ) ) . ToList ( ) ;
// Combine and return (you may need to map InvoiceDisplay to ApplicationManager or create a combined DTO)
// For now, return ApplicationManagers as before, but this shows the structure
return Request . CreateResponse ( DataSourceLoader . Load ( appManagerInvoices , loadOptions ) ) ;
2025-11-15 11:14:31 +00:00
}
[HttpGet]
public HttpResponseMessage GetInvoiceByUserExpansion ( DataSourceLoadOptions loadOptions , string ProjectCode )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . ApplicationManagers . Where ( t = > t . ProjectCode = = ProjectCode & & t . ServiceName = = "Expansion" ) . OrderByDescending ( t = > t . CreatedDate ) , loadOptions ) ) ;
}
[HttpGet]
public HttpResponseMessage GetProjectsByUserExpansion ( DataSourceLoadOptions loadOptions , string Username )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . ApplicationManagers . Where ( t = > t . CompanyEmail = = Username & & t . ServiceName = = "Expansion" ) . OrderByDescending ( t = > t . CreatedDate ) , loadOptions ) ) ;
}
[HttpGet]
public HttpResponseMessage GetInvoiceByUserAmendment ( DataSourceLoadOptions loadOptions , string ProjectCode )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . ApplicationManagers . Where ( t = > t . ProjectCode = = ProjectCode & & ( t . ServiceName = = "Amendment" | | t . ServiceName = = "Extension" ) ) . OrderByDescending ( t = > t . CreatedDate ) , loadOptions ) ) ;
}
[HttpGet]
public HttpResponseMessage GetApprovedProjectsByUser ( DataSourceLoadOptions loadOptions , string Username )
{
return Request . CreateResponse ( DataSourceLoader . Load ( myContext . ApplicationManagers . Where ( t = > t . CompanyEmail = = Username & & t . EvaluationStatus = = "Approved" ) . OrderByDescending ( t = > t . CreatedDate ) , loadOptions ) ) ;
}
2025-11-22 04:41:21 +00:00
[HttpGet]
public HttpResponseMessage GetInvoicesByApplicationCode ( DataSourceLoadOptions loadOptions , string ProjectCode )
{
try
{
if ( string . IsNullOrEmpty ( ProjectCode ) )
{
return Request . CreateResponse ( DataSourceLoader . Load ( new List < InvoiceDisplay > ( ) , loadOptions ) ) ;
}
// Query invoices directly from tblInvoice using ApplicationCode (ProjectCode)
var invoices = myContext . Database . SqlQuery < InvoiceDisplay > (
"SELECT InvoiceID, SubServiceName, Amount, Currency, StartDate, Expiredate, BillItemRefNo, ControlNo, PaymentStatus, ApplicationCode FROM tblInvoice WHERE ApplicationCode = @p0 ORDER BY InvoiceID DESC" ,
new System . Data . SqlClient . SqlParameter ( "@p0" , ProjectCode ) ) . ToList ( ) ;
// Convert to anonymous objects for DevExtreme (with null-safe defaults)
var invoiceList = invoices . Select ( i = > new
{
InvoiceID = i . InvoiceID ,
SubServiceName = i . SubServiceName ? ? "" ,
Amount = i . Amount ? ? 0 m ,
Currency = i . Currency ? ? "" ,
StartDate = i . StartDate ,
Expiredate = i . Expiredate ,
BillItemRefNo = i . BillItemRefNo ? ? "" ,
ControlNo = i . ControlNo ? ? "" ,
PaymentStatus = i . PaymentStatus ? ? false ,
ApplicationCode = i . ApplicationCode ? ? ""
} ) . ToList ( ) ;
return Request . CreateResponse ( DataSourceLoader . Load ( invoiceList , loadOptions ) ) ;
}
catch ( Exception ex )
{
// Log error and return empty list to prevent 500 error
System . Diagnostics . Debug . WriteLine ( "Error loading invoices: " + ex . Message ) ;
return Request . CreateResponse ( DataSourceLoader . Load ( new List < InvoiceDisplay > ( ) , loadOptions ) ) ;
}
}
2025-11-15 11:14:31 +00:00
[HttpPost]
public HttpResponseMessage AddCompanyProfile ( FormDataCollection form )
{
var values = form . Get ( "values" ) ;
JObject data = JObject . Parse ( values ) ;
var newCompanyProfile = new CompanyProfileExternal ( ) ;
JsonConvert . PopulateObject ( values , newCompanyProfile ) ;
Validate ( newCompanyProfile ) ;
if ( ! ModelState . IsValid )
return Request . CreateErrorResponse ( HttpStatusCode . BadRequest , ModelState . GetFullErrorMessage ( ) ) ;
myContext . CompanyProfileExternal . Add ( newCompanyProfile ) ;
myContext . SaveChanges ( ) ;
return Request . CreateResponse ( HttpStatusCode . Created ) ;
}
[HttpPut]
public HttpResponseMessage UpdateProfile ( FormDataCollection form )
{
var key = Convert . ToString ( form . Get ( "key" ) ) ;
var values = form . Get ( "values" ) ;
var CompanyInfo = myContext . CompanyProfileExternal . First ( o = > o . CompanyTIN = = key ) ;
JObject data = JObject . Parse ( values ) ;
values = data . ToString ( ) ;
JsonConvert . PopulateObject ( values , CompanyInfo ) ;
Validate ( CompanyInfo ) ;
if ( ! ModelState . IsValid )
{
return Request . CreateErrorResponse ( HttpStatusCode . BadRequest , ModelState . GetFullErrorMessage ( ) ) ;
}
else
{
myContext . SaveChanges ( ) ;
return Request . CreateResponse ( HttpStatusCode . OK ) ;
}
}
}
}