Skip to content

Commit

Permalink
Merge pull request #371 from shreesh-webkul/tax-changes
Browse files Browse the repository at this point in the history
Updated tax flow, Taxes are now calculated based on hotel address
  • Loading branch information
rohit053 authored Jun 28, 2022
2 parents 06c7851 + 02e3a1d commit 2813fb8
Show file tree
Hide file tree
Showing 25 changed files with 307 additions and 223 deletions.
6 changes: 6 additions & 0 deletions classes/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class AddressCore extends ObjectModel
*/
public $id_warehouse = null;

/**
* @var int Hotel id which address belongs to
*/
public $id_hotel = null;

/** @var int Country id */
public $id_country;

Expand Down Expand Up @@ -115,6 +120,7 @@ class AddressCore extends ObjectModel
'id_manufacturer' => array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'copy_post' => false),
'id_supplier' => array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'copy_post' => false),
'id_warehouse' => array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'copy_post' => false),
'id_hotel' => array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId', 'copy_post' => false),
'id_country' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_state' => array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId'),
'alias' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 32),
Expand Down
28 changes: 10 additions & 18 deletions classes/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,7 @@ public static function getTaxesAverageUsed($id_cart)
foreach ($products as $product) {
// products refer to the cart details

if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') {
$address_id = (int)$cart->id_address_invoice;
} else {
$address_id = (int)$product['id_address_delivery'];
} // Get delivery address of the product from the cart
$address_id = Cart::getIdAddressForTaxCalculation($product['id_product']);
if (!Address::addressExists($address_id)) {
$address_id = null;
}
Expand Down Expand Up @@ -630,8 +626,6 @@ public function getProducts($refresh = false, $id_product = false, $id_country =
return array();
}

$ecotax_rate = (float)Tax::getProductEcotaxRate($this->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$apply_eco_tax = Product::$_taxCalculationMethod == PS_TAX_INC && (int)Configuration::get('PS_TAX');
$cart_shop_context = Context::getContext()->cloneContext();

foreach ($result as &$row) {
Expand All @@ -647,11 +641,8 @@ public function getProducts($refresh = false, $id_product = false, $id_country =
$row['weight'] = (float)$row['weight_attribute'];
}

if (Configuration::get('PS_TAX_ADDRESS_TYPE') == 'id_address_invoice') {
$address_id = (int)$this->id_address_invoice;
} else {
$address_id = (int)$row['id_address_delivery'];
}
// hotel address for tax calculation
$address_id = Cart::getIdAddressForTaxCalculation($row['id_product']);
if (!Address::addressExists($address_id)) {
$address_id = null;
}
Expand Down Expand Up @@ -1492,7 +1483,6 @@ public function getOrderTotal($with_taxes = true, $type = Cart::BOTH, $products
$price_calculator = Adapter_ServiceLocator::get('Adapter_ProductPriceCalculator');
$configuration = Adapter_ServiceLocator::get('Core_Business_ConfigurationInterface');

$ps_tax_address_type = $configuration->get('PS_TAX_ADDRESS_TYPE');
$ps_use_ecotax = $configuration->get('PS_USE_ECOTAX');
$ps_round_type = $configuration->get('PS_ROUND_TYPE');
$ps_ecotax_tax_rules_group_id = $configuration->get('PS_ECOTAX_TAX_RULES_GROUP_ID');
Expand Down Expand Up @@ -1590,11 +1580,8 @@ public function getOrderTotal($with_taxes = true, $type = Cart::BOTH, $products
$virtual_context->shop = new Shop((int)$product['id_shop']);
}

if ($ps_tax_address_type == 'id_address_invoice') {
$id_address = (int)$this->id_address_invoice;
} else {
$id_address = (int)$product['id_address_delivery'];
} // Get delivery address of the product from the cart
// hotel address for tax calculation
$id_address = Cart::getIdAddressForTaxCalculation($product['id_product']);
if (!$address_factory->addressExists($id_address)) {
$id_address = null;
}
Expand Down Expand Up @@ -2691,6 +2678,11 @@ public function getAddressCollection()
return $collection;
}

public static function getIdAddressForTaxCalculation($id_product)
{
return HotelRoomType::getHotelIdAddressByIdProduct($id_product);
}

/**
* Set the delivery option and id_carrier, if there is only one carrier
*/
Expand Down
20 changes: 12 additions & 8 deletions classes/PaymentModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ public function validateOrder($id_cart, $id_order_state, $amount_paid, $payment_
$order->id_customer = (int)$this->context->cart->id_customer;
$order->id_address_invoice = (int)$this->context->cart->id_address_invoice;
$order->id_address_delivery = (int)$id_address;
$order->id_address_tax = (int)Cart::getIdAddressForTaxCalculation($order->product_list[0]['id_product']);
$order->id_currency = $this->context->currency->id;
$order->id_lang = (int)$this->context->cart->id_lang;
$order->id_cart = (int)$this->context->cart->id;
Expand Down Expand Up @@ -482,8 +483,9 @@ public function validateOrder($id_cart, $id_order_state, $amount_paid, $payment_

$product_var_tpl_list = array();
foreach ($order->product_list as $product) {
$price = Product::getPriceStatic((int)$product['id_product'], false, ($product['id_product_attribute'] ? (int)$product['id_product_attribute'] : null), 6, null, false, true, $product['cart_quantity'], false, (int)$order->id_customer, (int)$order->id_cart, (int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$price_wt = Product::getPriceStatic((int)$product['id_product'], true, ($product['id_product_attribute'] ? (int)$product['id_product_attribute'] : null), 2, null, false, true, $product['cart_quantity'], false, (int)$order->id_customer, (int)$order->id_cart, (int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$price = Product::getPriceStatic((int)$product['id_product'], false, ($product['id_product_attribute'] ? (int)$product['id_product_attribute'] : null), 6, null, false, true, $product['cart_quantity'], false, (int)$order->id_customer, (int)$order->id_cart);
$price_wt = Product::getPriceStatic((int)$product['id_product'], true, ($product['id_product_attribute'] ? (int)$product['id_product_attribute'] : null), 2, null, false, true, $product['cart_quantity'], false, (int)$order->id_customer, (int)$order->id_cart);


$product_price = Product::getTaxCalculationMethod() == PS_TAX_EXC ? Tools::ps_round($price, 2) : $price_wt;

Expand Down Expand Up @@ -694,7 +696,7 @@ public function validateOrder($id_cart, $id_order_state, $amount_paid, $payment_

$objAdvancedPayment = new HotelAdvancedPayment();

$vatAddress = new Address((int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$vatAddress = new Address((int)$order->id_address_tax);

$idLang = (int)$this->context->cart->id_lang;

Expand Down Expand Up @@ -746,14 +748,16 @@ public function validateOrder($id_cart, $id_order_state, $amount_paid, $payment_
))) {
$objBookingDetail->hotel_name = $objHotelBranch->hotel_name;
$objBookingDetail->room_type_name = $product['name'];
$objBookingDetail->city = $objHotelBranch->city;
$objBookingDetail->state = State::getNameById($objHotelBranch->state_id);
$objBookingDetail->country = Country::getNameById($idLang, $objHotelBranch->country_id);
$objBookingDetail->zipcode = $objHotelBranch->zipcode;
$objBookingDetail->phone = $objHotelBranch->phone;
$objBookingDetail->email = $objHotelBranch->email;
$objBookingDetail->check_in_time = $objHotelBranch->check_in;
$objBookingDetail->check_out_time = $objHotelBranch->check_out;
if ($hotelAddress = $objHotelBranch->getAddress($objCartBkData->id_hotel)) {
$objHtlBkDtl->city = $hotelAddress['city'];
$objHtlBkDtl->state = $hotelAddress['state'];
$objHtlBkDtl->country = $hotelAddress['country'];
$objHtlBkDtl->zipcode = $hotelAddress['postcode'];
$objHtlBkDtl->phone = $hotelAddress['phone'];
}
}
if ($roomTypeInfo = $objRoomType->getRoomTypeInfoByIdProduct($idProduct)) {
$objBookingDetail->adult = $roomTypeInfo['adult'];
Expand Down
34 changes: 13 additions & 21 deletions classes/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,8 @@ public function __construct($id_product = null, $full = false, $id_lang = null,
$this->manufacturer_name = Manufacturer::getNameById((int)$this->id_manufacturer);
$this->supplier_name = Supplier::getNameById((int)$this->id_supplier);
$address = null;
if (is_object($context->cart) && $context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')} != null) {
$address = $context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')};
}

$this->tax_rate = $this->getTaxesRate(new Address($address));
$id_address = Cart::getIdAddressForTaxCalculation($this->id);
$this->tax_rate = $this->getTaxesRate(new Address($id_address));

$this->new = $this->isNew();

Expand Down Expand Up @@ -2844,21 +2841,16 @@ public static function getPriceStatic($id_product, $usetax = true, $id_product_a
$id_state = 0;
$zipcode = 0;

if (!$id_address && Validate::isLoadedObject($cur_cart)) {
$id_address = $cur_cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')};
}

if ($id_address) {
$address_infos = Address::getCountryAndState($id_address);
if ($address_infos['id_country']) {
$id_country = (int)$address_infos['id_country'];
$id_state = (int)$address_infos['id_state'];
$zipcode = $address_infos['postcode'];
}
} elseif (isset($context->customer->geoloc_id_country)) {
$id_country = (int)$context->customer->geoloc_id_country;
$id_state = (int)$context->customer->id_state;
$zipcode = $context->customer->postcode;
} else {
$address_infos = Address::getCountryAndState(Cart::getIdAddressForTaxCalculation($id_product));
}

if ($address_infos['id_country']) {
$id_country = (int)$address_infos['id_country'];
$id_state = (int)$address_infos['id_state'];
$zipcode = $address_infos['postcode'];
}

if (Tax::excludeTaxeOption()) {
Expand Down Expand Up @@ -2965,7 +2957,7 @@ public static function priceCalculation($id_shop, $id_product, $id_product_attri
(int)$id_product,
$id_shop,
$id_currency,
$id_country,
(int)$context->country->id,
$id_group,
$quantity,
$id_product_attribute,
Expand Down Expand Up @@ -3160,7 +3152,7 @@ public static function isDiscounted($id_product, $quantity = 1, Context $context
$quantity = $cart_quantity ? $cart_quantity : $quantity;

$id_currency = (int)$context->currency->id;
$ids = Address::getCountryAndState((int)$context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$ids = Address::getCountryAndState(cart::getIdAddressForTaxCalculation($id_product));
$id_country = $ids['id_country'] ? (int)$ids['id_country'] : (int)Configuration::get('PS_COUNTRY_DEFAULT');
return (bool)SpecificPrice::getSpecificPrice((int)$id_product, $context->shop->id, $id_currency, $id_country, $id_group, $quantity, null, 0, 0, $quantity);
}
Expand Down Expand Up @@ -5048,7 +5040,7 @@ public static function getIdTaxRulesGroupByIdProduct($id_product, Context $conte
public function getTaxesRate(Address $address = null)
{
if (!$address || !$address->id_country) {
$address = Address::initialize();
$address = Address::initialize(Cart::getIdAddressForTaxCalculation($this->id));
}

$tax_manager = TaxManagerFactory::getManager($address, $this->id_tax_rules_group);
Expand Down
9 changes: 7 additions & 2 deletions classes/order/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class OrderCore extends ObjectModel
/** @var int Invoice address id */
public $id_address_invoice;

/** @var int Hotel address id */
public $id_address_tax;

public $id_shop_group;

public $id_shop;
Expand Down Expand Up @@ -196,6 +199,7 @@ class OrderCore extends ObjectModel
'fields' => array(
'id_address_delivery' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_address_invoice' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_address_tax' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_cart' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_currency' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_shop_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
Expand Down Expand Up @@ -747,6 +751,7 @@ protected function setProductImageInformations(&$product)

public function getTaxesAverageUsed()
{
// @todo should use order table to get tax average
return Cart::getTaxesAverageUsed((int)$this->id_cart);
}

Expand Down Expand Up @@ -1349,7 +1354,7 @@ protected function setInvoiceDetails($order_invoice)
return;
}

$address = new Address((int)$this->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$address = new Address((int)$this->id_address_tax);
$carrier = new Carrier((int)$this->id_carrier);
$tax_calculator = $carrier->getTaxCalculator($address);
$order_invoice->total_discount_tax_excl = $this->total_discounts_tax_excl;
Expand Down Expand Up @@ -2482,7 +2487,7 @@ public function hasCompletelyRefunded($refundFlag = 0)
if ($refundFlag) {
foreach ($refundBookings as $refundRow) {
if (Validate::isLoadedObject(
$objReturnState = new OrderReturnState($refundRow['current_state']
$objReturnState = new OrderReturnState($refundRow['state']
))) {
if ($refundFlag == OrderReturnState::ORDER_RETURN_STATE_FLAG_REFUNDED
&& !$objReturnState->refunded
Expand Down
12 changes: 6 additions & 6 deletions classes/order/OrderDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public function saveTaxCalculator(Order $order, $replace = false)
public function updateTaxAmount($order)
{
$this->setContext((int)$this->id_shop);
$address = new Address((int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$address = new Address((int)$order->id_address_tax);
$tax_manager = TaxManagerFactory::getManager($address, (int)Product::getIdTaxRulesGroupByIdProduct((int)$this->product_id, $this->context));
$this->tax_calculator = $tax_manager->getTaxCalculator();

Expand Down Expand Up @@ -509,7 +509,7 @@ protected function setProductTax(Order $order, $product)

$this->ecotax_tax_rate = 0;
if (!empty($product['ecotax'])) {
$this->ecotax_tax_rate = Tax::getProductEcotaxRate($order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$this->ecotax_tax_rate = Tax::getProductEcotaxRate($order->id_address_tax);
}
}

Expand Down Expand Up @@ -561,7 +561,7 @@ protected function setSpecificPrice(Order $order, $product = null)
protected function setDetailProductPrice(Order $order, Cart $cart, $product)
{
$this->setContext((int)$product['id_shop']);
Product::getPriceStatic((int)$product['id_product'], true, (int)$product['id_product_attribute'], 6, null, false, true, $product['cart_quantity'], false, (int)$order->id_customer, (int)$order->id_cart, (int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}, $specific_price, true, true, $this->context);
Product::getPriceStatic((int)$product['id_product'], true, (int)$product['id_product_attribute'], 6, null, false, true, $product['cart_quantity'], false, (int)$order->id_customer, (int)$order->id_cart, null, $specific_price, true, true, $this->context);
$this->specificPrice = $specific_price;
$this->original_product_price = Product::getPriceStatic($product['id_product'], false, (int)$product['id_product_attribute'], 6, null, false, false, 1, false, null, null, null, $null, true, true, $this->context);
$this->product_price = $this->original_product_price;
Expand All @@ -587,7 +587,7 @@ protected function setDetailProductPrice(Order $order, Cart $cart, $product)

$unit_price = Product::getPriceStatic((int)$product['id_product'], true,
($product['id_product_attribute'] ? intval($product['id_product_attribute']) : null),
2, null, false, true, 1, false, (int)$order->id_customer, null, (int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}, $null, true, true, $this->context);
2, null, false, true, 1, false, (int)$order->id_customer, null, null, $null, true, true, $this->context);
$this->product_quantity_discount = 0.00;
if ($quantity_discount) {
$this->product_quantity_discount = $unit_price;
Expand Down Expand Up @@ -672,7 +672,7 @@ protected function create(Order $order, Cart $cart, $product, $id_order_state, $
*/
public function createList(Order $order, Cart $cart, $id_order_state, $product_list, $id_order_invoice = 0, $use_taxes = true, $id_warehouse = 0)
{
$this->vat_address = new Address((int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$this->vat_address = new Address((int)$order->id_address_tax);
$this->customer = new Customer((int)$order->id_customer);

$this->id_order = $order->id;
Expand Down Expand Up @@ -708,7 +708,7 @@ public function setShippingCost(Order $order, $product)

$carrier = OrderInvoice::getCarrier((int)$this->id_order_invoice);
if (isset($carrier) && Validate::isLoadedObject($carrier)) {
$tax_rate = $carrier->getTaxesRate(new Address((int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}));
$tax_rate = $carrier->getTaxesRate(new Address((int)$order->id_address_tax));
}

$this->total_shipping_price_tax_excl = (float)$product['additional_shipping_cost'];
Expand Down
2 changes: 1 addition & 1 deletion classes/order/OrderSlip.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public static function create(Order $order, $booking_list, $shipping_cost = fals
$order_detail->product_quantity_refunded += $numDays;
}

$address = Address::initialize($order->id_address_invoice, false);
$address = Address::initialize($order->id_address_tax, false);
$id_address = (int)$address->id;
$id_tax_rules_group = Product::getIdTaxRulesGroupByIdProduct((int)$order_detail->product_id);
$tax_calculator = TaxManagerFactory::getManager($address, $id_tax_rules_group)->getTaxCalculator();
Expand Down
Loading

0 comments on commit 2813fb8

Please sign in to comment.