Rerun. Reports checked for warnings.
[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:       Purchase Orders
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 + ".TB_PREF."debtor_trans.ov_discount) AS Total, 
37                                 ".TB_PREF."debtors_master.name AS DebtorName,  ".TB_PREF."debtors_master.debtor_ref,
38                                 ".TB_PREF."debtors_master.curr_code, ".TB_PREF."debtors_master.payment_terms, ".TB_PREF."debtors_master.tax_id AS tax_id, 
39                                 ".TB_PREF."debtors_master.email, ".TB_PREF."debtors_master.address
40                         FROM ".TB_PREF."debtor_trans, ".TB_PREF."debtors_master 
41                                 WHERE ".TB_PREF."debtor_trans.debtor_no = ".TB_PREF."debtors_master.debtor_no
42                                 AND ".TB_PREF."debtor_trans.type = ".db_escape($type)."
43                                 AND ".TB_PREF."debtor_trans.trans_no = ".db_escape($trans_no);
44         $result = db_query($sql, "The remittance cannot be retrieved");
45         if (db_num_rows($result) == 0)
46                 return false;
47     return db_fetch($result);
48 }
49
50 function get_allocations_for_receipt($debtor_id, $type, $trans_no)
51 {
52         $sql = get_alloc_trans_sql("amt, trans.reference, trans.alloc", "trans.trans_no = alloc.trans_no_to
53                 AND trans.type = alloc.trans_type_to
54                 AND alloc.trans_no_from=$trans_no
55                 AND alloc.trans_type_from=$type
56                 AND trans.debtor_no=".db_escape($debtor_id),
57                 TB_PREF."cust_allocations as alloc");
58         $sql .= " ORDER BY trans_no";
59         return db_query($sql, "Cannot retreive alloc to transactions");
60 }
61
62 function print_receipts()
63 {
64         global $path_to_root, $systypes_array;
65
66         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
67
68         $from = $_POST['PARAM_0'];
69         $to = $_POST['PARAM_1'];
70         $currency = $_POST['PARAM_2'];
71         $comments = $_POST['PARAM_3'];
72
73         if ($from == null)
74                 $from = 0;
75         if ($to == null)
76                 $to = 0;
77         $dec = user_price_dec();
78
79         $fno = explode("-", $from);
80         $tno = explode("-", $to);
81
82         $cols = array(4, 85, 150, 225, 275, 360, 450, 515);
83
84         // $headers in doctext.inc
85         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right');
86
87         $params = array('comments' => $comments);
88
89         $cur = get_company_Pref('curr_default');
90
91         $rep = new FrontReport(_('RECEIPT'), "ReceiptBulk", user_pagesize());
92         $rep->currency = $cur;
93         $rep->Font();
94         $rep->Info($params, $cols, null, $aligns);
95
96         for ($i = $fno[0]; $i <= $tno[0]; $i++)
97         {
98                 if ($fno[0] == $tno[0])
99                         $types = array($fno[1]);
100                 else
101                         $types = array(ST_BANKDEPOSIT, ST_CUSTPAYMENT, ST_CUSTCREDIT);
102                 foreach ($types as $j)
103                 {
104                         $myrow = get_receipt($j, $i);
105                         if (!$myrow)
106                                 continue;                       
107                         $baccount = get_default_bank_account($myrow['curr_code']);
108                         $params['bankaccount'] = $baccount['id'];
109
110                         $rep->title = _('RECEIPT');
111                         $rep->Header2($myrow, null, $myrow, $baccount, ST_CUSTPAYMENT);
112                         $result = get_allocations_for_receipt($myrow['debtor_no'], $myrow['type'], $myrow['trans_no']);
113
114                         $linetype = true;
115                         $doctype = ST_CUSTPAYMENT;
116                         if ($rep->currency != $myrow['curr_code'])
117                         {
118                                 include($path_to_root . "/reporting/includes/doctext2.inc");
119                         }
120                         else
121                         {
122                                 include($path_to_root . "/reporting/includes/doctext.inc");
123                         }
124
125                         $total_allocated = 0;
126                         $rep->TextCol(0, 4,     $doc_Towards, -2);
127                         $rep->NewLine(2);
128                         
129                         while ($myrow2=db_fetch($result))
130                         {
131                                 $rep->TextCol(0, 1,     $systypes_array[$myrow2['type']], -2);
132                                 $rep->TextCol(1, 2,     $myrow2['reference'], -2);
133                                 $rep->TextCol(2, 3,     sql2date($myrow2['tran_date']), -2);
134                                 $rep->TextCol(3, 4,     sql2date($myrow2['due_date']), -2);
135                                 $rep->AmountCol(4, 5, $myrow2['Total'], $dec, -2);
136                                 $rep->AmountCol(5, 6, $myrow2['Total'] - $myrow2['alloc'], $dec, -2);
137                                 $rep->AmountCol(6, 7, $myrow2['amt'], $dec, -2);
138
139                                 $total_allocated += $myrow2['amt'];
140                                 $rep->NewLine(1);
141                                 if ($rep->row < $rep->bottomMargin + (15 * $rep->lineHeight))
142                                         $rep->Header2($myrow, null, $myrow, $baccount, ST_CUSTPAYMENT);
143                         }
144
145                         $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
146
147                         $rep->TextCol(3, 6, $doc_Total_Allocated, -2);
148                         $rep->AmountCol(6, 7, $total_allocated, $dec, -2);
149                         $rep->NewLine();
150                         $rep->TextCol(3, 6, $doc_Left_To_Allocate, -2);
151                         $rep->AmountCol(6, 7, $myrow['Total'] - $total_allocated, $dec, -2);
152                         $rep->NewLine();
153                         $rep->Font('bold');
154                         $rep->TextCol(3, 6, $doc_Total_Payment, - 2);
155                         $rep->AmountCol(6, 7, $myrow['Total'], $dec, -2);
156                         $words = price_in_words($myrow['Total'], ST_CUSTPAYMENT);
157                         if ($words != "")
158                         {
159                                 $rep->NewLine(1);
160                                 $rep->TextCol(0, 7, $myrow['curr_code'] . ": " . $words, - 2);
161                         }       
162                         $rep->Font();
163                         $rep->NewLine();
164                         $rep->TextCol(6, 7, $doc_Received, - 2);
165                         $rep->NewLine();
166                         $rep->TextCol(0, 2, $doc_by_Cheque, - 2);
167                         $rep->TextCol(2, 4, "______________________________", - 2);
168                         $rep->TextCol(4, 5, $doc_Dated, - 2);
169                         $rep->TextCol(5, 6, "__________________", - 2);
170                         $rep->NewLine(1);
171                         $rep->TextCol(0, 2, $doc_Drawn, - 2);
172                         $rep->TextCol(2, 4, "______________________________", - 2);
173                         $rep->TextCol(4, 5, $doc_Drawn_Branch, - 2);
174                         $rep->TextCol(5, 6, "__________________", - 2);
175                         $rep->TextCol(6, 7, "__________________");
176                 }       
177         }
178         $rep->End();
179 }
180
181 ?>