Merhaba , pandemide uzun zamandır yazmadım. Ama boş durmadım. Uzun zamandır küçük işyerim için faturaları kayıt altına alabileceğim bir uygulama arıyorum. İnternette birkaç hazır site de kullandım ama hiç biri bana uymuyor ben hep onlara uymak zorunda kalıyorum. Ben kendi uygulamamı yaparım dedim. Başladım dönmeye. Önce kodsuz uygulama platformları denedim, AppGyver, Buble, FireBase, AirTable vs. baktım hepsi beni kısıtlıyor bir türlü istediğim gibi olmuyor. Döndüm yine eski ve her zaman dostum RubyOnRails'e. Debelene debelene ortaya bir uygulama çıkardım. Bu yazımda uygulamayı nasıl oluşturduğumu sizinle paylaşacağım.
Önce veri tabanı tasarımından başlamak lazım. En korktuğum yer burası, eğer veri tabanını yanlış tasarlarsak sonra bir sürü yanlış yollarda boğulabiliriz. Şöyle bir veritabanı tasarladım.
Önce firmalar tablosu geliyor, arkasından firmalarda çalışan insanların bilgilerinin kaydedildiği bir tablo ve projelerin kaydedildiği bir tablo var. Projelere bağlı çalışanları ilişkilendireceğimiz bir bağlantı referans tablosu var. Projeler için yaptığımız harcamalar ve müşteriye kesilen faturaları kaydedeceğimiz iki adet birbirine çok benzer tablomuz var. Fatura satırları içeren ve içerdiği bilgilere ait bağlantılı tablolar da resmi bitiriyor. Sonradan bu faturalara ait ödemeler için de bir tablo ekledim yeri geldiğinde onu da açıklarım.
Projeye Başlayalım
Çalışmak istediğimiz klasörde bir konsol açıp, yeni proje üretme komutunu girelim,
rails new TakipApp
Ben Windows'ta yaparken bundle komutu https adres ile çalışmıyor, uğraşmadan gemfile içindeki gem server adresini https den http olarak değiştirdim.
TakipApp/Gemfile
source 'http://rubygems.org'
git_source(:github) do |repo_name|
...
Bakalım uygulama çalışıyor mu? Konsolda şunları girelim sırayla,
cd TakipApp
bundle
rails s
Artık http://localhost:3000 adresini tarayıcıda açınca Rails yeni uygulama başlangıç sayfası gelmiş olmalı,
Bu kadarını yapmayı başardıysak bundan sonrası çok kolay. Demek isterdim tabi, ama buraya gelince "yürümeye başladım, hadi koşayım" demek geliyor içimden.
Firmalar Tablosu
rails g scaffold Company name:string address:string taxOffice:string taxNumber:string webSite:string logo:string note:string
Companies tablosu firmalara ait bilgileri barındırıyor. name sütununda firma ismi, address sütununda firma adresi, taxOffice sütununda vergi dairesi, taxNumber sütununda vergi numarası, webSite sütununda firma web sitesi adresi, logo sütununda firma logosuna ait resim dosyasının adı ve note sütununda da firmaya ait ne not almak istersek yazarız.
Veri tabanına tabloyu eklemek için migrate komutu çalıştıralım ve server'ı tekrar başlatalım.
rails db:migrate
rails s
Tarayıcıda http://localhost:3000/companies adresini açtığımızda Rails'in bizim için hazırladığı sayfayı görürüz.
Görünümü düzenlemeye geçmeden önce birkaç test verisi girelim.
Bootstrap CSS ile Güzelleştirelim
Server'ı durduralım. Bootstrap CSS kütüphanesini kullanmak için Gemfile dosyamıza şu satırları ekliyoruz,
gem "bootstrap-sass"
gem "jquery-rails"
Arkasından konsolda ,
bundle
yazarak Bootstrap CSS kütüphanesi ve jQuery JavaScript kütüphanesini uygulamamıza ekliyoruz. Bootstrap kullanmak için yapılacak bir şey daha var application.css dosyası adını. application.scss olarak değiştirdikten sonra içine ilave şunları giriyoruz,
@import "bootstrap-sprockets";
@import "bootstrap";
Ayrıca default stillerden kurtulmak için
*= require_tree .
*= require_self
Saturlarını siliyoruz.
jQuery kütüphanesini kullanabilmek için de application.js dosyasına şu satırları ekliyoruz,
//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
Şimdi server'ı tekrar çalıştırıp sayfayı yenilersek Bootstrap CSS etkisini Yazılarda görürüz. Tabi bu yetmiyor, şimdi görselimizi biraz düzenleyelim.
Başlıktan başlayalım, index.html.erb dosyasındaki başlık şöyle
<h1>Companies</h1>
<div class="row">
<div class="col-sm-4"><h1>Firmalar</h1></div>
<div class="col-sm"><%= link_to 'Yeni firma', new_company_path, class: "btn btn-primary", style: "margin: 20px;" %></div>
</div>
Başlığı ve yeni firma ekleme linkini aynı row içine alıp yan yana gösteriyoruz.
<div class="row">
<table class="table">
<thead>
<tr>
<th>İsim</th>
<th>Adres</th>
<th>Vergi Dairesi</th>
<th>Vergi No</th>
<th>Web Sitesi</th>
<th>Logo</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @companies.each do |company| %>
<tr>
<td><%= link_to company.name, company %></td>
<td><%= company.address %></td>
<td><%= company.taxOffice %></td>
<td><%= company.taxNumber %></td>
<td><%= link_to company.webSite, company.webSite, target: "_blank" %></td>
<td><%= image_tag company.logo, style: "height: 70px; width: auto; max-width: 150px;" %></td>
<td><%= link_to 'Düzenle', edit_company_path(company), class: "btn btn-primary" %></td>
<td><%= link_to 'Sil', company, method: :delete, data: { confirm: "Firma " + company.name + " silinecek emin misin?" }, class: "btn btn-danger" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<body>
<nav class="navbar navbar-default">
<a href="http://www.turkpoem.com" class="navbar-brand" target="_blank">POEM</a>
<a href="/companies" class="btn btn-success">Firmalar</a>
</nav>
<div class="container">
<%= yield %>
</div>
</body>
<h1>Yeni firma ekle</h1>
<%= render 'form', company: @company, etiket: "Ekle", logo_txt: "/images/" %>
<%= link_to 'Geri', companies_path, class:"btn btn-primary" %>
<%= form_with(model: company, local: true) do |form| %>
<% if company.errors.any? %>
...
<div class="form-group col-sm-12">
<%= form.label "İsim :" %>
<%= form.text_field :name, id: :company_name, class: "form-control" %>
</div>
<div class="form-group col-sm-12">
<%= form.label "Adres :" %>
<%= form.text_field :address, id: :company_address, class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= form.label "Vergi dairesi" %>
<%= form.text_field :taxOffice, id: :company_taxOffice, class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= form.label "Vergi No :" %>
<%= form.text_field :taxNumber, id: :company_taxNumber, class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= form.label "Web sitesi :" %>
<%= form.text_field :webSite, id: :company_webSite, class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= form.label "Logo :" %>
<%= form.text_field :logo, id: :company_logo, class: "form-control", value: logo_txt %>
</div>
<div class="form-group col-sm-6">
<%= form.label :note %>
<%= form.text_field :note, id: :company_note, class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= image_tag("/images/logo.png", alt: "rss feed", id: "logo-image", style: "width: auto; max-height: 100%; max-width: 100%") %>
</div>
<div class="actions form-group">
<%= form.submit etiket, class: "btn btn-success" %>
</div>
<% end %>
<%= javascript_tag do %>
$("#logo-image").attr("src", "<%= logo_txt %>");
$("#company_logo").change(function(){
$("#logo-image").attr("src", this.value);
});
<% end %>
<h1>Firma düzenle</h1>
<%= render 'form', company: @company, etiket: "Kaydet", logo_txt: @company.logo %>
<%= link_to 'Göster', @company, class: "btn btn-primary" %> |
<%= link_to 'Liste', companies_path, class: "btn btn-info" %>
Kişiler Tablosu
rails g scaffold Person name:string email:string phone:string note:string card:string company:belongs_to
rails db:migrate
...
<body>
<nav class="navbar navbar-default">
<a href="http://www.turkpoem.com" class="navbar-brand" target="_blank">POEM</a>
<a href="/companies" class="btn btn-success">Firmalar</a>
<a href="/people" class="btn btn-success">Kişiler</a>
</nav>
...
<h1>Yeni kişi ekle</h1>
<%= render 'form', person: @person, etiket: "Ekle", card_txt: "/cards/" %>
<%= link_to 'Geri', people_path, class:"btn btn-primary" %>
<h1>Kişi Düzenleme</h1>
<%= render 'form', person: @person, etiket: "Kaydet", card_txt: @person.card %>
<%= link_to 'Göster', @person, class: "btn btn-primary" %> |
<%= link_to 'Liste', people_path, class: "btn btn-info" %>
<%= form_with(model: person, local: true) do |form| %>
<% if person.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(person.errors.count, "error") %> prohibited this person from being saved:</h2>
<ul>
<% person.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group col-sm-12">
<%= form.label "İsim :" %>
<%= form.text_field :name, id: :person_name, class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= form.label :email %>
<%= form.text_field :email, id: :person_email, class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= form.label "Telefon :" %>
<%= form.text_field :phone, id: :person_phone, class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= form.label "Firması :" %>
<%= form.text_field :company_id, id: :person_company_id, class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= form.label "Kartvizit :" %>
<%= form.text_field :card, id: :person_card, class: "form-control", value: card_txt %>
</div>
<div class="form-group col-sm-6">
<%= form.label "Notlar :" %>
<%= form.text_field :note, id: :person_note, class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= image_tag("/cards/umit_kayacik.jpg", alt: "person card", id: "card-image", style: "width: auto; max-height: 100%; max-width: 100%") %>
</div>
<div class="actions form-group">
<%= form.submit etiket, class: "btn btn-success" %>
</div>
<% end %>
<%= javascript_tag do %>
$("#card-image").attr("src", "<%= card_txt %>");
$("#person_card").change(function(){
$("#card-image").attr("src", this.value);
});
<% end %>
...
<div class="form-group col-sm-6">
<%= form.label "Firması :" %>
<%= form.collection_select(:company_id, Company.all, :id, :name, {:include_blank => true},
{class: "form-control"}) %>
</div>
...
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-sm-4"><h1>Kişiler</h1></div>
<div class="col-sm"><%= link_to 'Yeni kişi', new_person_path, class: "btn btn-primary", style: "margin: 20px;" %></div>
</div>
<div class="row">
<table class="table table-striped">
<thead>
<tr>
<th>İsim</th>
<th>Firması</th>
<th>Email</th>
<th>Telefon</th>
<th>Kartvizit</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @people.each do |person| %>
<tr>
<td><%= link_to person.name, person %></td>
<td><%= link_to person.company.name, person.company %></td>
<td><%= mail_to person.email %></td>
<td><%= person.phone %></td>
<td><%= image_tag person.card, style: "height: 70px; width: auto; max-width: 150px;" %></td>
<td><%= link_to 'Düzenle', edit_person_path(person), class: "btn btn-primary" %></td>
<td><%= link_to 'Sil', person, method: :delete, data: { confirm: person.name + ' kişisi silinecek emin misin?' }, class: "btn btn-danger" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-sm-8">
<p>
<strong class="col-sm-3">İsim :</strong>
<%= @person.name %>
</p>
<p>
<strong class="col-sm-3">Firması :</strong>
<%= @person.company.name %>
</p>
<p>
<strong class="col-sm-3">Email :</strong>
<%= @person.email %>
</p>
<p>
<strong class="col-sm-3">Telefon :</strong>
<%= @person.phone %>
</p>
<p>
<strong class="col-sm-3">Kartvizit :</strong>
<%= @person.card %>
</p>
<p>
<strong class="col-sm-3">Notlar :</strong>
<%= @person.note %>
</p>
</div>
<div class="col-sm-4">
<img src="<%= @person.card %>" alt="kartvizit", style="width: auto; max-height: 100%; max-width: 100%" />
</div>
</div>
<%= link_to 'Düzenle', edit_person_path(@person), class: "btn btn-primary" %> |
<%= link_to 'Liste', people_path, class: "btn btn-info" %>
Projeler Tablosu
rails g scaffold Project name:string note:string folder:string company:belongs_to
rails db:migrate
<h1>Yeni Proje</h1>
<%= render 'form', project: @project, etiket: "Ekle" %>
<div class="row"></div>
<%= link_to 'Geri', projects_path, class:"btn btn-primary" %>
<%= form_with(model: project, local: true) do |form| %>
<% if project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group col-sm-8">
<%= form.label "İsim :" %>
<%= form.text_field :name, id: :project_name, class: "form-control" %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Firması :" %>
<%= form.collection_select(:company_id, Company.all, :id, :name, {:include_blank => true}, {class: "form-control"}) %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Klasör :" %>
<%= form.text_field :folder, id: :project_folder, class: "form-control" %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Notlar :" %>
<%= form.text_field :note, id: :project_note, class: "form-control" %>
</div>
<div class="actions form-group col-sm-8">
<%= form.submit etiket, class: "btn btn-success" %>
</div>
<% end %>
<h1>Proje Düzenleme</h1>
<%= render 'form', project: @project, etiket: "Kaydet" %>
<div class="row"></div>
<%= link_to 'Göster', @project, class: "btn btn-primary" %> |
<%= link_to 'Liste', projects_path, class: "btn btn-info" %>
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-sm-10">
<p>
<strong class="col-sm-2">İsim :</strong>
<%= @project.name %>
</p>
<p>
<strong class="col-sm-2">Firma :</strong>
<%= @project.company.name %>
</p>
<p>
<strong class="col-sm-2">Klasör :</strong>
<%= @project.folder %>
</p>
<p>
<strong class="col-sm-2">Not :</strong>
<%= @project.note %>
</p>
</div>
</div>
<%= link_to 'Düzenle', edit_project_path(@project), class: "btn btn-primary" %> |
<%= link_to 'Liste', projects_path, class: "btn btn-info" %>
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-sm-4"><h1>Projeler</h1></div>
<div class="col-sm"><%= link_to 'Yeni Proje', new_project_path, class: "btn btn-primary", style: "margin: 20px;" %></div>
</div>
<div class="row">
<table class="table table-striped">
<thead>
<tr>
<th>İsim</th>
<th>Firma</th>
<th>Klasör</th>
<th>Notlar</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @projects.each do |project| %>
<tr>
<td><%= link_to project.name, project %></td>
<td><%= link_to project.company.name, project.company %></td>
<td><%= project.folder %></td>
<td class="col-sm-3"><%= project.note %></td>
<td><%= link_to 'Düzenle', edit_project_path(project), class: "btn btn-primary" %></td>
<td><%= link_to 'Sil', project, method: :delete, data: { confirm: project.name + 'projesi silinecek emin misin??' }, class: "btn btn-danger" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<br>
Ve son olarak menü barda projeler için link ilavesi,
...
<body>
<nav class="navbar navbar-default">
<a href="http://www.turkpoem.com" class="navbar-brand" target="_blank">POEM</a>
<a href="/companies" class="btn btn-success">Firmalar</a>
<a href="/people" class="btn btn-success">Kişiler</a>
<a href="/projects" class="btn btn-success">Projeler</a>
</nav>
...
Proje Kişileri Ara Tablosu
rails g scaffold ProjectPerson project:belongs_to person:belongs_to
rails db:migrate
<p id="notice"><%= notice %></p>
<h1>Project People</h1>
<table class="table">
<thead>
<tr>
<th>Project</th>
<th>Person</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @project_people.each do |project_person| %>
<tr>
<td><%= project_person.project.name %></td>
<td><%= project_person.person.name %></td>
<td><%= link_to 'Show', project_person %></td>
<td><%= link_to 'Edit', edit_project_person_path(project_person) %></td>
<td><%= link_to 'Destroy', project_person, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Project Person', new_project_person_path %>
<h1>New Project Person</h1>
<%= render 'form', project_person: @project_person, etiket: "Ekle" %>
<%= link_to 'Back', project_people_path %>
<h1>Editing Project Person</h1>
<%= render 'form', project_person: @project_person, etiket: "Kaydet" %>
<%= link_to 'Show', @project_person %> |
<%= link_to 'Back', project_people_path %>
<%= form_with(model: project_person, local: true) do |form| %>
<% if project_person.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(project_person.errors.count, "error") %> prohibited this project_person from being saved:</h2>
<ul>
<% project_person.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group col-sm-6">
<%= form.label "Proje :" %>
<%= form.collection_select(:project_id,
Project.all, :id, :name,
{:include_blank => true},
{class: "form-control",
id: "project-select",
:"data-remote" => "true", # UJS içinmiş
:"data-url" => url_for(controller: "project_people", action: "getData"), # burada veriyi istiyoruz
:"data-type" => "json", # jQuery ile dönen veriyi parse etmek için
# bu select seçim yapıldığı anda ajax çağrısını çakar
}) %>
</div>
<div class="form-group col-sm-6">
<%= form.label "Kişi :" %>
<%= form.collection_select(:person_id, Person.all, :id, :name,
# bu selectin değerleri proje seçimi değişince değişecek
{:include_blank => true},
{class: "form-control",
id: "second-select"}) %>
</div>
<div class="actions form-group">
<%= form.submit etiket, class: "btn btn-success" %>
</div>
</div>
<% end %>
...
def getData
@data_from_project_select = project_person_params["project_id"] # seçilen proje id'si
@data_for_person_select = Project.find(@data_from_project_select).company.people.all
# kişi seçimi iin o projenin firmasındaki kişileri topla
render json: @data_for_person_select.map{|p| [p.id, p.name]} # json olarak çağrıya dönüş yap
end
...
Rails.application.routes.draw do
resources :project_people do
collection do
get "getData"
end
end
resources :projects
resources :people
resources :companies
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
class Company < ApplicationRecord
has_many :people
has_many :projects
end
//= require bootstrap-sprockets
$(document).ready(function(){
// ajax request başarılı olduysa
$("#project-select").on("ajax:success", function(evt, xhr, settings){
// ikinci select elemanını seç
var secondSelect = $("#second-select");
//içini boşalt
secondSelect.empty();
// gelen data id ve name olarak gelecekti
$.each(evt.detail[0], function(index, value){
// bir optiomn elemanı oluştur
var opt = $("<option/>");
// gelen data [id, name] şeklinde olacaktı
opt.attr("value", value[0]);
opt.text(value[1]);
// option elemanını select'e ekle
opt.appendTo(secondSelect);
});
});
});
...
<%= link_to 'Düzenle', edit_project_path(@project), class: "btn btn-primary" %> |
<%= link_to 'Liste', projects_path, class: "btn btn-info" %>
<div class="row">
<div class="col-sm-4">
<h3>İlgili Kişiler</h3>
<ul>
<% @project.project_people.each do |project_person| %>
<li>
<%= link_to project_person.person.name, project_person.person %>
<%= link_to "-", "destroyPerson?id=" + project_person.id.to_s, class: "btn btn-sm" %>
</li>
<% end %>
<%= form_with(url: "addPerson", remote: true) do |form| %>
<%= form.collection_select :person_id, @project.company.people.all, :id, :name, { :include_blank => true } %>
<%= form.hidden_field :project_id, value: @project.id %>
<%= form.submit '+', { class: "btn" } %>
<% end %>
</ul>
</div>
<div class="col-sm-8">
</div>
</div>
class Person < ApplicationRecord
belongs_to :company
has_many :project_people
end
class Project < ApplicationRecord
belongs_to :company
has_many :project_people
end
...
def addPerson
redirect_to controller: "project_people", action: "create_from_project",
person_id: params["person_id"], project_id: params["project_id"]
end
def destroyPerson
redirect_to controller: "project_people", action: "destroy_from_project",
id: params["id"]
end
...
...
resources :projects do
collection do
post "addPerson"
get "destroyPerson"
end
end
resources :people
resources :companies
...
def create_from_project
@project_person = ProjectPerson.new(person_id: params["person_id"], project_id: params["project_id"])
respond_to do |format|
if @project_person.save
format.html { redirect_to @project_person.project, notice: 'Project person was successfully created.' }
format.json { render :show, status: :created, location: @project_person.project }
else
format.html { render :new }
format.json { render json: @project_person.errors, status: :unprocessable_entity }
end
end
end
...
def destroy_from_project
@project_person = ProjectPerson.find(params["id"])
@project = @project_person.project
@project_person.destroy
respond_to do |format|
format.html { redirect_to @project, notice: 'Project person was successfully destroyed.' }
format.json { head :no_content }
end
end
...
Rails.application.routes.draw do
resources :project_people do
collection do
get "getData"
get "create_from_project"
get "destroy_from_project"
end
end
resources :projects do
...
Satış Faturaları
rails g scaffold Sales project:belongs_to name:string salesDate:date paymentDate:date
rails db:migrate
...
<nav class="navbar navbar-default">
<a href="http://www.turkpoem.com" class="navbar-brand" target="_blank">POEM</a>
<a href="/companies" class="btn btn-success">Firmalar</a>
<a href="/people" class="btn btn-success">Kişiler</a>
<a href="/projects" class="btn btn-success">Projeler</a>
<a href="/sales" class="btn btn-success">Satışlar</a>
</nav>
...
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-sm-4"><h1>Satışlar</h1></div>
<div class="col-sm"><%= link_to 'Fatura Ekle', new_sale_path, class: "btn btn-primary", style: "margin: 20px;" %></div>
</div>
<div class="row">
<table class="table table-striped">
<thead>
<tr>
<th>Proje</th>
<th>İsim</th>
<th>Satış Tarihi</th>
<th>Ödeme Tarihi</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @sales.each do |sale| %>
<tr>
<td><%= link_to sale.project.name, sale.project %></td>
<td><%= link_to sale.name, sale %></td>
<td><%= sale.salesDate %></td>
<td><%= sale.paymentDate %></td>
<td><%= link_to 'Düzenle', edit_sale_path(sale), class: "btn btn-primary" %></td>
<td><%= link_to 'Sil', sale, method: :delete, data: { confirm: sale.name + ' faturası silinecek emin misin?' }, class: "btn btn-danger" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<br>
<h1>Yeni Fatura</h1>
<%= render 'form', sale: @sale, etiket: "Ekle" %>
<div class="row"></div>
<%= link_to 'Geri', sales_path, class:"btn btn-primary" %>
<h1>Fatura Düzenleme</h1>
<%= render 'form', sale: @sale, etiket: "Kaydet" %>
<div class="row"></div>
<%= link_to 'Göster', @sale, class: "btn btn-primary" %> |
<%= link_to 'Liste', sales_path, class: "btn btn-info" %>
<%= form_with(model: sale, local: true) do |form| %>
<% if sale.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(sale.errors.count, "error") %> prohibited this sale from being saved:</h2>
<ul>
<% sale.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group col-sm-8">
<%= form.label "Firması :" %>
<%= form.collection_select("company_id", Company.all, :id, :name,
{:include_blank => true,
selected: @sale.project.nil? ? nil : @sale.project.company.id},
{ class: "form-control", id: "company-select" }) %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Projesi :" %>
<%= form.collection_select(:project_id, @sale.project.nil? ? Project.all : Project.where(company_id: @sale.project.company_id), :id, :name,
{ include_blank: true },
{ class: "form-control", id: "project-select" }) %>
</div>
<div class="form-group col-sm-8">
<%= form.label "İsim :" %>
<%= form.text_field :name, id: :sale_name, class: "form-control" %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Fatura Tarihi :" %><br/>
<%= form.date_select :salesDate, id: :sale_salesDate,
:start_year => 2016,
:end_year => 2030 %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Ödeme Tarihi" %><br/>
<%= form.date_select :paymentDate, id: :sale_paymentDate,
:start_year => 2016,
:end_year => 2030 %>
</div>
<div class="actions form-group col-sm-8">
<%= form.submit etiket, class: "btn btn-success" %>
</div>
<% end %>
<%= javascript_tag do %>
$("#company-select").change(function(){
$.ajax({url: "/companies/getProjects?id=" + this.value, success: function(result){
var project_select = $("#project-select");
project_select.empty();
$.each(result, function(index, value){
var opt = $("<option/>");
opt.attr("value", value[0]);
opt.text(value[1]);
opt.appendTo(project_select);
});
}});
});
<% end %>
...
def getProjects
render json: Company.find(params["id"]).projects.map{|p| [p.id, p.name]}
end
private
...
...
resources :people
resources :companies do
collection do
get "getProjects"
end
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-sm-10">
<p>
<strong class="col-sm-2">Projesi :</strong>
<%= link_to @sale.project.name, @sale.project %>
</p>
<p>
<strong class="col-sm-2">İsmi :</strong>
<%= @sale.name %>
</p>
<p>
<strong class="col-sm-2">Fatura Tarihi ::</strong>
<%= @sale.salesDate %>
</p>
<p>
<strong class="col-sm-2">Ödeme Tarihi :</strong>
<%= @sale.paymentDate %>
</p>
</div>
<div class="col-sm-2">
</div>
</div>
<%= link_to 'Düzenle', edit_sale_path(@sale), class: "btn btn-primary" %> |
<%= link_to 'Liste', sales_path, class: "btn btn-info" %>
<hr/>
Fatura Satırları
rails g scaffold Unit name:string
rails db:migrate
rails g scaffold Tax name:string value:float
rails db:migrate
rails g scaffold InvoiceLine sale:belongs_to kod:string product:string unitPrice:float qty:float unit:belongs_to tax:belongs_to
rails db:migrate
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-sm-4"><h1>Fatura Satırları</h1></div>
<div class="col-sm"><%= link_to 'Yeni fatura satırı', new_invoice_line_path, class: "btn btn-primary", style: "margin: 20px;" %></div>
</div>
<div class="row">
<table class="table table-striped">
<thead>
<tr>
<th>Kodu</th>
<th>Satış Faturası</th>
<th>Ürün</th>
<th>Fiyatı</th>
<th>Miktar</th>
<th>Birim</th>
<th>Vergi</th>
<th>Toplam</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @invoice_lines.each do |invoice_line| %>
<tr>
<td><%= invoice_line.kod %></td>
<td><%= link_to invoice_line.sale.name, invoice_line.sale %></td>
<td><%= link_to invoice_line.product, invoice_line %></td>
<td><%= invoice_line.unitPrice %></td>
<td><%= invoice_line.qty %></td>
<td><%= invoice_line.unit.name %></td>
<td><%= invoice_line.tax.name + "-%.2f" % (invoice_line.tax.value *
invoice_line.qty * invoice_line.unitPrice) %></td>
<td><%= "%.2f" % ((invoice_line.tax.value + 1) * invoice_line.qty * invoice_line.unitPrice) %></td>
<td><%= link_to 'Düzenle', edit_invoice_line_path(invoice_line), class: "btn btn-primary" %></td>
<td><%= link_to 'Sil', invoice_line, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<br>
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-sm-12">
<p>
<strong class="col-sm-3">Satış Faturası :</strong>
<%= @invoice_line.sale.nil? ? "..." : (link_to @invoice_line.sale.name, @invoice_line.sale) %>
</p>
<p>
<strong class="col-sm-3">Kodu :</strong>
<%= @invoice_line.kod %>
</p>
<p>
<strong class="col-sm-3">Ürün :</strong>
<%= @invoice_line.product %>
</p>
<p>
<strong class="col-sm-3">Fiyatı :</strong>
<%= @invoice_line.unitPrice %>
</p>
<p>
<strong class="col-sm-3">Miktar :</strong>
<%= @invoice_line.qty %>
</p>
<p>
<strong class="col-sm-3">Birim :</strong>
<%= @invoice_line.unit.name %>
</p>
<p>
<strong class="col-sm-3">KDV :</strong>
<%= @invoice_line.tax.name + "-%.2f" % (@invoice_line.tax.value *
@invoice_line.qty * @invoice_line.unitPrice) %>
</p>
<p>
<strong class="col-sm-3">Toplam :</strong>
<%= "%.2f" % ((@invoice_line.tax.value + 1) * @invoice_line.qty * @invoice_line.unitPrice) %>
</p>
</div>
</div>
<%= link_to 'Düzenle', edit_invoice_line_path(@invoice_line), class: "btn btn-primary" %> |
<%= link_to 'Liste', invoice_lines_path, class: "btn btn-info" %>
<h1>Yeni Fatura Satırı</h1>
<%= render 'form', invoice_line: @invoice_line, etiket: "Ekle" %>
<div class="row"></div>
<%= link_to 'Geri', invoice_lines_path, class:"btn btn-primary" %>
<h1>Fatura Satırı Düzenleme</h1>
<%= render 'form', invoice_line: @invoice_line, etiket: "Kaydet" %>
<div class="row"></div>
<%= link_to 'Göster', @invoice_line, class: "btn btn-primary" %> |
<%= link_to 'Liste', invoice_lines_path, class: "btn btn-info" %>
<%= form_with(model: invoice_line, local: true) do |form| %>
<% if invoice_line.errors.any? %>
...
<% end %>
<div class="form-group col-sm-8">
<%= form.label "Firması :" %>
<%= form.collection_select("company_id", Company.all, :id, :name,
{:include_blank => true,
selected: @invoice_line.sale.nil? ? nil : @invoice_line.sale.project.company_id},
{ class: "form-control", id: "company-select" }) %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Projesi :" %>
<%= form.collection_select("project_id", @invoice_line.sale.nil? ? Project.all :
Project.where(company_id: @invoice_line.sale.project.company_id), :id, :name,
{ include_blank: true,
selected: @invoice_line.sale.nil? ? nil : @invoice_line.sale.project_id },
{ class: "form-control", id: "project-select" }) %>
</div>
<div class="form-group col-sm-6">
<%= form.label "Satış Faturası :" %>
<%= form.collection_select(:sale_id, @invoice_line.sale.nil? ? Sale.all : Sale.where(project_id: @invoice_line.sale.project_id), :id, :name,
{ include_blank: true,
selected: @invoice_line.sale.nil? ? nil : @invoice_line.sale.id },
{ class: "form-control", id: "sale-select" }) %>
</div>
<div class="form-group col-sm-6">
<%= form.label "Kodu :" %>
<%= form.text_field :kod, id: :invoice_line_kod, class: "form-control" %>
</div>
<div class="form-group col-sm-6">
<%= form.label "Ürün :" %>
<%= form.text_field :product, id: :invoice_line_product, class: "form-control" %>
</div>
<div class="form-group col-sm-3">
<%= form.label "Fiyatı :" %>
<%= form.text_field :unitPrice, id: :invoice_line_unitPrice, class: "form-control" %>
</div>
<div class="form-group col-sm-3">
<%= form.label "Miktar :" %>
<%= form.text_field :qty, id: :invoice_line_qty, class: "form-control" %>
</div>
<div class="form-group col-sm-3">
<%= form.label "Birimi :" %>
<%= form.collection_select(:unit_id, Unit.all, :id, :name, {},
{ class: "form-control"}) %>
</div>
<div class="form-group col-sm-3">
<%= form.label "KDV :" %>
<%= form.collection_select(:tax_id, Tax.all, :id, :name, {},
{ class: "form-control", id: "invoice_line_tax"}) %>
</div>
<div class="actions form-group col-sm-8">
<%= form.submit etiket, class: "btn btn-success" %>
</div>
<% end %>
...
<%= javascript_tag do %>
$("#company-select").change(function(){
if(this.value){
$.ajax({url: "/companies/getProjects?id=" + this.value, success: function(result){
var project_select = $("#project-select");
project_select.empty();
$.each(result, function(index, value){
var opt = $("<option/>");
opt.attr("value", value[0]);
opt.text(value[1]);
opt.appendTo(project_select);
});
$("<option/>").attr("selected", "selected").attr("value", 0).text("").prependTo(project_select);
}});
};
});
$("#project-select").change(function(){
if(this.value){
$.ajax({url: "/projects/getSales?id=" + this.value, success: function(result){
var sale_select = $("#sale-select");
sale_select.empty();
$.each(result, function(index, value){
var opt = $("<option/>");
opt.attr("value", value[0]);
opt.text(value[1]);
opt.appendTo(sale_select);
});
$("<option/>").attr("selected", "selected").attr("value", 0).text("").prependTo(sale_select);
}});
};
});
<% end %>
...
def getSales
render json: Project.find(params["id"]).sales.map{|p| [p.id, p.name]}
end
private
...
...
resources :projects do
collection do
post "addPerson"
get "destroyPerson"
get "getSales"
end
end
...
class Project < ApplicationRecord
belongs_to :company
has_many :project_people
has_many :sales
end
Proje Ayrıntısına Fatura Listesi Eklenmesi
...
<div class="col-sm-8">
<strong class="h3">Satış Faturaları</strong>
<%= link_to '+', new_sale_path + "?project_id=" + @project.id.to_s, class: "btn btn-success", style: "margin: 20px;" %>
<table class="table table-striped">
<thead>
<tr>
<th>İsim</th>
<th>Satış Tarihi</th>
<th>Ödeme Tarihi</th>
<th>Toplam</th>
</tr>
</thead>
<tbody>
<% @project.sales.each do |sale| %>
<tr>
<td><%= link_to sale.name, sale %></td>
<td><%= sale.salesDate %></td>
<td><%= sale.paymentDate %></td>
<td><%= invoice_total(sale) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
...
module SalesHelper
def invoice_total(sale)
total = 0
sale.invoice_lines.each do |line|
total += (line.tax.value + 1) * line.qty * line.unitPrice
end
"%.2f" % total
end
end
class Sale < ApplicationRecord
belongs_to :project
has_many :invoice_lines
end
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-sm-8">
<p>
<strong class="col-sm-3">İsim:</strong>
<%= @company.name %>
</p>
<p>
<strong class="col-sm-3">Adres :</strong>
<%= @company.address %>
</p>
<p>
<strong class="col-sm-3">Vergi dairesi:</strong>
<%= @company.taxOffice %>
</p>
<p>
<strong class="col-sm-3">Vergi numarası:</strong>
<%= @company.taxNumber %>
</p>
<p>
<strong class="col-sm-3">Web sitesi:</strong>
<%= @company.webSite %>
</p>
<p>
<strong class="col-sm-3">Logo :</strong>
<%= @company.logo %>
</p>
<p>
<strong class="col-sm-3">Notlar :</strong>
<%= @company.note %>
</p>
</div>
<div class="col-sm-4">
<img src="<%= @company.logo %>" alt="logo", style="width: auto; max-height: 100%; max-width: 100%" />
</div>
</div>
<%= link_to 'Düzenle', edit_company_path(@company), class: "btn btn-primary" %> |
<%= link_to 'Liste', companies_path, class: "btn btn-info" %>
<div class="row">
<div class="col-sm-6">
<h3>Kişiler</h3>
<hr/>
<% @company.people.each do |person| %>
<p><strong><%= link_to person.name, person %></strong> - <%= mail_to person.email %> - <%= person.phone %></p>
<% end %>
</div>
<div class="col-sm-6">
<h3>Projeler</h3>
<hr/>
<% @company.projects.each do |project| %>
<p><strong><%= link_to project.name, project %></strong> - <%= project.folder %></p>
<% end %>
</div>
</div>
<hr/>
<div class="row">
<div class="col-sm-6">
<h3>Satışlar</h3>
<hr/>
<table class="table table-striped">
<thead>
<tr>
<th>İsim</th>
<th>Satış Tarihi</th>
<th>Ödeme Tarihi</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @company.projects.each do |project| %>
<% project.sales.each do |sale| %>
<tr>
<td><%= link_to sale.name, sale %></td>
<td><%= sale.salesDate %></td>
<td><%= sale.paymentDate %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
<div class="col-sm-6">
<h3>Alımlar</h3>
<hr/>
</div>
</div>
...
<%= link_to 'Liste', sales_path, class: "btn btn-info" %>
<hr/>
<div class="row">
<div class="col-sm-12">
<strong class="h3">Fatura Satırları</strong>
<%= link_to '+', new_invoice_line_path + "?sale_id=" + @sale.id.to_s, class: "btn btn-success", style: "margin: 20px;" %>
</div>
<div class="col-sm-12">
<table class="table table-striped">
<thead>
<tr>
<th>Kod</th>
<th>Ürün</th>
<th>Fiyat</th>
<th>Miktar</th>
<th>Birim</th>
<th>KDV</th>
<th>Toplam</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @sale.invoice_lines.each do |invoice_line| %>
<tr>
<td><%= invoice_line.kod %></td>
<td><%= link_to invoice_line.product, invoice_line %></td>
<td><%= invoice_line.unitPrice %></td>
<td><%= invoice_line.qty %></td>
<td><%= invoice_line.unit.name %></td>
<td><%= invoice_line.tax.name + "-%.2f" % (invoice_line.tax.value *
invoice_line.qty * invoice_line.unitPrice) %></td>
<td>
<% total = (invoice_line.tax.value + 1) * invoice_line.qty * invoice_line.unitPrice %>
<%= "%.2f" % (total) %>
</td>
<td><%= link_to 'Düzenle', edit_invoice_line_path(invoice_line), class: "btn btn-primary" %></td>
<td><%= link_to 'Sil', invoice_line, method: :delete, data: { confirm: invoice_line.product + ' satırı silinecek emin misin?' }, class: "btn btn-danger" %></td>
</tr>
<% end %>
<tr>
<td colspan="5"></td>
<td><strong>Fatura<br> Toplamı :</strong></td>
<td><strong class="h3" id="invoice-total"><%= invoice_total(@sale) %></strong></td>
</tr>
</tbody>
</table>
</div>
</div>
...
# GET /invoice_lines/new
def new
@invoice_line = InvoiceLine.new
if !params[:sale_id].nil?
@invoice_line.sale = Sale.find(params[:sale_id])
end
end
...
...
def new
@sale = Sale.new
if !params[:project_id].nil?
@sale.project = Project.find(params[:project_id])
end
end
...
Ödemeler Tablosu
rails g scaffold Payment sale:belongs_to note:string date:date value:float
rails db:migrate
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-sm-4"><h1>Ödemeler</h1></div>
<div class="col-sm"><%= link_to 'Yeni Ödeme', new_payment_path, class: "btn btn-primary", style: "margin: 20px;" %></div>
</div>
<div class="row">
<table class="table table-striped">
<thead>
<tr>
<th>Satış Faturası</th>
<th>Not</th>
<th>Tarih</th>
<th>Değer</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @payments.each do |payment| %>
<tr>
<td><%= link_to payment.sale.name, payment.sale %></td>
<td><%= link_to payment.note, payment %></td>
<td><%= payment.date %></td>
<td><%= payment.value %></td>
<td><%= link_to 'Düzenle', edit_payment_path(payment), class: "btn btn-primary" %></td>
<td><%= link_to 'Sil', payment, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<br>
<h1>Yeni Ödeme</h1>
<%= render 'form', payment: @payment, etiket: "Ekle" %>
<div class="row"></div>
<%= link_to 'Geri', payments_path, class:"btn btn-primary" %>
<h1>Ödeme Düzenleme</h1>
<%= render 'form', payment: @payment, etiket: "Kaydet" %>
<div class="row"></div>
<%= link_to 'Göster', @payment, class: "btn btn-primary" %> |
<%= link_to 'Liste', payments_path, class: "btn btn-info" %>
<%= form_with(model: payment, local: true) do |form| %>
...
...
<div class="form-group col-sm-8">
<%= form.label "Firması :" %>
<%= form.collection_select("company_id", Company.all, :id, :name,
{:include_blank => true,
selected: @payment.sale.nil? ? nil : @payment.sale.project.company_id},
{ class: "form-control", id: "company-select" }) %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Projesi :" %>
<%= form.collection_select("project_id", @payment.sale.nil? ? Project.all : Project.where(company_id: @payment.sale.project.company_id), :id, :name,
{ include_blank: true,
selected: @payment.sale.nil? ? nil : @payment.sale.project_id },
{ class: "form-control", id: "project-select" }) %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Satış Faturası :" %>
<%= form.collection_select(:sale_id, @payment.sale.nil? ? Sale.all : Sale.where(project_id: @payment.sale.project_id), :id, :name,
{ include_blank: true,
selected: @payment.sale.nil? ? nil : @payment.sale.id },
{ class: "form-control", id: "sale-select" }) %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Not :" %>
<%= form.text_field :note, id: :payment_note, class: "form-control" %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Tarih :" %>
<%= form.date_select :date, id: :payment_date, class: "form-control" %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Ödenen Miktar :" %>
<%= form.text_field :value, id: :payment_value, class: "form-control" %>
</div>
<div class="actions form-group col-sm-8">
<%= form.submit etiket, class: "btn btn-success" %>
</div>
<% end %>
<%= javascript_tag do %>
$("#company-select").change(function(){
if(this.value){
$.ajax({url: "/companies/getProjects?id=" + this.value, success: function(result){
var project_select = $("#project-select");
project_select.empty();
$.each(result, function(index, value){
var opt = $("<option/>");
opt.attr("value", value[0]);
opt.text(value[1]);
opt.appendTo(project_select);
});
$("<option/>").attr("selected", "selected").attr("value", 0).text("").prependTo(project_select);
}});
};
});
$("#project-select").change(function(){
if(this.value){
$.ajax({url: "/projects/getSales?id=" + this.value, success: function(result){
var sale_select = $("#sale-select");
sale_select.empty();
$.each(result, function(index, value){
var opt = $("<option/>");
opt.attr("value", value[0]);
opt.text(value[1]);
opt.appendTo(sale_select);
});
$("<option/>").attr("selected", "selected").attr("value", 0).text("").prependTo(sale_select);
}});
};
});
<% end %>
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-sm-12">
<p>
<strong class="col-sm-3">Satış Faturası :</strong>
<%= link_to @payment.sale.name, @payment.sale %>
</p>
<p>
<strong class="col-sm-3">Not :</strong>
<%= @payment.note %>
</p>
<p>
<strong class="col-sm-3">Tarih :</strong>
<%= @payment.date %>
</p>
<p>
<strong class="col-sm-3">Ödenen :</strong>
<%= @payment.value %>
</p>
</div>
</div>
<%= link_to 'Düzenle', edit_payment_path(@payment), class: "btn btn-primary" %> |
<%= link_to 'Liste', payments_path, class: "btn btn-info" %>
Fatura Ayrıntısına Ödemeler Listesi Eki
class Sale < ApplicationRecord
belongs_to :project
has_many :invoice_lines
has_many :payments
end
...
<div class="row">
<div class="col-sm-12">
<strong class="h3">Ödemeler</strong>
<%= link_to '+', new_payment_path + "?sale_id=" + @sale.id.to_s, class: "btn btn-success", style: "margin: 20px;" %>
</div>
<div class="col-sm"></div>
<hr/>
<div class="col-sm-12">
<table class="table table-striped">
<thead>
<tr>
<th>Not</th>
<th>Tarih</th>
<th>Değer</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @sale.payments.each do |payment| %>
<tr>
<td><%= link_to payment.note, payment %></td>
<td><%= payment.date %></td>
<td><%= payment.value %></td>
<td><%= link_to 'Düzenle', edit_payment_path(payment), class: "btn btn-primary" %></td>
<td><%= link_to 'Sil', payment, method: :delete, data: { confirm: payment.note + ' satırı silinecek emin misin?' }, class: "btn btn-danger" %></td>
</tr>
<% end %>
<tr>
<td colspan="1"></td>
<td><strong>Ödemeler<br> Toplamı :</strong></td>
<td><strong class="h3" id="payment-total"><%= payment_total(@sale) %></strong></td>
</tr>
</tbody>
</table>
</div>
</div>
...
def payment_total(sale)
total = 0
sale.payments.each do |payment|
total += payment.value
end
"%.2f" % total
end
...
# GET /payments/new
def new
@payment = Payment.new
if !params[:sale_id].nil?
@payment.sale = Sale.find(params[:sale_id])
end
end
...
...
</div>
<div class="col-sm-2">
<h3>Net Alacak</h3>
<h3 id="net-total" class="text-center">YOK</h3>
</div>
</div>
<%= link_to 'Düzenle', edit_sale_path(@sale), class: "btn btn-primary" %> |
...
...
<%= javascript_tag do %>
$(function(){
var netTotal = $("#invoice-total").text() - $("#payment-total").text();
$("#net-total").text(netTotal.toFixed(2));
if(netTotal > 1.0)
$("#net-total").css("background-color", "pink");
else $("#net-total").css("background-color", "lime");
});
<% end %>
...
Alacakların Diğer Sayfalarda Gösterimi
...
<div class="row">
<table class="table table-striped">
<thead>
<tr>
<th>Proje</th>
<th>İsim</th>
<th>Satış Tarihi</th>
<th>Ödeme Tarihi</th>
<th>Toplam</th>
<th>Alacak</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @sales.each do |sale| %>
<tr>
<td><%= link_to sale.project.name, sale.project %></td>
<td><%= link_to sale.name, sale %></td>
<td><%= sale.salesDate %></td>
<td><%= sale.paymentDate %></td>
<td><%= invoice_total(sale) %></td>
<% net_total = invoice_total(sale).to_f - payment_total(sale).to_f %>
<td class="h4" style="color:<% if (net_total > 1) %>red<% else %>blue<% end %>"><%= net_total %></td>
<td><%= link_to 'Düzenle', edit_sale_path(sale), class: "btn btn-primary" %></td>
<td><%= link_to 'Sil', sale, method: :delete, data: { confirm: sale.name + ' faturası silinecek emin misin?' }, class: "btn btn-danger" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<br>
...
<div class="col-sm-8">
<strong class="h3">Satış Faturaları</strong>
<%= link_to '+', new_sale_path + "?project_id=" + @project.id.to_s, class: "btn btn-success", style: "margin: 20px;" %>
<table class="table table-striped">
<thead>
<tr>
<th>İsim</th>
<th>Satış Tarihi</th>
<th>Ödeme Tarihi</th>
<th>Toplam</th>
<th>Alacak</th>
</tr>
</thead>
<tbody>
<% @project.sales.each do |sale| %>
<tr>
<td><%= link_to sale.name, sale %></td>
<td><%= sale.salesDate %></td>
<td><%= sale.paymentDate %></td>
<td><%= invoice_total(sale) %></td>
<% net = invoice_total(sale).to_f - payment_total(sale).to_f %>
<td style="color: <%= net > 1 ? 'red' : 'blue' %>;"><%= "%.2f" % net %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
...
Alımlar Tablosu
rails g scaffold Purchase project:belongs_to name:string salesDate:date paymentDate:date
rails db:migrate
class Project < ApplicationRecord
belongs_to :company
has_many :project_people
has_many :sales
has_many :purchases
end
...
<nav class="navbar navbar-default">
<a href="http://www.turkpoem.com" class="navbar-brand" target="_blank">POEM</a>
<a href="/companies" class="btn btn-success">Firmalar</a>
<a href="/people" class="btn btn-success">Kişiler</a>
<a href="/projects" class="btn btn-success">Projeler</a>
<a href="/sales" class="btn btn-success">Satışlar</a>
<a href="/purchases" class="btn btn-success">Alımlar</a>
</nav>
...
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-sm-4"><h1>Alımlar</h1></div>
<div class="col-sm"><%= link_to 'Yeni Alım Faturası', new_purchase_path, class: "btn btn-primary", style: "margin: 20px;" %></div>
</div>
<div class="row">
<table class="table table-striped">
<thead>
<tr>
<th>Proje</th>
<th>İsim</th>
<th>Alım Tarihi</th>
<th>Ödeme Tarihi</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @purchases.each do |purchase| %>
<tr>
<td><%= link_to purchase.project.name, purchase.project %></td>
<td><%= link_to purchase.name, purchase %></td>
<td><%= purchase.salesDate %></td>
<td><%= purchase.paymentDate %></td>
<td><%= link_to 'Düzenle', edit_purchase_path(purchase), class: "btn btn-primary" %></td>
<td><%= link_to 'Sil', purchase, method: :delete, data: { confirm: purchase.name + ' faturası silinecek emin misin?' }, class: "btn btn-danger" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<br>
<h1>Yeni Alım Faturası</h1>
<%= render 'form', purchase: @purchase, etiket: "Ekle" %>
<div class="row"></div>
<%= link_to 'Geri', purchases_path, class:"btn btn-primary" %>
<h1>Alım Faturası Düzenleme</h1>
<%= render 'form', purchase: @purchase, etiket: "Kaydet" %>
<div class="row"></div>
<%= link_to 'Göster', @purchase, class: "btn btn-primary" %> |
<%= link_to 'Liste', purchases_path, class: "btn btn-info" %>
...
<div class="form-group col-sm-8">
<%= form.label "Firması :" %>
<%= form.collection_select("company_id", Company.all, :id, :name,
{:include_blank => true,
selected: @purchase.project.nil? ? nil : @purchase.project.company.id},
{ class: "form-control", id: "company-select" }) %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Projesi :" %>
<%= form.collection_select(:project_id, @purchase.project.nil? ? Project.all : Project.where(company_id: @purchase.project.company_id), :id, :name,
{ include_blank: true },
{ class: "form-control", id: "project-select" }) %>
</div>
<div class="form-group col-sm-8">
<%= form.label "İsim :" %>
<%= form.text_field :name, id: :purchase_name, class: "form-control" %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Fatura Tarihi :" %>
<%= form.date_select :salesDate, id: :purchase_salesDate,
:start_year => 2016,
:end_year => 2030 %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Ödeme Tarihi" %>
<%= form.date_select :paymentDate, id: :purchase_paymentDate,
:start_year => 2016,
:end_year => 2030 %>
</div>
<div class="actions form-group col-sm-8">
<%= form.submit etiket, class: "btn btn-success" %>
</div>
<% end %>
<%= javascript_tag do %>
$("#company-select").change(function(){
$.ajax({url: "/companies/getProjects?id=" + this.value, success: function(result){
var project_select = $("#project-select");
project_select.empty();
$.each(result, function(index, value){
var opt = $("<option/>");
opt.attr("value", value[0]);
opt.text(value[1]);
opt.appendTo(project_select);
});
$("<option/>").attr("selected", "selected").attr("value", 0).text("").prependTo(project_select);
}});
});
<% end %>
<p id="notice"><%= notice %></p>
<div class="row">
<div class="col-sm-10">
<p>
<strong class="col-sm-2">Projesi :</strong>
<%= link_to @purchase.project.name, @purchase.project %>
</p>
<p>
<strong class="col-sm-2">İsmi :</strong>
<%= @purchase.name %>
</p>
<p>
<strong class="col-sm-2">Fatura Tarihi ::</strong>
<%= @purchase.salesDate %>
</p>
<p>
<strong class="col-sm-2">Ödeme Tarihi :</strong>
<%= @purchase.paymentDate %>
</p>
</div>
<div class="col-sm-2">
</div>
</div>
<%= link_to 'Düzenle', edit_purchase_path(@purchase), class: "btn btn-primary" %> |
<%= link_to 'Liste', purchases_path, class: "btn btn-info" %>
<hr/>
Fatura Satırlarına Alım Faturalarının Eklenmesi
rails g migration AddPurchaseToInvoiceLines purchase:belongs_to
rails db:migrate
...
<table class="table table-striped">
<thead>
<tr>
<th>Kodu</th>
<th>Satış Faturası</th>
<th>Alım Faturası</th>
...
<tbody>
<% @invoice_lines.each do |invoice_line| %>
<tr>
<td><%= invoice_line.kod %></td>
<td>
<%= invoice_line.sale_id.nil? ? "..." :
(link_to invoice_line.sale.name, invoice_line.sale) %></td>
<td>
<%= invoice_line.purchase_id.nil? ? "..." :
(link_to invoice_line.purchase.name, invoice_line.purchase) %></td>
...
...
<div class="row">
<div class="col-sm-12">
<strong class="h3">Fatura Satırları</strong>
<%= link_to '+', new_invoice_line_path + "?purchase_id=" + @purchase.id.to_s, class: "btn btn-success", style: "margin: 20px;" %>
</div>
<div class="col-sm-12">
<table class="table table-striped">
<thead>
<tr>
<th>Kod</th>
<th>Ürün</th>
<th>Fiyat</th>
<th>Miktar</th>
<th>Birim</th>
<th>KDV</th>
<th>Toplam</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @purchase.invoice_lines.each do |invoice_line| %>
<tr>
<td><%= invoice_line.kod %></td>
<td><%= link_to invoice_line.product, invoice_line %></td>
<td><%= invoice_line.unitPrice %></td>
<td><%= invoice_line.qty %></td>
<td><%= invoice_line.unit.name %></td>
<td><%= invoice_line.tax.name + "-%.2f" % (invoice_line.tax.value *
invoice_line.qty * invoice_line.unitPrice) %></td>
<td>
<% total = (invoice_line.tax.value + 1) * invoice_line.qty * invoice_line.unitPrice %>
<%= "%.2f" % (total) %>
</td>
<td><%= link_to 'Düzenle', edit_invoice_line_path(invoice_line), class: "btn btn-primary" %></td>
<td><%= link_to 'Sil', invoice_line, method: :delete, data: { confirm: invoice_line.product + ' satırı silinecek emin misin?' }, class: "btn btn-danger" %></td>
</tr>
<% end %>
<tr>
<td colspan="5"></td>
<td><strong>Fatura<br> Toplamı :</strong></td>
<td><strong class="h3" id="invoice-total"><%= purchase_total(@purchase) %></strong></td>
</tr>
</tbody>
</table>
</div>
</div>
class Purchase < ApplicationRecord
belongs_to :project
has_many :invoice_lines
end
module PurchasesHelper
def purchase_total(purchase)
total = 0
purchase.invoice_lines.each do |line|
total += (line.tax.value + 1) * line.qty * line.unitPrice
end
"%.2f" % total
end
end
class InvoiceLine < ApplicationRecord
belongs_to :sale
belongs_to :purchase
belongs_to :unit
belongs_to :tax
end
...
def new
@invoice_line = InvoiceLine.new
if !params[:sale_id].nil?
@invoice_line.sale = Sale.find(params[:sale_id])
elsif !params[:purchase_id].nil?
@invoice_line.purchase = Purchase.find(params[:purchase_id])
end
end
...
...
<div class="form-group col-sm-8">
<%= form.label "Firması :" %>
<%= form.collection_select("company_id", Company.all, :id, :name,
{:include_blank => true,
selected: @invoice_line.sale.nil? ?
(@invoice_line.purchase.nil? ? nil :
@invoice_line.purchase.project.company_id) :
@invoice_line.sale.project.company_id},
{ class: "form-control", id: "company-select" }) %>
</div>
...
...
<div class="form-group col-sm-8">
<%= form.label "Projesi :" %>
<%= form.collection_select("project_id", @invoice_line.sale.nil? ?
(@invoice_line.purchase.nil? ? Project.all : Project.where(company_id: @invoice_line.purchase.project.company_id)) :
Project.where(company_id: @invoice_line.sale.project.company_id), :id, :name,
{ include_blank: true,
selected: @invoice_line.sale.nil? ?
(@invoice_line.purchase.nil? ? nil :
@invoice_line.purchase.project_id) :
@invoice_line.sale.project_id },
{ class: "form-control", id: "project-select" }) %>
</div>
...
...
<div class="form-group col-sm-6">
<%= form.label "Satış Faturası :" %>
<%= form.collection_select(:sale_id, @invoice_line.sale.nil? ?
(@invoice_line.purchase.nil? ? Sale.all : Sale.where(project_id: @invoice_line.purchase.project_id)) :
Sale.where(project_id: @invoice_line.sale.project_id), :id, :name,
{ include_blank: true,
selected: @invoice_line.sale.nil? ? nil : @invoice_line.sale.id },
{ class: "form-control", id: "sale-select" }) %>
</div>
...
...
<div class="form-group col-sm-6">
<%= form.label "Alım Faturası :" %>
<%= form.collection_select(:purchase_id, @invoice_line.purchase.nil? ?
(@invoice_line.sale.nil? ? Purchase.all : Purchase.where(project_id: @invoice_line.sale.project_id)) :
Purchase.where(project_id: @invoice_line.purchase.project_id), :id, :name,
{ include_blank: true,
selected: @invoice_line.purchase.nil? ? nil : @invoice_line.purchase.id },
{ class: "form-control", id: "purchase-select" }) %>
</div>
...
...
$("#project-select").change(function(){
if(this.value){
$.ajax({url: "/projects/getSales?id=" + this.value, success: function(result){
var sale_select = $("#sale-select");
sale_select.empty();
$.each(result, function(index, value){
var opt = $("<option/>");
opt.attr("value", value[0]);
opt.text(value[1]);
opt.appendTo(sale_select);
});
$("<option/>").attr("selected", "selected").attr("value", 0).text("").prependTo(sale_select);
}});
$.ajax({url: "/projects/getPurchases?id=" + this.value, success: function(result){
var purchase_select = $("#purchase-select");
purchase_select.empty();
$.each(result, function(index, value){
var opt = $("<option/>");
opt.attr("value", value[0]);
opt.text(value[1]);
opt.appendTo(purchase_select);
});
$("<option/>").attr("selected", "selected").attr("value", 0).text("").prependTo(purchase_select);
}});
};
});
...
...
def getPurchases
render json: Project.find(params["id"]).purchases.map{|p| [p.id, p.name]}
end
private
...
...
resources :projects do
collection do
post "addPerson"
get "destroyPerson"
get "getSales"
get "getPurchases"
end
end
...
...
# Only allow a list of trusted parameters through.
def invoice_line_params
params.require(:invoice_line).permit(:sale_id, :kod, :product, :unitPrice, :qty, :unit_id, :tax_id)
end
end
...
def invoice_line_params
params.require(:invoice_line).permit(:sale_id, :purchase_id, :kod, :product, :unitPrice, :qty, :unit_id, :tax_id)
end
end
class InvoiceLine < ApplicationRecord
belongs_to :sale, optional: true
belongs_to :purchase, optional: true
belongs_to :unit
belongs_to :tax
end
...
<strong class="col-sm-3">Satış Faturası :</strong>
<%= @invoice_line.sale.nil? ? "..." : (link_to @invoice_line.sale.name, @invoice_line.sale) %>
</p>
<p>
<strong class="col-sm-3">Alım Faturası :</strong>
<%= @invoice_line.purchase.nil? ? "..." : (link_to @invoice_line.purchase.name, @invoice_line.purchase) %>
</p>
...
...
<% end %>
<div class="col-sm-2">
<input type="text" name="" id="line-total" class="form-control">
</div>
<div class="col-sm-2">
<button id="line-button" class="btn btn-primary">Hesapla</button>
</div>
<p id="taxes" style="display: none;"><%= Tax.all.map{ |t| t.value }.join(",") %></p>
...
...
$("#line-button").click(function(){
var line_total = $("#line-total").val();
var i_l_qty = $("#invoice_line_qty").val();
var i_l_tax = parseFloat($("#taxes").text().split(",")[$("#invoice_line_tax").val()-1]);
$("#invoice_line_unitPrice").val((line_total / (1 + i_l_tax) / i_l_qty).toFixed(2));
});
...
Proje Ayrıntısına Alım Faturaları İlavesi
...
<div class="row">
<div class="col-sm-4"></div>
<div class="col-sm-8">
<strong class="h3">Alım Faturaları</strong>
<%= link_to '+', new_purchase_path + "?project_id=" + @project.id.to_s, class: "btn btn-success", style: "margin: 20px;" %>
<table class="table table-striped">
<thead>
<tr>
<th>İsim</th>
<th>Alım Tarihi</th>
<th>Ödeme Tarihi</th>
<th>Toplam</th>
</tr>
</thead>
<tbody>
<% @project.purchases.each do |purchase| %>
<tr>
<td><%= link_to purchase.name, purchase %></td>
<td><%= purchase.salesDate %></td>
<td><%= purchase.paymentDate %></td>
<td><%= purchase_total(purchase) %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
Ödemeler Tablosuna Alım Faturaları Eklenmesi
rails g migration AddPurchaseToPayments purchase:belongs_to
rails db:migrate
...
<table class="table table-striped">
<thead>
<tr>
<th>Satış Faturası</th>
<th>Alım Faturası</th>
<th>Not</th>
<th>Tarih</th>
<th>Değer</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @payments.each do |payment| %>
<tr>
<td><%= payment.sale_id.nil? ? "..." :
(link_to payment.sale.name, payment.sale) %></td>
<td><%= payment.purchase_id.nil? ? "..." :
(link_to payment.purchase.name, payment.purchase) %></td>
<td><%= link_to payment.note, payment %></td>
...
...
<div class="row">
<div class="col-sm-12">
<strong class="h3">Ödemeler</strong>
<%= link_to '+', new_payment_path + "?purchase_id=" + @purchase.id.to_s, class: "btn btn-success", style: "margin: 20px;" %>
</div>
<div class="col-sm"></div>
<hr/>
<div class="col-sm-12">
<table class="table table-striped">
<thead>
<tr>
<th>Not</th>
<th>Tarih</th>
<th>Değer</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
<% @purchase.payments.each do |payment| %>
<tr>
<td><%= link_to payment.note, payment %></td>
<td><%= payment.date %></td>
<td><%= payment.value %></td>
<td><%= link_to 'Düzenle', edit_payment_path(payment), class: "btn btn-primary" %></td>
<td><%= link_to 'Sil', payment, method: :delete, data: { confirm: payment.note + ' satırı silinecek emin misin?' }, class: "btn btn-danger" %></td>
</tr>
<% end %>
<tr>
<td colspan="1"></td>
<td><strong>Ödemeler<br> Toplamı :</strong></td>
<td><strong class="h3" id="payment-total">
<%= purchase_payment_total(@purchase) %></strong></td>
</tr>
</tbody>
</table>
</div>
</div>
...
def purchase_payment_total(purchase)
total = 0
purchase.payments.each do |payment|
total += payment.value
end
"%.2f" % total
end
end
class Purchase < ApplicationRecord
belongs_to :project
has_many :invoice_lines
has_many :payments
end
class Payment < ApplicationRecord
belongs_to :sale, optional: true
belongs_to :purchase, optional: true
end
...
# GET /payments/new
def new
@payment = Payment.new
if !params[:sale_id].nil?
@payment.sale = Sale.find(params[:sale_id])
elsif !params[:purchase_id].nil?
@payment.purchase = Purchase.find(params[:purchase_id])
end
end
...
...
# Only allow a list of trusted parameters through.
def payment_params
params.require(:payment).permit(:sale_id, :purchase_id, :note, :date, :value)
end
end
...
<div class="form-group col-sm-8">
<%= form.label "Firması :" %>
<%= form.collection_select("company_id", Company.all, :id, :name,
{:include_blank => true,
selected: @payment.sale.nil? ?
(@payment.purchase.nil? ? nil :
@payment.purchase.project.company_id) :
@payment.sale.project.company_id},
{ class: "form-control", id: "company-select" }) %>
</div>
<div class="form-group col-sm-8">
<%= form.label "Projesi :" %>
<%= form.collection_select("project_id", @payment.sale.nil? ?
(@payment.purchase.nil? ? Project.all : Project.where(company_id: @payment.purchase.project.company_id)) :
Project.where(company_id: @payment.sale.project.company_id), :id, :name,
{ include_blank: true,
selected: @payment.sale.nil? ?
(@payment.purchase.nil? ? nil :
@payment.purchase.project_id) :
@payment.sale.project_id },
{ class: "form-control", id: "project-select" }) %>
</div>
<div class="form-group col-sm-6">
<%= form.label "Satış Faturası :" %>
<%= form.collection_select(:sale_id, @payment.sale.nil? ?
(@payment.purchase.nil? ? Sale.all : Sale.where(project_id: @payment.purchase.project_id)) :
Sale.where(project_id: @payment.sale.project_id), :id, :name,
{ include_blank: true,
selected: @payment.sale.nil? ? nil : @payment.sale.id },
{ class: "form-control", id: "sale-select" }) %>
</div>
<div class="form-group col-sm-6">
<%= form.label "Alım Faturası :" %>
<%= form.collection_select(:purchase_id, @payment.purchase.nil? ?
(@payment.sale.nil? ? Purchase.all : Purchase.where(project_id: @payment.sale.project_id)) :
Purchase.where(project_id: @payment.purchase.project_id), :id, :name,
{ include_blank: true,
selected: @payment.purchase.nil? ? nil : @payment.purchase.id },
{ class: "form-control", id: "purchase-select" }) %>
</div>
...
<%= javascript_tag do %>
...
$("#project-select").change(function(){
if(this.value){
...
$.ajax({url: "/projects/getPurchases?id=" + this.value, success: function(result){
var purchase_select = $("#purchase-select");
purchase_select.empty();
$.each(result, function(index, value){
var opt = $("<option/>");
opt.attr("value", value[0]);
opt.text(value[1]);
opt.appendTo(purchase_select);
});
$("<option/>").attr("selected", "selected").attr("value", 0).text("").prependTo(purchase_select);
}});
};
});
<% end %>
...
<div class="row">
<div class="col-sm-12">
<p>
<strong class="col-sm-3">Satış Faturası :</strong>
<%= @payment.sale.nil? ? "..." : (link_to @payment.sale.name, @payment.sale) %>
</p>
<p>
<strong class="col-sm-3">Alım Faturası :</strong>
<%= @payment.purchase.nil? ? "..." : (link_to @payment.purchase.name, @payment.purchase) %>
</p>
...
Alım Fatura ile İlgili Toplam Değerler
...
<div class="col-sm-2">
<h3>Net Borç</h3>
<% net_total = purchase_total(@purchase).to_f - purchase_payment_total(@purchase).to_f %>
<h3 id="net-total" class="text-center" style="background-color:<%= net_total > 1 ? 'pink' : 'lime' %>">
<%= "%.2f" % net_total %>
</h3>
</div>
</div>
...
...
<th>Ödeme Tarihi</th>
<th>Toplam</th>
<th>Borç</th>
<th colspan="2"></th>
</tr>
</thead>
<tbody>
...
<td><%= purchase_total(purchase) %></td>
<% net_total = purchase_total(purchase).to_f - purchase_payment_total(purchase).to_f %>
<td class="h4" style="color:<% if (net_total > 1) %>red<% else %>blue<% end %>"><%= "%.2f" % net_total %></td>
...
...
# GET /purchases/new
def new
@purchase = Purchase.new
if !params[:project_id].nil?
@purchase.project = Project.find(params[:project_id])
end
end
...
...
<th>İsim</th>
<th>Alım Tarihi</th>
<th>Ödeme Tarihi</th>
<th>Toplam</th>
<th>Borç</th>
</tr>
...
<td><%= purchase_total(purchase) %></td>
<% net = purchase_total(purchase).to_f -
purchase_payment_total(purchase).to_f %>
<td style="color: <%= net > 1 ? 'red' : 'blue' %>;"><%= "%.2f" % net %></td>
</tr>
<% end %>
</tbody>
</table>
...
...
<strong class="col-sm-2">Not :</strong>
<%= @project.note %>
</p>
</div>
<div class="col-sm-2">
<h3>Net Kar</h3>
<% net_total = project_invoice_total(@project).to_f - project_purchase_total(@project).to_f %>
<h3 id="net-total" class="text-center" style="background-color:<%= net_total > 1 ? 'lime' : 'pink' %>">
<%= "%.2f" % net_total %>
</h3>
<h3>Anlık Net</h3>
<% net_total = project_payment_total(@project).to_f - project_purchase_payment_total(@project).to_f %>
<h3 id="net-total" class="text-center" style="background-color:<%= net_total > 1 ? 'lime' : 'pink' %>">
<%= "%.2f" % net_total %>
</h3>
</div>
</div>
...
module ProjectsHelper
def project_invoice_total(project)
total = 0
project.sales.each do |sale|
total += invoice_total(sale).to_f
end
"%.2f" % total
end
def project_purchase_total(project)
total = 0
project.purchases.each do |purchase|
total += purchase_total(purchase).to_f
end
"%.2f" % total
end
def project_payment_total(project)
total = 0
project.sales.each do |sale|
total += payment_total(sale).to_f
end
"%.2f" % total
end
def project_purchase_payment_total(project)
total = 0
project.purchases.each do |purchase|
total += purchase_payment_total(purchase).to_f
end
"%.2f" % total
end
end
Hiç yorum yok:
Yorum Gönder