Build the stuff for incoming invoices.
This commit is contained in:
@@ -3,8 +3,10 @@
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Incoming;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Payment;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
||||
@@ -31,7 +33,7 @@ class ExcelController extends Controller
|
||||
$requestData = $request->validate([
|
||||
'from' => 'required|date',
|
||||
'end' => 'required|date',
|
||||
'report' => 'required|in:invoice,payment',
|
||||
'report' => 'required|in:invoice,payment,incoming,outgoing',
|
||||
]);
|
||||
|
||||
$this->from = $requestData['from'];
|
||||
@@ -51,7 +53,14 @@ class ExcelController extends Controller
|
||||
case 'payment':
|
||||
$this->payment();
|
||||
break;
|
||||
case 'incoming':
|
||||
$this->incoming();
|
||||
break;
|
||||
case 'outgoing':
|
||||
$this->outgoing();
|
||||
break;
|
||||
}
|
||||
|
||||
return response()->json($this->writer->save('php://output'));
|
||||
}
|
||||
|
||||
@@ -159,6 +168,7 @@ class ExcelController extends Controller
|
||||
$row = 2;
|
||||
foreach ($payments as $payment) {
|
||||
$invoice = $payment->invoice;
|
||||
|
||||
foreach ($invoice->items as $item) {
|
||||
if (!isset($net[$item->tax])) {
|
||||
$net[$item->tax] = 0;
|
||||
@@ -169,6 +179,7 @@ class ExcelController extends Controller
|
||||
$tax[$item->tax] += $item->amount * $item->price * $item->tax * $payment->paid_amount / ($invoice->sum * 100);
|
||||
$gross[$item->tax] += $item->total * $payment->paid_amount / ($invoice->sum);
|
||||
}
|
||||
|
||||
$worksheet->setCellValue('A' . $row, $invoice->number);
|
||||
$worksheet->setCellValue('B' . $row, $invoice->customer->name);
|
||||
$worksheet->setCellValue('C' . $row, $invoice->address->name);
|
||||
@@ -204,6 +215,97 @@ class ExcelController extends Controller
|
||||
}
|
||||
|
||||
$this->writer = new Xlsx($spreadsheet);
|
||||
}
|
||||
|
||||
protected function incoming(): void
|
||||
{
|
||||
$incoming = Incoming::whereBetween('issue_date', [$this->from, $this->end])->with(['supplier', 'taxes'])->orderBy('issue_date', 'desc')->get();
|
||||
$this->incomingToSheet($incoming);
|
||||
}
|
||||
|
||||
protected function outgoing(): void
|
||||
{
|
||||
$incoming = Incoming::whereBetween('pay_date', [$this->from, $this->end])->with(['supplier', 'taxes'])->orderBy('pay_date', 'desc')->get();
|
||||
$this->incomingToSheet($incoming);
|
||||
}
|
||||
|
||||
protected function incomingToSheet(Collection $incoming): void
|
||||
{
|
||||
$net = [];
|
||||
$tax = [];
|
||||
$gross = [];
|
||||
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
$worksheet->setTitle(__('incoming.Incoming'));
|
||||
$worksheet->getPageSetup()->setOrientation(PageSetup::ORIENTATION_LANDSCAPE);
|
||||
$worksheet->getHeaderFooter()->setEvenHeader(__('incoming.Invoices from to', ['from' => $this->from, 'end' => $this->end]));
|
||||
$worksheet->getHeaderFooter()->setOddHeader(__('incoming.Invoices from to', ['from' => $this->from, 'end' => $this->end]));
|
||||
|
||||
$worksheet->setCellValue('A1', __('invoice.Invoice Number'));
|
||||
$worksheet->setCellValue('B1', __('customer.Customer'));
|
||||
$worksheet->setCellValue('C1', __('common.Name'));
|
||||
$worksheet->setCellValue('D1', __('invoice.Net'));
|
||||
$worksheet->setCellValue('E1', __('invoice.Tax'));
|
||||
$worksheet->setCellValue('F1', __('invoice.Gross'));
|
||||
$worksheet->setCellValue('G1', __('common.Created at'));
|
||||
$worksheet->setCellValue('H1', __('common.Paid at'));
|
||||
$worksheet->getStyle('A1:H1')->getBorders()->getBottom()->applyFromArray(['borderStyle' => Border::BORDER_DOUBLE]);
|
||||
$worksheet->getStyle('A1:H1')->getFont()->setBold(true);
|
||||
$worksheet->getStyle('D1:H1')->getAlignment()->applyFromArray(['horizontal' => Alignment::HORIZONTAL_RIGHT]);
|
||||
|
||||
$row = 2;
|
||||
foreach ($incoming as $invoice) {
|
||||
foreach ($invoice->taxes as $item) {
|
||||
if (!isset($net[$item->percentage])) {
|
||||
$net[$item->percentage] = 0;
|
||||
$tax[$item->percentage] = 0;
|
||||
$gross[$item->percentage] = 0;
|
||||
}
|
||||
$net[$item->percentage] += $item->taxable_amount;
|
||||
$tax[$item->percentage] += $item->amount;
|
||||
$gross[$item->percentage] += $item->taxable_amount + $item->amount;
|
||||
}
|
||||
$worksheet->setCellValue('A' . $row, $invoice->invoice_number);
|
||||
$worksheet->setCellValue('B' . $row, $invoice->supplier->name);
|
||||
$worksheet->setCellValue('C' . $row, $invoice->supplier->email);
|
||||
$worksheet->getCell('D' . $row)->getStyle()->getNumberFormat()->setFormatCode($this->currencyMask);
|
||||
$worksheet->setCellValue('D' . $row, $invoice->net);
|
||||
$worksheet->getCell('E' . $row)->getStyle()->getNumberFormat()->setFormatCode($this->currencyMask);
|
||||
$worksheet->setCellValue('E' . $row, $invoice->tax);
|
||||
$worksheet->getCell('F' . $row)->getStyle()->getNumberFormat()->setFormatCode($this->currencyMask);
|
||||
$worksheet->setCellValue('F' . $row, $invoice->gross);
|
||||
$worksheet->setCellValue('G' . $row, Date::PHPToExcel(\DateTime::createFromFormat('!Y-m-d', $invoice->issue_date)));
|
||||
$worksheet->getCell('G' . $row)->getStyle()->getNumberFormat()->setFormatCode('dd.mm.yyyy');
|
||||
if (is_null($invoice->pay_date)) {
|
||||
$worksheet->setCellValue('H' . $row, '');
|
||||
} else {
|
||||
$worksheet->setCellValue('H' . $row, Date::PHPToExcel(\DateTime::createFromFormat('!Y-m-d', $invoice->pay_date)));
|
||||
}
|
||||
$worksheet->getCell('H' . $row)->getStyle()->getNumberFormat()->setFormatCode('dd.mm.yyyy');
|
||||
$row++;
|
||||
}
|
||||
|
||||
$worksheet->getStyle('A' . $row - 1 . ':H' . $row - 1)->getBorders()->getBottom()->applyFromArray(['borderStyle' => Border::BORDER_DOUBLE]);
|
||||
|
||||
foreach ($net as $tax_value => $amount) {
|
||||
$worksheet->getStyle('A' . $row . ':G' . $row)->getFont()->setBold(true);
|
||||
$worksheet->getStyle('D' . $row . ':G' . $row)->getAlignment()->applyFromArray(['horizontal' => Alignment::HORIZONTAL_RIGHT]);
|
||||
$worksheet->setCellValue('B' . $row, __('invoice.Sails from vat', ['tax' => \Illuminate\Support\Number::percentage($tax_value)]));
|
||||
$worksheet->getCell('D' . $row)->getStyle()->getNumberFormat()->setFormatCode($this->currencyMask);
|
||||
$worksheet->setCellValue('D' . $row, $amount);
|
||||
$worksheet->getCell('E' . $row)->getStyle()->getNumberFormat()->setFormatCode($this->currencyMask);
|
||||
$worksheet->setCellValue('E' . $row, $tax[$tax_value]);
|
||||
$worksheet->getCell('F' . $row)->getStyle()->getNumberFormat()->setFormatCode($this->currencyMask);
|
||||
$worksheet->setCellValue('F' . $row, $gross[$tax_value]);
|
||||
|
||||
$row++;
|
||||
}
|
||||
|
||||
foreach ($worksheet->getColumnIterator() as $column) {
|
||||
$worksheet->getColumnDimension($column->getColumnIndex())->setAutoSize(true);
|
||||
}
|
||||
|
||||
$this->writer = new Xlsx($spreadsheet);
|
||||
}
|
||||
}
|
||||
|
||||
180
app/Http/Controllers/Api/IncomingController.php
Normal file
180
app/Http/Controllers/Api/IncomingController.php
Normal file
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Incoming;
|
||||
use App\Models\Incomingitem;
|
||||
use App\Models\Incomingtax;
|
||||
use App\Models\Supplier;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class IncomingController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index($from = null, $end = null): JsonResponse
|
||||
{
|
||||
return response()->json(Incoming::whereBetween('issue_date', [$from, $end])->with(['supplier'])->orderBy('issue_date', 'desc')->get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Process an upload file for the resource.
|
||||
*/
|
||||
public function upload(Request $request): JsonResponse
|
||||
{
|
||||
$xmlString = base64_decode(preg_replace('#^data:text/\w+;base64,#i', '', $request->uploadFile));
|
||||
$reader = new \App\XmlReader($xmlString);
|
||||
$supplierData = $reader->supplier();
|
||||
$incomingData = $reader->incoming();
|
||||
$itemsData = $reader->items();
|
||||
$taxesData = $reader->taxes();
|
||||
|
||||
$supplier = Supplier::where('email', '=', $supplierData['email'])->firstOrNew($supplierData);
|
||||
$supplier->save();
|
||||
|
||||
$incomingData['supplier_id'] = $supplier->id;
|
||||
|
||||
$incoming = new Incoming($incomingData);
|
||||
$incoming->save();
|
||||
|
||||
foreach ($itemsData as $item) {
|
||||
$item['incoming_id'] = $incoming->id;
|
||||
$incomingItem = new Incomingitem($item);
|
||||
$incomingItem->save();
|
||||
}
|
||||
|
||||
foreach ($taxesData as $tax) {
|
||||
$tax['incoming_id'] = $incoming->id;
|
||||
$incomingTax = new Incomingtax($tax);
|
||||
$incomingTax->save();
|
||||
}
|
||||
|
||||
return response()->json($incoming);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$supplierData = $request->validate([
|
||||
'supplier.name' => 'required|string',
|
||||
'supplier.registration_name' => 'nullable|string',
|
||||
'supplier.email' => 'nullable|email',
|
||||
'supplier.address' => 'nullable|string',
|
||||
'supplier.city' => 'nullable|string',
|
||||
'supplier.zip' => 'nullable|string',
|
||||
'supplier.country_code' => 'nullable|string',
|
||||
'supplier.tax_fc' => 'nullable|string',
|
||||
'supplier.tax_vat' => 'nullable|string',
|
||||
'supplier.contact_name' => 'nullable|string',
|
||||
'supplier.contact_phone' => 'nullable|string',
|
||||
'supplier.contact_email' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$supplier = Supplier::where('email', '=', $supplierData['supplier']['email'])->firstOrNew($supplierData['supplier']);
|
||||
$supplier->save();
|
||||
|
||||
$incomingData = $request->validate([
|
||||
'incoming.invoice_number' => 'required|string',
|
||||
'incoming.issue_date' => 'required|date',
|
||||
'incoming.due_date' => 'nullable|date',
|
||||
'incoming.invoice_type_code' => 'required|string',
|
||||
'incoming.currency_code' => 'required|string',
|
||||
'incoming.net' => 'required|numeric',
|
||||
'incoming.gross' => 'required|numeric',
|
||||
'incoming.tax' => 'required|numeric',
|
||||
'incoming.pay_date' => 'nullable|date',
|
||||
'incoming.pay_name' => 'nullable|string',
|
||||
'incoming.pay_bic' => 'nullable|string',
|
||||
'incoming.pay_iban' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$incomingData['incoming']['supplier_id'] = $supplier->id;
|
||||
$incoming = new Incoming($incomingData['incoming']);
|
||||
$incoming->save();
|
||||
|
||||
$taxesData = $request->validate([
|
||||
'taxes.*.taxable_amount' => 'required|numeric',
|
||||
'taxes.*.amount' => 'required|numeric',
|
||||
'taxes.*.percentage' => 'required|numeric',
|
||||
'taxes.*.currency' => 'required|string',
|
||||
]);
|
||||
|
||||
$incoming->taxes()->createMany($taxesData['taxes']);
|
||||
|
||||
return response()->json($supplierData['supplier']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Incoming $incoming): JsonResponse
|
||||
{
|
||||
$incomingData = $request->validate([
|
||||
'incoming.invoice_number' => 'required|string',
|
||||
'incoming.issue_date' => 'required|date',
|
||||
'incoming.due_date' => 'nullable|date',
|
||||
'incoming.invoice_type_code' => 'required|string',
|
||||
'incoming.currency_code' => 'required|string',
|
||||
'incoming.net' => 'required|numeric',
|
||||
'incoming.gross' => 'required|numeric',
|
||||
'incoming.tax' => 'required|numeric',
|
||||
'incoming.pay_date' => 'nullable|date',
|
||||
'incoming.pay_name' => 'nullable|string',
|
||||
'incoming.pay_bic' => 'nullable|string',
|
||||
'incoming.pay_iban' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$incoming->update($incomingData['incoming']);
|
||||
|
||||
$supplierData = $request->validate([
|
||||
'supplier.name' => 'required|string',
|
||||
'supplier.registration_name' => 'nullable|string',
|
||||
'supplier.email' => 'nullable|email',
|
||||
'supplier.address' => 'nullable|string',
|
||||
'supplier.city' => 'nullable|string',
|
||||
'supplier.zip' => 'nullable|string',
|
||||
'supplier.country_code' => 'nullable|string',
|
||||
'supplier.tax_fc' => 'nullable|string',
|
||||
'supplier.tax_vat' => 'nullable|string',
|
||||
'supplier.contact_name' => 'nullable|string',
|
||||
'supplier.contact_phone' => 'nullable|string',
|
||||
'supplier.contact_email' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$incoming->supplier()->update($supplierData['supplier']);
|
||||
|
||||
$itemsData = $request->validate([
|
||||
'items.*.name' => 'required|string',
|
||||
'items.*.article_number' => 'nullable|string',
|
||||
'items.*.description' => 'nullable|string',
|
||||
'items.*.amount' => 'required|numeric',
|
||||
'items.*.discount' => 'nullable|numeric',
|
||||
'items.*.tax' => 'required|numeric',
|
||||
'items.*.price' => 'required|numeric',
|
||||
'items.*.total' => 'required|numeric',
|
||||
]);
|
||||
|
||||
$incoming->items()->delete();
|
||||
|
||||
if (!empty($itemsData)) {
|
||||
$incoming->items()->createMany($itemsData['items']);
|
||||
}
|
||||
|
||||
$taxesData = $request->validate([
|
||||
'taxes.*.taxable_amount' => 'required|numeric',
|
||||
'taxes.*.amount' => 'required|numeric',
|
||||
'taxes.*.percentage' => 'required|numeric',
|
||||
'taxes.*.currency' => 'required|string',
|
||||
]);
|
||||
|
||||
$incoming->taxes()->delete();
|
||||
$incoming->taxes()->createMany($taxesData['taxes']);
|
||||
|
||||
return response()->json($incoming);
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,9 @@ class InvoiceController extends Controller
|
||||
return response()->json(Invoice::whereBetween('created_at', [$from, $end])->with(['address', 'customer'])->orderBy('created_at', 'desc')->get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource that have a status "sent".
|
||||
*/
|
||||
public function open(): JsonResponse
|
||||
{
|
||||
return response()->json(Invoice::where('status', '=', 'sent')->orderBy('created_at', 'desc')->with(['customer', 'address', 'payments'])->get());
|
||||
@@ -49,14 +52,6 @@ class InvoiceController extends Controller
|
||||
return response()->json($invoice);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Invoice $invoice)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
@@ -76,6 +71,9 @@ class InvoiceController extends Controller
|
||||
return response()->json($invoice);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource's status in storage.
|
||||
*/
|
||||
public function state(Request $request, Invoice $invoice): JsonResponse
|
||||
{
|
||||
$invoiceData = $request->validate([
|
||||
|
||||
Reference in New Issue
Block a user