Print

Tutorial Code Examples




The following are code examples used in the tutorial videos.

Transaction form, Custom Code, Javascript tab

function getTotal(){
    var theSubform    = 'transaction_item';   //---- the name of the subform
    var total         = 0;
//-- nuSubformRowArray() returns an array containing the prefix for each row in this subform 
//-- the second parameter gives us just the "unticked" rows
    var a             = nuSubformRowArray(theSubform, true);
    for (var i = 0 ; i < a.length ; i++){
        var quantity   = Number(document.getElementById(a[i] + 'tri_quantity').value);
        var price      = Number(document.getElementById(a[i] + 'tri_price').value);
          
        document.getElementById(a[i] + 'tri_total').value = quantity * price;
        nuFormatField(document.getElementById(a[i] + 'tri_price'));
        nuFormatField(document.getElementById(a[i] + 'tri_total'));
        total          = total + Number(document.getElementById(a[i] + 'tri_total').value);
    }
    
    document.getElementById('tran_total').value = total;
    nuFormatField(document.getElementById('tran_total'));
}
   
function postTransaction(){
    //update transaction table
    document.getElementById('tran_posted').value = 1;
    nuFormatField(document.getElementById('tran_posted'));

    //save and refresh
    nuSaveThis(0);
}

Transaction form, Custom Code, After Save tab

if('#tran_posted#' == 1){  // transaction has just been posted update stock levels
    $a             = nuSubformArray('transaction_item', true);

    // loop through rows of subform and
    // update stock values for each transaction item
 
    for($i = 0 ; $i < sizeof($a) ; $i ++){
        $numSold   = $_POST[$a[$i] . 'tri_quantity'];
        $prodID    = $_POST[$a[$i] . 'tri_product_id'];
    
	$s         = "UPDATE product ";
	$s        .= "SET prod_sold = prod_sold + $numSold ";
	$s        .= "WHERE product_id = '$prodID'";

	nuRunQuery($s);
    }
}

Transaction Report, form data

$start = nuReformatField('#start#',6);
$end = nuReformatField('#end#',6);

$s  = 
<<<EOSQL

CREATE TABLE #dataTable# select *, $start AS start_date, $end AS end_date 
FROM customer INNER JOIN transaction ON tran_cust_id = customer_id 
INNER JOIN #cuslist# ON customer_id = #cuslist# 

EOSQL;

$where = "WHERE tran_date BETWEEN $start AND $end";
   
if($start == "NULL" and $end != "NULL"){
  $where = "WHERE tran_date BETWEEN (SELECT MIN(tran_date) FROM transaction) AND $end";}

if($start != "NULL" and $end == "NULL"){
  $where = "WHERE tran_date BETWEEN $start and (SELECT MAX(tran_date) FROM transaction)";}

if($start == "NULL" and $end == "NULL"){
  $where = "";}

$s .= $where;
nuRunQuery($s);


Transaction Report, Graph Code

nuRunQuery(
  <<<EOSQL

create table #graphTable#
select cus_name, SUM(Tran_Total) as value
from customer INNER JOIN transaction 
on transaction.tran_cust_id = customer.customer_id
where customer_id = '#customer#'
group by customer_id
EOSQL
  );

nuRunQuery(
  <<<EOSQL

insert into #graphTable#
select distinct 'others', SUM(tran_total)
from transaction
where tran_cust_id != '#customer#'
group by tran_cust_id
EOSQL
  );



Created by: system. Last Modification: Thursday 16 of February, 2012 09:38:30 CST by admin.