2009-01-06 11:49:19 +01:00
|
|
|
class SuppliersController < ApplicationController
|
2023-05-12 13:01:12 +02:00
|
|
|
before_action :authenticate_suppliers, except: %i[index list]
|
2009-01-18 17:42:51 +01:00
|
|
|
helper :deliveries
|
2009-01-06 11:49:19 +01:00
|
|
|
|
|
|
|
def index
|
2013-03-16 17:53:24 +01:00
|
|
|
@suppliers = Supplier.undeleted.order(:name)
|
2009-01-18 17:42:51 +01:00
|
|
|
@deliveries = Delivery.recent
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@supplier = Supplier.find(params[:id])
|
2009-01-18 17:42:51 +01:00
|
|
|
@deliveries = @supplier.deliveries.recent
|
2014-02-24 14:33:05 +01:00
|
|
|
@orders = @supplier.orders.recent
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# new supplier
|
|
|
|
# if shared_supplier_id is given, the new supplier will filled whith its attributes
|
|
|
|
def new
|
|
|
|
if params[:shared_supplier_id]
|
2021-03-01 15:27:26 +01:00
|
|
|
shared_supplier = SharedSupplier.find(params[:shared_supplier_id])
|
2013-06-12 10:47:54 +02:00
|
|
|
@supplier = shared_supplier.suppliers.new(shared_supplier.autofill_attributes)
|
2009-01-06 11:49:19 +01:00
|
|
|
else
|
|
|
|
@supplier = Supplier.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
def edit
|
|
|
|
@supplier = Supplier.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2019-10-29 15:16:43 +01:00
|
|
|
def create
|
|
|
|
@supplier = Supplier.new(supplier_params)
|
2019-11-17 11:41:34 +01:00
|
|
|
@supplier.supplier_category ||= SupplierCategory.first
|
2009-01-06 11:49:19 +01:00
|
|
|
if @supplier.save
|
2013-02-08 00:31:09 +01:00
|
|
|
flash[:notice] = I18n.t('suppliers.create.notice')
|
2009-01-08 16:33:27 +01:00
|
|
|
redirect_to suppliers_path
|
2009-01-06 11:49:19 +01:00
|
|
|
else
|
2023-05-12 13:01:12 +02:00
|
|
|
render action: 'new'
|
2019-10-29 15:16:43 +01:00
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@supplier = Supplier.find(params[:id])
|
2022-10-13 18:25:52 +02:00
|
|
|
if @supplier.update(supplier_params)
|
2013-02-08 00:31:09 +01:00
|
|
|
flash[:notice] = I18n.t('suppliers.update.notice')
|
2009-01-08 16:33:27 +01:00
|
|
|
redirect_to @supplier
|
2009-01-06 11:49:19 +01:00
|
|
|
else
|
2023-05-12 13:01:12 +02:00
|
|
|
render action: 'edit'
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2009-01-08 16:33:27 +01:00
|
|
|
@supplier = Supplier.find(params[:id])
|
2013-03-16 17:53:24 +01:00
|
|
|
@supplier.mark_as_deleted
|
2013-02-08 00:31:09 +01:00
|
|
|
flash[:notice] = I18n.t('suppliers.destroy.notice')
|
2009-01-08 16:33:27 +01:00
|
|
|
redirect_to suppliers_path
|
2023-05-12 13:01:12 +02:00
|
|
|
rescue StandardError => e
|
|
|
|
flash[:error] = I18n.t('errors.general_msg', msg: e.message)
|
2021-03-01 15:27:26 +01:00
|
|
|
redirect_to @supplier
|
2019-10-29 15:16:43 +01:00
|
|
|
end
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# gives a list with all available shared_suppliers
|
|
|
|
def shared_suppliers
|
2017-03-04 12:10:34 +01:00
|
|
|
@shared_suppliers = SharedSupplier.all
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2019-10-29 15:16:43 +01:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def supplier_params
|
|
|
|
params
|
|
|
|
.require(:supplier)
|
|
|
|
.permit(:name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number,
|
2019-11-17 11:41:34 +01:00
|
|
|
:iban, :custom_fields, :delivery_days, :order_howto, :note, :supplier_category_id,
|
2019-10-29 15:16:43 +01:00
|
|
|
:shared_supplier_id, :min_order_quantity, :shared_sync_method)
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|