From d89690ebe4f4b1d35aecfbdbfc94bc71d464ef02 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 23 Feb 2017 16:33:02 +0200 Subject: [PATCH] Support custom product fields --- app/Constants.php | 13 ++- app/Http/Controllers/ProductController.php | 3 +- app/Models/Frequency.php | 7 ++ app/Models/Product.php | 2 + app/Ninja/Repositories/InvoiceRepository.php | 2 + ...02_23_095934_add_custom_product_fields.php | 80 +++++++++++++++++++ database/seeds/ConstantsSeeder.php | 8 -- database/seeds/CurrenciesSeeder.php | 2 +- database/seeds/DatabaseSeeder.php | 1 + database/seeds/FrequencySeeder.php | 31 +++++++ database/seeds/UpdateSeeder.php | 3 +- resources/lang/en/texts.php | 5 ++ .../views/accounts/invoice_settings.blade.php | 6 +- resources/views/accounts/product.blade.php | 10 +++ resources/views/invoices/knockout.blade.php | 10 +++ 15 files changed, 165 insertions(+), 18 deletions(-) create mode 100644 database/migrations/2017_02_23_095934_add_custom_product_fields.php create mode 100644 database/seeds/FrequencySeeder.php diff --git a/app/Constants.php b/app/Constants.php index 9c4dd9f64a60..b02ec238fb7e 100644 --- a/app/Constants.php +++ b/app/Constants.php @@ -204,9 +204,10 @@ if (! defined('APP_NAME')) { define('FREQUENCY_TWO_WEEKS', 2); define('FREQUENCY_FOUR_WEEKS', 3); define('FREQUENCY_MONTHLY', 4); - define('FREQUENCY_THREE_MONTHS', 5); - define('FREQUENCY_SIX_MONTHS', 6); - define('FREQUENCY_ANNUALLY', 7); + define('FREQUENCY_TWO_MONTHS', 5); + define('FREQUENCY_THREE_MONTHS', 6); + define('FREQUENCY_SIX_MONTHS', 7); + define('FREQUENCY_ANNUALLY', 8); define('SESSION_TIMEZONE', 'timezone'); define('SESSION_CURRENCY', 'currency'); @@ -392,6 +393,12 @@ if (! defined('APP_NAME')) { define('REMINDER2', 'reminder2'); define('REMINDER3', 'reminder3'); + define('RESET_FREQUENCY_DAILY', 1); + define('RESET_FREQUENCY_WEEKLY', 2); + define('RESET_FREQUENCY_MONTHLY', 3); + define('RESET_FREQUENCY_QUATERLY', 4); + define('RESET_FREQUENCY_YEARLY', 5); + define('REMINDER_DIRECTION_AFTER', 1); define('REMINDER_DIRECTION_BEFORE', 2); diff --git a/app/Http/Controllers/ProductController.php b/app/Http/Controllers/ProductController.php index e53850d8f599..daba96174e55 100644 --- a/app/Http/Controllers/ProductController.php +++ b/app/Http/Controllers/ProductController.php @@ -140,8 +140,7 @@ class ProductController extends BaseController $product->product_key = trim(Input::get('product_key')); $product->notes = trim(Input::get('notes')); $product->cost = trim(Input::get('cost')); - $product->default_tax_rate_id = Input::get('default_tax_rate_id'); - + $product->fill(Input::all()); $product->save(); $message = $productPublicId ? trans('texts.updated_product') : trans('texts.created_product'); diff --git a/app/Models/Frequency.php b/app/Models/Frequency.php index a30e3c8e823d..bd4bf66bcb06 100644 --- a/app/Models/Frequency.php +++ b/app/Models/Frequency.php @@ -16,6 +16,13 @@ class Frequency extends Eloquent */ public $timestamps = false; + /** + * @var array + */ + protected $fillable = [ + 'name', + ]; + public static function selectOptions() { $data = []; diff --git a/app/Models/Product.php b/app/Models/Product.php index b089eabdbd52..6c040b979ca9 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -31,6 +31,8 @@ class Product extends EntityModel 'cost', 'qty', 'default_tax_rate_id', + 'custom_value1', + 'custom_value2', ]; /** diff --git a/app/Ninja/Repositories/InvoiceRepository.php b/app/Ninja/Repositories/InvoiceRepository.php index 39f047b38edd..0722e126d2d4 100644 --- a/app/Ninja/Repositories/InvoiceRepository.php +++ b/app/Ninja/Repositories/InvoiceRepository.php @@ -632,6 +632,8 @@ class InvoiceRepository extends BaseRepository if ($product && (Auth::user()->can('edit', $product))) { $product->notes = ($task || $expense) ? '' : $item['notes']; $product->cost = $expense ? 0 : $item['cost']; + $product->custom_value1 = isset($item['custom_value1']) ? $item['custom_value1'] : null; + $product->custom_value2 = isset($item['custom_value2']) ? $item['custom_value2'] : null; $product->save(); } } diff --git a/database/migrations/2017_02_23_095934_add_custom_product_fields.php b/database/migrations/2017_02_23_095934_add_custom_product_fields.php new file mode 100644 index 000000000000..e060f8be07cf --- /dev/null +++ b/database/migrations/2017_02_23_095934_add_custom_product_fields.php @@ -0,0 +1,80 @@ +string('custom_value1')->nullable(); + $table->string('custom_value2')->nullable(); + }); + + Schema::table('account_gateway_settings', function ($table) { + $table->decimal('fee_amount', 13, 2)->nullable(); + $table->decimal('fee_percent', 13, 3)->nullable(); + $table->string('fee_tax_name1')->nullable(); + $table->string('fee_tax_name2')->nullable(); + $table->decimal('fee_tax_rate1', 13, 3)->nullable(); + $table->decimal('fee_tax_rate2', 13, 3)->nullable(); + }); + + Schema::table('invoice_items', function ($table) { + $table->smallInteger('invoice_item_type_id')->default(1); + }); + + Schema::table('accounts', function ($table) { + $table->smallInteger('reset_counter_frequency_id')->nullable(); + }); + + DB::table('currencies')->where('code', '=', 'HKR')->update(['code' => 'HRK']); + + // Add 'Two Months' frequency option + if (DB::table('frequencies')->count() == 7) { + DB::table('frequencies')->where('id', '=', 5)->update(['name' => 'Two months']); + DB::table('frequencies')->where('id', '=', 6)->update(['name' => 'Three months']); + DB::table('frequencies')->where('id', '=', 7)->update(['name' => 'Six months']); + DB::table('frequencies')->insert(['name' => 'Yearly']); + DB::statement('update invoices set frequency_id = frequency_id + 1 where frequency_id >= 5'); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('products', function ($table) { + $table->dropColumn('custom_value1'); + $table->dropColumn('custom_value2'); + }); + + Schema::table('account_gateway_settings', function ($table) { + $table->dropColumn('fee_amount'); + $table->dropColumn('fee_percent'); + $table->dropColumn('fee_tax_rate1'); + $table->dropColumn('fee_tax_name1'); + $table->dropColumn('fee_tax_rate2'); + $table->dropColumn('fee_tax_name2'); + }); + + Schema::table('invoice_items', function ($table) { + $table->dropColumn('invoice_item_type_id'); + }); + + Schema::table('accounts', function ($table) { + $table->dropColumn('reset_counter_frequency_id'); + }); + + DB::table('currencies')->where('code', '=', 'HRK')->update(['code' => 'HKR']); + } +} diff --git a/database/seeds/ConstantsSeeder.php b/database/seeds/ConstantsSeeder.php index ce36f3a38d46..3ab0e5497df9 100644 --- a/database/seeds/ConstantsSeeder.php +++ b/database/seeds/ConstantsSeeder.php @@ -24,14 +24,6 @@ class ConstantsSeeder extends Seeder Theme::create(['name' => 'united']); Theme::create(['name' => 'yeti']); - Frequency::create(['name' => 'Weekly']); - Frequency::create(['name' => 'Two weeks']); - Frequency::create(['name' => 'Four weeks']); - Frequency::create(['name' => 'Monthly']); - Frequency::create(['name' => 'Three months']); - Frequency::create(['name' => 'Six months']); - Frequency::create(['name' => 'Annually']); - Size::create(['name' => '1 - 3']); Size::create(['name' => '4 - 10']); Size::create(['name' => '11 - 50']); diff --git a/database/seeds/CurrenciesSeeder.php b/database/seeds/CurrenciesSeeder.php index ff801bc3408d..a31d267282ef 100644 --- a/database/seeds/CurrenciesSeeder.php +++ b/database/seeds/CurrenciesSeeder.php @@ -52,7 +52,7 @@ class CurrenciesSeeder extends Seeder ['name' => 'Aruban Florin', 'code' => 'AWG', 'symbol' => 'Afl. ', 'precision' => '2', 'thousand_separator' => ' ', 'decimal_separator' => '.'], ['name' => 'Turkish Lira', 'code' => 'TRY', 'symbol' => 'TL ', 'precision' => '2', 'thousand_separator' => '.', 'decimal_separator' => ','], ['name' => 'Romanian New Leu', 'code' => 'RON', 'symbol' => '', 'precision' => '2', 'thousand_separator' => ',', 'decimal_separator' => '.'], - ['name' => 'Croatian Kuna', 'code' => 'HKR', 'symbol' => 'kn', 'precision' => '2', 'thousand_separator' => '.', 'decimal_separator' => ','], + ['name' => 'Croatian Kuna', 'code' => 'HRK', 'symbol' => 'kn', 'precision' => '2', 'thousand_separator' => '.', 'decimal_separator' => ','], ['name' => 'Saudi Riyal', 'code' => 'SAR', 'symbol' => '', 'precision' => '2', 'thousand_separator' => ',', 'decimal_separator' => '.'], ['name' => 'Japanese Yen', 'code' => 'JPY', 'symbol' => '¥', 'precision' => '0', 'thousand_separator' => ',', 'decimal_separator' => '.'], ['name' => 'Maldivian Rufiyaa', 'code' => 'MVR', 'symbol' => '', 'precision' => '2', 'thousand_separator' => ',', 'decimal_separator' => '.'], diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index e6c3f632046c..0d384ceb6889 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -28,5 +28,6 @@ class DatabaseSeeder extends Seeder $this->call('PaymentTypesSeeder'); $this->call('LanguageSeeder'); $this->call('IndustrySeeder'); + $this->call('FrequencySeeder'); } } diff --git a/database/seeds/FrequencySeeder.php b/database/seeds/FrequencySeeder.php new file mode 100644 index 000000000000..437898dccece --- /dev/null +++ b/database/seeds/FrequencySeeder.php @@ -0,0 +1,31 @@ + 'Weekly'], + ['name' => 'Two weeks'], + ['name' => 'Four weeks'], + ['name' => 'Monthly'], + ['name' => 'Two months'], + ['name' => 'Three months'], + ['name' => 'Six months'], + ['name' => 'Annually'], + ]; + + foreach ($frequencies as $frequency) { + $record = Frequency::whereName($frequency['name'])->first(); + if ($record) { + //$record->save(); + } else { + Frequency::create($frequency); + } + } + } +} diff --git a/database/seeds/UpdateSeeder.php b/database/seeds/UpdateSeeder.php index a0a43cee3a7c..0d494cdfc688 100644 --- a/database/seeds/UpdateSeeder.php +++ b/database/seeds/UpdateSeeder.php @@ -24,7 +24,8 @@ class UpdateSeeder extends Seeder $this->call('PaymentTypesSeeder'); $this->call('LanguageSeeder'); $this->call('IndustrySeeder'); - + $this->call('FrequencySeeder'); + Cache::flush(); } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index daa26f774de8..47443ab258e0 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2374,6 +2374,11 @@ $LANG = array( 'month_year' => 'MONTH/YEAR', 'valid_thru' => 'Valid\nthru', + 'product_fields' => 'Product Fields', + 'custom_product_fields_help' => 'Add a field when creating a product or invoice and display the label and value on the PDF.', + 'freq_two_months' => 'Two months', + 'freq_yearly' => 'Annually', + ); return $LANG; diff --git a/resources/views/accounts/invoice_settings.blade.php b/resources/views/accounts/invoice_settings.blade.php index 6c184730ea3d..0c304d09a69c 100644 --- a/resources/views/accounts/invoice_settings.blade.php +++ b/resources/views/accounts/invoice_settings.blade.php @@ -174,7 +174,7 @@ {{ trans('texts.invoice_fields') }}
  • - {{ trans('texts.invoice_item_fields') }} + {{ trans('texts.product_fields') }}
  • {{ trans('texts.invoice_charges') }} @@ -220,14 +220,14 @@ -
    +
    {!! Former::text('custom_invoice_item_label1') ->label(trans('texts.field_label')) !!} {!! Former::text('custom_invoice_item_label2') ->label(trans('texts.field_label')) - ->help(trans('texts.custom_invoice_item_fields_help')) !!} + ->help(trans('texts.custom_product_fields_help')) !!}
    diff --git a/resources/views/accounts/product.blade.php b/resources/views/accounts/product.blade.php index 3c25495ff150..7b6757bde4fd 100644 --- a/resources/views/accounts/product.blade.php +++ b/resources/views/accounts/product.blade.php @@ -21,6 +21,16 @@ {!! Former::text('product_key')->label('texts.product') !!} {!! Former::textarea('notes')->rows(6) !!} + + @if ($account->hasFeature(FEATURE_INVOICE_SETTINGS)) + @if ($account->custom_invoice_item_label1) + {!! Former::text('custom_value1')->label($account->custom_invoice_item_label1) !!} + @endif + @if ($account->custom_invoice_item_label2) + {!! Former::text('custom_value2')->label($account->custom_invoice_item_label2) !!} + @endif + @endif + {!! Former::text('cost') !!} @if ($account->invoice_item_taxes) diff --git a/resources/views/invoices/knockout.blade.php b/resources/views/invoices/knockout.blade.php index 04aa9e197785..d23c8c9e7c6d 100644 --- a/resources/views/invoices/knockout.blade.php +++ b/resources/views/invoices/knockout.blade.php @@ -921,6 +921,16 @@ ko.bindingHandlers.productTypeahead = { $select.val('0 ' + datum.default_tax_rate.rate + ' ' + datum.default_tax_rate.name).trigger('change'); } @endif + @if (Auth::user()->isPro() && $account->custom_invoice_item_label1) + if (datum.custom_value1) { + model.custom_value1(datum.custom_value1); + } + @endif + @if (Auth::user()->isPro() && $account->custom_invoice_item_label2) + if (datum.custom_value2) { + model.custom_value2(datum.custom_value2); + } + @endif @endif onItemChange(); }).on('typeahead:change', function(element, datum, name) {