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' )
-> join ( 'invoice_statuses' , 'invoice_statuses.id' , '=' , 'invoices.invoice_status_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 )
-> where ( 'invoices.deleted_at' , '=' , null )
-> where ( 'clients.deleted_at' , '=' , null )
2013-12-30 21:17:45 +01:00
-> where ( 'invoices.is_recurring' , '=' , false )
-> where ( 'contacts.is_primary' , '=' , true )
-> 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' , 'invoices.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 );
}
if ( $filter )
{
$query -> where ( function ( $query ) use ( $filter )
{
$query -> where ( 'clients.name' , 'like' , '%' . $filter . '%' )
-> orWhere ( 'invoices.invoice_number' , 'like' , '%' . $filter . '%' )
-> orWhere ( 'invoice_statuses.name' , 'like' , '%' . $filter . '%' );
});
}
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 )
-> where ( 'invoices.deleted_at' , '=' , null )
-> where ( 'invoices.is_recurring' , '=' , true )
2013-12-30 21:17:45 +01:00
-> where ( 'contacts.is_primary' , '=' , true )
-> 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' , 'invoices.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 );
}
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 ;
$rules = [ 'invoice_number' => 'unique:invoices,invoice_number,' . $invoiceId . ',id,account_id,' . \Auth :: user () -> account_id ];
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' ];
2013-12-31 20:49:54 +01:00
$invoice -> discount = floatval ( $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' ]);
2013-12-29 18:40:11 +01:00
$invoice -> currency_id = $data [ 'currency_id' ];
2014-01-01 00:50:13 +01:00
2014-01-01 16:23:32 +01:00
if ( isset ( $data [ 'tax_rate' ]) && floatval ( $data [ 'tax_rate' ]) > 0 )
2013-12-25 22:34:42 +01:00
{
2014-01-01 16:23:32 +01:00
$invoice -> tax_rate = floatval ( $data [ 'tax_rate' ]);
$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-25 22:34:42 +01:00
$invoice -> save ();
2013-12-31 17:38:49 +01:00
$invoice -> invoice_items () -> forceDelete ();
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 ;
}
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 = floatval ( $item -> cost );
$invoiceItem -> qty = floatval ( $item -> qty );
2013-12-31 20:49:54 +01:00
$invoiceItem -> tax_rate = 0 ;
2013-12-25 22:34:42 +01:00
2014-01-01 16:23:32 +01:00
if ( isset ( $item -> tax_rate ) && floatval ( $item -> tax_rate ) > 0 )
2013-12-29 00:33:48 +01:00
{
2014-01-01 16:23:32 +01:00
$invoiceItem -> tax_rate = floatval ( $item -> tax_rate );
$invoiceItem -> tax_name = trim ( $item -> tax_name );
2013-12-29 00:33:48 +01:00
}
2013-12-25 22:34:42 +01:00
$invoice -> invoice_items () -> save ( $invoiceItem );
2013-12-31 20:49:54 +01:00
$lineTotal = $invoiceItem -> cost * $invoiceItem -> qty ;
$total += $lineTotal + ( $lineTotal * $invoiceItem -> tax_rate / 100 );
}
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 ();
2013-12-25 22:34:42 +01:00
return $invoice ;
}
}