/*
 * Author       : zchelp
 * Generated on : 22-Nov-2009 21:12:50
 * Version      : 3.0
 */
application "ProfitCalculation"
{
    type = public
    date format = "dd-MMM-yyyy"
    time zone = "America/Los_Angeles"
    section Home
    {
        form  Details
        {
            
            JobNo
            (
                type  =  number
                width  =  20
            )

            Item
            (
                type  =  text
            )

            Description
            (
                type  =  text
            )

            Cost
            (
                type  =  GBP
                width  =  20
            )

            Sale
            (
                type  =  GBP
                width  =  20
            )

            Profit
            (
                type  =  GBP
                width  =  20
            )

            PercentageProfit
            (
                type  =  decimal
                width  =  20
            )

            AvgProfit
            (
                type  =  decimal
                width  =  20
            )

            actions
            {
                on add
                {
                    on load
                    {
                        hide AvgProfit;
                        hide Profit;
                        hide PercentageProfit;
                    }
                    Submit
                    (
                        type  =  submit
                        displayname  =  "Submit"
                        on success
                        {
                            input.Profit = (input.Sale  -  input.Cost);
                            input.PercentageProfit = ((input.Sale  -  input.Cost)  /  input.Sale  *  100);
                            totaldat = 0;
                            totalcount = 0;
                            for each x in Details  [JobNo == input.JobNo]
                            {
                                totaldat = ((totaldat  +  x.PercentageProfit)).toLong();
                                totalcount = (totalcount  +  1);
                            }
                            if ((totaldat  >  0)  &&  (totalcount  >  0))
                            {
                                dat  =  Details  [JobNo == input.JobNo];
                                dat.AvgProfit = (totaldat  /  totalcount);
                            }
                        }
                    )
                    Reset
                    (
                        type  =  reset
                        displayname  =  "Reset"
                    )
                }
                on edit
                {
                    Update
                    (
                        type  =  submit
                        displayname  =  "Update"
                    )
                    Cancel
                    (
                        type  =  cancel
                        displayname  =  "Cancel"
                    )
                }
            }
        }

        list  Details_View
        {
            displayname = "Details View"
            show  all  rows  from  Details 
            (
                JobNo
                Item
                Description
                Cost, display total
                Sale, display total
                Profit, display total
                PercentageProfit
                AvgProfit
            )
            group by
            (
                JobNo   ascending
            )
            options
            (
                display rows = 100
            )
        }

    }

}
