function generateCouponCode() { $code = "UNI" . substr(date("Y"), 2, 2) . date("m") . date("d") . date("his"); $code .= mt_rand(11111, 99999); $codeExist = getFieldValue("code", "coupon", "WHERE code='" . $code . "'"); if (empty($codeExist)) { return $code; } else { generateCouponCode(); } }
public function addVoucher() { $merchant_id = Yii::app()->functions->getMerchantID(); $db_ext = new DbExt(); $voucher_code = ''; $validator = new Validator(); $req = array('voucher_name' => Yii::t("default", "Voucher name is required"), 'amount' => Yii::t("default", "Amount is required"), 'number_of_voucher' => Yii::t("default", "Number of voucher is required"), 'status' => Yii::t("default", "Status is required")); if (!empty($this->data['id'])) { unset($req['number_of_voucher']); } $validator->required($req, $this->data); if ($validator->validate()) { $params = array('voucher_name' => $this->data['voucher_name'], 'number_of_voucher' => $this->data['number_of_voucher'], 'amount' => $this->data['amount'], 'status' => $this->data['status'], 'date_created' => date('c'), 'ip_address' => $_SERVER['REMOTE_ADDR'], 'voucher_type' => $this->data['voucher_type'], 'merchant_id' => $merchant_id); if (!empty($this->data['id'])) { unset($params['number_of_voucher']); unset($params['date_created']); $params['date_modified'] = date('c'); $res = $db_ext->updateData("{{voucher}}", $params, 'voucher_id', $this->data['id']); if ($res) { $this->code = 1; $this->msg = Yii::t("default", 'Voucher updated.'); } else { $this->msg = Yii::t("default", "ERROR: cannot updated."); } } else { $db_ext->insertData("{{voucher}}", $params); $voucher_id = Yii::app()->db->getLastInsertID(); for ($i = 1; $i <= $this->data['number_of_voucher']; $i++) { //$voucher_code=$i.date('YmdGis'); //$voucher_code=generateCouponCode(3).date("YmdHis"); $voucher_code = generateCouponCode(3) . date("Ymd") . $voucher_id . $i; $params_voucher = array('voucher_id' => $voucher_id, 'voucher_code' => $voucher_code); $db_ext->insertData("{{voucher_list}}", $params_voucher); } $this->code = 1; $this->msg = Yii::t("default", "Voucher successfully generated"); } } else { $this->msg = $validator->getErrorAsHTML(); } }
<?php require_once "lib/config.php"; $error = array(); $message = ""; if (isset($_REQUEST['action']) && $_REQUEST['action'] == "get_code") { echo generateCouponCode(); die; } if (isset($_POST['save'])) { $coupon_type_user = $_POST['coupon_type_user']; $coupon_type_offer = $_POST['coupon_type_offer']; $created_on = date("Y-m-d H:i:s"); $status = "1"; if (empty(trim($_POST['name']))) { $error[] = "Coupon name can't be empty."; } else { $name = trim($_POST['name']); } if (empty(trim($_POST['code']))) { $error[] = "Coupon code can't be empty. Please click on generate code button to generate code."; } else { $code = trim($_POST['code']); } if (empty(trim($_POST['amount_offer']))) { $error[] = "Amount can't be empty."; } else { if ($coupon_type_offer == "percentage" && trim($_POST['amount_offer']) > 99) { $error[] = "Offer amount must be less or equal to 99 percent."; } else { $amount_offer = trim($_POST['amount_offer']);