<?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 findItems($ItemDetails)
 {
     /* 
             <items>       
                 <item>
             </item_id>
             </item_sku>
             </name><!-- used to differentiate between sizes or colors in multiple item products -->
             </price>
             </prepay_price>                
             </msrp>
             </map><!-- Minimum Advertised Price - retailers cannot advertise this product below this amount -->
             </qty_avail>
             </stock><!-- in-stock, out-of-stock or discontinued -->
             </last_update><!-- date the item was last updated --> 
                 </item>
             </items>		
     */
     $itemList = array();
     if (array_key_exists('item', $ItemDetails['items'])) {
         //Only 1 item
         $tempDPD = new DobaProductData();
         $tempDPD->item_id($ItemDetails['items']['item']['item_id']);
         $tempDPD->msrp($ItemDetails['items']['item']['msrp']);
         $tempDPD->map($ItemDetails['items']['item']['map']);
         $tempDPD->wholesale_price($ItemDetails['items']['item']['price']);
         $tempDPD->quantity($ItemDetails['items']['item']['qty_avail']);
         $itemList[0] = $tempDPD;
     } else {
         $cnt = 0;
         foreach ($ItemDetails['items'] as $item) {
             $tempDPD = new DobaProductData();
             $tempDPD->item_id($item['item_id']);
             $tempDPD->msrp($item['msrp']);
             $tempDPD->map($item['map']);
             $tempDPD->wholesale_price($item['price']);
             $tempDPD->quantity($item['qty_avail']);
             $itemList[$cnt++] = $tempDPD;
         }
     }
     return $itemList;
 }