diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index f6c78e0ac11c..4ee9d2b0c24d 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -66,6 +66,8 @@ class CompanySettings extends BaseSettings public $custom_invoice_taxes1 = false; public $custom_invoice_taxes2 = false; + public $custom_invoice_taxes3 = false; + public $custom_invoice_taxes4 = false; public $default_task_rate = 0; public $send_reminders = false; @@ -233,6 +235,8 @@ class CompanySettings extends BaseSettings // 'custom_expense_label4' => 'string', 'custom_invoice_taxes1' => 'bool', 'custom_invoice_taxes2' => 'bool', + 'custom_invoice_taxes3' => 'bool', + 'custom_invoice_taxes4' => 'bool', 'default_task_rate' => 'float', 'send_reminders' => 'bool', 'show_tasks_in_portal' => 'bool', diff --git a/app/Helpers/Invoice/Balancer.php b/app/Helpers/Invoice/Balancer.php new file mode 100644 index 000000000000..e0d5deed8b08 --- /dev/null +++ b/app/Helpers/Invoice/Balancer.php @@ -0,0 +1,32 @@ +invoice->id) && $this->invoice->id >= 1) + { + return round($total - ($this->invoice->amount - $this->invoice->balance), 2); + } + + return $total; + + } + +} diff --git a/app/Helpers/Invoice/CustomValuer.php b/app/Helpers/Invoice/CustomValuer.php new file mode 100644 index 000000000000..5903e6c7c190 --- /dev/null +++ b/app/Helpers/Invoice/CustomValuer.php @@ -0,0 +1,38 @@ +invoice->tax_rate1/100) ,2) + round($custom_value * ($this->invoice->tax_rate2/100) ,2) + round($custom_value * ($this->invoice->tax_rate3/100) ,2); + + return 0; + } + +} + diff --git a/app/Helpers/Invoice/Discounter.php b/app/Helpers/Invoice/Discounter.php index 8110b3bdc12b..42f9216d0457 100644 --- a/app/Helpers/Invoice/Discounter.php +++ b/app/Helpers/Invoice/Discounter.php @@ -17,15 +17,20 @@ namespace App\Helpers\Invoice; trait Discounter { - public function discount($amount, $discount, $is_amount_discount) + public function discount($amount) { - if($is_amount_discount){ - return $discount; - } - else { - return round($amount * ($discount / 100), 2); - } + if($this->invoice->is_amount_discount === true) + return $this->invoice->discount; + + + return round($amount * ($this->invoice->discount / 100), 2); + } + // public function pro_rata_discount($amount) + // { + // return round(($this->invoice->discount/$this->getSubTotal() * $amount),2); + // } + } diff --git a/app/Helpers/Invoice/InvoiceItemCalc.php b/app/Helpers/Invoice/InvoiceItemCalc.php index 3a6757273bb6..0eb33a981c02 100644 --- a/app/Helpers/Invoice/InvoiceItemCalc.php +++ b/app/Helpers/Invoice/InvoiceItemCalc.php @@ -115,9 +115,24 @@ class InvoiceItemCalc $this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total); + } + + if(isset($this->item->tax_rate3) && $this->item->tax_rate3 > 0) + { + $tax_rate3 = $this->formatValue($this->item->tax_rate3, $this->currency->precision); + + if($this->settings->inclusive_taxes) + $item_tax_rate3_total = $this->formatValue(($this->getLineTotal() - ($this->getLineTotal() / (1+$tax_rate3/100))) , $this->currency->precision); + else + $item_tax_rate3_total = $this->formatValue(($this->getLineTotal() * $tax_rate3/100), $this->currency->precision); + + $item_tax += $item_tax_rate3_total; + + $this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total); } + $this->setTotalTaxes($item_tax); return $this; diff --git a/app/Helpers/Invoice/InvoiceItemSum.php b/app/Helpers/Invoice/InvoiceItemSum.php new file mode 100644 index 000000000000..846175e0d670 --- /dev/null +++ b/app/Helpers/Invoice/InvoiceItemSum.php @@ -0,0 +1,237 @@ +settings = $settings; + + $this->tax_collection = collect([]); + + $this->invoice = $invoice; + + $this->currency = $invoice->client->currency(); + + $this->line_items = []; + } + + public function process() + { + if(!$this->invoice->line_items || count($this->invoice->line_items) == 0){ + $this->items = []; + return $this; + } + + $this->buildLineItems(); + + return $this; + } + + private function buildLineItems() + { + foreach($this->invoice->line_items as $this->item) + { + $this->sumLineItem() + ->setDiscount() + ->calcTaxes() + ->push(); + } + + return $this; + } + + private function push() + { + + $this->sub_total += $this->getLineTotal(); + + $this->line_items[] = $this->item; + + return $this; + } + + private function sumLineItem() + { + $this->setLineTotal($this->formatValue($this->item->cost, $this->currency->precision) * $this->formatValue($this->item->quantity, $this->currency->precision)); + return $this; + } + + private function setDiscount() + { + + if($this->item->is_amount_discount) + { + $this->setLineTotal($this->getLineTotal() - $this->formatValue($this->item->discount, $this->currency->precision)); + } + else + { + $this->setLineTotal($this->getLineTotal() - $this->formatValue(round($this->item->line_total * ($this->item->discount / 100),2), $this->currency->precision)); + } + + return $this; + + } + + private function calcTaxes() + { + $item_tax = 0; + + if(isset($this->item->tax_rate1) && $this->item->tax_rate1 > 0) + { + $tax_rate1 = $this->formatValue($this->item->tax_rate1, $this->currency->precision); + + if($this->settings->inclusive_taxes) + $item_tax_rate1_total = $this->formatValue(($this->item->line_total - ($this->item->line_total / (1+$tax_rate1/100))) , $this->currency->precision); + else + $item_tax_rate1_total = $this->formatValue(($this->item->line_total * $tax_rate1/100), $this->currency->precision); + + $item_tax += $item_tax_rate1_total; + + $this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total); + } + + if(isset($this->item->tax_rate2) && $this->item->tax_rate2 > 0) + { + $tax_rate2 = $this->formatValue($this->item->tax_rate2, $this->currency->precision); + + if($this->settings->inclusive_taxes) + $item_tax_rate2_total = $this->formatValue(($this->item->line_total - ($this->item->line_total / (1+$tax_rate2/100))) , $this->currency->precision); + else + $item_tax_rate2_total = $this->formatValue(($this->item->line_total * $tax_rate2/100), $this->currency->precision); + + $item_tax += $item_tax_rate2_total; + + $this->groupTax($this->item->tax_name2, $this->item->tax_rate2, $item_tax_rate2_total); + + } + + if(isset($this->item->tax_rate3) && $this->item->tax_rate3 > 0) + { + $tax_rate3 = $this->formatValue($this->item->tax_rate3, $this->currency->precision); + + if($this->settings->inclusive_taxes) + $item_tax_rate3_total = $this->formatValue(($this->item->line_total - ($this->item->line_total / (1+$tax_rate3/100))) , $this->currency->precision); + else + $item_tax_rate3_total = $this->formatValue(($this->item->line_total * $tax_rate3/100), $this->currency->precision); + + $item_tax += $item_tax_rate3_total; + + $this->groupTax($this->item->tax_name3, $this->item->tax_rate3, $item_tax_rate3_total); + + } + + //todo if exclusive add on top, if inclusive need to reduce item rates + + $this->setTotalTaxes($item_tax); + + return $this; + } + + private function groupTax($tax_name, $tax_rate, $tax_total) + { + $group_tax = []; + + $key = str_replace(" ", "", $tax_name.$tax_rate); + + $group_tax = ['key' => $key, 'total' => $tax_total, 'tax_name' => $tax_name . ' ' . $tax_rate.'%']; + + $this->tax_collection->push(collect($group_tax)); + + } + + public function getTotalTaxes() + { + return $this->total_taxes; + } + + public function setTotalTaxes($total) + { + $this->total_taxes = $total; + + return $this; + } + + public function setLineTotal($total) + { + $this->item->line_total = $total; + + return $this; + } + + public function getLineTotal() + { + return $this->item->line_total; + } + + public function getLineItems() + { + return $this->line_items; + } + + public function getGroupedTaxes() + { + return $this->tax_collection; + } + + public function setGroupedTaxes($group_taxes) + { + $this->tax_collection = $group_taxes; + + return $this; + } + + public function getSubTotal() + { + return $this->sub_total; + } + + public function setSubTotal($value) + { + $this->sub_total = $value; + return $this; + } + +} \ No newline at end of file diff --git a/app/Helpers/Invoice/InvoiceSum.php b/app/Helpers/Invoice/InvoiceSum.php new file mode 100644 index 000000000000..841b5071c38f --- /dev/null +++ b/app/Helpers/Invoice/InvoiceSum.php @@ -0,0 +1,271 @@ +invoice = $invoice; + + $this->settings = $settings; + + $this->tax_map = new Collection; + + } + + public function build() + { + $this->calculateLineItems() + ->calculateDiscount() + ->calculateCustomValues() + ->calculateInvoiceTaxes() + ->setTaxMap() + ->calculateTotals() + ->calculateBalance() + ->calculatePartial(); + + return $this; + } + + private function calculateLineItems() + { + $this->invoice_items = new InvoiceItemSum($this->invoice, $this->settings); + $this->invoice_items->process(); + $this->invoice->line_items = $this->invoice_items->getLineItems(); + $this->total = $this->invoice_items->getSubTotal(); + + return $this; + } + + private function calculateDiscount() + { + $this->total_discount = $this->discount($this->invoice_items->getSubTotal()); + + $this->total -= $this->total_discount; + + return $this; + } + + private function calculateCustomValues() + { + $this->total_taxes += $this->valuerTax($this->invoice->custom_value1, $this->settings->custom_invoice_taxes1); + $this->total_custom_values += $this->valuer($this->invoice->custom_value1); + + $this->total_taxes += $this->valuerTax($this->invoice->custom_value2, $this->settings->custom_invoice_taxes2); + $this->total_custom_values += $this->valuer($this->invoice->custom_value2); + + $this->total_taxes += $this->valuerTax($this->invoice->custom_value3, $this->settings->custom_invoice_taxes3); + $this->total_custom_values += $this->valuer($this->invoice->custom_value3); + + $this->total_taxes += $this->valuerTax($this->invoice->custom_value4, $this->settings->custom_invoice_taxes4); + $this->total_custom_values += $this->valuer($this->invoice->custom_value4); + + $this->total += $this->total_custom_values; + + return $this; + } + + private function calculateInvoiceTaxes() + { + + if($this->invoice->tax_rate1 > 0){ + $tax = $this->taxer($this->total, $this->invoice->tax_rate1); + $this->total_taxes += $tax; + $this->total_tax_map[] = ['name' => $this->invoice->tax_name1 . ' ' . $this->invoice->tax_rate1.'%', 'total' => $tax]; + } + + if($this->invoice->tax_rate2 > 0){ + $tax = $this->taxer($this->total, $this->invoice->tax_rate2); + $this->total_taxes += $tax; + $this->total_tax_map[] = ['name' => $this->invoice->tax_name2. ' ' . $this->invoice->tax_rate2.'%', 'total' => $tax]; + } + + if($this->invoice->tax_rate3 > 0){ + $tax = $this->taxer($this->total, $this->invoice->tax_rate3); + $this->total_taxes += $tax; + $this->total_tax_map[] = ['name' => $this->invoice->tax_name3 . ' ' . $this->invoice->tax_rate3.'%', 'total' => $tax]; + } + + return $this; + } + + /** + * Calculates the balance. + * + * @return self The balance. + */ + private function calculateBalance() + { + //$this->invoice->balance = $this->balance($this->getTotal(), $this->invoice); + $this->setCalculatedAttributes(); + + return $this; + } + + private function calculatePartial() + { + if ( !isset($this->invoice->id) && isset($this->invoice->partial) ) { + $this->invoice->partial = max(0, min($this->formatValue($this->invoice->partial, 2), $this->invoice->balance)); + } + + return $this; + } + + private function calculateTotals() + { + + if($this->settings->inclusive_taxes === false) + $this->total += $this->total_taxes; + + return $this; + + } + + public function getInvoice() + { + //Build invoice values here and return Invoice + $this->setCalculatedAttributes(); + + return $this->invoice; + } + + + /** + * Build $this->invoice variables after + * calculations have been performed. + */ + private function setCalculatedAttributes() + { + /* If amount != balance then some money has been paid on the invoice, need to subtract this difference from the total to set the new balance */ + if($this->invoice->amount != $this->invoice->balance) + { + $paid_to_date = $this->invoice->amount - $this->invoice->balance; + + $this->invoice->balance = $this->getTotal() - $paid_to_date; + } + else + $this->invoice->balance = $this->getTotal(); + + /* Set new calculated total */ + $this->invoice->amount = $this->getTotal(); + + return $this; + } + + public function getSubTotal() + { + return $this->invoice_items->getSubTotal(); + } + + public function getTotalDiscount() + { + return $this->total_discount; + } + + public function getTotalTaxes() + { + return $this->total_taxes; + } + + public function getTotalTaxMap() + { + return $this->total_tax_map; + } + + public function getTotal() + { + return $this->total; + } + + public function setTaxMap() + { + $this->tax_map = collect(); + + $keys = $this->invoice_items->getGroupedTaxes()->pluck('key')->unique(); + + $values = $this->invoice_items->getGroupedTaxes(); + + foreach($keys as $key) + { + + $tax_name = $values->filter(function ($value, $k) use($key){ + return $value['key'] == $key; + })->pluck('tax_name')->first(); + + $total_line_tax = $values->filter(function ($value, $k) use($key){ + return $value['key'] == $key; + })->sum('total'); + + $total_line_tax -= $this->discount($total_line_tax); + + $this->tax_map[] = ['name' => $tax_name, 'total' => $total_line_tax]; + + $this->total_taxes += $total_line_tax; + } + + return $this; + + } + + public function getTaxMap() + { + return $this->tax_map; + } + + public function getBalance() + { + return $this->invoice->balance; + } + + public function getItemTotalTaxes() + { + return $this->getTotalTaxes(); + } +} \ No newline at end of file diff --git a/app/Helpers/Invoice/Taxer.php b/app/Helpers/Invoice/Taxer.php new file mode 100644 index 000000000000..d353230b7b3a --- /dev/null +++ b/app/Helpers/Invoice/Taxer.php @@ -0,0 +1,25 @@ +settings); + $invoice_calc = new InvoiceSum($this, $this->settings); return $invoice_calc->build(); diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index e9e0f387c5ec..1286dab5ce83 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -14,7 +14,7 @@ namespace App\Repositories; use App\Events\Invoice\InvoiceWasCreated; use App\Events\Invoice\InvoiceWasUpdated; use App\Factory\InvoiceInvitationFactory; -use App\Helpers\Invoice\InvoiceCalc; +use App\Helpers\Invoice\InvoiceSum; use App\Jobs\Company\UpdateCompanyLedgerWithInvoice; use App\Listeners\Invoice\CreateInvoiceInvitation; use App\Models\ClientContact; @@ -46,9 +46,9 @@ class InvoiceRepository extends BaseRepository * Saves the invoices * * @param array. $data The invoice data - * @param InvoiceCalc|\App\Models\Invoice $invoice The invoice + * @param InvoiceSum|\App\Models\Invoice $invoice The invoice * - * @return Invoice|InvoiceCalc|\App\Models\Invoice|null Returns the invoice object + * @return Invoice|InvoiceSum|\App\Models\Invoice|null Returns the invoice object */ public function save($data, Invoice $invoice) : ?Invoice { @@ -75,7 +75,7 @@ class InvoiceRepository extends BaseRepository event(new CreateInvoiceInvitation($invoice)); - $invoice_calc = new InvoiceCalc($invoice, $invoice->settings); + $invoice_calc = new InvoiceSum($invoice, $invoice->settings); $invoice = $invoice_calc->build()->getInvoice(); diff --git a/app/Repositories/QuoteRepository.php b/app/Repositories/QuoteRepository.php index 2b4bb61de174..4bbcfc642b22 100644 --- a/app/Repositories/QuoteRepository.php +++ b/app/Repositories/QuoteRepository.php @@ -11,7 +11,7 @@ namespace App\Repositories; -use App\Helpers\Invoice\InvoiceCalc; +use App\Helpers\Invoice\InvoiceSum; use App\Models\Quote; use Illuminate\Http\Request; @@ -34,7 +34,7 @@ class QuoteRepository extends BaseRepository $quote->save(); - $invoice_calc = new InvoiceCalc($quote, $quote->settings); + $invoice_calc = new InvoiceSum($quote, $quote->settings); $quote = $invoice_calc->build()->getInvoice(); diff --git a/app/Repositories/RecurringInvoiceRepository.php b/app/Repositories/RecurringInvoiceRepository.php index d192fd744fd1..b164132ff16d 100644 --- a/app/Repositories/RecurringInvoiceRepository.php +++ b/app/Repositories/RecurringInvoiceRepository.php @@ -11,7 +11,7 @@ namespace App\Repositories; -use App\Helpers\Invoice\InvoiceCalc; +use App\Helpers\Invoice\InvoiceSum; use App\Models\RecurringInvoice; use Illuminate\Http\Request; @@ -33,7 +33,7 @@ class RecurringInvoiceRepository extends BaseRepository $invoice->save(); - $invoice_calc = new InvoiceCalc($invoice, $invoice->settings); + $invoice_calc = new InvoiceSum($invoice, $invoice->settings); $invoice = $invoice_calc->build()->getInvoice(); diff --git a/app/Repositories/RecurringQuoteRepository.php b/app/Repositories/RecurringQuoteRepository.php index 26dd047c2213..0e655b37a8ed 100644 --- a/app/Repositories/RecurringQuoteRepository.php +++ b/app/Repositories/RecurringQuoteRepository.php @@ -11,7 +11,7 @@ namespace App\Repositories; -use App\Helpers\Invoice\InvoiceCalc; +use App\Helpers\Invoice\InvoiceSum; use App\Models\RecurringQuote; use Illuminate\Http\Request; @@ -34,7 +34,7 @@ class RecurringQuoteRepository extends BaseRepository $quote->save(); - $quote_calc = new InvoiceCalc($quote, $quote->settings); + $quote_calc = new InvoiceSum($quote, $quote->settings); $quote = $quote_calc->build()->getInvoice(); diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index 24055a6038c0..9be9915a79c7 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -428,6 +428,9 @@ trait MakesInvoiceValues $data = ''; + if(count($this->calc()->getTotalTaxMap()) == 0) + return $data; + foreach($total_tax_map as $tax) { $data .= '