Eg_SaveInvoice - Version 0.1.1

Version Notes

This extension Is still in beta otherwise it's running in production on some stores.

Download this release

Release Info

Developer Emanuele Gian
Extension Eg_SaveInvoice
Version 0.1.1
Comparing to
See all releases


Version 0.1.1

app/code/community/Eg/SaveInvoice/Helper/Data.php ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Class Eg_SaveInvoice_Helper_Data
5
+ */
6
+ class Eg_SaveInvoice_Helper_Data extends Mage_Core_Helper_Abstract
7
+ {
8
+
9
+ public function formatInvoiceData($InvoiceData)
10
+ {
11
+ return Mage::getModel('core/date')->date('Ymd', strtotime($InvoiceData));
12
+ }
13
+
14
+ public function getDefaultStoreID()
15
+ {
16
+ $defaultStoreId = Mage::app()
17
+ ->getWebsite(true)
18
+ ->getDefaultGroup()
19
+ ->getDefaultStoreId();
20
+ return $defaultStoreId;
21
+ }
22
+
23
+ protected function getInvoiceEntityTypeId() {
24
+ $entityType = Mage::getModel('eav/config')->getEntityType('invoice');
25
+ return $entityType->getEntityTypeId();
26
+ }
27
+
28
+ public function getInvoiceIncrementPrefix() {
29
+ $entityStoreConfig = Mage::getModel('eav/entity_store')
30
+ ->loadByEntityStore($this->getInvoiceEntityTypeId(), $this->getDefaultStoreID());
31
+ return $entityStoreConfig->getIncrementPrefix();
32
+
33
+ }
34
+
35
+ public function _log($msg) {
36
+ Mage::log($msg, null, 'Eg_SaveInvoice.log', true);
37
+ }
38
+
39
+ }
app/code/community/Eg/SaveInvoice/Model/Export.php ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Class Eg_SaveInvoice_Model_Export
5
+ */
6
+ class Eg_SaveInvoice_Model_Export extends Mage_Core_Model_Abstract
7
+ {
8
+ const XML_PATH_SAVE_INVOICE_ENABLED = 'sales/saveinvoice/enabled';
9
+ const XML_PATH_PDF_EXPORT_FOLDER = 'sales/saveinvoice/export_folder';
10
+
11
+ const INVOICE_EXTENSION = '.pdf';
12
+
13
+ protected function getPdfExportFolder()
14
+ {
15
+ return $this->checkPath(Mage::getStoreConfig(self::XML_PATH_PDF_EXPORT_FOLDER)) . DS;
16
+ }
17
+
18
+ protected function isSavePdfInvoiceEnabled()
19
+ {
20
+ return Mage::getStoreConfig(self::XML_PATH_SAVE_INVOICE_ENABLED);
21
+ }
22
+
23
+ private function getIoAdapter()
24
+ {
25
+ if (is_null($this->_ioFile)) {
26
+ $this->_ioFile = new Varien_Io_File();
27
+ }
28
+ return $this->_ioFile;
29
+ }
30
+
31
+ protected function checkPath($pathToCheck) {
32
+ $ioAdapter = $this->getIoAdapter();
33
+ try {
34
+ $path = $ioAdapter->getCleanPath($pathToCheck);
35
+ $ioAdapter->checkAndCreateFolder($path);
36
+ }
37
+ catch (Exception $e) {
38
+ Mage::helper('eg_saveinvoice')->_log("*** SaveInvoice ERROR: " . $e->getMessage());
39
+ }
40
+ return $pathToCheck;
41
+ }
42
+
43
+ public function createInvoiceFileName($IncrementID) {
44
+ return $IncrementID . self::INVOICE_EXTENSION;
45
+ }
46
+
47
+ public function _saveInvoicePdf($_invoice) {
48
+
49
+ if (!$this->isSavePdfInvoiceEnabled())
50
+ return;
51
+
52
+ $orderId = $_invoice->getOrder()->getId();
53
+
54
+ $orderModel = Mage::getModel('sales/order')->load($orderId);
55
+ $invoices = $orderModel->getInvoiceCollection();
56
+ $invoicesSet = array();
57
+ foreach ($invoices as $_invoice) {
58
+ array_push($invoicesSet, $_invoice);
59
+ }
60
+ try {
61
+ $pdf = Mage::getModel('sales/order_pdf_invoice')->getPdf($invoicesSet);
62
+ $InvoiceIncrementId = $_invoice->getIncrementId();
63
+ $InvoiceFileName = $this->createInvoiceFileName($InvoiceIncrementId);
64
+ $pdf->save($this->getPdfExportFolder() . $InvoiceFileName);
65
+ Mage::helper('eg_saveinvoice')->_log( 'PDF invoice ' . $InvoiceFileName . ' saved on ' . $this->getPdfExportFolder());
66
+
67
+ } catch (Exception $ex) {
68
+ Mage::helper('eg_saveinvoice')->_log("saveInvoice:" . $ex->getMessage());
69
+ }
70
+ }
71
+
72
+ }
app/code/community/Eg/SaveInvoice/Model/Observer.php ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * Class Eg_SaveInvoice_Model_Observer
5
+ */
6
+ class Eg_SaveInvoice_Model_Observer
7
+ {
8
+ public function saveInvoice(Varien_Event_Observer $observer)
9
+ {
10
+ $_export = Mage::getModel('eg_saveinvoice/export');
11
+
12
+ $_event = $observer->getEvent();
13
+ $_invoice = $_event->getInvoice();
14
+
15
+ Mage::helper('eg_saveinvoice')->_log('Eg_SaveInvoice_Model_Observer :: called');
16
+
17
+ $_export->_saveInvoicePdf($_invoice);
18
+ }
19
+ }
app/code/community/Eg/SaveInvoice/etc/config.xml ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Eg_SaveInvoice>
5
+ <version>0.1.1</version>
6
+ </Eg_SaveInvoice>
7
+ </modules>
8
+ <global>
9
+ <models>
10
+ <eg_saveinvoice>
11
+ <class>Eg_SaveInvoice_Model</class>
12
+ </eg_saveinvoice>
13
+ </models>
14
+ <helpers>
15
+ <eg_saveinvoice>
16
+ <class>Eg_SaveInvoice_Helper</class>
17
+ </eg_saveinvoice>
18
+ </helpers>
19
+ <events>
20
+ <sales_order_invoice_save_after>
21
+ <observers>
22
+ <eg_saveinvoice_invoice_observer>
23
+ <class>eg_saveinvoice/observer</class>
24
+ <method>saveInvoice</method>
25
+ </eg_saveinvoice_invoice_observer>
26
+ </observers>
27
+ </sales_order_invoice_save_after>
28
+ </events>
29
+ </global>
30
+ <default>
31
+ <sales>
32
+ <saveinvoice>
33
+ <enabled>0</enabled>
34
+ <export_folder>var/importexport/invoices</export_folder>
35
+ </saveinvoice>
36
+ </sales>
37
+ </default>
38
+ </config>
app/code/community/Eg/SaveInvoice/etc/system.xml ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <sections>
4
+ <sales>
5
+ <groups>
6
+ <saveinvoice translate="label">
7
+ <label>Save Pdf Invoice to file system</label>
8
+ <sort_order>10</sort_order>
9
+ <show_in_default>1</show_in_default>
10
+ <show_in_website>1</show_in_website>
11
+ <show_in_store>1</show_in_store>
12
+ <fields>
13
+ <enabled translate="label">
14
+ <label>Enabled</label>
15
+ <frontend_type>select</frontend_type>
16
+ <source_model>adminhtml/system_config_source_yesno</source_model>
17
+ <sort_order>1</sort_order>
18
+ <show_in_default>1</show_in_default>
19
+ <show_in_website>1</show_in_website>
20
+ <show_in_store>1</show_in_store>
21
+ <comment>Enabling saving invoice</comment>
22
+ </enabled>
23
+ <export_folder translate="label">
24
+ <label>PDF saving folder</label>
25
+ <frontend_type>text</frontend_type>
26
+ <sort_order>3</sort_order>
27
+ <show_in_default>1</show_in_default>
28
+ <show_in_website>1</show_in_website>
29
+ <show_in_store>1</show_in_store>
30
+ <comment>Folder where invoices will be saved</comment>
31
+ </export_folder>
32
+ </fields>
33
+ </saveinvoice>
34
+ </groups>
35
+ </sales>
36
+ </sections>
37
+ </config>
app/etc/modules/Eg_SaveInvoice.xml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Eg_SaveInvoice>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ <depends></depends>
8
+ </Eg_SaveInvoice>
9
+ </modules>
10
+ </config>
package.xml ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Eg_SaveInvoice</name>
4
+ <version>0.1.1</version>
5
+ <stability>beta</stability>
6
+ <license>GNU</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>This magento extension enables saving PDF invoices on filesystem.</summary>
10
+ <description>Every invoice created will be saved as pdf file on filesystem in a configurable path from admin.&#xD;
11
+ &#xD;
12
+ A shell utility is provided for saving invoice manually by increment id. </description>
13
+ <notes>This extension Is still in beta otherwise it's running in production on some stores.</notes>
14
+ <authors><author><name>Emanuele Gian</name><user>emastyle</user><email>emastyle@libero.it</email></author></authors>
15
+ <date>2016-02-27</date>
16
+ <time>17:12:48</time>
17
+ <contents><target name="magecommunity"><dir name="Eg"><dir name="SaveInvoice"><dir name="Helper"><file name="Data.php" hash="548bb0e5d1c06ab862e2e8fa5ff959e6"/></dir><dir name="Model"><file name="Export.php" hash="cbbb9ecd404aa8349f3c3b5d522a9952"/><file name="Observer.php" hash="b97a2d9dd6c353856fb507838a067059"/></dir><dir name="etc"><file name="config.xml" hash="689cd412fd9e53c6c4554dfcad603a8b"/><file name="system.xml" hash="c9b173b12e31ca515288d4c7245f0ee5"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Eg_SaveInvoice.xml" hash="0c57fd8ee1c2d0dcc2aee492fec1925a"/></dir></target><target name="mage"><dir name="."><dir name="shell"><file name="EgSaveInvoice.php" hash="bdfcd3e59af4c3eb600c9d23d493253d"/></dir></dir></target></contents>
18
+ <compatible/>
19
+ <dependencies><required><php><min>5.2.17</min><max>5.6.0</max></php></required></dependencies>
20
+ </package>
shell/EgSaveInvoice.php ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ require_once 'abstract.php';
3
+
4
+ class Mage_Shell_Runtime extends Mage_Shell_Abstract
5
+ {
6
+
7
+ /**
8
+ * Additional initialize instruction
9
+ *
10
+ * @return Mage_Shell_Abstract
11
+ */
12
+ protected function _construct()
13
+ {
14
+ parent::_construct();
15
+ Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_ADMINHTML);
16
+ Mage::app()->setCurrentStore(1);
17
+ return $this;
18
+ }
19
+
20
+ public function run()
21
+ {
22
+ if ($this->getArg('increment_id') && $this->getArg('increment_id') != '' && $this->getArg('increment_id') != ' ') {
23
+
24
+ $invoiceIncrementId = $this->getArg('increment_id');
25
+ Mage::helper('eg_saveinvoice')->_log('Invoice saving by shell script was called for invoice increment_id ' . $invoiceIncrementId);
26
+ $saveInvoiceModel = Mage::getModel('eg_saveinvoice/export');
27
+
28
+ $_invoice = Mage::getModel('sales/order_invoice')->loadByIncrementId($invoiceIncrementId);
29
+
30
+ /** got to shop root directory **/
31
+ chdir('../');
32
+ $saveInvoiceModel->_saveInvoicePdf($_invoice);
33
+
34
+ echo 'Invoice ' . $invoiceIncrementId . ' created' . PHP_EOL;
35
+ } else {
36
+ echo $this->usageHelp();
37
+ }
38
+
39
+ }
40
+
41
+ /**
42
+ * Retrieve Usage Help Message
43
+ *
44
+ */
45
+ public function usageHelp()
46
+ {
47
+ return <<<USAGE
48
+ Usage: php -f saveinvoice.php -- [parameters]
49
+
50
+ --increment_id <invoice increment id> Save PDF invoice on filesystem passing invoice increment_id
51
+ help This help
52
+
53
+
54
+ USAGE;
55
+ }
56
+ }
57
+
58
+ $shell = new Mage_Shell_Runtime();
59
+ $shell->run();
60
+
61
+
62
+