0

I want to create a csv for particular order but my code is generate all order csv. Please check my code if i do any thing wrong suggest me its really helping me.

My code

    $arvio=Mage::getSingleton('core/session')->getCardName();

        /* csv generateed*/
        if ($arvio !="") {

           $postdata = $this->getRequest()->getParams();  
           //$postdata['card_name'] = 1111;    
           $resource = Mage::getSingleton('core/resource'); 
           $writeConnection = $resource->getConnection('core_write');
           $orders = Mage::getModel('sales/order')->getCollection(); 

           Mage::log(count($orders), null, 'order.log',true);

           $orders->getSelect()->join( array('p' => $orders->getResource()->getTable('sales/order_payment')), 'p.parent_id = main_table.entity_id', array('cc_last4') );
           $orders->getSelect()->join( array('ghgh' => $orders->getResource()->getTable('sales/payment_transaction')), 'main_table.entity_id = ghgh.order_id', array('transaction_id'));


           Mage::log(count($orders), null, 'order.log',true);
           //$orders->addFieldToFilter('cc_last4',$postdata['card_name']);  
                /*$ddate = date("Y-m-d");
                $date = new DateTime($ddate);*/

                $week = date("W",strtotime("- 1 day")); 
                if(!file_exists('csvorder/'.$week)){
                    mkdir('csvorder/'.$week,0777,true); 
                }
                $file = 'csvorder/'.$week.'/'.date("Y-m-d H:i:s").'.csv'; 
                $file = str_replace(array(' ',':'), '_', $file);
                $fp = fopen($file, 'w');

                $csvHeader = array(
                'Member id /Card no.',
                'Merchant id',
                'Receipt no',
                'TransactionDate Time',
                'Transaction Amount',
                "CompaignAwarded Point",
                "CampaignCode",
                "Reward"
                );

                $csv .= implode(',', $csvHeader)."\n";
                fputcsv( $fp, $csvHeader, ",");
                $count = count($orders);
                if($count != 0){
                   foreach ($orders as $order){ 
                   //echo '<pre>'; print_r($order->getData());
                   //  echo '<pre>'; print_r($order->getId());
                   //echo "check"; 

                   //Mage::log('My log entry', null, 'order.log', true);
                   Mage::log($order->getId(), null, 'order.log', true);

                   $grandtotal = $order->getGrandTotal();
                   $entityid = $order->getEntityId();

                    /*------------invoice number-------------------------*/
                    if ($order->hasInvoices()) {
                        $invIncrementIDs = array();
                        foreach ($order->getInvoiceCollection() as $inv) {
                            $invIncrementIDs[] = $inv->getIncrementId();
                            foreach($invIncrementIDs as $invIncrementID){
                                $invIncrementID;
                            }
                        }
                    }
                    /*------------end invoice number-----------------------*/
                    /*------------transaction date-------------------------*/
                        $query = "select created_at from sales_payment_transaction where payment_id ='$entityid'";

                        $results = $writeConnection->fetchAll($query);

                        $createddate = $results[0]['created_at'];

                        $createddate_custom = $order['created_at'];

                        $invIncrementID = $order['increment_id'];

                    /*------------card number------------------------*/
                    $cardnumber = Mage::getSingleton('core/session')->getCardName(); 
                    /*------------compain award---------------------------------*/  
                        $percent = $grandtotal * (15/1000).'%';
                    /*------------------merchant number-------------------------*/ 
                        $merchangnumber = '784'; 


                        $data = array();
                        $data['cardnumber'] = $cardnumber;
                        $data['merchant'] = $merchangnumber;
                        $data['invoice'] = $invIncrementID;
                        $data['transactiont'] = $createddate_custom;
                        $data['transactiond'] = $grandtotal;
                        $data['awardpoint'] = $percent;
                        $data['compaigncode'] = 'ORDERA.MY'; 
                        //echo '<pre>';print_r($data);
                        $csv .= implode(',', $data)."\n";
                        fputcsv( $fp, $data, ",");  
                        //$val = $postdata['card_name'];
                        //$allcdetail = "select DISTINCT cc_last4 from sales_flat_order_payment"; 
                        //$carddata = $writeConnection->fetchAll($allcdetail); 
                        //$filterval = $carddata[0]['cc_last4'];  

                                //$this->_prepareDownloadResponse('file'.date("Y-m-d H:i:s").'.csv', $csv, 'text/csv'); 

            }
        }
    }
 }

I really thankful to you.

2 Answers 2

1
$products=Mage::getModel('sales/order')->loadByIncrementId('100000103');    
$fp = fopen('exports.csv', 'w+');  
$csvHeader = array('entity_id',);  
fputcsv( $fp, $csvHeader,",");  
fputcsv($fp, array($products['entity_id']), ",");  

fclose($fp);

this code will generate csv file in root and only store entity id of order. you can store the data that you want

0
if ($arvio !="") {



     $postdata = $this->getRequest()->getParams();     
     $resource = Mage::getSingleton('core/resource'); 
     $writeConnection = $resource->getConnection('core_write');
     $orders = Mage::getModel('sales/order')->getCollection(); 
     Mage::log(count($orders), null, 'order.log',true);

$orders->getSelect()->join( array('p' => $orders->getResource()->getTable('sales/order_payment')), 'p.parent_id = main_table.entity_id', array('cc_last4') );
$orders->getSelect()->join( array('ghgh' => $orders->getResource()->getTable('sales/payment_transaction')), 'main_table.entity_id = ghgh.order_id', array('transaction_id'));


    Mage::log(count($orders), null, 'order.log',true);
        $week = date("W",strtotime("- 1 day")); 
            if(!file_exists('csvorder/'.$week)){
                mkdir('csvorder/'.$week,0777,true); 
            }
            $file = 'csvorder/'.$week.'/'.date("Y-m-d H:i:s").'.csv'; 
        $file = str_replace(array(' ',':'), '_', $file);
        $fp = fopen($file, 'w');
        $csvHeader = array(
          'Member id /Card no.',
          'Merchant id',
          'Receipt no',
          'TransactionDate Time',
          'Transaction Amount',
          "CompaignAwarded Point",
          "CampaignCode",
          "Reward"
        );
        $csv .= implode(',', $csvHeader)."\n";
        fputcsv( $fp, $csvHeader, ",");
        $count = count($orders);

    if($count != 0){



        $i = 1;
        foreach ($orders as $order){ 
            if($i > $count-1){
            Mage::log($order->getId(), null, 'order.log', true);
            $grandtotal = $order->getGrandTotal();
                $entityid = $order->getEntityId();

                /*------------invoice number-------------------------*/
                if ($order->hasInvoices()) {
                    $invIncrementIDs = array();
                    foreach ($order->getInvoiceCollection() as $inv) {
                        $invIncrementIDs[] = $inv->getIncrementId();
                        foreach($invIncrementIDs as $invIncrementID){
                            $invIncrementID;
                        }
                    }
                }
                /*------------end invoice number-----------------------*/
                /*------------transaction date-------------------------*/
                    $query = "select created_at from sales_payment_transaction where payment_id ='$entityid'";

                    $results = $writeConnection->fetchAll($query);

                    $createddate = $results[0]['created_at'];

                    $createddate_custom = $order['created_at'];

                    $invIncrementID = $order['increment_id'];

                /*------------card number------------------------*/
                $cardnumber = Mage::getSingleton('core/session')->getCardName(); 
                /*------------compain award---------------------------------*/  
                    $percent = $grandtotal * (15/1000).'%';
                /*------------------merchant number-------------------------*/ 
                    $merchangnumber = '784'; 



                    $data = array();
                    $data['cardnumber'] = $cardnumber;
                    $data['merchant'] = $merchangnumber;
                    $data['invoice'] = $invIncrementID;
                    $data['transactiont'] = $createddate_custom;
                    $data['transactiond'] = $grandtotal;
                    $data['awardpoint'] = $percent;
                    $data['compaigncode'] = 'ORDERA.MY'; 
                    //echo '<pre>';print_r($data);
                    $csv .= implode(',', $data)."\n";
                    fputcsv( $fp, $data, ",");                        
        }
        $i++;
    }
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.