module LedgerSync module Adaptors module QuickBooksOnline module Expense module Operations class Create < Operation::Create class Contract < LedgerSync::Adaptors::Contract params do required(:ledger_id).value(:nil) required(:vendor).hash(Types::Reference) required(:account).hash(Types::Reference) required(:amount).filled(:integer) required(:currency).filled(:string) required(:memo).filled(:string) required(:payment_type).filled(:string) required(:transaction_date).filled(:date?) required(:exchange_rate).maybe(:float) required(:line_items).array(Types::Reference) end end private def operate response = adaptor.post( resource: 'purchase', payload: local_resource_data ) resource.ledger_id = response.dig('Id') success(response: response) end def local_resource_data { 'CurrencyRef' => { 'value' => resource.currency, }, 'PaymentType' => Mapping::PAYMENT_TYPES[resource.payment_type], 'TxnDate' => resource.transaction_date.to_s, # Format: YYYY-MM-DD 'PrivateNote' => resource.memo, 'ExchangeRate' => resource.exchange_rate, 'EntityRef' => { 'value' => resource.vendor.ledger_id, }, 'AccountRef' => { 'value' => resource.account.ledger_id }, 'Line' => resource.line_items.map do |line_item| { 'DetailType' => 'AccountBasedExpenseLineDetail', 'AccountBasedExpenseLineDetail' => { 'AccountRef' => { 'value' => line_item.account&.ledger_id } }, 'Amount' => line_item.amount / 100.0, 'Description' => line_item.description } end } end end end end end end end