auth->check_module_permissions('billing'); $invoice_id = 1; */ require_once("lib/fpdf/fpdf.php"); class PDFInvoice extends FPDF { var $B; var $I; var $U; var $HREF; var $widths; var $aligns; var $date_format; var $company; var $wb; function PDF($orientation='P',$unit='mm',$format='A4') { //Call parent constructor $this->FPDF($orientation,$unit,$format); //Initialization $this->B=0; $this->I=0; $this->U=0; $this->HREF=''; $this->invoice_id=0; $this->date_format="m/d/Y"; $this->company=array(); } function Footer() { $company = $this->company; //Go to 1.5 cm from bottom $this->SetY(-20); $this->Cell(0,2,'',T,1,'C'); $this->SetFont('Arial','',7); $this->Cell(50,4,$company['company_name'],0); $this->Cell(50,4,$this->lng('bank_account_txt').': '.$company['bank_account_number'].' '.$this->lng('bank_code_txt').': '.$company['bank_code'],0); $this->Cell(50,4,$this->lng('iban_txt').': '.$company['bank_account_iban'],0); if($company['ceo_name'] != '') { $this->Cell(50,4,$this->lng('ceo_txt').': '.$company['ceo_name'],0,1); } else { $this->Cell(50,4,' ',0,1); } $this->Cell(50,4,$company['street'],0); $this->Cell(50,4,$this->lng('bank_name_txt').': '.$company['bank_name'],0); $this->Cell(50,4,$this->lng('vat_id_txt').': '.$company['vat_id'],0); $this->Cell(50,4,$this->lng('telephone_txt').': '.$company['telephone'],0,1); $this->Cell(50,4,$company['zip'].' '.$company['city'].' - '.$this->lng($company['country']),0); if($company['bank_account_swift'] != '') { $this->Cell(50,4,$this->lng('swift_txt').': '.$company['bank_account_swift'],0); } else { $this->Cell(50,4,' ',0); } if($company['company_register'] != '') { $this->Cell(50,4,$company['company_register'],0); } else { $this->Cell(50,4,' ',0); } if($company['fax'] != ''){ $this->Cell(50,4,$this->lng('fax_txt').': '.$company['fax'],0,1); } else { $this->Cell(50,4,' ',0,1); } } function WriteHTML($html) { //HTML parser $html=str_replace("\n",' ',$html); $a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE); foreach($a as $i=>$e) { if($i%2==0) { //Text if($this->HREF) $this->PutLink($this->HREF,$e); else $this->Write(5,$e); } else { //Tag if($e{0}=='/') $this->CloseTag(strtoupper(substr($e,1))); else { //Extract attributes $a2=explode(' ',$e); $tag=strtoupper(array_shift($a2)); $attr=array(); foreach($a2 as $v) if(ereg('^([^=]*)=["\']?([^"\']*)["\']?$',$v,$a3)) $attr[strtoupper($a3[1])]=$a3[2]; $this->OpenTag($tag,$attr); } } } } function OpenTag($tag,$attr) { //Opening tag if($tag=='B' or $tag=='I' or $tag=='U') $this->SetStyle($tag,true); if($tag=='A') $this->HREF=$attr['HREF']; if($tag=='BR') $this->Ln(5); } function CloseTag($tag) { //Closing tag if($tag=='B' or $tag=='I' or $tag=='U') $this->SetStyle($tag,false); if($tag=='A') $this->HREF=''; } function SetStyle($tag,$enable) { //Modify style and select corresponding font $this->$tag+=($enable ? 1 : -1); $style=''; foreach(array('B','I','U') as $s) if($this->$s>0) $style.=$s; $this->SetFont('',$style); } function PutLink($URL,$txt) { //Put a hyperlink $this->SetTextColor(0,0,255); $this->SetStyle('U',true); $this->Write(5,$txt,$URL); $this->SetStyle('U',false); $this->SetTextColor(0); } function WriteLn($height,$text,$height2 = 0) { $this->Write($height,$text); if($height2 > 0) { $this->Ln($height2); } else { $this->Ln(); } } function SetWidths($w) { //Set the array of column widths $this->widths=$w; } function SetAligns($a) { //Set the array of column alignments $this->aligns=$a; } function Row($data) { //Calculate the height of the row $nb=0; for($i=0;$iNbLines($this->widths[$i],$data[$i])); $h=5*$nb; //Issue a page break first if needed $this->CheckPageBreak($h); //Draw the cells of the row for($i=0;$iwidths[$i]; $a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L'; //Save the current position $x=$this->GetX(); $y=$this->GetY(); //Draw the border $this->Rect($x,$y,$w,$h); //Print the text $this->MultiCell($w,5,$data[$i],0,$a); //Put the position to the right of the cell $this->SetXY($x+$w,$y); } //Go to the next line $this->Ln($h); } function CheckPageBreak($h) { //If the height h would cause an overflow, add a new page immediately if($this->GetY()+$h>$this->PageBreakTrigger) $this->AddPage($this->CurOrientation); } function NbLines($w,$txt) { //Computes the number of lines a MultiCell of width w will take $cw=&$this->CurrentFont['cw']; if($w==0) $w=$this->w-$this->rMargin-$this->x; $wmax=($w-2*$this->cMargin)*1000/$this->FontSize; $s=str_replace("\r",'',$txt); $nb=strlen($s); if($nb>0 and $s[$nb-1]=="\n") $nb--; $sep=-1; $i=0; $j=0; $l=0; $nl=1; while($i<$nb) { $c=$s[$i]; if($c=="\n") { $i++; $sep=-1; $j=$i; $l=0; $nl++; continue; } if($c==' ') $sep=$i; $l+=$cw[$c]; if($l>$wmax) { if($sep==-1) { if($i==$j) $i++; } else $i=$sep+1; $sep=-1; $j=$i; $l=0; $nl++; } else $i++; } return $nl; } function ImprovedTable($header,$data,$invoice_net_sum,$invoice_vat_array) { global $invoice_settings; $w=explode(',',$this->lng('invoice_column_widths')); $this->SetWidths($w); $a = array('L','L','R','R','R'); $this->SetAligns($a); $this->SetFont('Arial','B',9); $this->Row($header); $this->SetFont('Arial','',8); foreach($data as $row) { $this->Row($row); } $invoice_vat = array_sum($invoice_vat_array); $this->Cell(130,6,$this->lng('net_amount_txt'),'LB',0,'L'); $this->Cell(50,6,$invoice_settings['currency'].' '.$this->currency_format($invoice_net_sum),'RB',1,'R'); $vat_txt = ''; foreach($invoice_vat_array as $tmp_rate => $tmp_sum) { $tmp_sum = $this->currency_format($tmp_sum); $vat_txt .= $this->lng('plus_txt')." $tmp_sum ".$this->lng('vat_at_txt')." $tmp_rate% "; } $vat_txt = substr($vat_txt,0,-1); $vat_txt .= '. '; $this->Cell(130,6,$vat_txt,'LB',0,'L'); $this->Cell(50,6,$this->lng('total_vat_txt').': '.$invoice_settings['currency'].' '.$this->currency_format($invoice_vat),'RB',1,'R'); $this->SetFont('Arial','B',10); $this->Cell(130,6,$this->lng('total_amount_txt'),'LB',0,'L'); $this->Cell(50,6,$invoice_settings['currency'].' '.$this->currency_format($invoice_net_sum+$invoice_vat),'RB',1,'R'); $this->SetFont('Arial','',8); $this->Ln(); /* //Column widths $w=array(10,120,20,10,20); //Header for($i=0;$iCell($w[$i],7,$header[$i],1,0,'C'); $this->Ln(); //Data foreach($data as $row) { $this->Cell($w[0],6,$row[0],'LR'); $this->Cell($w[1],6,$row[1],'LR'); $this->Cell($w[2],6,$row[2],'LR',0,'R'); $this->Cell($w[3],6,$row[3],'LR',0,'R'); $this->Cell($w[4],6,$row[4],'LR',0,'R'); $this->Ln(); } //Closure line $this->Cell(array_sum($w),0,'','T'); */ } function lng($text) { global $app; if(!is_array($this->wb)) { if(is_file('lib/lang/'.strtolower($this->client['language']).'_invoice_pdf.lng')) { include('lib/lang/'.strtolower($this->client['language']).'_invoice_pdf.lng'); } else { include('lib/lang/en_invoice_pdf.lng'); } $this->wb = $wb; } if(isset($this->wb[$text])) { return $this->wb[$text]; } else { return $text; } } function textdecode($records) { if(is_array($records)) { foreach($records as $key => $val) { if(is_array($val)) { $records[$key] = $this->textdecode($val); } else { $records[$key] = utf8_decode($val); } } } return $records; } function currency_format($value) { global $app; return $app->functions->currency_format($value); } function formatted_date($datestring) { /* $tmp_parts = explode('-',$datestring); $tmp_search = array('d','m','Y'); $tmp_replace = array($tmp_parts[2],$tmp_parts[1],$tmp_parts[0]); return str_replace($tmp_search,$tmp_replace,$invoice_settings['date_format']); */ $tmp = explode('-',$datestring); return date($this->invoice_settings['date_format'],mktime(0, 0, 0, $tmp[1] , $tmp[2], $tmp[0])); } function createInvoice($invoice_id) { global $app, $conf; $this->invoice_id = $invoice_id; $invoice = $this->textdecode($app->db->queryOneRecord("SELECT * FROM invoice WHERE invoice_id = ".$this->invoice_id)); $invoice_items = $this->textdecode($app->db->queryAllRecords("SELECT * FROM invoice_item WHERE invoice_id = ".$this->invoice_id)); $company = $this->textdecode($app->db->queryOneRecord("SELECT * FROM invoice_company WHERE invoice_company_id = ".$invoice['invoice_company_id'])); $payment_terms = $this->textdecode($app->db->queryOneRecord("SELECT * FROM invoice_payment_term WHERE invoice_payment_term_id = ".$invoice['payment_terms'])); $client = $this->textdecode($app->db->queryOneRecord("SELECT * FROM client WHERE client_id = ".$invoice['client_id'])); $invoice_settings = $this->textdecode($app->db->queryOneRecord("SELECT * FROM invoice_settings WHERE invoice_settings_id = 1")); $this->invoice = $invoice; $this->company = $company; $this->client = $client; $this->invoice_settings = $invoice_settings; //First page $this->AddPage(); $this->SetMargins(15,5,15); $this->SetFillColor(220); // Add the logo image if($conf['demo_mode'] == false) { $logo_path = 'lib/logo/'.$company['company_logo']; } else { $logo_path = 'lib/logo/demo_logo.png'; } if(is_file($logo_path)) $this->Image($logo_path,15,10,60); $short_company_name = (trim($company['company_name_short']) != '')?$company['company_name_short']:$company['company_name']; // Invoice sender $this->Ln(1); $this->SetFont('Arial','',10); $this->SetX(130); $this->WriteLn(5,$short_company_name); $this->SetX(130); $this->WriteLn(5,$company['street']); $this->SetX(130); $this->WriteLn(5,$company['zip'].' '.$company['city']); $this->SetX(130); if($company['country'] == 'DE') { $this->WriteLn(5,' '); } else { $this->WriteLn(5,$company['country']); } /* if(in_array($_POST['residence_country'], $vat_countries)){ $vat_pct = 0.19; $net = number_format(round($_POST['mc_gross']/(1 + $vat_pct),2), 2, '.', ','); $tax = number_format($_POST['mc_gross'] - $net, 2, '.', ','); } else { $vat_pct = 0; $net = number_format($_POST['mc_gross'], 2, '.', ','); $tax = number_format(0, 2, '.', ','); } */ $this->Ln(5); $this->SetX(130); $this->SetFont('Arial','B',14); if($invoice['invoice_type'] == 'invoice') { $this->WriteLn(5,$this->lng("invoice_txt")); } else { $this->WriteLn(5,$this->lng("refund_txt")); } // Client $this->Ln(5); $recipient_lines = array(); $current_recipient_line = 0; // add a empty line if we have enough space if($invoice['company_name'] == '' or $invoice['contact_name'] == '') $recipient_lines[] = ' '; // add only non empty rows in the recipient field if($invoice['company_name'] != '') $recipient_lines[] = $invoice['company_name']; if($invoice['contact_name'] != '') $recipient_lines[] = $invoice['contact_name']; if($invoice['street'] != '') $recipient_lines[] = $invoice['street']; if($invoice['city'] != '') $recipient_lines[] = $invoice['zip']." ".$invoice['city']; $this->SetFont('Arial','B',8); $this->Cell(70,6,$this->lng('bill_to_txt'),1); $this->SetX(130); $this->SetFont('Arial','B',8); $this->Cell(20,6,$this->lng('date_txt'),1,0,'C',1); $this->Cell(40,6,$this->lng('invoice_no_txt'),1,0,'C',1); $this->Ln(); $this->SetFont('Arial','',10); $this->Cell(70,6,$recipient_lines[$current_recipient_line].' ','LR'); $current_recipient_line++; $this->SetX(130); $this->SetFont('Arial','',8); //$tmp_parts = explode('-',$invoice['invoice_date']); //$this->Cell(20,6,"$tmp_parts[2].$tmp_parts[1].$tmp_parts[0]",1,0,'C',1); $this->Cell(20,6,$this->formatted_date($invoice['invoice_date']),1,0,'C',1); $this->Cell(40,6,$invoice['invoice_number'],1,0,'C',1); $this->Ln(); $this->SetFont('Arial','',10); $this->Cell(70,6,$recipient_lines[$current_recipient_line].' ','LR'); $current_recipient_line++; $this->SetX(130); $this->SetFont('Arial','',8); $this->Cell(60,6,$this->lng('payment_terms_txt').': '.$this->lng($payment_terms['description']),1,0,'C',1); if($invoice['vat_id'] != '') { $this->Ln(); $this->SetFont('Arial','',10); $this->Cell(70,6,$recipient_lines[$current_recipient_line].' ','LR'); $current_recipient_line++; } $this->SetX(130); if($invoice['vat_id'] != '') { $this->SetFont('Arial','B',8); $this->Cell(60,6,$invoice['company_name'].' VAT ID No.',1,0,'C',1); } $this->Ln(); $this->SetFont('Arial','',10); $this->Cell(70,6,$recipient_lines[$current_recipient_line].' ','LR'); $current_recipient_line++; $this->SetX(130); if($invoice['vat_id'] != '') { $this->SetFont('Arial','',8); $this->Cell(60,6,$invoice['vat_id'],1,0,'C',1); } else { $this->Ln(); $this->SetFont('Arial','',10); $this->Cell(70,6,$recipient_lines[$current_recipient_line].' ','LR'); $current_recipient_line++; } $this->Ln(); $this->SetFont('Arial','',10); if($invoice['country'] == 'DE') { $this->Cell(70,6,' ','LBR'); } else { $this->Cell(70,6,$invoice['country'],'LBR'); } $this->Ln(20); $header = array($this->lng('quantity_txt'),$this->lng('description_txt'),$this->lng('unit_price_txt'),$this->lng('vat_txt'),$this->lng('value_txt')); $data = array(); $invoice_net_sum = 0; $invoice_vat = array(); if(is_array($invoice_items)) { foreach($invoice_items as $item) { $tmp = array(); $tmp[0] = $item['quantity']; $tmp[1] = $app->db->unquote($item['description']); $tmp[2] = $this->currency_format($item['price']); $tmp[3] = $item['vat'].'%'; $tmp[4] = $this->currency_format($item['quantity']*$item['price']); $data[] = $tmp; $invoice_net_sum += $item['quantity']*$item['price']; $vat_rate = $item['vat']; $invoice_vat[$vat_rate] += ($item['quantity']*$item['price'])*($vat_rate/100); } } $this->ImprovedTable($header,$data,$invoice_net_sum,$invoice_vat); /* $this->SetFont('Courier','',10); $this->WriteLn(5,'(PayPal Transaction ID: '.$_POST['txn_id'].')'); $this->WriteLn(5," "); $this->WriteLn(5,str_pad("Item",65).str_pad("Qty.",12).str_pad("Amount", 7, ' ', STR_PAD_LEFT)); $this->WriteLn(5,"------------------------------------------------------------------------------------"); $this->WriteLn(5,$_POST['item_name'].' '.$_POST['item_number']); foreach($this_invoice_items as $this_invoice_item){ $this->WriteLn(5,$this_invoice_item); } $this->WriteLn(5,"------------------------------------------------------------------------------------"); $this->WriteLn(5,str_pad("Net Amount",32).":".str_pad($net,10,' ',STR_PAD_LEFT)." ".$_POST['mc_currency']); $this->WriteLn(5,str_pad("Total VAT @ ".($vat_pct*100)."%",32).":".str_pad($tax,10,' ',STR_PAD_LEFT)." ".$_POST['mc_currency']); $this->WriteLn(5,"-------------------------------------------------"); $this->SetFont('Courier','B',10); $this->WriteLn(5,str_pad("Total Amount",32).":".str_pad(number_format($_POST['mc_gross'], 2, '.', ','),10,' ',STR_PAD_LEFT)." ".$_POST['mc_currency'],15); */ if($this->lng("free_end_txt") != '') { $this->SetFont('Arial','',8); $this->WriteLn(5,$this->lng("free_end_txt"),8); $this->WriteLn(5,' ',8); } $this->SetFont('Arial','',10); $short_company_name = (trim($this->company['company_name_short']) != '')?$this->company['company_name_short']:$this->company['company_name']; $this->WriteLn(5,$this->lng("thank_you_txt"),8); //$this->WriteLn(5,"We appreciate your business.",10); $this->WriteLn(5,$this->lng("regards_txt"),8); $this->WriteLn(5,$short_company_name,8); } } /* function sendinvoice($to, $subject, $text, $from, $content, $type, $filename) { $content = chunk_split(base64_encode($content)); $uid = strtoupper(md5(uniqid(time()))); $header = "From: $from\nReply-To: $from\n"; $header .= "MIME-Version: 1.0\n"; $header .= "Content-Type: multipart/mixed; boundary=$uid\n"; $header .= "--$uid\n"; $header .= "Content-Type: text/plain\n"; $header .= "Content-Transfer-Encoding: 8bit\n\n"; $header .= "$text\n"; $header .= "--$uid\n"; $header .= "Content-Type: $type; name=\"$filename\"\n"; $header .= "Content-Transfer-Encoding: base64\n"; $header .= "Content-Disposition: attachment; filename=\"$filename\"\n\n"; $header .= "$content\n"; $header .= "--$uid--"; mail($to, $subject, "", $header); return true; } */ /* $pdf=new PDFInvoice(); $pdf->createInvoice($invoice_id); $pdf_content = $pdf->Output('doc.pdf','S'); $invoice_filename = ISPC_ROOT_PATH.'/invoices/invoice_'.$pdf->invoice['invoice_id'].'.pdf'; file_put_contents($invoice_filename,$pdf_content); */ ?>