function invoice_number_set()
{
    Configuration::loadConfiguration();
    $number = 1;
    // Update each order with a number
    $result = Db::getInstance()->ExecuteS('
	SELECT id_order
	FROM ' . _DB_PREFIX_ . 'orders
	ORDER BY id_order');
    foreach ($result as $row) {
        $order = new Order((int) $row['id_order']);
        $history = $order->getHistory(false);
        foreach ($history as $row2) {
            $oS = new OrderState((int) $row2['id_order_state'], (int) Configuration::get('PS_LANG_DEFAULT'));
            if ($oS->invoice) {
                Db::getInstance()->Execute('UPDATE ' . _DB_PREFIX_ . 'orders SET invoice_number = ' . (int) $number++ . ', `invoice_date` = `date_add` WHERE id_order = ' . (int) $order->id);
                break;
            }
        }
    }
    // Add configuration var
    Configuration::updateValue('PS_INVOICE_NUMBER', (int) $number);
}
Example #2
0
function delivery_number_set()
{
    Configuration::loadConfiguration();
    $number = 1;
    // Update each order with a number
    $result = Db::getInstance()->ExecuteS('
	SELECT id_order
	FROM ' . _DB_PREFIX_ . 'orders
	ORDER BY id_order');
    foreach ($result as $row) {
        $order = new Order(intval($row['id_order']));
        $history = $order->getHistory(false);
        foreach ($history as $row2) {
            $oS = new OrderState(intval($row2['id_order_state']));
            if ($oS->delivery) {
                Db::getInstance()->Execute('UPDATE ' . _DB_PREFIX_ . 'orders SET delivery_number = ' . intval($number++) . ', `delivery_date` = `date_add` WHERE id_order = ' . intval($order->id));
                break;
            }
        }
    }
    // Add configuration var
    Configuration::updateValue('PS_DELIVERY_NUMBER', intval($number));
}
 public function execNotification()
 {
     $veritrans = new Veritrans_Config();
     Veritrans_Config::$isProduction = Configuration::get('VT_ENVIRONMENT') == 'production' ? true : false;
     Veritrans_Config::$serverKey = Configuration::get('VT_SERVER_KEY');
     $veritrans_notification = new Veritrans_Notification();
     $history = new OrderHistory();
     $history->id_order = (int) $veritrans_notification->order_id;
     error_log('message notif');
     error_log(print_r($veritrans_notification, TRUE));
     error_log('==============================================');
     // check if order history already been updated to payment success, then save to array $order_history.
     $order_id_notif = (int) $veritrans_notification->order_id;
     $order = new Order($order_id_notif);
     $order_histories = $order->getHistory($this->context->language->id, Configuration::get('VT_PAYMENT_SUCCESS_STATUS_MAP'));
     // if (empty($order_histories))
     // 	error_log("not found in DB");
     // error_log(print_r($order_histories,true));
     // print_r($order_histories,true);
     //Validating order
     //if ($veritrans_notification->isVerified())
     //{
     //$history->id_order = (int)$veritrans_notification->order_id;
     //error_log('notif verified');
     //error_log('message notif: '.(int)$veritrans_notification->order_id);
     if ($veritrans_notification->transaction_status == 'capture') {
         if ($veritrans_notification->fraud_status == 'accept') {
             // if order history !contains payment accepted, then update DB. Else, don't update DB
             if (empty($order_histories)) {
                 $history->changeIdOrderState(Configuration::get('VT_PAYMENT_SUCCESS_STATUS_MAP'), $order_id_notif);
                 echo 'Valid success notification accepted.';
             } else {
                 error_log("########## Transaction has already been updated to success status once, no need to update again");
             }
         } else {
             if ($veritrans_notification->fraud_status == 'challenge') {
                 $history->changeIdOrderState(Configuration::get('VT_PAYMENT_CHALLENGE_STATUS_MAP'), $order_id_notif);
                 echo 'Valid challenge notification accepted.';
             }
         }
     } else {
         if ($veritrans_notification->transaction_status == 'settlement') {
             if ($veritrans_notification->payment_type != 'credit_card') {
                 // if order history !contains payment accepted, then update DB. Else, don't update DB
                 if (empty($order_histories)) {
                     $history->changeIdOrderState(Configuration::get('VT_PAYMENT_SUCCESS_STATUS_MAP'), $order_id_notif);
                     echo 'Valid success notification accepted.';
                 } else {
                     error_log("########## Transaction has already been updated to success status once, no need to update again");
                 }
             } else {
                 echo 'Credit card settlement notification accepted.';
             }
         } else {
             if ($veritrans_notification->transaction_status == 'pending') {
                 $history->changeIdOrderState(Configuration::get('VT_PAYMENT_CHALLENGE_STATUS_MAP'), $order_id_notif);
                 echo 'Pending notification accepted.';
             } else {
                 if ($veritrans_notification->transaction_status == 'cancel') {
                     $history->changeIdOrderState(Configuration::get('VT_PAYMENT_FAILURE_STATUS_MAP'), $order_id_notif);
                     echo 'Pending notification accepted.';
                 } else {
                     if ($veritrans_notification->transaction_status == 'expire') {
                         $history->changeIdOrderState(Configuration::get('VT_PAYMENT_FAILURE_STATUS_MAP'), $order_id_notif);
                         echo 'Expire notification accepted.';
                     } else {
                         $history->changeIdOrderState(Configuration::get('VT_PAYMENT_FAILURE_STATUS_MAP'), $order_id_notif);
                         echo 'Valid failure notification accepted';
                     }
                 }
             }
         }
     }
     try {
         $history->add(true);
     } catch (Exception $e) {
         echo 'Order history not added: ' . $e->getMessage();
         exit;
     }
     //}
     exit;
 }
 public function hookOrderConfirmation($params)
 {
     if (!$this->active) {
         return;
     }
     if (!$this->checkCurrency($params['cart'])) {
         return;
     }
     $order = new Order(Tools::getValue('id_order'));
     $history = $order->getHistory($this->context->cookie->id_lang);
     $history = $history[0];
     $this->context->smarty->assign(array('transaction_status' => $history['id_order_state'], 'cart' => $this->context->cart, 'this_path' => $this->_path, 'this_path_ssl' => Tools::getShopDomainSsl(true, true) . __PS_BASE_URI__ . 'modules/' . $this->name . '/'));
     if (version_compare(Configuration::get('PS_VERSION_DB'), '1.5') == -1) {
         return $this->display(__FILE__, 'views/templates/hook/order_confirmation.tpl');
     } else {
         return $this->display(__FILE__, 'order_confirmation.tpl');
     }
 }
function hipayResetOrderStatus($cart = null)
{
    echo '-fnROS';
    if ($_POST['status'] == '117' || $_POST['status'] == '118') {
        $cart = new Cart((int) $_POST['order']->id);
        if ($cart->orderExists()) {
            $orderState = _PS_OS_PAYMENT_;
            if ($_POST['captured_amount'] < $_POST['authorized_amount']) {
                $orderState = Configuration::get('HIPAY_PARTIALLY_CAPTURED') ? Configuration::get('HIPAY_PARTIALLY_CAPTURED') : HipayClass::getConfiguration('HIPAY_PARTIALLY_CAPTURED');
            }
            // FORCE INVOICE CREATION IF OrderState = _PS_OS_PAYMENT_
            if ($orderState == _PS_OS_PAYMENT_) {
                $order_id = retrieveOrderId($cart->id);
                // Retrieve order id
                $order = new Order((int) $order_id);
                // Recreate order
                $newOS = new OrderState((int) $orderState, $order->id_lang);
                // Emulate the order state _PS_OS_PAYMENT_
                // Uf the order state allows invoice and there is no invoice number, then generate the invoice
                if ($newOS->invoice and !$order->invoice_number) {
                    $order->setInvoice();
                }
            }
        }
    }
    // New modification for status challenged
    // Second check for status 112 -> 117 -> 118
    if ($_POST['status'] == '117') {
        if ((bool) $order->getHistory($context->language->id, Configuration::get('HIPAY_CHALLENGED'))) {
            $cart = new Cart((int) $_POST['order']->id);
            if ($cart->orderExists()) {
                $orderState = _PS_OS_PAYMENT_;
            }
            if ($_POST['captured_amount'] < $_POST['authorized_amount']) {
                $orderState = Configuration::get('HIPAY_PARTIALLY_CAPTURED') ? Configuration::get('HIPAY_PARTIALLY_CAPTURED') : HipayClass::getConfiguration('HIPAY_PARTIALLY_CAPTURED');
            }
            // FORCE INVOICE CREATION IF OrderState = _PS_OS_PAYMENT_
            if ($orderState == _PS_OS_PAYMENT_) {
                $order_id = retrieveOrderId($cart->id);
                // Retrieve order id
                $order = new Order((int) $order_id);
                // Recreate order
                $newOS = new OrderState((int) $orderState, $order->id_lang);
                // Emulate the order state _PS_OS_PAYMENT_
                // Uf the order state allows invoice and there is no invoice number, then generate the invoice
                if ($newOS->invoice and !$order->invoice_number) {
                    $order->setInvoice();
                }
            }
        }
    }
    // Update to minimize risk of simultaneous calls for status 116 and 117
    if ($_POST['status'] == '116') {
        usleep(500000);
        // 0.5sec
        echo '/116';
        // If order exists for cart
        $cart = new Cart((int) $_POST['order']->id);
        if ($cart->orderExists()) {
            echo '/C_OK' . $GLOBALS['_HIPAY_CALLBACK_ORDER_ID_'];
            $context = Context::getContext();
            // Retrieve Order ID
            $order_id = retrieveOrderId($cart->id);
            $order = new Order((int) $order_id);
            echo '/' . (int) $order_id;
            // If current state is paiement accepted or capture partielle
            // then skip the process
            if ((bool) $order->getHistory($context->language->id, _PS_OS_PAYMENT_)) {
                echo '/' . (int) _PS_OS_PAYMENT_ . '_U';
                // Update orders
                $sql_update = "UPDATE `" . _DB_PREFIX_ . "orders`\r\n                    SET `current_state` = '" . _PS_OS_PAYMENT_ . "'\r\n                    WHERE `id_order`='" . (int) $order_id . "'";
                Db::getInstance()->execute($sql_update);
                // Insert into order_history
                $sql_insert = "INSERT INTO `" . _DB_PREFIX_ . "order_history` (`id_employee`, `id_order`, `id_order_state`, `date_add`)\r\n                    VALUES ('0', '" . (int) $order_id . "', '" . _PS_OS_PAYMENT_ . "', now());";
                Db::getInstance()->execute($sql_insert);
            }
            if ((bool) $order->getHistory($context->language->id, Configuration::get('HIPAY_PARTIALLY_CAPTURED'))) {
                echo '/' . (int) Configuration::get('HIPAY_PARTIALLY_CAPTURED') . '_U';
                // Update orders
                $sql_update = "UPDATE `" . _DB_PREFIX_ . "orders`\r\n                    SET `current_state` = '" . Configuration::get('HIPAY_PARTIALLY_CAPTURED') . "'\r\n                    WHERE `id_order`='" . (int) $order_id . "'";
                Db::getInstance()->execute($sql_update);
                // Insert into order_history
                $sql_insert = "INSERT INTO `" . _DB_PREFIX_ . "order_history` (`id_employee`, `id_order`, `id_order_state`, `date_add`)\r\n                    VALUES ('0', '" . (int) $order_id . "', '" . Configuration::get('HIPAY_PARTIALLY_CAPTURED') . "', now());";
                Db::getInstance()->execute($sql_insert);
            }
            HipayLogger::addLog($hipay->l('Callback process', 'hipay'), HipayLogger::NOTICE, 'hipayResetOrderStatus status 116 cart already ok - cid : ' . (int) $_POST['order']->id);
            die;
        } else {
            echo '/C_KO' . $GLOBALS['_HIPAY_CALLBACK_ORDER_ID_'];
        }
    }
    HipayLogger::addLog($hipay->l('Callback process', 'hipay'), HipayLogger::NOTICE, 'hipayResetOrderStatus ended - cid : ' . (int) $_POST['order']->id);
    die;
}
 public static function setOrderStatusDeclined($order_ref, $check = true)
 {
     $oid = self::getOrdersIdFromOrderRef($order_ref);
     if ($oid) {
         $amz_payments = new AmzPayments();
         $new_status = $amz_payments->decline_status_id;
         if ($check) {
             $order = new Order((int) $oid);
             $history = $order->getHistory(Context::getContext()->language->id, $amz_payments->decline_status_id);
             if (sizeof($history) > 0) {
                 return false;
             }
         }
         self::setOrderStatus($oid, $new_status);
     }
 }
Example #7
0
    public function sendReminders()
    {
        $id_order_state_for_reminder = (int) Configuration::get('PS_OS_MF_REMINDER');
        $id_order_state_for_lastreminder = (int) Configuration::get('PS_OS_MF_LASTREMINDER');
        if ($id_order_state_for_reminder == 0 && !Validate::isObjectLoaded(new OrderState((int) $id_order_state_for_reminder))) {
            self::logToFile('cron has been stopped - "Reminder" order state has not been defined. Please check configuration.', 'cron');
        } elseif ($id_order_state_for_lastreminder == 0 && !Validate::isObjectLoaded(new OrderState((int) $id_order_state_for_lastreminder))) {
            self::logToFile('cron has been stopped - "Last Reminder" order state has not been defined. Please check configuration.', 'cron');
        } else {
            $reminder_init_os = array();
            $reminder_days = array();
            $active_euro_shops = array();
            $orders = Db::getInstance()->executeS('SELECT a.`id_order`, a.`id_shop`, a.`current_state`, a.`id_lang`, a.`id_customer`, a.`id_currency`
			FROM ' . _DB_PREFIX_ . 'orders a LEFT JOIN ' . _DB_PREFIX_ . 'mf_claims c ON a.`id_order`=c.`id_order` WHERE c.`id_order` IS NULL');
            foreach ($orders as $order) {
                //check currency
                if (!isset($active_euro_shops[$order['id_shop']])) {
                    $active_euro_shops[$order['id_shop']] = $this->getActiveEuroCurrencyID($order['id_shop']);
                    $active_euro = $active_euro_shops[$order['id_shop']];
                } else {
                    $active_euro = $active_euro_shops[$order['id_shop']];
                }
                //$active_euro = 1; // for testing
                if ((int) $active_euro == 0) {
                    self::logToFile('Order ' . $order['id_order'] . ' - euro is not active for shop', 'cron');
                } elseif ($active_euro != $order['id_currency']) {
                    self::logToFile('Order ' . $order['id_order'] . ' - euro is not currency of order', 'cron');
                } else {
                    //get customer group
                    $id_customer_group = Customer::getDefaultGroupId($order['id_customer']);
                    // *send reminder*
                    //check current order state
                    if (!isset($reminder_init_os[$order['id_shop']])) {
                        $reminder_init_os[$order['id_shop']] = Tools::jsonDecode(Configuration::get('MEDIAFINANZ_REMINDER_INIT_OS', null, null, $order['id_shop']), true);
                        $init_os = $reminder_init_os[$order['id_shop']];
                    } else {
                        $init_os = $reminder_init_os[$order['id_shop']];
                    }
                    if (in_array($order['current_state'], $init_os)) {
                        // get days
                        if (!isset($reminder_days[$order['id_shop']])) {
                            $reminder_days[$order['id_shop']] = Tools::jsonDecode(Configuration::get('MEDIAFINANZ_GROUP_REM', null, null, $order['id_shop']), true);
                            $days = $reminder_days[$order['id_shop']];
                        } else {
                            $days = $reminder_days[$order['id_shop']];
                        }
                        $days_x = (int) $days[(string) $id_customer_group];
                        $order_obj = new Order($order['id_order']);
                        $history_entities = $order_obj->getHistory($order['id_lang'], $order['current_state']);
                        $last_history_entity = end($history_entities);
                        $date_reminder = $last_history_entity['date_add'];
                        $days_between = ceil(abs(strtotime(date('Y-m-d H:i:s')) - strtotime($date_reminder)) / 86400);
                        if ($days_between >= $days_x) {
                            try {
                                if ($this->changeOrderState($order['id_order'], $id_order_state_for_reminder)) {
                                    self::logToFile('Order ' . $order['id_order'] . ' - send reminder - successful - mail has been sent', 'cron');
                                } else {
                                    self::logToFile('Order ' . $order['id_order'] . ' - send reminder - successful - mail has not been sent ', 'cron');
                                }
                            } catch (Exception $e) {
                                self::logToFile('Order ' . $order['id_order'] . ' - send reminder - failed - ' . $e->getMessage(), 'cron');
                            }
                        }
                    }
                    //*send last reminder*
                    if ($order['current_state'] == $id_order_state_for_reminder) {
                        // get days
                        if (!isset($reminder_days[$order['id_shop']])) {
                            $reminder_days[$order['id_shop']] = Tools::jsonDecode(Configuration::get('MEDIAFINANZ_GROUP_LASTREM', null, null, $order['id_shop']), true);
                            $days = $reminder_days[$order['id_shop']];
                        } else {
                            $days = $reminder_days[$order['id_shop']];
                        }
                        $days_y = (int) $days[(string) $id_customer_group];
                        $order_obj = new Order($order['id_order']);
                        $history_entities = $order_obj->getHistory($order['id_lang'], $order['current_state']);
                        $last_history_entity = end($history_entities);
                        $date_reminder = $last_history_entity['date_add'];
                        $days_between = ceil(abs(strtotime(date('Y-m-d H:i:s')) - strtotime($date_reminder)) / 86400);
                        if ($days_between >= $days_y) {
                            try {
                                if ($this->changeOrderState($order['id_order'], $id_order_state_for_lastreminder)) {
                                    self::logToFile('Order ' . $order['id_order'] . ' - send lastreminder - successful - mail has been sent', 'cron');
                                } else {
                                    self::logToFile('Order ' . $order['id_order'] . ' - send lastreminder - successful - mail has not been sent ', 'cron');
                                }
                            } catch (Exception $e) {
                                self::logToFile('Order ' . $order['id_order'] . ' - send lastreminder - failed - ' . $e->getMessage(), 'cron');
                            }
                        }
                    }
                }
            }
        }
    }
    public function renderCreateClaimsList()
    {
        $order_ids = Tools::getValue('order_list');
        if (count($order_ids) > 0) {
            $list = Db::getInstance()->executeS('SELECT a.`id_shop`, a.`id_order`, a.`id_address_invoice`, a.`date_add`, a.`id_customer`,
												a.`total_paid_tax_incl`, a.`id_currency` FROM `' . _DB_PREFIX_ . 'orders` a LEFT JOIN `' . _DB_PREFIX_ . 'orders` o ON (o.`id_order` = a.`id_order` AND o.`id_shop` = a.`id_shop`) LEFT JOIN `' . _DB_PREFIX_ . 'customer` c ON (c.`id_customer` = a.`id_customer`) WHERE a.`id_order` IN (' . implode(', ', array_map('intval', $order_ids)) . ') ' . Shop::addSqlRestriction(Shop::SHARE_ORDER, 'a', 'shop'));
            $claim = Tools::getValue('claim');
            foreach ($list as &$row) {
                $row['accepted_for_claiming'] = true;
                $row['claim']['file_number'] = 0;
                if ($claim_object = MediafinanzClaim::getMediafinanzClaimByOrderId($row['id_order'])) {
                    $row['claim']['file_number'] = (int) $claim_object->file_number;
                    $row['claim']['id'] = (int) $claim_object->id;
                    $row['accepted_for_claiming'] = false;
                }
                $order = new Order($row['id_order']);
                // get Reminder date
                $row['date_reminder'] = '';
                $state_reminder = Configuration::get('PS_OS_MF_REMINDER');
                if ($state_reminder > 0) {
                    $history_entities = $order->getHistory($this->context->language->id, $state_reminder);
                    $last_history_entity = end($history_entities);
                    if ($last_history_entity) {
                        $row['date_reminder'] = $last_history_entity['date_add'];
                    }
                }
                // get Last Reminder date
                $row['date_lastreminder'] = '';
                $state_reminder = Configuration::get('PS_OS_MF_LASTREMINDER');
                if ($state_reminder > 0) {
                    $history_entities = $order->getHistory($this->context->language->id, $state_reminder);
                    $last_history_entity = end($history_entities);
                    if ($last_history_entity) {
                        $row['date_lastreminder'] = $last_history_entity['date_add'];
                    }
                }
                $euro_currency_id = $this->module->getActiveEuroCurrencyID($row['id_shop']);
                // check currency
                if ($euro_currency_id == 0) {
                    $row['accepted_for_claiming'] = false;
                    $row['supported_currency_by_shop'] = false;
                } else {
                    $row['supported_currency_by_shop'] = true;
                    if ($row['id_currency'] == $euro_currency_id) {
                        $row['supported_currency'] = true;
                    } else {
                        $row['accepted_for_claiming'] = false;
                        $row['supported_currency'] = false;
                    }
                }
                $row['claim']['invoice'] = $row['id_order'];
                $row['claim']['type'] = !isset($claim[$row['id_order']]['type']) ? Configuration::get('MEDIAFINANZ_CLAIM_TYPE') : $claim[$row['id_order']]['type'];
                $row['claim']['reason'] = $this->module->getOrderReason($row['id_order']);
                $currency = new Currency($row['id_currency']);
                $row['claim']['display_originalvalue'] = Tools::displayPrice($row['total_paid_tax_incl'], $currency);
                $row['claim']['originalvalue'] = Tools::ps_round($row['total_paid_tax_incl'], 2);
                $row['claim']['overduefees'] = !isset($claim[$row['id_order']]['overduefees']) ? Configuration::get('MEDIAFINANZ_OVERDUEFEES') : $claim[$row['id_order']]['overduefees'];
                $row['claim']['dateoforigin'] = date('Y-m-d', strtotime($row['date_add']));
                //'2015-5-16';
                $row['claim']['dateoflastreminder'] = date('Y-m-d', strtotime($row['date_lastreminder']));
                $row['claim']['note'] = !isset($claim[$row['id_order']]['note']) ? Configuration::get('MEDIAFINANZ_NOTE') : $claim[$row['id_order']]['note'];
                $customer = new Customer($row['id_customer']);
                $address = new Address($row['id_address_invoice']);
                $row['debtor']['id'] = $customer->id;
                if ($address->company != '') {
                    $row['debtor']['address'] = 'c';
                } elseif ($customer->id_gender == 1) {
                    $row['debtor']['address'] = 'm';
                } elseif ($customer->id_gender == 2) {
                    $row['debtor']['address'] = 'f';
                } else {
                    $row['debtor']['address'] = '@';
                }
                $row['debtor']['firstname'] = $address->firstname;
                $row['debtor']['lastname'] = $address->lastname;
                $row['debtor']['company'] = $address->company;
                $row['debtor']['street'] = $address->address1 . ($address->address2 != '' ? ' ' . $address->address2 : '');
                $row['debtor']['postcode'] = $address->postcode;
                $row['debtor']['city'] = $address->city;
                $country = new Country($address->id_country);
                $row['debtor']['country'] = $country->iso_code;
                //$row['debtor']['addressstatus'] = $row['id_customer'];
                $row['debtor']['telephone1'] = $address->phone;
                $row['debtor']['telephone2'] = $address->phone_mobile;
                //$row['debtor']['fax'] = $row['id_customer'];
                $row['debtor']['email'] = $customer->email;
                //$row['debtor']['dateofbirth'] = $row['id_customer'];
                //$row['debtor']['deliveryaddress'] = $row['id_customer'];
                $row['configuration_completed'] = (int) $this->module->isModuleConfigurationCompleted($order->id_shop);
                $row['id_shop'] = (int) $order->id_shop;
                $row['mode_for_shop'] = $this->module->getCurrentModeTitle($order->id_shop);
            }
        }
        $this->context->smarty->assign('currency', new Currency($this->module->getActiveEuroCurrencyID(Context::getContext()->shop->id)));
        $this->context->smarty->assign('claim_types', $this->module->getClaimTypes());
        $this->context->smarty->assign('createclaims_data', $list);
        $this->setTemplate('createclaims-list' . (_PS_VERSION_ < '1.6.0.0' ? '_15' : '') . '.tpl');
    }