'sold/record', 'title' => t('Record products sold.'), 'callback' => 'sold_record', 'access' => $access); // This path is for the routine that actually does the multplication // It is only setup as a callback (no menu entry) $items[] = array('path' => 'sold/report', 'title' => t('Products information.'), 'type' => MENU_CALLBACK, 'callback' => 'sold_report', 'access' => $access); } $items[] = array('path' => 'sales/history', 'title' => t('Sales History'), 'callback' => 'sales_history', 'access' => user_access('access content'), 'type' => MENU_CALLBACK); $items[] = array('path' => 'test/form', 'title' => t('Test Form'), 'callback' => 'drupal_get_form', 'callback arguments' => 'test_form', 'access' => user_access('access content'), 'type' => MENU_CALLBACK); return $items; } function test_form() { $form = array(); // Build up the form $form['date1'] = array( '#type' => 'textfield', '#width' => 10, ); $form['date2'] = array( '#type' => 'textfield', '#width' => 5, ); // Make sure we have a submit button $form['submit'] = array('#type' => 'submit', '#value' => t('Record')); return ($form); } function test_form_submit($form_id, $form_values) { global $user; // Break the textrea into lines $lines = explode("\n", trim($form_values['products'])); $output.=$lines[0]; $output.="testing"; $_SESSION['test']=$output; return 'sales/history'; } function sold_record() { $form = array(); // Build up the form $form['products'] = array( '#type' => 'textarea', '#title' => t('Products sold'), '#cols' => 10, '#rows' => 10, '#description' => t('Paste in the product information.') ); // Make sure we have a submit button $form['submit'] = array('#type' => 'submit', '#value' => t('Record')); return drupal_get_form('sold_record', $form); } function sold_record_validate($form_id, $form_values) { if (strlen($form_values['products']) > 30000) { form_set_error('products', t('Your submission contains more than 30,000 characters. Please break it down into smaller pieces, then resubmit')); } } function deeptrim($string) { $string = trim($string); $tmp = explode(" ",$string); foreach($tmp as $key => $val) { if (!$val == "") { $return[] = $val; } } return implode(" ",$return); } function sold_record_submit($form_id, $form_values) { global $user; // Parse and save the values. $sql = "INSERT INTO {sold} (uid, date, product, name, price, extra) VALUES(%d, '%s', '%s', '%s', %d, '%s')"; // Break the textrea into lines $lines = explode("\n", trim($form_values['products'])); // For each line entered foreach ( $lines as $line_item ) { // Break the line on the space character $line_item = str_replace("\t", " ", $line_item); $line_item = deeptrim($line_item); $details = explode(" ", $line_item); // The first item will always be the date // The last threee items will be name amount extra (in that order) // The parts in between will be the product name $cnt = count($details); $date = $details[0]; $date_parts = explode('/', $date); $when = mktime(12, 0, 0, $date_parts[1], $date_parts[0], $date_parts[2]); $dates = explode("/",$date); //array of values $when = $dates[2]."-".$dates[1]."-".$dates[0]; //d-m-y to y-m-d $extra = trim($details[$cnt-1]); // remove any extra white space from the last part $amount = str_replace(',', '', $details[$cnt-2]); // Remove any comma's from the amount $name = $details[$cnt-3]; // The product starts at index 1 and includes upto everything but the last three parts // So we slice out that part of the array // And make a string of words separated by a space $product = implode(' ', array_slice($details, 1, -3)); // Record the data db_query($sql, $user->uid, $when, $product, $name, $amount, $extra); } return 'sold/report'; } function sold_report() { global $user; // Report on the data just submitted by the user (recorded will be 1) $results = db_query("SELECT COUNT(sid) as count,price, product FROM {sold} WHERE recorded = 1 AND uid = %d GROUP BY product,price", $user->uid); $output.="

"; while ( $data = db_fetch_object($results)) {$output .= "";} $output.="
Item
Number Sold
Price
$data->product
$data->count
$data->price
"; $results = db_query("SELECT SUM(price) as total, name, extra FROM {sold} WHERE recorded = 1 AND uid = %d GROUP BY name, extra", $user->uid); $output.="

"; while ( $data = db_fetch_object($results) ) {$output .= "";} $output.="
User
NP Spent
" . $data->name . "" . number_format($data->total) . "
"; $results = db_query("SELECT SUM(price) as total, date FROM {sold} WHERE recorded = 1 AND uid = %d GROUP BY date", $user->uid); $output.="

"; while ( $data = db_fetch_object($results) ) {$output .= "";} $output.="
Date
NP Spent
" . $data->date . "" . number_format($data->total) . "
"; $output.="Sales History"; // Clear the recorded flag so data is not included on next recording of data db_query("UPDATE {sold} SET recorded = 0 WHERE uid = %d", $user->uid); return $output; } function my_search_form() { //filtering section html code $output.='
*Sales do not automatically show on this page, you must click Show All*


Product:  
 

Name:  
 


 /   / 
Month Day Year
To:  
 /   / 
Month Day Year



'; return $output; } function sales_history() { global $user; //delete row $sid_to_delete=mysql_real_escape_string($_REQUEST['sid_to_delete']); if(!($sid_to_delete=="")) db_query("delete from sold where sid=$sid_to_delete"); if( $_REQUEST['delete_all'] == "true" ) db_query("delete from sold where uid='$user->uid'"); //set default page $page=mysql_real_escape_string($_REQUEST['page']); if($page=="") $page=1; //save filtering information if the page has been submited by form- this will allow clicking of page links to continue working if($_REQUEST['searchpost']=="true") { $_SESSION['item_textbox'] = mysql_real_escape_string($_REQUEST['item_textbox']); $_SESSION['username_textbox'] = mysql_real_escape_string($_REQUEST['username_textbox']); $_SESSION['date1_from_textbox']=mysql_real_escape_string($_REQUEST['date1_from_textbox']); $_SESSION['date2_from_textbox']=mysql_real_escape_string($_REQUEST['date2_from_textbox']); $_SESSION['date3_from_textbox']=mysql_real_escape_string($_REQUEST['date3_from_textbox']); $_SESSION['date1_to_textbox']=mysql_real_escape_string($_REQUEST['date1_to_textbox']); $_SESSION['date2_to_textbox']=mysql_real_escape_string($_REQUEST['date2_to_textbox']); $_SESSION['date3_to_textbox']=mysql_real_escape_string($_REQUEST['date3_to_textbox']); $date_from = $_SESSION['date3_from_textbox'] . '-' . $_SESSION['date1_from_textbox'] . '-' . $_SESSION['date2_from_textbox']; $_SESSION['date_from']=$date_from; $date_to = $_SESSION['date3_to_textbox'] . '-' . $_SESSION['date1_to_textbox'] . '-' . $_SESSION['date2_to_textbox']; $_SESSION['date_to']=$date_to; } //search form $output.=my_search_form(); //start sessions $output.=''; $old_order_by=$_SESSION['order_by']; //set default if none set allready for ordering table if(!($_REQUEST['order_by']=="")) $_SESSION['order_by']=mysql_real_escape_string($_REQUEST['order_by']); if($_SESSION['order_by']=="") $_SESSION['order_by']="date"; //reverse ordering if link is clicked from same page $old_page=$_SESSION['old_page']; $_SESSION['old_page']=$page; $order_by=$_SESSION['order_by']; //basically does same thing - checks table should be reversed if(!($_REQUEST['sid_to_delete_desc_asc']=="true")) { if($old_page==$page) { if($_SESSION['order_by']==$old_order_by) { if($_SESSION['desc_asc']=="desc") $_SESSION['desc_asc']="asc"; else $_SESSION['desc_asc']="desc"; } } } $desc_asc=$_SESSION['desc_asc']; $results = db_query("select date,product,name,price from sold where uid='$user->uid'"); //determine what row to start at $start_at=20*($page-1); //start portion of query string $db_query_string="select sid, date,product,name,price from sold where uid='$user->uid' "; //for statistics function $user_statistics.=$user->uid; //appends restrictions to mysql query for limiting search results if(!($_SESSION['username_textbox']=="")) $db_query_restrictions.= " and name='" . $_SESSION['username_textbox'] . "' "; if(!($_SESSION['item_textbox']=="")) $db_query_restrictions.= " and product='" . $_SESSION['item_textbox'] . "' "; if(!($_SESSION['date_from']=="--")) $db_query_restrictions.= " and date >= '" . $_SESSION['date_from'] . "' and date <= '" . $_SESSION['date_to'] . "' "; $db_query_string.=$db_query_restrictions; //count rows & pages $db_query_string_num_rows.=$db_query_string . " order by $order_by $desc_asc"; $results = db_query($db_query_string_num_rows); $num_rows = mysql_num_rows($results); $num_pages=ceil($num_rows/20); $db_query_string.=" order by $order_by $desc_asc limit $start_at, 20"; $results = db_query($db_query_string); //sales history table $output.=""; $output.=""; $output.="
DateProductNamePrice
Delete
"; //form starts here $output.='
'; $output.=''; $output.=""; $output.=''; while ( $data = db_fetch_object($results)) { if($_REQUEST[$data->sid]=="on") db_query("delete from sold where sid='$data->sid';"); else $output.=""; } $output.="
$data->date$data->product$data->name$data->price
" . mysql_real_escape_string($_REQUEST[$data->sid]) . "
"; $output.=''; $output.=''; $output.='
'; $output.='
'; $output.='
'; $output.='
Mass Deletion Page
'; //starts the page number navigation system $output.=""; if(!($page==1)) $output.=""; else $output.=""; if($page>=20) $index=($page-10); else $index=1; $start_page=$index; for(;$index<($start_page+20);$index++) if($index<=$num_pages) if($page!=$index) $output.=""; else $output.=""; // >> if page is the last page don't display line if((!($num_pages==$page)) && (!($num_pages==0))) $output.=""; else $output.=""; $output.="
<< <<
$index
$index
>>
>>
"; //form to submit sales data $output.=sold_record(); //statistics $output.=statistics_for_sales_history($db_query_restrictions, $user_statistics); return $output; } function statistics_for_sales_history($db_query_restrictions, $user_statistics) { //sum of the amount of items a user has sold and sum of the price of all those items $sum_results = mysql_query("select count(*) as sum_sold, sum(price) as sum_price from sold where uid='$user_statistics'"); $sum_sold = number_format(mysql_result($sum_results, 0, sum_sold)); $sum_price = number_format(mysql_result($sum_results, 0, sum_price)); $output.= "
You have sold " . $sum_sold . " items for a total of " . $sum_price . " NP

"; //jump down page links $output.='
Total Items Sold | '; $output.='Sale Totals by User | '; $output.='Sale Totals by Date
'; $output.="
"; //total items sold $output.="
Total Items Sold
(Full List - More in-depeth)
"; $output.=""; $results=db_query("select product, count(*) as number_sold, price from sold where uid='$user_statistics' " . $db_query_restrictions . " group by product,price order by number_sold desc limit 200"); while ( $data = db_fetch_object($results)) { $output.=""; $output.=""; $output.=""; } $output.="
Item
Number Sold
Price
" . $data->product . "
" . number_format($data->number_sold) . "
$data->price
"; $output.="
Jump to top

"; //sales total by user $output.="
Sale Totals by User (Full List)
"; $output.=""; $results=db_query("select name,sum(price) as price from sold where uid='$user_statistics' " . $db_query_restrictions . " group by name order by price desc limit 0, 30;"); while ( $data = db_fetch_object($results)) { $output.=""; $output.=""; } $output.="
UserNP Spent
" . $data->name . "
" . number_format($data->price) . "
"; $output.="
Jump to top

"; //sales total by date $output.="
Sale Totals by Date (Full List)
"; $output.=""; $results = db_query("select date,sum(price) as price from sold where uid='$user_statistics' " . $db_query_restrictions . " group by date order by date desc limit 7;"); while ( $data = db_fetch_object($results)) { $output.=""; $output.=""; } $output.="
DateNP Spent
" . $data->date . "
" . number_format($data->price) . "
"; $output.="
"; $output.="
Jump to top

"; return $output; } ?>