Invoiceable.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Traits;
  3. use App\Models\PartnerDiscount;
  4. use App\Models\Payment;
  5. use App\Models\ShopProduct;
  6. use App\Models\Invoice;
  7. use App\Notifications\InvoiceNotification;
  8. use App\Settings\InvoiceSettings;
  9. use Illuminate\Support\Facades\Storage;
  10. use LaravelDaily\Invoices\Classes\Buyer;
  11. use LaravelDaily\Invoices\Classes\InvoiceItem;
  12. use LaravelDaily\Invoices\Classes\Party;
  13. use LaravelDaily\Invoices\Invoice as DailyInvoice;
  14. use Symfony\Component\Intl\Currencies;
  15. trait Invoiceable
  16. {
  17. public function createInvoice(Payment $payment, ShopProduct $shopProduct, InvoiceSettings $invoice_settings)
  18. {
  19. $user = $payment->user;
  20. //create invoice
  21. $lastInvoiceID = Invoice::where("invoice_name", "like", "%" . now()->format('mY') . "%")->count("id");
  22. $newInvoiceID = $lastInvoiceID + 1;
  23. $logoPath = storage_path('app/public/logo.png');
  24. $seller = new Party([
  25. 'name' => $invoice_settings->company_name,
  26. 'phone' => $invoice_settings->company_phone,
  27. 'address' => $invoice_settings->company_address,
  28. 'vat' => $invoice_settings->company_vat,
  29. 'custom_fields' => [
  30. 'E-Mail' => $invoice_settings->company_mail,
  31. "Web" => $invoice_settings->company_website
  32. ],
  33. ]);
  34. $customer = new Buyer([
  35. 'name' => $user->name,
  36. 'custom_fields' => [
  37. 'E-Mail' => $user->email,
  38. 'Client ID' => $user->id,
  39. ],
  40. ]);
  41. $item = (new InvoiceItem())
  42. ->title($shopProduct->description)
  43. ->pricePerUnit($shopProduct->price);
  44. $notes = [
  45. __("Payment method") . ": " . $payment->payment_method,
  46. ];
  47. $notes = implode("<br>", $notes);
  48. $invoice = DailyInvoice::make()
  49. ->template('controlpanel')
  50. ->name(__("Invoice"))
  51. ->buyer($customer)
  52. ->seller($seller)
  53. ->discountByPercent(PartnerDiscount::getDiscount())
  54. ->taxRate(floatval($shopProduct->getTaxPercent()))
  55. ->shipping(0)
  56. ->addItem($item)
  57. ->status(__($payment->status))
  58. ->series(now()->format('mY'))
  59. ->delimiter("-")
  60. ->sequence($newInvoiceID)
  61. ->serialNumberFormat($invoice_settings->prefix . '{DELIMITER}{SERIES}{SEQUENCE}')
  62. ->currencyCode(strtoupper($payment->currency_code))
  63. ->currencySymbol(Currencies::getSymbol(strtoupper($payment->currency_code)))
  64. ->notes($notes);
  65. if (file_exists($logoPath)) {
  66. $invoice->logo($logoPath);
  67. }
  68. //Save the invoice in "storage\app\invoice\USER_ID\YEAR"
  69. $invoice->filename = $invoice->getSerialNumber() . '.pdf';
  70. $invoice->render();
  71. Storage::disk("local")->put("invoice/" . $user->id . "/" . now()->format('Y') . "/" . $invoice->filename, $invoice->output);
  72. Invoice::create([
  73. 'invoice_user' => $user->id,
  74. 'invoice_name' => $invoice->getSerialNumber(),
  75. 'payment_id' => $payment->payment_id,
  76. ]);
  77. //Send Invoice per Mail
  78. $user->notify(new InvoiceNotification($invoice, $user, $payment));
  79. }
  80. }