Webcreta_Updateqty - Version 0.1.0

Version Notes

Fixed minor bugs

Download this release

Release Info

Developer WebCreta
Extension Webcreta_Updateqty
Version 0.1.0
Comparing to
See all releases


Version 0.1.0

app/code/local/Webcreta/Updateqty/Block/Adminhtml/Adminform.php ADDED
@@ -0,0 +1,8 @@
1
+ <?php
2
+ class Webcreta_Updateqty_Block_Adminhtml_Adminform extends Mage_Adminhtml_Block_Template {
3
+ public function __construct() {
4
+ parent::__construct();
5
+ $this->setTemplate('webcreta/updateqty.phtml');
6
+ $this->setFormAction(Mage::getUrl('*/*/new'));
7
+ }
8
+ }
app/code/local/Webcreta/Updateqty/Block/Adminhtml/Updateqty.php ADDED
@@ -0,0 +1,12 @@
1
+ <?php
2
+ class Webcreta_Updateqty_Block_Adminhtml_Updateqty extends Mage_Core_Block_Template{
3
+
4
+ public function __construct()
5
+ {
6
+ parent::__construct();
7
+ $this->setTemplate('webcreta/updateqty.phtml');
8
+ $this->setFormAction(Mage::getUrl('*/*/upload'));
9
+ // return $this;
10
+ }
11
+
12
+ }
app/code/local/Webcreta/Updateqty/Helper/Data.php ADDED
@@ -0,0 +1,5 @@
1
+ <?php
2
+ class Webcreta_Updateqty_Helper_Data extends Mage_Core_Helper_Abstract
3
+ {
4
+ }
5
+
app/code/local/Webcreta/Updateqty/controllers/Adminhtml/UpdateqtyController.php ADDED
@@ -0,0 +1,252 @@
1
+ <?php
2
+ class Webcreta_Updateqty_Adminhtml_UpdateqtyController extends Mage_Adminhtml_Controller_Action
3
+ {
4
+ protected function _initAction()
5
+ {
6
+ $this->loadLayout()->_setActiveMenu("updateqty")->_addBreadcrumb(Mage::helper("updateqty")->__("Updatestock Manager"),Mage::helper("updateqty")->__("Updatestock Manager"));
7
+ return $this;
8
+ }
9
+ public function indexAction() {
10
+ $this->_title($this->__("Update Mass Qty"));
11
+ $this->_initAction();
12
+
13
+ $block_strt = $this->getLayout()
14
+ ->createBlock('core/text', 'example-block')
15
+ ->setText("
16
+ <h1>Product Quantity Import/update</h1>
17
+ <p>If you dont have the CSV already, click on to the follwoing button for the export</p>
18
+ ");
19
+ $this->_addContent($block_strt);
20
+
21
+ $url_export = $this->getUrl('*/*/exportcsvmy');
22
+
23
+ $block_export = $this->getLayout()
24
+ ->createBlock('adminhtml/widget_button')
25
+ ->setData(array(
26
+ 'label' => Mage::helper('updateqty')->__('Export CSV'),
27
+ 'onclick' => 'setLocation(\'' . $url_export .'\')',
28
+ 'class' => 'import',
29
+ ));
30
+ $this->_addContent($block_export);
31
+
32
+ $block_mid_exprot = $this->getLayout()
33
+ ->createBlock('core/text', 'example-block5')
34
+ ->setText("<p></p>
35
+ <p>Use following uploader to import csv file..!</p>
36
+ ");
37
+ $this->_addContent($block_mid_exprot);
38
+ $this->_addContent($this->getLayout()->createBlock('updateqty/adminhtml_updateqty'));
39
+
40
+ $block_mid = $this->getLayout()
41
+ ->createBlock('core/text', 'example-block2')
42
+ ->setText("<p></p>
43
+ <p>Once done with the import click onto the follwoing button to Update the products</p>
44
+ ");
45
+ $this->_addContent($block_mid);
46
+
47
+ $uploader = $this->getLayout()->createBlock('adminhtml/media_uploader');
48
+ $url = $this->getUrl('*/*/importcsvmy');
49
+
50
+ $block = $this->getLayout()
51
+ ->createBlock('adminhtml/widget_button')
52
+ ->setData(array(
53
+ 'label' => Mage::helper('updateqty')->__('Start Update'),
54
+ 'onclick' => 'setLocation(\'' . $url .'\')',
55
+ 'class' => 'save',
56
+ ));
57
+ $this->_addContent($block);
58
+
59
+ $this->renderLayout();
60
+ }
61
+
62
+ public function uploadAction() {
63
+ $post_data=$this->getRequest()->getPost();
64
+ if ($post_data) {
65
+ try {
66
+ if(isset($_FILES['name']['name']) and (file_exists($_FILES['name']['tmp_name']))) {
67
+ try {
68
+ $uploader = new Varien_File_Uploader('name');
69
+ $uploader->setAllowedExtensions(array('csv'));
70
+ $uploader->setAllowCreateFolders(true); //for creating the directory if not exists
71
+ $uploader->setAllowRenameFiles(false);
72
+ $uploader->setFilesDispersion(false);
73
+ $path = Mage::getBaseDir().DS.'import'.DS;
74
+ if($_FILES['name']['name']) {array_map('unlink', glob($path.DS.'*')); }
75
+ $uploader->save($path, $_FILES['name']['name']);
76
+ Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("File's been imported successfully!"));
77
+ }
78
+ catch(Exception $e) {
79
+ Mage::getSingleton("adminhtml/session")->addError($e->getMessage());
80
+ Mage::getSingleton("adminhtml/session")->setUpdatestockData($this->getRequest()->getPost());return;
81
+ }
82
+ }
83
+ }
84
+ catch (Exception $e) {
85
+ Mage::getSingleton("adminhtml/session")->addError($e->getMessage());
86
+ Mage::getSingleton("adminhtml/session")->setUpdatestockData($this->getRequest()->getPost());
87
+ return;
88
+ }
89
+
90
+ }
91
+ $this->_redirect("*/*/");
92
+ }
93
+
94
+ public function importcsvmyAction()
95
+ {
96
+
97
+ $csv_folder = Mage::getBaseDir().DS.'import'.DS;
98
+ $dir = $csv_folder;
99
+ $dh = opendir($dir);
100
+ while (($file2 = readdir($dh)) !== false) {
101
+ if ($file2 == '.' or $file2 == '..') continue;
102
+ $file_name_2=$file2;
103
+ }
104
+ closedir($dh);
105
+ echo $CSVFileName = $csv_folder.$file_name_2;
106
+ umask(0);
107
+ Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
108
+
109
+ $csv = new Varien_File_Csv();
110
+ $data = $csv->getData($CSVFileName);
111
+
112
+ array_shift($data);
113
+ $message = '';
114
+ $count = 1;
115
+
116
+ foreach($data as $_data){
117
+ if($this->_checkIfSkuExists($_data[0])){
118
+ try{
119
+ $this->_updateStocks($_data);
120
+
121
+ }catch(Exception $e){
122
+ $message .= $count .'> Error:: while Upating Qty (' . $_data[1] . ') of Sku (' . $_data[0] . ') => '.$e->getMessage().'<br />';
123
+ }
124
+ }else{
125
+ $message .= $count .'> Error:: Product with Sku (' . $_data[0] . ') does\'t exist.<br />';
126
+ }
127
+ $count++;
128
+ }
129
+ Mage::getSingleton('adminhtml/session')->addSuccess("Quantity updated!");
130
+ if(!empty($message)){
131
+ Mage::getSingleton('adminhtml/session')->addNotice($message);
132
+ }
133
+
134
+
135
+ $this->_redirect('*/*/');
136
+ }
137
+ public function exportcsvmyAction()
138
+ {
139
+ $csv_folder = Mage::getBaseDir().DS.'import'.DS;
140
+ $CSVFileName = $csv_folder.'/'.'qty_update.csv';
141
+ $fp = fopen($CSVFileName, "w");
142
+ $line= "sku,qty\n";
143
+ fputs($fp, $line);
144
+ try {
145
+ $line = "";
146
+ foreach(Mage::getModel('catalog/product')->getCollection() as $product)
147
+ {
148
+ $productId=$product->getId();
149
+ $product_qty=(int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId)->getQty();
150
+ $product = Mage::getSingleton('catalog/product')->load($productId);
151
+ $line .= "\"{$product->getSku()}\",\"{$product_qty}\"\n";
152
+ }
153
+ fputs($fp, $line);
154
+ fclose($fp);
155
+ if (! is_file ( $CSVFileName ) || ! is_readable ( $CSVFileName )) {
156
+ throw new Exception ( );
157
+ }
158
+
159
+ $this->getResponse ()
160
+ ->setHttpResponseCode ( 200 )
161
+ ->setHeader ( 'Pragma', 'public', true )
162
+ ->setHeader ( 'Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true )
163
+ ->setHeader ( 'Content-type', 'application/force-download' )
164
+ ->setHeader ( 'Content-Length', filesize($CSVFileName) )
165
+ ->setHeader ('Content-Disposition', 'inline' . '; filename=' . basename($CSVFileName) );
166
+ $this->getResponse ()->clearBody ();
167
+ $this->getResponse ()->sendHeaders ();
168
+ readfile ( $CSVFileName );
169
+ exit();
170
+
171
+ }
172
+ catch (Exception $e) {
173
+ Mage::getSingleton("adminhtml/session")->addError($e->getMessage());
174
+ }
175
+ $this->_redirect('*/*/');
176
+ }
177
+ public function openmyAction()
178
+ {
179
+ $post_data=$this->getRequest()->getPost();
180
+ print_r($post_data);
181
+ exit();
182
+ Mage::getSingleton("adminhtml/session")->addSuccess(Mage::helper("adminhtml")->__("File's been imported successfully!"));
183
+ $this->_redirect("*/*/");
184
+ }
185
+
186
+ public function _getConnection($type = 'core_read'){
187
+ return Mage::getSingleton('core/resource')->getConnection($type);
188
+ }
189
+
190
+ public function _getTableName($tableName){
191
+ return Mage::getSingleton('core/resource')->getTableName($tableName);
192
+ }
193
+
194
+ public function _getAttributeId($attribute_code = 'price'){
195
+ $connection = $this->_getConnection('core_read');
196
+ $sql = "SELECT attribute_id
197
+ FROM " . $this->_getTableName('eav_attribute') . "
198
+ WHERE
199
+ entity_type_id = ?
200
+ AND attribute_code = ?";
201
+ $entity_type_id = $this->_getEntityTypeId();
202
+ return $connection->fetchOne($sql, array($entity_type_id, $attribute_code));
203
+ }
204
+
205
+ public function _getEntityTypeId($entity_type_code = 'catalog_product'){
206
+ $connection = $this->_getConnection('core_read');
207
+ $sql = "SELECT entity_type_id FROM " . $this->_getTableName('eav_entity_type') . " WHERE entity_type_code = ?";
208
+ return $connection->fetchOne($sql, array($entity_type_code));
209
+ }
210
+
211
+
212
+
213
+ public function _getIdFromSku($sku){
214
+ $connection = $this->_getConnection('core_read');
215
+ $sql = "SELECT entity_id FROM " . $this->_getTableName('catalog_product_entity') . " WHERE sku = ?";
216
+ return $connection->fetchOne($sql, array($sku));
217
+ }
218
+
219
+ public function _updateStocks($data){
220
+ $connection = $this->_getConnection('core_write');
221
+ $sku = $data[0];
222
+ $newQty = $data[1];
223
+ $productId = $this->_getIdFromSku($sku);
224
+ $attributeId = $this->_getAttributeId();
225
+
226
+ $sql = "UPDATE " . $this->_getTableName('cataloginventory_stock_item') . " csi,
227
+ " . $this->_getTableName('cataloginventory_stock_status') . " css
228
+ SET
229
+ csi.qty = ?,
230
+ csi.is_in_stock = ?,
231
+ css.qty = ?,
232
+ css.stock_status = ?
233
+ WHERE
234
+ csi.product_id = ?
235
+ AND csi.product_id = css.product_id";
236
+ $isInStock = $newQty > 0 ? 1 : 0;
237
+ $stockStatus = $newQty > 0 ? 1 : 0;
238
+ $connection->query($sql, array($newQty, $isInStock, $newQty, $stockStatus, $productId));
239
+ }
240
+
241
+ public function _checkIfSkuExists($sku){
242
+ $connection = $this->_getConnection('core_read');
243
+ $sql = "SELECT COUNT(*) AS count_no FROM " . $this->_getTableName('catalog_product_entity') . " WHERE sku = ?";
244
+ $count = $connection->fetchOne($sql, array($sku));//echo $count;die;
245
+ if($count > 0){
246
+ return true;
247
+ }else{
248
+ return false;
249
+ }
250
+ }
251
+ }
252
+ ?>
app/code/local/Webcreta/Updateqty/etc/config.xml ADDED
@@ -0,0 +1,45 @@
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Webcreta_Updateqty>
5
+ <version>0.1.0</version>
6
+ </Webcreta_Updateqty>
7
+ </modules>
8
+ <admin>
9
+ <routers>
10
+ <Webcreta_Updateqty>
11
+ <use>admin</use>
12
+ <args>
13
+ <module before="Mage_Adminhtml">Webcreta_Updateqty</module>
14
+ <frontName>updateqty</frontName>
15
+ </args>
16
+ </Webcreta_Updateqty>
17
+ </routers>
18
+ </admin>
19
+ <adminhtml>
20
+ <menu>
21
+ <tutorial_menu translate="title" module="updateqty">
22
+ <title>Update Stock</title>
23
+ <sort_order>9999</sort_order>
24
+ <children>
25
+ <first_page module="updateqty">
26
+ <title>Update Mass Qty</title>
27
+ <action>updateqty/adminhtml_updateqty</action>
28
+ </first_page>
29
+ </children>
30
+ </tutorial_menu>
31
+ </menu>
32
+ </adminhtml>
33
+ <global>
34
+ <helpers>
35
+ <updateqty>
36
+ <class>Webcreta_Updateqty_Helper</class>
37
+ </updateqty>
38
+ </helpers>
39
+ <blocks>
40
+ <updateqty>
41
+ <class>Webcreta_Updateqty_Block</class>
42
+ </updateqty>
43
+ </blocks>
44
+ </global>
45
+ </config>
app/design/adminhtml/default/default/template/webcreta/updateqty.phtml ADDED
@@ -0,0 +1,7 @@
1
+ <div>
2
+ <form action="<?php echo $this->getFormAction(); ?>" method="POST" enctype="multipart/form-data">
3
+ <input type="hidden" name="form_key" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />
4
+ <input type="file" id="name" name="name" />
5
+ <input type="submit" id="submit" name="submit" value="Import CSV" class="save form-button"/>
6
+ </form>
7
+ </div>
app/etc/modules/Webcreta_Updateqty.xml ADDED
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Webcreta_Updateqty>
5
+ <active>true</active>
6
+ <codePool>local</codePool>
7
+ </Webcreta_Updateqty>
8
+ </modules>
9
+ </config>
import/lt.csv ADDED
@@ -0,0 +1,11574 @@