/** * Changes the status of an order * @author pablo * @author soeren * @author Uli * * * @param array $d * @return boolean */ function order_status_update(&$d) { global $mosConfig_offset; $db = new ps_DB(); //$timestamp = time() + ($mosConfig_offset*60*60); //Original $timestamp = time(); //Custom //$mysqlDatetime = date("Y-m-d G:i:s",$timestamp); //Original $mysqlDatetime = date("Y-m-d G:i:s", $timestamp + $mosConfig_offset * 60 * 60); //Custom if (empty($_REQUEST['include_comment'])) { $include_comment = "N"; } // get the current order status $curr_order_status = @$d["current_order_status"]; $notify_customer = empty($d['notify_customer']) ? "N" : $d['notify_customer']; if ($notify_customer == "Y") { $notify_customer = 1; } else { $notify_customer = 0; } $d['order_comment'] = empty($d['order_comment']) ? "" : $d['order_comment']; if (empty($d['order_item_id'])) { // When the order is set to "confirmed", we can capture // the Payment with authorize.net if ($curr_order_status == "P" && $d["order_status"] == "C") { $q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE "; $q .= "#__{vm}_order_payment.order_id='" . $db->getEscaped($d['order_id']) . "' "; $q .= "AND #__{vm}_orders.order_id='" . $db->getEscaped($d['order_id']) . "' "; $q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id"; $db->query($q); $db->next_record(); $payment_class = $db->f("payment_class"); $d["order_number"] = $db->f("order_number"); switch ($payment_class) { case "ps_authorize": require_once CLASSPATH . "payment/ps_authorize.cfg.php"; if (AN_TYPE == 'AUTH_ONLY') { require_once CLASSPATH . "payment/ps_authorize.php"; $authorize = new ps_authorize(); if (!$authorize->capture_payment($d)) { return false; } } break; default: // default case for payment methods that allow to "capture" the payment if (is_file(CLASSPATH . 'payment/' . basename($payment_class))) { require_once CLASSPATH . 'payment/' . basename($payment_class); if (!class_exists($payment_class)) { break; } $paymentObj = new $payment_class(); if (!method_exists($paymentObj, 'capture_payment')) { break; } if (!$paymentObj->capture_payment($d)) { return false; } } } } /* * This is like the test above for delayed capture only * we (well, I - durian) don't think the credit card * should be captured until the item(s) are shipped. * In fact, VeriSign says not to capture the cards until * the item ships. Maybe this behavior should be a * configurable item? * * When the order changes from Confirmed or Pending to * Shipped, perform the delayed capture. * * Restricted to PayFlow Pro for now. */ if (($curr_order_status == "P" || $curr_order_status == "C") && $d["order_status"] == "S") { $q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE "; $q .= "#__{vm}_order_payment.order_id='" . $db->getEscaped($d['order_id']) . "' "; $q .= "AND #__{vm}_orders.order_id='" . $db->getEscaped($d['order_id']) . "' "; $q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id"; $db->query($q); $db->next_record(); $payment_class = $db->f("payment_class"); if ($payment_class == "payflow_pro") { require_once CLASSPATH . "payment/payflow_pro.cfg.php"; if (PFP_TYPE == 'A') { require_once CLASSPATH . "payment/payflow_pro.php"; $pfp = new ps_pfp(); $d["order_number"] = $db->f("order_number"); if (!$pfp->capture_payment($d)) { return false; } } } } /* * If a pending order gets cancelled, void the authorization. * * It might work on captured cards too, if we want to * void shipped orders. * * Restricted to PayFlow Pro for now. */ if ($curr_order_status == "P" && $d["order_status"] == "X") { $q = "SELECT order_number,payment_class,order_payment_trans_id FROM #__{vm}_payment_method,#__{vm}_order_payment,#__{vm}_orders WHERE "; $q .= "#__{vm}_order_payment.order_id='" . $db->getEscaped($d['order_id']) . "' "; $q .= "AND #__{vm}_orders.order_id='" . $db->getEscaped($d['order_id']) . "' "; $q .= "AND #__{vm}_order_payment.payment_method_id=#__{vm}_payment_method.payment_method_id"; $db->query($q); $db->next_record(); $payment_class = $db->f("payment_class"); if ($payment_class == "payflow_pro") { require_once CLASSPATH . "payment/payflow_pro.cfg.php"; if (PFP_TYPE == 'A') { require_once CLASSPATH . "payment/payflow_pro.php"; $pfp = new ps_pfp(); $d["order_number"] = $db->f("order_number"); if (!$pfp->void_authorization($d)) { return false; } } } } $fields = array('order_status' => $d["order_status"], 'mdate' => $timestamp); $db->buildQuery('UPDATE', '#__{vm}_orders', $fields, "WHERE order_id='" . $db->getEscaped($d["order_id"]) . "'"); $db->query(); // Update the Order History. $fields = array('order_id' => $d["order_id"], 'order_status_code' => $d["order_status"], 'date_added' => $mysqlDatetime, 'customer_notified' => $notify_customer, 'comments' => $d['order_comment']); $db->buildQuery('INSERT', '#__{vm}_order_history', $fields); $db->query(); // Do we need to re-update the Stock Level? if ((strtoupper($d["order_status"]) == "X" || strtoupper($d["order_status"]) == "R") && $curr_order_status != $d["order_status"]) { // Get the order items and update the stock level // to the number before the order was placed $q = "SELECT product_id, product_quantity FROM #__{vm}_order_item WHERE order_id='" . $db->getEscaped($d["order_id"]) . "'"; $db->query($q); $dbu = new ps_DB(); require_once CLASSPATH . 'ps_product.php'; // Now update each ordered product while ($db->next_record()) { if (ENABLE_DOWNLOADS == '1' && ps_product::is_downloadable($db->f("product_id")) && VM_DOWNLOADABLE_PRODUCTS_KEEP_STOCKLEVEL == '1') { $q = "UPDATE #__{vm}_product \r\n\t\t\t\t\t\t\t\tSET product_sales=product_sales-" . $db->f("product_quantity") . " \r\n\t\t\t\t\t\t\tWHERE product_id=" . $db->f("product_id"); $dbu->query($q); } else { $q = "UPDATE #__{vm}_product \r\n\t\t\t\t\t\t\tSET product_in_stock=product_in_stock+" . $db->f("product_quantity") . ",\r\n\t\t\t\t\t\t\t\tproduct_sales=product_sales-" . $db->f("product_quantity") . " \r\n\t\t\t\t\t\t\tWHERE product_id=" . $db->f("product_id"); $dbu->query($q); } } } // Update the Order Items' status $q = "SELECT order_item_id FROM #__{vm}_order_item WHERE order_id=" . $db->getEscaped($d['order_id']); $db->query($q); $dbu = new ps_DB(); while ($db->next_record()) { $item_id = $db->f("order_item_id"); $fields = array('order_status' => $d["order_status"], 'mdate' => $timestamp); $dbu->buildQuery('UPDATE', '#__{vm}_order_item', $fields, "WHERE order_item_id='" . (int) $item_id . "'"); $dbu->query(); } if (ENABLE_DOWNLOADS == '1') { ################## ## DOWNLOAD MOD $this->mail_download_id($d); } if (!empty($notify_customer)) { $this->notify_customer($d); } } elseif (!empty($d['order_item_id'])) { // Update the Order Items' status $q = "SELECT order_item_id, product_id, product_quantity FROM #__{vm}_order_item \r\n\t\t\t\t\t\t\tWHERE order_id=" . $db->getEscaped($d['order_id']) . ' AND order_item_id=' . intval($d['order_item_id']); $db->query($q); $item_product_id = $db->f('product_id'); $item_product_quantity = $db->f('product_quantity'); if (ENABLE_DOWNLOADS == '1' && ps_product::is_downloadable($item_product_id) && VM_DOWNLOADABLE_PRODUCTS_KEEP_STOCKLEVEL == '1') { $q = "UPDATE #__{vm}_product \r\n\t\t\t\t\t\t\t\tSET product_sales=product_sales-" . $item_product_quantity . " \r\n\t\t\t\t\t\t\tWHERE product_id=" . $item_product_id; $db->query($q); } else { $q = "UPDATE #__{vm}_product \r\n\t\t\t\t\t\t\tSET product_in_stock=product_in_stock+" . $item_product_quantity . ",\r\n\t\t\t\t\t\t\t\tproduct_sales=product_sales-" . $item_product_quantity . " \r\n\t\t\t\t\t\t\tWHERE product_id=" . $item_product_id; $db->query($q); } $fields = array('order_status' => $d["order_status"], 'mdate' => $timestamp); $db->buildQuery('UPDATE', '#__{vm}_order_item', $fields, 'WHERE order_item_id=' . intval($d['order_item_id'])); return $db->query() !== false; } return true; }