detail " . $row["ou_name"] . "\n"; $sql = "select " . "od_nr, " . "od_amount, " . "od_reason, " . "to_char( od_date, 'DD.MM.YYYY' ) as od_date_str " . "from " . "( " . "select " . "od_nr, " . "od_amount, " . "od_reason, " . "od_date " . "from " . "orderdebt " . "where " . "ou_nr_creditor = $user and " . "ou_nr_debitor = $debitor and " . "od_paid_date is null " . "union select " . "od_nr, " . "-od_amount, " . "od_reason, " . "od_date " . "from " . "orderdebt " . "where " . "ou_nr_creditor = $debitor and " . "ou_nr_debitor = $user and " . "od_paid_date is null " . ") d " . "order by " . "od_date"; $ress = db_query( $sql ); if( $ress ) { $row = db_next( $ress ); if( $row ) { echo "
date | \n"; echo "amount | \n"; echo "reason | \n"; echo "
---|---|---|
$date | \n"; echo "$amount | \n"; echo "$reason | \n"; echo "
\n"; echo " | " . make_price_str($total) . " | \n"; echo "total | \n"; echo "
No open debts.
\n"; } } else { echo "ERROR"; $rc = false; } return $rc; } function cmd_debts_overview( $user ) { $rc = true; $sql = "select " . "u.ou_nr, " . "u.ou_name, " . "sum(d.od_amount) as amount " . "from " . "( " . "select " . "od_amount, " . "ou_nr_debitor " . "from " . "orderdebt " . "where " . "ou_nr_creditor = $user and " . "od_paid_date is null " . "union select " . "-od_amount, " . "ou_nr_creditor as ou_nr_debitor " . "from " . "orderdebt " . "where " . "ou_nr_debitor = $user and " . "od_paid_date is null " . ") d, " . "orderuser u " . "where " . "u.ou_nr = d.ou_nr_debitor " . "group by " . "u.ou_nr, " . "u.ou_name"; echo "debitor | \n"; echo "amount | \n"; echo "
---|---|
" . $row["ou_name"] . " | \n"; echo "" . make_price_str($amount) . " | \n"; echo "
No open debts.
\n"; } } else { echo "ERROR"; $rc = false; } return $rc; } function cmd_debts_add( $user ) { $rc = true; $debitor = get_POST_var( "debitor" ); if( !ereg( "^[1-9][0-9]*\$", $debitor ) ) { print_error( "missing or invalid debitor ($debitor)" ); return false; } $amount = get_POST_var( "amount" ); if( !ereg( "^[+-]?[0-9]*[.][0-9][0-9]\$", $amount ) ) { print_error( "missing or invalid amount ($amount)" ); return false; } $amount = (int)str_replace( ".", "", $amount ); $reason = get_POST_var( "reason" ); if( $reason == "" ) { print_error( "missing or invalid reason ($reason)" ); return false; } $sql = "select ou_name from orderuser where ou_nr = $debitor"; $ress = db_query( $sql ); if( !$ress ) { print_error( "error reading debitor $debitor" ); return false; } $row = db_next( $ress ); if( !$row ) { print_error( "debitor $debitor not found" ); return false; } $debitorname = $row["ou_name"]; $odnr = db_next_nr( "orderdebt", "od_nr" ); if( $odnr == 0 ) { print_error( "error getting next od_nr" ); return false; } $sql = "select " . "sum(d.od_amount) as amount " . "from " . "( " . "select " . "od_amount " . "from " . "orderdebt " . "where " . "od_nr < $odnr and " . "ou_nr_creditor = $user and " . "ou_nr_debitor = $debitor and " . "od_paid_date is null " . "union select " . "-od_amount " . "from " . "orderdebt " . "where " . "od_nr < $odnr and " . "ou_nr_creditor = $debitor and " . "ou_nr_debitor = $user and " . "od_paid_date is null " . ") d"; $ress = db_query( $sql ); if( !$ress ) { print_error( "error reading debitor $debitor" ); return false; } $row = db_next( $ress ); if( !$row ) { print_error( "debitor $debitor not found" ); return false; } $debt = -$row["amount"]; $odpaiddate = "null"; $ounrpaid = "null"; $text = "$debitorname owes you " . make_price_str( $amount ) . " (more)."; if( $amount < 0 ) { $text = "you owe $debitorname " . make_price_str( -$amount ) . " (more)."; } db_begin(); if( $debt == $amount ) { $odpaiddate = "current_timestamp"; $ounrpaid = $user; $sql = "update " . "orderdebt " . "set " . "od_paid_date = current_timestamp, " . "ou_nr_paid = $user " . "where " . "od_paid_date is null and " . "od_nr < $odnr and ". "( " . "( " . "ou_nr_creditor = $user and " . "ou_nr_debitor = $debitor " . ") or " . "( " . "ou_nr_creditor = $debitor and " . "ou_nr_debitor = $user " . ") " . ")"; if( db_update( $sql ) <= 0 ) { $rc = false; } $text = "$debitorname and you are even now."; } $crednr = $user; $debtnr = $debitor; if( $amount < 0 ) { $crednr = $debitor; $debtnr = $user; $amount = -$amount; } $sql = "insert into " . "orderdebt " . "( " . "od_nr, " . "ou_nr_creditor, " . "ou_nr_debitor, " . "od_amount, " . "od_reason, " . "od_date, " . "od_paid_date, " . "ou_nr_paid " . ") values ( " . "$odnr, " . "$crednr, " . "$debtnr, " . "$amount, " . db_escape_str( $reason ) . ", " . "current_timestamp, " . "$odpaiddate, " . "$ounrpaid " . ")"; if( db_update( $sql ) != 1 ) { $rc = false; } if( $rc ) { echo "$text
\n"; db_commit(); } else { echo "error saving data
\n"; db_rollback(); } return $rc; } function cmd_debts_print_addform( $user ) { echo "\n"; echo "\n"; } function cmd_debts( $subcmd ) { global $userid; echo "