1<?php
2/*Code to print footer details for each supplier being paid and process payment total for each supplier
3as necessary an include file used since the same code is used twice */
4$YPos -= (0.5*$line_height);
5$pdf->line($Left_Margin, $YPos+$line_height,$Page_Width-$Right_Margin, $YPos+$line_height);
6
7$LeftOvers = $pdf->addTextWrap($Left_Margin+10,$YPos,340-$Left_Margin,$FontSize,_('Total Due For') . ' ' . $SupplierName, 'left');
8
9$TotalPayments += $AccumBalance;
10$TotalAccumDiffOnExch += $AccumDiffOnExch;
11
12$LeftOvers = $pdf->addTextWrap(340,$YPos,60,$FontSize,locale_number_format($AccumBalance,$CurrDecimalPlaces), 'right');
13$LeftOvers = $pdf->addTextWrap(405,$YPos,60,$FontSize,locale_number_format($AccumDiffOnExch,$CurrDecimalPlaces), 'right');
14
15
16if (isset($_POST['PrintPDFAndProcess'])){
17
18	if (is_numeric(filter_number_format($_POST['Ref']))) {
19		$PaytReference = filter_number_format($_POST['Ref']) + $RefCounter;
20	} else {
21		$PaytReference = $_POST['Ref'] . ($RefCounter + 1);
22	}
23	$RefCounter++;
24
25	/*Do the inserts for the payment transaction into the Supp Trans table*/
26
27	$SQL = "INSERT INTO supptrans (type,
28					transno,
29					suppreference,
30					supplierno,
31					trandate,
32					duedate,
33					inputdate,
34					settled,
35					rate,
36					ovamount,
37					diffonexch,
38					alloc)
39			VALUES (22,
40				'" . $SuppPaymentNo . "',
41				'" . $PaytReference . "',
42				'" . $SupplierID . "',
43				'" . FormatDateForSQL($_POST['AmountsDueBy']) . "',
44				'" . FormatDateForSQL($_POST['AmountsDueBy']) . "',
45				'" . date('Y-m-d H-i-s') . "',
46				1,
47				'" . filter_number_format($_POST['ExRate']) . "',
48				'" . -$AccumBalance . "',
49				'" . -$AccumDiffOnExch . "',
50				'" . -$AccumBalance . "')";
51
52	$ProcessResult = DB_query($SQL,'','',false,false);
53	if (DB_error_no() !=0) {
54		$Title = _('Payment Processing - Problem Report');
55		include('header.php');
56		prnMsg(_('None of the payments will be processed because the payment record for') . ' ' . $SupplierName . ' ' . _('could not be inserted because') . ' - ' . DB_error_msg(),'error');
57		echo '<br>
58				<a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
59		if ($debug==1){
60			prnMsg(_('The SQL that failed was') . ' ' . $SQL,'error');
61		}
62		$ProcessResult = DB_Txn_Rollback();
63		include('footer.php');
64		exit;
65	}
66
67	$PaymentTransID = DB_Last_Insert_ID('supptrans','id');
68
69	/*Do the inserts for the allocation record against the payment for this charge */
70
71	foreach ($Allocs AS $AllocTrans){ /*loop through the array of allocations */
72
73		$SQL = "INSERT INTO suppallocs (amt,
74						datealloc,
75						transid_allocfrom,
76						transid_allocto)
77				VALUES (
78						'" . $AllocTrans->Amount . "',
79						'" . FormatDateForSQL($_POST['AmountsDueBy']) . "',
80						'" . $PaymentTransID . "',
81						'" . $AllocTrans->TransID . "')";
82
83		$ProcessResult = DB_query($SQL);
84		if (DB_error_no() !=0) {
85			$Title = _('Payment Processing - Problem Report') . '.... ';
86			include('header.php');
87			prnMsg(_('None of the payments will be processed since an allocation record for') . $SupplierName . _('could not be inserted because') . ' - ' . DB_error_msg(),'error');
88			echo '<br><a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
89			if ($debug==1){
90				prnMsg(_('The SQL that failed was') . $SQL,'error');
91			}
92			$ProcessResult = DB_Txn_Rollback();
93			include('footer.php');
94			exit;
95		}
96	} /*end of the loop to insert the allocation records */
97
98
99	/*Do the inserts for the payment transaction into the BankTrans table*/
100	$SQL="INSERT INTO banktrans (bankact,
101					ref,
102					exrate,
103					transdate,
104					banktranstype,
105					amount) ";
106   	$SQL = $SQL .  "VALUES ( " . $_POST['BankAccount'] . ",
107				'" . $PaytReference . " " . $SupplierID . "',
108				" . filter_number_format($_POST['ExRate']) . ",
109				'" . FormatDateForSQL($_POST['AmountsDueBy']) . "',
110				'" . $_POST['PaytType'] . "',
111				" .  -$AccumBalance . ")";
112	$ProcessResult = DB_query($SQL,'','',false,false);
113	if (DB_error_no() !=0) {
114		$Title = _('Payment Processing - Problem Report');
115		include('header.php');
116		prnMsg(_('None of the payments will be processed because the bank account payment record for') . ' ' . $SupplierName . ' ' . _('could not be inserted because') . ' - ' . DB_error_msg(),'error');
117		echo '<br />
118				<a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
119		if ($debug==1){
120			prnMsg(_('The SQL that failed was') . ' ' . $SQL,'error');
121		}
122		$ProcessResult = DB_Txn_Rollback();
123		include('footer.php');
124		exit;
125	}
126
127	/*If the General Ledger Link is activated */
128	if ($_SESSION['CompanyRecord']['gllink_creditors']==1){
129
130		$PeriodNo = GetPeriod($_POST['AmountsDueBy']);
131
132		/*Do the GL trans for the payment CR bank */
133
134		$SQL = "INSERT INTO gltrans (type,
135						typeno,
136						trandate,
137						periodno,
138						account,
139						narrative,
140						amount )
141				VALUES (22,
142					'" . $SuppPaymentNo . "',
143					'" . FormatDateForSQL($_POST['AmountsDueBy']) . "',
144					'" . $PeriodNo . "',
145					'" . $_POST['BankAccount'] . "',
146					'" . $SupplierID . " - " . $SupplierName . ' ' . _('payment run on') . ' ' . Date($_SESSION['DefaultDateFormat']) . ' - ' . $PaytReference . "',
147					'" . (-$AccumBalance/ filter_number_format($_POST['ExRate'])) . "')";
148
149		$ProcessResult = DB_query($SQL,'','',false,false);
150		if (DB_error_no() !=0) {
151			$Title = _('Payment Processing') . ' - ' . _('Problem Report') . '.... ';
152			include('header.php');
153			prnMsg(_('None of the payments will be processed since the general ledger posting for the payment to') . ' ' . $SupplierName . ' ' . _('could not be inserted because') . ' - ' . DB_error_msg(),'error');
154			echo '<br />
155					<a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
156			if ($debug==1){
157				prnMsg(_('The SQL that failed was') . ':<br />' . $SQL, 'error');
158			}
159			$ProcessResult = DB_Txn_Rollback();
160			include('footer.php');
161			exit;
162		}
163
164		/*Do the GL trans for the payment DR creditors */
165
166		$SQL = "INSERT INTO gltrans (type,
167						typeno,
168						trandate,
169						periodno,
170						account,
171						narrative,
172						amount )
173				VALUES (22,
174					'" . $SuppPaymentNo . "',
175					'" . FormatDateForSQL($_POST['AmountsDueBy']) . "',
176					'" . $PeriodNo . "',
177					'" . $_SESSION['CompanyRecord']['creditorsact'] . "',
178					'" . $SupplierID . ' - ' . $SupplierName . ' ' . _('payment run on') . ' ' . Date($_SESSION['DefaultDateFormat']) . ' - ' . $PaytReference . "',
179					'" . ($AccumBalance/ filter_number_format($_POST['ExRate'])  + $AccumDiffOnExch) . "')";
180
181		$ProcessResult = DB_query($SQL,'','',false,false);
182		if (DB_error_no() !=0) {
183			$Title = _('Payment Processing - Problem Report');
184			include('header.php');
185			prnMsg(_('None of the payments will be processed since the general ledger posting for the payment to') . ' ' . $SupplierName . ' ' . _('could not be inserted because') . ' - ' . DB_error_msg(),'error');
186			echo '<br /><a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
187			if ($debug==1){
188				prnMsg(_('The SQL that failed was') . ':<BR>' . $SQL,'error');
189			}
190			$ProcessResult = DB_Txn_Rollback();
191			include('footer.php');
192			exit;
193		}
194
195		/*Do the GL trans for the exch diff */
196		if ($AccumDiffOnExch != 0){
197			$SQL = "INSERT INTO gltrans (type,
198										typeno,
199										trandate,
200										periodno,
201										account,
202										narrative,
203										amount )
204						VALUES (22,
205							'" . $SuppPaymentNo . "',
206							'" . FormatDateForSQL($_POST['AmountsDueBy']) . "',
207							'" . $PeriodNo . "',
208							'" . $_SESSION['CompanyRecord']['purchasesexchangediffact'] . "',
209							'" . $SupplierID . ' - ' . $SupplierName . ' ' . _('payment run on') . ' ' . Date($_SESSION['DefaultDateFormat']) . " - " . $PaytReference . "',
210							'" . (-$AccumDiffOnExch) . "')";
211
212			$ProcessResult = DB_query($SQL,'','',false,false);
213			if (DB_error_no() !=0) {
214				$Title = _('Payment Processing - Problem Report');
215				include('header.php');
216				prnMsg(_('None of the payments will be processed since the general ledger posting for the exchange difference on') . ' ' . $SupplierName . ' ' . _('could not be inserted because') .' - ' . DB_error_msg(),'error');
217				echo '<br /><a href="' . $RootPath . '/index.php">' . _('Back to the menu') . '</a>';
218				if ($debug==1){
219					prnMsg(_('The SQL that failed was: ') . '<br />' . $SQL,'error');
220				}
221				$ProcessResult = DB_Txn_Rollback();
222				include('footer.php');
223				exit;
224			}
225		}
226		EnsureGLEntriesBalance(22,$SuppPaymentNo);
227	} /*end if GL linked to creditors */
228
229
230}
231
232$YPos -= (1.5*$line_height);
233
234$pdf->line($Left_Margin, $YPos+$line_height,$Page_Width-$Right_Margin, $YPos+$line_height);
235
236$YPos -= $line_height;
237
238if ($YPos < $Bottom_Margin + $line_height){
239	$PageNumber++;
240	include('PDFPaymentRunPageHeader.inc');
241}
242
243?>