This repository has been archived on 2021-11-24. You can view files and clone it, but cannot push or open issues or pull requests.
phone_app/app/controllers/admin/buy_lists_controller.rb

240 lines
7.1 KiB
Ruby

# -*- encoding : utf-8 -*-
class Admin::BuyListsController < ApplicationController
layout "admin"
before_action :auth_admin
def index
@p_customer_sheets = PCustomerSheet.all
@p_customer_sheets = @p_customer_sheets.where(:state => ["brouillon", "offre", "commande"])
@price_line_blocks = PriceLineBlock.where(:price_lineable_type => "PCustomerSheet", :price_lineable_id => @p_customer_sheets.ids)
@price_lines = PriceLine.where(:price_line_block_id => @price_line_blocks.ids).unscoped
if params[:search][:s_brand].present? && params[:search][:s_brand] != ""
@price_lines = @price_lines.joins(p_product_ref: :p_product).where("s_brand_id = ?", params[:search][:s_brand])
end
if params[:search][:state].present? && params[:search][:state] != ""
@p_customer_sheets = @p_customer_sheets.where(:state => params[:search][:state])
@price_line_blocks = PriceLineBlock.where(:price_lineable_type => "PCustomerSheet", :price_lineable_id => @p_customer_sheets.ids)
@price_lines = PriceLine.where(:price_line_block_id => @price_line_blocks.ids)
end
if params[:search][:p_customer_code].present?
@p_customer_sheets = @p_customer_sheets.joins(:p_customer).where("code LIKE ?", params[:search][:p_customer_code])
@price_line_blocks = PriceLineBlock.where(:price_lineable_type => "PCustomerSheet", :price_lineable_id => @p_customer_sheets.ids)
@price_lines = PriceLine.where(:price_line_block_id => @price_line_blocks.ids)
end
date_regex = /^(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20)\d\d$/i
params[:cc_creation_date_start] = Date.today.beginning_of_month.strftime('%d/%m/%Y') if !params[:cc_creation_date_start]
if params[:cc_creation_date_start] and params[:cc_creation_date_start] =~ date_regex
#fsfds = sdfsfd
@cc_creation_date_start = Date.parse(params[:cc_creation_date_start]).beginning_of_day
params[:cc_creation_date_start]= @cc_creation_date_start.strftime('%d/%m/%Y')
else
@cc_creation_date_start = nil
end
if true
params[:cc_creation_date_stop] = Date.today.end_of_month.strftime('%d/%m/%Y') if !params[:cc_creation_date_stop]
if params[:cc_creation_date_stop].to_s != "" # and params[:stop] =~ date_regex
@cc_creation_date_stop = Date.parse(params[:cc_creation_date_stop]).end_of_day
params[:cc_creation_date_stop]= @cc_creation_date_stop.strftime('%d/%m/%Y')
else
@cc_creation_date_stop = nil
end
end
@price_lines = @price_lines.where("price_lines.cc_creation_date >= ?", @cc_creation_date_start) if @cc_creation_date_start
@price_lines = @price_lines.where("price_lines.cc_creation_date <= ?", @cc_creation_date_stop.end_of_day) if @cc_creation_date_stop
params[:wish_date_start] = Date.today.beginning_of_month.strftime('%d/%m/%Y') if !params[:wish_date_start]
if params[:wish_date_start] and params[:wish_date_start] =~ date_regex
#fsfds = sdfsfd
@wish_date_start = Date.parse(params[:wish_date_start]).beginning_of_day
params[:wish_date_start]= @wish_date_start.strftime('%d/%m/%Y')
else
@wish_date_start = nil
end
if true
params[:wish_date_stop] = Date.today.end_of_month.strftime('%d/%m/%Y') if !params[:wish_date_stop]
if params[:wish_date_stop].to_s != "" # and params[:stop] =~ date_regex
@wish_date_stop = Date.parse(params[:wish_date_stop]).end_of_day
params[:wish_date_stop]= @wish_date_stop.strftime('%d/%m/%Y')
else
@wish_date_stop = nil
end
end
@price_lines = @price_lines.joins(:price_line_block).where("price_line_blocks.wish_date >= ?", @wish_date_start) if @wish_date_start
@price_lines = @price_lines.joins(:price_line_block).where("price_line_blocks.wish_date <= ?", @wish_date_stop.end_of_day) if @wish_date_stop
respond_to do |format|
format.html{
}
end
end
def show
@p_customer_sheet = PCustomerSheet.find(params[:id])
end
def new
@p_customer_sheet = PCustomerSheet.new()
@p_customer = PCustomer.find(params[:p_customer_id]) if params[:p_customer_id]
@p_customer_sheet.p_customer = @p_customer
@p_customer_sheet.price_line_block = PriceLineBlock.new(:particular_bill_id => @p_customer.particular_bill_id, :particular_send_id => @p_customer.particular_send_id)
@p_customer_sheet.price_line_block.price_lines.build
@p_customer_sheet.price_line_block.p_customer = @p_customer
end
def edit
@p_customer_sheet = PCustomerSheet.find(params[:id])
@p_customer = @p_customer_sheet.p_customer
end
def create
@p_customer_sheet = PCustomerSheet.new(params.require(:p_customer_sheet).permit!)
@p_customer_sheet.admin = current_admin
@p_customer = @p_customer_sheet.p_customer
if @p_customer_sheet.save
@p_customer_sheets = PCustomerSheet.order(:id).all
redirect_to admin_p_customer_sheet_path(@p_customer_sheet)
else
render action: "new"
end
end
def update
@p_customer_sheet = PCustomerSheet.find(params[:id])
@p_customer = @p_customer_sheet.p_customer
@price_line_id = params[:price_line_id]
if @p_customer_sheet.update(state: params[:state])
@p_customer_sheets = PCustomerSheet.where(:state => ["AV BPA", "PAS BPA", "BPA"])
return @p_customer_sheet
# redirect_to admin_p_customer_sheet_path(@p_customer_sheet)
else
render action: "edit"
end
end
def destroy
@p_customer_sheet = PCustomerSheet.find(params[:id])
@p_customer_sheet.destroy
end
def generate_d
@p_customer_sheet = PCustomerSheet.find(params[:id])
@p_customer_sheet.generate_d
redirect_back(fallback_location: admin_p_customer_sheets_path)
end
def generate_fp
@p_customer_sheet = PCustomerSheet.find(params[:id])
@p_customer_sheet.generate_fp
redirect_back(fallback_location: admin_p_customer_sheets_path)
end
def generate_bc
@p_customer_sheet = PCustomerSheet.find(params[:id])
@p_customer_sheet.generate_bc
redirect_back(fallback_location: admin_p_customer_sheets_path)
end
def generate_bl
@p_customer_sheet = PCustomerSheet.find(params[:id])
@p_customer_sheet.generate_bl
redirect_back(fallback_location: admin_p_customer_sheets_path)
end
def generate_f
@p_customer_sheet = PCustomerSheet.find(params[:id])
@p_customer_sheet.generate_f
redirect_back(fallback_location: admin_p_customer_sheets_path)
end
def reject
@p_customer_sheet = PCustomerSheet.find(params[:id])
@p_customer_sheet.state = "refusée"
@p_customer_sheet.save
@p_customer_sheet.archive_now if !@p_customer_sheet.archived
redirect_back(fallback_location: admin_p_customer_sheets_path)
end
def block_price
@p_customer_sheet = PCustomerSheet.find(params[:id])
if @p_customer_sheet.price_blocked
@p_customer_sheet.unblock_price
else
@p_customer_sheet.block_price
end
redirect_back(fallback_location: admin_p_customer_sheets_path)
end
end