|
Apache/2.4.41 (Ubuntu) Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) server ip : 62.171.164.128 | your ip : 127.0.0.1 safemode OFF > / home / dev2.destoffenstraat.com / app / code / Mrgig / MV / Model / CustomerInvoice / |
Filename | /home/dev2.destoffenstraat.com/app/code/Mrgig/MV/Model/CustomerInvoice/Line.php |
Size | 3.69 kb |
Permission | rwxrwxrwx |
Owner | root : root |
Create time | 17-Aug-2025 10:26 |
Last modified | 13-Apr-2022 07:08 |
Last accessed | 23-Aug-2025 02:07 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
<?php
namespace Mrgig\MV\Model\CustomerInvoice;
use Mrgig\MV\Helper\Data as DataHelper;
use Magento\Framework\App\ResourceConnection;
class Line
{
private const SALES_ORDER_TAX_TABLE = 'sales_order_tax';
protected $container = [];
public function __construct(
DataHelper $dataHelper,
ResourceConnection $resourceConnection
) {
$this->dataHelper = $dataHelper;
$this->resourceConnection = $resourceConnection;
}
public function invoiceLine($invoice, $countryId, $taxId)
{
$order = $invoice->getOrder();
$vatId = $this->getVatId($order);
if(!empty($vatId)) {
$accountCode = '820100';
} else {
$accountCode = $this->dataHelper->getAccountCodeArrayMapping($countryId);
}
$total = $this->getTotal($invoice);
$taxAmount = $this->getTaxAmount($invoice);
$this->container['accountId'] = $accountCode;
$this->container['creditAmount'] = $total;
$this->container['creditAmountCur'] = $total;
$this->container['transactionDate'] = $this->getInvoiceDate($invoice->getCreatedAt());
$this->container['vatAmount'] = $taxAmount;
$this->container['vatAmountCur'] = $taxAmount;
$this->container['vatCodeId'] = $taxId;
$this->container['vatType'] = 0;
return [$this->container];
}
public function getVatId($order)
{
$shippingAddress = $order->getShippingAddress();
if(empty($shippingAddress)) {
$shippingAddress = $order->getBillingAddress();
}
$vatId = null;
if($shippingAddress->getCountryId() != 'NL') {
$vatId = $shippingAddress->getVatId();
}
return $vatId;
}
public function getInvoiceDate($date)
{
return date('d-m-Y', strtotime($date));
}
public function getTotal($invoice)
{
$discountAmount = $invoice->getBaseDiscountAmount();
if (strpos($invoice->getBaseDiscountAmount(), '-') !== false) {
$discountAmount = str_replace('-', '', $invoice->getBaseDiscountAmount());
}
$total = $invoice->getBaseSubtotal() + $invoice->getShippingAmount();
if($discountAmount > 0) {
$taxPercent = $this->getTaxPercent($invoice->getOrder()->getId());
if(!empty($taxPercent) && $taxPercent > 0) {
$getTaxAmount = $invoice->getBaseGrandTotal() * $taxPercent / (100 + $taxPercent);
$total = $invoice->getBaseGrandTotal() - $getTaxAmount;
$total = number_format((float)$total, 2, '.', '');
}
}
return $total;
}
public function getTaxAmount($invoice)
{
$discountAmount = $invoice->getBaseDiscountAmount();
if (strpos($invoice->getBaseDiscountAmount(), '-') !== false) {
$discountAmount = str_replace('-', '', $invoice->getBaseDiscountAmount());
}
$taxAmount = $invoice->getTaxAmount();
if($discountAmount > 0) {
$taxPercent = $this->getTaxPercent($invoice->getOrder()->getId());
if(!empty($taxPercent) && $taxPercent > 0) {
$taxAmount = $invoice->getBaseGrandTotal() * $taxPercent / (100 + $taxPercent);
$taxAmount = number_format((float)$taxAmount, 2, '.', '');
}
}
return $taxAmount;
}
public function getTaxPercent(int $orderId): float
{
$table = $this->resourceConnection->getTableName(self::SALES_ORDER_TAX_TABLE);
$connection = $this->resourceConnection->getConnection();
$select = $connection->select();
$select->from(['order_tax' => $table], ['percent' => 'order_tax.percent']);
$select->where('order_tax.order_id = ?', $orderId);
$percent = (float) $connection->fetchOne($select);
return $percent;
}
}
namespace Mrgig\MV\Model\CustomerInvoice;
use Mrgig\MV\Helper\Data as DataHelper;
use Magento\Framework\App\ResourceConnection;
class Line
{
private const SALES_ORDER_TAX_TABLE = 'sales_order_tax';
protected $container = [];
public function __construct(
DataHelper $dataHelper,
ResourceConnection $resourceConnection
) {
$this->dataHelper = $dataHelper;
$this->resourceConnection = $resourceConnection;
}
public function invoiceLine($invoice, $countryId, $taxId)
{
$order = $invoice->getOrder();
$vatId = $this->getVatId($order);
if(!empty($vatId)) {
$accountCode = '820100';
} else {
$accountCode = $this->dataHelper->getAccountCodeArrayMapping($countryId);
}
$total = $this->getTotal($invoice);
$taxAmount = $this->getTaxAmount($invoice);
$this->container['accountId'] = $accountCode;
$this->container['creditAmount'] = $total;
$this->container['creditAmountCur'] = $total;
$this->container['transactionDate'] = $this->getInvoiceDate($invoice->getCreatedAt());
$this->container['vatAmount'] = $taxAmount;
$this->container['vatAmountCur'] = $taxAmount;
$this->container['vatCodeId'] = $taxId;
$this->container['vatType'] = 0;
return [$this->container];
}
public function getVatId($order)
{
$shippingAddress = $order->getShippingAddress();
if(empty($shippingAddress)) {
$shippingAddress = $order->getBillingAddress();
}
$vatId = null;
if($shippingAddress->getCountryId() != 'NL') {
$vatId = $shippingAddress->getVatId();
}
return $vatId;
}
public function getInvoiceDate($date)
{
return date('d-m-Y', strtotime($date));
}
public function getTotal($invoice)
{
$discountAmount = $invoice->getBaseDiscountAmount();
if (strpos($invoice->getBaseDiscountAmount(), '-') !== false) {
$discountAmount = str_replace('-', '', $invoice->getBaseDiscountAmount());
}
$total = $invoice->getBaseSubtotal() + $invoice->getShippingAmount();
if($discountAmount > 0) {
$taxPercent = $this->getTaxPercent($invoice->getOrder()->getId());
if(!empty($taxPercent) && $taxPercent > 0) {
$getTaxAmount = $invoice->getBaseGrandTotal() * $taxPercent / (100 + $taxPercent);
$total = $invoice->getBaseGrandTotal() - $getTaxAmount;
$total = number_format((float)$total, 2, '.', '');
}
}
return $total;
}
public function getTaxAmount($invoice)
{
$discountAmount = $invoice->getBaseDiscountAmount();
if (strpos($invoice->getBaseDiscountAmount(), '-') !== false) {
$discountAmount = str_replace('-', '', $invoice->getBaseDiscountAmount());
}
$taxAmount = $invoice->getTaxAmount();
if($discountAmount > 0) {
$taxPercent = $this->getTaxPercent($invoice->getOrder()->getId());
if(!empty($taxPercent) && $taxPercent > 0) {
$taxAmount = $invoice->getBaseGrandTotal() * $taxPercent / (100 + $taxPercent);
$taxAmount = number_format((float)$taxAmount, 2, '.', '');
}
}
return $taxAmount;
}
public function getTaxPercent(int $orderId): float
{
$table = $this->resourceConnection->getTableName(self::SALES_ORDER_TAX_TABLE);
$connection = $this->resourceConnection->getConnection();
$select = $connection->select();
$select->from(['order_tax' => $table], ['percent' => 'order_tax.percent']);
$select->where('order_tax.order_id = ?', $orderId);
$percent = (float) $connection->fetchOne($select);
return $percent;
}
}