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

class PesB extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		check_not_login();
		$this->load->model(['Supl_m', 'SsProses_m']);
	}
	public function index()
	{
		$data['invid'] = $this->Supl_m->get();
		$data['invoicea'] = $this->Supl_m->invoice_nobrg();
		$data['getpart'] = $this->SsProses_m->get_part();
		$data['buysparepart'] = $this->Supl_m->buy_sparepart();
		$data['rowpesanbrg'] = $this->Supl_m->get_pesanbrg();

		//	$data['rowindex'] = $this->stokbrg_m->indexstokbrg();
		//	$data['rowa'] = $this->pesanbrg_m->get_pesanbrg();
		$this->template->load('template', 'pesB', $data);
	}

	// input pesan barang tabel t_pesanbrg_cart ok ok ok ok
	public function addpartpesan()
	{
		$post = $this->input->post(null, TRUE);
		$this->Supl_m->addpartpesan($post);
		if ($this->db->affected_rows() > 0) {
			$this->session->set_flashdata('success', 'Data Item Barang yang Dipesan Sudah Di Tambahkan');
			redirect($_SERVER['HTTP_REFERER']);
		}
	}

	public function addpesanbrgdetail()
	{
		$post = $this->input->post(null, TRUE);
		$sale_id = $this->Supl_m->addpesanbrgdetail($post);

		$cart = $this->Supl_m->get_cart()->result();
		$row = [];
		foreach ($cart as $c => $value) {
			array_push(
				$row,
				array(

					'id_pesbrg' => $sale_id,
					'id_katbrgstok' => $value->id_katbrgstok,
					'barcode_brg' => $value->barcode_brg,
					'name_brg' => $value->name_brg,
					'hargam_brg' => $value->hargam_brg,
					'hargam_brgup' => $value->hargam_brg,
					'hargau_brg' => $value->hargau_brg,
					'hargat_brg' =>  $value->hargat_brg,
					'stok_brg' => $value->stok_brg,
					'stok_brgup' => $value->stok_brg,
					'datein' => $value->datein,


				)
			);
		}
		$this->Supl_m->t_pesanbrg_cart_detil($row);

		if ($this->db->affected_rows() > 0)
			// $row = array("success" => true, "sale_id" => $sale_id);
			$this->Supl_m->delall(); {
			$this->session->set_flashdata('success', 'Data Item Barang Sudah Bisa Di Order Ke Supllier');
		}

		redirect($_SERVER['HTTP_REFERER']);
	}




	public function cetakbrg($id)
	{
		$data = array(
			'sale' => $this->Supl_m->getpesansuplier($id)->row(),
			'sale_detail' => $this->Supl_m->get_pesanbrg_detail($id)->result(),
		);
		$this->load->view('cetakbrg', $data);
	}

	//	public function delpesanitem($id)
	//	{
	//		$this->pesanbrg_m->delpesanitem($id);
	//		if ($this->db->affected_rows() > 0) {
	//		$this->session->set_flashdata('success', 'Data Item Perbaikan berhasil Dihapus');
	///		}
	//		redirect($_SERVER['HTTP_REFERER']);
	//	}

	public function delpesananbrg($id)
	{
		$this->Supl_m->delpesananbrg($id);
		if ($this->db->affected_rows() > 0) {
			$this->session->set_flashdata('success', 'Data Item hutang berhasil Dihapus');
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
}
