Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. Route::post('/customer/invoice/create/{customer}', function (Request $request, Customer $customer) {
  2. if (!$request->input('customer_prices') || !($request->input('customer_prices', ['quantity' => 'min:1']))) {
  3. return 'Dodaj produkty';
  4. }
  5. $year = date('Y');
  6. $now = new Carbon();
  7. $yearly_counter = Invoice::whereYear('issued_at', $year)->max('yearly_counter');
  8.  
  9. $seller = Customer::find(1);
  10. $newInvoice = new Invoice;
  11. $newInvoice->user_id = $customer->user_id;
  12. $newInvoice->customer_id = $customer->id;
  13. $newInvoice->yearly_counter = $yearly_counter + 1;
  14. $newInvoice->issued_at = $now;
  15. $newInvoice->sell_date = $now;
  16. $newInvoice->incoming = '0';
  17. $newInvoice->pay_type = $request->input('payTypeSend');
  18. $newInvoice->place = 'Kraków';
  19. $newInvoice->number = $newInvoice->yearly_counter . '/' . $year;
  20. $newInvoice->seller_address = $seller->name . "\n" . $seller->street . "\n" . $seller->postal_code . ' ' . $seller->city . "\n NIP: " . $seller->nip;
  21. if ($customer->purchaser != null) {
  22. $newInvoice->buyer_address_company = $customer->purchaser;
  23. $newInvoice->buyer_address_recipient = $customer->name . "\n" . $customer->street . "\n" . $customer->postal_code . ' ' . $customer->city;} else {
  24. $newInvoice->buyer_address_company = $customer->name . "\n" . $customer->street . "\n" . $customer->postal_code . ' ' . $customer->city . "\n NIP: " . $customer->nip;
  25.  
  26. }
  27. $lines = array_map('trim', explode("\n", $newInvoice->buyer_address_company));
  28. $totalLines = count($lines);
  29. $nameLine = implode("\n", array_slice($lines, 0, -3));
  30. $addressLine = $lines[$totalLines - 3];
  31. $postalLine = $lines[$totalLines - 2];
  32. $nipLine = $lines[$totalLines - 1];
  33. if (!preg_match('#^(?<postal>\d{2}[\s–-]+\d{3})\s+(?<city>.+)$#', $postalLine, $matches) && !preg_match('#^(?<city>.+)\s+(?<postal>\d{2}[\s–-]+\d{3})$#', $postalLine, $matches)) {
  34. // throw new InvoiceException('Invalid postal line: ' . $postalLine, $invoice->id);
  35. return response()->json('Invalid postal line: ' . $postalLine, $newInvoice->id);
  36. }
  37. $postalCode = preg_replace('#\D+#', '-', $matches['postal']);
  38. $city = $matches['city'];
  39. if (!preg_match('#^nip:\s*([\d\s–-]+|brak)$|^(?:nip:)?[\s\/:]*pesel[\s:]+[\d\s–-]+$|^nip:$#i', $nipLine, $matches)) {
  40. // throw new InvoiceException('Invalid NIP line: ' . $nipLine, $invoice->id);
  41. return response('Invalid NIP line: ' . $nipLine, $newInvoice->id);
  42.  
  43. }
  44. $nip = preg_replace('#\D#', '', $matches[1] ?? '') ?: null;
  45. $newInvoice->buyer_address_company_name = $nameLine;
  46. $newInvoice->buyer_address_company_address = $addressLine;
  47. $newInvoice->buyer_address_company_city = $city;
  48. $newInvoice->buyer_address_company_postal_code = $postalCode;
  49. $newInvoice->buyer_address_company_nip = $nip;
  50. $newInvoice->comments = $request->input('comments');
  51. $newInvoice->is_paid = '0';
  52. $newInvoice->pay_deadline = (new Carbon())->addDays($request->input('payTermSend'));
  53. $newInvoice->net_value = -1;
  54. $newInvoice->total_value = -1.23;
  55. $newInvoice->save();
  56.  
  57. if ($request->input('invoice_products.*.pivot.release_id')) {
  58. Release::whereIn('id', $request->input('invoice_products.*.pivot.release_id'))->update(['is_paid' => true]);
  59. Release::whereIn('id', $request->input('invoice_products.*.pivot.release_id'))->update(['child_id' => $newInvoice->id]);
  60.  
  61. };
  62.  
  63. if ($request->input('invoice_products.*.pivot.order_id')) {
  64. Order::whereIn('id', $request->input('invoice_products.*.pivot.order_id'))->update(['is_paid' => true]);
  65.  
  66. };
  67.  
  68. if ($request->input('invoice_products.*.pivot.test_id')) {
  69. Test::whereIn('id', $request->input('invoice_products.*.pivot.test_id'))->update(['is_paid' => true]);
  70. Test::whereIn('id', $request->input('invoice_products.*.pivot.test_id'))->update(['rate' => 'Pozytywny']);
  71.  
  72. };
  73.  
  74. if ($newInvoice->pay_type == 'cash') {
  75. $comment = new InvoiceComment;
  76. $comment->invoice_id = $newInvoice->id;
  77. $comment->user_id = $newInvoice->user_id;
  78. $comment->note = 'Gotówka';
  79. $comment->save();
  80. };
  81.  
  82. $productsInput = $request->input('customer_prices');
  83. $pivotData = array_map(function (array $productInput) use ($customer) {
  84. return $productInput + ['customer_id' => $customer->id,
  85. 'gross_unit_price' => round(($productInput['net_unit_price']) * 1.23, 2),
  86. ];}, $productsInput);
  87. $newInvoice->products()->sync($pivotData);
  88. $newInvoice->net_value = $newInvoice->total_sum_net();
  89. $newInvoice->total_value = $newInvoice->total_sum_gross();
  90. $newInvoice->update();
  91.  
  92. if ($yearly_counter === 1) {
  93. $when = now()->addSeconds(10);
  94. $user = User::findOrFail(5);
  95. $user->notify((new InvoiceMade($newInvoice))->delay($when));
  96. };
  97.  
  98. $when = now()->addSeconds(10);
  99. $user = User::findOrFail($newInvoice->user_id);
  100. $user->notify((new InvoiceMade($newInvoice))->delay($when));
  101.  
  102. return ['url' => route('invoice-view', ['id' => $newInvoice->id, 'download' => true])];
  103.  
  104. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement