Created
February 28, 2018 13:38
-
-
Save renepenner/ad73ddf34bf3f88adc7f90225efe95ac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Wambo_DatevExport_Model_Export_DatevCSV extends Wambo_DatevExport_Model_Export_Abstract | |
{ | |
const FOLDERNAME = 'datev'; | |
const FILENAME_POSTFIX = '.csv'; | |
const KONTO_DE = '8400'; | |
const KONTO_EU_PRIVATE = '8331'; | |
const KONTO_EU_COMPANY = '8336'; | |
const KONTO_WORLD = '8338'; | |
/** | |
* @var Wambo_DatevExport_Model_Options | |
*/ | |
private $options; | |
/** | |
* @var Wambo_DatevExport_Model_ExchangeRate_ExchangeRateInterface | |
*/ | |
private $exchangeRates; | |
/** | |
* @var Wambo_DatevExport_Model_Export_CSV_Buchungsstapel | |
*/ | |
private $buchungsstapel; | |
/** | |
* @var Wambo_DatevExport_Model_Export_CSV_Debitoren | |
*/ | |
private $debitoren; | |
/** @var array */ | |
private $eu_countries; | |
public function __construct(Wambo_DatevExport_Model_Options $options) | |
{ | |
$this->options = $options; | |
$this->exchangeRates = Mage::getModel('datev_export/ExchangeRate_UmrechnungskurseOrgExchangeRate', $options); | |
$this->eu_countries = explode(',',Mage::getStoreConfig('general/country/eu_countries')); | |
} | |
public function export($year, $month, $force) | |
{ | |
$fistDayInMonth = strtotime('01-'.$month.'-'.$year); | |
$lastDayInMonth = strtotime('last day of this month', $fistDayInMonth); | |
$month_start = date("Ymt", $fistDayInMonth); | |
$month_end = date("Ymt", $lastDayInMonth); | |
$buchungsstapelFilename = 'EXTF_Buchungsstapel_' . $month_start .'_bis_' . $month_end . '.csv'; | |
$debitorenFilename = 'EXTF_GP_Stamm_' . $month_start . '_bis_' . $month_end .'.csv'; | |
$basePath = Mage::getBaseDir('export') . DS . self::BASE_FOLDERNAME . DS . self::FOLDERNAME; | |
$monthFolder = date("Y_m", $fistDayInMonth ); | |
$storeCodeFolder = "default"; | |
$targetPath = $basePath . DS . $monthFolder . DS . $storeCodeFolder; | |
if(!is_dir($targetPath)){ | |
mkdir($targetPath, 0777, true); | |
} | |
if( | |
!file_exists($targetPath . DS . $buchungsstapelFilename) || | |
!file_exists($targetPath . DS . $debitorenFilename) || | |
){ | |
$this->buchungsstapel = new Wambo_DatevExport_Model_Export_CSV_Buchungsstapel( | |
$this->options->getMandantenNumber(), | |
$this->options->getCurrency(), | |
$month_start, | |
$month_end | |
); | |
$this->debitoren = new Wambo_DatevExport_Model_Export_CSV_Debitoren( | |
$this->options->getMandantenNumber(), | |
$this->options->getCurrency(), | |
$month_start | |
); | |
$this->addInvoicesToCSV($fistDayInMonth, $lastDayInMonth); | |
$this->addCreditmemosToCSV($fistDayInMonth, $lastDayInMonth); | |
$csv = new Varien_File_Csv(); | |
$csv->setDelimiter(';'); | |
$csv->setEnclosure('"'); | |
$csv->saveData($targetPath . DS . $buchungsstapelFilename, $this->buchungsstapel->getLines()); | |
$csv->saveData($targetPath . DS . $debitorenFilename, $this->debitoren->getLines()); | |
} | |
} | |
private function addInvoicesToCSV($from, $to) | |
{ | |
/** @var Mage_Sales_Model_Resource_Order_Invoice_Collection $collection */ | |
$collection = Mage::getModel('sales/order_invoice') | |
->getCollection() | |
->addFieldToFilter('created_at', | |
array( | |
'from' => date("Y-m-d 00:00:00", $from), | |
'to' => date("Y-m-d 23:59:59", $to), | |
'date' => true) | |
); | |
/** @var Mage_Sales_Model_Order_Invoice $invoice */ | |
foreach ($collection as $invoice) | |
{ | |
$billingAddress = $invoice->getBillingAddress(); | |
if($billingAddress->getData('customer_id')==Null){ | |
$customerId = 69999; | |
}else{ | |
$customerId = $billingAddress->getData('customer_id'); | |
} | |
$total = $invoice->getData('base_grand_total'); | |
$currency = $invoice->getData('order_currency_code'); | |
if($this->options->canRecalculateCurrency() && $currency != $this->options->getCurrency()){ | |
$total = $total / $this->exchangeRates->getExchangeRate($invoice->getCreatedAt(), $currency); | |
$total = round($total,2); | |
} | |
$konto = $this->getKonto($billingAddress); | |
$this->buchungsstapel->addLine(array( | |
'Umsatz (ohne Soll/Haben-Kz)' => number_format($total, 2,',',''), | |
'Soll/Haben-Kennzeichen' => 'S', | |
'Konto' => $customerId, | |
'Gegenkonto (ohne BU-Schlüssel)' => $konto, | |
'Belegdatum' => date('jm',strtotime($invoice->getCreatedAt())), | |
'Belegfeld 1' => $invoice->getData('transaction_id'), | |
'Belegfeld 2' => $invoice->getData('increment_id'), | |
'Buchungstext' => $billingAddress->getData('firstname') . ' ' . $billingAddress->getData('lastname') | |
)); | |
if($customerId == 10000){ | |
$this->debitoren->addLine(array( | |
'Konto' => $customerId, | |
'Name (Adressattyp Unternehmen)' => 'Sammelkunde', | |
'Adressatentyp' => 1 | |
)); | |
}else{ | |
$this->debitoren->addLine(array( | |
'Konto' => $customerId, | |
'Name (Adressattyp natürl. Person)' => $billingAddress->getData('lastname'), | |
'Vorname (Adressattyp natürl. Person)' => $billingAddress->getData('firstname'), | |
'Adressatentyp' => '1', | |
'EU-Land' => $billingAddress->getData('country_id') | |
)); | |
} | |
} | |
} | |
/** | |
* @param $billingAddress Mage_Sales_Model_Order_Address | |
* | |
*/ | |
private function getKonto($billingAddress) | |
{ | |
if($billingAddress->getData('country_id') == 'DE') { | |
return self::KONTO_DE; | |
} | |
$isCompany = $billingAddress->getCompany() != ''; | |
$isEUCountry = in_array($billingAddress->getData('country_id'), $this->eu_countries); | |
if($isEUCountry){ | |
if($isCompany){ | |
return self::KONTO_EU_COMPANY; | |
}else{ | |
return self::KONTO_EU_PRIVATE; | |
} | |
} | |
return self::KONTO_WORLD; | |
} | |
private function addCreditmemosToCSV($from, $to) | |
{ | |
/** @var Mage_Sales_Model_Resource_Order_Creditmemo_Collection $collection */ | |
$collection = Mage::getModel('sales/order_creditmemo') | |
->getCollection() | |
->addFieldToFilter('created_at', | |
array( | |
'from' => date("Y-m-d 00:00:00", $from), | |
'to' => date("Y-m-d 23:59:59", $to), | |
'date' => true) | |
); | |
/** @var Mage_Sales_Model_Order_Creditmemo $creditmemo */ | |
foreach ($collection as $creditmemo) { | |
$billingAddress = $creditmemo->getBillingAddress(); | |
if( $billingAddress->getData('country_id') == NULL){ //Amazon Workaround | |
$billingAddress = $creditmemo->getShippingAddress(); | |
} | |
$customer_id = 69999; // Immer SammelId verwenden | |
$total = $creditmemo->getData('base_grand_total'); | |
$currency = $creditmemo->getData('order_currency_code'); | |
if($currency != $this->options->getCurrency()){ | |
$total = $total / $this->exchangeRates->getExchangeRate($creditmemo->getCreatedAt(), $currency); | |
$total = round($total, 2); | |
} | |
$buchungs_nr = $this->getKonto($billingAddress); | |
$this->buchungsstapel->addLine(array( | |
'Umsatz (ohne Soll/Haben-Kz)' => number_format($total, 2,',',''), | |
'Soll/Haben-Kennzeichen' => 'H', | |
'Konto' => $customer_id, | |
'Gegenkonto (ohne BU-Schlüssel)' => $buchungs_nr, | |
'Belegdatum' => date('jm',strtotime( $creditmemo->getData('created_at') )), | |
'Belegfeld 1' => $creditmemo->getData('increment_id'), | |
'Belegfeld 2' => $creditmemo->getData('increment_id'), | |
'Buchungstext' => $billingAddress->getData('firstname') . ' ' . $billingAddress->getData('lastname') | |
)); | |
if($customer_id == 10000){ | |
$this->debitoren->addLine(array( | |
'Konto' => $customer_id, | |
'Name (Adressattyp Unternehmen)' => 'Sammelkunde', | |
'Adressatentyp' => 1 | |
)); | |
}else{ | |
$this->debitoren->addLine(array( | |
'Konto' => $customer_id, | |
'Name (Adressattyp natürl. Person)' => $billingAddress->getData('lastname'), | |
'Vorname (Adressattyp natürl. Person)' => $billingAddress->getData('firstname'), | |
'Adressatentyp' => '1', | |
'EU-Land' => $billingAddress->getData('country_id') | |
)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment