X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/timetracker.git/blobdiff_plain/af4d194c7beb4020c7aff5f8c9eb36156b9b407e..280d6ea0ee096843551d7734b23377ec06d42e81:/WEB-INF/lib/ttOrgImportHelper.class.php diff --git a/WEB-INF/lib/ttOrgImportHelper.class.php b/WEB-INF/lib/ttOrgImportHelper.class.php index 96ac5708..d95aabb4 100644 --- a/WEB-INF/lib/ttOrgImportHelper.class.php +++ b/WEB-INF/lib/ttOrgImportHelper.class.php @@ -32,6 +32,7 @@ import('ttTaskHelper'); import('ttProjectHelper'); import('ttClientHelper'); import('ttInvoiceHelper'); +import('ttTimeHelper'); import('ttCustomFieldHelper'); import('ttExpenseHelper'); import('ttFavReportHelper'); @@ -203,8 +204,8 @@ class ttOrgImportHelper { foreach ($tasks as $id) $mapped_tasks[] = $this->currentGroupTaskMap[$id]; - $project_id = ttProjectHelper::insert(array( - 'group_id' => $this->current_group_id, + $project_id = ttProjectHelper::insert(array( // TODO: continue refactoring, write a separate, simple function to import projects + 'group_id' => $this->current_group_id, // because ttProjectHelper::insert is complicated. 'org_id' => $this->org_id, 'name' => $attrs['NAME'], 'description' => $attrs['DESCRIPTION'], @@ -414,11 +415,11 @@ class ttOrgImportHelper { if ($name == 'EXPENSE_ITEM') { // We get here when processing tags for the current group. - $expense_item_id = ttExpenseHelper::insert(array( + $expense_item_id = $this->insertExpense(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. + 'org_id' => $this->org_id, 'client_id' => $this->currentGroupClientMap[$attrs['CLIENT_ID']], 'project_id' => $this->currentGroupProjectMap[$attrs['PROJECT_ID']], 'name' => $attrs['NAME'], @@ -430,12 +431,24 @@ class ttOrgImportHelper { return; } + if ($name == 'PREDEFINED_EXPENSE') { + if (!$this->insertPredefinedExpense(array( + 'group_id' => $this->current_group_id, + 'org_id' => $this->org_id, + 'name' => $attrs['NAME'], + 'cost' => $attrs['COST']))) { + $this->errors->add($i18n->get('error.db')); + } + return; + } + if ($name == 'MONTHLY_QUOTA') { - if (!$this->insertMonthlyQuota($this->current_group_id, - // 'org_id' => $this->org_id, TODO: add this when org_id field is added to the table. - $attrs['YEAR'], - $attrs['MONTH'], - $attrs['MINUTES'])) { + if (!$this->insertMonthlyQuota(array( + 'group_id' => $this->current_group_id, + 'org_id' => $this->org_id, + 'year' => $attrs['YEAR'], + 'month' => $attrs['MONTH'], + 'minutes' => $attrs['MINUTES']))) { $this->errors->add($i18n->get('error.db')); } return; @@ -639,9 +652,56 @@ class ttOrgImportHelper { } // insertMonthlyQuota - a helper function to insert a monthly quota. - private function insertMonthlyQuota($group_id, $year, $month, $minutes) { + private function insertMonthlyQuota($fields) { $mdb2 = getConnection(); - $sql = "INSERT INTO tt_monthly_quotas (group_id, year, month, minutes) values ($group_id, $year, $month, $minutes)"; + $group_id = (int) $fields['group_id']; + $org_id = (int) $fields['org_id']; + $year = (int) $fields['year']; + $month = (int) $fields['month']; + $minutes = (int) $fields['minutes']; + + $sql = "INSERT INTO tt_monthly_quotas (group_id, org_id, year, month, minutes)". + " values ($group_id, $org_id, $year, $month, $minutes)"; + $affected = $mdb2->exec($sql); + return (!is_a($affected, 'PEAR_Error')); + } + + // insertPredefinedExpense - a helper function to insert a predefined expense. + private function insertPredefinedExpense($fields) { + $mdb2 = getConnection(); + $group_id = (int) $fields['group_id']; + $org_id = (int) $fields['org_id']; + $name = $mdb2->quote($fields['name']); + $cost = $mdb2->quote($fields['cost']); + + $sql = "INSERT INTO tt_predefined_expenses (group_id, org_id, name, cost)". + " values ($group_id, $org_id, $name, $cost)"; + $affected = $mdb2->exec($sql); + return (!is_a($affected, 'PEAR_Error')); + } + + // insertExpense - a helper function to insert an expense item. + private function insertExpense($fields) { + global $user; + $mdb2 = getConnection(); + + $group_id = (int) $fields['group_id']; + $org_id = (int) $fields['org_id']; + $date = $fields['date']; + $user_id = (int) $fields['user_id']; + $client_id = $fields['client_id']; + $project_id = $fields['project_id']; + $name = $fields['name']; + $cost = str_replace(',', '.', $fields['cost']); + $invoice_id = $fields['invoice_id']; + $status = $fields['status']; + $paid = (int) $fields['paid']; + $created = ', now(), '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', '.$mdb2->quote($user->id); + + $sql = "insert into tt_expense_items". + " (date, user_id, group_id, org_id, client_id, project_id, name, cost, invoice_id, paid, created, created_ip, created_by, status)". + " values (".$mdb2->quote($date).", $user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id). + ", ".$mdb2->quote($name).", ".$mdb2->quote($cost).", ".$mdb2->quote($invoice_id).", $paid $created, ".$mdb2->quote($status).")"; $affected = $mdb2->exec($sql); return (!is_a($affected, 'PEAR_Error')); }