<?php
ini_set('display_errors', 1);

function InsertStockTransactionDetails($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, $stock_id, $qty, $per_price, $total_amount, $unit_id, $description, $transactiondate, $index_batch, $transactiontype, $transaction_id, $region_id, $boolean_inventory)
{
    try {
        // insert query of stock transaction 
        $sql = "INSERT INTO inventory_stocktransaction(workspace_id,company_id,branch_id,book_id,stock_id,qty,per_price,total_amount,unit_id,date,type,transaction_id,description,index_batch,status,region_id)
                    VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        $stmt =  $specific_db_conn->prepare($sql);
        $stmt->execute(array($workspace_id, $company_id, $branch_id, $book_id, $stock_id, $qty, $per_price, $total_amount, $unit_id, $transactiondate, $transactiontype, $transaction_id, $description, $index_batch, '1', $region_id));
        //$inventory_transaction_stockid = $specific_db_conn->lastInsertId();

        if ($transactiontype == 'purchase' and $boolean_inventory == 1) {

            $sql_update_stockdetails_currentqty_currentbal = "Update inventory_stockdetails SET current_qty = current_qty + ?  WHERE id = ? and workspace_id  = ? and company_id =? and branch_id =? and book_id =? ";
            $update_stockdetails_currentqty_currentbal = $specific_db_conn->prepare($sql_update_stockdetails_currentqty_currentbal);

            $update_stockdetails_currentqty_currentbal->execute(array($qty, $stock_id, $workspace_id, $company_id, $branch_id, $book_id));
        } else if ($boolean_inventory == 0 and $transactiontype == 'purchase') {
            //echo $total_amount;
            $sql_update_stockdetails_currentqty_currentbal = "Update inventory_stockdetails SET current_balance = current_balance + ?, current_qty = current_qty + ?  WHERE id=? and workspace_id  =? and company_id =? and branch_id =? and book_id =? ";
            $update_stockdetails_currentqty_currentbal = $specific_db_conn->prepare($sql_update_stockdetails_currentqty_currentbal);
            $update_stockdetails_currentqty_currentbal->execute(array($total_amount, $qty, $stock_id, $workspace_id, $company_id, $branch_id, $book_id));
        }


        return $stock_id;
    } catch (Exception $e) {
        return false;
    }
}

function insertDispatchInfo($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, $transaction_id, $dispatch)
{

    try {

        $doc_no = (isset($dispatch['doc_no']) and !empty($dispatch['doc_no'])) ? trim($dispatch['doc_no']) : null;
        $delivery_note = (isset($dispatch['delivery_note']) and !empty($dispatch['delivery_note'])) ? trim($dispatch['delivery_note']) : null;
        $destination = (isset($dispatch['destination']) and !empty($dispatch['destination'])) ? trim($dispatch['destination']) : null;
        $carrier_name = (isset($dispatch['carrier_name']) and !empty($dispatch['carrier_name'])) ? trim($dispatch['carrier_name']) : null;
        $bill_of_lading = (isset($dispatch['bill_of_lading']) and !empty($dispatch['bill_of_lading'])) ? trim($dispatch['bill_of_lading']) : null;
        $dispatch_date = (isset($dispatch['bill_of_lading']) and !empty($dispatch['dispatch_date'])) ? trim($dispatch['dispatch_date']) : null;
        $motor = (isset($dispatch['motor']) and !empty($dispatch['motor'])) ? trim($dispatch['motor']) : null;

        if (!empty($doc_no) or !empty($destination) or !empty($carrier_name) or !($bill_of_lading) or !empty($dispatch_date) or !empty($motor)) {
            // insert query of stock transaction
            $sql = "INSERT INTO act_dispatch_info(workspace_id,company_id,branch_id,book_id,transaction_id,doc_no,delivery_note,destination,carrier_name,bill_of_lading,dispatch_date,motor,status) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
            $stmt =  $specific_db_conn->prepare($sql);
            $stmt->execute(array($workspace_id, $company_id, $branch_id, $book_id, $transaction_id, $doc_no, $delivery_note, $destination, $carrier_name, $bill_of_lading, $dispatch_date, $motor, '1'));
            $dispatch_id = $specific_db_conn->lastInsertId();
            return $dispatch_id;
        }
        return true;
    } catch (Exception $e) {
        return false;
    }
}

function validateStockClassification($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, $ledger_entries, $transactiontype)
{

    $res = $specific_db_conn->prepare("SELECT id FROM act_ledgerdetails WHERE id = ? and  workspace_id  =? and company_id= ? and  branch_id =? and book_id =?");

    foreach ($ledger_entries as $item) {

        $ledgerid = $item['ledgerid'];

        $res->execute(array($ledgerid, $workspace_id, $company_id, $branch_id, $book_id));
        $result1 = $res->fetchAll();

        if (count($result1) != 1) {
            errorreturn('Ledger items found.');
        }
    }
}
