<?php
	ini_set('display_errors', 1);
	ini_set('display_startup_errors', 1);
	error_reporting(E_ALL);
	show_msg();
    
	$product_stock_log=$db->selectAll('product_stock_log','where change_type=1');



	$purchase_details=$db->selectAll('purchase_details');
	$general->arrayIndexChange($purchase_details);
	$purchase_ids=[];
	foreach($purchase_details as $pd){
		$purchase_ids[$pd['purchase_id']]=$pd['purchase_id'];
	}
	$purchase=$db->selectAllByID('purchase','id',$purchase_ids);
	$general->arrayIndexChange($purchase);
	$not_exist_purchase=[];
	$not_exist_details=[];
	foreach($purchase_details as $pd){
		if(!isset($purchase[$pd['purchase_id']])){
			$not_exist_purchase[$pd['purchase_id']]=$pd['purchase_id'];
		}
	}
	foreach($product_stock_log as $l){
		if(!isset($purchase_details[$l['reference_id']])){
			$not_exist_details[$l['reference_id']]=$l['reference_id'];
		}
	}
	$general->printArray('$not_exist_details');
	$general->printArray($not_exist_details);
	$general->printArray('$not_exist_purchase');
	$general->printArray($not_exist_purchase);
	$general->printArray($purchase);





// 	$returns=$db->selectAll('sale_return');
// 	foreach($returns as $pr){
// 		$process=$db->selectAll('sale_return_process_data','where sale_return_id='.$pr['id'].' and type='.SALE_RETURN_PROCESS_TYPE_GOOD,'','');
// 		if(!empty($process)){
// 			foreach($process as $p){
// 				$l=$db->getRowData('product_stock_log','where change_type='.ST_CH_SALE_RETURN.' and reference_id='.$p['id'],'a');
// 				if(!empty($l)){
// 					$data=[
// 						'action_time'=>$pr['approved_date']
// 					];
// 					$db->update('product_stock_log',$data,['id'=>$l['id']],'d');
// 			}
// 		}
// 	}
// 	else{
		
// 		$general->printArray($pr);
// 	}
// }
	// $purchase=$db->selectAll('production_product');
	// foreach($purchase as $pr){
	// 	$l=$db->getRowData('product_stock_log','where change_type='.ST_CH_PRODUCTION.' and reference_id='.$pr['id']);
	// 	if(!empty($l)){
	// 	$data=[
	// 		'action_time'=>$pr['date']
	// 	];
	// 	$db->update('product_stock_log',$data,['id'=>$l['id']],'d');
	// }
	// else{
	// 	$l=$db->getRowData('product_stock_log','where change_type='.ST_CH_PRODUCTION.' and reference_id='.$pr['id'],'a');
	// 	$general->printArray($pr);
	// }
	// 	if($pr['type']==PRODUCT_TYPE_PACKAGING){
			
	// 		$l=$db->getRowData('product_stock_log','where change_type='.ST_CH_PRODUCTION_SOURCE_MAN.' and reference_id='.$pr['id']);
	// 		if(!empty($l)){
	// 			$data=[
	// 				'action_time'=>$pr['date']
	// 			];
	// 			$db->update('product_stock_log',$data,['id'=>$l['id']],'d');
	// 		}
	// 		else{
	// 			$l=$db->getRowData('product_stock_log','where change_type='.ST_CH_PRODUCTION_SOURCE_MAN.' and reference_id='.$pr['id'],'');
	// 			$general->printArray($pr);
	// 		}
				
	// 	}
	// 	$products=$db->selectAll('production_product_source','where production_id='.$pr['id']);
	// 	if(!empty($products)){
	// 		foreach($products as $p){
	// 			$l=$db->getRowData('product_stock_log','where change_type='.ST_CH_PRODUCTION_SOURCE.' and reference_id='.$p['id']);
	// 			$data=[
	// 				'action_time'=>$pr['date']
	// 			];
	// 			$db->update('product_stock_log',$data,['id'=>$l['id']],'d');
	// 		}
	// 	}
	// }








    // $productions=$db->selectAll('production_product');
	// foreach($productions as $p){
	// 	$details=$db->selectAll('production_product_source',"where production_id=$p[id]");
	// 	$product_cost=0;
	// 	if(!empty($details)){
	// 		foreach($details as $d){
	// 			$cost=$d['unit_cost']*$d['quantity'];
	// 			$cost+=$d['extra_cost'];
	// 			$db->update('production_product_source',['total_cost'=>$cost],['id'=>$d['id']],'');
	// 			$product_cost+=$cost;
	// 		}
	// 		$total_cost=$product_cost+$p['extra_cost'];
	// 		$db->update('production_product',['product_cost'=>$product_cost,'total_cost'=>$total_cost],['id'=>$p['id']],'');
	// 	}
	// }