Skip to content

Commit

Permalink
IN-392 Changes line_itens entity and add pricebook finders
Browse files Browse the repository at this point in the history
  • Loading branch information
reinaldob committed Oct 22, 2014
1 parent 47a0940 commit 9b63522
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
11 changes: 6 additions & 5 deletions lib/SF_services/line_item.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
module SFService
class LineItem < Base

def initialize(config)
super("LineItem__c", config)
super("OpportunityLineItem", config)
end

def find_order(order_code)
results = salesforce.query("select Id from Order__c where Name = '#{order_code}'")
results = salesforce.query("select Id from Opportunity where Name = '#{order_code}'")
results.any? ? results.first['Id'] : nil
end

def find_product(product_code)
results = salesforce.query("select Id from Product2 where ProductCode = '#{product_code}'")
results = salesforce.query("select Id from PricebookEntry where ProductCode = '#{product_code}'")
results.any? ? results.first['Id'] : nil
end

def find_line_item(order_id, product_id)
results = salesforce.query("select Id from LineItem__c where Order__c = '#{order_id}' and Product__c = '#{product_id}'")
results = salesforce.query("select Id from OpportunityLineItem where OpportunityId = '#{order_id}' and PricebookEntryId = '#{product_id}'")
results.any? ? results.first['Id'] : nil
end

Expand All @@ -24,7 +25,7 @@ def upsert!(line_item_attr = {}, order_code, product_code)
product_id = find_product(product_code)
line_item_id = find_line_item(order_id, product_id)

line_item_attr = [line_item_attr, { 'Order__c' => order_id }, { 'Product__c' => product_id }].reduce &:merge
line_item_attr = [line_item_attr, { 'OpportunityId' => order_id }, { 'PricebookEntryId' => product_id }].reduce &:merge
line_item_id.present? ? update!(line_item_attr.merge({ Id: line_item_id })) : create!(line_item_attr)
end
end
Expand Down
7 changes: 3 additions & 4 deletions lib/integrations/builders/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ def initialize(object)

def build
params = {
'Quantity__c' => object['quantity'],
'Price__c' => object['price'],
'Currency__c' => object['currency'],
'Quantity' => object['quantity'],
'UnitPrice' => object['price'],
'CurrencyIsoCode' => object['currency'],
}
end

end
end
end

0 comments on commit 9b63522

Please sign in to comment.