<?php
/**
* This class is ran from a page in their own site
*/
class AFTownsville
{
protected static $sSaleCountryId = 'AUS';
protected static $sSaleCurrencyId = 'AUD';
protected static $arrFiles = [
'memberships' => [
'media/af-import/Memberships.csv',
],
];
public static function import()
{
set_time_limit(0);
\Framework\System\Limits::increaseMemoryLimit(4096);
// ob_end_clean();
$executionStartTime = microtime(true);
echo "<hr/><hr/> Start Import<hr/><hr/>";
// static::truncate();
static::processMemberships();
echo "<br /><hr/>Import Compeleted in " . (microtime(true) - $executionStartTime) / 60 . " minutes(s)<hr/>";
}
public static function processMemberships()
{
foreach (static::$arrFiles['memberships'] as $iKey => $arrFile)
static::processMembershipFile($arrFile);
}
protected static function processMembershipFile($arrFile)
{
$arrMembers = static::getDataFromCSV(file_get_contents($arrFile), ',', true);
$iCounter = 1;
foreach ($arrMembers as $arrMember)
{
$strFirstName = $arrMember['First Name'];
$strLastName = $arrMember['Last Name'];
$strEmail = $arrMember['Email'];
// if (!$strEmail)
// continue;
// Find the Contact
$arrFilter = [];
$arrFilter[] = ['contact_first_name', '=', $strFirstName];
$arrFilter[] = 'AND';
$arrFilter[] = ['contact_last_name', '=', $strLastName];
$arrFilter[] = 'AND';
$arrFilter[] = ['contact_email', '=', $strEmail];
if (!$arrContact = \Components\Customers\Contacts::getFirst(['filter' => $arrFilter]))
{
// Create the contact and return the id
$arrContact = [];
// $arrContact['contact_last_online'] = dbDateTime(time());
$arrContact['contact_first_name'] = $strFirstName;
$arrContact['contact_last_name'] = $strLastName;
$arrContact['contact_email'] = $strEmail;
// $arrContact['contact_company'] = $arrMember['Company'];
$arrContact['contact_phone_home'] = $arrMember['Phone'];
// $arrContact['contact_address'] = $arrMember['Address'];
$arrContact['contact_suburb_city'] = $arrMember['Address - Suburb'];
// $arrContact['contact_postcode'] = $arrMember['Postcode'] ;
$arrContact['contact_state'] = $arrMember['Address- State'];
$arrContact['country_id'] = static::$sSaleCountryId;
// $arrContact['contact_how_did_you_hear_about_us'] = $arrMember['How did you hear about us'];
$arrContact['contact_native_french_speaker'] = $arrMember['Native French'] === 'Y' ? 1 : null;
$iContactId = \Components\Customers\Contacts::save($arrContact);
$arrContact = \Components\Customers\Contacts::get($iContactId);
}
if (isset($arrMember['Relationship']) && $arrMember['Relationship'] && ($arrMember['Relationship'] != $arrContact['contact_email']))
{
static::createRelationship($arrMember['Relationship'], $arrContact);
continue;
}
// Set the membership product cause sometimes in the excel file there is a typo in their membership type
$sMembershipType = $arrMember['Membership type'];
if (str_starts_with($sMembershipType, 'Fam'))
$strProductTitle = 'Family Membership';
elseif (str_starts_with($sMembershipType, 'Sing'))
$strProductTitle = 'Single Membership';
$arrProduct = \Components\Commerce\Products::getByProductTitle($strProductTitle);
// $arrProduct = \Components\Commerce\Products::get($iProductId);
// Create membership for contact
$arrSale = [];
$arrSale['sale_datetime'] = dbDateTime(time());
$arrSale['payment_method_id'] = 1;
$arrSale['sale_country_id'] = static::$sSaleCountryId;
$arrSale['sale_currency_id'] = static::$sSaleCurrencyId;
$arrSale['sale_total_tax'] = 0;
$arrSale['sale_tax_label'] = 'Tax';
$arrSale['sale_total'] = 0;
$arrSale['sale_receipt_status'] = 'paid';
$arrSale['sale_receipt_balance'] = 0;
// Set invoicee
$arrSale['contact_id'] = $arrContact['contact_id'];
$arrSale['contact_first_name'] = $arrContact['contact_first_name'] ?: '';
$arrSale['contact_last_name'] = $arrContact['contact_last_name'];
$arrSale['contact_email'] = $arrContact['contact_email'];
$arrSale['contact_phone_home'] = $arrContact['contact_phone_home'];
$arrSale['contact_address'] = $arrContact['contact_address'];
$arrSale['contact_suburb_city'] = $arrContact['contact_suburb_city'];
$arrSale['contact_postcode'] = $arrContact['contact_postcode'];
$arrSale['contact_state'] = $arrContact['contact_state'];
$arrSale['contact_country_id'] = $arrContact['country_id'];
$iSaleId = \Components\Commerce\Sales::save($arrSale);
$arrSaleItem = [];
$arrSaleItem['_dont_validate_totals'] = true; //import test run
$arrSaleItem['sale_item_title'] = $arrProduct['product_title'];
$arrSaleItem['sale_item_quantity'] = 1;
$arrSaleItem['sale_item_price'] = $arrProduct['product_price_base'];
$arrSaleItem['sale_item_total'] = $arrProduct['product_price_base'];
$arrSaleItem['sale_item_tax_percent'] = 0;
$arrSaleItem['sale_item_total_tax'] = 0;
$arrSaleItem['sale_id'] = $iSaleId;
$arrSaleItem['product_id'] = $arrProduct['product_id'];
$iSaleItemId = \Components\Commerce\Sales\Items::save($arrSaleItem);
// Create receipt and allocate
$arrReceipt = [];
$arrReceipt['receipt_amount'] = $arrProduct['product_price_base'];
$arrReceipt['receipt_country_id'] = static::$sSaleCountryId;
$arrReceipt['receipt_currency_id'] = static::$sSaleCurrencyId;
$iReceiptId = \Components\Commerce\Sales\Receipts::save($arrReceipt);
$arrReceiptAllocation = [];
$arrReceiptAllocation['sale_id'] = $iSaleId;
$arrReceiptAllocation['receipt_id'] = $iReceiptId;
$arrReceiptAllocation['receipt_allocation_amount'] = $arrProduct['product_price_base'];
\Components\Commerce\Sales\Receipts\Allocations::save($arrReceiptAllocation);
// Create Membership
$arrNewMembership = [];
$arrNewMembership['membership_type'] = $arrProduct['product_title'];
$arrNewMembership['membership_start_date'] = date('Y-m-d', strtotime(str_replace('/', '-', $arrMember['Start Date'])));
$arrNewMembership['membership_end_date'] = date('Y-m-d', strtotime(str_replace('/', '-', $arrMember['Expiry Date'])));
$arrNewMembership['sale_item_id'] = $iSaleItemId;
\Components\AF\Memberships::save($arrNewMembership);
static::setLoopDump($iCounter, 1, $arrMember, 'Importing memberships');
$iCounter++;
}
}
public static function createRelationship($sMainMemberEmail, $arrContact)
{
$arrFilter = [];
$arrFilter[] = ['contact_email', '=', $sMainMemberEmail];
$arrMainContact = \Components\Customers\Contacts::getFirst(['filter' => $arrFilter]);
$arrRelationship = [];
$arrRelationship['contact_id_1'] = $arrMainContact['contact_id'];
$arrRelationship['contact_id_2'] = $arrContact['contact_id'];
$arrRelationship['relationship_type_id'] = 'I2I_Family';
\Components\Customers\Contacts\Relationships::save($arrRelationship);
}
//---------------------------------------------------------
public static function getDataFromCSV($strFilePath, $strSeparator = ',', $bDisableHeading = false)
{
$pCSV = new \Framework\DataFormats\CSV();
$pCSV->importFromString($strFilePath);
$pCSV->configSetSeparator($strSeparator);
$pCSV->configSetDisableHeadingPrefix($bDisableHeading);
$arrEntities = $pCSV->exportToArray();
// Clean the array keys
return array_map (function ($arrEntity) {
foreach (array_keys ($arrEntity) as $strKey)
{
$newKey = static::removeInviChars($strKey);
// Not the same because it was cleaned
if ($newKey != $strKey)
{
// Add the clean key
$arrEntity[static::removeInviChars($strKey)] = $arrEntity[$strKey];
// Remove the unclean key
unset($arrEntity[$strKey]);
}
}
return $arrEntity;
}, $arrEntities);
}
public static function removeInviChars($str)
{
return preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $str);
}
public static function setLoopDump(&$iCounter, $iLimit, $sData, $strTitle = '')
{
if ($iCounter == $iLimit)
{
varDump($strTitle);
varDump($sData);
echo '<hr/>';
$iCounter = 0;
}
}
}
function import()
{
die;
return AFTownsville::import();
}
?>
<?php
/**
* This class is ran from a page in their own site
*/
class AFTownsville
{
protected static $sSaleCountryId = 'AUS';
protected static $sSaleCurrencyId = 'AUD';
protected static $arrFiles = [
'memberships' => [
'media/af-import/Memberships.csv',
],
];
public static function import()
{
set_time_limit(0);
\Framework\System\Limits::increaseMemoryLimit(4096);
// ob_end_clean();
$executionStartTime = microtime(true);
echo "<hr/><hr/> Start Import<hr/><hr/>";
// static::truncate();
static::processMemberships();
echo "<br /><hr/>Import Compeleted in " . (microtime(true) - $executionStartTime) / 60 . " minutes(s)<hr/>";
}
public static function processMemberships()
{
foreach (static::$arrFiles['memberships'] as $iKey => $arrFile)
static::processMembershipFile($arrFile);
}
protected static function processMembershipFile($arrFile)
{
$arrMembers = static::getDataFromCSV(file_get_contents($arrFile), ',', true);
$iCounter = 1;
foreach ($arrMembers as $arrMember)
{
$strFirstName = $arrMember['First Name'];
$strLastName = $arrMember['Last Name'];
$strEmail = $arrMember['Email'];
// if (!$strEmail)
// continue;
// Find the Contact
$arrFilter = [];
$arrFilter[] = ['contact_first_name', '=', $strFirstName];
$arrFilter[] = 'AND';
$arrFilter[] = ['contact_last_name', '=', $strLastName];
$arrFilter[] = 'AND';
$arrFilter[] = ['contact_email', '=', $strEmail];
if (!$arrContact = \Components\Customers\Contacts::getFirst(['filter' => $arrFilter]))
{
// Create the contact and return the id
$arrContact = [];
// $arrContact['contact_last_online'] = dbDateTime(time());
$arrContact['contact_first_name'] = $strFirstName;
$arrContact['contact_last_name'] = $strLastName;
$arrContact['contact_email'] = $strEmail;
// $arrContact['contact_company'] = $arrMember['Company'];
$arrContact['contact_phone_home'] = $arrMember['Phone'];
// $arrContact['contact_address'] = $arrMember['Address'];
$arrContact['contact_suburb_city'] = $arrMember['Address - Suburb'];
// $arrContact['contact_postcode'] = $arrMember['Postcode'] ;
$arrContact['contact_state'] = $arrMember['Address- State'];
$arrContact['country_id'] = static::$sSaleCountryId;
// $arrContact['contact_how_did_you_hear_about_us'] = $arrMember['How did you hear about us'];
$arrContact['contact_native_french_speaker'] = $arrMember['Native French'] === 'Y' ? 1 : null;
$iContactId = \Components\Customers\Contacts::save($arrContact);
$arrContact = \Components\Customers\Contacts::get($iContactId);
}
if (isset($arrMember['Relationship']) && $arrMember['Relationship'] && ($arrMember['Relationship'] != $arrContact['contact_email']))
{
static::createRelationship($arrMember['Relationship'], $arrContact);
continue;
}
// Set the membership product cause sometimes in the excel file there is a typo in their membership type
$sMembershipType = $arrMember['Membership type'];
if (str_starts_with($sMembershipType, 'Fam'))
$strProductTitle = 'Family Membership';
elseif (str_starts_with($sMembershipType, 'Sing'))
$strProductTitle = 'Single Membership';
$arrProduct = \Components\Commerce\Products::getByProductTitle($strProductTitle);
// $arrProduct = \Components\Commerce\Products::get($iProductId);
// Create membership for contact
$arrSale = [];
$arrSale['sale_datetime'] = dbDateTime(time());
$arrSale['payment_method_id'] = 1;
$arrSale['sale_country_id'] = static::$sSaleCountryId;
$arrSale['sale_currency_id'] = static::$sSaleCurrencyId;
$arrSale['sale_total_tax'] = 0;
$arrSale['sale_tax_label'] = 'Tax';
$arrSale['sale_total'] = 0;
$arrSale['sale_receipt_status'] = 'paid';
$arrSale['sale_receipt_balance'] = 0;
// Set invoicee
$arrSale['contact_id'] = $arrContact['contact_id'];
$arrSale['contact_first_name'] = $arrContact['contact_first_name'] ?: '';
$arrSale['contact_last_name'] = $arrContact['contact_last_name'];
$arrSale['contact_email'] = $arrContact['contact_email'];
$arrSale['contact_phone_home'] = $arrContact['contact_phone_home'];
$arrSale['contact_address'] = $arrContact['contact_address'];
$arrSale['contact_suburb_city'] = $arrContact['contact_suburb_city'];
$arrSale['contact_postcode'] = $arrContact['contact_postcode'];
$arrSale['contact_state'] = $arrContact['contact_state'];
$arrSale['contact_country_id'] = $arrContact['country_id'];
$iSaleId = \Components\Commerce\Sales::save($arrSale);
$arrSaleItem = [];
$arrSaleItem['_dont_validate_totals'] = true; //import test run
$arrSaleItem['sale_item_title'] = $arrProduct['product_title'];
$arrSaleItem['sale_item_quantity'] = 1;
$arrSaleItem['sale_item_price'] = $arrProduct['product_price_base'];
$arrSaleItem['sale_item_total'] = $arrProduct['product_price_base'];
$arrSaleItem['sale_item_tax_percent'] = 0;
$arrSaleItem['sale_item_total_tax'] = 0;
$arrSaleItem['sale_id'] = $iSaleId;
$arrSaleItem['product_id'] = $arrProduct['product_id'];
$iSaleItemId = \Components\Commerce\Sales\Items::save($arrSaleItem);
// Create receipt and allocate
$arrReceipt = [];
$arrReceipt['receipt_amount'] = $arrProduct['product_price_base'];
$arrReceipt['receipt_country_id'] = static::$sSaleCountryId;
$arrReceipt['receipt_currency_id'] = static::$sSaleCurrencyId;
$iReceiptId = \Components\Commerce\Sales\Receipts::save($arrReceipt);
$arrReceiptAllocation = [];
$arrReceiptAllocation['sale_id'] = $iSaleId;
$arrReceiptAllocation['receipt_id'] = $iReceiptId;
$arrReceiptAllocation['receipt_allocation_amount'] = $arrProduct['product_price_base'];
\Components\Commerce\Sales\Receipts\Allocations::save($arrReceiptAllocation);
// Create Membership
$arrNewMembership = [];
$arrNewMembership['membership_type'] = $arrProduct['product_title'];
$arrNewMembership['membership_start_date'] = date('Y-m-d', strtotime(str_replace('/', '-', $arrMember['Start Date'])));
$arrNewMembership['membership_end_date'] = date('Y-m-d', strtotime(str_replace('/', '-', $arrMember['Expiry Date'])));
$arrNewMembership['sale_item_id'] = $iSaleItemId;
\Components\AF\Memberships::save($arrNewMembership);
static::setLoopDump($iCounter, 1, $arrMember, 'Importing memberships');
$iCounter++;
}
}
public static function createRelationship($sMainMemberEmail, $arrContact)
{
$arrFilter = [];
$arrFilter[] = ['contact_email', '=', $sMainMemberEmail];
$arrMainContact = \Components\Customers\Contacts::getFirst(['filter' => $arrFilter]);
$arrRelationship = [];
$arrRelationship['contact_id_1'] = $arrMainContact['contact_id'];
$arrRelationship['contact_id_2'] = $arrContact['contact_id'];
$arrRelationship['relationship_type_id'] = 'I2I_Family';
\Components\Customers\Contacts\Relationships::save($arrRelationship);
}
//---------------------------------------------------------
public static function getDataFromCSV($strFilePath, $strSeparator = ',', $bDisableHeading = false)
{
$pCSV = new \Framework\DataFormats\CSV();
$pCSV->importFromString($strFilePath);
$pCSV->configSetSeparator($strSeparator);
$pCSV->configSetDisableHeadingPrefix($bDisableHeading);
$arrEntities = $pCSV->exportToArray();
// Clean the array keys
return array_map (function ($arrEntity) {
foreach (array_keys ($arrEntity) as $strKey)
{
$newKey = static::removeInviChars($strKey);
// Not the same because it was cleaned
if ($newKey != $strKey)
{
// Add the clean key
$arrEntity[static::removeInviChars($strKey)] = $arrEntity[$strKey];
// Remove the unclean key
unset($arrEntity[$strKey]);
}
}
return $arrEntity;
}, $arrEntities);
}
public static function removeInviChars($str)
{
return preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $str);
}
public static function setLoopDump(&$iCounter, $iLimit, $sData, $strTitle = '')
{
if ($iCounter == $iLimit)
{
varDump($strTitle);
varDump($sData);
echo '<hr/>';
$iCounter = 0;
}
}
}
function import()
{
die;
return AFTownsville::import();
}
?>