function getBillingCategory()
 {
     if (is_null($this->billing_category)) {
         $this->billing_category = BillingCategories::findById($this->getBillingId());
     }
     // if
     return $this->billing_category;
 }
 function delete()
 {
     ajx_current("empty");
     $billingCategory = BillingCategories::findById(get_id());
     if (!$billingCategory instanceof BillingCategory) {
         flash_error(lang('billing category dnx'));
         return;
     }
     // if
     if (!$billingCategory->canDelete(logged_user())) {
         flash_error(lang('no access permissions'));
         return;
     }
     // if
     try {
         DB::beginWork();
         $billingCategory->delete();
         DB::commit();
         flash_success(lang('success delete billing category', $billingCategory->getName()));
         ajx_current("reload");
     } catch (Exception $e) {
         DB::rollback();
         flash_error($e->getMessage());
     }
     // try
 }
 function edit_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');
     $timeslot = Timeslots::findById(array_var($timeslot_data, 'id', 0));
     if (!$timeslot instanceof Timeslot) {
         flash_error(lang('timeslot dnx'));
         return;
     }
     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'];
         //Only admins can change timeslot user
         if (array_var($timeslot_data, 'contact_id', false) && !logged_user()->isAdministrator()) {
             $timeslot_data['contact_id'] = $timeslot->getContactId();
         }
         $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 = json_decode(array_var($_POST, 'members', ''));
         if ($member_ids && count($member_ids)) {
             ajx_add("time-panel", "reload");
         } else {
             foreach (active_context() as $dimension) {
                 $names[] = $dimension->getName();
             }
             flash_error(lang('select member to add timeslots', implode(", ", $names)));
             //flash_error(lang('must choose at least one member'));
             DB::rollback();
             return;
         }
         $object_controller = new ObjectController();
         $object_controller->add_to_members($timeslot, $member_ids);
         DB::commit();
         ajx_extra_data(array("timeslot" => $timeslot->getArrayInfo()));
     } catch (Exception $e) {
         DB::rollback();
         flash_error($e->getMessage());
     }
     // try
 }
 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());
     }
 }
Example #5
0
 function getBillingAmount($billing_category_id)
 {
     if (!is_numeric($billing_category_id)) {
         $billing_category_id = 0;
     }
     $wsBilling = WorkspaceBillings::findOne(array('conditions' => array('project_id = ? AND billing_id = ?', $this->getId(), $billing_category_id)));
     if ($wsBilling) {
         return $wsBilling->getValue();
     } else {
         $parent = $this->getParentWorkspace();
         if ($parent instanceof Project) {
             return $parent->getBillingAmount($billing_category_id);
         } else {
             $billing_category = BillingCategories::findById($billing_category_id);
             if ($billing_category instanceof BillingCategory) {
                 return $billing_category->getDefaultValue();
             } else {
                 return 0;
             }
         }
     }
 }
Example #6
0
 static function updateBillingValues()
 {
     $timeslot_rows = DB::executeAll("SELECT * FROM " . TABLE_PREFIX . "timeslots WHERE `end_time` > 0 AND billing_id = 0 AND is_fixed_billing = 0");
     $users = Contacts::getAllUsers();
     $usArray = array();
     foreach ($users as $u) {
         $usArray[$u->getId()] = $u;
     }
     $count = 0;
     $categories_cache = array();
     foreach ($timeslot_rows as $ts_row) {
         if ($ts_row['start_time'] == EMPTY_DATETIME) {
             $ts_row['minutes'] = 0;
         } else {
             $startTime = DateTimeValueLib::makeFromString($ts_row['start_time']);
             if ($ts_row['start_time'] == EMPTY_DATETIME) {
                 $endTime = $ts_row['is_paused'] ? DateTimeValueLib::makeFromString($ts_row['paused_on']) : DateTimeValueLib::now();
             } else {
                 $endTime = DateTimeValueLib::makeFromString($ts_row['end_time']);
             }
             $timeDiff = DateTimeValueLib::get_time_difference($startTime->getTimestamp(), $endTime->getTimestamp(), $ts_row['subtract']);
             $ts_row['minutes'] = $timeDiff['days'] * 1440 + $timeDiff['hours'] * 60 + $timeDiff['minutes'];
         }
         $user = $usArray[$ts_row['contact_id']];
         if ($user instanceof Contact) {
             $billing_category_id = $user->getDefaultBillingId();
             if ($billing_category_id > 0) {
                 $hours = $ts_row['minutes'] / 60;
                 $billing_category = array_var($categories_cache, $billing_category_id);
                 if (!$billing_category instanceof BillingCategory) {
                     $billing_category = BillingCategories::findById($billing_category_id);
                     $categories_cache[$billing_category_id] = $billing_category;
                 }
                 if ($billing_category instanceof BillingCategory) {
                     $hourly_billing = $billing_category->getDefaultValue();
                     DB::execute("UPDATE " . TABLE_PREFIX . "timeslots SET billing_id='{$billing_category_id}', hourly_billing='{$hourly_billing}', \r\n\t\t\t\t\t\t\tfixed_billing='" . round($hourly_billing * $hours, 2) . "', is_fixed_billing=0 \r\n\t\t\t\t\t\t\tWHERE object_id=" . $ts_row['object_id']);
                     $count++;
                 }
             }
         } else {
             DB::execute("UPDATE " . TABLE_PREFIX . "timeslots SET is_fixed_billing=1 WHERE object_id=" . $ts_row['object_id']);
         }
     }
     return $count;
 }
Example #7
0
 /**
  * This function sets the selected billing values for all timeslots which lack any type of billing values (value set to 0). 
  * This function is used when users start to use billing in the system.
  * 
  * @return unknown_type
  */
 static function updateBillingValues()
 {
     $timeslots = Timeslots::findAll(array('conditions' => '`end_time` > 0 AND billing_id = 0 AND is_fixed_billing = 0', 'join' => array('table' => Objects::instance()->getTableName(true), 'jt_field' => 'id', 'e_field' => 'rel_object_id')));
     $users = Contacts::getAllUsers();
     $usArray = array();
     foreach ($users as $u) {
         $usArray[$u->getId()] = $u;
     }
     $pbidCache = array();
     $count = 0;
     $categories_cache = array();
     foreach ($timeslots as $ts) {
         /* @var $ts Timeslot */
         $user = $usArray[$ts->getContactId()];
         if ($user instanceof Contact) {
             $billing_category_id = $user->getDefaultBillingId();
             if ($billing_category_id > 0) {
                 $hours = $ts->getMinutes() / 60;
                 $billing_category = array_var($categories_cache, $billing_category_id);
                 if (!$billing_category instanceof BillingCategory) {
                     $billing_category = BillingCategories::findById($billing_category_id);
                     $categories_cache[$billing_category_id] = $billing_category;
                 }
                 if ($billing_category instanceof BillingCategory) {
                     $hourly_billing = $billing_category->getDefaultValue();
                     $ts->setBillingId($billing_category_id);
                     $ts->setHourlyBilling($hourly_billing);
                     $ts->setFixedBilling(round($hourly_billing * $hours, 2));
                     $ts->setIsFixedBilling(false);
                     $ts->save();
                     $count++;
                 }
             }
         } else {
             $ts->setIsFixedBilling(true);
             $ts->save();
         }
     }
     return $count;
 }
 function close($description = null)
 {
     if ($this->isPaused()) {
         $this->setEndTime($this->getPausedOn());
     } else {
         $dt = DateTimeValueLib::now();
         $this->setEndTime($dt);
     }
     //Billing
     $user = Contacts::findById(array_var($timeslot_data, 'contact_id', logged_user()->getId()));
     $billing_category_id = $user->getDefaultBillingId();
     $bc = BillingCategories::findById($billing_category_id);
     if ($bc instanceof BillingCategory) {
         $this->setBillingId($billing_category_id);
         $hourly_billing = $bc->getDefaultValue();
         $this->setHourlyBilling($hourly_billing);
         $this->setFixedBilling(number_format($hourly_billing * $hours, 2));
         $this->setIsFixedBilling(false);
     }
     if ($description) {
         $this->setDescription($description);
     }
 }
 function edit_timeslot()
 {
     ajx_current("empty");
     $timeslot_data = array_var($_POST, 'timeslot');
     $timeslot = Timeslots::findById(array_var($timeslot_data, 'id', 0));
     if (!$timeslot instanceof Timeslot) {
         flash_error(lang('timeslot dnx'));
         return;
     }
     //context permissions or members
     $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;
         }
     }
     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'];
         //Only admins can change timeslot user
         if (!array_var($timeslot_data, 'contact_id') && !logged_user()->isAdministrator()) {
             $timeslot_data['contact_id'] = $timeslot->getContactId();
         }
         $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 = 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_EDIT);
         ajx_extra_data(array("timeslot" => $timeslot->getArrayInfo()));
     } catch (Exception $e) {
         DB::rollback();
         flash_error($e->getMessage());
     }
     // try
 }
Example #10
0
 function getDefaultBilling()
 {
     if (is_null($this->default_billing) && $this->getDefaultBillingId() != false) {
         $this->default_billing = BillingCategories::findById($this->getDefaultBillingId());
     }
     // if
     return $this->default_billing;
 }
 function close()
 {
     $this->setTemplate('add_timeslot');
     $timeslot = Timeslots::findById(get_id());
     if (!$timeslot instanceof Timeslot) {
         flash_error(lang('timeslot dnx'));
         ajx_current("empty");
         return;
     }
     $object = $timeslot->getRelObject();
     if (!$object instanceof ContentDataObject) {
         flash_error(lang('object dnx'));
         ajx_current("empty");
         return;
     }
     if (!$object->canAddTimeslot(logged_user())) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     $timeslot_data = array_var($_POST, 'timeslot');
     $timeslot->close();
     $timeslot->setFromAttributes($timeslot_data);
     //Billing
     $user = Contacts::findById(array_var($timeslot_data, 'contact_id', logged_user()->getId()));
     $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(number_format($hourly_billing * $hours, 2));
         $timeslot->setIsFixedBilling(false);
     }
     try {
         DB::beginWork();
         if (array_var($_GET, 'cancel') && array_var($_GET, 'cancel') == 'true') {
             $timeslot->delete();
         } else {
             $timeslot->save();
         }
         $object = $timeslot->getRelObject();
         if ($object instanceof ProjectTask) {
             $object->calculatePercentComplete();
         }
         DB::commit();
         if ($object instanceof ProjectTask) {
             $this->notifier_work_estimate($object);
         }
         ApplicationLogs::createLog($timeslot, ApplicationLogs::ACTION_CLOSE);
         if (array_var($_GET, 'cancel') && array_var($_GET, 'cancel') == 'true') {
             flash_success(lang('success cancel timeslot'));
         } else {
             flash_success(lang('success close timeslot'));
         }
         ajx_current("reload");
     } catch (Exception $e) {
         DB::rollback();
         ajx_current("empty");
         flash_error($e->getMessage());
     }
 }