| @@ -3,6 +3,9 @@ package com.ffii.tsms.modules.data.entity; | |||||
| import com.ffii.core.entity.BaseEntity; | import com.ffii.core.entity.BaseEntity; | ||||
| import jakarta.persistence.Column; | import jakarta.persistence.Column; | ||||
| import jakarta.persistence.Entity; | import jakarta.persistence.Entity; | ||||
| import jakarta.persistence.JoinColumn; | |||||
| import jakarta.persistence.ManyToOne; | |||||
| import jakarta.persistence.OneToOne; | |||||
| import jakarta.persistence.Table; | import jakarta.persistence.Table; | ||||
| import jakarta.validation.constraints.NotNull; | import jakarta.validation.constraints.NotNull; | ||||
| @@ -23,18 +26,13 @@ public class Customer extends BaseEntity<Long> { | |||||
| @Column(name = "district", length = 30) | @Column(name = "district", length = 30) | ||||
| private String district; | private String district; | ||||
| @Column(name = "email") | |||||
| private String email; | |||||
| @Column(name = "phone", length = 20) | |||||
| private String phone; | |||||
| @Column(name = "contactName", length = 30) | |||||
| private String contactName; | |||||
| @Column(name = "brNo", length = 20) | @Column(name = "brNo", length = 20) | ||||
| private String brNo; | private String brNo; | ||||
| @OneToOne | |||||
| @JoinColumn(name = "typeId") | |||||
| private CustomerType customerType; | |||||
| public String getAddress() { | public String getAddress() { | ||||
| return address; | return address; | ||||
| } | } | ||||
| @@ -51,30 +49,6 @@ public class Customer extends BaseEntity<Long> { | |||||
| this.district = district; | this.district = district; | ||||
| } | } | ||||
| public String getEmail() { | |||||
| return email; | |||||
| } | |||||
| public void setEmail(String email) { | |||||
| this.email = email; | |||||
| } | |||||
| public String getPhone() { | |||||
| return phone; | |||||
| } | |||||
| public void setPhone(String phone) { | |||||
| this.phone = phone; | |||||
| } | |||||
| public String getContactName() { | |||||
| return contactName; | |||||
| } | |||||
| public void setContactName(String contactName) { | |||||
| this.contactName = contactName; | |||||
| } | |||||
| public String getBrNo() { | public String getBrNo() { | ||||
| return brNo; | return brNo; | ||||
| } | } | ||||
| @@ -98,4 +72,12 @@ public class Customer extends BaseEntity<Long> { | |||||
| public void setCode(String code) { | public void setCode(String code) { | ||||
| this.code = code; | this.code = code; | ||||
| } | } | ||||
| public CustomerType getCustomerType() { | |||||
| return customerType; | |||||
| } | |||||
| public void setCustomerType(CustomerType customerType) { | |||||
| this.customerType = customerType; | |||||
| } | |||||
| } | } | ||||
| @@ -0,0 +1,62 @@ | |||||
| package com.ffii.tsms.modules.data.entity; | |||||
| import com.ffii.core.entity.IdEntity; | |||||
| import jakarta.persistence.Column; | |||||
| import jakarta.persistence.Entity; | |||||
| import jakarta.persistence.JoinColumn; | |||||
| import jakarta.persistence.OneToOne; | |||||
| import jakarta.persistence.Table; | |||||
| import jakarta.validation.constraints.NotNull; | |||||
| @Entity | |||||
| @Table(name = "customer_contact") | |||||
| public class CustomerContact extends IdEntity<Long> { | |||||
| @NotNull | |||||
| @OneToOne | |||||
| @JoinColumn(name = "customerId") | |||||
| private Customer customer; | |||||
| @NotNull | |||||
| @Column(name = "name", length = 30) | |||||
| private String name; | |||||
| @NotNull | |||||
| @Column(name = "email", length = 255) | |||||
| private String email; | |||||
| @NotNull | |||||
| @Column(name = "phone", length = 20) | |||||
| private String phone; | |||||
| public Customer getCustomer() { | |||||
| return customer; | |||||
| } | |||||
| public void setCustomer(Customer customer) { | |||||
| this.customer = customer; | |||||
| } | |||||
| public String getName() { | |||||
| return name; | |||||
| } | |||||
| public void setName(String name) { | |||||
| this.name = name; | |||||
| } | |||||
| public String getEmail() { | |||||
| return email; | |||||
| } | |||||
| public void setEmail(String email) { | |||||
| this.email = email; | |||||
| } | |||||
| public String getPhone() { | |||||
| return phone; | |||||
| } | |||||
| public void setPhone(String phone) { | |||||
| this.phone = phone; | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,7 @@ | |||||
| package com.ffii.tsms.modules.data.entity; | |||||
| import com.ffii.core.support.AbstractRepository; | |||||
| public interface CustomerContactRepository extends AbstractRepository<CustomerContact, Long> { | |||||
| } | |||||
| @@ -1,6 +1,9 @@ | |||||
| package com.ffii.tsms.modules.data.entity; | package com.ffii.tsms.modules.data.entity; | ||||
| import java.util.Optional; | |||||
| import com.ffii.core.support.AbstractRepository; | import com.ffii.core.support.AbstractRepository; | ||||
| public interface CustomerRepository extends AbstractRepository<Customer, Long> { | public interface CustomerRepository extends AbstractRepository<Customer, Long> { | ||||
| } | } | ||||
| @@ -29,7 +29,7 @@ public class CustomerSubsidiary extends IdEntity<Long> { | |||||
| return customer; | return customer; | ||||
| } | } | ||||
| public void setCustomerId(Customer customer) { | |||||
| public void setCustomer(Customer customer) { | |||||
| this.customer = customer; | this.customer = customer; | ||||
| } | } | ||||
| } | } | ||||
| @@ -0,0 +1,14 @@ | |||||
| package com.ffii.tsms.modules.data.entity; | |||||
| import java.util.List; | |||||
| import java.util.Optional; | |||||
| import org.springframework.data.repository.query.Param; | |||||
| import com.ffii.core.support.AbstractRepository; | |||||
| public interface CustomerSubsidiaryRepository extends AbstractRepository<CustomerSubsidiary, Long> { | |||||
| List<CustomerSubsidiary> findAllByCustomerId(@Param("customerId") Long customerId); | |||||
| } | |||||
| @@ -0,0 +1,23 @@ | |||||
| package com.ffii.tsms.modules.data.entity; | |||||
| import com.ffii.core.entity.IdEntity; | |||||
| import jakarta.persistence.Column; | |||||
| import jakarta.persistence.Entity; | |||||
| import jakarta.persistence.Table; | |||||
| import jakarta.validation.constraints.NotNull; | |||||
| @Entity | |||||
| @Table(name = "customer_type") | |||||
| public class CustomerType extends IdEntity<Long> { | |||||
| @NotNull | |||||
| @Column(name = "name") | |||||
| private String name; | |||||
| public String getName() { | |||||
| return name; | |||||
| } | |||||
| public void setName(String name) { | |||||
| this.name = name; | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,7 @@ | |||||
| package com.ffii.tsms.modules.data.entity; | |||||
| import com.ffii.core.support.AbstractRepository; | |||||
| public interface CustomerTypeRepository extends AbstractRepository<CustomerType, Long> { | |||||
| } | |||||
| @@ -22,21 +22,12 @@ public class Subsidiary extends BaseEntity<Long> { | |||||
| @Column(name = "brNo", length = 20) | @Column(name = "brNo", length = 20) | ||||
| private String brNo; | private String brNo; | ||||
| @Column(name = "contactName", length = 30) | |||||
| private String contactName; | |||||
| @Column(name = "phone", length = 20) | |||||
| private String phone; | |||||
| @Column(name = "address", length = 500) | @Column(name = "address", length = 500) | ||||
| private String address; | private String address; | ||||
| @Column(name = "district", length = 30) | @Column(name = "district", length = 30) | ||||
| private String district; | private String district; | ||||
| @Column(name = "email") | |||||
| private String email; | |||||
| public String getAddress() { | public String getAddress() { | ||||
| return address; | return address; | ||||
| } | } | ||||
| @@ -53,30 +44,6 @@ public class Subsidiary extends BaseEntity<Long> { | |||||
| this.district = district; | this.district = district; | ||||
| } | } | ||||
| public String getEmail() { | |||||
| return email; | |||||
| } | |||||
| public void setEmail(String email) { | |||||
| this.email = email; | |||||
| } | |||||
| public String getPhone() { | |||||
| return phone; | |||||
| } | |||||
| public void setPhone(String phone) { | |||||
| this.phone = phone; | |||||
| } | |||||
| public String getContactName() { | |||||
| return contactName; | |||||
| } | |||||
| public void setContactName(String contactName) { | |||||
| this.contactName = contactName; | |||||
| } | |||||
| public String getBrNo() { | public String getBrNo() { | ||||
| return brNo; | return brNo; | ||||
| } | } | ||||
| @@ -0,0 +1,62 @@ | |||||
| package com.ffii.tsms.modules.data.entity; | |||||
| import com.ffii.core.entity.IdEntity; | |||||
| import jakarta.persistence.Column; | |||||
| import jakarta.persistence.Entity; | |||||
| import jakarta.persistence.JoinColumn; | |||||
| import jakarta.persistence.OneToOne; | |||||
| import jakarta.persistence.Table; | |||||
| import jakarta.validation.constraints.NotNull; | |||||
| @Entity | |||||
| @Table(name = "subsidiary_contact") | |||||
| public class SubsidiaryContact extends IdEntity<Long> { | |||||
| @NotNull | |||||
| @OneToOne | |||||
| @JoinColumn(name = "subsidiaryId") | |||||
| private Subsidiary subsidiary; | |||||
| @NotNull | |||||
| @Column(name = "name", length = 30) | |||||
| private String name; | |||||
| @NotNull | |||||
| @Column(name = "email", length = 255) | |||||
| private String email; | |||||
| @NotNull | |||||
| @Column(name = "phone", length = 20) | |||||
| private String phone; | |||||
| public Subsidiary getSubsidiary() { | |||||
| return subsidiary; | |||||
| } | |||||
| public void setSubsidiary(Subsidiary subsidiary) { | |||||
| this.subsidiary = subsidiary; | |||||
| } | |||||
| public String getName() { | |||||
| return name; | |||||
| } | |||||
| public void setName(String name) { | |||||
| this.name = name; | |||||
| } | |||||
| public String getEmail() { | |||||
| return email; | |||||
| } | |||||
| public void setEmail(String email) { | |||||
| this.email = email; | |||||
| } | |||||
| public String getPhone() { | |||||
| return phone; | |||||
| } | |||||
| public void setPhone(String phone) { | |||||
| this.phone = phone; | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,7 @@ | |||||
| package com.ffii.tsms.modules.data.entity; | |||||
| import com.ffii.core.support.AbstractRepository; | |||||
| public interface SubsidiaryContactRepository extends AbstractRepository<SubsidiaryContact, Long> { | |||||
| } | |||||
| @@ -0,0 +1,23 @@ | |||||
| package com.ffii.tsms.modules.data.entity; | |||||
| import com.ffii.core.entity.IdEntity; | |||||
| import jakarta.persistence.Column; | |||||
| import jakarta.persistence.Entity; | |||||
| import jakarta.persistence.Table; | |||||
| import jakarta.validation.constraints.NotNull; | |||||
| @Entity | |||||
| @Table(name = "subsidiary_type") | |||||
| public class SubsidiaryType extends IdEntity<Long> { | |||||
| @NotNull | |||||
| @Column(name = "name") | |||||
| private String name; | |||||
| public String getName() { | |||||
| return name; | |||||
| } | |||||
| public void setName(String name) { | |||||
| this.name = name; | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,7 @@ | |||||
| package com.ffii.tsms.modules.data.entity; | |||||
| import com.ffii.core.support.AbstractRepository; | |||||
| public interface SubsidiaryTypeRepository extends AbstractRepository<SubsidiaryType, Long> { | |||||
| } | |||||
| @@ -1,20 +1,54 @@ | |||||
| package com.ffii.tsms.modules.data.service | package com.ffii.tsms.modules.data.service | ||||
| import com.ffii.tsms.modules.data.entity.Customer | import com.ffii.tsms.modules.data.entity.Customer | ||||
| import com.ffii.tsms.modules.data.entity.CustomerType | |||||
| import com.ffii.tsms.modules.data.entity.CustomerRepository | import com.ffii.tsms.modules.data.entity.CustomerRepository | ||||
| import com.ffii.tsms.modules.data.entity.CustomerTypeRepository | |||||
| import com.ffii.tsms.modules.project.web.models.SaveCustomerRequest | |||||
| import org.springframework.stereotype.Service | import org.springframework.stereotype.Service | ||||
| @Service | @Service | ||||
| class CustomerService(private val customerRepository: CustomerRepository) { | |||||
| fun saveCustomer(name: String, code: String, email: String, phone: String, contactName: String): Customer { | |||||
| return customerRepository.save( | |||||
| Customer().apply { | |||||
| this.name = name | |||||
| this.email = email | |||||
| this.phone = phone | |||||
| this.code = code | |||||
| this.contactName = contactName | |||||
| class CustomerService(private val customerRepository: CustomerRepository, private val customerTypeRepository: CustomerTypeRepository, private val customerSubsidiaryService: CustomerSubsidiaryService) { | |||||
| fun allCustomers(): List<Customer> { | |||||
| return customerRepository.findAll() | |||||
| } | |||||
| fun allCustomerTypes(): List<CustomerType> { | |||||
| return customerTypeRepository.findAll() | |||||
| } | |||||
| fun findCustomer(id: Long): Customer { | |||||
| return customerRepository.findById(id).orElseThrow() | |||||
| } | |||||
| fun saveCustomer(saveCustomer: SaveCustomerRequest): Customer { | |||||
| val customer = | |||||
| customerRepository.saveAndFlush( | |||||
| Customer().apply { | |||||
| id = saveCustomer.id | |||||
| code = saveCustomer.code | |||||
| name = saveCustomer.name | |||||
| address = saveCustomer.address | |||||
| district = saveCustomer.district | |||||
| brNo = saveCustomer.brNo | |||||
| } | |||||
| ) | |||||
| val customerId = customer.id | |||||
| if (customerId != null && customerId > 0) { | |||||
| if (saveCustomer.deleteSubsidiaryIds.size > 0) { | |||||
| customerSubsidiaryService.deleteSubsidiariesByCustomer(customerId, saveCustomer.deleteSubsidiaryIds) | |||||
| } | |||||
| if (saveCustomer.addSubsidiaryIds.size > 0) { | |||||
| customerSubsidiaryService.saveSubsidiariesByCustomer(customerId, saveCustomer.addSubsidiaryIds) | |||||
| } | } | ||||
| ) | |||||
| } | |||||
| return customer | |||||
| } | } | ||||
| } | |||||
| } | |||||
| @@ -0,0 +1,67 @@ | |||||
| package com.ffii.tsms.modules.data.service | |||||
| import com.ffii.tsms.modules.data.entity.CustomerRepository | |||||
| import com.ffii.tsms.modules.data.entity.CustomerSubsidiary | |||||
| import com.ffii.tsms.modules.data.entity.CustomerSubsidiaryRepository | |||||
| import com.ffii.tsms.modules.data.entity.SubsidiaryRepository | |||||
| import org.springframework.stereotype.Service | |||||
| @Service | |||||
| class CustomerSubsidiaryService( | |||||
| private val customerSubsidiaryRepository: CustomerSubsidiaryRepository, | |||||
| private val customerRepository: CustomerRepository, | |||||
| private val subsidiaryRepository: SubsidiaryRepository | |||||
| ) { | |||||
| fun allCustomeriesSubsidiaries(): List<CustomerSubsidiary> { | |||||
| return customerSubsidiaryRepository.findAll() | |||||
| } | |||||
| fun findAllSubsidiaryIdsByCustomerId(customerId : Long) : List<Long> { | |||||
| val customerSubsidiaryList: List<CustomerSubsidiary> = customerSubsidiaryRepository.findAllByCustomerId(customerId) | |||||
| if (customerSubsidiaryList.size > 0) { | |||||
| return customerSubsidiaryList.map { it.subsidiary.id }.filterNotNull() | |||||
| } else return emptyList() | |||||
| } | |||||
| fun saveSubsidiariesByCustomer(saveCustomerId: Long, saveSubsidiaryIds: List<Long>) { | |||||
| val customerSubsidiaryList = mutableListOf<CustomerSubsidiary>() | |||||
| val subsidiaryIdsSize = saveSubsidiaryIds.size | |||||
| for (i in 0 until subsidiaryIdsSize) { | |||||
| val customer = customerRepository.findById(saveCustomerId).orElseThrow() | |||||
| val subsidiary = subsidiaryRepository.findById(saveSubsidiaryIds[i]).orElseThrow() | |||||
| val customerSubsidiary = CustomerSubsidiary().apply { | |||||
| this.customer = customer | |||||
| this.subsidiary = subsidiary | |||||
| } | |||||
| customerSubsidiaryList.add(customerSubsidiary) | |||||
| } | |||||
| customerSubsidiaryRepository.saveAll(customerSubsidiaryList) | |||||
| } | |||||
| fun deleteSubsidiariesByCustomer(deleteCustomerId: Long, deleteSubsidiaryIds: List<Long>) { | |||||
| val customerSubsidiaryList = mutableListOf<CustomerSubsidiary>() | |||||
| val subsidiaryIdsSize = deleteSubsidiaryIds.size | |||||
| for (i in 0 until subsidiaryIdsSize) { | |||||
| val customer = customerRepository.findById(deleteCustomerId).orElseThrow() | |||||
| val subsidiary = subsidiaryRepository.findById(deleteSubsidiaryIds[i]).orElseThrow() | |||||
| val customerSubsidiary = CustomerSubsidiary().apply { | |||||
| this.customer = customer | |||||
| this.subsidiary = subsidiary | |||||
| } | |||||
| customerSubsidiaryList.add(customerSubsidiary) | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,30 @@ | |||||
| package com.ffii.tsms.modules.project.service | |||||
| import com.ffii.tsms.modules.data.entity.SubsidiaryRepository | |||||
| import com.ffii.tsms.modules.data.entity.Subsidiary | |||||
| import com.ffii.tsms.modules.data.service.CustomerService | |||||
| import com.ffii.tsms.modules.project.web.models.NewSubsidiaryRequest | |||||
| import org.springframework.stereotype.Service | |||||
| @Service | |||||
| class SubsidiaryService( | |||||
| private val subsidiaryRepository: SubsidiaryRepository | |||||
| ) { | |||||
| fun allSubsidiaries(): List<Subsidiary> { | |||||
| return subsidiaryRepository.findAll() | |||||
| } | |||||
| fun saveSubsidiary(request: NewSubsidiaryRequest): Subsidiary { | |||||
| // TODO: Add tasks, milestones, allocated | |||||
| val subsidary = Subsidiary().apply { | |||||
| code = request.code | |||||
| name = request.name | |||||
| brNo = request.brNo | |||||
| address = request.address | |||||
| district = request.district | |||||
| } | |||||
| return subsidiaryRepository.save(subsidary) | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,43 @@ | |||||
| package com.ffii.tsms.modules.data.web | |||||
| import com.ffii.tsms.modules.data.entity.Customer | |||||
| import com.ffii.tsms.modules.data.entity.CustomerType | |||||
| import com.ffii.tsms.modules.data.service.CustomerService | |||||
| import com.ffii.tsms.modules.data.service.CustomerSubsidiaryService | |||||
| import com.ffii.tsms.modules.project.web.models.SaveCustomerRequest | |||||
| import com.ffii.tsms.modules.project.web.models.CustomerResponse | |||||
| import org.springframework.web.bind.annotation.GetMapping | |||||
| import org.springframework.web.bind.annotation.RequestMapping | |||||
| import org.springframework.web.bind.annotation.RestController | |||||
| import org.springframework.web.bind.annotation.RequestBody | |||||
| import org.springframework.web.bind.annotation.PostMapping | |||||
| import org.springframework.web.bind.annotation.PathVariable | |||||
| import jakarta.validation.Valid | |||||
| @RestController | |||||
| @RequestMapping("/customer") | |||||
| class CustomerController(private val customerService: CustomerService, private val customerSubsidiaryService: CustomerSubsidiaryService) { | |||||
| @GetMapping | |||||
| fun allCustomers(): List<Customer> { | |||||
| return customerService.allCustomers() | |||||
| } | |||||
| @GetMapping("/types") | |||||
| fun allCustomerTypes(): List<CustomerType> { | |||||
| return customerService.allCustomerTypes() | |||||
| } | |||||
| @GetMapping("/{id}") | |||||
| fun findCustomer(@PathVariable id: Long): CustomerResponse { | |||||
| val customer = customerService.findCustomer(id) | |||||
| val subsidiaryIds = customerSubsidiaryService.findAllSubsidiaryIdsByCustomerId(id) | |||||
| return CustomerResponse(customer, subsidiaryIds) | |||||
| } | |||||
| @PostMapping("/save") | |||||
| fun saveCustomer(@Valid @RequestBody saveCustomer: SaveCustomerRequest): Customer { | |||||
| return customerService.saveCustomer(saveCustomer) | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,31 @@ | |||||
| package com.ffii.tsms.modules.data.web | |||||
| import com.ffii.tsms.modules.data.entity.Customer | |||||
| import com.ffii.tsms.modules.data.entity.CustomerSubsidiary | |||||
| import com.ffii.tsms.modules.data.service.CustomerService | |||||
| import com.ffii.tsms.modules.data.service.CustomerSubsidiaryService | |||||
| import com.ffii.tsms.modules.project.web.models.SaveCustomerRequest | |||||
| import com.ffii.tsms.modules.project.web.models.CustomerResponse | |||||
| import org.springframework.web.bind.annotation.GetMapping | |||||
| import org.springframework.web.bind.annotation.RequestMapping | |||||
| import org.springframework.web.bind.annotation.RestController | |||||
| import org.springframework.web.bind.annotation.RequestBody | |||||
| import org.springframework.web.bind.annotation.PostMapping | |||||
| import org.springframework.web.bind.annotation.PathVariable | |||||
| import jakarta.validation.Valid | |||||
| import com.ffii.core.support.JdbcDao | |||||
| @RestController | |||||
| @RequestMapping("/customer-subsidiary") | |||||
| class CustomerSubsidiaryController(private val customerSubsidiaryService: CustomerSubsidiaryService) { | |||||
| @GetMapping | |||||
| fun allCustomeriesSubsidiaries(): List<CustomerSubsidiary> { | |||||
| // JdbcDao.queryForString(sql) | |||||
| return customerSubsidiaryService.allCustomeriesSubsidiaries() | |||||
| } | |||||
| @GetMapping("/subsidiaries/{customerId}") | |||||
| fun findAllSubsidiaryIdsByCustomerId(@PathVariable customerId: Long): List<Long> { | |||||
| return customerSubsidiaryService.findAllSubsidiaryIdsByCustomerId(customerId) | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,27 @@ | |||||
| package com.ffii.tsms.modules.data.web | |||||
| import com.ffii.tsms.modules.data.entity.Staff | |||||
| import com.ffii.tsms.modules.data.entity.Subsidiary | |||||
| import com.ffii.tsms.modules.data.service.StaffsService | |||||
| import com.ffii.tsms.modules.project.service.SubsidiaryService | |||||
| import com.ffii.tsms.modules.project.web.models.NewSubsidiaryRequest | |||||
| import org.springframework.web.bind.annotation.GetMapping | |||||
| import org.springframework.web.bind.annotation.RequestMapping | |||||
| import org.springframework.web.bind.annotation.RestController | |||||
| import org.springframework.web.bind.annotation.RequestBody | |||||
| import org.springframework.web.bind.annotation.PostMapping | |||||
| import jakarta.validation.Valid | |||||
| @RestController | |||||
| @RequestMapping("/subsidiary") | |||||
| class SubsidiaryController(private val subsidiaryService: SubsidiaryService) { | |||||
| @GetMapping | |||||
| fun allSubsidiaries(): List<Subsidiary> { | |||||
| return subsidiaryService.allSubsidiaries() | |||||
| } | |||||
| @PostMapping("/new") | |||||
| fun saveSubsidiary(@Valid @RequestBody newSubsidary: NewSubsidiaryRequest): Subsidiary { | |||||
| return subsidiaryService.saveSubsidiary(newSubsidary) | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,8 @@ | |||||
| package com.ffii.tsms.modules.project.web.models | |||||
| import com.ffii.tsms.modules.data.entity.Customer | |||||
| data class CustomerResponse( | |||||
| val customer: Customer, | |||||
| val subsidiaryIds: List<Long>, | |||||
| ) | |||||
| @@ -0,0 +1,19 @@ | |||||
| package com.ffii.tsms.modules.project.web.models | |||||
| import jakarta.validation.constraints.NotBlank | |||||
| data class NewSubsidiaryRequest( | |||||
| @field:NotBlank(message = "subsidary code cannot be empty") | |||||
| val code: String, | |||||
| @field:NotBlank(message = "subsidary name cannot be empty") | |||||
| val name: String, | |||||
| val brNo: String, | |||||
| val contactName: String, | |||||
| val phone: String, | |||||
| val address: String, | |||||
| val district: String, | |||||
| val email: String, | |||||
| val allocatedCustomerIds: List<Long>, | |||||
| ) | |||||
| @@ -0,0 +1,19 @@ | |||||
| package com.ffii.tsms.modules.project.web.models | |||||
| import jakarta.validation.constraints.NotBlank | |||||
| data class SaveCustomerRequest( | |||||
| @field:NotBlank(message = "customer code cannot be empty") | |||||
| val code: String, | |||||
| @field:NotBlank(message = "customer name cannot be empty") | |||||
| val name: String, | |||||
| val brNo: String?, | |||||
| val address: String?, | |||||
| val district: String?, | |||||
| val deleteSubsidiaryIds: List<Long>, | |||||
| val addSubsidiaryIds: List<Long>, | |||||
| val id: Long?, | |||||
| ) | |||||
| @@ -8,13 +8,15 @@ import com.ffii.tsms.modules.project.entity.ProjectCategory | |||||
| import com.ffii.tsms.modules.project.entity.ProjectCategoryRepository | import com.ffii.tsms.modules.project.entity.ProjectCategoryRepository | ||||
| import com.ffii.tsms.modules.project.entity.ProjectRepository | import com.ffii.tsms.modules.project.entity.ProjectRepository | ||||
| import com.ffii.tsms.modules.project.web.models.NewProjectRequest | import com.ffii.tsms.modules.project.web.models.NewProjectRequest | ||||
| import com.ffii.tsms.modules.project.web.models.SaveCustomerRequest | |||||
| import org.springframework.stereotype.Service | import org.springframework.stereotype.Service | ||||
| @Service | @Service | ||||
| class ProjectsService( | class ProjectsService( | ||||
| private val projectRepository: ProjectRepository, | |||||
| private val customerService: CustomerService, private val projectCategoryRepository: ProjectCategoryRepository, | |||||
| private val staffRepository: StaffRepository | |||||
| private val projectRepository: ProjectRepository, | |||||
| private val customerService: CustomerService, | |||||
| private val projectCategoryRepository: ProjectCategoryRepository, | |||||
| private val staffRepository: StaffRepository | |||||
| ) { | ) { | ||||
| fun allProjects(): List<ProjectSearchInfo> { | fun allProjects(): List<ProjectSearchInfo> { | ||||
| return projectRepository.findProjectSearchInfoBy() | return projectRepository.findProjectSearchInfoBy() | ||||
| @@ -25,20 +27,37 @@ class ProjectsService( | |||||
| } | } | ||||
| fun saveProject(request: NewProjectRequest): Project { | fun saveProject(request: NewProjectRequest): Project { | ||||
| val projectCategory = projectCategoryRepository.findById(request.projectCategoryId).orElseThrow() | |||||
| val projectCategory = | |||||
| projectCategoryRepository.findById(request.projectCategoryId).orElseThrow() | |||||
| val teamLead = staffRepository.findById(request.projectLeadId).orElseThrow() | val teamLead = staffRepository.findById(request.projectLeadId).orElseThrow() | ||||
| val customer = customerService.saveCustomer(request.clientName, request.clientCode, request.clientEmail, request.clientPhone, request.clientContactName) | |||||
| val _customer = | |||||
| SaveCustomerRequest( | |||||
| name = request.clientName, | |||||
| code = request.clientCode, | |||||
| // email = request.clientEmail, | |||||
| // phone = request.clientPhone, | |||||
| // contactName = request.clientContactName, | |||||
| brNo = null, | |||||
| address = null, | |||||
| district = null, | |||||
| deleteSubsidiaryIds = emptyList(), | |||||
| addSubsidiaryIds = emptyList(), | |||||
| id = null | |||||
| ) | |||||
| val customer = customerService.saveCustomer(_customer) | |||||
| // TODO: Add tasks, milestones, allocated | // TODO: Add tasks, milestones, allocated | ||||
| val project = Project().apply { | |||||
| name = request.projectName | |||||
| description = request.projectDescription | |||||
| code = request.projectCode | |||||
| this.projectCategory = projectCategory | |||||
| this.teamLead = teamLead | |||||
| this.customer = customer | |||||
| } | |||||
| val project = | |||||
| Project().apply { | |||||
| name = request.projectName | |||||
| description = request.projectDescription | |||||
| code = request.projectCode | |||||
| this.projectCategory = projectCategory | |||||
| this.teamLead = teamLead | |||||
| this.customer = customer | |||||
| } | |||||
| return projectRepository.save(project) | return projectRepository.save(project) | ||||
| } | } | ||||
| } | |||||
| } | |||||
| @@ -0,0 +1,45 @@ | |||||
| -- liquibase formatted sql | |||||
| -- changeset cyril:update_customer | |||||
| ALTER TABLE `customer` | |||||
| DROP COLUMN `contactName`, | |||||
| DROP COLUMN `phone`, | |||||
| DROP COLUMN `email`, | |||||
| ADD COLUMN `typeId` INT NOT NULL AFTER `brNo`; | |||||
| CREATE TABLE `customer_type` ( | |||||
| `id` INT NOT NULL AUTO_INCREMENT, | |||||
| `name` VARCHAR(20) NOT NULL, | |||||
| PRIMARY KEY (`id`)); | |||||
| ALTER TABLE customer ADD CONSTRAINT FK_CUSTOMER_ON_TYPEID FOREIGN KEY (typeId) REFERENCES customer (id); | |||||
| CREATE TABLE `customer_contact` ( | |||||
| `id` INT NOT NULL AUTO_INCREMENT, | |||||
| `version` INT NOT NULL DEFAULT '0', | |||||
| `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | |||||
| `createdBy` VARCHAR(30) NULL DEFAULT NULL, | |||||
| `modified` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | |||||
| `modifiedBy` VARCHAR(30) NULL DEFAULT NULL, | |||||
| `deleted` TINYINT(1) NOT NULL DEFAULT '0', | |||||
| `customerId` INT NOT NULL, | |||||
| `name` VARCHAR(30) NOT NULL, | |||||
| `email` VARCHAR(255) NOT NULL, | |||||
| `phone` VARCHAR(20) NOT NULL, | |||||
| PRIMARY KEY (`id`)); | |||||
| ALTER TABLE customer_contact ADD CONSTRAINT FK_CUSTOMER_CONTACT_ON_CUSTOMERID FOREIGN KEY (customerId) REFERENCES customer (id); | |||||
| INSERT | |||||
| INTO | |||||
| customer_type | |||||
| (name) | |||||
| VALUES | |||||
| ('Architect'), | |||||
| ('Contractor'), | |||||
| ('Developer'), | |||||
| ('Government'), | |||||
| ('Institution'), | |||||
| ('NGO'), | |||||
| ('Private'), | |||||
| ('Utility'); | |||||
| @@ -0,0 +1,45 @@ | |||||
| -- liquibase formatted sql | |||||
| -- changeset cyril:update_subsidiary | |||||
| ALTER TABLE `subsidiary` | |||||
| DROP COLUMN `contactName`, | |||||
| DROP COLUMN `phone`, | |||||
| DROP COLUMN `email`, | |||||
| ADD COLUMN `typeId` INT NOT NULL AFTER `brNo`; | |||||
| CREATE TABLE `subsidiary_type` ( | |||||
| `id` INT NOT NULL AUTO_INCREMENT, | |||||
| `name` VARCHAR(20) NOT NULL, | |||||
| PRIMARY KEY (`id`)); | |||||
| ALTER TABLE subsidiary ADD CONSTRAINT FK_SUBSIDIARY_ON_TYPEID FOREIGN KEY (typeId) REFERENCES subsidiary (id); | |||||
| CREATE TABLE `subsidiary_contact` ( | |||||
| `id` INT NOT NULL AUTO_INCREMENT, | |||||
| `version` INT NOT NULL DEFAULT '0', | |||||
| `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | |||||
| `createdBy` VARCHAR(30) NULL DEFAULT NULL, | |||||
| `modified` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | |||||
| `modifiedBy` VARCHAR(30) NULL DEFAULT NULL, | |||||
| `deleted` TINYINT(1) NOT NULL DEFAULT '0', | |||||
| `subsidiaryId` INT NOT NULL, | |||||
| `name` VARCHAR(30) NOT NULL, | |||||
| `email` VARCHAR(255) NOT NULL, | |||||
| `phone` VARCHAR(20) NOT NULL, | |||||
| PRIMARY KEY (`id`)); | |||||
| ALTER TABLE subsidiary_contact ADD CONSTRAINT FK_SUBSIDIARY_CONTACT_ON_SUBSIDIARYID FOREIGN KEY (subsidiaryId) REFERENCES subsidiary (id); | |||||
| INSERT | |||||
| INTO | |||||
| subsidiary_type | |||||
| (name) | |||||
| VALUES | |||||
| ('Architect'), | |||||
| ('Contractor'), | |||||
| ('Developer'), | |||||
| ('Government'), | |||||
| ('Institution'), | |||||
| ('NGO'), | |||||
| ('Private'), | |||||
| ('Utility'); | |||||