<?php
	include("includes/dbconfig.php");
	
	$batchitemsdata = DB::query("SELECT id FROM item_batch WHERE expiry_date <= CURRENT_DATE() AND is_active = 1");
	
	if(DB::count() > 0)
	{
		foreach($batchitemsdata as $batchitem)
		{			
			$trxnConnextn = new MeekroDB();
			$nonTrxnConnextn = new MeekroDB();
			
			$trxnConnextn->startTransaction();
			
			try
			{
				$trxnConnextn->query("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE");
				$trxnConnextn->query("SELECT * FROM item_batch WHERE id=%i FOR UPDATE", $batchitem['id']);
				
				$trxnConnextn->update
				('item_batch', 
					array
					(
						'updated_date' => $trxnConnextn->sqleval("current_timestamp()"),
						'updated_by' => 0,
						'is_active' => 0
					),
					"id=%i", $batchitem['id']
				);
				
				$trxnConnextn->update
				('item_stock_level', 
					array
					(
						'updated_date' => $trxnConnextn->sqleval("current_timestamp()"),
						'updated_by' => 0,
						'is_active' => 0
					),
					"batch=%i", $batchitem['id']
				);
				
				$trxnConnextn->commit();
				
			}
			catch(MeekroDBException $e)
			{
				$trxnConnextn->rollback();
			}
		}
	}
?>