Automatic picklist printing with check for order payment.

Last modified by newacct on Fri, June 25, 2010 09:21
Source|Old Revisions  

This is an old revision of the document!


Overview

This script runs on the development machine in the office (linux) and polls the hosted server over the SOAP API to see if there are any new orders.

If there are it then checks to see if any payment has been received. If all is well then a picklist pops out of the default printer.

If payment has not been received then it waits three minutes before checking again. If the payment is still missing then the order status is updated to ‘on hold’. An order comment is added and the customer is notified that their order is placed on hold via email. A picklist is still created under these circumstance however the bottom has a big message for the benefit of the ware house staff, alerting them to the payment problem.

Setup

The orders that have already been looked at are placed in a text file. This will need to have read/write permission for the script.

This script runs from a cron job, the crontab -e line you will need is:

*/5 * * * * /home/MAGENTO_USER/cron_scripts/print_invoices

You will need to put together a little shell script:

/usr/bin/php /home/MAGENTO_USER/cron_scripts/pickinglist.php > /home/MAGENTO_USER/cron_scripts/picking_list.txt
/usr/bin/lpr /home/MAGENTO_USER/cron_scripts/picking_list.txt

and save it as /home/MAGENTO_USER/cron_scripts/print_invoices with the executable mode set (chmod 777 /home/MAGENTO_USER/cron_scripts/print_invoices).

Script

You will need to put this on the local machine in /home/MAGENTO_USER/cron_scripts/pickinglist.php

<?php
// Magento Picking List Generator - pickinglist.php
//
//
// Login to Magento SOAP

$host= 'magento.co.uk';
$client= new SoapClient('http://'.$host.'/index.php/api/soap/?wsdl');
$apiuser= 'USER';
$apikey = 'PASS';

$sess_id= $client->login($apiuser, $apikey);

$current_month=(int)date("m");
$current_year=(int)date("y");
$last_month=($current_month+11)%12;
$last_year=$current_year;
if ($last_month==12) $last_year=$last_year-1;

// read the existing orders that have been picked
$picked=array();
$picked_file = "/home/webservice/picking_list/picked.txt";
$picked_handle=fopen($picked_file, 'r');
while (!feof($picked_handle)) $picked[] = trim(fgets($picked_handle));
// then close and reopen it in append mode so we can add to it
fclose($picked_handle);
$picked_handle=fopen($picked_file, 'a');

//Get the orders
$orders = $client->call($sess_id, 'sales_order.list');
foreach ($orders as $order)
//See if they have been seen before (($order['state']=="new")&&
{ if (!in_array($order['increment_id'], $picked))
  { // Get the full customer detail
    try
    { $customer_info = $client->call($sess_id, 'customer.info', $order['customer_id']);
    }
    catch (Exception $e)
    { echo $e;
    }
    echo "nnPicklist for order: ".$order['increment_id']." (Placed on ".mysql2date('l, jS F, Y g:i a', $order['created_at']).")nn";
    // if B2B then have the account number in the header
    if($order['customer_taxvat']=="")
    { echo $order['billing_name']." - ".$order['customer_email']."nn";
    }
    else
    { echo $customer_info['company_name']." (".$order['customer_taxvat'].") - ".$order['customer_email']."nn";
    }
    // try to read in the order
    try
    { $order_info = $client->call($sess_id, 'sales_order.info', $order['increment_id']);
    }
    catch (Exception $e)
    { echo $e;
    }
    $items=$order_info['items'];
    $items_this_order=0;
    foreach ($items as $item)
    { if (strcmp($item['product_type'],"simple")==0)
      { echo (int)$item['qty_ordered']."t".$item['sku']."t".$item['name']."n";
        $items_this_order=$items_this_order+(int)$item['qty_ordered'];
      }
    }
    fwrite($picked_handle, $order['increment_id']."n");
    echo "nnTotal items: ".$items_this_order."nnnn";
    if((int)$order['total_paid']==0)
    { sleep (180);
      try
      { $suspect_order = $client->call($sess_id, 'sales_order.info', $order['increment_id']);
      }
      catch (Exception $e)
      { echo $e;
      }

      if((int)$suspect_order['total_paid']==0)
      { try
        { $hold_order = $client->call($sess_id, 'sales_order.addComment', array($order['increment_id'], 'holded', 'Payment has yet to be received for this order from payment gateway. This order is now on hold. Please call if you wish to proceed with this order, thankyou.', true));
          echo "THIS ORDER HAS BEEN PLACED ON HOLD AS PAYMENT MIGHT NOT HAVE BEEN RECEIVED!nnn";
        }
        catch (Exception $e)
        { echo "THIS ORDER IS ON HOLD AS PAYMENT MIGHT NOT HAVE BEEN RECEIVED!nnn";
          echo "The customer may wish to try again - have they been called yet?nn";
        }
      }
    }
  }
}

fclose($picked_handle);

function mysql2date($dateformatstring, $mysqlstring, $translate = true) {
    global $month, $weekday, $month_abbrev, $weekday_abbrev;
    $m = $mysqlstring;
    if ( empty($m) ) {
        return false;
    }
    $i = mktime(substr($m,11,2),substr($m,14,2),substr($m,17,2),substr($m,5,2),substr($m,8,2),substr($m,0,4));
    if ( -1 == $i || false == $i )
        $i = 0;
    if ( !empty($month) && !empty($weekday) && $translate ) {
        $datemonth = $month[date('m', $i)];
        $datemonth_abbrev = $month_abbrev[$datemonth];
        $dateweekday = $weekday[date('w', $i)];
        $dateweekday_abbrev = $weekday_abbrev[$dateweekday];
        $dateformatstring = ' '.$dateformatstring;
        $dateformatstring = preg_replace("/([^])D/", "1".backslashit($dateweekday_abbrev), $dateformatstring);
        $dateformatstring = preg_replace("/([^])F/", "1".backslashit($datemonth), $dateformatstring);
        $dateformatstring = preg_replace("/([^])l/", "1".backslashit($dateweekday), $dateformatstring);
        $dateformatstring = preg_replace("/([^])M/", "1".backslashit($datemonth_abbrev), $dateformatstring);
        $dateformatstring = substr($dateformatstring, 1, strlen($dateformatstring)-1);
    }
    $j = @date($dateformatstring, $i);
    return $j;
}

?>




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs