Invoiceable.php 3.2 KB

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