function add_timespan() { $object_id = get_id('object_id'); $object = Objects::findObject($object_id); if (!$object instanceof ContentDataObject || !$object->canAddTimeslot(logged_user())) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } $timeslot_data = array_var($_POST, 'timeslot'); $hours = array_var($timeslot_data, 'hours'); $minutes = array_var($timeslot_data, 'minutes'); if (strpos($hours, ',') && !strpos($hours, '.')) { $hours = str_replace(',', '.', $hours); } if ($minutes) { $min = str_replace('.', '', $minutes / 6); $hours = $hours + ("0." . $min); } $timeslot = new Timeslot(); $dt = DateTimeValueLib::now(); $dt2 = DateTimeValueLib::now(); $timeslot->setEndTime($dt); $dt2 = $dt2->add('h', -$hours); $timeslot->setStartTime($dt2); $timeslot->setDescription(array_var($timeslot_data, 'description')); $timeslot->setContactId(array_var($timeslot_data, 'contact_id', logged_user()->getId())); $timeslot->setRelObjectId($object_id); $billing_category_id = logged_user()->getDefaultBillingId(); $bc = BillingCategories::findById($billing_category_id); if ($bc instanceof BillingCategory) { $timeslot->setBillingId($billing_category_id); $hourly_billing = $bc->getDefaultValue(); $timeslot->setHourlyBilling($hourly_billing); $timeslot->setFixedBilling($hourly_billing * $hoursToAdd); $timeslot->setIsFixedBilling(false); } try { DB::beginWork(); $timeslot->save(); /* dont add timeslots to members, members are taken from the related object $object_controller = new ObjectController(); $object_controller->add_to_members($timeslot, $object->getMemberIds()); */ ApplicationLogs::createLog($timeslot, ApplicationLogs::ACTION_OPEN); $task = ProjectTasks::findById($object_id); if ($task->getTimeEstimate() > 0) { $timeslots = $task->getTimeslots(); if (count($timeslots) == 1) { $task->setPercentCompleted(0); } $timeslot_percent = round($hours * 100 / ($task->getTimeEstimate() / 60)); $total_percentComplete = $timeslot_percent + $task->getPercentCompleted(); if ($total_percentComplete < 0) { $total_percentComplete = 0; } $task->setPercentCompleted($total_percentComplete); $task->save(); $this->notifier_work_estimate($task); } DB::commit(); flash_success(lang('success create timeslot')); ajx_current("reload"); } catch (Exception $e) { DB::rollback(); ajx_current("empty"); flash_error($e->getMessage()); } }
function add_project_timeslot() { if (!can_manage_time(logged_user(), true)) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } ajx_current("empty"); $timeslot_data = array_var($_POST, 'timeslot'); try { $hoursToAdd = array_var($timeslot_data, 'hours', 0); if (strpos($hoursToAdd, ',') && !strpos($hoursToAdd, '.')) { $hoursToAdd = str_replace(',', '.', $hoursToAdd); } if (strpos($hoursToAdd, ':') && !strpos($hoursToAdd, '.')) { $pos = strpos($hoursToAdd, ':') + 1; $len = strlen($hoursToAdd) - $pos; $minutesToAdd = substr($hoursToAdd, $pos, $len); if (!strlen($minutesToAdd) <= 2 || !strlen($minutesToAdd) > 0) { $minutesToAdd = substr($minutesToAdd, 0, 2); } $mins = $minutesToAdd / 60; $hours = substr($hoursToAdd, 0, $pos - 1); $hoursToAdd = $hours + $mins; } if ($hoursToAdd <= 0) { flash_error(lang('time has to be greater than 0')); return; } $startTime = getDateValue(array_var($timeslot_data, 'date')); $startTime = $startTime->add('h', 8 - logged_user()->getTimezone()); $endTime = getDateValue(array_var($timeslot_data, 'date')); $endTime = $endTime->add('h', 8 - logged_user()->getTimezone() + $hoursToAdd); $timeslot_data['start_time'] = $startTime; $timeslot_data['end_time'] = $endTime; $timeslot_data['object_id'] = array_var($timeslot_data, 'project_id'); $timeslot_data['object_manager'] = 'Projects'; $timeslot = new Timeslot(); //Only admins can change timeslot user if (!array_var($timeslot_data, 'user_id', false) || !logged_user()->isAdministrator()) { $timeslot_data['user_id'] = logged_user()->getId(); } $timeslot->setFromAttributes($timeslot_data); /* Billing */ $user = Users::findById($timeslot_data['user_id']); $billing_category_id = $user->getDefaultBillingId(); $project = Projects::findById(array_var($timeslot_data, 'project_id')); $timeslot->setBillingId($billing_category_id); $hourly_billing = $project->getBillingAmount($billing_category_id); $timeslot->setHourlyBilling($hourly_billing); $timeslot->setFixedBilling($hourly_billing * $hoursToAdd); $timeslot->setIsFixedBilling(false); DB::beginWork(); $timeslot->save(); DB::commit(); $show_billing = can_manage_security(logged_user()) && logged_user()->isAdministrator(); ajx_extra_data(array("timeslot" => $timeslot->getArrayInfo($show_billing))); } catch (Exception $e) { DB::rollback(); flash_error($e->getMessage()); } // try }
function add_timeslot() { $object_id = array_var($_REQUEST, "object_id", false); ajx_current("empty"); $timeslot_data = array_var($_POST, 'timeslot'); if ($object_id) { $object = Objects::findObject($object_id); if (!$object instanceof ContentDataObject || !$object->canAddTimeslot(logged_user())) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } $member_ids = $object->getMemberIds(); } else { $member_ids = json_decode(array_var($_POST, 'members', array())); // clean member_ids $tmp_mids = array(); foreach ($member_ids as $mid) { if (!is_null($mid) && trim($mid) != "") { $tmp_mids[] = $mid; } } $member_ids = $tmp_mids; if (empty($member_ids)) { if (!can_add(logged_user(), active_context(), Timeslots::instance()->getObjectTypeId())) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } } else { if (count($member_ids) > 0) { $enteredMembers = Members::findAll(array('conditions' => 'id IN (' . implode(",", $member_ids) . ')')); } else { $enteredMembers = array(); } if (!can_add(logged_user(), $enteredMembers, Timeslots::instance()->getObjectTypeId())) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } } $object_id = 0; } try { $hoursToAdd = array_var($timeslot_data, 'hours', 0); $minutes = array_var($timeslot_data, 'minutes', 0); if (strpos($hoursToAdd, ',') && !strpos($hoursToAdd, '.')) { $hoursToAdd = str_replace(',', '.', $hoursToAdd); } if (strpos($hoursToAdd, ':') && !strpos($hoursToAdd, '.')) { $pos = strpos($hoursToAdd, ':') + 1; $len = strlen($hoursToAdd) - $pos; $minutesToAdd = substr($hoursToAdd, $pos, $len); if (!strlen($minutesToAdd) <= 2 || !strlen($minutesToAdd) > 0) { $minutesToAdd = substr($minutesToAdd, 0, 2); } $mins = $minutesToAdd / 60; $hours = substr($hoursToAdd, 0, $pos - 1); $hoursToAdd = $hours + $mins; } if ($minutes) { $min = str_replace('.', '', $minutes / 6); $hoursToAdd = $hoursToAdd + ("0." . $min); } if ($hoursToAdd <= 0) { flash_error(lang('time has to be greater than 0')); return; } $startTime = getDateValue(array_var($timeslot_data, 'date')); $startTime = $startTime->add('h', 8 - logged_user()->getTimezone()); $endTime = getDateValue(array_var($timeslot_data, 'date')); $endTime = $endTime->add('h', 8 - logged_user()->getTimezone() + $hoursToAdd); //use current time if (array_var($_REQUEST, "use_current_time", false)) { $currentStartTime = DateTimeValueLib::now(); $currentEndTime = DateTimeValueLib::now(); $currentStartTime = $currentStartTime->add('h', -$hoursToAdd); $startTime->setHour($currentStartTime->getHour()); $startTime->setMinute($currentStartTime->getMinute()); $endTime->setHour($currentEndTime->getHour()); $endTime->setMinute($currentEndTime->getMinute()); } $timeslot_data['start_time'] = $startTime; $timeslot_data['end_time'] = $endTime; $timeslot_data['description'] = html_to_text($timeslot_data['description']); $timeslot_data['name'] = $timeslot_data['description']; $timeslot_data['rel_object_id'] = $object_id; //array_var($timeslot_data,'project_id'); $timeslot = new Timeslot(); //Only admins can change timeslot user if (!array_var($timeslot_data, 'contact_id', false) || !SystemPermissions::userHasSystemPermission(logged_user(), 'can_manage_time')) { $timeslot_data['contact_id'] = logged_user()->getId(); } $timeslot->setFromAttributes($timeslot_data); $user = Contacts::findById($timeslot_data['contact_id']); $billing_category_id = $user->getDefaultBillingId(); $bc = BillingCategories::findById($billing_category_id); if ($bc instanceof BillingCategory) { $timeslot->setBillingId($billing_category_id); $hourly_billing = $bc->getDefaultValue(); $timeslot->setHourlyBilling($hourly_billing); $timeslot->setFixedBilling($hourly_billing * $hoursToAdd); $timeslot->setIsFixedBilling(false); } DB::beginWork(); $timeslot->save(); $task = ProjectTasks::findById($object_id); if ($task instanceof ProjectTask) { $task->calculatePercentComplete(); } if (!isset($member_ids) || !is_array($member_ids) || count($member_ids) == 0) { $member_ids = json_decode(array_var($_POST, 'members')); } $object_controller = new ObjectController(); $object_controller->add_to_members($timeslot, $member_ids); DB::commit(); ApplicationLogs::createLog($timeslot, ApplicationLogs::ACTION_ADD); $show_billing = can_manage_billing(logged_user()); ajx_extra_data(array("timeslot" => $timeslot->getArrayInfo($show_billing), "real_obj_id" => $timeslot->getRelObjectId())); } catch (Exception $e) { DB::rollback(); flash_error($e->getMessage()); } // try }
function add_timeslot() { if (!can_add(logged_user(), active_context(), Timeslots::instance()->getObjectTypeId())) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } ajx_current("empty"); $timeslot_data = array_var($_POST, 'timeslot'); try { $hoursToAdd = array_var($timeslot_data, 'hours', 0); $minutes = array_var($timeslot_data, 'minutes', 0); if (strpos($hoursToAdd, ',') && !strpos($hoursToAdd, '.')) { $hoursToAdd = str_replace(',', '.', $hoursToAdd); } if (strpos($hoursToAdd, ':') && !strpos($hoursToAdd, '.')) { $pos = strpos($hoursToAdd, ':') + 1; $len = strlen($hoursToAdd) - $pos; $minutesToAdd = substr($hoursToAdd, $pos, $len); if (!strlen($minutesToAdd) <= 2 || !strlen($minutesToAdd) > 0) { $minutesToAdd = substr($minutesToAdd, 0, 2); } $mins = $minutesToAdd / 60; $hours = substr($hoursToAdd, 0, $pos - 1); $hoursToAdd = $hours + $mins; } if ($minutes) { $min = str_replace('.', '', $minutes / 6); $hoursToAdd = $hoursToAdd + ("0." . $min); } if ($hoursToAdd <= 0) { flash_error(lang('time has to be greater than 0')); return; } $startTime = getDateValue(array_var($timeslot_data, 'date')); $startTime = $startTime->add('h', 8 - logged_user()->getTimezone()); $endTime = getDateValue(array_var($timeslot_data, 'date')); $endTime = $endTime->add('h', 8 - logged_user()->getTimezone() + $hoursToAdd); $timeslot_data['start_time'] = $startTime; $timeslot_data['end_time'] = $endTime; $timeslot_data['name'] = $timeslot_data['description']; $timeslot_data['object_id'] = 0; //array_var($timeslot_data,'project_id'); $timeslot = new Timeslot(); //Only admins can change timeslot user if (!array_var($timeslot_data, 'contact_id', false) || !logged_user()->isAdministrator()) { $timeslot_data['contact_id'] = logged_user()->getId(); } $timeslot->setFromAttributes($timeslot_data); $user = Contacts::findById($timeslot_data['contact_id']); $billing_category_id = $user->getDefaultBillingId(); $bc = BillingCategories::findById($billing_category_id); if ($bc instanceof BillingCategory) { $timeslot->setBillingId($billing_category_id); $hourly_billing = $bc->getDefaultValue(); $timeslot->setHourlyBilling($hourly_billing); $timeslot->setFixedBilling($hourly_billing * $hoursToAdd); $timeslot->setIsFixedBilling(false); } DB::beginWork(); $timeslot->save(); $member_ids = array(); $context = active_context(); foreach ($context as $selection) { if ($selection instanceof Member) { $member_ids[] = $selection->getId(); } } $object_controller = new ObjectController(); $object_controller->add_to_members($timeslot, $member_ids); DB::commit(); $show_billing = can_manage_billing(logged_user()); ajx_extra_data(array("timeslot" => $timeslot->getArrayInfo($show_billing))); } catch (Exception $e) { DB::rollback(); flash_error($e->getMessage()); } // try }
function add_timespan() { if (!can_manage_time(logged_user(), true)) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } $object_id = get_id('object_id'); $object_manager = array_var($_GET, 'object_manager'); if (!is_valid_function_name($object_manager)) { flash_error(lang('invalid request')); ajx_current("empty"); return; } // if $object = get_object_by_manager_and_id($object_id, $object_manager); if (!$object instanceof ProjectDataObject || !$object->canAddTimeslot(logged_user())) { flash_error(lang('no access permissions')); ajx_current("empty"); return; } // if $timeslot_data = array_var($_POST, 'timeslot'); $hours = array_var($timeslot_data, 'time'); if (strpos($hours, ',') && !strpos($hours, '.')) { $hours = str_replace(',', '.', $hours); } $timeslot = new Timeslot(); $dt = DateTimeValueLib::now(); $dt2 = DateTimeValueLib::now(); $timeslot->setEndTime($dt); $dt2 = $dt2->add('h', -$hours); $timeslot->setStartTime($dt2); $timeslot->setDescription(array_var($timeslot_data, 'description')); $timeslot->setUserId(logged_user()->getId()); $timeslot->setObjectManager($object_manager); $timeslot->setObjectId($object_id); /* Billing */ $billing_category_id = logged_user()->getDefaultBillingId(); $project = $object->getProject(); $timeslot->setBillingId($billing_category_id); $hourly_billing = $project->getBillingAmount($billing_category_id); $timeslot->setHourlyBilling($hourly_billing); $timeslot->setFixedBilling($hourly_billing * $hours); $timeslot->setIsFixedBilling(false); try { DB::beginWork(); $timeslot->save(); ApplicationLogs::createLog($timeslot, $timeslot->getWorkspaces(), ApplicationLogs::ACTION_OPEN); DB::commit(); flash_success(lang('success create timeslot')); ajx_current("reload"); } catch (Exception $e) { DB::rollback(); ajx_current("empty"); flash_error($e->getMessage()); } // try }