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

// ******************************************************************************************
// SKLADANIE ZAMOWIENIA
//******************************************************************************************
  //
  $temat = MakeUTF(EMAIL_TEXT_SUBJECT) . ' - ' . $insert_id;
  $owner = MakeUTF(STORE_NAME);
  $adresat = MakeUTF($order->customer['firstname'] . ' ' . $order->customer['lastname']);

  // GENEROWANIE TRESCI MAILA
  if (EMAIL_USE_HTML == 'true') {
        $ArrayLNTargets = array("\r\n", "\n\r", "\n", "\r", "\t");
        //
        $email_order = '
        <table border="0" cellspacing="0" cellpadding="5" width="100%">
        <tr><td align="left" style="font-family:Tahoma,Arial;font-size:16px;color:#4f7d13"><b>'.UHE_TEXT_DEAR.' '.$order->customer['firstname'] . ' ' . $order->customer['lastname'].'</b></td></tr>
        <tr><td align="left" style="font-family:Tahoma,Arial;font-size:11px;color:#474747">'.EMAIL_WARNING.'</td></tr>
        </table>
        <table border="0" cellspacing="0" cellpadding="5" width="100%">
            <tr><td align="left" style="font-family:Tahoma,Arial;font-size:12px;color:#474747">'.
            UHE_TEXT_ORDER_NUMBER.' <b>'.$insert_id.'</b><br>'.
            UHE_TEXT_INVOICE_URL.' <b>'.tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, 'order_id=' . $insert_id, 'SSL', false).'</b><br>'.
            UHE_TEXT_DATE_ORDERED.' <b>'.strftime(DATE_FORMAT_LONG).'</b></td></tr>';
            // komentarz do zamowienia
            if ($order->info['comments']) {
                $email_order .= '<tr><td align="left" style="font-family:Tahoma,Arial;font-size:12px;color:#474747">';
                $email_order .= '<br><b>'.UHE_TEXT_COMMENTS.'</b><br>'. str_replace($ArrayLNTargets, '<br>', tep_db_output($order->info['comments']));
                $email_order .= '</td></tr>';
            }
            $email_order .= '
            <tr><td align="left" style="font-family:Tahoma,Arial;font-size:12px;color:#474747"><br>'.UHE_TEXT_ORDER_CONTENTS.'</td></tr>
            <tr><td align="left">
                <table cellspacing="3" width="100%" cellpadding="2" style="border:1px;border-style:solid;border-color:#868785">
    				<tr>
    					<td align="left" style="background-color:#bbd1bc;font-family:Tahoma,Arial;font-size:12px;color:#474747">'.UHE_TEXT_PRODUCTS_ARTICLE.'</td>
    					<td align="center" style="background-color:#bbd1bc;font-family:Tahoma,Arial;font-size:12px;color:#474747">'.UHE_TEXT_PRODUCTS_MODEL.'</td>
    					<td align="center" style="background-color:#bbd1bc;font-family:Tahoma,Arial;font-size:12px;color:#474747">'.UHE_TEXT_PRODUCTS_PRICE.'</td>
    					<td align="center" style="background-color:#bbd1bc;font-family:Tahoma,Arial;font-size:12px;color:#474747">'.UHE_TEXT_PRODUCTS_QTY.'</td>
    					<td align="center" style="background-color:#bbd1bc;font-family:Tahoma,Arial;font-size:12px;color:#474747">'.UHE_TEXT_PRODUCTS_TOTAL.'</td>
    				</tr>'.$products_ordered_html.'
                </table>
            </td></tr>
        </table>';

        $email_order .= '
        <table border="0" cellspacing="0" cellpadding="5" width="100%">
            <tr><td align="left" style="font-family:Tahoma,Arial;font-size:12px;color:#474747">
                <table cellspacing="3" cellpadding="3" style="border:1px;border-style:solid;border-color:#868785">';
                	for ($i=0, $n=sizeof($order_totals); $i<$n; $i++) {
                		$temp_title = trim(strip_tags($order_totals[$i]['title']));
                		if(substr($temp_title, -1) == ':'): $temp_title = substr($temp_title, 0, -1); endif;
                		$email_order .= '<tr><td align="left" style="background-color:#f0f0f0;font-family:Tahoma,Arial;font-size:12px;color:#474747">'.$temp_title .'</td><td align="right" style="background-color:#f0f0f0;font-family:Tahoma,Arial;font-size:12px;color:#474747" nowrap><b>' . strip_tags($order_totals[$i]['text']) . "</b></td></tr>";
                	}
                $email_order .= '</table>';
        if ($dokument != '') {
          $email_order .= '<br><b>'.UHE_TEXT_DOKUMENT . ' </b>'. $dokument . '<br>';
        }
        $email_order .= '</td></tr></table>';

        if (is_object($$payment)) {
            $email_order .= '
            <table border="0" cellspacing="0" cellpadding="5" width="100%">
                <tr><td align="left" style="font-family:Tahoma,Arial;font-size:12px;color:#474747">';
                $email_order .= '<b>'.UHE_TEXT_PAYMENT_METHOD . ': </b>'. $order->info['payment_method'] . '<br>';
                $payment_class = $$payment;
                if ($TrDate != "") {
                 $email_order .= $mtransfer_mail . "<br>";
                }
                $payment_class = $$payment;
                if ($payment_class->email_footer) {
                    $email_order .= str_replace($ArrayLNTargets, '<br>', $payment_class->email_footer).'<br><br>';
                } else {
                    $email_order .= '<br>';
                }
            $email_order .= '</td></tr></table>';
        }

        $email_order .='<table border="0" cellspacing="0" cellpadding="5">
            <tr><td align="left">
                <table cellspacing="3" width="100%" cellpadding="5" style="border:1px;border-style:solid;border-color:#868785">
                    <tr>';
                        if ($order->content_type != 'virtual') {
                            $email_order .= '<td align="left" style="background-color:#bbd1bc;font-family:Tahoma,Arial;font-size:13px;color:#474747">'. UHE_TEXT_DELIVERY_ADDRESS .'</td>';
                        }
                        $email_order .= '<td align="left" style="background-color:#bbd1bc;font-family:Tahoma,Arial;font-size:13px;color:#474747">'. UHE_TEXT_BILLING_ADDRESS .'</td>
                    </tr>
                    <tr>';
                        if ($order->content_type != 'virtual') {
                            $email_order .= '<td align="left" style="background-color:#f0f0f0;font-family:Tahoma,Arial;font-size:13px;color:#474747">'. tep_address_label($customer_id, $sendto, 0, '', '<br>') .'</td>';
                        }
                        $email_order .= '<td align="left" style="background-color:#f0f0f0;font-family:Tahoma,Arial;font-size:13px;color:#474747">'. tep_address_label($customer_id, $billto, 0, '', '<br>') .'</td>
                    </tr>
                </table>
            </td></tr>
        </table>';
        $email_order = str_replace($ArrayLNTargets, '', $email_order);
        $email_order = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $email_order);
     } else {
        $email_order = STORE_NAME . "\n";
        $email_order .= EMAIL_SEPARATOR . "\n";

        if ($order->customer['is_dummy_account']) {
        $email_order .= EMAIL_WARNING . "\n\n";
        }
        $email_order .= EMAIL_SEPARATOR . "\n" .
                        EMAIL_TEXT_ORDER_NUMBER . ' ' . $insert_id . "\n" .
                        (($customer_id == 0)? '' : EMAIL_TEXT_INVOICE_URL . ' ' . tep_href_link(FILENAME_ACCOUNT_HISTORY_INFO, 'order_id=' . $insert_id, 'SSL', false) . "\n") .
                        EMAIL_TEXT_DATE_ORDERED . ' ' . strftime(DATE_FORMAT_LONG) . "\n\n";
        $email_order .= EMAIL_TEXT_PRODUCTS . "\n" .
                        EMAIL_SEPARATOR . "\n" .
                        $products_ordered .
                        EMAIL_SEPARATOR . "\n";

        for ($i=0, $n=sizeof($order_totals); $i<$n; $i++) {
        $email_order .= strip_tags($order_totals[$i]['title']) . ' ' . strip_tags($order_totals[$i]['text']) . "\n";
        }

        if ($order->content_type != 'virtual') {
        $email_order .= "\n" . EMAIL_TEXT_DELIVERY_ADDRESS . "\n" .
                        EMAIL_SEPARATOR . "\n" .
                        tep_address_label($customer_id, $sendto, 0, '', "\n") . "\n";
        }

        $email_order .= "\n" . EMAIL_TEXT_BILLING_ADDRESS . "\n" .
                      EMAIL_SEPARATOR . "\n" .
                      tep_address_label($customer_id, $billto, 0, '', "\n") . "\n\n";
        if (is_object($$payment)) {

          // START extra fields in email
          $it=0;
          $semextrfields = tep_db_query("select * from " . TABLE_EXTRA_FIELDS . " where fields_required_email = '1'");
          while($dataexfes = tep_db_fetch_array($semextrfields)) {
            $cusextrfields = tep_db_query("select * from " . TABLE_CUSTOMERS_TO_EXTRA_FIELDS . " where customers_id = '" . (int)$customer_id . "' and fields_id = '" . $dataexfes['fields_id'] . "'");
            $rescusextrfields = tep_db_fetch_array($cusextrfields);
            if ($it < 1) {
              $email_order .= "\n" . CATEGORY_EXTRA_FIELDS . "\n" . EMAIL_SEPARATOR . "\n" ;
              $it++;
            }

            $extrfieldsinf = tep_db_query("select fields_name from " . TABLE_EXTRA_FIELDS_INFO . " where fields_id = '" . $dataexfes[fields_id] . "' and languages_id = '" . $languages_id . "'");

            $extrfieldsres = tep_db_fetch_array($extrfieldsinf);
            $email_order .= $extrfieldsres['fields_name'] . ' : ' .
            $rescusextrfields['value'] . "\n";
          }
          $email_order .= "\n";
          // END extra fields in email

          $email_order .= EMAIL_TEXT_PAYMENT_METHOD . "\n" .
                        EMAIL_SEPARATOR . "\n";
          //tu sprawdzamy, czy klient wybrac mtransfer, i dodajemy link do platnosci w mailu z info o zakupie.
          if ($TrDate != "") {
            $email_order .= $mtransfer_mail . "\n" . EMAIL_SEPARATOR . "\n";
          }
          $payment_class = $$payment;
          $email_order .= $order->info['payment_method'] . "\n\n";
          if ($payment_class->email_footer) {
            $email_order .= $payment_class->email_footer . "\n\n";
          }
        }
  }
  if ($_POST['proforma'] == true || $f_proforma == true) {
    tep_mail($adresat, $order->customer['email_address'], $temat, html_entity_decode($email_order), $owner, STORE_OWNER_EMAIL_ADDRESS, $file_name, $file_mail_name, 'application/pdf');
  } else {
    tep_mail($adresat, $order->customer['email_address'], $temat, html_entity_decode($email_order), $owner, STORE_OWNER_EMAIL_ADDRESS);
  }

  if (EMAIL_USE_HTML == 'false') {
	  //extra info o kliencie
      $email_order .= "\n\n" . COMMENTS_EMAIL_INFO . "\n" . EMAIL_SEPARATOR . "\n" . NAME_EMAIL_INFO . $order->customer['firstname'] . " " . $order->customer['lastname'];
      if ($order->customer['company'] != '') {$email_order .= "\n" . FIRM_EMAIL_INFO . $order->customer['company'];};
      if ($order->billing['nip'] != '') {$email_order .= "\n" . NIP_ORDER_EMAIL_INFO . $order->billing['nip'];};
      if ($order->customer['nip'] != '') {$email_order .= "\n" . NIP_MAIN_EMAIL_INFO . $order->customer['nip'] . "\n";};

      $email_order .= "\n" . EMAIL_EMAIL_INFO . $order->customer['email_address'] . "\n" . PHONE_EMAIL_INFO . $order->customer['telephone'];
      if ($order->customer['fax'] != '') {$email_order .= "\n" . FAX_GSM_EMAIL_INFO . $order->customer['fax'] . "\n\n";};
      if ($order->info['comments'] != '') {$email_order .= "\n" . EMAIL_SEPARATOR . "\n" .KOMENTARZ_EMAIL_INFO ."\n". EMAIL_SEPARATOR . "\n". $order->info['comments'] . "\n\n";};

	 } else {

       $email_order .= '<table border="0" cellspacing="0" cellpadding="5">
            <tr><td align="left">
               <table cellspacing="3" cellpadding="2" style="border:1px;border-style:solid;border-color:#868785">
        		<tr>
        			<td align="left" width="450" style="background-color:#bbd1bc;font-family:Tahoma,Arial;font-size:12px;color:#474747">'. COMMENTS_EMAIL_INFO .'</td>
        		</tr>
        		<tr>
        			<td align="left" style="background-color:#f0f0f0;font-family:Tahoma,Arial;font-size:13px;color:#474747">';
        				$email_order .=  NAME_EMAIL_INFO . " <b>" . $order->customer['firstname'] . ' ' . $order->customer['lastname'] . "</b>";
        								if ($order->customer['company'] != '') {$email_order .= "<br>" . FIRM_EMAIL_INFO. " <b>" .$order->customer['company']. " </b>";};
        								if ($order->billing['nip'] != '') {$email_order .= "<br>" . NIP_ORDER_EMAIL_INFO . " <b>" . $order->billing['nip']. " </b>";};
        								if ($order->customer['nip'] != '') {$email_order .= "<br>" . NIP_MAIN_EMAIL_INFO . " <b>" . $order->customer['nip']. " </b>" . "<br>";};
        				$email_order .=  "<br>" . EMAIL_EMAIL_INFO . " <b>" . $order->customer['email_address']. " </b>" . "<br>" . PHONE_EMAIL_INFO . " <b>" . $order->customer['telephone']. " </b>";
        								if ($order->customer['fax'] != '') {$email_order .= "<br>" . FAX_GSM_EMAIL_INFO . " <b>" . $order->customer['fax'] . " </b>" . "<br><br>";};
        			$email_order .= '</td>
        		</tr>
        	  </table>
            </td></tr>
        </table><br>';

       $email_order .= '<table border="0" cellspacing="0" cellpadding="5">
            <tr><td align="left">
               <table cellspacing="3" cellpadding="2" style="border:1px;border-style:solid;border-color:#868785">
                <tr>
                  <td align="left" width="450" style="background-color:#bbd1bc;font-family:Tahoma,Arial;font-size:12px;color:#474747">'. CATEGORY_EXTRA_FIELDS .'</td>
                </tr>';
                // START extra fields in email
                $it=0;
                $semextrfields = tep_db_query("select * from " . TABLE_EXTRA_FIELDS . " where fields_required_email = '1'");
                while($dataexfes = tep_db_fetch_array($semextrfields)) {
                  $cusextrfields = tep_db_query("select * from " . TABLE_CUSTOMERS_TO_EXTRA_FIELDS . " where customers_id = '" . (int)$customer_id . "' and fields_id = '" . $dataexfes['fields_id'] . "'");
                  $rescusextrfields = tep_db_fetch_array($cusextrfields);
                  if ($it < 1) {
                    $it++;
                  }

                  $extrfieldsinf = tep_db_query("select fields_name from " . TABLE_EXTRA_FIELDS_INFO . " where fields_id = '" . $dataexfes[fields_id] . "' and languages_id = '" . $languages_id . "'");

                  $extrfieldsres = tep_db_fetch_array($extrfieldsinf);
                  $email_order .= '<tr><td align="left" style="background-color:#f0f0f0;font-family:Tahoma,Arial;font-size:13px;color:#474747">';
                    $email_order .= $extrfieldsres['fields_name'] . ' : ' . $rescusextrfields['value'];
                    $email_order .= '</td></tr>';
                }
                $email_order .= '</table>
              </td></tr>
            </table><br>';
          // END extra fields in email

      $email_order = str_replace($ArrayLNTargets, '', $email_order);
  }

  tep_mail($temat, STORE_OWNER_EMAIL_ADDRESS, $temat, html_entity_decode($email_order), $owner, STORE_OWNER_EMAIL_ADDRESS);
  // wysylanie od innych administratorw kopii
  if (SEND_EXTRA_ORDER_EMAILS_TO != '') {
    tep_mail('', SEND_EXTRA_ORDER_EMAILS_TO, $temat, html_entity_decode($email_order), $owner, STORE_OWNER_EMAIL_ADDRESS);
  }
?>