<?
 /* Copy of report_stock as it exists for this update */
 $report_stock = array();
 $report_stock['fullpage'] = array('name' => 'Letter 8.5 x 11 (3/4" margin)',
			'page_format' => 'LETTER',
			'page_orientation' => 'P',
			'label_width' => 177.8,
			'label_height' => 241.3,
			'x_spacing' => 0,
			'y_spacing' => 0,
			'cols' => 1,
			'rows' => 1,
			);

 $report_stock['fullpage_landscape'] = array('name' => 'Letter 8.5 x 11 Landscape (3/4" margin)',
			'page_format' => 'LETTER',
			'page_orientation' => 'L',
			'label_width' => 241.3,
			'label_height' => 177.8,
			'x_spacing' => 0,
			'y_spacing' => 0,
			'cols' => 1,
			'rows' => 1,
			);

 $report_stock['fullpage_full'] = array('name' => 'Letter 8.5 x 11 (no margin)',
			'page_format' => 'LETTER',
			'page_orientation' => 'P',
			'label_width' => 215.9,
			'label_height' => 279.4,
			'x_spacing' => 0,
			'y_spacing' => 0,
			'cols' => 1,
			'rows' => 1,
			);

 $report_stock['fullpage_landscape_full'] = array('name' => 'Letter 8.5 x 11 Landscape (no margin)',
			'page_format' => 'LETTER',
			'page_orientation' => 'L',
			'label_width' => 279.4,
			'label_height' => 215.9,
			'x_spacing' => 0,
			'y_spacing' => 0,
			'cols' => 1,
			'rows' => 1,
			);

 $report_stock['5161'] = array('name' => 'Avery 5161/5261/5961/8161, G&T 99189 (1"x4")',
			'page_format' => 'LETTER',
			'page_orientation' => 'P',
			'label_width' => 101.6,
			'label_height' => 25.4,
			'x_spacing' => 3.81,
			'y_spacing' => 0.00,
			'cols' => 2,
			'rows' => 10,
			);
	
 $report_stock['5162'] = array('name' => 'Avery 5162/5262/5962/8162/8462, G&T 99190 (1 1/3"x4")',
			'page_format' => 'LETTER',
			'page_orientation' => 'P',
			'label_width' => 101.35,
			'label_height' => 33.6804,
			'x_spacing' => 4.7498,
			'y_spacing' => 0.00,
			'cols' => 2,
			'rows' => 7,
			);
 $report_stock['5163'] = array('name' => 'Avery 5163/5263/5963/8163/8463, G&T 99181 (2"x4")',
			'page_format' => 'LETTER',
			'page_orientation' => 'P',
			'label_width' => 101.6,
			'label_height' => 50.8,
			'x_spacing' => 4.3663,
			'y_spacing' => 0.00,
			'cols' => 2,
			'rows' => 5,
			);

 $report_stock['5164'] = array('name' => 'Avery 5164/5264/5964/8164, G&T 99763 (4"x3 1/3")',
			'page_format' => 'LETTER',	/* tcpdf format */
			'page_orientation' => 'P',	/* tcpdf orientation */
			'label_width' => 101.6,
			'label_height' => 84.6667,
			'x_spacing' => 4.7625,
			'y_spacing' => 0,
			'cols' => 2,
			'rows' => 3,

			);
 $report_stock['nametag'] = array('name' => 'Cards 4"x3"',
			'page_format' => 'LETTER',
			'page_orientation' => 'P',
			'label_width' => 101.6,
			'label_height' => 76.2,
			'x_spacing' => 0,
			'y_spacing' => 0,
			'cols' => 2,
			'rows' => 3,
			);

 $report_stock['letter_4up'] = array('name' => 'Fullpage, 4up',
			'page_format' => 'LETTER',
			'page_orientation' => 'P',
			'label_width' => 101.6,
			'label_height' => 127.0,
			'x_spacing' => 0.25,
			'y_spacing' => 0.25,
			'cols' => 2,
			'rows' => 2,
			);

 $report_stock['ledger'] = array('name' => 'Ledger/Tabloid 11 x 17',
			'page_format' => 'TABLOID',
			'page_orientation' => 'P',
			'label_width' => 279.4,
			'label_height' => 431.8,
			'x_spacing' => 0,
			'y_spacing' => 0,
			'cols' => 1,
			'rows' => 1,
			);
	
 $report_stock['ledger_landscape'] = array('name' => 'Ledger/Tabloid 11 x 17 Landscape',
			'page_format' => 'TABLOID',
			'page_orientation' => 'L',
			'label_width' => 431.8,
			'label_height' => 279.4,
			'x_spacing' => 0,
			'y_spacing' => 0,
			'cols' => 1,
			'rows' => 1,
			);

function db_update_175_pre() 
{
	global $report_stock;

	/* Fix the fair_name for report_type = fair */
	$q = mysql_query("SELECT id FROM reports WHERE `type`='fair'");
	while(($f = mysql_fetch_assoc($q))) {
		$id = $f['id'];
		mysql_query("UPDATE reports_items SET `field`='fairinfo_name' WHERE `field`='fair_name' AND reports_id='$id'");
	}
	
	/* Load all labels */
	$q = mysql_query("SELECT reports_id FROM reports_items WHERE `type`='option' AND `field`='type' AND `value`='label'");
	while(($r = mysql_fetch_assoc($q))) {
		$id = $r['reports_id'];

		$qq = mysql_query("SELECT * FROM reports_items WHERE `type`='option' AND `field`='stock' AND reports_id='$id'");
		if(mysql_num_rows($qq) != 1) {
			$stock = $report_stock['5164'];
		} else {
			$s = mysql_fetch_assoc($qq);
			$stock = $report_stock[$s['value']];
		}

		/* Get options: label_fairname and label_logo*/
		$use_name = false;
		$name = '';
		$qq = mysql_query("SELECT * FROM reports_items WHERE `type`='option' AND `field`='label_fairname' AND reports_id='$id'");
		if(mysql_num_rows($qq) == 1) {
			$name = mysql_fetch_assoc($qq);
			$use_name = ($name['value'] == 'yes') ? true : false; 
		}
		$use_logo = false;
		$logo = '';
		$qq = mysql_query("SELECT * FROM reports_items WHERE `type`='option' AND `field`='label_logo' AND reports_id='$id'");
		if(mysql_num_rows($qq) == 1) {
			$logo = mysql_fetch_assoc($qq);
			$use_logo = ($logo['value'] == 'yes') ? true : false;
		}

		/* Compute logo size if needed */
		if($use_logo) {
			$logo_w = $stock['label_width'] * 0.2;
			$logo_h = $stock['label_height'] * 0.2;
			if($logo_w < $logo_h) $logo_h = $logo_w;
			else $logo_w = $logo_h;
			$logo_x = 1;
			$logo_y = 1;
		} else {
			$logo_x = 0;
			$logo_y = 0;
			$logo_w = 0;
			$logo_h = 0;
		}

		/* Compute name size if needed */
		if($use_name) {
			$name_x = $logo_x + $logo_w + 1;
			$name_y = 1;
			$name_w = $stock['label_width'] - 1 - $logo_x - $logo_w;
			$name_h = ($logo_h == 0) ? ($stock['label_height'] * 0.2) : $logo_h;
			$cMargin = 1; /* TCPDF's cmargin is 1mm */
			$name_fs = ((($name_h / 3) - $cMargin) / 25.4) * 72;
		} else {
			$name_x=0;
			$name_y=0;
			$name_w=0;
			$name_h=0;
			$name_fs=0;
		}

		if($use_name || $use_logo) {
			$toph = ($logo_h == 0) ? $name_h : $logo_h;

			/* Scale all other cols */
			$qq = mysql_query("SELECT * FROM reports_items WHERE `type`='col' AND reports_id='$id'");
			$ord = 0;
			while(($col = mysql_fetch_assoc($qq))) {
				$scale = 1 - ($toph / $stock['label_height']);
				$y = $toph + ($col['y'] * $scale);
				$h = $col['h'] * $scale;
				$fs = $col['fontsize'] * $scale;

				mysql_query("UPDATE reports_items SET `y`='$y', `h`='$h', `fontsize`='$fs'
							WHERE id='{$col['id']}'");
				if($col['ord'] > $ord) $ord = $col['ord'];
			}

			/* Update items */
			if(is_array($name)) {
				$ord++;
				mysql_query("UPDATE reports_items SET `type`='col', `field`='fair_name', 
								x='$name_x', y='$name_y', w='$name_w', h='$name_h',
								fontsize='$name_fs', `ord`='$ord',
								align='left', valign='middle'
								WHERE id='{$name['id']}'");
			}
			if(is_array($logo)) {
				$ord++;
				mysql_query("UPDATE reports_items SET `type`='col', `field`='fair_logo', 
								x='$logo_x', y='$logo_y', w='$logo_w', h='$logo_h',`ord`='$ord'
								WHERE id='{$logo['id']}'");
			}
		}

	}
}

?>
