function leaveMessage() { if (!empty($this->params['shippingmessageid'])) { $sm = new shippingmethod($this->params['shippingmessageid']); if ($this->params['nosave'] == false) { $sm->to = empty($this->params['shpmessageto']) ? null : $this->params['shpmessageto']; $sm->from = empty($this->params['shpmessagefrom']) ? null : $this->params['shpmessagefrom']; $sm->message = empty($this->params['shpmessage']) ? null : $this->params['shpmessage']; $sm->save(); } } $ar = new expAjaxReply(200, 'ok', $sm, array('controller' => 'cart', 'action' => 'checkout')); $ar->send(); }
public function saveShippingMethods() { global $order; $shipping = new shipping(); $order->shippingmethods = array(); // if they didn't fill out anything if (empty($this->params['methods'])) { expValidator::failAndReturnToForm(gt("You did not pick any shipping options"), $this->params); } // if they don't check all the radio buttons if (count($this->params['methods']) < count($this->params['calcs'])) { expValidator::failAndReturnToForm(gt("You must select a shipping options for all of your packages."), $this->params); } foreach ($this->params['methods'] as $id => $method) { $cost = $this->params['cost'][$method]; $title = $this->params['title'][$method]; $shippingmethod = new shippingmethod($id); $shippingmethod->update(array('option' => $method, 'option_title' => $title, 'shipping_cost' => $cost, 'shippingcalculator_id' => $this->params['calcs'][$id])); $order->shippingmethods[] = $shippingmethod->id; } redirect_to(array('controller' => 'cart', 'action' => 'checkout')); }
public function calculateGrandTotal() { // calulate promo codes and group discounts //we need to tally up the cart, apply discounts, TAX that TOTAL somehow (different tax clases come into play), then add shipping //grab our discounts $cartDiscounts = $this->getOrderDiscounts(); //reset totals $this->total_discounts = 0; $this->shipping_total = 0; $this->shipping_total_before_discounts = 0; $this->shippingDiscount = 0; $this->surcharge_total = 0; $this->subtotal = 0; $this->total = 0; $this->grand_total = 0; $this->tax = 0; $validateDiscountMessage = ''; //eDebug($this->surcharge_total); //hate doing double loops, but we need to have the subtotal figured out already for //doing the straight dollar disoount calculations below for ($i = 0; $i < count($this->orderitem); $i++) { // figure out the amount of the discount /*if (!empty($this->product_discounts)) { $discount_amount = ($this->orderitem[$i]->products_price * ($this->product_discounts * .01)); // change the price of the orderitem..this is needed for when we calculate tax below. $this->orderitem[$i]->products_price = $this->orderitem[$i]->products_price - $discount_amount; // keep a tally of the total amount being subtracted by this discount. $this->total_discounts += $discount_amount; }*/ //$this->orderitem[$i]->products_price = $this->orderitem[$i]->getPriceWithOptions(); // * $this->orderitem[$i]->quantity; $this->orderitem[$i]->products_price_adjusted = $this->orderitem[$i]->products_price; //$this->orderitem[$i]->products_price_original = $this->orderitem[$i]->product->getPrice(); $this->subtotal += $this->orderitem[$i]->products_price * $this->orderitem[$i]->quantity; $this->surcharge_total += $this->orderitem[$i]->product->getSurcharge() * $this->orderitem[$i]->quantity; } for ($i = 0; $i < count($this->orderitem); $i++) { //only allowing one discount for now, but in future we'll need to process //multiple and accomdate the "weight" and 'allow other discounts' type settings //this foreach will only fire once as of now, and will only hit on one or the other //TODO: We need to use produce_price_adjusted in the loops to accomodate for more than one disocunt //otherwise it's just resetting them now instead of adding them foreach ($cartDiscounts as $od) { //do not calculate invalid discounts, but don't remove either $discount = new discounts($od->discounts_id); /*$validateDiscountMessage = $discount->validateDiscount(); if($validateDiscountMessage != '') break;*/ //percentage discount if ($discount->action_type == 3) { $discount_amount = round($this->orderitem[$i]->products_price * ($discount->discount_percent / 100), 2); // change the price of the orderitem..this is needed for when we calculate tax below. $this->orderitem[$i]->products_price_adjusted = $this->orderitem[$i]->products_price - $discount_amount; // keep a tally of the total amount being subtracted by this discount. $this->total_discounts += $discount_amount * $this->orderitem[$i]->quantity; } //straight $$ discount if ($discount->action_type == 4) { $this->total_discounts = $discount->discount_amount; //what % of the order is this product with all it's quantity $percentOfTotalOrder = $this->orderitem[$i]->products_price * $this->orderitem[$i]->quantity / $this->subtotal; //figoure out how much that'll be and what each quanityt piece will bare $discountAmountPerItem = round($percentOfTotalOrder * $discount->discount_amount / $this->orderitem[$i]->quantity, 2); //$discount_amount = $this->orderitem[$i]->products_price * ($discount->discount_percent / 100); // change the price of the orderitem..this is needed for when we calculate tax below. $this->orderitem[$i]->products_price_adjusted = $this->orderitem[$i]->products_price - $discountAmountPerItem; // keep a tally of the total amount being subtracted by this discount. //$this->total_discounts += $discountAmountPerItem * $this->orderitem[$i]->quantity; //eDebug($discountAmountPerItem); } } // calculate the tax for this product $taxclass = new taxclass($this->orderitem[$i]->product->tax_class_id); $this->orderitem[$i]->products_tax = $taxclass->getProductTax($this->orderitem[$i]); $this->tax += $this->orderitem[$i]->products_tax * $this->orderitem[$i]->quantity; //save out the order item $this->orderitem[$i]->save(); } // add the "cart discounts" - percentage for sure, but straight can work also should be added after the final total is calculated, //including tax but not shipping // $this->updateOrderDiscounts(); /*foreach ($cartDiscounts as $od) { $discount = new discounts($od->discounts_id); if ($discount->action_type == 4) { $this->total_discounts += $discount->discount_amount; } } */ // calculate the shipping costs - need to check shipping discounts here in the future $estimate_shipping = false; if ($this->shipping_required) { $shippingmethods = $this->getShippingMethods(); if (count($shippingmethods) > 0) { foreach ($shippingmethods as $sm_id) { $method = new shippingmethod($sm_id, true); if ($method->requiresShipping($this)) { /* //need to implement handling $shippingCalc = new shippingcalculator($method->shippingcalculator_id); $calc = new $shippingCalc->calculator_name($method->shippingcalculator_id); eDebug($calc,true);*/ $this->shipping_total += $method->shipping_cost; // + $method->calculator->getHandling(); } } } else { $estimate_shipping = true; } } $this->shipping_total_before_discounts = $this->shipping_total; if (isset($cartDiscounts)) { foreach ($cartDiscounts as $od) { $discount = new discounts($od->discounts_id); $this->shipping_total = $discount->calculateShippingTotal($this->shipping_total); } } $this->shippingDiscount = $this->shipping_total_before_discounts - $this->shipping_total; //check here to make sure we don't discount ourselves into oblivion $orderTotalPreDiscounts = $this->subtotal + $this->tax + $this->shipping_total; if ($this->total_discounts > $orderTotalPreDiscounts) { $this->total_discounts = $orderTotalPreDiscounts; } $this->total = $this->subtotal - $this->total_discounts; if ($estimate_shipping) { $this->shipping_total = shipping::estimateShipping($this); } // figure out which tax zones apply to this order. $this->taxzones = taxclass::getCartTaxZones($this); $this->grand_total = $this->subtotal - $this->total_discounts + $this->tax + $this->shipping_total + $this->surcharge_total; //if($validateDiscountMessage != '') flash('message',$validateDiscountMessage); //eDebug($this, true); }
function process_orders() { /* Testing */ /*echo "Here?"; $inv = 30234; $req = 'a29f9shsgh32hsf80s7'; $amt = 101.00; for($count=1;$count<=25;$count+=2) { $data[2] = $inv + $count; $amt += $count*$count; $successSet[$count]['message'] = "Sucessfully imported row " . $count . ", order: " . $data[2] . "<br/>"; $successSet[$count]['order_id'] = $data[2]; $successSet[$count]['amount'] = $amt; $successSet[$count]['request_id'] = $req; $successSet[$count]['reference_id'] = $req; $successSet[$count]['authorization_code'] = $req; $successSet[$count]['shipping_tracking_number'] = '1ZNF453937547'; $successSet[$count]['carrier'] = 'UPS'; } for($count=2;$count<=25;$count+=2) { $data[2] = $inv + $count; $amt += $count*$count; $errorSet[$count]['error_code'] = '42'; $errorSet[$count]['message'] = "No go for some odd reason. Try again."; $errorSet[$count]['order_id'] = $data[2]; $errorSet[$count]['amount'] = $amt; } assign_to_template(array('errorSet'=>$errorSet, 'successSet'=>$successSet)); return;*/ ########### global $db; $template = get_template_for_action(new orderController(), 'setStatus', $this->loc); //eDebug($_FILES); //eDebug($this->params,true); set_time_limit(0); //$file = new expFile($this->params['expFile']['batch_process_upload'][0]); if (!empty($_FILES['batch_upload_file']['error'])) { flash('error', gt('There was an error uploading your file. Please try again.')); redirect_to(array('controller' => 'store', 'action' => 'batch_process')); } $file->path = $_FILES['batch_upload_file']['tmp_name']; echo "Validating file...<br/>"; $checkhandle = fopen($file->path, "r"); $checkdata = fgetcsv($checkhandle, 10000, ","); $fieldCount = count($checkdata); $count = 1; while (($checkdata = fgetcsv($checkhandle, 10000, ",")) !== FALSE) { $count++; if (count($checkdata) != $fieldCount) { echo "Line " . $count . " of your CSV import file does not contain the correct number of columns.<br/>"; echo "Found " . $fieldCount . " header fields, but only " . count($checkdata) . " field in row " . $count . " Please check your file and try again."; exit; } } fclose($checkhandle); echo "<br/>CSV File passed validation...<br/><br/>Detecting carrier type....<br/>"; //exit(); $handle = fopen($file->path, "r"); $data = fgetcsv($handle, 10000, ","); //eDebug($data); $dataset = array(); $carrier = ''; if (trim($data[0]) == 'ShipmentInformationShipmentID') { echo "Detected UPS file...<br/>"; $carrier = "UPS"; $carrierTrackingLink = "http://wwwapps.ups.com/etracking/tracking.cgi?TypeOfInquiryNumber=T&InquiryNumber1="; } elseif (trim($data[0]) == 'PIC') { echo "Detected United States Post Service file...<br/>"; $carrier = "USPS"; $carrierTrackingLink = "http://trkcnfrm1.smi.usps.com/PTSInternetWeb/InterLabelInquiry.do?origTrackNum="; } //eDebug($carrier); $count = 1; $errorSet = array(); $successSet = array(); $oo = new order(); while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) { $count++; $originalOrderId = $data[2]; $data[2] = intval($data[2]); $order = null; $bm = null; $transactionState = null; //check for valid order number - if not present or not order, fail and continue with next record if (isset($data[2]) && !empty($data[2])) { $order = $oo->findBy('invoice_id', $data[2]); if (empty($order->id)) { $errorSet[$count]['message'] = $originalOrderId . " is not a valid order in this system."; $errorSet[$count]['order_id'] = $originalOrderId; continue; } } else { $errorSet[$count]['message'] = "Row " . $count . " has no order number."; $errorSet[$count]['order_id'] = "N/A"; continue; } /*we have a valid order, so let's see what we can do: */ //set status of order to var $currentStat = $order->order_status; //eDebug($currentStat,true); //-- check the order for a closed status - if so, do NOT process or set shipping if ($currentStat->treat_as_closed == true) { $errorSet[$count]['message'] = "This is currently a closed order. Not processing."; $errorSet[$count]['order_id'] = $data[2]; continue; } //ok, if we made it here we have a valid order that is "open" //we'll try to capture the transaction if it's in an authorized state, but set shipping regardless if (isset($order->billingmethod[0])) { $bm = $order->billingmethod[0]; $transactionState = $bm->transaction_state; } else { $bm = null; $transactionState = ''; } if ($transactionState == 'authorized') { //eDebug($order,true); $calc = $bm->billingcalculator->calculator; $calc->config = $bm->billingcalculator->config; if (method_exists($calc, 'delayed_capture')) { //$result = $calc->delayed_capture($bm,$bm->billing_cost); $result = $calc->delayed_capture($bm, $order->grand_total); if ($result->errorCode == 0) { //we've succeeded. transaction already created and billing info updated. //just need to set the order shipping info, check and see if we send user an email, and set statuses. //shipping info: $successSet[$count]['order_id'] = $data[2]; $successSet[$count]['message'] = "Sucessfully captured order " . $data[2] . " and set shipping information."; $successSet[$count]['amount'] = $order->grand_total; $successSet[$count]['request_id'] = $result->request_id; $successSet[$count]['reference_id'] = $result->PNREF; $successSet[$count]['authorization_code'] = $result->AUTHCODE; $successSet[$count]['shipping_tracking_number'] = $data[0]; $successSet[$count]['carrier'] = $carrier; } else { //failed capture, so we report the error but still set the shipping information //because it's already out the door //$failMessage = "Attempted to delay capture order " . $data[2] . " and it failed with the following error: " . $result->errorCode . " - " .$result->message; //if the user seelected to set a different status for failed orders, set it here. /*if(isset($this->params['order_status_fail'][0]) && $this->params['order_status_fail'][0] > -1) { $change = new order_status_changes(); // save the changes $change->from_status_id = $order->order_status_id; //$change->comment = $this->params['comment']; $change->to_status_id = $this->params['order_status_fail'][0]; $change->orders_id = $order->id; $change->save(); // update the status of the order $order->order_status_id = $this->params['order_status_fail'][0]; $order->save(); }*/ $errorSet[$count]['error_code'] = $result->errorCode; $errorSet[$count]['message'] = "Capture failed: " . $result->message . "<br/>Setting shipping information."; $errorSet[$count]['order_id'] = $data[2]; $errorSet[$count]['amount'] = $order->grand_total; $errorSet[$count]['shipping_tracking_number'] = $data[0]; $errorSet[$count]['carrier'] = $carrier; //continue; } } else { //dont suppose we do anything here, as it may be set to approved manually //$errorSet[$count] = "Order " . $data[2] . " does not use a billing method with delayed capture ability."; $successSet[$count]['message'] = 'No capture processing available for order:' . $data[2] . '. Setting shipping information.'; $successSet[$count]['order_id'] = $data[2]; $successSet[$count]['amount'] = $order->grand_total; $successSet[$count]['shipping_tracking_number'] = $data[0]; $successSet[$count]['carrier'] = $carrier; } } else { $successSet[$count]['message'] = 'No processing necessary for order:' . $data[2] . '. Setting shipping information.'; $successSet[$count]['order_id'] = $data[2]; $successSet[$count]['amount'] = $order->grand_total; $successSet[$count]['shipping_tracking_number'] = $data[0]; $successSet[$count]['carrier'] = $carrier; } $order->shipped = time(); $order->shipping_tracking_number = $data[0]; $order->save(); $s = array_pop($order->shippingmethods); $sm = new shippingmethod($s->id); $sm->carrier = $carrier; $sm->save(); //statuses and email if (isset($this->params['order_status_success'][0]) && $this->params['order_status_success'][0] > -1) { $change = new order_status_changes(); // save the changes $change->from_status_id = $order->order_status_id; //$change->comment = $this->params['comment']; $change->to_status_id = $this->params['order_status_success'][0]; $change->orders_id = $order->id; $change->save(); // update the status of the order $order->order_status_id = $this->params['order_status_success'][0]; $order->save(); // email the user if we need to if (!empty($this->params['email_customer'])) { $email_addy = $order->billingmethod[0]->email; if (!empty($email_addy)) { $from_status = $db->selectValue('order_status', 'title', 'id=' . $change->from_status_id); $to_status = $db->selectValue('order_status', 'title', 'id=' . $change->to_status_id); $template->assign(array('comment' => $change->comment, 'to_status' => $to_status, 'from_status' => $from_status, 'order' => $order, 'date' => date("F j, Y, g:i a"), 'storename' => ecomconfig::getConfig('storename'), 'include_shipping' => true, 'tracking_link' => $carrierTrackingLink . $order->shipping_tracking_number, 'carrier' => $carrier)); $html = $template->render(); $html .= ecomconfig::getConfig('footer'); try { $mail = new expMail(); $mail->quickSend(array('html_message' => $html, 'text_message' => str_replace("<br>", "\r\n", $template->render()), 'to' => $email_addy, 'from' => ecomconfig::getConfig('from_address'), 'subject' => 'Your Order Has Been Shipped (#' . $order->invoice_id . ') - ' . ecomconfig::getConfig('storename'))); } catch (Exception $e) { //do nothing for now eDebug("Email error:"); eDebug($e); } } //else { // $errorSet[$count]['message'] .= "<br/>Order " . $data[2] . " was captured successfully, however the email notification was not successful."; //} } } //eDebug($product); } assign_to_template(array('errorSet' => $errorSet, 'successSet' => $successSet)); }
function save_order_item() { $oi = new orderitem($this->params['id']); //eDebug($this->params); /*eDebug($oi); eDebug(expUnserialize($oi->options)); eDebug(expUnserialize($oi->user_input_fields),true);*/ $oi->products_price = $this->params['products_price']; $oi->quantity = $this->params['quantity']; $oi->products_name = $this->params['products_name']; if ($oi->product->parent_id != 0) { $oi->product = new product($oi->product->parent_id, true, false); } else { //reattach the product so we get the optoin fields and such $oi->product = new product($oi->product->id, true, false); } //eDebug($oi->product,true); foreach ($oi->product->optiongroup as $og) { $isOptionEmpty = true; if (!empty($this->params['options'][$og->id])) { foreach ($this->params['options'][$og->id] as $opt) { if (!empty($opt)) { $isOptionEmpty = false; } } } if (!$isOptionEmpty) { foreach ($this->params['options'][$og->id] as $opt_id) { $selected_option = new option($opt_id); $cost = $selected_option->modtype == '$' ? $selected_option->amount : $this->getBasePrice() * ($selected_option->amount * 0.01); $cost = $selected_option->updown == '+' ? $cost : $cost * -1; $options[] = array($selected_option->id, $selected_option->title, $selected_option->modtype, $selected_option->updown, $selected_option->amount); } } } eDebug($this->params); //eDebug($oi,true); $user_input_info = array(); //check user input fields //$this->user_input_fields = expUnserialize($this->user_input_fields); //eDebug($this,true); foreach ($oi->product->user_input_fields as $uifkey => $uif) { /*if ($uif['is_required'] || (!$uif['is_required'] && strlen($params['user_input_fields'][$uifkey]) > 0)) { if (strlen($params['user_input_fields'][$uifkey]) < $uif['min_length']) { //flash('error', 'test'); //redirect_to(array('controller'=>cart, 'action'=>'displayForm', 'form'=>'addToCart', 'product_id'=>$this->id, 'product_type'=>$this->product_type)); $params['error'] .= $uif['name'].' field has a minimum requirement of ' . $uif['min_length'] . ' characters.<br/>'; }else if (strlen($params['user_input_fields'][$uifkey]) > $uif['max_length'] && $uif['max_length'] > 0) { //flash('error', ); //redirect_to(array('controller'=>cart, 'action'=>'displayForm', 'form'=>'addToCart', 'product_id'=>$this->id, 'product_type'=>$this->product_type)); $params['error'] .= $uif['name'].' field has a maximum requirement of ' . $uif['max_length'] . ' characters.<br/>'; } }*/ $user_input_info[] = array($uif['name'] => $this->params['user_input_fields'][$uifkey]); } //eDebug($options); //eDebug($user_input_info,true); $oi->options = serialize($options); $oi->user_input_fields = serialize($user_input_info); //eDebug($oi); $oi->save(); $oi->refresh(); //eDebug($oi,true); $order = new order($oi->orders_id); $order->calculateGrandTotal(); $s = array_pop($order->shippingmethods); eDebug($s); $sm = new shippingmethod($s->id); $shippingCalc = new shippingcalculator($sm->shippingcalculator_id); $calcName = $shippingCalc->calculator_name; $calculator = new $calcName($shippingCalc->id); $pricelist = $calculator->getRates($order); foreach ($pricelist as $rate) { if ($rate['id'] == $sm->option) { $sm->shipping_cost = $rate['cost']; break; } } $sm->save(); $order->refresh(); $order->calculateGrandTotal(); $order->save(); flashAndFlow('message', 'Order item updated and order totals recalculated.'); redirect_to(array('controller' => 'order', 'action' => 'show', 'id' => $this->params['orderid'])); }