Skip to content

Commit

Permalink
add generating document number and enums for transaction type
Browse files Browse the repository at this point in the history
  • Loading branch information
glenmoreilagan committed May 25, 2024
1 parent e25c783 commit 8a15fc8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
13 changes: 13 additions & 0 deletions app/Enums/TransactionTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Enums;

use BenSampo\Enum\Enum;

final class TransactionTypeEnum extends Enum
{
const PURCHASES = 'PURCHASES';
const SALES = 'SALES';
}
23 changes: 18 additions & 5 deletions app/Http/Controllers/Api/PurchaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@
use Illuminate\Support\Facades\Log;
use League\CommonMark\Node\Block\Document;

use App\Traits\GenerateDocumentNumber;

use App\Enums\TransactionTypeEnum;

class PurchaseController extends Controller
{
use GenerateDocumentNumber;

public function index()
{
$document_numbers = DocumentNumber::with('purchases')
Expand All @@ -36,11 +42,11 @@ public function store(Request $request)
try {
$document = DocumentNumber::create([
'uuid' => Str::uuid(),
'document_no' => Str::ulid(),
'document_no' => $request->document_no,
'description1' => $request->description1 ?? '',
'description2' => $request->description2 ?? '',
'transaction_date' => Carbon::parse($request->transaction_date),
'transaction_type' => 'PURCHASE',
'transaction_type' => TransactionTypeEnum::PURCHASES,
]);
DB::commit();
} catch (\Throwable $th) {
Expand All @@ -57,7 +63,7 @@ public function show(string $uuid)

abort_if(!$document, '404', 'Document not found.');

$purchases = $this->getPurhcasesByDocumentId($document->id);
$purchases = $this->getPurchasesByDocumentId($document->id);

return response()->json(['document' => $document, 'purchases' => $purchases]);
}
Expand Down Expand Up @@ -123,12 +129,12 @@ public function addProducts(Request $request)
}


$purchases = $this->getPurhcasesByDocumentId($document_id);
$purchases = $this->getPurchasesByDocumentId($document_id);

return response()->json($purchases);
}

private function getPurhcasesByDocumentId($document_id)
private function getPurchasesByDocumentId($document_id)
{
return Purchase::where('purchases.document_id', $document_id)
->join(App(Product::class)->getTable() . ' as product', 'product.id', '=', 'purchases.product_id')
Expand All @@ -146,4 +152,11 @@ private function getPurhcasesByDocumentId($document_id)
])
->get();
}

public function generateDocumentNumber()
{
$document_number = $this->getDocumentNumber();

return response()->json($document_number);
}
}
4 changes: 3 additions & 1 deletion app/Http/Controllers/Api/SaleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;

use App\Enums\TransactionTypeEnum;

class SaleController extends Controller
{
public function store(Request $request)
Expand All @@ -21,7 +23,7 @@ public function store(Request $request)
'uuid' => Str::uuid(),
'document_no' => Str::ulid(),
'transaction_date' => Carbon::now(),
'transaction_type' => 'SALES',
'transaction_type' => TransactionTypeEnum::SALES,
]);

foreach ($payload as $key => $row) {
Expand Down
14 changes: 14 additions & 0 deletions app/Traits/GenerateDocumentNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Traits;

use Illuminate\Support\Str;

trait GenerateDocumentNumber
{

public function getDocumentNumber()
{
return Str::ulid();
}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Route::prefix('purchases')->group(function () {
Route::put('/update-quantity/{id}', [PurchaseController::class, 'updateQuantity']);
Route::post('/add-product', [PurchaseController::class, 'addProducts']);
Route::get('/generate-document-number', [PurchaseController::class, 'generateDocumentNumber']);
});
Route::apiResource('/purchases', PurchaseController::class);

Expand Down

0 comments on commit 8a15fc8

Please sign in to comment.