Class: MOCO::Invoice

Inherits:
BaseEntity show all
Defined in:
lib/moco/entities/invoice.rb

Overview

Represents a MOCO invoice

Required attributes for create:

customer_id - Integer, customer company ID recipient_address - String, full address (use \n for line breaks) date - String, "YYYY-MM-DD" invoice date due_date - String, "YYYY-MM-DD" payment due date title - String, invoice title (e.g., "Invoice") tax - Float, tax rate percentage (e.g., 19.0) currency - String, 3-letter code (e.g., "EUR") items - Array of Hashes, invoice line items (see below)

Item types (for items array):

{ type: "title", title: "Section Title" } { type: "description", description: "Some description text" } { type: "item", title: "Service", quantity: 10, unit: "h", unit_price: 150.0 } { type: "item", title: "Fixed Fee", net_total: 500.0 } # lump sum { type: "subtotal" } { type: "separator" } { type: "page-break" }

Optional attributes:

project_id - Integer, associated project ID status - String, "created" or "draft" (default: "created") service_period_from - String, "YYYY-MM-DD" service_period_to - String, "YYYY-MM-DD" change_address - String, "invoice", "project", or "customer" salutation - String, greeting text (HTML allowed) footer - String, footer text (HTML allowed) discount - Float, discount percentage cash_discount - Float, early payment discount percentage cash_discount_days - Integer, days for early payment discount tags - Array of Strings custom_properties - Hash

Read-only attributes:

id, identifier, status, net_total, gross_total, payments, reminders, created_at, updated_at

Example:

moco.invoices.create( customer_id: 123456, recipient_address: "Acme Corp\n123 Main St\n12345 City", date: "2024-01-15", due_date: "2024-02-15", title: "Invoice", tax: 19.0, currency: "EUR", items: [ { type: "title", title: "Services January 2024" }, { type: "item", title: "Development", quantity: 40, unit: "h", unit_price: 150.0 }, { type: "item", title: "Project Management", quantity: 8, unit: "h", unit_price: 120.0 } ] )

Instance Attribute Summary

Attributes inherited from BaseEntity

#attributes, #client

Instance Method Summary collapse

Methods inherited from BaseEntity

#==, #association, #destroy, #eql?, #has_many, #hash, #id, #initialize, #inspect, #reload, #save, #to_h, #to_json, #update

Constructor Details

This class inherits a constructor from MOCO::BaseEntity

Instance Method Details

#add_attachment(file_data) ⇒ Object

Add an attachment to the invoice



99
100
101
102
# File 'lib/moco/entities/invoice.rb', line 99

def add_attachment(file_data)
  client.post("invoices/#{id}/attachments", file_data)
  self
end

#attachmentsObject

Get attachments for this invoice



94
95
96
# File 'lib/moco/entities/invoice.rb', line 94

def attachments
  client.get("invoices/#{id}/attachments")
end

#companyObject

Associations



121
122
123
# File 'lib/moco/entities/invoice.rb', line 121

def company
  association(:customer, "Company")
end

#delete_attachment(attachment_id) ⇒ Object

Delete an attachment from the invoice



105
106
107
108
# File 'lib/moco/entities/invoice.rb', line 105

def delete_attachment(attachment_id)
  client.delete("invoices/#{id}/attachments/#{attachment_id}")
  self
end

#expensesObject



78
79
80
# File 'lib/moco/entities/invoice.rb', line 78

def expenses
  client.get("invoices/#{id}/expenses")
end

#paymentsObject

Fetches payments for this invoice



111
112
113
# File 'lib/moco/entities/invoice.rb', line 111

def payments
  MOCO::NestedCollectionProxy.new(client, self, :payments, "InvoicePayment")
end

#pdfObject



66
67
68
# File 'lib/moco/entities/invoice.rb', line 66

def pdf
  client.get("invoices/#{id}.pdf")
end

#projectObject



125
126
127
# File 'lib/moco/entities/invoice.rb', line 125

def project
  association(:project)
end

#remindersObject

Fetches reminders for this invoice



116
117
118
# File 'lib/moco/entities/invoice.rb', line 116

def reminders
  MOCO::NestedCollectionProxy.new(client, self, :reminders, "InvoiceReminder")
end

#send_email(recipient:, subject:, text:, **options) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/moco/entities/invoice.rb', line 82

def send_email(recipient:, subject:, text:, **options)
  payload = {
    recipient:,
    subject:,
    text:
  }.merge(options)

  client.post("invoices/#{id}/send_email", payload)
  self
end

#timesheetObject



70
71
72
# File 'lib/moco/entities/invoice.rb', line 70

def timesheet
  client.get("invoices/#{id}/timesheet")
end

#timesheet_pdfObject



74
75
76
# File 'lib/moco/entities/invoice.rb', line 74

def timesheet_pdf
  client.get("invoices/#{id}/timesheet.pdf")
end

#to_sObject



129
130
131
# File 'lib/moco/entities/invoice.rb', line 129

def to_s
  "#{identifier} - #{title} (#{date})"
end

#update_status(status) ⇒ Object

Instance methods for invoice-specific operations



61
62
63
64
# File 'lib/moco/entities/invoice.rb', line 61

def update_status(status)
  client.put("invoices/#{id}/update_status", { status: })
  self
end