public function pjActionUpdate()
 {
     $this->checkLogin();
     if ($this->isAdmin() || $this->isEditor()) {
         if (isset($_POST['extra_update'])) {
             pjExtraModel::factory()->where('id', $_POST['id'])->limit(1)->modifyAll($_POST);
             if (isset($_POST['i18n'])) {
                 pjMultiLangModel::factory()->updateMultiLang($_POST['i18n'], $_POST['id'], 'pjExtra', 'data');
             }
             pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminExtras&action=pjActionIndex&err=AE01");
         } else {
             $arr = pjExtraModel::factory()->find($_GET['id'])->getData();
             if (count($arr) === 0) {
                 pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminExtras&action=pjActionIndex&err=AE08");
             }
             $arr['i18n'] = pjMultiLangModel::factory()->getMultiLang($arr['id'], 'pjExtra');
             $this->set('arr', $arr);
             $locale_arr = pjLocaleModel::factory()->select('t1.*, t2.file')->join('pjLocaleLanguage', 't2.iso=t1.language_iso', 'left')->where('t2.file IS NOT NULL')->orderBy('t1.sort ASC')->findAll()->getData();
             $lp_arr = array();
             foreach ($locale_arr as $item) {
                 $lp_arr[$item['id'] . "_"] = $item['file'];
             }
             $this->set('lp_arr', $locale_arr);
             $this->set('locale_str', pjAppController::jsonEncode($lp_arr));
             $this->appendJs('jquery.validate.min.js', PJ_THIRD_PARTY_PATH . 'validate/');
             $this->appendJs('jquery.multilang.js', PJ_FRAMEWORK_LIBS_PATH . 'pj/js/');
             $this->appendJs('jquery.tipsy.js', PJ_THIRD_PARTY_PATH . 'tipsy/');
             $this->appendCss('jquery.tipsy.css', PJ_THIRD_PARTY_PATH . 'tipsy/');
             $this->appendJs('pjAdminExtras.js');
         }
     } else {
         $this->set('status', 2);
     }
 }
 public function pjActionGetTotal()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $price = 0;
         $subtotal = 0;
         $delivery = 0;
         $tax = 0;
         $total = 0;
         $pjProductModel = pjProductModel::factory();
         $pjProductPriceModel = pjProductPriceModel::factory();
         $pjExtraModel = pjExtraModel::factory();
         $product_arr = $pjProductModel->whereIn("t1.id", $_POST['product_id'])->findAll()->getData();
         $extra_arr = $pjExtraModel->findAll()->getData();
         foreach ($_POST['product_id'] as $hash => $product_id) {
             foreach ($product_arr as $product) {
                 if ($product['id'] == $product_id) {
                     $_price = 0;
                     $extra_price = 0;
                     if ($product['set_different_sizes'] == 'T') {
                         $price_arr = $pjProductPriceModel->reset()->find($_POST['price_id'][$hash])->getData();
                         if ($price_arr) {
                             $_price = $price_arr['price'];
                         }
                     } else {
                         $_price = $product['price'];
                     }
                     $product_price = $_price * $_POST['cnt'][$hash];
                     if (isset($_POST['extra_id']) && isset($_POST['extra_id'][$hash])) {
                         foreach ($_POST['extra_id'][$hash] as $oi_id => $extra_id) {
                             if (isset($_POST['extra_cnt'][$hash][$oi_id]) && (int) $_POST['extra_cnt'][$hash][$oi_id] > 0) {
                                 foreach ($extra_arr as $extra) {
                                     if ($extra['id'] == $extra_id) {
                                         $extra_price += $extra['price'] * $_POST['extra_cnt'][$hash][$oi_id];
                                         break;
                                     }
                                 }
                             }
                         }
                     }
                     $_price = $product_price + $extra_price;
                     $price += $_price;
                     break;
                 }
             }
         }
         if ($_POST['type'] == 'delivery' && isset($_POST['d_location_id']) && (int) $_POST['d_location_id'] > 0) {
             $arr = pjPriceModel::factory()->where("t1.location_id", $_POST['d_location_id'])->where("(t1.total_from <= {$price})")->where("(t1.total_to >= {$price})")->findAll()->limit(1)->getData();
             if (count($arr) === 1) {
                 $delivery = $arr[0]['price'];
             }
         }
         $discount = 0;
         if ($_POST['voucher_code'] !== false) {
             if ($_POST['type'] == 'delivery') {
                 $resp = pjAppController::getDiscount($_POST, $this->option_arr);
                 if ($resp['code'] == 200) {
                     $voucher_discount = $resp['voucher_discount'];
                     switch ($resp['voucher_type']) {
                         case 'percent':
                             $discount = ($subtotal + $delivery) * $voucher_discount / 100;
                             break;
                         case 'amount':
                             $discount = $voucher_discount;
                             break;
                     }
                 }
             }
         }
         $subtotal = $price + $delivery - $discount;
         if (!empty($this->option_arr['o_tax_payment'])) {
             $tax = $subtotal * $this->option_arr['o_tax_payment'] / 100;
         }
         $total = $subtotal + $tax;
         $price = number_format($price, 2);
         $discount = number_format($discount, 2);
         $delivery = number_format($delivery, 2);
         $subtotal = number_format($subtotal, 2);
         $tax = number_format($tax, 2);
         $total = number_format($total, 2);
         pjAppController::jsonResponse(compact('price', 'discount', 'delivery', 'subtotal', 'tax', 'total'));
     }
     exit;
 }
 public function pjActionGetLocale()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         if (isset($_GET['locale']) && (int) $_GET['locale'] > 0) {
             pjAppController::setFields($_GET['locale']);
             $this->set('category_arr', pjCategoryModel::factory()->select('t1.*, t2.content AS name')->join('pjMultiLang', "t2.model='pjCategory' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='" . pjObject::escapeString($_GET['locale']) . "'", 'left outer')->where('t1.status', 'T')->orderBy('`order` ASC')->findAll()->getData());
             $this->set('extra_arr', pjExtraModel::factory()->select('t1.*, t2.content AS name')->join('pjMultiLang', "t2.model='pjExtra' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='" . pjObject::escapeString($_GET['locale']) . "'", 'left outer')->orderBy('name ASC')->findAll()->getData());
         }
     }
 }
 public function pjActionSaveOrder()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         $cart = $this->_get('cart');
         $pjOrderModel = pjOrderModel::factory();
         $STORAGE = $_SESSION[$this->defaultStore];
         $FORM = $_SESSION[$this->defaultForm];
         $data = array();
         $data['status'] = $this->option_arr['o_booking_status'];
         $data['price'] = $this->_get('price');
         $data['price_delivery'] = $this->_get('delivery');
         $data['discount'] = $this->_get('discount');
         $data['subtotal'] = $this->_get('subtotal');
         $data['tax'] = $this->_get('tax');
         $data['total'] = $this->_get('total');
         $data['uuid'] = time();
         $data['ip'] = $_SERVER['REMOTE_ADDR'];
         $data['user_id'] = $this->_get('user_id');
         $data['location_id'] = $this->_get('p_location_id');
         switch ($this->_get('type')) {
             case 'pickup':
                 $data['p_dt'] = pjUtil::formatDate($this->_get('p_date'), $this->option_arr['o_date_format']) . " " . $this->_get('p_hour') . ":" . $this->_get('p_minute') . ":00";
                 unset($STORAGE['d_address_1']);
                 unset($STORAGE['d_address_2']);
                 unset($STORAGE['d_country_id']);
                 unset($STORAGE['d_state']);
                 unset($STORAGE['d_city']);
                 unset($STORAGE['d_zip']);
                 unset($STORAGE['d_notes']);
                 unset($STORAGE['d_date']);
                 unset($STORAGE['d_hour']);
                 unset($STORAGE['d_minute']);
                 break;
             case 'delivery':
                 $data['d_dt'] = pjUtil::formatDate($this->_get('d_date'), $this->option_arr['o_date_format']) . " " . $this->_get('d_hour') . ":" . $this->_get('d_minute') . ":00";
                 unset($STORAGE['p_date']);
                 unset($STORAGE['p_hour']);
                 unset($STORAGE['p_minute']);
                 break;
         }
         unset($STORAGE['cart']);
         unset($STORAGE['subtotal']);
         unset($STORAGE['total']);
         unset($STORAGE['delivery']);
         $payment = 'none';
         if (isset($FORM['payment_method'])) {
             if ($FORM['payment_method'] == 'creditcard') {
                 $data['cc_exp'] = $FORM['cc_exp_month'] . "/" . $FORM['cc_exp_year'];
             } else {
                 unset($FORM['cc_type']);
                 unset($FORM['cc_num']);
                 unset($FORM['cc_exp_month']);
                 unset($FORM['cc_exp_year']);
                 unset($FORM['cc_code']);
             }
             $payment = $FORM['payment_method'];
         }
         $is_new_client = false;
         $update_client = false;
         $pjClientModel = pjClientModel::factory();
         $data['client_id'] = ':NULL';
         if ($this->isFrontLogged()) {
             $cnt = $pjClientModel->where('t1.id', $this->getClientId())->findCount()->getData();
             if ($cnt == 0) {
                 $is_new_client = true;
             } else {
                 $update_client = true;
             }
         } else {
             $is_new_client = true;
         }
         if ($is_new_client == true) {
             $c_data = array();
             $c_data['status'] = 'T';
             $c_data['user_id'] = $this->_get('user_id');
             $c_data['c_password'] = pjUtil::getRandomPassword(6);
             $c_data = array_merge($FORM, $c_data);
             $client_id = $pjClientModel->reset()->setAttributes($c_data)->insert()->getInsertId();
             if ($client_id !== false && (int) $client_id > 0) {
                 $data['client_id'] = $client_id;
                 if ($this->isFrontLogged()) {
                     $client = $pjClientModel->reset()->find($client_id)->getData();
                     unset($_SESSION[$this->defaultClient]);
                     $_SESSION[$this->defaultClient] = $client;
                 }
                 pjFront::pjActionConfirmSend($this->option_arr, $c_data, PJ_SALT, 'account');
             }
         }
         if ($update_client == true) {
             if (isset($FORM['update_address'])) {
                 $c_data = array();
                 if (isset($FORM['c_address_1'])) {
                     $c_data['c_address_1'] = $FORM['c_address_1'];
                 }
                 if (isset($FORM['c_address_2'])) {
                     $c_data['c_address_1'] = $FORM['c_address_1'];
                 }
                 if (isset($FORM['c_country'])) {
                     $c_data['c_country'] = $FORM['c_country'];
                 }
                 if (isset($FORM['c_state'])) {
                     $c_data['c_state'] = $FORM['c_state'];
                 }
                 if (isset($FORM['c_city'])) {
                     $c_data['c_city'] = $FORM['c_city'];
                 }
                 if (isset($FORM['c_zip'])) {
                     $c_data['c_zip'] = $FORM['c_zip'];
                 }
                 $pjClientModel->reset()->where('id', $this->getClientId())->limit(1)->modifyAll($c_data);
             }
             if (isset($FORM['update_details'])) {
                 $c_data = array();
                 if (isset($FORM['c_title'])) {
                     $c_data['c_title'] = $FORM['c_title'];
                 }
                 if (isset($FORM['c_name'])) {
                     $c_data['c_name'] = $FORM['c_name'];
                 }
                 if (isset($FORM['c_email'])) {
                     $c_data['c_email'] = $FORM['c_email'];
                 }
                 if (isset($FORM['c_phone'])) {
                     $c_data['c_phone'] = $FORM['c_phone'];
                 }
                 if (isset($FORM['c_company'])) {
                     $c_data['c_company'] = $FORM['c_company'];
                 }
                 if (isset($FORM['c_notes'])) {
                     $c_data['c_notes'] = $FORM['c_notes'];
                 }
                 $pjClientModel->reset()->where('id', $this->getClientId())->limit(1)->modifyAll($c_data);
             }
             $client = $pjClientModel->reset()->find($this->getClientId())->getData();
             unset($_SESSION[$this->defaultClient]);
             $_SESSION[$this->defaultClient] = $client;
             $data['client_id'] = $this->getClientId();
         }
         $data = array_merge($STORAGE, $FORM, $data);
         $order_id = $pjOrderModel->setAttributes($data)->insert()->getInsertId();
         if ($order_id !== false && (int) $order_id > 0) {
             $pjOrderItemModel = pjOrderItemModel::factory();
             $pjProductPriceModel = pjProductPriceModel::factory();
             $pjProductModel = pjProductModel::factory();
             $pjExtraModel = pjExtraModel::factory();
             foreach ($cart as $item) {
                 $price_id = ':NULL';
                 $price = 0;
                 if (!empty($item['price_id'])) {
                     $price_arr = $pjProductPriceModel->find($item['price_id'])->getData();
                     if ($price_arr) {
                         $price_id = $price_arr['id'];
                         $price = $price_arr['price'];
                     }
                 } else {
                     $price_arr = $pjProductModel->reset()->find($item['product_id'])->getData();
                     if (!empty($price_arr)) {
                         $price = $price_arr['price'];
                     }
                 }
                 $hash = md5(uniqid(rand(), true));
                 $oid = $pjOrderItemModel->reset()->setAttributes(array('order_id' => $order_id, 'foreign_id' => $item['product_id'], 'type' => 'product', 'price_id' => $price_id, 'price' => $price, 'hash' => $hash, 'cnt' => $item['cnt']))->insert();
                 foreach ($item['extras'] as $extra_id => $extra_cnt) {
                     if ($extra_cnt > 0) {
                         $extra_price = 0;
                         $extra_arr = $pjExtraModel->reset()->find($extra_id)->getData();
                         if (!empty($extra_arr) && !empty($extra_arr['price'])) {
                             $extra_price = $extra_arr['price'];
                         }
                         $pjOrderItemModel->reset()->setAttributes(array('order_id' => $order_id, 'foreign_id' => $extra_id, 'type' => 'extra', 'price_id' => ':NULL', 'price' => $extra_price, 'hash' => $hash, 'cnt' => $extra_cnt))->insert();
                     }
                 }
             }
             $order_arr = $pjOrderModel->reset()->join('pjClient', "t2.id=t1.client_id", 'left outer')->select('t1.*, t2.c_title, t2.c_email, t2.c_name, t2.c_phone, t2.c_company, t2.c_address_1, t2.c_address_2, t2.c_country, t2.c_state, t2.c_city, t2.c_zip, t2.c_notes')->find($order_id)->getData();
             $pdata = array();
             $pdata['order_id'] = $order_id;
             $pdata['payment_method'] = $payment;
             $pdata['payment_type'] = 'online';
             $pdata['amount'] = $order_arr['total'];
             $pdata['status'] = 'notpaid';
             pjOrderPaymentModel::factory()->setAttributes($pdata)->insert();
             pjAppController::addOrderDetails($order_arr, $this->getLocaleId());
             pjFront::pjActionConfirmSend($this->option_arr, $order_arr, PJ_SALT, 'confirm');
             unset($_SESSION[$this->defaultStore]);
             unset($_SESSION[$this->defaultForm]);
             unset($_SESSION[$this->defaultClient]);
             //Redirect to Credit card payment url.
             if ($payment == 'creditcard') {
                 $cardData = $_SESSION['cardData'];
                 $params = 'amount=' . base64_encode($cardData['total']) . '&oid=' . $cardData['clover_order_id'] . '&mid=' . $cardData['clover_mid'] . '&at=' . $cardData['clover_access_token'] . '&uid=' . base64_encode($cardData['o_user_id']) . '&mname=' . base64_encode($cardData['o_m_name']);
                 $url = PJ_INSTALL_URL . 'payment/creditcard.php?' . $params;
                 $json = array('code' => 200, 'text' => '', 'order_id' => $order_id, 'payment' => $payment, 'path' => $url);
             } else {
                 $json = array('code' => 200, 'text' => '', 'order_id' => $order_id, 'payment' => $payment, 'path' => 'cash');
             }
         } else {
             $json = array('code' => 100, 'text' => '');
         }
         pjAppController::jsonResponse($json);
     }
 }