Generate bulk purchase order

This commit is contained in:
ahmedsophyudden
2022-07-04 09:14:16 +08:00
parent 65ae9d87ee
commit cf852eb120
6 changed files with 321 additions and 1 deletions
@@ -0,0 +1,103 @@
<?php
namespace App\Classes\Modules\Transactions\ControllersLogic;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Meneses\LaravelMpdf\Facades\LaravelMpdf;
use App\Classes\ValueObjects\Constants\DocumentType;
use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Transactions\Services\ListsGroups;
use App\Classes\Modules\Transactions\Services\FetchesGroup;
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
use App\Classes\Modules\Documents\Services\CreatesDocument;
use App\Classes\Modules\Documents\Services\CreatesFiles;
use App\Classes\Modules\Transactions\Processors\CreateInvoiceDocumentProcessor;
class CreateBulkPurchaseOrderDocumentLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification(): array
{
return [
'title' => 'Generate Bulk Purchase Order',
'message' => 'You have successfully generated bulk purchase order'
];
}
/** @var ListsGroups */
private $listsGroups;
/** @var FetchesGroup */
private $fetchesGroup;
/** @var FetchesCompany */
private $fetchesCompany;
/** @var CreatesDocument */
private $createsDocument;
/** @var CreatesFiles */
private $createsFile;
/**
* CreateBulkPurchaseOrderTransactionLogic constructor.
* @param ListsGroups $listsGroups
* @param FetchesGroup $fetchesGroup
* @param FetchesCompany $fetchesCompany
* @param CreatesDocument $createsDocument
* @param CreatesFiles $createsFile
*/
public function __construct(ListsGroups $listsGroups, FetchesGroup $fetchesGroup, FetchesCompany $fetchesCompany, CreatesDocument $createsDocument, CreatesFiles $createsFile)
{
$this->listsGroups = $listsGroups;
$this->fetchesGroup = $fetchesGroup;
$this->fetchesCompany = $fetchesCompany;
$this->createsDocument = $createsDocument;
$this->createsFile = $createsFile;
}
/**
* @param Request $request
* @return JsonResponse
* @throws \App\Classes\Exceptions\MalformedRequestException
*/
public function logic(Request $request): JsonResponse
{
$group = $this->fetchesGroup->execute(['id' => $request->route('id')]);
foreach ($group->transactions as $transaction) {
if ($transaction->owner()->owner()->transactions()->where('type', TransactionType::PURCHASE_ORDER)->where('status', '!=', ApprovalStatus::APPROVED)->exists()) {
throw new MalformedRequestException('You can\'t generate bulk purchased order if there in uncomplete transactions');
}
}
$supplier = $this->fetchesCompany->execute(['id' => $group->receiver]);
$document_type = DocumentType::BULK_PURCHASE_ORDER;
$lowercaseDocumentType = strtolower($document_type);
$order_pdf = LaravelMpdf::loadView('pages.pdfs.' . $lowercaseDocumentType, ['group' => $group, 'supplier' => $supplier]);
$document_object = new DocumentObject(
$document_type,
[chunk_split('data:application/pdf;base64,' . base64_encode($order_pdf->output()))],
'',
ApprovalStatus::COMPLETED,
$lowercaseDocumentType . 's'
);
$document = $this->createsDocument->execute($group, $document_object);
$this->createsFile->execute($document, $document_object);
return $this->response([]);
}
}
@@ -22,4 +22,5 @@ final class DocumentType {
public const DELIVER_ORDER = 'DELIVER_ORDER';
public const INVOICE = 'INVOICE';
public const SUPPLIER_DELIVER_ORDER = 'SUPPLIER_DELIVER_ORDER';
public const BULK_PURCHASE_ORDER = 'BULK_PURCHASE_ORDER';
}
@@ -0,0 +1,21 @@
<?php
namespace App\Http\Controllers\Transactions;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Classes\Modules\Transactions\ControllersLogic\CreateBulkPurchaseOrderDocumentLogic;
class CreateBulkPurchaseOrderDocumentController
{
/**
* @param Request $request
* @param CreateBulkPurchaseOrderDocumentLogic $logic
* @return JsonResponse
*/
public function create(Request $request, CreateBulkPurchaseOrderDocumentLogic $logic) : JsonResponse {
return $logic->execute($request);
}
}
+14 -1
View File
@@ -3,15 +3,28 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Classes\General\Interfaces\Documentable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
class Group extends Model
class Group extends Model implements Documentable
{
use HasRelationships;
public function transactions()
{
return $this->belongsToMany(Transaction::class, GroupTransaction::class);
}
/**
* @return MorphMany
*/
public function documents(): morphMany
{
return $this->morphMany(Document::class, 'owner');
}
/**
* @return BelongsTo
*/
@@ -0,0 +1,181 @@
@extends('layouts.base_pdf')
@section('inner_content')
<br>
<htmlpageheader name="page-header">
<br><br>
<div class="separator"><strong><i>{{ $group->transactions[0]->bill_no }}</i></strong></div>
</htmlpageheader>
<br>
<table>
<tr>
<td class="title">
<strong>Purchase Order</strong>
</td>
<td class="document-detail">
PO#: {{ $group->transactions[0]->bill_no }} <br>
Ref#: {{ $group->transactions[0]->owner()->first()->owner()->first()->marking }} <br>
Date: {{ $group->transactions[0]->owner()->first()->owner()->first()->created_at }}
</td>
</tr>
</table>
<br>
<table class="buyer-seller">
<tr>
<td width="50%" class="top">
<span class="buyer-seller-title">
Buyer
</span>
<br>
<div class="buyer-company">
Marking#: {{ $group->transactions[0]->owner()->first()->owner()->first()->marking }}
</div>
<span class="buyer-company">
{{ $supplier->name }}
</span>
<span class="reg">
{{-- {{ $supplier }} --}}
</span>
<br>
<span class="address">
@php
$addresses = $supplier->addresses()->where('billing', '=', true)->first();
@endphp
{{ $addresses->street_one }}
{{ $addresses->street_two }}
{{ $addresses->state()->first()->name }}
{{ $addresses->district()->first()->name }}
</span>
<br>
<span class="contact-no">
Phone: {{ $supplier->contacts()->first()->phone }}
</span>
</td>
<td width="50%" class="top">
<span class="buyer-seller-title">
Seller
</span>
<br>
<span class="buyer-company">
CIEF Worldwide Sdn Bhd (1134596-M)
</span>
<div class="address">
Malaysia Global Innovation & CreativityCentre, Level 1 CWS, Block 3730, PersiaranAPEC 63000 Cyberjaya
</div>
<div class="contact-no">
Phone: 0182909252
</div>
</td>
</tr>
</table>
<br>
<table class="line-table" style="overflow: wrap" autosize="1">
<thead>
<tr>
<th width="5%">No</th>
<th class="stock-code" width="10%">Stock Code</th>
<th class="description">Description</th>
<th width="10%">Quantity</th>
<th width="15%">Unit Price (RM)</th>
<th width="10%">Total Amount<br>(RM)</th>
</tr>
</thead>
<tbody>
@php
$subtotal = 0;
@endphp
@foreach($group->transactions as $po_order_transaction)
@foreach ($po_order_transaction->owner()->first()->transactionDetails as $key => $transaction_detail)
<tr>
<td width="5%" class="center top">{{ $key + 1 }}</td>
<td class="stock-code top" width="10%">{{ $transaction_detail->product_code }}</td>
<td class="description">{{ $transaction_detail->product_name }}</td>
<td width="10%" class="center top">{{ $transaction_detail->quantity }}</td>
<td width="15%" class="center top">
@if($po_order_transaction->owner()->first()->booking()->first()->fix_currency_id !== 1)
{{ number_format( (1/$group->currency_rate) * $transaction_detail->price, 2) }}
@else
{{ number_format($transaction_detail->price, 2) }}
@endif
</td>
<td width="20%" class="right top">
@if($po_order_transaction->owner()->first()->booking()->first()->fix_currency_id !== 1)
{{ number_format((float)number_format( (1/$group->currency_rate) * $transaction_detail->price, 2,'.','')*$transaction_detail->quantity,2) }}
@php
$subtotal += number_format((float)number_format( (1/$group->currency_rate) * $transaction_detail->price, 2,'.','')*$transaction_detail->quantity,2,'.','');
@endphp
@else
{{ number_format((float)number_format($transaction_detail->price, 2,'.','')*$transaction_detail->quantity,2) }}
@php
$subtotal += number_format((float)number_format($transaction_detail->price, 2,'.','')*$transaction_detail->quantity,2,'.','');
@endphp
@endif
</td>
</tr>
@endforeach
@endforeach
</tbody>
<tfoot>
<tr class="subtotal">
<td colspan="4"></td>
<td class="right middle">Subtotal</td>
<td class="right middle">
{{ number_format($subtotal, 2) }}
</td>
</tr>
<tr class="billingcharges">
<td colspan="4"></td>
<td class="right">Service Charges</td>
<td class="right">
{{ number_format($group->service_charge, 2) }}
</td>
</tr>
<tr class="billingcharges">
<td colspan="4"></td>
<td class="right">Adjustment</td>
<td class="right">
@if($group->transactions[0]->owner()->first()->owner()->first()->fix_currency_id !== 1)
{{ number_format((float)number_format( (1/$group->currency_rate) * $group->amount, 2,'.','') - (float)number_format($subtotal, 2,'.',''),2) }}
@else
{{ number_format((float)number_format($group->amount, 2,'.','') - (float)number_format($subtotal, 2,'.',''),2) }}
@endif
</td>
</tr>
@if($group->tax > 0)
<tr class="billingcharges">
<td colspan="4"></td>
<td class="right">Tax</td>
<td class="right">{{ number_format($group->tax, 2) }}</td>
</tr>
@endif
<tr>
<td colspan="4"></td>
<td class="right middle">Total</td>
<td class="total right middle">
@if($group->transactions[0]->owner()->first()->owner()->first()->fix_currency_id !== 1)
{{ number_format( ((1/$group->currency_rate) * $group->amount) + $group->service_charge + $group->tax, 2) }}
@else
{{ number_format($group->amount + $group->service_charge + $group->tax, 2) }}
@endif
</td>
</tr>
</tfoot>
</table>
<htmlpagefooter name="page-footer">
<table width="100%">
<tr>
<td style="text-align: right; ">This is generated by computer. No signature required.</td>
<td style="text-align: right; ">Page {PAGENO} of {nbpg}</td>
</tr>
</table>
</htmlpagefooter>
@endsection
+1
View File
@@ -29,5 +29,6 @@ Route::group(['prefix' => 'transactions', 'namespace' => 'Transactions', 'as' =>
Route::get('/list', 'ListGroupsController@list')->name('list');
Route::delete('/{id}/delete', 'DeleteGroupController@delete')->name('delete');
Route::put('/{id}/update', 'UpdateGroupController@update')->name('update');
Route::get('/{id}/bulk/po', 'CreateBulkPurchaseOrderDocumentController@create')->name('bulk.po');
});
});