2015-10-27 37 views
0

我想在导轨中创建发票。发票可以有物品,每个物品都会有数量,税金&价格。这是我们每天看到的典型发票。导轨进销存应用程序

为了创建发票什么是最好的办法。

什么是发票和项目的共同模式?

我知道的项目将是一个单独的模型。但是,我们如何才能有一个发票视图,这会创建发票和添加到它的项目?

我的意思是,一个新的发票页面中,会出现客户名单,和项目的名单,但在这里我不知道如何让足协当我创建发票。有没有我可以遵循的一个很好的例子?

请欣赏一些帮助。甚至只是通过我需要为了实现这个目标需要遵循的步骤散步...

这里是我的基本ERD

enter image description here

回答

0

相当宽泛的问题,这里就是我想要做的:

#app/models/invoice.rb 
class Invoice < ActiveRecord::Base 
    belongs_to :user 

    has_many :line_items 
    has_many :items, through: :line_items 

    accepts_nested_attributes_for :line_items 
end 

#app/models/line_item.rb 
class LineItem < ActiveRecord::Base 
    belongs_to :invoice 
    belongs_to :item 
end 

#app/models/item.rb 
class Item < ActiveRecord::Base 
    belongs_to :company 

    has_many :line_items 
    has_many :invoices, through: :line_items 
end 

- -

#app/models/user.rb 
class User < ActiveRecord::Base 
    has_many :invoices 
end 

这将是基准级“发票”关联结构 - 您的可以在其上构建/users

你的路线等可以如下:

#config/routes.rb 
resources :invoices 

#app/controllers/invoices_controller.rb 
class InvoicesController < ApplicationController 
    def new 
     @invoice = current_user.invoices.new 
     @invoice.line_items.build 
    end 

    def create 
     @invoice = current_user.invoices.new invoice_params 
     @invoice.save 
    end 
end 

那么你的看法会是这样的:

#app/views/invoices/new.html.erb 
<%= form_for @invoice do |f| %> 
    <%= f.fields_for :line_items do |l| %> 
    <%= f.text_field :quantity %> 
    <%= f.collection_select :product_id, Product.all, :id, :name %> 
    <% end %> 
    <%= f.submit %> 
<% end %> 

这将创建相应的@invoice,与您就可以打电话如下:

@user.invoices.first 

除此之外,我没有足够的地方具体信息可帮助明确

+0

谢谢你的反馈,我喜欢你迄今为止的建议。我会尝试这个,一旦我得到它的工作,或者如果我有任何问题(希望不是),会让你知道。干杯 –

+0

Jus想知道应该在项目表和line_item表内进行什么。我的意思是item_name价格数量&ect。就像我的情况一样,我喜欢current_company和调用现有的项目,我会这样current_company.items,所以我需要更改为current_company.line_items?我对物品和line_item以及它们内部的东西感到困惑。 –

0

我可以推荐使用payday gem?我已经在过去的应用程序中创建了发票模型,并且我会告诉你什么,它有时可能会非常棘手,具体取决于您正在构建的应用程序的类型。但除了便利因素之外,我喜欢使用这款宝石的原因是它也可以将您的发票作为可定制的PDF进行呈现。

这使得将项目添加到发票微风为好,例如从他们的GitHub页面:

invoice = Payday::Invoice.new(:invoice_number => 12) 
invoice.line_items << Payday::LineItem.new(:price => 20, :quantity => 5, :description => "Pants") 
invoice.line_items << Payday::LineItem.new(:price => 10, :quantity => 3, :description => "Shirts") 
invoice.line_items << Payday::LineItem.new(:price => 5, :quantity => 200, :description => "Hats") 
invoice.render_pdf_to_file("/path/to_file.pdf") 
+0

感谢您的建议:)我试图建立这种不使用大量的宝石。 –