<?php
defined('BASEPATH') or exit('No direct script access allowed');

/////////////// begin DEFINE ///////////////////////

		$CI =& get_instance();
		//--------------
		$CI->load->library('app_number_to_word', [
            'clientid' => $invoice->id,
        ], 'numberword');  //<-------
		//--------------
		
 //echo $CI->numberword->convert(145, $cash_receipt['invoice_data']->currency_name, 0, 1, false, true, 'C', true);


	
	
	if(!function_exists('header_pdf')){

		function header_pdf($cash_receipt, $company, $invoice, $pdf, $font_name){

		$CI =& get_instance();
		//--------------
		$CI->load->library('app_number_to_word', [
            'clientid' => $invoice->id,
        ], 'numberword');  

			$dimensions = $pdf->getPageDimensions();
			$pdf->SetFont($font_name, 'B', 10);
			$pdf->SetFillColor(251, 248, 248);
			$pdf->SetFillColor(255, 255, 255);
			


			$info_right_column1 = '';
			$info_left_column1  = '';
			$info_right_column1 .= '<span style="font-weight:bold;font-size:27px;">' . _l('cash_receipt_pdf_heading') . '</span><br />';

			$__number = $cash_receipt['number'];
        	$_padded_number = str_pad($__number, get_option('number_padding_prefixes'), '0', STR_PAD_LEFT);
			$info_right_column1 .= '<b style="color:#4e4e4e;"># ' . $cash_receipt['prefix'].''.$_padded_number. '</b>';


			// Add logo
			//$info_left_column .= pdf_logo_url();

							

			//$info_left_column .= ' <img src="' . $invoice->logoUrl . '" width="150" class="invoice-logo">';
			$info_left_column1 .= ' <img src="/uploads/company/'.$company['logoUrl'].'" width="150" class="invoice-logo">';
			$pdf->ln(4);
			$pdf->SetFont($font_name, '', 10);
			// Write top left logo and right column info/text
			pdf_multi_row($info_left_column1, $info_right_column1, $pdf, ($dimensions['wk'] / 2) - $dimensions['lm']);

			$info_right_column = '';
			$info_left_column  = '';
			$info_left_column .= '<br><h4 class="inv-title-1">'. _l('billing_vendor') .'</h4>';
			$info_left_column .= '<p class="p">'. mb_strtoupper($company['name']).'<br>';
			if($company['trade_no']){
				$info_left_column .= ''._l('billing_registrar_of_companies').': '.$company['trade_no'].'<br>';
			}
			if($company['vat_eu']){
				$info_left_column .= ''._l('billing_vat_code').': '.$company['vat_eu'].'<br>';	
			}
			if($company['address'] || $company['state'] || $company['city']){ 
				$info_left_column .= ''._l('billing_vendor_address').': '.format_address_line($company).'<br>';	
			}
			if((int)$company['invoice_settings']['view_website'] == 1 AND $company['website']){
				$info_left_column .= ''._l('billing_vendor_web') .': '. $company['website'].'<br>';
			}
			if((int)$company['invoice_settings']['view_phone'] == 1 AND $company['fax']){
				$info_left_column .= ''._l('billing_vendor_tel').':'. $company['phonenumber']. ' // ' .$company['fax'].'<br>'; 
			}
			if((int)$company['invoice_settings']['view_email'] == 1 AND $company['email']){
				$info_left_column .= ''._l('billing_vendor_email') .': '. $company['email'].'<br>'; 
			}
			if ( ! empty($company['share_capital']) AND $company['share_capital']) { 
				$info_left_column .= ''._l('billing_vendor_share_capital').': '.$company['share_capital'].'<br>'; 
			}
			$info_left_column .= '</p>';



			$info_right_column .= '<br><h4 class="inv-title-1">'. _l('billing_customer') .'</h4>';
			$info_right_column .= ' <p class="p">'. mb_strtoupper($cash_receipt['invoice_data']->client->company). '<br>';

			if ( ! empty($cash_receipt['invoice_data']->client->vat)) {
			$info_right_column .= ''._l('billing_registrar_of_companies').': '.$cash_receipt['invoice_data']->client->vat. '<br>';
			} 
			if ( ! empty($cash_receipt['invoice_data']->client->vat)) {
			$info_right_column .= ''._l('billing_vat_code').': '.$cash_receipt['invoice_data']->client->vat.'<br>';
			}
			if ( ! empty($cash_receipt['invoice_data']->client->address)) {
			$info_right_column .= ''._l('billing_vendor_address').': '.$cash_receipt['invoice_data']->client->address.'<br>';
			}
			if ( ! empty($cash_receipt['invoice_data']->client->city)) { 
			$info_right_column .= ''._l('billing_vendor_state').': '.$cash_receipt['invoice_data']->client->city.', '.get_state_name($cash_receipt['invoice_data']->client->state).'<br>';
			}
			if ( ! empty($cash_receipt['invoice_data']->client->organization_email)) {
			$info_right_column .= ''._l('billing_vendor_email').': '.$cash_receipt['invoice_data']->client->organization_email.'<br>';
			}
			$info_right_column .= '</p>';

			$pdf->SetFont($font_name, '', 10);
			// Write top left logo and right column info/text
			pdf_multi_row($info_left_column, $info_right_column, $pdf, ($dimensions['wk'] / 2) - $dimensions['lm']);
			
			

			$pdf->MultiCell(73, 10, '', 0, 'C', 1, 0, '', '', true, 0, false, true, 10, 'M');
			$pdf->SetFillColor(251, 248, 248);




			////////////////////////////////

			
			$y_line = $pdf->GetY() + 1;
			
		

			if (!extension_loaded('gd')) {
				$pdf->Error('GD not loaded');
			}


			$pdf->ln(5);
			// MultiCell($w, $h, $txt, $border=0, $align='J', $fill=0, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0)
			$x_line = $pdf->GetX() + 1;
			$y_line = $pdf->GetY() + 1;
			
			$pdf->SetXY($x_line, $y_line);
			
			$pdf->SetFillColor(251, 248, 248);
			$pdf->MultiCell(93, 10,  _l('cash_receipt_pdf_heading') . ': '.$cash_receipt['invoice_data']->prefix.''.$cash_receipt['invoice_data']->number, 0, 'C', 1, 0, '', '', true, 0, false, true, 10, 'M');
			$pdf->SetFillColor(37, 204, 126);
			$pdf->MultiCell(4, 10, '', 0, 'C', 1, 0, '', '', true, 0, false, true, 10, 'M');
			$pdf->SetFillColor(251, 248, 248);
			$pdf->MultiCell(93, 10, _l('billing_pdf_date').': '.$cash_receipt['invoice_data']->date, 0, 'C', 1, 1, '', '', true, 0, false, true, 10, 'M');

			$pdf->ln(5);
			$pdf->Write(5, ''.ucfirst(_l('receipt_received_from')).': '. mb_strtoupper($cash_receipt['invoice_data']->client->company). ', '.strtolower(_l('billing_vat_code')).': '.$cash_receipt['invoice_data']->client->vat.', ');
			$pdf->ln(5);
			$pdf->Write(5, _l('receipt_client_adress').': '.$cash_receipt['invoice_data']->client->address.' '.$cash_receipt['invoice_data']->client->city.', '.get_state_name($cash_receipt['invoice_data']->client->state).', ');
			$pdf->ln(5);
			$pdf->Write(5, _l('amount_of').': '.$cash_receipt['invoice_data']->total.' '.$cash_receipt['invoice_data']->currency_name.', '._l('receipt_meaning').' '. $CI->numberword->convert($cash_receipt['invoice_data']->total, $cash_receipt['invoice_data']->currency_name, 0, 1, false, true, 'C', true).', ');
			$pdf->ln(5);
			$pdf->Write(5, _l('receipt_represents_invoice_value').' '.$invoice->prefix.' '._l('receip_invoice_no').' '.$invoice->number.' '._l('receip_invoice_from').' '.$invoice->date.'.');
			
			$pdf->ln(10);
			$pdf->Write(5, '                                                                                                                                                        Casier,');

//////////////////// second


		    $pdf->SetXY($x_line, $y_line+78);
			
			pdf_multi_row($info_left_column1, $info_right_column1, $pdf, ($dimensions['wk'] / 2) - $dimensions['lm']);
			pdf_multi_row($info_left_column, $info_right_column, $pdf, ($dimensions['wk'] / 2) - $dimensions['lm']);
			$pdf->ln(5);
			$pdf->SetFillColor(251, 248, 248);
			$pdf->MultiCell(93, 10,  _l('cash_receipt_pdf_heading') . ': '.$cash_receipt['invoice_data']->prefix.''.$cash_receipt['invoice_data']->number, 0, 'C', 1, 0, '', '', true, 0, false, true, 10, 'M');
			$pdf->SetFillColor(37, 204, 126);
			$pdf->MultiCell(4, 10, '', 0, 'C', 1, 0, '', '', true, 0, false, true, 10, 'M');
			$pdf->SetFillColor(251, 248, 248);
			$pdf->MultiCell(93, 10, _l('billing_pdf_date').': '.$cash_receipt['invoice_data']->date, 0, 'C', 1, 1, '', '', true, 0, false, true, 10, 'M');

			$pdf->ln(5);
			$pdf->Write(5, ''.ucfirst(_l('receipt_received_from')).': '. mb_strtoupper($cash_receipt['invoice_data']->client->company). ', '.strtolower(_l('billing_vat_code')).': '.$cash_receipt['invoice_data']->client->vat.', ');
			$pdf->ln(5);
			$pdf->Write(5, _l('receipt_client_adress').': '.$cash_receipt['invoice_data']->client->address.' '.$cash_receipt['invoice_data']->client->city.', '.get_state_name($cash_receipt['invoice_data']->client->state).', ');
			$pdf->ln(5);
			$pdf->Write(5, _l('amount_of').': '.$cash_receipt['invoice_data']->total.' '.$cash_receipt['invoice_data']->currency_name.', '._l('receipt_meaning').' '. $CI->numberword->convert($cash_receipt['invoice_data']->total, $cash_receipt['invoice_data']->currency_name, 0, 1, false, true, 'C', true).', ');
			$pdf->ln(5);
			$pdf->Write(5, _l('receipt_represents_invoice_value').' '.$invoice->prefix.' '._l('receip_invoice_no').' '.$invoice->number.' '._l('receip_invoice_from').' '.$invoice->date.'.');
			
			$pdf->ln(10);
			$pdf->Write(5, '                                                                                                                                                        Casier,');

		}
	}

	if(!function_exists('table_lines_pdf')){
		function table_lines_pdf ($cash_receipt, $company, $invoice, $pdf, $font_name){
			


			// linii orizontale
			$pdf->SetDrawColor(192, 192, 192);
			$pdf->Line(10,10,200,10); // ------------ sus
			$pdf->Line(10,129, 200,129); // ------------ sus
			$pdf->Line(10,149,200,149); // ------------ sus
			$pdf->Line(10,266,200,266); // ------------ sus
			
			$pdf->SetFont($font_name, 'I', 8);	

			// linii vericale
			$pdf->Line(10,10,10,129); // |||||
			$pdf->Line(200,10,200,129); // |||||

			$pdf->Line(10,149,10,266); // |||||
			$pdf->Line(200,149,200,266); // |||||

			
			$pdf->SetXY(20, 130);
			$pdf->writeHTML( '<b></b>'._l('emited_with').'<b> <em><a href="www.crmconnect.ro">www.CRMconnect.ro</a></em></b>');
			$pdf->SetXY(20, 267);
			$pdf->writeHTML( '<b></b>'._l('emited_with').'<b> <em><a href="www.crmconnect.ro">www.CRMconnect.ro</a></em></b>');
			//$pdf->Write(5, _l('emited_with').' CRMconnect.ro' );


			$pdf->SetFont($font_name, 'B', 8);
			$pdf->SetFillColor(251, 248, 248);
			//$pdf->MultiCell(73, 10, 'Nr. Crt.', 0, 'C', 1, 0, '', '', true, 0, false, true, 10, 'M');
			
			
			
		}
	}

	//////////// first page 

	header_pdf($cash_receipt, $company, $invoice, $pdf, $font_name);

	table_lines_pdf ($cash_receipt, $company, $invoice, $pdf, $font_name);

