File: /www/exchange0old/exchange/classes/Model/MRejectedETR.php
<?php
include_once('admin/classes/Model/base_actions.php');
class Model_MRejectedETR extends Base_actions
{
//Starts Get Rejected Listing
function getRejectedListing(){
$user_id = $_SESSION['user']['userid'];
$query = new Bin_Query();
//&& invoice_authorise='1'
$sql = "SELECT * ,".
" DATE_FORMAT(rejected_date,'%Y-%m-%d') as rejected,reject_message FROM invoice_master ".
" WHERE user_id='".$_SESSION['user']['userid']."' && log_status='2' && draft='0' && root_invoice_id > 0 && revolving_draft = 0 && instalment_draft=0 ";
$query->executeQuery($sql);
$draft = $query->records;
for($i=0;$i<count($draft);$i++)
{
// $query=new Bin_Query();
$sql = "SELECT * FROM debtors_detail where debtor_id = ".$draft[$i]['debtor_id']." limit 1";
$query->executeQuery($sql);
$debtor = $query->records;
$draft[$i]['debtor_name'] = $debtor[0]['debtor_name'];
$draft[$i]['trade_name'] = $debtor[0]['trade_name'];
$draft[$i]['type'] = $debtor[0]['type'];
// $query1=new Bin_Query();
$sql1 = "SELECT nace_group_id, nace_group_code FROM `category_group`
where nace_group_id = '".$debtor[0]['nace_group_id']."' limit 1";
$query->executeQuery($sql1);
$nace_group_id = $query->records;
// $query2=new Bin_Query();
$sql2 = "SELECT nace_id, nace_code FROM `categories` where nace_id = '".$debtor[0]['nace_id']."' limit 1";
$query->executeQuery($sql2);
$nace_id = $query->records;
$draft[$i]['nace_id'] = $nace_id[0]['nace_code'];
$draft[$i]['nace_group_id'] = str_replace("Section","",$nace_group_id[0]['nace_group_code']).substr($nace_id[0]['nace_code'],0,2);
}
return $draft;
}//End of Get MyDraft Listing
function getTradeById($id){
if($_REQUEST['id'] !='' && is_numeric($_REQUEST['id'])){
$id = $_REQUEST['id'];
}else{
header('location: index.php?do=rejectedetr');
}
$query=new Bin_Query();
$sql = "SELECT m.* , d.invoice_document_path,d.invoice_support_document_path,d.purchase_order_document_path, ".
" DATE_FORMAT(m.created_at,'%d/%m/%y') as created FROM invoice_master as m , invoice_documents as d ".
" WHERE m.invoice_id ='".$id."' AND m.draft='0' && m.invoice_authorise='1' && m.log_status='2' && d.invoice_id = m.invoice_id LIMIT 1";
$query->executeQuery($sql);
$draft = $query->records;
if(count($draft) == 0){
header('location: index.php?do=rejectedetr');
}
return $draft;
}
function updateRejectTrade(){
if($_REQUEST['id'] !='' && is_numeric($_REQUEST['id'])){
$id = $_REQUEST['id'];
}else{
header('location: index.php?do=rejectedetr');
}
$user_id = $_SESSION['user']['userid'];
$sales_type = $_POST['sales_type'];
$sales_arr = explode("-",$sales_type);
$sales_type = $sales_arr[0];
$fieldSet = $sales_arr[1];
//Checking the User Details
$query=new Bin_Query();
$temp_sql = "SELECT `nace_group_id`,`nace_id` FROM `temp_registration` where user_id=".$user_id." limit 1";
$query->executeQuery($temp_sql);
$temp_reg = $query->records;
if(count($temp_reg) == 0){
header('Location: index.php?do=rejectedetr');
}
//Getting Debtor Details
// $query = new Bin_Query();
$debt_sql = "SELECT * FROM `debtors_detail` where debtor_id=".$_POST['debtor_id']." limit 1";
$query->executeQuery($debt_sql);
$debtor = $query->records;
if(count($debtor) == 0){
header('Location: index.php?do=rejectedetr');
}
//imploding the date's
if($sales_type!='4'){
$ac_date = $_POST['ac_year']."-".$_POST['ac_month']."-".$_POST['ac_date'];
$ex_date = $_POST['ex_year']."-".$_POST['ex_month']."-".$_POST['ex_date'];
$invoice_date = $_POST['invoice_year']."-".$_POST['invoice_month']."-".$_POST['invoice_date'];
$tr_date = $_POST['tr_year']."-".$_POST['tr_month']."-".$_POST['tr_date']." ".$_POST['hours'].":".$_POST['mins'].":00";
}else{
$ac_date = date('Y-m-d');
$invoice_date = date('Y-m-d');
if($_POST['etr_months']=='0'){
$etr_days = 1 * 30;
}else{
$etr_days = $_POST['etr_months'] * 30;
}
$d = new DateTime();
$d->modify($etr_days." days");
$ex_date = $d->format("Y-m-d");
$t = new DateTime();
$t->modify("15 days");
$tr_date = $t->format("Y-m-d");
}
//Buyout Setting
if($_POST['buyout'] =='') $buyout =2; else $buyout =$_POST['buyout'];
$min_advance =0;
$buy_min_advance =0;
$max_thirty_day_reserve =0;
$buy_max_thirty_day_reserve =0;
if($fieldSet=="model1"){
$min_advance = $_POST['purchase_price'];
if($_POST['buyout']=="1"){
$buy_min_advance = $_POST['buyout_price'];
}
}
else if($fieldSet=="model2"){
$max_thirty_day_reserve = $_POST['discount_price'];
if($_POST['buyout']=="1"){
$buy_max_thirty_day_reserve = $_POST['buyout_discount_price'];
}
}
else if($sales_type =="4"){
$max_thirty_day_reserve = $_POST['discount_price'];
// if($_POST['buyout']=="1"){
$buy_max_thirty_day_reserve = $_POST['buyout_discount_price'];
// }
$sales_revolving = 1;
}
$result11 = '';
$result1 = '';
$draft = 1;
$sql = "SELECT org.organisation_id FROM organisation_details org WHERE org.user_id='".$_SESSION['user']['userid']."' LIMIT 1";
$query->executeQuery($sql);
$org = $query->records[0];
$facevalue = $_REQUEST['etr_face_value'];
if($sales_type=="4"){
$sql = "UPDATE `invoice_master` SET `po_id` = '".$_POST['po']."',
`invoice_no` = '".trim($_POST['invoice_no'])."',
`root_invoice_id` = '0',
`debtor_id` = '".$_POST['debtor_id']."',
`face_value`='".$facevalue."',
`revolving` = '1',
`revolving_total` = '".$_POST['etr_no']."',
`revolving_months` = '".$_POST['etr_months']."',
`buy_out` = '".$buyout."',
`description` = '".$_POST['desc']."',
`payment_terms` = '".$_POST['payment']."',
`actual_date` = '".$invoice_date."',
`approved_date` = '".$invoice_date."',
`expected_date` = '".$ex_date."',
`trading_close_date` = '".$tr_date."',
`min_advance` = '".$min_advance."',
`max_thirty_day_reserve` = '".$max_thirty_day_reserve."',
`buy_min_advance` = '".$buy_min_advance."',
`buy_max_thirty_day_reserve` = '".$buy_max_thirty_day_reserve."',
`debx` = '".$_POST['debx']."',
`draft` = '".$draft."',
`closing_type` = '".$_POST['closing']."',
`modified_at` = NOW(),
`sales_type` = '".$_POST['etr_type']."' WHERE `invoice_id` ='".$_REQUEST['id']."' LIMIT 1 ;";
$_POST['face_value'] = $_POST['etr_face_value'];
$sales_type = $_POST['etr_type'];
}else{
$sql = "UPDATE `invoice_master` SET `po_id` = '".$_POST['po']."',
`invoice_no` = '".trim($_POST['invoice_no'])."',
`root_invoice_id` = '0',
`debtor_id` = '".$_POST['debtor_id']."',
`face_value`='".$facevalue."',
`buy_out` = '".$buyout."',
`description` = '".$_POST['desc']."',
`payment_terms` = '".$_POST['payment']."',
`actual_date` = '".$invoice_date."',
`approved_date` = '".$invoice_date."',
`expected_date` = '".$ex_date."',
`trading_close_date` = '".$tr_date."',
`min_advance` = '".$min_advance."',
`max_thirty_day_reserve` = '".$max_thirty_day_reserve."',
`buy_min_advance` = '".$buy_min_advance."',
`buy_max_thirty_day_reserve` = '".$buy_max_thirty_day_reserve."',
`debx` = '".$_POST['debx']."',
`draft` = '".$draft."',
`closing_type` = '".$_POST['closing']."',
`modified_at` = NOW(),
`sales_type` = '".$sales_type."',
`log_status` = '1'
WHERE `invoice_id` ='".$_REQUEST['id']."' LIMIT 1 ;";
}
// $query = new Bin_Query();
$query->updateQuery($sql);
$reg_id = $_REQUEST['id'];
// $query=new Bin_Query();
$temp_sql = "SELECT * FROM `invoice_master` where invoice_id=".$reg_id." LIMIT 1";
$query->executeQuery($temp_sql);
$temp_reg = $query->records;
$sales_type = $temp_reg[0]['sales_type'];
$face_value = $temp_reg[0]['face_value'];
$min_advance = $temp_reg[0]['min_advance'];
$max_thirty_day_reserve = $temp_reg[0]['max_thirty_day_reserve'];
$ex_date = $temp_reg[0]['expected_date'];
$tr_date = $temp_reg[0]['trading_close_date'];
$buy_min_advance = $temp_reg[0]['buy_min_advance'];
$buy_max_thirty_day_reserve = $temp_reg[0]['buy_max_thirty_day_reserve'];
$debx = $temp_reg[0]['debx'];
$reg_id = $temp_reg[0]['invoice_id'];
$actual_date = $temp_reg[0]['actual_date'];
if($sales_type =="1"){
$result1 = $face_value - 7;
if($debx == "1")
$result1 = $result1 - 50;
$value = $min_advance;
$tradeValues = $temp_reg[0];
$tradeValues['min_advance'] = $value;
$yield = Model_MHelpers::outrightYield($tradeValues);
$temp_reg[0]['yield'] = $yield;
$temp_reg[0]['min_advance'] = $min_advance;
$temp_reg[0]['max_thirty_day_reserve'] = $max_thirty_day_reserve;
$returnCalc = Model_MHelpers::outrightDiscountCalc($temp_reg[0]);
$tPurchase = number_format($min_advance,2, '.', ',');
$discount = $returnCalc['discount'];
if($temp_reg[0]['buy_out']=="1")
{
$result1 = $face_value - 7;
$result11 = $result1;
$tradeValues['min_advance'] = $buy_min_advance;
$ann_yield = Model_MHelpers::outrightYield($tradeValues);
}
}elseif($sales_type =="2"){
$result1 = $face_value - 7;
$rebate_refund = parent::getSiteSettings('rebate_refund');
$value = $max_thirty_day_reserve;
$tradeValues = $temp_reg[0];
$tradeValues['max_thirty_day_reserve'] = $max_thirty_day_reserve;
$yield = Model_MHelpers::managedYield($tradeValues);
$discount = $max_thirty_day_reserve;
if($temp_reg[0]['buy_out']=="1")
{
$result1 = $face_value - 7;
$result11 = $result1;
$tradeValues['max_thirty_day_reserve'] = $buy_max_thirty_day_reserve;
$ann_yield = Model_MHelpers::managedYield($tradeValues);
}
}else{
$result1 = $face_value - 7;
$rebate_refund = parent::getSiteSettings('rebate_refund');
$value = $max_thirty_day_reserve;
$tradeValues = $temp_reg[0];
$tradeValues['max_thirty_day_reserve'] = $max_thirty_day_reserve;
$yield = Model_MHelpers::performanceYield($tradeValues);
$discount = $max_thirty_day_reserve;
if($temp_reg[0]['buy_out']=="1")
{
$result1 = $face_value - 7;
$result11 = $result1;
$tradeValues['max_thirty_day_reserve'] = $buy_max_thirty_day_reserve;
$ann_yield = Model_MHelpers::performanceYield($tradeValues);
}
}
$history_sql = "UPDATE `invoice_master` SET `yield` = '".$yield."', `buy_yield` = '".$ann_yield."',".
" `return` = '".$result1."', `buy_return` = '".$result11."',`discount_rate` = '".$discount."' ,".
" bid_yield =0 , bid_return =0 WHERE `invoice_id` ='".$reg_id."' LIMIT 1";
$query = new Bin_Query();
$query->updateQuery($history_sql);
//Uploading all the Needed Documents for the Invoice
$uploadfile = array('file_invoice','file_support','file_po');
foreach($uploadfile as $key){
//Upload PDF FIle
$file = $key;
if($_FILES[$key]['tmp_name'] !=''){
$exp = explode('_',$key);
$name = $user_id;
$dir = "../documents/debtor/".$name; //Creating Folder W.r.t Debtor Name and Trader Id
if(!is_dir($dir)){
mkdir($dir, 0777);//Folder Path assigning
}
chmod($dir, 0777); //Folder Permission assigning
//File Path assigning
$path = pathinfo($_FILES[$file]["name"]);
$file_name = $user_id.'_'.$key."_".time().".".$path['extension'];
$dir_path = $dir."/".$file_name;
//Uploading File
if(!empty($_FILES[$file]["tmp_name"])){
move_uploaded_file($_FILES[$file]["tmp_name"],$dir_path);
}
chmod($dir."/".$file_name, 0777); //File Permission assigning
$filepath[$key] = "/documents/debtor/".$name."/".$file_name;
}
}
$update ='';
if($filepath['file_invoice'] !=''){
$update .= "`invoice_document_path` = '".$filepath['file_invoice']."' , ";
}elseif($_POST['invoicefile'] !=''){
$update .= "`invoice_document_path` = '/documents/debtor/".str_replace("/documents/debtor/","",$_POST['invoicefile'])."' , ";
}
if($filepath['file_support'] !=''){
$update .= "`invoice_support_document_path` = '".$filepath['file_support']."' , ";
}elseif($_POST['supportfile'] !=''){
$update .= "`invoice_support_document_path` = '/documents/debtor/".str_replace("/documents/debtor/","",$_POST['supportfile'])."' , ";
}
if($filepath['file_po'] !=''){
$update .= "`invoice_document_path` = '".$filepath['file_po']."' , ";
}elseif($_POST['pofile'] !=''){
$update .= "`purchase_order_document_path` = '/documents/debtor/".str_replace("/documents/debtor/","",$_POST['pofile'])."' , ";
}
//Inserting Data's Into Invoice Documents
if($update !=''){
// $query = new Bin_Query();
$sql1 = "UPDATE `invoice_documents` SET ".
" $update ".
" `modified_at` = '".date('Y-m-d H:i:s')."' ".
" WHERE `invoice_id` ='".$reg_id."' LIMIT 1 ; ";
$query->updateQuery($sql1);
}
// $query = new Bin_Query();
$sql = "SELECT address_email FROM address_details where ".
" address_id = '".$debtor[0]['contact_address_id']."' limit 1";
$query->executeQuery($sql);
$debtorAddress = $query->records;
//$notice = Model_MTrade::getAckNotice();
// send mail with pdf attachment
if($_POST['ack_notice'] == "1" && $debtorAddress[0]['address_email'] != "" && $draft != '1' && $notice !='3'){
}
if($reg_id) return true; else false;
}
}
?>