0

Once i click save tracking number it automatically generate invoice and order status will be changed complete. check my below code. It's working fine

Controller file

public function saveTrackingNumberAction(){
            try{
                if($this->getRequest()->isPost()){
                    $orderid=$this->getRequest()->getParam('order_id');
                    $trackingid=$this->getRequest()->getParam('tracking_id');
                    $carrier=$this->getRequest()->getParam('carrier');
                    $order=Mage::getModel('sales/order')->load($orderid);
                    $partnerid=Mage::getSingleton('customer/session')->getCustomer()->getId();
                    $items=array();
                    $shippingAmount=0;
                    $trackingsdata=Mage::getModel('mpshippingmanager/tracking')->getCollection()
                                                     ->addFieldToFilter('order_id',array('eq'=>$orderid))
                                                     ->addFieldToFilter('seller_id',array('eq'=>$partnerid));
                    $product=Mage::getModel('catalog/product')->load($itemid);
                    foreach($trackingsdata as $tracking){
                        $tracking->setTrackingNumber($trackingid);
                        $tracking->setCarrierName($carrier);
                        $tracking->save();
                        $product_name="";
                        $shippingAmount=$tracking->getShippingCharges();
                        $items=explode(',',$tracking->getItemIds());
                        foreach($order->getAllItems() as $item){
                            if(in_array($item->getId(),$items)){
                                $product_name =$product_name.$product->getName()." ,";
                            }
                        }

                        $product_name = $product->getName();
                        $to = $order->getCustomerEmail();

                        $emailTemp = Mage::getModel('core/email_template')->loadDefault('trackingnopartner');
                        $emailTempVariables = array();              
                        $adminEmail=Mage::getStoreConfig('trans_email/ident_general/email');
                        $emailTempVariables['myvar1'] = $order->getIncrementId();
                        $emailTempVariables['myvar2'] = $product_name;
                        $emailTempVariables['myvar3'] = $order->getIncrementId();
                        $emailTempVariables['myvar4'] = $trackingid;
                        $processedTemplate = $emailTemp->getProcessedTemplate($emailTempVariables);
                        $emailTemp->setSenderName('Admin');
                        $emailTemp->setSenderEmail($adminEmail);
                        $emailTemp->send($to,'Admin',$emailTempVariables);
                    }

                        /**/
                    $itemsarray = $this->_getItemQtys($order,$items);

                    if(count($itemsarray)>0){
                        if($order->canInvoice()) { 
                            $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($itemsarray['data']);
                            $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
                            $invoice->setShippingAmount($shippingAmount);
                            $invoice->setSubtotal($itemsarray['subtotal']);
                            $invoice->setBaseSubtotal($itemsarray['baseSubtotal']);
                            $invoice->setGrandTotal($itemsarray['subtotal']+$shippingAmount);
                            $invoice->setBaseGrandTotal($itemsarray['subtotal']+$shippingAmount);
                            $invoice->register();

                            $transactionSave = Mage::getModel('core/resource_transaction')
                                        ->addObject($invoice)
                                        ->addObject($invoice->getOrder());
                            $transactionSave->save();
                            $order->sendNewOrderEmail()->addStatusHistoryComment(
                                Mage::helper('mpshippingmanager')->__('Notified customer about invoice #%s.', $invoice->getId())
                            )
                            ->setIsCustomerNotified(true)
                            ->save();
                            $seller_invoice_id = $invoice->getId();
                        }else{
                            if($order->hasInvoices()) {
                                foreach ($order->getInvoiceCollection() as $inv) { 
                                    foreach ($inv->getAllItems() as $item) { 
                                        $product_id = $item->getProductId();
                                        $seller_pro_coll=Mage::getModel('marketplace/product')->getCollection()
                                                                ->addFieldToFilter('mageproductid',array('eq'=>$product_id))
                                                                ->addFieldToFilter('userid',array('eq'=>$partnerid));
                                        foreach ($seller_pro_coll as $value) {
                                            if($value->getMageproductid()){
                                                $seller_invoice_id = $inv->getId();
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }


                        $shipment = false;              
                        $shipmentId = $this->getRequest()->getParam('shipment_id');         
                        $orderId = $orderid;    
                        if($shipmentId){
                            $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);
                        }elseif($orderId){
                            $order  = Mage::getModel('sales/order')->load($orderId);
                            if (!$order->getId()) {
                                $this->_getSession()->addError($this->__('The order no longer exists.'));
                                return false;
                            }
                            if($order->getForcedDoShipmentWithInvoice()){
                                $this->_getSession()->addError($this->__('Cannot do shipment for the order separately from invoice.'));
                                return false;
                            }
                            if(!$order->canShip()){
                                $this->_getSession()->addError($this->__('Cannot do shipment for the order.'));
                                return false;
                            }

                            $savedQtys = $this->_getItemQtys($order,$items);
                            $shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($savedQtys['data']);
                        }
                        $shipment->register();
                        $comment = '';
                        $shipment->getOrder()->setCustomerNoteNotify(1);
                        $responseAjax = new Varien_Object();
                        $isNeedCreateLabel = isset($data['create_shipping_label']) && $data['create_shipping_label'];
                        if ($isNeedCreateLabel && true) {
                            $responseAjax->setOk(true);
                        }
                        $shipment->getOrder()->setIsInProcess(true);
                        $transactionSave = Mage::getModel('core/resource_transaction')
                                    ->addObject($shipment)->addObject($shipment->getOrder())->save();
                        /*sale list status update*/
                        $partnerid=Mage::getSingleton('customer/session')->getCustomer()->getId();
                        $collection = Mage::getModel('marketplace/saleslist')->getCollection()
                                    ->addFieldToFilter('mageproownerid', array('eq' => $partnerid))
                                    ->addFieldToFilter('mageorderid', array('eq' => $orderid));
                        foreach($collection as $saleproduct){
                            $saleproduct->setCpprostatus(1);
                            $saleproduct->save();
                        }
                        /*sale list status update*/
                        $shipment->sendEmail(1, $comment);
                        $shipmentCreatedMessage = $this->__('The shipment has been created.');
                        $labelCreatedMessage    = $this->__('The shipping label has been created.');
                        $this->_getSession()->addSuccess($isNeedCreateLabel ? $shipmentCreatedMessage . ' ' . $labelCreatedMessage
                            : $shipmentCreatedMessage); 

                        $trackingcol1=Mage::getModel('mpshippingmanager/tracking')->getCollection()
                                    ->addFieldtoFilter('order_id',array('eq'=>$orderid))
                                    ->addFieldtoFilter('seller_id ',array('in'=>$partnerid));
                        $courrier="custom";
                        foreach($trackingcol1 as $row) {
                            if($shipment->getId() != '') { 
                                $row->setShipmentId($shipment->getId());
                                $row->setInvoiceId($seller_invoice_id)->save();
                                $track = Mage::getModel('sales/order_shipment_track')
                                 ->setShipment($shipment)
                                 ->setData('title',  $row->getCarrierName())
                                 ->setData('number', $row->getTrackingNumber())
                                 ->setData('carrier_code',  $courrier)
                                 ->setData('order_id', $shipment->getData('order_id'))
                                 ->save();
                            }
                        }
                    }
                    $trackingcol=Mage::getModel('mpshippingmanager/tracking')->getCollection()
                                    ->addFieldtoFilter('order_id',array('eq'=>$orderid))
                                    ->addFieldtoFilter('tracking_number',array('eq'=>''));
                    if(count($trackingcol)==0){
                        $trackingcol=Mage::getModel('mpshippingmanager/tracking')->getCollection()
                                    ->addFieldtoFilter('order_id',array('eq'=>$orderid))
                                    ->addFieldtoFilter('tracking_number',array('eq'=>'canceled'));
                        $order = Mage::getModel('sales/order')->load($orderid);
                        if(count($trackingcol)==0){
                            $product_order_id = $order->getIncrementId();
                            $order->setStatus('invoice');
                            $order->save();
                        }else{
                            $order->cancel();
                            $order->save();
                        }
                        $order->sendNewOrderEmail();
                        $historyItem = Mage::getResourceModel('sales/order_status_history_collection')
                            ->getUnnotifiedForInstance($order, Mage_Sales_Model_Order::HISTORY_ENTITY_NAME);
                        if ($historyItem) {
                            $historyItem->setIsCustomerNotified(1);
                            $historyItem->save();
                        }       
                    }
                    if(count($trackingsdata)==0)
                        Mage::getSingleton('core/session')->addError("This order doesn't contain Tracking Number..");
                    else
                        Mage::getSingleton('core/session')->addSuccess('Tracking Number successfully Assign..');
                    $this->_redirect('mpshippingmanager/shipping/invoice/id/',array('id'=>$orderid));
                }
            }catch(Exception $e){
                Mage::getSingleton('core/session')->addError($e->getMessage());
                $this->_redirect('mpshippingmanager/shipping/invoice/id/',array('id'=>$orderid));
            }
        }

.phtml file

    <form action="<?php echo $this->getUrl('mpshippingmanager/shipping/savetrackingnumber')?>" id="shipping-form" method="post">
<div class="shipping_top">
<span class="shipping_service"><?php echo $helper->__('Carrier');?></span>
<span class="row_total"><?php echo $helper->__('Tracking Number');?></span>
</div>
<div class="items">
<input class="required-entry" type="hidden" value="<?php echo $_order->getId(); ?>" name="order_id"/>
<div class="wk_item">
<span class="carrier"><input class="required-entry carrier" value="<?php echo $tracking->getCarrierName(); ?>" <?php echo $disabled;?> type="text" name="carrier"/></span>
<span class="row_total wk_track_input"><input class="required-entry" value="<?php echo $tracking->getTrackingNumber(); ?>" <?php echo $disabled;?> type="text" name="tracking_id"/></span>
<?php if(count($shipping_coll)): ?>
<?php if($item_status=="Ordered"): ?>
<a href="<?php echo $this->getUrl('mpshippingmanager/shipping/cancelorder',array('id'=>$mageorderid))?>">
<button class="button wk_mp_btn" style="flaot:none" title="<?php echo $helper->__('Cancel Order') ?>" type="button" id="save_butn" >
<span><span><?php echo $helper->__('Cancel Order') ?></span></span>
</button>
</a>
<?php endif;  ?>
<?php endif; ?>
<?php if($item_status=="Ordered"||$item_status=="Invoiced"){?>
<button class="button wk_mp_btn" style="flaot:none" title="<?php echo $helper->__('Save Tracking Number') ?>" type="submit" id="save_butn" >
<span><span><?php echo $helper->__('Save Tracking Number') ?></span></span>
</button>
<?php } ?>
</div>
</div>
</form>

Now i change Complete into invoice it also working fine but after that i add custom order status called shipement if i click shipement order status automatically change into complete. But it should change shipement what is the problem.

Custom Method

public function shipementorderAction()
{
    $id = $this->getRequest()->getParam('id');

    $order = Mage::getModel('sales/order')->load($id);
    $order->setData('state', 'shipement');
    $order->setStatus('shipement');
    $history = $order->addStatusHistoryComment('', false);
    $history->setIsCustomerNotified(false);
    $order->save(); 

    $order->sendOrderUpdateEmail(true, null);

    $this->_getSession()->addSuccess(
        $this->__('The order state has been changed.')
    );
    //$this->_redirect('mpshippingmanager/shipping/index');
    Mage::getSingleton('core/session') ->addSuccess('Order Updated Successfully'); $this->_redirectReferer();


}

.phtml

 <a href="<?php echo $this->getUrl('mpshippingmanager/shipping/shipementorder',array('id'=>$mageorderid))?>">
<button class="button wk_mp_btn" style="flaot:none" title="<?php echo $helper->__('Shipment') ?>" type="button" id="save_butn" >
<span><span><?php echo $helper->__('Shipment') ?></span></span>
</button>
</a> 
2
  • are you facing this issue only order view page or order listing page?
    – Abdul
    Commented Dec 22, 2015 at 11:34
  • While Click save Tracking Number it automatically change order status Complete i want to change invoiced and one more button if i click status change into shipement
    – Magento 2
    Commented Dec 22, 2015 at 11:59

1 Answer 1

0

use this for creating the invoice

     public function saveTrackingNumberAction(){

                     $orderid=$this->getRequest()->getParam('order_id');

   $order=Mage::getModel('sales/order')->load($orderid);
     try {
           if(!$order->canInvoice())
           {

           }else {

           $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();

           if (!$invoice->getTotalQty()) {
              $error=true;

           }else {

           $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
           $invoice->register();
           $transactionSave = Mage::getModel('core/resource_transaction')
           ->addObject($invoice)
           ->addObject($invoice->getOrder());

           $transactionSave->save();
           $sucess=true;

              $order->setData('state', 'invoice');
              $order->setStatus('invoice');
              $history = $order->addStatusHistoryComment('', false);


              $history->setIsCustomerNotified(true);
              $invoice->sendEmail(true, '');

              $order->save();


        Mage::getSingleton('core/session')->addSuccess('Tracking Number successfully Assign..');
                $this->_redirect('mpshippingmanager/shipping/invoice/id/',array('id'=>$orderid));


           }}
        }
     catch (Mage_Core_Exception $e) {

        }}

use this code to create shipment simple create the shipment

public function shipementorderAction()
    {  
    $orderid=$this->getRequest()->getParam('order_id');
      $order = Mage::getModel('sales/order')->load($orderid);
   if($order->canShip())
{
$itemQty =  $order->getItemsCollection()->count();
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);
$shipment = new Mage_Sales_Model_Order_Shipment_Api();
$shipmentId = $shipment->create($orderId);
 $order->setData('state', 'shipment');
          $order->setStatus('shipment');
          $history = $order->addStatusHistoryComment('', false);


          $history->setIsCustomerNotified(true);
          $shipment->sendEmail(true, '');

          $order->save();


    Mage::getSingleton('core/session')->addSuccess('Tracking Number successfully Assign..');
            $this->_redirect('mpshippingmanager/shipping/invoice/id/',array('id'=>$orderid));

}}
45
  • While Click save Tracking Number it automatically change order status Complete i want to change invoiced and one more button if i click status change into shipement
    – Magento 2
    Commented Dec 22, 2015 at 11:59
  • you are put the code there for change the status to invoice that are not working? Commented Dec 22, 2015 at 12:03
  • Ya it change into invoiced but i have one more button shipement if i click it change into complete
    – Magento 2
    Commented Dec 22, 2015 at 12:06
  • it should change shipement only bt it changed complete
    – Magento 2
    Commented Dec 22, 2015 at 12:06
  • on shipmentorder action your are talking about? Commented Dec 22, 2015 at 12:10

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.