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 "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; $total = 0; while( $row ) { $date = $row["od_date_str"]; $total += $row["od_amount"]; $amount = make_price_str($row["od_amount"]); $reason = $row["od_reason"]; $style = ""; if( $amount < 0 ) { $style = " style=\"color:red\""; } echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; $row = db_next( $ress ); } $style = "font-weight:bold"; if( $total < 0 ) { $style .= "; color:red"; } echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "
dateamountreason
$date$amount$reason
 " . make_price_str($total) . "total
\n"; } else { 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 "

overview

\n"; $ress = db_query( $sql ); if( $ress ) { $row = db_next( $ress ); if( $row ) { echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; while( $row ) { $debitor = $row["ou_nr"]; $url = "?cmd=debts&subcmd=detail&debitor=$debitor"; $amount = $row["amount"]; $style = ""; if( $amount < 0 ) { $style = " style=\"color:red\""; } echo "\n"; echo "\n"; echo "\n"; echo "\n"; $row = db_next( $ress ); } echo "
debitoramount
" . $row["ou_name"] . "" . make_price_str($amount) . "
\n"; } else { 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"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "\n"; echo "
debitor "; $users = db_read_users( $user ); print_select( "debitor", $users, 0 ); echo "
amount (n.nn) 
reason 
 
\n"; echo "
\n"; } function cmd_debts( $subcmd ) { global $userid; echo "

debts

\n"; $rc = true; switch( $subcmd ) { /* case "delete": cmd_debts_(); break; */ case "add": $rc = cmd_debts_add( $userid ); if( $rc ) { $rc = cmd_debts_overview( $userid ); } break; case "detail": $rc = cmd_debts_detail( $userid ); break; default: $rc = cmd_debts_overview( $userid ); break; } if( $rc ) { cmd_debts_print_addform( $userid ); } } ?>