/[drupal]/contributions/modules/carbon/carbon_transfer.inc
ViewVC logotype

Contents of /contributions/modules/carbon/carbon_transfer.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.10 - (show annotations) (download) (as text)
Tue May 5 13:20:32 2009 UTC (6 months, 3 weeks ago) by johnackers
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, HEAD
Changes since 1.9: +4 -4 lines
File MIME type: text/x-php
pick up units from carbon settings
1 <?php
2 /**
3 * $Id$
4 *
5 * Drupal carbon module.
6 *
7 * Carbon transfers (purchases and sales of carbon) are recorded in
8 * carbon_transfers nodes. The functions in this file manage the
9 * database operations and reporting functions.
10 *
11 * Intended for (but not exclusively) CRAG use.
12 *
13 * Created on Feb 2008.
14 *
15 */
16
17
18 /**
19 * Implementation of hook_access().
20 *
21 * Delegate carbon_access to the carbon module because it
22 * is easier to check access for all carbon types in one place.
23 */
24
25 function carbon_transfer_access($op, $node)
26 {
27 return carbon_access($op, $node);
28 }
29
30
31 /**
32 * Render a page listing a one line summary of all carbon transfers
33 */
34
35
36 function carbon_transfer_page($account)
37 {
38 global $user;
39
40 require_once(CARBON_PATH ."/carbon_report.inc"); // has CarbonAccountMap
41
42 $aid = empty($account) ? null : $account->nid ;
43
44
45 $account_map = new CarbonAccountMap() ; $account_map->init();
46
47 // the nid's below correspond to carbon accounts
48
49 $sql = "SELECT t.*, n.*, u.name, u.uid " .
50 "FROM {carbon_transfer} t " .
51 "LEFT JOIN {node} n ON t.nid = n.nid " .
52 "INNER JOIN {users} u on n.uid = u.uid " ;
53
54 if (empty($aid))
55 {
56 $header = carbon_transfer_admin_header();
57 }
58 else {
59 $header = carbon_transfer_header();
60 $sql .= " WHERE t.seller=%d OR t.buyer=%d ";
61 }
62
63 $sql .= tablesort_sql($header);
64
65 // get the links (no range limit here)
66 $queryResult = db_query($sql, $aid, $aid);
67
68 $transfers = array();
69 while ($node = db_fetch_object($queryResult))
70 {
71 $node->protection = _log2($node->protect2);
72 $ro_access = carbon_access("view", $node, $node->uid);
73 if ($ro_access)
74 {
75 $transfers[] = $node ;
76 }
77 }
78 $output .= _create_transfer_table($account, "transfer", $header, $transfers, $singleRow = false, $account_map);
79
80
81 $format = $_GET["datatype"];
82 if ($format == 'json')
83 {
84 $json = drupal_json($output);
85 print($json);
86 exit(0);
87 }
88
89 if (empty($aid))
90 {
91 $title = t("All Carbon Purchases");
92 }
93 else
94 {
95 $title = t("Cash Movements for ". $account_map->getTitle($aid));
96 }
97
98 drupal_set_title(check_plain($title));
99 return $output;
100
101 }
102
103
104 function _create_transfer_table($account, $pagename, $header, $transfers, $singleRow, $account_map)
105 {
106 $rows = array() ; // updated when form created
107 $balance = array();
108
109 foreach ($transfers as $transfer)
110 {
111 // we have to format each cell because there may be embedded links.
112
113 $cols = _format_transfer_cells($header, $transfer, $account, $class = $pagename, $account_map, $balance);
114 $rows[] = array('data' => $cols, 'id'=> $transfer->nid, 'class'=> empty($singleRow) ? 'editable' : $singleRow) ;
115 $last = $transfer ;
116 }
117
118 if (empty($singleRow))
119 {
120 $cols = array(); $width = count($header);
121 for ($i = 0 ; $i < $width - 1 ; $i++)
122 $cols[] = "" ;
123 // $cols[] = _format_link_insitu_add($account, $pagename, $last);
124 $rows[] = array("data" => $cols, 'id'=> 0, 'class'=>'inSituEdit', 'class'=>'addable');
125 }
126 $output = theme("table_inside_form", $singleRow ? array() : $header, $rows);
127
128 return $output ;
129 }
130
131
132 function _format_transfer_cells($header, $transfer, $account, $class, $account_map, &$balance )
133 {
134 $cols = array();
135 foreach ($header as $h)
136 {
137 switch($f = $h['field'])
138 {
139 case 'valuedate' :$cols[] = _format_date($transfer->valuedate); break ;
140 case 'title' : $cols[] = carbon_link_from_title($transfer, $account, ""); break ;
141
142 case 'buyer' : $cols[] = $account_map->getTransferLink($transfer->buyer); break ;
143 case 'seller' : $cols[] = $account_map->getTransferLink($transfer->seller); break ;
144 case 'counterparty' :
145
146 if ($transfer->seller == $account->nid)
147 {
148 $cols[] = $account_map->getTransferLink($transfer->buyer); break ;
149 }
150 else
151 {
152 $cols[] = $account_map->getTransferLink($transfer->seller); break ;
153 }
154
155 case 'carbon' : $cols[] = $transfer->carbon ; break ;
156 case 'carbon-balance' :
157 if ($transfer->seller == $account->nid)
158 {
159 $cols[] = $balance['carbon'] -= $transfer->carbon ; break ;
160 }
161 else
162 {
163 $cols[] = $balance['carbon'] += $transfer->carbon ; break ;
164 }
165
166 case 'cost' : $cols[] = $transfer->cost ; break ;
167 case 'cost-balance' :
168 if ($transfer->seller == $account->nid)
169 {
170 $cols[] = $balance['cost'] += $transfer->cost ; break ;
171 }
172 else
173 {
174 $cols[] = $balance['cost'] -= $transfer->cost ; break ;
175 }
176
177 case 'status' : $cols[] = ($transfer->status >= 1 ? 'pub ' : ' '); break ;
178 case 'operations':$cols[] = _format_link_insitu_edit($account, $class, $transfer); break ;
179 default : $cols[] = $f." ?" ; break ;
180 }
181 }
182 return $cols ;
183 }
184
185
186 function carbon_transfer_header()
187 {
188 global $carbon_transfer_header ;
189 if (!isset($carbon_transfer_header))
190 {
191 $carbon_transfer_header = array(
192 array("data" => t("date"), "field" => "valuedate"),
193 array("data" => t("description"), "field" => "title"),
194 array("data" => t("counterparty"), "field" => "counterparty"),
195
196 array("data" => variable_get("carbon_units", "Kg of C02"), "field" => "carbon"),
197 array("data" => t("CO2 balance"), "field" => "carbon-balance"),
198
199 array("data" => variable_get("carbon_money_units", "$"), "field" => "cost"),
200 array("data" => t("balance"), "field" => "cost-balance")
201 );
202 }
203 return $carbon_transfer_header ;
204 }
205
206
207
208 function carbon_transfer_admin_header()
209 {
210 global $carbon_transfer_admin_header ;
211 if (!isset($carbon_transfer_admin_header))
212 {
213 $carbon_transfer_admin_header = array(
214 array("data" => t("date"), "field" => "valuedate"),
215 array("data" => t("description"), "field" => "title"),
216 array("data" => t("seller"), "field" => "seller"),
217 array("data" => t("buyer"), "field" => "buyer"),
218 array("data" => t("CO2"), "field" => "carbon"),
219 array("data" => t("cost"), "field" => "cost")
220 );
221 }
222 return $carbon_transfer_admin_header ;
223 }
224
225 /**
226 *
227 * Joins the recorded stamps in a carbon account with all the available carbon
228 * sources.
229 *
230 * Note that some source fields have been renamed e.g. sourceid, sourceclass, sourcetitle
231 *
232 *@param $nid select stamps attached to this footprint id, can be null
233 *@param $uid select stamps with this uid, can be null
234 *@param $org select stamps with this org, can be null
235 *@return an array of carbon stamps (and an array of carbon sources)
236 */
237
238
239 function carbon_transfer_array_load($nid, $uid, $org)
240 {
241 $query ="SELECT ntransfer.title, ntransfer.status, ntransfer.uid, ntransfer.type, " .
242 "transfer.* AS shared_uid " .
243 "FROM {carbon_transfer} transfer " .
244 "INNER JOIN {node} ntransfer ON transfer.nid = ntransfer.nid ".
245 "WHERE ntransfer.status " ; // transfer must be published
246
247 // "AND (j.fid = %d " . (empty($nid) ? " OR 1) " : ") ").
248 // "AND (ntransfer.uid = %d " . (empty($uid) ? " OR 1) " : ") ").
249 // "AND (account.organization = '%s' " . (empty($org) ? " OR 1) " : ") "). "" ;
250
251 if (!empty($nid))
252 {
253 $query .= "AND (transfer.seller=%d OR transfer.buyer=%d) ";
254 }
255 $queryResult = db_query($query, $nid, $nid); // needs fixing
256 // $query .= tablesort_sql(carbon_transfer_header());
257
258 $transfers = array();
259 $num = 0 ;
260 while ($record = db_fetch_object($queryResult))
261 {
262 $transfers[] = $record ;
263 // echo $record->title . " bb <br/>" . $query;
264 }
265 return array("transfer" => $transfers);
266 }
267
268
269 function carbon_transfer_form(&$node, $form_state) {
270 global $user ;
271
272 require_once(CARBON_PATH ."/carbon_report.inc"); // has CarbonAccountMap
273
274 $account_map = new CarbonAccountMap(); $account_map->init();
275 $form = array();
276
277 $form["title"] = array(
278 "#type" => "textfield",
279 "#title" => "description",
280 "#required" => true,
281 "#default_value" => $node->title,
282 "#size" => 10,
283 "#maxlength" => 40,
284 "#weight" => -8,
285 "#description" => "purpose or reason for transfer");
286
287
288 $form["seller_index"] = array(
289 "#type" => "select",
290 "#options" => $account_map->getArray(),
291 "#title" => "seller",
292 "#required" => true,
293 "#default_value" => $account_map->getArrayIndex($node->seller),
294 "#size" => 10,
295 "#maxlength" => 4,
296 "#weight" => -7,
297 "#description" => "carbon account to debit");
298
299 $form["buyer_index"] = array(
300 "#type" => "select",
301 "#options" => $account_map->getArray(),
302 "#title" => "buyer",
303 "#required" => true,
304 "#default_value" => $account_map->getArrayIndex($node->buyer),
305 "#size" => 10,
306 "#maxlength" => 4,
307 "#weight" => -6,
308 "#description" => "carbon account to credit");
309
310 $form["carbon"] = array(
311 "#type" => "textfield",
312 "#title" => "carbon",
313 "#required" => true,
314 "#default_value" => $node->carbon,
315 "#size" => 20,
316 "#maxlength" => 20,
317 "#weight" => -5,
318 "#description" => "amount of carbon credit"); // don't specify units'
319
320 $form["cost"] = array(
321 "#type" => "textfield",
322 "#title" => "cost",
323 "#required" => true,
324 "#default_value" => $node->cost,
325 "#size" => 20,
326 "#maxlength" => 20,
327 "#weight" => -4,
328 "#description" => "monetary value of transaction");
329
330 // $firstdate = mktime(0, 0, 0, substr($params["firstdate"],4,2), substr($params["firstdate"],6,2), substr($params["firstdate"],0,4));
331
332 $form["valuedate_s"] = array(
333 "#type" => "textfield",
334 "#title" => "value date",
335 "#required" => true,
336 "#default_value" => _format_date($node->valuedate == 0 ? CarbonDate::today() : $node->valuedate),
337 "#size" => 20,
338 "#maxlength" => 20,
339 "#weight" => -3,
340 "#description" => t("Effective date of transfer, format: %time", array("%time" => _format_date_only(time())))
341 );
342 $form['#submit'] = array('carbon_transfer_submit');
343
344 return $form ;
345 }
346
347
348
349 /**
350 * Validate our forms
351 * @param string form id
352 * @param array form values
353 */
354 function carbon_transfer_validate($form_id, $form_values)
355 {
356 /*
357 for ($i = 0 ; $i < 500 ; $i++)
358 {
359 _validate_adjustment($form_id, "ff". $i .".adjustment");
360 _validate_date($form_id, "ff". $i .".startdate_s");
361 _validate_date($form_id, "ff". $i .".enddate_s");
362 // _validate_date($form_id, "ff". $i .".firstdate");
363 }
364 */
365 }
366
367
368 /**
369 * Implementation of hook_load
370 */
371
372
373 function carbon_transfer_load(&$node) {
374
375 $query = db_query(
376 "SELECT t.* " .
377 "FROM {carbon_transfer} t " .
378 "LEFT JOIN {carbon_stamp_account} j ON t.nid = j.sid " .
379 "LEFT JOIN {node} n ON j.fid = n.nid " . // to get footprint title
380 "WHERE t.nid = %d " .
381 " ", $node->nid);
382 $first_record = null ;
383
384 while ($first = db_fetch_object($query))
385 {
386 return $first ;
387 }
388 drupal_set_message("Failed to load carbon_transfer where nid=", $node->nid);
389 }
390
391
392 function carbon_transfer_submit(&$form, &$form_state)
393 {
394 require_once(CARBON_PATH ."/carbon_report.inc"); // has CarbonAccountMap
395
396 $account_map = new CarbonAccountMap(); $account_map->init();
397
398 $transfer = &$form_state['values'];
399
400 $transfer['seller'] = $account_map->getNid($transfer['seller_index']) ;
401 $transfer['buyer'] = $account_map->getNid($transfer['buyer_index']) ;
402
403 $s = &$transfer['valuedate_s'] ;
404 $transfer['valuedate'] = mktime(0, 0, 0, substr($s,4,2), // should use drupal routines
405 substr($s,6,2),
406 substr($s,0,4));
407
408 //drupal_set_message("ghi " . $node->seller . " ... " . $node->buyer_index) ;
409 }
410
411 /**
412 * Implementation of hook_insert, which saves carbon-specific information
413 * into the carbon table
414 * @param node object
415 */
416
417 function carbon_transfer_insert(&$node)
418 {
419 db_query("INSERT INTO {carbon_transfer} ".
420 "(nid, seller, buyer, carbon, cost, valuedate) " .
421 "VALUES (%d, %d, %d, %f, %f, %d)",
422 $node->nid, $node->seller, $node->buyer, $node->carbon, $node->cost, $node->valuedate);
423 }
424
425
426
427 /**
428 * Implementation of hook_update, which saves updated todo-specific
429 * information into the todo table
430 * @param node object
431 */
432 function carbon_transfer_update(&$node)
433 {
434 // drupal_set_message("def " . $node->seller . " ... " . $node->seller_index) ;
435 db_query("UPDATE {carbon_transfer} set seller=%d, buyer=%d, carbon=%f, cost=%f, valuedate=%d " .
436 " WHERE nid=%d",
437 $node->seller, $node->buyer, $node->carbon, $node->cost, $node->valuedate,
438 $node->nid);
439 }
440
441
442 /**
443 * Implementation of hook_delete()
444 */
445 function carbon_transfer_delete($node) {
446 db_query("DELETE FROM {carbon_stamp_account} WHERE sid = %d", $node->nid);
447 db_query("DELETE FROM {carbon_transfer} WHERE nid = %d", $node->nid);
448 }
449
450
451 /**
452 * Implementation of hook_view, add our node specific information
453 * @param node object to display
454 * @param boolean is this a teaser or full node?
455 * @param boolean is this displaying on its own page
456 *
457 */
458 function carbon_transfer_view(&$node, $teaser = FALSE, $page = FALSE)
459 {
460 $node = node_prepare($node, $teaser);
461 $carbon_account_info = theme('carbon_transfer_table_view', $node);
462 $node->content["transfer"] = array("#value" => $carbon_account_info);
463 $node->body .= $carbon_account_info;
464 $node->teaser .= $carbon_account_info;
465 return $node ;
466 }
467
468
469
470 function theme_carbon_transfer_table_view($node)
471 {
472 // content variable that will be returned for display
473 $header = array("valuedate", "description", "seller", "buyer", "carbon", "cost", "published");
474
475 $account_map = new CarbonAccountMap() ; $account_map->init();
476
477 $rows = array();
478
479 $rows[] = array(_format_date($node->valuedate),
480 $node->title,
481 $account_map->getAccountLink($node->seller),
482 $account_map->getAccountLink($node->buyer),
483 $node->carbon,
484 $node->cost,
485 $node->status ? "Yes" : "No");
486
487 $output = theme("table", $header, $rows);
488
489 return "<h1></h1>".$output; // the H1s make bluebreeze theme work
490 }
491
492
493 class CarbonTransaction
494 {
495 var $title ; // used for references and descriptive text
496 var $seller ;
497 var $buyer ;
498 var $carbon ;
499 var $cost ;
500 var $valuedate ;
501
502 function initialize()
503 {
504 global $user ;
505 $this->type = 'carbon_transfer' ;
506 $this->status = 1 ; // published
507 $this->promote = 0 ;
508 $this->sticky = 0;
509 $this->body = null ;
510 $this->comment = 1 ;
511 $this->uid = $user->uid ;
512 $this->teaser = "" ;
513 $this->body = "" ;
514 }
515
516 function toString($account_map)
517 {
518 $carbon = sprintf("%.2f", $this->carbon);
519 $cost = sprintf("%.2f", $this->cost);
520 return $account_map[$this->seller]->title.
521 " sold to ".$account_map[$this->buyer]->title.
522 ", ".$carbon. " carbon credits".
523 " at a cost of ".$cost;
524 }
525 }
526
527
528 ?>

  ViewVC Help
Powered by ViewVC 1.1.2