2016-05-01 13:31:10 +02:00
|
|
|
<?php namespace App\Ninja\Repositories;
|
2016-03-23 03:23:45 +01:00
|
|
|
|
|
|
|
use DB;
|
|
|
|
use Utils;
|
|
|
|
use Response;
|
|
|
|
use App\Models\Document;
|
|
|
|
use App\Ninja\Repositories\BaseRepository;
|
2016-03-23 20:41:05 +01:00
|
|
|
use Intervention\Image\ImageManager;
|
2016-03-23 03:23:45 +01:00
|
|
|
use Session;
|
2016-03-24 16:58:30 +01:00
|
|
|
use Form;
|
2016-03-23 03:23:45 +01:00
|
|
|
|
|
|
|
class DocumentRepository extends BaseRepository
|
|
|
|
{
|
|
|
|
// Expenses
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return 'App\Models\Document';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function all()
|
|
|
|
{
|
|
|
|
return Document::scope()
|
|
|
|
->with('user')
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function find()
|
|
|
|
{
|
|
|
|
$accountid = \Auth::user()->account_id;
|
|
|
|
$query = DB::table('clients')
|
|
|
|
->join('accounts', 'accounts.id', '=', 'clients.account_id')
|
|
|
|
->leftjoin('clients', 'clients.id', '=', 'clients.client_id')
|
|
|
|
/*->leftJoin('expenses', 'expenses.id', '=', 'clients.expense_id')
|
|
|
|
->leftJoin('invoices', 'invoices.id', '=', 'clients.invoice_id')*/
|
|
|
|
->where('documents.account_id', '=', $accountid)
|
|
|
|
/*->where('vendors.deleted_at', '=', null)
|
|
|
|
->where('clients.deleted_at', '=', null)*/
|
|
|
|
->select(
|
|
|
|
'documents.account_id',
|
|
|
|
'documents.path',
|
|
|
|
'documents.deleted_at',
|
|
|
|
'documents.size',
|
|
|
|
'documents.width',
|
|
|
|
'documents.height',
|
|
|
|
'documents.id',
|
|
|
|
'documents.is_deleted',
|
|
|
|
'documents.public_id',
|
|
|
|
'documents.invoice_id',
|
|
|
|
'documents.expense_id',
|
|
|
|
'documents.user_id',
|
|
|
|
'invoices.public_id as invoice_public_id',
|
|
|
|
'invoices.user_id as invoice_user_id',
|
|
|
|
'expenses.public_id as expense_public_id',
|
|
|
|
'expenses.user_id as expense_user_id'
|
|
|
|
);
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2016-06-01 11:39:42 +02:00
|
|
|
public function upload($data, &$doc_array=null)
|
2016-03-23 03:23:45 +01:00
|
|
|
{
|
2016-06-01 11:39:42 +02:00
|
|
|
$uploaded = $data['file'];
|
2016-03-24 23:15:52 +01:00
|
|
|
$extension = strtolower($uploaded->getClientOriginalExtension());
|
2016-03-24 16:33:28 +01:00
|
|
|
if(empty(Document::$types[$extension]) && !empty(Document::$extraExtensions[$extension])){
|
2016-05-26 17:28:13 +02:00
|
|
|
$documentType = Document::$extraExtensions[$extension];
|
2016-03-24 16:33:28 +01:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
$documentType = $extension;
|
|
|
|
}
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-24 16:33:28 +01:00
|
|
|
if(empty(Document::$types[$documentType])){
|
2016-03-24 23:15:52 +01:00
|
|
|
return 'Unsupported file type';
|
2016-03-23 03:23:45 +01:00
|
|
|
}
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-24 16:33:28 +01:00
|
|
|
$documentTypeData = Document::$types[$documentType];
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-23 03:23:45 +01:00
|
|
|
$filePath = $uploaded->path();
|
|
|
|
$name = $uploaded->getClientOriginalName();
|
2016-03-24 00:52:51 +01:00
|
|
|
$size = filesize($filePath);
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-24 00:52:51 +01:00
|
|
|
if($size/1000 > MAX_DOCUMENT_SIZE){
|
2016-03-24 20:13:54 +01:00
|
|
|
return 'File too large';
|
2016-03-23 03:23:45 +01:00
|
|
|
}
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-06-01 11:39:42 +02:00
|
|
|
// don't allow a document to be linked to both an invoice and an expense
|
|
|
|
if (array_get($data, 'invoice_id') && array_get($data, 'expense_id')) {
|
|
|
|
unset($data['expense_id']);
|
|
|
|
}
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-23 20:41:05 +01:00
|
|
|
$hash = sha1_file($filePath);
|
2016-03-24 16:33:28 +01:00
|
|
|
$filename = \Auth::user()->account->account_key.'/'.$hash.'.'.$documentType;
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-23 03:23:45 +01:00
|
|
|
$document = Document::createNew();
|
2016-06-01 11:39:42 +02:00
|
|
|
$document->fill($data);
|
|
|
|
|
2016-03-23 03:23:45 +01:00
|
|
|
$disk = $document->getDisk();
|
|
|
|
if(!$disk->exists($filename)){// Have we already stored the same file
|
2016-03-24 00:52:51 +01:00
|
|
|
$stream = fopen($filePath, 'r');
|
2016-03-25 00:00:08 +01:00
|
|
|
$disk->getDriver()->putStream($filename, $stream, ['mimetype'=>$documentTypeData['mime']]);
|
2016-03-24 00:52:51 +01:00
|
|
|
fclose($stream);
|
2016-03-23 20:41:05 +01:00
|
|
|
}
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-23 20:41:05 +01:00
|
|
|
// This is an image; check if we need to create a preview
|
2016-03-24 16:33:28 +01:00
|
|
|
if(in_array($documentType, array('jpeg','png','gif','bmp','tiff','psd'))){
|
2016-03-23 20:41:05 +01:00
|
|
|
$makePreview = false;
|
|
|
|
$imageSize = getimagesize($filePath);
|
2016-03-23 23:40:42 +01:00
|
|
|
$width = $imageSize[0];
|
|
|
|
$height = $imageSize[1];
|
2016-03-23 20:41:05 +01:00
|
|
|
$imgManagerConfig = array();
|
2016-03-24 16:33:28 +01:00
|
|
|
if(in_array($documentType, array('gif','bmp','tiff','psd'))){
|
2016-03-23 20:41:05 +01:00
|
|
|
// Needs to be converted
|
|
|
|
$makePreview = true;
|
2016-03-24 00:52:51 +01:00
|
|
|
} else if($width > DOCUMENT_PREVIEW_SIZE || $height > DOCUMENT_PREVIEW_SIZE){
|
2016-05-26 17:28:13 +02:00
|
|
|
$makePreview = true;
|
2016-03-23 20:41:05 +01:00
|
|
|
}
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-24 16:33:28 +01:00
|
|
|
if(in_array($documentType,array('bmp','tiff','psd'))){
|
2016-03-23 20:41:05 +01:00
|
|
|
if(!class_exists('Imagick')){
|
|
|
|
// Cant't read this
|
|
|
|
$makePreview = false;
|
|
|
|
} else {
|
|
|
|
$imgManagerConfig['driver'] = 'imagick';
|
2016-05-26 17:28:13 +02:00
|
|
|
}
|
2016-03-23 20:41:05 +01:00
|
|
|
}
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-23 20:41:05 +01:00
|
|
|
if($makePreview){
|
2016-03-24 16:33:28 +01:00
|
|
|
$previewType = 'jpeg';
|
|
|
|
if(in_array($documentType, array('png','gif','tiff','psd'))){
|
2016-03-23 20:41:05 +01:00
|
|
|
// Has transparency
|
|
|
|
$previewType = 'png';
|
|
|
|
}
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-24 16:33:28 +01:00
|
|
|
$document->preview = \Auth::user()->account->account_key.'/'.$hash.'.'.$documentType.'.x'.DOCUMENT_PREVIEW_SIZE.'.'.$previewType;
|
2016-03-23 20:41:05 +01:00
|
|
|
if(!$disk->exists($document->preview)){
|
|
|
|
// We haven't created a preview yet
|
|
|
|
$imgManager = new ImageManager($imgManagerConfig);
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-23 20:41:05 +01:00
|
|
|
$img = $imgManager->make($filePath);
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-23 23:40:42 +01:00
|
|
|
if($width <= DOCUMENT_PREVIEW_SIZE && $height <= DOCUMENT_PREVIEW_SIZE){
|
|
|
|
$previewWidth = $width;
|
|
|
|
$previewHeight = $height;
|
|
|
|
} else if($width > $height) {
|
|
|
|
$previewWidth = DOCUMENT_PREVIEW_SIZE;
|
|
|
|
$previewHeight = $height * DOCUMENT_PREVIEW_SIZE / $width;
|
|
|
|
} else {
|
|
|
|
$previewHeight = DOCUMENT_PREVIEW_SIZE;
|
|
|
|
$previewWidth = $width * DOCUMENT_PREVIEW_SIZE / $height;
|
|
|
|
}
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-23 23:40:42 +01:00
|
|
|
$img->resize($previewWidth, $previewHeight);
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-23 20:41:05 +01:00
|
|
|
$previewContent = (string) $img->encode($previewType);
|
|
|
|
$disk->put($document->preview, $previewContent);
|
2016-03-23 23:40:42 +01:00
|
|
|
$base64 = base64_encode($previewContent);
|
2016-03-23 20:41:05 +01:00
|
|
|
}
|
2016-03-23 23:40:42 +01:00
|
|
|
else{
|
|
|
|
$base64 = base64_encode($disk->get($document->preview));
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
$base64 = base64_encode(file_get_contents($filePath));
|
2016-05-26 17:28:13 +02:00
|
|
|
}
|
2016-03-23 03:23:45 +01:00
|
|
|
}
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-23 03:23:45 +01:00
|
|
|
$document->path = $filename;
|
|
|
|
$document->type = $documentType;
|
2016-03-24 00:52:51 +01:00
|
|
|
$document->size = $size;
|
2016-03-24 03:41:19 +01:00
|
|
|
$document->hash = $hash;
|
2016-03-23 03:23:45 +01:00
|
|
|
$document->name = substr($name, -255);
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-23 20:41:05 +01:00
|
|
|
if(!empty($imageSize)){
|
|
|
|
$document->width = $imageSize[0];
|
|
|
|
$document->height = $imageSize[1];
|
2016-03-23 03:23:45 +01:00
|
|
|
}
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-23 03:23:45 +01:00
|
|
|
$document->save();
|
2016-03-23 23:40:42 +01:00
|
|
|
$doc_array = $document->toArray();
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-23 23:40:42 +01:00
|
|
|
if(!empty($base64)){
|
2016-03-24 16:33:28 +01:00
|
|
|
$mime = Document::$types[!empty($previewType)?$previewType:$documentType]['mime'];
|
2016-03-23 23:40:42 +01:00
|
|
|
$doc_array['base64'] = 'data:'.$mime.';base64,'.$base64;
|
|
|
|
}
|
2016-03-23 03:23:45 +01:00
|
|
|
|
2016-03-24 20:13:54 +01:00
|
|
|
return $document;
|
2016-03-23 03:23:45 +01:00
|
|
|
}
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-24 16:58:30 +01:00
|
|
|
public function getClientDatatable($contactId, $entityType, $search)
|
|
|
|
{
|
2016-05-26 17:28:13 +02:00
|
|
|
|
2016-03-24 23:15:52 +01:00
|
|
|
$query = DB::table('invitations')
|
2016-03-24 16:58:30 +01:00
|
|
|
->join('accounts', 'accounts.id', '=', 'invitations.account_id')
|
|
|
|
->join('invoices', 'invoices.id', '=', 'invitations.invoice_id')
|
2016-03-29 16:30:06 +02:00
|
|
|
->join('documents', 'documents.invoice_id', '=', 'invitations.invoice_id')
|
2016-03-24 16:58:30 +01:00
|
|
|
->join('clients', 'clients.id', '=', 'invoices.client_id')
|
|
|
|
->where('invitations.contact_id', '=', $contactId)
|
|
|
|
->where('invitations.deleted_at', '=', null)
|
|
|
|
->where('invoices.is_deleted', '=', false)
|
|
|
|
->where('clients.deleted_at', '=', null)
|
|
|
|
->where('invoices.is_recurring', '=', false)
|
|
|
|
// This needs to be a setting to also hide the activity on the dashboard page
|
2016-05-26 17:28:13 +02:00
|
|
|
//->where('invoices.invoice_status_id', '>=', INVOICE_STATUS_SENT)
|
2016-03-24 16:58:30 +01:00
|
|
|
->select(
|
|
|
|
'invitations.invitation_key',
|
|
|
|
'invoices.invoice_number',
|
|
|
|
'documents.name',
|
|
|
|
'documents.public_id',
|
|
|
|
'documents.created_at',
|
|
|
|
'documents.size'
|
|
|
|
);
|
|
|
|
|
|
|
|
$table = \Datatable::query($query)
|
|
|
|
->addColumn('invoice_number', function ($model) {
|
|
|
|
return link_to(
|
2016-05-26 17:28:13 +02:00
|
|
|
'/view/'.$model->invitation_key,
|
2016-03-24 16:58:30 +01:00
|
|
|
$model->invoice_number
|
2016-05-26 17:28:13 +02:00
|
|
|
)->toHtml();
|
2016-03-24 16:58:30 +01:00
|
|
|
})
|
|
|
|
->addColumn('name', function ($model) {
|
|
|
|
return link_to(
|
2016-05-26 17:28:13 +02:00
|
|
|
'/client/documents/'.$model->invitation_key.'/'.$model->public_id.'/'.$model->name,
|
2016-03-24 16:58:30 +01:00
|
|
|
$model->name,
|
|
|
|
['target'=>'_blank']
|
2016-05-26 17:28:13 +02:00
|
|
|
)->toHtml();
|
2016-03-24 16:58:30 +01:00
|
|
|
})
|
|
|
|
->addColumn('document_date', function ($model) {
|
2016-05-26 17:28:13 +02:00
|
|
|
return Utils::dateToString($model->created_at);
|
2016-03-24 16:58:30 +01:00
|
|
|
})
|
|
|
|
->addColumn('document_size', function ($model) {
|
2016-05-26 17:28:13 +02:00
|
|
|
return Form::human_filesize($model->size);
|
2016-03-24 16:58:30 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return $table->make();
|
|
|
|
}
|
2016-03-23 03:23:45 +01:00
|
|
|
}
|