private function _CommitCoupon($CouponId = 0) { include_once ISC_BASE_PATH . '/lib/api/coupon.api.php'; $coupon = new API_COUPON(); $_POST['couponappliesto'] = $_POST['usedfor']; if ($_POST['couponappliesto'] == "categories") { // Applies to categories $_POST['couponappliestovalues'] = implode(",", array_map('intval', $_POST['catids'])); } else { // Applies to products $_POST['couponappliestovalues'] = implode(',', array_map('intval', explode(',', $_POST['prodids']))); } if (!empty($_POST['couponexpires'])) { $_POST['couponexpires'] = ConvertDateToTime($_POST['couponexpires']); } else { $_POST['couponexpires'] = 0; } if (!isset($_POST['couponcode']) || empty($_POST['couponcode'])) { $_POST['couponcode'] = GenerateCouponCode(); } if (isset($_POST['couponenabled'])) { $_POST['couponenabled'] = 1; } else { $_POST['couponenabled'] = 0; } $_POST['couponminpurchase'] = str_replace(GetConfig('CurrencyToken'), "", $_POST['couponminpurchase']); if (isset($_POST['couponmaxuses'])) { $_POST['couponmaxuses'] = (int) $_POST['couponmaxuses']; } else { $_POST['couponmaxuses'] = 0; } if ($CouponId == 0) { $CouponId = $coupon->create(); } else { $coupon->load($CouponId); $coupon->save(); } if (!$coupon->error) { // Log this action $GLOBALS['ISC_CLASS_LOG']->LogAdminAction($CouponId, $_POST['couponcode']); } return $coupon->error; }
private function _CommitCompanyGiftCertificate($cgcId = 0) { $_POST['cgcappliesto'] = $_POST['usedfor']; if ($_POST['cgcappliesto'] == "categories") { // Applies to categories $_POST['cgcappliestovalues'] = implode(",", array_map('intval', $_POST['catids'])); } else { // Applies to products $_POST['cgcappliestovalues'] = implode(',', array_map('intval', explode(',', $_POST['prodids']))); } if (!empty($_POST['cgcexpires'])) { //$_POST['cgcexpires'] = ConvertDateToTime($_POST['cgcexpires']); comment by NI_20100901_Jack make it same with coupon $vals = explode("/", $_POST['cgcexpires']); $mktime = mktime(23, 59, 59, $vals[0], $vals[1], $vals[2]); $_POST['cgcexpires'] = $mktime; } else { $_POST['cgcexpires'] = 0; } if (!isset($_POST['cgccode']) || empty($_POST['cgccode'])) { $_POST['cgccode'] = GenerateCouponCode(); } if (isset($_POST['cgcenabled'])) { $_POST['cgcenabled'] = 1; } else { $_POST['cgcenabled'] = 0; } $_POST['cgcminpurchase'] = DefaultPriceFormat($_POST['cgcminpurchase']); $_POST['cgcamount'] = DefaultPriceFormat($_POST['cgcamount']); $_POST['cgcbalance'] = DefaultPriceFormat($_POST['cgcbalance']); for ($i = 1; $i <= $_POST['recipientcount']; $i++) { if (empty($_POST['to_name_' . $i]) && empty($_POST['to_email_' . $i])) { continue; } if ($i == 1 or empty($_POST['to_name']) and empty($_POST['to_email'])) { $_POST['to_name'] .= $_POST['to_name_' . $i]; $_POST['to_email'] .= $_POST['to_email_' . $i]; } else { $_POST['to_name'] .= '$' . $_POST['to_name_' . $i]; $_POST['to_email'] .= '$' . $_POST['to_email_' . $i]; } } if ($cgcId == 0) { //check if code or name already exist $query = sprintf("select * from [|PREFIX|]company_gift_certificates where cgccode = '%s'", $_POST['cgccode']); $result = $GLOBALS['ISC_CLASS_DB']->Query($query); if ($GLOBALS['ISC_CLASS_DB']->CountResult($result) > 0) { return GetLang('CompanyGiftCertificateCodeExists'); } //new cgc $query = sprintf("insert into [|PREFIX|]company_gift_certificates (\n\t\t\t\t\tcgccode, cgcto, cgctoemail, \n\t\t\t\t\tcgccustid, cgcamount, cgcbalance, cgcenabled, cgcmessage, \n\t\t\t\t\tcgcname, cgcappliesto, cgcappliestovalues, cgcminpurchase, cgctemplate, \n\t\t\t\t\tcgcexpirydate, cgcpurchasedate, cgcstatus) VALUES\n\t\t\t\t\t('%s', '%s', '%s', \n\t\t\t\t\t%s, %s, %s, %s, '%s', \n\t\t\t\t\t'%s', '%s', '%s', %s, '%s', \n\t\t\t\t\t%s, %s, 2);", $_POST['cgccode'], $_POST['to_name'], $_POST['to_email'], 0, $_POST['cgcamount'], $_POST['cgcbalance'], $_POST['cgcenabled'], $_POST['message'], $_POST['cgcname'], $_POST['cgcappliesto'], $_POST['cgcappliestovalues'], (int) $_POST['cgcminpurchase'], $_POST['certificate_theme'], $_POST['cgcexpires'], time()); $result = $GLOBALS['ISC_CLASS_DB']->Query($query); if ($result == false) { return GetLang('CompanyGiftCertificateInsertError'); } else { return; } } else { //check if code or name already exist $query = sprintf("select * from [|PREFIX|]company_gift_certificates where cgccode = '%s' and cgcid <> %s ", $_POST['cgccode'], $cgcId); $result = $GLOBALS['ISC_CLASS_DB']->Query($query); if ($GLOBALS['ISC_CLASS_DB']->CountResult($result) > 0) { return GetLang('CompanyGiftCertificateCodeExists'); } //check balance $query = sprintf("UPDATE [|PREFIX|]company_gift_certificates SET \n\t\t\t\t\tcgccode = '%s', cgcto = '%s',cgctoemail = '%s',cgcamount = %s,cgcbalance = %s,\n\t\t\t\t\tcgcenabled = %s,cgcmessage = '%s',cgcname = '%s',cgcappliesto = '%s',cgcappliestovalues = '%s',\n\t\t\t\t\tcgcminpurchase = %s,cgctemplate = '%s',cgcexpirydate = %s WHERE cgcid = %s;", $_POST['cgccode'], $_POST['to_name'], $_POST['to_email'], $_POST['cgcamount'], $_POST['cgcbalance'], $_POST['cgcenabled'], $_POST['message'], $_POST['cgcname'], $_POST['cgcappliesto'], $_POST['cgcappliestovalues'], $_POST['cgcminpurchase'], $_POST['certificate_theme'], $_POST['cgcexpires'], $cgcId); $result = $GLOBALS['ISC_CLASS_DB']->Query($query); if ($result == false) { return GetLang('ErrCompanyGiftCertificateNotUpdated'); } else { return; } } }
protected function _CommitCoupon($CouponId = 0) { $name = trim($_POST['couponname']); $type = $_POST['coupontype']; // dollar or percent $amount = DefaultPriceFormat($_POST['couponamount']); $appliesTo = $_POST['usedfor']; if($appliesTo == "categories") { $appliesValues = $_POST['catids']; // nothing selected then default to all categories if (empty($appliesValues)) { $appliesValues = array('0'); } } else { $appliesValues = explode(",", $_POST['prodids']); } if (!empty($_POST['couponexpires'])) { $expires = ConvertDateToTime($_POST['couponexpires']); } else { $expires = 0; } if (!isset($_POST['couponcode']) || empty($_POST['couponcode'])) { $code = GenerateCouponCode(); } else { $code = trim($_POST['couponcode']); } if (isset($_POST['couponenabled'])) { $enabled = 1; } else { $enabled = 0; } $minPurchase = DefaultPriceFormat($_POST['couponminpurchase']); $maxUses = 0; $maxUsesPerCus = 0; if (isset($_POST['couponmaxuses'])) { $maxUses = (int)$_POST['couponmaxuses']; } if (isset($_POST['couponmaxusespercus'])) { $maxUsesPerCus = (int)$_POST['couponmaxusespercus']; } $locationRestricted = 0; if (!empty ($_POST['YesLimitByLocation'])) { $locationRestricted = 1; } $shippingMethodRestricted = 0; if (!empty ($_POST['YesLimitByShipping'])) { $shippingMethodRestricted = 1; } $coupon = array( 'couponname' => $name, 'coupontype' => $type, 'couponamount' => $amount, 'couponminpurchase' => $minPurchase, 'couponexpires' => $expires, 'couponenabled' => $enabled, 'couponcode' => $code, 'couponappliesto' => $appliesTo, 'couponmaxuses' => $maxUses, 'couponmaxusespercus' => $maxUsesPerCus, 'location_restricted' => $locationRestricted, 'shipping_method_restricted' => $shippingMethodRestricted, ); // update existing coupon if ($CouponId) { $result = $GLOBALS['ISC_CLASS_DB']->UpdateQuery("coupons", $coupon, "couponid = '" . $GLOBALS['ISC_CLASS_DB']->Quote($CouponId) . "'"); if (!$result) { return "Failed to update coupon"; } //delete existing values $query = "DELETE FROM [|PREFIX|]coupon_values WHERE couponid = '" . $GLOBALS['ISC_CLASS_DB']->Quote($CouponId) . "'"; $GLOBALS['ISC_CLASS_DB']->Query($query); } else { // create new coupon $CouponId = $GLOBALS['ISC_CLASS_DB']->InsertQuery("coupons", $coupon); if (!isId($CouponId)) { return "Failed to create coupon"; } } // add applies to values if (!empty($appliesValues)) { foreach ($appliesValues as $value) { $couponvalue = array( 'couponid' => $CouponId, 'valueid' => $value ); $GLOBALS['ISC_CLASS_DB']->InsertQuery("coupon_values", $couponvalue); } } // Location restriction // Remove all the existing ones if exist $GLOBALS['ISC_CLASS_DB']->DeleteQuery('coupon_locations', "WHERE coupon_id = " . $GLOBALS['ISC_CLASS_DB']->Quote($CouponId)); if ($locationRestricted) { $selectedType = $_POST['LocationType']; if ($selectedType == 'country') { $countryList = GetCountryListAsIdValuePairs(); foreach($_POST['LocationTypeCountries'] as $countryId) { if(empty ($countryList[$countryId])) { continue; } $newLocation = array( 'coupon_id' => (int)$CouponId, 'selected_type' => $selectedType, 'value' => $countryList[$countryId], 'value_id' => $countryId, 'country_id' => 0, ); $GLOBALS['ISC_CLASS_DB']->InsertQuery('coupon_locations', $newLocation); } } else if ($selectedType == 'state') { $countryList = GetCountryListAsIdValuePairs(); $stateList = array(); foreach($_POST['LocationTypeStatesSelect'] as $stateRecord) { $state = explode('-', $stateRecord, 2); if(!isset($stateList[$state[0]])) { // Load the states in this country as we haven't done that before $stateList[$state[0]] = array(); $query = "SELECT * FROM [|PREFIX|]country_states WHERE statecountry='".(int)$state[0]."'"; $result = $GLOBALS['ISC_CLASS_DB']->Query($query); while($stateResult = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) { $stateList[$stateResult['statecountry']][$stateResult['stateid']] = $stateResult['statename']; } } // Start storing what we received if(isset($stateList[$state[0]][$state[1]])) { $stateName = $stateList[$state[0]][$state[1]]; } else { $stateName = ''; } $newLocation = array( 'coupon_id' => (int)$CouponId, 'selected_type' => $selectedType, 'value' => $stateName, 'value_id' => (int)$state[1], 'country_id' => (int)$state[0], ); $GLOBALS['ISC_CLASS_DB']->InsertQuery('coupon_locations', $newLocation); } } else if ($selectedType == 'zip') { $zipCodes = explode("\n", $_POST['LocationTypeZipPostCodes']); foreach($zipCodes as $zipCode) { $zipCode = trim($zipCode); if(!$zipCode) { continue; } $newLocation = array( 'coupon_id' => (int)$CouponId, 'selected_type' => $selectedType, 'value' => $zipCode, 'value_id' => '0', 'country_id' => (int)$_POST['LocationTypeZipCountry'], ); $GLOBALS['ISC_CLASS_DB']->InsertQuery('coupon_locations', $newLocation); } } } // Shipping Method restriction // Remove all the existing ones if exist $GLOBALS['ISC_CLASS_DB']->DeleteQuery('coupon_shipping_methods', "WHERE coupon_id = " . $GLOBALS['ISC_CLASS_DB']->Quote($CouponId)); if ($shippingMethodRestricted) { foreach ($_POST['LocationTypeShipping'] as $shipper) { $newShippingMethod = array( 'coupon_id' => (int)$CouponId, 'module_id' => $shipper, ); $GLOBALS['ISC_CLASS_DB']->InsertQuery('coupon_shipping_methods', $newShippingMethod); } } // Log this action $GLOBALS['ISC_CLASS_LOG']->LogAdminAction($CouponId, $_POST['couponcode']); return $CouponId; }
private function _CommitCoupon($CouponId = 0) { include_once ISC_BASE_PATH . '/lib/api/coupon.api.php'; $coupon = new API_COUPON(); $_POST['couponappliesto'] = $_POST['usedfor']; if ($_POST['couponappliesto'] == "categories") { // Applies to categories $_POST['couponappliestovalues'] = implode(",", array_map('intval', $_POST['catids'])); } else { // Applies to products $_POST['couponappliestovalues'] = implode(',', array_map('intval', explode(',', $_POST['prodids']))); } if (!empty($_POST['couponexpires'])) { // $_POST['couponexpires'] = ConvertDateToTime($_POST['couponexpires']); -- commented due to, it saving the date as selected date with 00:00:00. Its wrong, so we have added manual expiration time -- Baskaran $vals = explode("/", $_POST['couponexpires']); $mktime = mktime(23, 59, 59, $vals[0], $vals[1], $vals[2]); $_POST['couponexpires'] = $mktime; } else { $_POST['couponexpires'] = 0; } if (!isset($_POST['couponcode']) || empty($_POST['couponcode'])) { $_POST['couponcode'] = GenerateCouponCode(); } if (isset($_POST['couponenabled'])) { $_POST['couponenabled'] = 1; } else { $_POST['couponenabled'] = 0; } $_POST['couponminpurchase'] = str_replace(GetConfig('CurrencyToken'), "", $_POST['couponminpurchase']); if (isset($_POST['couponmaxuses'])) { $_POST['couponmaxuses'] = (int) $_POST['couponmaxuses']; } else { $_POST['couponmaxuses'] = 0; } if ($CouponId == 0) { $CouponId = $coupon->create(); $this->SavePromotionalURL($CouponId, 'add'); } else { $coupon->load($CouponId); $coupon->save(); $this->SavePromotionalURL($CouponId, 'edit'); } if (!$coupon->error) { // Log this action $GLOBALS['ISC_CLASS_LOG']->LogAdminAction($CouponId, $_POST['couponcode']); } return $coupon->error; }
public function GetRandomNumber() { return substr(GenerateCouponCode() . GenerateCouponCode(), 0, 5); }