<commit_before><?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with this program; if not, see http://www.gnu.org/licenses
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301  USA
 * @copyright <NAME> 2014
 * @author <NAME> <<EMAIL>>
 */


if (!defined('sugarEntry') || !sugarEntry) {
    die('Not A Valid Entry Point');
}

class ProjectController extends SugarController
{
    //Loads the gantt view
    public function action_view_GanttChart()
    {
        $this->view = 'GanttChart';
    }

    public function action_generate_chart()
    {
        $db = DBManagerFactory::getInstance();

        include_once('modules/Project/gantt.php');
        include_once('modules/Project/project_table.php');

        $project = new Project();
        $project->retrieve($_POST["pid"]);
        
        //Get project tasks
        $Task = BeanFactory::getBean('ProjectTask');
        $tasks = $Task->get_full_list("order_number", "project_task.project_id = '".$project->id."'");
        
        //Get the start and end date of the project in database format
        $query = "SELECT min(date_start) FROM project_task WHERE project_id = '{$project->id}'";
        $start_date = $db->getOne($query);
        
        $query = "SELECT max(date_finish) FROM project_task WHERE project_id = '{$project->id}'";
        $end_date = $db->getOne($query);

        $duration = $this->count_days($start_date, $end_date);
        if ($duration < 30) {
            $query = "SELECT max(date_finish) + INTERVAL " . (30 - $duration) . " DAY FROM project_task WHERE project_id = '{$project->id}'";
            $end_date = $db->getOne($query);
        } ?>

        <script type="text/javascript">
            //Get the height if the #gantt div and add 18px
            var size = $('#gantt').height() +18;
            //Call jquery splitter function
            $('#project').splitter({
                outline: true
            });
            //Set height of gantt wrapping divs to make sure it shows
            $('#project').css('height', size+'px');
            $('.splitter-bar').css('height', size+'px');

        </script>
        <div id="project">
            <div id="left_pane">
                <?php new ProjectTable($tasks); ?>
            </div>
            <div id="right_pane">
                <div id="gantt">
                    <?php new Gantt($start_date, $end_date, $tasks); ?>
                </div>
                <div id="arrow_divs" style=""></div>
            </div>
        </div>

<?php
        die();
    }

    //Create new project task
    public function action_update_GanttChart()
    {
        global $current_user;
        $db = DBManagerFactory::getInstance();

        $task_name = $_POST['task_name'];
        $project_id = $_POST['project_id'];
        $override_business_hours = (int)$_POST['override_business_hours'];
        $task_id = $_POST['task_id'];
        $predecessor = $_POST['predecessor'];
        $rel_type = $_POST['rel_type'];
        $resource = $_POST['resource'];
        $percent = $_POST['percent'];
        $note = $_POST['note'];
        $actual_duration = $_POST['actual_duration'];

        if ($_POST['milestone'] == 'Milestone') {
            $milestone_flag = '1';
        } elseif ($_POST['milestone'] == 'Task') {
            $milestone_flag = '0';
        }

        $dateformat = $current_user->getPreference('datef');

        $startdate = DateTime::createFromFormat($dateformat, $_POST['start']);
        $start = $startdate->format('Y-m-d');

        //Take 1 off duration so that task displays in correct number of table cells in gantt chart.
        $duration = $_POST['duration'] -1;

        $duration_unit = $_POST['unit'];

        //Compensate for resulting negative number when a 0 duration is passed in above
        if ($duration < 0) {
            $duration = 0;
        }


        //------ build business hours array
        $days = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
        $businessHours = BeanFactory::getBean("AOBH_BusinessHours");
        $bhours = array();
        foreach ($days as $day) {
            $bh = $businessHours->getBusinessHoursForDay($day);

            if ($bh) {
                $bh = $bh[0];
                if ($bh->open) {
                    $open_h = $bh ? $bh->opening_hours : 9;
                    $close_h = $bh ? $bh->closing_hours : 17;

                    $start_time = DateTime::createFromFormat($dateformat, $_POST['start']);
                    $start_time = $start_time->modify('+'.$open_h.' Hours');

                    $end_time = DateTime::createFromFormat($dateformat, $_POST['start']);
                    $end_time = $end_time->modify('+'.$close_h.' Hours');

                    $hours = ($end_time->getTimestamp() - $start_time->getTimestamp())/(60*60);
                    if ($hours < 0) {
                        $hours = 0 - $hours ;
                    }

                    //$hours = $bh->diffBusinessHours($start_time, $end_time);
                    $bhours[$day] = $hours;
                } else {
                    $bhours[$day] = 0;
                }
            }
        }
        //-----------------------------------<commit_msg>Fix #3129 Use correct Business Hours field name for opening hours check<commit_after><?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
 * along with this program; if not, see http://www.gnu.org/licenses
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301  USA
 * @copyright <NAME> 2014
 * @author <NAME> <<EMAIL>>
 */


if (!defined('sugarEntry') || !sugarEntry) {
    die('Not A Valid Entry Point');
}

class ProjectController extends SugarController
{
    //Loads the gantt view
    public function action_view_GanttChart()
    {
        $this->view = 'GanttChart';
    }

    public function action_generate_chart()
    {
        $db = DBManagerFactory::getInstance();

        include_once('modules/Project/gantt.php');
        include_once('modules/Project/project_table.php');

        $project = new Project();
        $project->retrieve($_POST["pid"]);
        
        //Get project tasks
        $Task = BeanFactory::getBean('ProjectTask');
        $tasks = $Task->get_full_list("order_number", "project_task.project_id = '".$project->id."'");
        
        //Get the start and end date of the project in database format
        $query = "SELECT min(date_start) FROM project_task WHERE project_id = '{$project->id}'";
        $start_date = $db->getOne($query);
        
        $query = "SELECT max(date_finish) FROM project_task WHERE project_id = '{$project->id}'";
        $end_date = $db->getOne($query);

        $duration = $this->count_days($start_date, $end_date);
        if ($duration < 30) {
            $query = "SELECT max(date_finish) + INTERVAL " . (30 - $duration) . " DAY FROM project_task WHERE project_id = '{$project->id}'";
            $end_date = $db->getOne($query);
        } ?>

        <script type="text/javascript">
            //Get the height if the #gantt div and add 18px
            var size = $('#gantt').height() +18;
            //Call jquery splitter function
            $('#project').splitter({
                outline: true
            });
            //Set height of gantt wrapping divs to make sure it shows
            $('#project').css('height', size+'px');
            $('.splitter-bar').css('height', size+'px');

        </script>
        <div id="project">
            <div id="left_pane">
                <?php new ProjectTable($tasks); ?>
            </div>
            <div id="right_pane">
                <div id="gantt">
                    <?php new Gantt($start_date, $end_date, $tasks); ?>
                </div>
                <div id="arrow_divs" style=""></div>
            </div>
        </div>

<?php
        die();
    }

    //Create new project task
    public function action_update_GanttChart()
    {
        global $current_user;
        $db = DBManagerFactory::getInstance();

        $task_name = $_POST['task_name'];
        $project_id = $_POST['project_id'];
        $override_business_hours = (int)$_POST['override_business_hours'];
        $task_id = $_POST['task_id'];
        $predecessor = $_POST['predecessor'];
        $rel_type = $_POST['rel_type'];
        $resource = $_POST['resource'];
        $percent = $_POST['percent'];
        $note = $_POST['note'];
        $actual_duration = $_POST['actual_duration'];

        if ($_POST['milestone'] == 'Milestone') {
            $milestone_flag = '1';
        } elseif ($_POST['milestone'] == 'Task') {
            $milestone_flag = '0';
        }

        $dateformat = $current_user->getPreference('datef');

        $startdate = DateTime::createFromFormat($dateformat, $_POST['start']);
        $start = $startdate->format('Y-m-d');

        //Take 1 off duration so that task displays in correct number of table cells in gantt chart.
        $duration = $_POST['duration'] -1;

        $duration_unit = $_POST['unit'];

        //Compensate for resulting negative number when a 0 duration is passed in above
        if ($duration < 0) {
            $duration = 0;
        }


        //------ build business hours array
        $days = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
        $businessHours = BeanFactory::getBean("AOBH_BusinessHours");
        $bhours = array();
        foreach ($days as $day) {
            $bh = $businessHours->getBusinessHoursForDay($day);

            if ($bh) {
                $bh = $bh[0];
                if ($bh->open_status) {
                    $open_h = $bh ? $bh->opening_hours : 9;
                    $close_h = $bh ? $bh->closing_hours : 17;

                    $start_time = DateTime::createFromFormat($dateformat, $_POST['start']);
                    $start_time = $start_time->modify('+'.$open_h.' Hours');

                    $end_time = DateTime::createFromFormat($dateformat, $_POST['start']);
                    $end_time = $end_time->modify('+'.$close_h.' Hours');

                    $hours = ($end_time->getTimestamp() - $start_time->getTimestamp())/(60*60);
                    if ($hours < 0) {
                        $hours = 0 - $hours ;
                    }

                    //$hours = $bh->diffBusinessHours($start_time, $end_time);
                    $bhours[$day] = $hours;
                } else {
                    $bhours[$day] = 0;
                }
            }
        }
        //-----------------------------------
{'fraction_non_alphanumeric': 0.14832209034241312, 'fraction_numerical': 0.007514302792246606, 'mean_word_length': 2.086979441222984, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '337829', 'n_tokens_mistral': 3443, 'n_tokens_neox': 3223, 'n_words': 867}