cash_outs_controller.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. class CashOutsController < ApplicationController
  2. ##--- Abilities
  3. load_and_authorize_resource
  4. ##--- Breadcrum_rails
  5. add_breadcrumb I18n.t("breadcrumbs." + controller_name), :cash_outs_path
  6. add_breadcrumb "Nueva Corte de caja ", :new_cash_out_path, only: :new
  7. add_breadcrumb "Detalle del corte de caja ", :cash_out_path, only: :show
  8. add_breadcrumb "Editar corte de caja ", :edit_cash_out_path, only: :edit
  9. add_breadcrumb "Cajas abiertas ", :opened_cash_registers_path, only: :opened_cash_registers
  10. before_action :set_cash_out, only: [:show, :edit, :update, :destroy]
  11. before_action :set_data, only: [:new, :create]
  12. before_action :get_filters, only: [:index, :show, :edit, :new]
  13. # GET /cash_outs.json
  14. def index
  15. case current_user.usertype
  16. when "A"
  17. @cash_outs = CashOut.all.includes(:open_cash_register, :user, :received_by).order('cash_outs.created_at desc')
  18. when "G"
  19. @cash_outs = Pointsale.find(current_user.pointsale_id).cash_outs.includes(:open_cash_register, :user, :received_by).order('cash_outs.created_at desc')
  20. when "C"
  21. @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')
  22. end
  23. end
  24. # GET /cash_outs/1
  25. # GET /cash_outs/1.json
  26. def show
  27. @incomings = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1", @cash_out.open_cash_register.id).order('created_at')
  28. @outgoings = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '0' and status = 1", @cash_out.open_cash_register.id).order('created_at')
  29. @sales_total = @incomings.sum(:quantity)
  30. @expenses_total = @outgoings.sum(:quantity)
  31. end
  32. # GET /cash_outs/new
  33. def new
  34. @cash_out = CashOut.new
  35. @cash_out.cash_out_details.new
  36. @cash_out.received_cash = nil
  37. @cash_out.physical_cash = nil
  38. @cash_out.cash_fund = nil
  39. end
  40. # GET /cash_outs/1/edit
  41. def edit; end
  42. # POST /cash_outs
  43. # POST /cash_outs.json
  44. def create
  45. final_cash = 0
  46. @cash_out = CashOut.new(cash_out_params)
  47. open_cash_register = OpenCashRegister.find(params[:open_cash_register_id])
  48. @cash_out.amount_in = 0
  49. @cash_out.amount_out = 0
  50. @cash_out.open_cash_register_id = open_cash_register.id
  51. @cash_out.user_id = current_user.id
  52. @cash_out.cash_out_details.each do |detail|
  53. @cash_out.amount_in = @cash_out.amount_in + detail.incoming
  54. @cash_out.amount_out = @cash_out.amount_out + detail.outgoing
  55. if detail.payment_method_id == @cash_payment_method_id
  56. final_cash = detail.adjustment
  57. end
  58. end
  59. open_cash_register.expenses.update_all(status: 2) # registered
  60. respond_to do |format|
  61. message = "Corte de caja de #{@cash_out.open_cash_register.cash_register.name} realizado correctamente"
  62. @cash_out.audit_comment = message
  63. if @cash_out.save
  64. # eliminar el id del open cash de la sesion
  65. session.delete(:open_cash_register_id)
  66. open_cash_register.update_attributes(status: 'closed', final_cash: final_cash)
  67. format.js { flash[:success] = message }
  68. else
  69. format.js
  70. format.json { render json: @cash_out.errors, status: :unprocessable_entity }
  71. end
  72. end
  73. end
  74. def get_open_cash_registers
  75. respond_to do |format|
  76. format.js
  77. end
  78. end
  79. def opened_cash_registers
  80. @opened_cash_registers = OpenCashRegister.abiertas
  81. end
  82. def select_open_cash_to_close
  83. @open_cash_register_id = params[:open_cash_register_id]
  84. respond_to do |format|
  85. format.js
  86. end
  87. end
  88. def find_cash_outs_by_date
  89. if params[:begin_date] != "null" && params[:end_date] != "null"
  90. start_date = params[:begin_date].in_time_zone(Time.zone).beginning_of_day + 1.days
  91. end_date = params[:end_date].in_time_zone(Time.zone).end_of_day + 1.days
  92. else
  93. start_date = DateTime.now
  94. end_date = DateTime.now
  95. end
  96. @cash_outs = params[:pointsale_id] != "null" ? Pointsale.find(params[:pointsale_id]).cash_outs.includes(:open_cash_register, :user, :received_by).where(created_at: start_date..end_date).order(" id DESC ") : CashOut.all.includes(:open_cash_register, :user, :received_by).where(created_at: start_date..end_date).order('cash_outs.created_at desc')
  97. respond_to do |format|
  98. format.js
  99. end
  100. end
  101. def print_receipt
  102. # ticket para la venta
  103. respond_to do |format|
  104. @cash_out = CashOut.find(params[:cash_out_id])
  105. products_ids = @cash_out.open_cash_register.sales_details.joins(:sale).where("sales.status != 1").pluck(:product_id).join(",")
  106. @pointsale = OpenCashRegister.get_pointsale(@cash_out.open_cash_register_id, "open_cash_register")
  107. @initial_cash = @cash_out.open_cash_register.initial_cash
  108. @details = @cash_out.open_cash_register.products.present? ? 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") : Array.new
  109. all_sales = Sale.where("open_cash_register_id = (?) and status != 1", @cash_out.open_cash_register_id)
  110. @cash_sales = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1 and sale_id IN (?)", @cash_out.open_cash_register_id, all_sales.where(saletype: 1).pluck(:id)).sum(:quantity)
  111. @credit_sales = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1 and concept = 3", @cash_out.open_cash_register_id).sum(:quantity)
  112. @reserved_sales = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1 and sale_id IN (?)", @cash_out.open_cash_register_id, all_sales.where(saletype: 2).pluck(:id)).sum(:quantity)
  113. @details.each do |detail|
  114. detail["price_sale"] = Product.find(detail['product_id']).get_price_sale(@pointsale.id)
  115. detail["total_without_discount"] = detail["price_sale"] * detail['quantity'].to_f
  116. detail["discount"] = detail["total_without_discount"] - detail["total"].to_f
  117. end
  118. @expenses = Expense.where(open_cash_register_id: @cash_out.open_cash_register_id).activos
  119. format.pdf do
  120. 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, sales_total: @cash_sales, credit_sales: @credit_sales, reserved_sales: @reserved_sales }, show_as_html: params.key?('debug'), page_width: '80mm', page_height: '300mm'
  121. end
  122. end
  123. end
  124. private
  125. def set_data
  126. @cash_out.cash_out_details.destroy_all
  127. @cash_payment_method_id = PaymentMethod.find_by(isCash: 1).id
  128. @opened_cash_register = (params[:open_cash_register_id].blank? ? OpenCashRegister.find(session[:open_cash_register_id]) : OpenCashRegister.find(params[:open_cash_register_id]))
  129. if @opened_cash_register.present?
  130. @initial_cash = @opened_cash_register.initial_cash
  131. @incomings = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1", @opened_cash_register.id).order('created_at')
  132. @outgoings = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '0' and status = 1", @opened_cash_register.id).order('created_at')
  133. # all_sales = Sale.where("open_cash_register_id = (?) and status != 1", @opened_cash_register.id)
  134. sales_ids = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1", @opened_cash_register.id).pluck("DISTINCT sale_id")
  135. all_sales = Sale.activas.where("id IN (?)", sales_ids)
  136. @cash_sales = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1 and sale_id IN (?)", @opened_cash_register.id, all_sales.where(saletype: 1).pluck(:id)).sum(:quantity)
  137. @credit_sales = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1 and concept = 3", @opened_cash_register.id).sum(:quantity)
  138. @reserved_sales = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1 and sale_id IN (?)", @opened_cash_register.id, all_sales.where(saletype: 2).pluck(:id)).sum(:quantity)
  139. @expenses_total = @outgoings.sum(:quantity)
  140. end
  141. @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")
  142. @payments.each do |payment|
  143. payment["incoming"] = "0" if payment["incoming"].nil?
  144. payment["outgoing"] = "0" if payment["outgoing"].nil?
  145. payment["total"] = payment["incoming"].to_f - payment["outgoing"].to_f
  146. # si es el efectivo sumarle el fondo
  147. if payment["payment_method_id"].to_i == @cash_payment_method_id
  148. payment["total"] += @initial_cash
  149. end
  150. @cash_out.cash_out_details.build
  151. end
  152. end
  153. # Use callbacks to share common setup or constraints between actions.
  154. def set_cash_out
  155. @cash_out = CashOut.find(params[:id])
  156. end
  157. def get_filters
  158. @current_page = params[:current_page].blank? ? 1 : params[:current_page]
  159. @filter = params[:filter]
  160. end
  161. # Never trust parameters from the scary internet, only allow the white list through.
  162. def cash_out_params
  163. 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])
  164. end
  165. end