319 lines
11 KiB
PHP
319 lines
11 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
use stdClass;
|
|
use App\Models\Coupon;
|
|
use App\Models\Booking;
|
|
use App\Libraries\CreatePDF;
|
|
use App\Models\BookingStorno;
|
|
use App\Libraries\MyPDFMerger;
|
|
use App\Models\BookingDocument;
|
|
use App\Libraries\CreatePDFCoupon;
|
|
use Carbon;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class BookingPDFRepository extends BaseRepository
|
|
{
|
|
|
|
protected $prepath;
|
|
|
|
public function __construct(Booking $model)
|
|
{
|
|
$this->model = $model;
|
|
$this->prepath = Storage::disk('public')->path('');
|
|
}
|
|
|
|
public function update($data)
|
|
{
|
|
return $this->model;
|
|
}
|
|
|
|
public function createPDF($id, $data)
|
|
{
|
|
|
|
$this->model = Booking::findOrFail($id);
|
|
|
|
switch ($data['action']) {
|
|
case 'createPDF_Registration':
|
|
return $this->createPDF_Registration();
|
|
break;
|
|
case 'createPDF_Confirmation':
|
|
return $this->createPDF_Confirmation();
|
|
break;
|
|
case 'createPDF_Coupon':
|
|
return $this->createPDF_Coupon($data);
|
|
break;
|
|
case 'createPDF_Voucher':
|
|
$this->createPDF_Voucher(false); //client
|
|
return $this->createPDF_Voucher(true); //acency
|
|
|
|
break;
|
|
case 'createPDF_VoucherAgency':
|
|
return $this->createPDF_Voucher(true);
|
|
break;
|
|
case 'createPDF_Storno':
|
|
return $this->createPDF_Storno($data);
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function createPDF_Registration()
|
|
{
|
|
$document = new stdClass();
|
|
$document->name = 'registration';
|
|
$document->number = $this->model->inquiry_id;
|
|
$document->title = 'BUCHUNGSAUFTRAG';
|
|
$document->voucher = null;
|
|
$document->date = now();
|
|
$document->total = $this->model->getPriceRaw();
|
|
$dir = $this->getDirPath('pdf', 'booking', $document->date->format('Y'));
|
|
$filename = "Buchnungsauftrag-" . $this->model->inquiry_id . ".pdf";
|
|
$pdf_file = new CreatePDF('pdf.booking_registration');
|
|
$data = [
|
|
'booking' => $this->model,
|
|
'document' => $document,
|
|
];
|
|
$pdf_file->create($data, $filename, 'save', $this->prepath . $dir);
|
|
$booking_document = $this->savePDF($dir, $filename, 'sterntours-template', 'registration', $document);
|
|
|
|
return $booking_document;
|
|
}
|
|
|
|
public function createPDF_Confirmation()
|
|
{
|
|
$document = new stdClass();
|
|
$document->name = 'confirmation';
|
|
$document->number = $this->model->inquiry_id;
|
|
$document->title = 'REISEBESTÄTIGUNG';
|
|
$document->voucher = null;
|
|
$document->date = now();
|
|
|
|
$document->total = $this->model->getPriceRaw();
|
|
$document->deposit = $this->model->getConfirmationDeposit();
|
|
$document->final_payment = $this->model->getConfirmationFinalPayment();
|
|
|
|
if ($this->model->isDepositPossible()) {
|
|
$document->deposit_payment_date = date('Y-m-d');
|
|
$maxDepositIntervalDays = config('booking.max_deposit_interval_days');
|
|
$_start_date = clone $this->model->start_date;
|
|
$_start_date->modify('-' . $maxDepositIntervalDays . ' days');
|
|
$document->final_payment_date = $_start_date->format('Y-m-d');
|
|
} else {
|
|
$document->final_payment_date = date('Y-m-d');
|
|
}
|
|
$dir = $this->getDirPath('pdf', 'booking', $document->date->format('Y'));
|
|
$filename = "Reisebestätigung-" . $this->model->inquiry_id . ".pdf";
|
|
|
|
$pdf_file = new CreatePDF('pdf.booking_confirmation');
|
|
$data = [
|
|
'booking' => $this->model,
|
|
'document' => $document,
|
|
];
|
|
$pdf_file->create($data, $filename, 'save', $this->prepath . $dir);
|
|
$booking_document = $this->savePDF($dir, $filename, 'sterntours-template', 'confirmation', $document);
|
|
|
|
return $booking_document;
|
|
}
|
|
|
|
public function createPDF_Coupon($data)
|
|
{
|
|
|
|
$document = new stdClass();
|
|
$document->name = 'coupon';
|
|
$document->title = 'COUPON';
|
|
$document->voucher = null;
|
|
$document->date = now();
|
|
|
|
$coupon = new Coupon();
|
|
$coupon->booking_id = $this->model->id;
|
|
$coupon->customer_id = $this->model->customer->id;
|
|
$coupon->value = $data['value'];
|
|
$coupon->issue_date = $data['issue_date'];
|
|
$coupon->valid_date = $data['valid_date'];
|
|
$coupon->is_redeemed = false;
|
|
$coupon->save();
|
|
|
|
$document->number = $coupon->number;
|
|
$document->value = $coupon->value;
|
|
$document->issue_date = $coupon->issue_date;
|
|
$document->valid_date = $coupon->valid_date;
|
|
$document->coupon_id = $coupon->id;
|
|
|
|
|
|
$dir = $this->getDirPath('pdf', 'coupon', $document->date->format('Y'));
|
|
$filename = "Gutschein-" . $coupon->number . ".pdf";
|
|
|
|
$pdf_file = new CreatePDFCoupon('pdf.booking_coupon');
|
|
$data = [
|
|
'booking' => $this->model,
|
|
'document' => $document,
|
|
'coupon' => $coupon,
|
|
];
|
|
$pdf_file->create($data, $filename, 'save', $this->prepath . $dir);
|
|
$booking_document = $this->savePDF($dir, $filename, 'sterntours-coupon', 'coupon', $document);
|
|
return $booking_document;
|
|
}
|
|
|
|
public function createPDF_Voucher($agency = false)
|
|
{
|
|
$document = new stdClass();
|
|
$document->name = 'voucher';
|
|
$document->number = $this->model->inquiry_id;
|
|
$document->name = 'voucher';
|
|
$document->title = $agency ? 'VOUCHER Agentur' : 'VOUCHER';
|
|
$document->voucher = $agency ? 'agency' : 'client';
|
|
$document->date = now();
|
|
|
|
$dir = $this->getDirPath('pdf', 'voucher', $document->date->format('Y'));
|
|
$filename = ($agency ? 'VoucherAgentur' : 'Voucher') . "-" . $this->model->inquiry_id . ".pdf";
|
|
|
|
$pdf_file = new CreatePDF('pdf.booking_voucher');
|
|
$data = [
|
|
'booking' => $this->model,
|
|
'document' => $document,
|
|
];
|
|
$pdf_file->create($data, $filename, 'save', $this->prepath . $dir);
|
|
$identifier = $agency ? 'voucher_agency' : 'voucher';
|
|
$booking_document = $this->savePDF($dir, $filename, 'sterntours-template', $identifier, $document);
|
|
|
|
return $booking_document;
|
|
}
|
|
|
|
public function createPDF_Storno($data)
|
|
{
|
|
//Storno Values
|
|
$storno_status_id = (int) $data['storno_status_id'];
|
|
$storno_level = (float) $data['storno_level'];
|
|
$storno_level_number = \Util::_clean_float($data['storno_level_number']);
|
|
$storno_total_price = \Util::_clean_float($data['storno_total_price']);
|
|
$price = $this->model->getPriceRaw();
|
|
if ($storno_level_number > 0 && $storno_level_number < 100) {
|
|
$storno_level = $storno_level_number;
|
|
}
|
|
//calculate price canceled
|
|
if ($storno_total_price > 0) {
|
|
$price_canceled = $storno_total_price;
|
|
$storno_level = ((100 / $price) * $storno_total_price);
|
|
} else {
|
|
$price_canceled = round($price / 100 * $storno_level, 2);
|
|
}
|
|
|
|
|
|
//init identifier
|
|
$identifier = 'storno';
|
|
|
|
//create / update BookingStorno
|
|
$fill = [
|
|
'booking_id' => $this->model->id,
|
|
'total' => $price,
|
|
'storno' => $price_canceled,
|
|
'storno_date' => $data['storno_date'],
|
|
'storno_print' => $data['storno_print'],
|
|
'binary_data' => NULL,
|
|
];
|
|
if ($this->model->hasDocument($identifier)) {
|
|
$booking_document = $this->model->getDocument($identifier);
|
|
$booking_storno = $booking_document->booking_storno;
|
|
$booking_storno->update($fill);
|
|
} else {
|
|
$booking_storno = BookingStorno::create($fill);
|
|
}
|
|
|
|
//init document
|
|
$document = new stdClass();
|
|
$document->name = $identifier;
|
|
$document->number = $this->model->inquiry_id;
|
|
$document->title = 'STORNOBESTÄTIGUNG';
|
|
$document->voucher = null;
|
|
$document->date = Carbon::parse($data['storno_print']);
|
|
$document->total = $price;
|
|
$document->storno = $price_canceled;
|
|
$document->storno_date = $data['storno_date'];
|
|
$document->storno_print = $data['storno_print'];
|
|
$document->storno_status_id = $storno_status_id;
|
|
$document->booking_storno_id = $booking_storno->id;
|
|
$document->storno_level = $storno_level;
|
|
|
|
|
|
|
|
//update Booking // Lead Status
|
|
if ($storno_status_id) {
|
|
$this->model->lead->status_id = $storno_status_id;
|
|
$this->model->lead->save();
|
|
}
|
|
|
|
$this->model->canceled = $storno_level;
|
|
$this->model->price_canceled = $price_canceled; //must pay
|
|
$this->model->price_total = $price_canceled; //is the same as Canceled( ...
|
|
$this->model->price_balance = round($price - $price_canceled, 2); //is the Rest - must not pay
|
|
$this->model->save();
|
|
|
|
|
|
|
|
$dir = $this->getDirPath('pdf', 'storno', $document->date->format('Y'));
|
|
$filename = "Reisestornierung -" . $this->model->inquiry_id . ".pdf";
|
|
|
|
$pdf_file = new CreatePDF('pdf.booking_storno');
|
|
$data = [
|
|
'booking' => $this->model,
|
|
'document' => $document,
|
|
];
|
|
$pdf_file->create($data, $filename, 'save', $this->prepath . $dir);
|
|
$this->savePDF($dir, $filename, 'sterntours-template', $identifier, $document);
|
|
}
|
|
|
|
|
|
|
|
//get Invoice Name / Paths / ...
|
|
private function getDirPath($file, $dir, $year)
|
|
{
|
|
|
|
$path = $file . '/' . $dir . '/' . $year . '/';
|
|
if (!Storage::disk('public')->exists($path)) {
|
|
Storage::disk('public')->makeDirectory($path); //creates directory
|
|
}
|
|
return $path;
|
|
}
|
|
|
|
private function savePDF($dir, $filename, $template, $identifier, $document = null, $status = 0)
|
|
{
|
|
|
|
$pdfMerger = new MyPDFMerger();
|
|
$pdfMerger->addPDF($this->prepath . $dir . $filename);
|
|
$file = $pdfMerger->myMerge('string', $filename, $template);
|
|
Storage::disk('public')->put($dir . $filename, $file);
|
|
|
|
$fill = [
|
|
'booking_id' => $this->model->id,
|
|
'customer_id' => $this->model->customer_id,
|
|
// booking_documents.lead_id ist ein Shadow-Feld von booking.inquiry_id;
|
|
// die Spalte selbst wird von Phase 2 nicht umbenannt.
|
|
'lead_id' => $this->model->inquiry_id,
|
|
'identifier' => $identifier,
|
|
'filename' => $filename,
|
|
'dir' => $dir,
|
|
'original_name' => $filename,
|
|
'ext' => 'pdf',
|
|
'mine' => 'application/pdf',
|
|
'size' => Storage::disk('public')->size($dir . $filename),
|
|
'date' => now(),
|
|
'data' => $document,
|
|
'status' => $status,
|
|
'booking_storno_id' => isset($document->booking_storno_id) ? $document->booking_storno_id : null,
|
|
'coupon_id' => isset($document->coupon_id) ? $document->coupon_id : null,
|
|
];
|
|
if ($this->model->hasDocument($identifier)) {
|
|
$booking_document = $this->model->getDocument($identifier);
|
|
$booking_document->update($fill);
|
|
} else {
|
|
$booking_document = BookingDocument::create($fill);
|
|
}
|
|
return $booking_document;
|
|
|
|
//return $pdfMerger->myMerge('browser', $filename, $template);
|
|
|
|
|
|
}
|
|
}
|