diff --git a/app/Http/Controllers/FileController.php b/app/Http/Controllers/FileController.php index bd395c3..6d1256f 100644 --- a/app/Http/Controllers/FileController.php +++ b/app/Http/Controllers/FileController.php @@ -2,9 +2,10 @@ namespace App\Http\Controllers; -use App\Services\Invoice; -use Response; use Storage; +use Response; +use App\Services\Credit; +use App\Services\Invoice; class FileController extends Controller { @@ -63,8 +64,8 @@ class FileController extends Controller if ($disk === 'credit'){ $UserCredit = \App\Models\UserCredit::findOrFail($id); - $filename = Invoice::getCreditFilename($UserCredit); - $path = Invoice::getCreditDownloadPath($UserCredit); + $filename = Credit::getFilename($UserCredit); + $path = Credit::getDownloadPath($UserCredit); if (!Storage::disk('public')->exists($path)) { return Response::make('File no found.', 404); } diff --git a/app/Http/Controllers/PaymentCreditController.php b/app/Http/Controllers/PaymentCreditController.php index 7643caf..bfae52f 100644 --- a/app/Http/Controllers/PaymentCreditController.php +++ b/app/Http/Controllers/PaymentCreditController.php @@ -5,11 +5,9 @@ namespace App\Http\Controllers; use Carbon; use Request; use App\User; -use App\Services\Invoice; -use App\Services\Payment; -use App\Models\ShoppingOrder; -use App\Models\ShoppingOrderMargin; +use App\Services\Credit; use App\Models\UserCredit; +use App\Models\ShoppingOrderMargin; use App\Repositories\CreditRepository; class PaymentCreditController extends Controller @@ -120,7 +118,7 @@ class PaymentCreditController extends Controller })*/ ->addColumn('credit', function (UserCredit $UserCredit) { $ret = ""; - if(Invoice::isCredit($UserCredit)){ + if(Credit::isCredit($UserCredit)){ $ret .= ' '; $ret .= ''; }else{ diff --git a/app/Http/Controllers/PaymentPayCreditController.php b/app/Http/Controllers/PaymentPayCreditController.php new file mode 100644 index 0000000..53948fa --- /dev/null +++ b/app/Http/Controllers/PaymentPayCreditController.php @@ -0,0 +1,85 @@ +middleware('auth'); + $this->filter_user_status = 'all'; + } + + public function index() + { + + $this->setActiveFilter(); + $data = [ + 'filter_user_status' => $this->filter_user_status, + ]; + return view('admin.payment.pay_credit.index', $data); + } + + private function setActiveFilter(){ + if(Request::get('filter_user_status')){ + $this->filter_user_status = Request::get('filter_user_status'); + } + } + + public function datatable(){ + + $this->setActiveFilter(); + + + $query = User::with('account')->select('users.*')->where('users.deleted_at', '=', null) + ->where('active', true); + + + //->orderBy('created_at', 'DESC'); + + return \DataTables::eloquent($query) + ->addColumn('id', function (User $user) { + return ''; + }) + ->addColumn('first_name', function (User $user) { + return $user->account ? $user->account->first_name : ''; + }) + ->addColumn('last_name', function (User $user) { + return $user->account ? $user->account->last_name : ''; + }) + ->addColumn('payment_credit', function (User $user) { + return $user->payment_credit ? $user->getFormattedPaymentCredit().' €' : ''; + }) + ->addColumn('is_active_account', function (User $user) { + if($user->payment_account){ + if($user->isActiveAccount()){ + return ''; + } + return ''; + } + return ''; + }) + ->addColumn('action', function (User $user) { + $ret = ''; + return $ret; + }) + + ->orderColumn('id', 'id $1') + ->orderColumn('txaction', 'txaction $1') + ->orderColumn('payment_credit', 'payment_credit $1') + ->orderColumn('is_active_account', 'is_active_account $1') + ->rawColumns(['id', 'shipping_order', 'is_active_account', 'action']) + ->make(true); + } +} \ No newline at end of file diff --git a/app/Mail/MailCredit.php b/app/Mail/MailCredit.php new file mode 100644 index 0000000..72109ff --- /dev/null +++ b/app/Mail/MailCredit.php @@ -0,0 +1,52 @@ +user_credit = $user_credit; + $this->subject = 'Gutschrift auf Grüne Seele'; + + } + + public function build() + { + $title = __('email.credit_title'); + $copy1line = __('email.credit_copy1line'); + + $filename = Credit::getFilename($this->user_credit); + $path = Credit::getDownloadPath($this->user_credit); + if (!Storage::disk('public')->exists($path)) { + return; + } + $file = Storage::disk('public')->path($path); + $mime = Storage::disk('public')->mimeType($path); + + return $this->view('emails.blank')->with([ + 'title' => $title, + 'copy1line' => $copy1line, + ])->attach($file,[ + 'as' => $filename, + 'mime' => $mime, + ]); // attach file; + } +} \ No newline at end of file diff --git a/app/Models/ShoppingOrderMargin.php b/app/Models/ShoppingOrderMargin.php index 9003296..fc0cb76 100644 --- a/app/Models/ShoppingOrderMargin.php +++ b/app/Models/ShoppingOrderMargin.php @@ -76,6 +76,7 @@ class ShoppingOrderMargin extends Model 'status' => 'int', 'partner_commission_pending_to' => 'datetime', 'partner_commission_paid' => 'bool', + 'user_credit_id' => 'int', ]; protected $dates = [ @@ -97,6 +98,7 @@ class ShoppingOrderMargin extends Model 'status', 'partner_commission_pending_to', 'partner_commission_paid', + 'user_credit_id', 'content' ]; diff --git a/app/Models/UserPayCredit.php b/app/Models/UserPayCredit.php new file mode 100644 index 0000000..b9d85bc --- /dev/null +++ b/app/Models/UserPayCredit.php @@ -0,0 +1,68 @@ + 'add from payment', + 2 => 'deduction from payment', + ]; + protected $table = 'user_pay_credits'; + + protected $casts = [ + 'user_id' => 'int', + 'credit' => 'float', + 'old_credit_total' => 'float', + 'new_credit_total' => 'float', + 'status' => 'int', + 'shopping_order_id' => 'int' + ]; + + protected $fillable = [ + 'user_id', + 'credit', + 'old_credit_total', + 'new_credit_total', + 'message', + 'status', + 'shopping_order_id' + ]; + + public function shopping_order() + { + return $this->belongsTo(ShoppingOrder::class); + } + + public function user() + { + return $this->belongsTo(User::class); + } +} diff --git a/app/Repositories/CreditRepository.php b/app/Repositories/CreditRepository.php index 516201b..ff3ebdc 100644 --- a/app/Repositories/CreditRepository.php +++ b/app/Repositories/CreditRepository.php @@ -4,7 +4,7 @@ namespace App\Repositories; use PDF; use Storage; -use App\Services\Invoice; +use App\Services\Credit; use App\Services\MyPDFMerger; use App\Services\UserMarign; use App\Models\UserCredit; @@ -21,10 +21,10 @@ class CreditRepository extends BaseRepository { public function create($request = []) { //need invoice $data - $credit_number = isset($request['credit_number']) ? $request['credit_number'] : Invoice::getInvoiceNumber(); + $credit_number = isset($request['credit_number']) ? $request['credit_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 = Invoice::createInvoiceNumber($credit_number, $credit_date); + $credit_number = Credit::createCreditNumber($credit_number, $credit_date); $user_credits = $this->makeUserCredit(); @@ -38,13 +38,13 @@ class CreditRepository extends BaseRepository { $pdf = PDF::loadView('pdf.credit', $data); $pdf->setPaper('A4', 'portrait'); - $dir = Invoice::getCreditStorageDir($credit_date); + $dir = Credit::getCreditStorageDir($credit_date); if(!Storage::disk('public')->exists( $dir )){ Storage::disk('public')->makeDirectory($dir); //creates directory } $path = Storage::disk('public')->getAdapter()->getPathPrefix(); - $filename = Invoice::makeCreditFilename($credit_number); + $filename = Credit::makeCreditFilename($credit_number); $pdf->save($path.$dir.$filename); @@ -61,7 +61,7 @@ class CreditRepository extends BaseRepository { 'invoice_number' => $credit_number, 'credit_date' => $credit_date, ]; - UserCredit::create([ + $user_credit = UserCredit::create([ 'auth_user_id' => $this->model->id, 'net' => $user_credits->net, 'tax_rate' => $user_credits->tax_rate, @@ -72,31 +72,47 @@ class CreditRepository extends BaseRepository { 'user_margins' => $user_credits->margins, ]); - + + if($credit_send_mail){ - //Invoice::sendInvoiceMail($this->model); + Credit::sendCreditMail($user_credit); } - //Invoice::makeNextInvoiceNumber(); + $this->finishUserCredit($user_credit); + return true; //return $pdf->stream('invoice.pdf'); //return $this->output($path.$dir, $filename); } + private function finishUserCredit($user_credit){ + //next number + Credit::makeNextCreditNumber(); + //mark as payed + $ShoppingOrderMargins = UserMarign::getOrderFromPartnerCommissionByID($this->model->id); + foreach($ShoppingOrderMargins as $ShoppingOrderMargin){ + $ShoppingOrderMargin->partner_commission_paid = true; + $ShoppingOrderMargin->user_credit_id = $user_credit->id; + $ShoppingOrderMargin->save(); + + } + + + } private function makeUserCredit(){ $ret = new \stdClass(); $ret->net = 0; - $UserMarigns = UserMarign::getOrderFromPartnerCommissionByID($this->model->id); - foreach($UserMarigns as $UserMarign){ + $ShoppingOrderMargins = UserMarign::getOrderFromPartnerCommissionByID($this->model->id); + foreach($ShoppingOrderMargins as $ShoppingOrderMargin){ $margin = new \stdClass(); - $margin->id = $UserMarign->id; - $margin->net = $UserMarign->net_partner_commission; - $margin->reference = $UserMarign->shopping_order->getLastShoppingPayment('reference'); - $margin->firstname = $UserMarign->shopping_order->shopping_user->billing_firstname; - $margin->lastname = $UserMarign->shopping_order->shopping_user->billing_lastname; - $margin->created_at = $UserMarign->shopping_order->created_at->format("d.m.Y"); + $margin->id = $ShoppingOrderMargin->id; + $margin->net = $ShoppingOrderMargin->net_partner_commission; + $margin->reference = $ShoppingOrderMargin->shopping_order->getLastShoppingPayment('reference'); + $margin->firstname = $ShoppingOrderMargin->shopping_order->shopping_user->billing_firstname; + $margin->lastname = $ShoppingOrderMargin->shopping_order->shopping_user->billing_lastname; + $margin->created_at = $ShoppingOrderMargin->shopping_order->created_at->format("d.m.Y"); $ret->margins[] = $margin; - $ret->net += $UserMarign->net_partner_commission; + $ret->net += $ShoppingOrderMargin->net_partner_commission; } /* taxable_sales //user tax 1 //umsatzsteuerpflichtig diff --git a/app/Services/Credit.php b/app/Services/Credit.php new file mode 100644 index 0000000..029c954 --- /dev/null +++ b/app/Services/Credit.php @@ -0,0 +1,81 @@ +format('Ym'); + return $prefix.$invoice_number; + } + + public static function getCreditStorageDir($invoice_date){ + return "/credit/".\Carbon::parse($invoice_date)->format('Y/m/'); + } + + public static function makeCreditFilename($invoice_number){ + return "Gutschrift-".$invoice_number.".pdf"; + } + + public static function isCredit(UserCredit $user_credit){ + return isset($user_credit->credit['filename']) ? true : false; + } + + public static function getFilename(UserCredit $user_credit){ + return isset($user_credit->credit['filename']) ? $user_credit->credit['filename'] : false; + } + + + public static function getDir(UserCredit $user_credit){ + return isset($user_credit->credit['dir']) ? $user_credit->credit['dir'] : false; + } + + public static function getDownloadURL(UserCredit $user_credit, $do = false){ + return route('storage_file', [$user_credit->id, 'cms_download_file', $do]); + } + + public static function getDownloadPath(UserCredit $user_credit, $full = false){ + $dir = self::getDir($user_credit); + $filename = self::getFilename($user_credit); + if(!$full){ + return $dir.$filename; + } + return \Storage::disk('public')->path($dir.$filename); + } + + public static function sendCreditMail(UserCredit $user_credit){ + $bcc = []; + $email = $user_credit->user->email; + $email = "kevin.adametz@me.com"; + if(!$email){ + if($user_credit->user->mode === 'test'){ + }else{ + $email = config('app.checkout_mail'); + } + } + if($user_credit->user->mode === 'test'){ + $bcc[] = config('app.checkout_test_mail'); + }else{ + $bcc[] = config('app.checkout_mail'); + } + Mail::to($email)->bcc($bcc)->send(new MailCredit($user_credit)); + } +} diff --git a/app/Services/Invoice.php b/app/Services/Invoice.php index 69ad32d..26fd61f 100644 --- a/app/Services/Invoice.php +++ b/app/Services/Invoice.php @@ -31,41 +31,22 @@ class Invoice return "/invoice/".\Carbon::parse($invoice_date)->format('Y/m/'); } - public static function getCreditStorageDir($invoice_date){ - return "/credit/".\Carbon::parse($invoice_date)->format('Y/m/'); - } - public static function makeInvoiceFilename($invoice_number){ return "Rechnung-".$invoice_number.".pdf"; } - public static function makeCreditFilename($invoice_number){ - return "Gutschrift-".$invoice_number.".pdf"; - } - public static function isInvoice(ShoppingOrder $shopping_order){ return isset($shopping_order->invoice['filename']) ? true : false; } - public static function isCredit(UserCredit $user_credit){ - return isset($user_credit->credit['filename']) ? true : false; - } public static function getFilename(ShoppingOrder $shopping_order){ return isset($shopping_order->invoice['filename']) ? $shopping_order->invoice['filename'] : false; } - public static function getCreditFilename(UserCredit $user_credit){ - return isset($user_credit->credit['filename']) ? $user_credit->credit['filename'] : false; - } - public static function getDir(ShoppingOrder $shopping_order){ return isset($shopping_order->invoice['dir']) ? $shopping_order->invoice['dir'] : false; } - public static function getCreditDir(UserCredit $user_credit){ - return isset($user_credit->credit['dir']) ? $user_credit->credit['dir'] : false; - } - public static function getDownloadURL(ShoppingOrder $shopping_order, $do = false){ return route('storage_file', [$shopping_order->id, 'cms_download_file', $do]); } @@ -78,15 +59,6 @@ class Invoice return \Storage::disk('public')->path($dir.$filename); } - public static function getCreditDownloadPath(UserCredit $user_credit, $full = false){ - $dir = self::getCreditDir($user_credit); - $filename = self::getCreditFilename($user_credit); - if(!$full){ - return $dir.$filename; - } - return \Storage::disk('public')->path($dir.$filename); - } - public static function sendInvoiceMail($shopping_order){ $bcc = []; $billing_email = $shopping_order->shopping_user->billing_email; diff --git a/app/Services/Payment.php b/app/Services/Payment.php index c52ec54..7614f37 100644 --- a/app/Services/Payment.php +++ b/app/Services/Payment.php @@ -6,6 +6,7 @@ use App\Mail\MailCheckout; use App\Models\Setting; use App\Models\ShoppingOrder; use App\Models\ShoppingPayment; +use App\Models\UserPayCredit; use App\User; use Illuminate\Support\Facades\Mail; @@ -91,7 +92,22 @@ class Payment return ''.self::getFormattedTxaction($shopping_payment->txaction).''; } + public static function addUserPayCredits(User $user, $credit, $status, $message, $shopping_order_id = null){ + $new_credit_total = $user->payment_credit; + UserPayCredit::create([ + 'user_id' => $user->id, + 'credit' => $credit, + 'old_credit_total' => $user->payment_credit, + 'new_credit_total' => $user->payment_credit + $credit, + 'message' => $message, + 'status' => $status, + 'shopping_order_id' => $shopping_order_id, + ]); + $user->payment_credit = $user->payment_credit + $credit; + $user->save(); + + } public static function paymentStatusPaidAction(ShoppingOrder $shopping_order, $paid){ $send_link = false; @@ -119,8 +135,10 @@ class Payment // $user->payment_order_id = $shopping_order_item->product->id; //34 $user->payment_account = $date; $user->wizard = 100; - $user->payment_credit = $shopping_order_item->product->price; + $user->save(); + self::addUserPayCredits($user, $shopping_order_item->product->price, 1, 'payment_for_account'); $shopping_order->setUserHistoryValue(['status' => 9]); + } /*if($shopping_order_item->product->getActionName($do) === 'payment_for_shop'){ $user->payment_order_id = $shopping_order_item->product->id; //35 @@ -140,7 +158,7 @@ class Payment $user->m_level = $shopping_order_item->product->upgrade_to_id; } }*/ - $user->save(); + //$user->save(); } } } @@ -154,10 +172,8 @@ class Payment $shopping_order->shopping_order_margin->save(); //is payment credit, reduce if($shopping_order->shopping_order_margin->from_payment_credit > 0){ - $new_credit = $shopping_order->auth_user->payment_credit - $shopping_order->shopping_order_margin->from_payment_credit; - $new_credit = $new_credit < 0 ? 0 : $new_credit; - $shopping_order->auth_user->payment_credit = $new_credit; - $shopping_order->auth_user->save(); + $credit = $shopping_order->shopping_order_margin->from_payment_credit * -1; + self::addUserPayCredits($shopping_order->auth_user, $credit, 2, 'user_order_deduction'); } } diff --git a/app/User.php b/app/User.php index ea9e231..ea38706 100644 --- a/app/User.php +++ b/app/User.php @@ -293,6 +293,9 @@ class User extends Authenticatable return Carbon::now()->diffForHumans(Carbon::parse($this->payment_shop)); } + public function isAboOption(){ + return false; + } /** * @return string */ diff --git a/database/migrations/2021_01_21_170707_create_shopping_order_margins_table.php b/database/migrations/2021_01_21_170707_create_shopping_order_margins_table.php index d310b80..fc0abd5 100644 --- a/database/migrations/2021_01_21_170707_create_shopping_order_margins_table.php +++ b/database/migrations/2021_01_21_170707_create_shopping_order_margins_table.php @@ -34,6 +34,8 @@ class CreateShoppingOrderMarginsTable extends Migration $table->timestamp('partner_commission_pending_to')->nullable(); $table->boolean('partner_commission_paid')->default(false); + $table->unsignedBigInteger('user_credit_id')->nullable(); + $table->text('content')->nullable(); diff --git a/database/migrations/2021_04_01_185905_create_user_pay_credits_table.php b/database/migrations/2021_04_01_185905_create_user_pay_credits_table.php new file mode 100644 index 0000000..af73d2c --- /dev/null +++ b/database/migrations/2021_04_01_185905_create_user_pay_credits_table.php @@ -0,0 +1,52 @@ +id(); + + $table->unsignedInteger('user_id'); + + $table->decimal('credit', 13, 2)->nullable(); + $table->decimal('old_credit_total', 13, 2)->nullable(); + $table->decimal('new_credit_total', 13, 2)->nullable(); + + $table->text('message')->nullable(); + + $table->unsignedTinyInteger('status')->index()->default(0); + + $table->unsignedInteger('shopping_order_id')->nullable(); + $table->timestamps(); + + $table->foreign('user_id') + ->references('id') + ->on('users'); + + $table->foreign('shopping_order_id') + ->references('id') + ->on('shopping_orders'); + + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('user_pay_credits'); + } +} diff --git a/resources/lang/de/email.php b/resources/lang/de/email.php index 7dadbbc..9b99fcc 100644 --- a/resources/lang/de/email.php +++ b/resources/lang/de/email.php @@ -46,7 +46,9 @@ 'checkout_copy3line_extern' => 'Bestellung über Berater:', 'status_copy1line' => 'Status zu Deiner Bestellung auf Grüne Seele', 'invoice_title' => 'Rechnung zu Deiner Bestellung auf Grüne Seele', + 'credit_title' => 'Gutschrift aus Vertriebspartnerbestellungen auf Grüne Seele', 'invoice_copy1line' => 'vielen Dank für Deine Bestellung bei Grüne Seele. Nachfolgend senden wir Dir die Rechnung zu deiner Bestellung: ', + 'credit_copy1line' => 'vielen Dank für Deine Vertriebspartnerschaft bei Grüne Seele. Nachfolgend senden wir Dir eine Gutschrift zu Bestellungen von deinen Vertriebspartnern: ', 'footer_copy1' => 'GRÜNE SEELE GbR | Hauptstrasse 174 | 51143 Köln | Telefon: (+49) 2203 183 86 14 | E-Mail: service@gruene-seele.bio', 'footer_copy2' => '', 'footer_copy3' => '© 2021 All Rights Reserved', diff --git a/resources/lang/de/navigation.php b/resources/lang/de/navigation.php index 4d454ec..6896b1f 100755 --- a/resources/lang/de/navigation.php +++ b/resources/lang/de/navigation.php @@ -44,4 +44,5 @@ return [ 'credit' => 'Gutschriften', 'invoice' => 'Rechnungen', 'revenue' => 'Umsätze', + 'paycredit' => 'Guthaben', ]; diff --git a/resources/views/admin/payment/credit/index.blade.php b/resources/views/admin/payment/credit/index.blade.php index d17d529..88ff780 100755 --- a/resources/views/admin/payment/credit/index.blade.php +++ b/resources/views/admin/payment/credit/index.blade.php @@ -211,8 +211,8 @@