コード例 #1
0
 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
 }