예제 #1
0
 function updateAction()
 {
     if (isset($_POST['ajax'])) {
         if (isset($_POST['attachOrder']) && $this->access->actionAccess('attachOrder')) {
             $orderID = AF::get($_POST, 'order_id');
             $campID = AF::get($_POST, 'camp_id', 0);
             if (!$orderID) {
                 Message::echoJsonError(__('order_attach_no_fields'));
             }
             $order = new Order();
             $order->fillFromDbPk($orderID);
             $o = clone $order;
             $r = Orders::createAttachedOrder($order, $campID, true);
             unset($order);
             unset($o);
             if ($r) {
                 Message::echoJsonSuccess(array('message' => __('order_attach_success'), 'order_id' => $r));
             } else {
                 Message::echoJsonError(__('order_attach_error'));
             }
         }
     }
     /*
     if(isset($_POST['ajax']))
     {
         if( isset($_POST['add_rocket']) && $this->access->actionAccess('rocket') )
         {
     
             $orderID = AF::get($_POST, 'order_id');
             $rocket = AF::get($_POST, 'rocket', 0);
     
             if(!$orderID)
                 Message::echoJsonError(__('order_rocket_no_fields'));
     
             $newRocket = ($rocket) ? 0 : 1;
             $orderModel = new Order();
             $orderModel->order_id = $orderID;
             $orderModel->rocket = $newRocket;
             $orderModel->setIsNewRecord(false);
     
             if($orderModel->save(false))
                 Message::echoJsonSuccess(__('order_rocket_action_'.$newRocket));
             else
                 Message::echoJsonError(__('order_rocket_not_action_'.$newRocket));
         }
     
         if(isset($_POST['update_expiry_date']) && $this->access->actionAccess('update'))
         {
             $expiryMonth = AF::get($_POST,'expiry_month');
             $expiryYear = AF::get($_POST,'expiry_year');
             $orderID = AF::get($_POST,'order_id');
     
             if(!$orderID)
                 Message::echoJsonError(__('order_incorrect_order_id'));
     
             if(!$expiryMonth || (int)$expiryMonth > 12 || !$expiryYear || (int) $expiryYear < date('Y'))
                 Message::echoJsonError(__('order_incorrect_expiry_date'));
     
             $pacnetModel = new OrderPn();
             $pacnetModel->order_id = $orderID;
             $pacnetModel->expiry_date = OrderPn::convertExpiryDateFormat($expiryMonth, $expiryYear);
             $pacnetModel->setIsNewRecord(false);
     
             if($pacnetModel->save(false))
             {
                 OrderLog::createLog($this->user->user_id, $orderID, 16, $pacnetModel->expiry_date);
                 Message::echoJsonSuccess(__('order_updeted_expiry_date'));
             }
             else
                 Message::echoJsonError(__('order_not_updeted_expiry_date'));
         }
     
         if(isset($_POST['update_cc_cvv']) && $this->access->actionAccess('update'))
         {
     
             $ccCvv = AF::get($_POST,'cc_cvv');
             $orderID = AF::get($_POST,'order_id');
     
             if(!$orderID)
                 Message::echoJsonError(__('order_incorrect_order_id'));
     
             if(!$ccCvv || strlen($ccCvv) > 4 || strlen($ccCvv) < 3)
                 Message::echoJsonError(__('order_incorrect_cc_cvv'));
     
             $pacnetModel = new OrderPn();
             $pacnetModel->fillFromDbPk($orderID);
             $arrayNote1 = unserialize(NoteBase64::decode($pacnetModel->note1));
             $arrayNote1['ccc'] = $ccCvv;
             $pacnetModel->note1 = NoteBase64::encode(serialize($arrayNote1));
             $pacnetModel->IsNewRecord = false;
             if($pacnetModel->save(false))
             {
                 OrderLog::createLog($this->user->user_id, $orderID, 17, '***');
                 Message::echoJsonSuccess(__('order_updeted_cc_cvv'));
             }
             else
                 Message::echoJsonSuccess(__('order_not_updeted_cc_cvv'));
     
     
     
         }
     }
     */
     //$model = new Order();
     $id = AF::get($this->params, 'id', FALSE);
     if (!$id) {
         throw new AFHttpException(0, 'no_id');
     }
     if (isset($_POST['set_void']) && $this->access->actionAccess('order_refund')) {
         $orderLogsModel = new OrderLog();
         $orderLogsModel->user_id = $this->user->user_id;
         $orderLogsModel->action = 3;
         if (!$orderLogsModel->canVoid()) {
             $this->redirect();
         }
         $orderModel = new Order();
         $orderModel->fillFromDbPk($id);
         if ($orderModel->status == 'void') {
             $this->redirect();
         }
         $orderModel->status = 'void';
         $orderModel->amount_refunded = '(`amount_product`+`amount_shipping`):sql';
         $orderModel->model_uset_id = $this->user->user_id;
         // remove recurring by all product
         /*
         if($orderModel->recurring == '0' || $orderModel->recurring == -1)
             $saveModel->recurring = 'NULL:sql';
         */
         if ($orderModel->save(false)) {
             //$saveModel->
             $jobModel = new Job();
             $jobModel->order_id = $orderModel->order_id;
             $jobModel->job_type = 'refund';
             $jobModel->addNew();
             //Set void events
             Event::setEvents($orderModel->order_id, 6);
             OrderLog::createLog($this->user->user_id, $id, 3);
         }
     }
     if (isset($_POST['set_shipping_address'])) {
         $orderModel = new Order();
         $orderModel->fillFromDbPk($id);
         $orderModel->address_id = $_POST['shipping_address'];
         $orderModel->save();
     }
     if (isset($_POST['set_billing_address'])) {
         $orderModel = new Order();
         $orderModel->fillFromDbPk($id);
         $orderModel->address_id = $_POST['billing_address'];
         $orderModel->save();
     }
     if (isset($_POST['cancel_retries']) && $this->access->actionAccess('update')) {
         $orderModel = Order::model()->findByPk($id);
         if ($orderModel->cancelRetries($this->user->user_id)) {
             // save success
         } else {
             // failed, now what?
         }
         $this->redirect();
     }
     if (isset($_POST['order_form_cancel_retry_set_recurring_next']) && $this->access->actionAccess('update')) {
         $productID = (int) AF::get($_POST, 'product_id');
         $recurringNext = AF::get($_POST, 'recurring_next');
         $parentOrder = Order::model()->findByPk($id);
         // have to use a different method of Order class population to not overwrite $parentOrder
         $childOrder = new Order();
         $childID = $parentOrder->hasDeclinedRebilled();
         if (!$childID) {
             return false;
         }
         $childOrder->fillFromDbPk($childID);
         if ($childOrder->cancelRetries($this->user->user_id)) {
             // set rebill date
             $orderProduct = new OrderProduct();
             $orderProduct->fillFromDbPks(array('order_id' => $id, 'product_id' => $productID));
             $orderProduct->addFlags('recurring');
             $orderProduct->recurring_next = $recurringNext;
             if ($orderProduct->save()) {
                 OrderLog::createLog($this->user->user_id, $id, 12, $recurringNext);
                 $this->redirect();
             } else {
                 return false;
             }
         } else {
             // failed, now what?
             return false;
         }
     }
     if (isset($_POST['discount_next_retries']) && $this->access->actionAccess('update')) {
         $discount = (int) AF::get($_POST, 'count_discount_next_retries');
         //$productID = (int) AF::get($_POST, 'product_id');
         if ($discount <= 0 || $discount > 100) {
             $this->redirect();
         }
         /*
         $orderModel=new Order();
         $orderModel->fillFromDbPk($id);
         */
         $orderModel = Order::model()->with('campaign')->findByPk($id);
         if ($orderModel) {
             $orderModel->amount_product = $orderModel->amount_product - $orderModel->amount_product * $discount / 100;
             $orderModel->model_uset_id = $this->user->user_id;
             if ($orderModel->save(false)) {
                 OrderLog::createLog($this->user->user_id, $id, 24, 'Discount ' . $discount . '%. New price ' . Currency::getSingPrice($orderModel->amount_product, $orderModel->campaign->currency_id));
             }
         }
         $this->redirect();
         /*
         $orderProduct=new OrderProduct();
         $orderProduct->order_id=$id;
         $orderProduct->product_id=$productID;
         //$orderProduct->fillFromDbPks(array('order_id'=>$id, 'product_id'=>$productID));
         
         $orderProduct->discount_next=$discount;
         if($orderProduct->tempUpdate())
         {
             OrderLog::createLog($this->user->user_id, $id, 24, 'Discount '.$discount.'%. New price '.$saveModel->amount_product);
         }
         */
         /*
         $orderModel = new Order();
         $orderModel->fillFromDbPk($id);
         $saveModel = new Order();
         $saveModel->order_id = $id;
         $saveModel->amount_product = $orderModel->amount_product - ($orderModel->amount_product*$discount/100);
         $saveModel->model_uset_id = $this->user->user_id;
         $saveModel->IsNewRecord = false;
         if($saveModel->save(false))
         {
             OrderLog::createLog($this->user->user_id, $id, 24, 'Discount '.$discount.'%. New price '.$saveModel->amount_product);
         }
         $this->redirect();
         */
     }
     if (isset($_POST['set_returned']) && $this->access->actionAccess('update')) {
         $reason = AF::get($_POST, 'returned_reason');
         if ($this->access->actionAccess('order_refund') && $reason) {
             $orderTempModel = new Order();
             $orderTempModel->fillFromDbPk($id);
             $saveModel = new Order();
             $saveModel->order_id = $id;
             $saveModel->returned = $reason;
             $saveModel->status = 'returned';
             $saveModel->model_uset_id = $this->user->user_id;
             $saveModel->IsNewRecord = false;
             $saveModel->save(false);
             // disable recurring to order products
             $orderProducts = OrderProducts::getProductsByOrderID($saveModel->order_id);
             foreach ($orderProducts as $orderProduct) {
                 $orderProduct->removeFlags('recurring');
                 $orderProduct->tempUpdate();
             }
             Event::setEvents($saveModel->order_id, 7);
             OrderLog::createLog($this->user->user_id, $id, 10, $reason);
         }
         $this->redirect();
     }
     if (isset($_POST['set_flags']) && $this->access->actionAccess('update')) {
         $flags = AF::get($_POST, 'flags', false);
         if (!$flags) {
             $flags = array();
         }
         $orderModel = new Order();
         $orderModel->fillFromDbPk($id);
         $orderModel->removeFlags(array('fraud', 'chargeback'));
         $orderModel->addFlags($flags);
         $orderModel->save(false);
         $customerModel = new Customer();
         $customerModel->fillFromDbPk($orderModel->customer_id);
         $customerModel->removeFlags('blacklisted');
         $customerModel->addFlags($flags);
         $customerModel->save(false);
         // Blacklisting must cancel all recurring
         if ($customerModel->isFlag('blacklisted') || $orderModel->isFlag('chargeback')) {
             Client::chargeBack($orderModel->customer_id);
         }
         OrderLog::createLog($this->user->user_id, $id, 11, implode(',', $flags));
         $this->redirect();
     }
     if (isset($_POST['set_recurring']) && $this->access->actionAccess('update')) {
         $productID = (int) AF::get($_POST, 'product_id');
         $isSet = (int) AF::get($_POST, 'is_set', 0);
         $orderProduct = OrderProduct::model()->find('order_id = :order_id AND product_id = :product_id', array(':order_id' => $id, ':product_id' => $productID));
         if ($isSet) {
             $orderProduct->addFlags('recurring');
             if (!$orderProduct->recurring_next) {
                 $updateDate = true;
             } else {
                 $date = new DateTime($orderProduct->recurring_next);
                 $updateDate = $date->getTimestamp() < time() ? true : false;
             }
             if ($updateDate) {
                 OrderProduct::updateRecurringNext($orderProduct->order_product_id);
             }
         } else {
             $orderProduct->removeFlags('recurring');
         }
         if ($orderProduct->save()) {
             OrderLog::createLog($this->user->user_id, $id, 4, $isSet ? 'yes' : 'no');
         }
         $this->redirect();
     }
     if (isset($_POST['stop_next_recurring']) && $this->access->actionAccess('update')) {
         $stopNextRecurring = AF::get($_POST, 'stop_next_recurring');
         $op_id = AF::get($_POST, 'order_product_id');
         $orderProduct = OrderProduct::model()->findByPk($op_id);
         if ($orderProduct->isFlag('recurring')) {
             if ($stopNextRecurring === '0' || $stopNextRecurring === '-1') {
                 if ($stopNextRecurring === '-1') {
                     $orderProduct->addFlags('stop_next_recurring');
                 } else {
                     $orderProduct->removeFlags('stop_next_recurring');
                 }
                 $orderProduct->save();
                 $notes = $stopNextRecurring === '0' ? 'no' : 'yes';
                 OrderLog::createLog($this->user->user_id, $id, 7, $notes);
             }
         }
         $this->redirect();
     }
     if (isset($_POST['cc_number']) && $this->access->actionAccess('update')) {
         $pacnetModel = new OrderPn();
         //$pacnetModel->fillFromDbPk($id);
         $pacnetModel->order_id = $id;
         $ccNumber = trim(AF::get($_POST, 'cc_number'));
         if ($ccNumber) {
             $pacnetModel->card_number = OrderPn::convertCreditCardNumberFormat($ccNumber);
             $arrayNote1 = unserialize(NoteBase64::decode($pacnetModel->note1));
             $arrayNote1['ccn'] = $ccNumber;
             $pacnetModel->note1 = NoteBase64::encode(serialize($arrayNote1));
             $pacnetModel->IsNewRecord = false;
             $pacnetModel->save(false);
             OrderLog::createLog($this->user->user_id, $id, 14, '**** **** **** ' . $pacnetModel->cc_number_last_four);
         }
         $this->redirect();
     }
     if (isset($_POST['set_recurring_next']) && $this->access->actionAccess('update')) {
         $productID = (int) AF::get($_POST, 'product_id');
         $recurringNext = AF::get($_POST, 'recurring_next');
         $orderProduct = new OrderProduct();
         $orderProduct->order_id = $id;
         $orderProduct->product_id = $productID;
         //$orderProduct->fillFromDbPks(array('order_id'=>$id, 'product_id'=>$productID));
         $orderProduct->recurring_next = $recurringNext;
         if ($orderProduct->tempUpdate()) {
             OrderLog::createLog($this->user->user_id, $id, 12, $recurringNext);
         }
         $this->redirect();
         /*
         $saveModel = new Order();
         $saveModel->order_id = $id;
         $saveModel->recurring_next = AF::get($_POST, 'recurring_next');
         
         if($saveModel->recurring_next)
         {
             $saveModel->model_uset_id = $this->user->user_id;
             $saveModel->IsNewRecord = false;
             $saveModel->save(false);
             OrderLog::createLog($this->user->user_id, $id, 12, $saveModel->recurring_next);
         }
         $this->redirect();
         */
     }
     $model = Order::model()->with('customer', 'campaign', 'affiliate', 'click', 'payment', 'gateway')->restrictions(array('campaign_id', 'aff_id'))->findByPk($id);
     if (!$model) {
         throw new AFHttpException(0, 'incorrect_id');
     }
     if (isset($_POST['amount_refunded']) && $this->access->actionAccess('update')) {
         $amount_refunded = AF::get($_POST, 'amount_refunded', 0);
         $model->refunded($amount_refunded);
         if (!$model->errors) {
             $jobModel = new Job();
             $jobModel->order_id = $model->order_id;
             $jobModel->job_type = 'refund';
             $jobModel->addNew();
             OrderLog::createLog($this->user->user_id, $id, 13, $amount_refunded);
         }
     }
     if (isset($_POST['set_discount_next']) && $this->access->actionAccess('update')) {
         $discount = (int) AF::get($_POST, 'discount_next');
         $productID = (int) AF::get($_POST, 'product_id');
         if ($discount <= 0 || $discount > 100) {
             $this->redirect();
         }
         $orderProduct = new OrderProduct();
         $orderProduct->order_id = $id;
         $orderProduct->product_id = $productID;
         $orderProduct->discount_next = $discount;
         if ($orderProduct->tempUpdate()) {
             OrderLog::createLog($this->user->user_id, $id, 6, $discount);
         }
         $this->redirect();
     }
     /*
     $infoPayment = array(
         'payment_type' => Payments::typeName($model->payment_type),
         'payment_method' => Payments::methodName($model->payment_type,$model->payment_method),
         //'payment_status' => $model->payment_status,
     );
     $addInfoPayment = array();
     if($model->payment_type == 'gc')
     {
         $addInfoPayment = array(
             'bank_code' => $model->bank_code,
             'issuer_id' => $model->issuer_id,
             'bank_name' => $model->bank_name,
             'bank_city' => $model->bank_city,
             'branch_code' => $model->branch_code,
             'checkdigit' => $model->checkdigit,
             'account_number' => $model->account_number,
             'account_holder' => $model->account_holder,
         );
     }
     		elseif($model->payment_type == 'pn')
     {
         $addInfoPayment = array(
             'cc_number' => '<span id="update_order_cc_number">'.$model->card_number.'</span> <a id="a_cc_update" href="javascript:AF.Orders.editCcNumber()"><i class="icon-edit"></i></a>',
             'ccv' => '<span id="update_order_cc_cvv">***</span> <a id="a_cvv_update" href="javascript:AF.Orders.editCcCvv()"><i class="icon-edit"></i></a>',
             'expiry_date' => '<span id="update_order_expiry_date">'.$model->expiry_date_formatted.'</span> <a id="a_expiry_date_update" href="javascript:AF.Orders.editExpiryDate()"><i class="icon-edit"></i></a>',
         );
     }
     $infoPayment = array_merge($infoPayment, $addInfoPayment);
     */
     /*
     $productModel = new Product();
     $productModel->fillFromDbPk($model->product_id);
     
     $recurringTotal = 0;
     
     if( $productModel->product_next_id && ($model->recurring === '0' || $model->recurring === '-1') )
     {
         $nextProductModel = new Product();
         $nextProductModel->fillFromDbPk($productModel->product_next_id);
         $shippingModel = new Shipping();
         $shippingModel->fillFromDbPk($model->shipping_id);
         $recurringTotal = ($nextProductModel->product_price + $shippingModel->amount_subscription);
         $recurringTotal =  number_format( ($recurringTotal - ($recurringTotal*$model->discount_next/100) ), 2);
     }
     
     $recurringTotal = Currency::getSingPrice($recurringTotal, $model->currency_id);
     */
     /*
     $nextProductModel = new Product();
     $nextProductModel->fillFromDbPk($productModel->product_next_id);
     */
     /*
     $msql=SafeMySQL::getInstance();
     $sql="SELECT c.*, a.*
             FROM `orders` as o
             JOIN `customers` as c USING (`customer_id`)
             JOIN `addresses` as a USING (`address_id`)
             WHERE o.`order_id`=?i";
     $allFields=$msql->getRow($sql, $model->order_id);
     fb($allFields);
     */
     $customerAddresses = Address::model()->getExistingAddresses($model->customer_id);
     $orderProducts = $model->getOrderProducts();
     $orderLogsModel = new OrderLog();
     $orderLogsModel->user_id = $this->user->user_id;
     $orderLogsModel->action = 3;
     $canVoid = $orderLogsModel->canVoid();
     $pagination = new Pagination(array('action' => $this->action, 'controller' => $this->controller, 'params' => $this->params));
     //$orderModels = AFActiveDataProvider::models('OrderLog', $this->params, $pagination);
     $orderModels = new OrderLogs('OrderLog');
     $dataProvider = $orderModels->getByOrderID($id);
     $maxAmountChargeback = 0;
     foreach ($dataProvider as $item) {
         if (in_array($item->action, array(29, 30)) && $item->amount) {
             $maxAmountChargeback += $item->amount;
         }
     }
     // we need to do some formatting here
     $paymentCRM = $model->payment->crmFormat();
     // grab attachment campaign info
     $attached = $model->getAttachedOrders();
     $model->user_id_updated = $this->user->user_id;
     $model->updated = 'NOW():sql';
     $modelAttempt = new Attempt();
     $modelAttempt->getLastOrderAttempt($model->order_id, Attempt::DEBIT_TYPE);
     //order children
     $orderChildren = Orders::getChildren($model->order_id);
     $this->addToPageTitle('Update Orders');
     Assets::js('jquery.form');
     Assets::css('jquery-ui');
     Assets::js('//code.jquery.com/ui/1.10.3/jquery-ui.js');
     Assets::js('dateRange/jquery.daterange');
     $this->render('update', array('model' => $model, 'paymentModel' => $paymentCRM, 'orderProducts' => $orderProducts, 'canVoid' => $canVoid, 'dataProvider' => $dataProvider, 'customerAddresses' => $customerAddresses, 'attached' => $attached, 'maxAmountChargeback' => $maxAmountChargeback, 'modelAttempt' => $modelAttempt, 'orderChildren' => $orderChildren));
 }