diff --git a/.env.example b/.env.example index 8ae5c8ae153a..4ca1bb2d3422 100644 --- a/.env.example +++ b/.env.example @@ -20,6 +20,6 @@ MAIL_FROM_ADDRESS MAIL_FROM_NAME MAIL_PASSWORD -#PHANTOMJS_CLOUD_KEY='a-demo-key-with-low-quota-per-ip-address' +PHANTOMJS_CLOUD_KEY='a-demo-key-with-low-quota-per-ip-address' -LOG=single +LOG=single \ No newline at end of file diff --git a/app/Http/Controllers/AppController.php b/app/Http/Controllers/AppController.php index 41e20efe0b02..ae5a7a1256a7 100644 --- a/app/Http/Controllers/AppController.php +++ b/app/Http/Controllers/AppController.php @@ -94,7 +94,7 @@ class AppController extends BaseController "MAIL_USERNAME={$mail['username']}\n". "MAIL_FROM_NAME={$mail['from']['name']}\n". "MAIL_PASSWORD={$mail['password']}\n\n". - "#PHANTOMJS_CLOUD_KEY='a-demo-key-with-low-quota-per-ip-address'"; + "PHANTOMJS_CLOUD_KEY='a-demo-key-with-low-quota-per-ip-address'"; // Write Config Settings $fp = fopen(base_path()."/.env", 'w'); diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 89ef59bd9189..54d74258c771 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -165,9 +165,7 @@ class InvoiceController extends BaseController } else { $invoice->invoice_design->javascript = $invoice->invoice_design->pdfmake; } - - $contact = $invitation->contact; - $contact->setVisible([ + $contact = $invitation->contact; $contact->setVisible([ 'first_name', 'last_name', 'email', @@ -306,7 +304,6 @@ class InvoiceController extends BaseController 'entityType' => $entityType, 'showBreadcrumbs' => $clone, 'invoice' => $invoice, - 'data' => false, 'method' => $method, 'invitationContactIds' => $contactIds, 'url' => $url, @@ -349,20 +346,17 @@ class InvoiceController extends BaseController public function create($clientPublicId = 0, $isRecurring = false) { - $client = null; + $account = Auth::user()->account; + $clientId = null; if ($clientPublicId) { - $client = Client::scope($clientPublicId)->firstOrFail(); + $clientId = Client::getPrivateId($clientPublicId); } + $entityType = $isRecurring ? ENTITY_RECURRING_INVOICE : ENTITY_INVOICE; + $invoice = $account->createInvoice($entityType, $clientId); - $invoice = Invoice::createNew(); - $invoice->client = $client; - $invoice->is_recurring = $isRecurring; - $invoice->initialize(); - $data = [ 'entityType' => $invoice->getEntityType(), 'invoice' => $invoice, - 'data' => Input::old('data'), 'method' => 'POST', 'url' => 'invoices', 'title' => trans('texts.new_invoice'), @@ -391,6 +385,7 @@ class InvoiceController extends BaseController } return [ + 'data' => Input::old('data'), 'account' => Auth::user()->account->load('country'), 'products' => Product::scope()->with('default_tax_rate')->orderBy('id')->get(), 'countries' => Cache::get('countries'), @@ -440,9 +435,8 @@ class InvoiceController extends BaseController if ($errors = $this->invoiceRepo->getErrors($input->invoice)) { Session::flash('error', trans('texts.invoice_error')); - - return Redirect::to("{$entityType}s/create") - ->withInput()->withErrors($errors); + $url = "{$entityType}s/" . ($publicId ?: 'create'); + return Redirect::to($url)->withInput()->withErrors($errors); } else { $invoice = $this->saveInvoice($publicId, $input, $entityType); $url = "{$entityType}s/".$invoice->public_id.'/edit'; diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index e987fc458e2c..f78a693d66b6 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -81,15 +81,12 @@ class QuoteController extends BaseController return Redirect::to('/invoices/create'); } - $client = null; + $account = Auth::user()->account; + $clientId = null; if ($clientPublicId) { - $client = Client::scope($clientPublicId)->firstOrFail(); + $clientId = Client::getPrivateId($clientPublicId); } - - $invoice = Invoice::createNew(); - $invoice->client = $client; - $invoice->is_quote = true; - $invoice->initialize(); + $invoice = $account->createInvoice(ENTITY_QUOTE, $clientId); $data = [ 'entityType' => $invoice->getEntityType(), diff --git a/app/Models/Account.php b/app/Models/Account.php index ea0ac297d897..904f03896cb3 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -243,6 +243,38 @@ class Account extends Eloquent return $height; } + public function createInvoice($entityType, $clientId = null) + { + $invoice = Invoice::createNew(); + + $invoice->invoice_date = Utils::today(); + $invoice->start_date = Utils::today(); + $invoice->invoice_design_id = $this->invoice_design_id; + $invoice->client_id = $clientId; + + if ($entityType === ENTITY_RECURRING_INVOICE) { + $invoice->invoice_number = microtime(true); + $invoice->is_recurring = true; + } else { + if ($entityType == ENTITY_QUOTE) { + $invoice->is_quote = true; + } + + if ($this->hasClientNumberPattern($invoice) && !$client) { + // do nothing, we don't yet know the value + } else { + $invoice->invoice_number = $this->getNextInvoiceNumber($invoice); + } + } + + if (!$clientId) { + $invoice->client = Client::createNew(); + $invoice->client->public_id = 0; + } + + return $invoice; + } + public function hasNumberPattern($isQuote) { return $isQuote ? ($this->quote_number_pattern ? true : false) : ($this->invoice_number_pattern ? true : false); @@ -371,13 +403,6 @@ class Account extends Eloquent $this->save(); } - public function getLocale() - { - $language = Language::where('id', '=', $this->account->language_id)->first(); - - return $language->locale; - } - public function loadLocalizationSettings($client = false) { $this->load('timezone', 'date_format', 'datetime_format', 'language'); diff --git a/app/Models/EntityModel.php b/app/Models/EntityModel.php index 4cef225a2d2e..f9a375dc443c 100644 --- a/app/Models/EntityModel.php +++ b/app/Models/EntityModel.php @@ -9,14 +9,14 @@ class EntityModel extends Eloquent public $timestamps = true; protected $hidden = ['id']; - public static function createNew($parent = false) + public static function createNew($context = null) { $className = get_called_class(); $entity = new $className(); - if ($parent) { - $entity->user_id = $parent instanceof User ? $parent->id : $parent->user_id; - $entity->account_id = $parent->account_id; + if ($context) { + $entity->user_id = $context instanceof User ? $context->id : $context->user_id; + $entity->account_id = $context->account_id; } elseif (Auth::check()) { $entity->user_id = Auth::user()->id; $entity->account_id = Auth::user()->account_id; diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 824b90b3a5c4..7a0a8c9535e5 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -6,7 +6,10 @@ use Illuminate\Database\Eloquent\SoftDeletes; class Invoice extends EntityModel { - use SoftDeletes; + use SoftDeletes { + SoftDeletes::trashed as parentTrashed; + } + protected $dates = ['deleted_at']; protected $casts = [ @@ -23,40 +26,14 @@ class Invoice extends EntityModel 'year', 'date:', ]; - - public function initialize() + + public function trashed() { - $account = $this->account; - - $this->invoice_date = Utils::today(); - $this->start_date = Utils::today(); - $this->invoice_design_id = $account->invoice_design_id; - - if (!$this->invoice_number) { - if ($this->is_recurring) { - $this->invoice_number = microtime(true); - } else { - if ($account->hasClientNumberPattern($this) && !$this->client) { - // do nothing, we don't yet know the value - } else { - $this->invoice_number = $account->getNextInvoiceNumber($this); - } - } - } - - if (!$this->client) { - $this->client = Client::createNew($this); - $this->client->public_id = 0; - } - } - - public function isTrashed() - { - if ($this->client && $this->client->isTrashed()) { + if ($this->client && $this->client->trashed()) { return true; } - return parent::isTrashed(); + return self::parentTrashed(); } public function account() diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index 92564cb13206..412ea14faa22 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -250,20 +250,17 @@ class InvoiceRepository public function save($publicId, $data, $entityType) { + $account = \Auth::user()->account; + if ($publicId) { $invoice = Invoice::scope($publicId)->firstOrFail(); } else { - $invoice = Invoice::createNew(); - $invoice->client_id = $data['client_id']; - $invoice->is_recurring = $data['is_recurring'] ? true : false; - if ($entityType == ENTITY_QUOTE) { - $invoice->is_quote = true; + if ($data['is_recurring']) { + $entityType = ENTITY_RECURRING_INVOICE; } - $invoice->initialize(); + $invoice = $account->createInvoice($entityType, $data['client_id']); } - $account = \Auth::user()->account; - if ((isset($data['set_default_terms']) && $data['set_default_terms']) || (isset($data['set_default_footer']) && $data['set_default_footer'])) { if (isset($data['set_default_terms']) && $data['set_default_terms']) { diff --git a/resources/views/invoices/edit.blade.php b/resources/views/invoices/edit.blade.php index cf80c3aed3ae..9b0b56ecddbb 100644 --- a/resources/views/invoices/edit.blade.php +++ b/resources/views/invoices/edit.blade.php @@ -4,6 +4,14 @@ @parent + + @stop @section('content') @@ -21,6 +29,7 @@ {!! Former::open($url)->method($method)->addClass('warn-on-exit')->rules(array( 'client' => 'required', + 'invoice_number' => 'required', 'product_key' => 'max:255' )) !!} @@ -34,7 +43,7 @@
- @if ($invoice->id) + @if ($invoice->id || $data)
@@ -57,7 +66,7 @@
- @if ($invoice && $invoice->id) + @if ($invoice->id || $data)
@endif @@ -107,9 +116,9 @@ @if ($entityType == ENTITY_INVOICE)
- @if ($invoice && $invoice->recurring_invoice) + @if ($invoice->recurring_invoice) {!! trans('texts.created_by_invoice', ['invoice' => link_to('/invoices/'.$invoice->recurring_invoice->public_id, trans('texts.recurring_invoice'))]) !!} - @elseif ($invoice) + @elseif ($invoice->id) @if (isset($lastSent) && $lastSent) {!! trans('texts.last_sent_on', ['date' => link_to('/invoices/'.$lastSent->public_id, $invoice->last_sent_date, ['id' => 'lastSent'])]) !!}
@endif @@ -149,7 +158,7 @@
-
+
@@ -341,7 +350,7 @@ {!! Former::text('data')->data_bind("value: ko.mapping.toJSON(model)") !!} {!! Former::text('pdfupload') !!} - @if ($invoice && $invoice->id) + @if ($invoice->id) {!! Former::populateField('id', $invoice->public_id) !!} {!! Former::text('id') !!} @endif @@ -361,7 +370,7 @@ {!! Button::success(trans("texts.save_{$entityType}"))->withAttributes(array('id' => 'saveButton', 'onclick' => 'onSaveClick()'))->appendIcon(Icon::create('floppy-disk')) !!} {!! Button::info(trans("texts.email_{$entityType}"))->withAttributes(array('id' => 'emailButton', 'onclick' => 'onEmailClick()'))->appendIcon(Icon::create('send')) !!} - @if ($invoice && $invoice->id) + @if ($invoice->id) {!! DropdownButton::normal(trans('texts.more_actions')) ->withContents($actions) ->dropup() !!} @@ -968,9 +977,12 @@ if (event.target.type == 'textarea') { return; } - event.preventDefault(); + event.preventDefault(); - submitAction(''); + @if($invoice->trashed()) + return; + @endif + submitAction(''); return false; } } @@ -1024,7 +1036,7 @@ } function showLearnMore() { - $('#recurringModal').modal('show'); + $('#recurringModal').modal('show'); } function setInvoiceNumber(client) { @@ -1037,16 +1049,6 @@ model.invoice().invoice_number(number); } - function padToFour(number) { - if (number<=9999) { number = ("000"+number).slice(-4); } - return number; - } - - function padToThree(number) { - if (number<=999) { number = ("00"+number).slice(-3); } - return number; - } - @stop \ No newline at end of file diff --git a/resources/views/invoices/knockout.blade.php b/resources/views/invoices/knockout.blade.php index 1ff3d7998f8a..10ebcee35b58 100644 --- a/resources/views/invoices/knockout.blade.php +++ b/resources/views/invoices/knockout.blade.php @@ -203,11 +203,11 @@ function InvoiceModel(data) { self.frequency_id = ko.observable(4); // default to monthly self.terms = ko.observable(''); self.default_terms = ko.observable(account.invoice_terms); - self.terms_placeholder = ko.observable({{ !$invoice && $account->invoice_terms ? 'account.invoice_terms' : false}}); + self.terms_placeholder = ko.observable({{ !$invoice->id && $account->invoice_terms ? 'account.invoice_terms' : false}}); self.set_default_terms = ko.observable(false); self.invoice_footer = ko.observable(''); self.default_footer = ko.observable(account.invoice_footer); - self.footer_placeholder = ko.observable({{ !$invoice && $account->invoice_footer ? 'account.invoice_footer' : false}}); + self.footer_placeholder = ko.observable({{ !$invoice->id && $account->invoice_footer ? 'account.invoice_footer' : false}}); self.set_default_footer = ko.observable(false); self.public_notes = ko.observable(''); self.po_number = ko.observable('');