<style>
.c1 .textbox-text{
		color: #666;
	}
  .textbox-readonly .textbox-text{
      background: #96281b;
			color : white;
  }
</style>

<script type="text/javascript">
	
	var url;
	
	function editForm() {
		$('#po_no').combogrid('readonly');
		$('#po_nomc').combogrid('readonly');
		$('#currency').combogrid('readonly');
		// $('#kurs').combogrid('readonly');
		$('#scraf_no').combogrid('readonly');
		$('#volume').combogrid('readonly');
		$('#tot_qty').combogrid('readonly');
	}

	function addForm() {
		$('#po_no').combogrid('readonly',false);
		$('#po_nomc').combogrid('readonly',false);
		$('#currency').combogrid('readonly',false);
		// $('#kurs').combogrid('readonly',false);
		$('#scraf_no').combogrid('readonly',false);
		$('#volume').combogrid('readonly',false);
		$('#tot_qty').combogrid('readonly',false);
	}

	function setDgHdr() {
		if(document.URL.indexOf("scin") >= 0){ 
		$('#dg_hdr').datagrid({
			title: "Header",
			toolbar: "#toolbar_hdr",
			fit: true,
			fitColumns: true,
			singleSelect: true,
			rownumbers: true,
			pagination: true,
			frozenColumns:[[  
				{field:'matin_id',title:'ID.'},
				{field:'matin_no',title:'Incoming No.'},
				{field:'Fmatin_date',title:'Incoming Date'},
				{field:'matin_name',title:'Incoming Type'},
			]],
			columns:[[  
				{field:'po_no',title:'PO No'},
				{field:'partial',title:'Partial',align:"right"},
				{field:'proses',title:'Proses'},
				{field:'NmPrshn',title:'Shipper'},
				{field:'supl_do',title:'Dok No'},
				{field:'supl_inv',title:'Inv No'},
				{field:'currency',title:'Currency'},
				{field:'kurs',title:'Kurs'},
				{field:'UrJnsDok',title:'Jenis BC'},
				{field:'NoDaf',title:'No. Dok. Pabean'},
				{field:'FTgDaf',title:'Tgl. Dok. Pabean'},
				{field:'NoAju',title:'No. Pengajuan'},
				{field:'FTgAju',title:'Tgl. Pengajuan'},
				{field:'tot_volume',title:'Total Volume',align:"right"},
				{field:'tot_qty',title:'Total Qty',align:"right"},
				{field:'packing',title:'Kemasan'},
				{field:'created_journal',title:'Created Journal'},
				{field:'transfer_ceisa',title:'Transfer Ceisa'}
			]],
			url: '<?= site_url($this->modul) ?>/dataHdr',
			onSelect: function(index, row) {
				//console.log(row);
				setDgDetUrl(row.matin_id);
			}
		});
	}else {
		$('#dg_hdr').datagrid({
			title: "Header",
			toolbar: "#toolbar_hdr",
			fit: true,
			fitColumns: true,
			singleSelect: true,
			rownumbers: true,
			pagination: true,
			frozenColumns:[[  
				{field:'matin_id',title:'ID.'},
				{field:'matin_no',title:'Incoming No.'},
				{field:'Fmatin_date',title:'Incoming Date'},
				{field:'matin_name',title:'Incoming Type'},
				{field:'po_no',title:'PO No'},
				{field:'partial',title:'Partial',align:"right"},
			]],
			columns:[[  
				{field:'proses',title:'Proses'},
				{field:'NmPrshn',title:'Shipper'},
				{field:'supl_do',title:'Dok No'},
				{field:'supl_inv',title:'Inv No'},
				{field:'currency',title:'Currency'},
				{field:'kurs',title:'Kurs'},
				{field:'UrJnsDok',title:'Jenis BC'},
				{field:'NoDaf',title:'No. Dok. Pabean'},
				{field:'FTgDaf',title:'Tgl. Dok. Pabean'},
				{field:'NoAju',title:'No. Pengajuan'},
				{field:'FTgAju',title:'Tgl. Pengajuan'},
				{field:'tot_amount',title:'Total Amount',align:"right"},
				{field:'tot_qty',title:'Total Qty',align:"right"},
				{field:'packing',title:'Kemasan'},
				{field:'created_journal',title:'Created Journal'},
				{field:'transfer_ceisa',title:'Transfer Ceisa'}
			]],
			url: '<?= site_url($this->modul) ?>/dataHdr',
			onSelect: function(index, row) {
				//console.log(row);
				setDgDetUrl(row.matin_id);
			}
		});
	}
	}
	function setDgDet() {
		if(document.URL.indexOf("matin") >= 0 || document.URL.indexOf("fgin_subkon") >= 0 || document.URL.indexOf("macin") >= 0){ 
		$('#dg_det').datagrid({
			title: "Detail",
			toolbar: "#toolbar_det",
			fit: true,
			fitColumns: true,
			singleSelect: true,
			rownumbers: true,
			pagination: true,
			frozenColumns:[[  
				{field:'child_no',title:'Child No.'},
				{field:'mat_id',title:'Item Code.'},
				{field:'PartNo',title:'Size'},
				{field:'NmBarang',title:'Item Description'},
			]],
			columns:[[  
				{field:'Sat',title:'UOM'},
				{field:'POqty',title:'PO Qty',align:"right"},
				{field:'Fqty',title:'Qty',align:"right"},
				{field:'NGqty',title:'NG Qty',align:"right"},
				{field:'Dqty',title:'In Qty',align:"right"},
				{field:'adj_qty',title:'Adjustment',align:"right",hidden: true},
				{field:'Bqty',title:'Balance',align:"right"},
				{field:'Fprice',title:'Price',align:"right"},
				{field:'amount',title:'Amount',align:"right"},
				{field:'has_edited',title:'Has Edited'},
				{field:'all_in',title:'All In'},
				{field:'locked',title:'Locked'}
			]],
			});
		}else if(document.URL.indexOf("scin") >= 0){ 
		$('#dg_det').datagrid({
			title: "Detail",
			toolbar: "#toolbar_det",
			fit: true,
			fitColumns: true,
			singleSelect: true,
			rownumbers: true,
			pagination: true,
			frozenColumns:[[  
				{field:'child_no',title:'Child No.',hidden: true},
				{field:'mat_id',title:'Item Code.'},
				{field:'PartNo',title:'Size'},
				{field:'NmBarang',title:'Item Description'},
			]],
			columns:[[  
				{field:'Sat',title:'UOM'},
				{field:'POqty',title:'PO Qty',align:"right",hidden: true},
				{field:'volume',title:'Volume',align:"right"},
				{field:'Fqty',title:'Qty',align:"right"},
				{field:'NGqty',title:'NG Qty',align:"right"},
				{field:'Dqty',title:'In Qty',align:"right",hidden: true},
				{field:'adj_qty',title:'Adjustment',align:"right",hidden: true},
				{field:'Bqty',title:'Balance',align:"right",hidden: true},
				{field:'Fprice',title:'Price',align:"right"},
				{field:'amount',title:'Amount',align:"right"},
				{field:'has_edited',title:'Has Edited',hidden: true},
				{field:'all_in',title:'All In',hidden: true},
				{field:'locked',title:'Locked',hidden: true}
			]],
			});
		} else {
			$('#dg_det').datagrid({
			title: "Detail",
			toolbar: "#toolbar_det",
			fit: true,
			fitColumns: true,
			singleSelect: true,
			rownumbers: true,
			pagination: true,
			frozenColumns:[[  
				{field:'child_no',title:'Child No.',hidden: true},
				{field:'mat_id',title:'Item Code.'},
				{field:'PartNo',title:'Size'},
				{field:'NmBarang',title:'Item Description'},
			]],
			columns:[[  
				{field:'Sat',title:'UOM'},
				{field:'POqty',title:'PO Qty',align:"right",hidden: true},
				{field:'Fqty',title:'Qty',align:"right"},
				{field:'NGqty',title:'NG Qty',align:"right"},
				{field:'Dqty',title:'In Qty',align:"right",hidden: true},
				{field:'adjusment',title:'Adjustment',align:"right",hidden: true},
				{field:'Bqty',title:'Balance',align:"right",hidden: true},
				{field:'Fprice',title:'Price',align:"right"},
				{field:'amount',title:'Amount',align:"right"},
				{field:'has_edited',title:'Has Edited',hidden: true},
				{field:'all_in',title:'All In',hidden: true},
				{field:'locked',title:'Locked',hidden: true}
			]],
		});
	}
}

	function setDgHdrDet() {
		$('#dg_hdrdet').datagrid({
			title: "Header",
			toolbar: "#toolbar_hdrdet",
			fit: true,
			fitColumns: true,
			singleSelect: true,
			rownumbers: true,
			pagination: true,
			columns: [
				[{
						field: 'matin_id',
						title: 'ID',
						width: 80,
						hidden: true
					},
					// {field:'company',title:'Company',width:50},		
					{
						field: 'matin_no',
						title: 'Incoming No.',
						width: 50
					},
					{
						field: 'Fmatin_date',
						title: 'Incoming Date',
						width: 50
					},
					{
						field: 'matin_name',
						title: 'Incoming Type',
						width: 50
					},
					{
						field: 'po_no',
						title: 'PO No.',
						width: 50
					},
					{
						field: 'NmPrshn',
						title: 'Shipper',
						width: 80
					},
					{
						field: 'supl_do',
						title: 'Dok No.',
						width: 50
					},
					{
						field: 'supl_inv',
						title: 'Inv No.',
						width: 50
					},
					{
						field: 'currency',
						title: 'Currency',
						width: 50
					},
					{
						field: 'kurs',
						title: 'Kurs',
						width: 50
					},
					{
						field: 'UrJnsDok',
						title: 'Jenis BC',
						width: 50
					},
					{
						field: 'NoDaf',
						title: 'No. Dok. Pabean',
						width: 50
					},
					{
						field: 'FTgDaf',
						title: 'Tgl. Dok. Pabean',
						width: 50
					},
					{
						field: 'NoAju',
						title: 'No. Pengajuan',
						width: 50
					},
					{
						field: 'FTgAju',
						title: 'Tgl. Pengajuan',
						width: 50
					},
					{
						field: 'child_no',
						title: 'Child No',
						width: 80,
						hidden: true
					},
					{
						field: 'mat_id',
						title: 'Item Code',
						width: 100
					},
					{
						field: 'NmBarang',
						title: 'Item Description',
						width: 200
					},
					{
						field: 'Sat',
						title: 'UOM',
						width: 40
					},
					{
						field: 'Fqty',
						title: 'Qty.',
						width: 100,
						align: 'right'
					},
					{
						field: 'Ngqty',
						title: 'NG Qty.',
						width: 100,
						align: 'right'
					},
					{
						field: 'Fprice',
						title: 'Price',
						width: 100,
						align: 'right'
					},
					{
						field: 'amount',
						title: 'Amount',
						width: 100,
						align: 'right'
					}
				]
			],
			url: '<?= site_url($this->modul) ?>/dataHdrDet',
			onSelect: function(index, row) {
				//console.log(row);
				setDgDetUrl(row.matin_id);
			}
		});
	}

	function setDgDetUrl(id) {
		$('#matin_id').val(id);
		$('#toolbar_det').show();
		$('#dg_det').datagrid({
			url: '<?= site_url($this->modul) ?>/dataDet',
			queryParams: {
				matin_id: id
			}
		});
	}

	function setItemUrl(po_id = 'empty') {
		if (po_id != 'empty' && po_id != '' && po_id != '0') {
			$('#mat_id').combogrid({
				url: '<?= site_url('general/gridBarangPoAll') ?>',
			});
		}
	}

	function cari(q) {
		KdJnsDok = $('#KdJnsDok2').combobox('getValue');
		$('#dg_hdr').datagrid({
			queryParams: {
				q: q,
				KdJnsDok: KdJnsDok
			}
		});
	}

	function cari2(q) {
		$('#dg_hdrdet').datagrid({
			queryParams: {
				q: q
			}
		});
	}

	function cari_det(q) {
		var row = $('#dg_hdr').datagrid('getSelected');
		if (row) {
			$('#dg_det').datagrid({
				queryParams: {
					matin_id: row.matin_id,
					q: q
				}
			});
		} else {
			$.messager.show({
				title: 'Info',
				msg: 'Data header belum dipilih'
			});
		}
	}

	function tambah() {
		$('#dlg').dialog('open').dialog('setTitle', 'Tambah Header');
		$('#fm').form('clear');
		//$('#matin_no').textbox('setValue', '<?= $ri ?>');
		$('#matin_date').datebox('setValue', '<?= date('d/m/Y') ?>');
		$('#TgDaf').datebox('setValue', '<?= date('d/m/Y') ?>');
		$('#TgAju').datebox('setValue', '<?= date('d/m/Y') ?>');
		$('#TgTTD').datebox('setValue', '<?= date('d/m/Y') ?>');
		url = '<?= site_url($this->modul) ?>/getform/add';
	}

	function ubah() {
		var row = $('#dg_hdr').datagrid('getSelected');
		if (row) {
			url = '<?= site_url($this->modul) ?>/getform/edit';
			// alert('t'+url);
			$('#dlg').dialog('open').dialog('setTitle', 'Ubah Header');
			$('#fm').form('load', row);
			$('#po_no').combobox('disable'); 
		}
	}

	function simpan() {
		// alert('ttt'+url);
		$('#fm').form('submit', {
			url: url,
			onSubmit: function() {
				return $(this).form('validate');
			},
			success: function(result) {
				//alert(result);
				var result = eval('(' + result + ')');
				if (result.success) {
					$('#dlg').dialog('close'); // close the dialog
					$('#dg_hdr').datagrid('reload');
					$.messager.show({
						title: 'Info',
						msg: result.msg
					});
				// 	var url = '<?= site_url($this->modul) ?>'; //redirect menggunakan time
				// 	setTimeout(function() {
				// 		location.replace(url);
				// }, 1000);
				} else {
					$.messager.show({
						title: 'Error',
						msg: result.msg
					});
				}
			}
		});
	}

	function validasi(strings) {
		var i = 0;
		var ch = '';
		num = 0;
		ucase = 0;
		lcase = 0;
		while (i <= strings.length) {
			character = strings.charAt(i);
			if (!isNaN(character * 1)) {
				num = 1;
			} else {
				if (character == character.toUpperCase()) {
					ucase = 1;
				}
				if (character == character.toLowerCase()) {
					lcase = 1;
				}
			}
			i++;
		}
		if (num == 0 || ucase == 0 || lcase == 0) {
			return false;
		} else {
			return true;
		}
	}

	function hapus() {
		var row = $('#dg_hdr').datagrid('getSelected');
		if (row && row.in === "yes") {
				$.messager.show({
					title: 'Info',
					msg: 'Transaksi dengan PO NO ini sudah masuk ke Invoice, tidak bisa diedit lagi!'
				});
		}else if (row && row.in === "no") {
			$.messager.confirm('Confirm', 'Apakah anda yakin akan menghapus data ini?', function(r) {
				if (r) {
					$.post('<?= site_url($this->modul) ?>/del', {
						id: row.matin_id,
						po_id: row.po_id,
						partial: row.partial
					}, function(result) {
						var result = eval('(' + result + ')');
						if (result.success) {
							$('#dg_hdr').datagrid('reload');
							$('#dg_det').datagrid('reload');
							$('#po_no').combogrid('grid').datagrid('reload');
							$.messager.show({
								title: 'Info',
								msg: result.msg
							});
						// 	var url = '<?= site_url($this->modul) ?>'; //redirect menggunakan time
						// 	setTimeout(function() {
						// 		location.replace(url);
						// }, 1000);
						} else {
							$.messager.show({ // show error message
								title: 'Error',
								msg: result.msg
							});
						}
					});
				}
			});
		}
	}

	function barScan() {
		$('#dlg3').dialog('open').dialog('setTitle', 'Tambah Detail (Barcode Scanner)');
		$('#barcode').val('');
		$('#fifo').prop('checked', true);
		url = '<?= site_url($this->modul) ?>/addBarScan';
	}

	function tambah2() {
		var rh = $('#dg_hdr').datagrid('getSelected');
		if (rh) {
			$('#dlg2').dialog('open').dialog('setTitle', 'Tambah Detail');
			$('#fm2').form('clear');
			//setItemUrl(rh.po_id);
			url = '<?= site_url($this->modul) ?>/getformDet/add';
		} else {
			$.messager.show({
				title: 'Info',
				msg: 'Data header harus dipilih terlebih dahulu'
			});
		}
	}
	
	function excelCeisa() {
		var row = $('#dg_hdr').datagrid('getSelected');
		if (row) {
			openurl("<?= site_url($this->modul); ?>/excelCeisa/" + row.matin_id);
		} else {
			$.messager.show({
				title: 'Info',
				msg: 'Harap pilih data yang akan di export ke excel'
			});
		}
	}

	function ubah2() {
		var rh = $('#dg_hdr').datagrid('getSelected');
		if (rh) {
			var row = $('#dg_det').datagrid('getSelected');
			if (row && row.locked === "yes") {
				$.messager.show({
					title: 'Info',
					msg: 'Data ini dikunci!'
				});
			} else if (row && rh.in === "yes") {
				$.messager.show({
					title: 'Info',
					msg: 'Transaksi dengan PO NO ini sudah masuk ke Invoice, tidak bisa diedit lagi!'
				});
			} else if (row && row.has_edited === "no" && rh.locked === "yes") {
				$.messager.confirm('Confirm','Jika anda rubah data ini, data yang ada di partial berikutnya akan berubah, apakah anda yakin?',function(r){
					if (r){
						setItemUrl(rh.po_id);
						$('#dlg2').dialog('open').dialog('setTitle', 'Ubah Detail');
						$('#fm2').form('load', row);
						url = '<?= site_url($this->modul) ?>/getformDet/edit';
					}
				});
			} else if (row && row.has_edited === "no" && rh.locked === "no") {
				//setItemUrl(rh.po_id);
				$('#dlg2').dialog('open').dialog('setTitle', 'Ubah Detail');
				$('#fm2').form('load', row);
				url = '<?= site_url($this->modul) ?>/getformDet/edit';
			} else if (row && row.has_edited === "yes") {
				// CHECK IF THE PARTIAL HAS BEEN LOCKED OR NOT (DIFFERENT MESSAGE) --ir
				if (rh.locked === "no"){
					var affect = 'no';
					var msg = "Data sudah diedit sebelumnya. Apakah anda yakin untuk reset kembali Qty Item ini?";
				} 
				else {
					var affect = 'yes';
					var msg = "Jika anda rubah data ini, data yang ada di partial berikutnya akan berubah, apakah anda yakin?";
				} 
				$.messager.confirm('Confirm',msg,function(r){
					if (r){
						// RESET FIRST BEFORE REEDITED --ir
						$.post('<?=site_url($this->modul)?>/reset_qty',{matin_id:row.matin_id, child_no:row.child_no, mat_id:row.mat_id, po_id:rh.po_id, partial:rh.partial, affect:affect},function(result){
							$('#dg_det').datagrid('reload');
							$('#dlg2').dialog('open').dialog('setTitle', 'Ubah Detail');
							$('#fm2').form('load', row);
							// RETURN VALUE ALL INPUT TO BEFORE WAS EDITED --ir
							var qty= $('#qty').numberbox('getValue');
							var Dqty2= $('#Dqty2').numberbox('getValue');
							var Dqty = (Dqty2-0) - qty;
							var poqty= $('#poqty').numberbox('getValue');
							var Bqty= poqty - Dqty;
							$('#qty').numberbox('setValue',0);
							$('#Dqty').numberbox('setValue',Dqty);
							$('#Dqty2').numberbox('setValue',Dqty2 - qty);
							$('#Bqty').numberbox('setValue',Bqty);
							$('#amount').numberbox('setValue',0);
							url = '<?= site_url($this->modul) ?>/getformDet/edit';
						});
					}
				});
			} else {
				$.messager.show({
					title: 'Info',
					msg: 'Data detail harus dipilih terlebih dahulu'
				});
			}
		} else {
			$.messager.show({
				title: 'Info',
				msg: 'Data header harus dipilih terlebih dahulu'
			});
		}
	}

// Function For NON Partial Transaction (Incoming Finished Good, etc) --ir
function ubah3() {
	var rh = $('#dg_hdr').datagrid('getSelected');
	if (rh) {
		var row = $('#dg_det').datagrid('getSelected');
		if (row) {
			//setItemUrl(rh.po_id);
			$('#dlg2').dialog('open').dialog('setTitle', 'Ubah Detail');
			$('#fm2').form('load', row);
			url = '<?= site_url($this->modul) ?>/getformDet/edit';
		} else {
			$.messager.show({
				title: 'Info',
				msg: 'Data detail harus dipilih terlebih dahulu'
			});
		}
	} else {
		$.messager.show({
			title: 'Info',
			msg: 'Data header harus dipilih terlebih dahulu'
		});
	}
}

function reset2() {
		// $('#price').prop('readonly', true);
		// $('#mat_id').prop('readonly', true);
		qty=$('#qty').numberbox('getValue');
		Dqty=$('#Dqty').numberbox('getValue');
		Dqty2=$('#Dqty2').numberbox('getValue');
		Bqty=$('#Bqty').numberbox('getValue');
		amount = Dqty-qty;
		$('#Dqty').numberbox('setValue',amount);
		amount = Dqty2-qty;
		$('#Dqty2').numberbox('setValue',amount);
		qty2 = qty-0;
		amount = Bqty - 0 + (qty2-0);
		$('#Bqty').numberbox('setValue',amount);
		amount = qty-qty;
		$('#qty').numberbox('setValue',amount);
		$('#btnResetQty2').linkbutton('disable');
	}

function saveAdj(){
	var row = $('#dg_det').datagrid('getSelected');
	if (row){
		$.post('<?=site_url($this->modul)?>/saveAdj',{matin_id:row.matin_id, child_no:row.child_no, mat_id:row.mat_id},function(result){
			var result = eval('('+result+')');
			if (result.success){
				$('#dg_det').datagrid('reload');
				$.messager.show({
					title: 'Info',
					msg: result.msg
				});
			} else {
				$.messager.show({	// show error message
					title: 'Error',
					msg: result.msg
				});
			}
		});
	} else {
		$.messager.show({
			title: 'Info',
			msg: 'Data detail harus dipilih terlebih dahulu'
		});
	}
}

function lock2(){
		var row = $('#dg_det').datagrid('getSelected');
		if (row){
			$.post('<?=site_url($this->modul)?>/lock2',{matin_id:row.matin_id, child_no:row.child_no, mat_id:row.mat_id, locked:row.locked},function(result){
				var result = eval('('+result+')');
				if (result.success){
					$('#dg_det').datagrid('reload');
					$.messager.show({
						title: 'Info',
						msg: result.msg
					});
				} else {
					$.messager.show({	// show error message
						title: 'Error',
						msg: result.msg
					});
				}
			});
		} else {
			$.messager.show({
				title: 'Info',
				msg: 'Data detail harus dipilih terlebih dahulu'
			});
		}
	}

	function rst2(){
		var rh = $('#dg_hdr').datagrid('getSelected');
		var row = $('#dg_det').datagrid('getSelected');
		if (row && row.locked === "yes") {
			$.messager.show({
				title: 'Info',
				msg: 'Data ini dikunci!'
			});
		} else if (row && rh.in === "yes") {
			$.messager.show({
				title: 'Info',
				msg: 'Transaksi dengan PO NO ini sudah masuk ke Invoice, tidak bisa diedit lagi!'
			});
		} else if (row && row.has_edited === "yes" && rh.locked === "yes"){
			$.messager.confirm('Confirm','Jika anda reset data ini, data yang ada di partial berikutnya akan berubah, apakah anda yakin?',function(l){
				if (l){
					$.messager.confirm('Confirm','Apakah anda yakin untuk reset Qty Item ini?',function(r){
						if (r){
							$.post('<?=site_url($this->modul)?>/reset_qty',{matin_id:row.matin_id, child_no:row.child_no, mat_id:row.mat_id, po_id:rh.po_id, partial:rh.partial, affect:"yes"},function(result){
								var result = eval('('+result+')');
								if (result.success){
									$('#dg_det').datagrid('reload');
									// $('#dg_hdr').datagrid('reload');
									$('#po_no').combogrid('grid').datagrid('reload');
									$.messager.show({
										title: 'Info',
										msg: result.msg
									});
								} else {
									$.messager.show({	// show error message
										title: 'Error',
										msg: result.msg
									});
								}
							});
						}
					});
				}
			});
		} else if (row && row.has_edited === "yes"){
			$.messager.confirm('Confirm','Apakah anda yakin untuk reset Qty Item ini?',function(r){
				if (r){
					$.post('<?=site_url($this->modul)?>/reset_qty',{matin_id:row.matin_id, child_no:row.child_no, mat_id:row.mat_id, po_id:rh.po_id, partial:rh.partial, affect:"no"},function(result){
						var result = eval('('+result+')');
						if (result.success){
							$('#dg_det').datagrid('reload');
							// $('#dg_hdr').datagrid('reload');
							$.messager.show({
								title: 'Info',
								msg: result.msg
							});
						} else {
							$.messager.show({	// show error message
								title: 'Error',
								msg: result.msg
							});
						}
					});
				}
			});
		} else if (row && row.has_edited === "no") {
			$.messager.show({
				title: 'Info',
				msg: 'Data ini belum diedit / sudah direset!'
			});
		} else {
			$.messager.show({
				title: 'Info',
				msg: 'Data detail harus dipilih terlebih dahulu'
			});
		}
	}
	
	function rst2All(){
		var row = $('#dg_hdr').datagrid('getSelected');
		$.messager.confirm('Confirm','Apakah anda yakin untuk reset Qty SEMUA Item ini?',function(r){
			// if (r && row.locked === "yes") {
			// 	$.messager.show({
			// 		title: 'Info',
			// 		msg: 'Data item ini sudah masuk ke partial berikutnya. Tidak bisa diedit kembali!'
			// 	});
			// } else 
			if (row && row.in === "yes") {
				$.messager.show({
					title: 'Info',
					msg: 'Transaksi dengan PO NO ini sudah masuk ke Invoice, tidak bisa diedit lagi!'
				});
			} else if (r && row.locked === "yes"){
				$.messager.confirm('Confirm','Jika anda reset data ini, data yang ada di partial berikutnya akan berubah, apakah anda yakin?',function(l){
					if (l){
						$.post('<?=site_url($this->modul)?>/reset_qty_all',{matin_id:row.matin_id, po_id:row.po_id, partial:row.partial, affect:"yes"},function(result){
							var result = eval('('+result+')');
							if (result.success){
								$('#dg_det').datagrid('reload');
								$('#dg_hdr').datagrid('reload');
								$('#po_no').combogrid('grid').datagrid('reload');
								$.messager.show({
									title: 'Info',
									msg: result.msg
								});
							} else {
								$.messager.show({	// show error message
									title: 'Error',
									msg: result.msg
								});
							}
						});
					}
				});
			} else if (r && row.in === "yes") {
				$.messager.show({
					title: 'Info',
					msg: 'Transaksi dengan PO NO ini sudah masuk ke Invoice, tidak bisa diedit lagi!'
				});
			} else if (r && row.locked === "no"){
				$.post('<?=site_url($this->modul)?>/reset_qty_all',{matin_id:row.matin_id, po_id:row.po_id, partial:row.partial, affect:"no"},function(result){
					var result = eval('('+result+')');
					if (result.success){
						$('#dg_det').datagrid('reload');
						$('#dg_hdr').datagrid('reload');
						$.messager.show({
							title: 'Info',
							msg: result.msg
						});
					} else {
						$.messager.show({	// show error message
							title: 'Error',
							msg: result.msg
						});
					}
				});
			}
		});
	}

	function spr2(){
		var rh = $('#dg_hdr').datagrid('getSelected');
		var row = $('#dg_det').datagrid('getSelected');
		if (row && row.locked === "yes") {
			$.messager.show({
				title: 'Info',
				msg: 'Data ini dikunci!'
			});
		} else if (row && rh.in === "yes") {
			$.messager.show({
				title: 'Info',
				msg: 'Transaksi dengan PO NO ini sudah masuk ke Invoice, tidak bisa diedit lagi!'
			});
		} else if (row && row.all_in === "no" && rh.locked === "yes"){
			$.messager.confirm('Confirm','Jika anda ubah data ini, data yang ada di partial berikutnya akan berubah, apakah anda yakin?',function(l){
				if (l){
					$.messager.confirm('Confirm','Apakah anda yakin untuk All In Incoming Qty Item ini?',function(r){
						if (r){
							$.post('<?=site_url($this->modul)?>/all_in_qty',{matin_id:row.matin_id, child_no:row.child_no, mat_id:row.mat_id, po_id:rh.po_id, partial:rh.partial, affect:"yes"},function(result){
								var result = eval('('+result+')');
								if (result.success){
									$('#dg_det').datagrid('reload');
									$('#dg_hdr').datagrid('reload');
									$('#po_no').combogrid('grid').datagrid('reload');
									$.messager.show({
										title: 'Info',
										msg: result.msg
									});
								} else {
									$.messager.show({	// show error message
										title: 'Error',
										msg: result.msg
									});
								}
							});
						}
					});
				}
			});
		} else if (row && row.all_in === "no" && rh.locked === "no"){
			$.messager.confirm('Confirm','Apakah anda yakin untuk All In Incoming Qty Item ini?',function(r){
				if (r){
					$.post('<?=site_url($this->modul)?>/all_in_qty',{matin_id:row.matin_id, child_no:row.child_no, mat_id:row.mat_id, po_id:rh.po_id, partial:rh.partial, affect:"no"},function(result){
						var result = eval('('+result+')');
						if (result.success){
							$('#dg_det').datagrid('reload');
							$('#dg_hdr').datagrid('reload');
							$.messager.show({
								title: 'Info',
								msg: result.msg
							});
						} else {
							$.messager.show({	// show error message
								title: 'Error',
								msg: result.msg
							});
						}
					});
				}
			});
		} else if (row && row.all_in === "yes") {
			$.messager.show({
				title: 'Info',
				msg: 'Data ini sudah masuk semua!'
			});
		} else {
			$.messager.show({
				title: 'Info',
				msg: 'Data detail harus dipilih terlebih dahulu'
			});
		}
	}

	function spr2All(){
		var row = $('#dg_hdr').datagrid('getSelected');
		$.messager.confirm('Confirm','Apakah anda yakin untuk All In Incoming Qty SEMUA item ini?',function(r){
			if (row && row.in === "yes") {
				$.messager.show({
					title: 'Info',
					msg: 'Transaksi dengan PO NO ini sudah masuk ke Invoice, tidak bisa diedit lagi!'
				});
			}else if (r && row.locked === "yes"){
				$.messager.confirm('Confirm','Jika anda ubah data ini, data yang ada di partial berikutnya akan berubah, apakah anda yakin?',function(l){
					if (l){
						$.post('<?=site_url($this->modul)?>/all_in_qty_all',{matin_id:row.matin_id, po_id:row.po_id, partial:row.partial, affect:"yes"},function(result){
							var result = eval('('+result+')');
							if (result.success){
								$('#dg_det').datagrid('reload');
								$('#dg_hdr').datagrid('reload');
								$('#po_no').combogrid('grid').datagrid('reload');
								$.messager.show({
									title: 'Info',
									msg: result.msg
								});
							} else {
								$.messager.show({	// show error message
									title: 'Error',
									msg: result.msg
								});
							}
						});
					}
				});
			} else if (r && row.in === "yes") {
				$.messager.show({
					title: 'Info',
					msg: 'Transaksi dengan PO NO ini sudah masuk ke Invoice, tidak bisa diedit lagi!'
				});
			} else if (r && row.locked === "no"){
				$.post('<?=site_url($this->modul)?>/all_in_qty_all',{matin_id:row.matin_id, po_id:row.po_id, partial:row.partial, affect:"yes"},function(result){
					var result = eval('('+result+')');
					if (result.success){
						$('#dg_det').datagrid('reload');
						$('#dg_hdr').datagrid('reload');
						$.messager.show({
							title: 'Info',
							msg: result.msg
						});
					} else {
						$.messager.show({	// show error message
							title: 'Error',
							msg: result.msg
						});
					}
				});
			}
		});
	}

	function simpan2() {
		$('#fm2').form('submit', {
			url: url,
			onSubmit: function(param) {
				param.matin_id = $('#matin_id').val();
				return $(this).form('validate');
			},
			success: function(result) {
				//alert(result);
				var result = eval('(' + result + ')');
				if (result.success) {
					$('#dlg2').dialog('close'); // close the dialog
					$('#dg_det').datagrid('reload');
					///$('#dg_hdr').datagrid('reload');
					$.messager.show({
						title: 'Info',
						msg: result.msg
					});
				} else {
					$.messager.show({
						title: 'Error',
						msg: result.msg
					});
				}
			}
		});
	}

	function hapus2() {
		var row = $('#dg_det').datagrid('getSelected');
		if (row) {
			$.messager.confirm('Confirm', 'Apakah anda yakin akan menghapus data ini?', function(r) {
				if (r) {
					$.post('<?= site_url($this->modul) ?>/delDet', {
						id: row.matin_id,
						child_no: row.child_no
					}, function(result) {
						var result = eval('(' + result + ')');
						if (result.success) {
							$('#dg_det').datagrid('reload');
							$('#dg_hdr').datagrid('reload');
							$.messager.show({
								title: 'Info',
								msg: result.msg
							});
						} else {
							$.messager.show({ // show error message
								title: 'Error',
								msg: result.msg
							});
						}
					});
				}
			});
		}
	}

	function simpan3() {
		$('#fm3').form('submit', {
			url: url,
			onSubmit: function(param) {
				param.matin_id = $('#matin_id').val();
				return $(this).form('validate');
			},
			success: function(result) {
				alert(result);
				/*var result = eval('('+result+')');
				if (result.success){
					$('#dlg2').dialog('close');		// close the dialog
					$('#dg_det').datagrid('reload');
					$.messager.show({
						title: 'Info',
						msg: result.msg
					});
				} else {
					$.messager.show({
						title: 'Error',
						msg: result.msg
					});
				}*/
			}
		});
	}

	function insert_det(row) {
		$('#PartNo').textbox('setValue', row.PartNo);
		$('#CF').textbox('setValue', row.CF);
		$('#Sat').textbox('setValue', row.Sat);
		if ($('#aksi').val() == 't') {
			$('#price').numberbox('setValue', row.Harga);
		}
	}

	function createJournal(){
		var row = $('#dg_hdr').datagrid('getSelected');
		if (row){
			$.messager.confirm('Confirm','Apakah anda yakin akan membuat journal untuk data ini?',function(r){
				if (r){
					$.post('<?=site_url($this->modul)?>/create_journal',{id:row.id, created_journal:row.created_journal},function(result){
						var result = eval('('+result+')');
						if (result.success){
							$('#dg_hdr').datagrid('reload');
							$.messager.show({
								title: 'Info',
								msg: result.msg
							});
						} else {
							$.messager.show({	// show error message
								title: 'Error',
								msg: result.msg
							});
						}
					});
				}
			});
		}
	}

	function transferCeisa(){
		var row = $('#dg_hdr').datagrid('getSelected');
		if (row){
			$.messager.confirm('Confirm','Apakah anda yakin akan transfer data ini ke Ceisa?',function(r){
				if (r){
					$.post('<?=site_url($this->modul)?>/transfer_ceisa',{id:row.id, transfer_ceisa:row.transfer_ceisa},function(result){
						var result = eval('('+result+')');
						if (result.success){
							$('#dg_hdr').datagrid('reload');
							$.messager.show({
								title: 'Info',
								msg: result.msg
							});
						} else {
							$.messager.show({	// show error message
								title: 'Error',
								msg: result.msg
							});
						}
					});
				}
			});
		}
	}

	function printSlip() {
		var row = $('#dg_hdr').datagrid('getSelected');
		if (row) {
			openurl("<?= site_url($this->modul); ?>/slip/" + row.matin_id);
		}
	}

	function aksi(cmd) {
		var opts = $('#dg_hdrdet').datagrid('options');
		q = $("#q").val();
		if (cmd == 'print') {
			var win = window.open('<?= site_url($this->modul) ?>/aksi/cetak?q=' + q + '&page=' + opts.pageNumber + '&rows=' + opts.pageSize, 'Popup', 'toolbar=no, location=no,status=no,menubar=no,scrollbars=yes,resizable=no');
			win.print();
		} else if (cmd == 'xls') {
			openurl('<?= site_url($this->modul); ?>/aksi/excel?q=' + q + '&page=' + opts.pageNumber + '&rows=' + opts.pageSize);
		}
	}
</script>