Example #1
0
 public function action_commit()
 {
     $user_id = \Session::get(self::SESSION_KEY_USER_ID);
     $user = Model_Member::find($user_id);
     $address = \Session::get(self::SESSION_KEY_ADDRESS);
     /*------make order----------------------------------------------------------*/
     $order = new Model_Order();
     $order->member_id = $user->id;
     $order->postalcode = $address['postalcode'];
     $order->destination = $address['address'] . $address['billname'] . $address['companyname'];
     $date = time();
     $order->order_date = date('Y-m-d H:i:s', $date);
     $order->print_flag = 0;
     $order->status = 0;
     $order->save();
     /*-----make orderline------------------------*/
     $cart = \Session::get(self::SESSION_KEY_CART);
     foreach ($cart['orders'] as $orderline) {
         $item_id = $orderline['item_id'];
         $item = Model_Item::find($item_id);
         $num = $orderline['quantity'];
         $size = $orderline['size'];
         $neworderline = new Model_Orderline();
         $neworderline->order_id = $order->id;
         $neworderline->item_id = $item_id;
         $neworderline->num = $num;
         $neworderline->size = $size;
         $neworderline->save();
         $earning = new Model_Earning();
         $earning->member_id = $user->id;
         $earning->item_id = $item_id;
         $earning->size = $size;
         switch ($size) {
             case 'S':
                 $unit_price = $neworderline->item->unit_price_s;
                 break;
             case 'M':
                 $unit_price = $neworderline->item->unit_price_m;
                 break;
             case 'L':
                 $unit_price = $neworderline->item->unit_price_m;
                 break;
             default:
                 $unit_price = $neworderline->item->unit_price;
                 break;
         }
         $earning->unit_price = $unit_price;
         $earning->num = $num;
         $earning->date = date('Y-m-d H:i:s', $date);
         $earning->category = $item->category;
         $earning->item_name = $item->name;
         $now = date('Ymd');
         $birthday = date('Ymd', strtotime($user->birthday));
         $earning->age = (int) floor(($now - $birthday) / 10000);
         $earning->save();
     }
     \Session::delete(self::SESSION_KEY_CART);
     return Response::redirect('index.php/message/commit');
 }
 public function setOrderServiceCompleteAction()
 {
     $sn = strtoupper(trim($this->_getParam('sn')));
     $result = 0;
     $ofd = new Model_OrderForDriver($sn);
     $order = new Model_Order($sn);
     if ($ofd->exists() && $ofd->get('driver_code') == $this->code && $order->get('status') < Model_Order::STATUS_SERVICE_COMPLATE) {
         $order->set('status', Model_Order::STATUS_SERVICE_COMPLATE);
         $result = $order->save();
     }
     $this->sendResult($result);
 }
Example #3
0
 /**
  *
  * view affiliates and payments
  */
 public function action_pay()
 {
     //create an order and mark it as paid to the user_id
     if (is_numeric($this->request->param('id'))) {
         //get the user
         $user = new Model_User($this->request->param('id'));
         if ($user->loaded()) {
             //commissions due to pay
             $query = DB::select(DB::expr('SUM(amount) total'))->from('affiliates')->where('id_user', '=', $user->id_user)->where('date_to_pay', '<', Date::unix2mysql())->where('status', '=', Model_Affiliate::STATUS_CREATED)->group_by('id_user')->execute();
             $due_to_pay = $query->as_array();
             $due_to_pay = isset($due_to_pay[0]['total']) ? $due_to_pay[0]['total'] : 0;
             if ($due_to_pay > 0) {
                 //create the order
                 $order = new Model_Order();
                 $order->id_user = $user->id_user;
                 $order->amount = $due_to_pay * -1;
                 //we add the order as a negative, since we pay, we don't get paid.
                 $order->currency = 'USD';
                 $order->paymethod = 'paypal';
                 $order->pay_date = Date::unix2mysql();
                 $order->notes = 'Affiliate Commissions';
                 $order->status = Model_Order::STATUS_PAID;
                 try {
                     $order->save();
                     //update the commissions
                     DB::update('affiliates')->set(array('date_paid' => Date::unix2mysql(), 'status' => Model_Affiliate::STATUS_PAID, 'id_order_payment' => $order->id_order))->where('id_user', '=', $user->id_user)->where('date_to_pay', '<', Date::unix2mysql())->where('status', '=', Model_Affiliate::STATUS_CREATED)->execute();
                     Alert::set(Alert::SUCCESS, __('Commission Paid'));
                 } catch (Exception $e) {
                 }
             }
         }
     }
     $this->template->title = __('Affiliates Payments');
     $query = DB::select(DB::expr('SUM(amount) total'))->select('id_user')->from('affiliates')->where('date_to_pay', '<', Date::unix2mysql())->where('status', '=', Model_Affiliate::STATUS_CREATED)->group_by('id_user')->having('total', '>=', core::config('affiliate.payment_min'))->execute();
     $users_to_pay = $query->as_array('id_user');
     $total_to_pay = 0;
     foreach ($users_to_pay as $key => $value) {
         $total_to_pay += $value['total'];
     }
     $users = new Model_User();
     if (count($users_to_pay)) {
         $users = $users->where('id_user', 'in', array_keys($users_to_pay))->where('status', '=', Model_User::STATUS_ACTIVE)->find_all();
     }
     $this->render('oc-panel/pages/affiliate/pay', array('users' => $users, 'total_to_pay' => $total_to_pay, 'users_to_pay' => $users_to_pay));
 }
Example #4
0
 /**
  * creates an order
  * @param  Model_Ad $ad    
  * @param  Model_User $user          
  * @param  integer   $id_product  
  * @param  numeric   $amount      
  * @param  string   $currency    
  * @param  string   $description 
  * @return Model_Order                
  */
 public static function new_order(Model_Ad $ad, $user, $id_product, $amount, $currency = NULL, $description = NULL, $featured_days = NULL)
 {
     if ($currency === NULL) {
         $currency = core::config('payment.paypal_currency');
     }
     if ($description === NULL) {
         $description = Model_Order::product_desc($id_product);
     }
     //get if theres an unpaid order for this product and this ad
     $order = new Model_Order();
     $order->where('id_ad', '=', $ad->id_ad)->where('id_user', '=', $user->id_user)->where('status', '=', Model_Order::STATUS_CREATED)->where('id_product', '=', $id_product)->where('amount', '=', $amount)->where('currency', '=', $currency)->limit(1)->find();
     //if no unpaid create order
     if (!$order->loaded()) {
         //add coupon ID and discount only if not AD_SELL
         if (Model_Coupon::valid($id_product)) {
             $amount = Model_Coupon::price($id_product, $amount);
             $order->id_coupon = Model_Coupon::current()->id_coupon;
         }
         //create order
         $order = new Model_Order();
         $order->id_user = $user->id_user;
         $order->id_ad = $ad->id_ad;
         $order->id_product = $id_product;
         $order->currency = $currency;
         $order->amount = $amount;
         $order->description = $description;
         //store how many days the ad is featured
         if ($featured_days !== NULL and is_numeric($featured_days)) {
             $order->featured_days = $featured_days;
         }
         try {
             $order->save();
         } catch (Exception $e) {
             throw HTTP_Exception::factory(500, $e->getMessage());
         }
         //send email to user with link to pay
         $url_checkout = $user->ql('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order));
         $replace = array('[ORDER.ID]' => $order->id_order, '[ORDER.DESC]' => $order->description, '[URL.CHECKOUT]' => $url_checkout);
         //$user->email('new-order',$replace);
     }
     return $order;
 }
Example #5
0
 public function action_commit()
 {
     $orders = \Session::get(self::ORDER);
     if (count($orders['cart']) == 0) {
         Response::redirect('index.php/controlsystem/order/order');
     }
     $post = $_POST;
     $order = new Model_Order();
     $order->postalcode = $post['postalcode1'] . '-' . $post['postalcode2'];
     $order->destination = $post['address'];
     $order->print_flag = false;
     $order->status = false;
     $date = date("Y-m-d", time());
     $order->order_date = $date;
     $customer = Model_Member::query()->where('name', $post['customer_name'])->get_one();
     if (!empty($customer)) {
         $order->member_id = $customer->id;
     } else {
         $order->member_id = null;
     }
     $order->save();
     $order_id = $order->id;
     //make orderlines
     $orders = \Session::get(self::ORDER);
     foreach ($orders['cart'] as $orderline) {
         $item_id = $orderline['item_id'];
         $item = Model_Item::find($item_id);
         $num = $orderline['num'];
         $size = strtoupper($orderline['size']);
         $neworderline = new Model_Orderline();
         $neworderline->order_id = $order->id;
         $neworderline->item_id = $item_id;
         $neworderline->num = $num;
         $neworderline->size = $size;
         $neworderline->save();
         $earning = new Model_Earning();
         if (!empty($customer)) {
             $earning->member_id = $customer->id;
         } else {
             $earning->member_id = null;
         }
         $earning->item_id = $item_id;
         $earning->size = $size;
         switch ($size) {
             case 'S':
                 $unit_price = $neworderline->item->unit_price_s;
                 break;
             case 'M':
                 $unit_price = $neworderline->item->unit_price_m;
                 break;
             case 'L':
                 $unit_price = $neworderline->item->unit_price_m;
                 break;
             default:
                 $unit_price = $neworderline->item->unit_price;
                 break;
         }
         $earning->unit_price = $unit_price;
         $earning->num = $num;
         $earning->date = $date;
         $earning->category = $item->category;
         $earning->item_name = $item->name;
         $now = date('Ymd');
         if (!empty($customer)) {
             $birthday = date('Ymd', strtotime($customer->birthday));
             $earning->age = (int) floor(($now - $birthday) / 10000);
         } else {
             $birthday = null;
             $earning->age = 0;
         }
         $earning->save();
     }
     return View::forge('controlsystem/order/commit');
 }
Example #6
0
 /**
  * [new_order description]
  * @param  Model_User    $user    [description]
  * @param  Model_Product $product [description]
  * @param  boolean       check_match_product, if set to false will update the order with the product if different
  * @return [type]                 [description]
  */
 public static function new_order(Model_User $user, Model_Product $product, $match_product = TRUE)
 {
     $order = new Model_Order();
     if ($user->loaded() and $product->loaded()) {
         //get if theres an unpaid order for this user we wwill use it..
         $order->where('id_user', '=', $user->id_user)->where('status', '=', Model_Order::STATUS_CREATED);
         //also check that matches the product for the order
         if ($match_product === TRUE) {
             $order->where('id_product', '=', $product->id_product)->where('amount', '=', $product->final_price())->where('currency', '=', $product->currency);
         }
         $order->limit(1)->find();
         //order didnt exist so lets create it.
         if ($order->loaded() === FALSE) {
             //create order
             $order = new Model_Order();
             $order->id_user = $user->id_user;
         }
         // no matter what happens if product is different save! this will also save the order if its new ;)
         if ($order->id_product != $product->id_product) {
             $order->ip_address = ip2long(Request::$client_ip);
             $order->id_product = $product->id_product;
             $order->currency = $product->currency;
             //add coupon ID and discount
             if (Model_Coupon::current()->loaded()) {
                 $order->id_coupon = Model_Coupon::current()->id_coupon;
             }
             $order->amount = $product->final_price();
             $order->VAT = euvat::vat_percentage();
             $order->VAT_number = $user->VAT_number;
             $order->country = $user->country;
             $order->city = $user->city;
             $order->postal_code = $user->postal_code;
             $order->address = $user->address;
             try {
                 $order->save();
             } catch (Exception $e) {
                 throw HTTP_Exception::factory(500, $e->getMessage());
             }
         }
     }
     return $order;
 }
 public function orderDetailAction()
 {
     $this->view->activeMenu = 'orders';
     $sn = trim($this->_getParam('sn'));
     if (empty($sn)) {
         $this->error('SN Does not exists.');
         return;
     }
     $order = new Model_Order($sn);
     if (!$order->exists() || $order->get('user_id') != $this->user->get('id')) {
         $this->error('Order does not exists.');
         return;
     }
     $this->view->order = $order;
     $ofd = new Model_OrderForDriver($sn);
     $driverAssigned = false;
     $vehicleAssigned = false;
     if ($ofd->exists() && !empty($ofd->get('driver_code'))) {
         $driverAssigned = new Model_Driver($ofd->get('driver_code'));
     }
     if ($ofd->exists() && !empty($ofd->get('vehicle_id'))) {
         $vehicleAssigned = new Model_Vehicle($ofd->get('vehicle_id'));
     }
     $this->view->driverAssigned = $driverAssigned;
     $this->view->vehicleAssigned = $vehicleAssigned;
     $this->view->vehicleTypes = (new Model_VehicleType())->fetchPagedList(1, 999999)['rows'];
     if (!$this->_request->isXmlHttpRequest()) {
         return;
     }
     // edit order
     $arp = new AjaxResponse();
     $arp->setStatus(AjaxResponse::STATUS_FAILED);
     $availableParams = array('forwho', 'city', 'contact-name', 'contact-email', 'contact-phone', 'passenger-names', 'passenger-phone', 'passenger-num', 'when', 'pickup-address', 'dropoff-address', 'vehicle', 'payment-method', 'notes');
     $via = $this->_getParam('via-address');
     $viaToSave = array();
     if (is_array($via)) {
         foreach ($via as $k => $v) {
             if (!empty($v)) {
                 $viaToSave[] = $v;
             }
         }
     }
     $via = json_encode($viaToSave);
     foreach ($availableParams as $paramName) {
         $value = trim($this->getParam($paramName, ''));
         $colName = str_replace('-', '_', $paramName);
         $order->set($colName, $value);
     }
     $order->set('via', $via);
     $order->save();
     $arp->setStatus(AjaxResponse::STATUS_OK);
     $arp->setMessage($sn);
     $this->json($arp);
 }
Example #8
0
 protected function saveEnrol($iAccountId, $iUserId, $aMeals, $sDayEnd)
 {
     $aMealIdsAll = array();
     // get submited meals id
     for ($i = 0; $i < 7; $i++) {
         if (isset($_POST['day' . $i])) {
             $aMealIdsAll = array_merge($aMealIdsAll, $_POST['day' . $i]);
         }
     }
     // wyszukujemy zamowienia
     $aOrderedMeals = array();
     $aOrderIndex = array();
     $oOrder = new Model_Order();
     $oCourse = new Model_Course();
     $iIndex = 0;
     $sDate = '';
     // rozpoczynamy transakcje
     $oOrder->transaction(true);
     // iterujemy po posilkach w danym tyg.
     foreach ($aMeals as $iMealId => $aMeal) {
         // sprawdzamy czy data nie jest starsza niz obecna
         if (strtotime($aMeal['date'] . ' ' . $sDayEnd) < time()) {
             continue;
         }
         // sprawdzamy czy zmieniono danie na obecny dzien
         if ($aMeal['date'] == date('Y-m-d')) {
             // czyscimy tekst paska informacyjnego
             $this->oCurrentUser->propertie('aMealName', '');
         }
         // zmieniamy na kolejny dzien index tablicy $aMealIds
         if ($sDate != $aMeal['date']) {
             // czyscimy zapisy na wszystkie dania tego dnia
             $oOrder->removeEnrols($iAccountId, $iUserId, $aMeal['date']);
             $sDate = $aMeal['date'];
             $iIndex++;
         }
         // sprawdzamy czy posilek z danego tyg zostal wybrany przez uzytkownika
         if (in_array($iMealId, $aMealIdsAll)) {
             // zapisujemy nowy
             $oOrder->reset();
             $oOrder->date = $aMeal['date'];
             $oOrder->user_id = (int) $this->oCurrentUser->user_id;
             $oOrder->meal_id = (int) $iMealId;
             $oOrder->price = (double) $aMeal['price'];
             $oOrder->account_id = (int) $this->oCurrentUser->account_id;
             if (!$oOrder->save()) {
                 throw new Lithium_Exception('catering.saving_failed');
             }
             // sprawdzamy ilosc zamowien na ten dzien
             $iCount = $oOrder->countOrders((int) $this->oCurrentUser->user_id, $aMeal['date']);
             if ($iCount > 1) {
                 $aOrders = $oOrder->getOrdersForDiscount((int) $this->oCurrentUser->user_id, $aMeal['date']);
                 foreach ($aOrders as $aOrder) {
                     $aOrder['discount'] = (int) $aOrder['discount'];
                     // gdy znizka rowna 0 nie ma co przeliczac
                     if ($aOrder['discount']) {
                         $oOrder->reset();
                         $oOrder->order_id = $aOrder['order_id'];
                         $oOrder->price = round($aOrder['price'] * $aOrder['discount'] / 100, 2);
                         if (!$oOrder->save()) {
                             //								throw new Lithium_Exception( 'catering.saving_failed' );
                         }
                     }
                 }
             }
         }
     }
     if (!$oOrder->transaction()) {
         throw new Lithium_Exception('save_meals_failed');
     }
     $aMeta = $this->mTemplate->aMeta;
     $aMeta[] = '<meta http-equiv="refresh" content="1;url=' . $this->mTemplate->anchor() . '" />';
     $this->mTemplate->aMeta = $aMeta;
     $this->mTemplate->content = $this->getLang('save_meals_successfully');
 }
Example #9
0
 /**
  * pay an invoice, renders the paymenthods button, anyone with an ID of an order can pay it, we do not have control
  * @return [type] [description]
  */
 public function action_checkout()
 {
     $order = new Model_Order($this->request->param('id'));
     if ($order->loaded()) {
         //if paid...no way jose
         if ($order->status != Model_Order::STATUS_CREATED) {
             Alert::set(Alert::INFO, __('This order was already paid.'));
             $this->redirect(Route::url('default'));
         }
         //update order based on the price and the amount of
         $days = core::get('featured_days');
         if (is_numeric($days) and ($price = Model_Order::get_featured_price($days)) !== FALSE) {
             $order->amount = $price;
             //get price from config
             $order->featured_days = $days;
             $order->save();
         }
         //template header
         $this->template->title = __('Checkout') . ' ' . Model_Order::product_desc($order->id_product);
         Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Home'))->set_url(Route::url('default')));
         Breadcrumbs::add(Breadcrumb::factory()->set_title($this->template->title));
         Controller::$full_width = TRUE;
         $this->template->bind('content', $content);
         $this->template->content = View::factory('pages/ad/checkout', array('order' => $order));
     } else {
         //throw 404
         throw HTTP_Exception::factory(404, __('Page not found'));
     }
 }
 public function editOrderForDriverAction()
 {
     $sn = $this->_getParam('sn');
     if (empty($sn)) {
         $this->error('nothing to edit');
         return;
     }
     $order = new Model_Order($sn);
     $ofd = new Model_OrderForDriver($sn);
     $ofd->set('sn', $sn);
     $availableParams = array('driver-code', 'vehicle-id', 'contact-name', 'contact-phone', 'pickup-address', 'pickup-coordinates', 'dropoff-address', 'dropoff-coordinates', 'payment-method', 'notes');
     foreach ($availableParams as $paramName) {
         $value = trim($this->_getParam($paramName, ''));
         $colName = str_replace('-', '_', $paramName);
         $ofd->set($colName, $value);
     }
     $when = strtotime(trim($this->_getParam('when', '')));
     $ofd->set('when', $when);
     $via = trim($this->_getParam('via', ''));
     $viaArray = explode("\n", $via);
     foreach ($viaArray as $k => $v) {
         $viaArray[$k] = trim($v);
         if (empty($viaArray[$k])) {
             unset($viaArray[$k]);
         }
     }
     $ofd->set('via', json_encode($viaArray));
     $viaCoordinates = trim($this->_getParam('via-coordinates', ''));
     $viaCoordinatesArray = explode("\n", $viaCoordinates);
     foreach ($viaCoordinatesArray as $k => $v) {
         $viaCoordinatesArray[$k] = trim($v);
         if (empty($viaCoordinatesArray[$k])) {
             unset($viaCoordinatesArray[$k]);
         }
     }
     $ofd->set('via_coordinates', json_encode($viaCoordinatesArray));
     $ofd->save();
     if ($this->_getParam('driver-code', '') && $order->get('status') < Model_Order::STATUS_DRIVER_ASSIGNED) {
         $order->set('status', Model_Order::STATUS_DRIVER_ASSIGNED);
         $order->save();
     }
     $this->_redirect('order-detail?sn=' . $sn);
 }
Example #11
0
 protected function saveOrders($iAccountId, $iUserId, $aMeals)
 {
     $aMealIdsAll = array();
     // get submited meals id
     for ($i = 0; $i < 7; $i++) {
         if (isset($_POST['day' . $i])) {
             $aMealIdsAll = array_merge($aMealIdsAll, $_POST['day' . $i]);
         }
     }
     // wyszukujemy zamowienia
     $aOrderedMeals = array();
     $aOrderIndex = array();
     $oOrder = new Model_Order();
     $oCourse = new Model_Course();
     $iIndex = 0;
     $sDate = '';
     // rozpoczynamy transakcje
     $oOrder->transaction(true);
     // iterujemy po posilkach w danym tyg.
     foreach ($aMeals as $iMealId => $aMeal) {
         // zmieniamy na kolejny dzien index tablicy $aMealIds
         if ($sDate != $aMeal['date']) {
             // czyscimy zapisy na wszystkie dania tego dnia
             $oOrder->removeEnrols($iAccountId, $iUserId, $aMeal['date']);
             $sDate = $aMeal['date'];
             $iIndex++;
         }
         // sprawdzamy czy posilek z danego tyg zostal wybrany przez uzytkownika
         if (in_array($iMealId, $aMealIdsAll)) {
             // zapisujemy nowy
             $oOrder->reset();
             $oOrder->date = $aMeal['date'];
             $oOrder->user_id = (int) $iUserId;
             $oOrder->meal_id = (int) $iMealId;
             $oOrder->price = (double) $aMeal['price'];
             $oOrder->account_id = (int) $iAccountId;
             if (!$oOrder->save()) {
                 throw new Lithium_Exception('Catering.saving_failed');
             }
             // sprawdzamy ilosc zamowien na ten dzien
             $iCount = $oOrder->countOrders((int) $iUserId, $aMeal['date']);
             if ($iCount > 1) {
                 $aOrders = $oOrder->getOrdersForDiscount((int) $iUserId, $aMeal['date']);
                 foreach ($aOrders as $aOrder) {
                     $aOrder['discount'] = (int) $aOrder['discount'];
                     // gdy znizka rowna 0 nie ma co przeliczac
                     if ($aOrder['discount']) {
                         $oOrder->reset();
                         $oOrder->order_id = $aOrder['order_id'];
                         $oOrder->price = round($aOrder['price'] * $aOrder['discount'] / 100, 2);
                         if (!$oOrder->save()) {
                             //								throw new Lithium_Exception( 'catering.saving_failed' );
                         }
                     }
                 }
             }
         }
     }
     if (!$oOrder->transaction()) {
         if (IN_PRODUCTION) {
             return false;
         } else {
             throw new Lithium_Exception('catering.saving_failed');
         }
     }
     return true;
 }
Example #12
0
 /**
  * Tests to ensure that a saved order cannot have new products added to it
  *
  * @return null
  */
 public function test_saved_order_is_fixed()
 {
     // Create a new product first
     $product = new Model_Vendo_Product();
     $product->set_fields(array('name' => 'Unit Test', 'price' => '9.99', 'description' => 'This is a unit test.', 'order' => '1'));
     $product->save();
     $order = new Model_Order();
     $order->user_id = self::$user->id;
     $order->address_id = self::$user->address_id;
     $order->contact_id = self::$contact->id;
     $order->add_product($product, 1);
     $order->save();
     try {
         $order->add_product($product, 1);
     } catch (Exception $e) {
         $this->assertEquals('Saved orders cannot be modified!', $e->getMessage());
         $order->delete();
         $product->delete();
         return;
     }
     $order->delete();
     $product->delete();
     $this->fail('Orders cannot be modified once saved.');
 }