407 lines
16 KiB
PHP
407 lines
16 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
use App\Libraries\CreditDetailsPDF;
|
|
use App\Libraries\MyPDFMerger;
|
|
use App\Models\UserCredit;
|
|
use App\Models\UserCreditItem;
|
|
use App\Models\UserSalesVolume;
|
|
use App\Services\BusinessPlan\TreeCalcBot;
|
|
use App\Services\Credit;
|
|
use App\User;
|
|
use PDF;
|
|
use Response;
|
|
use Storage;
|
|
|
|
class CreditRepository extends BaseRepository
|
|
{
|
|
private $user_credit;
|
|
|
|
public function __construct(User $model)
|
|
{
|
|
$this->model = $model;
|
|
}
|
|
|
|
public function create($request = [])
|
|
{
|
|
// need invoice $data
|
|
$number = Credit::getCreditNumber();
|
|
$credit_date = isset($request['credit_date']) ? $request['credit_date'] : \Carbon::now()->format('d.m.Y');
|
|
$credit_send_mail = isset($request['credit_send_mail']) ? true : false;
|
|
$credit_number = Credit::createCreditNumber($number, $credit_date);
|
|
|
|
$this->user_credit = new UserCredit;
|
|
$user_credit_items = $this->makeUserCredit();
|
|
if (! count($user_credit_items)) {
|
|
return false;
|
|
}
|
|
$data = [
|
|
'user' => $this->model,
|
|
'credit_date' => $credit_date,
|
|
'credit_number' => $credit_number,
|
|
'user_credits' => $this->user_credit,
|
|
'user_credit_items' => $user_credit_items,
|
|
];
|
|
|
|
$dir = Credit::getCreditStorageDir($credit_date);
|
|
if (! Storage::disk('public')->exists($dir)) {
|
|
Storage::disk('public')->makeDirectory($dir); // creates directory
|
|
}
|
|
$path = Storage::disk('public')->path('');
|
|
|
|
// Benutzersprache ermitteln
|
|
$userLocale = $this->model->account ? $this->model->account->getLocale() : 'de';
|
|
$originalLocale = \App::getLocale();
|
|
|
|
// Deutsches Original-Dateiname (wird in DB gespeichert)
|
|
$filename = Credit::makeCreditFilename($credit_number);
|
|
|
|
// 1. IMMER deutsches Original erstellen (Finanzamt-Anforderung)
|
|
\App::setLocale('de');
|
|
$this->createCreditPDF($data, $path, $dir, $filename, 'de', false);
|
|
|
|
// 2. Wenn Benutzersprache != DE, Kopie in Benutzersprache erstellen
|
|
if ($userLocale && $userLocale !== 'de') {
|
|
\App::setLocale($userLocale);
|
|
$localizedFilename = Credit::makeCreditFilenameLocale($credit_number, $userLocale);
|
|
$this->createCreditPDF($data, $path, $dir, $localizedFilename, $userLocale, true);
|
|
}
|
|
|
|
// Locale zurücksetzen
|
|
\App::setLocale($originalLocale);
|
|
|
|
$this->user_credit->user_id = $this->model->id;
|
|
$this->user_credit->year = \Carbon::parse($credit_date)->format('Y');
|
|
$this->user_credit->month = \Carbon::parse($credit_date)->format('n');
|
|
$this->user_credit->date = $credit_date;
|
|
$this->user_credit->filename = $filename;
|
|
$this->user_credit->dir = $dir;
|
|
$this->user_credit->disk = 'public';
|
|
$this->user_credit->number = $number;
|
|
$this->user_credit->full_number = $credit_number;
|
|
$this->user_credit->save();
|
|
|
|
if ($credit_send_mail) {
|
|
Credit::sendCreditMail($this->user_credit);
|
|
}
|
|
$this->finishUserCredit($this->user_credit->id, $user_credit_items);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Erstellt eine Gutschrift-PDF-Datei.
|
|
*
|
|
* @param string $locale Sprachcode für das Template (de, en, es, fr)
|
|
* @param bool $is_copy Ob es sich um eine Kopie handelt (nicht das Original)
|
|
*/
|
|
private function createCreditPDF(array $data, string $path, string $dir, string $filename, string $locale = 'de', bool $is_copy = false)
|
|
{
|
|
// Kopie-Flag an Template übergeben
|
|
$data['is_copy'] = $is_copy;
|
|
|
|
$pdf = PDF::loadView('pdf.credit', $data);
|
|
$pdf->setPaper('A4', 'portrait');
|
|
$pdf->save($path.$dir.$filename);
|
|
|
|
// Template basierend auf Locale
|
|
$template = $this->getTemplateForLocale($locale);
|
|
|
|
$pdfMerger = new MyPDFMerger;
|
|
$pdfMerger->addPDF($path.$dir.$filename);
|
|
$file = $pdfMerger->myMerge('string', $filename, $template);
|
|
Storage::disk('public')->put($dir.$filename, $file);
|
|
}
|
|
|
|
/**
|
|
* Gibt das PDF-Template für die angegebene Locale zurück.
|
|
* Verfügbare Templates werden aus config/localization.php geladen.
|
|
*/
|
|
private function getTemplateForLocale(string $locale): string
|
|
{
|
|
$availableTemplates = config('localization.availableTemplates', ['de']);
|
|
|
|
if (in_array($locale, $availableTemplates)) {
|
|
return 'template_invoice_'.$locale;
|
|
}
|
|
|
|
return 'template_invoice_de';
|
|
}
|
|
|
|
private function finishUserCredit($user_credit_id, $user_credit_items)
|
|
{
|
|
// next credits
|
|
Credit::makeNextCreditNumber();
|
|
// mark as payed
|
|
// $UserCreditItems = UserCreditItem::where('user_id', $this->model->id)->wherePaid(false)->get();
|
|
foreach ($user_credit_items as $user_credit_item) {
|
|
$user_credit_item->paid = true;
|
|
$user_credit_item->user_credit_id = $user_credit_id;
|
|
$user_credit_item->save();
|
|
}
|
|
}
|
|
|
|
private function makeUserCredit()
|
|
{
|
|
|
|
$this->user_credit->net = 0;
|
|
$this->user_credit->infos = [];
|
|
$infos = [];
|
|
$user_credit_items = [];
|
|
|
|
$UserCreditItems = UserCreditItem::where('user_id', $this->model->id)->wherePaid(false)->get();
|
|
foreach ($UserCreditItems as $userCreditItem) {
|
|
$user_credit_items[] = $userCreditItem;
|
|
$infos[] = ['id' => $userCreditItem->id, 'credit' => $userCreditItem->credit];
|
|
$this->user_credit->net += $userCreditItem->credit;
|
|
}
|
|
/* taxable_sales //user tax
|
|
1 //umsatzsteuerpflichtig / DE
|
|
2 // nicht umsatzsteuerpflichtig /DE
|
|
3 // nicht umsatzsteuerpflichtig / Ausland
|
|
*/
|
|
if ($this->model->account) {
|
|
$this->user_credit->taxable = $this->model->account->taxable_sales;
|
|
if ($this->model->account->country_id !== 1) {
|
|
$this->user_credit->taxable = 3;
|
|
}
|
|
if ($this->user_credit->taxable === 1) {
|
|
$this->user_credit->tax_rate = config('app.main_tax_rate');
|
|
$this->user_credit->total = round($this->user_credit->net * config('app.main_tax'), 2);
|
|
$this->user_credit->tax = $this->user_credit->total - $this->user_credit->net;
|
|
} else {
|
|
$this->user_credit->tax_rate = 0;
|
|
$this->user_credit->total = $this->user_credit->net;
|
|
$this->user_credit->tax = 0;
|
|
}
|
|
}
|
|
$this->user_credit->infos = $infos;
|
|
|
|
return $user_credit_items;
|
|
}
|
|
|
|
/*
|
|
Erstellt einen detalierten Report zur Gutschrift
|
|
Alle Postionen werden einzeln aufgelistet
|
|
//$do ?= html, pdf
|
|
*/
|
|
public function create_report(UserCredit $user_credit, $do = 'html')
|
|
{
|
|
// collect all data
|
|
$collection = new \stdClass;
|
|
$collection->calc_bot = [];
|
|
$collection->commission_shop = [];
|
|
$collection->commission_payline = [];
|
|
$collection->commission_growth_bonus = [];
|
|
$collection->own_order = [];
|
|
$collection->commission_registration = [];
|
|
$collection->commission_credit = [];
|
|
|
|
$dates = [];
|
|
/* für jede Postion aus der Gutschrift nach Status */
|
|
foreach ($user_credit->user_credit_items as $user_credit_item) {
|
|
|
|
$date = $user_credit_item->from_month.'-'.$user_credit_item->from_year;
|
|
if (! isset($dates[$date])) {
|
|
$dates[$date] = ['year' => $user_credit_item->from_year, 'month' => $user_credit_item->from_month];
|
|
}
|
|
|
|
/*
|
|
//calc bot for the month year
|
|
*/
|
|
if (! isset($collection->calc_bot[$date])) {
|
|
$TreeCalcBot = new TreeCalcBot($user_credit_item->from_month, $user_credit_item->from_year, 'admin');
|
|
$TreeCalcBot->initBusinesslUserDetail($user_credit->user);
|
|
$TreeCalcBot->initStructureUser($user_credit->user->id);
|
|
|
|
$collection->calc_bot[$date] = $TreeCalcBot;
|
|
}
|
|
|
|
/*
|
|
status === 1 commission_shop
|
|
Auswertung der Shopbestellungen vom User für einen Monat / Jahr
|
|
Auslistung der Positonen / Gesamter Umsatz / Marge / Provision
|
|
*/
|
|
if ($user_credit_item->status === 1) {
|
|
$user_sales_volumes = UserSalesVolume::where('user_id', $user_credit_item->user_id)
|
|
->where('month', $user_credit_item->from_month)
|
|
->where('year', $user_credit_item->from_year)
|
|
->where('status', 2) // 'shoporder', //hinzugefügt aus
|
|
->orderBy('id', 'ASC')->get();
|
|
|
|
$user_sales_volumes_total = UserSalesVolume::where('user_id', $user_credit_item->user_id)
|
|
->where('month', $user_credit_item->from_month)
|
|
->where('year', $user_credit_item->from_year)
|
|
->where('status', 2) // 'shoporder', //hinzugefügt aus
|
|
->orderBy('id', 'DESC')->first();
|
|
|
|
$obj = new \stdClass;
|
|
$obj->user_sales_volumes = $user_sales_volumes;
|
|
$obj->user_sales_volumes_total = $user_sales_volumes_total;
|
|
$obj->user_credit_item = $user_credit_item;
|
|
$collection->commission_shop[$date] = $obj;
|
|
|
|
/*
|
|
UserSalesVolume::status
|
|
4 => 'credit', //hinzugefügt aus //VE == status_turnover 2 shop verrechnung
|
|
Listen der hinzufegügten Gutschriften vom User für einen Monat / Jahr
|
|
*/
|
|
$user_sales_volumes_credit = UserSalesVolume::where('user_id', $user_credit_item->user_id)
|
|
->where('month', $user_credit_item->from_month)
|
|
->where('year', $user_credit_item->from_year)
|
|
->where('status', 4) // 'credit', //hinzugefügt aus
|
|
->where('status_turnover', 2) // VE shop
|
|
->orderBy('id', 'ASC')->get();
|
|
|
|
$collection->commission_credit[$date] = $user_sales_volumes_credit;
|
|
}
|
|
|
|
/*
|
|
status === 2 commission_payline
|
|
Auswertung der Payline nach der Struktur vom User für einen Monat / Jahr
|
|
Auslistung aller Berater mit Gesamter Umsatz / Provision / rang
|
|
*/
|
|
|
|
/*
|
|
status === 5 commission_growth_bonus
|
|
Auswertung der Payline nach der Struktur vom User für einen Monat / Jahr
|
|
Auslistung aller Berater mit Gesamter Umsatz / Provision / rang
|
|
*/
|
|
}
|
|
|
|
/*
|
|
nicht enhalten in der Gutschrift
|
|
für alle Monate / Jahr die in der Gutschrift enthalten sind
|
|
*/
|
|
foreach ($dates as $date => $dateObj) {
|
|
/*
|
|
UserSalesVolume::status
|
|
1 => 'advisor_order', own_order //hinzugefügt aus
|
|
Listen der Beraterbestellungen vom User für einen Monat / Jahr
|
|
*/
|
|
$user_sales_volumes = UserSalesVolume::where('user_id', $user_credit_item->user_id)
|
|
->where('month', $dateObj['month'])
|
|
->where('year', $dateObj['year'])
|
|
->where('status', 1) // 'own_order', //hinzugefügt aus
|
|
->orderBy('id', 'ASC')->get();
|
|
|
|
$credit_total_net = UserSalesVolume::where('user_id', $user_credit_item->user_id)
|
|
->where('month', $dateObj['month'])
|
|
->where('year', $dateObj['year'])
|
|
->where('status', 1) // 'own_order', //hinzugefügt aus
|
|
->sum('total_net'); // sum('total_net');
|
|
|
|
$credit_total_points = UserSalesVolume::where('user_id', $user_credit_item->user_id)
|
|
->where('month', $dateObj['month'])
|
|
->where('year', $dateObj['year'])
|
|
->where('status', 1) // 'own_order', //hinzugefügt aus
|
|
->sum('points'); // sum('points');
|
|
|
|
if ($user_sales_volumes->count() > 0) {
|
|
$obj = new \stdClass;
|
|
$obj->user_sales_volumes = $user_sales_volumes;
|
|
$obj->credit_total_net = $credit_total_net;
|
|
$obj->credit_total_points = $credit_total_points;
|
|
$collection->own_order[$date] = $obj;
|
|
}
|
|
/*
|
|
UserSalesVolume::status
|
|
5 => 'registration', //hinzugefügt aus
|
|
Listen der Gutschriften aus Reg vom User für einen Monat / Jahr
|
|
Enthält nur Punkte wird separat aufgeführt
|
|
turnover = immer E / 1 verrechnung mit Eigenem Umsatz
|
|
*/
|
|
|
|
$user_sales_volumes = UserSalesVolume::where('user_id', $user_credit_item->user_id)
|
|
->where('month', $dateObj['month'])
|
|
->where('year', $dateObj['year'])
|
|
->where('status', 5) // 'registration', //hinzugefügt aus
|
|
->orderBy('id', 'ASC')->get();
|
|
|
|
$credit_total_net = UserSalesVolume::where('user_id', $user_credit_item->user_id)
|
|
->where('month', $dateObj['month'])
|
|
->where('year', $dateObj['year'])
|
|
->where('status', 5) // 'registration', //hinzugefügt aus
|
|
->sum('total_net'); // sum('total_net');
|
|
|
|
$credit_total_points = UserSalesVolume::where('user_id', $user_credit_item->user_id)
|
|
->where('month', $dateObj['month'])
|
|
->where('year', $dateObj['year'])
|
|
->where('status', 5) // 'registration', //hinzugefügt aus
|
|
->sum('points'); // sum('points');
|
|
|
|
if ($user_sales_volumes->count() > 0) {
|
|
$obj = new \stdClass;
|
|
$obj->user_sales_volumes = $user_sales_volumes;
|
|
$obj->credit_total_net = $credit_total_net;
|
|
$obj->credit_total_points = $credit_total_points;
|
|
$collection->commission_registration[$date] = $obj;
|
|
}
|
|
}
|
|
|
|
/*
|
|
//need this?
|
|
$user_credit_item->status = 3; //credit_added
|
|
$user_credit_item->status = 4; //commission
|
|
*/
|
|
|
|
$data = [
|
|
'dates' => $dates,
|
|
'user_credit' => $user_credit,
|
|
'collection' => $collection,
|
|
];
|
|
if ($do === 'html') {
|
|
return view('admin.payment.credit_detail', $data);
|
|
}
|
|
|
|
if ($do === 'pdf') {
|
|
|
|
$dir = Credit::getCreditDetailStorageDir($user_credit->date);
|
|
if (! Storage::disk('public')->exists($dir)) {
|
|
Storage::disk('public')->makeDirectory($dir); // creates directory
|
|
}
|
|
$path = Storage::disk('public')->path('');
|
|
$filename = Credit::makeCreditDetailFilename($user_credit->full_number);
|
|
|
|
$pdf = new CreditDetailsPDF('pdf.credit_details');
|
|
// return $pdf->create($data, 'credit_details.pdf', 'stream');
|
|
|
|
$pdf->create($data, $filename, 'save', $path.$dir);
|
|
|
|
/*$pdf = PDF::loadView('pdf.credit', $data);
|
|
$pdf->setPaper('A4', 'portrait');
|
|
$pdf->save($path.$dir.$filename);*/
|
|
|
|
$pdfMerger = new MyPDFMerger;
|
|
$pdfMerger->addPDF($path.$dir.$filename);
|
|
$file = $pdfMerger->myMerge('string', $filename, 'template_report_de');
|
|
Storage::disk('public')->put($dir.$filename, $file);
|
|
$path = $dir.$filename;
|
|
// $file = Storage::disk('public')->get($path);
|
|
$mime = Storage::disk('public')->mimeType($path);
|
|
|
|
return Response::make($file, 200)
|
|
->header('Content-Type', $mime)
|
|
->header('Content-disposition', 'inline; filename="'.$filename.'"');
|
|
|
|
// return $dir.$filename;
|
|
/*
|
|
$dir = Credit::getCreditStorageDir($credit_date);
|
|
if(!Storage::disk('public')->exists( $dir )){
|
|
Storage::disk('public')->makeDirectory($dir); //creates directory
|
|
}
|
|
$path = Storage::disk('public')->path('');
|
|
|
|
$filename = Credit::makeCreditFilename($credit_number);
|
|
|
|
$pdf->save($path.$dir.$filename);
|
|
|
|
$pdfMerger = new MyPDFMerger();
|
|
$pdfMerger->addPDF($path.$dir.$filename);
|
|
$file = $pdfMerger->myMerge('string', $filename, 'template_invoice_de');
|
|
Storage::disk('public')->put($dir.$filename, $file);
|
|
*/
|
|
}
|
|
}
|
|
}
|