-
Notifications
You must be signed in to change notification settings - Fork 1
/
bidAsset.php
executable file
·416 lines (352 loc) · 13.9 KB
/
bidAsset.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/*
* UWS - Universal Wealth System
* bidAsset.php
* GPL license
* author: Fabio Barone
* date: 30. Nov. 2009
*
* This file handles the purchase of a certain amount of an asset.
* It will display a form with some numbers, like the physical amount of
* that asset in the inventory, its value in inventory units, its
* price in service units (for the total amount of that asset), the maximum
* amount the current user can purchase (based on his own balance), this
* maximum amount in inventory units, and the current users saldo.
* There is a editable entry field displaying how much of the physical
* amount the user wants to purchase, per default filled with the maximum
* the user can buy. The last number displays the price in service units
* for the amount the user choses to purchase.
*
* When the user edits the amount field, he needs to click somewhere outside the
* entry field in order for the numbers to get updated. Javascript functions
* determine then the new price for the chosen amount.
*
* Currently in UWS when a member earned service units he has the right to
* access ("purchase") a correspondent amount of assets in inventory units,
* according to the UWS formula: KE * LG / IG = LE
*
* When the user clicks on the "Bid" button, doConsume.php is called, which
* processes the bid/purchase/access/... of the asset.
*/
session_start();
include "config.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
terrafirma1.0 by nodethirtythree design
http://www.nodethirtythree.com
-->
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Universal Wealth System UWS - <?php echo translate("uws:bid") ?> </title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link rel="stylesheet" type="text/css" href="default.css" />
<script type="text/javascript">
var last_factor = 0;
function validate_form() {
//validate the form on submit
var price = parseInt(document.forms['bid'].elements["price"].value);
var balance = parseInt(document.forms['bid'].elements["balance"].value);
var max_bid = parseInt(document.forms['bid'].elements["my_share_physical"].value);
var bid = parseInt(document.forms['bid'].elements["my_bid_amount"].value);
if (bid == 0 || price == 0 || isNaN(bid) || isNaN(price))
{
var invalid= "<?php echo translate('uws:invalid_value')?>";
alert(invalid);
return false;
}
if (price > balance){
var invalid= "<?php echo translate('uws:price_above_balance')?>";
alert(invalid);
return false;
}
if (bid > max_bid)
{
var bid_too_high= "<?php echo translate('uws:bid_too_high')?>";
alert(bid_too_high);
alert("bid/max_bid: " + bid + "/" + max_bid);
return false;
}
else {
//the following doesn't apply currently, but
//is left here if auctions should be incorporated later
//check here if the factor has been changed
//if not, buy directly, otherwise an auction starts
//this means that the bid must be proportional to
//the physical amount which is one's part
//(= the factor remains the same)
return true;
}
}
function update_fields() {
//the user changed the purchase amount
calc_fair_price();
}
function calc_factor() {
//This one is not needed currently, but left here
//in case auctions should become reality in the future
var same_factor_price_per_unit = document.forms['bid'].elements["same_factor_price_per_unit"].value;
var new_price = document.forms['bid'].elements["price"].value;
var bid_amount = document.forms['bid'].elements["my_bid_amount"].value;
var new_price_per_unit = bid_amount / new_price;
var new_factor = same_factor_price_per_unit / new_price_per_unit;
document.forms['bid'].elements["my_factor"].value = new_factor;
}
function calc_fair_price() {
//calculate the new price for a purchase.
//in UWS, currently, if a member has an amount of service units,
//he has the right to get ("purchase") assets in corresponce of
//its value in inventory units --> fair price
var bid = document.forms['bid'].elements["my_bid_amount"].value;
var total_cost = document.forms['bid'].elements["total_cost"].value;
var total_units = document.forms['bid'].elements["physical"].value;
var unit_price = total_cost / total_units;
var bid_price = bid * unit_price;
document.forms['bid'].elements["price"].value = bid_price;
}
</script>
</head>
<body>
<div id="outer">
<div id="upbg"></div>
<div id="inner">
<?php
include "header.php";
$member_id = $_SESSION['member_id'];
$asset_id = $_GET['unitID'];
$inventory = 0;
$balance = 0;
$myshare = 0;
$my_share_price = 0;
//TODO: if unitID not set, first offer to choose unit
$sql = "SELECT asset,inventory,physical,last_factor from assetlist where asset_id='" . $asset_id . "'";
$query = mysql_query($sql);
if (!query)
{
die("Error: Query failed. ".mysql_error());
}
//Get the data about the asset the user wants to "buy" from
while ($result = mysql_fetch_array($query))
{
$inventory = $result['inventory'];
$unit = $result['asset'];
$physical = $result['physical'];
$factor = $result['last_factor'];
}
//get the users balance
$sql = "SELECT balance from members where member_id='" . $member_id . "'";
$query = mysql_query($sql);
while ($result = mysql_fetch_array($query))
{
$balance = $result['balance'];
}
//get the totals from the database, needed for price calculation
$sql = "SELECT total_services from totals";
$query = mysql_query($sql);
$total_services = mysql_fetch_row($query);
$sql = "SELECT total_inventory from totals";
$query = mysql_query($sql);
$total_inventory = mysql_fetch_row($query);
//the UWS formula!
//calculate how much service units are needed to access all of the inventory
//of that particular asset
$total_cost = $inventory * $total_services[0] / $total_inventory[0];
//what's the ration in relation to the balance
$ratio = $total_cost / $balance;
//with that ration, calculate the user's share, his right to access,
//on the physical amount of that particular asset
$my_share_physical = $physical / $ratio;
//the same share in inventory units
$my_share_inventory = $inventory / $ratio;
//My share can't be bigger than the total amount
if ($my_share_physical > $physical)
{
$my_share_physical = $physical;
$my_share_inventory = $inventory;
}
//Calculate the price in service units
$price = ($total_cost / $inventory) * $my_share_inventory;
//price per unit if the factor is the same, used for auctions, but
//disabled resp. not needed for now
$same_factor_price_per_unit = $physical / $total_cost;
?>
<h3><?php echo translate("uws:bid_asset") . ": " . $unit ?></h3>
<div class="date">
<?php
echo date('d F Y') ?></div>
</div>
<div class="content">
<form name="bid" id="story" action="doConsume.php" method="post" enctype="multipart/form-data">
<table class="formtable" width="470" cellspacing="0" cellpadding="0">
<tr>
<td width="150" class="text"><?php echo translate("uws:inventory")?>:</td>
<td width="10"> </td>
<td><span class="text">
<input name="inventory" type="text" id="inventory" value="<?php echo $inventory?>" size="40" readonly/>
</span>
</td>
</tr>
<tr>
<td width="150" height="5"></td>
<td height="5"></td>
<td></td>
</tr>
<tr>
<td width="150" class="text"><?php echo translate("uws:physical")?>:</td>
<td width="5"> </td>
<td><span class="text"><input name="physical" type="text" id="physical" value="<?php echo $physical ?>" size="40" readonly/>
</span></td>
</tr>
<tr>
<td width="150" height="5"></td>
<td height="5"></td>
<td></td>
</tr>
<!--
<tr>
<td width="150" class="text"><?php echo translate("uws:factor") ?></td>
<td width="10"> </td>
<td><span class="text"><input name="factor" type="text" id="factor" value="<?php echo $factor ?>" size="40" readonly/>
</span></td>
</tr>
<tr>
<td width="150" height="5"></td>
<td height="5"></td>
<td></td>
</tr>
-->
<tr>
<td width="150" class="text"><?php echo translate("uws:total_cost") ?></td>
<td width="10"> </td>
<td><span class="text"><input name="total_cost" type="text" id="total_cost" value="<?php echo $total_cost ?>" size="40" readonly/>
</span></td>
</tr>
<tr>
<td width="150" height="5"></td>
<td height="5"></td>
<td></td>
</tr>
<tr>
<td width="150" class="text"><?php echo translate("uws:my_share_physical") ?></td>
<td width="10"> </td>
<td><span class="text"><input name="my_share_physical" type="text" id="my_share_physical" value="<?php echo $my_share_physical ?>" size="40" readonly/>
</span></td>
</tr>
<tr>
<td width="150" height="5"></td>
<td height="5"></td>
<td></td>
</tr>
<tr>
<td width="150" class="text"><?php echo translate("uws:my_share_inventory") ?></td>
<td width="10"> </td>
<td><span class="text"><input name="my_share_inventory" type="text" id="my_share_inventory" value="<?php echo $my_share_inventory ?>" size="40" readonly/>
</span></td>
</tr>
<tr>
<td width="150" height="5"></td>
<td height="5"></td>
<td></td>
</tr>
<tr bgcolor=" #666666">
<td width="150" height="1"></td>
<td height="5"></td>
<td></td>
</tr>
<tr>
<td width="150" height="5"></td>
<td height="5"></td>
<td></td>
</tr>
<tr>
<td width="150" class="text"><?php echo translate("uws:balance") ?>:</td>
<td width="10"> </td>
<td><span class="text"><input name="balance" type="text" id="balance" value="<?php echo $balance?>" size="40" readonly/>
</span></td>
</tr>
<tr>
<td width="150" height="5"></td>
<td height="5"></td>
<td></td>
</tr>
<tr>
<td width="150" class="text"><?php echo translate("uws:my_bid_amount") ?></td>
<td width="10"> </td>
<td>
<span class="text">
<input name="my_bid_amount" type="text" id="my_bid_amount" value="<?php echo $my_share_physical ?>" onchange=update_fields() />
</span>
</td>
</tr>
<tr>
<td width="150" height="5"></td>
<td height="5"></td>
<td></td>
</tr>
<tr>
<td width="150" class="text"><?php echo translate("uws:price") ?></td>
<td width="10"> </td>
<td><span class="text"><input name="price" type="text" id="price" value="<?php echo $price ?>" size="40" readonly /> <!--onchange=calc_factor() -->
</span></td>
</tr>
<tr>
<td width="150" height="5"></td>
<td height="5"></td>
<td></td>
</tr>
<!--
<tr>
<td width="150" class="text"><?php echo translate("uws:my_factor") ?></td>
<td width="10"> </td>
<td><span class="text"><input name="my_factor" type="text" id="my_factor" value="<?php echo $factor ?>" size="40" readonly/>
</span></td>
</tr>
<tr>
<td width="150" height="5"></td>
<td height="5"></td>
<td></td>
</tr>
-->
<tr>
<td colspan="3"><div align="left" class="text">
<input type="submit" name="place_bid" id="place_bid" value="<?php echo translate("uws:submit_bid") ?>" onclick="return validate_form()" />
<br />
<br />
<!--
<input type="button" name="button2" id="button2" value="<?php echo translate("uws:save_record") ?>" onclick="javascript:saveStory()" />
-->
<br />
<br />
</div></td>
</tr>
</table>
<input type="hidden" name="asset_id" id="asset_id" value="<?php echo $asset_id ?>" />
<input type="hidden" name="unit" id="unit" value="<?php echo $unit ?>" />
<input type="hidden" name="old_factor" id="unit" value="<?php echo $factor ?>" />
<input type="hidden" name="same_factor_price_per_unit" id="same_factor_price_per_unit" value="<?php echo $same_factor_price_per_unit ?>" />
</form>
</div>
<div class="footer">
<ul>
<li class="printerfriendly"><a href="#">Printer Friendly</a></li>
<li class="readmore"><a href="#">Read more</a></li>
</ul>
</div>
</div>
</div>
<div id="secondarycontent">
<!-- Displaying lists links -->
<?php include "lists.php" ?>
<!-- Displaying action links -->
<?php include "actions.php" ?>
<!-- secondary content end -->
</div>
<div id="footer">
© UWS. </a>.
</div>
</div>
</div>
</body>
</html>