require_dependency "educode_sales/application_controller" module EducodeSales class InvoicesController < ApplicationController def index end def apply render layout: false end def create_apply business = Business.find(params[:business_id]) count = EducodeSales::InvoiceApply.where("created_at > ?", Time.now.beginning_of_day).size + 1 invoice_apply = InvoiceApply.find_or_initialize_by(business_id: business.id) do |d| d.number = Time.now.strftime("%y%m%d").gsub("-", "") + rand(10..99).to_s + ("%04d" % count) d.state = '审核中' end invoice_apply.staff = @current_admin invoice_apply.assign_attributes(apply_params) ActiveRecord::Base.transaction do invoice_apply.save! params[:details].each do |d| detail = invoice_apply.invoice_details.new({ sale_detail_id: d['id'], specification: d['specification'], business_id: business.id, unit: d['unit'], category: d['category'], state: '待开票', name: d['name'], num: d['num'], price: d['price'], amount: d['amount']}) detail.save! end end render_success end def details business = Business.find(params[:id]) invoice_apply = business.invoice_apply if invoice_apply if params[:state].present? @data = invoice_apply.invoice_details.where(state: params[:state]) else @data = invoice_apply.invoice_details end else @data = InvoiceDetail.none end @data = @data.page(params[:page]).per(params[:limit]) end def sales_details respond_to do |format| format.html do render layout: false end format.json do business = Business.find(params[:id]) @data = business.sales_details.page(params[:page]).per(params[:limit]) end end end def invoice_amount business = Business.find(params[:id]) render json: { actual_amount: business&.last_follow_up&.actual_amount.to_f.round(6), invoice_pass: business&.invoice_details&.sum(:amount).to_f.round(6), invoice_category: business&.invoice_apply&.category || '', is_tax_rebate: business&.invoice_apply&.is_tax_rebate.to_s, ticket_at: business&.invoice_apply&.ticket_at || '', name: business&.invoice_apply&.name || '', taxpaper_number: business&.invoice_apply&.taxpaper_number || '', address: business&.invoice_apply&.address || '', phone: business&.invoice_apply&.phone || '', bank: business&.invoice_apply&.bank || '', bank_number: business&.invoice_apply&.bank_number || '', } end def apply_records respond_to do |format| format.html do end format.js do @staffs = Staff.joins(:invoice_applys).includes(:user).distinct.map { |d| [d.user.real_name, d.id]} end format.json do @data = InvoiceApply unless @current_admin.is_admin? @data = @data.left_joins(business: [last_follow_up: :assign_follow_ups]).where("educode_sales_assign_follow_ups.staff_id = :staff OR educode_sales_invoice_applies.staff_id = :staff", staff: @current_admin.id) end if params[:q].present? && params[:q][:payer_name].present? @data = @data.where("payer_name like ?", "%#{params[:q][:payer_name]}%") end if params[:q].present? && params[:q][:business].present? @data = @data.joins(:business).where("educode_sales_businesses.name like ?", "%#{params[:q][:business]}%") end if params[:q].present? && params[:q][:number].present? @data = @data.where("educode_sales_invoice_applies.number like ?", "%#{params[:q][:number]}%") end if params[:q].present? && params[:q][:amount].present? @data = @data.where("educode_sales_invoice_applies.amount = ?", params[:q][:amount]) end if params[:q].present? && params[:q][:staff_id].present? @data = @data.where("educode_sales_invoice_applies.staff_id = ?", params[:q][:staff_id]) end if params[:q].present? && params[:q][:created_at].present? date = params[:q][:created_at].split(" - ") @data = @data.where("educode_sales_invoice_applies.created_at > ? AND educode_sales_invoice_applies.created_at < ?", date[0] + " 00:00:00", date[1] + " 23:59:59") end if params[:q].present? && params[:q][:date_at].present? date = params[:q][:date_at].split(" - ") @data = @data.joins(:invoices).where("educode_sales_invoices.date_at > ? AND educode_sales_invoices.date_at < ?", date[0] + " 00:00:00", date[1] + " 23:59:59") end if params[:q].present? && params[:q][:school].present? @data = @data.joins(business: :school).where("schools.name like ?", "%#{params[:q][:school]}%") end if params[:q].present? && params[:q][:invoice_number].present? @data = @data.joins(:invoices).where("educode_sales_invoices.number like ?", "%#{params[:q][:invoice_number]}%") end if params[:q].present? && params[:q][:date_at].present? date = params[:q][:date_at].split(" - ") @data = @data.where("date_at BETWEEN ? AND ?", date[0], date[1]) end if params[:q].present? && params[:q][:state].present? if params[:q][:state] == '已认领' @data = @data.having("claim_num > 0") elsif params[:q][:state] == '待认领' @data = @data.having("claim_num = 0 AND business_id IS NOT NULL") elsif params[:q][:state] == '无对应合同' @data = @data.having("claim_num = 0 AND business_id IS NULL") end end if params[:q].present? && params[:q][:amount].present? @data = @data.where(amount: params[:q][:amount]) end if params[:sort].present? && params[:sort][:field] @data = @data.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @data = @data.order(created_at: :desc) end @data = @data.page(params[:page]).per(params[:limit]) end end end def list invoice_apply = InvoiceApply.find(params[:id]) @data = invoice_apply.invoices @data = @data.order(created_at: :desc).page(params[:page]).per(params[:limit]) end def new render layout: false end def create invoice_apply = InvoiceApply.find(params[:invoice_apply_id]) invoice = invoice_apply.invoices.new(invoice_params) if invoice.save render_success else render_failure invoice end end def update invoice = Invoice.find(params[:id]) if invoice.update(invoice_params) render_success else render_failure invoice end end def destroy invoice = Invoice.find(params[:id]) if invoice.destroy render_success else render_failure invoice end end private def apply_params params.permit(:category, :is_tax_rebate, :ticket_at, :name, :taxpaper_number, :address, :phone, :bank, :bank_number) end def detail_params params.permit(:category, :specification, :unit, :num, :price, :amount) end def invoice_params params.permit(:number, :no_tax_amount, :taxt_amount, :amount, :date_at) end end end