bookyt_stock 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/app/assets/stylesheets/bookyt_stock.sass +0 -0
- data/app/controllers/stocks_controller.rb +56 -0
- data/app/helpers/stock_helper.rb +15 -0
- data/app/models/stock.rb +92 -0
- data/app/views/stocks/_form.html.haml +14 -0
- data/app/views/stocks/_list.html.haml +11 -0
- data/app/views/stocks/_sidebar.html.haml +7 -0
- data/app/views/stocks/_stock.html.haml +10 -0
- data/app/views/stocks/edit.html.haml +5 -0
- data/app/views/stocks/index.html.haml +9 -0
- data/app/views/stocks/show.html.haml +35 -0
- data/app/views/stocks/write_downs.html.haml +20 -0
- data/config/locales/de.yml +30 -0
- data/config/routes.rb +12 -0
- data/lib/bookyt_stock/navigation.rb +12 -0
- data/lib/bookyt_stock/railtie.rb +10 -0
- data/lib/bookyt_stock/version.rb +3 -0
- data/lib/bookyt_stock.rb +3 -0
- metadata +84 -0
File without changes
|
@@ -0,0 +1,56 @@
|
|
1
|
+
class StocksController < AuthorizedController
|
2
|
+
# States
|
3
|
+
has_scope :by_state, :default => 'available', :only => :index
|
4
|
+
has_scope :by_text
|
5
|
+
|
6
|
+
# Actions
|
7
|
+
def new
|
8
|
+
|
9
|
+
# Defaults
|
10
|
+
stock_params = {
|
11
|
+
:state => 'available'
|
12
|
+
}
|
13
|
+
|
14
|
+
# Load and assign parent invoice
|
15
|
+
if params[:invoice_id]
|
16
|
+
invoice = Invoice.find(params[:invoice_id])
|
17
|
+
stock_params.merge!(
|
18
|
+
:title => invoice.title,
|
19
|
+
:amount => invoice.amount
|
20
|
+
)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Paramameters
|
24
|
+
stock_params.merge!(params[:stock] || {})
|
25
|
+
|
26
|
+
@stock = Stock.new(stock_params)
|
27
|
+
end
|
28
|
+
|
29
|
+
def create
|
30
|
+
@stock = Stock.new(params[:stock])
|
31
|
+
@stock.build_booking
|
32
|
+
|
33
|
+
create!
|
34
|
+
end
|
35
|
+
|
36
|
+
def write_downs
|
37
|
+
# use current date if not specified otherwise
|
38
|
+
params[:profit] ||= {}
|
39
|
+
|
40
|
+
# use current date if not specified otherwise
|
41
|
+
if params[:by_value_period]
|
42
|
+
@end_date = Date.parse(params[:by_value_period][:to])
|
43
|
+
@start_date = Date.parse(params[:by_value_period][:from])
|
44
|
+
else
|
45
|
+
@end_date = Date.today
|
46
|
+
@start_date = @end_date.to_time.advance(:years => -1, :days => 1).to_date
|
47
|
+
end
|
48
|
+
|
49
|
+
@date = @start_date..@end_date
|
50
|
+
|
51
|
+
@stocks = Stock.all
|
52
|
+
@stocks = @stocks.select{|stock|
|
53
|
+
stock.balance(@start_date) != 0.0 or stock.balance(@end_date) != 0.0
|
54
|
+
}
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module StockHelper
|
2
|
+
# Translate stock state
|
3
|
+
def t_stock_state(state)
|
4
|
+
t(state, :scope => 'stock.state')
|
5
|
+
end
|
6
|
+
|
7
|
+
# Provide translated stock states for select fields
|
8
|
+
def stock_states_as_collection
|
9
|
+
states = Stock::STATES
|
10
|
+
states.inject({}) do |result, state|
|
11
|
+
result[t_stock_state(state)] = state
|
12
|
+
result
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/app/models/stock.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
class Stock < ActiveRecord::Base
|
2
|
+
# Invoices
|
3
|
+
belongs_to :purchase_invoice, :class_name => 'Invoice'
|
4
|
+
belongs_to :selling_invoice, :class_name => 'Invoice'
|
5
|
+
|
6
|
+
# Validations
|
7
|
+
validates_presence_of :title, :amount, :state
|
8
|
+
|
9
|
+
# String
|
10
|
+
def to_s(format = :default)
|
11
|
+
title
|
12
|
+
end
|
13
|
+
|
14
|
+
# Search
|
15
|
+
# ======
|
16
|
+
scope :by_text, lambda {|value|
|
17
|
+
text = '%' + value + '%'
|
18
|
+
|
19
|
+
amount = value.delete("'").to_f
|
20
|
+
if amount == 0.0
|
21
|
+
amount = nil unless value.match(/^[0.]*$/)
|
22
|
+
end
|
23
|
+
|
24
|
+
date = nil
|
25
|
+
begin
|
26
|
+
date = Date.parse(value)
|
27
|
+
rescue ArgumentError
|
28
|
+
end
|
29
|
+
|
30
|
+
includes(:purchase_invoice, :selling_invoice).where("stocks.title LIKE :text OR stocks.remarks LIKE :text OR stocks.amount = :amount OR date(invoices.value_date) = :date", :text => text, :amount => amount, :date => date)
|
31
|
+
}
|
32
|
+
|
33
|
+
# States
|
34
|
+
# ======
|
35
|
+
STATES = ['available', 'amortized', 'sold', 'removed']
|
36
|
+
scope :by_state, lambda {|value|
|
37
|
+
where(:state => value) unless value == 'all'
|
38
|
+
}
|
39
|
+
|
40
|
+
# Period
|
41
|
+
# ======
|
42
|
+
scope :active_at, lambda {|value| bookings.balance(value) == 0}
|
43
|
+
|
44
|
+
# Bookings
|
45
|
+
# ========
|
46
|
+
include HasAccounts::Model
|
47
|
+
|
48
|
+
# Guess direct_account
|
49
|
+
#
|
50
|
+
# We simply take the first booking and exclude accounts with codes
|
51
|
+
# 1100 and 2000 (credit and debit invoices) as candidates.
|
52
|
+
def direct_account
|
53
|
+
# We don't care if no bookings
|
54
|
+
return nil if bookings.empty?
|
55
|
+
|
56
|
+
# Take any booking
|
57
|
+
booking = bookings.first
|
58
|
+
involved_accounts = [booking.credit_account, booking.debit_account]
|
59
|
+
|
60
|
+
relevant_account = involved_accounts - [Account.find_by_code("1100"), Account.find_by_code("2000")]
|
61
|
+
|
62
|
+
return relevant_account.first
|
63
|
+
end
|
64
|
+
|
65
|
+
# Build booking
|
66
|
+
#
|
67
|
+
# We use the value_date of the purchase invoice but our own amount.
|
68
|
+
def build_booking(params = {}, template_code = nil)
|
69
|
+
template_code = self.class.to_s.underscore + ':activate'
|
70
|
+
|
71
|
+
# Prepare booking parameters
|
72
|
+
booking_params = {:amount => amount}
|
73
|
+
if purchase_invoice
|
74
|
+
booking_params[:value_date] = purchase_invoice.value_date
|
75
|
+
else
|
76
|
+
booking_params[:value_date] = Date.today
|
77
|
+
end
|
78
|
+
booking_params.merge!(params)
|
79
|
+
|
80
|
+
# Build and assign booking
|
81
|
+
super(booking_params, template_code)
|
82
|
+
end
|
83
|
+
|
84
|
+
# Calculations
|
85
|
+
def write_downs(value_date)
|
86
|
+
bookings.direct_balance(value_date, Account.find_by_code('6900'))
|
87
|
+
end
|
88
|
+
|
89
|
+
def amount_changes(value_date)
|
90
|
+
-(balance(value_date.first) - balance(value_date.last) - write_downs(value_date))
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
= semantic_form_for resource do |f|
|
2
|
+
= f.semantic_errors
|
3
|
+
= f.inputs do
|
4
|
+
= f.input :state, :collection => stock_states_as_collection
|
5
|
+
= f.input :title, :input_html => {'data-autofocus' => true}
|
6
|
+
= f.input :amount
|
7
|
+
= f.input :remarks, :input_html => {:rows => 4}
|
8
|
+
|
9
|
+
= f.inputs t('title.invoices') do
|
10
|
+
= f.input :purchase_invoice, :input_html => {:class => 'combobox'}
|
11
|
+
= f.input :selling_invoice, :input_html => {:class => 'combobox'}
|
12
|
+
|
13
|
+
= f.buttons do
|
14
|
+
= f.commit_button
|
@@ -0,0 +1,11 @@
|
|
1
|
+
%table.list#stocks_list
|
2
|
+
%tr
|
3
|
+
%th= t_attr :title, Stock
|
4
|
+
%th= t_attr :purchase_invoice, Stock
|
5
|
+
%th= t_attr :direct_account, Stock
|
6
|
+
%th= t_attr :state, Stock
|
7
|
+
%th.currency= t_attr :amount, Stock
|
8
|
+
%th.currency= t_attr :balance, Stock
|
9
|
+
%th.action-links
|
10
|
+
|
11
|
+
= render @stocks
|
@@ -0,0 +1,10 @@
|
|
1
|
+
%tr[stock]
|
2
|
+
%td= link_to stock.title, stock, {'data-href-container' => 'tr'}
|
3
|
+
%td= link_to stock.purchase_invoice, stock.purchase_invoice if stock.purchase_invoice
|
4
|
+
%td= link_to stock.direct_account, stock.direct_account
|
5
|
+
%td= t_stock_state(stock.state)
|
6
|
+
%td.currency= currency_fmt(stock.amount)
|
7
|
+
%td.currency= currency_fmt(stock.balance)
|
8
|
+
%td.action-links
|
9
|
+
= list_link_for(:edit, stock)
|
10
|
+
= list_link_for(:delete, stock)
|
@@ -0,0 +1,35 @@
|
|
1
|
+
.contextual
|
2
|
+
= icon_link_to :pdf, :format => :pdf
|
3
|
+
= contextual_links_for
|
4
|
+
|
5
|
+
%h1= resource.title
|
6
|
+
.box
|
7
|
+
%table{:style => "width: 100%"}
|
8
|
+
%tr
|
9
|
+
%th= t_attr(:title)
|
10
|
+
%td= resource.title
|
11
|
+
%th= t_attr(:amount)
|
12
|
+
%td.currency= currency_fmt(resource.amount)
|
13
|
+
%tr
|
14
|
+
%th= t_attr(:direct_account)
|
15
|
+
%td= link_to resource.direct_account, resource.direct_account
|
16
|
+
%th= t_attr(:state)
|
17
|
+
%td.state= t_stock_state(resource.state)
|
18
|
+
%tr
|
19
|
+
%th= t_attr(:purchase_invoice)
|
20
|
+
%td= link_to resource.purchase_invoice, resource.purchase_invoice if resource.purchase_invoice
|
21
|
+
%th= t_attr(:selling_invoice)
|
22
|
+
%td= link_to resource.selling_invoice, resource.selling_invoice if resource.selling_invoice
|
23
|
+
%tr
|
24
|
+
%th= t_attr(:remarks)
|
25
|
+
%tr
|
26
|
+
%td{:style => 'white-space: pre-line; padding-left: 2em', :colspan => 4}<= resource.remarks
|
27
|
+
|
28
|
+
.contextual
|
29
|
+
= form_tag new_direct_booking_path(:direct_booking => {:reference_id => resource.id, :reference_type => resource_class.base_class}), :method => :get, :remote => true do
|
30
|
+
= collection_select :direct_booking, :booking_template_id, BookingTemplate.by_type(resource_class.name.underscore), :id, :title
|
31
|
+
= submit_tag t_action('new')
|
32
|
+
|
33
|
+
%h3= t('title.bookings')
|
34
|
+
#stock_booking_list
|
35
|
+
= render 'direct_bookings/list', :reference => resource
|
@@ -0,0 +1,20 @@
|
|
1
|
+
= contextual_links
|
2
|
+
|
3
|
+
%h1= t_title
|
4
|
+
|
5
|
+
%table.list#stocks_write_downs_list
|
6
|
+
%tr
|
7
|
+
%th= t_attr :title, Stock
|
8
|
+
%th.currency= t_attr :balance_begin, Stock
|
9
|
+
%th.currency= t_attr :change, Stock
|
10
|
+
%th.currency= t_attr :write_downs, Stock
|
11
|
+
%th.currency= t_attr :balance_end, Stock
|
12
|
+
- for stock in @stocks
|
13
|
+
%tr[stock]
|
14
|
+
%td= link_to stock.title, stock, {'data-href-container' => 'tr'}
|
15
|
+
%td.currency= currency_fmt(stock.balance(@start_date))
|
16
|
+
%td.currency= currency_fmt(stock.amount_changes(@date))
|
17
|
+
%td.currency= currency_fmt(stock.write_downs(@date))
|
18
|
+
%td.currency= currency_fmt(stock.balance(@end_date))
|
19
|
+
|
20
|
+
= render 'bookings/sidebar'
|
@@ -0,0 +1,30 @@
|
|
1
|
+
de:
|
2
|
+
activerecord:
|
3
|
+
models:
|
4
|
+
stock: Bestand
|
5
|
+
|
6
|
+
attributes:
|
7
|
+
stock:
|
8
|
+
invoice_id: Rechnungs Nr.
|
9
|
+
purchase_invoice: Kaufbeleg
|
10
|
+
selling_invoice: Verkaufsbeleg
|
11
|
+
amount: Anschaffungswert
|
12
|
+
balance: Buchwert
|
13
|
+
balance_begin: Buchwert Anfang
|
14
|
+
balance_end: Buchwert Ende
|
15
|
+
write_downs: Abschreibungen
|
16
|
+
change: Zu-/Abgänge
|
17
|
+
remarks: Bemerkungen
|
18
|
+
title: Titel
|
19
|
+
state: Status
|
20
|
+
direct_account: Konto
|
21
|
+
|
22
|
+
stock:
|
23
|
+
state:
|
24
|
+
available: vorhanden
|
25
|
+
amortized: amortisiert
|
26
|
+
sold: verkauft
|
27
|
+
removed: entsorgt
|
28
|
+
|
29
|
+
title:
|
30
|
+
stocks: Posten
|
data/config/routes.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
module BookytStock
|
2
|
+
module Navigation
|
3
|
+
def setup_bookyt_stock(navigation)
|
4
|
+
navigation.item :stocks, t_title(:index, Stock), stocks_path,
|
5
|
+
:if => Proc.new { user_signed_in? } do |stocks|
|
6
|
+
stocks.item :stocks, t_title(:index, Stock), stocks_path, :highlights_on => /\/stocks($|\/[0-9]*($|\/.*))/
|
7
|
+
stocks.item :new_stock, t_title(:new, Stock), new_stock_path
|
8
|
+
stocks.item :write_downs, t_title(:write_downs, Stock), write_downs_stocks_path, :highlights_on => /\/stocks\/write_downs($|\?)/
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/bookyt_stock.rb
ADDED
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bookyt_stock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "Simon H\xC3\xBCrlimann (CyT)"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-08-03 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: This plugin extends bookyt with asset/stock functionality.
|
23
|
+
email: simon.huerlimann@cyt.ch
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- app/assets/stylesheets/bookyt_stock.sass
|
32
|
+
- app/controllers/stocks_controller.rb
|
33
|
+
- app/helpers/stock_helper.rb
|
34
|
+
- app/models/stock.rb
|
35
|
+
- app/views/stocks/_form.html.haml
|
36
|
+
- app/views/stocks/_list.html.haml
|
37
|
+
- app/views/stocks/_sidebar.html.haml
|
38
|
+
- app/views/stocks/_stock.html.haml
|
39
|
+
- app/views/stocks/edit.html.haml
|
40
|
+
- app/views/stocks/index.html.haml
|
41
|
+
- app/views/stocks/show.html.haml
|
42
|
+
- app/views/stocks/write_downs.html.haml
|
43
|
+
- config/locales/de.yml
|
44
|
+
- config/routes.rb
|
45
|
+
- lib/bookyt_stock.rb
|
46
|
+
- lib/bookyt_stock/navigation.rb
|
47
|
+
- lib/bookyt_stock/railtie.rb
|
48
|
+
- lib/bookyt_stock/version.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: https://github.com/huerlisi/bookyt_stock
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 3
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.5.2
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Stock plugin for bookyt
|
83
|
+
test_files: []
|
84
|
+
|