0

I'm new to programming and I have this problem.

public function create()
    {
        $this->load->language('sale/order');

        $json = array();

        if (!$this->user->hasPermission('modify', 'sale/order')) {
            $json['error'] = $this->language->get('error_permission');
        } elseif (isset($this->request->get['order_id'])) {
            $order_id = $this->request->get['order_id'];

            $this->load->model('sale/order');

            $order_info = $this->model_sale_order->getOrder($order_id);

            if ($order_info && !$order_info['invoice_no']) {
                if (($order_info['payment_firstname'] && $order_info['payment_lastname']) || ($order_info['payment_company'])) {
                    $this->db->query("
                        INSERT INTO `" . DB_PREFIX . "invoice` SET
                        order_id = " . (int)$order_info['order_id'] . ",
                        prefix = '" . $this->db->escape($order_info['invoice_prefix']) . "',
                        no = (SELECT ifnull(max(inv.no), 36552) + 1 FROM `" . DB_PREFIX . "invoice` inv WHERE inv.prefix = '" . $this->db->escape($order_info['invoice_prefix']) . "'),
                        

    date = now(),
                        type = ''
                    ");

                    $invoice_id = $this->db->getLastId();


                    $this->db->query("
                        UPDATE `" . DB_PREFIX . "order` SET
                        invoice_no = (SELECT no FROM `" . DB_PREFIX . "invoice` WHERE invoice_id = '" . $this->db->escape((int)$invoice_id) . "')
                        WHERE order_id = " . (int)$order_id . "
                    ");
                }
            }

            if ($invoice_id) {
                $json['invoice_id'] = $invoice_id;
                $this->get(true);
            } else {
                $json['error'] = $this->language->get('error_action');
            }
        }


        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }

Notice: Undefined variable: invoice_id in /home/vagrant/code/public/admin/controller/sale/invoices.php on line 425{"error":"Warning: Could not complete this action!"}

Calvink933
  • 45
  • 1
  • 6
  • Define `$invoice_id` before your if check, or use `isset` or `!empty` on your `if($invoice_id)` – aynber Jan 24 '22 at 20:41
  • `if ($invoice_id) {` change to: `if (!empty($invoice_id)) {` – K. B. Jan 24 '22 at 23:49
  • I tried, but the script doesn't go on in the get function, i get the error message – Calvink933 Jan 25 '22 at 14:40
  • Of course, because `$invoice_id` never got set. That's how you got here in the first place. Now you need to figure out why that happened. One of the condition in your `if` was not satisfied. – Tim Roberts Jan 26 '22 at 20:45

0 Answers0