芝麻web文件管理V1.00
编辑当前文件:/home/sditechnicalteam/public_html/app/DataTables/EstimatesDataTable.php
viewEstimatePermission = user()->permission('view_estimates'); $this->addEstimatePermission = user()->permission('add_estimates'); $this->editEstimatePermission = user()->permission('edit_estimates'); $this->deleteEstimatePermission = user()->permission('delete_estimates'); $this->addInvoicePermission = user()->permission('add_invoices'); } /** * Build DataTable class. * * @param mixed $query Results from query() method. * @return \Yajra\DataTables\DataTableAbstract */ public function dataTable($query) { $firstEstimate = $this->firstEstimate; $datatables = datatables()->eloquent($query); $datatables->addIndexColumn(); $datatables->addColumn('action', function ($row) use ($firstEstimate) { $action = '
'; $action .= '
' . __('app.view') . '
'; $action .= '
' . __('modules.estimates.copyLink') . '
'; $action .= '
' . trans('modules.estimates.viewLink') . '
'; if ($row->status != 'draft') { $action .= '
' . trans('app.download') . '
'; } if ($row->status == 'waiting' || $row->status == 'draft') { if ( $this->editEstimatePermission == 'all' || ($this->editEstimatePermission == 'added' && $row->added_by == user()->id) || ($this->editEstimatePermission == 'owned' && $row->client_id == user()->id) || ($this->editEstimatePermission == 'both' && ($row->client_id == user()->id || $row->added_by == user()->id)) ) { $action .= '
' . trans('app.edit') . '
'; } } if ($row->status != 'canceled' && $row->status != 'accepted' && !in_array('client', user_roles())) { $action .= '
' . __('app.send') . '
'; } if ($firstEstimate->id == $row->id) { if ( $this->deleteEstimatePermission == 'all' || ($this->deleteEstimatePermission == 'added' && $row->added_by == user()->id) || ($this->deleteEstimatePermission == 'owned' && $row->client_id == user()->id) || ($this->deleteEstimatePermission == 'both' && ($row->client_id == user()->id || $row->added_by == user()->id)) ) { $action .= '
' . trans('app.delete') . '
'; } } if ($row->status == 'waiting') { if ($this->addInvoicePermission == 'all' || $this->addInvoicePermission == 'added') { $action .= '
' . __('app.create') . ' ' . __('app.invoice') . '
'; } if ($this->editEstimatePermission == 'all' || ($this->editEstimatePermission == 'added' && $row->added_by == user()->id)) { $action .= '
' . __('app.cancelEstimate') . '
'; } } if ($this->addEstimatePermission == 'all' || $this->addEstimatePermission == 'added') { $action .= '
' . __('app.create') . ' ' . __('app.duplicate') . '
'; } $action .= '
'; return $action; }); $datatables->addColumn('original_estimate_number', function ($row) { return '
' . $row->estimate_number . '
'; }); $datatables->addColumn('client_name', function ($row) { return $row->name; }); $datatables->editColumn('name', function ($row) { return view('components.client', [ 'user' => $row->client ]); }); $datatables->editColumn('status', function ($row) { $status = ''; if ($row->status == 'waiting') { $status .= '
' . __('modules.estimates.' . $row->status) . ''; } elseif ($row->status == 'draft') { $status .= '
' . __('app.' . $row->status) . ''; } elseif ($row->status == 'canceled') { $status .= '
' . __('app.' . $row->status) . ''; } elseif ($row->status == 'declined') { $status .= '
' . __('modules.estimates.' . $row->status) . ''; } else { $status .= '
' . __('modules.estimates.' . $row->status) . ''; } if (!$row->send_status && $row->status != 'draft' && $row->status != 'canceled') { $status .= '
' . mb_strtoupper(__('modules.invoices.notSent')) . '
'; } return $status; }); $datatables->editColumn('total', function ($row) { return currency_formatter($row->total, $row->currency_symbol); }); $datatables->editColumn( 'valid_till', function ($row) { return Carbon::parse($row->valid_till)->format($this->company->date_format); } )->editColumn( 'created_at', function ($row) { return Carbon::parse($row->created_at)->format($this->company->date_format); } ); $datatables->rawColumns(['name', 'action', 'status', 'original_estimate_number']); $datatables->removeColumn('currency_symbol'); $datatables->removeColumn('client_id'); // Custom Fields For export CustomField::customFieldData($datatables, Estimate::CUSTOM_FIELD_MODEL); return $datatables; } public function ajax() { return $this->dataTable($this->query()) ->make(true); } /** * @return \Illuminate\Database\Eloquent\Builder */ public function query() { $request = $this->request(); $this->firstEstimate = Estimate::orderBy('id', 'desc')->first(); $model = Estimate::with('client', 'client.session', 'company:id') ->join('client_details', 'estimates.client_id', '=', 'client_details.user_id') ->join('currencies', 'currencies.id', '=', 'estimates.currency_id') ->join('users', 'users.id', '=', 'estimates.client_id') ->select([ 'estimates.id', 'estimates.company_id', 'estimates.client_id', 'users.name', 'estimates.total', 'currencies.currency_symbol', 'estimates.status', 'estimates.valid_till', 'estimates.estimate_number', 'estimates.send_status', 'estimates.added_by', 'estimates.hash', 'estimates.created_at' ]); 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(estimates.`valid_till`)'), '>=', $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(estimates.`valid_till`)'), '<=', $endDate); } if ($request->status != 'all' && !is_null($request->status)) { $model = $model->where('estimates.status', '=', $request->status); } if ($request->clientID != 'all' && !is_null($request->clientID)) { $model = $model->where('estimates.client_id', '=', $request->clientID); } if (in_array('client', user_roles())) { $model = $model->where('estimates.send_status', 1); $model = $model->where('estimates.client_id', user()->id); } if ($request->searchText != '') { $model->where(function ($query) { $query->where('estimates.estimate_number', 'like', '%' . request('searchText') . '%') ->orWhere('estimates.id', 'like', '%' . request('searchText') . '%') ->orWhere('estimates.total', 'like', '%' . request('searchText') . '%') ->orWhere(function ($query) { $query->whereHas('client', function ($q) { $q->where('name', 'like', '%' . request('searchText') . '%'); }); }) ->orWhere(function ($query) { $query->where('estimates.status', 'like', '%' . request('searchText') . '%'); }); }); } if ($this->viewEstimatePermission == 'added') { $model->where('estimates.added_by', user()->id); } if ($this->viewEstimatePermission == 'both') { $model->where(function ($query) { $query->where('estimates.added_by', user()->id) ->orWhere('estimates.client_id', user()->id); }); } if ($this->viewEstimatePermission == 'owned') { $model->where('estimates.client_id', user()->id); } return $model; } /** * Optional method if you want to use html builder. * * @return \Yajra\DataTables\Html\Builder */ public function html() { return $this->builder() ->setTableId('invoices-table') ->columns($this->getColumns()) ->minifiedAjax() ->orderBy(1) ->destroy(true) ->responsive(true) ->serverSide(true) ->stateSave(true) ->processing(true) ->dom($this->domHtml) ->language(__('app.datatable')) ->parameters([ 'initComplete' => 'function () { window.LaravelDataTables["invoices-table"].buttons().container() .appendTo( "#table-actions") }', 'fnDrawCallback' => 'function( oSettings ) { $("body").tooltip({ selector: \'[data-toggle="tooltip"]\' }) }', ]) ->buttons(Button::make(['extend' => 'excel', 'text' => '
' . trans('app.exportExcel')])); } /** * Get columns. * * @return array */ protected function getColumns() { $data = [ '#' => ['data' => 'DT_RowIndex', 'orderable' => false, 'searchable' => false, 'visible' => false], __('app.id') => ['data' => 'id', 'name' => 'id', 'title' => __('app.id'), 'visible' => false], __('app.estimate') . '#' => ['data' => 'original_estimate_number', 'name' => 'original_estimate_number', 'title' => __('app.estimate')], __('app.client') => ['data' => 'name', 'name' => 'users.name', 'exportable' => false, 'title' => __('app.client')], __('app.customers') => ['data' => 'client_name', 'name' => 'users.name', 'visible' => false, 'title' => __('app.customers')], __('modules.invoices.total') => ['data' => 'total', 'name' => 'total', 'title' => __('modules.invoices.total')], __('modules.estimates.validTill') => ['data' => 'valid_till', 'name' => 'valid_till', 'title' => __('modules.estimates.validTill')], __('app.createdOn') => ['data' => 'created_at', 'name' => 'created_at', 'title' => __('app.createdOn')], __('app.status') => ['data' => 'status', 'name' => 'status', 'title' => __('app.status')], Column::computed('action', __('app.action')) ->exportable(false) ->printable(false) ->orderable(false) ->searchable(false) ->addClass('text-right pr-20') ]; return array_merge($data, CustomFieldGroup::customFieldsDataMerge(new Estimate())); } /** * Get filename for export. * * @return string */ protected function filename() { return 'estimates_' . date('YmdHis'); } public function pdf() { set_time_limit(0); if ('snappy' == config('datatables-buttons.pdf_generator', 'snappy')) { return $this->snappyPdf(); } $pdf = app('dompdf.wrapper'); $pdf->loadView('datatables::print', ['data' => $this->getDataForPrint()]); return $pdf->download($this->getFilename() . '.pdf'); } }