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