<?php 
include 'session.php';

extract($_REQUEST);
$act=$action;


switch($act)
{
	case 'insert':		
	
		//Transaction sales main details insert
		$str_insert_main="insert into t_sales_entry_main(cgst_total, sgst_total, igst_total, notes,actu_bill_no, s_bill_date, financial_id, company_id, cus_id, total_sale_amount, discount_per, after_discount_amt, cgst_per, sgst_per, igst_per, tot_tax_amt, final_sale_amt, IsActive, UserId, createddate)values('".$txt_cgst_total."', '".$txt_sgst_total."', '".$txt_igst_total."', '".$txt_notes."','".$txt_bill_no."','".$txt_bill_date."', '".$financial_id."', '".$sel_company."', '".$sel_customer."', '".$txt_tot_sale_amt."', '".$txt_cmn_discount_per."', '".$txt_after_discount_amt."', '".$txt_cgst."', '".$txt_sgst."', '".$txt_igst."', '".$txt_tax_amt."', '".$txt_final_sale_amt."', '1', '".$_SESSION["UserId"]."', NOW())";
		
		$rslt = $db->insert($str_insert_main);	
		$Last_insert_id=$db->insert_id;
		
		//Transaction sales details insert [product or category details insert]
		if(isset($Last_insert_id) && !empty($Last_insert_id)){
			$pdt_cnt = count($hdn_pdt_cnt);
			for($i=0; $i<$pdt_cnt; $i++){
				
				if(isset($txt_pdt_sku[$i]) && !empty($txt_pdt_sku[$i]) && isset($txt_purchase_price[$i]) && !empty($txt_purchase_price[$i]))
				{			
					$str_insert_det="insert into t_sales_entry_details(s_bill_no, pdt_sku, saree_desc, pdt_img, purchase_price, sale_price, pdt_discount_percent, pdt_discount_amt, tot_amount, IsActive, UserId, financial_id, createddate) values('".$Last_insert_id."', '".$txt_pdt_sku[$i]."', '".$txt_pdt_desc[$i]."', '', '".$txt_purchase_price[$i]."', '".$txt_sale_price[$i]."', '".$txt_pdt_discount_per[$i]."',  '".$txt_pdt_discount_amt[$i]."', '".$txt_tot_amt[$i]."', '1', '".$_SESSION["UserId"]."', '".$_SESSION["Financial_id"]."', NOW())";
													
					$db->insert($str_insert_det);	
					
				}
				
			}
			
			//payment details add
			$rslt_payment = $db->insert("insert into t_sales_payment(s_bill_no, tot_payment_amt, tot_paid_amt, due_amt, financial_id, IsActive, UserId, createddate)values('".$Last_insert_id."', '".$txt_final_sale_amt."', '0.00', '".$txt_final_sale_amt."', '".$_SESSION["Financial_id"]."', '1', '".$_SESSION["UserId"]."', NOW())");
		
		}
		
		echo json_encode(array("rslt"=>"1")); //success
	
	break;
	
	
	case 'update':
         $update_arr = array();	 	
	//$edit_id			
		$str_update_main="update t_sales_entry_main set cgst_total='".$txt_cgst_total."', sgst_total='".$txt_sgst_total."', igst_total='".$txt_igst_total."', notes='".$txt_notes."',s_bill_date='".$txt_bill_date."', financial_id='".$financial_id."', company_id='".$sel_company."', cus_id='".$sel_customer."',total_sale_amount='".$txt_tot_sale_amt."', discount_per='".$txt_cmn_discount_per."', after_discount_amt='".$txt_after_discount_amt."', cgst_per='".$txt_cgst."', sgst_per='".$txt_sgst."', igst_per='".$txt_igst."', tot_tax_amt='".$txt_tax_amt."', final_sale_amt='".$txt_final_sale_amt."', UserId='".$_SESSION["UserId"]."' where s_bill_no='".$edit_id."'   ";		
		$rslt = $db->insert($str_update_main);	
		
		//Transaction purchase details update [product or category details update]
		$pdt_cnt = count($hdn_pdt_cnt);
		for($i=0; $i<$pdt_cnt; $i++){			
			
			if(!empty($hdn_prev_sv_id[$i])){
				//update
				
				$str_update_det="update t_sales_entry_details set pdt_sku='".$txt_pdt_sku[$i]."', saree_desc='".$txt_pdt_desc[$i]."', purchase_price='".$txt_purchase_price[$i]."', sale_price='".$txt_sale_price[$i]."', pdt_discount_percent='".$txt_pdt_discount_per[$i]."', pdt_discount_amt='".$txt_pdt_discount_amt[$i]."', tot_amount='".$txt_tot_amt[$i]."', UserId='".$_SESSION["UserId"]."' where s_bill_det_id='".$hdn_prev_sv_id[$i]."'  ";
								
				$update_arr[] = $hdn_prev_sv_id[$i];									
				$db->insert($str_update_det);
				
			}else{
				//insert								
				$str_insert_det="insert into t_sales_entry_details(s_bill_no, pdt_sku, saree_desc, pdt_img, purchase_price, sale_price, pdt_discount_percent, pdt_discount_amt, tot_amount, IsActive, UserId, financial_id, createddate) values('".$edit_id."', '".$txt_pdt_sku[$i]."', '".$txt_pdt_desc[$i]."', '', '".$txt_purchase_price[$i]."', '".$txt_sale_price[$i]."', '".$txt_pdt_discount_per[$i]."',  '".$txt_pdt_discount_amt[$i]."', '".$txt_tot_amt[$i]."', '1', '".$_SESSION["UserId"]."', '".$_SESSION["Financial_id"]."', NOW())";
											
				$db->insert($str_insert_det);
				$update_arr[] = $db->insert_id;	
			}
		}
                //Update Sales Detail
                $c_update_ids = implode(',',$update_arr);
                $rslt_payment = $db->insert("delete from t_sales_entry_details where s_bill_no='".$edit_id."' AND s_bill_det_id NOT IN (".$c_update_ids.")");		
		//payment details update
		$rslt_payment = $db->insert("update t_sales_payment set tot_payment_amt='".$txt_final_sale_amt."', tot_paid_amt='0.00', due_amt='".$txt_final_sale_amt."', UserId='".$_SESSION["UserId"]."' where s_bill_no='".$edit_id."'  ");
		
		echo json_encode(array("rslt"=>"2")); //update success
		
	break;
	
	case 'del':
	  $edit_id = base64_decode($Id);
	  
	  $chk_ref = check_reference($db, $edit_id);
	  
	  if($chk_ref == 1){
		   $str="update m_customer set IsActive = '2', UserId='".$_SESSION["UserId"]."'  where cus_id = '".$edit_id."'";
		  $db->insert($str); 	 
		  
		  $db->insert_log("delete","m_customer",$edit_id,"Customer deleted","Customer",$str);
		  echo json_encode(array("rslt"=>"5")); //deletion
	  }
	  else{
		 echo json_encode(array("rslt"=>"-1", "err_msg"=>"Reference exists with other data. Cannot Perform the action.")); //deletion  
	  }	 		
	break;
	
	case 'changestatus':
	  $edit_id = base64_decode($Id);
	  $status = $actval;
	  
	  $chk_ref = check_reference($db, $edit_id);
	  
	  if($chk_ref == 1){
			$str="update m_customer set IsActive = '".$status."', UserId='".$_SESSION["UserId"]."'  where cus_id = '".$edit_id."'";
			$db->insert($str); 	 
		  
			$db->insert_log("changestatus","m_customer",$edit_id,"Customer status changed","Customer",$str);
			echo json_encode(array("rslt"=>"6")); //status update
	  }
	  else{
			echo json_encode(array("rslt"=>"-1", "err_msg"=>"Reference exists with other data. Cannot Perform the action.")); //deletion  
	  }	 
	  	 
		
	break;
	
	case 'del_pdt_det':
		if(isset($pdt_det_id) && !empty($pdt_det_id)){
			$rslt_pdt_det = $db->get_a_line("select t1.*, t2.s_payment_id, t2.tot_paid_amt from t_sales_entry_details t1 inner join t_sales_payment t2 on t1.s_bill_no = t2.s_bill_no where 1=1 and t1.s_bill_det_id='".$pdt_det_id."' ");
						
			if(isset($rslt_pdt_det['tot_paid_amt']) && $rslt_pdt_det['tot_paid_amt']=='0.00' ){	
				$str="update t_sales_entry_details set IsActive = '2', UserId='".$_SESSION["UserId"]."'  where s_bill_det_id = '".$pdt_det_id."'";
				$db->insert($str); 
				
				echo json_encode(array("rslt"=>"1"));  
			}else{
				echo json_encode(array("rslt"=>"-9", "err_msg"=>"Reference exists with payment information. Cannot Perform the action."));  
			}
			
		}
		else{
			echo json_encode(array("rslt"=>"-1"));  
		}	 
	break;	
	
}


function check_reference($db, $customer_id){
	return 0;
}


?>