﻿<?php
// Minoan (SAP)
session_name('iAdmin'); session_start(); 
?><html>

<head>
<BASE HREF="http://<?php echo $_SERVER['HTTP_HOST']; ?>/">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1253">
<LINK REL="stylesheet" type="text/css" href="ishop/adm.css">
<SCRIPT SRC="ishop/xadm.js.php"></SCRIPT>
<title>Modern iShopping cart Setup</title>
<style>
.openTable {
   border-top: 1px solid #404040;
   border-left: 1px solid #404040;
   border-right: 1px solid #404040;
   table-layout:fixed;
}
</style>
</head>

<body>
<?php
include("../lib/ado/adodb.inc.php");
include("../lib/generic.inc.php");
require_once("ishop_all.inc.php");
include("ishop/ishop.inc.php");

$cfx=new cFILOXENIA();

$db_local = &db_open();

$x2=new cX2($db_local);

// confirm_login() needs LPATH, PASSWD or RESELID, REPASS; Reseller needs allowence for desktop login
if ( !$x2->quick_login() && !$x2->validate_session() ) // err
	die("Authentication error");

$x2->read_agent_main();

$fu=0;
if (isset($_REQUEST['FUNC2']))
   $fu=(int)$_REQUEST['FUNC2'];

print_header($fu);

switch ($fu)
{
case 0:
   main_menu();
   break;
case 1:
	load_waiters();
	main_menu();
	break;
case 2:
   load_item_orderplus();
   main_menu();
   break;
case 3:
   load_extras();
   main_menu();
   break;
case 4:
	load_customer();
   main_menu();
   break;
case 8:
   load_paymethod();
   main_menu();
   break;
case 11:
   //export_orders($db_remote,$db_local,true);
   main_menu();
   break;
case 12:
   //export_orders($db_remote,$db_local,false);
   main_menu();
   break;
case 13:
   parameter_ask();
   break;
case 14:
   parameter_save();
   main_menu();
   break;
case 15:
	load_rooms();
	main_menu();
	break;
case 16:
	load_accounts();
	main_menu();
	break;
case 17:
   load_menurep(1,'DEPART.TXT'); // warehouseid
   main_menu();
   break;
case 18:
   load_supplier();
   main_menu();
   break;
case 19:
   load_menurep(10,'TYPES.TXT'); // external docs
   main_menu();
   break;
case 20:
   ask_load_item_cart();
   main_menu();
   break;
case 21:
   load_ypoloipo();
   main_menu();
   break;
case 120:
   do_load_item_cart();
   main_menu();
   break;
}

$db_local->Close();

//======================================
// Functions
//======================================
function print_header()
{
   global $x2;
?>
<table width=100%>
<tr>
   <td class=bgHeader>Modern iShopping cart Setup (for Minoan) agentid=<?php
      echo $x2->agentid.'# '.myhtml($x2->lpath);
   ?></td>
</tr>
</table>
<?php
}
//======================================
function main_menu()
{
?>
<h5>��������</h5>
<ul>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=13"></a>����������
   <hr>
<!--
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=1">�������� ����������</a>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=4">�������� ���������</a>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=2">�������� �����/ ����������/ ���. - ORDER Plus</a>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=3">�������� extras</a>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=8">������� ��������</a>
	<br>&nbsp;
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=15">�������� ��������</a>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=16">�������� ����������� ���������</a>
	<br>&nbsp;
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=11">Export ����������� ORDER PLUS</a>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=12">������ Export ORDER PLUS</a>
   <hr>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=17">�������� ������������� �����</a>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=18">�������� �����������</a>
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=19">�������� ����� ������������</a>
-->
<li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=20">�������� �����</a>
</ul>
<button type=button onClick="self.close()">Close Window</button>
<p style="background-color:#D0D0D0;">
Auto-scheduler:
<BR>
<kbd><?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=...&amp;LPATH=...&amp;PASSWD=...</kbd>
<BR>�
<BR><kbd><?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=...&amp;RESELID=...&amp;REPASS=...</kbd>
</kbd>
</p>
<?php
}
//--------------------------------------
function parameter_ask()
{
   global $x2;
	$db=&$x2->db;
?>
<h5>����������</h5>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST>
<input type=HIDDEN name="FUNC2" value="14">

<table width=500 class=openTable style="border-bottom: 1px solid #404040;">
<tr>
   <td width=200><b>���������</b></td>
   <td width=300>&nbsp;</td>
</tr>
<tr>
   <td>Ascii imports</td>
   <td><kbd><?php  echo myhtml($GLOBALS['cfx']->ascii); ?></kbd></td>
</tr>
<tr>
   <td>ORDER Plus request</td>
   <td><kbd><?php  echo myhtml($GLOBALS['cfx']->request); ?></kbd></td>
</tr>
<tr>
   <td>FiloXenia answers</td>
   <td><kbd><?php  echo myhtml($GLOBALS['cfx']->answer); ?></kbd></td>
</tr>
</table>

<table width=500 style="table-layout:fixed;">
<tr>
   <td width=200>&nbsp;</td>
   <td width=300>
      <!--
      <input type=SUBMIT name="ok" value="<?php $x2->xprint('upd'); ?>">
      -->
      <input type=button onclick="location.href='<?php echo $_SERVER['PHP_SELF']; ?>?FUNC2=0';"
      value="<?php $x2->xprint('return'); ?>"></td>
</tr>
</table>

</form>
<?php
}
//--------------------------------------
function trim_leading_zero($src)
{
	$i=0;
	$l=strlen($src);
	while ($i<$l && $src{$i}=='0')
		$i++;

	if ($i>0)
		return substr($src,$i);
	else
		return $src;
}
//--------------------------------------
function trim_2spaces($src)
{
	while (strpos($src,'  ')!==false)
		$src=str_replace('  ',' ',$src);

	return $src;
}
//--------------------------------------
function parameter_save()
{
   global $x2;
   $db=&$x2->db;
}
//--------------------------------------
function load_rooms()
{
	global $x2;
	$db=&$x2->db;

	$prof=new cPROFILER1();

	$fname=$GLOBALS['cfx']->answer.'\\ROOMS.TXT';
	if (($fp=fopen($fname,"r"))===false) {
		echo "<h6>Could not open file ".myhtml($fname)."</h6>";
		return;
	}

	$nrec=0;
	$dataEx=array();
	while (!feof($fp)) {
		$buf=fgets($fp,1024);
		$var=explode('#',$buf);
		if (empty($var) || ($n=count($var))<7)
			continue;

		$var[0]=(int)trim_leading_zero($var[0]);
		for ($i=1;$i<6;$i++) {
			$var[$i]=trim_2spaces($var[$i]);
		}
		$var[6]=trim($var[6]);

		$var[]=$x2->agentid;

		$dataEx=array_merge($dataEx,$var);
		$nrec++;
	}
	fclose($fp);

   $db->StartTrans();
	do {
		$query="DELETE FROM ishop_filo_room WHERE AGENTID=?";
		$data=array($x2->agentid);
		if (!$db->Execute($query,$data)) {
			echo "<h6>".$db->ErrorMsg()."</h6>";
			$nrec=0;
			break;
		}

		$query="INSERT INTO ishop_filo_room (ROOMNR, FULLNAME, DEAL, DEALBR, DEALME, DEALDI, ACCOUNT, AGENTID) VALUES ";
		$n=count($dataEx)/8;
		for ($i=0;$i<$n;$i++) {
			if ($i>0) $query.=',';
			$query.='(?,?,?,?,  ?,?,?,?)';
		}
		if (!$db->Execute($query,$dataEx)) {
			echo "<h6>".$db->ErrorMsg()."</h6>";
			$nrec=0;
			break;
		}

	} while (false);
	$db->CompleteTrans(true);

	echo "<h5>".$nrec." records replaced in ".$prof->elapsed()." seconds</h5>";
}
//--------------------------------------
function load_accounts()
{
	global $x2;
	$db=&$x2->db;

	$prof=new cPROFILER1();

	$fname=$GLOBALS['cfx']->answer.'\\TABLES.TXT';
	if (($fp=fopen($fname,"rb"))===false) {
		echo "<h6>Could not open file ".myhtml($fname)."</h6>";
		return;
	}

	$t1='ishop_filo_bill';
	$prim=array('AGENTID', 'MBR_LOGIN');

	$nrec=0;
	$now=date('Y-m-d H:i:s');
	while (!feof($fp)) {
		$buf=fgets($fp,1024);
		pre($buf);
		$var=explode('#',$buf);
		if (empty($var) || ($n=count($var))<2)
			continue;

		$var[0]=trim($var[0]);
		$var[1]=toFloat(trim_leading_zero($var[1]));
		pre($var);

		$data=array('AGENTID'=>$x2->agentid, 'MBR_LOGIN'=>$var[0], 'ANSWERDAYTI'=>$now, 'AMOUNT'=>$var[1]);
		if (ReplaceEx($db,$t1,$data,$prim)==0) {
			echo "<h6>".$db->ErrorMsg()."</h6>";
		}
		else {
			$nrec++;
		}

		if (count($var)>=3) {
         $var[2]=(int)trim($var[2]);
         if ((int)$var[2]==1) {
            // Not very good, but...
            $query="UPDATE order_main SET STATE=110 WHERE AGENTID=? AND MBR_LOGIN=? AND STATE<110";
            $data=array($x2->agentid, $var[0]);
            if (!$db->Execute($query,$data))
               echo "<h6>".$db->ErrorMsg()."</h6>";
         }
      }
   }
	fclose($fp);

	echo "<h5>".$nrec." records replaced in ".$prof->elapsed()." seconds</h5>";
}
//--------------------------------------
function load_paymethod()
{
   global $x2;
	$db=&$x2->db;

	$prof=new cPROFILER1();

	$prim=array( 'AGENTID', 'MENUTYPE', 'MENUID' );

	$data=array( 'AGENTID'=>$x2->agentid, 'MENUTYPE'=>9, 'MENUID'=>-1, 'MENUDES'=>'���������' );
	if (!ReplaceEx($db, 'menurep', $data, $prim)) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      return;
   }
   
   $query="DELETE FROM menurep WHERE AGENTID=? AND MENUTYPE=9";
	$data=array($x2->agentid);
	if (!$db->Execute($query,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      return;
   }

	$fname=$GLOBALS['cfx']->ascii.'\\PAYMENTS.TXT';
	if (($fp=fopen($fname,"r"))===false) {
		echo "<h6>Could not open file ".myhtml($fname)."</h6>";
		return;
	}

	$nrec=0;
	while (!feof($fp)) {
		$buf=fgets($fp,1024);
		$var=explode('#',$buf);
		if (empty($var) || ($n=count($var))<2)
			continue;

		for ($i=0;$i<$n;$i++)
			$var[$i]=trim($var[$i]);

		$data=array( 'AGENTID'=>$x2->agentid, 'MENUTYPE'=>9, 'MENUID'=>(int)$var[0], 'MENUDES'=>$var[1] );
		if (!ReplaceEx($db, 'menurep', $data, $prim)) {
         echo "<h6>".$db->ErrorMsg()."</h6>";
         break;
      }
      else
         $nrec++;
	}
	fclose($fp);

	echo "<h5>".$nrec." records replaced in ".$prof->elapsed()." seconds</h5>";
}
//--------------------------------------
function load_waiters()
{
	global $x2;
	$db=&$x2->db;

	$prof=new cPROFILER1();

	$fname=$GLOBALS['cfx']->ascii.'\\WAITERS.TXT';
	if (($fp=fopen($fname,"r"))===false) {
		echo "<h6>Could not open file ".myhtml($fname)."</h6>";
		return;
	}
	
	/*

	We must not do this, because we have records created by INCA Hellas, which should not be deleted.

	$query="DELETE FROM reseller WHERE AGENTID=?";
	$data=array($x2->agentid);
	if (!$db->Execute($query,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      return;
   }
   */

	$query="SELECT AGENTID FROM reseller WHERE RESELID=?";
	$qin="INSERT INTO reseller (RESELID,AGENTID,PASSWD,RE_NAME) VALUES(?,?,?,?)";
	$qupd="UPDATE reseller SET PASSWD=?, RE_NAME=? WHERE RESELID=?";

	$nrec=0;
	while (!feof($fp)) {
		$buf=fgets($fp,1024);
		$var=explode('#',$buf);
		if (empty($var) || ($n=count($var))<3)
			continue;

		for ($i=0;$i<$n;$i++)
			$var[$i]=trim($var[$i]);

		$data=array( (int)$var[0] );
		if (($rs=$db->Execute($query,$data))===false) {
			echo "<h6>".$db->ErrorMsg()."</h6>";
			break;
		}
		$row=$rs->FetchRow();
		$rs->Close();

		if ($row===false) { // not found
			$data=array( (int)$var[0], $x2->agentid, $var[1], $var[2] );
			if (!$db->Execute($qin,$data))
				echo "<h6>".$db->ErrorMsg()."</h6>";
			else
				$nrec++;
		}
		else if ($row[0]==$x2->agentid) { // same agentid
			$data=array( $var[1], $var[2], (int)$var[0] );
			if (!$db->Execute($qupd,$data))
				echo "<h6>".$db->ErrorMsg()."</h6>";
			else
				$nrec++;
		}
		else { // oter agentid
			echo "<h6>Reseller id ".myhtml($var[0])." belongs to other agent!</h6>";
		}
	}
	fclose($fp);

	echo "<h5>".$nrec." records replaced in ".$prof->elapsed()." seconds</h5>";
}
//--------------------------------------
function load_customer()
{
   global $x2;
   $db=&$x2->db;

   $prof=new cPROFILER1();

   $fname=$GLOBALS['cfx']->ascii.'\\TABLES.TXT';
	if (($fp=fopen($fname,"r"))===false) {
		echo "<h6>Could not open file ".myhtml($fname)."</h6>";
		return;
	}

   // Delete old values
	$query="DELETE FROM ishop_mbr WHERE AGENTID=?";
	$data=array($x2->agentid);
	if (!$db->Execute($query,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      return;
   }

	$query="SELECT COUNT(*) FROM ishop_mbr WHERE AGENTID=? AND LOGIN=?";
	$qin="INSERT INTO ishop_mbr (AGENTID,LOGIN,COMPANY,EMAIL) VALUES(?,?,?,'TABLE')";
	$qupd="UPDATE ishop_mbr SET COMPANY=?, EMAIL='TABLE' WHERE AGENTID=? AND LOGIN=?";

	$nrec=0;
	while (!feof($fp)) {
		$buf=fgets($fp,1024);
		$var=explode('#',$buf);
		if (empty($var))
			continue;

		$login=trim($var[0]);
		if (empty($login))
			continue;

		$data=array( $x2->agentid, $login );
		if (($rs=$db->Execute($query,$data))===false) {
			echo "<h6>".$db->ErrorMsg()."</h6>";
			break;
		}
		$row=$rs->FetchRow();
		$rs->Close();

		if ($row===false) {
         echo "<h6>Error: no count for DB table!</h6>";
			continue;
		}
		else if ($row[0]==0) { // not found
			$data=array( $x2->agentid, $login, '������� '.$login );
			if (!$db->Execute($qin,$data))
				echo "<h6>".$db->ErrorMsg()."</h6>";
			else
				$nrec++;
		}
		else if ($row[0]==1) { // record exists
			$data=array( '������� '.$login, $x2->agentid, $login );
			if (!$db->Execute($qupd,$data))
				echo "<h6>".$db->ErrorMsg()."</h6>";
			else
				$nrec++;
		}
		else { // other situation
			echo "<h6>Unspecified situation for table ".myhtml($var[0])."</h6>";
			continue;
		}
	}
	fclose($fp);

	echo "<h5>".$nrec." records replaced in ".$prof->elapsed()." seconds</h5>";
}
//--------------------------------------
function load_item_orderplus()
{
   global $x2;
   $db=&$x2->db;

   $prof=new cPROFILER1();

	$fname=$GLOBALS['cfx']->ascii.'\\PRODUCTS.TXT';
	if (($fp=fopen($fname,"r"))===false) {
		echo "<h6>Could not open file ".myhtml($fname)."</h6>";
		return;
	}

	$t1='ishop_item';
	$prim1=array('AGENTID', 'CODE');

	$query="DELETE FROM ishop_item WHERE AGENTID=?";
	$data=array($x2->agentid);
	if (!$db->Execute($query,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      fclose($fp);
      return;
   }

	$t2='ishop_ctg';
	$prim2=array('AGENTID', 'PAGENO');

	$query="DELETE FROM ishop_ctg WHERE AGENTID=?";
	$data=array($x2->agentid);
	if (!$db->Execute($query,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      fclose($fp);
      return;
   }

	$query="SELECT COUNT(*) FROM ishop_item WHERE AGENTID=? AND CODE=?";
	$qin="INSERT INTO ishop_item (AGENTID,CODE,NAME,PAGENO) VALUES(?,?,?,?)";
	$qupd="UPDATE ishop_item SET NAME=?, PAGENO=? WHERE AGENTID=? AND CODE=?";

	$query2="SELECT COUNT(*) FROM ishop_ctg WHERE AGENTID=? AND PAGENO=?";
	$qin2="INSERT INTO ishop_ctg (ABBREV, TITLE, COLOR, BGCOLOR, AGENTID, PAGENO) VALUES(?,?,'black',?,?,?)";
	$qupd2="UPDATE ishop_ctg SET ABBREV=?, TITLE=?, COLOR='black', BGCOLOR=? WHERE AGENTID=? AND PAGENO=?";

	$nrec=$nrec2=0;
	while (!feof($fp)) {
		// Example $buf="100001         #ILIOS DRY 0,375L#1000#WHITE WINES     #217255247"
		// ���. ������, ���������, ���������, ���������, ���������� ������� (rgb)
		$buf=fgets($fp,1024);

		$var=explode('#',$buf);
		if (empty($var) || ($n=count($var))<5)
			continue;

		for ($i=0;$i<$n;$i++)
			$var[$i]=trim($var[$i]);

      //
      // ishop_item
      //
      $trkey=ToEnglish($var[0]." ".$var[1]);
      $data=array('AGENTID'=>$x2->agentid, 'CODE'=>$var[0], 'NAME'=>$var[1], 'PAGENO'=>$var[2], 'TRKEY'=>$trkey );
		if (ReplaceEx($db,$t1,$data,$prim1)==0) {
			echo "<h6>".$db->ErrorMsg()."</h6>";
			break;
		}
		else {
			$nrec++;
		}

		//
		// ishop_ctg
		//
		$bgcolor='';
		if (strlen($var[4])==9) {
         $rc=(int)substr($var[4],0,3);
         $gc=(int)substr($var[4],3,3);
         $bc=(int)substr($var[4],6,3);
         $bgcolor=sprintf("#%02x%02x%02x",$rc,$gc,$bc);
      }

      $data=array('ABBREV'=>$var[3], 'TITLE'=>$var[3], 'COLOR'=>'#000000', 'BGCOLOR'=>$bgcolor,
			'AGENTID'=>$x2->agentid, 'PAGENO'=>$var[2] );
      if (ReplaceEx($db,$t2,$data,$prim2)==0) {
			echo "<h6>".$db->ErrorMsg()."</h6>";
			break;
		}
		else {
			$nrec2++;
		}
	}
	fclose($fp);

	echo "<h5>$nrec + $nrec2 records replaced in ".$prof->elapsed()." seconds</h5>";
}
//--------------------------------------
function export_orders($db_remote,$db_local,$update)
{
   global $x2;

   $prof=new cPROFILER1();
   $nrec=0;
}
//--------------------------------------
function load_menurep($typ,$src)
{
   global $x2;
   $db=&$x2->db;

   $prof=new cPROFILER1();

   $fname=$GLOBALS['cfx']->ascii.'\\'.$src;
	if (($fp=fopen($fname,"r"))===false) {
		echo "<h6>Could not open file ".myhtml($fname)."</h6>";
		return;
	}

   // Delete old values
	$query="DELETE FROM menurep WHERE AGENTID=? AND MENUTYPE=?";
	$data=array($x2->agentid, $typ);
	if (!$db->Execute($query,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      fclose($fp);
      return;
   }

	$query="INSERT INTO menurep (AGENTID,MENUTYPE,MENUID,MENUDES) VALUES(?,?,?,?)";

	$nrec=0;
	$nerr=0;
	while (!feof($fp)) {
		$buf=fgets($fp,1024);
		$var=explode('#',$buf);
		if (empty($var) || count($var)<2)
			continue;

		$id=trim_leading_zero(trim($var[0]));
		$des=trim($var[1]);

		$data=array( $x2->agentid, $typ, $id, $des );
		if (!$db->Execute($query,$data)) {
			echo "<h6>".$db->ErrorMsg()."</h6>";
			$nerr++;
		}
		else {
			$nrec++;
		}
	}
	fclose($fp);

	echo "<h5>$nrec records replaced in ".$prof->elapsed()." seconds, $nerr errors.</h5>";
}
//--------------------------------------
function load_supplier()
{
   global $x2;
   $db=&$x2->db;

   $prof=new cPROFILER1();

   $fname=$GLOBALS['cfx']->ascii.'\\SUPPLIERS.TXT';
	if (($fp=fopen($fname,"r"))===false) {
		echo "<h6>Could not open file ".myhtml($fname)."</h6>";
		return;
	}

   // Delete old values
	$query="DELETE FROM ishop_sup WHERE AGENTID=?";
	$data=array($x2->agentid);
	if (!$db->Execute($query,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      fclose($fp);
      return;
   }

	$query="INSERT INTO ishop_sup (AGENTID,LOGIN,COMPANY,TRKEY) VALUES(?,?,?,?)";

	$nrec=0;
	$nerr=0;
	while (!feof($fp)) {
		$buf=fgets($fp,1024);
		$var=explode('#',$buf);
		if (empty($var) || count($var)<2)
			continue;

     	$trkey=ToEnglish( trim($var[0])." ".trim($var[1]) );
		$data=array( $x2->agentid, trim($var[0]), trim($var[1]), $trkey );
		if (!$db->Execute($query,$data)) {
			echo "<h6>".$db->ErrorMsg()."</h6>";
			$nerr++;
		}
		else {
			$nrec++;
		}
	}
	fclose($fp);

	echo "<h5>$nrec records replaced in ".$prof->elapsed()." seconds, $nerr errors.</h5>";
}
//--------------------------------------
function ask_load_item_cart()
{
   global $x2;

   echo "<table>";
   $f1=new cFORM('goForm');
   $f1->enctype='multipart/form-data';
	$f1->vars=array( 'FUNC2'=>'120' );
	$f1->open();
?>
<tr>
	<td class=bgLabel>Ascii file</td>
	<td class=bgInput><input type=FILE name="ASCIIFILE" size=40></td>
</tr>
<tr>
	<td class=bgLabel></td>
	<td class=bgInput><input type=SUBMIT name="ok" value="<?php $x2->xprint('send'); ?>"></td>
</tr>
</form>
</table>
<?php
}
//--------------------------------------
function do_load_item_cart()
{
   global $x2;
   $db=&$x2->db;

   $prof=new cPROFILER1();

   $fname=$_FILES['ASCIIFILE']['tmp_name'];
	if (($fp=fopen($fname,"rb"))===false) {
		echo "<h6>Could not open file ".myhtml($fname)."</h6>";
		return;
	}

   // Delete old values
	$query="DELETE FROM ishop_item WHERE AGENTID=?";
	$data=array($x2->agentid);
	if (!$db->Execute($query,$data)) {
      echo "<h6>".$db->ErrorMsg()."</h6>";
      fclose($fp);
      return;
   }

	$query="INSERT INTO ishop_item (AGENTID, CODE, BARCODE, NAME, TRKEY) VALUES(?,?,?,?,  ?)";

   $icode=false;
   $subid=0;
	$nrec=0;
	$nerr=0;
	while (!feof($fp)) {
		$buf=fgets($fp,1024);
		if (strlen($buf)<54)
         continue;

      $icode=trim(substr($buf,0,13));
      $iname=trim(substr($buf,13,39));
      $trkey=ToEnglish($icode." ".$iname);
   	$data=array( $x2->agentid, $icode, $icode, $iname, $trkey);
	   if (!$db->Execute($query,$data)) {
			echo "<h6>".$db->ErrorMsg()."</h6>";
			$nerr++;
		}
		else {
			$nrec++;
		}
	}
	fclose($fp);

	echo "<h5>$nrec records replaced in ".$prof->elapsed()." seconds, $nerr errors.</h5>";
}
//--------------------------------------
?>
</body>
</html>

