Ejemplo n.º 1
0
 public function action_check_discount()
 {
     $f_discount = 0.0;
     $discount = \Discountcode\Model_Discountcode::find_one_by_code(\Input::post('discount_code'));
     if ($discount && $discount->status == 'active') {
         // check for expiry date
         if ($discount->active_from != '0000-00-00' && $discount->active_to != '0000-00-00') {
             $current_date = strtotime(date('Y-m-d'));
             $from = strtotime($discount->active_from);
             $to = strtotime($discount->active_to);
             if ($current_date >= $from && $current_date <= $to) {
                 $valid = true;
             }
         } else {
             $valid = true;
         }
         if ($valid) {
             $valid = false;
             // check for single/multiple use
             if ($discount->use_type == 'multi use') {
                 $valid = true;
             } else {
                 // check if discount code was already used.
                 $order_exists = \Order\Model_Order::find_one_by_id_discount($discount->id);
                 if (is_null($order_exists)) {
                     $valid = true;
                 }
             }
         }
         if ($valid) {
             if (is_numeric(\Session::get('order.id'))) {
                 $order = \Order\Model_Order::find_one_by_id(\Session::get('order.id'));
             }
             if (!$order) {
                 $order = \Order\Model_Order::forge();
             }
             $method = \Input::post('method') == 'pickup' ? false : true;
             $shipping_price = $order->shipping_price(null, null, true, $method);
             // compute value of discount
             switch ($discount->type) {
                 case 'free shipping':
                     $f_discount = \Input::post('method') == 'pickup' ? 0 : $shipping_price;
                     break;
                 case 'percentage':
                     $f_discount = \Input::post('total_price') * ($discount->type_value / 100);
                     break;
                 case 'value':
                     $f_discount = $discount->type_value;
                     break;
             }
         }
     }
     echo json_encode(array('discount' => $f_discount));
 }
Ejemplo n.º 2
0
 public function action_get_discount_value()
 {
     $discount_action = 'Apply';
     $valid = false;
     $f_discount = 0.0;
     $success = false;
     $grand_total = 0.0;
     $gst_price = \Input::post('gst_price');
     if (\Input::post('discount_action') == 'Remove') {
         $method = \Input::post('method') == 'pickup' ? false : true;
         $order = \Order\Model_Order::forge();
         $shipping_price = $order->shipping_price(null, null, true, $method);
         $gst_price = number_format((\Input::post('total_price') + $shipping_price) * 0.1, 2, '.', '');
         $grand_total = number_format(\Input::post('total_price') + $shipping_price + (\Input::post('total_price') + $shipping_price) * 0.1, 2, '.', '');
         $success = true;
         if (is_numeric(\Session::get('admin_order.id'))) {
             $order = \Order\Model_Order::find_one_by_id(\Session::get('admin_order.id'));
             $order->set(array('id_discount' => NULL, 'discount_amount' => 0, 'total_price' => $grand_total, 'gst_price' => $gst_price));
             $order->save();
         }
     } else {
         $discount = \Discountcode\Model_Discountcode::find_one_by_code(\Input::post('discount_code'));
         if ($discount && $discount->status == 'active') {
             // check for expiry date
             if ($discount->active_from != '0000-00-00' && $discount->active_to != '0000-00-00') {
                 $current_date = strtotime(date('Y-m-d'));
                 $from = strtotime($discount->active_from);
                 $to = strtotime($discount->active_to);
                 if ($current_date >= $from && $current_date <= $to) {
                     $valid = true;
                 }
             } else {
                 $valid = true;
             }
             if ($valid) {
                 $valid = false;
                 // check for single/multiple use
                 if ($discount->use_type == 'multi use') {
                     $valid = true;
                 } else {
                     // check if discount code was already used.
                     $order_exists = \Order\Model_Order::find_one_by_id_discount($discount->id);
                     if (is_null($order_exists)) {
                         $valid = true;
                     }
                 }
             }
             if ($valid) {
                 if (is_numeric(\Session::get('admin_order.id'))) {
                     $order = \Order\Model_Order::find_one_by_id(\Session::get('admin_order.id'));
                 }
                 if (!$order) {
                     $order = \Order\Model_Order::forge();
                 }
                 $method = \Input::post('method') == 'pickup' ? false : true;
                 $shipping_price = $order->shipping_price(null, null, true, $method);
                 // compute value of discount
                 switch ($discount->type) {
                     case 'free shipping':
                         $f_discount = \Input::post('method') == 'pickup' ? 0 : $shipping_price;
                         break;
                     case 'percentage':
                         $f_discount = \Input::post('total_price') * ($discount->type_value / 100);
                         break;
                     case 'value':
                         $f_discount = $discount->type_value;
                         break;
                 }
                 $gst_price = number_format((\Input::post('total_price') + $shipping_price) * 0.1, 2, '.', '');
                 $grand_total = number_format(\Input::post('total_price') + $shipping_price + (\Input::post('total_price') + $shipping_price) * 0.1, 2, '.', '');
                 $order->set(array('finished' => 0, 'id_discount' => $discount->id, 'discount_amount' => $f_discount, 'shipping_price' => $shipping_price, 'gst_price' => $gst_price, 'total_price' => $grand_total));
                 $order->save();
                 \Session::set('admin_order.id', $order->id);
                 $discount_action = 'Remove';
                 $success = true;
             }
         }
     }
     echo json_encode(array('success' => $success, 'discount_action' => $discount_action, 'discount_amount' => $f_discount));
 }