diff --git a/app/DataMapper/ClientSettings.php b/app/DataMapper/ClientSettings.php index 1d517c750d02..68e5183e2a47 100644 --- a/app/DataMapper/ClientSettings.php +++ b/app/DataMapper/ClientSettings.php @@ -42,6 +42,7 @@ class ClientSettings extends BaseSettings public $language_id; public $currency_id; + public $precision; public $default_task_rate; public $send_reminders; public $show_tasks_in_portal; diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index 9248e614cbc8..0fab7475a7fd 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -28,6 +28,7 @@ class CompanySettings extends BaseSettings public $language_id; public $currency_id; + public $precision; public $show_currency_symbol; public $show_currency_code; @@ -136,6 +137,7 @@ class CompanySettings extends BaseSettings 'timezone_id' => config('ninja.i18n.timezone_id'), 'language_id' => config('ninja.i18n.language_id'), 'currency_id' => config('ninja.i18n.currency_id'), + 'precision' => 2, 'payment_terms' => config('ninja.i18n.payment_terms'), 'datetime_format_id' => config('ninja.i18n.datetime_format'), 'military_time' => config('ninja.i18n.military_time'), diff --git a/app/Factory/CompanyLedgerFactory.php b/app/Factory/CompanyLedgerFactory.php index 0f05fcde2fa5..c884c6a70b82 100644 --- a/app/Factory/CompanyLedgerFactory.php +++ b/app/Factory/CompanyLedgerFactory.php @@ -15,7 +15,7 @@ use App\Models\CompanyLedger; class CompanyLedgerFactory { - public static function create(int $company_id, int $user_id) :Client + public static function create(int $company_id, int $user_id) :CompanyLedger { $company_ledger = new CompanyLedger; $company_ledger->company_id = $company_id; diff --git a/app/Factory/InvoiceFactory.php b/app/Factory/InvoiceFactory.php index a9d35d94b0d2..44ab4e063b27 100644 --- a/app/Factory/InvoiceFactory.php +++ b/app/Factory/InvoiceFactory.php @@ -22,7 +22,7 @@ class InvoiceFactory { $invoice = new Invoice(); $invoice->status_id = Invoice::STATUS_DRAFT; - $invoice->invoice_number = ''; + $invoice->invoice_number = null; $invoice->discount = 0; $invoice->is_amount_discount = true; $invoice->po_number = ''; diff --git a/app/Helpers/Invoice/InvoiceItemCalc.php b/app/Helpers/Invoice/InvoiceItemCalc.php index 12cbb36a1ca8..09e6edaa3543 100644 --- a/app/Helpers/Invoice/InvoiceItemCalc.php +++ b/app/Helpers/Invoice/InvoiceItemCalc.php @@ -55,6 +55,9 @@ class InvoiceItemCalc private function setDiscount() { + if(!isset($this->item->is_amount_discount)) + return $this; + if($this->item->is_amount_discount) { $discount = $this->formatValue($this->item->discount, $this->settings->precision); @@ -81,10 +84,10 @@ class InvoiceItemCalc { $item_tax = 0; - $tax_rate1 = $this->formatValue($this->item->tax_rate1, $this->settings->precision); - - if($tax_rate1 != 0) + if(isset($this->item->tax_rate1) && $this->item->tax_rate1 > 0) { + $tax_rate1 = $this->formatValue($this->item->tax_rate1, $this->settings->precision); + if($this->settings->inclusive_taxes) $item_tax_rate1_total = $this->formatValue(($this->line_total - ($this->line_total / (1+$tax_rate1/100))) , $this->settings->precision); else @@ -95,10 +98,10 @@ class InvoiceItemCalc $this->groupTax($this->item->tax_name1, $this->item->tax_rate1, $item_tax_rate1_total); } - $tax_rate2 = $this->formatValue($this->item->tax_rate2, $this->settings->precision); - - if($tax_rate2 != 0) + if(isset($this->item->tax_rate2) && $this->item->tax_rate2 > 0) { + $tax_rate2 = $this->formatValue($this->item->tax_rate2, $this->settings->precision); + if($this->settings->inclusive_taxes) $item_tax_rate2_total = $this->formatValue(($this->line_total - ($this->line_total / (1+$tax_rate2/100))) , $this->settings->precision); else diff --git a/app/Http/Requests/Invoice/StoreInvoiceRequest.php b/app/Http/Requests/Invoice/StoreInvoiceRequest.php index e17b8ed8ab5d..07a1a71a60c8 100644 --- a/app/Http/Requests/Invoice/StoreInvoiceRequest.php +++ b/app/Http/Requests/Invoice/StoreInvoiceRequest.php @@ -30,20 +30,20 @@ class StoreInvoiceRequest extends Request public function rules() { - $this->sanitize(); + //$this->sanitize(); return [ 'client_id' => 'required', - 'documents' => 'mimes:png,ai,svg,jpeg,tiff,pdf,gif,psd,txt,doc,xls,ppt,xlsx,docx,pptx', + // 'documents' => 'mimes:png,ai,svg,jpeg,tiff,pdf,gif,psd,txt,doc,xls,ppt,xlsx,docx,pptx', ]; } - +/* If we have an email address instead of a client_id - harvest the client_id here public function sanitize() { $input = $this->all(); - /** If we have an email address instead of a client_id - harvest the client_id here */ + if(isset($input['email']) && !$input['client_id']) { $contact = ClientContact::company(auth()->user()->company()->id)->whereEmail($input['email'])->first(); @@ -59,7 +59,7 @@ public function sanitize() { } - +*/ } diff --git a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php index 2c0cb61a1675..0a3a75d3be83 100644 --- a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php +++ b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php @@ -22,7 +22,7 @@ class UpdateCompanyLedgerWithInvoice public $adjustment; - public $invoice + public $invoice; /** * Create a new job instance. * @@ -60,7 +60,7 @@ class UpdateCompanyLedgerWithInvoice $company_ledger->client_id = $this->invoice->client_id; $company_ledger->balance = $balance + $this->adjustment; $company_ledger->save(); - + $this->invoice->company_ledger()->save($company_ledger); } diff --git a/app/Jobs/Invoice/StoreInvoice.php b/app/Jobs/Invoice/StoreInvoice.php index 1b1ecdf58834..c6fb51fc346a 100644 --- a/app/Jobs/Invoice/StoreInvoice.php +++ b/app/Jobs/Invoice/StoreInvoice.php @@ -65,7 +65,7 @@ class StoreInvoice implements ShouldQueue $payment = false; /* Test if we should auto-bill the invoice */ - if((bool)$invoice->settings->auto_bill) + if((bool)$this->invoice->settings->auto_bill) { $this->invoice = $invoice_repo->markSent($this->invoice); @@ -109,5 +109,7 @@ class StoreInvoice implements ShouldQueue //fire invoice download and return PDF response from here } + return $this->invoice; + } } diff --git a/app/Listeners/Invoice/CreateInvoiceActivity.php b/app/Listeners/Invoice/CreateInvoiceActivity.php index adf71498483d..84d6e2b7e883 100644 --- a/app/Listeners/Invoice/CreateInvoiceActivity.php +++ b/app/Listeners/Invoice/CreateInvoiceActivity.php @@ -11,6 +11,7 @@ namespace App\Listeners\Invoice; +use App\Models\Activity; use App\Models\ClientContact; use App\Models\InvoiceInvitation; use App\Repositories\ActivityRepository; @@ -43,10 +44,10 @@ class CreateInvoiceActivity $fields = new \stdClass; - $fields->client_id = $event->invoice->id; + $fields->invoice_id = $event->invoice->id; $fields->user_id = $event->invoice->user_id; $fields->company_id = $event->invoice->company_id; - $fields->activity_type_id = Activity::CREATE_INVOIE; + $fields->activity_type_id = Activity::CREATE_INVOICE; $this->activity_repo->save($fields, $event->invoice); } diff --git a/app/Models/CompanyLedger.php b/app/Models/CompanyLedger.php index c73033227e5f..f2f430443afa 100644 --- a/app/Models/CompanyLedger.php +++ b/app/Models/CompanyLedger.php @@ -13,7 +13,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; -class CompanyLedger extends BaseModel +class CompanyLedger extends Model { protected $dateFormat = 'Y-m-d H:i:s.u'; diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 260cc9cb17cf..2e3d14aff346 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -27,9 +27,34 @@ class Invoice extends BaseModel use NumberFormatter; use MakesDates; - protected $guarded = [ - 'id', - ]; + protected $fillable = [ + 'invoice_number', + 'discount', + 'po_number', + 'invoice_date', + 'due_date', + 'terms', + 'public_notes', + 'private_notes', + 'invoice_type_id', + 'tax_name1', + 'tax_rate1', + 'tax_name2', + 'tax_rate2', + 'is_amount_discount', + 'invoice_footer', + 'partial', + 'partial_due_date', + 'custom_value1', + 'custom_value2', + 'custom_taxes1', + 'custom_taxes2', + 'custom_text_value1', + 'custom_text_value2', + 'line_items', + 'settings', + 'client_id', + ]; protected $casts = [ 'settings' => 'object', diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index 643728ab1853..f0f7f61409e1 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -61,7 +61,7 @@ class InvoiceRepository extends BaseRepository $finished_amount = $invoice->amount; if($finished_amount != $starting_amount) - UpdateCompanyLedgerWithInvoice::dispatchNow($invoice); + UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($finished_amount - $starting_amount)); return $invoice; diff --git a/app/Transformers/InvoiceTransformer.php b/app/Transformers/InvoiceTransformer.php index f810ffc65c05..b322590b5db0 100644 --- a/app/Transformers/InvoiceTransformer.php +++ b/app/Transformers/InvoiceTransformer.php @@ -160,8 +160,10 @@ class InvoiceTransformer extends EntityTransformer 'has_expenses' => (bool) $invoice->has_expenses, 'custom_text_value1' => $invoice->custom_text_value1 ?: '', 'custom_text_value2' => $invoice->custom_text_value2 ?: '', + 'line_items' => $invoice->line_items, 'backup' => $invoice->backup ?: '', 'settings' => $invoice->settings, + ]; } } \ No newline at end of file diff --git a/database/migrations/2014_10_13_000000_create_users_table.php b/database/migrations/2014_10_13_000000_create_users_table.php index 2f1b37f51581..91c6a038973e 100644 --- a/database/migrations/2014_10_13_000000_create_users_table.php +++ b/database/migrations/2014_10_13_000000_create_users_table.php @@ -848,7 +848,7 @@ class CreateUsersTable extends Migration }); - Schema::create('company_ledger', function ($table) { + Schema::create('company_ledgers', function ($table) { $table->increments('id'); $table->unsignedInteger('company_id');