Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 85 additions & 80 deletions code/DispatchITExporter.php
Original file line number Diff line number Diff line change
@@ -1,85 +1,90 @@
<?php
class DispatchITExporter extends Controller{
class DispatchITExporter extends Controller
{

static $url_segment = 'dispatchitexporter';
public static $url_segment = 'dispatchitexporter';

static $allowed_actions_disabled = array(
'index' => 'ADMIN',
'export' => 'ADMIN',
);
public static $allowed_actions_disabled = array(
'index' => 'ADMIN',
'export' => 'ADMIN',
);

function init(){
parent::init();
if(!(Director::isDev() || Director::is_cli() || Permission::check("ADMIN")))
return Security::permissionFailure($this);
}

function Link($action = null,$id = null){
return Controller::join_links(self::$url_segment,$action,$id);
}

function index(){
return $this->export();
}
public function init()
{
parent::init();
if (!(Director::isDev() || Director::is_cli() || Permission::check("ADMIN"))) {
return Security::permissionFailure($this);
}
}

public function Link($action = null, $id = null)
{
return Controller::join_links(self::$url_segment, $action, $id);
}

public function index()
{
return $this->export();
}

function export(){
$orders = Order::get()
->innerJoin("Address","\"Order\".\"ShippingAddressID\" = \"Address\".\"ID\"")
->filter("SentToDispatchIT", 0)
->filter("Status",Order::config()->placed_status)
->filter("Address.Country", "NZ")
->sort(array("Placed" => "ASC", "Created" => "ASC"));
$output = "";
if($orders->exists()){
foreach($orders as $order){
$address = $order->getShippingAddress();
$name = $address->Company; //TODO: company
if(!$name || $name == ""){
$name = $order->Name;
}
$line = array(
$order->ID, //ConsignmentNumber char(12) Yes Tracking number. Must be unique.
$order->MemberID, //CustomerID char(20) Blank if ommitted
$name, //CompanyName char(40) Yes
$address->Address, //Address1 char(40) Yes
$address->AddressLine2, //Address2 char(40) Yes
$address->Suburb, //Address3 char(40) Blank if ommitted
$address->State, //Address4 char(40) Blank if ommitted
$address->City, //Address5 char(40) Must be valid suburb/city/town.
"", //CustOrderRef char(30) Blank if ommitted
"0", //Carrier smallint Yes 0 = NZ Couriers
"1", //CostCentre smallint Yes 1 = Primary cost centre.
"", //PhoneSTD char(4) Blank if ommitted
$address->Phone, //PhoneNumber char(20) Blank if ommitted
"", //FaxSTD char(4) Blank if ommitted
$order->Fax, //FaxNumber char(20) Blank if ommitted
$order->Email, //EmailAddress char(255)Blank if ommitted
"1", //EmailAddressSend bit TRUE or FALSE
"", //EmailAddress2 char(255)Blank if ommitted
"0", //EmailAddress2Send bit TRUE or FALSE
"", //EmailAddress3 char(255)Blank if ommitted
"0" //EmailAddress3Send bit TRUE or FALSE;
);
$output .= implode("\t",$line)."\n";
$order->SentToDispatchIT = true;
$order->write();
}
//store output in a local file, then output the contents, or a link to the file
//name format: DDcxxxxxxx.TXT
$filename = "DDc".uniqid()."_".date('YmdHis').".txt";
$exportpath = ASSETS_PATH."/_dispatchitexports";
if(!is_dir($exportpath)){
mkdir($exportpath);
}
if(file_put_contents($exportpath."/$filename",$output) === false){
$output = "failed to save file";
}
}else{
$output = "no new orders";
}
header('Content-type: text/plain');
echo $output;
die();
}

}
public function export()
{
$orders = Order::get()
->innerJoin("Address", "\"Order\".\"ShippingAddressID\" = \"Address\".\"ID\"")
->filter("SentToDispatchIT", 0)
->filter("Status", Order::config()->placed_status)
->filter("Address.Country", "NZ")
->sort(array("Placed" => "ASC", "Created" => "ASC"));
$output = "";
if ($orders->exists()) {
foreach ($orders as $order) {
$address = $order->getShippingAddress();
$name = $address->Company; //TODO: company
if (!$name || $name == "") {
$name = $order->Name;
}
$line = array(
$order->ID, //ConsignmentNumber char(12) Yes Tracking number. Must be unique.
$order->MemberID, //CustomerID char(20) Blank if ommitted
$name, //CompanyName char(40) Yes
$address->Address, //Address1 char(40) Yes
$address->AddressLine2, //Address2 char(40) Yes
$address->Suburb, //Address3 char(40) Blank if ommitted
$address->State, //Address4 char(40) Blank if ommitted
$address->City, //Address5 char(40) Must be valid suburb/city/town.
"", //CustOrderRef char(30) Blank if ommitted
"0", //Carrier smallint Yes 0 = NZ Couriers
"1", //CostCentre smallint Yes 1 = Primary cost centre.
"", //PhoneSTD char(4) Blank if ommitted
$address->Phone, //PhoneNumber char(20) Blank if ommitted
"", //FaxSTD char(4) Blank if ommitted
$order->Fax, //FaxNumber char(20) Blank if ommitted
$order->Email, //EmailAddress char(255)Blank if ommitted
"1", //EmailAddressSend bit TRUE or FALSE
"", //EmailAddress2 char(255)Blank if ommitted
"0", //EmailAddress2Send bit TRUE or FALSE
"", //EmailAddress3 char(255)Blank if ommitted
"0" //EmailAddress3Send bit TRUE or FALSE;
);
$output .= implode("\t", $line)."\n";
$order->SentToDispatchIT = true;
$order->write();
}
//store output in a local file, then output the contents, or a link to the file
//name format: DDcxxxxxxx.TXT
$filename = "DDc".uniqid()."_".date('YmdHis').".txt";
$exportpath = ASSETS_PATH."/_dispatchitexports";
if (!is_dir($exportpath)) {
mkdir($exportpath);
}
if (file_put_contents($exportpath."/$filename", $output) === false) {
$output = "failed to save file";
}
} else {
$output = "no new orders";
}
header('Content-type: text/plain');
echo $output;
die();
}
}
14 changes: 7 additions & 7 deletions code/DispatchITOrderDecorator.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

class DispatchITOrderDecorator extends DataExtension{
private static $db = array(
'SentToDispatchIT' => 'Boolean'
);
}
class DispatchITOrderDecorator extends DataExtension
{
private static $db = array(
'SentToDispatchIT' => 'Boolean'
);
}