/*
 * Author       : zchelp
 * Generated on : 23-Nov-2009 01:23:15
 * Version      : 3.0
 */
application "Freight Forwarders"
{
    type = public
    date format = "dd-MMM-yyyy"
    time zone = "America/Los_Angeles"
    section Home
    {
        form  CountryRates
        {
            
            Country
            (
                type  =  text
            )

            CostPerPound
            (
                type  =  USD
                width  =  20
            )

            DeliveryDays
            (
                type  =  number
                width  =  20
            )

            actions
            {
                on add
                {
                    Submit
                    (
                        type  =  submit
                        displayname  =  "Submit"
                    )
                    Reset
                    (
                        type  =  reset
                        displayname  =  "Reset"
                    )
                }
                on edit
                {
                    Update
                    (
                        type  =  submit
                        displayname  =  "Update"
                    )
                    Cancel
                    (
                        type  =  cancel
                        displayname  =  "Cancel"
                    )
                }
            }
        }

        list  CountryRates_View
        {
            displayname = "CountryRates View"
            show  all  rows  from  CountryRates 
            (
                Country
                CostPerPound
                DeliveryDays
            )
            options
            (
                display rows = 100
            )
        }

        form  Order_form
        {
            displayname  =  "Order"
            
            column
            {
                Name
                (
                    type  =  text
                )

                EmailID
                (
                    type  =  email
                )

                PhoneNumber
                (
                    type  =  number
                    width  =  20
                )

                DeliveryAddress
                (
                    type  =  textarea
                )

            }
            column
            {
                plain
                (
                    type  =  plaintext
                    value = "<DIV id=ted>If the Cost is over $500, Insurance at the rate of 5% is automatically added to Cost. It is optional in other cases</DIV>\n"
                )

                Would_you_like_to_add_Insurance
                (
                    displayname  =  "Would you like to add Insurance?"
                    type  =  radiobuttons
                    values  =  {"Yes",   "No"}
                    on user input
                    {
                        if ((input.Would_you_like_to_add_Insurance  ==  "Yes")  &&  (input.Cost  <  500))
                        {
                            input.TotalCost = (1.05  *  input.Cost);
                        }
                        else
                        {
                            input.TotalCost = input.Cost;
                        }
                    }
                )

                Length
                (
                    type  =  decimal
                    width  =  20
                    on user input
                    {
                        if (((input.Height  !=  null)  &&  (input.Width  !=  null))  &&  (input.Length  !=  null))
                        {
                            input.VolumeWeight = (input.Height  *  input.Width  *  input.Length  /  166).round(2);
                        }
                    }
                )

                Width
                (
                    type  =  decimal
                    width  =  20
                    on user input
                    {
                        if (((input.Height  !=  null)  &&  (input.Width  !=  null))  &&  (input.Length  !=  null))
                        {
                            input.VolumeWeight = (input.Height  *  input.Width  *  input.Length  /  166).round(2);
                        }
                    }
                )

                Height
                (
                    type  =  decimal
                    width  =  20
                    on user input
                    {
                        if (((input.Height  !=  null)  &&  (input.Width  !=  null))  &&  (input.Length  !=  null))
                        {
                            input.VolumeWeight = (input.Height  *  input.Width  *  input.Length  /  166).round(2);
                        }
                    }
                )

                NetWeight_In_Pounds
                (
                    displayname  =  "NetWeight(In Pounds)"
                    type  =  decimal
                    width  =  20
                )

                CountryDeliveredTo
                (
                    type  =  picklist
                    values  =  CountryRates.Country
                    on user input
                    {
                        if (input.CountryDeliveredTo  !=  "-Select-")
                        {
                            dat  =  CountryRates  [Country == input.CountryDeliveredTo];
                            if ((input.NetWeight_In_Pounds  <=  10.0)  ||  (input.VolumeWeight  <=  10.0))
                            {
                                input.Cost = 10.0;
                            }
                            else
                            {
                                if (input.VolumeWeight  >  input.NetWeight_In_Pounds)
                                {
                                    input.Cost = (input.VolumeWeight  *  dat.CostPerPound).round(2);
                                }
                                else
                                {
                                    input.Cost = (input.NetWeight_In_Pounds  *  dat.CostPerPound).round(2);
                                }
                            }
                        }
                    }
                )

                VolumeWeight
                (
                    type  =  decimal
                    width  =  20
                )

                Cost
                (
                    type  =  USD
                    width  =  20
                    on user input
                    {
                        if (input.Cost  <  500)
                        {
                            show Would_you_like_to_add_Insurance;
                            input.TotalCost = input.Cost;
                        }
                        else
                        {
                            hide Would_you_like_to_add_Insurance;
                            input.TotalCost = (input.Cost  *  1.05);
                        }
                    }
                )

                TotalCost
                (
                    type  =  USD
                    width  =  20
                )

            }
            actions
            {
                on add
                {
                    on load
                    {
                        disable Cost;
                        hide Would_you_like_to_add_Insurance;
                        disable TotalCost;
                        hide VolumeWeight;
                        hide Cost;
                    }
                    Submit
                    (
                        type  =  submit
                        displayname  =  "Submit"
                        on success
                        {
                            tet  =  CountryRates  [Country == input.CountryDeliveredTo];
                            success message "The estimated delivery time to " + input.CountryDeliveredTo + " is " + tet.DeliveryDays + " Days";
                            sendmail
                            (
                                To       :  input.EmailID 
                                From     :  zoho.adminuserid 
                                Subject  :  "Order Details" 
                                Message  :  "<p>Your order has been confirmed. Your order number is " + input.Name + input.PhoneNumber + "</p>" 
                            )
                        }
                    )
                    Reset
                    (
                        type  =  reset
                        displayname  =  "Reset"
                    )
                }
                on edit
                {
                    Update
                    (
                        type  =  submit
                        displayname  =  "Update"
                    )
                    Cancel
                    (
                        type  =  cancel
                        displayname  =  "Cancel"
                    )
                }
            }
        }

        list  Order_View
        {
            displayname = "Order View"
            show  all  rows  from  Order_form 
            (
                Name
                Height
                Length
                Width
                NetWeight_In_Pounds as "NetWeight(In Pounds)"
                CountryDeliveredTo
                Cost
                VolumeWeight
                EmailID
                PhoneNumber
                DeliveryAddress
                Would_you_like_to_add_Insurance as "Would you like to add Insurance?"
                TotalCost
            )
            filters 
            (
                Would_you_like_to_add_Insurance
                CountryDeliveredTo
            )
            options
            (
                display rows = 100
            )
        }

    }

}
