]> wagnertech.de Git - timetracker.git/blobdiff - WEB-INF/lib/ttOrgImportHelper.class.php
Integrated expense items in new export-import.
[timetracker.git] / WEB-INF / lib / ttOrgImportHelper.class.php
index ccdc05b20f8c4665094894c5b90a6e557ec326b7..89428c4c9cd6a5732aff20e360e52319bf12e9da 100644 (file)
@@ -31,6 +31,9 @@ import('ttRoleHelper');
 import('ttTaskHelper');
 import('ttProjectHelper');
 import('ttClientHelper');
+import('ttInvoiceHelper');
+import('ttCustomFieldHelper');
+import('ttExpenseHelper');
 
 // ttOrgImportHelper - this class is a future replacement for ttImportHelper.
 // Currently, it is work in progress.
@@ -52,6 +55,10 @@ class ttOrgImportHelper {
   var $currentGroupProjectMap = array();
   var $currentGroupClientMap  = array();
   var $currentGroupUserMap    = array();
+  var $currentGroupInvoiceMap = array();
+  var $currentGroupLogMap     = array();
+  var $currentGroupCustomFieldMap = array();
+  var $currentGroupCustomFieldOptionMap = array();
 
   // Constructor.
   function __construct(&$errors) {
@@ -255,6 +262,132 @@ class ttOrgImportHelper {
           $this->errors->add($i18n->get('error.db'));
         }
       }
+
+      if ($name == 'INVOICES') {
+        // If we get here, we have to recycle $currentGroupInvoiceMap.
+        unset($this->currentGroupInvoiceMap);
+        $this->currentGroupInvoiceMap = array();
+        // Invoice map is reconstructed after processing <invoice> elements in XML. See below.
+      }
+
+      if ($name == 'INVOICE') {
+        // We get here when processing <invoice> tags for the current group.
+        $invoice_id = ttInvoiceHelper::insert(array(
+          'group_id' => $this->current_group_id,
+          'org_id' => $this->org_id,
+          'name' => $attrs['NAME'],
+          'date' => $attrs['DATE'],
+          'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
+          'status' => $attrs['STATUS']));
+        if ($invoice_id) {
+          // Add a mapping.
+          $this->currentGroupInvoiceMap[$attrs['ID']] = $invoice_id;
+        } else $this->errors->add($i18n->get('error.db'));
+      }
+
+      if ($name == 'LOG') {
+        // If we get here, we have to recycle $currentGroupLogMap.
+        unset($this->currentGroupLogMap);
+        $this->currentGroupLogMap = array();
+        // Log map is reconstructed after processing <log_item> elements in XML. See below.
+      }
+
+      if ($name == 'LOG_ITEM') {
+        // We get here when processing <log_item> tags for the current group.
+        $log_item_id = ttTimeHelper::insert(array(
+          'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
+          'group_id' => $this->current_group_id,
+          'org_id' => $this->org_id,
+          'date' => $attrs['DATE'],
+          'start' => $attrs['START'],
+          'finish' => $attrs['FINISH'],
+          'duration' => $attrs['DURATION'],
+          'client' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
+          'project' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
+          'task' => $this->currentGroupTaskMap[$attrs['TASK_ID']],
+          'invoice' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
+          'note' => (isset($attrs['COMMENT']) ? $attrs['COMMENT'] : ''),
+          'billable' => $attrs['BILLABLE'],
+          'paid' => $attrs['PAID'],
+          'status' => $attrs['STATUS']));
+        if ($log_item_id) {
+          // Add a mapping.
+          $this->currentGroupLogMap[$attrs['ID']] = $log_item_id;
+        } else $this->errors->add($i18n->get('error.db'));
+      }
+
+      if ($name == 'CUSTOM_FIELDS') {
+        // If we get here, we have to recycle $currentGroupCustomFieldMap.
+        unset($this->currentGroupCustomFieldMap);
+        $this->currentGroupCustomFieldMap = array();
+        // Custom field map is reconstructed after processing <custom_field> elements in XML. See below.
+      }
+
+      if ($name == 'CUSTOM_FIELD') {
+        // We get here when processing <custom_field> tags for the current group.
+        $custom_field_id = ttCustomFieldHelper::insertField(array(
+          'group_id' => $this->current_group_id,
+          // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
+          'type' => $attrs['TYPE'],
+          'label' => $attrs['LABEL'],
+          'required' => $attrs['REQUIRED'],
+          'status' => $attrs['STATUS']));
+        if ($custom_field_id) {
+          // Add a mapping.
+          $this->currentGroupCustomFieldMap[$attrs['ID']] = $custom_field_id;
+        } else $this->errors->add($i18n->get('error.db'));
+      }
+
+      if ($name == 'CUSTOM_FIELD_OPTIONS') {
+        // If we get here, we have to recycle $currentGroupCustomFieldOptionMap.
+        unset($this->currentGroupCustomFieldOptionMap);
+        $this->currentGroupCustomFieldOptionMap = array();
+        // Custom field option map is reconstructed after processing <custom_field_option> elements in XML. See below.
+      }
+
+      if ($name == 'CUSTOM_FIELD_OPTION') {
+        // We get here when processing <custom_field_option> tags for the current group.
+        $custom_field_option_id = ttCustomFieldHelper::insertOption(array(
+          // 'group_id' => $this->current_group_id, TODO: add this when group_id field is added to the table.
+          // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
+          'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
+          'value' => $attrs['VALUE']));
+        if ($custom_field_option_id) {
+          // Add a mapping.
+          $this->currentGroupCustomFieldOptionMap[$attrs['ID']] = $custom_field_option_id;
+        } else $this->errors->add($i18n->get('error.db'));
+      }
+
+      if ($name == 'CUSTOM_FIELD_LOG_ENTRY') {
+        // We get here when processing <custom_field_log_entry> tags for the current group.
+        if (!ttCustomFieldHelper::insertLogEntry(array(
+          // 'group_id' => $this->current_group_id, TODO: add this when group_id field is added to the table.
+          // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
+          'log_id' => $this->currentGroupLogMap[$attrs['LOG_ID']],
+          'field_id' => $this->currentGroupCustomFieldMap[$attrs['FIELD_ID']],
+          'option_id' => $this->currentGroupCustomFieldOptionMap[$attrs['OPTION_ID']],
+          'value' => $attrs['VALUE'],
+          'status' => $attrs['STATUS']))) {
+          $this->errors->add($i18n->get('error.db'));
+        }
+      }
+
+      if ($name == 'EXPENSE_ITEM') {
+        // We get here when processing <expense_item> tags for the current group.
+        $expense_item_id = ttExpenseHelper::insert(array(
+          'date' => $attrs['DATE'],
+          'user_id' => $this->currentGroupUserMap[$attrs['USER_ID']],
+          'group_id' => $this->current_group_id,
+          // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table.
+          'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']],
+          'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']],
+          'name' => $attrs['NAME'],
+          'cost' => $attrs['COST'],
+          'invoice_id' => $this->currentGroupInvoiceMap[$attrs['INVOICE_ID']],
+          'paid' => $attrs['PAID'],
+          'status' => $attrs['STATUS']));
+        if (!$expense_item_id) $this->errors->add($i18n->get('error.db'));
+      }
     }
   }