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

function InsertBatchDetails($specific_db_conn, $workspace_id, $company_id, $branch_id, $book_id, $stock_id, $inventory_batch_details, $added_by, $batch_type, $index_batch, $transaction_date, $transaction_id = null, $default_barcode_id = null)
{

    // print_r($inventory_batch_details);
    try {
        $amount =  0.00;
        $qty = 0;

        // veryifying that each ropw is apt in date
        if (count($inventory_batch_details) == 1 and $batch_type == 'opening') {
            // directly insert
        } else {
            foreach ($inventory_batch_details as $element) {
                // if (empty($element['rate']) or !is_numeric($element['rate']) or empty($element['qty']) or !is_numeric($element['qty'])) {
                //     errorreturn("Invalid batch details. Rate or Quantity cannot be non numeric or less than 1.");
                // }
            }
        }
        if ($batch_type != 'opening' and $batch_type != 'purchase' and $batch_type != 'sales') {
            return false;
        }

        foreach ($inventory_batch_details as $data) {

            $stock_barcode = (isset($data['stock_barcode_uin']) and !empty($data['stock_barcode_uin'])) ? $data['stock_barcode_uin'] : null;
            // $index_batch = (isset($data['index_batch']) and !empty($data['index_batch'])) ? $data['index_batch'] : null;
            if ($batch_type == 'opening' and count($inventory_batch_details) == 1) {
                // $rate = (isset($data['rate']) and is_numeric($data['rate']) and floatval($data['rate']) >= 0) ? $data['rate'] : errorreturn('Error Rate');
                $rate = (isset($data['rate'])  && (is_numeric($data['rate']) || $data['rate'] === '0')  && floatval($data['rate']) >= 0) ? $data['rate'] : errorreturn('Error Rate');

                $qty = (isset($data['qty']) and is_numeric($data['qty']) and floatval($data['qty']) >= 0) ? $data['qty'] : errorreturn('Error Quantity');
            } else {
                // $rate = (isset($data['rate']) and !empty($data['rate']) and floatval($data['rate']) > 0 and is_numeric($data['rate'])) ? $data['rate'] : errorreturn('Error Rate');
                $rate = (isset($data['rate'])  && (is_numeric($data['rate']) || $data['rate'] === '0')  && floatval($data['rate']) >= 0) ? $data['rate'] : errorreturn('Error Rate');

                if ($batch_type == 'sales') {
                    $qty = (isset($data['qty']) and !empty($data['qty']) and floatval($data['qty']) > 0) ? - ($data['qty']) : errorreturn('Error Quantity');
                } else {
                    $qty = (isset($data['qty']) and !empty($data['qty']) and floatval($data['qty']) > 0) ? $data['qty'] : errorreturn('Error Quantity');
                }
            }


            $expiry = (isset($data['expiry']) and !empty($data['expiry'])) ? trim(date('Y-m-d', strtotime($data['expiry']))) : null;
            $mfg_date = (isset($data['mfg_date']) and !empty($data['mfg_date'])) ? trim(date('Y-m-d', strtotime($data['mfg_date']))) : null;
            $remarks = (isset($data['remarks']) and !empty($data['remarks'])) ? $data['remarks'] : null;

            $amount =  $rate *  $qty;
            if ($stock_barcode ==  null) {

                $res = $specific_db_conn->prepare("SELECT stock_name FROM  inventory_stockdetails WHERE id = ? and workspace_id  =? and company_id=? and branch_id=? and book_id =?");
                $res->execute(array($stock_id, $workspace_id, $company_id, $branch_id, $book_id));
                $result1 = $res->fetchAll();

                $stock_barcode = $result1[0]['stock_name'];
            }


            $time_created = time();
            // insert query of stock transaction 
            $sql = "INSERT INTO inventory_batch_details(workspace_id, company_id, branch_id, book_id,stock_id,stock_barcode_uin,transaction_id,date,type,rate,qty,expiry,mfg_date,amount,remarks,index_batch,added_by,date_created,time_created)
                VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            $stmt =  $specific_db_conn->prepare($sql);
            $stmt->execute(array($workspace_id, $company_id, $branch_id, $book_id, $stock_id, $stock_barcode, $transaction_id, $transaction_date, $batch_type, $rate, $qty, $expiry, $mfg_date, $amount, $remarks, $index_batch, $added_by, date('Y-m-d'), $time_created));
        }
        return true;
    } catch (Exception $e) {
        return false;
    }
}
