Merged changes from main trunk up to 2.2.3
[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 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_remittances();
30
31 //----------------------------------------------------------------------------------------------------
32 function get_remittance($type, $trans_no)
33 {
34         $sql = "SELECT ".TB_PREF."supp_trans.*, 
35                 (".TB_PREF."supp_trans.ov_amount+".TB_PREF."supp_trans.ov_gst+".TB_PREF."supp_trans.ov_discount) AS Total, 
36                 ".TB_PREF."suppliers.supp_name,  ".TB_PREF."suppliers.supp_account_no,
37                 ".TB_PREF."suppliers.curr_code, ".TB_PREF."suppliers.payment_terms, ".TB_PREF."suppliers.gst_no AS tax_id, 
38                 ".TB_PREF."suppliers.email, ".TB_PREF."suppliers.address, ".TB_PREF."suppliers.contact
39                 FROM ".TB_PREF."supp_trans, ".TB_PREF."suppliers
40                 WHERE ".TB_PREF."supp_trans.supplier_id = ".TB_PREF."suppliers.supplier_id
41                 AND ".TB_PREF."supp_trans.type = ".db_escape($type)."
42                 AND ".TB_PREF."supp_trans.trans_no = ".db_escape($trans_no);
43         $result = db_query($sql, "The remittance cannot be retrieved");
44         if (db_num_rows($result) == 0)
45                 return false;
46     return db_fetch($result);
47 }
48
49 function get_allocations_for_remittance($supplier_id, $type, $trans_no)
50 {
51         $sql = get_alloc_supp_sql("amt, supp_reference, trans.alloc", "trans.trans_no = alloc.trans_no_to
52                 AND trans.type = alloc.trans_type_to
53                 AND alloc.trans_no_from=".db_escape($trans_no)."
54                 AND alloc.trans_type_from=".db_escape($type)."
55                 AND trans.supplier_id=".db_escape($supplier_id),
56                 TB_PREF."supp_allocations as alloc");
57         $sql .= " ORDER BY trans_no";
58         return db_query($sql, "Cannot retreive alloc to transactions");
59 }
60
61 function print_remittances()
62 {
63         global $path_to_root, $systypes_array;
64
65         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
66
67         $from = $_POST['PARAM_0'];
68         $to = $_POST['PARAM_1'];
69         $currency = $_POST['PARAM_2'];
70         $email = $_POST['PARAM_3'];
71         $comments = $_POST['PARAM_4'];
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         if ($email == 0)
92         {
93                 $rep = new FrontReport(_('PURCHASE ORDER'), "PurchaseOrderBulk", user_pagesize());
94                 $rep->currency = $cur;
95                 $rep->Font();
96                 $rep->Info($params, $cols, null, $aligns);
97         }
98
99         for ($i = $fno[0]; $i <= $tno[0]; $i++)
100         {
101                 if ($fno[0] == $tno[0])
102                         $types = array($fno[1]);
103                 else
104                         $types = array(ST_BANKPAYMENT, ST_SUPPAYMENT, ST_SUPPCREDIT);
105                 foreach ($types as $j)
106                 //for ($j = ST_BANKPAYMENT; $j <= ST_SUPPAYMENT; $j = ($j == ST_BANKPAYMENT ? ST_SUPPAYMENT : 999))
107                 //for ($j = $ft; $j <= $tt; $j = ($j == ST_BANKPAYMENT ? ST_SUPPAYMENT : 999))
108                 {
109                         $myrow = get_remittance($j, $i);
110                         if (!$myrow)
111                                 continue;                       
112                         $baccount = get_default_bank_account($myrow['curr_code']);
113                         $params['bankaccount'] = $baccount['id'];
114
115                         if ($email == 1)
116                         {
117                                 $rep = new FrontReport("", "", user_pagesize());
118                                 $rep->currency = $cur;
119                                 $rep->Font();
120                                 $rep->title = _('REMITTANCE');
121                                 $rep->filename = "Remittance" . $i . ".pdf";
122                                 $rep->Info($params, $cols, null, $aligns);
123                         }
124                         else
125                                 $rep->title = _('REMITTANCE');
126                         $rep->Header2($myrow, null, $myrow, $baccount, ST_SUPPAYMENT);
127                         $result = get_allocations_for_remittance($myrow['supplier_id'], $myrow['type'], $myrow['trans_no']);
128
129                         $total_allocated = 0;
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->Header2($myrow, null, $myrow, $baccount, ST_SUPPAYMENT);
144                         }
145
146                         $rep->row = $rep->bottomMargin + (15 * $rep->lineHeight);
147                         $linetype = true;
148                         $doctype = ST_SUPPAYMENT;
149                         if ($rep->currency != $myrow['curr_code'])
150                         {
151                                 include($path_to_root . "/reporting/includes/doctext2.inc");
152                         }
153                         else
154                         {
155                                 include($path_to_root . "/reporting/includes/doctext.inc");
156                         }
157
158                         $rep->TextCol(3, 6, $doc_Total_Allocated, -2);
159                         $rep->AmountCol(6, 7, $total_allocated, $dec, -2);
160                         $rep->NewLine();
161                         $rep->TextCol(3, 6, $doc_Left_To_Allocate, -2);
162                         $myrow['Total'] *= -1;
163                         $rep->AmountCol(6, 7, $myrow['Total'] - $total_allocated, $dec, -2);
164                         $rep->NewLine();
165                         $rep->Font('bold');
166                         $rep->TextCol(3, 6, $doc_Total_Payment, - 2);
167                         $rep->AmountCol(6, 7, $myrow['Total'], $dec, -2);
168                         $rep->Font();
169                         if ($email == 1)
170                         {
171                                 $myrow['contact_email'] = $myrow['email'];
172                                 $myrow['DebtorName'] = $myrow['supp_name'];
173                                 if ($myrow['contact'] != '') $myrow['DebtorName'] = $myrow['contact'];
174                                 $rep->End($email, $doc_Order_no . " " . $myrow['reference'], $myrow);
175                         }
176                 }       
177         }
178         if ($email == 0)
179                 $rep->End();
180 }
181
182 ?>