Example #1
0
 /**
  * @covers Model_Purchase_Item_Refund::get_price
  */
 public function test_get_price()
 {
     $mock = $this->getMock('stdClass', array('amount'));
     $purchase_item = $this->getMock('Model_Purchase_Item_Refund', array('get_reference_paranoid'), array('purchase_item_refund'));
     $purchase_item->expects($this->exactly(2))->method('get_reference_paranoid')->will($this->onConsecutiveCalls($mock, NULL));
     $refund_amount = new Jam_Price(10.25, 'GBP');
     $purchase_item_refund_price = $refund_amount->multiply_by(-1);
     $mock->expects($this->once())->method('amount')->will($this->returnValue($refund_amount));
     $this->assertEquals($purchase_item_refund_price, $purchase_item->get_price());
     $this->assertEquals(new Jam_Price(0, 'GBP'), $purchase_item->get_price());
 }
Example #2
0
 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);
 }
Example #3
0
 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());
 }
Example #4
0
 /**
  * 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;
 }
Example #5
0
 /**
  * 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());
 }
Example #6
0
 /**
  * @covers Jam_Price::as_html
  */
 public function test_as_html()
 {
     $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->assertSame('£13.23', $price1->as_html());
     $this->assertSame('$7.93', $price2->as_html('USD'));
     $this->assertSame('€8.50', $price3->as_html());
     $price1->display_currency('EUR');
     $this->assertSame('€15.76', $price1->as_html());
 }
Example #7
0
 /**
  * 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());
 }
Example #8
0
 /**
  * Return TRUE if total is bigger than discount_threshold
  * @return boolean
  */
 public function is_discounted(Jam_Price $total)
 {
     return $this->discount_threshold and $total->is(Jam_Price::GREATER_THAN, $this->discount_threshold);
 }
Example #9
0
 /**
  * @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());
 }
Example #10
0
 /**
  * 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;
 }