<?php
/*
  www.oscGold.com (C) Kamelia-Net Jacek Krysiak
  modul oscGold wer 4.0
*/
?>

<?php
	if ($messageStack->size('account') > 0) {
		echo $messageStack->output('account');
	}
?>

<table border="0" width="100%" cellspacing="0" cellpadding="0" align="center">
	<!-- Naglowek BOF -->
	<tr>
		<td>

			<?php
			$info_box_contents = array();
			$info_box_contents[] = array('text' => sprintf(HEADING_TITLE));
			new contentBoxHeading($info_box_contents);
			?>

		</td>
	</tr>
	<tr>
		<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '5'); ?></td>
	</tr>
	<!-- Naglowek EOF -->

	<tr>
		<td>
			<table width="100%" cellspacing="0" cellpadding="1" class="infoBox">
				<tr>
					<td>
						<table width="100%" cellspacing="0" cellpadding="0" class="infoBoxContents">
							<tr>
								<td>
									<table border="0" width="100%" cellspacing="0" cellpadding="4">

				<!-- TRESC STRONY START -->

					<?php
					if (tep_count_customer_orders() > 0) {
						?>
						<tr>
							<td>
								<fieldset>
								<legend><?php echo OVERVIEW_PREVIOUS_ORDERS . '&nbsp;<a href="' . tep_href_link(FILENAME_ACCOUNT_HISTORY, '', 'SSL') . '"><u>' . OVERVIEW_SHOW_ALL_ORDERS . '</u></a>'; ?></legend>
									<table border="0" width="100%" cellspacing="0" cellpadding="2">
										<tr>
											<td>
												<table border="0" width="100%" cellspacing="0" cellpadding="2">
													<?php
													$orders_query = tep_db_query("select o.orders_id, o.date_purchased, o.delivery_name, o.delivery_country, o.billing_name, o.billing_country, ot.text as order_total, s.orders_status_name from " . TABLE_ORDERS . " o, " . TABLE_ORDERS_TOTAL . " ot, " . TABLE_ORDERS_STATUS . " s where o.customers_id = '" . (int)$customer_id . "' and o.orders_id = ot.orders_id and ot.class = 'ot_total' and o.orders_status = s.orders_status_id and s.language_id = '" . (int)$languages_id . "' and s.public_flag = '1' order by orders_id desc limit 3");

													while ($orders = tep_db_fetch_array($orders_query)) {
														if (tep_not_null($orders['delivery_name'])) {
															$order_name = $orders['delivery_name'];
															$order_country = $orders['delivery_country'];
														} else {
															$order_name = $orders['billing_name'];
															$order_country = $orders['billing_country'];
														}
														?>
														<tr class="moduleRow" onMouseOver="rowOverEffect(this)" onMouseOut="rowOutEffect(this)" onClick="document.location.href='<?php echo tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, 'order_id=' . $orders['orders_id'], 'SSL'); ?>'">
															<td class="main"><?php echo tep_date_short($orders['date_purchased']); ?></td>
															<td class="main"><?php echo '#' . $orders['orders_id']; ?></td>
															<td class="main"><?php echo $orders['orders_status_name']; ?></td>
															<td class="main"><?php echo tep_output_string_protected($order_name) . '<br>' . $order_country; ?></td>
															<td class="main" align="right" nowrap><?php echo $orders['order_total']; ?></td>
															<td class="main" align="right"><?php echo '<a href="' . tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, 'order_id=' . $orders['orders_id'], 'SSL') . '">' . tep_image_button('small_view.gif', SMALL_IMAGE_BUTTON_VIEW) . '</a>'; ?></td>

<?php
if ($language == 'polish') {
  ?>
  <td class="main" align="center" width="10%">
    <?php
    // obliczanie unikalnego ID
    $nr_zamow = (int)$orders['orders_id'];
    $nr_klienta = (int)$customer_id;
    $oblicz_id = ($nr_zamow+489)+(($nr_klienta*3)+511)+tep_date_unix($orders['date_purchased']);
    //
    // sprawdzenia czy zostal wystawiony oryginal zeby mozna wystawic kopie lub duplikat
    if (CUSTOMER_DUPLIKAT == 'true') {
      $zapytanie = tep_db_query('select invoice_nr,invoice_date FROM ' . TABLE_ORDERS . '  WHERE orders_id=' . (int)$orders['orders_id']);
      $tablica = tep_db_fetch_array($zapytanie);
      if ($tablica['invoice_nr'] != 0) {
        //if (file_exists('administracja/'.POLISH_INVOICE_PDF_ARCHIVE.'faktura_zam_'.$oblicz_id.'.pdf')) {
          echo '<a href="administracja/'.POLISH_INVOICE_PDF_ARCHIVE.'faktura_zam_'.$oblicz_id.'.pdf" TARGET="_blank">' . tep_image(DIR_WS_TEMPLATES . '/images/misc/icons/faktura_d_pdf.gif', ICON_PDF_DUPLIKAT_INVOICE) . '</a>&nbsp;';
        //} else {
        //  echo '<a href="' . tep_href_link('pdffaktura_customer.php', 'type=4&order_id=' .$orders['orders_id']) . '" TARGET="_blank">' . tep_image(DIR_WS_TEMPLATES . '/images/misc/icons/faktura_d_pdf.gif', ICON_PDF_DUPLIKAT_INVOICE) . '</a>&nbsp;';
        //}
      }
    }
    //
    if (CUSTOMER_PROFORMA == 'true') {
      if (file_exists('administracja/'.POLISH_INVOICE_PDF_ARCHIVE.'faktura_zam_'.$oblicz_id.'_proforma.pdf')) {
        echo '<a href="administracja/'.POLISH_INVOICE_PDF_ARCHIVE.'faktura_zam_'.$oblicz_id.'_proforma.pdf" TARGET="_blank">' . tep_image(DIR_WS_TEMPLATES . '/images/misc/icons/faktura_f_pdf.gif', ICON_PDF_PROFORMA_INVOICE) . '</a>&nbsp;';
      } else {
        echo '<a href="' . tep_href_link('pdffaktura_customer.php', 'type=3&order_id=' .$orders['orders_id']) . '" TARGET="_blank">' . tep_image(DIR_WS_TEMPLATES . '/images/misc/icons/faktura_f_pdf.gif', ICON_PDF_PROFORMA_INVOICE) . '</a>';
      }
    }
  ?>
  </td>
<?php } ?>

														</tr>
														<?php
													}
													?>
												</table>
											</td>
										</tr>
									</table>
								</fieldset>
							</td>
						</tr>
						<?php
					}
					?>

										<?php
		if (strstr(MODULE_ORDER_TOTAL_INSTALLED, "ot_loyalty_discount.php") !== false) {
											?>

											<tr>
												<td>
													<fieldset>
													<legend><?php echo MY_DISCOUNTS_TITLE; ?></legend>
														<table border="0" width="100%" cellspacing="0" cellpadding="2">
															<tr>
																<td>
																	<table border="0" width="100%" cellspacing="0" cellpadding="2">

<?php
// Display Loyalty discount for customer plus the table and his current place in it.
//include (DIR_WS_MODULES.'order_total/ot_loyalty_discount.php');
$loyalty = new ot_loyalty_discount;
$cust_tot_order_amount = $loyalty->get_cum_order_total();
$table_cost = split("[:,]" , MODULE_LOYALTY_DISCOUNT_TABLE);
$discount_perc = 0;

for ($i = 0; $i < count($table_cost); $i+=2) {
  if ($cust_tot_order_amount >= $table_cost[$i]) {
    $discount_perc = $table_cost[$i+1];
  }
}

if ($cust_tot_order_amount > 0) {
  $order_hist_text = OT_LOYALTY_HISTORY;
  $order_hist_text .= OT_LOYALTY_SUM . $currencies->display_price3($cust_tot_order_amount,0) . '<br><br>';

  if ( $cust_tot_order_amount < $table_cost[0] ) {
    $order_hist_text .= OT_LOYALTY_ELIGIBLE_NO;
  } else {
    $order_hist_text .= OT_LOYALTY_ELIGIBLE . $discount_perc . OT_LOYALTY_PERCANTAGE;
  }
} else {
  $order_hist_text = OT_LOYALTY_ELIGIBLE_NO;
}

$order_hist_text .= '<br><br>' . OT_LOYALTY_LIST_TABLE . ':<br>';
  $order_hist_text .= OT_LOYALTY_PERIOD . ' - ';

  if ( MODULE_LOYALTY_DISCOUNT_CUMORDER_PERIOD == 'alltime' ) {
    $order_hist_text .= MODULE_LOYALTY_DISCOUNT_ALLTIME;
  } elseif ( MODULE_LOYALTY_DISCOUNT_CUMORDER_PERIOD == 'year' ) {
    $order_hist_text .= MODULE_LOYALTY_DISCOUNT_YEAR;
  } elseif ( MODULE_LOYALTY_DISCOUNT_CUMORDER_PERIOD == 'quarter' ) {
    $order_hist_text .= MODULE_LOYALTY_DISCOUNT_QUARTER;
  } elseif ( MODULE_LOYALTY_DISCOUNT_CUMORDER_PERIOD == 'month' ) {
    $order_hist_text .= MODULE_LOYALTY_DISCOUNT_MONTH;
  }

$order_hist_text .= '<table cellpadding="3" cellspacing="1" border="0" width="100%">';
$order_hist_text .= '
  <tr>
    <td align="center" class="ProductHead"><b>' . OT_LOYALTY_AMOUNT .'</b></td>
    <td align="center" class="ProductHead">&nbsp</td>
    <td align="center" class="ProductHead"><b>' . OT_LOYALTY_PERCENT .'</b></td>
    <td align="center" class="ProductHead"><b>' . OT_LOYALTY_TO_GO . '</b></td>
  </tr>';

for ($i = 0; $i < count($table_cost); $i+=2) {
  if ($discount_perc >= $table_cost[$i+1]) {
    $order_hist_text .= '
    <tr>
      <td align="center" class="main">' . $currencies->display_price3($table_cost[$i]) . '</td>
      <td align="center" class="main"><img src="'.DIR_WS_TEMPLATES.'images/misc/ball_green.gif" alt="' . OT_LOYALTY_DISCOUNT1 .'" title="' . OT_LOYALTY_DISCOUNT1 .'"></td>
      <td align="center" class="main">' . $table_cost[$i+1] . '%</td>
      <td align="right">&nbsp;</td>
    </tr>';
  } else {
    $order_hist_text .= '
    <tr>
      <td align="center" class="main">' . $currencies->display_price3($table_cost[$i]) . '</td>
      <td align="center" class="main"><img src="'.DIR_WS_TEMPLATES.'images/misc/ball_red.gif" alt="' . OT_LOYALTY_DISCOUNT2 . '" title="' . OT_LOYALTY_DISCOUNT2 . '"></td>
      <td align="center" class="main">' . $table_cost[$i+1] . '%</td>
      <td align="center" class="main">' . $currencies->display_price3($table_cost[$i]-$cust_tot_order_amount) . '</td>
    </tr>';
  }
}
$order_hist_text .= '</table>';
?>

	<tr>
	  <td class="main"><?php echo $order_hist_text; ?></td>
	</tr>
												</table>
											</td>
										</tr>
									</table>
								</fieldset>
							</td>
						</tr>
<!-- End display Loyalty discount for customer plus the table and his current place in it. -->
<?php } ?>
					<tr>
						<td>
							<fieldset>
							<legend><?php echo MY_ACCOUNT_TITLE; ?></legend>
								<table border="0" width="100%" cellspacing="0" cellpadding="2">
									<tr>
										<td width="60"><?php echo tep_image(DIR_WS_TEMPLATES . 'images/misc/account_personal.gif'); ?></td>
										<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
										<td>
											<table border="0" width="100%" cellspacing="0" cellpadding="2">
												<tr>
													<td class="main"><?php echo tep_image(DIR_WS_TEMPLATES . 'images/misc/arrow_green.gif') . ' <a class="boxLink" href="' . tep_href_link(FILENAME_ACCOUNT_EDIT, '', 'SSL') . '">' . MY_ACCOUNT_INFORMATION . '</a>'; ?></td>
												</tr>
												<tr>
													<td class="main"><?php echo tep_image(DIR_WS_TEMPLATES . 'images/misc/arrow_green.gif') . ' <a class="boxLink" href="' . tep_href_link(FILENAME_ADDRESS_BOOK, '', 'SSL') . '">' . MY_ACCOUNT_ADDRESS_BOOK . '</a>'; ?></td>
												</tr>
												<tr>
													<td class="main"><?php echo tep_image(DIR_WS_TEMPLATES . 'images/misc/arrow_green.gif') . ' <a class="boxLink" href="' . tep_href_link(FILENAME_ACCOUNT_PASSWORD, '', 'SSL') . '">' . MY_ACCOUNT_PASSWORD . '</a>'; ?></td>
												</tr>
											</table>
										</td>
									</tr>
								</table>
							</fieldset>
						</td>
					</tr>

					<tr>
						<td>
							<fieldset>
							<legend><?php echo MY_ORDERS_TITLE; ?></legend>
								<table border="0" width="100%" cellspacing="0" cellpadding="2">
									<tr>
										<td width="60"><?php echo tep_image(DIR_WS_TEMPLATES . 'images/misc/account_orders.gif'); ?></td>
										<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
										<td>
											<table border="0" width="100%" cellspacing="0" cellpadding="2">
												<tr>
													<td class="main"><?php echo tep_image(DIR_WS_TEMPLATES . 'images/misc/arrow_green.gif') . ' <a class="boxLink" href="' . tep_href_link(FILENAME_ACCOUNT_HISTORY, '', 'SSL') . '">' . MY_ORDERS_VIEW . '</a>'; ?></td>
												</tr>
											</table>
										</td>
									</tr>
								</table>
							</fieldset>
						</td>
					</tr>

					<tr>
						<td>
							<fieldset>
							<legend><?php echo EMAIL_NOTIFICATIONS_TITLE; ?></legend>
								<table border="0" width="100%" cellspacing="0" cellpadding="2">
									<tr>
										<td width="60"><?php echo tep_image(DIR_WS_TEMPLATES . 'images/misc/account_notifications.gif'); ?></td>
										<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
										<td>
											<table border="0" width="100%" cellspacing="0" cellpadding="2">
												<tr>
													<td class="main"><?php echo tep_image(DIR_WS_TEMPLATES . 'images/misc/arrow_green.gif') . ' <a class="boxLink" href="' . tep_href_link(FILENAME_ACCOUNT_NEWSLETTERS, '', 'SSL') . '">' . EMAIL_NOTIFICATIONS_NEWSLETTERS . '</a>'; ?></td>
												</tr>
												<tr>
													<td class="main"><?php echo tep_image(DIR_WS_TEMPLATES . 'images/misc/arrow_green.gif') . ' <a class="boxLink" href="' . tep_href_link(FILENAME_ACCOUNT_NOTIFICATIONS, '', 'SSL') . '">' . EMAIL_NOTIFICATIONS_PRODUCTS . '</a>'; ?></td>
												</tr>
											</table>
										</td>
									</tr>
								</table>
							</fieldset>
						</td>
					</tr>

						<!-- // Points/Rewards Module V2.1rc2a points_system_box_bof //-->
						<?php
							if (USE_POINTS_SYSTEM == 'true') {
						?>
						  <tr>
							<td>
							<fieldset>
							<legend><?php echo MY_POINTS_TITLE; ?></legend>
							  <table border="0" width="100%" cellspacing="0" cellpadding="2">
								<tr>
									<td width="60"><?php echo tep_image(DIR_WS_TEMPLATES . 'images/misc/payment.gif'); ?></td>
									<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
									<td><table border="0" width="100%" cellspacing="0" cellpadding="2">
										<?php
										  $has_points = tep_get_shopping_points($customer_id);
										  if ($has_points > 0) {
										?>
										<tr>
										  <td class="main" style="padding-bottom:5px"><?php echo sprintf(MY_POINTS_CURRENT_BALANCE, number_format($has_points,POINTS_DECIMAL_PLACES),$currencies->format(tep_calc_shopping_pvalue($has_points))); ?></td>
										</tr>
										<?php
										  }
										?>
									  <tr>
										<td class="main"><?php echo tep_image(DIR_WS_TEMPLATES . 'images/misc/arrow_green.gif') . ' <a class="boxLink" href="' . tep_href_link(FILENAME_MY_POINTS, '', 'SSL') . '">' . MY_POINTS_VIEW . '</a>'; ?></td>
									  </tr>
									  <tr>
										<td class="main"><?php echo tep_image(DIR_WS_TEMPLATES . 'images/misc/arrow_green.gif') . ' <a class="boxLink" href="' . tep_href_link(FILENAME_MY_POINTS_HELP, '', 'SSL') . '">' . MY_POINTS_VIEW_HELP . '</a>'; ?></td>
									  </tr>
									</table></td>
									<td width="10" align="right"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
								</tr>
							</table>
							</fieldset>
							</td>
						  </tr>
					<?php
					  }
					?>
					<!-- // Points/Rewards Module V2.1rc2a points_system_box_eof //-->

					<!-- //znizki //-->
				    <tr>
					<td>
					<fieldset>
					<legend><?php echo ZNIZKI_TITLE; ?></legend>
					  <table border="0" width="100%" cellspacing="0" cellpadding="2">
						<tr>
							<td width="60"><?php echo tep_image(DIR_WS_TEMPLATES . 'images/misc/payment.gif'); ?></td>
							<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
							<td><table border="0" width="100%" cellspacing="0" cellpadding="2">
							  <tr>
								<td class="main">
								<?php
								// do jakiej grupy nalezy klient
								$query = tep_db_query("select g.customers_groups_discount,g.customers_groups_name,g.customers_groups_id from " . TABLE_CUSTOMERS_GROUPS . " g inner join  " . TABLE_CUSTOMERS  . " c on g.customers_groups_id = c.customers_groups_id and c.customers_id = '" . $customer_id . "'");
								$query_result = tep_db_fetch_array($query);
								$id_grupy = $query_result['customers_groups_id'];
								$znizka_grupy = $query_result['customers_groups_discount'];
								$nazwa = $query_result['customers_groups_name'];
								// jaki ma indywidualna znizke
								$query = tep_db_query("select customers_discount from " . TABLE_CUSTOMERS . " where customers_id =  '" . $customer_id . "'");
								$query_result = tep_db_fetch_array($query);
								$znizka_klienta = $query_result['customers_discount'];
								//
								$znizka = $znizka_grupy + $znizka_klienta;
								if ($znizka == 0 || $znizka > 0) {
									echo ZNIZKI_INDYWIDUALNE_BRAK;
									} else {
									echo ZNIZKI_INDYWIDUALNE.' <b>'.$znizka."%</b><br>";
								}
								// znizki dla producentow
								$znizka_producentow = '';
								$query_producent = tep_db_query("select distinct manufacturers_id,manufacturers_name from " . TABLE_MANUFACTURERS);
								while ($producent = tep_db_fetch_array($query_producent)) {
									$query_A = tep_db_query("select m.manudiscount_discount from " . TABLE_MANUDISCOUNT .  " m where m.manudiscount_groups_id = 0 and m.manudiscount_customers_id = '" . $customer_id . "' and manudiscount_manufacturers_id = '".$producent['manufacturers_id']."'");
									$query_B = tep_db_query("select m.manudiscount_discount from " . TABLE_MANUDISCOUNT .  " m where m.manudiscount_groups_id = '".$id_grupy."' and m.manudiscount_customers_id = '0' and manudiscount_manufacturers_id = '".$producent['manufacturers_id']."'");
									$query_C = tep_db_query("select m.manudiscount_discount from " . TABLE_MANUDISCOUNT .  " m where m.manudiscount_groups_id = 0 and m.manudiscount_customers_id = '0' and manudiscount_manufacturers_id = '".$producent['manufacturers_id']."'");
									$customer_discount = '';
									if ($query_result = tep_db_fetch_array($query_A)) {
										$customer_discount = $query_result['manudiscount_discount'];
									} else if ($query_result = tep_db_fetch_array($query_B)) {
										$customer_discount = $query_result['manudiscount_discount'];
									} else if ($query_result = tep_db_fetch_array($query_C)) {
										$customer_discount = $query_result['manudiscount_discount'];
									}
									if ($customer_discount != '') {
										$znizka_producentow .= tep_image(DIR_WS_TEMPLATES . 'images/misc/arrow_green.gif') . ZNIZKA_PRODUCENTA.'<a href="' . tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $producent['manufacturers_id']) . '">' . $producent['manufacturers_name'] . '</a>: <b>'.$customer_discount."%</b><br>";
									}
								}
								if ($znizka_producentow != '') {
									$znizka_producentow = '<br>'.ZNIZKA_PRODUCENTA_TITLE.'<br>' . $znizka_producentow;
									echo $znizka_producentow;
								}
								?>
								</td>
							  </tr>
							</table></td>
							<td width="10" align="right"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
						</tr>
					</table>
					</fieldset>
					</td>
				    </tr>
				    <!-- // znizki //-->

					<?php
					// KLUB PRZYJACIOL
					if (defined(MODULE_KLUB_STATUS)) {
						if ( MODULE_KLUB_STATUS == 'true') {

							$wartosc_zamowien = suma_zamowien();
							if ($wartosc_zamowien >= (int)MODULE_KLUB_KWOTA) {
							?>
							<tr>
								<td>
									<fieldset>
									<legend><?php echo KLUB_TITLE; ?></legend>
										<table border="0" width="100%" cellspacing="0" cellpadding="2">
											<tr>
												<td width="60"><?php echo tep_image(DIR_WS_TEMPLATES . 'images/misc/payment.gif'); ?></td>
												<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
												<td>
													<table>
														<?php
														echo '<tr><td class="smallText">'.KLUB_WITAJ.'<br>'.KLUB_TEXT_1.MODULE_KLUB_CUMORDER_PERIOD. KLUB_TEXT_2.$currencies->format(MODULE_KLUB_KWOTA). KLUB_TEXT_3.'<br>&nbsp;</td></tr>';
														if (MODULE_KLUB_INC_SHIPPING == 'true') {
														  echo '<tr><td class="main" align="left"><b>'.tep_image(DIR_WS_TEMPLATES . 'images/misc/arrow_green.gif') .KLUB_PRZESYLKA.'</b></td></tr>';
														}
														$table_cost = split("[:,]" , MODULE_KLUB_TABLE);
														for ($i = 0; $i < count($table_cost); $i+=2) {
															$kwota = $currencies->format($table_cost[$i]);
															$znizka = $currencies->format($table_cost[$i+1]);
															echo '<tr><td class="main" align="left">'.tep_image(DIR_WS_TEMPLATES . 'images/misc/arrow_green.gif') .KLUB_TEKST_RABAT.' <b>'.$znizka.'</b> '.KLUB_TEKST_RABAT_1.' <b>'.$kwota.'</b></td></tr>';
														}
														?>
													</table>
												</td>
											</tr>
										</table>
									</fieldset>
								</td>
							</tr>
							<?php
							}
						}
					}
					//  KLUB PRZYJACIOL
					?>


					<?php
					if (MODULE_ORDER_TOTAL_INSTALLED) {
						?>
						<tr>
							<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '1'); ?></td>
						</tr>
						<?php
//																	$order_total_modules->process();
//																	echo $order_total_modules->output2();
						}
					?>

				<!-- TRESC STRONY KONIEC -->

				</table>
			</td>
		</tr>
	</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
