191 lines
9.1 KiB
PHP
191 lines
9.1 KiB
PHP
<?php
|
|
|
|
/*******************************************************************
|
|
|
|
PHP Class : /Controller/Report/Rekap/MarginController
|
|
Description : Laporan Rekap Margin Harian
|
|
Created By : Siau Pheng ([email protected]).
|
|
Created Date : January 19th, 2013.
|
|
Last Modified : October 10th, 2026.
|
|
|
|
(c) 3FONIA Software; WWW.3FONIA.COM.
|
|
|
|
*******************************************************************/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Controller\Report\Rekap;
|
|
|
|
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 MarginController {
|
|
|
|
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 = [
|
|
'tgl_from' => Param::Get("Tgl_From"),
|
|
'tgl_to' => Param::Get("Tgl_To"),
|
|
];
|
|
|
|
// generate report header
|
|
$apre['periode'] = $apre['tgl_from'] . ($apre['tgl_from'] !== $apre['tgl_to'] ? ' s/d '.$apre['tgl_to'] : '');
|
|
|
|
$tsql = ''; $asql = [];
|
|
if ($this->id_cab > 0) {
|
|
$tsql .= " AND CabID=?";
|
|
array_push($asql, $this->id_cab);
|
|
}
|
|
|
|
$this->db->delete("rep_rekap_margin", "UserID=?", [$this->app->user()->id()]);
|
|
// insert jual dan terima pos
|
|
$this->db->runQuery("INSERT INTO rep_rekap_margin (Tanggal,Jual,Masuk,UserID) SELECT Tgl_Jual,SUM(Total_Jual-Total_Diskon) Jual, SUM(IF(POS=1,Total_Jual-Total_Diskon,0)) Masuk,? FROM inv_jual WHERE (Tgl_Jual BETWEEN ? AND ?)".$tsql." GROUP BY Tgl_Jual", array_merge([$this->app->user()->id(), tgl_sql2str($apre['tgl_from']), tgl_sql2str($apre['tgl_to'])], $asql));
|
|
// insert tgl sesuai range
|
|
$dtgl = $apre['tgl_from'];
|
|
while ($dtgl <= $apre['tgl_to']) {
|
|
$this->db->runQuery("INSERT IGNORE INTO rep_rekap_margin (Tanggal,UserID) VALUES (?,?)", [tgl_sql2str($dtgl), $this->app->user()->id()]);
|
|
$dtgl = tgl_operasi($dtgl, 1);
|
|
}
|
|
// insert beli
|
|
$arow = $this->db->fetchAll("SELECT Tgl_Beli, SUM(Real_Beli) Beli FROM inv_beli WHERE (Tgl_Beli BETWEEN ? AND ?)".$tsql." GROUP BY Tgl_Beli", array_merge([tgl_sql2str($apre['tgl_from']), tgl_sql2str($apre['tgl_to'])], $asql));
|
|
foreach ($arow as $row) {
|
|
$this->db->runQuery("UPDATE rep_rekap_margin SET Beli=? WHERE Tanggal=? AND UserID=?", [$row['Beli'], $row['Tgl_Beli'], $this->app->user()->id()]);
|
|
}
|
|
// insert keluar / lunas beli
|
|
$arow = $this->db->fetchAll("SELECT Tgl_Bayar, SUM(Total_Bayar-Total_Potong) Keluar FROM inv_beli_lunas WHERE (Tgl_Bayar BETWEEN ? AND ?)".$tsql." GROUP BY Tgl_Bayar", array_merge([tgl_sql2str($apre['tgl_from']), tgl_sql2str($apre['tgl_to'])], $asql));
|
|
foreach ($arow as $row) {
|
|
$this->db->runQuery("UPDATE rep_rekap_margin SET Keluar=? WHERE Tanggal=? AND UserID=?", [$row['Keluar'], $row['Tgl_Bayar'], $this->app->user()->id()]);
|
|
}
|
|
// insert modal
|
|
$arow = $this->db->fetchAll("SELECT Tgl_Ref, SUM(-Qty*Harga_Beli) Modal FROM inv_detail WHERE (Tgl_Ref BETWEEN ? AND ?) AND NotaID IN (5,6)".$tsql." GROUP BY Tgl_Ref", array_merge([tgl_sql2str($apre['tgl_from']), tgl_sql2str($apre['tgl_to'])], $asql));
|
|
foreach ($arow as $row) {
|
|
$this->db->runQuery("UPDATE rep_rekap_margin SET Modal=? WHERE Tanggal=? AND UserID=?", [$row['Modal'], $row['Tgl_Ref'], $this->app->user()->id()]);
|
|
}
|
|
// update margin dan persen
|
|
$this->db->runQuery("UPDATE rep_rekap_margin SET Margin=Jual-Modal,Persen=(Margin/Jual)*100 WHERE UserID=?", [$this->app->user()->id()]);
|
|
$apre['query'] = $this->db->runQuery("SELECT * FROM rep_rekap_margin WHERE UserID=? ORDER BY Tanggal ASC", [$this->app->user()->id()]);
|
|
return $apre;
|
|
}
|
|
|
|
public function print() {
|
|
$apre = $this->prepare();
|
|
|
|
$pre = new PrintHTML($this->app);
|
|
$pre->add_title([strtoupper($this->reg_name), "REKAP MARGIN HARIAN", "Periode : ".$apre['periode']]);
|
|
|
|
$pre->add_field("Hari",10,"C");
|
|
$pre->add_field("Tanggal",20,"C","sqltgl");
|
|
$pre->add_field("Penjualan",20,"R","rupiah");
|
|
$pre->add_field("Pembelian",20,"R","rupiah");
|
|
$pre->add_field("Modal",20,"R","rupiah");
|
|
$pre->add_field("Penerimaan",20,"R","rupiah");
|
|
$pre->add_field("Pengeluaran",20,"R","rupiah");
|
|
$pre->add_field("Margin",20,"R","rupiah");
|
|
$pre->add_field("%",10,"R","rupiah2");
|
|
|
|
$pre->show_titles();
|
|
$pre->show_headers();
|
|
|
|
$item = 0; $tot_j = 0; $tot_b = 0; $tot_p = 0; $tot_m = 0; $tot_k = 0; $tot_mar = 0; $tot_psn = 0;
|
|
while ($row = $apre['query']->fetch(SimplePdo::FETCH_ASSOC)) {
|
|
$item++;
|
|
$tot_j += $row['Jual'];
|
|
$tot_b += $row['Beli'];
|
|
$tot_m += $row['Masuk'];
|
|
$tot_k += $row['Keluar'];
|
|
$tot_p += $row['Modal'];
|
|
$tot_mar += $row['Margin'];
|
|
$pre->add_row([ tgl_hari(tgl_sql2str($row['Tanggal'])), $row['Tanggal'], $row['Jual'], $row['Beli'], $row['Modal'], $row['Masuk'], $row['Keluar'], $row['Margin'], $row['Persen'] ]);
|
|
}
|
|
if ($item > 0 && $tot_j > 0) {
|
|
$pre->add_span([ ["JUMLAH",2,"R,B"],[$tot_j,1,"R,B","rupiah"],[$tot_b,1,"R,B","rupiah"],[$tot_p,1,"R,B","rupiah"],[$tot_m,1,"R,B","rupiah"],[$tot_k,1,"R,B","rupiah"],[$tot_mar,1,"R,B","rupiah"],[($tot_j===0?0:($tot_mar/$tot_j)*100),1,"R,B","rupiah2"] ]);
|
|
$pre->add_span([ ["RATA-RATA",2,"R,B"],[$tot_j/$item,1,"R,B","rupiah"],[$tot_b/$item,1,"R,B","rupiah"],[$tot_p/$item,1,"R,B","rupiah"],[$tot_m/$item,1,"R,B","rupiah"],[$tot_k/$item,1,"R,B","rupiah"],[$tot_mar/$item,1,"R,B","rupiah"],[($tot_mar/$tot_j)*100/$item,1,"R,B","rupiah2"] ]);
|
|
}
|
|
|
|
$pre->add_stamp(1, true);
|
|
$pre->show();
|
|
}
|
|
|
|
public function excel() {
|
|
$apre = $this->prepare();
|
|
$xls = new Xlsx($this->app);
|
|
$xls->set_meta([
|
|
'title' => "Data Rekap Margin Harian",
|
|
'desc' => "Export Data Rekap Margin Harian"
|
|
]);
|
|
$xls->add_title([ $this->reg_name, "DATA REKAP MARGIN HARIAN", "Periode : ".$apre['periode'] ]);
|
|
|
|
$xls->add_field("Hari", 15, "@");
|
|
$xls->add_field("Tanggal", 12, "@date");
|
|
$xls->add_field("Penjualan", 15, "#,##0.00");
|
|
$xls->add_field("Pembelian", 15, "#,##0.00");
|
|
$xls->add_field("Modal", 15, "#,##0.00");
|
|
$xls->add_field("Penerimaan", 15, "#,##0.00");
|
|
$xls->add_field("Pengeluaran", 15, "#,##0.00");
|
|
$xls->add_field("Margin", 15, "#,##0.00");
|
|
$xls->add_field("%", 15, "#,##0.00");
|
|
$xls->add_sheet("".str_replace("s/d","-",$apre['periode']));
|
|
|
|
$xls->show_titles();
|
|
$xls->show_headers();
|
|
|
|
$item = 0; $tot_j = 0; $tot_b = 0; $tot_p = 0; $tot_m = 0; $tot_k = 0; $tot_mar = 0; $tot_psn = 0;
|
|
while ($row = $apre['query']->fetch(SimplePdo::FETCH_ASSOC)) {
|
|
$item++;
|
|
$tot_j += $row['Jual'];
|
|
$tot_b += $row['Beli'];
|
|
$tot_m += $row['Masuk'];
|
|
$tot_k += $row['Keluar'];
|
|
$tot_p += $row['Modal'];
|
|
$tot_mar += $row['Margin'];
|
|
$xls->add_row([ tgl_hari(tgl_sql2str($row['Tanggal'])), $row['Tanggal'], $row['Jual'], $row['Beli'], $row['Modal'], $row['Masuk'], $row['Keluar'], $row['Margin'], $row['Persen'] ]);
|
|
}
|
|
if ($item > 0 && $tot_j > 0) {
|
|
$xls->add_row_bold([ "", "JUMLAH", $tot_j, $tot_b, $tot_p, $tot_m, $tot_k, $tot_mar, ($tot_j===0?0:($tot_mar/$tot_j)*100) ]);
|
|
$xls->add_row_bold([ "", "RATA-RATA", $tot_j/$item, $tot_b/$item, $tot_p/$item, $tot_m/$item, $tot_k/$item, $tot_mar/$item, ($tot_mar/$tot_j)*100/$item ]);
|
|
}
|
|
|
|
$xls->add_stamp(1, true);
|
|
$xls->download("Data Rekap Margin Harian ".str_replace("s/d","-",$apre['periode']).".xlsx");
|
|
unset($xls);
|
|
}
|
|
|
|
public function index() {
|
|
$form = new Form($this->app);
|
|
$tcab = $form->option_db("_", "CabID", $this->id_cab, true, false, "para_cabang", ["CabID","Nama_Cab"], "Aktif=1 ORDER BY CabID", [0,"SEMUA CABANG"]);
|
|
$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="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="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);
|
|
}
|
|
|
|
}
|