function do_edit()
 {
     global $ttH;
     $err = "";
     $arr_order_shipping = array();
     $sql = "select shipping_id,title,content  \r\r\n\t\t\t\t\t\tfrom order_shipping  \r\r\n\t\t\t\t\t\twhere is_show=1 \r\r\n\t\t\t\t\t\torder by show_order desc, date_create asc";
     //echo $sql;
     $result = $ttH->db->query($sql);
     $html_row = "";
     while ($row = $ttH->db->fetch_row($result)) {
         $arr_order_shipping[$row['shipping_id']] = $row;
     }
     $arr_order_method = array();
     $sql = "select method_id,title,content  \r\r\n\t\t\t\t\t\tfrom order_method  \r\r\n\t\t\t\t\t\twhere is_show=1 \r\r\n\t\t\t\t\t\torder by show_order desc, date_create asc";
     //echo $sql;
     $result = $ttH->db->query($sql);
     $html_row = "";
     while ($row = $ttH->db->fetch_row($result)) {
         $arr_order_method[$row['method_id']] = $row;
     }
     $dir = create_folder(date("Y_m"));
     $order_id = $ttH->input["id"];
     if (isset($ttH->post['do_submit'])) {
         /*print_arr($ttH->post);
         		die();*/
         if (empty($err)) {
             $col = array();
             $arr_key = array('o_full_name', 'o_email', 'o_phone', 'o_address', 'd_full_name', 'd_email', 'd_phone', 'd_address', 'is_status');
             foreach ($arr_key as $key) {
                 $col[$key] = isset($ttH->post[$key]) ? $ttH->post[$key] : '';
             }
             $col["date_update"] = time();
             $ok = $ttH->db->do_update("product_order", $col, " order_id='" . $order_id . "'");
             if ($ok) {
                 $err = $ttH->html->html_alert($ttH->lang["global"]["edit_success"], "success");
             } else {
                 $err = $ttH->html->html_alert($ttH->lang["global"]["edit_false"], "error");
             }
         }
     }
     $sql = "select * from product_order where order_id='" . $order_id . "'";
     $result = $ttH->db->query($sql);
     if ($data = $ttH->db->fetch_row($result)) {
     }
     $data["err"] = $err;
     $data["link_action"] = $ttH->admin->get_link_admin($this->modules, $this->action, $this->sub, array("id" => $order_id));
     $data["link_up"] = $ttH->admin->get_link_admin('library', 'library', 'popup_library') . '&type=1&folder_up=' . $this->modules . '&fldr=' . $dir . '&editor=mce_0&field_id=picture';
     $data["table_cart"] = $this->table_cart($order_id);
     $data["list_status_order"] = list_status_order('is_status', $data['is_status'], " class=\"form-control\"");
     $data['shipping'] = isset($data['shipping']) && array_key_exists($data['shipping'], $arr_order_shipping) ? $arr_order_shipping[$data['shipping']] : array();
     $data['method'] = isset($data['method']) && array_key_exists($data['method'], $arr_order_method) ? $arr_order_method[$data['method']] : array();
     $ttH->temp_act->assign('data', $data);
     $ttH->temp_act->parse("edit");
     return $ttH->temp_act->text("edit");
 }
 function do_edit()
 {
     global $ttH;
     $err = "";
     $arr_order_shipping = $ttH->load_data->data_table('order_shipping', 'shipping_id', 'shipping_id,title,content', "\tlang='" . $ttH->conf['lang_cur'] . "' order by show_order desc, date_create desc", array(), array('editor' => 'content'));
     $arr_order_method = $ttH->load_data->data_table('order_method', 'method_id', 'method_id,title,content', "\tlang='" . $ttH->conf['lang_cur'] . "' order by show_order desc, date_create desc", array(), array('editor' => 'content'));
     $dir = create_folder(date("Y_m"));
     $order_id = $ttH->input["id"];
     if (isset($ttH->post['do_submit'])) {
         //print_arr($ttH->post);
         //die();
         if (empty($err)) {
             $col = array();
             $arr_key = array('o_full_name', 'o_email', 'o_phone', 'o_address', 'd_full_name', 'd_email', 'd_phone', 'd_address', 'is_status');
             foreach ($arr_key as $key) {
                 $col[$key] = isset($ttH->post[$key]) ? $ttH->post[$key] : '';
             }
             //Update cart detail
             if (isset($ttH->post['detail']) && count($ttH->post['detail']) > 0) {
                 $sql_up = "select * from product_order where order_id='" . $order_id . "'";
                 $result_up = $ttH->db->query($sql_up);
                 if ($data_up_order = $ttH->db->fetch_row($result_up)) {
                     $col['total_order'] = 0;
                     $arr_cart = $ttH->post['detail'];
                     foreach ($ttH->post['detail'] as $cart_k => $cart_v) {
                         $ttH->db->do_update("product_order_detail", $cart_v, " order_id='" . $order_id . "' and detail_id='" . $cart_k . "'");
                         $col['total_order'] += $cart_v['price_buy'] * $cart_v['quantity'];
                     }
                     $col['total_payment'] = $col['total_order'];
                     //promotion_percent
                     if ($data_up_order['promotion_percent'] > 0 && $data_up_order['promotion_percent'] < 100) {
                         $col['total_payment'] = (100 - $data_up_order['promotion_percent']) / 100 * $col['total_payment'];
                     }
                     //end
                     //shipping_price
                     if ($data_up_order['shipping_price'] > 0) {
                         $col['total_payment'] += $data_up_order['shipping_price'];
                     }
                     //End
                     //voucher_amount
                     $sql_voucher = "select * \n\t\t\t\t\t\t\t\t\t\tfrom voucher \n\t\t\t\t\t\t\t\t\t\twhere is_show=1 \n\t\t\t\t\t\t\t\t\t\tand voucher_id='" . $data_up_order['voucher_id'] . "'";
                     //echo $sql;
                     $result_voucher = $ttH->db->query($sql_voucher);
                     if ($row_voucher = $ttH->db->fetch_row($result_voucher)) {
                         $voucher_amount_has = $row_voucher['amount'] - $row_voucher['amount_use'];
                         $row_voucher['amount_use'] -= $data_up_order['voucher_amount'];
                         $data_up_order['voucher_amount'] += $voucher_amount_has;
                         if ($data_up_order['voucher_amount'] >= $col['total_payment']) {
                             $data_up_order['voucher_amount'] = $col['total_payment'];
                             $col['total_payment'] = 0;
                         } else {
                             $col['total_payment'] -= $data_up_order['voucher_amount'];
                         }
                         $col['voucher_amount'] = $data_up_order['voucher_amount'];
                         $row_voucher['amount_use'] += $data_up_order['voucher_amount'];
                         $ttH->db->query("update voucher \n\t\t\t\t\t\t\t\t\t\t\tset amount_use=(" . $row_voucher['amount_use'] . ")\n\t\t\t\t\t\t\t\t\t\t\twhere voucher_id='" . $row_voucher['voucher_id'] . "'");
                         if ($ttH->db->affected()) {
                             //write log
                             $col_log = array();
                             $col_log["voucher_id"] = $row_voucher["voucher_id"];
                             $col_log["order_code"] = $data_up_order["order_code"];
                             $col_log["amount_type"] = 'ordering_edit';
                             $col_log["amount"] = $col["voucher_amount"];
                             $col_log["amount_has"] = $row_voucher["amount"] - $row_voucher["amount_use"];
                             $col_log["date_create"] = time();
                             $ttH->db->do_insert("voucher_history", $col_log);
                             //end
                         }
                     }
                     //end
                 }
             }
             //End update
             if (isset($ttH->post["message_send"])) {
                 $col["message_send"] = 1;
                 $col["message_title"] = $ttH->post["message_title"];
                 $col["message_content"] = $ttH->post["message_content"];
                 $ttH->func->send_mail($ttH->post["message_to"], $col["message_title"], $col["message_content"], $ttH->post["message_from"]);
             }
             $col["date_update"] = time();
             $ok = $ttH->db->do_update("product_order", $col, " order_id='" . $order_id . "'");
             if ($ok) {
                 $err = $ttH->html->html_alert($ttH->lang["global"]["edit_success"], "success");
             } else {
                 $err = $ttH->html->html_alert($ttH->lang["global"]["edit_false"], "error");
             }
         }
     }
     $sql = "select * from product_order where order_id='" . $order_id . "'";
     $result = $ttH->db->query($sql);
     if ($data = $ttH->db->fetch_row($result)) {
         if ($data['is_status'] == 0) {
             if ($data["voucher_amount"] > 0) {
                 // update voucher if is_status = 0
                 $ttH->db->query("update voucher  \n\t\t\t\t\t\t\t\t\t\t\tset amount_use=(amount_use-" . $data["voucher_amount"] . "), \n\t\t\t\t\t\t\t\t\t\t\t\tdate_update=" . time() . "  \n\t\t\t\t\t\t\t\t\t\t\twhere voucher_id='" . $data["voucher_id"] . "'");
                 if ($ttH->db->affected()) {
                     $sql_voucher = "select * from voucher where voucher_id='" . $data["voucher_id"] . "'";
                     $result_voucher = $ttH->db->query($sql_voucher);
                     if ($data_voucher = $ttH->db->fetch_row($result_voucher)) {
                         //write log
                         $col_log = array();
                         $col_log["voucher_id"] = $data_voucher["voucher_id"];
                         $col_log["order_code"] = $data["order_code"];
                         $col_log["amount_type"] = 'ordering_cancel';
                         $col_log["amount"] = $data["voucher_amount"];
                         $col_log["amount_has"] = $data_voucher["amount"] - $data_voucher["amount_use"];
                         $col_log["date_create"] = time();
                         $ttH->db->do_insert("voucher_history", $col_log);
                         //end
                     }
                     $ttH->db->query("update product_order  \n\t\t\t\t\t\t\t\t\t\t\tset voucher_id='', \n\t\t\t\t\t\t\t\t\t\t\t\t\ttotal_payment=(total_payment+voucher_amount), \n\t\t\t\t\t\t\t\t\t\t\t\t\tvoucher_amount=0  \n\t\t\t\t\t\t\t\t\t\t\twhere order_id='" . $data["order_id"] . "'");
                     if ($ttH->db->affected()) {
                         $data["total_payment"] += $data["voucher_amount"];
                         $data["voucher_id"] = '';
                         $data["voucher_amount"] = 0;
                     }
                 }
                 // End
             }
             if ($data["promotion_percent"] > 0) {
                 // update voucher if is_status = 0
                 $ttH->db->query("update promotion  \n\t\t\t\t\t\t\t\t\t\t\tset order_id='', \n\t\t\t\t\t\t\t\t\t\t\t\tdate_update=" . time() . "  \n\t\t\t\t\t\t\t\t\t\t\twhere promotion_id='" . $data["promotion_id"] . "'");
                 if ($ttH->db->affected()) {
                     $ttH->db->query("update product_order  \n\t\t\t\t\t\t\t\t\t\t\tset promotion_id='', \n\t\t\t\t\t\t\t\t\t\t\t\t\ttotal_payment=(total_order+shipping_price), \n\t\t\t\t\t\t\t\t\t\t\t\t\tpromotion_percent=0  \n\t\t\t\t\t\t\t\t\t\t\twhere order_id='" . $data["order_id"] . "'");
                     if ($ttH->db->affected()) {
                         $data["total_payment"] = $data["total_order"] + $data["shipping_price"];
                         $data["promotion_id"] = '';
                         $data["promotion_percent"] = 0;
                     }
                 }
                 // End
             }
         }
         //create_promotion
         if (isset($ttH->post["create_promotion"]) && $data['is_status'] >= 2) {
             $col_v = array();
             $col_v["order_create"] = $data['order_id'];
             $col_v["user_id"] = $data["user_id"];
             $col_v["percent"] = isset($ttH->post["promotion_percent"]) ? $ttH->post["promotion_percent"] : $ttH->setting_voucher["promotion_percent"];
             if ($col_v["percent"] >= 100 || $col_v["percent"] <= 0) {
                 $col_v["percent"] = $ttH->setting_voucher["promotion_percent"];
             }
             $col_v["is_show"] = 1;
             $col_v["date_start"] = time();
             $col_v["date_end"] = time() + $ttH->setting_voucher["promotion_percent"] * 86400;
             $col_v["date_create"] = time();
             $col_v["date_update"] = time();
             for ($j = 0; $j < 10; $j++) {
                 $len = 6 + $j;
                 $col_v['promotion_id'] = $ttH->func->random_str($len, 'un');
                 $ok = $ttH->db->do_insert("promotion", $col_v);
                 if ($ok) {
                     break;
                 }
             }
         }
         //end
         if (isset($ttH->post["send_promotion"]) && $data['is_status'] >= 2) {
             $arr_promotion = $ttH->load_data->data_table('promotion', 'promotion_id', 'promotion_id, percent, date_end', " order_create='" . $order_id . "'");
             //print_arr($row_promotion);die();
             if (is_array($arr_promotion) && count($arr_promotion) > 0) {
                 foreach ($arr_promotion as $row_promotion) {
                     $ttH->func->send_mail_temp('send-promotion', $data["d_email"], $ttH->conf['email'], array('{promotional_code}', '{percent}', '{min_cart_promotion}', '{date_end}'), array($row_promotion["promotion_id"], $row_promotion["percent"], $ttH->func->get_price_format($ttH->setting_voucher["min_cart_promotion"], 0), $ttH->func->get_date_format($row_promotion["date_end"])));
                     break;
                 }
                 if ($data['is_status'] == 2) {
                     $col_up = array();
                     $data['is_status'] = $col_up["is_status"] = 4;
                     $col_up["date_update"] = time();
                     $ttH->db->do_update("product_order", $col_up, " order_id='" . $order_id . "'");
                 }
             }
         }
     }
     $data["err"] = $err;
     $data["link_action"] = $ttH->admin->get_link_admin($this->modules_call, $this->action, $this->sub, array("id" => $order_id));
     $data["link_up"] = $ttH->admin->get_link_admin('library', 'library', 'popup_library') . '&type=1&folder_up=' . $this->modules . '&fldr=' . $dir . '&editor=mce_0&field_id=picture';
     $data["table_cart"] = $this->table_cart($data);
     $data["table_promotion"] = $this->table_promotion($order_id);
     if (!empty($data["table_promotion"])) {
         $ttH->temp_act->assign('data', $data);
         $ttH->temp_act->parse("edit.list_promotion");
     } elseif ($data['is_status'] >= 2) {
         $ttH->temp_act->assign('data', $data);
         $ttH->temp_act->parse("edit.create_promotion");
     }
     if ($data['is_status'] == 0) {
         $status_order_info = status_order_info($data['is_status']);
         $data["list_status_order"] = '<p style="background:' . $status_order_info['background_color'] . '; color:' . $status_order_info['color'] . '">' . $status_order_info['title'] . '</p><input type="hidden" name="is_status" value="' . $data['is_status'] . '" />';
     } else {
         $data["list_status_order"] = list_status_order('is_status', $data['is_status'], " class=\"form-control\"");
     }
     $data['shipping'] = isset($data['shipping']) && array_key_exists($data['shipping'], $arr_order_shipping) ? $arr_order_shipping[$data['shipping']] : array();
     $data['method'] = isset($data['method']) && array_key_exists($data['method'], $arr_order_method) ? $arr_order_method[$data['method']] : array();
     $data['message_send'] = $data['message_send'] == 1 ? 'Đã gửi' : 'Chưa gửi';
     $data["message_from"] = $ttH->conf['email'];
     $data["html_message"] = $ttH->editor->load_editor("message_content", "message_content", $data['message_content'], "", "full", array("folder_up" => $this->modules, "fldr" => $dir));
     $ttH->temp_act->assign('data', $data);
     $ttH->temp_act->parse("edit");
     return $ttH->temp_act->text("edit");
 }