Files
2026-09-11 16:03:00 +07:00

62 lines
25 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Purchasedocuments extends MY_Controller
{
private $types=array(
'purchase_order'=>array('purchase_orders','id','po_no','purchase_order_lines','purchase_order_id'),
'goods_receipt'=>array('goods_receipts','id','receipt_no','goods_receipt_lines','goods_receipt_id'),
'supplier_invoice'=>array('supplier_invoices','id','internal_no','supplier_invoice_lines','supplier_invoice_id'),
'supplier_payment'=>array('supplier_payments','id','payment_no','purchase_payment_sources','supplier_payment_id'),
'purchase_return'=>array('purchase_returns','id','return_no','purchase_return_lines','purchase_return_id'),
'supplier_debit_note'=>array('supplier_debit_notes','id','debit_note_no',null,null),
'purchase_refund'=>array('purchase_refunds','id','refund_no',null,null)
);
private function document($type,$id){
if(!isset($this->types[$type]))return null;$m=$this->types[$type];$items=array();
if($type==='purchase_order'){
$header=$this->db->select('po.*,pol.warehouse_id,s.supplier_code,s.name supplier_name,s.address supplier_address,s.phone supplier_phone,pr.request_no,pr.department,u.nama created_name,a.nama approved_name,w.nama warehouse_name')->from('purchase_orders po')->join('suppliers s','s.id=po.supplier_id')->join('purchase_requests pr','pr.id=po.purchase_request_id','left')->join('users u','u.id=po.created_by','left')->join('users a','a.id=po.approved_by','left')->join('purchase_order_lines pol','pol.purchase_order_id=po.id','left')->join('warehouses w','w.id=pol.warehouse_id','left')->where('po.id',(int)$id)->group_by('po.id')->get()->row_array();
if($header&&$this->db->table_exists('purchase_installment_schedules')){$schedule=$this->db->select('COUNT(*) installment_count,MIN(due_date) first_due_date',false)->where('purchase_order_id',(int)$id)->get('purchase_installment_schedules')->row_array();$header['installment_count']=(int)($schedule['installment_count']??0);$header['first_due_date']=$schedule['first_due_date']??null;}
$lines=$this->db->select('pol.*,k.kode_barang,k.nama nama_barang')->from('purchase_order_lines pol')->join('kode_barang k','k.id=pol.kode_barang_id','left')->where('pol.purchase_order_id',(int)$id)->order_by('pol.id')->get()->result_array();
}elseif($type==='supplier_payment'){
$header=$this->db->select('sp.*,s.supplier_code,s.name supplier_name,s.address supplier_address,po.po_no,po.procurement_method,po.payment_timing,po.subtotal po_subtotal,po.tax_total po_tax_total,po.shipping_cost,po.insurance_cost,po.service_fee,po.discount_amount,po.cashback_amount,po.total po_total,po.paid_amount po_paid_amount,u.nama created_name')->from('supplier_payments sp')->join('suppliers s','s.id=sp.supplier_id','left')->join('purchase_orders po','po.id=sp.purchase_order_id','left')->join('users u','u.id=sp.created_by','left')->where('sp.id',(int)$id)->get()->row_array();
if($header){$journal=$this->db->select('no_ref')->get_where('journals',array('id'=>(int)$header['journal_id']))->row_array();$header['journal_no']=$journal?$journal['no_ref']:null;}
$lines=$this->db->select('pps.amount,a.kode_akun,a.nama_akun')->from('purchase_payment_sources pps')->join('accounts a','a.id=pps.account_id','left')->where('pps.supplier_payment_id',(int)$id)->order_by('pps.id')->get()->result_array();
if(!$lines&&$header&&!empty($header['bank_account_id'])){$account=$this->db->select('kode_akun,nama_akun')->get_where('accounts',array('id'=>(int)$header['bank_account_id']))->row_array();$lines[]=array('amount'=>$header['amount'],'kode_akun'=>$account?$account['kode_akun']:'-','nama_akun'=>$account?$account['nama_akun']:'Kas/Bank');}
if($header&&!empty($header['purchase_order_id']))$items=$this->db->select('pol.description,pol.qty,pol.unit_price,pol.tax_amount,pol.line_total,pol.tracking_type,k.kode_barang,k.nama nama_barang')->from('purchase_order_lines pol')->join('kode_barang k','k.id=pol.kode_barang_id','left')->where('pol.purchase_order_id',(int)$header['purchase_order_id'])->order_by('pol.id')->get()->result_array();
}elseif($type==='goods_receipt'){
$header=$this->db->select('gr.*,po.po_no,po.order_date,po.procurement_method,po.marketplace,po.external_order_no,po.courier,po.tracking_no,po.payment_timing,po.payment_plan,po.payment_status,po.subtotal po_subtotal,po.tax_total po_tax_total,po.shipping_cost,po.insurance_cost,po.service_fee,po.discount_amount,po.cashback_amount,po.total po_total,po.paid_amount po_paid_amount,s.supplier_code,s.name supplier_name,s.address supplier_address,s.payment_term_days,u.nama received_name')->from('goods_receipts gr')->join('purchase_orders po','po.id=gr.purchase_order_id','left')->join('suppliers s','s.id=gr.supplier_id','left')->join('users u','u.id=gr.received_by','left')->where('gr.id',(int)$id)->get()->row_array();
if($header){
$receiptSummary=$this->db->select("COUNT(DISTINCT gr.id) receipt_count,GROUP_CONCAT(DISTINCT CONCAT(gr.receipt_no,' (',DATE_FORMAT(gr.receipt_date,'%d-%m-%Y'),')') ORDER BY gr.receipt_date,gr.id SEPARATOR ', ') receipt_numbers",false)->from('goods_receipts gr')->where('gr.purchase_order_id',(int)$header['purchase_order_id'])->where('gr.status','posted')->get()->row_array();
$header['receipt_count']=(int)($receiptSummary['receipt_count']??0);$header['receipt_numbers']=$receiptSummary['receipt_numbers']??'';
$schedule=$this->db->select('COUNT(*) installment_count,MIN(due_date) first_due_date',false)->where('purchase_order_id',(int)$header['purchase_order_id'])->get('purchase_installment_schedules')->row_array();
$header['installment_count']=(int)($schedule['installment_count']??0);$header['first_due_date']=$schedule['first_due_date']??null;
}
$lines=array();
if($header)$lines=$this->db->select("pol.id po_line_id,pol.qty ordered_qty,pol.received_qty total_received_qty,pol.invoiced_qty,pol.unit_price,pol.tax_rate,pol.line_total,pol.description,pol.tracking_type,pol.qty_per_barcode,k.kode_barang,k.nama nama_barang,w.nama warehouse_name,COALESCE((SELECT SUM(x.qty) FROM goods_receipt_lines x WHERE x.po_line_id=pol.id AND x.goods_receipt_id=".(int)$id."),0) selected_receipt_qty,COALESCE((SELECT SUM(x.qty) FROM goods_receipt_lines x JOIN goods_receipts gx ON gx.id=x.goods_receipt_id WHERE x.po_line_id=pol.id AND gx.status='posted'),0) accumulated_received_qty,COALESCE((SELECT SUM(prl.shipped_qty) FROM purchase_return_lines prl JOIN purchase_returns pr ON pr.id=prl.purchase_return_id JOIN goods_receipt_lines grlr ON grlr.id=prl.receipt_line_id WHERE grlr.po_line_id=pol.id AND pr.status IN('shipped','waiting_supplier','partially_resolved','resolved')),0) accumulated_returned_qty,GREATEST(0,COALESCE((SELECT SUM(x.qty) FROM goods_receipt_lines x JOIN goods_receipts gx ON gx.id=x.goods_receipt_id WHERE x.po_line_id=pol.id AND gx.status='posted'),0)-COALESCE((SELECT SUM(prl.shipped_qty) FROM purchase_return_lines prl JOIN purchase_returns pr ON pr.id=prl.purchase_return_id JOIN goods_receipt_lines grlr ON grlr.id=prl.receipt_line_id WHERE grlr.po_line_id=pol.id AND pr.status IN('shipped','waiting_supplier','partially_resolved','resolved')),0)-COALESCE((SELECT SUM(sil.qty) FROM supplier_invoice_lines sil WHERE sil.po_line_id=pol.id),0)) invoice_available_qty",false)->from('purchase_order_lines pol')->join('kode_barang k','k.id=pol.kode_barang_id','left')->join('warehouses w','w.id=pol.warehouse_id','left')->where('pol.purchase_order_id',(int)$header['purchase_order_id'])->order_by('pol.id')->get()->result_array();
}elseif($type==='supplier_invoice'){
$header=$this->db->select("si.*,s.supplier_code,s.name supplier_name,s.address supplier_address,u.nama created_name,po.po_no,po.subtotal po_subtotal,po.tax_total po_tax_total,po.shipping_cost,po.insurance_cost,po.service_fee,po.discount_amount,po.cashback_amount,po.total po_total,po.paid_amount po_paid_amount,GROUP_CONCAT(DISTINCT gr.receipt_no ORDER BY gr.receipt_date,gr.id SEPARATOR ', ') receipt_no,GROUP_CONCAT(DISTINCT COALESCE(gr.supplier_delivery_no,'-') ORDER BY gr.receipt_date,gr.id SEPARATOR ', ') supplier_delivery_no",false)->from('supplier_invoices si')->join('suppliers s','s.id=si.supplier_id','left')->join('users u','u.id=si.created_by','left')->join('supplier_invoice_lines sil','sil.supplier_invoice_id=si.id','left')->join('purchase_order_lines pol','pol.id=sil.po_line_id','left')->join('purchase_orders po','po.id=pol.purchase_order_id','left')->join('goods_receipt_lines grl','grl.id=sil.receipt_line_id','left')->join('goods_receipts gr','gr.id=grl.goods_receipt_id','left')->where('si.id',(int)$id)->group_by('si.id')->get()->row_array();
if($header)$header['status_label']=array('verified'=>'Perlu Finalisasi','partial'=>'Parsial','paid'=>'Lunas','cancelled'=>'Dibatalkan','reversed'=>'Dibalik')[$header['status']]??$header['status'];
$lines=$this->db->select("SUM(sil.qty) qty,sil.unit_price,SUM(sil.tax_amount) tax_amount,SUM(sil.line_total) line_total,IF(SUM(sil.match_status<>'matched')=0,'matched','mismatch') match_status,pol.description,k.kode_barang,k.nama nama_barang,GROUP_CONCAT(DISTINCT gr.receipt_no ORDER BY gr.receipt_date,gr.id SEPARATOR ', ') receipt_no",false)->from('supplier_invoice_lines sil')->join('purchase_order_lines pol','pol.id=sil.po_line_id','left')->join('kode_barang k','k.id=pol.kode_barang_id','left')->join('goods_receipt_lines grl','grl.id=sil.receipt_line_id','left')->join('goods_receipts gr','gr.id=grl.goods_receipt_id','left')->where('sil.supplier_invoice_id',(int)$id)->group_by('sil.po_line_id,sil.unit_price,pol.description,k.kode_barang,k.nama')->order_by('MIN(sil.id)',null,false)->get()->result_array();
}else{$header=$this->db->get_where($m[0],array($m[1]=>(int)$id))->row_array();$lines=$m[3]?$this->db->get_where($m[3],array($m[4]=>(int)$id))->result_array():array();}
if(!$header)return null;$attachments=$this->db->where(array('entity_type'=>$type,'entity_id'=>(int)$id))->order_by('id','DESC')->get('transaction_attachments')->result_array();return array('type'=>$type,'number'=>$header[$m[2]],'header'=>$header,'lines'=>$lines,'items'=>$items,'attachments'=>$attachments);
}
public function detail($type,$id){$d=$this->document($type,$id);if(!$d)return json_response(false,'Dokumen pembelian tidak ditemukan.',array(),404);foreach($d['attachments']as&$a){$a['preview_url']=base_url('accountingattachments/preview/'.$a['id']);$a['download_url']=$a['preview_url'];$a['actual_download_url']=base_url('accountingattachments/download/'.$a['id']);}return json_response(true,'Detail dokumen pembelian.',$d);}
public function payment_bank_pdf($id)
{
$data=$this->document('supplier_payment',$id);if(!$data)show_404();require_once APPPATH.'third_party/fpdf/fpdf.php';$h=$data['header'];
$principal=(float)($h['amount']??0);$charge=(float)($h['bank_charge_amount']??0);$cashOut=(float)($h['total_cash_out']??0);if($cashOut<=0)$cashOut=$principal+$charge;
$pdf=new FPDF('P','mm','A4');$pdf->SetMargins(15,15,15);$pdf->AddPage();$pdf->SetFont('Arial','B',15);$pdf->Cell(0,8,'BUKTI PEMBAYARAN PEMBELIAN',0,1,'C');$pdf->SetFont('Arial','',9);$pdf->Cell(0,6,$h['payment_no'],0,1,'C');$pdf->Ln(6);
foreach(array('Supplier'=>trim(($h['supplier_code']?:'').' - '.($h['supplier_name']?:'')),'Purchase Order'=>$h['po_no'],'Tanggal Pembayaran'=>$h['payment_date'],'Referensi Bank'=>$h['reference_no'],'Nomor Jurnal'=>$h['journal_no'],'Status'=>strtoupper($h['status']),'Diproses oleh'=>$h['created_name'])as$label=>$value){$pdf->SetFont('Arial','B',9);$pdf->Cell(42,6,$label,0,0);$pdf->SetFont('Arial','',9);$pdf->MultiCell(0,6,': '.($value?:'-'));}
if(!empty($data['items'])){$pdf->Ln(3);$pdf->SetFont('Arial','B',9);$pdf->Cell(0,6,'BARANG YANG DIBAYAR',0,1);$widths=array(9,32,64,17,25,25);foreach(array('No','Kode','Barang / Spesifikasi','Qty','Harga','Total')as$i=>$label)$pdf->Cell($widths[$i],7,$label,1,0,'C');$pdf->Ln();$pdf->SetFont('Arial','',7);foreach($data['items']as$i=>$item){$pdf->Cell($widths[0],7,$i+1,1,0,'C');$pdf->Cell($widths[1],7,substr($item['kode_barang']?:'-',0,19),1);$pdf->Cell($widths[2],7,substr(($item['nama_barang']?:'').' - '.($item['description']?:''),0,43),1);$pdf->Cell($widths[3],7,number_format($item['qty'],0,',','.'),1,0,'R');$pdf->Cell($widths[4],7,number_format($item['unit_price'],2,',','.'),1,0,'R');$pdf->Cell($widths[5],7,number_format($item['line_total'],2,',','.'),1,1,'R');}}
$pdf->Ln(4);$pdf->SetFont('Arial','B',9);$pdf->Cell(0,6,'SUMBER KAS / BANK',0,1);$pdf->Cell(12,8,'No',1,0,'C');$pdf->Cell(110,8,'Akun',1,0,'C');$pdf->Cell(50,8,'Nominal Keluar',1,1,'C');$pdf->SetFont('Arial','',9);foreach($data['lines']as$i=>$line){$pdf->Cell(12,8,$i+1,1,0,'C');$pdf->Cell(110,8,substr(($line['kode_akun']?:'-').' - '.($line['nama_akun']?:'-'),0,64),1);$pdf->Cell(50,8,'Rp '.number_format($line['amount'],2,',','.'),1,1,'R');}
$pdf->Ln(4);foreach(array('Pokok Pembayaran'=>$principal,'Biaya Admin Bank'=>$charge,'Total Keluar Kas/Bank'=>$cashOut)as$label=>$value){$pdf->SetFont('Arial',$label==='Total Keluar Kas/Bank'?'B':'',9);$pdf->Cell(122,7,$label,0,0,'R');$pdf->Cell(50,7,'Rp '.number_format($value,2,',','.'),0,1,'R');}
if(!empty($h['notes'])){$pdf->Ln(4);$pdf->SetFont('Arial','B',9);$pdf->Cell(0,6,'Catatan',0,1);$pdf->SetFont('Arial','',9);$pdf->MultiCell(0,5,$h['notes'],1);}
$pdf->Ln(12);$pdf->SetFont('Arial','',9);$pdf->Cell(0,6,'Finance / Pembayar',0,1,'C');$pdf->Ln(15);$pdf->SetFont('Arial','B',9);$pdf->Cell(0,6,'('.($h['created_name']?:'-').')',0,1,'C');$pdf->Output('I',$h['payment_no'].'.pdf');
}
public function payment_pdf($id){$d=$this->document('supplier_payment',$id);if(!$d)show_404();require_once APPPATH.'third_party/fpdf/fpdf.php';$h=$d['header'];$pdf=new FPDF('P','mm','A4');$pdf->SetMargins(15,15,15);$pdf->AddPage();$pdf->SetFont('Arial','B',15);$pdf->Cell(0,8,'BUKTI PEMBAYARAN PEMBELIAN',0,1,'C');$pdf->SetFont('Arial','',9);$pdf->Cell(0,6,$h['payment_no'],0,1,'C');$pdf->Ln(6);foreach(array('Supplier'=>trim(($h['supplier_code']?:'').' - '.($h['supplier_name']?:'')),'Purchase Order'=>$h['po_no'],'Tanggal Pembayaran'=>$h['payment_date'],'Referensi'=>$h['reference_no'],'Nomor Jurnal'=>$h['journal_no'],'Status'=>strtoupper($h['status']),'Diproses oleh'=>$h['created_name'])as$label=>$value){$pdf->SetFont('Arial','B',9);$pdf->Cell(42,6,$label,0,0);$pdf->SetFont('Arial','',9);$pdf->MultiCell(0,6,': '.($value?:'-'));}$pdf->Ln(3);if(!empty($d['items'])){$pdf->SetFont('Arial','B',9);$pdf->Cell(0,6,'BARANG YANG DIBAYAR',0,1);$pdf->SetFont('Arial','B',7);$pdf->Cell(8,7,'No',1,0,'C');$pdf->Cell(30,7,'Kode',1,0,'C');$pdf->Cell(62,7,'Barang / Spesifikasi',1,0,'C');$pdf->Cell(14,7,'Qty',1,0,'C');$pdf->Cell(28,7,'Harga',1,0,'C');$pdf->Cell(30,7,'Total',1,1,'C');$pdf->SetFont('Arial','',7);foreach($d['items']as$i=>$item){$pdf->Cell(8,7,$i+1,1,0,'C');$pdf->Cell(30,7,substr($item['kode_barang']?:'-',0,18),1);$pdf->Cell(62,7,substr(($item['nama_barang']?:'').' - '.($item['description']?:''),0,42),1);$pdf->Cell(14,7,number_format($item['qty'],0,',','.'),1,0,'R');$pdf->Cell(28,7,number_format($item['unit_price'],2,',','.'),1,0,'R');$pdf->Cell(30,7,number_format($item['line_total'],2,',','.'),1,1,'R');}$pdf->Ln(4);}$pdf->SetFont('Arial','B',9);$pdf->Cell(0,6,'RINCIAN NILAI PURCHASE ORDER',0,1);foreach(array('Subtotal Barang'=>$h['po_subtotal'],'Pajak'=>$h['po_tax_total'],'Ongkos Kirim'=>$h['shipping_cost'],'Asuransi'=>$h['insurance_cost'],'Biaya Layanan'=>$h['service_fee'],'Diskon / Voucher'=>-$h['discount_amount'],'Cashback'=>-$h['cashback_amount'],'Total PO'=>$h['po_total'],'Pembayaran Ini'=>$h['amount'],'Sisa Setelah Pembayaran'=>max(0,$h['po_total']-$h['po_paid_amount']))as$label=>$value){$pdf->SetFont('Arial',in_array($label,array('Total PO','Pembayaran Ini','Sisa Setelah Pembayaran'),true)?'B':'',8);$pdf->Cell(122,5,$label,0,0,'R');$pdf->Cell(50,5,'Rp '.number_format($value,2,',','.'),0,1,'R');}$pdf->Ln(3);$pdf->SetFont('Arial','B',9);$pdf->Cell(12,8,'No',1,0,'C');$pdf->Cell(110,8,'Sumber Kas / Bank',1,0,'C');$pdf->Cell(50,8,'Nominal',1,1,'C');$pdf->SetFont('Arial','',9);foreach($d['lines']as$i=>$line){$pdf->Cell(12,8,$i+1,1,0,'C');$pdf->Cell(110,8,substr(($line['kode_akun']?:'-').' - '.($line['nama_akun']?:'-'),0,64),1);$pdf->Cell(50,8,'Rp '.number_format($line['amount'],2,',','.'),1,1,'R');}$pdf->SetFont('Arial','B',10);$pdf->Cell(122,9,'TOTAL PEMBAYARAN',1,0,'R');$pdf->Cell(50,9,'Rp '.number_format($h['amount'],2,',','.'),1,1,'R');if(!empty($h['notes'])){$pdf->Ln(5);$pdf->SetFont('Arial','B',9);$pdf->Cell(0,6,'Catatan',0,1);$pdf->SetFont('Arial','',9);$pdf->MultiCell(0,5,$h['notes'],1);}$pdf->Ln(14);$pdf->SetFont('Arial','',9);$pdf->Cell(0,6,'Finance / Pembayar',0,1,'C');$pdf->Ln(16);$pdf->SetFont('Arial','B',9);$pdf->Cell(0,6,'('.($h['created_name']?:'-').')',0,1,'C');$pdf->Output('I',$h['payment_no'].'.pdf');}
public function receipt_pdf($id){$d=$this->document('goods_receipt',$id);if(!$d)show_404();require_once APPPATH.'third_party/fpdf/fpdf.php';$h=$d['header'];$pdf=new FPDF('P','mm','A4');$pdf->SetMargins(15,15,15);$pdf->AddPage();$pdf->SetFont('Arial','B',15);$pdf->Cell(0,8,'BUKTI PENERIMAAN BARANG',0,1,'C');$pdf->SetFont('Arial','',9);$pdf->Cell(0,6,$h['receipt_no'],0,1,'C');$pdf->Ln(6);foreach(array('Supplier'=>trim(($h['supplier_code']?:'').' - '.($h['supplier_name']?:'')),'Purchase Order'=>$h['po_no'],'Tanggal PO'=>$h['order_date'],'Tanggal Diterima'=>$h['receipt_date'],'Surat Jalan / Resi'=>$h['supplier_delivery_no'],'Status'=>strtoupper($h['status']),'Diterima oleh'=>$h['received_name'])as$label=>$value){$pdf->SetFont('Arial','B',9);$pdf->Cell(42,6,$label,0,0);$pdf->SetFont('Arial','',9);$pdf->MultiCell(0,6,': '.($value?:'-'));}$pdf->Ln(3);$widths=array(10,34,65,18,25,20);foreach(array('No','Kode','Barang / Spesifikasi','Qty','Tracking','Gudang')as$i=>$label){$pdf->SetFont('Arial','B',8);$pdf->Cell($widths[$i],8,$label,1,0,'C');}$pdf->Ln();$pdf->SetFont('Arial','',8);$total=0;foreach($d['lines']as$i=>$line){$total+=(float)$line['qty'];$pdf->Cell($widths[0],8,$i+1,1,0,'C');$pdf->Cell($widths[1],8,substr($line['kode_barang']?:'-',0,20),1);$pdf->Cell($widths[2],8,substr(($line['nama_barang']?:'').' - '.($line['description']?:''),0,43),1);$pdf->Cell($widths[3],8,number_format($line['qty'],2,',','.'),1,0,'R');$pdf->Cell($widths[4],8,$line['tracking_type']?:'-',1,0,'C');$pdf->Cell($widths[5],8,substr($line['warehouse_name']?:'-',0,12),1);$pdf->Ln();}$pdf->SetFont('Arial','B',9);$pdf->Cell(109,8,'TOTAL UNIT DITERIMA',1,0,'R');$pdf->Cell(18,8,number_format($total,2,',','.'),1,0,'R');$pdf->Cell(45,8,'',1,1);$pdf->Ln(14);$pdf->SetFont('Arial','',9);$pdf->Cell(86,6,'Petugas Gudang',0,0,'C');$pdf->Cell(86,6,'Pengirim / Supplier',0,1,'C');$pdf->Ln(17);$pdf->SetFont('Arial','B',9);$pdf->Cell(86,6,'('.($h['received_name']?:'-').')',0,0,'C');$pdf->Cell(86,6,'(____________________)',0,1,'C');$pdf->Output('I',$h['receipt_no'].'.pdf');}
public function invoice_pdf($id){$d=$this->document('supplier_invoice',$id);if(!$d)show_404();require_once APPPATH.'third_party/fpdf/fpdf.php';$h=$d['header'];$invoiceTotal=(float)($h['po_total']??$h['total']);$poPaid=max((float)($h['po_paid_amount']??0),(float)$h['paid_amount']);$invoiceBalance=max(0,$invoiceTotal-$poPaid);$pdf=new FPDF('P','mm','A4');$pdf->SetMargins(12,12,12);$pdf->AddPage();$pdf->SetFont('Arial','B',15);$pdf->Cell(0,8,'INVOICE SUPPLIER',0,1,'C');$pdf->SetFont('Arial','',9);$pdf->Cell(0,5,$h['internal_no'].' / '.$h['supplier_invoice_no'],0,1,'C');$pdf->Ln(6);foreach(array('Supplier'=>trim(($h['supplier_code']?:'').' - '.($h['supplier_name']?:'')),'Purchase Order'=>$h['po_no'],'Penerimaan'=>$h['receipt_no'],'Surat Jalan / Resi'=>$h['supplier_delivery_no'],'Tanggal Invoice'=>$h['invoice_date'],'Jatuh Tempo'=>$h['due_date'],'Rencana Pembayaran'=>strtoupper($h['payment_plan']),'Status'=>strtoupper($h['status_label']??$h['status']))as$label=>$value){$pdf->SetFont('Arial','B',8);$pdf->Cell(40,5,$label,0,0);$pdf->SetFont('Arial','',8);$pdf->MultiCell(0,5,': '.($value?:'-'));}$pdf->Ln(3);$widths=array(8,29,48,14,25,22,26);foreach(array('No','Kode','Barang','Qty','Harga','Pajak','Total')as$i=>$label){$pdf->SetFont('Arial','B',7);$pdf->Cell($widths[$i],7,$label,1,0,'C');}$pdf->Ln();$pdf->SetFont('Arial','',7);foreach($d['lines']as$i=>$line){$pdf->Cell($widths[0],7,$i+1,1,0,'C');$pdf->Cell($widths[1],7,substr($line['kode_barang']?:'-',0,17),1);$pdf->Cell($widths[2],7,substr(($line['nama_barang']?:'').' - '.($line['description']?:''),0,32),1);$pdf->Cell($widths[3],7,number_format($line['qty'],2,',','.'),1,0,'R');$pdf->Cell($widths[4],7,number_format($line['unit_price'],2,',','.'),1,0,'R');$pdf->Cell($widths[5],7,number_format($line['tax_amount'],2,',','.'),1,0,'R');$pdf->Cell($widths[6],7,number_format($line['line_total'],2,',','.'),1,1,'R');}$pdf->Ln(3);foreach(array('Subtotal Barang'=>$h['po_subtotal']??$h['subtotal'],'Pajak'=>$h['po_tax_total']??$h['tax_total'],'Ongkir, Asuransi & Layanan'=>(float)($h['shipping_cost']??0)+(float)($h['insurance_cost']??0)+(float)($h['service_fee']??0),'Diskon & Cashback'=>-((float)($h['discount_amount']??0)+(float)($h['cashback_amount']??0)),'Total Invoice'=>$invoiceTotal,'Total Sudah Dibayar'=>$poPaid,'Sisa Tagihan'=>$invoiceBalance)as$label=>$value){$pdf->SetFont('Arial',in_array($label,array('Total Invoice','Total Sudah Dibayar','Sisa Tagihan'),true)?'B':'',8);$pdf->Cell(145,5,$label,0,0,'R');$pdf->Cell(27,5,number_format($value,2,',','.'),0,1,'R');}$pdf->Ln(13);$pdf->SetFont('Arial','',8);$pdf->Cell(86,5,'Dicatat oleh',0,0,'C');$pdf->Cell(86,5,'Disetujui / Diperiksa',0,1,'C');$pdf->Ln(16);$pdf->SetFont('Arial','B',8);$pdf->Cell(86,5,'('.($h['created_name']?:'-').')',0,0,'C');$pdf->Cell(86,5,'(____________________)',0,1,'C');$pdf->Output('I',$h['internal_no'].'.pdf');}
public function pdf($type,$id){$d=$this->document($type,$id);if(!$d)show_404();require_once APPPATH.'third_party/fpdf/fpdf.php';if($type==='purchase_order')return$this->purchaseOrderPdf($d);$pdf=new FPDF('P','mm','A4');$pdf->SetMargins(14,14,14);$pdf->AddPage();$pdf->SetFont('Arial','B',14);$pdf->Cell(0,8,strtoupper(str_replace('_',' ',$type)),0,1,'C');$pdf->SetFont('Arial','',10);$pdf->Cell(0,6,'Nomor: '.$d['number'],0,1,'C');$pdf->Ln(4);foreach($d['header']as$key=>$value){if(is_null($value)||$value===''||in_array($key,array('id','snapshot'),true))continue;$label=ucwords(str_replace('_',' ',$key));$pdf->SetFont('Arial','B',8);$pdf->Cell(45,5,substr($label,0,28),0,0);$pdf->SetFont('Arial','',8);$pdf->MultiCell(0,5,': '.substr((string)$value,0,100));}foreach($d['lines']as$i=>$line){$pdf->SetFont('Arial','B',8);$pdf->Cell(0,5,'Baris '.($i+1),0,1);$pdf->SetFont('Arial','',7);$pdf->MultiCell(0,4,json_encode($line),1);}$pdf->Output('I',$d['number'].'.pdf');}
private function purchaseOrderPdf($d){$h=$d['header'];$method=array('online'=>'Pesanan Online','direct'=>'Pembelian Langsung','supplier'=>'Supplier Biasa');$timing=array('prepaid'=>'Bayar Sebelum Diterima','on_receipt'=>'Bayar Saat Diterima','installment'=>'Cicilan','credit'=>'Kredit/Jatuh Tempo');$pdf=new FPDF('P','mm','A4');$pdf->SetMargins(12,12,12);$pdf->AddPage();$pdf->SetFont('Arial','B',16);$pdf->Cell(0,8,'PURCHASE ORDER',0,1,'C');$pdf->SetFont('Arial','',9);$pdf->Cell(0,5,$h['po_no'],0,1,'C');$pdf->Ln(5);$pairs=array('Supplier'=>($h['supplier_code'].' - '.$h['supplier_name']),'Alamat'=>$h['supplier_address'],'Tanggal PO'=>$h['order_date'],'Estimasi Datang'=>$h['expected_date'],'Sumber PR'=>$h['request_no'],'Departemen'=>$h['department'],'Gudang'=>$h['warehouse_name'],'Metode Pengadaan'=>($method[$h['procurement_method']]??$h['procurement_method']),'Rencana Pembayaran'=>($timing[$h['payment_timing']]??$h['payment_timing']));if($h['procurement_method']==='online'){$pairs['Marketplace']=$h['marketplace'];$pairs['Nomor Pesanan']=$h['external_order_no'];$pairs['Kurir/Resi']=trim(($h['courier']?:'-').' / '.($h['tracking_no']?:'-'));}foreach($pairs as$l=>$v){$pdf->SetFont('Arial','B',8);$pdf->Cell(38,5,$l,0,0);$pdf->SetFont('Arial','',8);$pdf->MultiCell(0,5,': '.($v?:'-'));}$pdf->Ln(3);$widths=array(8,31,55,15,25,15,27);$headers=array('No','Kode','Barang / Spesifikasi','Qty','Harga','Pajak','Subtotal');$pdf->SetFont('Arial','B',7);foreach($headers as$i=>$label)$pdf->Cell($widths[$i],7,$label,1,0,'C');$pdf->Ln();$pdf->SetFont('Arial','',7);foreach($d['lines']as$i=>$l){$pdf->Cell($widths[0],7,$i+1,1,0,'C');$pdf->Cell($widths[1],7,substr($l['kode_barang']?:'-',0,18),1);$pdf->Cell($widths[2],7,substr(($l['nama_barang']?:'').' - '.$l['description'],0,38),1);$pdf->Cell($widths[3],7,number_format($l['qty'],2,',','.'),1,0,'R');$pdf->Cell($widths[4],7,number_format($l['unit_price'],2,',','.'),1,0,'R');$pdf->Cell($widths[5],7,number_format($l['tax_rate'],2,',','.').'%',1,0,'R');$pdf->Cell($widths[6],7,number_format($l['line_total'],2,',','.'),1,0,'R');$pdf->Ln();}$pdf->Ln(3);foreach(array('Subtotal'=>$h['subtotal'],'Pajak'=>$h['tax_total'],'Ongkos Kirim'=>$h['shipping_cost'],'Asuransi'=>$h['insurance_cost'],'Biaya Layanan'=>$h['service_fee'],'Diskon/Voucher'=>-$h['discount_amount'],'Cashback'=>-$h['cashback_amount'],'TOTAL PO'=>$h['total'])as$l=>$v){$pdf->SetFont('Arial',$l==='TOTAL PO'?'B':'',8);$pdf->Cell(145,5,$l,0,0,'R');$pdf->Cell(27,5,number_format($v,2,',','.'),0,1,'R');}$pdf->Ln(12);$pdf->SetFont('Arial','',8);$pdf->Cell(86,5,'Dibuat oleh',0,0,'C');$pdf->Cell(86,5,'Disetujui oleh',0,1,'C');$pdf->Ln(16);$pdf->SetFont('Arial','B',8);$pdf->Cell(86,5,'('.($h['created_name']?:'-').')',0,0,'C');$pdf->Cell(86,5,'('.($h['approved_name']?:'-').')',0,1,'C');$pdf->Output('I',$h['po_no'].'.pdf');}
}