001/* Copyright (C) 2014 konik.io 002 * 003 * This file is part of the Konik library. 004 * 005 * The Konik library is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * The Konik library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with the Konik library. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package io.konik.calculation; 019 020import io.konik.util.Invoices; 021import io.konik.zugferd.Invoice; 022 023import javax.validation.constraints.NotNull; 024import java.util.List; 025import java.util.concurrent.CopyOnWriteArrayList; 026 027/** 028 * Completes the {@link Invoice} and calculates with specified {@link Correction}s or additions. 029 */ 030public final class InvoiceCalculator { 031 032 private final Invoice invoice; 033 private final List<Correction<Invoice>> corrections = new CopyOnWriteArrayList<Correction<Invoice>>(); 034 035 /** 036 * Instantiates a new invoice calculator. 037 * 038 * @param invoice the invoice 039 */ 040 public InvoiceCalculator(@NotNull final Invoice invoice) { 041 this.invoice = invoice; 042 corrections.add(new InvoiceMonetarySummationAndTradeTaxCompleter()); 043 corrections.add(new ItemSpecifiedMonetarySummationCompleter()); 044 } 045 046 /** 047 * Registers {@link Correction} to apply on the {@link Invoice}. 048 * 049 * @param correction the correction 050 */ 051 public void applyCorrection(final Correction<Invoice> correction) { 052 this.corrections.add(correction); 053 } 054 055 /** 056 * Resets calculator and removes all applied {@link Correction}s. 057 */ 058 public void reset() { 059 this.corrections.clear(); 060 } 061 062 /** 063 * Runs all registered {@link Correction}s on the {@link Invoice} 064 * @return 065 */ 066 public Invoice complete() { 067 Invoice correctedInvoice = Invoices.clone(invoice); 068 for (Correction<Invoice> correction : corrections) { 069 correctedInvoice = correction.correct(correctedInvoice); 070 } 071 return correctedInvoice; 072 } 073}