' . __('app.total') . ': ' . currency_format($row->total, $currencyId) . '
' . __('app.paid') . ': ' . currency_format($row->amountPaid(), $currencyId) . '
' . __('app.unpaid') . ': ' . currency_format($row->amountDue(), $currencyId) . '
';
});
$datatables->editColumn(
'issue_date',
function ($row) {
return $row->issue_date->timezone($this->company->timezone)->translatedFormat($this->company->date_format);
}
);
$datatables->orderColumn('short_code', 'invoice_number $1');
$datatables->removeColumn('currency_symbol');
$datatables->removeColumn('currency_code');
$datatables->removeColumn('project_id');
// Custom Fields For export
$customFieldColumns = CustomField::customFieldData($datatables, Invoice::CUSTOM_FIELD_MODEL);
$datatables->rawColumns(array_merge(['project_name', 'action', 'status', 'invoice_number', 'total', 'name'], $customFieldColumns));
return $datatables;
}
/**
* @return \Illuminate\Database\Eloquent\Builder
*/
public function query()
{
$request = $this->request();
$this->firstInvoice = Invoice::orderBy('id', 'desc')->first();
$model = Invoice::with(
[
'project' => function ($q) {
$q->withTrashed();
$q->select('id', 'project_name', 'project_short_code', 'client_id');
},
'currency:id,currency_symbol,currency_code', 'project.client', 'client', 'payment', 'estimate', 'project.clientdetails'
]
)
->with('client', 'client.session', 'client.clientdetails', 'payment', 'clientdetails')
->select([
'invoices.id', 'invoices.due_amount', 'invoices.project_id', 'invoices.client_id', 'invoices.invoice_number',
'invoices.currency_id', 'invoices.total', 'invoices.status', 'invoices.issue_date', 'invoices.credit_note',
'invoices.show_shipping_address', 'invoices.send_status', 'invoices.invoice_recurring_id',
'invoices.added_by', 'invoices.hash', 'invoices.custom_invoice_number'
])
->addSelect('invoices.company_id'); // Company_id is fetched so the we have fetch company relation with it)
if ($request->startDate !== null && $request->startDate != 'null' && $request->startDate != '') {
$startDate = Carbon::createFromFormat($this->company->date_format, $request->startDate)->toDateString();
$model = $model->where(DB::raw('DATE(invoices.`issue_date`)'), '>=', $startDate);
}
if ($request->endDate !== null && $request->endDate != 'null' && $request->endDate != '') {
$endDate = Carbon::createFromFormat($this->company->date_format, $request->endDate)->toDateString();
$model = $model->where(DB::raw('DATE(invoices.`issue_date`)'), '<=', $endDate);
}
if ($request->status != 'all' && !is_null($request->status)) {
if ($request->status == 'pending') {
$model = $model->where(function ($q) {
$q->where('invoices.status', '=', 'unpaid');
$q->orWhere('invoices.status', '=', 'partial');
});
}
else {
$model = $model->where('invoices.status', '=', $request->status);
}
$model = $model->where('invoices.credit_note', 0);
}
if (request('amount') == 'pending') {
$model = $model->where(function ($query) {
$query->where('invoices.status', 'unpaid')
->orWhere('invoices.status', 'partial');
});
}
if ($request->projectID != 'all' && !is_null($request->projectID)) {
$model = $model->where('invoices.project_id', '=', $request->projectID);
}
if ($request->clientID != 'all' && !is_null($request->clientID)) {
$model = $model->where('invoices.client_id', '=', $request->clientID);
}
if ($request->searchText != '') {
$model->where(function ($query) {
$query->where('invoices.invoice_number', 'like', '%' . request('searchText') . '%')
->orWhere('invoices.custom_invoice_number', 'like', '%' . request('searchText') . '%')
->orWhere('invoices.id', 'like', '%' . request('searchText') . '%')
->orWhere('invoices.total', 'like', '%' . request('searchText') . '%')
->orWhere(function ($query) {
$query->whereHas('client', function ($q) {
$q->where('name', 'like', '%' . request('searchText') . '%');
});
})
->orWhere(function ($query) {
$query->whereHas('project', function ($q) {
$q->where('project_name', 'like', '%' . request('searchText') . '%')
->orWhere('project_short_code', 'like', '%' . request('searchText') . '%'); // project short code
});
})
->orWhere(function ($query) {
$query->where('invoices.status', 'like', '%' . request('searchText') . '%');
});
});
}
if (in_array('client', user_roles())) {
$model = $model->where('invoices.send_status', 1);
$model = $model->where('invoices.client_id', user()->id);
}
if ($this->viewInvoicePermission == 'added') {
$model = $model->where('invoices.added_by', user()->id);
}
if ($this->viewInvoicePermission == 'owned') {
$model = $model->where('invoices.client_id', user()->id);
}
if ($this->viewInvoicePermission == 'both') {
$model = $model->where('invoices.client_id', user()->id)->orWhere('invoices.added_by', user()->id);
}
$model = $model->whereHas('project', function ($q) {
$q->whereNull('deleted_at');
}, '>=', 0);
return $model;
}
/**
* Optional method if you want to use html builder.
*
* @return \Yajra\DataTables\Html\Builder
*/
public function html()
{
$dataTable = $this->setBuilder('invoices-table', 0)
->parameters([
'initComplete' => 'function () {
window.LaravelDataTables["invoices-table"].buttons().container()
.appendTo( "#table-actions")
}',
'fnDrawCallback' => 'function( oSettings ) {
$("body").tooltip({
selector: \'[data-toggle="tooltip"]\'
})
}',
]);
if (canDataTableExport()) {
$dataTable->buttons(Button::make(['extend' => 'excel', 'text' => '