Skip to content
Code-Schnipsel Gruppen Projekte
Commit 6a5ac228 erstellt von Henning Leutz's avatar Henning Leutz :martial_arts_uniform:
Dateien durchsuchen

refactor: payments

Übergeordneter 102a6e8d
Keine zugehörigen Branchen gefunden
Keine zugehörigen Tags gefunden
Keine zugehörigen Merge Requests gefunden
......@@ -29,6 +29,9 @@
<field type="TEXT NULL">payment_data</field> <!-- muss verschlüsselt sein -->
<field type="DATETIME NULL DEFAULT NULL">payment_time</field>
<field type="TEXT NULL">payment_address</field>
<field type="INT(2) NOT NULL">paid_status</field>
<field type="INT(11) NULL">paid_date</field>
<field type="TEXT NULL">paid_data</field>
<field type="TEXT NULL">history</field>
<field type="TEXT NULL">comments</field>
......@@ -65,6 +68,9 @@
<field type="TEXT NULL">payment_data</field> <!-- muss verschlüsselt sein -->
<field type="DATETIME NULL DEFAULT NULL">payment_time</field>
<field type="TEXT NULL">payment_address</field>
<field type="INT(2) NOT NULL">paid_status</field>
<field type="INT(11) NULL">paid_date</field>
<field type="TEXT NULL">paid_data</field>
<field type="VARCHAR(250) NOT NULL">hash</field>
<field type="timestamp NULL default NULL">c_date</field>
......
......@@ -13,5 +13,8 @@
<permission name="quiqqer.order.update" type="bool">
<defaultvalue>1</defaultvalue>
</permission>
<permission name="quiqqer.order.addPayment" type="bool">
<defaultvalue>0</defaultvalue>
</permission>
</permissions>
......@@ -9,6 +9,9 @@
use QUI;
use QUI\ERP\Accounting\ArticleList;
use QUI\ERP\Accounting\Payments\Payments;
use QUI\ERP\Accounting\Payments\Api\PaymentsInterface;
use QUI\ERP\Money\Price;
use QUI\Permissions\Permission;
/**
* Class AbstractOrder
......@@ -19,8 +22,15 @@
*
* @package QUI\ERP\Order
*/
abstract class AbstractOrder
abstract class AbstractOrder extends QUI\QDOM
{
const PAYMENT_STATUS_OPEN = 0;
const PAYMENT_STATUS_PAID = 1;
const PAYMENT_STATUS_PART = 2;
const PAYMENT_STATUS_ERROR = 4;
const PAYMENT_STATUS_CANCELED = 5;
const PAYMENT_STATUS_DEBIT = 11;
/**
* Order is only created
*/
......@@ -160,7 +170,6 @@ public function __construct($data = array())
$this->addressInvoice = json_decode($data['addressInvoice'], true);
$this->data = json_decode($data['data'], true);
// user
$this->customerId = (int)$data['customerId'];
......@@ -219,6 +228,12 @@ public function __construct($data = array())
// payment
$this->paymentId = $data['payment_id'];
$this->setAttributes(array(
'paid_status' => (int)$data['paid_status'],
'paid_data' => json_decode($data['paid_data'], true),
'paid_date' => $data['paid_date']
));
}
//region API
......@@ -426,23 +441,6 @@ public function getCurrency()
return QUI\ERP\Defaults::getCurrency();
}
/**
* Return the payment
*
* @return null|QUI\ERP\Accounting\Payments\Types\Payment
*/
public function getPayment()
{
$Payments = Payments::getInstance();
try {
return $Payments->getPayment($this->paymentId);
} catch (QUI\Exception $Exception) {
}
return null;
}
/**
* Has the order a delivery address?
*
......@@ -579,6 +577,46 @@ public function setCustomer($User)
}
}
//endregion
//region payments
/**
* Return the payment
*
* @return null|QUI\ERP\Accounting\Payments\Types\Payment
*/
public function getPayment()
{
$Payments = Payments::getInstance();
try {
return $Payments->getPayment($this->paymentId);
} catch (QUI\Exception $Exception) {
}
return null;
}
/**
* Return the payment paid status information
* - How many has already been paid
* - How many must be paid
*
* @return array
*/
public function getPaidStatusInformation()
{
QUI\ERP\Accounting\Calc::calculatePayments($this);
return array(
'paidData' => $this->getAttribute('paid_data'),
'paidDate' => $this->getAttribute('paid_date'),
'paid' => $this->getAttribute('paid'),
'toPay' => $this->getAttribute('toPay')
);
}
/**
* Set the payment method to the order
*
......@@ -608,6 +646,132 @@ public function setPayment($paymentId)
$this->paymentMethod = $Payment->getType();
}
/**
* @param $amount
* @param PaymentsInterface $PaymentMethod
* @param bool $date
* @param null $PermissionUser
*/
public function addPayment(
$amount,
PaymentsInterface $PaymentMethod,
$date = false,
$PermissionUser = null
) {
Permission::checkPermission(
'quiqqer.order.addPayment',
$PermissionUser
);
if ($this->getAttribute('paid_status') == self::PAYMENT_STATUS_PAID ||
$this->getAttribute('paid_status') == self::PAYMENT_STATUS_CANCELED
) {
return;
}
QUI::getEvents()->fireEvent(
'quiqqerOrderAddPaymentBegin',
array($this, $amount, $PaymentMethod, $date)
);
$User = QUI::getUserBySession();
$paidData = $this->getAttribute('paid_data');
$amount = Price::validatePrice($amount);
if (!$amount) {
return;
}
if (!is_array($paidData)) {
$paidData = json_decode($paidData, true);
}
if (!is_array($paidData)) {
$paidData = array();
}
if ($date === false) {
$date = time();
}
$isValidTimeStamp = function ($timestamp) {
return ((string)(int)$timestamp === $timestamp)
&& ($timestamp <= PHP_INT_MAX)
&& ($timestamp >= ~PHP_INT_MAX);
};
if ($isValidTimeStamp($date) === false) {
$date = strtotime($date);
if ($isValidTimeStamp($date) === false) {
$date = time();
}
}
$paidData[] = array(
'amount' => $amount,
'payment' => $PaymentMethod->getName(),
'date' => $date
);
$this->setAttribute('paid_data', json_encode($paidData));
$this->setAttribute('paid_date', $date);
// calculations
$this->Articles->calc();
$listCalculations = $this->Articles->getCalculations();
$this->setAttributes(array(
'currency_data' => json_encode($listCalculations['currencyData']),
'nettosum' => $listCalculations['nettoSum'],
'subsum' => $listCalculations['subSum'],
'sum' => $listCalculations['sum'],
'vat_array' => json_encode($listCalculations['vatArray'])
));
$this->addHistory(
QUI::getLocale()->get(
'quiqqer/order',
'history.message.addPayment',
array(
'username' => $User->getName(),
'uid' => $User->getId(),
'payment' => $PaymentMethod->getTitle()
)
)
);
QUI::getEvents()->fireEvent(
'quiqqerOrderAddPayment',
array($this, $amount, $PaymentMethod, $date)
);
$this->calculatePayments();
QUI::getEvents()->fireEvent(
'quiqqerOrderAddPaymentEnd',
array($this, $amount, $PaymentMethod, $date)
);
// if invoice exists, add payment, too
if (method_exists($this, 'getInvoice')) {
try {
/* @var $Invoice QUI\ERP\Accounting\Invoice\Invoice */
$Invoice = $this->getInvoice();
$Invoice->addPayment($amount, $PaymentMethod);
} catch (QUI\ERP\Accounting\Invoice\Exception $Exception) {
}
}
}
/**
* @return mixed
*/
abstract protected function calculatePayments();
//endregion
//region removing / deletion
......
......@@ -260,6 +260,61 @@ public function update($PermissionUser = null)
);
}
/**
* Calculates the payment for the order
*/
protected function calculatePayments()
{
$User = QUI::getUserBySession();
// old status
$oldPaidStatus = $this->getAttribute('paid_status');
QUI\ERP\Accounting\Calc::calculatePayments($this);
switch ($this->getAttribute('paid_status')) {
case self::PAYMENT_STATUS_OPEN:
case self::PAYMENT_STATUS_PAID:
case self::PAYMENT_STATUS_PART:
case self::PAYMENT_STATUS_ERROR:
case self::PAYMENT_STATUS_DEBIT:
case self::PAYMENT_STATUS_CANCELED:
break;
default:
$this->setAttribute('paid_status', self::PAYMENT_STATUS_ERROR);
}
$this->addHistory(
QUI::getLocale()->get(
'quiqqer/order',
'history.message.edit',
array(
'username' => $User->getName(),
'uid' => $User->getId()
)
)
);
QUI::getDataBase()->update(
Handler::getInstance()->table(),
array(
'paid_data' => $this->getAttribute('paid_data'),
'paid_date' => $this->getAttribute('paid_date'),
'paid_status' => $this->getAttribute('paid_status')
),
array('id' => $this->getId())
);
// Payment Status has changed
if ($oldPaidStatus != $this->getAttribute('paid_status')) {
QUI::getEvents()->fireEvent(
'onQuiqqerOrderAddComment',
array($this, $this->getAttribute('paid_status'), $oldPaidStatus)
);
}
}
/**
* Delete the order
*
......
......@@ -90,6 +90,61 @@ public function update($PermissionUser = null)
);
}
/**
* Calculates the payment for the order
*/
protected function calculatePayments()
{
$User = QUI::getUserBySession();
// old status
$oldPaidStatus = $this->getAttribute('paid_status');
QUI\ERP\Accounting\Calc::calculatePayments($this);
switch ($this->getAttribute('paid_status')) {
case self::PAYMENT_STATUS_OPEN:
case self::PAYMENT_STATUS_PAID:
case self::PAYMENT_STATUS_PART:
case self::PAYMENT_STATUS_ERROR:
case self::PAYMENT_STATUS_DEBIT:
case self::PAYMENT_STATUS_CANCELED:
break;
default:
$this->setAttribute('paid_status', self::PAYMENT_STATUS_ERROR);
}
$this->addHistory(
QUI::getLocale()->get(
'quiqqer/order',
'history.message.edit',
array(
'username' => $User->getName(),
'uid' => $User->getId()
)
)
);
QUI::getDataBase()->update(
Handler::getInstance()->tableOrderProcess(),
array(
'paid_data' => $this->getAttribute('paid_data'),
'paid_date' => $this->getAttribute('paid_date'),
'paid_status' => $this->getAttribute('paid_status')
),
array('id' => $this->getId())
);
// Payment Status has changed
if ($oldPaidStatus != $this->getAttribute('paid_status')) {
QUI::getEvents()->fireEvent(
'onQuiqqerOrderAddComment',
array($this, $this->getAttribute('paid_status'), $oldPaidStatus)
);
}
}
/**
* Delete the processing order
* The user itself or a super can delete it
......
0% Lade oder .
You are about to add 0 people to the discussion. Proceed with caution.
Bearbeitung dieser Nachricht zuerst beenden!
Bitte registrieren oder zum Kommentieren