/*------------------------------------------------------------------------
    File        : getCustBalance.p
    Purpose     : This is mapped as a remote method in Customer's model to 
                  be accessed from the client (rest).

    Syntax      :

    Description : Simple remote procedure call to get customer balance 
                  information.

    Author(s)   : Marian Edu
    Created     : Tue Oct 27 10:47:55 EET 2015
    Notes       :
  ----------------------------------------------------------------------*/

/* ***************************  Definitions  ************************** */

routine-level on error undo, throw.

define input    parameter custNum      as integer   no-undo.
define output   parameter amtBalance   as decimal   no-undo.
define output   parameter amtOrdered   as decimal   no-undo.
define output   parameter amtInvoiced  as decimal   no-undo.

for each Customer no-lock
   where Customer.CustNum = custNum:
   
   amtBalance = Customer.Balance.
   
   for each Order no-lock
      where Order.CustNum = Customer.CustNum,
      each OrderLine no-lock
      where OrderLine.Ordernum = Order.Ordernum:
      
      amtOrdered = amtOrdered + OrderLine.Qty * OrderLine.Price.
   end.
   
   for each Invoice no-lock
      where Invoice.CustNum = Customer.CustNum:
      
      amtInvoiced = amtInvoiced + Invoice.Amount.
   end.
end.
