80 if (is_null($this->cart)) {
81 $this->cart = Array();
98 $this->cart = Array();
119 if (!empty($this->cart[$assetid])) {
120 return $this->cart[$assetid];
167 function getTotal($taxation_cost=0.0, $taxation_rate=0.0)
170 foreach ($this->cart as $assetid => $details) {
172 if ($details[
'price'] != -1) {
174 $has_tax = array_get_index($details,
'has_tax', FALSE);
175 $calculate_tax = array_get_index($details,
'taxable', FALSE);
176 if ($calculate_tax) {
178 $cost = $item_costs[
'tax_adjusted_cost'];
180 $cost = $details[
'price'];
183 $total += $details[
'quantity'] * $cost;
187 $total += $taxation_cost;
211 $grand_total = ($this->
getTotal($total_tax, $taxation_rate) + $flat_charges_total) - $total_tax;
213 $grand_total = $this->
getTotal($total_tax, $taxation_rate) + $flat_charges_total;
231 $flat_charges_total = 0.0;
232 foreach ($flat_charges as $name => $price) {
233 $flat_charges_total += $price;
236 return $flat_charges_total;
251 $total_delivery_cost = 0.00;
252 if (isset($_SESSION[
'sq_region_specific'])) {
253 foreach ($this->cart as $assetid => $details) {
255 if (!array_get_index($details,
'is_donation', FALSE) && isset($_SESSION[
'sq_region_specific'][
'delivery_weight_fee'])) {
256 $delivery_weight_fee = $_SESSION[
'sq_region_specific'][
'delivery_weight_fee'];
258 if (isset($_SESSION[
'sq_region_specific'][
'delivery_weight_fee'][
'metadata_fieldid'])) {
259 $fieldid = $_SESSION[
'sq_region_specific'][
'delivery_weight_fee'][
'metadata_fieldid'];
260 $mm =& $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
261 $weight = (float) $mm->getMetadataValueByAssetid($assetid, $fieldid);
262 }
else if (isset($_SESSION[
'sq_region_specific'][
'delivery_weight_fee'][
'attribute_name'])) {
263 $attribute_name = $_SESSION[
'sq_region_specific'][
'delivery_weight_fee'][
'attribute_name'];
264 $asset =& $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
265 $weight = (float) $asset->attr($attribute_name);
268 if (isset($weight) && !empty($weight)) {
269 $product_quantity = array_get_index($details,
'quantity', 0);
271 if (isset($delivery_weight_fee[
'max_quantity']) && ($delivery_weight_fee[
'max_quantity'] < $product_quantity)) {
272 $product_quantity = $delivery_weight_fee[
'max_quantity'];
275 $total_delivery_cost += $weight * $delivery_weight_fee[
'fee'] * $product_quantity;
282 if (isset($_SESSION[
'sq_region_specific'][
'international_delivery_fee'])) {
283 $total_delivery_cost += $_SESSION[
'sq_region_specific'][
'international_delivery_fee'];
288 if (isset($_SESSION[
'sq_region_specific'][
'conditions'])) {
289 $grand_total_plus_delivery = $grand_total + $total_delivery_cost;
290 $conditions = $_SESSION[
'sq_region_specific'][
'conditions'];
291 foreach ($conditions as $key => $data) {
293 $operator = print_r($data[
'operator'], TRUE);
294 if (eval(
'$satisfied = $grand_total_plus_delivery'.$operator.
'$data[\'comparison_price\']; return $satisfied;')) {
295 if ($data[
'remove_fees']) {
296 $total_delivery_cost = 0;
298 $total_delivery_cost += $data[
'additional_cost'];
307 if (isset($_SESSION[
'sq_region_specific'][
'max_delivery_fee']) && ($_SESSION[
'sq_region_specific'][
'max_delivery_fee'] < $total_delivery_cost)) {
308 $total_delivery_cost = $_SESSION[
'sq_region_specific'][
'max_delivery_fee'];
311 return $total_delivery_cost;
328 foreach ($this->cart as $assetid => $details) {
330 if (($details[
'price'] != -1) && ($details[
'taxable'])) {
331 $has_tax = array_get_index($details,
'has_tax', FALSE);
333 $tax_cost = $item_costs[
'tax_cost'];
335 $total_tax += $details[
'quantity'] * $tax_cost;
360 $tax_cost = $cost - ($cost / (1 + $taxation_rate));
361 $cost = $cost - $tax_cost;
364 $tax_cost = $cost * $taxation_rate;
367 'tax_adjusted_cost' => $cost,
368 'tax_cost' => $tax_cost,
384 if (is_null($this->cart))
return 0;
386 foreach ($this->cart as $assetid => $details) {
387 if (!array_get_index($details,
'is_donation', FALSE)) {
388 $count += $details[
'quantity'];
405 return count($this->cart);
425 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
426 if (is_null($asset)) {
431 trigger_localised_error(
'ECOM0005', E_USER_WARNING, $assetid);
435 if ($quantity <= 0) {
436 if ($asset instanceof Donation) {
437 trigger_localised_error(
'ECOM0025', E_USER_WARNING);
438 }
else if ($quantity < 0) {
439 trigger_localised_error(
'ECOM0004', E_USER_WARNING);
445 if (!isset($this->cart[$assetid])) {
447 if ($asset instanceof
File) {
448 $name = $asset->attr(
'title');
450 $name = $asset->name;
457 $is_donation = FALSE;
460 $financial_code =
'';
462 if ($asset instanceof Donation) {
465 }
else if ($asset instanceof
Product) {
466 $price = $asset->attr(
'price');
468 $refund_type = $asset->attr(
'refund_type');
469 $product_code = $asset->attr(
'product_code');
470 $financial_code = $asset->attr(
'financial_code');
473 $tmp_price_obj = $asset->attr(
'price_obj');
474 if (!empty($tmp_price_obj) ) {
475 if ((!empty($tmp_price_obj->value_has_tax)) && ($tmp_price_obj->value_has_tax == 1)) {
478 if ((!empty($tmp_price_obj->calculate_tax)) && ($tmp_price_obj->calculate_tax == 1)) {
486 $this->cart[$assetid] = Array(
488 'quantity' => $quantity,
490 'has_tax' => $has_tax,
491 'taxable' => $taxable,
492 'is_donation' => $is_donation,
493 'refund_type' => $refund_type,
494 'product_code' => $product_code,
495 'financial_code' => $financial_code,
497 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($asset);
500 if ($asset instanceof Donation) {
501 $this->cart[$assetid][
'quantity'] = $quantity;
503 $this->cart[$assetid][
'quantity'] += $quantity;
529 if (!isset($this->cart[$assetid])) {
530 return $this->
addItem($assetid, $quantity);
534 if (array_get_index($this->cart[$assetid],
'quantity', 0) == $quantity) {
540 trigger_localised_error(
'ECOM0010', E_USER_WARNING);
542 }
else if ($quantity == 0) {
544 }
else if (isset($this->cart[$assetid][
'quantity']) && $this->cart[$assetid][
'quantity'] == $quantity) {
548 $this->cart[$assetid][
'quantity'] = $quantity;
574 if (!isset($this->cart[$assetid])) {
576 trigger_localised_error(
'ECOM0006', E_USER_WARNING);
583 if (is_null($quantity)) {
584 $quantity = $this->cart[$assetid][
'quantity'];
588 if ($quantity <= 0) {
590 trigger_localised_error(
'ECOM0007', E_USER_WARNING);
595 if ($quantity > $this->cart[$assetid][
'quantity']) {
596 trigger_localised_error(
'ECOM0008', E_USER_WARNING);
598 }
else if ($quantity == $this->cart[$assetid][
'quantity']) {
599 unset($this->cart[$assetid]);
601 $this->cart[$assetid][
'quantity'] -= $quantity;
620 $tmp_cart = $this->cart;
622 $hash = md5(serialize($tmp_cart));
644 function printReceipt($taxation_name=
'GST', $taxation_rate=0, $flat_charges=Array(), $remove_tax=FALSE, $currency_symbol=
'$', $float_precision=2, $refund_info=
'')
646 if (empty($this->cart))
return;
649 $float_precision = (int) $float_precision;
650 $price_format = $currency_symbol.
'%.'.$float_precision.
'f';
654 $has_products = FALSE;
657 $non_refundable_flag = FALSE;
660 foreach ($this->cart as $assetid => $details) {
663 $tmp_taxable = array_get_index($details,
'taxable', FALSE);
664 $product_name = array_get_index($details,
'name',
'');
666 if (!$non_refundable_flag) {
667 $refund_type = array_get_index($details,
'refund_type',
'NonRefundable');
668 $non_refundable_flag = ($refund_type ===
'NonRefundable') ? TRUE : FALSE;
671 if (strlen($product_name) > $name_length) {
672 $name_length = strlen(array_get_index($details,
'name',
''));
675 if (strlen(
'Excluded Tax ('.$taxation_name.
')') > $name_length) {
676 $name_length = strlen(
'Excluded Tax ('.$taxation_name.
')');
681 $price = array_get_index($details,
'price', -1);
682 if (!$has_price && ($price != -1)) {
687 if (!$has_products && !array_get_index($details,
'is_donation', FALSE) && ($price != -1)) {
688 $has_products = TRUE;
693 $flat_charges_total = 0.0;
694 foreach ($flat_charges as $name => $price) {
695 if (strlen($name) > $name_length) {
696 $name_length = strlen($name);
698 $flat_charges_total += $price;
702 $row_format =
'%-'.$name_length.
's %10s'.($has_price && $has_products ?
' %10s' :
'').
"\n";
706 $header = sprintf($row_format,
'Item',
'Quantity',
'Price');
708 $header = sprintf($row_format,
'Item',
'Amount');
711 $header = sprintf($row_format,
'Item',
'Quantity');
715 for ($ii = 1; $ii < strlen($header); $ii++) {
724 foreach ($this->cart as $assetid => $details) {
725 $cost = array_get_index($details,
'price', 0);
727 $has_tax = array_get_index($details,
'has_tax', FALSE);
728 $calculate_tax = array_get_index($details,
'taxable', FALSE);
729 if ($calculate_tax) {
731 $cost = $item_costs[
'tax_adjusted_cost'];
732 $tax_cost = $item_costs[
'tax_cost'];
737 $product_quantity = array_get_index($details,
'quantity', 0);
742 $cost = sprintf($price_format, $cost * $product_quantity);
743 $tax_cost = sprintf($price_format, $tax_cost * $product_quantity);
748 if (array_get_index($details,
'is_donation', FALSE)) {
749 $product_quantity =
'';
751 printf($row_format, array_get_index($details,
'name',
''), $product_quantity, $cost);
753 printf($row_format, array_get_index($details,
'name',
''), $cost);
756 printf($row_format, array_get_index($details,
'name',
''), $product_quantity);
763 $row_format =
'%'.$name_length.
's %10s'.($has_price && $has_products ?
' %10s' :
'').
"\n";
768 $sub_total = $remove_tax ? $this->
getTotal($total_tax, $taxation_rate)-$total_tax : $this->
getTotal(0, $taxation_rate);
771 echo sprintf($row_format,
'Total', $this->
getCount(), sprintf($price_format, $sub_total));
773 echo sprintf($row_format,
'Total', sprintf($price_format, $sub_total));
776 if (($total_tax > 0) || ($flat_charges_total > 0.0)) {
777 if ($total_tax > 0) {
778 $row_title = $remove_tax ?
'Excluded Tax ('.$taxation_name.
')' :
'Total ('.$taxation_name.
')';
779 echo sprintf($row_format, $row_title, $this->
getCount(), sprintf($price_format, $total_tax));
782 foreach ($flat_charges as $name => $price) {
783 echo sprintf($row_format, $name,
'', sprintf($price_format, $price));
790 if ($total_delivery_cost > 0) {
791 echo sprintf($row_format,
'Delivery Fee',
'', sprintf($price_format, $total_delivery_cost));
795 if (($total_delivery_cost > 0) || ($flat_charges_total > 0)) {
798 echo sprintf($row_format,
'Grand Total',
'', sprintf($price_format, $grand_total_ex_delivery + $total_delivery_cost));
800 if (!empty($refund_info) && $non_refundable_flag) {
801 echo
"\n".$refund_info.
"\n";
805 echo sprintf($row_format,
'Total', $this->
getCount());
829 function printCustomisedReceipt(&$contents_bodycopy, &$format_bodycopy, $taxation_name=
'GST', $taxation_rate=0, $flat_charges=Array(), $remove_tax=FALSE, $currency_symbol=
'$', $float_precision=2)
831 if (empty($this->cart))
return;
834 $float_precision = (int) $float_precision;
835 $price_format = $currency_symbol.
'%.'.$float_precision.
'f';
837 $content_keywords = $contents_bodycopy->getKeywords();
838 $content_replacements = Array();
841 $format_keywords = $format_bodycopy->getKeywords();
843 foreach ($this->cart as $assetid => $details) {
844 $asset =& $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
845 $format_replacements = Array();
848 foreach ($format_keywords as $keyword) {
849 $format_replacements[$keyword] = $asset->getKeywordReplacement($keyword);
852 $product_quantity = array_get_index($details,
'quantity', 0);
855 $cost = array_get_index($details,
'price', 0);
857 $has_tax = array_get_index($details,
'has_tax', FALSE);
858 $calculate_tax = array_get_index($details,
'taxable', FALSE);
859 if ($calculate_tax) {
861 $cost = $item_costs[
'tax_adjusted_cost'];
862 $tax_cost = $item_costs[
'tax_cost'];
868 if (array_get_index($details,
'is_donation', FALSE)) {
869 $format_replacements[
'item_quantity_added'] =
'';
870 $format_replacements[
'item_price_incl_tax'] = sprintf($price_format, $product_quantity);
872 $format_replacements[
'item_quantity_added'] = $product_quantity;
873 $format_replacements[
'item_price_incl_tax'] = sprintf($price_format, $cost);
876 $format_replacements[
'item_tax'] = sprintf($price_format, $tax_cost);
878 $format_replacements[
'total_item_price'] = sprintf($price_format, $cost * $product_quantity);
879 $format_replacements[
'total_item_tax'] = sprintf($price_format, $tax_cost * $product_quantity);
881 $format_bodycopy->setKeywordReplacements($format_replacements);
882 $format_bodycopy->printBody();
886 $content_replacements[
'receipt_listing'] = ob_get_clean();
889 $content_replacements[
'total_tax'] = $remove_tax ? sprintf($price_format, 0) : sprintf($price_format, $total_tax);
890 $content_replacements[
'tax_name'] = $taxation_name;
893 $flat_charges_total = 0.0;
895 foreach ($flat_charges as $name => $price) {
896 echo
'<tr><td>'.$name.
':</td><td>'.sprintf($price_format, $price).
'</td></tr>';
897 $flat_charges_total += $price;
900 $content_replacements[
'flat_fees_table'] = ob_get_clean();
901 $content_replacements[
'flat_fees_total'] = sprintf($price_format, $flat_charges_total);
903 $sub_total = $remove_tax ? $this->
getTotal($total_tax, $taxation_rate)-$total_tax : $this->
getTotal(0, $taxation_rate);
904 $content_replacements[
'sub_total'] = sprintf($price_format, $sub_total);
908 $content_replacements[
'delivery_total'] = sprintf($price_format, $total_delivery_cost);
909 $content_replacements[
'grand_total'] = sprintf($price_format, $grand_total_ex_delivery + $total_delivery_cost);
911 $contents_bodycopy->setKeywordReplacements($content_replacements);
914 $contents_bodycopy->printBody();
915 $customised_receipt = ob_get_clean();
917 return $customised_receipt;
936 function getCartXml($taxation_name=
'GST', $taxation_rate=0, $flat_charges=Array(), $remove_tax=FALSE, $currency_symbol=
'$', $float_precision=2)
938 if (empty($this->cart))
return '';
941 $float_precision = (int) $float_precision;
942 $price_format =
'%.'.$float_precision.
'f';
945 $mm =& $GLOBALS[
'SQ_SYSTEM']->getMetadataManager();
948 foreach ($this->cart as $assetid => $details) {
949 $quantity = array_get_index($details,
'quantity', 0);
952 $cost = array_get_index($details,
'price', 0);
953 if (array_get_index($details,
'taxable', FALSE)) {
954 $item_costs = $this->
_calculateItemCosts($cost, $taxation_rate, array_get_index($details,
'has_tax', FALSE));
955 $cost = $item_costs[
'tax_adjusted_cost'];
956 $tax_cost = $item_costs[
'tax_cost'];
962 if (array_get_index($details,
'is_donation', FALSE)) {
970 $item_price = sprintf($price_format, $cost);
971 $item_price_incl_tax = sprintf($price_format, $cost + $tax_cost);
972 $total_item_price = sprintf($price_format, $cost * $quantity);
973 $total_item_price_incl_tax = sprintf($price_format, ($cost + $tax_cost) * $quantity);
974 $total_item_tax = sprintf($price_format, $tax_cost * $quantity);
977 $metadata_values = $mm->getMetadataFieldValues($assetid);
978 $metadata_values_str =
'';
979 foreach ($metadata_values as $name => $value) {
980 $metadata_values_str .= <<<HEREDOC
983 <name><![CDATA[$name]]></name>
984 <value><![CDATA[$value]]></value>
990 if ($metadata_values_str !=
'') {
991 $metadata_str = <<<HEREDOC
993 <metadata>$metadata_values_str
998 $products_str .= <<<HEREDOC
1002 <name><![CDATA[{$details[
'name']}]]></name>
1003 <is_donation>$donation</is_donation>
1004 <quantity>$quantity</quantity>
1005 <item_price>$item_price</item_price>
1006 <total_item_price>$total_item_price</total_item_price>
1007 <item_price_incl_tax>$item_price_incl_tax</item_price_incl_tax>
1008 <total_item_price_incl_tax>$total_item_price_incl_tax</total_item_price_incl_tax>
1009 <item_tax>$tax_cost</item_tax>
1010 <total_item_tax>$total_item_tax</total_item_tax>$metadata_str
1017 $total_tax = $remove_tax ? sprintf(
'-'.$price_format, $total_tax) : sprintf($price_format, $total_tax);
1020 $flat_charge_values_str =
'';
1021 $flat_charges_total = 0.0;
1022 foreach ($flat_charges as $name => $price) {
1023 $flat_charge_values_str .= <<<HEREDOC
1026 <name><![CDATA[$name]]></name>
1027 <price>$price</price>
1030 $flat_charges_total += $price;
1033 $flat_charges_str =
'';
1034 if ($flat_charge_values_str !=
''){
1035 $flat_charges_str = <<<HEREDOC
1037 <flat_charges>$flat_charge_values_str
1043 $flat_fees_total = sprintf($price_format, $flat_charges_total);
1045 $sub_total = sprintf($price_format, $this->
getTotal(0, $taxation_rate));
1050 $grand_total = sprintf($price_format, $grand_total_ex_delivery + $delivery_total);
1051 $grand_total_ex_delivery = sprintf($price_format, $grand_total_ex_delivery);
1052 $delivery_total = sprintf($price_format, $delivery_total);
1054 $cart_xml = <<<HEREDOC
1056 <currency_symbol><![CDATA[$currency_symbol]]></currency_symbol>
1057 <sub_total>$sub_total</sub_total>
1058 <taxation_name><![CDATA[$taxation_name]]></taxation_name>
1059 <total_tax>$total_tax</total_tax>
1060 <flat_fees_total>$flat_fees_total</flat_fees_total>
1061 <delivery_total>$delivery_total</delivery_total>
1062 <grand_total_ex_delivery>$grand_total_ex_delivery</grand_total_ex_delivery>
1063 <grand_total>$grand_total</grand_total>
1064 <products>$products_str
1065 </products>$flat_charges_str
1083 if (empty($this->cart))
return;
1085 $asset_ids = array_keys($this->cart);
1086 $result = $GLOBALS[
'SQ_SYSTEM']->am->assetExists($asset_ids);
1089 if (is_array($result)) {
1091 foreach ($asset_ids as $id) {
1092 if (!is_int(array_search($id, $result))) {
1098 }
else if (is_bool($result) && !$result) {