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 setStatus() { global $db, $template; if (empty($this->params['id'])) { expHistory::back(); } // get the order and create a new order_Status_change $order = new order($this->params['id']); //set order type if (isset($this->params['order_type_id'])) { $order->order_type_id = $this->params['order_type_id']; } //only save the status change if it actually changed to something different if ($order->order_status_id != $this->params['order_status_id']) { $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_id']; $change->orders_id = $order->id; $change->save(); // update the status of the order $order->order_status_id = $this->params['order_status_id']; // Save the message for future use if that is what the user wanted. if (!empty($this->params['save_message'])) { $message->body = $this->params['comment']; $db->insertObject($message, 'order_status_messages'); } // email the user if we need to if (!empty($this->params['email_user'])) { $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); assign_to_template(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' => isset($this->params['include_shipping_info']) ? true : false)); $html = $template->render(); $html .= ecomconfig::getConfig('footer'); $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' => 'The status of your order (#' . $order->invoice_id . ') has been updated on ' . ecomconfig::getConfig('storename') . '.')); } else { flash('error', gt('The email address was NOT send. An email address count not be found for this customer')); } } } $order->save(); flash('message', gt('Order Type and/or Status Updated.')); expHistory::back(); }