<?php

/* $Id: Z_ReverseSuppPaymentRun.php 4835 2012-01-22 22:17:50Z vvs2012 $*/

/* Script to delete all supplier payments entered or created from a payment run on a specified day
 */

include ('includes/session.inc');
$title = _('Reverse and Delete Supplier Payments');
include('includes/header.inc');


/*Only do deletions if user hits the button */
if (isset($_POST['RevPayts']) AND Is_Date($_POST['PaytDate'])==1){

	$SQLTranDate = FormatDateForSQL($_POST['PaytDate']);

	$SQL = "SELECT id,
			transno,
			supplierno,
			ovamount,
			suppreference,
			rate
		FROM supptrans
		WHERE supptrans.type = 22
		AND trandate = '" . $SQLTranDate . "'";

	$Result = DB_query($SQL,$db);
	prnMsg(_('The number of payments that will be deleted is') . ' :' . DB_num_rows($Result),'info');

	while ($Payment = DB_fetch_array($Result)){
		prnMsg(_('Deleting payment number') . ' ' . $Payment['transno'] . ' ' . _('to supplier code') . ' ' . $Payment['supplierno'] . ' ' . _('for an amount of') . ' ' . $Payment['ovamount'],'info');

		$SQL = "DELETE FROM supptrans
			WHERE type=22
			AND transno='" . $Payment['transno'] . "'
			AND trandate='" . $SQLTranDate . "'";

		$DelResult = DB_query($SQL,$db);
		prnMsg(_('Deleted the SuppTran record'),'success');


		$SQL = "SELECT supptrans.transno,
				supptrans.type,
				suppallocs.amt
			FROM supptrans INNER JOIN suppallocs
			ON supptrans.id=suppallocs.transid_allocto
			WHERE suppallocs.transid_allocfrom = " .  $Payment['id'];

		$AllocsResult = DB_query($SQL,$db);
		while ($Alloc = DB_fetch_array($AllocsResult)){

			$SQL= "UPDATE supptrans SET settled=0,
										alloc=alloc-" . $Alloc['amt'] . ",
										diffonexch = diffonexch - ((" . $Alloc['Amt'] . "/rate ) - " . $Alloc['amt']/$Payment['rate'] . ")
									WHERE supptrans.type='" . $Alloc['type'] . "'
										AND transno='" . $Alloc['transno'] . "'";

			$ErrMsg =_('The update to the suppliers charges that were settled by the payment failed because');
			$UpdResult = DB_query($SQL,$db,$ErrMsg);

		}

		prnMsg(' ... ' . _('reversed the allocations'),'info');
		$SQL= "DELETE FROM suppallocs WHERE transid_allocfrom='" . $Payment['id'] . "'";
		$DelResult = DB_query($SQL,$db);
		prnMsg(' ... ' . _('deleted the SuppAllocs records'),'info');

		$SQL= "DELETE FROM gltrans WHERE typeno='" . $Payment['transno'] . "' AND type=22";
		$DelResult = DB_query($SQL,$db);
		prnMsg(' .... ' . _('the GLTrans records (if any)'),'info');

		$SQL= "DELETE FROM banktrans
				WHERE ref='" . $Payment['suppreference'] . ' ' . $Payment['supplierno'] . "'
				AND amount=" . $Payment['ovamount'] . "
				AND transdate = '" . $SQLTranDate . "'";
		$DelResult = DB_query($SQL,$db);
		prnMsg(' .... ' . _('and the BankTrans record'),'info');

	}


}


echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF']) . '">';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<br />' . _('Enter the date of the payment run') . ': <input type="text" name="PaytDate" maxlength="11" size="11" value="' . $_POST['PaytDate'] . '" />';
echo '<input type="submit" name="RevPayts" value="' . _('Reverse Supplier Payments on the Date Entered') . '" />';
echo '</form>';

include('includes/footer.inc');
?>