<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Dropshiper extends CI_Controller
{
    public $path_logo = FCPATH.'uploads/dropship/';

    public function __construct()
    {
        parent::__construct();
        // cek_tabel();
        cek_session_login();
        $this->load->model('model_dropshiper', 'dropship');
        $this->dropship->ensureInfoSetting();
        $this->dropship->ensureMenuEntry();

        $this->info = info();
        $this->back = $this->agent->referrer();
        $this->perPage = 10;
        $this->iduser = $this->session->idu;
        $this->level = $this->session->level;
    }

    // List all your items
    /**
     * index.
     *
     * @return void
     */
    public function index()
    {
        cek_menu_akses();
        cek_crud_akses(8);
        $data['title'] = 'Data Dropshipper | '.info()['title'];
        $data['judul'] = 'Dropshipper';

        $this->template->load('main/themes', 'dropship/view_index', $data);
    }

    /**
     * ajaxData.
     *
     * @return void
     */
    public function ajaxData()
    {
        cek_nput_post('GET');
        $page = $this->input->post('page');
        if (!$page) {
            $offset = 0;
        } else {
            $offset = $page;
        }

        $limits = $this->input->post('limits');
        if (!empty($limits)) {
            $limit = $limits;
        } else {
            $limit = $this->perPage;
        }
        $sortBy = $this->input->post('sortBy');
        if (!empty($sortBy)) {
            $conditions['search']['sortBy'] = $sortBy;
        }

        $keywords = $this->input->post('keywords');
        if (!empty($keywords)) {
            $conditions['search']['keywords'] = $keywords;
        }

        // Get record count
        $conditions['returnType'] = 'count';
        $totalRec = $this->dropship->getData($conditions);

        // Pagination configuration
        $config['target'] = '#data_dropship';
        $config['base_url'] = base_url('dropshiper/ajaxData');
        $config['total_rows'] = $totalRec;
        $config['per_page'] = $limit;
        $config['link_func'] = 'search_Dropship';

        // Initialize pagination library
        $this->ajax_pagination->initialize($config);

        // Get records
        $conditions['start'] = $offset;
        $conditions['limit'] = $limit;

        unset($conditions['returnType']);
        $data['result'] = $this->dropship->getData($conditions);

        $conditions['offset'] = $offset;
        // Load the data list view
        $this->load->view('dropship/ajax-cari', $data, false);
    }

    /**
     * cari_dropshiper.
     *
     * @return void
     */
    public function cari_dropshiper()
    {
        // cek_crud_akses(8);
        $data['title'] = '';
        $data['id'] = $this->input->post('id');
        $this->load->view('dropship/cari', $data);
    }

    /**
     * ajax_cari.
     *
     * @return void
     */
    public function ajax_cari()
    {
        cek_nput_post('GET');
        cek_crud_akses(8);
        $data = [];
        if ($_POST['type'] == 'konsumen_cari') {
            $row_num = $_POST['row_num'];
            $name = $_POST['name_startsWith'];
            $like = ['nama_toko' => $name];
            $no_hp = ['telp_toko' => $name];
            $no_telp = ['nama_pemilik' => $name];
            $result = $this->model_app->view_or_like('dropshiper', $like, $no_hp, $no_telp)->result_array();
            if (!empty($result)) {
                foreach ($result as $row) {
                    $nama = $row['nama_toko'];
                    $telp = $row['telp_toko'];
                    $alamat = $row['alamat_toko'];

                    $name = $nama.' - '.$telp.'|'.encrypt_url($row['id']).'|'.$nama.'|'.$alamat.'|'.$row['nama_pemilik'].'|'.$row['id_pelanggan'].'|'.$row_num;
                    array_push($data, $name);
                }
                echo json_encode($data);
            } else {
                array_push($data, 'Data tidak ditemukan');
                echo json_encode($data);
            }
        } else {
            array_push($data, 'Data tidak ditemukan');
            echo json_encode($data);
        }
    }

    public function ajax_cari_pemilik()
    {
        cek_nput_post('GET');
        cek_crud_akses(8);
        $data = [];
        if ($_POST['type'] == 'konsumen_cari') {
            $row_num = $_POST['row_num'];
            $name = $_POST['name_startsWith'];
            $like = ['nama' => $name];
            $no_hp = ['no_hp' => $name];
            $no_telp = ['no_telp' => $name];
            $result = $this->model_app->view_or_like('konsumen', $like, $no_hp, $no_telp)->result_array();
            if (!empty($result)) {
                foreach ($result as $row) {
                    $nama = $row['nama'];
                    $telp = $row['no_hp'];

                    $name = $nama.' - '.$telp.'|'.$nama.'|'.$telp.'|'.$row['id'].'|'.$row_num;
                    array_push($data, $name);
                }
                echo json_encode($data);
            } else {
                array_push($data, 'Data tidak ditemukan');
                echo json_encode($data);
            }
        } else {
            array_push($data, 'Data tidak ditemukan');
            echo json_encode($data);
        }
    }

    /**
     * update_data.
     *
     * @return void
     */
    public function update_data()
    {
        cek_nput_post('GET');
        $id_invoice = $this->input->post('id_invoice');
        $id_invoice = decrypt_url($id_invoice);
        $id_dropship = $this->input->post('id_dropship');
        $id_dropship = decrypt_url($id_dropship);
        $res = $this->model_app->update('invoice', ['id_dropshiper' => $id_dropship], ['id_invoice' => $id_invoice]);
        if ($res == true) {
            $data = ['status' => 200, 'msg' => 'Berhasil'];
        } else {
            $data = ['status' => 400, 'msg' => 'Gagal'];
        }
        $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($data));
    }

    // Add a new item
    /**
     * add.
     *
     * @return void
     */
    public function add()
    {
    }

    // Edit one item
    /**
     * edit.
     *
     * @return void
     */
    public function edit()
    {
        $id = $this->input->post('id');
        $id = decrypt_url($id);
        $edit = $this->model_app->view_where('dropshiper', ['id' => $id]);
        if ($edit->num_rows() > 0) {
            $row = $edit->row();
            $FileLunas = FCPATH.'uploads/dropship/'.$row->logo_lunas;
            $sizel = @getimagesize($FileLunas);
            if ($sizel !== false) {
                $logo_lunas = base_url().'uploads/dropship/'.$row->logo_lunas;
            } else {
                $logo_lunas = base_url().'uploads/no-photo-available.jpg';
            }

            $FileBLunas = FCPATH.'uploads/dropship/'.$row->logo_blunas;
            $sizeb = @getimagesize($FileBLunas);
            if ($sizeb !== false) {
                $logo_blunas = base_url().'uploads/dropship/'.$row->logo_blunas;
            } else {
                $logo_blunas = base_url().'uploads/no-photo-available.jpg';
            }

            $FilesLunas = FCPATH.'uploads/dropship/'.$row->stamp_lunas;
            $sizell = @getimagesize($FilesLunas);
            if ($sizell !== false) {
                $stamp_lunas = base_url().'uploads/dropship/'.$row->stamp_lunas;
            } else {
                $stamp_lunas = base_url().'uploads/no-photo-available.jpg';
            }

            $FilesBLunas = FCPATH.'uploads/dropship/'.$row->stamp_blunas;
            $sizesb = @getimagesize($FilesBLunas);
            if ($sizesb !== false) {
                $stamp_blunas = base_url().'uploads/dropship/'.$row->stamp_blunas;
            } else {
                $stamp_blunas = base_url().'uploads/no-photo-available.jpg';
            }
            $data = [
            'status' => 200,
            'id' => encrypt_url($row->id),
            'toko' => $row->nama_toko,
            'id_pelanggan' => $row->id_pelanggan,
            'pemilik' => $row->nama_pemilik,
            'nama_ttd' => $row->nama_ttd,
            'alamat' => $row->alamat_toko,
            'telp' => $row->telp_toko,
            'email' => $row->email_toko,
            'alamat_surat' => $row->alamat_surat,
            'rekening' => $row->rekening_toko,
            'persen' => $row->persen,
            'logo_lunas' => $logo_lunas,
            'logo_blunas' => $logo_blunas,
            'stamp_lunas' => $stamp_lunas,
            'stamp_blunas' => $stamp_blunas,
            'warna_lunas' => $row->warna_lunas,
            'warna_blunas' => $row->warna_blunas,
            'aktif' => $row->active,
            'msg' => 'sukses',
            ];
        } else {
            $data = ['status' => 400, 'msg' => 'Gagal'];
        }
        $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($data));
    }

    // Update one item
    /**
     * update.
     *
     * @return void
     */
    public function update()
    {
        // dump($_FILES);
        //  dump($_POST);

        if ($this->input->is_ajax_request()) {
            $rules = [
            [
            'field' => 'nama_toko',
            'label' => 'Nama Toko',
            'rules' => 'required|trim|min_length[3]|max_length[100]',
            ],

            [
            'field' => 'nama_pemilik',
            'label' => 'Nama pemilik',
            'rules' => 'required|trim',
            ],

            [
            'field' => 'nama_ttd',
            'label' => 'Nama ttd',
            'rules' => 'required|trim',
            ],

            [
            'field' => 'telp_toko',
            'label' => 'telp toko',
            'rules' => 'required|trim',
            ],

            [
            'field' => 'email_toko',
            'label' => 'Email toko',
            'rules' => 'required|trim',
            ],

            [
            'field' => 'alamat_toko',
            'label' => 'Alamat toko',
            'rules' => 'required|trim',
            ],

            [
            'field' => 'rekening_toko',
            'label' => 'Rekening toko',
            'rules' => 'required|trim',
            ],

            [
            'field' => 'warna_lunas',
            'label' => 'Warna lunas',
            'rules' => 'required|trim',
            ],

            [
            'field' => 'aktif',
            'label' => 'aktif',
            'rules' => 'required|trim',
            ],
            ];

            $this->form_validation->set_rules($rules);

            if ($this->form_validation->run()) {
                $type = xss_filter($this->input->post('type'), 'xss');

                if ($type == 'add') {
                    $logo_lunas = '';
                    $logo_blunas = '';
                    $stamp_lunas = '';
                    $stamp_blunas = '';
                    if (!empty($_FILES['logo']['name'])) {
                        $this->load->library('upload', [
                        'upload_path' => $this->path_logo,
                        'allowed_types' => 'jpg|png|jpeg|webp',
                        'max_size' => 1024 * 10,
                        'encrypt_name' => true,
                        ]);

                        if (!$this->upload->do_upload('logo')) {
                            $error = $this->upload->display_errors();
                        } else {
                            $data = bm_finalize_ci_upload_as_webp($this->upload->data(), 82);
                            if ($data['status']) $logo_lunas = $data['file_name'];
                        }
                    }

                    if (!empty($_FILES['logo_bw']['name'])) {
                        $this->load->library('upload', [
                        'upload_path' => $this->path_logo,
                        'allowed_types' => 'jpg|png|jpeg|webp',
                        'max_size' => 1024 * 10,
                        'encrypt_name' => true,
                        ]);

                        if (!$this->upload->do_upload('logo_bw')) {
                            $error = $this->upload->display_errors();
                        } else {
                            $data = bm_finalize_ci_upload_as_webp($this->upload->data(), 82);
                            if ($data['status']) $logo_blunas = $data['file_name'];
                        }
                    }
                    if (!empty($_FILES['lunas']['name'])) {
                        $this->load->library('upload', [
                        'upload_path' => $this->path_logo,
                        'allowed_types' => 'jpg|png|jpeg|webp',
                        'max_size' => 1024 * 10,
                        'encrypt_name' => true,
                        ]);

                        if (!$this->upload->do_upload('lunas')) {
                            $error = $this->upload->display_errors();
                        } else {
                            $data = bm_finalize_ci_upload_as_webp($this->upload->data(), 82);
                            if ($data['status']) $stamp_lunas = $data['file_name'];
                        }
                    }
                    if (!empty($_FILES['blunas']['name'])) {
                        $this->load->library('upload', [
                        'upload_path' => $this->path_logo,
                        'allowed_types' => 'jpg|png|jpeg|webp',
                        'max_size' => 1024 * 10,
                        'encrypt_name' => true,
                        ]);

                        if (!$this->upload->do_upload('blunas')) {
                            $error = $this->upload->display_errors();
                        } else {
                            $data = bm_finalize_ci_upload_as_webp($this->upload->data(), 82);
                            if ($data['status']) $stamp_blunas = $data['file_name'];
                        }
                    }
                    $data = [
                    'id_pelanggan' => xss_filter($this->input->post('id_pelanggan'), 'xss'),
                    'nama_toko' => xss_filter($this->input->post('nama_toko'), 'xss'),
                    'alamat_toko' => xss_filter($this->input->post('alamat_toko'), 'xss'),
                    'telp_toko' => xss_filter($this->input->post('telp_toko'), 'xss'),
                    'email_toko' => xss_filter($this->input->post('email_toko'), 'xss'),
                    'nama_pemilik' => xss_filter($this->input->post('nama_pemilik'), 'xss'),
                    'nama_ttd' => xss_filter($this->input->post('nama_ttd'), 'xss'),
                    'alamat_surat' => $this->input->post('alamat_surat'),
                    'persen' => $this->input->post('persen'),
                    'logo_lunas' => $logo_lunas,
                    'logo_blunas' => $logo_blunas,
                    'stamp_lunas' => $stamp_lunas,
                    'stamp_blunas' => $stamp_blunas,
                    'warna_lunas' => $this->input->post('warna_lunas'),
                    'warna_blunas' => $this->input->post('warna_blunas'),
                    'rekening_toko' => xss_filter($this->input->post('rekening_toko'), 'xss'),
                    ];
                    $input = $this->model_app->input('dropshiper', $data);

                    if ($input['status'] == true) {
                        $response = ['status' => true, 'tipe' => 'baru', 'title' => 'Simpan Informasi', 'msg' => 'Berhasil disimpan'];
                    } else {
                        $response = ['status' => false, 'title' => 'Simpan Informasi', 'msg' => 'Gagal disimpan'];
                    }
                }

                if ($type == 'edit') {
                    $id = xss_filter($this->input->post('id'), 'xss');
                    $id = decrypt_url($id);
                    $logo_lunas = (!empty($this->dropship->get_logo($id)->logo_lunas) ? $this->dropship->get_logo($id)->logo_lunas : '');
                    // logo lunas
                    if (!empty($_FILES['logo']['name'])) {
                        $this->load->library('upload', [
                        'upload_path' => $this->path_logo,
                        'allowed_types' => 'jpg|png|jpeg|webp',
                        'max_size' => 1024 * 10,
                        'encrypt_name' => true,
                        ]);

                        $opathFile = FCPATH.'uploads/dropship/'.$logo_lunas;

                        $size = @getimagesize($opathFile);
                        if ($size !== false) {
                            $img = $this->path_logo.$logo_lunas;
                            unlink($img);
                        }

                        if (!$this->upload->do_upload('logo')) {
                            $error = $this->upload->display_errors();
                        } else {
                            $data = bm_finalize_ci_upload_as_webp($this->upload->data(), 82);
                            if ($data['status']) $logo_lunas = $data['file_name'];
                        }
                    }
                    $logo_blunas = (!empty($this->dropship->get_logo($id)->logo_blunas) ? $this->dropship->get_logo($id)->logo_blunas : '');
                    // logo lunas
                    if (!empty($_FILES['logo_bw']['name'])) {
                        $this->load->library('upload', [
                        'upload_path' => $this->path_logo,
                        'allowed_types' => 'jpg|png|jpeg|webp',
                        'max_size' => 1024 * 10,
                        'encrypt_name' => true,
                        ]);

                        $opathFile = FCPATH.'uploads/dropship/'.$logo_blunas;

                        $size = @getimagesize($opathFile);
                        if ($size !== false) {
                            $img = $this->path_logo.$logo_blunas;
                            unlink($img);
                        }

                        if (!$this->upload->do_upload('logo_bw')) {
                            $error = $this->upload->display_errors();
                        } else {
                            $data = bm_finalize_ci_upload_as_webp($this->upload->data(), 82);
                            if ($data['status']) $logo_blunas = $data['file_name'];
                        }
                    }
                    $stamp_lunas = (!empty($this->dropship->get_logo($id)->stamp_lunas) ? $this->dropship->get_logo($id)->stamp_lunas : '');
                    // logo lunas
                    if (!empty($_FILES['lunas']['name'])) {
                        $this->load->library('upload', [
                        'upload_path' => $this->path_logo,
                        'allowed_types' => 'jpg|png|jpeg|webp',
                        'max_size' => 1024 * 10,
                        'encrypt_name' => true,
                        ]);

                        $opathFile = FCPATH.'uploads/dropship/'.$stamp_lunas;

                        $size = @getimagesize($opathFile);
                        if ($size !== false) {
                            $img = $this->path_logo.$stamp_lunas;
                            unlink($img);
                        }

                        if (!$this->upload->do_upload('lunas')) {
                            $error = $this->upload->display_errors();
                        } else {
                            $data = bm_finalize_ci_upload_as_webp($this->upload->data(), 82);
                            if ($data['status']) $stamp_lunas = $data['file_name'];
                        }
                    }

                    $stamp_blunas = (!empty($this->dropship->get_logo($id)->stamp_blunas) ? $this->dropship->get_logo($id)->stamp_blunas : '');
                    // logo lunas
                    if (!empty($_FILES['blunas']['name'])) {
                        $this->load->library('upload', [
                        'upload_path' => $this->path_logo,
                        'allowed_types' => 'jpg|png|jpeg|webp',
                        'max_size' => 1024 * 10,
                        'encrypt_name' => true,
                        ]);

                        $opathFile = FCPATH.'uploads/dropship/'.$stamp_blunas;

                        $size = @getimagesize($opathFile);
                        if ($size !== false) {
                            $img = $this->path_logo.$stamp_blunas;
                            unlink($img);
                        }

                        if (!$this->upload->do_upload('blunas')) {
                            $error = $this->upload->display_errors();
                        } else {
                            $data = bm_finalize_ci_upload_as_webp($this->upload->data(), 82);
                            if ($data['status']) $stamp_blunas = $data['file_name'];
                        }
                    }

                    if ($this->dropship->cek_id($id) == 1) {
                        $data = [
                        'id_pelanggan' => xss_filter($this->input->post('id_pelanggan'), 'xss'),
                        'nama_toko' => xss_filter($this->input->post('nama_toko'), 'xss'),
                        'alamat_toko' => xss_filter($this->input->post('alamat_toko'), 'xss'),
                        'telp_toko' => xss_filter($this->input->post('telp_toko'), 'xss'),
                        'email_toko' => xss_filter($this->input->post('email_toko'), 'xss'),
                        'nama_pemilik' => xss_filter($this->input->post('nama_pemilik'), 'xss'),
                        'alamat_surat' => xss_filter($this->input->post('alamat_surat'), 'xss'),
                        'nama_ttd' => xss_filter($this->input->post('nama_ttd'), 'xss'),
                        'rekening_toko' => xss_filter($this->input->post('rekening_toko'), 'xss'),
                        'persen' => xss_filter($this->input->post('persen'), 'xss'),
                        'logo_lunas' => $logo_lunas,
                        'logo_blunas' => $logo_blunas,
                        'stamp_lunas' => $stamp_lunas,
                        'stamp_blunas' => $stamp_blunas,
                        'warna_lunas' => $this->input->post('warna_lunas'),
                        'warna_blunas' => $this->input->post('warna_blunas'),
                        ];
                        $res = $this->model_app->update('dropshiper', $data, ['id' => $id]);
                        if ($res['status'] == 'ok') {
                            $response = ['status' => 200, 'msg' => 'Data berhasil disimpan'];
                        } else {
                            $response = ['status' => 400, 'msg' => 'Data gagal disimpan'];
                        }
                    } else {
                    }
                }
            } else {
            }
        } else {
        }
        $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($response));
    }

    // Delete one item
    /**
     * delete.
     *
     * @return void
     */
    public function delete()
    {
        $id = xss_filter($this->input->post('id'), 'xss');
        $id = decrypt_url($id);
        $cek_id = $this->dropship->cek_id_dropship($id);
        if ($cek_id == false) {
            $res = $this->model_app->hapus('dropshiper', ['id' => $id]);
            if ($res['status'] == 'ok') {
                $data = ['status' => 200, 'msg' => 'Data berhasil dihapus'];
            } else {
                $data = ['status' => 400, 'msg' => 'Data gagal dihapus'];
            }
        } else {
            // code...
            $data = ['status' => 400, 'msg' => 'Data tidak bisa dihapus'];
        }

        $this->output
        ->set_content_type('application/json')
        ->set_output(json_encode($data));
    }

    /**
     * print_invoice_dropship.
     *
     * @return void
     */
    public function print_invoice_dropship($noid = null)
    {
        $noid = decrypt_url($noid);
        $id = ['id_invoice' => $noid];
        $pub = ['slug' => 'in']; // printer aktif
        $cek_printer = $this->model_app->view_where('printer', $pub);
        $rowc = $cek_printer->row_array();

        $search = $this->model_app->view_where('invoice', $id);

        // icon
        $data['waicon'] = ['color' => FCPATH.'assets/img/wa_icon.png', 'bw' => FCPATH.'assets/img/wa_icon_bw.png'];
        $data['mail'] = ['color' => FCPATH.'assets/img/gmail_icon.png', 'bw' => FCPATH.'assets/img/gmail_icon_bw.png'];
        $data['fbicon'] = ['color' => FCPATH.'assets/img/fb_icon.png', 'bw' => FCPATH.'assets/img/fb_icon_bw.png'];
        $data['igicon'] = ['color' => FCPATH.'assets/img/ig_icon.png', 'bw' => FCPATH.'assets/img/ig_icon_bw.png'];

        $data['html'] = 'N';

        if ($search->num_rows() > 0) {
            $this->session->unset_userdata('cart');
            $row = $search->row();
            $jml = $row->cetak + 1;
            $this->model_app->update('tb_users', ['last_invoice' => 0], ['id_user' => $this->session->idu]);
            if ($row->status != 'batal') {
                $this->model_app->update('invoice', ['cetak' => $jml, 'status' => 'simpan', 'pos' => 'Y', 'oto' => 6], ['id_invoice' => $noid]);
            }
            // setting logo dropship

            $rowd = $this->model_app->view_where('dropshiper', ['id' => $row->id_dropshiper])->row_array();

            $data['logo_lunas'] = FCPATH.'uploads/'.info()['logo'];
            $data['logo_blunas'] = FCPATH.'uploads/'.info()['logo_bw'];
            $data['lunas'] = FCPATH.'uploads/'.info()['stamp_l'];
            $data['blunas'] = FCPATH.'uploads/'.info()['stamp_b'];

            if (!empty($rowd['logo_lunas'])) {
                $data['logo_lunas'] = FCPATH.'uploads/dropship/'.$rowd['logo_lunas'];
            }

            if (!empty($rowd['logo_blunas'])) {
                $data['logo_blunas'] = FCPATH.'uploads/dropship/'.$rowd['logo_blunas'];
            }

            if (!empty($rowd['stamp_lunas'])) {
                $data['lunas'] = FCPATH.'uploads/dropship/'.$rowd['stamp_lunas'];
            }

            if (!empty($rowd['stamp_blunas'])) {
                $data['blunas'] = FCPATH.'uploads/dropship/'.$rowd['stamp_blunas'];
            }

            $data['favicon'] = FCPATH.'uploads/'.info()['favicon'];
			$app_info = info();
			$invoice_qr = invoice_qr_context($noid, $app_info['kode_qris']);
			$data['qris'] = $invoice_qr['content'];
			$data['kode_qris'] = $invoice_qr['path'];
			$data['qr_available'] = $invoice_qr['available'];
			$data['qr_is_tracking'] = $invoice_qr['is_tracking'];
			$data['qr_label'] = $invoice_qr['label'];

            // cek sisa
            $_total = 'ROUND(SUM((`invoice_detail`.`jumlah` * harga) - (`invoice_detail`.`jumlah` * harga) * (`invoice_detail`.`diskon`/100))) AS sisa';
            $cari_total = $this->model_app->cek_total('invoice_detail', $_total, ['id_invoice' => $noid]);
            $data['sisanya'] = $cari_total->sisa;

            $data['dropship'] = $rowd;
            $data['cetak'] = $row;
            $data['info'] = info();
            $konsumen = $this->model_app->view_where('konsumen', ['id' => $row->id_konsumen])->row_array();

            if ($konsumen['max_utang'] > 0 and $jml <= 1) {
                $max_utang = $konsumen['max_utang'] - 1;
                $this->model_app->update('konsumen', ['max_utang' => $max_utang], ['id' => $row->id_konsumen]);
            }

            $data['konsumen'] = $konsumen;
            $data['marketing'] = $this->model_app->view_where('tb_users', ['id_user' => $row->id_marketing])->row_array();
            $data['detail'] = $this->model_app->produk_cart(['invoice_detail.id_invoice' => $noid]);
            $data['total'] = $this->model_app->total_bayar(['bayar_invoice_detail.id_invoice' => $noid])->row();
            $select = 'pajak,potongan_harga,cashback, total_bayar AS total, jumlah_bayar, kembalian';
            $where = ['id_invoice' => $noid];
            $tdetail = $this->model_app->cek_total('invoice', $select, $where);
            $data['tdetail'] = $tdetail->total;
            $data['pajak'] = $tdetail->pajak;
            $data['jumlah_bayar'] = $tdetail->jumlah_bayar;
            $data['potongan_harga'] = $tdetail->potongan_harga;
            $data['cashback'] = $tdetail->cashback;
            $data['kembalian'] = $tdetail->kembalian;
            $_diskon = 'SUM(diskon) AS `disc`';
            $cdiskon = $this->model_app->cek_total('invoice_detail', $_diskon, $where);
            $data['cdiskon'] = $cdiskon->disc;
            $_select = 'COUNT(id) AS `jml`';
            $cdetail = $this->model_app->cek_total('bayar_invoice_detail', $_select, $where);
            $data['cdetail'] = $cdetail->jml;
            // bayar detail
            $data['bdetail'] = $this->model_app->view_where('bayar_invoice_detail', $id)->result_array();
            // /
            $data['cara'] = $this->model_app->cara_bayar(['`jenis_bayar`.`publish`' => 'Y', '`bayar_invoice_detail`.`id_invoice`' => $noid]);
            $data['bank'] = $this->model_app->view_where('rekening_bank', ['footer_invoice' => 1, 'publish' => 'Y'])->result();

            $this->load->library('pdf');
            $this->pdf->setPaper($rowc['ukuran_kertas'], $rowc['posisi']);
            $this->pdf->filename = 'invoice_'.$noid.'_'.$row->tgl_trx;
            $data['show_footer'] = $rowc['show_footer'];
            if ($rowc['slug'] == 'dotmatrix' and $rowc['pub'] == 1) {
                $data['font_size'] = $rowc['ukuran_font'];
                $data['font_wight'] = 'bold';
                $this->pdf->load_view('produk/print_invoice_dotmatrix', $data);
            } else {
                $data['font_size'] = $rowc['ukuran_font'];
                $data['font_wight'] = 'normal';
                $this->pdf->load_view('produk/print_invoice_dropship', $data);
                //  $this->load->view('produk/print_invoice_dropship', $data);
            }
        } else {
            $data['cetak'] = 'error';
            $this->load->view('errors/404', $data);
        }
    }
}

/* End of file Controllername.php */
