public function price_for_purchase_item(Model_Purchase_Item $purchase_item) { $brand_purchase = $purchase_item->get_insist('brand_purchase'); $brand_total = $brand_purchase->total_price('product'); $purchase = $brand_purchase->get_insist('purchase'); $totals = array_map(function ($brand_purchase) { return $this->applies_to($brand_purchase) ? $brand_purchase->total_price('product') : 0; }, $purchase->brand_purchases->as_array()); $total = Jam_Price::sum($totals, $purchase_item->currency(), $purchase_item->monetary()); $multiplier = $total->is(Jam_Price::GREATER_THAN, 0) ? $brand_total->amount() / $total->amount() : 1; return $this->amount->monetary($purchase_item->monetary())->multiply_by(-$multiplier); }
public function total_price() { $total = $this->total_purchase_price(); $items = $this->available_items(); $items = Model_Shipping_Item::filter_discounted_items($items, $total); $groups = Array_Util::group_by($items, function ($item) { return $item->group_key(); }); $group_prices = array_map(function ($grouped_items) use($total) { $prices = Model_Shipping_Item::relative_prices($grouped_items); return Jam_Price::sum($prices, $total->currency(), $total->monetary(), $total->display_currency()); }, $groups); return Jam_Price::sum($group_prices, $total->currency(), $total->monetary(), $total->display_currency()); }
/** * Total amount to be refunded * @return Jam_Price */ public function amount() { if (!$this->amount) { if (!count($this->items)) { $this->amount = $this->brand_purchase->total_price(array('is_payable' => TRUE)); } else { $amounts = array_map(function ($item) { return $item->amount(); }, $this->items->as_array()); $this->amount = Jam_Price::sum($amounts, $this->currency(), $this->monetary(), $this->display_currency()); } } return $this->amount; }
/** * @covers Jam_Price::sum */ public function test_sum() { $monetary = new OpenBuildings\Monetary\Monetary('GBP', new OpenBuildings\Monetary\Source_Static()); $price1 = new Jam_Price(13.234, 'GBP', $monetary); $price2 = new Jam_Price(5, 'GBP', $monetary); $price3 = new Jam_Price(8.5, 'EUR', $monetary); $this->assertEquals('USD', Jam_Price::sum(array($price1, $price2, $price3), 'USD')->currency()); $this->assertSame(40.238901914488, Jam_Price::sum(array($price1, $price2, $price3), 'USD')->amount()); $this->assertSame(45.371025, Jam_Price::sum(array($price1, $price2, $price3, 20), 'GBP')->amount()); $price1 = new Jam_Price(13.234, 'GBP'); $price2 = new Jam_Price(5, 'GBP'); $price3 = new Jam_Price(8.5, 'EUR'); $expected = new Jam_Price(40.238901914488, 'USD', $monetary, 'EUR'); $this->assertEquals($expected, Jam_Price::sum(array($price1, $price2, $price3), 'USD', $monetary, 'EUR')); }
/** * Sum the total price of the filtered items. * * @param array $types * @return Jam_Price */ public function total_price($types = NULL) { $prices = array_map(function ($item) { return $item->total_price(); }, $this->items($types)); return Jam_Price::sum($prices, $this->currency(), $this->monetary(), $this->display_currency()); }
/** * Perform price arithmetic - add / remove prices with correct currency convertions * @return $this */ public function add($price) { $prices = func_get_args(); array_unshift($prices, $this); return Jam_Price::sum($prices, $this->currency(), $this->monetary(), $this->display_currency(), $this->ceil_on_convert()); }
/** * @deprecated 0.9.1 Having refund items per purchase item is deprecated * @return Jam_Price */ public function refunded_amount() { $amounts = array_map(function ($refund_item) { return $refund_item->amount(); }, $this->refund_items->as_array()); return Jam_Price::sum($amounts, $this->currency(), $this->monetary(), $this->display_currency()); }
/** * Convert multiple Model_Brand_Refund objects to an array of parameteres, suited for Omnipay * @param array $refunds * @return array */ public static function convert_multiple_refunds(array $refunds) { $payment = $refunds[0]->payment_insist(); $currency = $refunds[0]->display_currency() ?: $refund->currency(); $amounts = array(); $params = array('transactionReference' => $payment->payment_id, 'reason' => $refunds[0]->reason, 'currency' => $currency); foreach ($refunds as $refund) { if (count($refund->items)) { throw new Exception_Payment('Multiple refunds do not support refund items'); } else { $amounts[] = $refund->amount(); } } $params['amount'] = Jam_Price::sum($amounts, $currency)->as_string($currency); return $params; }