forked from webERP-team/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Z_SalesIntegrityCheck.php
213 lines (165 loc) · 6.56 KB
/
Z_SalesIntegrityCheck.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
// Script to do some Sales Integrity checks
// No SQL updates or Inserts - so safe to run
include ('includes/session.php');
$Title = _('Sales Integrity');
include('includes/header.php');
echo '<div class="centre"><h3>' . _('Sales Integrity Check') . '</h3></div>';
echo '<br /><br />' . _('Check every Invoice has a Sales Order') . '<br />';
echo '<br /><br />' . _('Check every Invoice has a Tax Entry') . '<br />';
echo '<br /><br />' . _('Check every Invoice has a GL Entry') . '<br />';
$SQL = 'SELECT id, transno, order_, trandate FROM debtortrans WHERE type = 10';
$Result = DB_query($SQL);
while ($myrow = DB_fetch_array($Result)) {
$SQL2 = "SELECT orderno, orddate FROM salesorders WHERE orderno = '" . $myrow['order_'] . "'";
$Result2 = DB_query($SQL2);
if ( DB_num_rows($Result2) == 0) {
echo '<br />' . _('Invoice '). ' '. $myrow['transno'] . ' : ';
echo '<div style="color:red">' . _('No Sales Order') . '</div>';
}
$SQL3 = "SELECT debtortransid FROM debtortranstaxes WHERE debtortransid = '" . $myrow['id'] . "'";
$Result3 = DB_query($SQL3);
if ( DB_num_rows($Result3) == 0) {
echo '<br />' . _('Invoice '). ' ' . $myrow['transno'] . ' : ';
echo '<div style="color:red">' . _('Has no Tax Entry') . '</div>';
}
$SQL4 = "SELECT typeno
FROM gltrans
WHERE type = 10
AND typeno = '" . $myrow['transno'] . "'";
$Result4 = DB_query($SQL4);
if ( DB_num_rows($Result4) == 0) {
echo '<br />' . _('Invoice') . ' ' . $myrow['transno'] . ' : ';
echo '<div style="color:red">' . _('has no GL Entry') . '</div>';
}
}
echo '<br /><br />' . _('Check for orphan GL Entries') . '<br />';
$SQL = "SELECT DISTINCT typeno, counterindex FROM gltrans WHERE type = 10";
$Result = DB_query($SQL);
while ($myrow = DB_fetch_array($Result)) {
$SQL2 = "SELECT id,
transno,
trandate
FROM debtortrans
WHERE type = 10
AND transno = '" . $myrow['typeno'] . "'";
$Result2 = DB_query($SQL2);
if ( DB_num_rows($Result2) == 0) {
echo "<br />"._('GL Entry ') . $myrow['counterindex'] . " : ";
echo ', <div style="color:red">' . _('Invoice ') . $myrow['typeno'] . _(' could not be found') . '</div>';
}
}
echo '<br /><br />' . _('Check Receipt totals') . '<br />';
$SQL = "SELECT typeno,
amount
FROM gltrans
WHERE type = 12
AND account = '" . $_SESSION['CompanyRecord']['debtorsact'] . "'";
$Result = DB_query($SQL);
while ($myrow = DB_fetch_array($Result)) {
$SQL2 = "SELECT SUM((ovamount+ovgst)/rate)
FROM debtortrans
WHERE type = 12
AND transno = '" . $myrow['typeno'] . "'";
$Result2 = DB_query($SQL2);
$myrow2 = DB_fetch_row($Result2);
if ( $myrow2[0] + $myrow['amount'] == 0 ) {
echo '<br />' . _('Receipt') . ' ' . $myrow['typeno'] . " : ";
echo '<div style="color:red">' . $myrow['amount']. ' ' . _('in GL but found'). ' ' . $myrow2[0] . ' ' . _('in debtorstrans') . '</div>';
}
}
echo '<br /><br />' . _('Check for orphan Receipts') . '<br />';
$SQL = "SELECT transno FROM debtortrans WHERE type = 12";
$Result = DB_query($SQL);
while ($myrow = DB_fetch_array($Result)) {
$SQL2 = "SELECT amount FROM gltrans WHERE type = 12 AND typeno = '" . $myrow['transno'] . "'";
$Result2 = DB_query($SQL2);
$myrow2 = DB_fetch_row($Result2);
if ( !$myrow2[0] ) {
echo '<br />' . _('Receipt') . ' ' . $myrow['transno'] . " : ";
echo '<div style="color:red">' . $myrow['transno'] . ' ' ._('not found in GL')."</div>";
}
}
echo '<br /><br />' . _('Check for orphan Sales Orders') . '<br />';
$SQL = "SELECT orderno, orddate FROM salesorders";
$Result = DB_query($SQL);
while ($myrow = DB_fetch_array($Result)) {
$SQL2 = "SELECT transno,
order_,
trandate
FROM debtortrans
WHERE type = 10
AND order_ = '" . $myrow['orderno'] . "'";
$Result2 = DB_query($SQL2);
if ( DB_num_rows($Result2) == 0) {
echo '<br />' . _('Sales Order') . ' ' . $myrow['orderno'] . ' : ';
echo '<div style="color:red">' . _('Has no Invoice') . '</div>';
}
}
echo '<br /><br />' . _('Check for orphan Order Items') . '<br />';
echo '<br /><br />' . _('Check Order Item Amounts') . '<br />';
$SQL = "SELECT orderno FROM salesorderdetails";
$Result = DB_query($SQL);
while ($myrow = DB_fetch_array($Result)) {
$SQL2 = "SELECT orderno, orddate FROM salesorders WHERE orderno = '" . $myrow['orderno'] . "'";
$Result2 = DB_query($SQL2);
if ( DB_num_rows($Result2) == 0) {
echo '<br />' . _('Order Item') . ' ' . $myrow['orderno'] . ' : ';
echo ', <div style="color:red">' . _('Has no Sales Order') . '</div>';
}
$sumsql = "SELECT ROUND(SUM(qtyinvoiced * unitprice * (1 - discountpercent)), 3) AS InvoiceTotal
FROM salesorderdetails
WHERE orderno = '" . $myrow['orderno'] . "'";
$sumresult = DB_query($sumsql);
if ($sumrow = DB_fetch_array($sumresult)) {
$invSQL = "SELECT transno,
type,
trandate,
settled,
rate,
SUM(ovamount) AS ovamount,
ovgst
FROM debtortrans WHERE order_ = '" . $myrow['orderno'] . "'";
$invResult = DB_query($invSQL);
while( $invrow = DB_fetch_array($invResult) ) {
// Ignore credit notes
if ( $invrow['type'] != 11 ) {
// Do an integrity check on sales order items
if ( $sumrow['InvoiceTotal'] != $invrow['ovamount'] ) {
echo '<br /><div style="color:red">' . _('Debtors trans') . ' ' . $invrow['ovamount'] . ' ' . _('differ from salesorderdetails') . ' ' . $sumrow['InvoiceTotal'] . '</div>';
}
}
}
}
}
echo '<br /><br />' . _('Check for orphan Stock Moves') . '<br />';
$SQL = "SELECT stkmoveno, transno FROM stockmoves";
$Result = DB_query($SQL);
while ($myrow = DB_fetch_array($Result)) {
$SQL2 = "SELECT transno,
order_,
trandate
FROM debtortrans
WHERE type BETWEEN 10 AND 11
AND transno = '" . $myrow['transno'] . "'";
$Result2 = DB_query($SQL2);
if ( DB_num_rows($Result2) == 0) {
echo '<br />' . _('Stock Move') . ' ' . $myrow['stkmoveno'] . ' : ';
echo ', <div style="color:red">' . _('Has no Invoice') . '</div>';
}
}
echo '<br /><br />' . _('Check for orphan Tax Entries') . '<br />';
$SQL = "SELECT debtortransid FROM debtortranstaxes";
$Result = DB_query($SQL);
while ($myrow = DB_fetch_array($Result)) {
$SQL2 = "SELECT id, transno, trandate FROM debtortrans WHERE type BETWEEN 10 AND 11 AND id = '" . $myrow['debtortransid'] . "'";
$Result2 = DB_query($SQL2);
if ( DB_num_rows($Result2) == 0) {
echo '<br />' . _('Tax Entry') . ' ' . $myrow['debtortransid'] . ' : ';
echo ', <div style="color:red">' . _('Has no Invoice') . '</div>';
}
}
echo '<br /><br />' . _('Sales Integrity Check completed.') . '<br /><br />';
prnMsg(_('Sales Integrity Check completed.'),'info');
include('includes/footer.php');
?>