Class: EducodeSales::InvoicesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/educode_sales/invoices_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin, #authenticate_request, #current_user, #filter, #is_commissioner_above?, #paginate, #render_failure, #render_success, #subject_members, #subject_staffs, #subject_url

Methods included from ApplicationHelper

#add_businesses_score, #base_url, #collection_amount_score, #completion_rate, #current?, #disk_filename, #get_businesses_chart, #handled_data, #handled_time_data, #handled_time_data_accurate, #relative_path, #signed_amount_score, #storage_path, #url_to_avatar, #visits_score

Instance Method Details

#applyObject



9
10
11
# File 'app/controllers/educode_sales/invoices_controller.rb', line 9

def apply
  render layout: false
end

#apply_recordsObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/controllers/educode_sales/invoices_controller.rb', line 87

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

#createObject



177
178
179
180
181
182
183
184
185
# File 'app/controllers/educode_sales/invoices_controller.rb', line 177

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

#create_applyObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/educode_sales/invoices_controller.rb', line 13

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

#destroyObject



196
197
198
199
200
201
202
203
# File 'app/controllers/educode_sales/invoices_controller.rb', line 196

def destroy
  invoice = Invoice.find(params[:id])
  if invoice.destroy
    render_success
  else
    render_failure invoice
  end
end

#detailsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/educode_sales/invoices_controller.rb', line 42

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

#indexObject



6
7
# File 'app/controllers/educode_sales/invoices_controller.rb', line 6

def index
end

#invoice_amountObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/educode_sales/invoices_controller.rb', line 69

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

#listObject



167
168
169
170
171
# File 'app/controllers/educode_sales/invoices_controller.rb', line 167

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

#newObject



173
174
175
# File 'app/controllers/educode_sales/invoices_controller.rb', line 173

def new
  render layout: false
end

#sales_detailsObject



57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/educode_sales/invoices_controller.rb', line 57

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

#updateObject



187
188
189
190
191
192
193
194
# File 'app/controllers/educode_sales/invoices_controller.rb', line 187

def update
  invoice = Invoice.find(params[:id])
  if invoice.update(invoice_params)
    render_success
  else
    render_failure invoice
  end
end