Skip to content

Commit

Permalink
Simplifies migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Brice TEXIER committed May 17, 2017
1 parent 535f8a6 commit fd564e5
Show file tree
Hide file tree
Showing 41 changed files with 1,601 additions and 668 deletions.
4 changes: 2 additions & 2 deletions app/models/payslip_nature.rb
Expand Up @@ -49,7 +49,7 @@ class PayslipNature < Ekylibre::Record::Base
validates :name, presence: true, uniqueness: true, length: { maximum: 500 }
# ]VALIDATORS]

selects_among_all
selects_among_all

after_initialize if: :new_record? do
self.active = true
Expand All @@ -61,6 +61,6 @@ class PayslipNature < Ekylibre::Record::Base
end

validate do
errors.add(:currency, :invalid) if self.journal && self.currency.present? && self.journal.currency != self.currency
errors.add(:currency, :invalid) if journal && currency.present? && journal.currency != currency
end
end
2 changes: 1 addition & 1 deletion app/views/backend/payslips/_form.html.haml
@@ -1,5 +1,5 @@
= field_set do
= f.association :nature, as: :hidden
= f.referenced_association :nature
= f.referenced_association :employee, source: :employees, new: { employee: 1 }, label: Payslip.human_attribute_name(:employee)
= f.input :started_on
= f.input :stopped_on
Expand Down
1 change: 1 addition & 0 deletions config/locales/arb/action.yml
Expand Up @@ -515,6 +515,7 @@ arb:
new: "دفعة دفع جديدة"
# show: "Payslip payment: %{number}"
backend/payslips:
# edit: "Edit payslip: %{name}"
index: "كشوف"
new: "جديد قسيمة الدفع"
# show: "Payslip: %{number}"
Expand Down
1 change: 1 addition & 0 deletions config/locales/cmn/action.yml
Expand Up @@ -422,6 +422,7 @@ cmn:
new: "新的支付费用"
# show: "Payslip payment: %{number}"
backend/payslips:
# edit: "Edit payslip: %{name}"
index: "工资单"
new: "新工资单"
# show: "Payslip: %{number}"
Expand Down
1 change: 1 addition & 0 deletions config/locales/deu/action.yml
Expand Up @@ -422,6 +422,7 @@ deu:
new: "Neue payslip Zahlung"
# show: "Payslip payment: %{number}"
backend/payslips:
# edit: "Edit payslip: %{name}"
# index: "Payslips"
new: "Neue Payslip"
# show: "Payslip: %{number}"
Expand Down
2 changes: 1 addition & 1 deletion config/locales/eng/action.yml
Expand Up @@ -540,7 +540,7 @@ eng:
show: "Payslip payment: %{number}"
backend/payslips:
# correct: "Correct payslip: %{name}"
# edit: "Edit payslip: %{name}"
edit: "Edit payslip: %{name}"
index: "Payslips"
# invoice: "Invoice payslip: %{name}"
new: "New payslip"
Expand Down
1 change: 1 addition & 0 deletions config/locales/fra/action.yml
Expand Up @@ -499,6 +499,7 @@ fra:
new: "Nouveau paiement de salaire"
show: "Paiement de salaire\_: %{number}"
backend/payslips:
edit: "Modifier le salaire\_: %{number}"
index: "Salaires"
new: "Nouveau salaire"
show: "Salaire\_: %{number}"
Expand Down
1 change: 1 addition & 0 deletions config/locales/ita/action.yml
Expand Up @@ -422,6 +422,7 @@ ita:
new: "Nuovo pagamento payslip"
# show: "Payslip payment: %{number}"
backend/payslips:
# edit: "Edit payslip: %{name}"
index: "Buste paga"
new: "Nuovo payslip"
# show: "Payslip: %{number}"
Expand Down
1 change: 1 addition & 0 deletions config/locales/jpn/action.yml
Expand Up @@ -422,6 +422,7 @@ jpn:
new: "新しいペイチップの支払い"
# show: "Payslip payment: %{number}"
backend/payslips:
# edit: "Edit payslip: %{name}"
index: "ペイスリップス"
new: "新しいペイプルップ"
# show: "Payslip: %{number}"
Expand Down
1 change: 1 addition & 0 deletions config/locales/por/action.yml
Expand Up @@ -427,6 +427,7 @@ por:
new: "Novo pagamento de salário"
# show: "Payslip payment: %{number}"
backend/payslips:
# edit: "Edit payslip: %{name}"
index: "Listas de pagamento"
new: "Novo salário"
# show: "Payslip: %{number}"
Expand Down
1 change: 1 addition & 0 deletions config/locales/spa/action.yml
Expand Up @@ -459,6 +459,7 @@ spa:
new: "Nuevo pago de la nómina"
# show: "Payslip payment: %{number}"
backend/payslips:
# edit: "Edit payslip: %{name}"
index: "Papeletas"
new: "Nuevo salario"
# show: "Payslip: %{number}"
Expand Down
Expand Up @@ -17,25 +17,26 @@ def change
END IF;
END IF;
UPDATE journal_entry_items AS jei
SET state = entries.state,
printed_on = entries.printed_on,
journal_id = entries.journal_id,
financial_year_id = entries.financial_year_id,
entry_number = entries.number,
real_currency = entries.real_currency,
real_currency_rate = entries.real_currency_rate
FROM journal_entries AS entries
WHERE jei.entry_id = synced_entry_id
AND entries.id = synced_entry_id
AND synced_entry_id IS NOT NULL
AND (jei.state <> entries.state
OR jei.printed_on <> entries.printed_on
OR jei.journal_id <> entries.journal_id
OR jei.financial_year_id <> entries.financial_year_id
OR jei.entry_number <> entries.number
OR jei.real_currency <> entries.real_currency
OR jei.real_currency_rate <> entries.real_currency_rate);
IF synced_entry_id IS NOT NULL THEN
UPDATE journal_entry_items AS jei
SET state = je.state,
printed_on = je.printed_on,
journal_id = je.journal_id,
financial_year_id = je.financial_year_id,
entry_number = je.number,
real_currency = je.real_currency,
real_currency_rate = je.real_currency_rate
FROM journal_entries AS je
WHERE jei.entry_id = je.id
AND je.id = synced_entry_id
AND (jei.state <> je.state
OR jei.printed_on <> je.printed_on
OR jei.journal_id <> je.journal_id
OR jei.financial_year_id <> je.financial_year_id
OR jei.entry_number <> je.number
OR jei.real_currency <> je.real_currency
OR jei.real_currency_rate <> je.real_currency_rate);
END IF;
RETURN NEW;
END;
$$ language plpgsql;
Expand Down
64 changes: 40 additions & 24 deletions db/migrate/20170421131536_add_payslips.rb
Expand Up @@ -93,36 +93,42 @@ def change

reversible do |r|
r.up do
add_column :payslip_natures, :purchase_nature_id, :integer
# INFO: Conserve nature_id to prevent nature matching
execute "INSERT INTO payslip_natures(purchase_nature_id, name, currency, active, with_accounting, account_id, journal_id, creator_id, created_at, updater_id, updated_at, lock_version) SELECT id, name || COALESCE(' - ' || account_name, ''), currency, active, with_accounting, account_id, journal_id, creator_id, created_at, updater_id, updated_at, lock_version FROM (SELECT DISTINCT n.*, a.id AS account_id, a.name AS account_name FROM purchase_natures AS n LEFT JOIN purchases AS p ON (n.id = p.nature_id) LEFT JOIN purchase_items AS pi ON (pi.purchase_id = p.id) LEFT JOIN accounts AS a ON (a.id = pi.account_id) WHERE n.nature = 'payslip') AS x"
execute 'UPDATE payslip_natures SET by_default = TRUE WHERE id IN (SELECT id FROM payslip_natures ORDER BY id LIMIT 1)'
count = select_value("SELECT count(*) FROM purchase_natures WHERE nature = 'payslip'").to_i
if count > 0
account_id = find_or_create_staff_account_id

add_column :payslip_natures, :purchase_nature_id, :integer
# INFO: Conserve nature_id to prevent nature matching
execute "INSERT INTO payslip_natures(purchase_nature_id, name, currency, active, with_accounting, account_id, journal_id, creator_id, created_at, updater_id, updated_at, lock_version) SELECT id, name, currency, active, with_accounting, #{account_id}, journal_id, creator_id, created_at, updater_id, updated_at, lock_version FROM purchase_natures WHERE nature = 'payslip'"
execute 'UPDATE payslip_natures SET by_default = TRUE WHERE id IN (SELECT id FROM payslip_natures ORDER BY id LIMIT 1)'

add_column :payslips, :purchase_id, :integer
execute "INSERT INTO payslips(purchase_id, number, nature_id, employee_id, state, emitted_on, started_on, stopped_on, amount, currency, account_id, accounted_at, journal_entry_id, affair_id, creator_id, created_at, updater_id, updated_at, lock_version) SELECT p.id, p.number, n.id, supplier_id, CASE WHEN state = 'invoice' THEN 'invoice' ELSE 'draft' END, invoiced_at::DATE, invoiced_at::DATE, invoiced_at::DATE, amount, p.currency, account_id, accounted_at, journal_entry_id, affair_id, p.creator_id, p.created_at, p.updater_id, p.updated_at, p.lock_version FROM purchases AS p JOIN (SELECT DISTINCT p.id AS purchase_id, n.id AS purchase_nature_id, pi.account_id, pn.id AS id FROM purchase_natures AS n LEFT JOIN purchases AS p ON (n.id = p.nature_id) LEFT JOIN purchase_items AS pi ON (pi.purchase_id = p.id) JOIN payslip_natures AS pn ON (COALESCE(pi.account_id, 0) = COALESCE(pn.account_id, 0) AND pn.purchase_nature_id = n.id) WHERE n.nature = 'payslip') AS n ON (p.id = n.purchase_id)"
add_column :payslips, :purchase_id, :integer
execute "INSERT INTO payslips(purchase_id, number, nature_id, employee_id, state, emitted_on, started_on, stopped_on, amount, currency, accounted_at, account_id, journal_entry_id, affair_id, creator_id, created_at, updater_id, updated_at, lock_version) SELECT p.id, p.number, n.id, supplier_id, CASE WHEN state = 'invoice' THEN 'invoice' ELSE 'draft' END, invoiced_at::DATE, invoiced_at::DATE, invoiced_at::DATE, amount, p.currency, accounted_at, #{account_id}, journal_entry_id, affair_id, p.creator_id, p.created_at, p.updater_id, p.updated_at, p.lock_version FROM purchases AS p JOIN payslip_natures AS n ON (n.purchase_nature_id = p.nature_id)"

update_polymorphic_keys("SELECT purchase_id AS old_id, 'Purchase' AS old_type, id AS new_id, 'Payslip' AS new_type FROM payslips")
update_polymorphic_keys("SELECT purchase_id AS old_id, 'Purchase' AS old_type, id AS new_id, 'Payslip' AS new_type FROM payslips")

execute "UPDATE affairs SET type = 'PayslipAffair' WHERE id IN (SELECT affair_id FROM payslips)"
execute "UPDATE outgoing_payments SET type = 'PayslipPayment' WHERE affair_id IN (SELECT id FROM affairs WHERE type = 'PayslipAffair')"
execute "UPDATE affairs SET type = 'PayslipAffair' WHERE id IN (SELECT affair_id FROM payslips)"
execute "UPDATE outgoing_payments SET type = 'PayslipPayment' WHERE affair_id IN (SELECT id FROM affairs WHERE type = 'PayslipAffair')"

execute 'DELETE FROM purchase_items WHERE purchase_id IN (SELECT purchase_id FROM payslips)'
execute 'DELETE FROM purchases WHERE id IN (SELECT purchase_id FROM payslips)'
remove_column :payslips, :purchase_id
execute 'DELETE FROM purchase_items WHERE purchase_id IN (SELECT purchase_id FROM payslips)'
execute 'DELETE FROM purchases WHERE id IN (SELECT purchase_id FROM payslips)'
remove_column :payslips, :purchase_id

update_polymorphic_keys("SELECT purchase_nature_id AS old_id, 'PurchaseNature' AS old_type, id AS new_id, 'PayslipNature' AS new_type FROM payslip_natures")
update_polymorphic_keys("SELECT purchase_nature_id AS old_id, 'PurchaseNature' AS old_type, id AS new_id, 'PayslipNature' AS new_type FROM payslip_natures")

execute "DELETE FROM purchase_natures WHERE nature = 'payslip'"
execute "DELETE FROM purchase_natures WHERE nature = 'payslip'"
remove_column :payslip_natures, :purchase_nature_id
end
execute "ALTER TABLE purchase_natures ADD CONSTRAINT purchase_natures_nature CHECK (nature = 'purchase')"
remove_column :payslip_natures, :purchase_nature_id
end
r.down do
execute 'ALTER TABLE purchase_natures DROP CONSTRAINT purchase_natures_nature'
add_column :purchase_natures, :payslip_nature_id, :integer
execute "INSERT INTO purchase_natures(payslip_nature_id, name, nature, with_accounting, journal_id, creator_id, created_at, updater_id, updated_at, lock_version) SELECT id, name, 'payslip', with_accounting, journal_id, creator_id, created_at, updater_id, updated_at, lock_version FROM payslip_natures"

count = select_value('SELECT count(*) FROM payslips').to_i
if count > 0
add_column :purchase_natures, :payslip_nature_id, :integer
execute "INSERT INTO purchase_natures(payslip_nature_id, name, nature, with_accounting, journal_id, creator_id, created_at, updater_id, updated_at, lock_version) SELECT id, name, 'payslip', with_accounting, journal_id, creator_id, created_at, updater_id, updated_at, lock_version FROM payslip_natures"

add_column :purchases, :payslip_id, :integer
execute 'INSERT INTO purchases(payslip_id, nature_id, supplier_id, state, number, amount, currency, accounted_at, journal_entry_id, affair_id, creator_id, created_at, updater_id, updated_at, lock_version) SELECT id, n.id, employee_id, state, number, amount, p.currency, accounted_at, journal_entry_id, affair_id, p.creator_id, p.created_at, p.updater_id, p.updated_at, p.lock_version FROM payslips AS p JOIN purchase_natures AS n ON (p.nature_id = n.payslip_nature_id)'
variant_id = find_or_create_worker_variant_id
Expand All @@ -133,10 +139,10 @@ def change
update_polymorphic_keys("SELECT payslip_id AS old_id, 'Payslip' AS old_type, id AS new_id, 'Purchase' AS new_type FROM purchases")

remove_column :purchases, :payslip_id
end

update_polymorphic_keys("SELECT payslip_nature_id AS old_id, 'PayslipNature' AS old_type, id AS new_id, 'PurchaseNature' AS new_type FROM purchase_natures")
remove_column :purchase_natures, :payslip_nature_id
update_polymorphic_keys("SELECT payslip_nature_id AS old_id, 'PayslipNature' AS old_type, id AS new_id, 'PurchaseNature' AS new_type FROM purchase_natures")
remove_column :purchase_natures, :payslip_nature_id
end
end
end
end
Expand All @@ -155,21 +161,31 @@ def find_or_create_worker_nature_id
nature_id = select_value("SELECT id FROM product_natures WHERE variety = 'worker' ORDER BY id LIMIT 1").to_i
if nature_id.zero?
category_id = find_or_create_worker_category_id
nature_id = select_value('INSERT INTO product_natures (category_id, population_counting, name, number, reference_name, variety, creator_id, created_at, updater_id, updated_at, lock_version)')
execute("INSERT INTO product_natures (category_id, population_counting, name, number, reference_name, variety, created_at, updated_at) SELECT #{category_id}, 'unitary', 'Staff', 'STAFF001', 'worker', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP")
nature_id = select_value("SELECT id FROM product_natures WHERE variety = 'worker' ORDER BY id LIMIT 1").to_i
end
nature_id
end

def find_or_create_worker_category_id
category_id = select_value("SELECT id FROM product_nature_categories WHERE reference_name = 'worker' ORDER BY id LIMIT 1").to_i
if category_id.zero?
charge_account_id = select_value("SELECT id FROM accounts WHERE usages = 'staff_expenses' LIMIT 1").to_i
if charge_account_id.zero?
end
charge_account_id = find_or_create_staff_account_id
execute("INSERT INTO product_nature_categories (name, number, reference_name, charge_account_id, created_at, updated_at) SELECT 'Staff', 'ST001', 'staff', #{charge_account_id}, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP")
category_id = select_value("SELECT id FROM product_nature_categories WHERE reference_name = 'worker' ORDER BY id LIMIT 1").to_i
end
category_id
end

def find_or_create_staff_account_id
account_id = select_value("SELECT id FROM accounts WHERE usages = 'staff_expenses' LIMIT 1").to_i
if account_id.zero?
execute("INSERT INTO accounts (name, number, label, usages, created_at, updated_at) SELECT 'Staff', '64', '64 - Staff', 'staff_expenses', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP")
account_id = select_value("SELECT id FROM accounts WHERE usages = 'staff_expenses' LIMIT 1").to_i
end
account_id
end

# Updates types in polymorphic reflections with a conversion query which should
# contains columns: old_id, old_type, new_id, new_type
def update_polymorphic_keys(conversion_query)
Expand Down
43 changes: 22 additions & 21 deletions db/structure.sql
Expand Up @@ -2,8 +2,8 @@
-- PostgreSQL database dump
--

-- Dumped from database version 9.5.6
-- Dumped by pg_dump version 9.5.6
-- Dumped from database version 9.5.7
-- Dumped by pg_dump version 9.5.7

SET statement_timeout = 0;
SET lock_timeout = 0;
Expand Down Expand Up @@ -145,25 +145,26 @@ BEGIN
END IF;
END IF;

UPDATE journal_entry_items AS jei
SET state = entries.state,
printed_on = entries.printed_on,
journal_id = entries.journal_id,
financial_year_id = entries.financial_year_id,
entry_number = entries.number,
real_currency = entries.real_currency,
real_currency_rate = entries.real_currency_rate
FROM journal_entries AS entries
WHERE jei.entry_id = synced_entry_id
AND entries.id = synced_entry_id
AND synced_entry_id IS NOT NULL
AND (jei.state <> entries.state
OR jei.printed_on <> entries.printed_on
OR jei.journal_id <> entries.journal_id
OR jei.financial_year_id <> entries.financial_year_id
OR jei.entry_number <> entries.number
OR jei.real_currency <> entries.real_currency
OR jei.real_currency_rate <> entries.real_currency_rate);
IF synced_entry_id IS NOT NULL THEN
UPDATE journal_entry_items AS jei
SET state = je.state,
printed_on = je.printed_on,
journal_id = je.journal_id,
financial_year_id = je.financial_year_id,
entry_number = je.number,
real_currency = je.real_currency,
real_currency_rate = je.real_currency_rate
FROM journal_entries AS je
WHERE jei.entry_id = je.id
AND je.id = synced_entry_id
AND (jei.state <> je.state
OR jei.printed_on <> je.printed_on
OR jei.journal_id <> je.journal_id
OR jei.financial_year_id <> je.financial_year_id
OR jei.entry_number <> je.number
OR jei.real_currency <> je.real_currency
OR jei.real_currency_rate <> je.real_currency_rate);
END IF;
RETURN NEW;
END;
$$;
Expand Down
50 changes: 46 additions & 4 deletions test/fixtures/accounts.yml
Expand Up @@ -2975,7 +2975,7 @@ accounts_226:
debtor: false
id: 572
label: 411P00000004 - Περικλῆς
last_letter: D
last_letter: F
lock_version: 0
name: "Περικλῆς"
number: 411P00000004
Expand Down Expand Up @@ -4177,16 +4177,58 @@ accounts_333:
reconcilable: false
updated_at: 2014-10-31 17:19:32.000000000 Z
accounts_334:
created_at: 2017-05-15 19:23:16.863815000 Z
created_at: 2017-05-17 20:27:38.938956000 Z
creator_id: 1
debtor: false
id: 680
label: 411EI00000003 - Mme Andrew BAKTOUBI
last_letter: A
lock_version: 0
name: Mme Andrew BAKTOUBI
number: 411EI00000003
reconcilable: true
updated_at: 2017-05-17 20:27:38.938956000 Z
updater_id: 1
usages: clients
accounts_335:
created_at: 2017-05-17 20:58:49.372008000 Z
creator_id: 1
debtor: false
id: 681
label: 421193 - M. Gendo IKARI
last_letter: A
lock_version: 0
name: M. Gendo IKARI
number: '421193'
reconcilable: true
updated_at: 2017-05-17 20:58:49.372008000 Z
updater_id: 1
usages: staff_due_remunerations
accounts_336:
created_at: 2017-05-17 20:59:31.403932000 Z
creator_id: 1
debtor: false
id: 682
label: 421194 - M. Kōzō FUYUTSUKI
last_letter: A
lock_version: 0
name: M. Kōzō FUYUTSUKI
number: '421194'
reconcilable: true
updated_at: 2017-05-17 20:59:31.403932000 Z
updater_id: 1
usages: staff_due_remunerations
accounts_337:
created_at: 2017-05-17 20:59:52.233372000 Z
creator_id: 1
debtor: false
id: 683
label: 421P00000002 - Mme Yui IKARI
last_letter: B
last_letter: A
lock_version: 0
name: Mme Yui IKARI
number: 421P00000002
reconcilable: true
updated_at: 2017-05-15 19:23:16.863815000 Z
updated_at: 2017-05-17 20:59:52.233372000 Z
updater_id: 1
usages: staff_due_remunerations

0 comments on commit fd564e5

Please sign in to comment.