<?php
	include("../class/class.general.php");
	include("../class/class.db.php");
	include("../class/class.somiti.php");
	include("../class/class.acc.php");
	include("../class/messages.php");
	include("../class/class.mail.php");
	$general= new General();
	$db     = new DB($general);
	$smt    = new SMT($general,$db);
	$acc    = new ACC($general,$db,$smt);
	include("../cus_define.php");

	$products=$db->selectAll($general->table(104),'where isActive=1');
	$stockData=[];
	foreach($products as $u){
		$stockAmount    = 0;
		$stockDmAmount  = 0;
		$pStock         = 0;
		$pStockDamage   = 0;
		if($u['stock']>0){
			$pStock=$u['stock'];
			$stockAmount=$u['stock']*$u['pBuyPrice'];
		}
		if($u['pStockDamage']>0){
			$pStockDamage=$u['pStockDamage'];
			$returProducts=$db->selectAll($general->table(16),'where inStock>0 and pID='.$u['pID'].' and srType='.PRODUCT_TYPE_DAMAGE.' group by srID','srID,(unitPrice*inStock) as amount');
			//$general->printArray($returProducts);
			if(!empty($returProducts)){
				foreach($returProducts as $rp){
					$stockDmAmount+=$rp['amount'];
				}
			}
			$openingProducts=$db->selectAll($general->table(3),'where inStock>0 and pID='.$u['pID'].' and posType='.PRODUCT_TYPE_DAMAGE.' group by posID','posID,(unitPrice*inStock) as amount');
			//$general->printArray($returProducts);
			if(!empty($openingProducts)){
				foreach($openingProducts as $rp){
					$stockDmAmount+=$rp['amount'];
				}
			}

		}
		//if($pStock>0||$pStockDamage>0){
		$stockData[$u['pID']]=[
			'pID'=>$u['pID'],
			'supID'=>$u['supID'],
			'scID'=>$u['scID'],
			's'=>$pStock,
			'sa'=>$stockAmount,
			'd'=>$pStockDamage,
			'da'=>$stockDmAmount
		];
		//}
	}
	//$general->printArray(count($stockData));
	//$general->printArray($stockData);
	if(!empty($stockData)){
		foreach($stockData as $st){
			
			$oldDmStock=0;
			$oldGdStock=0;
			$oldStock=$db->getRowData($general->table(27),'where pID='.$st['pID'].' and stockType='.PRODUCT_TYPE_GOOD.' order by actionDate desc');
			if(!empty($oldStock)){
					$oldGdStock=$oldStock['stock'];
			}
			$oldStock=$db->getRowData($general->table(27),'where pID='.$st['pID'].' and stockType='.PRODUCT_TYPE_DAMAGE.' order by actionDate desc');
			if(!empty($oldStock)){
					$oldDmStock=$oldStock['stock'];
			}
			if($oldGdStock!=$st['s']){
				$data=[
					'supID'         => $st['supID'],
					'scID'          => $st['scID'],
					'pID'           => $st['pID'],
					'stockType'     => PRODUCT_TYPE_GOOD,
					'stock'        => $st['s'],
					'pStockAmount'  => $st['sa'],
					'createdOn'     => TIME,
					'actionDate'    => YESTERDAY_TIME
				];
				$insert=$db->insert($general->table(27),$data);
			}
			if($oldDmStock!=$st['d']){
				$data=[
					'supID'         => $st['supID'],
					'scID'          => $st['scID'],
					'pID'           => $st['pID'],
					'stockType'     => PRODUCT_TYPE_DAMAGE,
					'stock'        => $st['d'],
					'pStockAmount'  => $st['da'],
					'createdOn'     => TIME,
					'actionDate'    => YESTERDAY_TIME
				];
				$insert=$db->insert($general->table(27),$data);
			}
		}
	}
?>