public function toJsonEvent() { if (is_object($this->start_at)) { $start = $this->start_at; } else { $start = new \DateTime($this->start_at); } $end = clone $start; $end->modify(sprintf('+%d minutes', $this->duration)); $user = Auth::user(); $start2 = clone $start; $start2->modify('-2 days'); $canManage = $user->isSuperAdmin() || $this->booking->user_id == $user->id && $start2->format('Y-m-d') >= date('Y-m-d'); $className = sprintf('booking-%d', $this->ressource_id); $ofuscated_title = $this->booking->title; if ($this->booking->is_private && !$user->isSuperAdmin() && $this->booking->user_id != $user->id) { $ofuscated_title = 'Réservé'; $className .= sprintf(' booking-ofuscated-%d', $this->ressource_id); } else { $className .= ' booking'; } $backgroundColor = $this->ressource->booking_background_color; $borderColor = adjustBrightness($this->ressource->booking_background_color, -32); $textColor = adjustBrightness($this->ressource->booking_background_color, -128); if ($end->format('Y-m-d H:i:s') < date('Y-m-d H:i:s')) { $backgroundColor = hexColorToRgbWithTransparency($backgroundColor, '0.4'); $borderColor = hexColorToRgbWithTransparency($borderColor, '0.4'); $textColor = hexColorToRgbWithTransparency($textColor, '0.4'); $className .= ' booking-completed'; } if ($user->isSuperAdmin()) { $time = new PastTime(); $time->user_id = $this->booking->user_id; $time->ressource_id = $this->ressource_id; $time->date_past = $this->start_at; $time->time_start = $this->start_at; $time->time_end = strtotime(sprintf('+ %d minutes', $this->duration), is_object($this->start_at) ? $this->start_at->getTimestamp() : strtotime($this->start_at)); $is_accounted = PastTime::query()->where('user_id', $time->user_id)->where('ressource_id', $time->ressource_id)->where('date_past', $time->date_past)->where('time_start', $time->time_start)->where('time_end', $time->time_end)->count() > 0; } else { $is_accounted = false; } return array('title' => $ofuscated_title, 'start' => $start->format('c'), 'end' => $end->format('c'), 'booking_id' => $this->booking->id, 'id' => $this->id, 'user_id' => $this->booking->user_id, 'is_private' => (bool) $this->booking->is_private, 'is_accounted' => (bool) $is_accounted, 'is_open_to_registration' => (bool) $this->is_open_to_registration, 'description' => (string) $this->booking->content, 'canDelete' => (bool) $canManage, 'editable' => (bool) $canManage, 'backgroundColor' => $backgroundColor, 'borderColor' => $borderColor, 'textColor' => $textColor, 'ressource_id' => $this->ressource->id, 'className' => $className); }
public function invoice() { $items = PastTime::query()->whereIn('id', Input::get('items'))->where('invoice_id', 0)->orderBy('ressource_id', 'ASC')->orderBy('time_start', 'ASC')->get(); $lines = array(); $ressources = array(); $users = array(); $user = null; foreach ($items as $item) { $ressources[$item->ressource_id] = $item->ressource()->getResults(); $lines[$item->ressource_id][] = $item; $users[$item->user_id] = true; if (null == $user) { /** @var User $user */ $user = $item->user()->getResults(); } } if (count($users) > 1) { return Redirect::route('pasttime_list')->with('mError', 'Impossible de générer la facture pour plusieurs utilisateurs à la fois'); } if (count($users) == 0) { return Redirect::route('pasttime_list'); } $organisation = $user->organisations->first(); $invoice = new Invoice(); $invoice->user_id = $user->id; $invoice->created_at = new \DateTime(); $invoice->organisation_id = $organisation->id; $invoice->type = 'F'; $invoice->days = date('Ym'); $invoice->number = $invoice->next_invoice_number($invoice->type, $invoice->days); $invoice->address = $organisation->fulladdress; $invoice->date_invoice = new \DateTime(); $invoice->deadline = new \DateTime(date('Y-m-d', strtotime('+1 month'))); $invoice->save(); $vat = VatType::where('value', 20)->first(); $orderIndex = 0; foreach ($lines as $ressource_id => $line) { $ressource = $ressources[$ressource_id]; $invoice_line = new InvoiceItem(); $invoice_line->invoice_id = $invoice->id; $invoice_line->amount = 0; $invoice_line->order_index = $orderIndex++; if ($ressource_id == Ressource::TYPE_COWORKING) { $invoice_line->text = 'Coworking'; $sum_duration = 0; foreach ($line as $item) { $duration = ceil((strtotime($item->time_end) - strtotime($item->time_start)) / 3600 / self::COWORKING_HALF_DAY_MAX_DURATION); $sum_duration += $duration; $invoice_line->text .= sprintf("\n - %s de %s à %s (%s demi journée%s)", date('d/m/Y', strtotime($item->time_start)), date('H:i', strtotime($item->time_start)), date('H:i', strtotime($item->time_end)), $duration, $duration > 1 ? 's' : ''); $invoice_line->amount += $duration * (self::COWORKING_HALF_DAY_PRICING / 1.2); $item->invoice_id = $invoice->id; $item->save(); } $invoice_line->text .= sprintf("\nTotal : %s demi journée%s", $sum_duration, $sum_duration > 1 ? 's' : ''); } else { $invoice_line->text = sprintf('Location d\'espace de réunion - %s', $ressource->name); foreach ($line as $item) { $invoice_line->text .= sprintf("\n - %s de %s à %s", date('d/m/Y', strtotime($item->time_start)), date('H:i', strtotime($item->time_start)), date('H:i', strtotime($item->time_end))); $invoice_line->amount += min(7, (strtotime($item->time_end) - strtotime($item->time_start)) / 3600) * $ressource->amount; $item->invoice_id = $invoice->id; $item->save(); } } $invoice_line->vat_types_id = $vat->id; $invoice_line->ressource_id = $item->ressource_id; $invoice_line->save(); } return Redirect::route('invoice_modify', $invoice->id)->with('mSuccess', 'La facture a bien été générée'); }
public function logTimeAjax($id) { $booking_item = BookingItem::find($id); if (!$booking_item) { return Response::json(array('status' => 'KO', 'message' => 'La réservation est inconnue')); } $time = new PastTime(); $time->user_id = $booking_item->booking->user_id; $time->ressource_id = $booking_item->ressource_id; $time->date_past = date('Y-m-d', strtotime($booking_item->start_at)); $time->time_start = $booking_item->start_at; $time->time_end = date('Y-m-d H:i:s', strtotime($booking_item->start_at) + $booking_item->duration * 60); $existing = PastTime::query()->where('user_id', $time->user_id)->where('ressource_id', $time->ressource_id)->where('date_past', $time->date_past)->where('time_start', $time->time_start)->where('time_end', $time->time_end)->count() > 0; if ($existing) { return Response::json(array('status' => 'KO', 'message' => sprintf('Un enregistrement similaire à %s est déjà présent', $booking_item->booking->title))); } $time->save(); return Response::json(array('status' => 'OK', 'message' => sprintf('La réunion %s a été comptabilisée', $booking_item->booking->title), 'event' => $booking_item->toJsonEvent())); }