RemainingCredit Function
Expand/Collapse source code of procedure RemainingCredit Source Code
        LPARAMETERS tcCustomerID
  LOCAL lyMaxOrderAmount, ;
        lyTotalOrders, ;
        lcCustomerAlias, ;
        lnOldArea

  IF PARAMETERS() < 1 OR EMPTY(tcCustomerID)
    RETURN 0
  ENDIF

  lyMaxOrderAmount = 0

  lnOldArea = SELECT(0)
  lcCustomerAlias = SYS(2015)
  USE tastrade!customer AGAIN IN 0 ALIAS (lcCustomerAlias)
  IF SEEK(tcCustomerID, lcCustomerAlias, "customer_i")
    lyMaxOrderAmount = EVAL(lcCustomerAlias + ".max_order_amt")
  ENDIF
  USE IN (lcCustomerAlias)

  *-- We use the orders table again to avoid "Illegal 
  *-- recursion" errors.
  USE tastrade!orders AGAIN ALIAS _orders IN 0

  SELECT SUM((b.unit_price * b.quantity) - ;
      (a.discount * .01) * (b.unit_price * b.quantity)) + a.freight AS totalorder ;
    FROM _orders a, order_line_items b ;
   WHERE a.order_id = b.order_id AND ;
     a.customer_id = tcCustomerID AND ;
     !a.paid ;
   GROUP by a.order_id ;
    INTO CURSOR orderamounts

  SUM totalorder to lyTotalOrders

  USE IN _orders
  USE IN orderamounts

  SELECT (lnOldArea)

  RETURN lyMaxOrderAmount - lyTotalOrders