v2 first commit

This commit is contained in:
denisdulici
2019-11-16 10:21:14 +03:00
parent 5b23e9c2c4
commit 6d50fa8442
3075 changed files with 3451681 additions and 65594 deletions

View File

@ -0,0 +1,162 @@
<?php
namespace App\Abstracts;
use Artisan;
use Illuminate\Database\Eloquent\Collection;
class BulkAction
{
public $model = false;
public $actions = [
'enable' => [
'name' => 'general.enable',
'message' => 'bulk_actions.message.enable',
'permission' => 'update-common-items'
],
'disable' => [
'name' => 'general.disable',
'message' => 'bulk_actions.message.disable',
'permission' => 'update-common-items'
],
'export' => [
'name' => 'general.export',
'message' => 'bulk_actions.message.exports',
],
'delete' => [
'name' => 'general.delete',
'message' => 'bulk_actions.message.deletes',
'permission' => 'delete-common-items'
]
];
/**
* Duplicate the specified resource.
*
* @param $request
*
* @return Response
*/
public function duplicate($request)
{
$selected = $request->get('selected', []);
$items = $this->model::find($selected);
foreach ($items as $item) {
$item->duplicate();
}
}
/**
* Enable the specified resource.
*
* @param $request
*
* @return Response
*/
public function enable($request)
{
$selected = $request->get('selected', []);
$items = $this->model::find($selected);
foreach ($items as $item) {
$item->enabled = 1;
$item->save();
}
}
/**
* Disable the specified resource.
*
* @param $request
*
* @return Response
*/
public function disable($request)
{
$selected = $request->get('selected', []);
$items = $this->model::find($selected);
foreach ($items as $item) {
$item->enabled = 0;
$item->save();
}
}
/**
* Remove the specified resource from storage.
*
* @param $request
*
* @return Response
*/
public function delete($request)
{
$this->destroy($request);
}
/**
* Remove the specified resource from storage.
*
* @param $request
*
* @return Response
*/
public function destroy($request)
{
$selected = $request->get('selected', []);
$items = $this->model::find($selected);
foreach ($items as $item) {
$item->delete();
}
Artisan::call('cache:clear');
}
public function countRelationships($model, $relationships)
{
$counter = [];
foreach ($relationships as $relationship => $text) {
if ($c = $model->$relationship()->count()) {
$counter[] = $c . ' ' . strtolower(trans_choice('general.' . $text, ($c > 1) ? 2 : 1));
}
}
return $counter;
}
/**
* Mass delete relationships with events being fired.
*
* @param $model
* @param $relationships
*
* @return void
*/
public function deleteRelationships($model, $relationships)
{
foreach ((array) $relationships as $relationship) {
if (empty($model->$relationship)) {
continue;
}
$items = $model->$relationship->all();
if ($items instanceof Collection) {
$items = $items->all();
}
foreach ((array) $items as $item) {
$item->delete();
}
}
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Abstracts\Http;
use Dingo\Api\Exception\ResourceException;
use Dingo\Api\Routing\Helpers;
use Illuminate\Http\Request;
abstract class ApiController extends Controller
{
use Helpers;
/**
* Create the response for when a request fails validation.
*
* @param \Illuminate\Http\Request $request
* @param array $errors
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function buildFailedValidationResponse(Request $request, array $errors)
{
if ($request->expectsJson()) {
throw new ResourceException('Validation Error', $errors);
}
return redirect()->to($this->getRedirectUrl())->withInput($request->input())->withErrors($errors, $this->errorBag());
}
}

View File

@ -0,0 +1,122 @@
<?php
namespace App\Abstracts\Http;
use App\Traits\Jobs;
use App\Traits\Relationships;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Routing\Route;
use Illuminate\Support\Str;
abstract class Controller extends BaseController
{
use AuthorizesRequests, Jobs, Relationships, ValidatesRequests;
/**
* Instantiate a new controller instance.
*/
public function __construct()
{
$this->setPermissions();
}
/**
* Assign permissions to methods.
*
* @return void
*/
protected function setPermissions()
{
// No need to check for permission in console
if (app()->runningInConsole()) {
return;
}
$route = app(Route::class);
// Get the controller array
$arr = array_reverse(explode('\\', explode('@', $route->getAction()['uses'])[0]));
$controller = '';
// Add folder
if (strtolower($arr[1]) != 'controllers') {
$controller .= Str::kebab($arr[1]) . '-';
}
// Add module
if (isset($arr[3]) && isset($arr[4]) && (strtolower($arr[4]) == 'modules')) {
$controller .= Str::kebab($arr[3]) . '-';
}
// Add file
$controller .= Str::kebab($arr[0]);
// Skip ACL
$skip = ['common-dashboard', 'portal-dashboard'];
if (in_array($controller, $skip)) {
return;
}
// Add CRUD permission check
$this->middleware('permission:create-' . $controller)->only(['create', 'store', 'duplicate', 'import']);
$this->middleware('permission:read-' . $controller)->only(['index', 'show', 'edit', 'export']);
$this->middleware('permission:update-' . $controller)->only(['update', 'enable', 'disable']);
$this->middleware('permission:delete-' . $controller)->only('destroy');
}
/**
* Generate a pagination collection.
*
* @param array|Collection $items
* @param int $perPage
* @param int $page
* @param array $options
*
* @return LengthAwarePaginator
*/
public function paginate($items, $perPage = 15, $page = null, $options = [])
{
$perPage = $perPage ?: request('limit', setting('default.list_limit', '25'));
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
/**
* Dispatch a job to its appropriate handler and return a response array for ajax calls.
*
* @param mixed $job
* @return mixed
*/
public function ajaxDispatch($job)
{
try {
$data = $this->dispatch($job);
$response = [
'success' => true,
'error' => false,
'data' => $data,
'message' => '',
];
} catch(\Exception $e) {
$response = [
'success' => false,
'error' => true,
'data' => null,
'message' => $e->getMessage(),
];
}
return $response;
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace App\Abstracts\Http;
use Illuminate\Foundation\Http\FormRequest as BaseFormRequest;
use Illuminate\Support\Arr;
abstract class FormRequest extends BaseFormRequest
{
/**
* Set the company id to the request.
*
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function getValidatorInstance()
{
// Get request data
$data = $this->all();
// Add active company id
$data['company_id'] = session('company_id');
// Reset the request data
$this->getInputSource()->replace($data);
return parent::getValidatorInstance();
}
/**
* Determine if the given offset exists.
*
* @param string $offset
* @return bool
*/
public function offsetExists($offset)
{
return Arr::has(
$this->route() ? $this->all() + $this->route()->parameters() : $this->all(),
$offset
);
}
}

View File

@ -0,0 +1,202 @@
<?php
namespace App\Abstracts\Http;
use App\Http\Requests\Portal\InvoicePayment as PaymentRequest;
use App\Models\Income\Invoice;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\URL;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
abstract class PaymentController extends BaseController
{
public $alias = '';
public $type = ''; // hosted, redirect
public $setting = [];
public $logger = null;
public $user = null;
public $module = null;
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->setting = setting($this->alias);
$this->setting['code'] = $this->alias;
$this->setting['language'] = app()->getLocale();
$this->logger = $this->getLogger();
$this->user = user();
$this->module = module($this->alias);
return $next($request);
});
}
public function show(Invoice $invoice, PaymentRequest $request)
{
return $this->getInvoiceShow($invoice, 'show');
}
public function signed(Invoice $invoice, PaymentRequest $request)
{
return $this->getInvoiceShow($invoice, 'signed');
}
public function cancel(Invoice $invoice, $force_redirect = false)
{
$message = trans('messages.warning.payment_cancel', ['method' => setting($this->alias . '.name')]);
$this->logger->info($this->module->getName() . ':: Invoice: ' . $invoice->id . ' - Cancel Message: ' . $message);
flash($message)->warning();
$invoice_url = $this->getInvoiceUrl($invoice);
if ($force_redirect || ($this->type == 'redirect')) {
return redirect($invoice_url);
}
return response()->json([
'error' => $message,
'redirect' => $invoice_url,
'success' => false,
'data' => false,
]);
}
public function finish($invoice, $request, $force_redirect = false)
{
$this->dispatchPaidEvent($invoice, $request);
$this->forgetReference($invoice);
$message = trans('messages.success.added', ['type' => trans_choice('general.payments', 1)]);
$this->logger->info($this->module->getName() . ':: Invoice: ' . $invoice->id . ' - Success Message: ' . $message);
flash($message)->success();
$invoice_url = $this->getInvoiceUrl($invoice);
if ($force_redirect || ($this->type == 'redirect')) {
return redirect($invoice_url);
}
return response()->json([
'error' => $message,
'redirect' => $invoice_url,
'success' => true,
'data' => false,
]);
}
public function getInvoiceShow(Invoice $invoice, $view = 'show')
{
$this->setContactFirstLastName($invoice);
$confirm_url = $this->getConfirmUrl($invoice);
$html = view('partials.portal.payment_method.' . $this->type . '.' . $view, [
'setting' => $this->setting,
'invoice' => $invoice,
'confirm_url' => $confirm_url,
])->render();
return response()->json([
'code' => $this->setting['code'],
'name' => $this->setting['name'],
'description' => trans($this->alias . '::general.description'),
'redirect' => false,
'html' => $html,
]);
}
public function getInvoiceUrl($invoice)
{
return $this->user
? route('portal.invoices.show', $invoice->id)
: URL::signedRoute('signed.invoices.show', [$invoice->id, 'company_id' => $invoice->company_id]);
}
public function getConfirmUrl($invoice)
{
return $this->getModuleUrl($invoice, 'confirm');
}
public function getReturnUrl($invoice)
{
return $this->getModuleUrl($invoice, 'return');
}
public function getCancelUrl($invoice)
{
return $this->getModuleUrl($invoice, 'cancel');
}
public function getNotifyUrl($invoice)
{
return route('portal.invoices.' . $this->alias . '.notify', $invoice->id);
}
public function getModuleUrl($invoice, $suffix)
{
return $this->user
? route('portal.invoices.' . $this->alias . '.' . $suffix, $invoice->id)
: URL::signedRoute('signed.invoices.' . $this->alias . '.' . $suffix, [$invoice->id, 'company_id' => $invoice->company_id]);
}
public function getLogger()
{
$log = new Logger($this->alias);
$log->pushHandler(new StreamHandler(storage_path('logs/' . $this->alias . '.log')), Logger::INFO);
return $log;
}
public function dispatchPaidEvent($invoice, $request)
{
$request['company_id'] = $invoice->company_id;
$request['amount'] = $invoice->amount;
$request['payment_method'] = $this->alias;
$request['reference'] = $this->getReference($invoice);
event(new \App\Events\Income\PaymentReceived($invoice, $request));
}
public function setReference($invoice, $reference)
{
session([
$this->alias . '_' . $invoice->id . '_reference' => $reference
]);
}
public function getReference($invoice)
{
return session($this->alias . '_' . $invoice->id . '_reference');
}
public function forgetReference($invoice)
{
session()->forget($this->alias . '_' . $invoice->id . '_reference');
}
public function setContactFirstLastName(&$invoice)
{
$contact = explode(" ", $invoice->contact_name);
$last_name = array_pop($contact);
$first_name = implode(" ", $contact);
$invoice->first_name = $first_name;
$invoice->last_name = $last_name;
}
}

28
app/Abstracts/Job.php Normal file
View File

@ -0,0 +1,28 @@
<?php
namespace App\Abstracts;
use App\Abstracts\Http\FormRequest;
use App\Traits\Jobs;
use App\Traits\Relationships;
use App\Traits\Uploads;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
abstract class Job implements ShouldQueue
{
use InteractsWithQueue, Jobs, Queueable, Relationships, SerializesModels, Uploads;
public function getRequestInstance($request)
{
if (!is_array($request)) {
return $request;
}
$class = new class() extends FormRequest {};
return $class->merge($request);
}
}

121
app/Abstracts/Model.php Normal file
View File

@ -0,0 +1,121 @@
<?php
namespace App\Abstracts;
use App\Scopes\Company;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kyslik\ColumnSortable\Sortable;
use Lorisleiva\LaravelSearchString\Concerns\SearchString;
abstract class Model extends Eloquent
{
use SearchString, SoftDeletes, Sortable;
protected $dates = ['deleted_at'];
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope(new Company);
}
/**
* Global company relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function company()
{
return $this->belongsTo('App\Models\Common\Company');
}
/**
* Scope to only include company data.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $company_id
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCompanyId($query, $company_id)
{
return $query->where($this->table . '.company_id', '=', $company_id);
}
/**
* Scope to get all rows filtered, sorted and paginated.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $sort
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCollect($query, $sort = 'name')
{
$request = request();
$search = $request->get('search');
$limit = $request->get('limit', setting('default.list_limit', '25'));
return $query->usingSearchString($search)->sortable($sort)->paginate($limit);
}
/**
* Scope to only include active models.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeEnabled($query)
{
return $query->where('enabled', 1);
}
/**
* Scope to only include passive models.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeDisabled($query)
{
return $query->where('enabled', 0);
}
/**
* Scope to only include reconciled models.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param $value
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeReconciled($query, $value = 1)
{
return $query->where('reconciled', $value);
}
public function scopeAccount($query, $accounts)
{
if (empty($accounts)) {
return $query;
}
return $query->whereIn('account_id', (array) $accounts);
}
public function scopeContact($query, $contacts)
{
if (empty($contacts)) {
return $query;
}
return $query->whereIn('contact_id', (array) $contacts);
}
}

View File

@ -0,0 +1,74 @@
<?php
namespace App\Abstracts;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification as BaseNotification;
abstract class Notification extends BaseNotification
{
/**
* Create a notification instance.
*/
public function __construct()
{
$this->queue = 'high';
$this->delay = config('queue.connections.database.delay');
}
/**
* Get the notification's channels.
*
* @param mixed $notifiable
* @return array|string
*/
public function via($notifiable)
{
return ['mail', 'database'];
}
/**
* Initialise the mail representation of the notification.
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function initMessage()
{
$message = (new MailMessage)
->from(config('mail.from.address'), config('mail.from.name'))
->subject($this->getSubject())
->view('partials.email.body', ['body' => $this->getBody()]);
return $message;
}
public function getSubject()
{
$content = setting('email.' . $this->template . '_subject');
return $this->replaceTags($content);
}
public function getBody()
{
$content = setting('email.' . $this->template . '_body');
return $this->replaceTags($content);
}
public function replaceTags($content)
{
return preg_replace($this->getTagsPattern(), $this->getTagsReplacement(), $content);
}
public function getTagsPattern()
{
$pattern = [];
foreach($this->getTags() as $tag) {
$pattern[] = "/" . $tag . "/";
}
return $pattern;
}
}

View File

@ -0,0 +1,226 @@
<?php
namespace App\Abstracts\Reports;
use App\Events\Common\ReportFilterApplying;
use App\Events\Common\ReportFilterShowing;
use App\Events\Common\ReportGroupApplying;
use App\Events\Common\ReportGroupShowing;
use App\Models\Banking\Account;
use App\Models\Common\Contact;
use App\Models\Setting\Category;
use App\Traits\Contacts;
use Date;
abstract class Listener
{
use Contacts;
protected $class = '';
protected $events = [
'App\Events\Common\ReportFilterShowing',
'App\Events\Common\ReportFilterApplying',
'App\Events\Common\ReportGroupShowing',
'App\Events\Common\ReportGroupApplying',
];
public function checkClass($event)
{
return (get_class($event->class) == $this->class);
}
public function getYears()
{
$now = Date::now();
$years = [];
$y = $now->addYears(2);
for ($i = 0; $i < 10; $i++) {
$years[$y->year] = $y->year;
$y->subYear();
}
return $years;
}
public function getAccounts()
{
return Account::enabled()->orderBy('name')->pluck('name', 'id')->toArray();
}
public function getItemCategories()
{
return $this->getCategories('item');
}
public function getIncomeCategories()
{
return $this->getCategories('income');
}
public function getExpenseCategories()
{
return $this->getCategories('expense');
}
public function getIncomeExpenseCategories()
{
return $this->getCategories(['income', 'expense']);
}
public function getCategories($types)
{
return Category::type($types)->enabled()->orderBy('name')->pluck('name', 'id')->toArray();
}
public function getCustomers()
{
return $this->getContacts($this->getCustomerTypes());
}
public function getVendors()
{
return $this->getContacts($this->getVendorTypes());
}
public function getContacts($types)
{
return Contact::type($types)->enabled()->orderBy('name')->pluck('name', 'id')->toArray();
}
public function applyDateFilter($event)
{
$event->model->monthsOfYear($event->args['date_field']);
}
public function applySearchStringFilter($event)
{
$event->model->usingSearchString(request('search'));
}
public function applyAccountGroup($event)
{
if (($event->model->getTable() != 'invoices') && ($event->model->getTable() != 'bills')) {
return;
}
$filter = request('accounts', []);
$event->model->account_id = 0;
foreach ($event->model->transactions as $transaction) {
if (!empty($filter) && !in_array($transaction->account_id, $filter)) {
continue;
}
$event->model->account_id = $transaction->account_id;
}
}
public function applyCustomerGroup($event)
{
foreach ($this->getCustomerTypes() as $type) {
$id_field = $type . '_id';
$event->model->$id_field = $event->model->contact_id;
}
}
public function applyVendorGroup($event)
{
foreach ($this->getVendorTypes() as $type) {
$id_field = $type . '_id';
$event->model->$id_field = $event->model->contact_id;
}
}
/**
* Handle filter showing event.
*
* @param $event
* @return void
*/
public function handleReportFilterShowing(ReportFilterShowing $event)
{
if (!$this->checkClass($event)) {
return;
}
$event->class->filters['years'] = $this->getYears();
}
/**
* Handle filter applying event.
*
* @param $event
* @return void
*/
public function handleReportFilterApplying(ReportFilterApplying $event)
{
if (!$this->checkClass($event)) {
return;
}
// Apply date
$this->applyDateFilter($event);
// Apply search
$this->applySearchStringFilter($event);
}
/**
* Handle group showing event.
*
* @param $event
* @return void
*/
public function handleReportGroupShowing(ReportGroupShowing $event)
{
if (!$this->checkClass($event)) {
return;
}
$event->class->groups['category'] = trans_choice('general.categories', 1);
}
/**
* Handle group applying event.
*
* @param $event
* @return void
*/
public function handleReportGroupApplying(ReportGroupApplying $event)
{
if (!$this->checkClass($event)) {
return;
}
$this->applyAccountGroup($event);
}
/**
* Register the listeners for the subscriber.
*
* @param \Illuminate\Events\Dispatcher $events
*/
public function subscribe($events)
{
$class = get_class($this);
foreach ($this->events as $event) {
$method = 'handle' . (new \ReflectionClass($event))->getShortName();
if (!method_exists($class, $method)) {
continue;
}
$events->listen(
$event,
$class . '@' . $method
);
}
}
}

View File

@ -0,0 +1,434 @@
<?php
namespace App\Abstracts\Reports;
use App\Exports\Common\Reports as Export;
use App\Models\Common\Report as Model;
use App\Utilities\Chartjs;
use App\Traits\DateTime;
use Date;
use Illuminate\Support\Str;
abstract class Report
{
use DateTime;
public $report;
public $year;
public $views = [];
public $tables = [];
public $dates = [];
public $rows = [];
public $totals = [];
public $groups = [];
public $filters = [];
public $category = 'income-expense';
public $icon = 'fa fa-chart-pie';
public $indents = [
'table_header' => '0px',
'table_rows' => '0px',
];
public $chart = [
'line' => [
'width' => '0',
'height' => '300',
'options' => [
'color' => '#6da252',
],
],
'dates' => [],
'datasets' => [],
];
public function __construct(Model $report = null, $get_totals = true)
{
$this->setGroups();
if (!$report) {
return;
}
$this->report = $report;
$this->setYear();
$this->setViews();
$this->setTables();
$this->setDates();
$this->setFilters();
$this->setRows();
if ($get_totals) {
$this->getTotals();
}
}
abstract public function getTotals();
public function getName()
{
return Str::title(str_replace('_', ' ', Str::snake((new \ReflectionClass($this))->getShortName())));
}
public function getCategory()
{
return $this->category;
}
public function getIcon()
{
return $this->icon;
}
public function getTotal()
{
$sum = 0;
foreach ($this->totals as $total) {
$sum += is_array($total) ? array_sum($total) : $total;
}
$total = money($sum, setting('default.currency'), true);
return $total;
}
public function getTableRowList()
{
$group_prl = Str::plural($this->report->group);
if ($group_filter = request($group_prl)) {
$rows = collect($this->filters[$group_prl])->filter(function ($value, $key) use ($group_filter) {
return in_array($key, $group_filter);
});
} else {
$rows = $this->filters[$group_prl];
}
return $rows;
}
public function getChart()
{
$chart = new Chartjs();
$config = $this->chart[$this->report->chart];
$default_options = [
'tooltips' => [
'backgroundColor' => '#f5f5f5',
'titleFontColor' => '#333',
'bodyFontColor' => '#666',
'bodySpacing' => 4,
'YrPadding' => 12,
'mode' => 'nearest',
'intersect' => 0,
'position' => 'nearest'
],
'responsive' => true,
'scales' => [
'yAxes' => [
[
'barPercentage' => '1.6',
'gridLines' => [
'drawBorder' => false,
'color' => 'rgba(29,140,248,0.1)',
'zeroLineColor' => 'transparent',
'borderDash' => [2],
'borderDashOffset' => [2],
],
'ticks' => [
'padding' => 10,
'fontColor' => '#9e9e9e'
]
]
],
'xAxes' => [
[
'barPercentage' => '1.6',
'gridLines' => [
'drawBorder' => false,
'color' => 'rgba(29,140,248,0.0)',
'zeroLineColor' => 'transparent'
],
'ticks' => [
'suggestedMin' => 60,
'suggestedMax' => 125,
'padding' => 20,
'fontColor' => '#9e9e9e'
]
]
]
]
];
$options = array_merge($default_options, (array) $config['options']);
$chart->type($this->report->chart)
->width((int) $config['width'])
->height((int) $config['height'])
->options($options)
->labels(!empty($config['dates']) ? array_values($config['dates']) : array_values($this->dates));
if (!empty($config['datasets'])) {
foreach ($config['datasets'] as $dataset) {
$chart->dataset($dataset['name'], 'line', array_values($dataset['totals']))
->backgroundColor(isset($dataset['backgroundColor']) ? $dataset['backgroundColor'] : '#6da252')
->color(isset($dataset['color']) ? $dataset['color'] : '#6da252')
->options((array) $dataset['options'])
->fill(false);
}
} else {
foreach ($this->totals as $total) {
$chart->dataset($this->report->name, 'line', array_values($total))
->backgroundColor(isset($config['backgroundColor']) ? $config['backgroundColor'] : '#6da252')
->color(isset($config['color']) ? $config['color'] : '#6da252')
->options([
'borderWidth' => 4,
'pointStyle' => 'line',
])
->fill(false);
}
}
return $chart;
}
public function show()
{
return view($this->views['show'])->with('class', $this);
}
public function print()
{
return view($this->views['print'])->with('class', $this);
}
public function export()
{
return \Excel::download(new Export($this->views['content'], $this), $this->report->name . '.xlsx');
}
public function setYear()
{
$this->year = request('year', Date::now()->year);
}
public function setViews()
{
$this->views = [
'chart' => 'partials.reports.chart',
'content' => 'partials.reports.content',
'content.header' => 'partials.reports.content.header',
'content.footer' => 'partials.reports.content.footer',
'show' => 'partials.reports.show',
'header' => 'partials.reports.header',
'filter' => 'partials.reports.filter',
'print' => 'partials.reports.print',
'table' => 'partials.reports.table',
'table.footer' => 'partials.reports.table.footer',
'table.header' => 'partials.reports.table.header',
'table.rows' => 'partials.reports.table.rows',
];
}
public function setTables()
{
$this->tables = [
'default' => 'default'
];
}
public function setDates()
{
$function = 'sub' . ucfirst(str_replace('ly', '', $this->report->period));
$start = $this->getFinancialStart()->copy()->$function();
for ($j = 1; $j <= 12; $j++) {
switch ($this->report->period) {
case 'yearly':
$start->addYear();
$date = $this->getFormattedDate($start);
$this->dates[$j] = $date;
foreach ($this->tables as $table) {
$this->totals[$table][$date] = 0;
}
$j += 11;
break;
case 'quarterly':
$start->addQuarter();
$date = $this->getFormattedDate($start);
$this->dates[$j] = $date;
foreach ($this->tables as $table) {
$this->totals[$table][$date] = 0;
}
$j += 2;
break;
default:
$start->addMonth();
$date = $this->getFormattedDate($start);
$this->dates[$j] = $date;
foreach ($this->tables as $table) {
$this->totals[$table][$date] = 0;
}
break;
}
}
}
public function setFilters()
{
event(new \App\Events\Common\ReportFilterShowing($this));
}
public function setGroups()
{
event(new \App\Events\Common\ReportGroupShowing($this));
}
public function setRows()
{
$list = $this->getTableRowList();
foreach ($this->dates as $date) {
foreach ($this->tables as $table) {
foreach ($list as $id => $name) {
$this->rows[$table][$id][$date] = 0;
}
}
}
}
public function setTotals($items, $date_field, $check_type = false, $table = 'default')
{
foreach ($items as $item) {
// Make groups extensible
$item = $this->applyGroups($item);
$date = $this->getFormattedDate(Date::parse($item->$date_field));
$id_field = $this->report->group . '_id';
if (!isset($this->rows[$table][$item->$id_field]) ||
!isset($this->rows[$table][$item->$id_field][$date]) ||
!isset($this->totals[$table][$date]))
{
continue;
}
$amount = $item->getAmountConvertedToDefault();
if (!$check_type) {
$this->rows[$table][$item->$id_field][$date] += $amount;
$this->totals[$table][$date] += $amount;
} else {
$type = (($item->getTable() == 'invoices') || (($item->getTable() == 'transactions') && ($item->type == 'income'))) ? 'income' : 'expense';
if ($type == 'income') {
$this->rows[$table][$item->$id_field][$date] += $amount;
$this->totals[$table][$date] += $amount;
} else {
$this->rows[$table][$item->$id_field][$date] -= $amount;
$this->totals[$table][$date] -= $amount;
}
}
}
}
public function applyFilters($model, $args = [])
{
event(new \App\Events\Common\ReportFilterApplying($this, $model, $args));
return $model;
}
public function applyGroups($model, $args = [])
{
event(new \App\Events\Common\ReportGroupApplying($this, $model, $args));
return $model;
}
public function getFormattedDate($date)
{
switch ($this->report->period) {
case 'yearly':
$i = $date->copy()->format($this->getYearlyDateFormat());
break;
case 'quarterly':
$start = $date->copy()->startOfQuarter()->format($this->getQuarterlyDateFormat());
$end = $date->copy()->endOfQuarter()->format($this->getQuarterlyDateFormat());
$i = $start . '-' . $end;
break;
default:
$i = $date->copy()->format($this->getMonthlyDateFormat());
break;
}
return $i;
}
public function getUrl($action = 'print')
{
$print_url = 'common/reports/' . $this->report->id . '/' . $action . '?year='. $this->year;
collect(request('accounts'))->each(function($item) use(&$print_url) {
$print_url .= '&accounts[]=' . $item;
});
collect(request('customers'))->each(function($item) use(&$print_url) {
$print_url .= '&customers[]=' . $item;
});
collect(request('categories'))->each(function($item) use(&$print_url) {
$print_url .= '&categories[]=' . $item;
});
return $print_url;
}
public function getPermission()
{
$permission = 'read-reports-' . Str::kebab((new \ReflectionClass($this))->getShortName());
return $permission;
}
public function canRead()
{
return user()->can($this->getPermission());
}
}