Initial commit
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
class FinancingController{
|
||||
private $model;
|
||||
|
||||
function __construct(){
|
||||
$this->model = new FinancingModel();
|
||||
}
|
||||
|
||||
/**
|
||||
* get interest rate
|
||||
* @return json value for intereset rate
|
||||
*/
|
||||
public function getInterestRate(){
|
||||
echo json_encode($this->model->getInterestRate(), JSON_NUMERIC_CHECK);
|
||||
}
|
||||
|
||||
/**
|
||||
* get interest rate for customers
|
||||
* @return json values for interest rate for each customer
|
||||
*/
|
||||
public function getInterestRateForCustomers(){
|
||||
echo json_encode($this->model->getInterestRateForCustomers(), JSON_NUMERIC_CHECK);
|
||||
}
|
||||
|
||||
/**
|
||||
* save interest rate
|
||||
* @return json update message
|
||||
*/
|
||||
public function saveInterestRate(){
|
||||
$interestRate = isset($_REQUEST['interestRate']) ? $_REQUEST['interestRate'] : 0;
|
||||
echo json_encode($this->model->saveInterestRate($interestRate));
|
||||
}
|
||||
|
||||
/**
|
||||
* get customers and their discount
|
||||
* @return json values for customers and dicounts
|
||||
*/
|
||||
public function getCustomersAndDiscount(){
|
||||
echo json_encode($this->model->getCustomersAndDiscount());
|
||||
}
|
||||
|
||||
/**
|
||||
* save customers discount
|
||||
* @return json update message
|
||||
*/
|
||||
public function saveCustomersDiscount() {
|
||||
$discounts = isset($_REQUEST['discounts']) ? $_REQUEST['discounts'] : [];
|
||||
echo json_encode($this->model->saveCustomersDiscount($discounts));
|
||||
}
|
||||
|
||||
/**
|
||||
* include financing template
|
||||
*/
|
||||
public function financingTemplate() {
|
||||
require_once('templates/FinancingTempalte.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* include set interest rate template
|
||||
*/
|
||||
public function setInterestRateTemplate() {
|
||||
require_once('templates/SetInterestRateTemplate.php');
|
||||
}
|
||||
|
||||
public function setCustomersDiscountTemplate() {
|
||||
require_once('templates/SetCustomersDiscountTemplate.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* open financing page
|
||||
*/
|
||||
public function showPage(){
|
||||
require_once('FinancingPage.php');
|
||||
}
|
||||
}
|
||||
52
api-wiaas/server/components/v1/financing/FinancingModel.php
Normal file
52
api-wiaas/server/components/v1/financing/FinancingModel.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
class FinancingModel{
|
||||
/**
|
||||
* get interest rate
|
||||
* @return HashArray interest rate value
|
||||
*/
|
||||
public function getInterestRate(){
|
||||
$interestRate = new InterestRate();
|
||||
return $interestRate->getInterestRate();
|
||||
}
|
||||
|
||||
/**
|
||||
* get interest rate for customers
|
||||
* @return HashArray values for interest rate for each customer
|
||||
*/
|
||||
public function getInterestRateForCustomers(){
|
||||
$interestRate = new InterestRate();
|
||||
return $interestRate->getInterestRateForCustomers();
|
||||
}
|
||||
|
||||
/**
|
||||
* save interest rate
|
||||
* @param Float $interestRateValue interest rate value
|
||||
* @return Array update message
|
||||
*/
|
||||
public function saveInterestRate($interestRateValue){
|
||||
$interestRate = new InterestRate();
|
||||
return $interestRate->saveInterestRate($interestRateValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* get customers discount
|
||||
* @return HashArray customers discount
|
||||
*/
|
||||
public function getCustomersAndDiscount() {
|
||||
$interestRate = new InterestRate();
|
||||
|
||||
return $interestRate->getCustomersAndDiscount();
|
||||
}
|
||||
|
||||
/**
|
||||
* save discount for customer - financing
|
||||
* @param Array $discounts id of the customer and the discount applied for each
|
||||
* @return Array update message
|
||||
*/
|
||||
public function saveCustomersDiscount($discounts) {
|
||||
$interestRate = new InterestRate();
|
||||
|
||||
return $interestRate->saveCustomersDiscount($discounts);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<script src="<?php echo PATH_JS_COMPONENTS.'financing/financing.directive.js?v='.APPLICATION_VERSION;?>" type="text/javascript"></script>
|
||||
<script src="<?php echo PATH_JS_COMPONENTS.'financing/set-interest-rate.directive.js?v='.APPLICATION_VERSION;?>" type="text/javascript"></script>
|
||||
<script src="<?php echo PATH_JS_COMPONENTS.'financing/set-customers-discount.directive.js?v='.APPLICATION_VERSION;?>" type="text/javascript"></script>
|
||||
|
||||
<div id="financing-module" class="container-fluid col-md-12">
|
||||
<h1>{{ 'financing.TITLE' | translate }}</h1>
|
||||
<financing ng-controller="financingController" ng-init="getInterestRate()"></financing>
|
||||
</div>
|
||||
158
api-wiaas/server/components/v1/financing/InterestRate.php
Normal file
158
api-wiaas/server/components/v1/financing/InterestRate.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
class InterestRate{
|
||||
const SYSTEM_FINANCING_RATE_ID = 1;
|
||||
|
||||
/**
|
||||
* get interest rate
|
||||
* @return HashArray interest rate value
|
||||
*/
|
||||
public function getInterestRate(){
|
||||
global $database;
|
||||
$data = [];
|
||||
$sql = "SELECT
|
||||
f.interestRate
|
||||
FROM ".TABLES['financing']." f
|
||||
WHERE f.id=".self::SYSTEM_FINANCING_RATE_ID;
|
||||
$query = $database->query($sql);
|
||||
$row = $database->fetchArray($query);
|
||||
$data['interestRate'] = isset($row['interestRate']) ? $row['interestRate'] : 0;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* get interest rate for each customer
|
||||
* @param Int $idCustomer the id of the customer for which to take the inters rate for
|
||||
* @return HashArray interest rate value for every customer or specific customer in the system
|
||||
*/
|
||||
public function getInterestRateForCustomers($idCustomer = 0){
|
||||
global $database;
|
||||
$data = [];
|
||||
$interestRate = $this->getInterestRate()['interestRate'];
|
||||
$whereSql = $idCustomer ? "WHERE rcd.idCustomer=".$idCustomer : '';
|
||||
|
||||
$sql = "SELECT
|
||||
rcd.discount,
|
||||
rcd.idCustomer
|
||||
FROM
|
||||
".TABLES['rel_customer_discount']." rcd
|
||||
$whereSql";
|
||||
$query = $database->query($sql);
|
||||
while($row = $database->fetchArray($query)) {
|
||||
$data[$row['idCustomer']] = $interestRate - $row['discount'];
|
||||
}
|
||||
|
||||
return $idCustomer && isset($data[$idCustomer]) ? $data[$idCustomer] : $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* save interest rate
|
||||
* @param Float $interestRateValue interest rate value
|
||||
* @return Array update message
|
||||
*/
|
||||
public function saveInterestRate($interestRate){
|
||||
global $database;
|
||||
$interestRate = $database->escapeValue($interestRate);
|
||||
$data = [];
|
||||
|
||||
if(!$interestRate){
|
||||
$data['messages'][] = [
|
||||
'code' => 'error',
|
||||
'message' => 'NO_INTEREST_RATE'
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
$checkMessage = $database->invalidNumber('interestRate', $interestRate, 0, 100);
|
||||
if($checkMessage){
|
||||
$data['messages'][] = $checkMessage;
|
||||
}
|
||||
|
||||
$sql = "UPDATE ".TABLES['financing']."
|
||||
SET interestRate = $interestRate
|
||||
WHERE id=".self::SYSTEM_FINANCING_RATE_ID;
|
||||
$query = $database->query($sql);
|
||||
|
||||
$data['messages'][] = [
|
||||
'code' => 'success',
|
||||
'message' => 'INTEREST_RATE_UPDATED'
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* get customers discounts
|
||||
* @return HashArray interest rate value
|
||||
*/
|
||||
public function getCustomersAndDiscount() {
|
||||
global $database;
|
||||
$data = $this->getInterestRate();
|
||||
|
||||
$sql = "SELECT
|
||||
c.id AS idCustomer,
|
||||
c.name,
|
||||
IFNULL(rcd.discount, 0) AS discount
|
||||
FROM
|
||||
".TABLES['customers']." c
|
||||
LEFT JOIN ".TABLES['rel_customer_discount']." rcd
|
||||
ON c.id = rcd.idCustomer
|
||||
ORDER BY c.name";
|
||||
$data['customers'] = $database->fetchResultArray($sql);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* save discount for customer - financing
|
||||
* @param Array $details id of the customer and the discount applied for each
|
||||
* @return Array update message
|
||||
*/
|
||||
public function saveCustomersDiscount($details) {
|
||||
global $database;
|
||||
$interestRate = $this->getInterestRate()['interestRate'];
|
||||
$insertValues = '';
|
||||
$affectedRows = 0;
|
||||
$data = [];
|
||||
|
||||
if(!count($details)) {
|
||||
$data['messages'][] = [
|
||||
'code' => 'warning',
|
||||
'message' => 'CUSTOMERS_DISCOUNT_EMPTY'
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
foreach($details as $customerDiscounts) {
|
||||
$discount = floatval($customerDiscounts['discount']);
|
||||
if($discount > $interestRate || $discount < 0) {
|
||||
$data['messages'][] = [
|
||||
'code' => 'error',
|
||||
'message' => 'DISCOUNT_NOT_VALID',
|
||||
'key' => $customerDiscounts['name']
|
||||
];
|
||||
} else {
|
||||
$sql = "INSERT INTO ".TABLES['rel_customer_discount']."
|
||||
(idCustomer, discount)
|
||||
VALUES (".$customerDiscounts['idCustomer'].", '$discount')
|
||||
ON DUPLICATE KEY
|
||||
UPDATE
|
||||
discount = '$discount'";
|
||||
$row = $database->query($sql);
|
||||
$affectedRows += $database->affectedRows();
|
||||
}
|
||||
}
|
||||
|
||||
if($affectedRows) {
|
||||
$data['messages'][] = [
|
||||
'code' => 'success',
|
||||
'message' => 'DISCOUNT_UPDATED'
|
||||
];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<button type="button"
|
||||
id="set-interest-rate-btn"
|
||||
subModule="setInterestRate"
|
||||
class="btn btn-default"
|
||||
ng-click="setSubModule($event)">Interest rate</button>
|
||||
<button type="button"
|
||||
id="set-discount-for-customers-btn"
|
||||
subModule="setCustomersDiscount"
|
||||
class="btn btn-default"
|
||||
ng-click="setSubModule($event)">Customers discount</button>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-sm-12"
|
||||
id="set-interest-rate-layer"
|
||||
ng-if="isSubmoduleVisible('setInterestRate')">
|
||||
<h3>Set interest rate</h3>
|
||||
<set-interest-rate ng-controller="setInterestRateCtrl" ng-init="getInterestRate()"></set-interest-rate>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-sm-12"
|
||||
id="set-customers-discount-layer"
|
||||
ng-if="isSubmoduleVisible('setCustomersDiscount')">
|
||||
<h3>Set customers discount</h3>
|
||||
<set-customers-discount ng-controller="setCustomersDiscountCtrl" ng-init="getCustomersAndDiscount()"></set-customers-discount>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,37 @@
|
||||
<div id="set-customers-discount-module-container">
|
||||
<div class="row">
|
||||
<div class="interest-rate col-md-12">
|
||||
<h4 class="col-md-2">Customer name</h4>
|
||||
<h4 class="col-md-1">Discount</h4>
|
||||
<h4 class="col-md-2">New financing rate</h4>
|
||||
</div>
|
||||
<div ng-repeat="customerDetails in customers track by customerDetails.idCustomer">
|
||||
<div class="customer-info-discount col-md-12">
|
||||
<span class="col-md-2">{{::customerDetails.name}}</span>
|
||||
<div class="customer-discount col-md-1">
|
||||
<input id="discount-input-{{customerDetails.idCustomer}}"
|
||||
class="edit-interest-rate-discount"
|
||||
type="number"
|
||||
placeholder="0"
|
||||
name="customerDiscount"
|
||||
ng-model="discounts[customerDetails.idCustomer]"
|
||||
ng-model-options="{ allowInvalid: true}"
|
||||
min="0"
|
||||
max="{{interestRate-0.01}}"
|
||||
step="0.01" />
|
||||
%
|
||||
</div>
|
||||
<span class="col-md-2">{{getNewInterestRate(customerDetails.idCustomer)}} %</span>
|
||||
</div>
|
||||
<div class="col-md-12 customer-discount-message" ng-if="showMessage">
|
||||
<span class="col-md-offset-1 alert alert-warning">
|
||||
Value should be between 0 and {{getMaxInterestRateValue()}} with maximum 2 decimals
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-md-12">
|
||||
<div class="btn btn-primary" ng-click="saveCustomersDiscount()">Save</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,15 @@
|
||||
<div id="set-interest-rate-container">
|
||||
<div class="row">
|
||||
<div class="interest-rate col-md-4">
|
||||
<label>Interest rate: </label>
|
||||
<input id="interest-rate-input"
|
||||
class="edit-interest-rate"
|
||||
type="text"
|
||||
placeholder="Interest rate"
|
||||
ng-model="interestRate" /> %
|
||||
</div>
|
||||
<div class="col-md-12">
|
||||
<div class="btn btn-primary" ng-click="saveInterestRate()">Save</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user