diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index cd1c47375a..193b376364 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -209,7 +209,7 @@ class InvoiceController extends BaseController { $client = Client::find($request->input('client_id')); - $invoice = $this->invoice_repo->save($request->all(), $client->setInvoiceDefaults()); + $invoice = $this->invoice_repo->save($request->all(), InvoiceFactory::create(auth()->user()->company(), auth()->user()->id)); $invoice = StoreInvoice::dispatchNow($invoice, $request->all(), $invoice->company);//todo potentially this may return mixed ie PDF/$invoice... need to revisit when we implement UI diff --git a/app/Models/Client.php b/app/Models/Client.php index 756461d93b..752fe7e2d4 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -469,28 +469,28 @@ class Client extends BaseModel implements HasLocalePreference public function setInvoiceDefaults() :Invoice { - $invoice_factory = InvoiceFactory::create($this->company_id, auth()->user()->id); - $invoice_factory->terms = $this->getSetting('invoice_terms'); - $invoice_factory->footer = $this->getSetting('invoice_footer'); - $invoice_factory->public_notes = isset($this->public_notes) ? $this->public_notes : ''; - return $invoice_factory; + $invoice->terms = $this->getSetting('invoice_terms'); + $invoice->footer = $this->getSetting('invoice_footer'); + $invoice->public_notes = isset($this->public_notes) ? $this->public_notes : ''; + + return $invoice; } public function setQuoteDefaults() :Quote { - $quote_factory = QuoteFactory::create($this->company_id, auth()->user()->id); - $quote_factory->terms = $this->getSetting('quote_terms'); - $quote_factory->footer = $this->getSetting('quote_footer'); - $quote_factory->public_notes = isset($this->public_notes) ? $this->public_notes : ''; - return $quote_factory; + $quote->terms = $this->getSetting('quote_terms'); + $quote->footer = $this->getSetting('quote_footer'); + $quote->public_notes = isset($this->public_notes) ? $this->public_notes : ''; + + return $quote; } public function setCreditDefaults() :Credit { - $credit_factory = CreditFactory::create($this->company_id, auth()->user()->id); - $credit_factory->terms = $this->getSetting('credit_terms'); - $credit_factory->footer = $this->getSetting('credit_footer'); - $credit_factory->public_notes = isset($this->public_notes) ? $this->public_notes : ''; - return $credit_factory; + $credit->terms = $this->getSetting('credit_terms'); + $credit->footer = $this->getSetting('credit_footer'); + $credit->public_notes = isset($this->public_notes) ? $this->public_notes : ''; + + return $credit; } } diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index e8a2f2331e..79ee175b4b 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -188,15 +188,23 @@ class BaseRepository { $class = new ReflectionClass($model); + $client = Client::find($data['client_id']); + $state = []; $resource = explode('\\', $class->name)[2]; /** This will extract 'Invoice' from App\Models\Invoice */ $lcfirst_resource_id = lcfirst($resource) . '_id'; + //if new, set defaults! + if(!$model->id) { + $methodName = "set" . $resource . "Defaults"; + $model = $client->{$methodName}(); + } + + if ($class->name == Invoice::class || $class->name == Quote::class) $state['starting_amount'] = $model->amount; if (!$model->id) { - $client = Client::find($data['client_id']); $model->uses_inclusive_taxes = $client->getSetting('inclusive_taxes'); }