Back to PhocacartOrder class

Method saveOrderGiftCoupons

public
saveOrderGiftCoupons
(mixed $orderProductId, mixed $v, mixed $orderId, mixed $k, mixed $fullItems)

Method saveOrderGiftCoupons - Source code

public function saveOrderGiftCoupons($orderProductId, $v, $orderId, $k, $fullItems)
{
    $app = Factory::getApplication();
    $db = Factory::getDBO();
    $d = array();
    if (!isset($v['type'])) {
        return false;
    }
    if ($v['type'] != 4) {
        // Product type != Gift Voucher
        return false;
    }
    if (isset($v['brutto']) && $v['brutto'] > 0) {
        $d['discount'] = $v['brutto'];
    } else {
        return false;
    }
    $d['calculation_type'] = 0;
    // Fixed amount
    $d['type'] = 0;
    // Coupon type (COMMON, ONLINE SHOP, POS)
    $d['coupon_type'] = 2;
    // Coupon type (DEFAULT COUPON | GIFT VOUCHER)
    $d['gift_type'] = -1;
    // Possible gift voucher type (BIRTHDAY, PRESENT, FESTIVE ...)
    $d['gift_order_id'] = (int) $orderId;
    $d['gift_product_id'] = (int) $v['id'];
    $d['gift_order_product_id'] = (int) $orderProductId;
    $d['gift_recipient_name'] = '';
    $d['gift_recipient_email'] = '';
    $d['gift_sender_name'] = '';
    $d['gift_sender_message'] = '';
    $d['available_quantity'] = 1;
    $d['published'] = 0;
    // will be published by order status
    $d['access'] = 1;
    $d['title'] = Text::_('COM_PHOCACART_GIFT_VOUCHER');
    $d['alias'] = PhocacartUtils::getAliasName($d['title']);
    $d['code'] = PhocacartCoupon::generateCouponCode();
    $product = PhocacartProduct::getProduct((int) $v['id'], (int) $v['catid'], $this->type);
    if (!empty($v['attributes'])) {
        // Attributes
        foreach ($v['attributes'] as $k2 => $v2) {
            if (!empty($v2)) {
                // Options
                foreach ($v2 as $k3 => $v3) {
                    // Check if attribute is type GIFT
                    if (!isset($v3['atype']) || isset($v3['atype']) && $v3['atype'] != 20) {
                        // if not continue with another attribute (not option)
                        continue 2;
                    }
                    if (isset($v3['otype'])) {
                        switch ($v3['otype']) {
                            case 20:
                                $d['gift_recipient_name'] = urldecode($v3['ovalue']);
                                break;
                            case 21:
                                $d['gift_recipient_email'] = '';
                                $v3['ovalue'] = urldecode($v3['ovalue']);
                                if (MailHelper::isEmailAddress($v3['ovalue'])) {
                                    $d['gift_recipient_email'] = $v3['ovalue'];
                                }
                                break;
                            case 22:
                                $d['gift_sender_name'] = urldecode($v3['ovalue']);
                                break;
                            case 23:
                                $d['gift_sender_message'] = urldecode($v3['ovalue']);
                                break;
                            case 24:
                                $d['gift_type'] = urldecode($v3['ovalue']);
                                break;
                        }
                    }
                }
            }
        }
    }
    if (!empty($product->gift_types)) {
        $registry = new Registry();
        $registry->loadString($product->gift_types);
        $giftTypes = $registry->toArray();
        if (isset($d['gift_type'])) {
            $giftType = 'gift_types' . (int) $d['gift_type'];
            if (isset($giftTypes[$giftType])) {
                $giftTypeA = $giftTypes[$giftType];
                if (isset($giftTypeA['title'])) {
                    $d['gift_title'] = $giftTypeA['title'];
                }
                if (isset($giftTypeA['description'])) {
                    $d['gift_description'] = $giftTypeA['description'];
                }
                if (isset($giftTypeA['image'])) {
                    $d['gift_image'] = $giftTypeA['image'];
                }
                if (isset($giftTypeA['expiration_date'])) {
                    $d['valid_to'] = $giftTypeA['expiration_date'];
                }
                if (isset($giftTypeA['class_name'])) {
                    $d['gift_class_name'] = $giftTypeA['class_name'];
                }
                if (isset($giftTypeA['title']) && $giftTypeA['title'] != '') {
                    $d['title'] = $d['title'] . ' - ' . $giftTypeA['title'];
                }
            }
        }
    }
    $db->setQuery('SELECT MAX(ordering) FROM #__phocacart_coupons');
    $max = $db->loadResult();
    $d['ordering'] = $max + 1;
    $row = Table::getInstance('PhocacartCoupon', 'Table', array());
    if (!$row->bind($d)) {
        //throw new Exception($row->getError());
        $msg = Text::_($row->getError());
        $app->enqueueMessage($msg, 'error');
        return false;
    }
    if (!$row->check()) {
        //throw new Exception($row->getError());
        $msg = Text::_($row->getErrorMsg());
        $app->enqueueMessage($msg, 'error');
        return false;
    }
    if (!$row->store()) {
        //throw new Exception($row->getError());
        $msg = Text::_($row->getErrorMsg());
        $app->enqueueMessage($msg, 'error');
        return false;
    }
    return true;
}