File: //www/exchange2/sql/archive/storage_migrate.php
#!/usr/local/bin/php
<?php
/**
* dev-rc - storage_migrate.php
*
* Initial version by: MisterX
* Initial version created on: 27.01.2016
*/
error_reporting(E_ALL);
ini_set('display_errors',true);
set_time_limit(0);
require_once "../exchange/Bin/init.php";
require_once ROOT_FOLDER."/classes/Storage.php";
class Migrate{
/*
* RSA + QA
* INVOICES + QA
* PURCHASE_ORDERS +
* ORIGINATOR_DOCUMENTS + NQA risks!!! uploadRiskTraderDocuments
* INVESTOR DOCUMENTS + QA
* TRANSACTION_DOCUMENTS + QA
* D/C documents + Risk
* NOA + QA??
* NOI + QA
*/
private $documentsPath;
private $samplesPath;
private $uploadsPath;
private $debug=false;
private $logFile=null;
private $flushCounter = 0;
public function __construct($documentsPath, $samplesPath, $uploadsPath){
$this->documentsPath = $documentsPath;
$this->samplesPath = $samplesPath;
$this->uploadsPath = $uploadsPath;
Storage_Repository::load();
$this->logFile = fopen(__DIR__.'/'.uniqid('SM',true).'.log','w+');
}
public function rsa(){
echo "Start ".__FUNCTION__."\n";
$query = new Bin_Query();
$query->executeQuery('SELECT * FROM invoice_documents WHERE invoice_support_document_path!=""');
$storage = Storage_Repository::get(Storage_Repository::RSA);
foreach($query->records as $record){
$fileName = '/'.ltrim($record['invoice_support_document_path'],'/');
$oldFilePath = $this->documentsPath.$fileName;
if(!$this->copy($oldFilePath,$fileName,$storage)){
echo 'SKIPPED '.strtoupper(__FUNCTION__).' '.$oldFilePath."\n";
}
}
}
public function invoice(){
echo "Start ".__FUNCTION__."\n";
$query = new Bin_Query();
$query->executeQuery("SELECT dd.type, invoice_documents.invoice_document_path FROM invoice_documents
JOIN invoice_master ON invoice_documents.invoice_id = invoice_master.invoice_id
JOIN debtors_detail dd ON dd.debtor_id = invoice_master.debtor_id
WHERE invoice_document_path!=''");
$dStorage = Storage_Repository::get(Storage_Repository::D_ETR);
$cStorage = Storage_Repository::get(Storage_Repository::C_ETR);
foreach($query->records as $record){
$fileName = '/'.ltrim($record['invoice_document_path'],'/');
$oldFilePath = $this->documentsPath.$fileName;
if(strtolower($record['type'])=='debtor'){
$storage = $dStorage;
}else{
$storage = $cStorage;
}
if(!$this->copy($oldFilePath,$fileName,$storage)){
echo 'SKIPPED '.strtoupper(__FUNCTION__).' '.$oldFilePath."\n";
}
}
}
public function purchase_order(){
echo "Start ".__FUNCTION__."\n";
$query = new Bin_Query();
$query->executeQuery("SELECT dd.type, invoice_documents.purchase_order_document_path FROM invoice_documents
JOIN invoice_master ON invoice_documents.invoice_id = invoice_master.invoice_id
JOIN debtors_detail dd ON dd.debtor_id = invoice_master.debtor_id
WHERE purchase_order_document_path!=''");
$dStorage = Storage_Repository::get(Storage_Repository::D_PO);
$cStorage = Storage_Repository::get(Storage_Repository::C_PO);
foreach($query->records as $record){
$fileName = '/'.ltrim($record['purchase_order_document_path'],'/');
$oldFilePath = $this->documentsPath.$fileName;
if(strtolower($record['type'])=='debtor'){
$storage = $dStorage;
}else{
$storage = $cStorage;
}
if(!$this->copy($oldFilePath,$fileName,$storage)){
echo 'SKIPPED '.strtoupper(__FUNCTION__).' '.$oldFilePath."\n";
}
}
}
public function org_documents(){
echo "Start ".__FUNCTION__."\n";
$sql = 'SELECT debtors_report_path, debtors_report_csv, creditors_report_path, creditors_report_csv, bank_report_path, bank_report_csv, loss_report_path, loss_report_csv, balance_report_path, balance_report_csv, vat_report_path, vat_report_csv, paye_report_path, paye_report_csv, photoid_report_path, photoid_report_csv, utility_report_path, utility_report_csv FROM organisation_documents';
$query = new Bin_Query();
$query->executeQuery($sql);
$storage = Storage_Repository::get(Storage_Repository::ORG_DOCS);
foreach($query->records as $record){
foreach($record as $file){
if(empty($file)){
continue;
}
$fileName = '/'.ltrim($file,'/');
$oldFilePath = $this->documentsPath.$fileName;
if(!$this->copy($oldFilePath,$fileName,$storage)){
echo 'SKIPPED '.strtoupper(__FUNCTION__).' '.$oldFilePath."\n";
}
}
}
//Second part
$sql = "SELECT attached_accounts as 'fileName' FROM trader_balance_details WHERE attached_accounts!=''";
$query = new Bin_Query();
$query->executeQuery($sql);
foreach($query->records as $record){
$fileName = '/'.ltrim($record['fileName'],'/');
$oldFilePath = $this->documentsPath.$fileName;
if(!$this->copy($oldFilePath,$fileName,$storage)){
echo 'SKIPPED '.strtoupper(__FUNCTION__).' '.$oldFilePath."\n";
}
}
//Third part, risk documents
$sql = "SELECT rcd.provider3_document_path, rcd.provider4_document_path FROM risk_credit_document rcd
JOIN risk_credit rc ON rc.risk_credit_id = rcd.risk_credit_id
WHERE (rcd.provider3_document_path!='' OR rcd.provider4_document_path!='') AND rc.organisation_id!=0";
$query = new Bin_Query();
$query->executeQuery($sql);
foreach($query->records as $record){
foreach($record as $file){
if(empty($file)){
continue;
}
$fileName = '/'.ltrim($file,'/');
$oldFilePath = $this->documentsPath.$fileName;
if(!$this->copy($oldFilePath,$fileName,$storage)){
echo 'SKIPPED '.strtoupper(__FUNCTION__).' '.$oldFilePath."\n";
}
}
}
}
public function inv_documents(){
echo "Start ".__FUNCTION__."\n";
//Second part
$sql = "SELECT tr.pasport, tr.bill, tr.signed_rpa FROM temp_retailer tr";
$query = new Bin_Query();
$query->executeQuery($sql);
$storage = Storage_Repository::get(Storage_Repository::INV_DOCS);
foreach($query->records as $record){
foreach($record as $file) {
if(empty($file)) continue;
$fileName = '/'.ltrim($file, '/');
$oldFilePath = $this->documentsPath.$fileName;
if (!$this->copy($oldFilePath, $fileName, $storage)) {
echo 'SKIPPED '.strtoupper(__FUNCTION__).' '.$oldFilePath."\n";
}
}
}
}
public function trans_documents(){
echo "Start ".__FUNCTION__."\n";
//Second part
$sql = "SELECT mt.transaction_file AS 'fileName' FROM manual_transactions mt WHERE mt.transaction_file!=''";
$query = new Bin_Query();
$query->executeQuery($sql);
$storage = Storage_Repository::get(Storage_Repository::TRANS_DOCS);
foreach($query->records as $record){
$fileName = '/'.ltrim($record['fileName'],'/');
$oldFilePath = $this->uploadsPath.$fileName;
if(!$this->copy($oldFilePath,$fileName,$storage)){
echo 'SKIPPED '.strtoupper(__FUNCTION__).' '.$oldFilePath."\n";
}
}
}
public function noa(){
echo "Start ".__FUNCTION__."\n";
$sql = "SELECT noa_link as 'fileName' from invoice_master WHERE noa_link!=''";
$query = new Bin_Query();
$query->executeQuery($sql);
$storage = Storage_Repository::get(Storage_Repository::NOA);
foreach($query->records as $record){
$fileName = '/'.ltrim($record['fileName'],'/');
$oldFilePath = $this->samplesPath.$fileName;
if(!$this->copy($oldFilePath,$fileName,$storage)){
echo 'SKIPPED '.strtoupper(__FUNCTION__).' '.$oldFilePath."\n";
}
}
}
public function noi(){
echo "Start ".__FUNCTION__."\n";
$sql = "SELECT etr_link as 'fileName' from invoice_master WHERE etr_link!=''";
$query = new Bin_Query();
$query->executeQuery($sql);
$storage = Storage_Repository::get(Storage_Repository::NOI);
foreach($query->records as $record){
$fileName = '/'.ltrim($record['fileName'],'/');
$oldFilePath = $this->samplesPath.$fileName;
if(!$this->copy($oldFilePath,$fileName,$storage)){
echo 'SKIPPED '.strtoupper(__FUNCTION__).' '.$oldFilePath."\n";
}
}
}
public function dc_documents(){
echo "Start ".__FUNCTION__."\n";
//Debitor documents
$dStorage = Storage_Repository::get(Storage_Repository::D_DOCS);
$cStorage = Storage_Repository::get(Storage_Repository::C_DOCS);
//Debitor documents
$sql = "SELECT rcd.provider3_document_path, rcd.provider4_document_path FROM risk_credit_document rcd
JOIN risk_credit rc ON rc.risk_credit_id = rcd.risk_credit_id
WHERE (rcd.provider3_document_path!='' OR rcd.provider4_document_path!='') AND rc.organisation_id!=0";
$query = new Bin_Query();
$query->executeQuery($sql);
foreach($query->records as $record){
foreach($record as $file){
if(empty($file)){
continue;
}
$fileName = '/'.ltrim($file,'/');
$oldFilePath = $this->documentsPath.$fileName;
if(!$this->copy($oldFilePath,$fileName,$dStorage)){
echo 'SKIPPED '.strtoupper(__FUNCTION__).' '.$oldFilePath."\n";
}
}
}
$sql = "SELECT noid_link,noad_link FROM debtor_relation
JOIN debtors_detail dd ON debtor_relation.debtor_id = dd.debtor_id
WHERE (noid_link!='' OR noad_link!='') AND dd.type='debtor'";
$query = new Bin_Query();
$query->executeQuery($sql);
foreach($query->records as $record){
foreach($record as $file){
if(empty($file)){
continue;
}
$fileName = '/'.ltrim($file,'/');
$oldFilePath = $this->documentsPath.$fileName;
if(!$this->copy($oldFilePath,$fileName,$dStorage)){
echo 'SKIPPED '.strtoupper(__FUNCTION__).' '.$oldFilePath."\n";
}
}
}
//Creditor documents
$sql = "SELECT noid_link,noad_link FROM debtor_relation
JOIN debtors_detail dd ON debtor_relation.debtor_id = dd.debtor_id
WHERE (noid_link!='' OR noad_link!='') AND dd.type='creditor'";
$query = new Bin_Query();
$query->executeQuery($sql);
foreach($query->records as $record){
foreach($record as $file){
if(empty($file)){
continue;
}
$fileName = '/'.ltrim($file,'/');
$oldFilePath = $this->documentsPath.$fileName;
if(!$this->copy($oldFilePath,$fileName,$cStorage)){
echo 'SKIPPED '.strtoupper(__FUNCTION__).' '.$oldFilePath."\n";
}
}
}
}
public function all(){
$this->rsa();
$this->noa();
$this->noi();
$this->invoice();
$this->purchase_order();
$this->org_documents();
$this->inv_documents();
$this->trans_documents();
$this->dc_documents();
}
public function def(){
echo 'Usage '.__FILE__." <rsa|invoice|dc_documents|purchase_order|org_documents|inv_documents|trans_documents|noa|noi|all>\n";
}
protected function copy($filePath,$fileName,Storage $storage){
if(!file_exists($filePath) or !is_file($filePath)){
return false;
}
$this->flushCounter++;
if($this->flushCounter>=100){
$this->flushCounter = 0;
$this->flushLog();
}
fwrite($this->logFile,$filePath.PHP_EOL);
$storage->set($fileName, $filePath, true);
return true;
}
public function finish(){
$this->flushLog();
fclose($this->logFile);
}
public function flushLog(){
fflush($this->logFile);
}
}
//$migrate = new Migrate(realpath(ROOT_FOLDER.'../'),realpath(ROOT_FOLDER),realpath(ROOT_FOLDER));
$testRoot = '/mnt/nfs/san/exchange/storage-migration';
$migrate = new Migrate($testRoot,$testRoot,$testRoot);
$method = isset($argv[1])?$argv[1]:'def';
if(method_exists($migrate,$method)){
$migrate->{$method}();
$migrate->finish();
}else{
$migrate->def();
}