Merge pull request #174 from cuneytsenturk/master

Attachment manager
This commit is contained in:
Cüneyt Şentürk
2018-01-09 02:02:38 +03:00
committed by GitHub
57 changed files with 1398 additions and 167 deletions

View File

@ -23,4 +23,4 @@ class Transfers extends ModelFilter
{
return $this->related('revenue', 'revenues.account_id', '=', $account_id);
}
}
}

View File

@ -7,11 +7,14 @@ use App\Http\Requests\Auth\User as Request;
use Illuminate\Http\Request as ARequest;
use App\Models\Auth\User;
use App\Models\Auth\Role;
use App\Traits\Uploads;
use Auth;
class Users extends Controller
{
use Uploads;
/**
* Display a listing of the resource.
*
@ -34,11 +37,12 @@ class Users extends Controller
*/
public function create()
{
$roles = Role::all()->reject(function($r) {
$roles = Role::all()->reject(function ($r) {
return $r->hasPermission('read-customer-panel');
});
$companies = Auth::user()->companies()->get()->sortBy('name');
foreach ($companies as $company) {
$company->setSettings();
}
@ -55,15 +59,16 @@ class Users extends Controller
*/
public function store(Request $request)
{
// Upload picture
$picture = $request->file('picture');
if ($picture && $picture->isValid()) {
$request['picture'] = $picture->store('uploads/users');
}
// Create user
$user = User::create($request->input());
// Upload picture
if ($request->file('picture')) {
$media = $this->getMedia($request->file('picture'), 'users');
$user->attachMedia($media, 'picture');
}
// Attach roles
$user->roles()->attach($request['roles']);
@ -88,17 +93,18 @@ class Users extends Controller
{
if ($user->customer) {
// Show only roles with customer permission
$roles = Role::all()->reject(function($r) {
$roles = Role::all()->reject(function ($r) {
return !$r->hasPermission('read-customer-panel');
});
} else {
// Don't show roles with customer permission
$roles = Role::all()->reject(function($r) {
$roles = Role::all()->reject(function ($r) {
return $r->hasPermission('read-customer-panel');
});
}
$companies = Auth::user()->companies()->get()->sortBy('name');
foreach ($companies as $company) {
$company->setSettings();
}
@ -116,12 +122,6 @@ class Users extends Controller
*/
public function update(User $user, Request $request)
{
// Upload picture
$picture = $request->file('picture');
if ($picture && $picture->isValid()) {
$request['picture'] = $picture->store('users');
}
// Do not reset password if not entered/changed
if (empty($request['password'])) {
unset($request['password']);
@ -131,6 +131,13 @@ class Users extends Controller
// Update user
$user->update($request->input());
// Upload picture
if ($request->file('picture')) {
$media = $this->getMedia($request->file('picture'), 'users');
$user->attachMedia($media, 'picture');
}
// Sync roles
$user->roles()->sync($request['roles']);

View File

@ -3,7 +3,9 @@
namespace App\Http\Controllers\Common;
use App\Http\Controllers\Controller;
use App\Models\Common\Media;
use Storage;
use File;
class Uploads extends Controller
{
@ -14,10 +16,12 @@ class Uploads extends Controller
* @param $file
* @return boolean|Response
*/
public function get($folder, $file)
public function get($id)
{
$media = Media::find($id);
// Get file path
if (!$path = $this->getPath($folder, $file)) {
if (!$path = $this->getPath($media)) {
return false;
}
@ -31,16 +35,45 @@ class Uploads extends Controller
* @param $file
* @return boolean|Response
*/
public function download($folder, $file)
public function download($id)
{
$media = Media::find($id);
// Get file path
if (!$path = $this->getPath($folder, $file)) {
if (!$path = $this->getPath($media)) {
return false;
}
return response()->download($path);
}
/**
* Destroy the specified resource.
*
* @param $folder
* @param $file
* @return callable
*/
public function destroy($id)
{
$media = Media::find($id);
// Get file path
if (!$path = $this->getPath($media)) {
$message = trans('messages.warning.deleted', ['name' => $media->basename, 'text' => $media->basename]);
flash($message)->warning();
return back();
}
$media->delete(); //will not delete files
File::delete($path);
return back();
}
/**
* Get the full path of resource.
*
@ -48,14 +81,13 @@ class Uploads extends Controller
* @param $file
* @return boolean|string
*/
protected function getPath($folder, $file)
protected function getPath($media)
{
// Add company id
if ($folder != 'users') {
$folder = session('company_id') . '/' . $folder;
}
$path = $media->basename;
$path = $folder . '/' . $file;
if (!empty($media->directory)) {
$path = $media->directory . '/' . $media->basename;
}
if (!Storage::exists($path)) {
return false;

View File

@ -59,9 +59,14 @@ class Companies extends Controller
setting()->set('general.company_email', $request->get('company_email'));
setting()->set('general.company_address', $request->get('company_address'));
$logo_path = $this->getUploadedFilePath($request->file('company_logo'), 'settings', $company->id);
if ($logo_path) {
setting()->set('general.company_logo', $logo_path);
if ($request->file('company_logo')) {
$company_logo = $this->getMedia($request->file('company_logo'), 'settings', $company->id);
if ($company_logo) {
$company->attachMedia($company_logo, 'company_logo');
setting()->set('general.company_logo', $company_logo->id);
}
}
setting()->set('general.default_currency', $request->get('default_currency'));
@ -135,9 +140,14 @@ class Companies extends Controller
setting()->set('general.company_email', $request->get('company_email'));
setting()->set('general.company_address', $request->get('company_address'));
$logo_path = $this->getUploadedFilePath($request->file('company_logo'), 'settings', $company->id);
if ($logo_path) {
setting()->set('general.company_logo', $logo_path);
if ($request->file('company_logo')) {
$company_logo = $this->getMedia($request->file('company_logo'), 'settings', $company->id);
if ($company_logo) {
$company->attachMedia($company_logo, 'company_logo');
setting()->set('general.company_logo', $company_logo->id);
}
}
setting()->set('general.default_payment_method', 'offlinepayment.cash.1');

View File

@ -5,9 +5,11 @@ namespace App\Http\Controllers\Customers;
use App\Http\Controllers\Controller;
use App\Http\Requests\Customer\Profile as Request;
use App\Models\Auth\User;
use App\Traits\Uploads;
class Profile extends Controller
{
use Uploads;
public function index()
{
@ -42,12 +44,6 @@ class Profile extends Controller
{
$user = auth()->user();
// Upload picture
$picture = $request->file('picture');
if ($picture && $picture->isValid()) {
$request['picture'] = $picture->store('users');
}
// Do not reset password if not entered/changed
if (empty($request['password'])) {
unset($request['password']);
@ -57,6 +53,13 @@ class Profile extends Controller
// Update user
$user->update($request->input());
// Upload picture
if ($request->file('picture')) {
$media = $this->getMedia($request->file('picture'), 'users');
$user->attachMedia($media, 'picture');
}
// Update customer
$user->customer->update($request->input());

View File

@ -128,16 +128,17 @@ class Bills extends Controller
$request['amount'] = 0;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'bills');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$bill = Bill::create($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'bills');
$bill->attachMedia($media, 'attachment');
}
$taxes = [];
$tax_total = 0;
$sub_total = 0;
@ -357,13 +358,6 @@ class Bills extends Controller
$request['amount'] = 0;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'bills');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$taxes = [];
$tax_total = 0;
$sub_total = 0;
@ -460,6 +454,13 @@ class Bills extends Controller
$bill->update($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'bills');
$bill->attachMedia($media, 'attachment');
}
// Added bill total total
$bill_total = [
'company_id' => $request['company_id'],
@ -594,13 +595,6 @@ class Bills extends Controller
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'revenues');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$bill = Bill::find($request['bill_id']);
$total_amount = $bill->amount;
@ -639,6 +633,13 @@ class Bills extends Controller
$bill_payment = BillPayment::create($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'bills');
$bill_payment->attachMedia($media, 'attachment');
}
$request['status_code'] = $bill->bill_status_code;
$request['notify'] = 0;

View File

@ -77,13 +77,14 @@ class Payments extends Controller
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'payments');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$payment = Payment::create($request->input());
Payment::create($request->input());
// Upload attachment
$media = $this->getMedia($request->file('attachment'), 'payments');
if ($media) {
$payment->attachMedia($media, 'attachment');
}
$message = trans('messages.success.added', ['type' => trans_choice('general.payments', 1)]);
@ -175,14 +176,15 @@ class Payments extends Controller
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'payments');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$payment->update($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'payments');
$payment->attachMedia($media, 'attachment');
}
$message = trans('messages.success.updated', ['type' => trans_choice('general.payments', 1)]);
flash($message)->success();

View File

@ -20,6 +20,7 @@ use App\Models\Item\Item;
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
use App\Models\Setting\Tax;
use App\Models\Common\Media;
use App\Notifications\Income\Invoice as Notification;
use App\Notifications\Item\Item as ItemNotification;
use App\Traits\Currencies;
@ -137,16 +138,17 @@ class Invoices extends Controller
$request['amount'] = 0;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'invoices');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$invoice = Invoice::create($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'invoices');
$invoice->attachMedia($media, 'attachment');
}
$taxes = [];
$tax_total = 0;
$sub_total = 0;
@ -349,13 +351,6 @@ class Invoices extends Controller
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'invoices');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$taxes = [];
$tax_total = 0;
$sub_total = 0;
@ -418,6 +413,13 @@ class Invoices extends Controller
$invoice->update($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'invoices');
$invoice->attachMedia($media, 'attachment');
}
// Delete previous invoice totals
InvoiceTotal::where('invoice_id', $invoice->id)->delete();
@ -624,13 +626,6 @@ class Invoices extends Controller
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'invoices');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$invoice = Invoice::find($request['invoice_id']);
$total_amount = $invoice->amount;
@ -669,6 +664,13 @@ class Invoices extends Controller
$invoice_payment = InvoicePayment::create($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'invoices');
$invoice_payment->attachMedia($media, 'attachment');
}
$request['status_code'] = $invoice->invoice_status_code;
$request['notify'] = 0;
@ -783,18 +785,26 @@ class Invoices extends Controller
{
$logo = '';
$media_id = setting('general.company_logo');
if (setting('general.invoice_logo')) {
$file = session('company_id') . '/' . setting('general.invoice_logo');
} else {
$file = session('company_id') . '/' . setting('general.company_logo');
$media_id = setting('general.invoice_logo');
}
$path = Storage::path($file);
$media = Media::find($media_id);
if (empty($media)) {
return $logo;
}
$path = Storage::path($media->getDiskPath());
if (!is_file($path)) {
return $logo;
}
$image = Image::make($path)->encode()->getEncoded();
if (empty($image)) {
return $logo;
}

View File

@ -79,13 +79,14 @@ class Revenues extends Controller
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'revenues');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$revenue = Revenue::create($request->input());
Revenue::create($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'revenues');
$revenue->attachMedia($media, 'attachment');
}
$message = trans('messages.success.added', ['type' => trans_choice('general.revenues', 1)]);
@ -177,14 +178,15 @@ class Revenues extends Controller
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
// Upload attachment
$attachment_path = $this->getUploadedFilePath($request->file('attachment'), 'revenues');
if ($attachment_path) {
$request['attachment'] = $attachment_path;
}
$revenue->update($request->input());
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'revenues');
$revenue->attachMedia($media, 'attachment');
}
$message = trans('messages.success.updated', ['type' => trans_choice('general.revenues', 1)]);
flash($message)->success();

View File

@ -111,9 +111,6 @@ class Updates extends Controller
// Clear cache after update
Artisan::call('cache:clear');
// Update database
Artisan::call('migrate', ['--force' => true]);
event(new UpdateFinished($alias, $old, $new));
flash(trans('updates.success'))->success();

View File

@ -53,13 +53,14 @@ class Items extends Controller
*/
public function store(Request $request)
{
// Upload picture
$picture_path = $this->getUploadedFilePath($request->file('picture'), 'items');
if ($picture_path) {
$request['picture'] = $picture_path;
}
$item = Item::create($request->input());
Item::create($request->input());
// Upload picture
if ($request->file('picture')) {
$media = $this->getMedia($request->file('picture'), 'items');
$item->attachMedia($media, 'picture');
}
$message = trans('messages.success.added', ['type' => trans_choice('general.items', 1)]);
@ -137,14 +138,15 @@ class Items extends Controller
*/
public function update(Item $item, Request $request)
{
// Upload picture
$picture_path = $this->getUploadedFilePath($request->file('picture'), 'items');
if ($picture_path) {
$request['picture'] = $picture_path;
}
$item->update($request->input());
// Upload picture
if ($request->file('picture')) {
$media = $this->getMedia($request->file('picture'), 'items');
$item->attachMedia($media, 'picture');
}
$message = trans('messages.success.updated', ['type' => trans_choice('general.items', 1)]);
flash($message)->success();

View File

@ -8,6 +8,7 @@ use App\Models\Banking\Account;
use App\Models\Company\Company;
use App\Models\Setting\Currency;
use App\Models\Setting\Setting;
use App\Models\Common\Media;
use App\Models\Setting\Tax;
use App\Traits\DateTime;
use App\Traits\Uploads;
@ -26,11 +27,20 @@ class Settings extends Controller
public function edit()
{
/*$setting = Setting::all()->pluck('value', 'key');*/
$setting = Setting::all()->map(function($s) {
$setting = Setting::all()->map(function ($s) {
$s->key = str_replace('general.', '', $s->key);
return $s;
})->pluck('value', 'key');
$company_logo = $setting->pull('company_logo');
$setting['company_logo'] = Media::find($company_logo);
$invoice_logo = $setting->pull('invoice_logo');
$setting['invoice_logo'] = Media::find($invoice_logo);
$timezones = $this->getTimezones();
$accounts = Account::enabled()->pluck('name', 'id');
@ -88,7 +98,13 @@ class Settings extends Controller
{
$fields = $request->all();
$company_id = $request->get('company_id');
if (empty($company_id)) {
$company_id = session('company_id');
}
$company = Company::find($company_id);
$skip_keys = ['company_id', '_method', '_token'];
$file_keys = ['company_logo', 'invoice_logo'];
@ -102,7 +118,14 @@ class Settings extends Controller
// Process file uploads
if (in_array($key, $file_keys)) {
$value = $this->getUploadedFilePath($request->file($key), 'settings');
// Upload attachment
if ($request->file($key)) {
$media = $this->getMedia($request->file($key), 'settings');
$company->attachMedia($media, $key);
$value = $media->id;
}
// Prevent reset
if (empty($value)) {
@ -133,5 +156,4 @@ class Settings extends Controller
return redirect('settings/settings');
}
}

View File

@ -37,5 +37,7 @@ class Version113 extends Listener
$currency->save();
}
// Update database
Artisan::call('migrate', ['--force' => true]);
}
}

View File

@ -0,0 +1,93 @@
<?php
namespace App\Listeners\Updates;
use App\Events\UpdateFinished;
use App\Models\Setting\Currency;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
class Version117 extends Listener
{
const ALIAS = 'core';
const VERSION = '1.1.7';
/**
* Handle the event.
*
* @param $event
* @return void
*/
public function handle(UpdateFinished $event)
{
// Check if should listen
if (!$this->check($event)) {
return;
}
Schema::create('media', function (Blueprint $table) {
$table->increments('id');
$table->string('disk', 32);
$table->string('directory');
$table->string('filename');
$table->string('extension', 32);
$table->string('mime_type', 128);
$table->string('aggregate_type', 32);
$table->integer('size')->unsigned();
$table->timestamps();
$table->softDeletes();
$table->index(['disk', 'directory']);
$table->unique(['disk', 'directory', 'filename', 'extension']);
$table->index('aggregate_type');
});
Schema::create('mediables', function (Blueprint $table) {
$table->integer('media_id')->unsigned();
$table->string('mediable_type');
$table->integer('mediable_id')->unsigned();
$table->string('tag');
$table->integer('order')->unsigned();
$table->primary(['media_id', 'mediable_type', 'mediable_id', 'tag']);
$table->index(['mediable_id', 'mediable_type']);
$table->index('tag');
$table->index('order');
$table->foreign('media_id')->references('id')->on('media')->onDelete('cascade');
});
$migrations = [
'\App\Models\Auth\User' => 'picture',
'\App\Models\Item\Item' => 'picture',
'\App\Models\Expense\Bill' => 'attachment',
'\App\Models\Expense\BillPayment' => 'attachment',
'\App\Models\Expense\Payment' => 'attachment',
'\App\Models\Income\Invoice' => 'attachment',
'\App\Models\Income\InvoicePayment' => 'attachment',
'\App\Models\Income\Revenue' => 'attachment',
];
foreach ($migrations as $model => $name) {
if ($model != '\App\Models\Auth\User') {
$items = $model::where('company_id', '<>', '0')->get();
} else {
$items = $model::all();
}
foreach ($items as $item) {
if ($item->$name) {
$path = explode('uploads/', $item->$name);
$media = MediaUploader::importPath(config('mediable.default_disk'), $path[1]);
$item->attachMedia($media, $name);
}
}
}
// Update database
Artisan::call('migrate', ['--force' => true]);
}
}

View File

@ -11,12 +11,13 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laratrust\Traits\LaratrustUserTrait;
use Kyslik\ColumnSortable\Sortable;
use Plank\Mediable\Mediable;
use Request;
use Route;
class User extends Authenticatable
{
use Filterable, LaratrustUserTrait, Notifiable, SoftDeletes, Sortable;
use Filterable, LaratrustUserTrait, Notifiable, SoftDeletes, Sortable, Mediable;
protected $table = 'users';
@ -25,7 +26,7 @@ class User extends Authenticatable
*
* @var array
*/
protected $fillable = ['name', 'email', 'password', 'locale', 'picture', 'enabled'];
protected $fillable = ['name', 'email', 'password', 'locale', 'enabled'];
/**
* The attributes that should be hidden for arrays.
@ -87,7 +88,15 @@ class User extends Authenticatable
}
}
return $value;
if (!empty($value)) {
return $value;
}
if (!$this->hasMedia('picture')) {
return false;
}
return $this->getMedia('picture')->last();
}
/**

View File

@ -0,0 +1,14 @@
<?php
namespace App\Models\Common;
use Illuminate\Database\Eloquent\SoftDeletes;
use Plank\Mediable\Media as PMedia;
class Media extends PMedia
{
use SoftDeletes;
protected $dates = ['deleted_at'];
}

View File

@ -7,10 +7,11 @@ use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kyslik\ColumnSortable\Sortable;
use Plank\Mediable\Mediable;
class Company extends Eloquent
{
use Filterable, SoftDeletes, Sortable;
use Filterable, SoftDeletes, Sortable, Mediable;
protected $table = 'companies';
@ -229,4 +230,22 @@ class Company extends Eloquent
->orderBy('value', $direction)
->select('companies.*');
}
/**
* Get the current balance.
*
* @return string
*/
public function getCompanyLogoAttribute($value)
{
if (!empty($value)) {
return $value;
}
if (!$this->hasMedia('company_logo')) {
return false;
}
return $this->getMedia('company_logo')->last();
}
}

View File

@ -7,10 +7,11 @@ use App\Traits\Currencies;
use App\Traits\DateTime;
use Bkwld\Cloner\Cloneable;
use Sofa\Eloquence\Eloquence;
use Plank\Mediable\Mediable;
class Bill extends Model
{
use Cloneable, Currencies, DateTime, Eloquence;
use Cloneable, Currencies, DateTime, Eloquence, Mediable;
protected $table = 'bills';
@ -21,7 +22,7 @@ class Bill extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'bill_number', 'order_number', 'bill_status_code', 'billed_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'vendor_id', 'vendor_name', 'vendor_email', 'vendor_tax_number', 'vendor_phone', 'vendor_address', 'notes', 'attachment'];
protected $fillable = ['company_id', 'bill_number', 'order_number', 'bill_status_code', 'billed_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'vendor_id', 'vendor_name', 'vendor_email', 'vendor_tax_number', 'vendor_phone', 'vendor_address', 'notes'];
/**
* Sortable columns.
@ -128,4 +129,22 @@ class Bill extends Model
{
$this->attributes['currency_rate'] = (double) $value;
}
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
{
if (!empty($value)) {
return $value;
}
if (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->last();
}
}

View File

@ -5,10 +5,11 @@ namespace App\Models\Expense;
use App\Models\Model;
use App\Traits\Currencies;
use App\Traits\DateTime;
use Plank\Mediable\Mediable;
class BillPayment extends Model
{
use Currencies, DateTime;
use Currencies, DateTime, Mediable;
protected $table = 'bill_payments';
@ -19,7 +20,7 @@ class BillPayment extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'bill_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'description', 'payment_method', 'reference', 'attachment'];
protected $fillable = ['company_id', 'bill_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'description', 'payment_method', 'reference'];
public function account()
{
@ -78,4 +79,22 @@ class BillPayment extends Model
{
return $query->sum('amount');
}
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
{
if (!empty($value)) {
return $value;
}
if (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->last();
}
}

View File

@ -7,10 +7,11 @@ use App\Traits\Currencies;
use App\Traits\DateTime;
use Bkwld\Cloner\Cloneable;
use Sofa\Eloquence\Eloquence;
use Plank\Mediable\Mediable;
class Payment extends Model
{
use Cloneable, Currencies, DateTime, Eloquence;
use Cloneable, Currencies, DateTime, Eloquence, Mediable;
protected $table = 'payments';
@ -19,7 +20,7 @@ class Payment extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'vendor_id', 'description', 'category_id', 'payment_method', 'reference', 'attachment'];
protected $fillable = ['company_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'vendor_id', 'description', 'category_id', 'payment_method', 'reference'];
/**
* Sortable columns.
@ -91,4 +92,22 @@ class Payment extends Model
{
return $query->orderBy('paid_at', 'desc');
}
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
{
if (!empty($value)) {
return $value;
}
if (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->last();
}
}

View File

@ -8,13 +8,21 @@ use App\Traits\DateTime;
use App\Traits\Incomes;
use Bkwld\Cloner\Cloneable;
use Sofa\Eloquence\Eloquence;
use Plank\Mediable\Mediable;
class Invoice extends Model
{
use Cloneable, Currencies, DateTime, Eloquence, Incomes;
use Cloneable, Currencies, DateTime, Eloquence, Incomes, Mediable;
protected $table = 'invoices';
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['attachment'];
protected $dates = ['deleted_at', 'invoiced_at', 'due_at'];
/**
@ -22,7 +30,7 @@ class Invoice extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'invoice_number', 'order_number', 'invoice_status_code', 'invoiced_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'customer_id', 'customer_name', 'customer_email', 'customer_tax_number', 'customer_phone', 'customer_address', 'notes', 'attachment'];
protected $fillable = ['company_id', 'invoice_number', 'order_number', 'invoice_status_code', 'invoiced_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'customer_id', 'customer_name', 'customer_email', 'customer_tax_number', 'customer_phone', 'customer_address', 'notes'];
/**
* Sortable columns.
@ -130,4 +138,22 @@ class Invoice extends Model
{
$this->attributes['currency_rate'] = (double) $value;
}
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
{
if (!empty($value)) {
return $value;
}
if (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->last();
}
}

View File

@ -5,10 +5,11 @@ namespace App\Models\Income;
use App\Models\Model;
use App\Traits\Currencies;
use App\Traits\DateTime;
use Plank\Mediable\Mediable;
class InvoicePayment extends Model
{
use Currencies, DateTime;
use Currencies, DateTime, Mediable;
protected $table = 'invoice_payments';
@ -19,7 +20,7 @@ class InvoicePayment extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'invoice_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'description', 'payment_method', 'reference', 'attachment'];
protected $fillable = ['company_id', 'invoice_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'description', 'payment_method', 'reference'];
public function account()
{
@ -78,4 +79,22 @@ class InvoicePayment extends Model
{
return $query->sum('amount');
}
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
{
if (!empty($value)) {
return $value;
}
if (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->last();
}
}

View File

@ -7,10 +7,11 @@ use App\Traits\Currencies;
use App\Traits\DateTime;
use Bkwld\Cloner\Cloneable;
use Sofa\Eloquence\Eloquence;
use Plank\Mediable\Mediable;
class Revenue extends Model
{
use Cloneable, Currencies, DateTime, Eloquence;
use Cloneable, Currencies, DateTime, Eloquence, Mediable;
protected $table = 'revenues';
@ -19,7 +20,7 @@ class Revenue extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'customer_id', 'description', 'category_id', 'payment_method', 'reference', 'attachment'];
protected $fillable = ['company_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'customer_id', 'description', 'category_id', 'payment_method', 'reference'];
/**
* Sortable columns.
@ -97,4 +98,22 @@ class Revenue extends Model
{
return $query->orderBy('paid_at', 'desc');
}
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
{
if (!empty($value)) {
return $value;
}
if (!$this->hasMedia('attachment')) {
return false;
}
return $this->getMedia('attachment')->last();
}
}

View File

@ -6,10 +6,11 @@ use App\Models\Model;
use App\Traits\Currencies;
use Bkwld\Cloner\Cloneable;
use Sofa\Eloquence\Eloquence;
use Plank\Mediable\Mediable;
class Item extends Model
{
use Cloneable, Currencies, Eloquence;
use Cloneable, Currencies, Eloquence, Mediable;
protected $table = 'items';
@ -18,7 +19,7 @@ class Item extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'name', 'sku', 'description', 'sale_price', 'purchase_price', 'quantity', 'category_id', 'tax_id', 'picture', 'enabled'];
protected $fillable = ['company_id', 'name', 'sku', 'description', 'sale_price', 'purchase_price', 'quantity', 'category_id', 'tax_id', 'enabled'];
/**
* Sortable columns.
@ -111,4 +112,22 @@ class Item extends Model
->orderBy('name', $direction)
->select('items.*');
}
/**
* Get the current balance.
*
* @return string
*/
public function getPictureAttribute($value)
{
if (!empty($value)) {
return $value;
}
if (!$this->hasMedia('picture')) {
return false;
}
return $this->getMedia('picture')->last();
}
}

View File

@ -21,6 +21,7 @@ class EventServiceProvider extends ServiceProvider
'App\Listeners\Updates\Version110',
'App\Listeners\Updates\Version112',
'App\Listeners\Updates\Version113',
'App\Listeners\Updates\Version116',
],
'Illuminate\Auth\Events\Login' => [
'App\Listeners\Auth\Login',

View File

@ -44,7 +44,7 @@ class FormServiceProvider extends ServiceProvider
]);
Form::component('fileGroup', 'partials.form.file_group', [
'name', 'text', 'attributes' => [], 'col' => 'col-md-6',
'name', 'text', 'attributes' => [], 'value' => null, 'col' => 'col-md-6',
]);
Form::component('deleteButton', 'partials.form.delete_button', [

View File

@ -2,6 +2,8 @@
namespace App\Traits;
use MediaUploader;
trait Uploads
{
@ -27,4 +29,21 @@ trait Uploads
return $path;
}
}
public function getMedia($file, $folder = 'settings', $company_id = null)
{
$path = '';
if (!$file || !$file->isValid()) {
return $path;
}
if (!$company_id) {
$company_id = session('company_id');
}
$path = $company_id . '/' . $folder;
return MediaUploader::fromSource($file)->toDirectory($path)->upload();
}
}