﻿<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate");  // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: text/plain; charset=utf-8");

include("../lib/ado/adodb.inc.php");
include("../lib/generic.inc.php");
require_once("ishop_all.inc.php");
include("restau_deskx.php");
include('restau.inc.php');

// www.json.org
class cERROR
{
   var $e;
   var $msg;

   function cERROR()
   {
      $this->e=-1; // unknown error
      $this->msg='';
   }
}

$cerror=new cERROR();
ob_start();

$c1->fields=array('RET');

$db=false;
$iid=false;
do {
   $db = &db_open();
   if (!$db) {
	   $cerror->e=3;
      $cerror->msg='Database connection failed!';
	   $db=false;
	   break;
	}
   $ADODB_FETCH_MODE = ADODB_FETCH_NUM;

   $iid=log_save_input($db, 'DO_LOG_SCANNER');

   $ok=true;
   $ar1=array('COM','SCAN','RESELID','REPASS');
   foreach ($ar1 as $k1) {
      if (!isset($_REQUEST[$k1])) {
         $cerror->e=1;
         $cerror->msg='Call error: missing variable!';
         $ok=false;
         break;
      }
   }
   if (!$ok)
      break;

   $scan=explode('.',$_REQUEST['SCAN']);
   if (count($scan)!=2) {
      $cerror->e=2;
      $cerror->msg='Data format error (a)!';
      break;
   }

   $ce=new cDESK($db);
   // quick_login() requires RESELID and REPASS
   if ( $ce->quick_login()) {
      $ce->read_agent_main();
   }
   else if (!$ce->validate_session() ) { // err
      $cerror->e=4;
      $cerror->msg='Authentication failure!';
      break;
   }
   // If it happened to call validate_session(), then read_agent_main() has been called too.

   $ce->read_opp();

   switch ((int)$scan[0])
   {
   case 1: // orderid
      process_orderid($ce,$scan[1]);
      break;
   case 2: // delivery id
      process_dlvid($ce,$scan[1]);
      break;
   default:
      $cerror->e=2;
      $cerror->msg='Data format error (b)!';
      break;
   }

} while (false);

$buffer=ob_get_clean();
log_save_output($db, $iid, $buffer);
if (!empty($_REQUEST['VERBOSE']))
   echo ($cerror->e.":".$cerror->msg."\n".$buffer);
else
   echo $cerror->e;

if ($db!==false)
   $db->Close();
//--------------------------------------
function process_orderid(&$ce,$datain)
{
   global $cerror;

   $db=&$ce->db;

   $i=0;
   while ($datain{$i}=='0') {
      $i++;
   }
   $orderid=(int)substr($datain,$i);

   if ($orderid<=0) {
      $cerror->e=2;
      $cerror->msg='Data format error (c)!';
      return;
   }

   // Check if order is acceptable (select should return 1)
   $n=0;
   $query="SELECT COUNT(*) FROM order_main WHERE ORDERID=? AND STATE IN (9,10) AND RE_RESELID=-1";
   $data=array($orderid);
   if (($rs=$db->Execute($query,$data))===false) {
   	$cerror->e=5; // SQL error!
      $cerror->msg=$db->ErrorMsg();
   	return;
	}
	$row=$rs->FetchRow();
	$rs->Close();
	if ($row!==false)
      $n=(int)$row[0];
   if ($n==0) {
      $cerror->e=6;
      $cerror->msg='���������� ����������';
      return;
   }

   // �������� ����������� (STATE=0) � ����������� & ���./������� (STATE=1, PRINTED=0)?
	$n=1;
   $query="SELECT COUNT(*) FROM order_line WHERE ORDERID=? AND SUBID=0 AND
      (STATE=0 OR (STATE=1 AND (PRINTED=0 OR PRINTED IS NULL)))";
   $data=array($orderid);
   if (($rs=$db->Execute($query,$data))===false) {
   	$cerror->e=5; // SQL error!
      $cerror->msg=$db->ErrorMsg();
   	return;
	}
	$row=$rs->FetchRow();
	$rs->Close();
	if ($row!==false)
      $n=(int)$row[0];
   if ($n>0) {
      $cerror->e=6;
      $cerror->msg='���������� ����������';
      return;
   }

   $query="INSERT INTO ishop_breader (AGENTID, RESELID, COMNR, ORDERID, ORDSCAN)
      VALUES(?,?,?,?,NOW())";
	$data=array($ce->agentid, (int)$_REQUEST['RESELID'], (int)$_REQUEST['COM'], $orderid );
	if (!$db->Execute($query,$data)) {
      $cerror->e=5; // SQL error!
      $cerror->msg=$db->ErrorMsg();
      return;
   }

   $cerror->e=0; // o.k.
}
//--------------------------------------
function process_dlvid(&$ce,$datain)
{
   global $cerror;

   $db=&$ce->db;

   // Validate input
   $i=0;
   while ($datain{$i}=='0') {
      $i++;
   }
   $bcode=(int)substr($datain,$i);
   $query="SELECT RESELID FROM reseller WHERE AGENTID=? AND RE_BARCODE=?";
   $data=array($ce->agentid, $bcode);
   if (($rs=$db->Execute($query,$data))===false) {
   	$cerror->e=5; // SQL error!
      $cerror->msg=$db->ErrorMsg();
   	return;
	}
	$row=$rs->FetchRow();
	$rs->Close();
	if ($row===false) {
      $cerror->e=7;
      $cerror->msg='Delivery login error';
   	return;
   }
   $delivery=(int)$row[0];

   // Log scan
   $query="INSERT INTO ishop_breader (AGENTID, RESELID, COMNR, ORDERID, DELIVERY, DLVRSCAN)
      VALUES( ?, ?, ?, 0, ?, NOW() )";
	$data=array($ce->agentid, (int)$_REQUEST['RESELID'], (int)$_REQUEST['COM'], $delivery );
	if (!$db->Execute($query,$data)) {
      $cerror->e=5; // SQL error!
      $cerror->msg=$db->ErrorMsg();
      return;
   }

   $oids=array();
   $delta=(int)@$ce->Agp['SCANDISTANCE'];
   $deltaType=(string)@$ce->Agp['SCANDISTUNIT'];
   if ($delta==0) {
      $delta=10;
      $deltaType='MINUTE';
   }
   $query="SELECT T1.ORDERID FROM ishop_breader T1
      LEFT JOIN order_main T2 ON (T2.ORDERID=T1.ORDERID AND T2.AGENTID=T1.AGENTID)
      WHERE T1.AGENTID=? AND T1.RESELID=? AND T1.COMNR=? AND T1.ORDERID>0 AND
      DATE_ADD(T1.ORDSCAN, INTERVAL $delta $deltaType)>=NOW()
      AND T2.ORDTYPE=0 AND T2.STATE IN (9,10)";
   $data=array($ce->agentid, (int)$_REQUEST['RESELID'], (int)$_REQUEST['COM'] );
   if (($rs=$db->Execute($query,$data))===false) {
      $cerror->e=5; // SQL error!
      $cerror->msg=$db->ErrorMsg();
      return;
   }
   while ($row=$rs->FetchRow()) {
      $oids[]=$row[0];
   }
   $rs->Close();

   if (empty($oids)) {
      $cerror->e=8; // No data
      $cerror->msg='No orders available!';
      return;
   }

   print_r($oids); // For VERBOSE

   $_REQUEST['DLVR']=$delivery;
   foreach ($oids as $oid) {
      $ce->orderid=$oid;
      pay_order_PRO($ce,110);
   }

   $cerror->e=0; // o.k.
}
//--------------------------------------
?>
