5 /* Script to delete a credit note - it expects and credit note number to delete
6 not included on any menu for obvious reasons
8 must be called directly with path/DeleteCreditnote.php?CreditNoteNo=???????
14 include ('includes/session.inc');
15 $title = _('Delete Credit Note');
16 include('includes/header.inc');
19 if (!isset($_GET['CreditNoteNo'])){
20 prnMsg(_('This page must be called with the credit note number') . ' - ' . _('it is not intended for use by non-system administrators'),'info');
22 /*get the order number that was credited */
24 $SQL = 'SELECT order_ FROM debtortrans WHERE transno=' . $_GET['CreditNoteNo'] . ' AND type=11';
25 $Result = DB_query($SQL, $db);
27 $myrow = DB_fetch_row($Result);
30 /*Now get the stock movements that were credited into an array */
32 $SQL = 'SELECT stockid,
39 WHERE transno =' .$_GET['CreditNoteNo'] . ' AND type=11';
40 $Result = DB_query($SQL,$db);
44 While ($myrow = DB_fetch_array($Result)){
45 $StockMovement[$i] = $myrow;
49 prnMsg(_('The number of stock movements to be deleted is') . ': ' . DB_num_rows($Result),'info');
52 $Result = DB_query('BEGIN',$db); /* commence a database transaction */
53 /*Now delete the DebtorTrans */
55 $SQL = 'DELETE FROM debtortrans
56 WHERE TransNo =' . $_GET['CreditNoteNo'] . ' AND Type=11';
57 $DbgMsg = _('The SQL that failed was');
58 $ErrMsg = _('A problem was encountered trying to delete the Debtor transaction record');
59 $Result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
61 /*Now reverse updated SalesOrderDetails for the quantities credited */
63 foreach ($StockMovement as $CreditLine) {
65 $SQL = 'UPDATE salesorderdetails SET qtyinvoiced = qtyinvoiced - ' . $CreditLine['qty'] . '
66 WHERE orderno = ' . $OrderNo . "
67 AND stkcode = '" . $CreditLine['stockid'] . "'";
69 $ErrMsg =_('A problem was encountered attempting to reverse the update the sales order detail record') . ' - ' . _('the SQL server returned the following error message');
70 $Result = DB_query($SQL,$db,$ErrMsg,$DbgMsg, true);
72 /*reverse the update to LocStock */
74 $SQL = 'UPDATE locstock SET locstock.quantity = locstock.quantity + ' . $CreditLine['qty'] . "
75 WHERE locstock.stockid = '" . $CreditLine['stockid'] . "'
76 AND loccode = '" . $CreditLine['loccode'] . "'";
78 $ErrMsg = _('SQL to reverse update to the location stock records failed with the error');
80 $Result = DB_query($SQL, $db,$ErrMsg,$DbgMsg, true);
82 /*Delete Sales Analysis records */
83 $SQL = 'DELETE FROM salesanalysis
84 WHERE periodno = ' . $CreditLine['prd'] . "
85 AND cust='" . $CreditLine['debtorno'] . "'
86 AND custbranch = '" . $CreditLine['branchcode'] . "'
87 AND qty = " . $CreditLine['qty'] . "
88 AND stockid = '" . $CreditLine['stockid'] . "'";
90 $ErrMsg = _('The SQL to delete the sales analysis records with the message');
92 $Result = DB_query($SQL, $db,$ErrMsg,$DbgMsg,true);
95 /* Delete the stock movements */
97 $SQL = 'DELETE FROM stockmoves
98 WHERE type=11 AND transno = ' . $_GET['CreditNoteNo'];
100 $ErrMsg = _('SQL to delete the stock movement record failed with the message');
101 $Result = DB_query($SQL, $db,$ErrMsg,$DbgMsg,true);
102 prnMsg(_('Deleted the credit note stock movements').'info');
105 $result = DB_query('COMMIT',$db);
106 prnMsg(_('Credit note number') . ' ' . $_GET['CreditNoteNo'] . ' ' . _('has been completely deleted') . '. ' . _('To ensure the integrity of the general ledger transactions must be reposted from the period the credit note was created'),'info');
108 include('includes/footer.inc');