Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[fa-stable.git] / reporting / rep210.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_SUPPTRANSVIEW' : 'SA_SUPPBULKREP';
15 // ----------------------------------------------------------------
16 // $ Revision:  2.0 $
17 // Creator:     Joe Hunt
18 // date_:       2005-05-19
19 // Title:       Purchase Remittance
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 include_once($path_to_root . "/includes/db/crm_contacts_db.inc");
27
28 //----------------------------------------------------------------------------------------------------
29
30 print_remittances();
31
32 //----------------------------------------------------------------------------------------------------
33 function get_remittance($type, $trans_no)
34 {
35         $sql = "SELECT trans.*, 
36                 (trans.ov_amount+trans.ov_gst) AS Total,
37                 trans.ov_discount,
38                 supplier.supp_name,  supplier.supp_account_no, 
39                 supplier.curr_code, supplier.payment_terms, supplier.gst_no AS tax_id, 
40                 supplier.address
41                 FROM "
42                         .TB_PREF."supp_trans trans,"
43                         .TB_PREF."suppliers supplier
44                 WHERE trans.supplier_id = supplier.supplier_id
45                 AND trans.type = ".db_escape($type)."
46                 AND trans.trans_no = ".db_escape($trans_no);
47         $result = db_query($sql, "The remittance cannot be retrieved");
48         if (db_num_rows($result) == 0)
49                 return false;
50     return db_fetch($result);
51 }
52
53 function print_remittances()
54 {
55         global $path_to_root, $systypes_array;
56
57         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
58
59         $from = $_POST['PARAM_0'];
60         $to = $_POST['PARAM_1'];
61         $currency = $_POST['PARAM_2'];
62         $email = $_POST['PARAM_3'];
63         $comments = $_POST['PARAM_4'];
64         $orientation = $_POST['PARAM_5'];
65
66         if (!$from || !$to) return;
67
68         $orientation = ($orientation ? 'L' : 'P');
69         $dec = user_price_dec();
70
71         $fno = explode("-", $from);
72         $tno = explode("-", $to);
73         $from = min($fno[0], $tno[0]);
74         $to = max($fno[0], $tno[0]);
75
76         $cols = array(4, 85, 150, 225, 275, 360, 450, 515);
77
78         // $headers in doctext.inc
79         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right');
80
81         $params = array('comments' => $comments);
82
83         $cur = get_company_Pref('curr_default');
84
85         if ($email == 0)
86                 $rep = new FrontReport(_('REMITTANCE'), "RemittanceBulk", user_pagesize(), 9, $orientation);
87     if ($orientation == 'L')
88         recalculate_cols($cols);
89
90         for ($i = $from; $i <= $to; $i++)
91         {
92                 if ($fno[0] == $tno[0])
93                         $types = array($fno[1]);
94                 else
95                         $types = array(ST_BANKPAYMENT, ST_SUPPAYMENT, ST_SUPPCREDIT);
96                 foreach ($types as $j)
97                 {
98                         $myrow = get_remittance($j, $i);
99                         if (!$myrow)
100                                 continue;
101                         if ($currency != ALL_TEXT && $myrow['curr_code'] != $currency) {
102                                 continue;
103                         }
104                         $res = get_bank_trans($j, $i);
105                         $baccount = db_fetch($res);
106                         $params['bankaccount'] = $baccount['bank_act'];
107
108                         if ($email == 1)
109                         {
110                                 $rep = new FrontReport("", "", user_pagesize(), 9, $orientation);
111                                 $rep->title = _('REMITTANCE');
112                                 $rep->filename = "Remittance" . $i . ".pdf";
113                         }
114                         $rep->currency = $cur;
115                         $rep->Font();
116                         $rep->Info($params, $cols, null, $aligns);
117
118                         $contacts = get_supplier_contacts($myrow['supplier_id'], 'invoice');
119                         $rep->SetCommonData($myrow, null, $myrow, $baccount, ST_SUPPAYMENT, $contacts);
120                         $rep->SetHeaderType('Header2');
121                         $rep->NewPage();
122                         $result = get_allocatable_to_supp_transactions($myrow['supplier_id'], $myrow['trans_no'], $myrow['type']);
123
124                         $doctype = ST_SUPPAYMENT;
125
126                         $total_allocated = 0;
127                         $rep->TextCol(0, 4,     _("As advance / full / part / payment towards:"), -2);
128                         $rep->NewLine(2);
129
130                         while ($myrow2=db_fetch($result))
131                         {
132                                 $rep->TextCol(0, 1,     $systypes_array[$myrow2['type']], -2);
133                                 $rep->TextCol(1, 2,     $myrow2['supp_reference'], -2);
134                                 $rep->TextCol(2, 3,     sql2date($myrow2['tran_date']), -2);
135                                 $rep->TextCol(3, 4,     sql2date($myrow2['due_date']), -2);
136                                 $rep->AmountCol(4, 5, $myrow2['Total'], $dec, -2);
137                                 $rep->AmountCol(5, 6, $myrow2['Total'] - $myrow2['alloc'], $dec, -2);
138                                 $rep->AmountCol(6, 7, $myrow2['amt'], $dec, -2);
139
140                                 $total_allocated += $myrow2['amt'];
141                                 $rep->NewLine(1);
142                                 if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
143                                         $rep->NewPage();
144                         }
145
146                         $memo = get_comments_string($j, $i);
147                         if ($memo != "")
148                         {
149                                 $rep->NewLine();
150                                 $rep->TextColLines(1, 5, $memo, -2);
151                         }
152                         $rep->row = $rep->bottomMargin + (16 * $rep->lineHeight);
153
154                         $rep->TextCol(3, 6, _("Total Allocated"), -2);
155                         $rep->AmountCol(6, 7, $total_allocated, $dec, -2);
156                         $rep->NewLine();
157                         $rep->TextCol(3, 6, _("Left to Allocate"), -2);
158                         $myrow['Total'] *= -1;
159                         $myrow['ov_discount'] *= -1;
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
168                         $rep->NewLine();
169                         $rep->Font('bold');
170                         $rep->TextCol(3, 6, _("TOTAL REMITTANCE"), - 2);
171                         $rep->AmountCol(6, 7, $myrow['Total'], $dec, -2);
172
173                         $words = price_in_words($myrow['Total'], ST_SUPPAYMENT);
174                         if ($words != "")
175                         {
176                                 $rep->NewLine(2);
177                                 $rep->TextCol(1, 7, $myrow['curr_code'] . ": " . $words, - 2);
178                         }
179                         $rep->Font();
180                         if ($email == 1)
181                         {
182                                 $myrow['DebtorName'] = $myrow['supp_name'];
183                                 $rep->End($email);
184                         }
185                 }
186         }
187         if ($email == 0)
188                 $rep->End();
189 }
190