227 lines
10 KiB
PHP
227 lines
10 KiB
PHP
<?php
|
|
|
|
/*******************************************************************
|
|
|
|
PHP Class : /Controller/Report/Beli/BeliController
|
|
Description : Laporan Pembelian
|
|
Created By : Siau Pheng ([email protected]).
|
|
Created Date : April 12th, 2011.
|
|
Last Modified : September 30th, 2026.
|
|
|
|
(c) 3FONIA Software; WWW.3FONIA.COM.
|
|
|
|
*******************************************************************/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\Report\Beli;
|
|
|
|
use flight\Engine;
|
|
use flight\database\SimplePdo;
|
|
use flight\Session;
|
|
use Webapp\Data\Xlsx;
|
|
use Webapp\Printer\PrintHTML;
|
|
use Webapp\Web\Form;
|
|
use Webapp\Web\Param;
|
|
|
|
class BeliController {
|
|
|
|
private int $id_cab;
|
|
private string $reg_name;
|
|
protected Engine $app;
|
|
protected SimplePdo $db;
|
|
|
|
public function __construct(Engine $app, SimplePdo $db, Session $session) {
|
|
$this->app = $app;
|
|
$this->db = $db;
|
|
$this->id_cab = Param::Int("CabID", 0);
|
|
$this->reg_name = $session->get('WEB_APP')['REG_NAME'];
|
|
}
|
|
|
|
private function prepare() {
|
|
$apre = [
|
|
'tipe' => Param::Get("Tipe", "R"), // R or D
|
|
'id_sup' => Param::Int("SupID", 0),
|
|
'id_grup' => Param::Int("Grup_InvID", 0),
|
|
'tgl_from' => Param::Get("Tgl_From"),
|
|
'tgl_to' => Param::Get("Tgl_To"),
|
|
];
|
|
|
|
$apre['nama_sup'] = ($apre['id_sup'] > 0) ? ($this->db->fetchField("SELECT CONCAT(Nama_Sup,' (',Kode_Sup,')') Nama FROM para_supplier WHERE SupID=?", [$apre['id_sup']]) ?? '') : '';
|
|
$apre['nama_grup'] = ($apre['id_grup'] > 0) ? ($this->db->fetchField("SELECT Nama_Grup FROM inv_produk_grup WHERE Grup_InvID=?", [$apre['id_grup']]) ?? '') : '';
|
|
|
|
// generate report header
|
|
$apre['periode'] = $apre['tgl_from'] . ($apre['tgl_from'] !== $apre['tgl_to'] ? ' s/d '.$apre['tgl_to'] : '');
|
|
|
|
$tsql = ''; $asql = [tgl_sql2str($apre['tgl_from']), tgl_sql2str($apre['tgl_to'])];
|
|
if ($this->id_cab > 0) {
|
|
$tsql .= " AND CabID=?";
|
|
array_push($asql, $this->id_cab);
|
|
}
|
|
if ($apre['id_sup'] > 0) {
|
|
$tsql .= " AND SupID=?";
|
|
array_push($asql, $apre['id_sup']);
|
|
}
|
|
//id_grup ?
|
|
|
|
if ($apre['tipe'] === "R") {
|
|
$apre['query'] = $this->db->runQuery("SELECT S.Nama_Sup,SUM(B.Real_Beli+B.Real_Diskon) Total_Beli,SUM(B.Real_Diskon) Total_Diskon,SUM(B.Real_Beli) Total FROM (SELECT * FROM inv_beli WHERE Data_Beli=1 AND (Tgl_Beli BETWEEN ? AND ?)".$tsql.") B LEFT JOIN para_supplier S ON (B.SupID=S.SupID) GROUP BY B.SupID ORDER BY S.Nama_Sup", $asql);
|
|
} else {
|
|
$apre['query'] = $this->db->runQuery("SELECT B.*,S.Nama_Sup,B.Total_Beli-B.Total_Diskon Total FROM (SELECT * FROM inv_beli WHERE Data_Beli=1 AND (Tgl_Beli BETWEEN ? AND ?)".$tsql.") B LEFT JOIN para_supplier S ON (B.SupID=S.SupID) ORDER BY B.No_Beli", $asql);
|
|
}
|
|
return $apre;
|
|
}
|
|
|
|
public function print() {
|
|
$apre = $this->prepare();
|
|
|
|
$pre = new PrintHTML($this->app);
|
|
$pre->add_title([strtoupper($this->reg_name), ($apre['tipe']==="R"?"REKAP":"DETAIL")." PEMBELIAN", "Periode : ".$apre['periode']]);
|
|
|
|
if ($apre['tipe'] === "R") {
|
|
$pre->add_field("No.",10,"C");
|
|
$pre->add_field("Nama Supplier",50,"L");
|
|
$pre->add_field("Sub. Total",20,"R","rupiah");
|
|
$pre->add_field("Diskon",20,"R","rupiah");
|
|
$pre->add_field("Total",20,"R","rupiah");
|
|
|
|
if ($apre['id_sup'] > 0) $pre->add_sub_title("Nama Supplier : ".$apre['nama_sup']);
|
|
if ($apre['id_grup'] > 0) $pre->add_sub_title("Grup Barang : ".$apre['nama_grup']);
|
|
|
|
$pre->show_titles();
|
|
$pre->show_headers();
|
|
|
|
$tot_beli = 0; $tot_diskon = 0; $nomor = 0;
|
|
while ($row = $apre['query']->fetch(SimplePdo::FETCH_ASSOC)) {
|
|
//if ($row['Total] === 0) continue;
|
|
$tot_beli += $row['Total_Beli'];
|
|
$tot_diskon += $row['Total_Diskon'];
|
|
$pre->add_row([ ++$nomor, $row['Nama_Sup'], $row['Total_Beli'], $row['Total_Diskon'], $row['Total'] ]);
|
|
}
|
|
$pre->add_span([ ["GRAND TOTAL",2,"R,B"],[$tot_beli,1,"R,B","rupiah"],[$tot_diskon,1,"R,B","rupiah"],[$tot_beli-$tot_diskon,1,"R,B","rupiah"] ]);
|
|
|
|
} else {
|
|
$pre->add_field("No. Nota",30,"C");
|
|
$pre->add_field("Tgl. Nota",30,"C", "tgl_sql2str");
|
|
$pre->add_field("No. Faktur",30,"L");
|
|
$pre->add_field("Nama Supplier",50,"L");
|
|
$pre->add_field("Sub. Total",20,"R","rupiah");
|
|
$pre->add_field("Diskon",20,"R","rupiah");
|
|
$pre->add_field("Total",30,"R","rupiah");
|
|
|
|
if ($apre['id_sup'] > 0) $pre->add_sub_title("Nama Supplier : ".$apre['nama_sup']);
|
|
if ($apre['id_grup'] > 0) $pre->add_sub_title("Grup Barang : ".$apre['nama_grup']);
|
|
|
|
$pre->show_titles();
|
|
$pre->show_headers();
|
|
|
|
$tot_beli = 0; $tot_diskon = 0;
|
|
while ($row = $apre['query']->fetch(SimplePdo::FETCH_ASSOC)) {
|
|
$tot_beli += $row['Total_Beli'];
|
|
$tot_diskon += $row['Total_Diskon'];
|
|
$pre->add_row([ $row['No_Beli'], $row['Tgl_Beli'], $row['No_Faktur'], $row['Nama_Sup'], $row['Total_Beli'], $row['Total_Diskon'], $row['Total'] ]);
|
|
}
|
|
$pre->add_span([ ["GRAND TOTAL",4,"R,B"],[$tot_beli,1,"R,B","rupiah"],[$tot_diskon,1,"R,B","rupiah"],[$tot_beli-$tot_diskon,1,"R,B","rupiah"] ]);
|
|
}
|
|
$pre->add_stamp(1, true);
|
|
$pre->show();
|
|
}
|
|
|
|
public function excel() {
|
|
$apre = $this->prepare();
|
|
$xls = new Xlsx($this->app);
|
|
$xls->set_meta([
|
|
'title' => "Data Pembelian",
|
|
'desc' => "Export Data Pembelian"
|
|
]);
|
|
$xls->add_title([ $this->reg_name, "DATA ".($apre['tipe']==="R"?"REKAP":"DETAIL")." PEMBELIAN", "Periode : ".$apre['periode'] ]);
|
|
|
|
if ($apre['tipe'] === "R") {
|
|
$xls->add_field("No.", 5, "@integer");
|
|
$xls->add_field("Nama Supplier", 25, "@text");
|
|
$xls->add_field("Sub. Total", 15, "#,##0.00");
|
|
$xls->add_field("Diskon", 15, "#,##0.00");
|
|
$xls->add_field("Total", 15, "#,##0.00");
|
|
$xls->add_sheet("".str_replace("s/d","-",$apre['periode']));
|
|
|
|
if ($apre['id_sup'] > 0) $xls->add_sub_title("Nama Supplier : ".$apre['nama_sup']);
|
|
if ($apre['id_grup'] > 0) $xls->add_sub_title("Grup Barang : ".$apre['nama_grup']);
|
|
|
|
$xls->show_titles();
|
|
$xls->show_headers();
|
|
|
|
$tot_beli = 0; $tot_diskon = 0; $nomor = 0;
|
|
while ($row = $apre['query']->fetch(SimplePdo::FETCH_ASSOC)) {
|
|
$tot_beli += $row['Total_Beli'];
|
|
$tot_diskon += $row['Total_Diskon'];
|
|
$xls->add_row([ ++$nomor, $row['Nama_Sup'], $row['Total_Beli'], $row['Total_Diskon'], $row['Total'] ]);
|
|
}
|
|
$xls->add_row_bold([ "", "GRAND TOTAL", $tot_beli, $tot_diskon, $tot_beli-$tot_diskon ]);
|
|
|
|
} else {
|
|
$xls->add_field("No. Nota", 15, "@");
|
|
$xls->add_field("Tgl. Nota", 12, "@date");
|
|
$xls->add_field("No. Faktur", 15, "@text");
|
|
$xls->add_field("Nama Supplier", 20, "@text");
|
|
$xls->add_field("Sub. Total", 15, "#,##0.00");
|
|
$xls->add_field("Diskon", 15, "#,##0.00");
|
|
$xls->add_field("Total", 15, "#,##0.00");
|
|
$xls->add_sheet("".str_replace("s/d","-",$apre['periode']));
|
|
|
|
if ($apre['id_sup'] > 0) $xls->add_sub_title("Nama Supplier : ".$apre['nama_sup']);
|
|
if ($apre['id_grup'] > 0) $xls->add_sub_title("Grup Barang : ".$apre['nama_grup']);
|
|
|
|
$xls->show_titles();
|
|
$xls->show_headers();
|
|
|
|
$tot_beli = 0; $tot_diskon = 0;
|
|
while ($row = $apre['query']->fetch(SimplePdo::FETCH_ASSOC)) {
|
|
$tot_beli += $row['Total_Beli'];
|
|
$tot_diskon += $row['Total_Diskon'];
|
|
$xls->add_row([ $row['No_Beli'], $row['Tgl_Beli'], $row['No_Faktur'], $row['Nama_Sup'], $row['Total_Beli'], $row['Total_Diskon'], $row['Total'] ]);
|
|
}
|
|
$xls->add_row_bold([ "", "", "", "GRAND TOTAL", $tot_beli, $tot_diskon, $tot_beli-$tot_diskon ]);
|
|
}
|
|
$xls->add_stamp(1, true);
|
|
$xls->download("Data Pembelian ".str_replace("s/d","-",$apre['periode']).".xlsx");
|
|
unset($xls);
|
|
}
|
|
|
|
public function index() {
|
|
$form = new Form($this->app);
|
|
$ttipe = $form->radio("_", "Tipe", "R", true, false, ["R"=>"Rekap","D"=>"Detail"]);
|
|
$tcab = $form->option_db("_", "CabID", $this->id_cab, true, false, "para_cabang", ["CabID","Nama_Cab"], "Aktif=1 ORDER BY CabID", [0,"SEMUA CABANG"]);
|
|
$tsup = $form->option_db("_", "SupID", 0, true, false, "para_supplier", ["SupID","Nama_Sup"], "Aktif=1 ORDER BY Nama_Sup", [0,"SEMUA SUPPLIER"]);
|
|
$tgrup = $form->option_db("_", "Grup_InvID", 0, true, false, "inv_produk_grup", ["Grup_InvID","Nama_Grup"], "Aktif=1 ORDER BY Nama_Grup", [0,"SEMUA GRUP"]);
|
|
$tfrom = $form->datebox("_", "Tgl_From", tgl_ymd(), true, false);
|
|
$tto = $form->datebox("_", "Tgl_To", tgl_ymd(), true, false);
|
|
$ajson['html'] = '
|
|
<div class="row mb-1">
|
|
<label class="col-sm-4 col-lg-3 col-form-label" for="Tipe">Tipe Laporan</label>
|
|
<div class="col">'.$ttipe.'</div>
|
|
</div>
|
|
<div class="row mb-1">
|
|
<label class="col-sm-4 col-lg-3 col-form-label" for="CabID">Nama Cabang</label>
|
|
<div class="col">'.$tcab.'</div>
|
|
</div>
|
|
<div class="row mb-1">
|
|
<label class="col-sm-4 col-lg-3 col-form-label" for="SupID">Nama Supplier</label>
|
|
<div class="col">'.$tsup.'</div>
|
|
</div>
|
|
<div class="row mb-1">
|
|
<label class="col-sm-4 col-lg-3 col-form-label" for="Grup_InvID">Grup Barang</label>
|
|
<div class="col">'.$tgrup.'</div>
|
|
</div>
|
|
<div class="row mb-1">
|
|
<label class="col-sm-4 col-lg-3 col-form-label" for="Tgl_From">Dari Tanggal</label>
|
|
<div class="col-sm-6 col-lg-5">'.$tfrom.'</div>
|
|
</div>
|
|
<div class="row mb-1">
|
|
<label class="col-sm-4 col-lg-3 col-form-label" for="Tgl_To">Sampai Tanggal</label>
|
|
<div class="col-sm-6 col-lg-5">'.$tto.'</div>
|
|
</div>';
|
|
$this->app->json($ajson);
|
|
}
|
|
|
|
}
|