<?php
include("include/config.inc.php");
if (!isset($_SESSION['s_activId'])) {
    $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
    header("Location:checkLogin.php");
} else {

    $rojmelId = isset($_REQUEST['rojmelId']) && $_REQUEST['rojmelId'] != '' ? $_REQUEST['rojmelId'] : 0;
    $partyId = isset($_REQUEST['partyId']) && $_REQUEST['partyId'] != '' ? $_REQUEST['partyId'] : 0;
    $particular = isset($_REQUEST['particular']) && $_REQUEST['particular'] != '' ? $_REQUEST['particular'] : "";
    $acc_type = isset($_REQUEST['acc_type']) && $_REQUEST['acc_type'] != '' ? $_REQUEST['acc_type'] : "";
    $fine = isset($_REQUEST['fine']) && $_REQUEST['fine'] != '' ? $_REQUEST['fine'] : 0; 
    $rojTransType = isset($_REQUEST['rojTransType']) && $_REQUEST['rojTransType'] != '' ? $_REQUEST['rojTransType'] : 0;
    $amount = isset($_REQUEST['amount']) && $_REQUEST['amount'] != '' ? $_REQUEST['amount'] : 0;
    $branch = isset($_REQUEST['branch']) && $_REQUEST['branch'] != '' ? $_REQUEST['branch'] : 0;
    $rojmelDate = $_REQUEST['rdtYear'] . "-" . $_REQUEST['rdtMonth'] . "-" . $_REQUEST['rdtDay'];

    if (isset($_POST['submit']) && $rojmelId == 0) {
        $insertRojmel = "INSERT INTO rojmel (partyId,rojmelDate,particular,fine,rojTransType,amount,acc_type,branch)
	                    VALUES (" . $partyId . ",'" . $rojmelDate . "','" . $particular . "','" . $fine . "','" . $rojTransType . "'," . $amount . ",'" . $acc_type . "',".$branch.")";
        $insertRojmelRes = mysql_query($insertRojmel);
        $id = mysql_insert_id();
        if ($insertRojmelRes) {
            $select = "SELECT partyType FROM party WHERE partyId = " . $partyId;
            $res = mysql_query($select);
            if ($row = mysql_fetch_array($res)) {
                $partyType = $row['partyType'];
                if ($partyType == 'C') {
                    $a = 1;
                } else {
                    $a = 0;
                }
            }
            if ($a == 1) {
                if ($acc_type == 'K') {
                    $insertClient = "INSERT INTO client (partyId,cDate,cliTransType,particular,kg,totalAmt,rojmelId)
                                        VALUES(" . $partyId . ",'" . $rojmelDate . "','" . $rojTransType . "','" . $particular . "','" . $fine . "'," . $amount . "," . $id . ")";
                    $insertClientRes = mysql_query($insertClient);
                    if (!$insertClientRes) {
                        echo "Rojmel Client Insert Fail";
                    } else {
                        header("Location:rojmel.php");
                    }
                } else {
                    header("Location:rojmel.php");
                }
            }
            if ($a == 0) {
                if ($acc_type == 'K') {
                    $insertVepari = "INSERT INTO vepari(partyId,vDate,veTransType,particular,kg,totalAmt,rojmelId)
                                        VALUES(" . $partyId . ",'" . $rojmelDate . "','" . $rojTransType . "','" . $particular . "','" . $fine . "'," . $amount . "," . $id . ")";
                    $insertVepariRes = mysql_query($insertVepari);
                    if (!$insertVepariRes) {
                        echo "Rojmel Vepari Insert Fail";
                    } else {
                        header("Location:rojmel.php");
                    }
                } else {
                    header("Location:rojmel.php");
                }
            }
        }
    }

    if (isset($_POST['submit']) && $rojmelId > 0) {
        $old_rojmel_sql = "SELECT * FROM rojmel WHERE rojmelId = " . $rojmelId;
        $old_rojmel_res = mysql_query($old_rojmel_sql);

        if ($old_rojmel_row = mysql_fetch_array($old_rojmel_res)) {
            //print_r($old_rojmel_row); exit;
            $deleteSauda = "DELETE FROM rojmel WHERE rojmelId = " . $rojmelId;
            mysql_query($deleteSauda);
            $deleteSaudaClient = "DELETE FROM client WHERE rojmelId = " . $rojmelId;
            mysql_query($deleteSaudaClient);
            $deleteSaudaVepari = "DELETE FROM vepari WHERE rojmelId = " . $rojmelId;
            mysql_query($deleteSaudaVepari);

            $insertRojmel = "INSERT INTO rojmel (partyId,rojmelDate,particular,fine,rojTransType,amount,acc_type,branch)
	                    VALUES (" . $partyId . ",'" . $rojmelDate . "','" . $particular . "','" . $fine . "','" . $rojTransType . "'," . $amount . ",'" . $acc_type . "',".$branch.")";
            $insertRojmelRes = mysql_query($insertRojmel);
            $id = mysql_insert_id();
            if ($insertRojmelRes) {
                $select = "SELECT partyType FROM party WHERE partyId = " . $partyId;
                $res = mysql_query($select);
                if ($row = mysql_fetch_array($res)) {
                    $partyType = $row['partyType'];
                    if ($partyType == 'C') {
                        $a = 1;
                    } else {
                        $a = 0;
                    }
                }
                if ($a == 1) {
                    if ($acc_type == 'K') {
                        $insertClient = "INSERT INTO client (partyId,cDate,cliTransType,particular,kg,totalAmt,rojmelId)
                                            VALUES(" . $partyId . ",'" . $rojmelDate . "','" . $rojTransType . "','" . $particular . "','" . $fine . "'," . $amount . "," . $id . ")";
                        $insertClientRes = mysql_query($insertClient);
                        if (!$insertClientRes) {
                            echo "Rojmel Client Insert Fail";
                        } else {
                            header("Location:rojmel.php");
                        }
                    } else {
                        header("Location:rojmel.php");
                    }
                }
                if ($a == 0) {
                    if ($acc_type == 'K') {
                        $insertVepari = "INSERT INTO vepari(partyId,vDate,veTransType,particular,kg,totalAmt,rojmelId)
                                            VALUES(" . $partyId . ",'" . $rojmelDate . "','" . $rojTransType . "','" . $particular . "','" . $fine . "'," . $amount . "," . $id . ")";
                        $insertVepariRes = mysql_query($insertVepari);
                        if (!$insertVepariRes) {
                            echo "Rojmel Vepari Insert Fail";
                        } else {
                            header("Location:rojmel.php");
                        }
                    } else {
                        header("Location:rojmel.php");
                    }
                }
            }
        }

    }

    if ($rojmelId > 0) {
        $select = "SELECT * FROM rojmel WHERE rojmelId = " . $rojmelId;
        $res = mysql_query($select);
        if ($row = mysql_fetch_array($res)) {
            $partyId = $row['partyId'];
            $rojmelDate = $row['rojmelDate'];
            $rojTransType = $row['rojTransType'];
            $particular = $row['particular'];
            $fine = $row['fine'];
            $amount = $row['amount'];
            $acc_type = $row['acc_type'];
            $branch = $row['branch'];
        }
    }

    $k = 0;
    $clientArray = array();
    $selectClient = "SELECT partyId,partyName
	                   FROM party
					  ORDER BY partyName ASC";
    $selectClientRes = mysql_query($selectClient);
    while ($clientRow = mysql_fetch_array($selectClientRes)) {
        $clientArray['partyId'][$k] = $clientRow['partyId'];
        $clientArray['partyName'][$k] = $clientRow['partyName'];
        $k++;
    }

    $typeArrayValue[0] = 'J';
    $typeArrayOutput[0] = 'Javak';
    $typeArrayValue[1] = 'A';
    $typeArrayOutput[1] = 'Aavak';

    $sellTypeValue[0] = 'K';
    $sellTypOutput[0] = 'Khata';
    $sellTypeValue[1] = 'D';
    $sellTypOutput[1] = 'Direct';
	
	$branchInput[0] = '1';
    $branchOutput[0] = '1';

    include("./bottom.php");
    $smarty->assign("branch",$branch);
    $smarty->assign("branchOutput", $branchOutput);
    $smarty->assign("branchInput", $branchInput);
    $smarty->assign("acc_type", $acc_type);
    $smarty->assign("sellTypeValue", $sellTypeValue);
    $smarty->assign("sellTypOutput", $sellTypOutput);
    $smarty->assign("partyId", $partyId);
    $smarty->assign("rojmelDate", $rojmelDate);
    $smarty->assign("rojTransType", $rojTransType);
    $smarty->assign("particular", $particular);
    $smarty->assign("fine", $fine);
    $smarty->assign("amount", $amount);
    $smarty->assign("rojmelId", $rojmelId);
    $smarty->assign("typeArrayValue", $typeArrayValue);
    $smarty->assign("typeArrayOutput", $typeArrayOutput);
    $smarty->assign("clientArray", $clientArray);
    $smarty->display("rojmel.tpl");
}
?>