|
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 / |
Filename | /home/dev2.destoffenstraat.com/app/code/Mrgig/MV/Model/CustomerInvoice.php |
Size | 10.15 kb |
Permission | rwxrwxrwx |
Owner | root : root |
Create time | 17-Aug-2025 10:26 |
Last modified | 13-Apr-2022 07:08 |
Last accessed | 22-Aug-2025 19:05 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
<?php
namespace Mrgig\MV\Model;
use Mrgig\MV\Model\CustomerInvoice\Line as CustomerInvoiceLine;
use Mrgig\MV\Model\CustomerInvoice\VatTransactionLine;
use Mrgig\MV\Helper\Data as DataHelper;
use Mrgig\MV\Helper\Api as ApiHelper;
use Mrgig\MV\Model\Customer as CreateCustomer;
use Mrgig\MV\Logger\Logger;
use Magento\Framework\App\ResourceConnection;
use Mrgig\MV\Model\ResourceModel\Report;
use Mrgig\MV\Model\ReportFactory;
use Mrgig\MV\Model\Config\Source\Status as MVStatus;
class CustomerInvoice
{
private const SALES_ORDER_TAX_TABLE = 'sales_order_tax';
protected $container = [];
public function __construct(
CustomerInvoiceLine $customerInvoiceLine,
VatTransactionLine $transactionLine,
DataHelper $dataHelper,
ApiHelper $apiHelper,
CreateCustomer $createCustomer,
Logger $logger,
ResourceConnection $resourceConnection,
Report $report,
ReportFactory $reportFactory
) {
$this->customerInvoiceLine = $customerInvoiceLine;
$this->transactionLine = $transactionLine;
$this->dataHelper = $dataHelper;
$this->apiHelper = $apiHelper;
$this->createCustomer = $createCustomer;
$this->logger = $logger;
$this->resourceConnection = $resourceConnection;
$this->report = $report;
$this->reportFactory = $reportFactory;
}
public function createInvoice($invoice)
{
if($invoice->getIsProcess() == MVStatus::INVOICE_SEND) {
$this->dataHelper->addComment('Invoice already imported on MV.', $invoice->getId());
return true;
}
$invoice->setIsProcess(MVStatus::INVOICE_PROCESSING)->save();
$reportData = [
'invoice_id' => $invoice->getId(),
'increment_id' => $invoice->getIncrementId(),
];
$this->report->insertData($reportData);
$order = $invoice->getOrder();
$customerId = $order->getCustomerId();
if($order->getCustomerIsGuest() == 1) {
$customerId = $order->getIncrementId();
}
$response = $this->apiHelper->getCustomerById($customerId, $invoice->getId());
if(isset($response['error'])) {
return false;
} else {
if(isset($response['isFound']) && !$response['isFound']) {
$createCustomer = $this->createCustomer->createCustomer($order, $invoice->getId());
if($createCustomer === null) {
return null;
}
$this->apiHelper->sendCustomer($this->dataHelper->serialize($createCustomer), $invoice->getId());
}
}
$invoiceInfo = $this->getInvoiceInfo($invoice);
if($invoiceInfo === null) {
return null;
}
$invoiceInfo = $this->dataHelper->serialize($invoiceInfo);
$updateData = ['send_content' => $invoiceInfo];
$this->updateData($invoice->getId(), $updateData);
return $this->apiHelper->sendInvoice($invoiceInfo, $invoice->getId());
}
public function getInvoiceInfo($invoice)
{
$order = $invoice->getOrder();
$countryId = $this->getCountryId($order);
$customerId = $order->getCustomerId();
$vatId = $this->getVatId($order);
if(!empty($vatId)) {
$taxId = 0;
$vatScenarioId = 7;
} else {
$taxId = $this->dataHelper->getTaxIdArrayMapping($countryId);
$vatScenarioId = $this->getVatScenarioId($taxId);
}
if($order->getCustomerIsGuest() == 1) {
$customerId = $order->getIncrementId();
}
$this->container['amountTotal'] = $invoice->getGrandTotal();
$this->container['amountTotalCur'] = $invoice->getGrandTotal();
$this->container['autoCalculateVat'] = 0;
$this->container['customerId'] = $customerId;
$this->container['customerInvoiceLines'] = $this->customerInvoiceLine->invoiceLine($invoice, $countryId, $taxId);
$this->container['documentLink'] = null;
$this->container['fiscalYear'] = $this->getFiscalYear($invoice->getCreatedAt());
$this->container['invoiceDate'] = $this->getInvoiceDate($invoice->getCreatedAt());
$this->container['invoiceId'] = $invoice->getIncrementId();
$this->container['journalId'] = $this->dataHelper->getJournalId();
$this->container['journalSection'] = null;
$this->container['journalTransaction'] = 0;
$this->container['orderId'] = $order->getIncrementId();
$this->container['paymentConditionId'] = 7;
$this->container['periodNumber'] = $this->getPeriodNumber($invoice->getCreatedAt());
$this->container['vatAmount'] = $this->getTaxAmount($invoice);
$this->container['vatAmountCur'] = $this->getTaxAmount($invoice);
$this->container['vatScenarioId'] = $vatScenarioId;
$this->container['vatTransactionLines'] = $this->transactionLine->transactionLine($invoice, $taxId, $vatScenarioId);
$valid = $this->listInvalidProperties();
if(!empty($valid) && count($valid) > 0) {
$message = sprintf(
'Invalid the required parameter when calling createInvoice: #%s. %s',
$invoice->getIncrementId(),
implode(', ', $valid)
);
$this->dataHelper->addComment($message, $invoice->getId());
$this->logger->addError($message);
return null;
}
return $this->container;
}
public function getFiscalYear($date)
{
return date('Y', strtotime($date));
}
public function getPeriodNumber($date)
{
return date('m',strtotime($date));
}
public function getInvoiceDate($date)
{
return date('d-m-Y', strtotime($date));
}
public function getCountryId($order)
{
$shippingAddress = $order->getShippingAddress();
if(empty($shippingAddress)) {
$shippingAddress = $order->getBillingAddress();
}
return $shippingAddress->getCountryId();
}
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 getVatScenarioId($vatCodeId)
{
$vatScenarioRuleInfoList = $this->apiHelper->getVatScenarioRuleInfoList();
if(!empty($vatScenarioRuleInfoList)) {
foreach($vatScenarioRuleInfoList as $vatScenarioRule) {
if(isset($vatScenarioRule['vatCodeId']) && $vatScenarioRule['vatCodeId'] == $vatCodeId) {
if(isset($vatScenarioRule['vatScenarioId']) && !empty($vatScenarioRule['vatScenarioId'])){
return $vatScenarioRule['vatScenarioId'];
}
}
}
}
}
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;
}
public function listInvalidProperties()
{
$invalidProperties = [];
if ($this->container['customerId'] === null) {
$invalidProperties[] = "'customerId' can't be null";
}
if ((mb_strlen($this->container['customerId']) > 8)) {
$customerId = $this->container['customerId'];
$this->container['customerId'] = substr($customerId, 3, 10);
//$invalidProperties[] = "invalid value for 'customerId', the character length must be smaller than or equal to 8.";
}
if ($this->container['fiscalYear'] === null) {
$invalidProperties[] = "'fiscalYear' can't be null";
}
if ($this->container['invoiceDate'] === null) {
$invalidProperties[] = "'invoiceDate' can't be null";
}
if (!is_null($this->container['invoiceId']) && (mb_strlen($this->container['invoiceId']) > 8)) {
$invalidProperties[] = "invalid value for 'invoiceId', the character length must be smaller than or equal to 8.";
}
if ($this->container['journalId'] === null) {
$invalidProperties[] = "'journalId' can't be null";
}
if ((mb_strlen($this->container['journalId']) > 3)) {
$invalidProperties[] = "invalid value for 'journalId', the character length must be smaller than or equal to 3.";
}
if (!is_null($this->container['journalSection']) && (mb_strlen($this->container['journalSection']) > 4)) {
$invalidProperties[] = "invalid value for 'journalSection', the character length must be smaller than or equal to 4.";
}
if ($this->container['paymentConditionId'] === null) {
$invalidProperties[] = "'paymentConditionId' can't be null";
}
if ((mb_strlen($this->container['paymentConditionId']) > 3)) {
$invalidProperties[] = "invalid value for 'paymentConditionId', the character length must be smaller than or equal to 3.";
}
if ($this->container['periodNumber'] === null) {
$invalidProperties[] = "'periodNumber' can't be null";
}
return $invalidProperties;
}
public function valid()
{
return count($this->listInvalidProperties()) === 0;
}
public function updateData($invoiceId, $data)
{
$report = $this->reportFactory->create()->load($invoiceId, 'invoice_id');
if($report->getId()) {
$where = ['invoice_id = ?' => (int)$invoiceId];
$this->report->updateData($data, $where);
}
}
}
namespace Mrgig\MV\Model;
use Mrgig\MV\Model\CustomerInvoice\Line as CustomerInvoiceLine;
use Mrgig\MV\Model\CustomerInvoice\VatTransactionLine;
use Mrgig\MV\Helper\Data as DataHelper;
use Mrgig\MV\Helper\Api as ApiHelper;
use Mrgig\MV\Model\Customer as CreateCustomer;
use Mrgig\MV\Logger\Logger;
use Magento\Framework\App\ResourceConnection;
use Mrgig\MV\Model\ResourceModel\Report;
use Mrgig\MV\Model\ReportFactory;
use Mrgig\MV\Model\Config\Source\Status as MVStatus;
class CustomerInvoice
{
private const SALES_ORDER_TAX_TABLE = 'sales_order_tax';
protected $container = [];
public function __construct(
CustomerInvoiceLine $customerInvoiceLine,
VatTransactionLine $transactionLine,
DataHelper $dataHelper,
ApiHelper $apiHelper,
CreateCustomer $createCustomer,
Logger $logger,
ResourceConnection $resourceConnection,
Report $report,
ReportFactory $reportFactory
) {
$this->customerInvoiceLine = $customerInvoiceLine;
$this->transactionLine = $transactionLine;
$this->dataHelper = $dataHelper;
$this->apiHelper = $apiHelper;
$this->createCustomer = $createCustomer;
$this->logger = $logger;
$this->resourceConnection = $resourceConnection;
$this->report = $report;
$this->reportFactory = $reportFactory;
}
public function createInvoice($invoice)
{
if($invoice->getIsProcess() == MVStatus::INVOICE_SEND) {
$this->dataHelper->addComment('Invoice already imported on MV.', $invoice->getId());
return true;
}
$invoice->setIsProcess(MVStatus::INVOICE_PROCESSING)->save();
$reportData = [
'invoice_id' => $invoice->getId(),
'increment_id' => $invoice->getIncrementId(),
];
$this->report->insertData($reportData);
$order = $invoice->getOrder();
$customerId = $order->getCustomerId();
if($order->getCustomerIsGuest() == 1) {
$customerId = $order->getIncrementId();
}
$response = $this->apiHelper->getCustomerById($customerId, $invoice->getId());
if(isset($response['error'])) {
return false;
} else {
if(isset($response['isFound']) && !$response['isFound']) {
$createCustomer = $this->createCustomer->createCustomer($order, $invoice->getId());
if($createCustomer === null) {
return null;
}
$this->apiHelper->sendCustomer($this->dataHelper->serialize($createCustomer), $invoice->getId());
}
}
$invoiceInfo = $this->getInvoiceInfo($invoice);
if($invoiceInfo === null) {
return null;
}
$invoiceInfo = $this->dataHelper->serialize($invoiceInfo);
$updateData = ['send_content' => $invoiceInfo];
$this->updateData($invoice->getId(), $updateData);
return $this->apiHelper->sendInvoice($invoiceInfo, $invoice->getId());
}
public function getInvoiceInfo($invoice)
{
$order = $invoice->getOrder();
$countryId = $this->getCountryId($order);
$customerId = $order->getCustomerId();
$vatId = $this->getVatId($order);
if(!empty($vatId)) {
$taxId = 0;
$vatScenarioId = 7;
} else {
$taxId = $this->dataHelper->getTaxIdArrayMapping($countryId);
$vatScenarioId = $this->getVatScenarioId($taxId);
}
if($order->getCustomerIsGuest() == 1) {
$customerId = $order->getIncrementId();
}
$this->container['amountTotal'] = $invoice->getGrandTotal();
$this->container['amountTotalCur'] = $invoice->getGrandTotal();
$this->container['autoCalculateVat'] = 0;
$this->container['customerId'] = $customerId;
$this->container['customerInvoiceLines'] = $this->customerInvoiceLine->invoiceLine($invoice, $countryId, $taxId);
$this->container['documentLink'] = null;
$this->container['fiscalYear'] = $this->getFiscalYear($invoice->getCreatedAt());
$this->container['invoiceDate'] = $this->getInvoiceDate($invoice->getCreatedAt());
$this->container['invoiceId'] = $invoice->getIncrementId();
$this->container['journalId'] = $this->dataHelper->getJournalId();
$this->container['journalSection'] = null;
$this->container['journalTransaction'] = 0;
$this->container['orderId'] = $order->getIncrementId();
$this->container['paymentConditionId'] = 7;
$this->container['periodNumber'] = $this->getPeriodNumber($invoice->getCreatedAt());
$this->container['vatAmount'] = $this->getTaxAmount($invoice);
$this->container['vatAmountCur'] = $this->getTaxAmount($invoice);
$this->container['vatScenarioId'] = $vatScenarioId;
$this->container['vatTransactionLines'] = $this->transactionLine->transactionLine($invoice, $taxId, $vatScenarioId);
$valid = $this->listInvalidProperties();
if(!empty($valid) && count($valid) > 0) {
$message = sprintf(
'Invalid the required parameter when calling createInvoice: #%s. %s',
$invoice->getIncrementId(),
implode(', ', $valid)
);
$this->dataHelper->addComment($message, $invoice->getId());
$this->logger->addError($message);
return null;
}
return $this->container;
}
public function getFiscalYear($date)
{
return date('Y', strtotime($date));
}
public function getPeriodNumber($date)
{
return date('m',strtotime($date));
}
public function getInvoiceDate($date)
{
return date('d-m-Y', strtotime($date));
}
public function getCountryId($order)
{
$shippingAddress = $order->getShippingAddress();
if(empty($shippingAddress)) {
$shippingAddress = $order->getBillingAddress();
}
return $shippingAddress->getCountryId();
}
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 getVatScenarioId($vatCodeId)
{
$vatScenarioRuleInfoList = $this->apiHelper->getVatScenarioRuleInfoList();
if(!empty($vatScenarioRuleInfoList)) {
foreach($vatScenarioRuleInfoList as $vatScenarioRule) {
if(isset($vatScenarioRule['vatCodeId']) && $vatScenarioRule['vatCodeId'] == $vatCodeId) {
if(isset($vatScenarioRule['vatScenarioId']) && !empty($vatScenarioRule['vatScenarioId'])){
return $vatScenarioRule['vatScenarioId'];
}
}
}
}
}
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;
}
public function listInvalidProperties()
{
$invalidProperties = [];
if ($this->container['customerId'] === null) {
$invalidProperties[] = "'customerId' can't be null";
}
if ((mb_strlen($this->container['customerId']) > 8)) {
$customerId = $this->container['customerId'];
$this->container['customerId'] = substr($customerId, 3, 10);
//$invalidProperties[] = "invalid value for 'customerId', the character length must be smaller than or equal to 8.";
}
if ($this->container['fiscalYear'] === null) {
$invalidProperties[] = "'fiscalYear' can't be null";
}
if ($this->container['invoiceDate'] === null) {
$invalidProperties[] = "'invoiceDate' can't be null";
}
if (!is_null($this->container['invoiceId']) && (mb_strlen($this->container['invoiceId']) > 8)) {
$invalidProperties[] = "invalid value for 'invoiceId', the character length must be smaller than or equal to 8.";
}
if ($this->container['journalId'] === null) {
$invalidProperties[] = "'journalId' can't be null";
}
if ((mb_strlen($this->container['journalId']) > 3)) {
$invalidProperties[] = "invalid value for 'journalId', the character length must be smaller than or equal to 3.";
}
if (!is_null($this->container['journalSection']) && (mb_strlen($this->container['journalSection']) > 4)) {
$invalidProperties[] = "invalid value for 'journalSection', the character length must be smaller than or equal to 4.";
}
if ($this->container['paymentConditionId'] === null) {
$invalidProperties[] = "'paymentConditionId' can't be null";
}
if ((mb_strlen($this->container['paymentConditionId']) > 3)) {
$invalidProperties[] = "invalid value for 'paymentConditionId', the character length must be smaller than or equal to 3.";
}
if ($this->container['periodNumber'] === null) {
$invalidProperties[] = "'periodNumber' can't be null";
}
return $invalidProperties;
}
public function valid()
{
return count($this->listInvalidProperties()) === 0;
}
public function updateData($invoiceId, $data)
{
$report = $this->reportFactory->create()->load($invoiceId, 'invoice_id');
if($report->getId()) {
$where = ['invoice_id = ?' => (int)$invoiceId];
$this->report->updateData($data, $where);
}
}
}