1 /*
2 * *************************************************************************************************************************************************************
3 *
4 * blueHour: open source accounting
5 * http://tidalwave.it/projects/bluehour
6 *
7 * Copyright (C) 2013 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8 *
9 * *************************************************************************************************************************************************************
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18 *
19 * *************************************************************************************************************************************************************
20 *
21 * git clone https://bitbucket.org/tidalwave/bluehour-src
22 * git clone https://github.com/tidalwave-it/bluehour-src
23 *
24 * *************************************************************************************************************************************************************
25 */
26 package it.tidalwave.accounting.model.impl;
27
28 import javax.annotation.Nonnull;
29 import javax.annotation.concurrent.Immutable;
30 import java.time.Duration;
31 import java.time.LocalDate;
32 import java.util.List;
33 import it.tidalwave.accounting.model.Accounting;
34 import it.tidalwave.accounting.model.Customer;
35 import it.tidalwave.accounting.model.Project;
36 import it.tidalwave.accounting.model.ProjectRegistry.JobEventFinder;
37 import it.tidalwave.accounting.model.spi.ProjectSpi;
38 import it.tidalwave.accounting.model.types.Money;
39 import it.tidalwave.util.As;
40 import it.tidalwave.util.Id;
41 import lombok.AllArgsConstructor;
42 import lombok.EqualsAndHashCode;
43 import lombok.Getter;
44 import lombok.Setter;
45 import lombok.ToString;
46 import lombok.With;
47 import lombok.experimental.Delegate;
48 import static lombok.AccessLevel.PRIVATE;
49
50 /***************************************************************************************************************************************************************
51 *
52 * This class models a project.
53 *
54 * @author Fabrizio Giudici
55 *
56 **************************************************************************************************************************************************************/
57 @Immutable @With
58 @AllArgsConstructor(access = PRIVATE) @EqualsAndHashCode @ToString(exclude = {"events", "as", "accounting"})
59 public class InMemoryProject implements ProjectSpi
60 {
61 @Setter // FIXME
62 private Accounting accounting;
63
64 @Delegate
65 private final As as = As.forObject(this);
66
67 @Getter @Nonnull
68 private final Id id;
69
70 @Getter @Nonnull
71 private final Customer customer;
72
73 @Getter @Nonnull
74 private final String name;
75
76 @Getter @Nonnull
77 private final String number;
78
79 @Getter @Nonnull
80 private final String description;
81
82 @Getter @Nonnull
83 private final String notes;
84
85 @Getter
86 private final Status status;
87
88 @Getter @Nonnull
89 private final Money hourlyRate;
90
91 @Getter @Nonnull
92 private final Money budget;
93
94 @Getter @Nonnull
95 private final LocalDate startDate;
96
97 @Getter @Nonnull
98 private final LocalDate endDate;
99
100 @Nonnull
101 private final List<InMemoryJobEvent> events; // FIXME: immutable
102
103 /***********************************************************************************************************************************************************
104 *
105 **********************************************************************************************************************************************************/
106 public /* FIXME protected */ InMemoryProject (@Nonnull final Builder builder)
107 {
108 this.id = builder.getId();
109 this.customer = builder.getCustomer();
110 this.name = builder.getName();
111 this.number = builder.getNumber();
112 this.description = builder.getDescription();
113 this.notes = builder.getNotes();
114 this.status = builder.getStatus();
115 this.hourlyRate = builder.getHourlyRate();
116 this.budget = builder.getBudget();
117 this.startDate = builder.getStartDate();
118 this.endDate = builder.getEndDate();
119 this.events = (List<InMemoryJobEvent>)builder.getEvents();
120 }
121
122 /***********************************************************************************************************************************************************
123 * {@inheritDoc}
124 **********************************************************************************************************************************************************/
125 @Override @Nonnull
126 public JobEventFinder findChildren()
127 {
128 return new InMemoryJobEventFinderFromList(events);
129 }
130
131 /***********************************************************************************************************************************************************
132 * {@inheritDoc}
133 **********************************************************************************************************************************************************/
134 @Override @Nonnull
135 public Money getEarnings()
136 {
137 return findChildren().getEarnings();
138 }
139
140 /***********************************************************************************************************************************************************
141 * {@inheritDoc}
142 **********************************************************************************************************************************************************/
143 @Override @Nonnull
144 public Duration getDuration()
145 {
146 return findChildren().getDuration();
147 }
148
149 /***********************************************************************************************************************************************************
150 * {@inheritDoc}
151 **********************************************************************************************************************************************************/
152 @Override @Nonnull
153 public Money getInvoicedEarnings()
154 {
155 return accounting.getInvoiceRegistry().findInvoices().withProject(this).getEarnings();
156 }
157
158 /***********************************************************************************************************************************************************
159 * {@inheritDoc}
160 **********************************************************************************************************************************************************/
161 @Override @Nonnull
162 public Builder toBuilder()
163 {
164 return new Builder(id, customer, name, number, description, notes, status, hourlyRate, budget,
165 startDate, endDate, events, Project.Builder.Callback.DEFAULT);
166 }
167 }