Вы находитесь на странице: 1из 5

/*

* LaporanHarianAction.java
* Part of Project Vega
*
* Author: Tantowi Mustofa. tantowi@spjbaut.com
*/
package com.tantowi.vega.finance;

import com.tantowi.vega.absen.*;
import com.tantowi.nebula.*;
import com.tantowi.nebula.DbConnection;
import com.tantowi.vega.VegaAction;
import static com.tantowi.vega.VegaAction.format;
import static com.tantowi.vega.finance.GiroEditAction.getBankItems;
import static com.tantowi.vega.finance.GiroEditAction.getMataUang;
import java.io.IOException;
import java.sql.SQLException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import java.text.DecimalFormat;

/**
*
* @author Tantowi
*/
public class SlipGiroAction extends VegaAction {

int fid = 0;
String fnomor = "";
LocalDate ftanggal = LocalDate.now();
String fkepada = "";
int fbank = 0;
LocalDate fjtempo = LocalDate.now();
double fjumlah = 0;
String fmtu = "";
String fket = "";
@Override
protected void doInit() throws ServletException, SQLException, IOException {
authorize("finance");
}

@Override
protected void doGet() throws ServletException, SQLException, IOException {
try (DbConnection db = this.getDbConnection()) {
String cnomor = (String) request.getAttribute("nomor");
if (cnomor == null || cnomor.isEmpty()) {
throw new HttpNotFoundException("");
}
if (!cnomor.equals("~")) {
// fid = M.validInt(cnomor);
db.query("select
id,nomor,tanggal,kepada,bank_id,jtempo,jumlah,mtu,keterangan from finance_giro_req
where nomor=?");
db.setString(1, cnomor);
db.execute();
if (!db.next()) {
throw new HttpNotFoundException("Nomor not found: " + cnomor);
}
fid = db.getInt(1);
fnomor = db.getString(2);
ftanggal = db.getLocalDate(3);
fkepada = db.getString(4);
fbank = db.getInt(5);
fjtempo = db.getLocalDate(6);
fjumlah = db.getInt(7);
fmtu = db.getString(8);
fket = db.getString(9);
}
byte[] pdf = doSlip();
String filename = "Slip giro -"+ ".pdf";

response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=" +
filename);
response.setContentLength(pdf.length);
ServletOutputStream out = response.getOutputStream();
out.write(pdf);

// display(db);
}

@Override
protected void doPost() throws ServletException, SQLException, IOException {
try (DbConnection db = this.getDbConnection()) {
fid = form.getInt("id");
fnomor = form.getString("nomor").toUpperCase();
ftanggal = form.getLocalDate("tanggal");
fkepada = form.getString("kepada");
fbank = form.getInt("bank");
fjtempo = form.getLocalDate("jtempo");
fjumlah = form.getDouble("jumlah");
fmtu = form.getString("mtu");
fket = form.getString("keterangan");
data.add (d);

db.commit();
redirect("./");
}

//byte[] pdf = testPage();


String filename = "laporan - harian giro -" +
date.format(DateTimeFormatter.ISO_LOCAL_DATE) + ".pdf";
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=" +
filename);
response.setContentLength(pdf.length);
ServletOutputStream out = response.getOutputStream();
out.write(pdf);
}

/**
* doLoad
*
* @param adate
* @param bdate
* @return
* @throws SQLException
*/
List<LaporanHarianGiroBean> doLoad(LocalDate adate, LocalDate bdate) throws
SQLException {
List<LaporanHarianGiroBean> data = new ArrayList<>();

try (DbConnection db = getDbConnection("default")) {


String sql = "SELECT
A.id,A.nomor,A.tanggal,A.kepada,A.bank_id,B.BANK,A.jtempo,A.jumlah,A.mtu from
finance_giro_req AS A "
+ " JOIN FINANCE_BANK AS B ON A.BANK_ID=B.ID"
+ " order by A.nomor";
db.query(sql);
//db.setLocalDate(1, adate);
// db.setLocalDate(2, bdate);
db.execute();
while (db.next()) {
LaporanHarianGiroBean d = new LaporanHarianGiroBean();

d.id = db.getInt(1);
d.nomor = db.getString(2);
d.tanggal = db.getString(3);
d.kepada = db.getString(4);
d.bank_id = db.getString(5);
d.bank_nama = db.getString(6);
d.jtempo = db.getString(7);
d.jumlah = db.getInt(8);
d.mtu = db.getString(9);

data.add(d);
}
}
return data;
}

byte[] testPage() throws SQLException, IOException {


PDFDocument doc = new PDFDocument();
PDFPage pg = doc.newPage(8.5f * 72f, 5.5f * 72f); // 612pt x 396pt

//pg.setStrokingColor(255, 0, 0);
//pg.setLinePattern(5, 0);
pg.setFont(PDType1Font.HELVETICA, 9);
pg.write(50, 50, "[50,50] v14");
pg.write(50, 300, "[50,300]");
pg.write(500, 50, "[500,50]");
pg.write(500, 300, "[500,300]");

pg.line(50, 50, 50, 300, 1);


pg.line(50, 300, 500, 300, 3);
pg.line(500, 300, 500, 50, 5);
pg.line(50, 50, 500, 50, 7);

pg.close();
byte[] pdf = doc.build();
doc.close();
return pdf;
}

/**
* doslip
*
* @param nomor
*/
byte[] doSlip() throws SQLException, IOException {
float ly = 0 ;
final float Col1=35;
final float Col2=175;
final float Col3=250;
// DecimalFormat df = new DecimalFormat("#,###,##0.00");
PDFDocument doc = new PDFDocument();
PDFPage pg = null;
pg = doc.newPage(8.5f*72f, 5.5f*72f);// 612pt x 396pt
ly = pg.getPageHeight()-50;

int line = 1;

pg.setFont(PDType1Font.HELVETICA_BOLD,15);
pg.line(20,350,400,350, 2);
pg.line(20,150,400,150, 2);
pg.line(20, 150,20,350, 2);
pg.line(400,150,400,350,2);
pg.write(Col1, ly-50 ,"Nomor giro ");
pg.write(Col2, ly-50,":");
pg.writeR(Col3, ly-50,50,fnomor);
pg.write(Col1, ly-75 ,"Tanggal");
pg.write(Col2,ly-75,":");
pg.writeR(Col3, ly-75,50,ftanggal.format(DateTimeFormatter.ISO_DATE));
pg.write(Col1, ly-100,"Jatuh Tempo");
pg.write(Col2, ly-100,":");
pg.writeR(Col3, ly-100,50,fjtempo.format(DateTimeFormatter.ISO_DATE));
pg.write(Col1, ly-125,"Kepada ");
pg.write(Col2, ly-125,":");
pg.writeR(Col3, ly-125,50,fkepada);
pg.write(Col1,ly-150,"Jumlah");
pg.write(Col2,ly-150,":");
pg.writeR(Col3, ly-150,50,String.valueOf(format(fjumlah)));

pg.close();

byte[] pdf = doc.build();


doc.close();

return pdf;
}

void display(DbConnection db) throws ServletException, IOException, SQLException


{
form.setInt("id", fid);
form.setString("nomor", fnomor);
form.setLocalDate("tanggal", ftanggal);
form.setString("kepada", fkepada);

form.setLocalDate("jtempo", fjtempo);
form.setString("jumlah", format(fjumlah));
form.setString("", fmtu);
String sfmtu = getMataUang(db, "IDR");
form.setString("mtu", sfmtu);
String fbankitems = getBankItems(db, fbank);
form.setString("bank_items", fbankitems);
form.setString("keterangan", fket);

forwardView("/finance/giroedit.jsp");
}

Вам также может понравиться