cash_outs_controller.rb 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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
  42. end
  43. # POST /cash_outs
  44. # POST /cash_outs.json
  45. def create
  46. final_cash = 0
  47. @cash_out = CashOut.new(cash_out_params)
  48. open_cash_register = OpenCashRegister.find(params[:open_cash_register_id])
  49. @cash_out.amount_in = 0
  50. @cash_out.amount_out = 0
  51. @cash_out.open_cash_register_id = open_cash_register.id
  52. @cash_out.user_id = current_user.id
  53. @cash_out.cash_out_details.each do |detail|
  54. @cash_out.amount_in = @cash_out.amount_in + detail.incoming
  55. @cash_out.amount_out = @cash_out.amount_out + detail.outgoing
  56. if detail.payment_method_id == @cash_payment_method_id
  57. final_cash = detail.adjustment
  58. end
  59. end
  60. open_cash_register.expenses.update_all(:status => 2) #registered
  61. respond_to do |format|
  62. message = "Corte de caja de #{@cash_out.open_cash_register.cash_register.name} realizado correctamente"
  63. @cash_out.audit_comment = message
  64. if @cash_out.save
  65. open_cash_register.update_attributes(:status => 'closed', :final_cash => final_cash)
  66. format.js { flash[:success] = message }
  67. else
  68. format.js
  69. format.json { render json: @cash_out.errors, status: :unprocessable_entity }
  70. end
  71. end
  72. end
  73. # PATCH/PUT /cash_outs/1
  74. # PATCH/PUT /cash_outs/1.json
  75. def update
  76. respond_to do |format|
  77. if @cash_out.update(cash_out_params)
  78. format.html { redirect_to @cash_out, notice: 'Cash out was successfully updated.' }
  79. format.json { render :show, status: :ok, location: @cash_out }
  80. else
  81. format.html { render :edit }
  82. format.json { render json: @cash_out.errors, status: :unprocessable_entity }
  83. end
  84. end
  85. end
  86. def get_open_cash_registers
  87. respond_to do |format|
  88. format.js
  89. end
  90. end
  91. def opened_cash_registers
  92. @opened_cash_registers = OpenCashRegister.abiertas
  93. end
  94. def select_open_cash_to_close
  95. @open_cash_register_id = params[:open_cash_register_id]
  96. respond_to do |format|
  97. format.js
  98. end
  99. end
  100. # DELETE /cash_outs/1
  101. # DELETE /cash_outs/1.json
  102. def destroy
  103. @cash_out.destroy
  104. respond_to do |format|
  105. format.html { redirect_to cash_outs_url, notice: 'Cash out was successfully destroyed.' }
  106. format.json { head :no_content }
  107. end
  108. end
  109. def find_cash_outs_by_date
  110. respond_to do |format|
  111. startDate = DateTime.parse(params[:begin_date])
  112. endDate = DateTime.parse(params[:end_date])
  113. @cash_outs = Pointsale.find(params[:pointsale_id]).cash_outs.includes(:open_cash_register, :user, :received_by).where(:created_at => startDate..endDate).order(" id DESC ")
  114. format.js
  115. end
  116. end
  117. def print_receipt
  118. #ticket para la venta
  119. respond_to do |format|
  120. @cash_out = CashOut.find(params[:cash_out_id])
  121. products_ids = @cash_out.open_cash_register.sales_details.joins(:sale).where("sales.status != 1").pluck(:product_id).join(",")
  122. @pointsale = OpenCashRegister.get_pointsale(@cash_out.open_cash_register_id, "open_cash_register")
  123. @initial_cash = @cash_out.open_cash_register.initial_cash
  124. if @cash_out.open_cash_register.products.size > 0
  125. @details = ActiveRecord::Base.connection.exec_query("SELECT
  126. 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
  127. FROM
  128. sales_details
  129. INNER JOIN sales ON sales_details.sale_id = sales.id
  130. INNER JOIN (
  131. SELECT
  132. products. ID as product_id,
  133. category_id,
  134. categories.category,
  135. products. NAME,
  136. categories.parent_id,
  137. parents.category as parent
  138. FROM
  139. products
  140. INNER JOIN categories_products ON categories_products.product_id = products. ID
  141. INNER JOIN categories ON categories. ID = categories_products.category_id
  142. LEFT JOIN categories as parents ON parents.ID = categories.parent_id
  143. WHERE
  144. (products. ID IN (#{products_ids}) )
  145. ) sub ON (
  146. sales_details.product_id = sub.product_id
  147. )
  148. WHERE
  149. sales.open_cash_register_id = #{@cash_out.open_cash_register_id} and sales.status != 1
  150. GROUP BY sales_details.product_id, sub.category, sub.name, sub.parent
  151. ORDER BY
  152. sub.category, sub.parent")
  153. else
  154. @details = Array.new
  155. end
  156. @details.each do |detail|
  157. detail["price_sale"] = Product.find(detail['product_id']).get_price_sale(@pointsale.id)
  158. detail["total_without_discount"] = detail["price_sale"] * detail['quantity'].to_f
  159. detail["discount"] = detail["total_without_discount"] - detail["total"].to_f
  160. end
  161. @expenses = Expense.where(:open_cash_register_id => @cash_out.open_cash_register_id).activos
  162. format.pdf do
  163. render pdf: "ticket_corte_#{@cash_out.id}",
  164. template: "cash_outs/receipt.pdf.erb",
  165. layout: 'receipt.html.erb',
  166. locals: { :cash_out => @cash_out, :details => @details, :pointsale => @pointsale, :expenses => @expenses, :initial_cash => @initial_cash },
  167. show_as_html: params.key?('debug'),
  168. page_width: '80mm',
  169. page_height: '300mm'
  170. end
  171. end
  172. end
  173. private
  174. def set_data
  175. @cash_out.cash_out_details.destroy_all
  176. @cash_payment_method_id = PaymentMethod.find_by(:isCash => 1).id
  177. @opened_cash_register = (params[:open_cash_register_id].blank? ? current_user.get_open_cash_register : OpenCashRegister.find(params[:open_cash_register_id]))
  178. if @opened_cash_register.present?
  179. @initial_cash = @opened_cash_register.initial_cash
  180. @incomings = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '1' and status = 1", @opened_cash_register.id).order('created_at')
  181. @outgoings = CashRegistersMove.where("open_cash_register_id = (?) and move_type = '0' and status = 1", @opened_cash_register.id).order('created_at')
  182. @sales_total = @incomings.sum(:quantity)
  183. @expenses_total = @outgoings.sum(:quantity)
  184. end
  185. @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")
  186. @payments.each do |payment|
  187. payment["incoming"] = "0" if payment["incoming"].nil?
  188. payment["outgoing"] = "0" if payment["outgoing"].nil?
  189. payment["total"] = payment["incoming"].to_f - payment["outgoing"].to_f
  190. @cash_out.cash_out_details.build
  191. end
  192. end
  193. # Use callbacks to share common setup or constraints between actions.
  194. def set_cash_out
  195. @cash_out = CashOut.find(params[:id])
  196. end
  197. def get_filters
  198. if params[:current_page].blank?
  199. @current_page = 1
  200. else
  201. @current_page = params[:current_page]
  202. end
  203. @filter = params[:filter]
  204. end
  205. # Never trust parameters from the scary internet, only allow the white list through.
  206. def cash_out_params
  207. 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])
  208. end
  209. end