<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dstb_clean extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('az_auth');
az_check_auth('dstb_clean');
$this->table = 'dstb_clean';
$this->load->helper('az_crud');
$this->load->library('AZApp');
$this->crud = $this->azapp->add_crud();
$this->role_allow = array('production', 'administrator', '');
$this->role_name = $this->session->userdata('role_name');
}
function check_role() {
if (!in_array($this->session->userdata('role_name'), $this->role_allow)) { show_404(); }
}
public function index(){
$azapp = $this->azapp;
$crud = $this->crud;
$crud->set_column(array(azlang('No'), 'Tanggal', 'Nomor DSTB', 'Kasir', 'Driver', 'Total Bill', 'Total PCS', 'Status', azlang('Action')));
$crud->set_width('10px,,,,,,120px');
$crud->set_id($this->table);
$crud->set_default_url(true);
$crud->set_btn_add(false);
$btn = "<a href='".app_url().'dstb_clean/add'."'><button class='btn btn-primary btn-add-dstb' type='button'><i class='fa fa-plus'></i> Tambah DSTB Bersih</button></a>";
$btn .= '&nbsp;<button class="btn btn-info" type="button" id="btn_scan"><i class="fa fa-qrcode"></i> Scan QR Code DSTB</button>';
if (in_array($this->session->userdata('role_name'), $this->role_allow)) { $crud->set_btn_top_custom($btn); }
$crud = $crud->render();
$azapp->add_content($crud);
$azapp->add_content("<div class='container-scanner hide'><button class='btn btn-danger btn-sm btn-close-scanner' type='button'>".azlang('Close')."</button><br><video class='scanner' id='scanner'></video></div>");
$js = az_add_js('dstb_clean/vjs_dstb_clean_list');
$azapp->add_js($js);
$data_header['title'] = 'DSTB Bersih';
$data_header['breadcrumb'] = array('dstb_clean');
$azapp->set_data_header($data_header);
echo $azapp->render();
}
public function get() {
$crud = $this->crud;
$crud->set_select('iddstb_clean, dstb_date, dstb_code, cashier.name as cashier_name, driver.name as driver_name, total_bill, total_pcs, dstb_clean_status');
$crud->set_select_table('iddstb_clean, dstb_date, dstb_code, cashier_name, driver_name, total_bill, total_pcs, dstb_clean_status');
$crud->set_filter('dstb_date, dstb_code, cashier.name, driver.name, total_bill, total_pcs, dstb_clean_status');
$crud->set_sorting('dstb_date, dstb_code, cashier.name, driver.name, total_bill, total_pcs, dstb_clean_status');
$crud->set_id($this->table);
$crud->add_join_manual('user cashier', 'dstb_clean.idcashier = cashier.iduser', 'left');
$crud->add_join_manual('user driver', 'dstb_clean.iddriver = driver.iduser', 'left');
$crud->add_where("dstb_clean.status > 0");
$crud->set_table($this->table);
$crud->set_edit(false);
if (!aznav('role_delete_dstb_clean')) { $crud->set_delete(false); }
if ($this->role_name == 'driver') { $crud->add_where('dstb_clean.iddriver = "'.$this->session->userdata("iduser").'"'); }
else if($this->role_name == 'cashier') { $crud->add_where('dstb_clean_status in("DIANTAR", "END PROCESS")'); }
if (strlen($this->session->userdata('idoutlet'))> 0) { $crud->add_where('dstb_clean.idoutlet = "'.$this->session->userdata('idoutlet').'"'); }
if (!in_array($this->session->userdata('idrole'), array('', '1'))) { $crud->add_where("dstb_date > (NOW() - INTERVAL 3 DAY)"); }
$crud->set_order_by('dstb_date desc');
$crud->set_custom_style('custom_style');
echo $crud->get_table();
}
function custom_style($key, $value, $data) {
if ($key == 'action') {
$id = azarr($data, 'iddstb_clean');
$btn = "<a href='".app_url().'dstb_clean/qr/'.az_encode_url($id)."' target='_blank'><button class='btn btn-xs btn-default'><i class='fa fa-qrcode'></i> Cetak QR</button></a>";
$btn .= "<a href='".app_url().'dstb_clean/detail/'.az_encode_url($id)."'><button class='btn btn-xs btn-info btn-detail'><i class='fa fa-list'></i> Detail</button></a>";
if ($this->role_name == 'driver') { $btn = "<a href='".app_url().'dstb_clean/detail/'.az_encode_url($id)."/driver'><button class='btn btn-xs btn-warning btn-detail'><i class='fa fa-check'></i> Verifikasi Driver</button></a>"; }
else if($this->role_name == 'cashier') { $btn = "<a href='".app_url().'dstb_clean/detail/'.az_encode_url($id)."/cashier'><button class='btn btn-xs btn-success btn-detail'><i class='fa fa-check'></i> Verifikasi Penerimaan</button></a>"; }
return $btn.$value;
}
else if ($key == 'dstb_clean_status') {
$lbl = 'primary';
if ($value == 'DIANTAR') { $lbl = 'info'; }
else if ($value == 'END PROCESS') { $lbl = 'success'; }
return "<label class='label label-".$lbl."'>".$value."</label>";
}
return $value;
}
function add() {
$this->check_role();
$azapp = $this->azapp;
$data_header['title'] = 'Tambah DSTB Bersih';
$data_header['breadcrumb'] = array('dstb_clean');
$azapp->set_data_header($data_header);
$idoutlet = $this->input->get("idoutlet");
$duedate = $this->input->get("duedate");
if (strlen($this->session->userdata('idoutlet')) > 0) { $idoutlet = $this->session->userdata('idoutlet'); }
// ===== BILL BIASA (tanpa PU aktif) =====
$this->db->where('transaction_group.idoutlet', $idoutlet);
$this->db->where('transaction_group.status', 1);
$this->db->join('customer', 'transaction_group.idcustomer = customer.idcustomer');
$this->db->where("date(duedate) <=", Date('Y-m-d', strtotime($duedate)));
$this->db->where('dstb_status', 'DIPROSES');
$this->db->where('(transaction_group.has_pu_active IS NULL OR transaction_group.has_pu_active = 0)', NULL, FALSE);
$data['data'] = $this->db->get('transaction_group');
$arr_data = array();
foreach ($data['data']->result() as $key => $value) {
$this->db->where('idtransaction_group', $value->idtransaction_group);
$check = $this->db->get('dstb_clean_detail');
if ($check->num_rows() == 0) {
$arr_data[] = array(
'idtransaction_group' => $value->idtransaction_group,
'code' => $value->code,
'customer_name' => $value->customer_name,
'dstb_status' => $value->dstb_status,
'total_pcs' => $value->total_pcs,
'date' => $value->date,
'duedate' => $value->duedate,
'note' => $value->note,
'ready' => isset($value->ready_to_deliver) ? (int)$value->ready_to_deliver : 0,
'has_pu_active' => isset($value->has_pu_active) ? (int)$value->has_pu_active : 0,
);
}
}
$data['arr_data'] = $arr_data;
// ===== BILL DENGAN PU AKTIF =====
$this->db->where('transaction_group.idoutlet', $idoutlet);
$this->db->where('transaction_group.status', 1);
$this->db->join('customer', 'transaction_group.idcustomer = customer.idcustomer');
$this->db->where("date(transaction_group.duedate) <=", Date('Y-m-d', strtotime($duedate)));
$this->db->where('dstb_status', 'DIPROSES');
$this->db->where('transaction_group.has_pu_active', 1);
$pu_bills = $this->db->get('transaction_group');
$arr_pu_bills = array();
foreach ($pu_bills->result() as $key => $value) {
$this->db->where('idtransaction_group', $value->idtransaction_group);
$this->db->where_in('status', array(0, 1));
$pu_count = $this->db->get('dstb_clean_pu')->num_rows();
$this->db->where('idtransaction_group', $value->idtransaction_group);
$check_shipped = $this->db->get('dstb_clean_detail');
$arr_pu_bills[] = array(
'idtransaction_group' => $value->idtransaction_group,
'code' => $value->code,
'customer_name' => $value->customer_name,
'dstb_status' => $value->dstb_status,
'total_pcs' => $value->total_pcs,
'pu_count' => $pu_count,
'sisa_pcs' => max(0, (int)$value->total_pcs - $pu_count),
'already_shipped' => ($check_shipped->num_rows() > 0),
'date' => $value->date,
'duedate' => $value->duedate,
'note' => $value->note,
);
}
$data['arr_pu_bills'] = $arr_pu_bills;
// ===== BARANG PU SIAP KIRIM =====
$arr_pu_ready = array();
if (strlen($idoutlet) > 0 && strlen($duedate) > 0) {
$this->db->select('dstb_clean_pu.*, transaction_group.code as bill_code, customer.customer_name, product.product_name, item.item_code, transaction.trx_note');
$this->db->from('dstb_clean_pu');
$this->db->join('transaction_group', 'dstb_clean_pu.idtransaction_group = transaction_group.idtransaction_group');
$this->db->join('customer', 'transaction_group.idcustomer = customer.idcustomer', 'left');
$this->db->join('transaction', 'dstb_clean_pu.idtransaction = transaction.idtransaction');
$this->db->join('item', 'transaction.iditem = item.iditem', 'left');
$this->db->join('product', 'item.idproduct = product.idproduct', 'left');
$this->db->where_in('dstb_clean_pu.status', array(0, 1));
$this->db->where('transaction_group.idoutlet', $idoutlet);
$this->db->where('dstb_clean_pu.est_finish_date <=', Date('Y-m-d', strtotime($duedate)));
$this->db->where('transaction_group.status', 1);
$this->db->order_by('dstb_clean_pu.status DESC, dstb_clean_pu.est_finish_date ASC, transaction_group.code ASC');
$pu_ready = $this->db->get();
foreach ($pu_ready->result() as $key => $value) {
$arr_pu_ready[] = array(
'iddstb_clean_pu' => $value->iddstb_clean_pu,
'idtransaction_group' => $value->idtransaction_group,
'bill_code' => $value->bill_code,
'customer_name' => $value->customer_name,
'product_name' => $value->product_name,
'item_code' => $value->item_code,
'reason' => $value->reason,
'photo_proof' => $value->photo_proof,
'est_finish_date' => $value->est_finish_date,
'trx_note' => $value->trx_note,
'status' => (int)$value->status,
);
}
}
$data['arr_pu_ready'] = $arr_pu_ready;
// ===== DATA UMUM =====
$this->db->where('status', 1);
$data['outlet'] = $this->db->get('outlet');
$date = $azapp->add_datetime();
$date->set_id('dstb_date');
$date->set_name('dstb_date');
$date->set_value(Date('d-m-Y H:i:s'));
$data['dstb_date'] = $date->render();
$v_duedate = $duedate;
if (strlen($v_duedate) == 0) { $v_duedate = Date('Y-m-d'); }
else { $v_duedate = Date('Y-m-d', strtotime($v_duedate)); }
$data['duedate'] = '<input type="date" class="form-control" id="duedate" name="duedate" value="'.$v_duedate.'" style="width:190px;">';
$this->db->where('idrole', 3);
$this->db->where('status', 1);
if (strlen($idoutlet) > 0) { $this->db->where('idoutlet', $idoutlet); }
$this->db->order_by('name');
$data['drivers'] = $this->db->get('user');
$data['sudah_filter'] = (strlen($idoutlet) > 0 && strlen($duedate) > 0);
$view = $this->load->view('dstb_clean/v_dstb_clean', $data, true);
$azapp->add_content($view);
$js = az_add_js('dstb_clean/vjs_dstb_clean');
$azapp->add_js($js);
$modal_detail = $azapp->add_modal();
$modal_detail->set_id('detail');
$modal_detail->set_modal_title(azlang('Detail'));
$modal_detail->set_modal('');
$azapp->add_content($modal_detail->render());
$vmodal = $this->load->view('dstb_clean/v_modal_pending', '', true);
$modal_detail = $azapp->add_modal();
$modal_detail->set_id('pending');
$modal_detail->set_modal_title('Pending');
$modal_detail->set_modal($vmodal);
$modal_detail->set_action_modal(array('save-pending' => 'Simpan'));
$azapp->add_content($modal_detail->render());
$vmodal_pu = $this->load->view('dstb_clean/v_modal_pu', '', true);
$modal_pu = $azapp->add_modal();
$modal_pu->set_id('modal_pu');
$modal_pu->set_modal_title('Form Proses Ulang (PU)');
$modal_pu->set_modal($vmodal_pu);
$modal_pu->set_action_modal(array('save-pu' => 'Simpan PU'));
$azapp->add_content($modal_pu->render());
echo $azapp->render();
}
function get_pu_items() {
$idtransaction_group = $this->input->post('idtransaction_group');
$err_code = 0; $err_message = ''; $items = array();
if (strlen($idtransaction_group) == 0) { $err_code++; $err_message = 'ID Bill tidak valid'; }
if ($err_code == 0) {
$this->db->select('transaction.idtransaction, product.product_name, item.item_code, transaction.trx_note, transaction.qty');
$this->db->from('transaction');
$this->db->join('item', 'transaction.iditem = item.iditem', 'left');
$this->db->join('product', 'item.idproduct = product.idproduct', 'left');
$this->db->where('transaction.idtransaction_group', $idtransaction_group);
$this->db->where('transaction.status', 1);
$this->db->order_by('transaction.idtransaction ASC');
$result = $this->db->get();
foreach ($result->result() as $key => $value) {
$items[] = array('idtransaction' => $value->idtransaction, 'product_name' => $value->product_name, 'item_code' => $value->item_code, 'trx_note' => $value->trx_note, 'qty' => $value->qty);
}
}
echo json_encode(array('err_code' => $err_code, 'err_message' => $err_message, 'items' => $items));
}
function get_pu_data() {
$idtransaction_group = $this->input->post('idtransaction_group');
$err_code = 0; $err_message = ''; $pu_data = array();
if (strlen($idtransaction_group) == 0) { $err_code++; $err_message = 'ID Bill tidak valid'; }
if ($err_code == 0) {
$this->db->where('idtransaction_group', $idtransaction_group);
$this->db->where('status', 0);
$result = $this->db->get('dstb_clean_pu');
foreach ($result->result() as $key => $value) {
$pu_data[] = array('iddstb_clean_pu' => $value->iddstb_clean_pu, 'idtransaction' => $value->idtransaction, 'reason' => $value->reason, 'photo_proof' => $value->photo_proof, 'est_finish_date' => $value->est_finish_date);
}
}
echo json_encode(array('err_code' => $err_code, 'err_message' => $err_message, 'pu_data' => $pu_data));
}
function get_pu_reasons() {
$this->db->order_by('usage_count', 'desc');
$this->db->limit(10);
$result = $this->db->get('pu_reason_history');
$reasons = array();
foreach ($result->result() as $value) { $reasons[] = $value->reason_text; }
echo json_encode(array('reasons' => $reasons));
}
function save_pu() {
$err_code = 0; $err_message = '';
$idtransaction_group = $this->input->post('idtransaction_group');
$selected_items = $this->input->post('selected_items');
$reason = $this->input->post('reason');
$est_finish_date = $this->input->post('est_finish_date');
if (strlen($idtransaction_group) == 0) { $err_code++; $err_message = 'ID Bill tidak valid. '; }
if (empty($selected_items) || !is_array($selected_items) || count($selected_items) == 0) { $err_code++; $err_message .= 'Harap pilih minimal 1 barang untuk di-PU. '; }
if (strlen($reason) == 0) { $err_code++; $err_message .= 'Alasan penundaan wajib diisi. '; }
if (strlen($est_finish_date) == 0) { $err_code++; $err_message .= 'Tanggal estimasi selesai wajib diisi. '; }
$photo_filename = '';
if ($err_code == 0 && isset($_FILES['photo_proof']['name']) && strlen($_FILES['photo_proof']['name']) > 0) {
$config['upload_path'] = APPPATH . 'assets/pu_proof/';
if (!is_dir($config['upload_path'])) { @mkdir($config['upload_path'], 0755, true); }
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '5000';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('photo_proof')) { $err_code++; $err_message .= 'Gagal upload foto: ' . $this->upload->display_errors('', ''); }
else { $photo_filename = $this->upload->data('file_name'); }
}
if ($err_code == 0) {
$this->db->trans_begin();
$this->db->where('idtransaction_group', $idtransaction_group);
$this->db->where('status', 0);
$this->db->delete('dstb_clean_pu');
foreach ($selected_items as $item_id) {
$arr_pu = array(
'idtransaction_group' => $idtransaction_group,
'idtransaction' => $item_id,
'reason' => $reason,
'photo_proof' => $photo_filename,
'est_finish_date' => Date('Y-m-d', strtotime($est_finish_date)),
'status' => 0,
'created' => Date('Y-m-d H:i:s'),
'createdby' => $this->session->userdata('username'),
);
$this->db->insert('dstb_clean_pu', $arr_pu);
}
$arr_tg = array('has_pu_active' => 1, 'pu_est_finish_date' => Date('Y-m-d', strtotime($est_finish_date)));
$this->db->where('idtransaction_group', $idtransaction_group);
$this->db->update('transaction_group', $arr_tg);
$this->db->where('reason_text', $reason);
$check_reason = $this->db->get('pu_reason_history');
if ($check_reason->num_rows() > 0) {
$this->db->set('usage_count', 'usage_count + 1', FALSE);
$this->db->where('reason_text', $reason);
$this->db->update('pu_reason_history');
} else {
$this->db->insert('pu_reason_history', array('reason_text' => $reason, 'usage_count' => 1));
}
if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); $err_code++; $err_message = 'Gagal menyimpan data PU.'; }
else { $this->db->trans_commit(); }
}
echo json_encode(array('err_code' => $err_code, 'err_message' => $err_message));
}
function delete_pu() {
$err_code = 0; $err_message = '';
$idtransaction_group = $this->input->post('idtransaction_group');
if (strlen($idtransaction_group) == 0) { $err_code++; $err_message = 'ID Bill tidak valid'; }
if ($err_code == 0) {
$this->db->trans_begin();
$this->db->where('idtransaction_group', $idtransaction_group);
$this->db->where('status', 0);
$this->db->delete('dstb_clean_pu');
$arr_tg = array('has_pu_active' => 0, 'pu_est_finish_date' => NULL);
$this->db->where('idtransaction_group', $idtransaction_group);
$this->db->update('transaction_group', $arr_tg);
if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); $err_code++; $err_message = 'Gagal menghapus data PU.'; }
else { $this->db->trans_commit(); }
}
echo json_encode(array('err_code' => $err_code, 'err_message' => $err_message));
}
function get_invoice() {
$this->load->helper('az_core');
$code = $this->input->post('code');
$this->load->library('Laundry');
$data = $this->laundry->invoice($code);
$err_code = azarr($data, 'err_code');
$err_message = azarr($data, 'err_message');
$this->load->helper('az_config');
$data['product_only'] = true;
$view = $this->load->view('dstb_clean/v_detail_product', $data, true);
$return = array('err_code' => $err_code, 'err_message' => $err_message, 'data' => $view);
echo json_encode($return);
}
function save_dstb() {
$err_code = 0; $err_message = '';
$chk = $this->input->post('chk');
$chk_pu = $this->input->post('chk_pu');
$iddriver = $this->input->post("iddriver");
$idoutlet = $this->input->post('idoutlet');
$dstb_date = date('Y-m-d H:i:s');
$dstb_note = $this->input->post('dstb_note');
$sess_idoutlet = $this->session->userdata('idoutlet');
if (strlen($sess_idoutlet) > 0) { $idoutlet = $sess_idoutlet; }
if (strlen($iddriver) == 0) { $err_code++; $err_message = 'Driver harus diisi'; }
if ($err_code == 0 && strlen($dstb_date) == 0) { $err_code++; $err_message = 'Tanggal harus diisi'; }
if ($err_code == 0 && strlen($idoutlet) == 0) { $err_code++; $err_message = 'Outlet harus diisi'; }
$has_bill = (is_array($chk) && count($chk) > 0);
$has_pu = (is_array($chk_pu) && count($chk_pu) > 0);
if ($err_code == 0 && !$has_bill && !$has_pu) { $err_code++; $err_message = 'Harap melakukan checklist pada Bill atau Barang PU'; }
$this->db->trans_begin();
if ($err_code == 0) {
$dstb_number = 1;
$this->db->where('status', 1);
$this->db->order_by('dstb_number', 'desc');
$this->db->where('date(dstb_date)', Date('Y-m-d', strtotime($dstb_date)));
$data_dstb = $this->db->get('dstb_clean', 1);
if ($data_dstb->num_rows() > 0) { $dstb_number = $data_dstb->row()->dstb_number; $dstb_number++; }
$this->db->where('idoutlet', $idoutlet);
$outlet_code = $this->db->get('outlet')->row()->outlet_code;
$dstb_code = $outlet_code.'-'.Date('dmY', strtotime($dstb_date)).'-'.sprintf('%03d', $dstb_number);
$total_bill = 0; $total_pcs = 0;
if ($has_bill) {
$total_bill += count($chk);
foreach ((array) $chk as $key => $value) {
$this->db->where('idtransaction_group', $value);
$tg = $this->db->get('transaction_group');
if ($tg->num_rows() > 0) {
$this->db->where('idtransaction_group', $value);
$this->db->where_in('status', array(0, 1));
$pu_c = $this->db->get('dstb_clean_pu')->num_rows();
$total_pcs += max(0, (int)$tg->row()->total_pcs - $pu_c);
}
}
}
$pu_bill_groups = array();
if ($has_pu) {
foreach ((array) $chk_pu as $pu_id) {
$this->db->where('iddstb_clean_pu', $pu_id);
$pu = $this->db->get('dstb_clean_pu');
if ($pu->num_rows() > 0) {
$tg_id = $pu->row()->idtransaction_group;
if (!in_array($tg_id, $pu_bill_groups)) { $pu_bill_groups[] = $tg_id; }
$total_pcs += 1;
}
}
$total_bill += count($pu_bill_groups);
}
$arr_data = array(
'idoutlet' => $idoutlet,
'idcashier' => $this->session->userdata('iduser'),
'iddriver' => $iddriver,
'dstb_date' => az_crud_date($dstb_date),
'dstb_number' => $dstb_number,
'dstb_code' => $dstb_code,
'total_bill' => $total_bill,
'total_pcs' => $total_pcs,
'dstb_note' => $dstb_note,
'dstb_clean_status' => 'DIPROSES'
);
$save = az_crud_save('', 'dstb_clean', $arr_data);
$iddstb_clean = azarr($save, 'insert_id');
if ($has_bill) {
foreach ((array) $chk as $key => $value) {
az_crud_save('', 'dstb_clean_detail', array('iddstb_clean' => $iddstb_clean, 'idtransaction_group' => $value));
}
}
if ($has_pu) {
foreach ($pu_bill_groups as $tg_id) {
$this->db->where('iddstb_clean', $iddstb_clean);
$this->db->where('idtransaction_group', $tg_id);
$check_detail = $this->db->get('dstb_clean_detail');
if ($check_detail->num_rows() == 0) {
az_crud_save('', 'dstb_clean_detail', array('iddstb_clean' => $iddstb_clean, 'idtransaction_group' => $tg_id));
}
}
foreach ((array) $chk_pu as $pu_id) {
$arr_pu_update = array(
'status' => 2,
'iddstb_clean' => $iddstb_clean,
'shipped_at' => Date('Y-m-d H:i:s'),
'updated' => Date('Y-m-d H:i:s'),
'updatedby' => $this->session->userdata('username'),
);
$this->db->where('iddstb_clean_pu', $pu_id);
$this->db->update('dstb_clean_pu', $arr_pu_update);
}
foreach ($pu_bill_groups as $tg_id) {
$this->db->where('idtransaction_group', $tg_id);
$this->db->where_in('status', array(0, 1));
$remaining_pu = $this->db->get('dstb_clean_pu')->num_rows();
if ($remaining_pu == 0) {
$this->db->where('idtransaction_group', $tg_id);
$this->db->update('transaction_group', array('has_pu_active' => 0, 'pu_est_finish_date' => NULL));
} else {
$this->db->select('MIN(est_finish_date) as min_est');
$this->db->where('idtransaction_group', $tg_id);
$this->db->where_in('status', array(0, 1));
$minrow = $this->db->get('dstb_clean_pu')->row();
if ($minrow && $minrow->min_est) {
$this->db->where('idtransaction_group', $tg_id);
$this->db->update('transaction_group', array('pu_est_finish_date' => $minrow->min_est));
}
}
}
}
}
if ($this->db->trans_status() === FALSE || $err_code > 0) { $this->db->trans_rollback(); }
else { $this->db->trans_commit(); }
$return = array('err_code' => $err_code, 'err_message' => $err_message);
echo json_encode($return);
}
function detail($id, $type = 'cashier') {
$azapp = $this->azapp;
$data_header['title'] = 'Detail DSTB Bersih';
$data_header['breadcrumb'] = array('dstb_clean');
$azapp->set_data_header($data_header);
$db_data = $this->get_db_detail($id);
$data['data'] = $db_data;
$page_type = 'cashier';
if (strlen($type) > 0) { $page_type = $type; }
$page_type = $this->session->userdata('role_name');
$data['page_type'] = $page_type;
$azapp->add_content("<div class='container-scanner hide'><button class='btn btn-danger btn-sm btn-close-scanner' type='button'>".azlang('Close')."</button><br><video class='scanner' id='scanner'></video></div>");
$view = $this->load->view('dstb_clean/v_dstb_detail', $data, true);
$azapp->add_content($view);
$js = az_add_js('dstb_clean/vjs_dstb_detail');
$azapp->add_js($js);
$js = az_add_js('dstb_clean/vjs_dstb_clean');
$azapp->add_js($js);
$modal_detail = $azapp->add_modal();
$modal_detail->set_id('detail');
$modal_detail->set_modal_title(azlang('Detail'));
$modal_detail->set_modal('');
$azapp->add_content($modal_detail->render());
$vmodal = $this->load->view('dstb_clean/v_modal_stock_position', '', true);
$modal_detail = $azapp->add_modal();
$modal_detail->set_id('stock_position');
$modal_detail->set_modal_title('Posisi Stok');
$modal_detail->set_modal($vmodal);
$modal_detail->set_action_modal(array('save-stock-position' => 'Simpan'));
$azapp->add_content($modal_detail->render());
echo $azapp->render();
}
private function get_db_detail($id) {
$id = az_decode_url($id);
$this->db->where('transaction_group.status', 1);
$this->db->where('dstb_clean_detail.iddstb_clean', $id);
$this->db->join('transaction_group', 'dstb_clean_detail.idtransaction_group = transaction_group.idtransaction_group');
$this->db->join('outlet', 'transaction_group.idoutlet = outlet.idoutlet');
$this->db->join('customer', 'transaction_group.idcustomer = customer.idcustomer');
$this->db->join('dstb_clean', 'dstb_clean_detail.iddstb_clean = dstb_clean.iddstb_clean');
$this->db->join('user driver', 'dstb_clean.iddriver = driver.iduser', 'left');
$this->db->join('user cashier', 'dstb_clean.idcashier = cashier.iduser', 'left');
$this->db->select('*, cashier.name as cashier_name, driver.name as driver_name, dstb_clean.total_pcs as total_pcs_dstb, transaction_group.total_pcs');
$data = $this->db->get('dstb_clean_detail');
return $data;
}
function get_db_detail_pending($id) {
$id = az_decode_url($id);
$this->db->where('iddstb_clean', $id);
$data_dstb = $this->db->get('dstb_clean')->row();
$dstb_date = $data_dstb->dstb_date;
$idoutlet = $data_dstb->idoutlet;
$arr_dstb = array();
$this->db->where('iddstb_clean', $id);
$detail = $this->db->get('dstb_clean_detail');
foreach ($detail->result() as $key => $value) { $arr_dstb[] = $value->idtransaction_group; }
$this->db->where('transaction_group.idoutlet', $idoutlet);
$this->db->where('transaction_group.status', 1);
$this->db->join('customer', 'transaction_group.idcustomer = customer.idcustomer');
$this->db->where("date(duedate) <=", $dstb_date);
if (count($arr_dstb) > 0) { $this->db->where_not_in('idtransaction_group', $arr_dstb); }
$this->db->where('dstb_status', 'DIPROSES');
$data = $this->db->get('transaction_group');
return $data;
}
function print_detail() {
$id = $this->input->get('id');
$type = $this->input->get('type');
$data['page_type'] = $type;
$data['data'] = $this->get_db_detail($id);
$data['pending'] = $this->get_db_detail_pending($id);
$this->load->view('dstb_clean/v_print_dstb_detail', $data);
}
function qr($id) {
$id = az_decode_url($id);
$this->db->where('iddstb_clean', $id);
$this->db->join('user driver', 'dstb_clean.iddriver = driver.iduser', 'left');
$this->db->join('user cashier', 'dstb_clean.idcashier = cashier.iduser', 'left');
$this->db->join('outlet', 'dstb_clean.idoutlet = outlet.idoutlet');
$this->db->select('*, cashier.name as cashier_name, driver.name as driver_name');
$data['data'] = $this->db->get('dstb_clean')->row();
$this->load->view('dstb_clean/v_qr', $data);
}
function remove_list() {
$err_code = 0; $err_message = '';
$id = $this->input->post('id');
$id = az_decode_url($id);
$this->db->where('iddstb_clean_detail', $id);
$old_data = $this->db->get('dstb_clean_detail');
$iddstb_clean = $old_data->row()->iddstb_clean;
$this->db->where('iddstb_clean_detail', $id);
$this->db->delete('dstb_clean_detail');
$this->db->where("iddstb_clean", $iddstb_clean);
$arr_bill = $this->db->get('dstb_clean_detail');
$total_bill = $arr_bill->num_rows();
$total_pcs = 0;
foreach ($arr_bill->result() as $key => $value) {
$this->db->where('idtransaction_group', $value->idtransaction_group);
$tg = $this->db->get('transaction_group');
if ($tg->num_rows() > 0) { $total_pcs += $tg->row()->total_pcs; }
}
$arr_tot = array('total_bill' => $total_bill, 'total_pcs' => $total_pcs);
$this->db->where('iddstb_clean', $iddstb_clean);
$this->db->update('dstb_clean', $arr_tot);
echo json_encode(array('err_code' => $err_code, 'err_message' => $err_message));
}
function scan_data() {
$err_code = 0; $err_message = '';
$code = $this->input->post('code');
$type = $this->input->post('type');
$this->db->where('code', $code);
$this->db->where('status', 1);
$tg = $this->db->get('transaction_group');
if ($tg->num_rows() == 0) { $err_code++; $err_message = 'Data tidak ditemukan'; }
else {
$idtransaction_group = $tg->row()->idtransaction_group;
$arr_tg = array();
if ($type == 'driver') {
$arr_tg['dstb_status'] = 'DIANTAR';
}
else if ($type == 'cashier') {
// LOGIKA 3 TINGKAT: cek PU milik bill ini
$this->db->where('idtransaction_group', $idtransaction_group);
$pu_rows = $this->db->get('dstb_clean_pu')->result();
$pu_total   = count($pu_rows);
$pu_pending = 0;
foreach ($pu_rows as $p) {
if (in_array((int)$p->status, array(0, 1))) { $pu_pending++; }
}
if ($pu_total == 0) {
$arr_tg['dstb_status'] = 'END PROCESS';            // bill biasa
}
else if ($pu_pending > 0) {
$arr_tg['dstb_status'] = 'ADA PU';                 // masih ada PU belum dikirim
}
else {
$arr_tg['dstb_status'] = 'END PROCESS LENGKAP';    // PU sudah dikirim semua
}
}
$this->db->where('idtransaction_group', $idtransaction_group);
$this->db->update('transaction_group', $arr_tg);
}
echo json_encode(array('err_code' => $err_code, 'err_message' => $err_message));
}
function verif_driver() {
$err_code = 0;
$err_message = '';
$id = $this->input->post('id');
$id = az_decode_url($id);
$type = $this->session->userdata('role_name');
// Status yang dianggap SUDAH diverifikasi
if ($type == 'driver') {
$ok_status = array('DIANTAR', 'END PROCESS', 'ADA PU', 'END PROCESS LENGKAP');
} else {
$ok_status = array('END PROCESS', 'ADA PU', 'END PROCESS LENGKAP');
}
$this->db->select('transaction_group.code, transaction_group.dstb_status');
$this->db->where('dstb_clean_detail.iddstb_clean', $id);
$this->db->join('transaction_group', 'dstb_clean_detail.idtransaction_group = transaction_group.idtransaction_group');
$this->db->where_not_in('transaction_group.dstb_status', $ok_status);
$data = $this->db->get('dstb_clean_detail');
if ($data->num_rows() > 0) {
$codes = array();
foreach ($data->result() as $r) { $codes[] = $r->code . ' (' . $r->dstb_status . ')'; }
$err_code++;
$err_message = 'Terdapat data yang belum diverifikasi: ' . implode(', ', $codes);
}
if ($err_code == 0) {
if ($type == 'driver') {
$arr_up = array('dstb_clean_status' => 'DIANTAR', 'tgl_diantar' => Date("Y-m-d H:i:s"));
} else if ($type == 'cashier') {
$arr_up = array('dstb_clean_status' => 'END PROCESS', 'tgl_end_process' => Date("Y-m-d H:i:s"));
}
$this->db->where('iddstb_clean', $id);
$this->db->update('dstb_clean', $arr_up);
}
echo json_encode(array('err_code' => $err_code, 'err_message' => $err_message));
}
function get_pending() {
$err_code = 0; $err_message = '';
$id = $this->input->post("id");
$this->db->where("idtransaction_group", $id);
$data = $this->db->get('transaction_group')->row()->pending_description;
echo json_encode(array('err_code' => $err_code, 'err_message' => $err_message, 'data' => $data));
}
function save_pending_description() {
$err_code = 0; $err_message = '';
$id = $this->input->post("idpending");
$description = $this->input->post("pending_description");
$this->db->where("idtransaction_group", $id);
$this->db->update('transaction_group', array('pending_description' => $description));
echo json_encode(array('err_code' => $err_code, 'err_message' => $err_message));
}
function get_stock_position() {
$err_code = 0; $err_message = '';
$id = $this->input->post("id");
$this->db->where("idtransaction_group", $id);
$data = $this->db->get('transaction_group')->row()->stock_position;
echo json_encode(array('err_code' => $err_code, 'err_message' => $err_message, 'data' => $data));
}
function save_stock_position() {
$err_code = 0; $err_message = '';
$id = $this->input->post("idstock_position");
$description = $this->input->post("stock_position");
$this->db->where("idtransaction_group", $id);
$this->db->update('transaction_group', array('stock_position' => $description));
echo json_encode(array('err_code' => $err_code, 'err_message' => $err_message));
}
public function delete() {
$id = $this->input->post("id");
$this->db->where('iddstb_clean', $id);
$this->db->where('status', 2);
$pu_shipped = $this->db->get('dstb_clean_pu');
foreach ($pu_shipped->result() as $pu) {
$this->db->where('iddstb_clean_pu', $pu->iddstb_clean_pu);
$this->db->update('dstb_clean_pu', array('status' => 1, 'iddstb_clean' => NULL, 'shipped_at' => NULL, 'updated' => Date('Y-m-d H:i:s'), 'updatedby' => $this->session->userdata('username')));
$this->db->where('idtransaction_group', $pu->idtransaction_group);
$this->db->update('transaction_group', array('has_pu_active' => 1, 'pu_est_finish_date' => $pu->est_finish_date));
}
az_crud_delete('dstb_clean', $id);
$this->db->where_in('iddstb_clean', $id);
$data = $this->db->get('dstb_clean_detail');
foreach ($data->result() as $key => $value) {
$this->db->where('idtransaction_group', $value->idtransaction_group);
$this->db->update('transaction_group', array('dstb_status' => 'DIPROSES'));
}
$this->db->where_in('iddstb_clean', $id);
$this->db->delete('dstb_clean_detail');
}
function scan($code) {
$this->db->where('dstb_code', $code);
$this->db->where('status', 1);
$data = $this->db->get('dstb_clean');
if ($data->num_rows() == 0) { redirect(app_url().'dstb_clean'); }
else { redirect(app_url().'dstb_clean/detail/'.az_encode_url($data->row()->iddstb_clean)); }
}
}