Add SupplierCategory

This allows the categorization of suppliers. For a better reporting
it is necessary to split the expenses of the invoices.
E.g. we want to be able to generate independent sums of general cost
like the rent or electricity and the cost of the bought articles.
This commit is contained in:
Patrick Gansterer 2019-11-17 11:41:34 +01:00
parent f5bbe0d5ae
commit d90d188dbf
11 changed files with 90 additions and 25 deletions

View file

@ -27,6 +27,7 @@ class SuppliersController < ApplicationController
def create
@supplier = Supplier.new(supplier_params)
@supplier.supplier_category ||= SupplierCategory.first
if @supplier.save
flash[:notice] = I18n.t('suppliers.create.notice')
redirect_to suppliers_path
@ -70,7 +71,7 @@ class SuppliersController < ApplicationController
params
.require(:supplier)
.permit(:name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number,
:iban, :custom_fields, :delivery_days, :order_howto, :note,
:iban, :custom_fields, :delivery_days, :order_howto, :note, :supplier_category_id,
:shared_supplier_id, :min_order_quantity, :shared_sync_method)
end

View file

@ -1,5 +1,6 @@
class FinancialTransactionClass < ApplicationRecord
has_many :financial_transaction_types, dependent: :destroy
has_many :supplier_category, dependent: :restrict_with_exception
validates :name, presence: true
validates_uniqueness_of :name

View file

@ -8,6 +8,7 @@ class Supplier < ApplicationRecord
has_many :orders
has_many :deliveries
has_many :invoices
belongs_to :supplier_category
belongs_to :shared_supplier # for the sharedLists-App
validates :name, :presence => true, :length => { :in => 4..30 }

View file

@ -0,0 +1,19 @@
class SupplierCategory < ActiveRecord::Base
belongs_to :financial_transaction_class
has_many :suppliers
normalize_attributes :name, :description
validates :name, presence: true, uniqueness: true, length: { minimum: 2 }
before_destroy :check_for_associated_suppliers
protected
# Deny deleting the category when there are associated suppliers.
def check_for_associated_suppliers
raise I18n.t('activerecord.errors.has_many_left', collection: Supplier.model_name.human) if suppliers.undeleted.any?
end
end

View file

@ -12,6 +12,9 @@
= f.input :url
= f.input :contact_person
= f.input :customer_number
- supplier_categories = SupplierCategory.order(:name)
- if supplier_categories.count > 1
= f.association :supplier_category, collection: supplier_categories, include_blank: false
- if FoodsoftConfig[:use_iban]
= f.input :iban
= f.input :delivery_days

View file

@ -29,6 +29,9 @@
%dd= @supplier.contact_person
%dt= heading_helper(Supplier, :customer_number) + ':'
%dd= @supplier.customer_number
- if SupplierCategory.count > 1
%dt= heading_helper(Supplier, :supplier_category) + ':'
%dd= @supplier.supplier_category.try(:name)
- if FoodsoftConfig[:use_iban]
%dt= heading_helper(Supplier, :iban) + ':'
%dd= @supplier.iban