<?php

ini_set('include_path', ini_get('include_path') . ':' . $_SERVER['DOCUMENT_ROOT'] . '/admin/includes/');
include_once 'doba/DobaProductData.php';
include_once 'doba/DobaProducts.php';
$tempData = array('product_id' => 1234, 'title' => 'the title of the product', 'product_sku' => 'the sku of the product', 'upc' => 'the upc of the product', 'brand' => 'the brand name of the product', 'description' => 'a description of the product', 'ship_width' => 12, 'ship_height' => 12, 'ship_weight' => 12, 'ship_cost' => 12, 'image_url' => 'www.photodumby.foo', 'thumb_url' => 'www.photodumby2.foo', 'image_height' => 12, 'image_width' => 2, 'last_update' => '2008-01-02 11:54:02', 'items' => array());
$dpd = new DobaProductData();
echo "<ul>";
echo "<li>\"" . $dpd->product_id($tempData['product_id']) . "\" should be \"" . $tempData['product_id'] . "\"</li>";
echo "<li>\"" . $dpd->title($tempData['title']) . "\" should be \"" . $tempData['title'] . "\"</li>";
echo "<li>\"" . $dpd->product_sku($tempData['product_sku']) . "\" should be \"" . $tempData['product_sku'] . "\"</li>";
echo "<li>\"" . $dpd->upc($tempData['upc']) . "\" should be \"" . $tempData['upc'] . "\"</li>";
echo "<li>\"" . $dpd->brand($tempData['brand']) . "\" should be \"" . $tempData['brand'] . "\"</li>";
echo "<li>\"" . $dpd->description($tempData['description']) . "\" should be \"" . $tempData['description'] . "\"</li>";
echo "</ul>";
echo "<p>Done</p>";
$dpdAry = new DobaProducts();
echo "<p>" . ($dpdAry->productExists($tempData['product_id']) ? 'Product added' : 'Product not added') . "</p>";
$dpdAry->addProduct($dpd);
echo "<p>" . ($dpdAry->productExists($tempData['product_id']) ? 'Product added' : 'Product not added') . "</p>";
 /**
  * Proccess a file and store the files elements in a DobaProduts object
  * @static method
  * @return 
  * @param $file string : full path to file on server 
  * @param $type string : "tab" or "csv"
  */
 function processFile($file, $type)
 {
     $delm = '';
     $DobaProds = new DobaProducts();
     $headers;
     $fp = fopen($file, 'r');
     $delm = $type == 'csv' ? ',' : "\t";
     if (!feof($fp)) {
         $data = fgets($fp);
         $tHeaders = explode($delm, $data);
         foreach ($tHeaders as $item) {
             $headers[] = DobaProductFile::pruneQuotes(strtoupper(trim($item)));
         }
     }
     while (!feof($fp)) {
         $values = DobaProductFile::parseLine(fgets($fp), $delm);
         /* Fields to fill in on the osCommerce add product page
          * Products Status:   In Stock,  Out of Stock		. Passing the current available in the object, needs to be processed before being added to database
          * Date Available									/
          * Products Manufacturer							/
          * Products Name (English, Spanish, Ducth)			/
          * Tax Class: none, taxable goods					. Default to Taxable, will need setting in the osCommerce config files
          * Products Price (Net)								. Will use the MSRP for now.
          * Products Description (English, Spanish, Ducth)	/
          * Products Quantity								. Will just use the amount in the file till a better solution is found
          * Products Model									/ = Product SKU
          * Products Image									. Will need more processing while the object in being added to the database
          * Products URL (English, Spanish, Ducth)			/ Leaveing blank
          * Products Weight									/
          */
         $tempDPD = new DobaProductData();
         $temp = array_keys($headers, 'PRODUCT_ID');
         $tempDPD->product_id($values[$temp[0]]);
         $temp = array_keys($headers, 'ITEM_ID');
         $tempDPD->item_id($values[$temp[0]]);
         $temp = array_keys($headers, 'TITLE');
         $tempDPD->title(DobaProductFile::pruneQuotes($values[$temp[0]]));
         $temp = array_keys($headers, 'DESCRIPTION');
         $descr = DobaProductFile::pruneQuotes($values[$temp[0]]);
         $temp = array_keys($headers, 'DETAILS');
         $details = DobaProductFile::pruneQuotes($values[$temp[0]]);
         if (!empty($descr) && !empty($details)) {
             $descr .= '<br><br>' . $details;
         } else {
             $descr .= $details;
         }
         $tempDPD->description($descr);
         $temp = array_keys($headers, 'QTY_AVAIL');
         $supplied_qty = $values[$temp[0]];
         $temp = array_keys($headers, 'IMAGE_URL');
         $tempDPD->image_url(DobaProductFile::pruneQuotes($values[$temp[0]]));
         $temp = array_keys($headers, 'WEIGHT');
         $tempDPD->ship_weight($values[$temp[0]]);
         $temp = array_keys($headers, 'SKU');
         $tempDPD->product_sku(DobaProductFile::pruneQuotes($values[$temp[0]]));
         $temp = array_keys($headers, 'PRICE');
         $wholesale = $values[$temp[0]];
         $temp = array_keys($headers, 'MAP');
         $map = $values[$temp[0]];
         $temp = array_keys($headers, 'MSRP');
         $msrp = $values[$temp[0]];
         /*
          * This set of if statements checks if the specified pricing manipulation fields exist
          * in the file. If they do, the column header name and the value of the field are sent
          * as parameters to DobaInteraction::setPrice(). The supplied wholesale cost, the map price
          * and the msrp are also sent as parameters and used in setPrice() to ensure the correct 
          * price is calculated.
          */
         if (in_array('OSC_WHOLESALE_MARKUP_PERCENT', $headers)) {
             $temp = array_keys($headers, 'OSC_WHOLESALE_MARKUP_PERCENT');
             if (isset($values[$temp[0]]) && !empty($values[$temp[0]])) {
                 $tempDPD->price(DobaInteraction::setPrice(PRICE_FMT_WHOLESALE_PERCENT, $values[$temp[0]], $wholesale, $map, $msrp));
             } else {
                 $tempDPD->price(DobaInteraction::setPrice(PRICE_FMT_NONE, $wholesale, $wholesale, $map, $msrp));
             }
         } elseif (in_array('OSC_WHOLESALE_MARKUP_DOLLAR', $headers)) {
             $temp = array_keys($headers, 'OSC_WHOLESALE_MARKUP_DOLLAR');
             if (isset($values[$temp[0]]) && !empty($values[$temp[0]])) {
                 $tempDPD->price(DobaInteraction::setPrice(PRICE_FMT_WHOLESALE_DOLLAR, $values[$temp[0]], $wholesale, $map, $msrp));
             } else {
                 $tempDPD->price(DobaInteraction::setPrice(PRICE_FMT_NONE, $wholesale, $wholesale, $map, $msrp));
             }
         } elseif (in_array('OSC_MARKUP_EXACT', $headers)) {
             $temp = array_keys($headers, 'OSC_MARKUP_EXACT');
             if (isset($values[$temp[0]]) && !empty($values[$temp[0]])) {
                 $tempDPD->price(DobaInteraction::setPrice(PRICE_FMT_EXACT, $values[$temp[0]], $wholesale, $map, $msrp));
             } else {
                 $tempDPD->price(DobaInteraction::setPrice(PRICE_FMT_NONE, $wholesale, $wholesale, $map, $msrp));
             }
         } elseif (in_array('OSC_MSRP_MARKUP_PERCENT', $headers)) {
             $temp = array_keys($headers, 'OSC_MSRP_MARKUP_PERCENT');
             if (isset($values[$temp[0]]) && !empty($values[$temp[0]])) {
                 $tempDPD->price(DobaInteraction::setPrice(PRICE_FMT_MSRP_PERCENT, $values[$temp[0]], $wholesale, $map, $msrp));
             } else {
                 $tempDPD->price(DobaInteraction::setPrice(PRICE_FMT_NONE, $wholesale, $wholesale, $map, $msrp));
             }
         } elseif (in_array('OSC_MSRP_MARKUP_DOLLAR', $headers)) {
             $temp = array_keys($headers, 'OSC_MSRP_MARKUP_DOLLAR');
             if (isset($values[$temp[0]]) && !empty($values[$temp[0]])) {
                 $tempDPD->price(DobaInteraction::setPrice(PRICE_FMT_MSRP_DOLLAR, $values[$temp[0]], $wholesale, $map, $msrp));
             } else {
                 $tempDPD->price(DobaInteraction::setPrice(PRICE_FMT_NONE, $wholesale, $wholesale, $map, $msrp));
             }
         } else {
             $tempDPD->price(DobaInteraction::setPrice(PRICE_FMT_NONE, $wholesale, $wholesale, $map, $msrp));
         }
         /*
          * This set of if statements checks if the specified quantity manipulation fields exist
          * in the file. If they do, the column header name and the value of the field are sent
          * as parameters to DobaInteraction::setQuantity(). The supplied quantity is also 
          * sent as a parameter and used in setQuantity() to ensure the correct quantity is calculated.
          */
         if (in_array('OSC_QUANTITY_AUTOADJUST', $headers)) {
             $temp = array_keys($headers, 'OSC_QUANTITY_AUTOADJUST');
             if (isset($values[$temp[0]]) && !empty($values[$temp[0]])) {
                 $tempDPD->quantity(DobaInteraction::setQuantity(QTY_FMT_AUTOADJUST, $values[$temp[0]], $supplied_qty));
             } else {
                 $tempDPD->quantity(DobaInteraction::setQuantity(QTY_FMT_NONE, $supplied_qty, $supplied_qty));
             }
         } elseif (in_array('OSC_QUANTITY_EXACT', $headers)) {
             $temp = array_keys($headers, 'OSC_QUANTITY_EXACT');
             if (isset($values[$temp[0]]) && !empty($values[$temp[0]])) {
                 $tempDPD->quantity(DobaInteraction::setQuantity(QTY_FMT_EXACT, $values[$temp[0]], $supplied_qty));
             } else {
                 $tempDPD->quantity(DobaInteraction::setQuantity(QTY_FMT_NONE, $supplied_qty, $supplied_qty));
             }
         } else {
             $tempDPD->quantity(DobaInteraction::setQuantity(QTY_FMT_NONE, $supplied_qty, $supplied_qty));
         }
         /*
          * if the OSC_CATEGORY field exists, the category name is loaded into the DobaProductsData
          * object so that it can be processed.
          */
         if (in_array('OSC_CATEGORY', $headers)) {
             $tempCategory = array_keys($headers, 'OSC_CATEGORY');
             if (isset($values[$tempCategory[0]]) && !empty($values[$tempCategory[0]])) {
                 $tempDPD->category_name(DobaProductFile::pruneQuotes($values[$tempCategory[0]]));
             }
         }
         $temp = array_keys($headers, 'BRAND');
         $tempDPD->brand(DobaProductFile::pruneQuotes($values[$temp[0]]));
         /*
          * if the OSC_BRAND field exists, the brand name is loaded into the DobaProductsData
          * object so that it can be processed.
          */
         if (in_array('OSC_BRAND', $headers)) {
             $tempBrand = array_keys($headers, 'OSC_BRAND');
             if (isset($values[$tempBrand[0]]) && !empty($values[$tempBrand[0]])) {
                 $tempDPD->brand(DobaProductFile::pruneQuotes($values[$tempBrand[0]]));
             }
         }
         /*
          * if the OSC_PRODUCT_LINK field exists, the brand name is loaded into the DobaProductsData
          * object so that it can be processed.
          */
         if (in_array('OSC_PRODUCT_LINK', $headers)) {
             $temp = array_keys($headers, 'OSC_PRODUCT_LINK');
             if (isset($values[$temp[0]]) && !empty($values[$temp[0]])) {
                 $tempDPD->product_url(DobaProductFile::pruneQuotes($values[$temp[0]]));
             }
         }
         $DobaProds->addProduct($tempDPD);
     }
     fclose($fp);
     return $DobaProds;
 }
 function parseProductDetails($data)
 {
     $productList = new DobaProducts();
     $p = new XMLParser($data);
     $ProdDetails = $p->getOutput();
     if ($ProdDetails['dce']['response']['outcome'] == 'success') {
         if (array_key_exists('product', $ProdDetails['dce']['response']['products'])) {
             //Only 1 product;
             $prod = $ProdDetails['dce']['response']['products']['product'];
             $tempDPDArray = DobaProductAPI::findItems($prod);
             foreach ($tempDPDArray as $currProd) {
                 $currProd->product_id($prod['product_id']);
                 $currProd->title($prod['title']);
                 $currProd->description($prod['description']);
                 DobaProductAPI::findImage($prod, $currProd);
                 $currProd->product_sku($prod['product_sku']);
                 $currProd->ship_weight($prod['ship_weight']);
                 $currProd->ship_cost($prod['ship_cost']);
                 $currProd->upc($prod['upc']);
                 $currProd->brand($prod['brand']);
                 if (isset($prod['OSC_BRAND'])) {
                     $currProd->brand($prod['OSC_BRAND']);
                 }
                 $currProd->category_name('');
                 if (isset($prod['OSC_CATEGORY'])) {
                     $currProd->category_name($prod['OSC_CATEGORY']);
                 }
                 //Set Price
                 if (isset($prod['OSC_WHOLESALE_MARKUP_PERCENT'])) {
                     $currProd->price(DobaInteraction::setPrice('osc_wholesale_markup_percent', $prod['OSC_WHOLESALE_MARKUP_PERCENT'], $currProd->wholesale_price(), $currProd->map(), $currProd->msrp()));
                 } elseif (isset($prod['OSC_WHOLESALE_MARKUP_DOLLAR'])) {
                     $currProd->price(DobaInteraction::setPrice('osc_wholesale_markup_dollar', $prod['OSC_WHOLESALE_MARKUP_DOLLAR'], $currProd->wholesale_price(), $currProd->map(), $currProd->msrp()));
                 } elseif (isset($prod['OSC_MARKUP_EXACT'])) {
                     $currProd->price(DobaInteraction::setPrice('osc_markup_exact', $prod['OSC_MARKUP_EXACT'], $currProd->wholesale_price(), $currProd->map(), $currProd->msrp()));
                 } elseif (isset($prod['OSC_MSRP_MARKUP_PERCENT'])) {
                     $currProd->price(DobaInteraction::setPrice('osc_msrp_markup_percent', $prod['OSC_MSRP_MARKUP_PERCENT'], $currProd->wholesale_price(), $currProd->map(), $currProd->msrp()));
                 } elseif (isset($prod['OSC_MSRP_MARKUP_DOLLAR'])) {
                     $currProd->price(DobaInteraction::setPrice('osc_msrp_markup_dollar', $prod['OSC_MSRP_MARKUP_DOLLAR'], $currProd->wholesale_price(), $currProd->map(), $currProd->msrp()));
                 } else {
                     $currProd->price(DobaInteraction::setPrice('none', $currProd->wholesale_price(), $currProd->wholesale_price(), $currProd->map(), $currProd->msrp()));
                 }
                 //Set Quantity
                 if (isset($prod['OSC_QUANTITY_AUTOADJUST'])) {
                     $currProd->quantity(DobaInteraction::setQuantity('osc_quantity_autoadjust', $prod['OSC_QUANTITY_AUTOADJUST'], $currProd->quantity));
                 } elseif (isset($prod['OSC_QUANTITY_EXACT'])) {
                     $currProd->quantity(DobaInteraction::setQuantity('osc_quantity_exact', $prod['OSC_QUANTITY_EXACT'], $currProd->quantity));
                 } else {
                     $currProd->quantity(DobaInteraction::setQuantity('none', $currProd->quantity, $currProd->quantity));
                 }
                 $productList->addProduct($currProd);
             }
         } else {
             foreach ($ProdDetails['dce']['response']['products'] as $prod) {
                 $tempDPDArray = DobaProductAPI::findItems($prod);
                 foreach ($tempDPDArray as $currProd) {
                     $currProd->product_id($prod['product_id']);
                     $currProd->title($prod['title']);
                     $currProd->description($prod['description']);
                     DobaProductAPI::findImage($prod, $currProd);
                     $currProd->product_sku($prod['product_sku']);
                     $currProd->ship_weight($prod['ship_weight']);
                     $currProd->ship_cost($prod['ship_cost']);
                     $currProd->upc($prod['upc']);
                     $currProd->brand($prod['brand']);
                     if (isset($prod['OSC_BRAND'])) {
                         $currProd->brand($prod['OSC_BRAND']);
                     } else {
                         if (isset($prod['osc_brand'])) {
                             $currProd->brand($prod['osc_brand']);
                         }
                     }
                     $currProd->category_name('');
                     if (isset($prod['OSC_CATEGORY'])) {
                         $currProd->category_name($prod['OSC_CATEGORY']);
                     } else {
                         if (isset($prod['osc_category'])) {
                             $currProd->category_name($prod['osc_category']);
                         }
                     }
                     //Set Price
                     if (isset($prod['OSC_WHOLESALE_MARKUP_PERCENT'])) {
                         $currProd->price(DobaInteraction::setPrice('osc_wholesale_markup_percent', $prod['OSC_WHOLESALE_MARKUP_PERCENT'], $currProd->wholesale_price(), $currProd->map(), $currProd->msrp()));
                     } elseif (isset($prod['OSC_WHOLESALE_MARKUP_DOLLAR'])) {
                         $currProd->price(DobaInteraction::setPrice('osc_wholesale_markup_dollar', $prod['OSC_WHOLESALE_MARKUP_DOLLAR'], $currProd->wholesale_price(), $currProd->map(), $currProd->msrp()));
                     } elseif (isset($prod['OSC_MARKUP_EXACT'])) {
                         $currProd->price(DobaInteraction::setPrice('osc_markup_exact', $prod['OSC_MARKUP_EXACT'], $currProd->wholesale_price(), $currProd->map(), $currProd->msrp()));
                     } elseif (isset($prod['OSC_MSRP_MARKUP_PERCENT'])) {
                         $currProd->price(DobaInteraction::setPrice('osc_msrp_markup_percent', $prod['OSC_MSRP_MARKUP_PERCENT'], $currProd->wholesale_price(), $currProd->map(), $currProd->msrp()));
                     } elseif (isset($prod['OSC_MSRP_MARKUP_DOLLAR'])) {
                         $currProd->price(DobaInteraction::setPrice('osc_msrp_markup_dollar', $prod['OSC_MSRP_MARKUP_DOLLAR'], $currProd->wholesale_price(), $currProd->map(), $currProd->msrp()));
                     } else {
                         $currProd->price(DobaInteraction::setPrice('none', $currProd->wholesale_price(), $currProd->wholesale_price(), $currProd->map(), $currProd->msrp()));
                     }
                     //Set Quantity
                     if (isset($prod['OSC_QUANTITY_AUTOADJUST'])) {
                         $currProd->quantity(DobaInteraction::setQuantity('osc_quantity_autoadjust', $prod['OSC_QUANTITY_AUTOADJUST'], $currProd->quantity));
                     } elseif (isset($prod['OSC_QUANTITY_EXACT'])) {
                         $currProd->quantity(DobaInteraction::setQuantity('osc_quantity_exact', $prod['OSC_QUANTITY_EXACT'], $currProd->quantity));
                     } else {
                         $currProd->quantity(DobaInteraction::setQuantity('none', $currProd->quantity, $currProd->quantity));
                     }
                     $productList->addProduct($currProd);
                 }
             }
         }
         return $productList;
     } else {
         return $ProdDetails['dce']['response']['outcome'];
     }
 }