You are on page 1of 7

<?

php
//
// DEFINES AND INCLUDES
//
define('SQL_QUERY_ADDRESS', <<<EOS
SELECT
"public"."tbl_mstr_business_partner"."name",
"public"."tbl_mstr_business_partner"."street_address",
"public"."tbl_mstr_business_partner"."telephone_fixed",
"public"."tbl_mstr_business_partner"."facsimile",
"public"."tbl_mstr_business_partner"."email"
FROM
"public"."tbl_so_sales_details"
Inner Join "public"."tbl_mstr_business_partner" ON "public"."tbl_so_sales_detail
s"."intermediary_code" = "public"."tbl_mstr_business_partner"."code"
WHERE
"public"."tbl_so_sales_details".sales_number=? AND
"public"."tbl_so_sales_details".revision_number=?
EOS
);
define('SQL_QUERY_PPH', <<<EOS
SELECT
"public"."tbl_fn_taxes"."percentage"
FROM
"public"."tbl_so_sales_details"
Inner Join "public"."tbl_fn_taxes" ON "public"."tbl_so_sales_details"."tax_witho
lding_income" = "public"."tbl_fn_taxes"."tax_code"
WHERE
"public"."tbl_so_sales_details"."sales_number" = ? AND
"public"."tbl_so_sales_details"."revision_number" = ?
EOS
);

define('SQL_QUERY_TAX', <<<EOS
SELECT
"public"."tbl_fn_taxes"."percentage"
FROM
"public"."tbl_so_sales_details"
Inner Join "public"."tbl_fn_taxes" ON "public"."tbl_so_sales_details"."int_sales
_tax" = "public"."tbl_fn_taxes"."tax_code"
WHERE
"public"."tbl_so_sales_details"."sales_number" = ? AND
"public"."tbl_so_sales_details"."revision_number" = ?
EOS
);

define('SQL_QUERY_OFFICE', <<<EOS
SELECT * FROM adm_back_office WHERE code = ?
EOS
);

define('SQL_QUERY_TOTPREMIUM', <<<EOS
SELECT
currency_code AS currency,
--ROUND(SUM((COALESCE(marine,0) + COALESCE(non_marine,0) + COALESCE(cat_nat,0) +
COALESCE(cat_soc,0)) * premium_base / (CASE WHEN rate <> 'Fixed Amount' THEN 10
0 ELSE 1 END)),2) AS totprem
ROUND(SUM(premium),2) AS totprem
FROM tbl_so_sales_objects
WHERE
sales_number = ? AND revision_number = ?
AND
premium > '0' AND premium IS NOT NULL
GROUP BY currency_code
ORDER BY currency_code ASC
EOS
);

define('SQL_QUERY_OBJECTS', <<<EOS
SELECT
coverage_name, object_number, location_id, interest_name, currency_code, premium
_base, adjustment, premium, declared_value,
rate AS ratedesc, COALESCE(marine,0) + COALESCE(non_marine,0) + COALESCE(cat_nat
,0) + COALESCE(cat_soc,0) AS rate, description
FROM
tbl_so_sales_objects AS o
WHERE
o.sales_number = ? AND o.revision_number = ?
AND
o.print_on_policy = 't'
ORDER BY object_number ASC, coverage_name ASC, currency_code ASC
EOS
);
define('SQL_QUERY_DETAILS', <<<EOS
SELECT int_name,intermediary_code,intermediary_type,commission,substring(sales_n
umber,2,2) as office_code,sales_number,revision_number,substring(sales_number,4,
3) as kode_polis,sales_number || revision_number AS sales_numbers, sales_type,
(SELECT b.name FROM adm_back_office AS b, adm_front_office AS f WHERE f.code = f
ront_office_code_handling AND b.code = f.back_office_code) AS office_name,
(SELECT b.address || ' PH :'||b.telephone||', FAX :'||b.fax FROM adm_back_offic
e AS b, adm_front_office AS f WHERE f.code = front_office_code_handling AND b.co
de = f.back_office_code) AS office_address,
(SELECT b.back_office_head_name FROM adm_back_office AS b, adm_front_office AS f
WHERE f.code = front_office_code_handling AND b.code = f.back_office_code) AS h
ead_name,
(SELECT b.back_office_head FROM adm_back_office AS b, adm_front_office AS f WHER
E f.code = front_office_code_handling AND b.code = f.back_office_code) AS headid
,
CURRENT_DATE as print_date,
(
SELECT insurance_product_name FROM
tbl_pd_insurance_products
WHERE
insurance_product_code = s.insurance_product_code
) AS insurance_product_name,
(
SELECT (title || ' ' || name)
FROM tbl_mstr_business_partner
WHERE code = s.customer_code_orig
)
||
CASE WHEN s.customer_code_on_behalf IS NOT NULL THEN
(SELECT ' QQ ' || (title || ' ' || name || ' ') FROM tbl_mstr_business_partner
WHERE code = s.customer_code_on_behalf)
ELSE ''
END
||
COALESCE(' QQ ' || s.on_behalf_others, '')
||
CASE WHEN extend_customer_definition = 't' THEN
' and/or subsidiary and/or affiliated and/or associated for their respective ri
ght/interests.'
ELSE ''
END
AS customer_name,
(
SELECT
COALESCE(l.street_address,'') || ' ' ||
COALESCE((SELECT str_kelurahan_name FROM tbl_mstr_kelurahan WHERE num_kelurahan_
id = l.kelurahan_code)||'. ' ,'') ||
COALESCE((SELECT str_city_name FROM tbl_mstr_city WHERE num_city_id = l.kecamata
n_code)||'. ','') ||
COALESCE((SELECT str_kabupaten_name FROM tbl_mstr_kabupaten WHERE num_kabupaten_
id = l.kabupaten_kotamadya_code)||'. ','') ||
COALESCE((SELECT str_province_name FROM tbl_mstr_province WHERE num_province_id
= l.state_province_code)||'. ','') ||
COALESCE(l.postal_code||'. ','') ||
COALESCE((SELECT str_country_name FROM tbl_mstr_country WHERE str_country_code =
l.country_code)||'.','')
FROM
tbl_mstr_business_partner AS l
WHERE
code = s.customer_code_orig
) AS customer_address,
inward_customer_orig, reference, own_share, discount,
policy_cost_currency_code,
policy_cost_amount,
stamp_duty_currency_code,
stamp_duty_amount,
insurance_period_from AS period_from, insurance_period_to AS period_to,
print_cover,
print_schedule,
print_policy_wording,
print_extension_clauses
FROM
tbl_so_sales_details AS s
WHERE
s.sales_number = ? AND s.revision_number = ?
EOS
);

define('SQL_PRINTED_APAR',<<<EOS
SELECT substring(sales_number,4,3) as kode_polis,sales_number, revision_number,
sales_number||revision_number AS slsid,
COALESCE(
(SELECT name FROM tbl_pd_alias WHERE code = q.insurance_product_code),
(SELECT insurance_product_name FROM tbl_pd_insurance_products WHERE ins
urance_product_code = q.insurance_product_code)
) AS insurance_product_code, inward_customer_orig AS insured,
(select name from tbl_mstr_business_partner where code=q.customer_code_on_behalf
) as on_behalf_name,
sales_number || revision_number AS sales_numbers,
COALESCE(cco_name,(SELECT title || ' ' || name FROM tbl_mstr_business_partner WH
ERE code = customer_code_orig)) AS customer_name,
insurance_period_from, insurance_period_to, sales_status,
(sales_number || revision_number || '.pdf') AS pdf_report_name
FROM
tbl_so_sales_details q,
adm_front_office f
WHERE
sales_status = 'Master JP'
AND
sales_type = 'Policy'
AND
front_office_code_handling = f.code
AND
(
f.code LIKE ?
)
AND
(
LOWER(sales_number||(SELECT title || ' ' || name FROM tbl_mstr_business_partner
WHERE code = customer_code_orig)||sales_status||insurance_period_from||COALESCE(
insurance_period_to,insurance_period_from)) LIKE LOWER(?)
)
ORDER BY insurance_period_from DESC
EOS
);
define('SQL_GET_OKNUMS',<<<vyan
SELECT *, 'oknum' as jenis
FROM
tbl_so_sales_oknums
WHERE
sales_number = ? and revision_number = ?
vyan
);
if($go_user['attr']['userid'] == 'ades'){
$go_db->debug=1;
}
require_once('extlib/PEAR/Image/Barcode.php');
///
// VARIABLE DECLARATION AND INITIALIZATION
//
$a_data = array();
$n_nextpage = 1;
if (isset($_SESSION['adodb_curr_page'])) {
$n_nextpage = $_SESSION['adodb_curr_page'];
}
if (!empty($_REQUEST['adodb_next_page'])) {
$n_nextpage = $_REQUEST['adodb_next_page'];
}
//
// FORM LOGIC
//
//
// ACTION / MODEL LOGIC
//
$s_boffcode = $go_user['attr']['back_office_code'];
if ($s_boffcode == 'XX') $s_boffcode = '%';
$s_findvalue = '%';
if (!empty($_REQUEST['findvalue']))
$s_findvalue .= $_REQUEST['findvalue'].'%';
if ($s_findvalue == '%') {
if ($_SESSION['accounting_findvalue'])
$s_findvalue = $_SESSION['accounting_findvalue'];
}
$_SESSION['accounting_findvalue'] = $s_findvalue;
$a_bind = array($s_boffcode, '%'.$s_findvalue.'%'); //, $s_boffcode);
$reprint = $_GET['reprint'];
$go_tpl->assign('reprint',$reprint);
$a_bind=array($s_boffcode, '%'.$s_findvalue.'%');
$o_dbres =& $go_db->PageExecute(SQL_PRINTED_APAR, $ga_conf['rows_per_page
'], $n_nextpage, $a_bind);

$a_navLinks = array();
if ($o_dbres && !empty($o_dbres->fields)) {
// set up the needed find variables
//
$a_privs =& $go_user['privs'];
$a_data['head'] = array_keys($o_dbres->fields);
$n_currPage = $o_dbres->AbsolutePage();
$n_lastPage = $o_dbres->LastPageNo();
$_SESSION['adodb_curr_page'] = $n_currPage;
if ($n_currPage < $n_lastPage) {
$a_navLinks['next'] = $n_currPage+1;
}
if ($n_currPage > 1 ) {
$a_navLinks['prev'] = $n_currPage-1;
}
if ($n_lastPage > 1) {
$a_navLinks['last'] = $n_lastPage;
}
$a_navLinks['curr_page'] = $n_currPage;
$a_navLinks['last_page'] = $n_lastPage;
while (!$o_dbres->EOF) {
$a_data['rows'][] = $o_dbres->fields;
if ($o_dbres->fields['posted'] == 't') {
print_apar($s_apar_type, $o_dbres->fields['apar_
number']);
}
$o_dbres->MoveNext();
}
}

if($_GET["printed"]){
//disini harusnya ada query
$a_bind = array($_GET["sales_number"], $_GET["revision_number"])
;
$reportData = $go_db->GetRow(SQL_QUERY_DETAILS, $a_bind);
$reportData['objects'] = $go_db->GetAll(SQL_QUERY_OBJECTS, $
a_bind);
$reportData['premiums'] = $go_db->GetAll(SQL_QUERY_TOTPREMIUM
, $a_bind);
$reportData['office'] = $go_db->GetAll(SQL_QUERY_OFFICE, $rep
ortData["office_code"]);
$reportData['tax'] = $go_db->GetAll(SQL_QUERY_TAX, $a_bind);
$reportData['pph'] = $go_db->GetAll(SQL_QUERY_PPH, $a_bind);
$reportData['intermediary'] = $go_db->GetAll(SQL_QUERY_ADDRES
S, $a_bind);
$reportData['Oknums'] = $go_db->GetAll(SQL_GET_OKNUMS, $a_bind);
$gbg = array();
$gbg['gbrel']['int'] = $reportData['intermediary'];
$gbg['gbrel']['oks'] = $reportData['Oknums'];

//printr($reportData);
$go_tpl->assign_by_ref('tpl_inv', $reportData);
$go_tpl->assign_by_ref('tpl_gab', $gbg);

clearstatcache();
$tplName = 'ap_JP_new.tpl';
//$tplName = 'ap_JP.tpl';
$strHTML = $go_tpl->fetch('reports/accounting/'.$tplName);

global $_APPDIR;
$sales_numbers = $reportData['sales_numbers'];
$rhname = $sales_numbers.'.html';
$rpname = $sales_numbers.'.pdf';
// create logo image (barcode of the sales_numbers)
$logoimage = $_APPDIR.'/images/images/barcode_'.$sales_numbers.'
.png';
Image_Barcode::draw($sales_numbers, 'Code39', 'png',$logoimage);
// FIXME : add error check here
if (!$f = fopen($_APPDIR.'/cache/reports/'.$rhname, "w")) {
$go_errors[] = "Failed to create file for report $rpname
";
continue;
}
if (fwrite($f, $strHTML) === FALSE) {
$go_errors[] = "Failed to write file for report $rpname"
;
continue;
}
fclose($f);
$pdfCmd = "/usr/bin/htmldoc -t pdf --no-embedfonts --compression
=9 --textfont Arial --no-pscommands --no-xrxcomments --size A4 --quiet --jpeg --
top 10mm --logoimage %s --bottom 30mm --webpage -f ";
$pdfCmd = sprintf($pdfCmd, $logoimage);
putenv("HTMLDOC_NOCGI=1");
exec($pdfCmd." $_APPDIR/reports/accounting/$rpname $_APPDIR/cach
e/reports/$rhname");
//echo mainURL();
echo "<script> window.open ('".mainURL()."/reports/accounting/$r
pname','mywindow','status=1,scrollbars=1,width=800,height=600'); </script>";
}
//
// RESULT VIEW RENDER
//
//printr($a_data);
$go_tpl->assign_by_ref('tpl_nav_link', $a_navLinks);
$go_tpl->assign_by_ref('tpl_qry_res', $a_data);
$go_tpl->assign_by_ref('tpl_messages', $a_messages);
return $go_tpl->showPage('accounting/print_list_resJP2.tpl');
?>