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_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 }