-
Notifications
You must be signed in to change notification settings - Fork 0
Add SQL for Invoice #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Pipatq
wants to merge
1
commit into
feature/handle/jwt
Choose a base branch
from
backend/api/pdfservice
base: feature/handle/jwt
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
backend/src/main/java/apartment/example/backend/controller/InvoiceController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| package apartment.example.backend.controller; | ||
|
|
||
| import apartment.example.backend.entity.Invoice; | ||
| import apartment.example.backend.entity.enums.PaymentType; | ||
| import apartment.example.backend.service.InvoiceService; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.LocalDate; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Invoice Controller | ||
| * | ||
| * REST API endpoints for invoice management | ||
| */ | ||
| @RestController | ||
| @RequestMapping("/invoices") | ||
| @CrossOrigin(origins = "*") | ||
| public class InvoiceController { | ||
|
|
||
| @Autowired | ||
| private InvoiceService invoiceService; | ||
|
|
||
| /** | ||
| * Create a new invoice with payment line items | ||
| * | ||
| * POST /invoices/create | ||
| * | ||
| * Request Body: | ||
| * { | ||
| * "leaseId": 1, | ||
| * "invoiceDate": "2025-11-13", | ||
| * "dueDate": "2025-11-28", | ||
| * "rentAmount": 5000, | ||
| * "electricityAmount": 800, | ||
| * "waterAmount": 200, | ||
| * "notes": "ค่าเช่าประจำเดือน พฤศจิกายน 2025" | ||
| * } | ||
| * | ||
| * Response: Invoice object with invoice number and payment line items | ||
| */ | ||
| @PostMapping("/create") | ||
| public ResponseEntity<Invoice> createInvoice(@RequestBody CreateInvoiceRequest request) { | ||
| try { | ||
| // Build payment items list | ||
| List<InvoiceService.PaymentItem> paymentItems = new ArrayList<>(); | ||
|
|
||
| if (request.getRentAmount() != null && request.getRentAmount().compareTo(BigDecimal.ZERO) > 0) { | ||
| paymentItems.add(new InvoiceService.PaymentItem( | ||
| PaymentType.RENT, | ||
| request.getRentAmount(), | ||
| "ค่าเช่า" | ||
| )); | ||
| } | ||
|
|
||
| if (request.getElectricityAmount() != null && request.getElectricityAmount().compareTo(BigDecimal.ZERO) > 0) { | ||
| paymentItems.add(new InvoiceService.PaymentItem( | ||
| PaymentType.ELECTRICITY, | ||
| request.getElectricityAmount(), | ||
| "ค่าไฟฟ้า" | ||
| )); | ||
| } | ||
|
|
||
| if (request.getWaterAmount() != null && request.getWaterAmount().compareTo(BigDecimal.ZERO) > 0) { | ||
| paymentItems.add(new InvoiceService.PaymentItem( | ||
| PaymentType.WATER, | ||
| request.getWaterAmount(), | ||
| "ค่าน้ำ" | ||
| )); | ||
| } | ||
|
|
||
| if (paymentItems.isEmpty()) { | ||
| return ResponseEntity.badRequest().build(); | ||
| } | ||
|
|
||
| // Create invoice with payments | ||
| Invoice invoice = invoiceService.createInvoiceWithPayments( | ||
| request.getLeaseId(), | ||
| request.getInvoiceDate(), | ||
| request.getDueDate(), | ||
| paymentItems, | ||
| request.getNotes() | ||
| ); | ||
|
|
||
| return ResponseEntity.ok(invoice); | ||
| } catch (RuntimeException e) { | ||
| return ResponseEntity.badRequest().body(null); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get invoice by ID | ||
| * | ||
| * GET /invoices/{id} | ||
| */ | ||
| @GetMapping("/{id}") | ||
| public ResponseEntity<Invoice> getInvoiceById(@PathVariable Long id) { | ||
| try { | ||
| Invoice invoice = invoiceService.getInvoiceById(id); | ||
| return ResponseEntity.ok(invoice); | ||
| } catch (RuntimeException e) { | ||
| return ResponseEntity.notFound().build(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get invoice by invoice number | ||
| * | ||
| * GET /invoices/number/{invoiceNumber} | ||
| */ | ||
| @GetMapping("/number/{invoiceNumber}") | ||
| public ResponseEntity<Invoice> getInvoiceByNumber(@PathVariable String invoiceNumber) { | ||
| try { | ||
| Invoice invoice = invoiceService.getInvoiceByNumber(invoiceNumber); | ||
| return ResponseEntity.ok(invoice); | ||
| } catch (RuntimeException e) { | ||
| return ResponseEntity.notFound().build(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get all invoices for a lease | ||
| * | ||
| * GET /invoices/lease/{leaseId} | ||
| */ | ||
| @GetMapping("/lease/{leaseId}") | ||
| public ResponseEntity<List<Invoice>> getInvoicesByLeaseId(@PathVariable Long leaseId) { | ||
| List<Invoice> invoices = invoiceService.getInvoicesByLeaseId(leaseId); | ||
| return ResponseEntity.ok(invoices); | ||
| } | ||
|
|
||
| /** | ||
| * Create Invoice Request DTO | ||
| */ | ||
| public static class CreateInvoiceRequest { | ||
| private Long leaseId; | ||
| private LocalDate invoiceDate; | ||
| private LocalDate dueDate; | ||
| private BigDecimal rentAmount; | ||
| private BigDecimal electricityAmount; | ||
| private BigDecimal waterAmount; | ||
| private String notes; | ||
|
|
||
| // Getters and Setters | ||
| public Long getLeaseId() { | ||
| return leaseId; | ||
| } | ||
|
|
||
| public void setLeaseId(Long leaseId) { | ||
| this.leaseId = leaseId; | ||
| } | ||
|
|
||
| public LocalDate getInvoiceDate() { | ||
| return invoiceDate; | ||
| } | ||
|
|
||
| public void setInvoiceDate(LocalDate invoiceDate) { | ||
| this.invoiceDate = invoiceDate; | ||
| } | ||
|
|
||
| public LocalDate getDueDate() { | ||
| return dueDate; | ||
| } | ||
|
|
||
| public void setDueDate(LocalDate dueDate) { | ||
| this.dueDate = dueDate; | ||
| } | ||
|
|
||
| public BigDecimal getRentAmount() { | ||
| return rentAmount; | ||
| } | ||
|
|
||
| public void setRentAmount(BigDecimal rentAmount) { | ||
| this.rentAmount = rentAmount; | ||
| } | ||
|
|
||
| public BigDecimal getElectricityAmount() { | ||
| return electricityAmount; | ||
| } | ||
|
|
||
| public void setElectricityAmount(BigDecimal electricityAmount) { | ||
| this.electricityAmount = electricityAmount; | ||
| } | ||
|
|
||
| public BigDecimal getWaterAmount() { | ||
| return waterAmount; | ||
| } | ||
|
|
||
| public void setWaterAmount(BigDecimal waterAmount) { | ||
| this.waterAmount = waterAmount; | ||
| } | ||
|
|
||
| public String getNotes() { | ||
| return notes; | ||
| } | ||
|
|
||
| public void setNotes(String notes) { | ||
| this.notes = notes; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The constraint
chk_invoice_datesuses>=which allows invoice date and due date to be the same. While this might be intentional, it's unusual in business contexts where typically a grace period is expected between invoice issuance and payment due date. Consider if this should be>instead to enforce at least one day difference.