Project:Balance Tracker
Version:6.x-1.x-dev
Component:Miscellaneous
Category:task
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

For those looking for an example of the API use.

I implemented this on a webshop where designers earn a percentage on every design sold, the code is executed based on a conditional actions of ubercart.

function attiks_minifabriek_action_update_user_balance($order, $settings) {

  $som = array();
 
  foreach ($order->products as $p) {
    // Load designer
    $result = db_query('select field_ontwerper_uid from {content_type_product} where nid = ' . $p->nid);
    $ontwerper = db_fetch_object($result);
    if (isset($ontwerper->field_ontwerper_uid)) {
      $profile = content_profile_load('ontwerper', $ontwerper->field_ontwerper_uid);
      $result = db_query('select field_commissie_value from {content_type_ontwerper} where nid = ' . $profile->nid);
      $commissie = db_fetch_object($result);
      $commissie = $commissie->field_commissie_value;
      $som[$ontwerper->field_ontwerper_uid] += $commissie * $p->qty * $p->price;
    }
  }
 
  foreach ($som as $o => $s) {
    if ($s > 0) {
      balance_tracker_credit_account ($o, $s, 'Commissie op bestelling ' . $order->order_id);
    }
  }

}

Comments

#1

atticks,

That is more or less how I intended it to be used.

I should also mention that I have created a two new hook in the CVS - hook_balance_prewrite() and hook_balance_write(). These hooks are called before and after a balance item is written.

<?php
/**
* Implementation of hook_balance_write().
*
* Called after a successful write of a balance item.
*/
function bt_simplestats_balance_write($uid, $type, $amount, $message){
 
$month = date('M_Y');
 
// Clear the cached statistic for this month, so they are rebuilt with the new transaction included.
 
cache_clear_all('bt_simplestats_month_' . $month, 'cache');
}
?>

<?php
/**
* Implementation of hook_balance_prewrite().
*
* Called before a successful write of a balance item.
*/
function bt_simplestats_balance_prewrite($uid, $type, &$amount,& $message){
 
// Alter $amount or $message before they are saved.
}
?>

$uid, $amount and $message are the same as used on balance_tracker_credit_account(). $type is either 'debit' or 'credit'.

Are there any other API suggestions you have that will help you as a developer?

#2

not really any suggestion for the api, i think it's pretty complete (at least for me). I didn't had time yet to look at the reporting side, but maybe some function to get the current balance (and last transaction date) so it's easy to display on the user tab.

maybe an option to add the balance to the user tabs?

#3

Hi guys,

I am looking to use balance tracker and it's API to credit and debit commissions to a user's account. I currently have a site that will be selling graphic designs and each respective seller needs their sales/commission amounts to be tracked. @attiks I have cut and pasted your example code above into a conditional action in Ubercart, but I am a little lost as to what parts I am missing to make this work for me. I'm assuming I have to edit my content type so that I have a user reference field similar to 'field_ontwerper_uid' in your example usage above?

Any help on this would be greatly appreciated!

#4

@rynoceris

attached the include file for the CA

AttachmentSize
attiks_minifabriek.ca_.inc_.txt 4.69 KB

#5

#3

I needed the user reference field because the commission in my example depends on the user, if you use the same commission for all users you can hard-code it or use a site-wide variable

#6

@attiks

You are a total lifesaver! Thank you very much for posting your code, it made getting this working on our current project a breeze!

nobody click here