Created
November 3, 2017 08:24
-
-
Save trabulium/36264f7d14afcc90271cd64c55c098ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('MAGENTO_ROOT', "/var/www/hatsbythe100.com.au/web"); | |
$mageFilename = MAGENTO_ROOT . '/app/Mage.php'; | |
require_once $mageFilename; | |
umask(0); | |
Mage::app(); | |
Mage::app()->getStore()->setId(0); | |
class Mage_Shell_getCodistoStock { | |
public function getRecords() { | |
$file = 'codisto.csv'; | |
$csv = new Varien_File_Csv(); | |
$data = $csv->getData($file); | |
$productSkus = array(); | |
for ($i = 0; $i < count($data); $i++) { | |
if($data[$i][0] != '' & $data[$i][0] != 'Code'){ | |
$codistoProducts[] = $data[$i]; | |
} | |
if($data[$i][3] != '' & $data[$i][3] != 'Code'){ | |
$productSkus[] = $data[$i][3]; | |
} | |
} | |
$collection = Mage::getModel('catalog/product')->getCollection(); | |
$collection->addAttributeToFilter('sku', array('in' => array($productSkus))); | |
foreach ($collection as $product){ | |
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty(); | |
$productData[$product->getSku()] = $stock; | |
} | |
foreach($codistoProducts as $codistoProduct){ | |
if($codistoProduct[3] === 'SKU Code'){ | |
$stock = 'StockLevel'; | |
array_push($codistoProduct, $stock); | |
} else { | |
$stock = $productData[$codistoProduct[3]]; | |
array_push($codistoProduct, $stock); | |
} | |
$uploadProducts[] = $codistoProduct; | |
} | |
return $uploadProducts; | |
} | |
public function run() | |
{ | |
$productData = $this->getRecords(); | |
$csvFile = $this->saveToCsv($productData); | |
$emailTo = '[email protected]'; // @FIXME Take it from configuration xml | |
$emailCc = array('[email protected]'); | |
try { | |
$this->sendEmail($emailTo, $emailCc, $csvFile); | |
echo 'Done'; | |
} catch (Exception $e) { | |
Mage::throwException('cannot send email'); //logException(new Exception("Path is not writable $filename")); | |
return false; | |
} | |
return true; | |
} | |
public function saveToCsv($productData) | |
{ | |
$name = $this->buildCsvName(); | |
$filename = Mage::getBaseDir('var') . DS . 'export' . DS . $name; | |
try { | |
$fp = fopen($filename, 'w'); | |
foreach ($productData as $fields) { | |
fputcsv($fp, $fields); | |
} | |
fclose($fp); | |
} catch (Exception $e) { | |
Mage::throwException("Cannot write $filename"); | |
} | |
return $filename; | |
} | |
public function buildCsvName() | |
{ | |
return 'codisto-stock-' . date('Y-m-d-h-i') . '.csv'; | |
} | |
public function sendEmail($emailTo, $emailCc, $csvFile) | |
{ | |
$mail = new Zend_Mail(); | |
$mail->addTo($emailTo); | |
$mail->addCc($emailCc); | |
$mail->setFrom('[email protected]'); // @FIXME Take it from configuration xml | |
$mail->setSubject('Codisto Daily File'); | |
$mail->setBodyText('Stock Report for Today.'); | |
$csvFileContent = file_get_contents($csvFile); | |
$mail->createAttachment($csvFileContent, | |
'text/csv', | |
Zend_Mime::DISPOSITION_ATTACHMENT, | |
Zend_Mime::ENCODING_BASE64, | |
$this->buildCsvName() | |
); | |
return $mail->send(); | |
} | |
} | |
$codisto = new Mage_Shell_getCodistoStock(); | |
$codisto->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment