/** * Add all the products * * Method will add all the products within the $input['products'] array * * @access private * @param array &$input The referenced input data * @return bool TRUE if all the products were added, FALSE otherwise */ private function addProducts(&$input, $editingExisting = false) { if (!array_key_exists('products', $input) || !is_array($input['products'])) { return false; } $existingOrder = array(); if ($editingExisting) { $existingOrder = GetOrder($input['orderid'], true); } $couponsUsed = array(); $giftCertificates = array(); foreach ($input['products'] as $product) { $existingProduct = false; if (isset($product['existing_order_product']) && isset($existingOrder['products'][$product['existing_order_product']])) { $existingProduct = $existingOrder['products'][$product['existing_order_product']]; unset($existingOrder['products'][$product['existing_order_product']]); } if (!isset($product['product_code'])) { $product['product_code'] = ''; } if (!isset($product['variation_id'])) { $product['variation_id'] = 0; } if (isset($product['discount_price'])) { $price = $product['discount_price']; } else { $price = $product['product_price']; } // Set up some default values for the product $newProduct = array('ordprodsku' => $product['product_code'], "ordprodname" => $product['product_name'], "ordprodtype" => '', "ordprodcost" => $price, "ordprodoriginalcost" => $product['product_price'], "ordprodweight" => 0, "ordprodqty" => $product['quantity'], "orderorderid" => $input['orderid'], "ordprodid" => $product['product_id'], "ordprodvariationid" => $product['variation_id'], "ordprodoptions" => '', "ordprodcostprice" => 0, "ordprodfixedshippingcost" => 0, "ordprodistaxable" => 1); //YMM info added by Simha $newProduct['ordyear'] = $product['year']; $newProduct['ordmake'] = $product['make']; $newProduct['ordmodel'] = $product['model']; //blessen if (isset($input['offerid'])) { $newProduct['offerid'] = $input['offerid']; } /* $mmyvals = array( "omake" => $product['make'], "model" => $product['model'], "year" => $product['year'], ); $MMYInfo = serialize($mmyvals); $newProduct['mmyinfo'] = $MMYInfo; */ //YMM info added by Simha Ends // This product is a gift certificate so set the appropriate values if (isset($product['type']) && $product['type'] == "giftcertificate") { // Gift certificates can't be edited if (isset($product['existing_order_product'])) { continue; } $newProduct['ordprodtype'] = 'giftcertificate'; $giftCertificates[] = $product; } else { if (isset($product['data'])) { $newProduct['ordprodtype'] = $product['data']['prodtype']; } else { $newProduct['ordprodtype'] = 'physical'; } } if (isset($product['data']['prodcostprice'])) { $newProduct['ordprodcostprice'] = (double) $product['data']['prodcostprice']; } if (isset($product['options'])) { $newProduct['ordprodoptions'] = serialize($product['options']); } if (isset($product['data']['prodweight'])) { $newProduct['ordprodweight'] = $product['data']['prodweight']; } if (isset($product['data']['prodfixedshippingcost'])) { $newProduct['ordprodfixedshippingcost'] = $product['data']['prodfixedshippingcost']; } if (isset($product['data']['prodistaxable'])) { $newProduct['ordprodistaxable'] = $product['data']['prodistaxable']; } if (isset($product['event_date']) && isset($product['event_name'])) { $newProduct['ordprodeventdate'] = $product['event_date']; $newProduct['ordprodeventname'] = $product['event_name']; } // If wrapping has been applied to this product, add it in if (isset($product['wrapping'])) { $newProduct['ordprodwrapid'] = $product['wrapping']['wrapid']; $newProduct['ordprodwrapname'] = $product['wrapping']['wrapname']; $newProduct['ordprodwrapcost'] = $product['wrapping']['wrapprice']; if (isset($product['wrapping']['wrapmessage'])) { $newProduct['ordprodwrapmessage'] = $product['wrapping']['wrapmessage']; } } if (isset($product['original_price'])) { $newProduct['ordoriginalprice'] = $product['original_price']; } if (is_array($existingProduct)) { $ordProdID = $existingProduct['orderprodid']; $GLOBALS['ISC_CLASS_DB']->UpdateQuery('order_products', $newProduct, "orderprodid='" . (int) $ordProdID . "'"); // Delete any existing product fields we don't have $query = "\n\t\t\t\t\tSELECT orderfieldid, filename\n\t\t\t\t\tFROM [|PREFIX|]order_configurable_fields\n\t\t\t\t\tWHERE ordprodid='" . $ordProdID . "' AND fieldtype='file'\n\t\t\t\t"; $result = $GLOBALS['ISC_CLASS_DB']->Query($query); while ($field = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) { @unlink(ISC_BASE_PATH . '/' . GetConfig('ImageDirectory') . '/configured_products/' . $field['filename']); } $GLOBALS['ISC_CLASS_DB']->DeleteQuery('order_configurable_fields', "WHERE ordprodid='" . $ordProdID . "'"); } else { $ordProdID = $GLOBALS['ISC_CLASS_DB']->InsertQuery("order_products", $newProduct); /* To insert the complementary items to order_products table -- Baskaran */ if ($product['compitem'] == 1) { for ($y = 0; $y < count($product['complementary']); $y++) { $compprice = $product['complementary'][$y]['comp_original_price']; $CompProduct = array('ordprodsku' => $product['complementary'][$y]['comp_product_code'], "ordprodname" => $product['complementary'][$y]['comp_product_name'], "ordprodtype" => '', "ordprodcost" => $product['complementary'][$y]['comp_original_price'], "ordprodoriginalcost" => $product['complementary'][$y]['comp_original_price'], "ordprodweight" => 0, "ordprodqty" => $product['complementary'][$y]['quantity'], "orderorderid" => $input['orderid'], "ordprodid" => $product['complementary'][$y]['comp_productid'], "ordprodvariationid" => '', "ordprodoptions" => '', "ordprodcostprice" => 0, "ordprodfixedshippingcost" => 0, "ordprodistaxable" => 1, "ordoriginalprice" => $product['complementary'][$y]['comp_original_price'], "ordcompmainproductid" => $product['complementary'][$y]['comp_mainproductid']); /* $path = ISC_BASE_PATH."/simha.txt"; $fp = fopen($path, "w+"); if ($fp) { fwrite($fp, implode("\r\n", $CompProduct)."\r\n----------------------\r\n"); fclose($fp); } */ $ordProdID1 = $GLOBALS['ISC_CLASS_DB']->InsertQuery("order_products", $CompProduct); } } /* Code Ends */ } // Add configurable product fields come with the order to database if (isset($product['product_fields'])) { foreach ($product['product_fields'] as $fieldId => $field) { //move the uploaded file to configured_products folder from the temp folder. if ($field['fieldType'] == 'file' && trim($field['fileName']) != '') { $filePath = ISC_BASE_PATH . '/' . GetConfig('ImageDirectory') . '/configured_products/' . $field['fileName']; $fileTmpPath = ISC_BASE_PATH . '/' . GetConfig('ImageDirectory') . '/configured_products_tmp/' . $field['fileName']; //do not remove the temp file here, because the payment may not successful //the file should still be viewable in in the cart, @copy($fileTmpPath, $filePath); } $fieldArray = array('ordprodid' => (int) $ordProdID, 'fieldid' => (int) $fieldId, 'orderid' => (int) $input['orderid'], 'fieldname' => $field['fieldName'], 'fieldtype' => $field['fieldType'], 'textcontents' => '', 'filename' => '', 'filetype' => '', 'originalfilename' => '', 'productid' => $product['product_id']); if ($field['fieldType'] == 'file' && trim($field['fileName']) != '') { $fieldArray['filename'] = trim($field['fileName']); $fieldArray['filetype'] = trim($field['fileType']); $fieldArray['originalfilename'] = trim($field['fileOriginName']); } else { $fieldArray['textcontents'] = trim($field['fieldValue']); } $GLOBALS['ISC_CLASS_DB']->InsertQuery("order_configurable_fields", $fieldArray); } } // Ensure that coupons aren't being saved with gift certificates if (isset($product['couponcode'])) { $newOrderCoupon = array("ordcouporderid" => $input['orderid'], "ordcoupprodid" => $ordProdID, "ordcouponid" => $product['coupon'], "ordcouponcode" => $product['couponcode'], "ordcouponamount" => $product['discount'], "ordcoupontype" => $product['coupontype']); $update_coup = false; if (is_array($existingProduct)) { $query = "SELECT ordcoupid FROM [|PREFIX|]order_coupons WHERE ordcoupprodid = '" . $ordProdID . "'"; $result = $GLOBALS['ISC_CLASS_DB']->Query($query); if ($row = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) { $GLOBALS['ISC_CLASS_DB']->UpdateQuery("order_coupons", $newOrderCoupon, "ordcoupid = " . $row["ordcoupid"]); $update_coup = true; } } if (!$update_coup) { $GLOBALS['ISC_CLASS_DB']->InsertQuery("order_coupons", $newOrderCoupon); } } else { if (is_array($existingProduct)) { $GLOBALS['ISC_CLASS_DB']->DeleteQuery('order_coupons', "WHERE ordcoupprodid='" . $ordProdID . "'"); } } if (isset($existingOrder['ordinventoryupdated']) && $existingOrder['ordinventoryupdated'] == 1) { // If we're editing an existing order and the quantities or variation have changed, do we need to // update the inventory quantities? if (is_array($existingProduct) && $existingProduct['ordprodvariationid'] != $newProduct['ordprodvariationid'] || $existingProduct['ordprodqty'] != $newProduct['ordprodqty']) { AdjustProductInventory($existingProduct['ordprodid'], $existingProduct['ordprodvariationid'], @$product['data']['prodinvtrack'], '+' . $existingProduct['ordprodqty']); AdjustProductInventory($newProduct['ordprodid'], $newProduct['ordprodvariationid'], @$product['data']['prodinvtrack'], '-' . $newProduct['ordprodqty']); } else { if (!is_array($existingProduct)) { AdjustProductInventory($newProduct['ordprodid'], $newProduct['ordprodvariationid'], @$product['data']['prodinvtrack'], '+' . $newProduct['ordprodqty']); } } } } // If we have one or more gift certificates to create, we need to create them now. if (count($giftCertificates) > 0) { $GLOBALS['ISC_CLASS_GIFT_CERTIFICATES'] = GetClass('ISC_GIFTCERTIFICATES'); $GLOBALS['ISC_CLASS_GIFT_CERTIFICATES']->CreateGiftCertificatesFromOrder($input['orderid'], $giftCertificates, 1); } // Now remove any deleted items from the order if ($editingExisting) { $removeItemIds = implode(',', array_keys($existingOrder['products'])); if ($removeItemIds != '') { $query = "\n\t\t\t\t\t\t\tSELECT op.orderprodid, p.productid, p.prodinvtrack\n\t\t\t\t\t\t\tFROM [|PREFIX|]order_products as op\n\t\t\t\t\t\t\tINNER JOIN [|PREFIX|]products as p\n\t\t\t\t\t\t\tON op.ordprodid = p.productid\n\t\t\t\t\t\t\tWHERE op.orderprodid IN (" . $removeItemIds . ") AND ordprodid > 0\n\t\t\t\t\t\t"; $result = $GLOBALS['ISC_CLASS_DB']->Query($query); while ($prod = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) { $existingOrder['products'][$prod['orderprodid']]['prodinvtrack'] = $prod['prodinvtrack']; } //update product inventory level foreach ($existingOrder['products'] as $rmProd) { if (!$rmProd['ordprodid']) { continue; } AdjustProductInventory($rmProd['ordprodid'], $rmProd['ordprodvariationid'], $rmProd['prodinvtrack'], '+' . $rmProd['ordprodqty']); } // Delete any existing product fields we don't have $query = "\n\t\t\t\t\tSELECT orderfieldid, filename\n\t\t\t\t\tFROM [|PREFIX|]order_configurable_fields\n\t\t\t\t\tWHERE ordprodid IN (" . $removeItemIds . ") AND fieldtype='file'\n\t\t\t\t"; $result = $GLOBALS['ISC_CLASS_DB']->Query($query); while ($field = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) { @unlink(ISC_BASE_PATH . '/' . GetConfig('ImageDirectory') . '/configured_products/' . $field['filename']); } $GLOBALS['ISC_CLASS_DB']->DeleteQuery('order_configurable_fields', "WHERE ordprodid IN (" . $removeItemIds . ")"); $GLOBALS['ISC_CLASS_DB']->DeleteQuery('order_products', "WHERE orderprodid IN (" . $removeItemIds . ")"); $GLOBALS['ISC_CLASS_DB']->DeleteQuery('order_coupons', "WHERE ordcoupprodid IN (" . $removeItemIds . ")"); } } return true; }
/** * Increase the inventory for items from an order when an order is returned. Also updates the number sold (decreases it) * * @param int The order ID * @return boolean True if successful */ function UpdateInventoryOnReturn($orderId) { $order = GetOrder($orderId, false); if (!$order) { return false; } // Fetch all of the products in this order // (we can't use the function above because we also need to fetch the inventory tracking option for each product) $query = sprintf(" SELECT op.*, p.prodinvtrack FROM [|PREFIX|]order_products op LEFT JOIN [|PREFIX|]products p ON (p.productid=op.ordprodid) WHERE orderorderid='%d' and ordprodtype!=3", $orderId ); $result = $GLOBALS['ISC_CLASS_DB']->Query($query); while ($product = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) { AdjustProductInventory($product['ordprodid'], $product['ordprodvariationid'], $product['prodinvtrack'], '+'.$product['ordprodqty']); } // Update this order to say we've increased the quantity $updatedOrder = array('ordinventoryupdated' => 0); $GLOBALS['ISC_CLASS_DB']->UpdateQuery("orders", $updatedOrder, sprintf("orderid='%d'", $GLOBALS['ISC_CLASS_DB']->Quote($orderId))); return true; }
/** * Marks the specified order as deleted and adjusts store inventory levels as though the order was actually deleted. * The order can be restored with undelete(). To permanently remove all data for an order, use purge() instead. * * @param int $orderId The order id to delete, or the array result of entity->get() -- providing full data saves a db select call * @param mixed $extraOption see ISC_ENTITY_BASE::delete() * @return bool false on error otherwise true */ public function delete ($orderId, $extraOption = false) { // note: do not call parent::delete(), save that for purge() instead if (is_array($orderId) && isset($orderId[$this->primaryKeyName])) { // order data provided $order = $orderId; $orderId = (int)$order[$this->primaryKeyName]; } else { // get order data $orderId = (int)$orderId; $order = $this->get($orderId); } if (!$orderId || !$order) { return false; } if ($order['deleted']) { return true; } $GLOBALS["ISC_CLASS_LOG"]->LogSystemDebug("general", "Marking order " . $orderId . " as deleted."); // query directly because we can't use edit(): parseInput() for orders is expecting an ISC_QUOTE instance which // we don't want or need at this point $update = array( 'ordlastmodified' => time(), 'deleted' => 1, ); if (!$this->db->UpdateQuery('orders', $update, 'orderid = ' . $orderId)) { return false; } if (!GetConfig('UpdateInventoryOnOrderDelete') || !$order['ordinventoryupdated']) { // either the store is not configured to adjust inventory on order deletes or the order has not adjusted // stock levels yet - nothing more to do return true; } // put inventory for this order back into the store - find products even if they have inv tracking off because // AdjustProductInventory will adjust numsold too $products = " SELECT op.orderprodid, op.ordprodid, op.ordprodvariationid, op.ordprodqty, op.orderorderid, p.productid, p.prodinvtrack FROM [|PREFIX|]order_products op, [|PREFIX|]products p WHERE op.orderorderid = " . $orderId . " AND p.productid = op.ordprodid "; $products = $this->db->Query($products); if (!$products) { return false; } while ($product = $this->db->Fetch($products)) { if (!AdjustProductInventory($product['productid'], $product['ordprodvariationid'], $product['prodinvtrack'], $product['ordprodqty'] )) { return false; } } return true; }