public function updateorderstatus()
 {
     //$from = 'Robin'; // sender
     //$subject = 'ABCD';
     $message = 'Cron Job Running123456';
     // message lines should not exceed 70 characters (PHP rule), so wrap it
     //$message = wordwrap($message, 70);
     // send mail
     $from = 'Arun';
     // sender
     $subject = 'ABCD12345';
     $wsdlUrl = Mage::getStoreConfig('unicom_options/unigroup/uniurl');
     $wsdllUrl = Mage::getStoreConfig('unicom_options/unigroup/unilurl');
     $username = Mage::getStoreConfig('unicom_options/unigroup/uniusername');
     $password = Mage::getStoreConfig('unicom_options/unigroup/unipass');
     /*$wsdlUrl = "https://demo.unicommerce.com/services/soap/uniware15.wsdl";
     	$wsdllUrl = "http://demo.unicommerce.com/services/soap/?version=1.5&facility=01";
     	$username = "******";
     	$password = "******"; */
     $soapClient = new SoapClient($wsdlUrl, array('trace' => 1, 'exception' => 0));
     $soapClient->__setLocation($wsdllUrl);
     $strWSSENS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
     $objSoapVarUser = new SoapVar($username, XSD_STRING, NULL, $strWSSENS, "UserName", $strWSSENS);
     $objSoapVarPass = new SoapVar($password, XSD_STRING, NULL, $strWSSENS, "Password", $strWSSENS);
     $objWSSEAuth = new clsWSSEAuth($objSoapVarUser, $objSoapVarPass);
     $objSoapVarWSSEAuth = new SoapVar($objWSSEAuth, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
     $objWSSEToken = new clsWSSEToken($objSoapVarWSSEAuth);
     $objSoapVarWSSEToken = new SoapVar($objWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
     $objSoapVarHeaderVal = new SoapVar($objSoapVarWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'Security', $strWSSENS);
     $objSoapVarWSSEHeader = new SoapHeader($strWSSENS, 'Security', $objSoapVarHeaderVal, true);
     $soapClient->__setSoapHeaders(array($objSoapVarWSSEHeader));
     //Mage::app('default');
     $model = Mage::getModel('rcredit/rcredit');
     $order = new Mage_Sales_Model_Order();
     $order_details = $model->orderids();
     //print_r($order_details);
     $i = 0;
     $check = array();
     $msg = array();
     foreach ($order_details as $details) {
         $msg[] = $details[order_id];
         $shipping = $soapClient->GetSaleOrder(array('SaleOrder' => array('Code' => $details[order_id])));
         //echo '<pre>';
         //print_r($shipping);
         $delivcnt = count($shipping->SaleOrder->ShippingPackages->ShippingPackage);
         foreach ($shipping->SaleOrder->ShippingPackages as $v) {
             $order->loadByIncrementId($details[order_id]);
             $payment = $order->getPayment()->getMethodInstance()->getCode();
             $customer_status = $model->customerstatus($order->getCustomerEmail());
             if ($delivcnt > 1) {
                 $deliverDate = $v[$delivcnt - 1]->DeliveredOn;
                 $vstatus = $v[$delivcnt - 1]->StatusCode;
             } else {
                 $deliverDate = $v->DeliveredOn;
                 $vstatus = $v->StatusCode;
             }
             if ($vstatus == 'DELIVERED') {
                 $day = explode('T', $deliverDate);
                 if ($day[0] >= $customer_status['credit_start_date']) {
                     //echo 'In Second Cycle'.$details[cycle].'-->'.$details[order_id].'-->'.$details[order_value].'-->'.$order->getCustomerEmail().'-->'.$customer_status[credit_cycle].'-->'.$customer_status[utlized_amt];
                     //echo '<br/>';
                     $model->updateordercycle($details[order_id], $customer_status[credit_cycle]);
                 }
                 $model->updateorderdate($details[order_id], $day[0]);
             }
             $i++;
         }
         //mail("*****@*****.**",$subject,$details[order_id],"From: $from\n");
         $order = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('increment_id', $details[order_id])->getFirstItem();
         if ($order->getStatus() == 'Canceled' || $order->getStatus() == 'canceled') {
             $order->loadByIncrementId($details[order_id]);
             $payment = $order->getPayment()->getMethodInstance()->getCode();
             if ($payment == 'rcredit') {
                 $payment_status = 'Refund';
                 $order_cycle_value = $model->ordercyclehistory($details[order_id]);
                 //Getting the cycle using the order_id.
                 $cycle = $order_cycle_value['cycle'];
                 $customer_email = $order->getCustomerEmail();
                 $grandtotal = round($details[order_value], 2);
                 $customer_status = $model->customerstatus($customer_email);
                 // Getting the customer status from the Shopper Credit Module
                 $customer_credit_limit = $customer_status['credit_limit'];
                 // Getting customer's credit limit.
                 $customer_current_bal = $customer_status['bal_amt'];
                 // Getting customer's current balance
                 $balance_amount = $customer_status['bal_amt'] + $grandtotal;
                 // Calculating the updated balance after sucessful purchase.
                 $utlized_amt = $customer_status['utlized_amt'];
                 // Getting customer's current balance
                 $total_used_amt = $utlized_amt - $grandtotal;
                 // Getting the total utlized amt.
                 $pay_interval = $customer_status['pay_interval'];
                 // Getting customer's payinterval date
                 $expiry_date = $customer_status['enddate'];
                 // Getting expiry date for the customer
                 $action = 'Refund due to cancellation';
                 $todate = date("Y-m-d");
                 $order_status = "Cancel";
                 if ($order_cycle_value['action'] != 'Cancel') {
                     $model->cancelledorderdetails($details[order_id], $total_used_amt, $balance_amount, $action, $order_status);
                     $model->updateoncancel($grandtotal, $customer_email, $cycle);
                 }
             }
         }
     }
 }
Example #2
0
 public function unishipimport()
 {
     $wsdlUrl = Mage::getStoreConfig('unicom_options/unigroup/uniurl');
     $wsdllUrl = Mage::getStoreConfig('unicom_options/unigroup/unilurl');
     $username = Mage::getStoreConfig('unicom_options/unigroup/uniusername');
     $password = Mage::getStoreConfig('unicom_options/unigroup/unipass');
     $soapClient = new SoapClient($wsdlUrl, array('trace' => 1, 'exception' => 0));
     $soapClient->__setLocation($wsdllUrl);
     $strWSSENS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
     $objSoapVarUser = new SoapVar($username, XSD_STRING, NULL, $strWSSENS, "UserName", $strWSSENS);
     $objSoapVarPass = new SoapVar($password, XSD_STRING, NULL, $strWSSENS, "Password", $strWSSENS);
     $objWSSEAuth = new clsWSSEAuth($objSoapVarUser, $objSoapVarPass);
     $objSoapVarWSSEAuth = new SoapVar($objWSSEAuth, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
     $objWSSEToken = new clsWSSEToken($objSoapVarWSSEAuth);
     $objSoapVarWSSEToken = new SoapVar($objWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
     $objSoapVarHeaderVal = new SoapVar($objSoapVarWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'Security', $strWSSENS);
     $objSoapVarWSSEHeader = new SoapHeader($strWSSENS, 'Security', $objSoapVarHeaderVal, true);
     $soapClient->__setSoapHeaders(array($objSoapVarWSSEHeader));
     $resource = Mage::getSingleton('core/resource');
     $readConnection = $resource->getConnection('core_read');
     $writeConnection = $resource->getConnection('core_write');
     $query = 'SELECT a.*,b.* FROM `sales_flat_order` as a,`unicom_invoice` as b WHERE a.increment_id = b.order_id and b.pdf_complete = "No" and a.state NOT IN ("canceled","pending")';
     $orders = $readConnection->fetchAll($query);
     foreach ($orders as $order) {
         $shipping = $soapClient->GetSaleOrder(array('SaleOrder' => array('Code' => $order['increment_id'])));
         if ($shipping->Successful == 1) {
             $chkshippackages = count(get_object_vars($shipping->SaleOrder->ShippingPackages));
             if ($chkshippackages > 0) {
                 $shippackages = $shipping->SaleOrder->ShippingPackages->ShippingPackage;
                 if (is_array($shippackages)) {
                     foreach ($shippackages as $shippackage) {
                         if ($shippackage->ShipmentCode) {
                             if ($order['ship_code'] == '' && $order['state'] == 'complete') {
                                 $shipSelect = "SELECT ship_code FROM `unicom_invoice` WHERE ship_code != '' and order_id = '" . $order['increment_id'] . "'";
                                 $shipcodes = $readConnection->fetchAll($shipSelect);
                                 if (count($shipcodes) > 0) {
                                     $insertquery = "INSERT INTO `unicom_invoice` (`order_id`, `ship_code`, `state_complete`, `pdf_complete`) VALUES ('" . $order['increment_id'] . "', '" . $shippackage->ShipmentCode . "', 'Yes', 'No')";
                                     $writeConnection->query($insertquery);
                                 } else {
                                     $insertquery = "UPDATE `unicom_invoice` SET state_complete = 'Yes',ship_code = '" . $shippackage->ShipmentCode . "' WHERE order_id = '" . $order['increment_id'] . "'";
                                     $writeConnection->query($insertquery);
                                 }
                             } elseif ($order['ship_code'] == '') {
                                 $shipSelect = "SELECT ship_code FROM `unicom_invoice` WHERE ship_code != '' and order_id = '" . $order['increment_id'] . "'";
                                 $shipcodes = $readConnection->fetchAll($shipSelect);
                                 if (count($shipcodes) > 0) {
                                     $insertquery = "INSERT INTO `unicom_invoice` (`order_id`, `ship_code`, `state_complete`, `pdf_complete`) VALUES ('" . $order['increment_id'] . "', '" . $shippackage->ShipmentCode . "', 'No', 'No')";
                                     $writeConnection->query($insertquery);
                                 } else {
                                     $insertquery = "UPDATE `unicom_invoice` SET ship_code = '" . $shippackage->ShipmentCode . "' WHERE order_id = '" . $order['increment_id'] . "'";
                                     $writeConnection->query($insertquery);
                                 }
                             } elseif ($order['ship_code'] != '' && $order['state'] == 'complete') {
                                 $shipSelect = "SELECT ship_code FROM `unicom_invoice` WHERE ship_code ='" . $shippackage->ShipmentCode . "'";
                                 $shipcodes = $readConnection->fetchAll($shipSelect);
                                 if (count($shipcodes) > 0) {
                                     $insertquery = "UPDATE `unicom_invoice` SET state_complete = 'Yes' WHERE ship_code = '" . $order['ship_code'] . "'";
                                     $writeConnection->query($insertquery);
                                 } else {
                                     $insertquery = "INSERT INTO `unicom_invoice` (`order_id`, `ship_code`, `state_complete`, `pdf_complete`) VALUES ('" . $order['increment_id'] . "', '" . $shippackage->ShipmentCode . "', 'Yes', 'No')";
                                     $writeConnection->query($insertquery);
                                 }
                             } else {
                                 $shipSelect = "SELECT ship_code FROM `unicom_invoice` WHERE ship_code ='" . $shippackage->ShipmentCode . "'";
                                 $shipcodes = $readConnection->fetchAll($shipSelect);
                                 if (count($shipcodes) < 1) {
                                     $insertquery = "INSERT INTO `unicom_invoice` (`order_id`, `ship_code`, `state_complete`, `pdf_complete`) VALUES ('" . $order['increment_id'] . "', '" . $shippackage->ShipmentCode . "', 'Yes', 'No')";
                                     $writeConnection->query($insertquery);
                                 }
                             }
                         }
                     }
                 } else {
                     if ($shippackages->ShipmentCode) {
                         if ($order['ship_code'] == '' && $order['state'] == 'complete') {
                             $insertquery = "UPDATE `unicom_invoice` SET state_complete = 'Yes',ship_code = '" . $shippackages->ShipmentCode . "' WHERE order_id = '" . $order['increment_id'] . "'";
                             $writeConnection->query($insertquery);
                         } elseif ($order['ship_code'] == '') {
                             $insertquery = "UPDATE `unicom_invoice` SET ship_code = '" . $shippackages->ShipmentCode . "' WHERE order_id = '" . $order['increment_id'] . "'";
                             $writeConnection->query($insertquery);
                         } elseif ($order['ship_code'] != '' && $order['state'] == 'complete') {
                             $insertquery = "UPDATE `unicom_invoice` SET state_complete = 'Yes' WHERE order_id = '" . $order['increment_id'] . "'";
                             $writeConnection->query($insertquery);
                         }
                     }
                 }
             }
         }
     }
 }
$objSoapVarPass = new SoapVar($password, XSD_STRING, NULL, $strWSSENS, "Password", $strWSSENS);
$objWSSEAuth = new clsWSSEAuth($objSoapVarUser, $objSoapVarPass);
$objSoapVarWSSEAuth = new SoapVar($objWSSEAuth, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
$objWSSEToken = new clsWSSEToken($objSoapVarWSSEAuth);
$objSoapVarWSSEToken = new SoapVar($objWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
$objSoapVarHeaderVal = new SoapVar($objSoapVarWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'Security', $strWSSENS);
$objSoapVarWSSEHeader = new SoapHeader($strWSSENS, 'Security', $objSoapVarHeaderVal, true);
$soapClient->__setSoapHeaders(array($objSoapVarWSSEHeader));
$write->query("truncate `mw_partial_ship`");
$results = $read->fetchAll("select `DisplayOrderCode` from mw_loyalty_invoice_uniware where invoice_done=0");
//$results = $read->fetchAll("select `DisplayOrderCode` from mw_loyalty_invoice_uniware where DisplayOrderCode='150516C-528516'");
if (count($results) > 0) {
    foreach ($results as $rs) {
        $orderIncrementId = $rs['DisplayOrderCode'];
        echo $orderIncrementId;
        $orderDetails = $soapClient->GetSaleOrder(array('SaleOrder' => array('Code' => $orderIncrementId)));
        if (count($orderDetails->SaleOrder->SaleOrderItems->SaleOrderItem) > 0) {
            if (count($orderDetails->SaleOrder->SaleOrderItems->SaleOrderItem) == 1) {
                $temporderDetails = $orderDetails->SaleOrder->SaleOrderItems;
            } else {
                $temporderDetails = $orderDetails->SaleOrder->SaleOrderItems->SaleOrderItem;
            }
            foreach ($temporderDetails as $tm) {
                echo $tm->ItemSKU . " >>> " . $tm->ShippingPackageCode . "<br>";
                $resultqty = $read->fetchRow("select `qty` from `mw_partial_ship` where ItemSKU='" . $tm->ItemSKU . "' and ShippingPackageCode='" . $tm->ShippingPackageCode . "'");
                if ($resultqty['qty'] > 0) {
                    $qty = $resultqty['qty'] + 1;
                    $write->query("update `mw_partial_ship` SET qty={$qty} where ItemSKU='" . $tm->ItemSKU . "' and ShippingPackageCode='" . $tm->ShippingPackageCode . "'");
                } else {
                    $write->query("insert into `mw_partial_ship` (order_id, ItemSKU, ShippingPackageCode, qty) values('" . $orderIncrementId . "', '" . $tm->ItemSKU . "', '" . $tm->ShippingPackageCode . "', 1)");
                }
 public function runbabyrun()
 {
     $from = 'Robin';
     // sender
     $subject = 'RunbabyRun';
     $message = 'RunbabyRun';
     // message lines should not exceed 70 characters (PHP rule), so wrap it
     $message = wordwrap($message, 70);
     // send mail
     mail("*****@*****.**", $subject, $message, "From: {$from}\n");
     $wsdlUrl = Mage::getStoreConfig('unicom_options/unigroup/uniurl');
     $wsdllUrl = Mage::getStoreConfig('unicom_options/unigroup/unilurl');
     $username = Mage::getStoreConfig('unicom_options/unigroup/uniusername');
     $password = Mage::getStoreConfig('unicom_options/unigroup/unipass');
     /*$wsdlUrl = "https://demo.unicommerce.com/services/soap/uniware15.wsdl";
     		$wsdllUrl = "http://demo.unicommerce.com/services/soap/?version=1.5&facility=01";
     		$username = "******";
     		$password = "******"; */
     $soapClient = new SoapClient($wsdlUrl, array('trace' => 1, 'exception' => 0));
     $soapClient->__setLocation($wsdllUrl);
     $strWSSENS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
     $objSoapVarUser = new SoapVar($username, XSD_STRING, NULL, $strWSSENS, "UserName", $strWSSENS);
     $objSoapVarPass = new SoapVar($password, XSD_STRING, NULL, $strWSSENS, "Password", $strWSSENS);
     $objWSSEAuth = new clsWSSEAuth($objSoapVarUser, $objSoapVarPass);
     $objSoapVarWSSEAuth = new SoapVar($objWSSEAuth, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
     $objWSSEToken = new clsWSSEToken($objSoapVarWSSEAuth);
     $objSoapVarWSSEToken = new SoapVar($objWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
     $objSoapVarHeaderVal = new SoapVar($objSoapVarWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'Security', $strWSSENS);
     $objSoapVarWSSEHeader = new SoapHeader($strWSSENS, 'Security', $objSoapVarHeaderVal, true);
     $soapClient->__setSoapHeaders(array($objSoapVarWSSEHeader));
     $count = "";
     $flag = 0;
     umask(0);
     Mage::app('default');
     $model = Mage::getModel('rcredit/rcredit');
     $todate = date("Y-m-d");
     $start_date_month = date('Y-m-02', strtotime($todate));
     $j = 0;
     $customer_details = $model->customerdetails();
     //echo '<pre>';
     //print_r($customer_details);
     $date = new DateTime($start_date_month);
     //if($todate == $start_date_month)
     //{
     for ($j = 0; $j < count($customer_details); $j++) {
         $order_details = $model->orderhistory($customer_details[$j]['email'], $customer_details[$j]['credit_cycle'] - 1);
         //print_r($order_details);die;
         $new_cycle = $customer_details[$j]['credit_cycle'] + 1;
         $month = $date->format("m");
         $year = $date->format("Y");
         $days_in_month = date('t');
         $payment_days = $days_in_month + 0 + $customer_details[$j]['pay_interval'];
         //days in current month + 2 days + pay interval days
         $expiry_date = date('Y-m-d', strtotime('+' . $days_in_month . ' days', strtotime($start_date_month)));
         // Getting the credit expiry date
         $payment_date = date('Y-m-d', strtotime('+' . $payment_days . ' days', strtotime($start_date_month)));
         // Getting the payment expiry date
         $sub_total_final = array();
         $order_ids = array();
         $subtotal = 0;
         $i = 0;
         if ($order_details != NULL) {
             foreach ($order_details as $details) {
                 $order = Mage::getModel('sales/order')->load($details['order_id'], 'increment_id');
                 $items = $order->getAllItems();
                 foreach ($items as $item) {
                     $itemcount = $item->getQtyOrdered();
                     $item_count = $item_count + $itemcount;
                 }
                 $deliverydate = $model->deliverydate($details['order_id']);
                 foreach ($deliverydate as $delivery) {
                     if (($order->getStatus() == 'Complete' || $order->getStatus() == 'complete') && $delivery[order_complete_date] < $start_date_month) {
                         $order_ids[$i] = trim($details['order_id']);
                         $shipping = $soapClient->GetSaleOrder(array('SaleOrder' => array('Code' => $order_ids[$i])));
                         if ($item_count == 1) {
                             foreach ($shipping->SaleOrder->SaleOrderItems as $SaleOrderItem) {
                                 if (trim($SaleOrderItem->StatusCode) == 'DELIVERED' || $SaleOrderItem->StatusCode == 'DELIVERED') {
                                     $total = $SaleOrderItem->SellingPrice;
                                     $subtotal = $subtotal + $total;
                                 }
                             }
                         } elseif ($item_count > 1) {
                             foreach ($shipping->SaleOrder->SaleOrderItems->SaleOrderItem as $v) {
                                 if (trim($v->StatusCode) == 'DELIVERED' || $v->StatusCode == 'DELIVERED') {
                                     $total = $v->SellingPrice;
                                     $subtotal = $subtotal + $total;
                                 }
                             }
                         }
                         round($subtotal, 2);
                         $deciaml_pos = explode('.', $subtotal);
                         if ($deciaml_pos[1] >= 5) {
                             $subtotal = ceil($subtotal);
                         } elseif ($deciaml_pos[1] < 5) {
                             $subtotal = floor($subtotal);
                         }
                         $sub_total_final[$i] = $subtotal;
                         $i++;
                         $subtotal = 0;
                     }
                     if (($order->getStatus() == 'Complete' || $order->getStatus() == 'complete') && $delivery[order_complete_date] > $start_date_month) {
                         $customer_total = 0;
                     }
                 }
                 $item_count = 0;
             }
             $lasy_key = key(array_slice($order_ids, -1, 1, TRUE));
             for ($k = 0; $k <= $lasy_key; $k++) {
                 if ($order_ids[$k] != NULL && $sub_total_final[$k] != NULL) {
                     //$model->unicomupdate($order_ids[$k],$sub_total_final[$k]); //Update against each order
                 }
                 $customer_total = $customer_total + $sub_total_final[$k];
             }
             round($customer_total, 2);
             $deciaml_no = explode('.', $customer_total);
             if ($deciaml_no[1] >= 5) {
                 $customer_total = ceil($customer_total);
             } elseif ($deciaml_no[1] < 5) {
                 $customer_total = floor($customer_total);
             }
             $bal_amount = $customer_details[$j]['credit_limit'] - $customer_total;
             //echo $customer_details[$j]['email'].$customer_total.'---'.$customer_details[$j]['credit_limit'].'Balance '.$bal_amount;
             //echo '<br/>';
             //$model->finalupadte($customer_details[$j]['email'],$customer_total,$bal_amount,2);
             $freeSubTotal = Mage::getStoreConfig('carriers/freeshipping/free_shipping_subtotal');
             if ($customer_total > $freeSubTotal) {
                 $customer_total = $customer_total;
             } else {
                 if ($customer_total < $freeSubTotal && $customer_total != 0) {
                     $shipping_rate = Mage::getStoreConfig('carriers/flatrate/price');
                     $shipping_rate = number_format($shipping_rate, 2);
                     $customer_total = $customer_total + $shipping_rate;
                 }
             }
             //Sending mail
             $pdf = new FPDF('P', 'pt', array(500, 233));
             $pdf->AddPage();
             $pdf->SetFont('arial', '', 16);
             $pdf = new FPDF();
             $pdf->AddPage();
             // email stuff (change data below)
             //$to = $customer_details[$j]['email'];
             $to = '*****@*****.**';
             $from = "*****@*****.**";
             $subject = 'Invoice';
             $message = "<p>Please see the attachment.</p>";
             $img_path = Mage::getBaseUrl() . 'skin/frontend/default/zoffio/images/logo_print.gif';
             $store_address = Mage::getStoreConfig('general/store_information/address');
             $end_date = $model->enddate($customer_details[$j]['email'], $customer_details[$j]['credit_cycle'] - 1);
             // Getting the customer payment date
             $pay_date = new DateTime($end_date[pay_date]);
             $customer_pay_date = $pay_date->format('d-m-Y');
             //Formatting the payment date.
             $start_date = new DateTime($end_date[start_date]);
             $customer_start_date = $start_date->format('d-m-Y');
             $end_date = new DateTime($end_date[end_date]);
             $customer_end_date = $end_date->format('d-m-Y');
             $customer_info = $model->customercycledetails($customer_details[$j]['email']);
             $split_store_address = explode(',', $store_address);
             $linestyle = array('width' => 0.1, 'cap' => 'butt', 'join' => 'miter', 'dash' => '', 'phase' => 10, 'color' => array(255, 0, 0));
             $pdf->Image($img_path, 10, 10, 31, 20, 'PNG', '', '', false, 300, '', false, false, 0, false, false, false);
             $pdf->SetY(20);
             $pdf->SetFont('Times', 'B', 11);
             $pdf->Cell(180, 0, 'Corporate Credit Invoice', 2, 2, 'C');
             $pdf->Line(80, 22, 120, 22, $linestyle);
             $pdf->SetX(100);
             $pdf->SetFont('Times', 'B', 11);
             $pdf->Cell(0, 0, $split_store_address[0], 0, 0, 'R');
             $pdf->SetFont('Times', '', 9);
             $pdf->Cell(0, 10, $split_store_address[1] . ',' . $split_store_address[2], 0, 0, 'R');
             $pdf->Cell(0, 20, $split_store_address[3] . ',' . $split_store_address[4] . ',' . $split_store_address[5], 0, 0, 'R');
             $pdf->SetFont('Times', '', 8);
             $pdf->Line(5, 40, 200, 40, $linestyle);
             $pdf->SetX(20);
             $pdf->SetY(30);
             $pdf->Cell(0, 30, 'Customer: ' . $customer_info[fname] . '.' . $customer_info[lname], 0, 0, 'L');
             $pdf->SetX(75);
             $pdf->Cell(0, 30, 'Company: ' . $customer_info[company_name], 0, 0, 'L');
             $pdf->SetX(150);
             $pdf->Cell(0, 30, 'Email: ' . $customer_details[$j]['email'], 0, 0, 'L');
             $pdf->Line(5, 50, 200, 50, $linestyle);
             $pdf->SetX(20);
             $pdf->SetY(40);
             $pdf->Cell(0, 30, 'Credit Period: ' . $customer_start_date . '  to  ' . $customer_end_date, 0, 0, 'L');
             $pdf->SetX(75);
             $pdf->Cell(0, 30, 'Amount Need to Pay: ' . $customer_total, 0, 0, 'L');
             $pdf->SetX(150);
             $pdf->Cell(0, 30, 'Payment Due Date: ' . $customer_pay_date, 0, 0, 'L');
             $pdf->Line(5, 60, 200, 60, $linestyle);
             $i = 10;
             $pdf->SetFont('Times', '', 6);
             $pdf->SetX(20);
             $pdf->SetY(70);
             $pdf->SetX(20);
             $pdf->Cell(20, 10, 'Order Number', 1, 0, 'C');
             $pdf->Cell(20, 10, 'Order Amount', 1, 0, 'C');
             $pdf->Cell(25, 10, 'Order Date', 1, 0, 'C');
             $pdf->Cell(30, 10, 'Order Delivery Date', 1, 1, 'C');
             $pdf->SetX(20);
             $pdf->SetY(80);
             $pdf->SetX(20);
             foreach ($order_details as $details) {
                 $order = Mage::getModel('sales/order')->load($details['order_id'], 'increment_id');
                 if ($order->getStatus() == 'Complete' || $order->getStatus() == 'complete') {
                     echo $details['order_id'];
                     echo '<br/>';
                     $i = $i + 1;
                     $pdf->SetX(20);
                     $pdf->Cell(20, $i, $details['order_id'], 1, 0, 'C');
                     $pdf->Cell(20, $i, $details['order_value'], 1, 0, 'C');
                     $pdf->Cell(25, $i, $details['order_date'], 1, 0, 'C');
                     $pdf->Cell(30, $i, $details['order_complete_date'], 1, 1, 'C');
                     //$pdf->SetX(20);
                 }
             }
             $pdf->SetFont('Times', 'B', 8);
             $pdf->SetX(80);
             //$pdf->Cell(100,20,'Total : Rs.'.$details['utlized_amt'].'/-', 0,0,'R');
             // a random hash will be necessary to send mixed content
             $separator = md5(time());
             // carriage return type (we use a PHP end of line constant)
             $eol = PHP_EOL;
             // attachment name
             $filename = "Invoice.pdf";
             // encode data (puts attachment in proper format)
             $pdfdoc = $pdf->Output("", "S");
             $attachment = chunk_split(base64_encode($pdfdoc));
             // main header
             $headers = "From: " . $from . $eol;
             $headers .= "MIME-Version: 1.0" . $eol;
             $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"";
             // no more headers after this, we start the body! //
             $body = "--" . $separator . $eol;
             $body .= "Content-Transfer-Encoding: 7bit" . $eol . $eol;
             //$body .= "This is a MIME encoded message.".$eol;
             // message
             $body .= "--" . $separator . $eol;
             $body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
             $body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
             $body .= $message . $eol;
             // attachment
             $body .= "--" . $separator . $eol;
             $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
             $body .= "Content-Transfer-Encoding: base64" . $eol;
             $body .= "Content-Disposition: attachment" . $eol . $eol;
             $body .= $attachment . $eol;
             $body .= "--" . $separator . "--";
             // send message
             if ($customer_total != 0) {
                 mail($to, $subject, $body, $headers);
             }
             $customer_total = 0;
         }
         //$model->updatecycle($todate,$expiry_date,$new_cycle,$customer_details[$j]['email'],$payment_date);
     }
     //}
 }
$objWSSEAuth = new clsWSSEAuth($objSoapVarUser, $objSoapVarPass);
$objSoapVarWSSEAuth = new SoapVar($objWSSEAuth, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
$objWSSEToken = new clsWSSEToken($objSoapVarWSSEAuth);
$objSoapVarWSSEToken = new SoapVar($objWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'UsernameToken', $strWSSENS);
$objSoapVarHeaderVal = new SoapVar($objSoapVarWSSEToken, SOAP_ENC_OBJECT, NULL, $strWSSENS, 'Security', $strWSSENS);
$objSoapVarWSSEHeader = new SoapHeader($strWSSENS, 'Security', $objSoapVarHeaderVal, true);
$soapClient->__setSoapHeaders(array($objSoapVarWSSEHeader));
Mage::app('default');
$model = Mage::getModel('rcredit/rcredit');
$order = new Mage_Sales_Model_Order();
$order_details = $model->orderids();
//print_r($order_details);
$i = 0;
$check = array();
foreach ($order_details as $details) {
    $shipping = $soapClient->GetSaleOrder(array('SaleOrder' => array('Code' => $details[order_id])));
    //echo '<pre>';
    //print_r($shipping);
    foreach ($shipping->SaleOrder->ShippingPackages as $v) {
        $order->loadByIncrementId($details[order_id]);
        $payment = $order->getPayment()->getMethodInstance()->getCode();
        $customer_status = $model->customerstatus($order->getCustomerEmail());
        if ($v->StatusCode == 'DELIVERED') {
            $day = explode('T', $v->DeliveredOn);
            if ($day[0] >= $customer_status['credit_start_date']) {
                //echo 'In Second Cycle'.$details[cycle].'-->'.$details[order_id].'-->'.$details[order_value].'-->'.$order->getCustomerEmail().'-->'.$customer_status[credit_cycle].'-->'.$customer_status[utlized_amt];
                //echo '<br/>';
                $model->updateordercycle($details[order_id], $customer_status[credit_cycle]);
            }
            $model->updateorderdate($details[order_id], $day[0]);
        }