예제 #1
0
 public function __construct($fileName)
 {
     $this->fileName = $fileName;
     $inputFileType = 'Excel2007';
     /**  Create a new Reader of the type defined in $inputFileType  **/
     $this->objReader = PHPExcel_IOFactory::createReader($inputFileType);
     $worksheetList = $this->objReader->listWorksheetNames($this->fileName);
     $sheets = array($worksheetList[2], $worksheetList[3]);
     $this->objReader->setLoadSheetsOnly($sheets);
     $this->objReader->setReadDataOnly(true);
     SyncVendor::log("Begin loading " . $this->fileName . " base file.");
     $this->objPHPExcel = $this->objReader->load($this->fileName);
     SyncVendor::log("Base excel file loaded.");
 }
예제 #2
0
 public function getNextLine()
 {
     if ($this->endOfFile) {
         if (!$this->fileSupplier->endReached()) {
             $this->currentSheetConfig = $this->fileSupplier->getCurrentSheetConfig();
             $this->currentFile = $this->fileSupplier->nextFile();
             SyncVendor::log("Loading " . pathinfo($this->currentFile, PATHINFO_BASENAME) . "...");
             if (file_exists($this->currentFile)) {
                 $this->objPHPExcel = PHPExcel_IOFactory::load($this->currentFile);
                 SyncVendor::log("Loading done. Parsing ...");
                 $this->lastRow = $this->objPHPExcel->getActiveSheet()->getHighestRow();
                 $this->currentLine = $this->currentSheetConfig['first-row'];
                 $this->endOfFile = false;
             } else {
                 SyncVendor::log("WARNING: File " . pathinfo($this->currentFile, PATHINFO_BASENAME) . " not found");
                 $this->endOfFile = true;
                 return $this->getNextLine();
             }
         } else {
             $this->endReached = true;
             return false;
         }
     }
     $returnData = array();
     $itemColumn = $this->currentSheetConfig['item-column'];
     $priceColumn = $this->currentSheetConfig['price-column'];
     $qtyColumn = $this->currentSheetConfig['qty-column'];
     $returnData['item'] = $this->objPHPExcel->getActiveSheet()->getCell($itemColumn . $this->currentLine)->getValue();
     if (isset($this->currentSheetConfig['separator'])) {
         $returnData['item'] = explode($this->currentSheetConfig['separator'], $returnData['item']);
         $returnData['item'] = $returnData['item'][0];
     }
     $returnData['price'] = $this->objPHPExcel->getActiveSheet()->getCell($priceColumn . $this->currentLine)->getValue() * (100 - $this->config['discount']) / 100;
     $returnData['qty'] = (int) $this->objPHPExcel->getActiveSheet()->getCell($qtyColumn . $this->currentLine)->getCalculatedValue();
     if (!$returnData['qty']) {
         $returnData['qty'] = 1;
     }
     if ($this->currentLine > $this->lastRow) {
         SyncVendor::log("File " . $this->currentFile . " parsed. " . $this->currentLine . " lines.");
         unset($this->objPHPExcel);
         $this->endOfFile = true;
         return $this->getNextLine();
     }
     $this->currentLine++;
     return $returnData;
 }
예제 #3
0
 public function __construct($syncConfig)
 {
     $this->config = $syncConfig;
     if ($syncConfig['base-recreate-table'] == true) {
         $counter = 0;
         SyncVendor::log("Database table creation started.");
         $columns[] = 'item_id';
         $columns[] = 'interest';
         db_query("DROP TABLE IF EXISTS " . $syncConfig['base-table']);
         $query = "CREATE TABLE " . $syncConfig['base-table'] . " ( item_id VARCHAR(50) NOT NULL, interest DECIMAL(5,2) NOT NULL DEFAULT 100.00, ";
         foreach ($syncConfig['vendors'] as $vendor) {
             $query .= $vendor['base-data-column-name'] . " VARCHAR(50) NOT NULL,";
             $columns[] = $vendor['base-data-column-name'];
         }
         $query = rtrim($query, ",");
         $query .= ") ENGINE=InnoDB   DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;";
         db_query($query);
         $excelBaseData = new ExcelBaseData(DIR_SYNC_VENDORS . $syncConfig['base-file']);
         $excel = $excelBaseData->objPHPExcel;
         $this->excel = $excel;
         $lastRow = $excel->setActiveSheetIndex(0)->getHighestRow();
         for ($row = 2; $row <= $lastRow; $row++) {
             $query = "INSERT INTO " . $syncConfig['base-table'] . " ";
             $values = array();
             $itemId = $excel->setActiveSheetIndex(0)->getCell("A" . $row)->getValue();
             $values['item_id'] = "'" . $itemId . "'";
             $values['interest'] = $this->getInterest($itemId);
             foreach ($syncConfig['vendors'] as $vendor) {
                 $values[] = "'" . $excel->setActiveSheetIndex(0)->getCell($vendor["base-data-column"] . $row)->getValue() . "'";
                 //$values[] = $value ? $value : "''";
             }
             $query .= "(" . implode(",", $columns) . ") VALUES (" . implode(",", $values) . ")";
             db_query($query);
             $counter++;
             if (fmod($counter, 1000) == 0) {
                 SyncVendor::log($counter . " records inserted.");
             }
         }
         SyncVendor::log("Database table creation ended. " . $counter . " records inserted.");
         $excel->disconnectWorksheets();
         unset($this->objPHPExcel);
     }
 }