Bill.java 10.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fi.insomnia.bortal.model;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

import org.eclipse.persistence.annotations.OptimisticLocking;
import org.eclipse.persistence.annotations.OptimisticLockingType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.insomnia.bortal.utilities.BillUtils;

/**
 * The system can send bills to the users. When user pays bill a AccountEvent
 * row is created for product whose price is +1 and Bills paidDate is changed to
 * not null.
 */
@Entity
@Table(name = "bills", uniqueConstraints = { @UniqueConstraint(columnNames = { Bill.EVENT_ID_COLUMN, "bill_number" }) })
@OptimisticLocking(type = OptimisticLockingType.CHANGED_COLUMNS)
public class Bill extends GenericEntity {

    /**
     * 
     */
    private static final long serialVersionUID = -6713643278149221869L;
    public static final String EVENT_ID_COLUMN = "event_id";

    @ManyToOne()
    @JoinColumn(name = EVENT_ID_COLUMN, nullable = false)
    private LanEvent event;

    /**
     * When the bill is due to be paid.
     */
    // Can be calculated from SentDate + paymentTime
    // @Column(name = "due_date")
    // @Temporal(TemporalType.TIMESTAMP)
    // private Calendar dueDate;

    /**
     * When the money has appeared on the bank account.
     */
    @Column(name = "paid_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date paidDate;

    /**
     * The bill number from which the reference number will be generated
     * 
     * @see http://www.fkl.fi/www/page/fk_www_1293
     * 
     */
    @Column(name = "bill_number")
    private Integer billNumber;

    /**
     * Address where the bill should be sent;
     */
    private String addr1;
    private String addr2;
    private String addr3;
    private String addr4;
    private String addr5;

    @Column(nullable = false, name = "sent_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar sentDate = Calendar.getInstance();

    @Column(name = "payment_time", nullable = false)
    private Integer paymentTime = 0;
    @Column(name = "notice_days", nullable = false)
    private String noticetime = "8 vrk";
    @Column(name = "their_reference", nullable = false)
    private String theirReference = "";
    @Column(name = "our_reference", nullable = false)
    private String ourReference = "";
    @Column(name = "delivery_terms", nullable = false)
    private String deliveryTerms = "";
    @Column(name = "delay_intrest", nullable = false)
    private Integer delayIntrest = 11;

    /**
     * Notes for the event organisators about the bill.
     */
    @Lob
    @Column(name = "notes")
    private String notes;

    /**
     * Bill may have multiple items on multiple rows.
     */

    @OrderBy("id")
    // @PrivateOwned
    @OneToMany(mappedBy = "bill", cascade = CascadeType.ALL)
    private List<BillLine> billLines;

    public boolean isPaid()
    {
        return accountEvent != null || paidDate != null;
    }

    /**
     * When the bill is paid this AccountEvent is created and this is a
     * reference to that accountAction. if this bill
     */
    @JoinColumn(name = "account_event_id", referencedColumnName = "id", updatable = false)
    @OneToOne
    private AccountEvent accountEvent;

    /**
     * User who should pay this bill.
     */
    @ManyToOne(optional = false)
    @JoinColumn(updatable = false, name = "eventuser_id")
    private EventUser user;

    private static final Logger logger = LoggerFactory.getLogger(Bill.class);

    public Integer getReferenceNumberBase() {
        if (getEvent() != null && getEvent().getReferenceNumberBase() != null && getBillNumber() != null) {
            return getEvent().getReferenceNumberBase() + getBillNumber();
        }
        return null;
    }

    public Integer getReferenceNumber()
    {
        return BillUtils.createReferenceNumber(getReferenceNumberBase());
    }

    /**
     * Commodity function to calculate the total price of the bill.
     * 
     * @return The total sum of the bill ( unitPrice * units * vat )
     */
    public BigDecimal totalPrice() {
        BigDecimal total = BigDecimal.ZERO;
        for (BillLine line : getBillLines()) {
            total = total.add(line.getLinePrice());
        }
        return total;
    }

    /**
     * Commodity function to calculate the total price of the bill.
     * 
     * @return The total sum of the bill ( unitPrice * units * vat )
     */
    public BigDecimal totalVat() {
        BigDecimal total = BigDecimal.ZERO;
        for (BillLine line : getBillLines()) {
            total = total.add(line.getLineVat());
        }
        return total;
    }

    /**
     * Commodity function to return the vatless price of the bill
     * 
     * @return The total VAT-less sum of the bill ( unitPrice * units )
     */
    public BigDecimal totalPriceVatless() {
        BigDecimal total = BigDecimal.ZERO;
        for (BillLine line : getBillLines()) {
            total = total.add(line.getLinePriceVatless());
        }
        return total;
    }

    public Bill(LanEvent event, EventUser user) {
        this(event);
        this.setUser(user);
        this.setAddr1(user.getUser().getFirstnames() + " " + user.getUser().getLastname());
        this.setAddr2(user.getUser().getAddress());
        this.setAddr3(user.getUser().getZip() + " " + user.getUser().getTown());
    }

    public Bill(LanEvent event) {
        this.event = event;
    }

    public Bill() {
        super();
    }

    public Calendar getDueDate() {

        Calendar dueDate = Calendar.getInstance();
        dueDate.setTime(this.getSentDate().getTime());
        dueDate.add(Calendar.DATE, this.getPaymentTime());
        return dueDate;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public List<BillLine> getBillLines() {
        return billLines;
    }

    public void setBillLines(List<BillLine> billLineList) {
        this.billLines = billLineList;
    }

    public AccountEvent getAccountEvent() {
        return accountEvent;
    }

    public void setAccountEvent(AccountEvent accoutEventsId) {
        this.accountEvent = accoutEventsId;
    }

    public EventUser getUser() {
        return user;
    }

    public void setUser(EventUser usersId) {
        this.user = usersId;
    }

    public Integer getBillNumber() {
        return billNumber;
    }

    public String getAddr1() {
        return addr1;
    }

    public void setAddr1(String addr1) {
        this.addr1 = addr1;
    }

    public String getAddr2() {
        return addr2;
    }

    public void setAddr2(String addr2) {
        this.addr2 = addr2;
    }

    public String getAddr3() {
        return addr3;
    }

    public void setAddr3(String addr3) {
        this.addr3 = addr3;
    }

    public String getAddr4() {
        return addr4;
    }

    public void setAddr4(String addr4) {
        this.addr4 = addr4;
    }

    public String getAddr5() {
        return addr5;
    }

    public void setAddr5(String addr5) {
        this.addr5 = addr5;
    }

    public String getNoticetime() {
        return noticetime;
    }

    public void setNoticetime(String noticetime) {
        this.noticetime = noticetime;
    }

    public String getOurReference() {
        return ourReference;
    }

    public void setOurReference(String ourReference) {
        this.ourReference = ourReference;
    }

    public void setTheirReference(String theirReference) {
        this.theirReference = theirReference;
    }

    public String getTheirReference() {
        return theirReference;
    }

    public void setDeliveryTerms(String deliveryTerms) {
        this.deliveryTerms = deliveryTerms;
    }

    public String getDeliveryTerms() {
        return deliveryTerms;
    }

    public void setPaymentTime(Integer paymentTime) {
        this.paymentTime = paymentTime;
    }

    public Integer getPaymentTime() {
        return paymentTime;
    }

    public void setDelayIntrest(Integer delayIntrest) {
        this.delayIntrest = delayIntrest;
    }

    public Integer getDelayIntrest() {
        return delayIntrest;
    }

    public void addProduct(Product product, BigDecimal count) {
        // If bill number > 0 bill has been sent and extra privileges are needed
        // to modify.
        // if (!iscurrent || billnr != null) {
        // permbean.fatalPermission(BillPermission.WRITE_ALL,
        // "User tried to modify bill ", bill,
        // "without sufficient permissions");
        // }
        if (this.billLines == null) {
            billLines = new ArrayList<BillLine>();
        }
        this.getBillLines().add(new BillLine(this, product, count));

        for (Discount disc : product.getActiveDiscounts(count, sentDate)) {

            this.getBillLines().add(new BillLine(this, product, disc, count));

        }
    }

    public void setBillNumber(Integer billNumber) {
        this.billNumber = billNumber;
    }

    public LanEvent getEvent() {
        return event;
    }

    public void setEvent(LanEvent event) {
        this.event = event;
    }

    public Date getPaidDate() {
        return paidDate;
    }

    public void setPaidDate(Date paidDate) {
        this.paidDate = paidDate;
    }

    public Date getSentDateTime()
    {
        Date ret = null;
        if (sentDate != null)
        {
            ret = sentDate.getTime();
        }
        return ret;
    }

    public void setSentDateTime(Date date)
    {
        if (date == null)
        {
            sentDate = null;
        } else {
            if (sentDate == null)
            {
                sentDate = Calendar.getInstance();
            }
            sentDate.setTime(date);
        }
    }

    public Calendar getSentDate() {
        return sentDate;
    }

    public void setSentDate(Calendar sentDate) {
        this.sentDate = sentDate;
    }

}