예제 #1
0
 public function actionUpdateRestrictions()
 {
     $blnShipping = false;
     $errormsg = Yii::t('admin', "An error occurred saving Promo Code restrictions. Please check your System Log.");
     $arrRPost = Yii::app()->getRequest()->getPost('RestrictionForm');
     $arrSPost = Yii::app()->getRequest()->getPost('ShippingRestrictionForm');
     if (isset($arrRPost)) {
         $arrPost = $arrRPost;
     }
     if (isset($arrSPost)) {
         $arrPost = $arrSPost;
         $blnShipping = true;
     }
     if (isset($arrPost['id'])) {
         $id = $arrPost['id'];
         $objPromoCode = PromoCode::model()->findByPk($id);
         if ($objPromoCode instanceof PromoCode) {
             $lscodes = '';
             unset($arrPost['id']);
             $arrSections = array('category:' => 'categories', 'family:' => 'families', 'keyword:' => 'keywords', 'class:' => 'classes', '' => 'codes');
             foreach ($arrSections as $key => $arrSection) {
                 if (isset($arrPost[$arrSection])) {
                     foreach ($arrPost[$arrSection] as $sectionValue) {
                         $lscodes .= $key . $sectionValue . ",";
                     }
                     unset($arrPost[$arrSection]);
                 }
             }
             //Since we're using our Promo Code restriction structure for general shipping restrictions...
             if ($blnShipping && $objPromoCode->module != "freeshipping") {
                 if (strlen($lscodes) > 0) {
                     $objPromoCode->enabled = 1;
                 } else {
                     $objPromoCode->enabled = 0;
                 }
             }
             if (strlen($lscodes) > 0) {
                 $lscodes = substr($lscodes, 0, -1);
             } else {
                 $arrPost['exception'] = 0;
             }
             $arrPost['lscodes'] = $blnShipping == true ? "shipping:," . $lscodes : $lscodes;
             $objPromoCode->attributes = $arrPost;
             if ($objPromoCode->validate()) {
                 if (!$objPromoCode->save()) {
                     echo $errormsg;
                 } else {
                     echo "success";
                 }
             } else {
                 echo $errormsg;
                 Yii::log("Admin panel validation error saving promo code " . print_r($objPromoCode->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
             }
         } else {
             echo $errormsg;
         }
     } else {
         echo $errormsg;
     }
 }
예제 #2
0
 protected function checkCode()
 {
     //Does this item have a promo code?
     if (isset($this->config['promocode'])) {
         if (!empty($this->config['promocode'])) {
             Yii::log(get_class($this) . " module requires promo code '" . $this->config['promocode'] . "', checking...", 'info', 'application.' . __CLASS__ . "." . __FUNCTION__);
             $cart = $this->objCart;
             if ($cart->fk_promo_id > 0) {
                 $pcode = PromoCode::model()->findbyPk($cart->fk_promo_id);
                 if ($pcode->code == $this->config['promocode']) {
                     return true;
                 }
             }
             return false;
         }
     }
     return true;
 }
 public function actionUpdatepromo()
 {
     $pk = Yii::app()->getRequest()->getPost('pk');
     $name = Yii::app()->getRequest()->getPost('name');
     $value = Yii::app()->getRequest()->getPost('value');
     if ($name == 'valid_from' && $value == '') {
         $value = null;
     }
     if ($name == 'valid_until' && $value == '') {
         $value = null;
     }
     if ($name == 'qty_remaining' && $value == '') {
         $value = null;
     }
     if ($name == 'threshold' && $value == '') {
         $value = null;
     }
     // Check for existing code.
     $existingPromoCode = PromoCode::model()->findByAttributes(array('code' => $value));
     if (is_null($existingPromoCode) === false) {
         throw new CHttpException(400, Yii::t('global', 'Promo code must be unique.'));
         return;
     }
     if ($name == 'code' && $value == '') {
         PromoCode::model()->deleteByPk($pk);
         echo "delete";
     } else {
         PromoCode::model()->updateByPk($pk, array($name => $value));
         echo "success";
     }
 }
 public static function EnableShippingPromoCodes()
 {
     PromoCode::model()->updateAll(array('enabled' => 1), "lscodes LIKE 'shipping:,%'");
 }
예제 #5
0
 public function getFormCreate()
 {
     return array('title' => 'This feature is for creating one-time use promo codes in bulk for loyalty programs. Paste in your list of codes into the entry blank below, and select an existing promo code to use as a template. The following fields will be copied from this code: Amount, Percent or Money, optional Start and Stop date, Product restrictions and Good Above. Codes created with this process can be used once, then are rendered invalid.', 'elements' => array('createCodes' => array('type' => 'textarea', 'layout' => '<div class="span4">{label}</div><div class="span4">{input}</div>{error}'), 'existingCodes' => array('type' => 'dropdownlist', 'items' => CHtml::listData(PromoCode::model()->findAll(array('condition' => 'enabled=:status AND code IS NOT NULL AND module IS NULL', 'params' => array(':status' => 1), 'order' => 'code')), 'id', 'code'), 'prompt' => 'Please select:', 'layout' => '<div class="span4">{label}</div><div class="span4">{input}</div>{error}')));
 }
예제 #6
0
 /**
  * Extract shipping and billing address information, create address book and map to the carts
  */
 protected function actionConvertAddressBook()
 {
     $sql = "select * from xlsws_cart where billaddress_id IS NULL and address_bill IS NOT NULL order by id limit 500";
     $arrProducts = Yii::app()->db->createCommand($sql)->query();
     while (($result = $arrProducts->read()) !== false) {
         $result['email'] = strtolower($result['email']);
         //verify that Customer ID really exists in customer table
         $objCust = Customer::model()->findByPk($result['customer_id']);
         if (!$objCust instanceof Customer) {
             $result['customer_id'] = 0;
         }
         if (strlen($result['address_bill']) > 0) {
             $arrAddress = explode("\n", $result['address_bill']);
             if (count($arrAddress) == 5) {
                 //old format address, should be 6 pieces
                 $arrAddress[5] = $arrAddress[4];
                 $strSt = $arrAddress[3];
                 if ($strSt[0] == " ") {
                     //no state on this address
                     $arrAddress[4] = substr($strSt, 1, 100);
                     $arrAddress[3] = "";
                 } else {
                     $arrSt = explode(" ", $strSt);
                     $arrAddress[3] = $arrSt[0];
                     $arrAddress[4] = str_replace($arrSt[0] . " ", "", $strSt);
                 }
             }
             $objAddress = new CustomerAddress();
             if (count($arrAddress) >= 5) {
                 $objCountry = Country::LoadByCode($arrAddress[5]);
                 if ($objCountry) {
                     $objAddress->country_id = $objCountry->id;
                     $objState = State::LoadByCode($arrAddress[3], $objCountry->id);
                     if ($objState) {
                         $objAddress->state_id = $objState->id;
                     }
                 }
                 $objAddress->address1 = $arrAddress[0];
                 $objAddress->address2 = $arrAddress[1];
                 $objAddress->city = $arrAddress[2];
                 $objAddress->postal = $arrAddress[4];
                 $objAddress->first_name = $result['first_name'];
                 $objAddress->last_name = $result['last_name'];
                 $objAddress->company = $result['company'];
                 $objAddress->phone = $result['phone'];
                 $objAddress->residential = CustomerAddress::RESIDENTIAL;
                 $objAddress->created = $result['datetime_cre'];
                 $objAddress->modified = $result['datetime_cre'];
                 $objAddress->active = 1;
                 if (empty($objAddress->address2)) {
                     $objAddress->address2 = null;
                 }
                 if (empty($objAddress->company)) {
                     $objAddress->company = null;
                 }
                 $blnFound = false;
                 if ($result['customer_id'] > 0) {
                     //See if this is already in our database
                     $objPriorAddress = CustomerAddress::model()->findByAttributes(array('address1' => $objAddress->address1, 'address2' => $objAddress->address2, 'city' => $objAddress->city, 'postal' => $objAddress->postal, 'first_name' => $objAddress->first_name, 'last_name' => $objAddress->last_name, 'company' => $objAddress->company, 'phone' => $objAddress->phone));
                     if ($objPriorAddress instanceof CustomerAddress) {
                         Yii::app()->db->createCommand("update xlsws_cart set billaddress_id=" . $objPriorAddress->id . " where id=" . $result['id'])->execute();
                         $blnFound = true;
                     } else {
                         $objAddress->customer_id = $result['customer_id'];
                     }
                 } else {
                     //We need a shell customer record just for the email
                     $objC = Customer::model()->findByAttributes(array('email' => $result['email']));
                     if ($objC instanceof Customer) {
                         Yii::app()->db->createCommand("UPDATE xlsws_cart set customer_id=" . $objC->id . " where id=" . $result['id'])->execute();
                     } else {
                         $objC = new Customer();
                         $objC->record_type = Customer::GUEST;
                         $objC->email = $result['email'];
                         $objC->first_name = $objAddress->first_name;
                         $objC->last_name = $objAddress->last_name;
                         $objC->company = $objAddress->company;
                         if (!$objC->validate()) {
                             $arrErr = $objC->getErrors();
                             if (isset($arrErr['email'])) {
                                 $objC->email = $result['id'] . "*****@*****.**";
                             }
                             if (!$objC->validate()) {
                                 return print_r($objC->getErrors(), true);
                             }
                         }
                         if (!$objC->save()) {
                             Yii::log("Import Error " . print_r($objC->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                             return print_r($objC->getErrors(), true);
                         } else {
                             $cid = $objC->id;
                         }
                         Yii::app()->db->createCommand("UPDATE xlsws_cart set customer_id=" . $cid . " where id=" . $result['id'])->execute();
                     }
                     $result['customer_id'] = $objC->id;
                     $objAddress->customer_id = $result['customer_id'];
                 }
                 if (!$blnFound) {
                     if (!$objAddress->save()) {
                         //We have a corrupt billing address, just blank it out so import goes on
                         Yii::app()->db->createCommand("update xlsws_cart set address_bill=null where id=" . $result['id'])->execute();
                     } else {
                         $cid = $objAddress->id;
                         Yii::app()->db->createCommand("update xlsws_cart set billaddress_id=" . $cid . " where id=" . $result['id'])->execute();
                     }
                 }
             } else {
                 //We have a corrupt billing address, just blank it out so import goes on
                 Yii::app()->db->createCommand("update xlsws_cart set address_bill=null where id=" . $result['id'])->execute();
             }
             $objAddress = new CustomerAddress();
             $objCountry = Country::LoadByCode($result['ship_country']);
             if ($objCountry) {
                 $objAddress->country_id = $objCountry->id;
                 $objState = State::LoadByCode($result['ship_state'], $objCountry->id);
                 if ($objState) {
                     $objAddress->state_id = $objState->id;
                 }
             }
             $objAddress->first_name = $result['ship_firstname'];
             $objAddress->last_name = $result['ship_lastname'];
             $objAddress->company = $result['ship_company'];
             $objAddress->address1 = $result['ship_address1'];
             $objAddress->address2 = $result['ship_address2'];
             $objAddress->city = $result['ship_city'];
             $objAddress->postal = $result['ship_zip'];
             $objAddress->phone = $result['ship_phone'];
             $objAddress->residential = CustomerAddress::RESIDENTIAL;
             $objAddress->created = $result['datetime_cre'];
             $objAddress->modified = $result['datetime_cre'];
             $objAddress->active = 1;
             if (empty($objAddress->address2)) {
                 $objAddress->address2 = null;
             }
             if (empty($objAddress->company)) {
                 $objAddress->company = null;
             }
             $blnFound = false;
             if ($result['customer_id'] > 0) {
                 //See if this is already in our database
                 $objPriorAddress = CustomerAddress::model()->findByAttributes(array('address1' => $objAddress->address1, 'city' => $objAddress->city, 'postal' => $objAddress->postal, 'first_name' => $objAddress->first_name, 'last_name' => $objAddress->last_name, 'company' => $objAddress->company, 'phone' => $objAddress->phone));
                 if ($objPriorAddress instanceof CustomerAddress) {
                     Yii::app()->db->createCommand("update xlsws_cart set shipaddress_id=" . $objPriorAddress->id . " where id=" . $result['id'])->execute();
                     $blnFound = true;
                 } else {
                     $objAddress->customer_id = $result['customer_id'];
                 }
             }
             if (!$blnFound) {
                 if (!$objAddress->save()) {
                     Yii::log("Import Error " . print_r($objAddress->getErrors(), true), 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
                 } else {
                     $cid = $objAddress->id;
                     Yii::app()->db->createCommand("update xlsws_cart set shipaddress_id=" . $cid . " where id=" . $result['id'])->execute();
                 }
             }
         }
         $objShipping = new CartShipping();
         $objShipping->shipping_method = $result['shipping_method'];
         $objShipping->shipping_module = $result['shipping_module'];
         $objShipping->shipping_data = $result['shipping_data'];
         $objShipping->shipping_cost = $result['shipping_cost'];
         $objShipping->shipping_sell = $result['shipping_sell'];
         if (!$objShipping->save()) {
             return print_r($objShipping->getErrors());
         } else {
             $cid = $objShipping->id;
         }
         Yii::app()->db->createCommand("update xlsws_cart set shipping_id=" . $cid . " where id=" . $result['id'])->execute();
         $objPayment = new CartPayment();
         $objPayment->payment_method = $result['payment_method'];
         $objPayment->payment_module = str_replace(".php", "", $result['payment_module']);
         $objPayment->payment_data = $result['payment_data'];
         $objPayment->payment_amount = $result['payment_amount'];
         $objPayment->datetime_posted = $result['datetime_posted'];
         if ($result['fk_promo_id'] > 0) {
             $objPromo = PromoCode::model()->findByPk($result['fk_promo_id']);
             if ($objPromo) {
                 $objPayment->promocode = $objPromo->code;
             }
         }
         if (!$objPayment->save()) {
             return print_r($objPayment->getErrors());
         } else {
             $cid = $objPayment->id;
         }
         Yii::app()->db->createCommand("update xlsws_cart set payment_id=" . $cid . " where id=" . $result['id'])->execute();
     }
     $results2 = Yii::app()->db->createCommand("select count(*) from xlsws_cart where billaddress_id IS NULL and address_bill IS NOT NULL")->queryScalar();
     if ($results2 == 0) {
         $remain = 8;
     } else {
         $remain = 3;
     }
     return array('result' => "success", 'makeline' => $remain, 'total' => 50, 'tag' => 'Converting cart addresses, ' . $results2 . ' remaining');
 }
예제 #7
0
 /**
  * Check that the promo code applied to the cart is still valid.
  * If the promo code is no longer valid, remove it and return an error
  * message.
  *
  * @return array|null Null if the promo code is still valid. If the promo
  * code is invalid an array with the following keys will be returned:
  *	code => The promo code.
  *	reason => The reason that the promo code is invalid.
  */
 public function revalidatePromoCode()
 {
     if ($this->fk_promo_id === null) {
         // No promo code applied.
         return null;
     }
     $hasInvalidPromoCode = false;
     $objPromoCode = PromoCode::model()->findByPk($this->fk_promo_id);
     if ($objPromoCode === null) {
         // The promo code has been deleted from Web Store.
         $hasInvalidPromoCode = true;
     } else {
         $objPromoCode->validatePromocode('code', null);
         $arrErrors = $objPromoCode->getErrors();
         if (count($arrErrors) > 0) {
             // After validating the promo code, there were errors.
             $hasInvalidPromoCode = true;
         }
     }
     if ($hasInvalidPromoCode === false) {
         return null;
     }
     if ($objPromoCode === null) {
         $promoCodeCode = '';
         $reason = 'This Promo Code has been disabled.';
     } else {
         $promoCodeCode = $objPromoCode->code;
         $reason = _xls_convert_errors_display(_xls_convert_errors($arrErrors));
     }
     return array('code' => $promoCodeCode, 'reason' => $reason);
 }
예제 #8
0
 /**
  * During the Cart completion process, mark the Promo Code has used (if qty) and save to notes
  */
 protected function completeUpdatePromoCode()
 {
     $objCart = Yii::app()->shoppingcart;
     $objPromo = null;
     if ($objCart->fk_promo_id > 0) {
         $objPromo = PromoCode::model()->findByPk($objCart->fk_promo_id);
         $objCart->printed_notes = implode("\n\n", array($objCart->printed_notes, sprintf("%s: %s", _sp('Promo Code'), $objPromo->code)));
         foreach ($objCart->cartItems as $objItem) {
             if ($objItem->discount > 0) {
                 $objCart->printed_notes = implode("\n", array($objCart->printed_notes, sprintf("%s discount: %.2f", $objItem->code, $objItem->discount)));
             }
         }
         if ($objPromo->qty_remaining > 0) {
             $objPromo->qty_remaining--;
             $objPromo->save();
         }
     }
     $objCart->save();
 }