| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- class CashOutsController < ApplicationController
- ##--- Abilities
- load_and_authorize_resource
- ##--- Breadcrum_rails
- add_breadcrumb I18n.t("breadcrumbs." + controller_name), :cash_outs_path
- add_breadcrumb "Nueva Corte de caja " , :new_cash_out_path, only: :new
- add_breadcrumb "Detalle del corte de caja " , :cash_out_path, only: :show
- add_breadcrumb "Editar corte de caja " , :edit_cash_out_path, only: :edit
- add_breadcrumb "Cajas abiertas " , :opened_cash_registers_path, only: :opened_cash_registers
- before_action :set_cash_out, only: [:show, :edit, :update, :destroy]
- before_action :set_data, only: [:new, :create]
- before_action :get_filters, only: [:index, :show, :edit, :new]
- # GET /cash_outs.json
- def index
- case current_user.usertype
- when "A"
- @cash_outs = CashOut.all.includes(:open_cash_register, :user, :received_by).order('cash_outs.created_at desc')
- when "G"
- @cash_outs = Pointsale.find(current_user.pointsale_id).cash_outs.includes(:open_cash_register, :user, :received_by).order('cash_outs.created_at desc')
- when "C"
- @cash_outs = Pointsale.find(current_user.pointsale_id).cash_outs.includes(:open_cash_register, :user, :received_by).where("cash_outs.user_id = ?", current_user.id).order('cash_outs.created_at desc')
- end
- end
- # GET /cash_outs/1
- # GET /cash_outs/1.json
- def show
- @incomings = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1", @cash_out.open_cash_register.id).order('created_at')
- @outgoings = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '0' and status = 1", @cash_out.open_cash_register.id).order('created_at')
- @sales_total = @incomings.sum(:quantity)
- @expenses_total = @outgoings.sum(:quantity)
- end
- # GET /cash_outs/new
- def new
- @cash_out = CashOut.new
- @cash_out.cash_out_details.new
- @cash_out.received_cash = nil
- @cash_out.physical_cash = nil
- @cash_out.cash_fund = nil
- end
- # GET /cash_outs/1/edit
- def edit
- end
- # POST /cash_outs
- # POST /cash_outs.json
- def create
- final_cash = 0
- @cash_out = CashOut.new(cash_out_params)
- open_cash_register = OpenCashRegister.find(params[:open_cash_register_id])
- @cash_out.amount_in = 0
- @cash_out.amount_out = 0
- @cash_out.open_cash_register_id = open_cash_register.id
- @cash_out.user_id = current_user.id
- @cash_out.cash_out_details.each do |detail|
- @cash_out.amount_in = @cash_out.amount_in + detail.incoming
- @cash_out.amount_out = @cash_out.amount_out + detail.outgoing
- if detail.payment_method_id == @cash_payment_method_id
- final_cash = detail.adjustment
- end
- end
- open_cash_register.expenses.update_all(:status => 2) #registered
- respond_to do |format|
- message = "Corte de caja de #{@cash_out.open_cash_register.cash_register.name} realizado correctamente"
- @cash_out.audit_comment = message
- if @cash_out.save
- open_cash_register.update_attributes(:status => 'closed', :final_cash => final_cash)
- format.js { flash[:success] = message }
- else
- format.js
- format.json { render json: @cash_out.errors, status: :unprocessable_entity }
- end
- end
- end
- # PATCH/PUT /cash_outs/1
- # PATCH/PUT /cash_outs/1.json
- def update
- respond_to do |format|
- if @cash_out.update(cash_out_params)
- format.html { redirect_to @cash_out, notice: 'Cash out was successfully updated.' }
- format.json { render :show, status: :ok, location: @cash_out }
- else
- format.html { render :edit }
- format.json { render json: @cash_out.errors, status: :unprocessable_entity }
- end
- end
- end
- def get_open_cash_registers
- respond_to do |format|
- format.js
- end
- end
- def opened_cash_registers
- @opened_cash_registers = OpenCashRegister.abiertas
- end
- def select_open_cash_to_close
- @open_cash_register_id = params[:open_cash_register_id]
- respond_to do |format|
- format.js
- end
- end
- # DELETE /cash_outs/1
- # DELETE /cash_outs/1.json
- def destroy
- @cash_out.destroy
- respond_to do |format|
- format.html { redirect_to cash_outs_url, notice: 'Cash out was successfully destroyed.' }
- format.json { head :no_content }
- end
- end
- def find_cash_outs_by_date
- respond_to do |format|
- startDate = DateTime.parse(params[:begin_date])
- endDate = DateTime.parse(params[:end_date])
- @cash_outs = Pointsale.find(params[:pointsale_id]).cash_outs.includes(:open_cash_register, :user, :received_by).where(:created_at => startDate..endDate).order(" id DESC ")
- format.js
- end
- end
- def print_receipt
- #ticket para la venta
- respond_to do |format|
- @cash_out = CashOut.find(params[:cash_out_id])
- products_ids = @cash_out.open_cash_register.sales_details.joins(:sale).where("sales.status != 1").pluck(:product_id).join(",")
- @pointsale = OpenCashRegister.get_pointsale(@cash_out.open_cash_register_id, "open_cash_register")
- @initial_cash = @cash_out.open_cash_register.initial_cash
- if @cash_out.open_cash_register.products.size > 0
- @details = ActiveRecord::Base.connection.exec_query("SELECT
- SUM(sales_details.quantity) as quantity, SUM(sales_details.total) as total, sales_details.product_id, sub.name as product_name, sub.category, sub.parent
- FROM
- sales_details
- INNER JOIN sales ON sales_details.sale_id = sales.id
- INNER JOIN (
- SELECT
- products. ID as product_id,
- category_id,
- categories.category,
- products. NAME,
- categories.parent_id,
- parents.category as parent
- FROM
- products
- INNER JOIN categories_products ON categories_products.product_id = products. ID
- INNER JOIN categories ON categories. ID = categories_products.category_id
- LEFT JOIN categories as parents ON parents.ID = categories.parent_id
- WHERE
- (products. ID IN (#{products_ids}) )
- ) sub ON (
- sales_details.product_id = sub.product_id
- )
- WHERE
- sales.open_cash_register_id = #{@cash_out.open_cash_register_id} and sales.status != 1
- GROUP BY sales_details.product_id, sub.category, sub.name, sub.parent
- ORDER BY
- sub.category, sub.parent")
- else
- @details = Array.new
- end
- @details.each do |detail|
- detail["price_sale"] = Product.find(detail['product_id']).get_price_sale(@pointsale.id)
- detail["total_without_discount"] = detail["price_sale"] * detail['quantity'].to_f
- detail["discount"] = detail["total_without_discount"] - detail["total"].to_f
- end
- @expenses = Expense.where(:open_cash_register_id => @cash_out.open_cash_register_id).activos
- format.pdf do
- render pdf: "ticket_corte_#{@cash_out.id}",
- template: "cash_outs/receipt.pdf.erb",
- layout: 'receipt.html.erb',
- locals: { :cash_out => @cash_out, :details => @details, :pointsale => @pointsale, :expenses => @expenses, :initial_cash => @initial_cash },
- show_as_html: params.key?('debug'),
- page_width: '80mm',
- page_height: '300mm'
- end
- end
- end
- private
- def set_data
- @cash_out.cash_out_details.destroy_all
- @cash_payment_method_id = PaymentMethod.find_by(:isCash => 1).id
- @opened_cash_register = (params[:open_cash_register_id].blank? ? current_user.get_open_cash_register : OpenCashRegister.find(params[:open_cash_register_id]))
- if @opened_cash_register.present?
- @initial_cash = @opened_cash_register.initial_cash
- @incomings = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1", @opened_cash_register.id).order('created_at')
- @outgoings = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '0' and status = 1", @opened_cash_register.id).order('created_at')
- @sales_total = @incomings.sum(:quantity)
- @expenses_total = @outgoings.sum(:quantity)
- end
- @payments = ActiveRecord::Base.connection.exec_query("SELECT DISTINCT(crm.payment_method_id), pm.method, i.total as incoming, o.total as outgoing, SUM(crm.quantity) AS total from cash_registers_moves as crm LEFT JOIN (SELECT DISTINCT(payment_method_id), SUM(quantity) as total FROM cash_registers_moves WHERE move_type='1' and status = 1 and open_cash_register_id=#{@opened_cash_register.id} group by payment_method_id) as i ON (i.payment_method_id=crm.payment_method_id) LEFT JOIN (SELECT DISTINCT(payment_method_id), SUM(quantity) as total FROM cash_registers_moves WHERE move_type='0' and status = 1 and open_cash_register_id=#{@opened_cash_register.id} group by payment_method_id) as o ON (o.payment_method_id=crm.payment_method_id), payment_methods as pm WHERE pm.id=crm.payment_method_id and crm.open_cash_register_id=#{@opened_cash_register.id} group by crm.payment_method_id, pm.method, i.total, o.total")
- @payments.each do |payment|
- payment["incoming"] = "0" if payment["incoming"].nil?
- payment["outgoing"] = "0" if payment["outgoing"].nil?
- payment["total"] = payment["incoming"].to_f - payment["outgoing"].to_f
- @cash_out.cash_out_details.build
- end
- end
- # Use callbacks to share common setup or constraints between actions.
- def set_cash_out
- @cash_out = CashOut.find(params[:id])
- end
- def get_filters
- if params[:current_page].blank?
- @current_page = 1
- else
- @current_page = params[:current_page]
- end
- @filter = params[:filter]
- end
- # Never trust parameters from the scary internet, only allow the white list through.
- def cash_out_params
- params.require(:cash_out).permit(:received_by_id, :received_cash, :cash_fund, :physical_cash, :observations, :cash_out_details_attributes => [:payment_method_id, :observations, :incoming, :outgoing, :total, :adjustment])
- end
- end
|