<?php
session_start();                                     /* Here session is start */
require_once("../codelibrary/inc/variables.php"); /* This file is required for database connection.  */
require_once("../codelibrary/inc/functions.php"); /* This file contain all function.  */
@extract($_REQUEST);                                 /* This is used for get the request. */
validate_user();                                     /* Validate user for open/access this page with login. */

$_SESSION['sess_load']=1;
/*Get data from prevois page to store in database*/

/* log4php start here */
include('../log4php/Logger.php');
Logger::configure('../logger-config.xml');
$logger = Logger::getLogger("-Return Sales Calculate-");
/* log4php end here */

$billID=$_POST['billID'];
$customer_id=$_POST['customer_id'];
$cartId=$_POST['cartId'];
$qnty=$_POST['qnty'];
$returnQty=$_POST['returnQty'];
echo $cartID=count($cartId);
$returnType=$_POST['returnType'];
//die;
for($i=0;$i<$cartID;$i++){
    
    $sql= mysql_query_db("select * from pharmacy_cart where cart_id='".$cartId[$i]."'");
    $line= mysql_fetch_db($sql);
    $prod= mysql_query_db("select * from pharmacy_product where id='".$line['product_id']."'");
    $fprod= mysql_fetch_db($prod);
    $slt= mysql_query_db("select * from pharmacy_salt where saltId='".$fprod['name']."'");
    $fslt= mysql_fetch_db($slt);
    
    if($returnQty[$i]>0){
		/*
        if(($line['quantity_type']==1)||($line['quantity_type']==3)){
            
            $fprod['quantity']=$fprod['quantity']+($returnQty[$i]/$fslt['tabletStrip']);
            $fprod['totalTablet']=$fprod['totalTablet']+$returnQty[$i];
            $wh=" quantity='".$fprod['quantity']."',totalTablet='".$fprod['totalTablet']."'";
            
        } 
		else if(($line['quantity_type']==2)||($line['quantity_type']==4)){
            
            if(($returnType[$i]==1)||($returnType[$i]==3)){
                
               $fprod['quantity']=$fprod['quantity']+($returnQty[$i]/$fslt['tabletStrip']);
               $fprod['totalTablet']=$fprod['totalTablet']+$returnQty[$i];
               
          } 
		  */
            $fprod['quantity']=$fprod['quantity']+$returnQty[$i];
            $fprod['totalTablet']=$fprod['totalTablet']+($returnQty[$i]*$fslt['tabletStrip']);

            $wh=" quantity='".$fprod['quantity']."',totalTablet='".$fprod['totalTablet']."'";
            
        } 
		else {
            
            $fprod['quantity']=$fprod['quantity']+$returnQty[$i];
            $wh=" quantity='".$fprod['quantity']."'";
            
        }
        //echo"update pharmacy_product set $wh where id='".$line['product_id']."'";
        mysql_query_db("update pharmacy_product set $wh where id='".$line['product_id']."'");
        
        if(($returnType[$i]==1)&&($line['quantity_type']==2)){
            $line['quantity_type']=$returnType[$i];
            
            $rtqnt=($line['quantity']*$fslt['tabletStrip'])-$returnQty[$i];
            $rtqnt=$rtqnt/$fslt['tabletStrip'];
            $updatePrice=($line['price'])*$rtqnt;
            $returnPrice=($line['price']/$fslt['tabletStrip'])*$returnQty[$i];
            ////$total_tt=$total_tt+$line['ttl_price'];
        } else {
            $rtqnt=$line['quantity']-$returnQty[$i];
            $updatePrice=($line['price'])*$rtqnt;
            //$updatePrice=$line['price']*$rtqnt;
            $returnPrice=($line['price'])*$returnQty[$i];
            ////$total_tt=$total_tt+$line['ttl_price'];
        }
        
        
        //$total_return=$total_return+$returnPrice;
        //$nn="update pharmacy_cart set quantity='$rtqnt',ttl_price='$updatePrice',lastUpdate='$curDateTime',updateBy='".$_SESSION['sess_uid']."',ipAddress='$ipAddress' where cart_id='".$cartId[$i]."'";
        //mysql_query_db("update pharmacy_cart set quantity='$rtqnt',ttl_price='$updatePrice',lastUpdate='$curDateTime',updateBy='".$_SESSION['sess_uid']."',ipAddress='$ipAddress' where cart_id='".$cartId[$i]."'");
        $nn2="insert into pharmacy_salereturn set orderId='$billID',customerId='$customer_id',productId='".$line['product_id']."',quantity='".$returnQty[$i]."',quantity_type='".$line['quantity_type']."',returnAmount='$returnPrice',returnDate='$save_date',lastUpdate='$curDateTime',updateBy='".$_SESSION['sess_uid']."',ipAddress='$ipAddress'";
        mysql_query_db("insert into pharmacy_salereturn set orderId='$billID',customerId='$customer_id',productId='".$line['product_id']."',quantity='".$returnQty[$i]."',quantity_type='".$line['quantity_type']."',returnAmount='$returnPrice',returnDate='$save_date',lastUpdate='$curDateTime',updateBy='".$_SESSION['sess_uid']."',ipAddress='$ipAddress'");
        $logger->warn("Sale return success by ".$_SESSION['sess_uname']." - ".$curDateTime." and IP: ".$ipAddress.", query is-".$nn." and ".$nn2);
    } 
}

////$_SESSION['sess_opd_msg']=1;
////header("location:inventory.php");
header("location:sale-return-print.php?billId=".$billID);
?>