-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurchases_sales.php
212 lines (203 loc) · 7.8 KB
/
purchases_sales.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
require_once 'includes/header.php';
$dateFrom = '';
$dateTo = '';
if (isset($_REQUEST['dateFrom']) && isset($_REQUEST['dateTo'])) {
$dateFrom = stripslashes($_POST['dateFrom']);
$dateTo = stripslashes($_POST['dateTo']);
} else { // Otherwise, set both to today!
$dateFrom = new DateTime();
$dateFrom = $dateFrom->format('Y-m-d');
$dateTo = new DateTime();
$dateTo = $dateTo->format('Y-m-d');
}
$transactions = $pheal->WalletTransactions(array('characterID' => $apiKeys['charId']))->toArray()['result']['transactions'];
$journals = $pheal->WalletJournal(array('characterID' => $apiKeys['charId'], 'rowCount' => '2560'))->toArray()['result']['transactions'];
$avgItemPrices = array();
$totalSold = 0.00;
$totalBought = 0.00;
foreach ($transactions as $trn) {
$transDate = substr($trn['transactionDateTime'], 0, 10);
if ((strtotime($transDate) >= strtotime($dateFrom)) && (strtotime($transDate) <= strtotime($dateTo))) {
if (!array_key_exists($trn['typeID'], $avgItemPrices)) {
$avgItemPrices[$trn['typeID']] = array(
'purchased' => array(
'items' => 0,
'totalPrice' => 0.00,
'typeID' => $trn['typeID'],
),
'sold' => array(
'items' => 0,
'totalPrice' => 0.00,
'typeID' => $trn['typeID'],
)
);
}
if ($trn['transactionType'] == 'buy') {
$avgItemPrices[$trn['typeID']]['purchased']['items'] += (int)$trn['quantity'];
$avgItemPrices[$trn['typeID']]['purchased']['totalPrice'] += ((double)$trn['price'] * (double)$trn['quantity']);
$totalBought += ((double)$trn['price'] * (double)$trn['quantity']);
} else { // It's a sell order
$avgItemPrices[$trn['typeID']]['sold']['items'] += (int)$trn['quantity'];
$avgItemPrices[$trn['typeID']]['sold']['totalPrice'] += ((double)$trn['price'] * (double)$trn['quantity']);
$totalSold += ((double)$trn['price'] * (double)$trn['quantity']);
}
}
}
$brokerFees = 0.00;
$transactionTaxes = 0.00;
$otherFees = 0.00;
foreach ($journals as $jr) {
$transDate = substr($jr['date'], 0, 10);
if ((strtotime($transDate) > strtotime($dateFrom)) && (strtotime($transDate) < strtotime($dateTo))) {
if ($jr['refTypeID'] == '54') {
$transactionTaxes = (float)$transactionTaxes + (float)$jr['amount'];
} elseif ($jr['refTypeID'] == '46'){
$brokerFees = (float)$brokerFees + (float)$jr['amount'];
} else {
$otherFees = (float)$otherFees + (float)$jr['amount'];
}
}
}
?>
<h1>PURCHASES / SALES</h1>
<h2>PERIOD OF TIME</h2>
<table>
<tr>
<td>From:</td>
<td>
<form action="<?=$_SERVER['PHP_SELF'] ?>" method="post">
<?php
$date4_default = $dateTo;
$date3_default = $dateFrom;
$myCalendar = new tc_calendar("dateFrom", true, false);
$myCalendar->setIcon("calendar/images/iconCalendar.gif");
$myCalendar->setDate(date('d', strtotime($date3_default))
, date('m', strtotime($date3_default))
, date('Y', strtotime($date3_default)));
$myCalendar->setPath("calendar/");
$myCalendar->setYearInterval(1970, 2020);
$myCalendar->setAlignment('left', 'bottom');
$myCalendar->setDatePair('date3', 'date4', $date4_default);
$myCalendar->writeScript();
?>
</td>
<td>To:</td>
<td>
<?php
$myCalendar = new tc_calendar("dateTo", true, false);
$myCalendar->setIcon("calendar/images/iconCalendar.gif");
$myCalendar->setDate(date('d', strtotime($date4_default))
, date('m', strtotime($date4_default))
, date('Y', strtotime($date4_default)));
$myCalendar->setPath("calendar/");
$myCalendar->setYearInterval(1970, 2020);
$myCalendar->setAlignment('left', 'bottom');
$myCalendar->setDatePair('date3', 'date4', $date3_default);
$myCalendar->writeScript();
?>
</td>
<td>
<input type="submit" value="GO" />
</td>
</form>
<table border="1" style="width: 100%">
<tr>
<td colspan="2">REVENUE</td>
<td colspan="2">EXPENDITURE</td>
</tr>
<tr>
<td><b>Sales</b></td>
<td style="color: green"><?=number_format($totalSold) ?> ISK</td>
<td><b>Purchases</b></td>
<td style="color: red">-<?=number_format($totalBought) ?> ISK</td>
</tr>
<tr>
<td colspan="4" style="text-align: right; color: <?php print (($totalSold - $totalBought) <= 0 ? 'red' : 'green') ?>">Summary: <?php print number_format($totalSold - $totalBought) ?> ISK</td>
</tr>
<tr>
<td colspan="2"></td>
<td><b>Broker's Fees</b></td>
<td style="color: red"><?=number_format($brokerFees) ?> ISK</td>
</tr>
<tr>
<td colspan="2"></td>
<td><b>Transaction Taxes</b></td>
<td style="color: red"><?=number_format($transactionTaxes) ?> ISK</td>
</tr>
<tr>
<td colspan="4" style="text-align: right; color: <?php print (($totalSold - $totalBought - $brokerFees - $transactionTaxes) <= 0 ? 'red' : 'green') ?>">Summary: <?php print number_format($totalSold - $totalBought - $brokerFees - $transactionTaxes) ?> ISK</td>
</tr>
</table>
<h2>TRANSACTIONS</h2><br />
<table border="1" style="width: 100%">
<thead>
<tr>
<th rowspan="2">Product Type</th>
<th colspan="2">Purchased</th>
<th colspan="2">Sold</th>
<th rowspan="2">Margin</th>
<th rowspan="2">Profit</th>
</tr>
<tr>
<th>Items</th>
<th>Average Price</th>
<th>Items</th>
<th>Average Price</th>
</tr>
</thead>
<tbody>
<?php foreach ($avgItemPrices as $item): ?>
<?php
$avgBought = $item['purchased']['items'] == 0 ? 0 : ($item['purchased']['totalPrice'] / $item['purchased']['items']);
$avgSold = $item['sold']['items'] == 0 ? 0 : ($item['sold']['totalPrice'] / $item['sold']['items']);
?>
<tr>
<td>
<?php
$stmt = $db->prepare('SELECT typeName FROM invTypes WHERE typeID = ?');
$stmt->bind_param('i', $item['purchased']['typeID']);
$stmt->execute();
$typeName = $stmt->get_result()->fetch_assoc()['typeName'];
print '<a href="product.php?typeID=' . $item['purchased']['typeID'] . '">';
print $typeName;
print '</a>';
?>
</td>
<td>
<?php
print number_format($item['purchased']['items'])
?>
</td>
<td style="color: red"><?=$item['purchased']['items'] == 0 ? 0 : number_format(sprintf('%0.2f', $avgBought)) ?> ISK</td>
<td>
<?php
print number_format($item['sold']['items'])
?>
</td>
<td style="color: green"><?=$item['sold']['items'] == 0 ? 0 : number_format((int)$avgSold) ?> ISK</td>
<td>
<?php
if ($avgSold == 0 && $avgBought == 0) {
print 0;
} elseif ($avgBought == 0 && $avgSold != 0) {
print 100 * $item['purchased']['items'];
} elseif ($avgSold == 0 && $avgBought != 0) {
$margin = 100*(($avgSold + 0.01) / $avgBought-1);
print (int)$margin;
}
else {
$margin = 100*($avgSold / $avgBought-1);
print (int)$margin;
}
1 ?>%
</td>
<td style="color: <?=($avgSold * $item['sold']['items']) - ($avgBought * $item['purchased']['items']) <= 0 ? 'red' : 'green' ?>">
<?php
print number_format(($avgSold * $item['sold']['items']) - ($avgBought * $item['purchased']['items']));
?> ISK
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>