mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Support saving invoice_id and expense_id with the document
This commit is contained in:
parent
51d9b2b427
commit
17eb2a7a79
@ -33,7 +33,7 @@ class DocumentAPIController extends BaseAPIController
|
||||
|
||||
public function store(CreateDocumentRequest $request)
|
||||
{
|
||||
$document = $this->documentRepo->upload($request->file);
|
||||
$document = $this->documentRepo->upload($request->all());
|
||||
|
||||
return $this->itemResponse($document);
|
||||
}
|
||||
|
@ -102,7 +102,7 @@ class DocumentController extends BaseController
|
||||
|
||||
public function postUpload(CreateDocumentRequest $request)
|
||||
{
|
||||
$result = $this->documentRepo->upload($request->file, $doc_array);
|
||||
$result = $this->documentRepo->upload($request->all(), $doc_array);
|
||||
|
||||
if(is_string($result)){
|
||||
return Response::json([
|
||||
|
@ -1,7 +1,15 @@
|
||||
<?php namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Expense;
|
||||
|
||||
class CreateDocumentRequest extends DocumentRequest
|
||||
{
|
||||
protected $autoload = [
|
||||
ENTITY_INVOICE,
|
||||
ENTITY_EXPENSE,
|
||||
];
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
@ -9,7 +17,19 @@ class CreateDocumentRequest extends DocumentRequest
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return $this->user()->can('create', ENTITY_DOCUMENT) && $this->user()->hasFeature(FEATURE_DOCUMENTS);
|
||||
if ( ! $this->user()->hasFeature(FEATURE_DOCUMENTS)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->invoice && $this->user()->cannot('edit', $this->invoice)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->expense && $this->user()->cannot('edit', $this->expense)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->user()->can('create', ENTITY_DOCUMENT);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -24,21 +44,4 @@ class CreateDocumentRequest extends DocumentRequest
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize input before validation.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
/*
|
||||
public function sanitize()
|
||||
{
|
||||
$input = $this->all();
|
||||
|
||||
$input['phone'] = 'test123';
|
||||
|
||||
$this->replace($input);
|
||||
|
||||
return $this->all();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@ -5,6 +5,9 @@ use Illuminate\Foundation\Http\FormRequest;
|
||||
// https://laracasts.com/discuss/channels/general-discussion/laravel-5-modify-input-before-validation/replies/34366
|
||||
abstract class Request extends FormRequest {
|
||||
|
||||
// populate in subclass to auto load record
|
||||
protected $autoload = [];
|
||||
|
||||
/**
|
||||
* Validate the input.
|
||||
*
|
||||
@ -25,11 +28,24 @@ abstract class Request extends FormRequest {
|
||||
*/
|
||||
protected function sanitizeInput()
|
||||
{
|
||||
if (method_exists($this, 'sanitize'))
|
||||
{
|
||||
return $this->container->call([$this, 'sanitize']);
|
||||
if (method_exists($this, 'sanitize')) {
|
||||
$input = $this->container->call([$this, 'sanitize']);
|
||||
} else {
|
||||
$input = $this->all();
|
||||
}
|
||||
|
||||
// autoload referenced entities
|
||||
foreach ($this->autoload as $entityType) {
|
||||
if ($id = $this->input("{$entityType}_public_id") ?: $this->input("{$entityType}_id")) {
|
||||
$class = "App\\Models\\" . ucwords($entityType);
|
||||
$entity = $class::scope($id)->firstOrFail();
|
||||
$input[$entityType] = $entity;
|
||||
$input[$entityType . '_id'] = $entity->id;
|
||||
}
|
||||
}
|
||||
|
||||
$this->replace($input);
|
||||
|
||||
return $this->all();
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,11 @@ use Auth;
|
||||
|
||||
class Document extends EntityModel
|
||||
{
|
||||
protected $fillable = [
|
||||
'invoice_id',
|
||||
'expense_id',
|
||||
];
|
||||
|
||||
public static $extraExtensions = array(
|
||||
'jpg' => 'jpeg',
|
||||
'tif' => 'tiff',
|
||||
|
@ -57,8 +57,9 @@ class DocumentRepository extends BaseRepository
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function upload($uploaded, &$doc_array=null)
|
||||
public function upload($data, &$doc_array=null)
|
||||
{
|
||||
$uploaded = $data['file'];
|
||||
$extension = strtolower($uploaded->getClientOriginalExtension());
|
||||
if(empty(Document::$types[$extension]) && !empty(Document::$extraExtensions[$extension])){
|
||||
$documentType = Document::$extraExtensions[$extension];
|
||||
@ -81,12 +82,17 @@ class DocumentRepository extends BaseRepository
|
||||
return 'File too large';
|
||||
}
|
||||
|
||||
|
||||
// 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']);
|
||||
}
|
||||
|
||||
$hash = sha1_file($filePath);
|
||||
$filename = \Auth::user()->account->account_key.'/'.$hash.'.'.$documentType;
|
||||
|
||||
$document = Document::createNew();
|
||||
$document->fill($data);
|
||||
|
||||
$disk = $document->getDisk();
|
||||
if(!$disk->exists($filename)){// Have we already stored the same file
|
||||
$stream = fopen($filePath, 'r');
|
||||
|
Loading…
x
Reference in New Issue
Block a user