ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilPurchase.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 require_once './include/inc.header.php';
12 require_once './Services/Payment/classes/class.ilPaymentObject.php';
13 require_once './Services/Payment/classes/class.ilPaymentBookings.php';
14 require_once './Services/Payment/classes/class.ilPaymentShoppingCart.php';
15 require_once './Services/User/classes/class.ilObjUser.php';
16 require_once './Services/Payment/classes/class.ilERP.php';
17 
18 
19 // TODO: rename class to ilPurchaseERP
21 {
22  private $deb;
23  private $ilUser;
24  private $active_erp;
25  private $cart;
26  private $sc;
27  private $pay_type;
28 
29  public function __construct( $usr_id, $pay_type )
30  {
31  $this->ilUser = new ilObjUser($usr_id);
32  $this->pay_type = $pay_type;
33  $this->active_erp = ilERP::getActive();
34  $this->erp_cls = "ilERPDebtor_" . $this->active_erp['erp_short'];
35  require_once './Services/Payment/classes/class.' . $this->erp_cls. '.php';
36 
37  $this->deb = new $this->erp_cls();
38  $this->cart = new ilPaymentShoppingCart( $this->ilUser );
39  $this->sc = $this->cart->getShoppingCart( $pay_type );
40 
41  if (! $this->sc) throw new ilERPException("EmptyCart");
42  }
43 
44 
45 
46  private function getDebtor()
47  {
48  if (!$this->deb->getDebtorByNumber($this->ilUser->getId()))
49  {
50  $this->deb->setAll( array(
51  'number' => $this->ilUser->getId(),
52  'name' => $this->ilUser->getFullName(),
53  'email' => $this->ilUser->email,
54  'address' => $this->ilUser->street,
55  'postalcode' => $this->ilUser->zipcode,
56  'city' => $this->ilUser->city,
57  'country' => $this->ilUser->country,
58  'phone' => $this->ilUser->phone_mobile)
59  );
60  $this->deb->createDebtor($this->ilUser->getId());
61  }
62  }
63 
64  public function purchase($tid)
65  {
66  global $lng;
67  $this->getDebtor();
68  $this->deb->createInvoice();
69  $products = array();
70  foreach ($this->sc as $i)
71  {
72  $pod = ilPaymentObject::_getObjectData($i['pobject_id']);
73  $bo = new ilPaymentBookings( $this->ilUser->getId());
74 
75  $ilias_tid = $this->ilUser->getId() . "_" . $tid;
76 
77  // psc_id, pobject_id, obj_id, typ, betrag_string
78  $bo->setTransaction($ilias_tid);
79  $bo->setPobjectId( isset($i['pobject_id']) ? $i['pobject_id'] : 0 );
80  $bo->setCustomerId( $this->ilUser->getId() );
81  $bo->setVendorId( $pod['vendor_id'] );
82  $bo->setPayMethod($this->paytype);
83  $bo->setOrderDate(time());
84  // $bo->setDuration($i['dauer']); // duration
85  // $bo->setPrice( $i['betrag'] ); // amount
86  //$bo->setPrice( ilPaymentPrices::_getPriceString( $i['price_id'] ));
87  $bo->setDuration($i['duration']);
88  $bo->setPrice($i['price_string']);
89 
90  $bo->setDiscount(0);
91  $bo->setVoucher('');
92  $bo->setVatRate( $i['vat_rate'] );
93  $bo->setVatUnit( $i['vat_unit'] );
94 
95  $bo->setTransactionExtern($tid);
96  // $product_name = $i['buchungstext'];
97  //$duration = $i['dauer'];
98  //$amount = $i['betrag'];
99  $product_name = $i['object_title'];
100  $duration = $i['duration'];
101  $amount = $i['price']; // -> ? $i['price_string']
102 
103  include_once './Services/Payment/classes/class.ilPayMethods.php';
104  $save_adr = (int) ilPaymethods::_EnabledSaveUserAddress($this->paytype) ? 1 : 0;
105  //if($save_adr == 1)
106  //{
107  $bo->setStreet($this->ilUser->getStreet(), '');
108  $bo->setPoBox('');//$this->ilUser->);
109  $bo->setZipcode($this->ilUser->getZipcode());
110  $bo->setCity($this->ilUser->getCity());
111  $bo->setCountry($this->ilUser->getCountry());
112  //}
113 
114  $bo->setPayed(1);
115  $bo->setAccess(1);
116  $bo->setAccessExtension($this->sc['extension']);
117  $boid = $bo->add();
118  //$bo->update();
119 
120  if ( $i['typ'] == 'crs')
121  {
122  include_once './Modules/Course/classes/class.ilCourseParticipants.php';
123  $this->deb->createInvoiceLine( 0, $product_name . " (" . $duration. ")", 1, $amount );
124  $products[] = $product_name;
125  $obj_id = ilObject::_lookupObjId($pod["ref_id"]);
127  $cp->add($this->ilUser->getId(), IL_CRS_MEMBER);
128  $cp->sendNotification($cp->NOTIFY_ACCEPT_SUBSCRIBER, $this->ilUser->getId());
129  }
130  }
131  $inv = $this->deb->bookInvoice();
132  $invoice_number = $this->deb->getInvoiceNumber();
133 
134  $attach = $this->deb->getInvoicePDF($inv);
135  $this->deb->saveInvoice($attach, false);
136  $lng->loadLanguageModule('payment');
137  $this->deb->sendInvoice($lng->txt('pay_order_paid_subject'),
138  $this->ilUser->getFullName() . ",\n" .
139  str_replace( '%products%', implode(", ", $products), $lng->txt('pay_order_paid_body')) ,
140  $this->ilUser->getEmail(),
141  $attach, $lng->txt('pays_invoice') ."-" . $invoice_number
142  );
143  $this->cart->emptyShoppingCart();
144  }
145 }
146 ?>