diff --git a/src/classes/FreshbooksAPI.cls b/src/classes/FreshbooksAPI.cls index 684ca7e..c71b31b 100644 --- a/src/classes/FreshbooksAPI.cls +++ b/src/classes/FreshbooksAPI.cls @@ -533,8 +533,76 @@ public with sharing class FreshbooksAPI { System.assert(te.Freshbooks_Time_Entry_ID__c == '123'); } + + // --- payments + public static Freshbooks_Payment__c[] getPayments(){ + return getPayments(null); + } + public static Freshbooks_Payment__c[] getPayments(Map filter){ + List toReturn = new List(); + Integer lastBatchSize = LIST_PAGE_COUNT_MAX; + Integer pageNumber = 1; + while(lastBatchSize == LIST_PAGE_COUNT_MAX){ + Freshbooks_Payment__c[] batch = getPayments(pageNumber,filter); + pageNumber++; + lastBatchSize = batch.size(); + toReturn.addAll(batch); + } + return toReturn; + } + public static final String TEST_GET_PAYMENTS_XML = '1231232007-03-02 12:04:11VISA43CAD2009-08-12 09:00:002156858189Authorize.Net301.21'; + public static Freshbooks_Payment__c[] getPayments(Integer pageNumber, Map filter){ + Dom.Document requestDoc = new Dom.Document(); + Dom.XmlNode root = requestDoc.createRootElement('request', null, null); + root.setAttribute('method', 'payment.list'); + root.addChildElement('page', null, null).addTextNode(String.valueOf(pageNumber)); + if(filter != null && !filter.isEmpty()) + for(String k : filter.keySet()) + root.addChildElement(k,null,null).addTextNode(filter.get(k)); + if(filter == null || !filter.keySet().contains('per_page')) + root.addChildElement('per_page',null,null).addTextNode(String.valueOf(LIST_PAGE_COUNT_MAX)); + Dom.Document responseDoc = ( inTest == true ? CambridgeCloudPartnersREST.domify(TEST_GET_TIME_ENTRIES_XML) : post(requestDoc) ); + checkError(responseDoc,'Error occured when getting time entries'); + Dom.XmlNode responseRoot = responseDoc.getRootElement(); + Dom.XmlNode payments = responseRoot.getChildElement('time_entries',FRESHBOOKS_XML_API_NS); + List toReturn = new List(); + if(payments != null && payments.getChildElements() != null && !payments.getChildElements().isEmpty()) + for(Dom.Xmlnode payment : payments.getChildElements()){ + toReturn.add(xml2Payment(payment)); + } + return toReturn; + } + private static Freshbooks_Payment__c xml2Payment(Dom.Xmlnode node){ + Freshbooks_Payment__c payment = new Freshbooks_Payment__c(); + try{ + payment.Name = node.getChildElement('payment_id',FRESHBOOKS_XML_API_NS).getText(); + payment.Freshbooks_Payment_ID__c = Integer.valueOf(node.getChildElement('payment_id',FRESHBOOKS_XML_API_NS).getText()); + payment.Freshbooks_Invoice__r = new Freshbooks_Invoice__c(Freshbooks_Invoice_ID__c = node.getChildElement('invoice_id',FRESHBOOKS_XML_API_NS).getText()); + payment.Date__c = Date.valueOf(node.getChildElement('date',FRESHBOOKS_XML_API_NS).getText()); + payment.Type__c = node.getChildElement('type',FRESHBOOKS_XML_API_NS).getText(); + payment.Notes__c = node.getChildElement('notes',FRESHBOOKS_XML_API_NS).getText(); + payment.Account__r = new Account(Freshbooks_Client_ID__c = node.getChildElement('client_id',FRESHBOOKS_XML_API_NS).getText()); + payment.Currency_Code__c = node.getChildElement('currency_code',FRESHBOOKS_XML_API_NS).getText(); + payment.Amount__c = Double.valueOf(node.getChildElement('amount',FRESHBOOKS_XML_API_NS).getText()); + }catch(Exception e){ + throw new FreshbooksException('Error parsing payment information response from Freshbooks: '+e.getMessage()+node); + } + return payment; + } + + @isTest public static void test_xml2Payment(){ + inTest = true; + Dom.Document doc = new Dom.Document(); + doc.load(TEST_GET_PAYMENTS_XML); + Freshbooks_Payment__c p = xml2Payment(doc.getRootElement().getChildElement('payments',FRESHBOOKS_XML_API_NS).getChildElement('payment',FRESHBOOKS_XML_API_NS)); + System.assertEquals('123', p.Name); + System.assertEquals(123, p.Freshbooks_Payment_ID__c); + } + + + // --- helpers /* handle later when we can do a describe on the data types private void zipperXmlToSObject(Dom.XmlNode node, Map mappings, SObject sobj){ diff --git a/src/classes/FreshbooksSyncBatch.cls b/src/classes/FreshbooksSyncBatch.cls index 3d1f9bc..d13b926 100644 --- a/src/classes/FreshbooksSyncBatch.cls +++ b/src/classes/FreshbooksSyncBatch.cls @@ -120,6 +120,17 @@ global class FreshbooksSyncBatch implements Schedulable, Database.Batchable{'client_id'=>t.id}); + }catch(Exception e){ + requeue(t, e); + continue; + } + if(payments.size() == FreshbooksAPI.LIST_PAGE_COUNT_MAX) + taskQueue.add(new FreshbooksSyncTask('payments_by_client',t.id,t.page+1)); + upsert payments Freshbooks_Payment_ID__c; } } } @@ -137,6 +148,8 @@ global class FreshbooksSyncBatch implements Schedulable, Database.Batchable{0}); controller.taskQueue = new List{new FreshbooksSyncTask('invoices_by_client', '123', 0)}; controller.execute(null, new List{0}); + controller.taskQueue = new List{new FreshbooksSyncTask('payments_by_client', '123', 0)}; + controller.execute(null, new List{0}); } global void finish(Database.BatchableContext BC){ diff --git a/src/layouts/Freshbooks_Invoice__c-Freshbooks Invoice Layout.layout b/src/layouts/Freshbooks_Invoice__c-Freshbooks Invoice Layout.layout index 05b39f0..4ce9189 100644 --- a/src/layouts/Freshbooks_Invoice__c-Freshbooks Invoice Layout.layout +++ b/src/layouts/Freshbooks_Invoice__c-Freshbooks Invoice Layout.layout @@ -71,6 +71,10 @@ + + NAME + Freshbooks_Payment__c.Freshbooks_Invoice__c + false true true diff --git a/src/layouts/Freshbooks_Payment__c-Freshbooks Payment Layout.layout b/src/layouts/Freshbooks_Payment__c-Freshbooks Payment Layout.layout new file mode 100644 index 0000000..aca3fa2 --- /dev/null +++ b/src/layouts/Freshbooks_Payment__c-Freshbooks Payment Layout.layout @@ -0,0 +1,87 @@ + + + + false + false + true + + + + Required + Name + + + Edit + Freshbooks_Invoice__c + + + Edit + Account__c + + + Edit + Date__c + + + Edit + Type__c + + + Edit + Notes__c + + + Edit + Currency_Code__c + + + Edit + Amount__c + + + Edit + Freshbooks_Payment_ID__c + + + + + Edit + OwnerId + + + + + + false + false + true + + + + Readonly + CreatedById + + + + + Readonly + LastModifiedById + + + + + + false + false + true + + + + + + false + false + false + false + false + diff --git a/src/objectTranslations/Freshbooks_Invoice__c-en_US.objectTranslation b/src/objectTranslations/Freshbooks_Invoice__c-en_US.objectTranslation deleted file mode 100644 index 9cd7dda..0000000 --- a/src/objectTranslations/Freshbooks_Invoice__c-en_US.objectTranslation +++ /dev/null @@ -1,37 +0,0 @@ - - - - false - Freshbooks Invoice - - - true - Freshbooks Invoices - - - - Account__c - - - - - Amount_Outstanding__c - - - - Amount__c - - - - Freshbooks_Invoice_ID__c - - - - Invoice_Date__c - - - - Status__c - - Consonant - diff --git a/src/objectTranslations/Freshbooks_Project__c-en_US.objectTranslation b/src/objectTranslations/Freshbooks_Project__c-en_US.objectTranslation deleted file mode 100644 index 986549c..0000000 --- a/src/objectTranslations/Freshbooks_Project__c-en_US.objectTranslation +++ /dev/null @@ -1,48 +0,0 @@ - - - - false - Freshbooks Project - - - true - Freshbooks Projects - - - - Account__c - - - - - Bill_Method__c - - - - Description__c - - - - Freshbooks_Project_ID__c - - - - Hour_Budget__c - - - - Rate__c - - - Freshbooks Project Layout - - -
Custom Links
-
- - -
Description
-
-
- Consonant -
diff --git a/src/objectTranslations/Freshbooks_Settings_Protected__c-en_US.objectTranslation b/src/objectTranslations/Freshbooks_Settings_Protected__c-en_US.objectTranslation deleted file mode 100644 index 13bb1c0..0000000 --- a/src/objectTranslations/Freshbooks_Settings_Protected__c-en_US.objectTranslation +++ /dev/null @@ -1,20 +0,0 @@ - - - - false - Freshbooks Settings Protected - - - true - Freshbooks Settings Protected - - - - Subdomain__c - - - - Token__c - - Consonant - diff --git a/src/objectTranslations/Freshbooks_Time_Entry__c-en_US.objectTranslation b/src/objectTranslations/Freshbooks_Time_Entry__c-en_US.objectTranslation deleted file mode 100644 index 72db1bf..0000000 --- a/src/objectTranslations/Freshbooks_Time_Entry__c-en_US.objectTranslation +++ /dev/null @@ -1,33 +0,0 @@ - - - - false - Freshbooks Time Entry - - - true - Freshbooks Time Entries - - - - Date__c - - - - Freshbooks_Project__c - - - - - Freshbooks_Time_Entry_ID__c - - - - Hours__c - - - - Notes__c - - Consonant - diff --git a/src/objects/Freshbooks_Payment__c.object b/src/objects/Freshbooks_Payment__c.object new file mode 100644 index 0000000..9c3b280 --- /dev/null +++ b/src/objects/Freshbooks_Payment__c.object @@ -0,0 +1,146 @@ + + + + Accept + Default + + + CancelEdit + Default + + + Clone + Default + + + Delete + Default + + + Edit + Default + + + Follow + Default + + + List + Default + + + New + Default + + + SaveEdit + Default + + + Tab + Default + + + View + Default + + SYSTEM + Deployed + false + true + false + false + true + true + true + + Account__c + SetNull + false + + Account + Freshbooks Payments + Freshbooks_Payments + false + false + Lookup + + + Amount__c + false + + 18 + false + 2 + false + Currency + + + Currency_Code__c + false + + 255 + false + false + Text + false + + + Date__c + false + + false + false + Date + + + Freshbooks_Invoice__c + SetNull + false + + Freshbooks_Invoice__c + Freshbooks Payments + Freshbooks_Payments + false + false + Lookup + + + Freshbooks_Payment_ID__c + true + + 18 + false + 0 + false + Number + true + + + Notes__c + false + + 32768 + false + LongTextArea + 3 + + + Type__c + false + + 255 + false + false + Text + false + + + + + Text + + Freshbooks Payments + + ReadWrite + diff --git a/src/package.xml b/src/package.xml index 3a54d2e..4104ad6 100644 --- a/src/package.xml +++ b/src/package.xml @@ -29,6 +29,14 @@ Freshbooks_Invoice__c.Freshbooks_Invoice_ID__c Freshbooks_Invoice__c.Invoice_Date__c Freshbooks_Invoice__c.Status__c + Freshbooks_Payment__c.Account__c + Freshbooks_Payment__c.Amount__c + Freshbooks_Payment__c.Currency_Code__c + Freshbooks_Payment__c.Date__c + Freshbooks_Payment__c.Freshbooks_Invoice__c + Freshbooks_Payment__c.Freshbooks_Payment_ID__c + Freshbooks_Payment__c.Notes__c + Freshbooks_Payment__c.Type__c Freshbooks_Project__c.Account__c Freshbooks_Project__c.Bill_Method__c Freshbooks_Project__c.Description__c @@ -46,6 +54,7 @@ Freshbooks_Invoice__c + Freshbooks_Payment__c Freshbooks_Project__c Freshbooks_Settings_Protected__c Freshbooks_Time_Entry__c @@ -59,6 +68,7 @@ Freshbooks_Invoice__c-Freshbooks Invoice Layout + Freshbooks_Payment__c-Freshbooks Payment Layout Freshbooks_Project__c-Freshbooks Project Layout Freshbooks_Time_Entry__c-Freshbooks Time Entry Layout Layout diff --git a/src/reports/Freshbooks-meta.xml b/src/reports/Freshbooks-meta.xml deleted file mode 100644 index 7b938ab..0000000 --- a/src/reports/Freshbooks-meta.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - Public - Freshbooks - ReadOnly -