diff --git a/src/main/java/com/ffii/tsms/modules/data/service/CustomerContactService.kt b/src/main/java/com/ffii/tsms/modules/data/service/CustomerContactService.kt index 350f798..6d80457 100644 --- a/src/main/java/com/ffii/tsms/modules/data/service/CustomerContactService.kt +++ b/src/main/java/com/ffii/tsms/modules/data/service/CustomerContactService.kt @@ -17,7 +17,7 @@ class CustomerContactService( } fun findByContactId(contactId: Long): CustomerContact { - return customerContactRepository.findById(contactId).orElseThrow() + return customerContactRepository.findById(contactId).orElse(null) } fun saveContactsByCustomer(saveCustomerId: Long, saveCustomerContact: List) { diff --git a/src/main/java/com/ffii/tsms/modules/project/entity/Project.kt b/src/main/java/com/ffii/tsms/modules/project/entity/Project.kt index 9294413..533dcd3 100644 --- a/src/main/java/com/ffii/tsms/modules/project/entity/Project.kt +++ b/src/main/java/com/ffii/tsms/modules/project/entity/Project.kt @@ -123,4 +123,12 @@ open class Project : BaseEntity() { // @JsonBackReference @JoinColumn(name = "mainProjectId") open var mainProject: Project? = null + + @ManyToOne + @JoinColumn(name = "customerContactId") + open var customerContact: CustomerContact? = null + + @ManyToOne + @JoinColumn(name = "subsidiaryContactId") + open var subsidiaryContact: SubsidiaryContact? = null } \ No newline at end of file diff --git a/src/main/java/com/ffii/tsms/modules/project/entity/ProjectRepository.kt b/src/main/java/com/ffii/tsms/modules/project/entity/ProjectRepository.kt index b1c38f9..57d1720 100644 --- a/src/main/java/com/ffii/tsms/modules/project/entity/ProjectRepository.kt +++ b/src/main/java/com/ffii/tsms/modules/project/entity/ProjectRepository.kt @@ -25,7 +25,7 @@ interface ProjectRepository : AbstractRepository { "") fun getLatestCodeNumberByMainProject(isClpProject: Boolean, id: Serializable?): Long? - @Query("SELECT max(cast(substring_index(p.code, '-', -1) as long)) FROM Project p WHERE p.code like ?1 and p.id != ?2") + @Query("SELECT max(case when length(p.code) - length(replace(p.code, '-', '')) > 1 then cast(substring_index(p.code, '-', -1) as long) end) FROM Project p WHERE p.code like ?1 and p.id != ?2") fun getLatestCodeNumberBySubProject(code: String, id: Serializable?): Long? fun findAllByStatusIsNotAndMainProjectIsNull(status: String): List diff --git a/src/main/java/com/ffii/tsms/modules/project/entity/projections/ProjectSearchInfo.kt b/src/main/java/com/ffii/tsms/modules/project/entity/projections/ProjectSearchInfo.kt index 72dfde5..7843a95 100644 --- a/src/main/java/com/ffii/tsms/modules/project/entity/projections/ProjectSearchInfo.kt +++ b/src/main/java/com/ffii/tsms/modules/project/entity/projections/ProjectSearchInfo.kt @@ -11,6 +11,9 @@ interface ProjectSearchInfo { val code: String? val status: String? + @get:Value("#{target.mainProject?.code}") + val mainProject: String? + @get:Value("#{target.projectCategory.name}") val category: String? diff --git a/src/main/java/com/ffii/tsms/modules/project/service/ProjectsService.kt b/src/main/java/com/ffii/tsms/modules/project/service/ProjectsService.kt index 73ba11f..16012b6 100644 --- a/src/main/java/com/ffii/tsms/modules/project/service/ProjectsService.kt +++ b/src/main/java/com/ffii/tsms/modules/project/service/ProjectsService.kt @@ -210,6 +210,8 @@ open class ProjectsService( custLeadPhone = if (request.isSubsidiaryContact == null || !request.isSubsidiaryContact) clientContact.phone else subsidiaryContact.phone this.customerSubsidiary = customerSubsidiary + this.customerContact = if (customerSubsidiary == null) clientContact else null + this.subsidiaryContact = if (customerSubsidiary != null) subsidiaryContact else null } @@ -325,11 +327,11 @@ open class ProjectsService( val project = projectRepository.findById(projectId) return project.getOrNull()?.let { - val subsidiaryContact = it.customerSubsidiary?.id?.let { subsidiaryId -> - subsidiaryContactService.findAllBySubsidiaryId(subsidiaryId) - } ?: emptyList() - val customerContact = it.customer?.id?.let { customerId -> customerContactService.findAllByCustomerId(customerId) } - ?: emptyList() +// val subsidiaryContact = it.customerSubsidiary?.id?.let { subsidiaryId -> +// subsidiaryContactService.findAllBySubsidiaryId(subsidiaryId) +// } ?: emptyList() +// val customerContact = it.customer?.id?.let { customerId -> customerContactService.findAllByCustomerId(customerId) } +// ?: emptyList() val milestoneMap = it.milestones.filter { milestone -> milestone.taskGroup?.id != null } .associateBy { milestone -> milestone.taskGroup!!.id!! } @@ -345,6 +347,7 @@ open class ProjectsService( projectActualEnd = it.actualEnd, projectStatus = it.status, isClpProject = it.isClpProject, + mainProjectId = it.mainProject?.id, serviceTypeId = it.serviceType?.id, fundingTypeId = it.fundingType?.id, contractTypeId = it.contractType?.id, @@ -353,7 +356,8 @@ open class ProjectsService( buildingTypeIds = it.buildingTypes.mapNotNull { buildingType -> buildingType.id }, workNatureIds = it.workNatures.mapNotNull { workNature -> workNature.id }, clientId = it.customer?.id, - clientContactId = subsidiaryContact.find { contact -> contact.name == it.custLeadName }?.id ?: customerContact.find { contact -> contact.name == it.custLeadName }?.id, + clientContactId = it.subsidiaryContact?.id ?: it.customerContact?.id, +// subsidiaryContact.find { contact -> contact.name == it.custLeadName }?.id ?: customerContact.find { contact -> contact.name == it.custLeadName }?.id, clientSubsidiaryId = it.customerSubsidiary?.id, totalManhour = it.totalManhour, manhourPercentageByGrade = gradeAllocationRepository.findByProject(it) diff --git a/src/main/java/com/ffii/tsms/modules/project/web/models/EditProjectDetails.kt b/src/main/java/com/ffii/tsms/modules/project/web/models/EditProjectDetails.kt index c7ea88c..704f0fd 100644 --- a/src/main/java/com/ffii/tsms/modules/project/web/models/EditProjectDetails.kt +++ b/src/main/java/com/ffii/tsms/modules/project/web/models/EditProjectDetails.kt @@ -15,6 +15,7 @@ data class EditProjectDetails( val projectActualEnd: LocalDate?, val projectStatus: String?, val isClpProject: Boolean?, + val mainProjectId: Long?, val serviceTypeId: Long?, val fundingTypeId: Long?, diff --git a/src/main/resources/db/changelog/changes/20240522_01_cyril/01_update_project.sql b/src/main/resources/db/changelog/changes/20240522_01_cyril/01_update_project.sql new file mode 100644 index 0000000..96bc4d4 --- /dev/null +++ b/src/main/resources/db/changelog/changes/20240522_01_cyril/01_update_project.sql @@ -0,0 +1,20 @@ +-- liquibase formatted sql +-- changeset cyril:project + +ALTER TABLE `project` + ADD COLUMN `customerContactId` INT NULL DEFAULT NULL AFTER `mainProjectId`, +ADD COLUMN `subsidiaryContactId` INT NULL DEFAULT NULL AFTER `customerContactId`, +ADD INDEX `FK_PROJECT_ON_CUSTOMERCONTACTID` (`customerContactId` ASC) VISIBLE, +ADD INDEX `FK_PROJECT_ON_SUBSIDIARYCONTACTID` (`subsidiaryContactId` ASC) VISIBLE; +; +ALTER TABLE `project` + ADD CONSTRAINT `FK_PROJECT_ON_CUSTOMERCONTACTID` + FOREIGN KEY (`customerContactId`) + REFERENCES `customer_contact` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION, +ADD CONSTRAINT `FK_PROJECT_ON_SUBSIDIARYCONTACTID` + FOREIGN KEY (`subsidiaryContactId`) + REFERENCES `subsidiary_contact` (`id`) + ON DELETE NO ACTION + ON UPDATE NO ACTION;