예제 #1
0
 public function placeOrder()
 {
     // set merchant timezone
     $mtid = $_SESSION['kr_merchant_id'];
     $mt_timezone = Yii::app()->functions->getOption("merchant_timezone", $mtid);
     if (!empty($mt_timezone)) {
         Yii::app()->timeZone = $mt_timezone;
     }
     /** re-check delivery address */
     if ($this->data['delivery_type'] == "delivery") {
         $functionsk = new FunctionsK();
         if (!$functionsk->reCheckDelivery($this->data, $mtid)) {
             $mt_delivery_miles = Yii::app()->functions->getOption("merchant_delivery_miles", $mtid);
             $unit = Yii::app()->functions->getOption("merchant_distance_type", $mtid);
             $this->msg = t("Sorry but this merchant delivers only with in ") . $mt_delivery_miles . "{$unit}";
             return;
         }
     }
     /** re-check delivery address */
     /*guest checkout*/
     if (isset($this->data['is_guest_checkout'])) {
         $Validator = new Validator();
         if (empty($this->data['email_address'])) {
             $this->data['email_address'] = str_replace(" ", "_", $this->data['first_name']) . Yii::app()->functions->generateRandomKey() . "@" . $_SERVER['HTTP_HOST'];
         }
         if (!($res_check = Yii::app()->functions->isClientExist($this->data['email_address']))) {
             $this->data['password'] = isset($this->data['password']) ? $this->data['password'] : '';
             if (empty($this->data['password'])) {
                 $this->data['password'] = Yii::app()->functions->generateRandomKey();
             }
             $params = array('first_name' => $this->data['first_name'], 'last_name' => $this->data['last_name'], 'email_address' => $this->data['email_address'], 'password' => md5($this->data['password']), 'street' => $this->data['street'], 'city' => $this->data['city'], 'state' => $this->data['state'], 'zipcode' => $this->data['zipcode'], 'contact_phone' => $this->data['contact_phone'], 'location_name' => $this->data['location_name'], 'date_created' => date('c'), 'ip_address' => $_SERVER['REMOTE_ADDR'], 'contact_phone' => $this->data['contact_phone']);
             if ($this->insertData("{{client}}", $params)) {
                 Yii::app()->functions->clientAutoLogin($this->data['email_address'], $this->data['password']);
             } else {
                 $Validator->msg[] = t("Something went wrong during processing your request. Please try again later.");
             }
         } else {
             $Validator->msg[] = t("Sorry but your email address already exist in our records.");
         }
         if (!$Validator->validate()) {
             $this->msg = $Validator->getErrorAsHTML();
             return;
         }
     }
     /*guest checkout*/
     $this->data['merchant_id'] = $_SESSION['kr_merchant_id'];
     $default_order_status = Yii::app()->functions->getOption("default_order_status", $_SESSION['kr_merchant_id']);
     $order_item = $_SESSION['kr_item'];
     if (is_array($order_item) && count($order_item) >= 1) {
         //dump($this->data);
         /** card fee condition */
         $card_fee = '';
         switch ($this->data['payment_opt']) {
             case "pyp":
                 if (Yii::app()->functions->isMerchantCommission($this->data['merchant_id'])) {
                     $card_fee = Yii::app()->functions->getOptionAdmin('admin_paypal_fee');
                 } else {
                     $card_fee = Yii::app()->functions->getOption('merchant_paypal_fee', $this->data['merchant_id']);
                 }
                 break;
             default:
                 break;
         }
         if (!empty($card_fee) && $card_fee >= 0.1) {
             $this->data['card_fee'] = $card_fee;
         }
         /** end card fee */
         Yii::app()->functions->displayOrderHTML($this->data, $_SESSION['kr_item']);
         if (Yii::app()->functions->code == 1) {
             //dump("<h2>RESP</h2>");
             $raw = Yii::app()->functions->details['raw'];
             if (is_array($raw) && count($raw) >= 1) {
                 $params = array('merchant_id' => $this->data['merchant_id'], 'client_id' => Yii::app()->functions->getClientId(), 'json_details' => json_encode($order_item), 'trans_type' => isset($_SESSION['kr_delivery_options']['delivery_type']) ? $_SESSION['kr_delivery_options']['delivery_type'] : '', 'payment_type' => isset($this->data['payment_opt']) ? $this->data['payment_opt'] : '', 'sub_total' => isset($raw['total']['subtotal']) ? $raw['total']['subtotal'] : '', 'tax' => isset($raw['total']['tax']) ? $raw['total']['tax'] : '', 'taxable_total' => isset($raw['total']['taxable_total']) ? $raw['total']['taxable_total'] : '', 'total_w_tax' => isset($raw['total']['total']) ? $raw['total']['total'] : '', 'delivery_charge' => isset($raw['total']['delivery_charges']) ? $raw['total']['delivery_charges'] : '', 'delivery_date' => isset($_SESSION['kr_delivery_options']['delivery_date']) ? $_SESSION['kr_delivery_options']['delivery_date'] : '', 'delivery_time' => isset($_SESSION['kr_delivery_options']['delivery_time']) ? $_SESSION['kr_delivery_options']['delivery_time'] : '', 'delivery_asap' => isset($_SESSION['kr_delivery_options']['delivery_asap']) ? $_SESSION['kr_delivery_options']['delivery_asap'] : '', 'date_created' => date('c'), 'ip_address' => $_SERVER['REMOTE_ADDR'], 'delivery_instruction' => isset($this->data['delivery_instruction']) ? $this->data['delivery_instruction'] : '', 'cc_id' => isset($this->data['cc_id']) ? $this->data['cc_id'] : '', 'order_change' => isset($this->data['order_change']) ? $this->data['order_change'] : '', 'payment_provider_name' => isset($this->data['payment_provider_name']) ? $this->data['payment_provider_name'] : '');
                 /*FIXED ORDER STATUS*/
                 if ($this->data['payment_opt'] == "cod" || $this->data['payment_opt'] == "pyr" || $this->data['payment_opt'] == "ccr") {
                     if (!empty($default_order_status)) {
                         $params['status'] = $default_order_status;
                     } else {
                         $params['status'] = "pending";
                     }
                 } else {
                     $params['status'] = initialStatus();
                 }
                 if ($this->data['payment_opt'] == "obd") {
                     $params['status'] = "pending";
                 }
                 /*PROMO*/
                 if (isset($raw['total']['discounted_amount'])) {
                     if ($raw['total']['discounted_amount'] >= 0.1) {
                         $params['discounted_amount'] = $raw['total']['discounted_amount'];
                         $params['discount_percentage'] = $raw['total']['merchant_discount_amount'];
                     }
                 }
                 /*VOUCHER*/
                 $has_voucher = false;
                 if (isset($_SESSION['voucher_code'])) {
                     if (is_array($_SESSION['voucher_code'])) {
                         $params['voucher_amount'] = $_SESSION['voucher_code']['amount'];
                         $params['voucher_code'] = $_SESSION['voucher_code']['voucher_name'];
                         $params['voucher_type'] = $_SESSION['voucher_code']['voucher_type'];
                         $has_voucher = true;
                     }
                 }
                 /** add tips */
                 $params['cart_tip_percentage'] = isset($this->data['cart_tip_percentage']) ? $this->data['cart_tip_percentage'] : '';
                 $params['cart_tip_value'] = isset($this->data['cart_tip_value']) ? $this->data['cart_tip_value'] : '';
                 /*if (isset($this->data['cart_tip_value'])){
                   	if (is_numeric($this->data['cart_tip_value'])){
                   		$params['total_w_tax']=$params['total_w_tax']+$this->data['cart_tip_value'];
                   	}                        
                   }*/
                 /*Commission*/
                 if (Yii::app()->functions->isMerchantCommission($this->data['merchant_id'])) {
                     $admin_commision_ontop = Yii::app()->functions->getOptionAdmin('admin_commision_ontop');
                     if ($com = Yii::app()->functions->getMerchantCommission($this->data['merchant_id'])) {
                         $params['percent_commision'] = $com;
                         $params['total_commission'] = $com / 100 * $params['total_w_tax'];
                         $params['merchant_earnings'] = $params['total_w_tax'] - $params['total_commission'];
                         if ($admin_commision_ontop == 1) {
                             $params['total_commission'] = $com / 100 * $params['sub_total'];
                             $params['commision_ontop'] = $admin_commision_ontop;
                             $params['merchant_earnings'] = $params['sub_total'] - $params['total_commission'];
                         }
                     }
                     /** check if merchant commission is fixed  */
                     $merchant_com_details = Yii::app()->functions->getMerchantCommissionDetails($this->data['merchant_id']);
                     if ($merchant_com_details['commision_type'] == "fixed") {
                         $params['percent_commision'] = $merchant_com_details['percent_commision'];
                         $params['total_commission'] = $merchant_com_details['percent_commision'];
                         $params['merchant_earnings'] = $params['total_w_tax'] - $merchant_com_details['percent_commision'];
                         if ($admin_commision_ontop == 1) {
                             $params['merchant_earnings'] = $params['sub_total'] - $merchant_com_details['percent_commision'];
                         }
                     }
                 }
                 /** end commission condition*/
                 // fixed packaging by saving the packaging charge to db
                 $merchant_packaging_charge = Yii::app()->functions->getOption('merchant_packaging_charge', $mtid);
                 if ($merchant_packaging_charge > 0) {
                     $params['packaging'] = $merchant_packaging_charge;
                     /** if packaging is incremental*/
                     if (Yii::app()->functions->getOption("merchant_packaging_increment", $mtid) == 2) {
                         $total_cart_item = 0;
                         foreach ($raw['item'] as $cart_item_x) {
                             $total_cart_item += $cart_item_x['qty'];
                         }
                         $params['packaging'] = $total_cart_item * $merchant_packaging_charge;
                     }
                 }
                 /** card fee */
                 if (!empty($card_fee) && $card_fee >= 0.1) {
                     $params['card_fee'] = $card_fee;
                 }
                 /*if has address book selected*/
                 if (isset($this->data['address_book_id'])) {
                     if ($address_book = Yii::app()->functions->getAddressBookByID($this->data['address_book_id'])) {
                         $this->data['street'] = $address_book['street'];
                         $this->data['city'] = $address_book['city'];
                         $this->data['state'] = $address_book['state'];
                         $this->data['zipcode'] = $address_book['zipcode'];
                         $this->data['location_name'] = $address_book['location_name'];
                     }
                 }
                 $country_code = '';
                 $country_name = '';
                 if (Yii::app()->functions->getOptionAdmin('website_enabled_map_address') == 2) {
                     if (isset($this->data['map_address_toogle'])) {
                         if ($this->data['map_address_toogle'] == 2) {
                             $geo_res = geoCoding($this->data['map_address_lat'], $this->data['map_address_lng']);
                             if ($geo_res) {
                                 //dump($geo_res);
                                 $this->data['street'] = isset($geo_res['street_number']) ? $geo_res['street_number'] . " " : '';
                                 $this->data['street'] .= isset($geo_res['street']) ? $geo_res['street'] . " " : '';
                                 $this->data['street'] .= isset($geo_res['street2']) ? $geo_res['street2'] . " " : '';
                                 $this->data['city'] = $geo_res['locality'];
                                 $this->data['state'] = $geo_res['admin_1'];
                                 $this->data['zipcode'] = isset($geo_res['postal_code']) ? $geo_res['postal_code'] : '';
                                 $country_code = $geo_res['country_code'];
                                 $country_name = $geo_res['country'];
                             } else {
                                 $this->msg = t("Sorry but something wrong when geocoding your address");
                                 return false;
                             }
                         }
                     }
                 }
                 /*dump($country_code);
                   dump($this->data);
                   dump($params);*/
                 /** check if item is taxable*/
                 if (Yii::app()->functions->getOption("merchant_tax_charges", $mtid) == 2) {
                     $params['donot_apply_tax_delivery'] = 2;
                 }
                 if ($this->insertData("{{order}}", $params)) {
                     $order_id = Yii::app()->db->getLastInsertID();
                     /** add delivery address */
                     if ($this->data['delivery_type'] == "delivery") {
                         $params_address = array('order_id' => $order_id, 'client_id' => Yii::app()->functions->getClientId(), 'street' => isset($this->data['street']) ? $this->data['street'] : '', 'city' => isset($this->data['city']) ? $this->data['city'] : '', 'state' => isset($this->data['state']) ? $this->data['state'] : '', 'zipcode' => isset($this->data['zipcode']) ? $this->data['zipcode'] : '', 'location_name' => isset($this->data['location_name']) ? $this->data['location_name'] : '', 'country' => Yii::app()->functions->adminCountry(), 'date_created' => date('c'), 'ip_address' => $_SERVER['REMOTE_ADDR'], 'contact_phone' => $this->data['contact_phone']);
                         if (!empty($country_name)) {
                             $params_address['country'] = $country_name;
                         }
                         $this->insertData("{{order_delivery_address}}", $params_address);
                         /** quick update mobile*/
                         $params_mobile = array('contact_phone' => $this->data['contact_phone'], 'location_name' => isset($this->data['location_name']) ? $this->data['location_name'] : '');
                         $this->updateData("{{client}}", $params_mobile, 'client_id', Yii::app()->functions->getClientId());
                     }
                     /** save to address book*/
                     if ($this->data['saved_address'] == 2) {
                         $sql_up = "UPDATE {{address_book}}\r\n\t\t\t\t\t     \t\tSET as_default='1' \t     \t\t\r\n\t\t\t\t\t     \t\t";
                         $this->qry($sql_up);
                         $params_i = array('client_id' => Yii::app()->functions->getClientId(), 'street' => $this->data['street'], 'city' => $this->data['city'], 'state' => $this->data['state'], 'zipcode' => $this->data['zipcode'], 'location_name' => $this->data['location_name'], 'date_created' => date('c'), 'ip_address' => $_SERVER['REMOTE_ADDR'], 'country_code' => Yii::app()->functions->adminCountry(true), 'as_default' => 2);
                         $this->insertData("{{address_book}}", $params_i);
                     }
                     /*VOUCHER*/
                     if ($has_voucher == TRUE) {
                         Yii::app()->functions->updateVoucher($_SESSION['voucher_code']['voucher_code'], Yii::app()->functions->getClientId(), $order_id);
                     }
                     foreach ($raw['item'] as $val) {
                         $params_order_details = array('order_id' => $order_id, 'client_id' => Yii::app()->functions->getClientId(), 'item_id' => isset($val['item_id']) ? $val['item_id'] : '', 'item_name' => isset($val['item_name']) ? $val['item_name'] : '', 'order_notes' => isset($val['order_notes']) ? $val['order_notes'] : '', 'normal_price' => isset($val['normal_price']) ? $val['normal_price'] : '', 'discounted_price' => isset($val['discounted_price']) ? $val['discounted_price'] : '', 'size' => isset($val['size_words']) ? $val['size_words'] : '', 'qty' => isset($val['qty']) ? $val['qty'] : '', 'addon' => isset($val['sub_item']) ? json_encode($val['sub_item']) : '', 'cooking_ref' => isset($val['cooking_ref']) ? $val['cooking_ref'] : '', 'ingredients' => isset($val['ingredients']) ? json_encode($val['ingredients']) : '', 'non_taxable' => isset($val['non_taxable']) ? $val['non_taxable'] : 1);
                         $this->insertData("{{order_details}}", $params_order_details);
                     }
                     $this->code = 1;
                     /*if( $this->data['payment_opt'] =="pyp"){
                     			 $this->msg=Yii::t("default","Please wait while we redirect you to paypal.");
                     		} else if ($this->data['payment_opt'] =="stp") {
                     			$this->msg=Yii::t("default","Please wait while we connect you to Stripe.");	
                     		} else if ($this->data['payment_opt'] =="mcd") {
                     			$this->msg=Yii::t("default","Please wait while we connect you to Mercadopago.");
                     		} else if ($this->data['payment_opt'] =="ide") {
                     			$this->msg=Yii::t("default","Please wait while we connect you to Sisow.");	
                     		} else $this->msg=Yii::t("default","Your order has been placed.");*/
                     switch ($this->data['payment_opt']) {
                         case "cod":
                         case "ccr":
                             $this->msg = Yii::t("default", "Your order has been placed.");
                             break;
                         case "obd":
                             /** Send email if payment type is Offline bank deposit*/
                             $functionsk = new FunctionsK();
                             $functionsk->MerchantSendBankInstruction($mtid, $params['total_w_tax'], $order_id);
                             $this->msg = Yii::t("default", "Your order has been placed.");
                             break;
                         default:
                             $this->msg = Yii::t("default", "Please wait while we redirect...");
                             break;
                     }
                     $this->details = array('order_id' => $order_id, 'payment_type' => $this->data['payment_opt']);
                     //Yii::app()->functions->updateClient($this->data);
                 } else {
                     $this->msg = Yii::t("default", "ERROR: Cannot insert records.");
                 }
             } else {
                 $this->msg = Yii::t("default", "ERROR: Something went wrong");
             }
         } else {
             $this->msg = Yii::app()->functions->msg;
         }
     } else {
         $this->msg = Yii::t("default", "Sorry but your order is empty");
     }
 }