177 lines
5.8 KiB
PHP
177 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Http;
|
|
use App\Komoditi;
|
|
use PDF;
|
|
|
|
class KomuditiController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
}
|
|
|
|
public function index(){
|
|
$auth_user = Auth::user();
|
|
$api_url = config('api.url');
|
|
$user_name = $auth_user['name'];
|
|
$komoditi = Komoditi::first();
|
|
$min_komoditi = $komoditi['min_komoditi'];
|
|
return view('operasi.komuditi.index',compact('user_name','api_url','min_komoditi'));
|
|
}
|
|
|
|
public function cetakRekodKomoditi(){
|
|
$rekod = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/komuditi/rekodkomuditi')->json();
|
|
|
|
return view('print.laporan_komoditi',compact('rekod'));
|
|
}
|
|
|
|
public function rekodKomoditi(Request $request){
|
|
$auth_user = Auth::user();
|
|
$api_url = config('api.url');
|
|
$user_name = $auth_user['name'];
|
|
return view('operasi.komuditi.rekod_transaksi',compact('user_name','api_url'));
|
|
}
|
|
|
|
public function printRekodTransaksi(Request $request){
|
|
$tarikh_rekod = $request->tarikh_print;
|
|
$rekod_print = Http::withOptions(['verify' => config('app.api_verify')])->get(config('api.url') . '/api/komoditi/rekod/transaksi',[
|
|
'tarikh' => $request->tarikh_print
|
|
])->json();
|
|
|
|
return view('print.laporan_transaksi_komoditi',compact('rekod_print','tarikh_rekod'));
|
|
}
|
|
|
|
public function komoditiUpdate(Request $request){
|
|
|
|
$komoditi = Komoditi::first();
|
|
|
|
$komoditi->min_komoditi = $request->komoditi_limit;
|
|
$komoditi->save();
|
|
|
|
return redirect()->route('komuditi');
|
|
}
|
|
|
|
/**
|
|
* Dummy BSAS eCert layout preview (2-page A4).
|
|
*/
|
|
public function previewBsasEcert()
|
|
{
|
|
$cert = [
|
|
'ecert_no' => 'CPO13OCT26-0000136-025',
|
|
'seller' => 'KOPERASI PERMODALAN KELANTAN BERHAD',
|
|
'buyer' => 'WAN MOHAMAD AIMAN',
|
|
'bid_no' => '1',
|
|
'report_time' => '13:39:23.751',
|
|
'report_date' => '13 OCTOBER 2026',
|
|
'value_date' => '13 OCTOBER 2026',
|
|
'product' => 'CPO-MSIA-09',
|
|
'unit' => 'Tonnage',
|
|
'volume' => '.72562358',
|
|
'currency' => 'MYR',
|
|
'price' => '5,256.72',
|
|
'price_myr' => '5,256.72',
|
|
'value' => '3,200.00',
|
|
'txn_type' => '-',
|
|
'murabaha' => '3,814.40',
|
|
'lines' => [
|
|
[
|
|
'supplier' => 'POA RESOURCES BERHAD',
|
|
'volume' => '.72562358',
|
|
],
|
|
],
|
|
];
|
|
|
|
return view('print.bsas_ecert', [
|
|
'cert' => $cert,
|
|
'isDummy' => true,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Live BSAS eCert from stored bsas_orders.ecert_response.
|
|
*/
|
|
public function showBsasEcert($uuid)
|
|
{
|
|
$response = Http::withOptions(['verify' => config('app.api_verify')])
|
|
->get(config('api.url') . '/api/komuditi/bsas/order/' . urlencode($uuid) . '/ecert');
|
|
|
|
if ($response->status() === 404) {
|
|
abort(404, $response->json('message') ?: 'eCert tidak dijumpai.');
|
|
}
|
|
|
|
if (!$response->successful()) {
|
|
abort(502, 'Gagal memuatkan eCert BSAS.');
|
|
}
|
|
|
|
$payload = $response->json();
|
|
$ecert = $payload['ecert'] ?? null;
|
|
|
|
if (!is_array($ecert) || empty($ecert)) {
|
|
abort(404, 'eCert belum tersedia untuk order ini.');
|
|
}
|
|
|
|
return view('print.bsas_ecert', [
|
|
'cert' => $this->mapBsasEcertToView($ecert, $payload),
|
|
'isDummy' => false,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Map stored otcXML / eCert JSON into print view fields.
|
|
*/
|
|
protected function mapBsasEcertToView(array $ecert, array $orderMeta = []): array
|
|
{
|
|
$reporting = trim((string) ($ecert['REPORTINGTIMEDATE'] ?? ''));
|
|
$reportTime = '-';
|
|
$reportDate = '-';
|
|
if ($reporting !== '') {
|
|
$parts = explode(' ', $reporting, 2);
|
|
$reportTime = $parts[0] ?? '-';
|
|
$reportDate = isset($parts[1]) ? strtoupper($parts[1]) : '-';
|
|
}
|
|
|
|
$lines = [];
|
|
foreach (($ecert['LINE'] ?? []) as $line) {
|
|
if (!is_array($line)) {
|
|
continue;
|
|
}
|
|
$lines[] = [
|
|
'supplier' => $line['SUPPLIER'] ?? '-',
|
|
'volume' => $line['VOLUME'] ?? ($ecert['PVOLUME'] ?? '-'),
|
|
];
|
|
}
|
|
|
|
if (empty($lines)) {
|
|
$lines[] = [
|
|
'supplier' => '-',
|
|
'volume' => $ecert['PVOLUME'] ?? '-',
|
|
];
|
|
}
|
|
|
|
return [
|
|
'ecert_no' => $ecert['ECERTNO'] ?? ($orderMeta['e_cert_no'] ?? '-'),
|
|
'seller' => $ecert['SELLER'] ?? 'KOPERASI PERMODALAN KELANTAN BERHAD',
|
|
'buyer' => $ecert['BUYER'] ?? ($orderMeta['otc_counter_party'] ?? '-'),
|
|
'bid_no' => $orderMeta['serial_number'] ?? '1',
|
|
'report_time' => $reportTime,
|
|
'report_date' => $reportDate,
|
|
'value_date' => isset($ecert['VALUEDATE']) ? strtoupper((string) $ecert['VALUEDATE']) : '-',
|
|
'product' => $ecert['PNAME'] ?? ($orderMeta['product_code'] ?? '-'),
|
|
'unit' => 'Tonnage',
|
|
'volume' => $ecert['PVOLUME'] ?? '-',
|
|
'currency' => $ecert['CURRENCY'] ?? 'MYR',
|
|
'price' => $ecert['PRICE'] ?? '-',
|
|
'price_myr' => $ecert['PRICE_MYR_EQUIVALENT'] ?? ($ecert['PRICE'] ?? '-'),
|
|
'value' => $ecert['TOTALVALUE'] ?? '-',
|
|
'txn_type' => '-',
|
|
'murabaha' => $ecert['MURABAHAVALUE'] ?? ($orderMeta['otc_murabaha_value'] ?? '-'),
|
|
'lines' => $lines,
|
|
];
|
|
}
|
|
}
|