bluvertigo
4/16/2015 - 12:46 PM

Export data in excel / xls

Export data in excel / xls

<?php
// File Security Check
if ( ! empty( $_SERVER['SCRIPT_FILENAME'] ) && basename( __FILE__ ) == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
    die ( 'You do not have sufficient permissions to access this page!' );
}
?>
<?php
/**
 * Template Name: CSV Export
 *
 * This template is a full-width version of the page.php template file. It removes the sidebar area.
 *
 * @package WooFramework
 * @subpackage Template
 */
	get_header();
	global $woo_options;
?>
    
    <div id="content" class="page col-full">
    	<?php  if ( !post_password_required() ) : ?>
    <pre>
    	<?php 
    		$json_codici_nazioni = '[
  ["AF","AF","fedex"],
  ["AL","AL","fedex"],
  ["DZ","DZ","fedex"],
  ["AD","AND","fedex"],
  ["AO","AO","fedex"],
  ["AI","AI","fedex"],
  ["AG","AG","fedex"],
  ["AN","AN","fedex"],
  ["SA","SA","fedex"],
  ["AR","AR","fedex"],
  ["AM","AM","fedex"],
  ["AW","AW","fedex"],
  ["AU","AU","fedex"],
  ["AT","A","dpd"],
  ["AZ","AZ","fedex"],
  ["BS","BS","fedex"],
  ["BH","BH","fedex"],
  ["BD","BD","fedex"],
  ["BB","BB","fedex"],
  ["BE","B","dpd"],
  ["BZ","BZ","fedex"],
  ["BJ","BJ","fedex"],
  ["BM","BM","fedex"],
  ["BT","BT","fedex"],
  ["BY","BY","fedex"],
  ["BO","BO","fedex"],
  ["BA","BA","fedex"],
  ["BW","BW","fedex"],
  ["BR","BR","fedex"],
  ["BN","BN","fedex"],
  ["BG","BG","dpd"],
  ["BF","BF","fedex"],
  ["BI","BI","fedex"],
  ["KH","KH","fedex"],
  ["CM","CM","fedex"],
  ["CA","CA","fedex"],
  ["CV","CV","fedex"],
  ["TD","TD","fedex"],
  ["CL","CL","fedex"],
  ["CN","CN","fedex"],
  ["CY","CY","fedex"],
  ["CO","CO","fedex"],
  ["KR","KR","fedex"],
  ["CI","CI","fedex"],
  ["CR","CR","fedex"],
  ["HR","HR","euroexpress"],
  ["DK","DK","dpd"],
  ["DM","DM","fedex"],
  ["EC","EC","fedex"],
  ["EG","EG","fedex"],
  ["SV","SV","fedex"],
  ["AE","AE","fedex"],
  ["ER","ER","fedex"],
  ["EE","EE","dpd"],
  ["ET","ET","fedex"],
  ["PH","PH","fedex"],
  ["FI","FIN","dpd"],
  ["FR","F","fedex"],
  ["GA","GA","fedex"],
  ["GM","GM","fedex"],
  ["GE","GE","fedex"],
  ["DE","D","dpd"],
  ["GH","GH","fedex"],
  ["JM","JM","fedex"],
  ["JP","JP","fedex"],
  ["GI","GBZ","fedex"],
  ["DJ","DJ","fedex"],
  ["JO","JO","fedex"],
  ["GR","GR","dpd"],
  ["GD","GD","fedex"],
  ["GL","GL","fedex"],
  ["GP","GP","fedex"],
  ["GT","GT","fedex"],
  ["GY","GY","fedex"],
  ["HT","HT","fedex"],
  ["HN","HN","fedex"],
  ["HK","HK","fedex"],
  ["IN","IN","fedex"],
  ["ID","ID","fedex"],
  ["IQ","IQ","fedex"],
  ["IE","IRL","dpd"],
  ["IS","IS","fedex"],
  ["KY","KY","fedex"],
  ["CK","CK","fedex"],
  ["IL","IL","fedex"],
  ["KZ","KZ","fedex"],
  ["KE","KE","fedex"],
  ["KG","KG","fedex"],
  ["KW","KW","fedex"],
  ["LA","LA","fedex"],
  ["LS","LS","fedex"],
  ["LV","LV","dpd"],
  ["LB","LB","fedex"],
  ["LR","LR","fedex"],
  ["LI","LI","dpd"],
  ["LT","LT","dpd"],
  ["LU","L","dpd"],
  ["MO","MO","fedex"],
  ["MK","MK","fedex"],
  ["MG","MG","fedex"],
  ["MW","MW","fedex"],
  ["MV","MV","fedex"],
  ["MY","MY","fedex"],
  ["ML","ML","fedex"],
  ["MT","MT","fedex"],
  ["MA","MA","fedex"],
  ["MQ","MQ","fedex"],
  ["MR","MR","fedex"],
  ["MU","MU","fedex"],
  ["MX","MX","fedex"],
  ["FM","FM","fedex"],
  ["MD","MD","fedex"],
  ["MN","MN","fedex"],
  ["ME","ME","fedex"],
  ["MS","MS","fedex"],
  ["MZ","MZ","fedex"],
  ["NA","NA","fedex"],
  ["NP","NP","fedex"],
  ["NI","NI","fedex"],
  ["NE","NE","fedex"],
  ["NG","NG","fedex"],
  ["NO","N","fedex"],
  ["NC","NC","fedex"],
  ["NZ","NZ","fedex"],
  ["NL","NL","dpd"],
  ["OM","OM","fedex"],
  ["PK","PK","fedex"],
  ["PA","PA","fedex"],
  ["PG","PG","fedex"],
  ["PY","PY","fedex"],
  ["PE","PE","fedex"],
  ["PL","PL","dpd"],
  ["PT","P","euroexpress"],
  ["MC","MC","dpd"],
  ["QA","QA","fedex"],
  ["GB","GB","dpd"],
  ["CZ","CZ","dpd"],
  ["DO","DO","fedex"],
  ["SK","SK","dpd"],
  ["RE","RE","fedex"],
  ["RO","RO","dpd"],
  ["RU","RU","fedex"],
  ["RW","RW","fedex"],
  ["SN","SN","fedex"],
  ["RS","RS","fedex"],
  ["SC","SC","fedex"],
  ["SG","SG","fedex"],
  ["SI","SLO","dpd"],
  ["SO","SO","fedex"],
  ["ES","E","euroexpress"],
  ["LK","LK","fedex"],
  ["KN","KN","fedex"],
  ["LC","LC","fedex"],
  ["VC","VC","fedex"],
  ["ZA","ZA","fedex"],
  ["SR","SR","fedex"],
  ["SE","S","dpd"],
  ["CH","CH","fedex"],
  ["SZ","SZ","fedex"],
  ["TH","TH","fedex"],
  ["TW","TW","fedex"],
  ["TZ","TZ","fedex"],
  ["TL","TL","fedex"],
  ["TG","TG","fedex"],
  ["TO","TO","fedex"],
  ["TT","TT","fedex"],
  ["TN","TN","fedex"],
  ["TR","TR","fedex"],
  ["TV","TV","fedex"],
  ["UA","UA","fedex"],
  ["UG","UG","fedex"],
  ["HU","HU","dpd"],
  ["UY","UY","fedex"],
  ["US","US","fedex"],
  ["UZ","UZ","fedex"],
  ["VU","VU","fedex"],
  ["VE","VE","fedex"],
  ["VN","VN","fedex"],
  ["YE","YE","fedex"],
  ["ZM","ZM","fedex"],
  ["ZW","ZW","fedex"]
]';

			$associaNazioniBrt = convertiNazioniBrtWoo(json_decode($json_codici_nazioni));

    		$metodo_contrassegno = "Pagamento con assegno";

			$var_orders= new WP_Query(array('post_type'=>'shop_order','posts_per_page'=>-1, 'order'=>'ASC', 'post_status' => array( 'wc-processing', 'wc-on-hold' )));
			$order_rows = array();

			// prima riga
			$order_row = array();
			$order_row[0] = "VABATB";
			$order_row[1] = "VABCCM";
			$order_row[2] = "VABCTR";
			$order_row[3] = "VABCBO";
			$order_row[4] = "VABLNP";
			$order_row[5] = "VABRSD";
			$order_row[6] = "VABRD2";
			$order_row[7] = "VABIND";
			$order_row[8] = "VABCAD";
			$order_row[9] = "VABLOD";
			$order_row[10] = "VABPRD";
			$order_row[11] = "VABNZD";
			$order_row[12] = "VABTRC";
			$order_row[13] = "VABEMD";
			$order_row[14] = "VABCEL";
			$order_row[15] = "VABTNO";
			$order_row[16] = "VABTSP";
			$order_row[17] = "VABNCL";
			$order_row[18] = "VABPKB";
			$order_row[19] = "VABCAS";
			$order_row[20] = "VABVCA";
			$order_row[21] = "VABTIC";
			$order_row[22] = "VABIAS";
			$order_row[23] = "VABVAS";
			$order_row[24] = "VABNOT";
			$order_row[25] = "VABNT2";
			$order_row[26] = "VABRMA";
			$order_row[27] = "VABFFD";
			$order_row[28] = "VABTC1";
			$order_row[29] = "VABTC2";

			$order_rows[] = $order_row;

			if($var_orders){
				foreach ($var_orders->posts as $key => $single_order){

					$order_row = array();

					$order = new WC_Order($single_order->ID);

					// se "in attesa" e il pagamento è diverso da "Pagamento con assegno" vado al prossimo ciclo
					if( $order->post_status == "wc-on-hold" ) {
						if( $order->payment_method_title != $metodo_contrassegno ) {
							continue;
						}
					}

					// CAMPO DI COMODO PER SPEDIZIONI ESTERE
					if( $order->billing_country == 'IT' ) 
						$order_row[0] = ""; 
					else 
						$order_row[0] = $associaNazioniBrt[$order->billing_country]['ship_type'];

					// CODICE CLIENTE
					$order_row[1] = "1341086";

					// TARIFFA
					$order_row[2] = "000";

					// PORTO=1 FRANCO / 4 FRANCO + CONTRASSEGNO
					if($order->payment_method_title == $metodo_contrassegno) {
						$order_row[3] = "4";
					} else {
						$order_row[3] = "1";
					}

					//var_dump($order);

					// LINEA PARTENZA
					$order_row[4] = "134";

					// RAGIONE SOCIALE LUNGHEZZA MAX 35
					$order_row[5] = $order->billing_first_name . " " . $order->billing_last_name;

					// ESTENSIONE RAGIONE SOCIALE LUNGHEZZA MAX 35
					$order_row[6] = "";

					// INDIRIZZO
					$order_row[7] = $order->billing_address_1;

					// CAP
					$order_row[8] = $order->billing_postcode;

					// LOCALITA
					$order_row[9] = $order->billing_city;

					// PROVINCIA
					$order_row[10] = $order->billing_state;

					// NAZIONE: ITALIA=BLANK
					if( $order->billing_country == 'IT' ) 
						$order_row[11] = ""; 
					else 
						$order_row[11] = $associaNazioniBrt[$order->billing_country]['brt'];

					// TELEFONO
					$order_row[12] = $order->billing_phone;

					// EMAIL PER TRACKING
					$order_row[13] = $order->billing_email;

					// CELLULARE PER INVIO TRACKING
					$order_row[14] = "";

					// CAMPO TIPO NOTIFICA= 1 PER SOLO MAIL / 2 PER SOLO CELLULARE / 3 PER ENTRAMBI 
					$order_row[15] = "1";

					// TIPO SERVIZIO C=STANDARD / E=ORE 12 / H=10.30
					$order_row[16] = "C";

					// COLLI
					$order_row[17] = $order->get_item_count() ;

					// PESO
					$order_row[18] = $order->get_item_count();

					// IMPORTO CONTRASSEGNO
					if($order->payment_method_title == $metodo_contrassegno) {
						$order_row[19] = str_replace(".", ",", str_replace(",", "", $order->order_total));
					} else {
						$order_row[19] = "";
					}

					// VALUTA
					$order_row[20] = "EUR";

					// MODALITA INCASSO CONTRASSEGNO:BLANK=CONTANTI / BM=ASSEGNO A VISTA INTESTA AL MITTENTE / TM=TITOLI COME RILASCIATI
					$order_row[21] = "";

					// IMPORTO ASSICURAZIONE
					$order_row[22] = "";

					// VALUTA ASSICURAZIONE
					$order_row[23] = "";

					// EVENTUALI NOTE:35 CARATTERI
					$order_row[24] = "";

					// NOTE AGGIUNTIVE (35 CARATTERI)
					$order_row[25] = "";

					// NUMERO BOLLA
					$order_row[26] = "";

					// FERMO DEPOSITO
					$order_row[27] = "";

					// CONSEGNE PARTICOLARI
					$order_row[28] = "";

					// VABTC2
					$order_row[29] = "";

					$order_rows[] = $order_row;
				}
				
			}


			//var_dump($order_rows);

			$csvfile = fopen(get_stylesheet_directory() . '/csv/export_orders.csv' , 'w');
			foreach ($order_rows as $row) {
			    $line = join(";", $row) . "\n";
			    fputs($csvfile, $line);
			}
			fclose($csvfile);

		?>
	</pre>
	<div>
	<?php echo do_shortcode('[box type="download"]<a href="'. get_stylesheet_directory_uri() . '/csv/export_orders.csv' .'">Scarica il file CSV</a>[/box]'); ?>
	</div>
	<?php else : ?>
	<?php echo get_the_password_form(); ?>
	<?php endif; ?>
    </div><!-- /#content -->
	
<?php get_footer(); ?>