1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
<?php
/* --------------------------------------------------------------
QuickEditProductGraduatedPricesReader.inc.php 2017-03-09
Gambio GmbH
http://www.gambio.de
Copyright (c) 2017 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
/**
* Class QuickEditProductGraduatedPricesReader
*
* @category System
* @package QuickEdit
* @subpackage Repositories
*/
class QuickEditProductGraduatedPricesReader implements QuickEditProductGraduatedPricesReaderInterface
{
/**
* @var CI_DB_query_builder
*/
protected $db;
/**
* QuickEditProductGraduatedPricesReader constructor.
*
* @param CI_DB_query_builder $db Database query builder instance.
*/
public function __construct(CI_DB_query_builder $db)
{
$this->db = $db;
}
/**
* Returns the graduated prices of a product or an empty array nothing was found.
*
* @param array|null $productIds Array containing the selected product IDs to be processed.
*
* @return array Returns array that contains the graduated prices information.
*/
public function getGraduatedPrices(array $productIds = null)
{
if(empty($productIds))
{
return [];
}
$taxRate = 0;
$result = [];
foreach($productIds as $productId)
{
$personalOffers = $this->_getPersonalOffersByProductId($productId);
if(PRICE_IS_BRUTTO === 'true')
{
$taxRate = $this->_getTaxRateByProductId($productId);
}
$result[$productId]['customer'] = array_map(function ($value) use ($taxRate)
{
$value['graduations'] = array_map(function ($item) use ($taxRate)
{
$item['quantity'] = (new DecimalType($item['quantity']))->asDecimal();
$item['personal_offer'] = sprintf('%01.2f',
round($item['personal_offer'] * (1 + $taxRate / 100), 2));
return $item;
}, $value['graduations']);
return $value;
}, $personalOffers);
}
return $result;
}
/**
* Provides customer-specific product offers.
*
* @param int|null $productId Optional, product ID to be processed.
*
* @return array Returns the graduated prices or an empty array if nothing was found.
*/
protected function _getPersonalOffersByProductId($productId = null)
{
$result = [];
$customers = $this->_getCustomerStatuses();
foreach($customers as $customer)
{
$customerStatusId = $customer['customers_status_id'];
$result[$customerStatusId]['graduations'] = $this->db->where('products_id', $productId)
->order_by('products_id', 'asc')
->order_by('quantity', 'asc')
->get('personal_offers_by_customers_status_'
. $customerStatusId)
->result_array();
$result[$customerStatusId]['status_id'] = $customer['customers_status_id'];
$result[$customerStatusId]['status_name'] = $customer['customers_status_name'];
}
return $result;
}
/**
* Returns the names of the existing customer groups.
*
* @return array Returns the customer status info or an empty array if nothing was found.
*/
protected function _getCustomerStatuses()
{
$customers = $this->db->select(['customers_status_id', 'customers_status_name'])
->where('language_id', $_SESSION['languages_id'])
->where_not_in('customers_status_id', [0])
->get('customers_status')
->result_array();
return $customers;
}
/**
* Returns the tax rate for the specified product.
*
* @param int $productId Product ID to be processed.
*
* @return array Returns an object that contains the product tax rate.
*/
protected function _getTaxRateByProductId($productId)
{
$result = $this->db->select('tax_rate')
->join('tax_rates', 'tax_rates.tax_class_id = products.products_tax_class_id')
->join('zones_to_geo_zones', 'zones_to_geo_zones.zone_country_id = ' . (int)STORE_COUNTRY)
->where('products.products_id', $productId)
->where('tax_rates.tax_zone_id = zones_to_geo_zones.geo_zone_id')
->get('products')
->row();
return $result->tax_rate;
}
}