jeudi 27 août 2015

Converting a web-based PHP file into MVC (Model View Controller)

One of our company's clients have sent us some code to fix up, it's not the greatest and needs a lot of work. However, my boss wants it to be converted into MVC form. And if possible, may be to break down the code as well, I'm only a beginner at PHP so I'm not quite so good yet and also have very little experience with MVC and yes I have been reading up on it but I still don't know how to go about converting this code into MVC form :/ I noticed there is a very long if statement which is very irritating. Help appreciated very much! This is meant to be a booking calculator for a hotel and calculates prices based on the season and how many adults, children, and infants are booked into the hotel. The website you can see it at is "book.winduvillas.com"

<?php


require(dirname(__FILE__).'/backbone/page_top.php');


/***************************************************************/


# Mainline

thisBlah();


/***************************************************************/


function thisBlah() {

    $gDate                              = $_REQUEST['date'];
    $gDate1                             = $_REQUEST['date1'];
    $gAdult1                            = $_REQUEST['adult'];
    $gChild1                            = $_REQUEST['child'];
    $gInfant1                           = $_REQUEST['infant'];
    $gRateScheduleID            = $_REQUEST['rateScheduleId'];
    $gLan_Val                           = $_REQUEST['lan_val'];


/************ NO SUPER GLOBALS BELOW THIS LINE *****************/   

    # Globals
    $date                                   = $gDate;
    $date1                              = $gDate1;
    $adult1                             = $gAdult1;
    $child1                             = $gChild1;
    $infant1                            = $gInfant1;
    $rateScheduleId             = $gRateScheduleID;
    $lan_val                            = $gLan_Val;


/***************************************************************/

# 

    if ($adult1=='') {
        $adult = 0;
    }

    if ($adult1!='') {
        $adult = $adult1;
    }

    if ($child1=='') {
        $child = 0;
    }

    if ($child1!='') {
        $child = $child1;
    }

    if ($infant1=='') {
        $infant = 0;
    }

    if ($infant1!='') {
        $infant = $infant1;
    }


/***************************************************************/


    # Date Difference
    $check_in                           = explode("/",$date);
    $check_out                      = explode("/",$date1);

    $new_in                             = $check_in[2]."-".$check_in[1]."-".$check_in[0];
    $new_out                            = $check_out[2]."-".$check_out[1]."-".$check_out[0];


/***************************************************************/


    # For Seasons

        $newyear                            = '2014';

        $new_inseason                   = $newyear."-".$check_in[1]."-".$check_in[0];

        $new_outseason              = $newyear."-".$check_out[1]."-".$check_out[0];


/***************************************************************/


    # Fetch dates from different seasons (Low, High, & Peak) -- Could use better terminology?

        $date_diff1                     = mysql_query("SELECT DATEDIFF('$new_out','$new_in') AS DiffDate");

        $date_diff                      = mysql_fetch_array($date_diff1);

        $diff                                   = $date_diff['DiffDate'];

        $date_diff2                     = mysql_query("SELECT DATEDIFF('$new_in',curdate()) AS DiffDate");

        $date_diff3                     = mysql_fetch_array($date_diff2);

        $diff2                              = $date_diff3['DiffDate'];


/***************************************************************/


    # Define booking rate based on schedule?

        $rate_schedulemod1          = mysql_query("select * from booking_rate_schedule where 'rate_schedule_id'='".$gRateScheduleID."'");

        $rate_schedulemod2          = mysql_fetch_array($rate_schedulemod1);

        $rate_schedulemod3          = $rate_schedulemod2['no_of_room'];

        $currencymod                        = mysql_query("select * from booking_currency where currency_id='".$gLan_Val."'");

        $cony                                       = mysql_fetch_array($currencymod);


/***************************************************************/


    # Calculate costs based on what season it is (Low, High, & Peak)

        $cost_cal                           = mysql_query("select * from booking_season_chart where 'start_date'<='".$new_inseason."' AND 'end_date' >='".$new_outseason."'");

        $sea_type                           = mysql_fetch_array($cost_cal);

        $season_type                    = $sea_type['season_type'];


/***************************************************************/


    # Define when seasons start and end?

        if ($season_type=='') {
            // this tag closes on Line 475


                $date_diffnew1              = mysql_fetch_array(mysql_query("SELECT DATE_ADD($new_inseason,INTERVAL 1 DAY) AS crossindate FROM dual"));

                $date_diffnew1              = $date_diffnew1['crossindate'];

                $start_season                   = mysql_fetch_array(mysql_query("select * from booking_season_chart where  '".$new_inseason."' BETWEEN 'start_date' AND 'end_date'"));

                $start_season                   = $start_season['season_type'];

                $start_enddate              = mysql_fetch_array(mysql_query("select * from booking_season_chart where  '".$new_inseason."' BETWEEN 'start_date' AND 'end_date'"));

                $start_enddate              = $start_enddate['end_date'];
                $date_diffmod                   = mysql_query("SELECT DATEDIFF('$start_enddate','$new_inseason') AS DiffDate");

                $date_diffmod                   = mysql_fetch_array($date_diffmod);
                $diffmod                            = $date_diffmod['DiffDate']+1;

                $date_diffnew2              = mysql_fetch_array(mysql_query("SELECT DATE_ADD($new_outseason,INTERVAL 1 DAY) AS crossinout FROM dual"));
                $date_diffnew2              = $date_diffnew2['crossinout'];

                $end_season                     = mysql_fetch_array(mysql_query("select * from booking_season_chart where  '".$new_outseason."' BETWEEN 'start_date' AND 'end_date'"));

                $end_season                     = $end_season['season_type'];

                $end_enddate                    = mysql_fetch_array(mysql_query("select * from booking_season_chart where  '".$new_outseason."' BETWEEN 'start_date' AND 'end_date'"));

                $end_enddate                    = $end_enddate['start_date'];

                $date_diffmodend            = mysql_query("SELECT DATEDIFF('$new_outseason','$end_enddate') AS DiffDate");

                $date_diffmodend            = mysql_fetch_array($date_diffmodend);

                $diffmodend                     = $date_diffmodend['DiffDate'];

                $total_cost           = mysql_query("select * from booking_rate_schedule where 'rate_schedule_id'='".$gRateScheduleID."'");

                $total_costnew              = mysql_fetch_array($total_cost);

                $roomtype1                      = mysql_query("select * from booking_master_room_type where 'room_type_id'='".$total_costnew['room_type_id']."'");

                $roomtype2                      = mysql_fetch_array($roomtype1);

                $roomtype3                      = $roomtype2['room_type'];

                $accomodation                   = $total_costnew['accomodation'];


/***************************************************************/


    # Rates for different seasons

                if ($start_season=='Low Season')
                    {
                        $start_rate = $total_costnew['low_season'];
                    }

                if ($start_season=='High Season')
                    {
                        $start_rate = $total_costnew['high_season'];
                    }

                if ($start_season=='Peak Season')
                    {
                        $start_rate = $total_costnew['peak_season'];
                    }

                $start_datenew = $diffmod * $start_rate;
                if ($end_season=='Low Season')
                    {
                        $end_rate = $total_costnew['low_season'];
                    }

                if ($end_season=='High Season')
                    {
                        $end_rate = $total_costnew['high_season'];
                    }

                if ($end_season=='Peak Season')
                    {
                        $end_rate = $total_costnew['peak_season'];
                    }


/***************************************************************/


    # Commencing discounts in regards to a season change

                $end_datenew                    = $diffmodend * $end_rate;

                $actual_rate                    = ($start_datenew + $end_datenew);

                $no_of_additional           = ($adult + $child) - $accomodation;


                if ($no_of_additional > 0)
                    {

                        $add_cost = mysql_query("select * from booking_additional_cost");

                        $add_cost1 = mysql_fetch_array($add_cost);

                        $add_cost2 = $add_cost1['additional_cost'];

                        $extra_charge = $add_cost2 * $no_of_additional * $diff;

                    }


                $total_cost1                    = $actual_rate + $extra_charge;

                $tax_rates                      = mysql_query("select * from booking_tax"); 

                $tax_rate                           = mysql_fetch_array($tax_rates);

                $tax                                    = ($total_cost1 * $tax_rate['tax'])/100;


                $new_season                     = mysql_query("select * from booking_discount_night where 'season_type'='".$start_season."'");

                $new_season1                    = mysql_fetch_array($new_season);

                $night_number                   = $new_season1['night_number'];

                $discount_rate              = $new_season1['discount_rate'];

                $new_season_end             = mysql_query("select * from booking_discount_night where 'season_type'='".$end_season."'");

                $new_season2                    = mysql_fetch_array($new_season_end);

                $night_number2              = $new_season2['night_number'];

                $discount_rate2             = $new_season2['discount_rate'];


                $commence_discount      = mysql_query("select * from booking_commencement_discount");

                $commence_discount1     = mysql_fetch_array($commence_discount);

                $commence_discount2     = $commence_discount1['discount_rate'];

                $commence_discount3     = $commence_discount1['day'];

                $gross_amount                   = $total_cost1 + $tax;

        $diff_night                     = $diff;


/***************************************************************/


    # Rebates

                if ($diffmodend > $night_number2)
                    {
                        $rebate4 = ($gross_amount * $discount_rate2)/100;
                    }

                if ($diffmod > $night_number)
                    {
                        $rebate = ($gross_amount * $discount_rate)/100;
                    }

                if ($diff2 < $commence_discount3)
                    {
                        $rebate1 = ($gross_amount * $commence_discount2)/100;
                    }


/***************************************************************/


            # 

                $another_discount                       = mysql_query("select * from booking_another_discount where 'season_type'='".$start_season."'");

                $rec_ano_discount                       = mysql_fetch_array($another_discount);

                $ano_night_no                               = $rec_ano_discount['night_number'];


                if ($diffmod > $ano_night_no)
                    {
                        $rebate2 = $rec_ano_discount['ano_dis_amt'];
                    }


                $another_discountend                    = mysql_query("select * from booking_another_discount where 'season_type'='".$end_season."'");

                $rec_ano_discountend                    = mysql_fetch_array($another_discountend);

                $ano_night_noend                            = $rec_ano_discountend['night_number'];


                if ($diffmodend > $ano_night_noend)
                    {
                        $rebate5 = $rec_ano_discountend['ano_dis_amt'];
                    }


                $total_discount                             = $rebate + $rebate1 + $rebate2 + $rebate4 + $rebate5;

                $final_cost                                     = $gross_amount - $total_discount;

                $noroom                                             = $total_costnew['no_of_room'];

                $diffroom                                       = $diff * $noroom;

                $roomcost                                       = $final_cost / $diffroom;

                $total1                                             = $final_cost / $diffroom;


/***************************************************************/       


            # Call the function to get the currency converted

                $final_cost_usd1                            = get_currency("USD", "USD", "$final_cost");

                $final_cost_usd                             = number_format($final_cost_usd1,2);

                $final_cost_con1                            = get_currency("USD", "$cony[currency_code]", "$final_cost");

                $final_cost_con                             = number_format($final_cost_con1,2);


/***************************************************************/       


            # 

                $actual_rate_usd1                       = get_currency("USD", "USD", "$roomcost");

                $actual_rate_usd                            = number_format($actual_rate_usd1,2);

                $actual_rate_con1                       = get_currency("USD", "$cony[currency_code]", "$roomcost");

                $actual_rate_con                            = number_format($actual_rate_con1,2);


/***************************************************************/       


            # 

                $book_cost_usd1                             = get_currency("USD", "USD", "$total_cost1");

                $book_cost_usd                              = number_format($book_cost_usd1,2);

                $tax1                                               = get_currency("USD", "USD", "$tax");

                $tax2                                               = number_format($tax1,2);


/***************************************************************/


            #

                $con1                                   = $cony[currency_code];


                if ($con1=='')
                    {
                        $con = '';
                    }

                if ($con1!='')
                    {
                        $con = $con1;
                    }


                $bedroom                                = $rate_schedulemod2['no_of_room'];

                $in_mod                                 = $check_in[0]."/".$check_in[1]."/".$check_in[2];

                $out_mod                                = $check_out[0]."/".$check_out[1]."/".$check_out[2];

                $datemod                                = $in_mod;

                $datemod1                           = $out_mod;


                $output = array
                ( 
                'final_cost_usd' => $final_cost_usd, 
                'final_cost_con' => $final_cost_con,
                'actual_rate_usd' => $actual_rate_usd,
                'actual_rate_con' => $actual_rate_con,
                'book_cost_usd' => $book_cost_usd,
                'tax' => $tax2,
                'con' => $con,
                'date' => $datemod,
                'date1' => $datemod1,
                'adult' => $adult,
                'child' => $child,
                'infant' => $infant,
                'bedroom' => $bedroom,
                'roomtype' => $roomtype3,
                'diff_night' => $diff_night
                );


                        echo json_encode($output);


            } // this is the closing tag for Line 164


/***************************************************************/


        #

             else 

            { // this tag closes on Line 762


            $total_cost                         = mysql_query("select * from booking_rate_schedule where 'rate_schedule_id'='".$gRateScheduleID."'");


            $total_costnew                  = mysql_fetch_array($total_cost);


            $roomtype1                          = mysql_query("select * from booking_master_room_type where 'room_type_id'='".$total_costnew['room_type_id']."'");


            $roomtype2                          = mysql_fetch_array($roomtype1);

            $roomtype3                          = $roomtype2['room_type'];

            $accomodation                   = $total_costnew['accomodation'];


            if ($season_type=='Low Season')
                {
                $actual_rate = $total_costnew['low_season'];
                }


            if ($season_type=='High Season')
                {
                $actual_rate = $total_costnew['high_season'];
                }


            if ($season_type=='Peak Season')
                {
                $actual_rate = $total_costnew['peak_season'];
                }


            $no_of_additional           = ($adult + $child) - $accomodation;


            if ($no_of_additional > 0)
                {
                    $add_cost           = mysql_query("select * from booking_additional_cost");
                    $add_cost1          = mysql_fetch_array($add_cost);
                    $add_cost2          = $add_cost1['additional_cost'];
                    $extra_charge   = $add_cost2 * $no_of_additional * $diff;
                }


            $total_cost1                        = ($actual_rate * $diff) + $extra_charge;


/***************************************************************/


        # Tax factor

            $tax_rates                  = mysql_query("select * from booking_tax"); 

            $tax_rate                   = mysql_fetch_array($tax_rates);

            $tax                                = ($total_cost1 * $tax_rate['tax'])/100;


/***************************************************************/


        #

            $adult1                             = $gAdult1;

            $child1                             = $gChild1;

            $infant1                            = $gInfant1;


            if ($adult1=='')
                {
                    $adult = 0;
                }


            if ($adult1!='')
                {
                    $adult = $adult1;
                }


            if ($child1=='')
                {
                    $child = 0;
                }


            if ($child1!='')
                {
                    $child = $child1;
                }


            if ($infant1=='')
                {
                    $infant = 0;
                }


            if ($infant1!='')
                {
                    $infant = $infant1;
                }


/***************************************************************/


        #

            $new_season                             = mysql_query("select * from booking_discount_night where 'season_type'='".$season_type."'");

            $new_season1                            = mysql_fetch_array($new_season);

            $night_number                       = $new_season1['night_number'];

            $discount_rate                      = $new_season1['discount_rate'];

            $commence_discount              = mysql_query("select * from booking_commencement_discount");

            $commence_discount1             = mysql_fetch_array($commence_discount);

            $commence_discount2             = $commence_discount1['discount_rate'];

            $commence_discount3             = $commence_discount1['day'];

            $gross_amount                       = $total_cost1 + $tax;

            $diff_night                             = $diff;


            if ($diff > $night_number)
                {
                    $rebate = ($gross_amount * $discount_rate)/100;
                }


            if ($diff2 < $commence_discount3)
                {
                    $rebate1 = ($gross_amount * $commence_discount2)/100;
                }


            $another_discount               = mysql_query("select * from booking_another_discount where 'season_type'='".$season_type."'");

            $rec_ano_discount               = mysql_fetch_array($another_discount);

            $ano_night_no                       = $rec_ano_discount['night_number'];


            if ($diff > $ano_night_no)
                {
                    $rebate2 = $rec_ano_discount['ano_dis_amt'];
                }


            $total_discount                     = $rebate + $rebate1 + $rebate2;

            $final_cost                             = $gross_amount - $total_discount;

            $noroom                                     = $total_costnew['no_of_room'];

            $diffroom                               = $diff * $noroom;

            $roomcost                               = $final_cost / $diffroom;

            $total1                                     = $final_cost / $diffroom;


/***************************************************************/


        # Call the function to get the currency converted

            $final_cost_usd1                    = get_currency("USD", "USD", "$final_cost");
            $final_cost_usd                     = number_format($final_cost_usd1,2);

            $final_cost_con1                    = get_currency("USD", "$cony[currency_code]", "$final_cost");
            $final_cost_con                     = number_format($final_cost_con1,2);


/***************************************************************/


        # Get original cost of room

            $actual_rate_usd1               = get_currency("USD", "USD", "$roomcost");
            $actual_rate_usd                    = number_format($actual_rate_usd1,2);

            $actual_rate_con1               = get_currency("USD", "$cony[currency_code]", "$roomcost");
            $actual_rate_con                    = number_format($actual_rate_con1,2);


/***************************************************************/


        # A

            $book_cost_usd1                     = get_currency("USD", "USD", "$total_cost1");
            $book_cost_usd                      = number_format($book_cost_usd1,2);

            $tax1                                       = get_currency("USD", "USD", "$tax");
            $tax2                                       = number_format($tax1,2);


/***************************************************************/


        #

            $con1                                       = $cony[currency_code];

            if ($con1=='')
                {
                    $con='';
                }

            if ($con1!='')
                {
                    $con = $con1;
                }


/***************************************************************/


        #

            $bedroom                                = $rate_schedulemod2['no_of_room'];

            $datemod                                = date('d M,Y',strtotime($date));
            $datemod1                           = date('d M,Y',strtotime($date1));

            $in_mod                                 = $check_in[0]."/".$check_in[1]."/".$check_in[2];
            $out_mod                                = $check_out[0]."/".$check_out[1]."/".$check_out[2];

            $datemod                                = $in_mod;
            $datemod1                           = $out_mod;


/***************************************************************/


    # Output array


            $output = array
                ( 
                    'final_cost_usd' => $final_cost_usd, 
                    'final_cost_con' => $final_cost_con,
                    'actual_rate_usd' => $actual_rate_usd,
                    'actual_rate_con' => $actual_rate_con,
                    'book_cost_usd' => $book_cost_usd,
                    'tax' => $tax2,
                    'con' => $con,
                    'date' => $datemod,
                    'date1' => $datemod1,
                    'adult' => $adult,
                    'child' => $child,
                    'infant' => $infant,
                    'bedroom' => $bedroom,
                    'roomtype' => $roomtype3,
                    'diff_night' => $diff_night
                );


            echo json_encode($output);


/***************************************************************/


            } // closing tag for Line 485
    }
?>




Aucun commentaire:

Enregistrer un commentaire