<?php 
include("../config.php");
if(!empty($_POST)){
	if($_POST["id"] != ''){
		$code  = mysqli_real_escape_string($conn, $_POST['code']);
	}else{
		$code  = mysqli_real_escape_string($conn, $_POST['code']);

		$checkpoint = mysqli_query($conn,'SELECT code FROM credit_note WHERE code = "'. $code .'"');
		if(mysqli_num_rows($checkpoint) > 0) {
	    	$code = CreditNote($conn);
    	}	
	}

	if(!empty($_POST['date'])){
		$date = mysqli_real_escape_string($conn, risFormatDate($_POST['date'], 'Y-m-d'));
	}else{
		$date = '';
	}

	if(!empty($_POST['supplier_id'])){
		$supplier_id = mysqli_real_escape_string($conn, $_POST['supplier_id']);
  	}else{
		$supplier_id = '';
	}

	if(!empty($_POST['gross_amount'])){
		$gross_amount = mysqli_real_escape_string($conn, $_POST['gross_amount']);
  	}else{
		$gross_amount = '';
	}

	if(!empty($_POST['addition'])){
		$addition = mysqli_real_escape_string($conn, $_POST['addition']);
  	}else{
		$addition = '';
	}

	if(!empty($_POST['deduction'])){
		$deduction = mysqli_real_escape_string($conn, $_POST['deduction']);
  	}else{
		$deduction = '';
	}

	if(!empty($_POST['cgst_per'])){
		$cgst_per = mysqli_real_escape_string($conn, $_POST['cgst_per']);
  	}else{
		$cgst_per = '';
	}

	if(!empty($_POST['cgst_amount'])){
		$cgst_amount = mysqli_real_escape_string($conn, $_POST['cgst_amount']);
  	}else{
		$cgst_amount = '';
	}

	if(!empty($_POST['sgst_per'])){
		$sgst_per = mysqli_real_escape_string($conn, $_POST['sgst_per']);
  	}else{
		$sgst_per = '';
	}

	if(!empty($_POST['sgst_amount'])){
		$sgst_amount = mysqli_real_escape_string($conn, $_POST['sgst_amount']);
  	}else{
		$sgst_amount = '';
	}

	if(!empty($_POST['igst_per'])){
		$igst_per = mysqli_real_escape_string($conn, $_POST['igst_per']);
  	}else{
		$igst_per = '';
	}

	if(!empty($_POST['igst_amount'])){
		$igst_amount = mysqli_real_escape_string($conn, $_POST['igst_amount']);
  	}else{
		$igst_amount = '';
	}

	if(!empty($_POST['net_amount'])){
		$net_amount = mysqli_real_escape_string($conn, $_POST['net_amount']);
  	}else{
		$net_amount = '';
	}

	if(!empty($_POST['taxable_amount'])){
		$taxable_amount = mysqli_real_escape_string($conn, $_POST['taxable_amount']);
  	}else{
		$taxable_amount = '';
	}

	$created_datetime    = date('Y-m-d H:i:s');
	$created_id          = $_SESSION['user_detail']['id'];
	$updated_datetime    = date('Y-m-d H:i:s');
	$updated_id          = $_SESSION['user_detail']['id'];
	$company_id          = $_SESSION['company_id'];
  	$branch_id           = $_SESSION['branch_id'];
  	$credit_grid         = $_POST['material'];
  	$addition_grid 			 = $_POST['add'];
  	$deduction_grid 		 = $_POST['sub'];
	$master_accounts     = combine(getAllRecords('act_master'), 'id', 'act_code');
	$master_vendor       = combine(getAllRecords('vendor_master'), 'id', 'name');
	$account_code        = combine(getAllRecords('act_master'), 'ac_name', 'act_code');
	if($_POST["id"] != ''){
  		$type     = 'credit_note_update';
		$user_id  = $_SESSION['user_detail']['id'];
		$data     = serialize($_POST);
		
		$gl_delete = mysqli_query($conn, "DELETE FROM `general_ledger` WHERE `voucher_no` = '".$code."' AND (`flag` = 'CREDIT NOTE' OR `mark` = 'CREDIT NOTE')");
		
		$trans_delete = mysqli_query($conn, "DELETE FROM `trans` WHERE `voucher_no` = '".$code."' AND `flag` = 'CREDIT NOTE'");

		$detail_delete = mysqli_query($conn, "DELETE FROM `credit_note_details` WHERE `master_id` = '".$_POST['id']."'");

		$tax_delete = mysqli_query($conn, "DELETE FROM `credit_note_tax_details` WHERE `master_id` = '".$_POST['id']."'");

		$account 				= mysqli_query($conn, "SELECT * FROM account_control WHERE descr = 'CREDIT NOTE'");
  		$account_row 		= mysqli_fetch_assoc($account);
  		
  		$debit_account 	= @$master_accounts[$account_row['account_group']];
		$vendor_name    = @$master_vendor[$supplier_id];
		$credit_account = @$account_code[$vendor_name];

		$update = mysqli_query($conn, "UPDATE `credit_note` SET `date`='".$date."',`supplier_id`='".$supplier_id."',`gross_amount`='".$gross_amount."',`addition`='".$addition."',`deduction`='".$deduction."',`cgst_per`='".$cgst_per."',`cgst_amount`='".$cgst_amount."',`sgst_per`='".$sgst_per."',`sgst_amount`='".$sgst_amount."',`igst_per`='".$igst_per."',`igst_amount`='".$igst_amount."',`taxable_amount`='".$taxable_amount."',`net_amount`='".$net_amount."',`updated_id`='".$updated_id."',`updated_datetime`='".$updated_datetime."' WHERE `id` = '".$_POST['id']."'");

		$last_id = $_POST['id'];

		/*COMMON GENERAL LEDGER*/
		$gl = mysqli_query($conn, "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','CREDIT NOTE','".$credit_account."','".$debit_account."','".$net_amount."','','','','".$net_amount."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");

		if(count($credit_grid) > 0){
			foreach ($credit_grid as $credit_grids) {
				if( isset($credit_grids['description']) && !empty($credit_grids['description']) ) {
					$detail = mysqli_query($conn, "INSERT INTO `credit_note_details`(`master_id`, `description`, `hsn`, `qty`, `rate`, `amount`) VALUES ('".$last_id."','".$credit_grids['description']."','".$credit_grids['hsn']."','".$credit_grids['qty']."','".$credit_grids['rate']."','".$credit_grids['amount']."')");
				}
			}
		}

		if(count($addition_grid) > 0) {
			foreach ($addition_grid as $add_key => $addition_grid_data) {
				if($addition_grid_data['tax_mst_id'] > 0 && $addition_grid_data['amount'] > 0 ) {

					$get_tax = mysqli_query($conn, "SELECT * FROM `tax_mst` WHERE `id` = '".$addition_grid_data['tax_mst_id']."'");
					$get_tax_row = mysqli_fetch_assoc($get_tax);
					$account_code= @$master_accounts[$get_tax_row['account_group']];


					$add_insert = mysqli_query($conn, "INSERT INTO `credit_note_tax_details`(`master_id`, `type`, `key`, `tax_mst_id`, `amount`, `branch_id`, `company_id`) VALUES ('".$last_id."','add','". $add_key ."','".$addition_grid_data['tax_mst_id']."','".$addition_grid_data['amount']."','".$branch_id."','".$company_id."')");

					$add_gl = mysqli_query($conn, "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','CREDIT NOTE','".$credit_account."','".$account_code."','".$addition_grid_data['amount']."','','','','".$net_amount."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
				}
			}
		}

		if(count($deduction_grid) > 0) {
			foreach ($deduction_grid as $less_key => $deduction_grid_data) {
				if($deduction_grid_data['tax_mst_id'] > 0 && $deduction_grid_data['amount'] > 0 ) {

					$get_tax = mysqli_query($conn, "SELECT * FROM `tax_mst` WHERE `id` = '".$deduction_grid_data['tax_mst_id']."'");
					$get_tax_row = mysqli_fetch_assoc($get_tax);
					$account_code= @$master_accounts[$get_tax_row['account_group']];
					
					$add_insert = mysqli_query($conn, "INSERT INTO `credit_note_tax_details`(`master_id`, `type`, `key`, `tax_mst_id`, `amount`, `branch_id`, `company_id`) VALUES ('".$last_id."','less','". $less_key ."','".$deduction_grid_data['tax_mst_id']."','".$deduction_grid_data['amount']."','".$branch_id."','".$company_id."')");

					$less_gl = mysqli_query($conn, "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','CREDIT NOTE','".$account_code."','".$credit_account."','".$deduction_grid_data['amount']."','','','','".$net_amount."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
				}
			}
		}

		if($cgst_amount > 0){
			$ac_control = mysqli_query($conn, "SELECT * FROM account_control WHERE descr = 'CGST'");
			$rows       = mysqli_fetch_assoc($ac_control);
			$cgst_account_code = @$master_accounts[$rows['account_group']];

			$cgst_gl = mysqli_query($conn, "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','CGST','".$credit_account."','".$cgst_account_code."','".$cgst_amount."','','','CREDIT NOTE','".$net_amount."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
		}

		if($sgst_amount > 0){
			$ac_control = mysqli_query($conn, "SELECT * FROM account_control WHERE descr = 'SGST'");
			$rows       = mysqli_fetch_assoc($ac_control);
			$cgst_account_code = @$master_accounts[$rows['account_group']];

			$cgst_gl = mysqli_query($conn, "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','SGST','".$credit_account."','".$cgst_account_code."','".$sgst_amount."','','','CREDIT NOTE','".$net_amount."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
		}

		if($igst_amount > 0){
			$ac_control = mysqli_query($conn, "SELECT * FROM account_control WHERE descr = 'IGST'");
			$rows       = mysqli_fetch_assoc($ac_control);
			$cgst_account_code = @$master_accounts[$rows['account_group']];

			$cgst_gl = mysqli_query($conn, "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','IGST','".$credit_account."','".$cgst_account_code."','".$igst_amount."','','','CREDIT NOTE','".$net_amount."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
		}

		$trans = mysqli_query($conn, "INSERT INTO `trans`(`voucher_no`, `voucher_date`, `act_code`, `amount`, `adjust`, `balance`, `discount`, `tds`, `other_add`, `other_less`, `flag`, `mark`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','".$credit_account."','".$net_amount."','0.00','".$net_amount."','0.00','0.00','0.00','0.00','CREDIT NOTE','','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");

		$log = mysqli_query($conn, "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
	}else{
		$type     = 'credit_note_add';
		$user_id  = $_SESSION['user_detail']['id'];
		$data     = serialize($_POST);

		$account 				= mysqli_query($conn, "SELECT * FROM account_control WHERE descr = 'CREDIT NOTE'");
  	$account_row 		= mysqli_fetch_assoc($account);
  	$debit_account 	= @$master_accounts[$account_row['account_group']];
		$vendor_name    = @$master_vendor[$supplier_id];
		$credit_account = @$account_code[$vendor_name];
		
		$insert = mysqli_query($conn, "INSERT INTO `credit_note`(`code`, `date`, `supplier_id`, `gross_amount`, `addition`, `deduction`, `cgst_per`, `cgst_amount`, `sgst_per`, `sgst_amount`, `igst_per`, `igst_amount`, `taxable_amount`, `net_amount`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','".$supplier_id."','".$gross_amount."','".$addition."','".$deduction."','".$cgst_per."','".$cgst_amount."','".$sgst_per."','".$sgst_amount."','".$igst_per."','".$igst_amount."','".$taxable_amount."','".$net_amount."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");

		$last_id = mysqli_insert_id($conn);

		/*COMMON GENERAL LEDGER*/
		$gl = mysqli_query($conn, "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','CREDIT NOTE','".$credit_account."','".$debit_account."','".$net_amount."','','','','".$net_amount."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");

		if(count($credit_grid) > 0){
			foreach ($credit_grid as $credit_grids) {
				if( isset($credit_grids['description']) && !empty($credit_grids['description']) ) {
					$detail = mysqli_query($conn, "INSERT INTO `credit_note_details`(`master_id`, `description`, `hsn`, `qty`, `rate`, `amount`) VALUES ('".$last_id."','".$credit_grids['description']."','".$credit_grids['hsn']."','".$credit_grids['qty']."','".$credit_grids['rate']."','".$credit_grids['amount']."')");
				}
			}
		}

		if(count($addition_grid) > 0) {
			foreach ($addition_grid as $add_key => $addition_grid_data) {
				if($addition_grid_data['tax_mst_id'] > 0 && $addition_grid_data['amount'] > 0 ) {

					$get_tax = mysqli_query($conn, "SELECT * FROM `tax_mst` WHERE `id` = '".$addition_grid_data['tax_mst_id']."'");
					$get_tax_row = mysqli_fetch_assoc($get_tax);
					$account_code= @$master_accounts[$get_tax_row['account_group']];


					$add_insert = mysqli_query($conn, "INSERT INTO `credit_note_tax_details`(`master_id`, `type`, `key`, `tax_mst_id`, `amount`, `branch_id`, `company_id`) VALUES ('".$last_id."','add','". $add_key ."','".$addition_grid_data['tax_mst_id']."','".$addition_grid_data['amount']."','".$branch_id."','".$company_id."')");

					$add_gl = mysqli_query($conn, "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','CREDIT NOTE','".$credit_account."','".$account_code."','".$addition_grid_data['amount']."','','','','".$net_amount."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
				}
			}
		}

		if(count($deduction_grid) > 0) {
			foreach ($deduction_grid as $less_key => $deduction_grid_data) {
				if($deduction_grid_data['tax_mst_id'] > 0 && $deduction_grid_data['amount'] > 0 ) {

					$get_tax = mysqli_query($conn, "SELECT * FROM `tax_mst` WHERE `id` = '".$deduction_grid_data['tax_mst_id']."'");
					$get_tax_row = mysqli_fetch_assoc($get_tax);
					$account_code= @$master_accounts[$get_tax_row['account_group']];
					
					$add_insert = mysqli_query($conn, "INSERT INTO `credit_note_tax_details`(`master_id`, `type`, `key`, `tax_mst_id`, `amount`, `branch_id`, `company_id`) VALUES ('".$last_id."','less','". $less_key ."','".$deduction_grid_data['tax_mst_id']."','".$deduction_grid_data['amount']."','".$branch_id."','".$company_id."')");

					$less_gl = mysqli_query($conn, "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','CREDIT NOTE','".$account_code."','".$credit_account."','".$deduction_grid_data['amount']."','','','','".$net_amount."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
				}
			}
		}

		if($cgst_amount > 0){
			$ac_control = mysqli_query($conn, "SELECT * FROM account_control WHERE descr = 'CGST'");
			$rows       = mysqli_fetch_assoc($ac_control);
			$cgst_account_code = @$master_accounts[$rows['account_group']];

			$cgst_gl = mysqli_query($conn, "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','CGST','".$credit_account."','".$cgst_account_code."','".$cgst_amount."','','','CREDIT NOTE','".$net_amount."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
		}

		if($sgst_amount > 0){
			$ac_control = mysqli_query($conn, "SELECT * FROM account_control WHERE descr = 'SGST'");
			$rows       = mysqli_fetch_assoc($ac_control);
			$cgst_account_code = @$master_accounts[$rows['account_group']];

			$cgst_gl = mysqli_query($conn, "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','SGST','".$credit_account."','".$cgst_account_code."','".$sgst_amount."','','','CREDIT NOTE','".$net_amount."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
		}

		if($igst_amount > 0){
			$ac_control = mysqli_query($conn, "SELECT * FROM account_control WHERE descr = 'IGST'");
			$rows       = mysqli_fetch_assoc($ac_control);
			$cgst_account_code = @$master_accounts[$rows['account_group']];

			$cgst_gl = mysqli_query($conn, "INSERT INTO `general_ledger`(`voucher_no`, `voucher_date`, `flag`, `credit_account`, `debit_account`, `amount`, `remark`, `checked`, `mark`, `net_amount`, `company_id`, `branch_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','IGST','".$credit_account."','".$cgst_account_code."','".$igst_amount."','','','CREDIT NOTE','".$net_amount."','".$company_id."','".$branch_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
		}

		$trans = mysqli_query($conn, "INSERT INTO `trans`(`voucher_no`, `voucher_date`, `act_code`, `amount`, `adjust`, `balance`, `discount`, `tds`, `other_add`, `other_less`, `flag`, `mark`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$code."','".$date."','".$credit_account."','".$net_amount."','0.00','".$net_amount."','0.00','0.00','0.00','0.00','CREDIT NOTE','','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");

		$log = mysqli_query($conn, "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')");
  }
  echo json_encode( array('response' => 1, 'msg' => 'DATA SAVED SUCCESSFULLY....!!!', 'alert' => 1, 'redirect' => 'index.php?page=credit_note/list.php') );die();
}else{
  echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=credit_note/add.php') );die();
} 