Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[fa-stable.git] / reporting / rep112.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12
13 $page_security = $_POST['PARAM_0'] == $_POST['PARAM_1'] ?
14         'SA_SALESTRANSVIEW' : 'SA_SALESBULKREP';
15 // ----------------------------------------------------------------
16 // $ Revision:  2.0 $
17 // Creator:     Joe Hunt
18 // date_:       2005-05-19
19 // Title:       Receipts
20 // ----------------------------------------------------------------
21 $path_to_root="..";
22
23 include_once($path_to_root . "/includes/session.inc");
24 include_once($path_to_root . "/includes/date_functions.inc");
25 include_once($path_to_root . "/includes/data_checks.inc");
26
27 //----------------------------------------------------------------------------------------------------
28
29 print_receipts();
30
31 //----------------------------------------------------------------------------------------------------
32 function get_receipt($type, $trans_no)
33 {
34     $sql = "SELECT trans.*,
35                                 (trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax) AS Total,
36                                 trans.ov_discount, 
37                                 debtor.name AS DebtorName,
38                                 debtor.debtor_ref,
39                                 debtor.curr_code,
40                                 debtor.payment_terms,
41                                 debtor.tax_id AS tax_id,
42                                 debtor.address
43                         FROM ".TB_PREF."debtor_trans trans,"
44                                 .TB_PREF."debtors_master debtor
45                                 WHERE trans.debtor_no = debtor.debtor_no
46                                 AND trans.type = ".db_escape($type)."
47                                 AND trans.trans_no = ".db_escape($trans_no);
48         $result = db_query($sql, "The remittance cannot be retrieved");
49         if (db_num_rows($result) == 0)
50                 return false;
51     return db_fetch($result);
52 }
53
54 function print_receipts()
55 {
56         global $path_to_root, $systypes_array;
57
58         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
59
60         $from = $_POST['PARAM_0'];
61         $to = $_POST['PARAM_1'];
62         $currency = $_POST['PARAM_2'];
63     $email = $_POST['PARAM_3'];
64         $comments = $_POST['PARAM_4'];
65         $orientation = $_POST['PARAM_5'];
66
67         if (!$from || !$to) return;
68
69         $orientation = ($orientation ? 'L' : 'P');
70         $dec = user_price_dec();
71
72         $fno = explode("-", $from);
73         $tno = explode("-", $to);
74         $from = min($fno[0], $tno[0]);
75         $to = max($fno[0], $tno[0]);
76
77         $cols = array(4, 85, 150, 225, 275, 360, 450, 515);
78
79         // $headers in doctext.inc
80         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right');
81
82         $params = array('comments' => $comments);
83
84         $cur = get_company_Pref('curr_default');
85
86         if ($email == 0)
87                 $rep = new FrontReport(_('RECEIPT'), "ReceiptBulk", user_pagesize(), 9, $orientation);
88         if ($orientation == 'L')
89         recalculate_cols($cols);
90
91         for ($i = $from; $i <= $to; $i++)
92         {
93                 if ($fno[0] == $tno[0])
94                         $types = array($fno[1]);
95                 else
96                         $types = array(ST_BANKDEPOSIT, ST_CUSTPAYMENT);
97                 foreach ($types as $j)
98                 {
99                         $myrow = get_receipt($j, $i);
100                         if (!$myrow)
101                                 continue;
102                         if ($currency != ALL_TEXT && $myrow['curr_code'] != $currency) {
103                                 continue;
104                         }
105                         $res = get_bank_trans($j, $i);
106                         $baccount = db_fetch($res);
107                         $params['bankaccount'] = $baccount['bank_act'];
108
109                         if ($email == 1)
110                         {
111                                 $rep = new FrontReport("", "", user_pagesize(), 9, $orientation);
112                                 $rep->title = _('RECEIPT');
113                                 $rep->filename = "Receipt" . $i . ".pdf";
114                         }
115                         $rep->currency = $cur;
116                         $rep->Font();
117                         $rep->Info($params, $cols, null, $aligns);
118
119                         $contacts = get_branch_contacts($myrow['branch_code'], 'invoice', $myrow['debtor_no']);
120                         $rep->SetCommonData($myrow, null, $myrow, $baccount, ST_CUSTPAYMENT, $contacts);
121                         $rep->SetHeaderType('Header2');
122                         $rep->NewPage();
123                         $result = get_allocatable_to_cust_transactions($myrow['debtor_no'], $myrow['trans_no'], $myrow['type']);
124
125                         $doctype = ST_CUSTPAYMENT;
126
127                         $total_allocated = 0;
128                         $rep->TextCol(0, 4,     _("As advance / full / part / payment towards:"), -2);
129                         $rep->NewLine(2);
130
131                         while ($myrow2=db_fetch($result))
132                         {
133                                 $rep->TextCol(0, 1,     $systypes_array[$myrow2['type']], -2);
134                                 $rep->TextCol(1, 2,     $myrow2['reference'], -2);
135                                 $rep->TextCol(2, 3,     sql2date($myrow2['tran_date']), -2);
136                                 $rep->TextCol(3, 4,     sql2date($myrow2['due_date']), -2);
137                                 $rep->AmountCol(4, 5, $myrow2['Total'], $dec, -2);
138                                 $rep->AmountCol(5, 6, $myrow2['Total'] - $myrow2['alloc'], $dec, -2);
139                                 $rep->AmountCol(6, 7, $myrow2['amt'], $dec, -2);
140
141                                 $total_allocated += $myrow2['amt'];
142                                 $rep->NewLine(1);
143                                 if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
144                                         $rep->NewPage();
145                         }
146
147                         $memo = get_comments_string($j, $i);
148                         if ($memo != "")
149                         {
150                                 $rep->NewLine();
151                                 $rep->TextColLines(1, 5, $memo, -2);
152                         }
153
154                         $rep->row = $rep->bottomMargin + (16 * $rep->lineHeight);
155
156                         $rep->TextCol(3, 6, _("Total Allocated"), -2);
157                         $rep->AmountCol(6, 7, $total_allocated, $dec, -2);
158                         $rep->NewLine();
159                         $rep->TextCol(3, 6, _("Left to Allocate"), -2);
160                         $rep->AmountCol(6, 7, $myrow['Total'] + $myrow['ov_discount'] - $total_allocated, $dec, -2);
161                         if (floatcmp($myrow['ov_discount'], 0))
162                         {
163                                 $rep->NewLine();
164                                 $rep->TextCol(3, 6, _("Discount"), - 2);
165                                 $rep->AmountCol(6, 7, -$myrow['ov_discount'], $dec, -2);
166                         }       
167                         $rep->NewLine();
168                         $rep->Font('bold');
169                         $rep->TextCol(3, 6, _("TOTAL RECEIPT"), - 2);
170                         $rep->AmountCol(6, 7, $myrow['Total'], $dec, -2);
171
172                         $words = price_in_words($myrow['Total'], ST_CUSTPAYMENT);
173                         if ($words != "")
174                         {
175                                 $rep->NewLine(1);
176                                 $rep->TextCol(0, 7, $myrow['curr_code'] . ": " . $words, - 2);
177                         }
178                         $rep->Font();
179                         $rep->NewLine();
180                         $rep->TextCol(6, 7, _("Received / Sign"), - 2);
181                         $rep->NewLine();
182                         $rep->TextCol(0, 2, _("By Cash / Cheque* / Draft No."), - 2);
183                         $rep->TextCol(2, 4, "______________________________", - 2);
184                         $rep->TextCol(4, 5, _("Dated"), - 2);
185                         $rep->TextCol(5, 6, "__________________", - 2);
186                         $rep->NewLine(1);
187                         $rep->TextCol(0, 2, _("Drawn on Bank"), - 2);
188                         $rep->TextCol(2, 4, "______________________________", - 2);
189                         $rep->TextCol(4, 5, _("Branch"), - 2);
190                         $rep->TextCol(5, 6, "__________________", - 2);
191                         $rep->TextCol(6, 7, "__________________");
192                         if ($email == 1)
193                         {
194                                 $rep->End($email);
195                         }
196                 }
197         }
198     if ($email == 0)
199                 $rep->End();
200 }
201