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 pjActionPrice()
 {
     $this->checkLogin();
     if ($this->isAdmin() || $this->isEditor()) {
         if (isset($_POST['price_update'])) {
             $pjPriceModel = pjPriceModel::factory();
             $pjPriceModel->where('location_id', $_POST['location_id'])->eraseAll();
             if (isset($_POST['price']) && count($_POST['price']) > 0) {
                 foreach ($_POST['price'] as $k => $price) {
                     if ((double) $_POST['total_from'][$k] >= 0 && (double) $_POST['total_to'][$k] > 0 && (double) $_POST['total_from'][$k] <= (double) $_POST['total_to'][$k]) {
                         $pjPriceModel->reset()->setAttributes(array('location_id' => $_POST['location_id'], 'total_from' => $_POST['total_from'][$k], 'total_to' => $_POST['total_to'][$k], 'price' => $_POST['price'][$k]))->insert();
                     }
                 }
             }
             pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminLocations&action=pjActionPrice&id=" . $_POST['location_id'] . "&err=AL09");
         } else {
             $this->set('arr', pjPriceModel::factory()->where('location_id', $_GET['id'])->orderBy("t1.total_from ASC, t1.total_to ASC")->findAll()->getData());
             $this->appendJs('jquery.validate.min.js', PJ_THIRD_PARTY_PATH . 'validate/');
             $this->appendJs('pjAdminLocations.js');
         }
     } else {
         $this->set('status', 2);
     }
 }
 public function pjActionSetTypes()
 {
     $this->setAjax(true);
     if ($this->isXHR()) {
         if (isset($_POST['loadTypes'])) {
             $this->_set('type', $_POST['type']);
             $this->_set('user_id', $_POST['user_id']);
             if ($_POST['type'] == 'delivery') {
                 $this->_set('d_date', $_POST['d_date']);
                 $this->_set('d_hour', $_POST['d_hour']);
                 $this->_set('d_minute', $_POST['d_minute']);
                 $this->_set('d_location_id', $_POST['d_location_id']);
                 $this->_set('d_address_1', isset($_POST['d_address_1']) ? $_POST['d_address_1'] : NULL);
                 $this->_set('d_address_2', isset($_POST['d_address_2']) ? $_POST['d_address_2'] : NULL);
                 $this->_set('d_country_id', isset($_POST['d_country_id']) ? $_POST['d_country_id'] : NULL);
                 $this->_set('d_state', isset($_POST['d_state']) ? $_POST['d_state'] : NULL);
                 $this->_set('d_city', isset($_POST['d_city']) ? $_POST['d_city'] : NULL);
                 $this->_set('d_zip', isset($_POST['d_zip']) ? $_POST['d_zip'] : NULL);
                 $this->_set('d_notes', isset($_POST['d_notes']) ? $_POST['d_notes'] : NULL);
                 $arr = pjPriceModel::factory()->where('t1.location_id', $this->_get('d_location_id'))->where('t1.total_from <= ' . $this->_get('price'))->where('t1.total_to >= ' . $this->_get('price'))->limit(1)->findAll()->getData();
                 $delivery = 0;
                 if (count($arr) === 1) {
                     $delivery = $arr[0]['price'];
                 }
                 $this->_set('delivery', $delivery);
             } else {
                 $this->_set('p_location_id', $_POST['p_location_id']);
                 $this->_set('p_date', $_POST['p_date']);
                 $this->_set('p_hour', $_POST['p_hour']);
                 $this->_set('p_minute', $_POST['p_minute']);
             }
         }
         $this->_unset('voucher_code');
         $this->_unset('voucher_discount');
         $this->_unset('voucher_type');
     }
     exit;
 }