Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Route::post('/customer/invoice/create/{customer}', function (Request $request, Customer $customer) {
- if (!$request->input('customer_prices') || !($request->input('customer_prices', ['quantity' => 'min:1']))) {
- return 'Dodaj produkty';
- }
- $year = date('Y');
- $now = new Carbon();
- $yearly_counter = Invoice::whereYear('issued_at', $year)->max('yearly_counter');
- $seller = Customer::find(1);
- $newInvoice = new Invoice;
- $newInvoice->user_id = $customer->user_id;
- $newInvoice->customer_id = $customer->id;
- $newInvoice->yearly_counter = $yearly_counter + 1;
- $newInvoice->issued_at = $now;
- $newInvoice->sell_date = $now;
- $newInvoice->incoming = '0';
- $newInvoice->pay_type = $request->input('payTypeSend');
- $newInvoice->place = 'Kraków';
- $newInvoice->number = $newInvoice->yearly_counter . '/' . $year;
- $newInvoice->seller_address = $seller->name . "\n" . $seller->street . "\n" . $seller->postal_code . ' ' . $seller->city . "\n NIP: " . $seller->nip;
- if ($customer->purchaser != null) {
- $newInvoice->buyer_address_company = $customer->purchaser;
- $newInvoice->buyer_address_recipient = $customer->name . "\n" . $customer->street . "\n" . $customer->postal_code . ' ' . $customer->city;} else {
- $newInvoice->buyer_address_company = $customer->name . "\n" . $customer->street . "\n" . $customer->postal_code . ' ' . $customer->city . "\n NIP: " . $customer->nip;
- }
- $lines = array_map('trim', explode("\n", $newInvoice->buyer_address_company));
- $totalLines = count($lines);
- $nameLine = implode("\n", array_slice($lines, 0, -3));
- $addressLine = $lines[$totalLines - 3];
- $postalLine = $lines[$totalLines - 2];
- $nipLine = $lines[$totalLines - 1];
- 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)) {
- // throw new InvoiceException('Invalid postal line: ' . $postalLine, $invoice->id);
- return response()->json('Invalid postal line: ' . $postalLine, $newInvoice->id);
- }
- $postalCode = preg_replace('#\D+#', '-', $matches['postal']);
- $city = $matches['city'];
- if (!preg_match('#^nip:\s*([\d\s–-]+|brak)$|^(?:nip:)?[\s\/:]*pesel[\s:]+[\d\s–-]+$|^nip:$#i', $nipLine, $matches)) {
- // throw new InvoiceException('Invalid NIP line: ' . $nipLine, $invoice->id);
- return response('Invalid NIP line: ' . $nipLine, $newInvoice->id);
- }
- $nip = preg_replace('#\D#', '', $matches[1] ?? '') ?: null;
- $newInvoice->buyer_address_company_name = $nameLine;
- $newInvoice->buyer_address_company_address = $addressLine;
- $newInvoice->buyer_address_company_city = $city;
- $newInvoice->buyer_address_company_postal_code = $postalCode;
- $newInvoice->buyer_address_company_nip = $nip;
- $newInvoice->comments = $request->input('comments');
- $newInvoice->is_paid = '0';
- $newInvoice->pay_deadline = (new Carbon())->addDays($request->input('payTermSend'));
- $newInvoice->net_value = -1;
- $newInvoice->total_value = -1.23;
- $newInvoice->save();
- if ($request->input('invoice_products.*.pivot.release_id')) {
- Release::whereIn('id', $request->input('invoice_products.*.pivot.release_id'))->update(['is_paid' => true]);
- Release::whereIn('id', $request->input('invoice_products.*.pivot.release_id'))->update(['child_id' => $newInvoice->id]);
- };
- if ($request->input('invoice_products.*.pivot.order_id')) {
- Order::whereIn('id', $request->input('invoice_products.*.pivot.order_id'))->update(['is_paid' => true]);
- };
- if ($request->input('invoice_products.*.pivot.test_id')) {
- Test::whereIn('id', $request->input('invoice_products.*.pivot.test_id'))->update(['is_paid' => true]);
- Test::whereIn('id', $request->input('invoice_products.*.pivot.test_id'))->update(['rate' => 'Pozytywny']);
- };
- if ($newInvoice->pay_type == 'cash') {
- $comment = new InvoiceComment;
- $comment->invoice_id = $newInvoice->id;
- $comment->user_id = $newInvoice->user_id;
- $comment->note = 'Gotówka';
- $comment->save();
- };
- $productsInput = $request->input('customer_prices');
- $pivotData = array_map(function (array $productInput) use ($customer) {
- return $productInput + ['customer_id' => $customer->id,
- 'gross_unit_price' => round(($productInput['net_unit_price']) * 1.23, 2),
- ];}, $productsInput);
- $newInvoice->products()->sync($pivotData);
- $newInvoice->net_value = $newInvoice->total_sum_net();
- $newInvoice->total_value = $newInvoice->total_sum_gross();
- $newInvoice->update();
- if ($yearly_counter === 1) {
- $when = now()->addSeconds(10);
- $user = User::findOrFail(5);
- $user->notify((new InvoiceMade($newInvoice))->delay($when));
- };
- $when = now()->addSeconds(10);
- $user = User::findOrFail($newInvoice->user_id);
- $user->notify((new InvoiceMade($newInvoice))->delay($when));
- return ['url' => route('invoice-view', ['id' => $newInvoice->id, 'download' => true])];
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement