8000 Unlock time entries if invoice is deleted by apoorv1316 · Pull Request #1874 · saeloun/miru-web · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Unlock time entries if invoice is deleted #1874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Invoice < ApplicationRecord
before_validation :set_external_view_key, on: :create
after_commit :refresh_invoice_index
after_save :lock_timesheet_entries, if: :draft?
after_discard :unlock_timesheet_entries, if: :draft?

validates :issue_date, :due_date, :invoice_number, presence: true
validates :due_date, comparison: { greater_than_or_equal_to: :issue_date }, if: :not_waived
Expand Down Expand Up @@ -202,4 +203,9 @@ def lock_timesheet_entries
timesheet_entry_ids = invoice_line_items.pluck(:timesheet_entry_id)
TimesheetEntry.where(id: timesheet_entry_ids).update!(locked: true)
end

def unlock_timesheet_entries
timesheet_entry_ids = invoice_line_items.pluck(:timesheet_entry_id)
TimesheetEntry.where(id: timesheet_entry_ids).update!(locked: false)
end
end
10 changes: 10 additions & 0 deletions app/models/invoice_line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class InvoiceLineItem < ApplicationRecord
belongs_to :invoice
belongs_to :timesheet_entry, optional: true

before_destroy :unlock_timesheet_entry

validates :name, :date, :rate, :quantity, presence: true
validates :rate, numericality: { greater_than_or_equal_to: 0 }
validates :quantity, numericality: { greater_than_or_equal_to: 0 }
Expand Down Expand Up @@ -67,4 +69,12 @@ def total_cost
def formatted_date
CompanyDateFormattingService.new(date, company: invoice.company).process
end

private

def unlock_timesheet_entry
if invoice.draft?
timesheet_entry.update!(locked: false)
end
end
end
4 changes: 4 additions & 0 deletions spec/models/invoice_line_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
end
end

describe "callbacks" do
it { is_expected.to callback(:unlock_timesheet_entry).before(:destroy) }
end

describe "Associations" do
describe "belongs to" do
it { is_expected.to belong_to(:invoice) }
Expand Down
7 changes: 7 additions & 0 deletions spec/models/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@
end
end

describe "callbacks" do
it { is_expected.to callback(:set_external_view_key).before(:validation).on(:create) }
it { is_expected.to callback(:refresh_invoice_index).after(:commit) }
it { is_expected.to callback(:lock_timesheet_entries).after(:save).if(:draft?) }
it { is_expected.to callback(:unlock_timesheet_entries).after(:discard).if(:draft?) }
end

describe ".client_name" do
it { is_expected.to delegate_method(:name).to(:client).with_prefix(:client) }
end
Expand Down
Loading
0