Currency filter not working in documents.
[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 ".TB_PREF."debtor_trans.*,
35                                 (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + 
36                                 ".TB_PREF."debtor_trans.ov_freight_tax) AS Total,
37                                 ".TB_PREF."debtor_trans.ov_discount,
38                                 ".TB_PREF."debtors_master.name AS DebtorName,  ".TB_PREF."debtors_master.debtor_ref,
39                                 ".TB_PREF."debtors_master.curr_code, ".TB_PREF."debtors_master.payment_terms, "
40                                 .TB_PREF."debtors_master.tax_id AS tax_id,
41                                 ".TB_PREF."debtors_master.address
42                         FROM ".TB_PREF."debtor_trans, ".TB_PREF."debtors_master
43                                 WHERE ".TB_PREF."debtor_trans.debtor_no = ".TB_PREF."debtors_master.debtor_no
44                                 AND ".TB_PREF."debtor_trans.type = ".db_escape($type)."
45                                 AND ".TB_PREF."debtor_trans.trans_no = ".db_escape($trans_no);
46         $result = db_query($sql, "The remittance cannot be retrieved");
47         if (db_num_rows($result) == 0)
48                 return false;
49     return db_fetch($result);
50 }
51
52 function get_allocations_for_receipt($debtor_id, $type, $trans_no)
53 {
54         $sql = get_alloc_trans_sql("amt, trans.reference, trans.alloc", "trans.trans_no = alloc.trans_no_to
55                 AND trans.type = alloc.trans_type_to
56                 AND alloc.trans_no_from=$trans_no
57                 AND alloc.trans_type_from=$type
58                 AND trans.debtor_no=".db_escape($debtor_id),
59                 TB_PREF."cust_allocations as alloc");
60         $sql .= " ORDER BY trans_no";
61         return db_query($sql, "Cannot retreive alloc to transactions");
62 }
63
64 function print_receipts()
65 {
66         global $path_to_root, $systypes_array;
67
68         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
69
70         $from = $_POST['PARAM_0'];
71         $to = $_POST['PARAM_1'];
72         $currency = $_POST['PARAM_2'];
73         $comments = $_POST['PARAM_3'];
74         $orientation = $_POST['PARAM_4'];
75
76         if (!$from || !$to) return;
77
78         $orientation = ($orientation ? 'L' : 'P');
79         $dec = user_price_dec();
80
81         $fno = explode("-", $from);
82         $tno = explode("-", $to);
83         $from = min($fno[0], $tno[0]);
84         $to = max($fno[0], $tno[0]);
85
86         $cols = array(4, 85, 150, 225, 275, 360, 450, 515);
87
88         // $headers in doctext.inc
89         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right');
90
91         $params = array('comments' => $comments);
92
93         $cur = get_company_Pref('curr_default');
94
95         $rep = new FrontReport(_('RECEIPT'), "ReceiptBulk", user_pagesize(), 9, $orientation);
96         if ($orientation == 'L')
97         recalculate_cols($cols);
98         $rep->SetHeaderType('Header2');
99         $rep->currency = $cur;
100         $rep->Font();
101         $rep->Info($params, $cols, null, $aligns);
102
103         for ($i = $from; $i <= $to; $i++)
104         {
105                 if ($fno[0] == $tno[0])
106                         $types = array($fno[1]);
107                 else
108                         $types = array(ST_BANKDEPOSIT, ST_CUSTPAYMENT);
109                 foreach ($types as $j)
110                 {
111                         $myrow = get_receipt($j, $i);
112                         if (!$myrow)
113                                 continue;                       
114                         if ($currency != ALL_TEXT && $myrow['curr_code'] != $currency) {
115                                 continue;
116                         }
117                         $res = get_bank_trans($j, $i);
118                         $baccount = db_fetch($res);
119                         $params['bankaccount'] = $baccount['bank_act'];
120
121                         $contacts = get_branch_contacts($myrow['branch_code'], 'invoice', $myrow['debtor_no']);
122                         $rep->SetCommonData($myrow, null, $myrow, $baccount, ST_CUSTPAYMENT, $contacts);
123                         $rep->NewPage();
124                         $result = get_allocations_for_receipt($myrow['debtor_no'], $myrow['type'], $myrow['trans_no']);
125
126                         $doctype = ST_CUSTPAYMENT;
127
128                         $total_allocated = 0;
129                         $rep->TextCol(0, 4,     _("As advance / full / part / payment towards:"), -2);
130                         $rep->NewLine(2);
131                         
132                         while ($myrow2=db_fetch($result))
133                         {
134                                 $rep->TextCol(0, 1,     $systypes_array[$myrow2['type']], -2);
135                                 $rep->TextCol(1, 2,     $myrow2['reference'], -2);
136                                 $rep->TextCol(2, 3,     sql2date($myrow2['tran_date']), -2);
137                                 $rep->TextCol(3, 4,     sql2date($myrow2['due_date']), -2);
138                                 $rep->AmountCol(4, 5, $myrow2['Total'], $dec, -2);
139                                 $rep->AmountCol(5, 6, $myrow2['Total'] - $myrow2['alloc'], $dec, -2);
140                                 $rep->AmountCol(6, 7, $myrow2['amt'], $dec, -2);
141
142                                 $total_allocated += $myrow2['amt'];
143                                 $rep->NewLine(1);
144                                 if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
145                                         $rep->NewPage();
146                         }
147
148                         $memo = get_comments_string($j, $i);
149                         if ($memo != "")
150                         {
151                                 $rep->NewLine();
152                                 $rep->TextColLines(1, 5, $memo, -2);
153                         }
154
155                         $rep->row = $rep->bottomMargin + (16 * $rep->lineHeight);
156
157                         $rep->TextCol(3, 6, _("Total Allocated"), -2);
158                         $rep->AmountCol(6, 7, $total_allocated, $dec, -2);
159                         $rep->NewLine();
160                         $rep->TextCol(3, 6, _("Left to Allocate"), -2);
161                         $rep->AmountCol(6, 7, $myrow['Total'] + $myrow['ov_discount'] - $total_allocated, $dec, -2);
162                         if (floatcmp($myrow['ov_discount'], 0))
163                         {
164                                 $rep->NewLine();
165                                 $rep->TextCol(3, 6, _("Discount"), - 2);
166                                 $rep->AmountCol(6, 7, -$myrow['ov_discount'], $dec, -2);
167                         }       
168                         $rep->NewLine();
169                         $rep->Font('bold');
170                         $rep->TextCol(3, 6, _("TOTAL RECEIPT"), - 2);
171                         $rep->AmountCol(6, 7, $myrow['Total'], $dec, -2);
172
173                         $words = price_in_words($myrow['Total'], ST_CUSTPAYMENT);
174                         if ($words != "")
175                         {
176                                 $rep->NewLine(1);
177                                 $rep->TextCol(0, 7, $myrow['curr_code'] . ": " . $words, - 2);
178                         }       
179                         $rep->Font();
180                         $rep->NewLine();
181                         $rep->TextCol(6, 7, _("Received / Sign"), - 2);
182                         $rep->NewLine();
183                         $rep->TextCol(0, 2, _("By Cash / Cheque* / Draft No."), - 2);
184                         $rep->TextCol(2, 4, "______________________________", - 2);
185                         $rep->TextCol(4, 5, _("Dated"), - 2);
186                         $rep->TextCol(5, 6, "__________________", - 2);
187                         $rep->NewLine(1);
188                         $rep->TextCol(0, 2, _("Drawn on Bank"), - 2);
189                         $rep->TextCol(2, 4, "______________________________", - 2);
190                         $rep->TextCol(4, 5, _("Branch"), - 2);
191                         $rep->TextCol(5, 6, "__________________", - 2);
192                         $rep->TextCol(6, 7, "__________________");
193                 }       
194         }
195         $rep->End();
196 }
197
198 ?>