1<?php
2
3
4include('includes/session.php');
5$Title = _('Daily Sales Inquiry');
6include('includes/header.php');
7
8echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/transactions.png" title="' . _('Daily Sales') . '" alt="" />' . ' ' . _('Daily Sales') . '</p>';
9echo '<div class="page_help_text">' . _('Select the month to show daily sales for') . '</div>
10	<br />';
11
12echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '" method="post">';
13echo '<div>';
14echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
15
16if (!isset($_POST['MonthToShow'])){
17	$_POST['MonthToShow'] = GetPeriod(Date($_SESSION['DefaultDateFormat']));
18	$Result = DB_query("SELECT lastdate_in_period FROM periods WHERE periodno='" . $_POST['MonthToShow'] . "'");
19	$myrow = DB_fetch_array($Result);
20	$EndDateSQL = $myrow['lastdate_in_period'];
21}
22
23echo '<table class="selection">
24	<tr>
25		<td>' . _('Month to Show') . ':</td>
26		<td><select tabindex="1" name="MonthToShow">';
27
28$PeriodsResult = DB_query("SELECT periodno, lastdate_in_period FROM periods");
29
30while ($PeriodRow = DB_fetch_array($PeriodsResult)){
31	if ($_POST['MonthToShow']==$PeriodRow['periodno']) {
32		echo '<option selected="selected" value="' . $PeriodRow['periodno'] . '">' . MonthAndYearFromSQLDate($PeriodRow['lastdate_in_period']) . '</option>';
33		$EndDateSQL = $PeriodRow['lastdate_in_period'];
34	} else {
35		echo '<option value="' . $PeriodRow['periodno'] . '">' . MonthAndYearFromSQLDate($PeriodRow['lastdate_in_period']) . '</option>';
36	}
37}
38echo '</select></td>
39	<td>' . _('Salesperson') . ':</td>';
40
41if($_SESSION['SalesmanLogin'] != '') {
42	echo '<td>';
43	echo $_SESSION['UsersRealName'];
44	echo '</td>';
45}else{
46	echo '<td><select tabindex="2" name="Salesperson">';
47
48	$SalespeopleResult = DB_query("SELECT salesmancode, salesmanname FROM salesman");
49	if (!isset($_POST['Salesperson'])){
50		$_POST['Salesperson'] = 'All';
51		echo '<option selected="selected" value="All">' . _('All') . '</option>';
52} else {
53	echo '<option value="All">' . _('All') . '</option>';
54}
55while ($SalespersonRow = DB_fetch_array($SalespeopleResult)){
56
57	if ($_POST['Salesperson']==$SalespersonRow['salesmancode']) {
58		echo '<option selected="selected" value="' . $SalespersonRow['salesmancode'] . '">' . $SalespersonRow['salesmanname'] . '</option>';
59	} else {
60		echo '<option value="' . $SalespersonRow['salesmancode'] . '">' . $SalespersonRow['salesmanname'] . '</option>';
61	}
62}
63echo '</select></td>';
64}
65echo '</tr>
66	</table>
67	<br />
68	<div class="centre">
69		<input tabindex="4" type="submit" name="ShowResults" value="' . _('Show Daily Sales For The Selected Month') . '" />
70	</div>
71    </div>
72	</form>
73	<br />';
74/*Now get and display the sales data returned */
75if (mb_strpos($EndDateSQL,'/')) {
76	$Date_Array = explode('/',$EndDateSQL);
77} elseif (mb_strpos ($EndDateSQL,'-')) {
78	$Date_Array = explode('-',$EndDateSQL);
79} elseif (mb_strpos ($EndDateSQL,'.')) {
80	$Date_Array = explode('.',$EndDateSQL);
81}
82
83if (mb_strlen($Date_Array[2])>4) {
84	$Date_Array[2]= mb_substr($Date_Array[2],0,2);
85}
86
87$StartDateSQL =  date('Y-m-d', mktime(0,0,0, (int)$Date_Array[1],1,(int)$Date_Array[0]));
88
89$sql = "SELECT 	trandate,
90				SUM(price*(1-discountpercent)* (-qty)) as salesvalue,
91				SUM(CASE WHEN mbflag='A' THEN 0 ELSE (standardcost * -qty) END) as cost
92			FROM stockmoves
93			INNER JOIN stockmaster
94			ON stockmoves.stockid=stockmaster.stockid
95			INNER JOIN custbranch
96			ON stockmoves.debtorno=custbranch.debtorno
97				AND stockmoves.branchcode=custbranch.branchcode
98			WHERE (stockmoves.type=10 or stockmoves.type=11)
99			AND trandate>='" . $StartDateSQL . "'
100			AND trandate<='" . $EndDateSQL . "'";
101
102if ($_SESSION['SalesmanLogin'] != '') {
103	$SQL .= " AND custbranch.salesman='" . $_SESSION['SalesmanLogin'] . "'";
104}elseif ($_POST['Salesperson']!='All') {
105	$sql .= " AND custbranch.salesman='" . $_POST['Salesperson'] . "'";
106}
107
108$sql .= " GROUP BY stockmoves.trandate ORDER BY stockmoves.trandate";
109$ErrMsg = _('The sales data could not be retrieved because') . ' - ' . DB_error_msg();
110$SalesResult = DB_query($sql,$ErrMsg);
111
112echo '<table class="selection">
113	<tr>
114		<th style="width: 14%">' . _('Sunday') . '</th>
115		<th style="width: 14%">' . _('Monday') . '</th>
116		<th style="width: 14%">' . _('Tuesday') . '</th>
117		<th style="width: 14%">' . _('Wednesday') . '</th>
118		<th style="width: 14%">' . _('Thursday') . '</th>
119		<th style="width: 14%">' . _('Friday') . '</th>
120		<th style="width: 14%">' . _('Saturday') . '</th>
121	</tr>';
122
123$CumulativeTotalSales = 0;
124$CumulativeTotalCost = 0;
125$BilledDays = 0;
126$DaySalesArray = array();
127while ($DaySalesRow=DB_fetch_array($SalesResult)) {
128
129	if ($DaySalesRow['salesvalue'] > 0) {
130		$DaySalesArray[DayOfMonthFromSQLDate($DaySalesRow['trandate'])]['Sales'] = $DaySalesRow['salesvalue'];
131	} else {
132		$DaySalesArray[DayOfMonthFromSQLDate($DaySalesRow['trandate'])]['Sales'] = 0;
133	}
134	if ($DaySalesRow['salesvalue'] > 0 ) {
135		$DaySalesArray[DayOfMonthFromSQLDate($DaySalesRow['trandate'])]['GPPercent'] = ($DaySalesRow['salesvalue']-$DaySalesRow['cost'])/$DaySalesRow['salesvalue'];
136	} else {
137		$DaySalesArray[DayOfMonthFromSQLDate($DaySalesRow['trandate'])]['GPPercent'] = 0;
138	}
139	$BilledDays++;
140	$CumulativeTotalSales += $DaySalesRow['salesvalue'];
141	$CumulativeTotalCost += $DaySalesRow['cost'];
142}
143//end of while loop
144echo '<tr>';
145$ColumnCounter = DayOfWeekFromSQLDate($StartDateSQL);
146for ($i=0;$i<$ColumnCounter;$i++){
147	echo '<td></td>';
148}
149$DayNumber = 1;
150/*Set up day number headings*/
151for ($i=$ColumnCounter;$i<=6;$i++){
152	   echo '<th>' . $DayNumber . '</th>';
153	   $DayNumber++;
154}
155echo '</tr><tr>';
156for ($i=0;$i<$ColumnCounter;$i++){
157	echo '<td></td>';
158}
159
160$LastDayOfMonth = DayOfMonthFromSQLDate($EndDateSQL);
161for ($i=1;$i<=$LastDayOfMonth;$i++){
162		$ColumnCounter++;
163		if(isset($DaySalesArray[$i])) {
164			echo '<td class="number" style="outline: 1px solid gray;">' . locale_number_format($DaySalesArray[$i]['Sales'],0) . '<br />' .  locale_number_format($DaySalesArray[$i]['GPPercent']*100,1) . '%</td>';
165		} else {
166			echo '<td class="number" style="outline: 1px solid gray;">' . locale_number_format(0,0) . '<br />' .  locale_number_format(0,1) . '%</td>';
167		}
168		if ($ColumnCounter==7){
169			echo '</tr><tr>';
170						for ($j=1;$j<=7;$j++){
171								   echo '<th>' . $DayNumber. '</th>';
172							$DayNumber++;
173							if($DayNumber>$LastDayOfMonth){
174								   break;
175							}
176						}
177						echo '</tr><tr>';
178			$ColumnCounter=0;
179		}
180
181
182}
183if ($ColumnCounter!=0) {
184	echo '</tr><tr>';
185}
186
187if ($CumulativeTotalSales !=0){
188	$AverageGPPercent = ($CumulativeTotalSales - $CumulativeTotalCost)*100/$CumulativeTotalSales;
189	$AverageDailySales = $CumulativeTotalSales/$BilledDays;
190} else {
191	$AverageGPPercent = 0;
192	$AverageDailySales = 0;
193}
194
195echo '<th colspan="7">' . _('Total Sales for month') . ': ' . locale_number_format($CumulativeTotalSales,0) . ' ' . _('GP%') . ': ' . locale_number_format($AverageGPPercent,1) . '% ' . _('Avg Daily Sales') . ': ' . locale_number_format($AverageDailySales,0) . '</th></tr>';
196
197echo '</table>';
198
199include('includes/footer.php');
200?>
201