示例#1
0
 /**
  * Returns a connection to the Harvest API based on the configuration.
  * 
  * @return \HarvestAPI
  */
 protected function getHarvestApi()
 {
     $harvest = new \HarvestAPI();
     $harvest->setAccount($this->harvestConfig['account']);
     $harvest->setUser($this->harvestConfig['username']);
     $harvest->setPassword($this->harvestConfig['password']);
     $harvest->setSSL($this->harvestConfig['account']);
     return $harvest;
 }
示例#2
0
 /**
  * Get the root path to Harvest API
  *
  * <code>
  * $api = new HarvestAPI();
  * $api->getPath();
  * </code>
  *
  * @return String
  */
 public static function getPath()
 {
     if (!self::$_path) {
         self::$_path = dirname(__FILE__);
     }
     return self::$_path;
 }
示例#3
0
 /**
  * Final step is to import invoices and payments
  * @return
  */
 public static function import_invoices()
 {
     // Store the import progress
     $progress = get_option(self::PROGRESS_OPTION, array());
     // Suppress notifications
     add_filter('suppress_notifications', '__return_true');
     if (!isset($progress['invoices_complete'])) {
         set_time_limit(0);
         // run script forever
         $progress_key = 'invoices_import_progress';
         if (!isset($progress[$progress_key])) {
             $progress[$progress_key] = 0;
             update_option(self::PROGRESS_OPTION, $progress);
         }
         // Check which chunk we're at
         // Since increment of 50 invoices will tend to bring a server to it's knees
         // break up the returned results down.
         $progress_pagechunk_key = 'invoices_import_progress_pagechunk';
         if (!isset($progress[$progress_pagechunk_key])) {
             $progress[$progress_pagechunk_key] = 0;
             update_option(self::PROGRESS_OPTION, $progress);
         }
         // If we're just starting out than provide messaging
         if ($progress[$progress_key] == 0) {
             $progress[$progress_key]++;
             update_option(self::PROGRESS_OPTION, $progress);
             // Return the progress
             self::return_progress(array('authentication' => array('message' => self::__('Attempting to import your invoices and their payments...'), 'progress' => 60 + $progress[$progress_key]), 'invoices' => array('message' => sprintf(self::__('Currently importing invoices and their payments in increments of %s. Thank you for your patience, this is a very slow process.'), 50), 'progress' => 15 + $progress[$progress_key] * 5), 'payments' => array('message' => self::__('Payments will be imported with new invoices'), 'progress' => 15 + $progress[$progress_key] * 5, 'next_step' => 'invoices')));
         }
         require_once SI_PATH . '/importers/lib/harvest/HarvestAPI.php';
         spl_autoload_register(array('HarvestAPI', 'autoload'));
         $api = new HarvestAPI();
         $api->setUser(self::$harvest_user);
         $api->setPassword(self::$harvest_pass);
         $api->setAccount(self::$harvest_account);
         $api->setRetryMode(HarvestAPI::RETRY);
         $api->setSSL(true);
         $filter = new Harvest_Invoice_Filter();
         $filter->set('page', $progress[$progress_key]);
         $result = $api->getInvoices($filter);
         if (!$result->isSuccess()) {
             self::return_error(self::__('Invoice import error.'));
         }
         if ($result->isSuccess()) {
             $payments_imported = 0;
             $invoices_imported = 0;
             // Break the array up into pages of 10 items
             $paged_data = array_chunk($result->data, apply_filters('si_harvest_import_increments_for_invoices', 10));
             do_action('si_error', __CLASS__ . '::' . __FUNCTION__ . ' page: ', $progress[$progress_key], false);
             do_action('si_error', __CLASS__ . '::' . __FUNCTION__ . ' chunk count: ', count($paged_data), false);
             do_action('si_error', __CLASS__ . '::' . __FUNCTION__ . ' chunk progress: ', $progress[$progress_pagechunk_key], false);
             if (isset($paged_data[$progress[$progress_pagechunk_key]])) {
                 $current_chunk = $paged_data[$progress[$progress_pagechunk_key]];
                 foreach ($current_chunk as $key => $invoice) {
                     $invoices_imported++;
                     $invoice_id = $invoice->id;
                     $new_invoice = self::create_invoice($invoice);
                     if (is_a($new_invoice, 'SI_Invoice')) {
                         ////////////////
                         // Line Items //
                         ////////////////
                         $result = $api->getInvoice($invoice_id);
                         if ($result->isSuccess()) {
                             self::add_invoice_line_items($result->data, $new_invoice);
                         }
                         //////////////
                         // Payments //
                         //////////////
                         $result = $api->getInvoicePayments($invoice_id);
                         if ($result->isSuccess()) {
                             foreach ($result->data as $payment_id => $payment) {
                                 $payments_imported++;
                                 self::create_invoice_payment($payment, $new_invoice);
                             }
                         }
                     }
                 }
                 // Update the page chunk currently processed
                 $progress[$progress_pagechunk_key]++;
                 // If the last chunk was just processed than
                 // start the progress over
                 if ($progress[$progress_pagechunk_key] == count($paged_data)) {
                     unset($progress[$progress_pagechunk_key]);
                     // Total progress for paged HAPI filter
                     $progress[$progress_key]++;
                     update_option(self::PROGRESS_OPTION, $progress);
                 }
                 update_option(self::PROGRESS_OPTION, $progress);
                 // Return the progress
                 self::return_progress(array('authentication' => array('message' => sprintf(self::__('Attempting to import %s new invoices and their payments...'), $invoices_imported), 'progress' => 60 + $progress[$progress_key]), 'payments' => array('message' => sprintf(self::__('Just imported %s more payments.'), $payments_imported), 'progress' => 15 + $progress[$progress_key] * 2), 'invoices' => array('message' => sprintf(self::__('Importing invoices in increments of %s. Thank you for your patience, this is a very slow process.'), apply_filters('si_harvest_import_increments_for_invoices', 10)), 'progress' => 15 + $progress[$progress_key] * 2, 'next_step' => 'invoices')));
             }
         }
         // Mark as complete
         $progress['invoices_complete'] = 1;
         update_option(self::PROGRESS_OPTION, $progress);
         // Complete
         self::return_progress(array('authentication' => array('message' => self::__('Successfully imported a bunch of invoices...'), 'progress' => 100), 'payments' => array('message' => self::__('Successfully imported a bunch of payments.'), 'progress' => 100), 'invoices' => array('message' => self::__('Finished importing your invoices!'), 'progress' => 100, 'next_step' => 'complete')));
     }
     // Completed previously
     self::return_progress(array('authentication' => array('message' => self::__('Successfully imported invoices already, moving on...'), 'progress' => 50), 'payments' => array('message' => self::__('Successfully imported a bunch of payments already.'), 'progress' => 100), 'invoices' => array('progress' => 100, 'message' => self::__('Imported all the invoices already!'), 'next_step' => 'complete')));
     // If this is needed something went wrong since json should have been printed and exited.
     return;
 }