New inventory sales report. Shows total sales - costs - contribution per category...
[fa-stable.git] / reporting / rep304.php
1 <?php
2
3 $page_security = 2;
4 // ----------------------------------------------------------------
5 // $ Revision:  2.0 $
6 // Creator:     Joe Hunt
7 // date_:       2005-05-19
8 // Title:       Inventory Planning
9 // ----------------------------------------------------------------
10 $path_to_root="../";
11
12 include_once($path_to_root . "includes/session.inc");
13 include_once($path_to_root . "includes/date_functions.inc");
14 include_once($path_to_root . "includes/data_checks.inc");
15 include_once($path_to_root . "gl/includes/gl_db.inc");
16 include_once($path_to_root . "inventory/includes/db/items_category_db.inc");
17
18 //----------------------------------------------------------------------------------------------------
19
20 // trial_inquiry_controls();
21 print_inventory_sales();
22
23 function getTransactions($category, $location, $from, $to)
24 {
25         $from = date2sql($from);
26         $to = date2sql($to);
27         $sql = "SELECT ".TB_PREF."stock_master.category_id,
28                         ".TB_PREF."stock_category.description AS cat_description,
29                         ".TB_PREF."stock_master.stock_id,
30                         ".TB_PREF."stock_master.description,
31                         ".TB_PREF."stock_moves.loc_code,
32                         SUM(-".TB_PREF."stock_moves.qty*".TB_PREF."stock_moves.price*(1-".TB_PREF."stock_moves.discount_percent)) AS amt,
33                         SUM(-".TB_PREF."stock_moves.qty) *(".TB_PREF."stock_master.material_cost + ".TB_PREF."stock_master.labour_cost + ".TB_PREF."stock_master.overhead_cost) AS cost
34                 FROM ".TB_PREF."stock_master,
35                         ".TB_PREF."stock_category,
36                         ".TB_PREF."debtor_trans,
37                         ".TB_PREF."stock_moves
38                 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id
39                 AND ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
40
41                 AND ".TB_PREF."stock_moves.type=".TB_PREF."debtor_trans.type
42                 AND ".TB_PREF."stock_moves.trans_no=".TB_PREF."debtor_trans.trans_no
43                 AND ".TB_PREF."stock_moves.tran_date>='$from'
44                 AND ".TB_PREF."stock_moves.tran_date<='$to'
45                 AND ((".TB_PREF."debtor_trans.type=13 AND ".TB_PREF."debtor_trans.version=1) OR ".TB_PREF."stock_moves.type=11)
46                 AND (".TB_PREF."stock_master.mb_flag='B' OR ".TB_PREF."stock_master.mb_flag='M')";
47                 if ($category != 0)
48                         $sql .= " AND ".TB_PREF."stock_master.category_id = '$category'";
49                 if ($location != 'all')
50                         $sql .= " AND ".TB_PREF."stock_moves.loc_code = '$location'";
51                 //$sql .= " AND SUM(".TB_PREF."stock_moves.qty) != 0
52                 $sql .= " GROUP BY ".TB_PREF."stock_master.category_id,
53                         ".TB_PREF."stock_category.description,
54                         ".TB_PREF."stock_master.stock_id,
55                         ".TB_PREF."stock_master.description
56                 ORDER BY ".TB_PREF."stock_master.category_id,
57                         ".TB_PREF."stock_master.stock_id";
58
59     return db_query($sql,"No transactions were returned");
60
61 }
62
63 //----------------------------------------------------------------------------------------------------
64
65 function print_inventory_sales()
66 {
67     global $path_to_root;
68
69     include_once($path_to_root . "reporting/includes/pdf_report.inc");
70
71         $from = $_POST['PARAM_0'];
72         $to = $_POST['PARAM_1'];
73     $category = $_POST['PARAM_2'];
74     $location = $_POST['PARAM_3'];
75     $detail = $_POST['PARAM_4'];
76         $comments = $_POST['PARAM_5'];
77
78     $dec = user_price_dec();
79
80         if ($category == reserved_words::get_all_numeric())
81                 $category = 0;
82         if ($category == 0)
83                 $cat = _('All');
84         else
85                 $cat = get_category_name($category);
86
87         if ($location == reserved_words::get_all())
88                 $location = 'all';
89         if ($location == 'all')
90                 $loc = _('All');
91         else
92                 $loc = get_location_name($location);
93
94         $cols = array(0, 100, 250, 350, 450,    515);
95
96         $headers = array(_('Category'), '', _('Sales'), _('Cost'), _('Contribution'));
97
98         $aligns = array('left', 'left', 'right', 'right', 'right');
99
100     $params =   array(  0 => $comments,
101                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
102                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
103                                     3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
104
105     $rep = new FrontReport(_('Inventory Sales Report'), "InventorySalesReport.pdf", user_pagesize());
106
107     $rep->Font();
108     $rep->Info($params, $cols, $headers, $aligns);
109     $rep->Header();
110
111         $res = getTransactions($category, $location, $from, $to);
112         $total = $grandtotal = 0.0;
113         $total1 = $grandtotal1 = 0.0;
114         $total2 = $grandtotal2 = 0.0;
115         $catt = '';
116         while ($trans=db_fetch($res))
117         {
118                 if ($catt != $trans['cat_description'])
119                 {
120                         if ($catt != '')
121                         {
122                                 if ($detail)
123                                 {
124                                         $rep->NewLine(2, 3);
125                                         $rep->TextCol(0, 2, _('Total'));
126                                 }
127                                 $rep->TextCol(2, 3, number_format2($total, $dec));
128                                 $rep->TextCol(3, 4, number_format2($total1, $dec));
129                                 $rep->Textcol(4, 5, number_format2($total2, $dec));
130                                 if ($detail)
131                                 {
132                                         $rep->Line($rep->row - 2);
133                                         $rep->NewLine();
134                                 }
135                                 $rep->NewLine();
136                                 $total = $total1 = $total2 = 0.0;
137                         }
138                         $rep->TextCol(0, 1, $trans['category_id']);
139                         $rep->TextCol(1, 2, $trans['cat_description']);
140                         $catt = $trans['cat_description'];
141                         if ($detail)
142                                 $rep->NewLine();
143                 }
144                 $cb = $trans['amt'] - $trans['cost'];
145                 if ($detail)
146                 {
147                         $rep->NewLine();
148                         $rep->fontsize -= 2;
149                         $rep->TextCol(0, 1, $trans['stock_id']);
150                         $rep->TextCol(1, 2, $trans['description']);
151                         $rep->TextCol(2, 3, number_format2($trans['amt'], user_qty_dec()));
152                         $rep->TextCol(3, 4, number_format2($trans['cost'], $dec));
153                         $rep->TextCol(4, 5, number_format2($cb, $dec));
154                         $rep->fontsize += 2;
155                 }
156                 $total += $trans['amt'];
157                 $total1 += $trans['cost'];
158                 $total2 += $cb;
159                 $grandtotal += $trans['amt'];
160                 $grandtotal1 += $trans['cost'];
161                 $grandtotal2 += $cb;
162         }
163         if ($detail)
164         {
165                 $rep->NewLine(2, 3);
166                 $rep->TextCol(0, 2, _('Total'));
167         }
168         $rep->TextCol(2, 3, number_format2($total, $dec));
169         $rep->TextCol(3, 4, number_format2($total1, $dec));
170         $rep->Textcol(4, 5, number_format2($total2, $dec));
171         if ($detail)
172         {
173                 $rep->Line($rep->row - 2);
174                 $rep->NewLine();
175         }
176         $rep->NewLine(2, 1);
177         $rep->TextCol(0, 2, _('Grand Total'));
178         $rep->TextCol(2, 3, number_format2($grandtotal, $dec));
179         $rep->TextCol(3, 4, number_format2($grandtotal1, $dec));
180         $rep->Textcol(4, 5, number_format2($grandtotal2, $dec));
181
182         $rep->Line($rep->row  - 4);
183     $rep->End();
184 }
185
186 ?>