示例#1
0
    function index()
    {
        if ($this->user->admin == 1) {
            $settings = Setting::first();
            $this->load->helper('curl');
            $object = remote_get_contents('http://fc2.luxsys-apps.com/updates/xml.php?code=' . $settings->pc);
            $object = json_decode($object);
            $this->view_data['update'] = FALSE;
            if (isset($object->error)) {
                if ($object->error == FALSE && $object->lastupdate > $settings->version) {
                    $this->view_data['update'] = $object->lastupdate;
                }
            }
        }
        $year = date('Y', time());
        $this->view_data["year"] = $year;
        $thismonth = date('m');
        $this->view_data["month"] = date('M');
        $tax = $this->view_data['core_settings']->tax;
        //calculate stats
        $result = Invoice::find_by_sql("SELECT paid_date, SUM(`sum`) AS summary FROM `invoices` WHERE estimate != 1 AND status = 'Paid' AND paid_date between '{$year}-01-01' AND '{$year}-12-31' GROUP BY SUBSTR(paid_date,1,7)");
        $this->view_data["stats"] = $result;
        //Projects
        //open
        $this->view_data["projects_open"] = Project::count(array('conditions' => array('progress < ?', 100)));
        //all
        $this->view_data["projects_all"] = Project::count();
        //invoices
        //open
        $this->view_data["invoices_open"] = Invoice::count(array('conditions' => array('status != ? AND estimate != ?', 'Paid', 1)));
        //all
        $this->view_data["invoices_all"] = Invoice::count(array('conditions' => array('estimate != ?', 1)));
        //payments open
        $result = Invoice::find_by_sql("SELECT SUM(`sum`) AS summary FROM `invoices` WHERE estimate != 1 AND status = 'Paid' AND paid_date between '{$year}-{$thismonth}-01' AND '{$year}-{$thismonth}-31'");
        $this->view_data["payments"] = $result;
        //payments outstanding
        $result = Invoice::find_by_sql("SELECT SUM(`sum`) AS summary FROM `invoices` WHERE estimate != 1 AND status != 'Paid'");
        $this->view_data["paymentsoutstanding"] = $result;
        //Events
        $events = array();
        $date = date('Y-m-d', time());
        $eventcount = 0;
        foreach ($this->view_data['menu'] as $key => $value) {
            if ($value->link == "invoices") {
                $sql = 'SELECT * FROM invoices WHERE status != "Paid" AND due_date < "' . $date . '" AND estimate != 1 ORDER BY due_date';
                $res = Invoice::find_by_sql($sql);
                //$res = $res->result();
                foreach ($res as $key2 => $value2) {
                    $eventline = str_replace("{invoice_number}", '<a href="' . base_url() . 'invoices/view/' . $value2->id . '">#' . $this->view_data['core_settings']->invoice_prefix . $value2->reference . '</a>', $this->lang->line('event_invoice_overdue'));
                    $events[$value2->due_date . "." . $value2->id] = $eventline;
                    $eventcount = $eventcount + 1;
                }
            }
            if ($value->link == "projects") {
                $sql = 'SELECT * FROM projects WHERE progress != "100" AND end < "' . $date . '" ORDER BY end';
                $res = Project::find_by_sql($sql);
                //$res = $res->result();
                foreach ($res as $key2 => $value2) {
                    if ($this->user->admin == 0) {
                        $sql = "SELECT id FROM `project_has_workers` WHERE project_id = " . $value->id . " AND user_id = " . $this->user->id;
                        $res = Project::find_by_sql($sql);
                        //$res = $query;
                        if ($res) {
                            $eventline = str_replace("{project_number}", '<a href="' . base_url() . 'projects/view/' . $value2->id . '">#' . $this->view_data['core_settings']->project_prefix . $value2->reference . '</a>', $this->lang->line('event_project_overdue'));
                            $events[$value2->end . "." . $value2->id] = $eventline;
                            $eventcount = $eventcount + 1;
                        }
                    } else {
                        $eventline = str_replace("{project_number}", '<a href="' . base_url() . 'projects/view/' . $value2->id . '">#' . $this->view_data['core_settings']->project_prefix . $value2->reference . '</a>', $this->lang->line('event_project_overdue'));
                        $events[$value2->end . "." . $value2->id] = $eventline;
                        $eventcount = $eventcount + 1;
                    }
                }
            }
            if ($value->link == "subscriptions") {
                $sql = 'SELECT * FROM subscriptions WHERE status != "Inactive" AND end_date > "' . $date . '" AND next_payment <= "' . $date . '" ORDER BY next_payment';
                $res = Subscription::find_by_sql($sql);
                //$res = $res->result();
                foreach ($res as $key2 => $value2) {
                    $eventline = str_replace("{subscription_number}", '<a href="' . base_url() . 'subscriptions/view/' . $value2->id . '">#' . $this->view_data['core_settings']->subscription_prefix . $value2->reference . '</a>', $this->lang->line('event_subscription_new_invoice'));
                    $events[$value2->next_payment . "." . $value2->id] = $eventline;
                    $eventcount = $eventcount + 1;
                }
            }
            if ($value->link == "messages") {
                $sql = 'SELECT privatemessages.id, privatemessages.`status`, privatemessages.subject, privatemessages.message, privatemessages.`time`, privatemessages.`recipient`, clients.`userpic` as userpic_c, users.`userpic` as userpic_u  , users.`email` as email_u , clients.`email` as email_c , CONCAT(users.firstname," ", users.lastname) as sender_u, CONCAT(clients.firstname," ", clients.lastname) as sender_c
							FROM privatemessages
							LEFT JOIN clients ON CONCAT("c",clients.id) = privatemessages.sender
							LEFT JOIN users ON CONCAT("u",users.id) = privatemessages.sender 
							GROUP by privatemessages.id HAVING privatemessages.recipient = "u' . $this->user->id . '"AND privatemessages.status != "deleted" ORDER BY privatemessages.`time` DESC LIMIT 6';
                $query = Privatemessage::find_by_sql($sql);
                $this->view_data["message"] = array_filter($query);
            }
            if ($value->link == "projects") {
                $sql = 'SELECT * FROM project_has_tasks WHERE status != "done" AND user_id = "' . $this->user->id . '" ORDER BY project_id';
                $taskquery = ProjectHasTask::find('all', array('conditions' => array('status != ? and user_id = ?', 'done', $this->user->id), 'order' => 'project_id desc'));
                $this->view_data["tasks"] = $taskquery;
            }
        }
        krsort($events);
        $this->view_data["events"] = $events;
        $this->view_data["eventcount"] = $eventcount;
        $this->content_view = 'dashboard/dashboard';
    }
 function tasks($id = FALSE, $condition = FALSE, $task_id = FALSE)
 {
     $this->view_data['submenu'] = array($this->lang->line('application_back') => 'projects', $this->lang->line('application_overview') => 'projects/view/' . $id);
     switch ($condition) {
         case 'add':
             $this->content_view = 'projects/_tasks';
             if ($_POST) {
                 unset($_POST['send']);
                 unset($_POST['files']);
                 $description = $_POST['description'];
                 $_POST = array_map('htmlspecialchars', $_POST);
                 $_POST['description'] = $description;
                 $_POST['project_id'] = $id;
                 $task = ProjectHasTask::create($_POST);
                 if (!$task) {
                     $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_save_task_error'));
                 } else {
                     $this->session->set_flashdata('message', 'success:' . $this->lang->line('messages_save_task_success'));
                 }
                 redirect('projects/view/' . $id);
             } else {
                 $this->theme_view = 'modal';
                 $this->view_data['project'] = Project::find($id);
                 $this->view_data['title'] = $this->lang->line('application_add_task');
                 $this->view_data['form_action'] = 'projects/tasks/' . $id . '/add';
                 $this->content_view = 'projects/_tasks';
             }
             break;
         case 'update':
             $this->content_view = 'projects/_tasks';
             $this->view_data['task'] = ProjectHasTask::find($task_id);
             if ($_POST) {
                 unset($_POST['send']);
                 unset($_POST['files']);
                 if (!isset($_POST['public'])) {
                     $_POST['public'] = 0;
                 }
                 $description = $_POST['description'];
                 $_POST = array_map('htmlspecialchars', $_POST);
                 $_POST['description'] = $description;
                 $task_id = $_POST['id'];
                 $task = ProjectHasTask::find($task_id);
                 $task->update_attributes($_POST);
                 if (!$task) {
                     $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_save_task_error'));
                 } else {
                     $this->session->set_flashdata('message', 'success:' . $this->lang->line('messages_save_task_success'));
                 }
                 redirect('projects/view/' . $id);
             } else {
                 $this->theme_view = 'modal';
                 $this->view_data['project'] = Project::find($id);
                 $this->view_data['title'] = $this->lang->line('application_edit_task');
                 $this->view_data['form_action'] = 'projects/tasks/' . $id . '/update/' . $task_id;
                 $this->content_view = 'projects/_tasks';
             }
             break;
         case 'check':
             $task = ProjectHasTask::find($task_id);
             if ($task->status == 'done') {
                 $task->status = 'open';
             } else {
                 $task->status = 'done';
             }
             $task->save();
             $project = Project::find($id);
             $tasks = ProjectHasTask::count(array('conditions' => 'project_id = ' . $id));
             $tasks_done = ProjectHasTask::count(array('conditions' => array('status = ? AND project_id = ?', 'done', $id)));
             if ($project->progress_calc == 1) {
                 if ($tasks) {
                     $progress = round($tasks_done / $tasks * 100);
                 }
                 $attr = array('progress' => $progress);
                 $project->update_attributes($attr);
             }
             if (!$task) {
                 $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_save_task_error'));
             }
             $this->theme_view = 'ajax';
             $this->content_view = 'projects';
             break;
         case 'delete':
             $task = ProjectHasTask::find($task_id);
             $task->delete();
             if (!$task) {
                 $this->session->set_flashdata('message', 'error:' . $this->lang->line('messages_delete_task_error'));
             } else {
                 $this->session->set_flashdata('message', 'success:' . $this->lang->line('messages_delete_task_success'));
             }
             redirect('projects/view/' . $id);
             break;
         default:
             $this->view_data['project'] = Project::find($id);
             $this->content_view = 'projects/tasks';
             break;
     }
 }