1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/ninja/repositories/InvoiceRepository.php

261 lines
7.7 KiB
PHP
Raw Normal View History

2013-12-25 22:34:42 +01:00
<?php namespace ninja\repositories;
use Invoice;
use InvoiceItem;
use Product;
use Utils;
2013-12-29 00:33:48 +01:00
use TaxRate;
2013-12-25 22:34:42 +01:00
class InvoiceRepository
{
public function getInvoices($accountId, $clientPublicId = false, $filter = false)
{
$query = \DB::table('invoices')
->join('clients', 'clients.id', '=','invoices.client_id')
2014-02-16 21:32:25 +01:00
->join('invoice_statuses', 'invoice_statuses.id', '=', 'invoices.invoice_status_id')
->join('contacts', 'contacts.client_id', '=', 'clients.id')
->where('invoices.account_id', '=', $accountId)
2013-12-25 22:34:42 +01:00
->where('clients.deleted_at', '=', null)
2013-12-30 21:17:45 +01:00
->where('invoices.is_recurring', '=', false)
->where('contacts.is_primary', '=', true)
2014-02-16 21:32:25 +01:00
->select('clients.public_id as client_public_id', 'invoice_number', 'clients.name as client_name', 'invoices.public_id', 'amount', 'invoices.balance', 'invoice_date', 'due_date', 'invoice_statuses.name as invoice_status_name', 'clients.currency_id', 'contacts.first_name', 'contacts.last_name', 'contacts.email');
2013-12-25 22:34:42 +01:00
2014-02-19 20:59:46 +01:00
if (!\Session::get('show_trash'))
2014-02-18 22:56:18 +01:00
{
$query->where('invoices.deleted_at', '=', null);
}
2013-12-25 22:34:42 +01:00
if ($clientPublicId)
{
$query->where('clients.public_id', '=', $clientPublicId);
}
if ($filter)
{
$query->where(function($query) use ($filter)
{
$query->where('clients.name', 'like', '%'.$filter.'%')
->orWhere('invoices.invoice_number', 'like', '%'.$filter.'%')
2014-02-26 17:08:40 +01:00
->orWhere('invoice_statuses.name', 'like', '%'.$filter.'%')
->orWhere('contacts.first_name', 'like', '%'.$filter.'%')
->orWhere('contacts.last_name', 'like', '%'.$filter.'%')
->orWhere('contacts.email', 'like', '%'.$filter.'%');
2013-12-25 22:34:42 +01:00
});
}
return $query;
}
public function getRecurringInvoices($accountId, $clientPublicId = false, $filter = false)
{
$query = \DB::table('invoices')
->join('clients', 'clients.id', '=','invoices.client_id')
->join('frequencies', 'frequencies.id', '=', 'invoices.frequency_id')
2013-12-30 21:17:45 +01:00
->join('contacts', 'contacts.client_id', '=', 'clients.id')
2013-12-25 22:34:42 +01:00
->where('invoices.account_id', '=', $accountId)
2014-02-18 22:56:18 +01:00
->where('clients.deleted_at', '=', null)
2013-12-25 22:34:42 +01:00
->where('invoices.is_recurring', '=', true)
2013-12-30 21:17:45 +01:00
->where('contacts.is_primary', '=', true)
2014-01-15 15:01:24 +01:00
->select('clients.public_id as client_public_id', 'clients.name as client_name', 'invoices.public_id', 'amount', 'frequencies.name as frequency', 'start_date', 'end_date', 'clients.currency_id', 'contacts.first_name', 'contacts.last_name', 'contacts.email');
2013-12-25 22:34:42 +01:00
if ($clientPublicId)
{
$query->where('clients.public_id', '=', $clientPublicId);
}
2014-02-19 20:59:46 +01:00
if (!\Session::get('show_trash'))
2014-02-18 22:56:18 +01:00
{
$query->where('invoices.deleted_at', '=', null);
}
2013-12-25 22:34:42 +01:00
if ($filter)
{
$query->where(function($query) use ($filter)
{
$query->where('clients.name', 'like', '%'.$filter.'%')
->orWhere('invoices.invoice_number', 'like', '%'.$filter.'%');
});
}
return $query;
}
2014-01-01 16:23:32 +01:00
public function getErrors($input)
{
$contact = (array) $input->client->contacts[0];
$rules = ['email' => 'required|email'];
$validator = \Validator::make($contact, $rules);
if ($validator->fails())
{
return $validator;
}
$invoice = (array) $input;
2014-01-02 18:16:00 +01:00
$invoiceId = isset($invoice['public_id']) && $invoice['public_id'] ? Invoice::getPrivateId($invoice['public_id']) : null;
2014-03-05 21:19:12 +01:00
$rules = ['invoice_number' => 'required|unique:invoices,invoice_number,' . $invoiceId . ',id,account_id,' . \Auth::user()->account_id];
2014-01-14 12:52:56 +01:00
if ($invoice['is_recurring'] && $invoice['start_date'] && $invoice['end_date'])
{
$rules['end_date'] = 'after:' . $invoice['start_date'];
}
2014-01-01 16:23:32 +01:00
$validator = \Validator::make($invoice, $rules);
if ($validator->fails())
{
return $validator;
}
return false;
}
2013-12-25 22:34:42 +01:00
public function save($publicId, $data)
{
if ($publicId)
{
$invoice = Invoice::scope($publicId)->firstOrFail();
}
else
{
$invoice = Invoice::createNew();
}
$invoice->client_id = $data['client_id'];
2014-01-14 12:52:56 +01:00
$invoice->discount = Utils::parseFloat($data['discount']);
2013-12-25 22:34:42 +01:00
$invoice->invoice_number = trim($data['invoice_number']);
$invoice->invoice_date = Utils::toSqlDate($data['invoice_date']);
$invoice->due_date = Utils::toSqlDate($data['due_date']);
$invoice->is_recurring = $data['is_recurring'];
$invoice->frequency_id = $data['frequency_id'] ? $data['frequency_id'] : 0;
$invoice->start_date = Utils::toSqlDate($data['start_date']);
$invoice->end_date = Utils::toSqlDate($data['end_date']);
$invoice->terms = trim($data['terms']);
2013-12-30 21:17:45 +01:00
$invoice->public_notes = trim($data['public_notes']);
2013-12-25 22:34:42 +01:00
$invoice->po_number = trim($data['po_number']);
$invoice->invoice_design_id = $data['invoice_design_id'];
2014-01-14 12:52:56 +01:00
if (isset($data['tax_rate']) && Utils::parseFloat($data['tax_rate']) > 0)
2013-12-25 22:34:42 +01:00
{
2014-01-14 12:52:56 +01:00
$invoice->tax_rate = Utils::parseFloat($data['tax_rate']);
2014-01-01 16:23:32 +01:00
$invoice->tax_name = trim($data['tax_name']);
2014-01-01 00:50:13 +01:00
}
else
{
$invoice->tax_rate = 0;
$invoice->tax_name = '';
2013-12-25 22:34:42 +01:00
}
2014-01-01 00:50:13 +01:00
2013-12-31 20:49:54 +01:00
$total = 0;
2013-12-25 22:34:42 +01:00
foreach ($data['invoice_items'] as $item)
{
if (!$item->cost && !$item->qty && !$item->product_key && !$item->notes)
{
continue;
}
2014-02-01 21:01:32 +01:00
$invoiceItemCost = Utils::parseFloat($item->cost);
$invoiceItemQty = Utils::parseFloat($item->qty);
$invoiceItemTaxRate = 0;
2013-12-25 22:34:42 +01:00
2014-01-14 12:52:56 +01:00
if (isset($item->tax_rate) && Utils::parseFloat($item->tax_rate) > 0)
2013-12-29 00:33:48 +01:00
{
2014-02-01 21:01:32 +01:00
$invoiceItemTaxRate = Utils::parseFloat($item->tax_rate);
2013-12-29 00:33:48 +01:00
}
2014-02-01 21:01:32 +01:00
$lineTotal = $invoiceItemCost * $invoiceItemQty;
$total += $lineTotal + ($lineTotal * $invoiceItemTaxRate / 100);
2013-12-31 20:49:54 +01:00
}
if ($invoice->discount > 0)
{
$total *= (100 - $invoice->discount) / 100;
2013-12-25 22:34:42 +01:00
}
2013-12-31 20:49:54 +01:00
$total += $total * $invoice->tax_rate / 100;
$invoice->amount = $total;
$invoice->balance = $total;
$invoice->save();
2014-02-01 21:01:32 +01:00
$invoice->invoice_items()->forceDelete();
foreach ($data['invoice_items'] as $item)
{
if (!$item->cost && !$item->qty && !$item->product_key && !$item->notes)
{
continue;
}
if ($item->product_key)
{
$product = Product::findProductByKey(trim($item->product_key));
if (!$product)
{
$product = Product::createNew();
$product->product_key = trim($item->product_key);
}
/*
$product->notes = $item->notes;
$product->cost = $item->cost;
$product->qty = $item->qty;
*/
$product->save();
}
$invoiceItem = InvoiceItem::createNew();
$invoiceItem->product_id = isset($product) ? $product->id : null;
$invoiceItem->product_key = trim($item->product_key);
$invoiceItem->notes = trim($item->notes);
$invoiceItem->cost = Utils::parseFloat($item->cost);
$invoiceItem->qty = Utils::parseFloat($item->qty);
$invoiceItem->tax_rate = 0;
if (isset($item->tax_rate) && Utils::parseFloat($item->tax_rate) > 0)
{
$invoiceItem->tax_rate = Utils::parseFloat($item->tax_rate);
$invoiceItem->tax_name = trim($item->tax_name);
}
$invoice->invoice_items()->save($invoiceItem);
}
2014-01-08 21:09:47 +01:00
if ($data['set_default_terms'])
{
$account = \Auth::user()->account;
$account->invoice_terms = $invoice->terms;
$account->save();
}
2013-12-25 22:34:42 +01:00
return $invoice;
}
2014-01-12 19:55:33 +01:00
public function bulk($ids, $action)
{
if (!$ids)
{
return 0;
}
$invoices = Invoice::scope($ids)->get();
foreach ($invoices as $invoice)
{
if ($action == 'delete')
{
$invoice->is_deleted = true;
$invoice->save();
}
$invoice->delete();
}
return count($invoices);
}
2013-12-25 22:34:42 +01:00
}