/** * calculateDiscountAndDelivery */ protected function calculateDiscountAndDelivery(&$basket) { $Delivery = new ecommerce_delivery(); // order exists? if ($this->orderFinished($basket['id'], $this->GET['order_id'])) { $Promotion = new ecommerce_promotion(); $Promotion->setCacheable(false); // get data from database $code = $Promotion->getPromotionCodeForOrder($this->GET['order_id']); $verify_code = false; $this->Basket->calculateBasketDiscount($basket, $code, $verify_code); $basket['delivery'] = $Delivery->getDeliveryByOrderId($this->GET['order_id']); } else { // calculate data $code = $_SESSION['promotion_code']; $verify_code = true; $promotion_detail = $this->Basket->calculateBasketDiscount($basket, $code, $verify_code); $this->Basket->saveDiscount($basket); if ($this->guest_customer) { $basket['delivery'] = $Delivery->calculateDeliveryForCountry($basket, $this->delivery_options['carrier_id'], $this->delivery_country, $promotion_detail); } else { $basket['delivery'] = $Delivery->calculateDelivery($basket, $this->delivery_options['carrier_id'], $this->delivery_address_id, $promotion_detail); } // this only applies when using wizard checkout if ($basket['delivery'] == false) { $this->redirectToDeliveryOptionsPage(); } } }
/** * main action */ public function mainAction() { if (!is_numeric($this->GET['promotion_id'])) { msg("Onxshop_Controller_Component_Ecommerce_Gift_Voucher_Generate: promotion_id isn't numeric"); return false; } $promotion_id = $this->GET['promotion_id']; if ($gift_voucher_product_id = $this->getGiftVoucherProductId($order_id)) { require_once 'models/ecommerce/ecommerce_promotion.php'; $Promotion = new ecommerce_promotion(); $Promotion->setCacheable(false); $promotion_data = $Promotion->getDetail($promotion_id); if ($promotion_data) { $gift_voucher_directory = ONXSHOP_PROJECT_DIR . "var/vouchers/"; $gift_voucher_filename = "{$promotion_data['code_pattern']}.png"; $gift_voucher_filename_fullpath = $gift_voucher_directory . $gift_voucher_filename; $voucher_data = array(); $voucher_data['recipient_name'] = $promotion_data['other_data']['recipient_name']; $voucher_data['recipient_email'] = $promotion_data['other_data']['recipient_email']; $voucher_data['message'] = $promotion_data['other_data']['message']; $voucher_data['sender_name'] = $promotion_data['other_data']['sender_name']; if ($promotion_data['other_data']['delivery_date']) { $voucher_data['delivery_date'] = $promotion_data['other_data']['delivery_date']; } $this->sendEmail($promotion_data, $voucher_data, $gift_voucher_filename); msg("Email sent"); } } return true; }
/** * generateSingleVoucher */ public function generateSingleVoucher($voucher_basket_item) { if (!is_array($voucher_basket_item)) { return false; } $voucher_data = array(); $voucher_data['variety_id'] = $voucher_basket_item['product_variety_id']; $voucher_data['recipient_name'] = $voucher_basket_item['other_data']['recipient_name']; $voucher_data['recipient_email'] = $voucher_basket_item['other_data']['recipient_email']; $voucher_data['message'] = $voucher_basket_item['other_data']['message']; $voucher_data['sender_name'] = $voucher_basket_item['other_data']['sender_name']; if ($voucher_basket_item['other_data']['delivery_date']) { $voucher_data['delivery_date'] = $voucher_basket_item['other_data']['delivery_date']; } if (!$this->validateData($voucher_data)) { msg("Voucher data are not valid", 'error'); return false; } /** * create discount code */ $code_pattern_base = "GIFT-{$voucher_basket_item['id']}" . '-'; $promotion_data = array(); $promotion_data['type'] = 4; // Gift Voucher $promotion_data['code_pattern'] = $code_pattern_base . $this->randomCode(); $promotion_data['title'] = $promotion_data['code_pattern']; $promotion_data['discount_percentage_value'] = 0; $promotion_data['discount_fixed_value'] = $voucher_basket_item['total']; $promotion_data['uses_per_coupon'] = $voucher_basket_item['quantity']; $promotion_data['other_data'] = $voucher_basket_item['other_data']; $promotion_data['publish'] = 1; $promotion_data['generated_by_order_id'] = $this->GET['order_id']; require_once 'models/ecommerce/ecommerce_promotion.php'; $Promotion = new ecommerce_promotion(); $Promotion->setCacheable(false); //TODO: check if the code wasn't generated before for the same order if ($Promotion->checkCodeMatchPartially($code_pattern_base)) { msg("Code {$code_pattern_base}* was previously generated", 'error'); return false; } //preg_match("/GIFT-{$voucher_basket_item['id']}/", $all_patterns_list) if ($promotion_id = $Promotion->addPromotion($promotion_data)) { msg("Promotion code {$promotion_data['code_pattern']} generated as promotion ID {$promotion_id}", 'ok', 1); } else { msg('Promotion code generation failed', 'error'); //return false; } /** * create the voucher file */ $url = "http://{$_SERVER['SERVER_NAME']}/request/sys/html5.node/site/print.component/ecommerce/gift_voucher~voucher_code={$promotion_data['code_pattern']}~"; $gift_voucher_directory = ONXSHOP_PROJECT_DIR . "var/vouchers/"; $gift_voucher_filename = "{$promotion_data['code_pattern']}.png"; $gift_voucher_filename_fullpath = $gift_voucher_directory . $gift_voucher_filename; //check directory exits if (!is_dir($gift_voucher_directory)) { mkdir($gift_voucher_directory); } $shell_command = "wkhtmltoimage {$url} {$gift_voucher_filename_fullpath}"; if ($result = local_exec($shell_command)) { msg("File {$gift_voucher_filename_fullpath} generated by wkhtmltoimage", 'ok', 1); } /** * send email * postpone if delivery_date is set */ if (preg_match("/[0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{4}/", $voucher_data['delivery_date'])) { $this->postponeDelivery($promotion_id, $voucher_data['delivery_date']); } else { $this->sendEmail($promotion_data, $voucher_data, $gift_voucher_filename); } return true; }
/** * apply discount code * requires basket full detail with calculated sub totals */ public function calculateBasketDiscount(&$basket, $code, $check_code = true) { $promotion_data = false; $basket['face_value_voucher'] = 0; $basket['face_value_voucher_claim'] = 0; $basket['discount'] = 0; $basket['discount_fixed_claim'] = 0; $basket['discount_percentage_claim'] = 0; foreach ($basket['items'] as &$item) { $item['discount'] = 0; } if ($code) { require_once 'models/ecommerce/ecommerce_promotion.php'; $Promotion = new ecommerce_promotion(); $Promotion->setCacheable(false); if ($check_code) { $promotion_data = $Promotion->checkCodeBeforeApply($code, $basket['customer_id'], $basket); } else { $promotion_data = $Promotion->checkCodeMatch($code); } if ($promotion_data) { $promotion_data['discount_fixed_value'] = ecommerce_price::convertCurrency($promotion_data['discount_fixed_value'], GLOBAL_DEFAULT_CURRENCY, $basket['currency']); if ($promotion_data['type']['taxable']) { $this->calculateVoucherDiscount($basket, $promotion_data); } else { $this->calculateCouponDiscount($basket, $promotion_data); } } } return $promotion_data; }
/** * main action */ public function mainAction() { /** * find code */ if ($_SESSION['promotion_code']) { $code = $_SESSION['promotion_code']; } else { if (trim($_POST['promotion_code']) != '') { $code = trim($_POST['promotion_code']); } else { $code = false; } } /** * Check Actions */ if ($_POST['promotion_code_add'] && $code) { $_SESSION['promotion_code'] = $code; onxshopGoTo("/page/{$_SESSION['active_pages'][0]}"); } else { if ($_POST['promotion_code_remove']) { $_SESSION['promotion_code'] = false; onxshopGoTo("/page/{$_SESSION['active_pages'][0]}"); } } /** * initialize */ require_once 'models/ecommerce/ecommerce_promotion.php'; $Promotion = new ecommerce_promotion(); $Promotion->setCacheable(false); require_once 'models/ecommerce/ecommerce_basket.php'; $Basket = new ecommerce_basket(); $Basket->setCacheable(false); /** * basket detail */ if (is_numeric($_SESSION['basket']['id'])) { $basket = $Basket->getFullDetail($_SESSION['basket']['id']); $Basket->calculateBasketSubTotals($basket, $this->isVatEligible($basket['customer_id'])); $Basket->calculateBasketDiscount($basket, $_SESSION['promotion_code']); $Basket->saveDiscount($basket); } else { $basket = false; } /** * Allow Guest Checkout */ if ($basket['customer_id'] == 0 && $_SESSION['client']['customer']['guest'] == 1) { $customer_email = $_SESSION['client']['customer']['email']; } else { $customer_email = ''; } /** * Display */ if ($basket && ($promotion_code = $Promotion->checkCodeBeforeApply($code, $basket['customer_id'], $basket, $customer_email))) { $promotion_code['value'] = $code; $this->tpl->assign('PROMOTION_CODE', $promotion_code); if ($promotion_code['discount_percentage_value'] > 0) { $this->tpl->parse('content.applied.discount_percentage_value'); } if ($promotion_code['discount_fixed_value'] > 0) { $this->tpl->parse('content.applied.discount_fixed_value'); } if ($promotion_code['discount_free_delivery'] == 1) { $this->tpl->parse('content.applied.discount_free_delivery'); } if ($promotion_code['free_promo_product']) { $this->tpl->parse('content.applied.free_item_info'); } $this->tpl->parse('content.applied'); } else { //remove code $code = false; $promotion_code = array(); $promotion_code['value'] = $code; $this->tpl->assign('PROMOTION_CODE', $promotion_code); $this->tpl->parse('content.enter'); } /** * Save to session */ $_SESSION['promotion_code'] = $code; return true; }
/** * Calculate delivery rate for given carrier and basket content * * @param Array $basket Basket content * @param int $carrier_id Carrier id * @param int $country_id Delivery Country id * @return Array Delivery rate and VAT */ function calculateDeliveryForCountry($basket, $carrier_id, $country_id, $promotion_detail) { //if there is a product with vat rate > 0, add vat to the shipping $add_vat = $this->findVATEligibility($basket); require_once 'models/ecommerce/ecommerce_delivery_carrier.php'; $Delivery_Carrier = new ecommerce_delivery_carrier(); // first check if there are restricted items in the basket if (!$this->checkDeliveryRestrictions($basket, $country_id)) { return false; } // check if the delivery is available for given order value and weight $price = $Delivery_Carrier->getDeliveryRate($carrier_id, $basket['sub_total']['price'], $basket['total_weight_gross']); // false means method is not available for given weight and amount if ($price === false) { return false; } // zero weight means free delivery if ($basket['total_weight_gross'] == 0) { return $this->getFreeDelivery(); } // check free delivery promotion require_once 'models/ecommerce/ecommerce_promotion.php'; $Promotion = new ecommerce_promotion(); $Promotion->setCacheable(false); if ($Promotion->freeDeliveryAvailable($carrier_id, $country_id, $promotion_detail)) { return $this->getFreeDelivery($basket['total_weight_gross']); } return array('value_net' => sprintf("%0.2f", $price), 'weight' => $basket['total_weight_gross'], 'vat_rate' => $add_vat, 'vat' => $price * $add_vat / 100, 'value' => sprintf("%0.2f", $price * ($add_vat + 100) / 100)); }
/** * getVoucherData */ public function getVoucherData($voucher_code) { require_once 'models/ecommerce/ecommerce_promotion.php'; $Promotion = new ecommerce_promotion(); $Promotion->setCacheable(false); $promotion_list = $Promotion->listing("code_pattern = '{$voucher_code}'"); $promotion_data = $Promotion->getDetail($promotion_list[0]['id']); $data = $promotion_data['other_data']; $data['variety_name'] = "£" . round($promotion_data['discount_fixed_value']); $data['variety_description'] = "VOUCHER CODE: {$voucher_code}"; return $data; }