function payCompany($thisdate = 0) { global $cnn_cs; global $companyInfo; global $mi_notes; global $bank_ids; global $deduct_bank; global $error_msg; global $date_hold; global $date_delay; $error_msg = "No Error"; $fdaysback = $companyInfo['cd_paydelay']; $tdaysback = $companyInfo['cd_paydelay']; if (!$deduct_bank) { $deduct_bank = $companyInfo['bank_Creditcard']; } if (!is_array($bank_ids)) { $sql = "SELECT group_concat(distinct bk.`bank_id`) as banks\r\n\t\tFROM `cs_bank` as bk left join `cs_transactiondetails` as td on bk.`bank_id` = td.bank_id \r\n\t\tWHERE td.userId='" . $companyInfo['userId'] . "'"; $result = sql_query_read($sql) or dieLog(mysql_error() . " ~ {$sql}"); $bank_ids = explode(",", mysql_result($result, 0, 0)); if (!$deduct_bank) { $deduct_bank = $companyInfo['bank_Creditcard']; } if (!$deduct_bank) { $deduct_bank = $companyInfo['bank_check']; } if (!$deduct_bank) { $deduct_bank = $companyInfo['cd_web900bank']; } } if ($companyInfo['cd_pay_bimonthly'] == 'trimonthly') { $monthDay = intval(date("d", $thisdate)); $payableToday = false; if ($monthDay == 10 || $monthDay == 20 || $monthDay == 1) { $payableToday = true; } if ($monthDay == 1) { $last20th = strtotime(date("Y-m-20", $thisdate - 10 * 60 * 60 * 24)); } if ($monthDay == 10) { $last20th = strtotime(date("Y-m-01", $thisdate)) - 24 * 60 * 60; } if ($monthDay == 20) { $last20th = strtotime(date("Y-m-10", $thisdate)); } $fdaysback = ($thisdate - $last20th) / (60 * 60 * 24); } $fdate = $fdaysback * 24 * 60 * 60; if ($fdate < 0) { $fdate = 0; } if (!$thisdate) { $thisdate = time(); } $date_hold = date("Y-m-d g:i:s", $thisdate - $fdate); $date_delay = date("Y-m-d g:i:s", $thisdate - 60); $nextPayDay = pushBackOnePeriod($thisdate); $mi_pay_info = calcReal(); $mi_pay_info['Status'] = "Paid"; $mi_pay_info['bank_ids'] = $bank_ids; $error_msg = "No Error"; if (!is_array($bank_ids)) { $error_msg = "Bank IDs Invalid"; return -1; } if (!$mi_pay_info['pay']) { $error_msg = "Not Payable: " . $mi_pay_info['pay_msg'] . "."; return -1; } // Calculate Etel Deductions $bank_id_sql = ""; foreach ($bank_ids as $bank_id) { if ($deduct_bank == $bank_id) { $mi_pay_info['BankInfo'][$bank_id]['Balance'] -= $mi_pay_info['EtelDeductions']; $mi_pay_info['BankInfo'][$bank_id]['EtelDeductions'] = $mi_pay_info['EtelDeductions']; $mi_pay_info['BankInfo'][$bank_id]['WireFee'] = $mi_pay_info['WireFee']; } } // Create an Invoice $invoiceName = quote_smart($companyInfo['companyname'] . " - " . date("l, F j, Y", $thisdate) . " - \$" . formatMoney($mi_pay_info['Balance'])); $invoice_sql = "INSERT INTO `cs_merchant_invoice` \r\n\t(`mi_title`, `mi_company_id` , `mi_date` , `mi_paydate`, `mi_balance` , `mi_deduction` , `mi_company_info`, `mi_notes` )\r\nVALUES (\r\n'{$invoiceName}', '" . $companyInfo['userId'] . "', NOW( ) , '" . date("Y-m-d", $thisdate) . "' , '" . $mi_pay_info['Balance'] . "', '" . $mi_pay_info['TotalDeductions'] . "', '" . addslashes(serialize($companyInfo)) . "', '{$mi_notes}'\r\n);\r\n"; $result = sql_query_write($invoice_sql, $cnn_cs) or dieLog(mysql_error() . " ~ {$invoice_sql}"); $invoice_id = mysql_insert_id($cnn_cs); $mib_wire_type = 'non-us'; if ($companyInfo['bank_wiremethod'] == 'ACH') { $mib_wire_type = 'us'; } // Create SubInvoice $bank_id_sql = ""; foreach ($bank_ids as $bank_id) { $balance = round($mi_pay_info['BankInfo'][$bank_id]['Balance'], 2); $deduction = round($mi_pay_info['BankInfo'][$bank_id]['EtelDeductions'], 2); $mib_monthly_fee = round($mi_pay_info['BankInfo'][$bank_id]['MonthlyFee'], 2); $mib_setup_fee = round($mi_pay_info['BankInfo'][$bank_id]['SetupFee'], 2); $mib_wire_fee = round($mi_pay_info['BankInfo'][$bank_id]['WireFee'], 2); $invoiceName = $companyInfo['companyname'] . " - " . date("l, F j, Y", $thisdate) . " - \$" . formatMoney($mi_pay_info['Balance']); $subinvoice_sql = "INSERT INTO `cs_merchant_invoice_banksub` \r\n\t\t( `mib_mi_ID` , `mib_bank_id`, `mib_company_id`, `mib_wire_fee`, `mib_wire_type`, `mib_monthly_fee`, `mib_setup_fee`, `mib_balance`, `mib_etelDeduction`)\r\n\tVALUES ('{$invoice_id}', '{$bank_id}' , '" . $companyInfo['userId'] . "' , '{$mib_wire_fee}' , '{$mib_wire_type}' , '{$mib_monthly_fee}' , '{$mib_setup_fee}' , '{$balance}', '{$deduction}');\r\n\t"; $result = mysql_query($subinvoice_sql, $cnn_cs) or dieLog(mysql_error() . " ~ {$invoice_sql}"); $mi_pay_info['BankInfo'][$bank_id]['mib_ID'] = mysql_insert_id(); } $bank_id_sql = " AND t.bank_id in (" . implode(",", $bank_ids) . ")"; // Update Transactions $transaction_sql = "UPDATE cs_transactiondetails as t \r\nleft join cs_transactiondetails as td on td.td_merchant_paid = 0\r\nand t.`transactionId` = td.`transactionId` and td.transactionDate < '{$date_hold}' and td.td_merchant_paid = 0\r\nleft join cs_transactiondetails as tdc ON tdc.td_merchant_deducted = 0\r\nand t.`transactionId` = tdc.`transactionId` and tdc.cancellationDate < '{$date_delay}'\r\nset td.td_merchant_paid = {$invoice_id}, tdc.td_merchant_deducted={$invoice_id}\r\n WHERE t.userId='" . $companyInfo['userId'] . "' {$bank_id_sql} "; $result = mysql_query($transaction_sql, $cnn_cs) or dieLog(mysql_error() . " ~ {$transaction_sql}"); // Update Company $company_sql = "UPDATE `cs_companydetails` as cd SET \r\n`cd_paid_setup_fee` = '1',\r\n`cd_last_paid_monthlyfee` = NOW( ) ,\r\n`cd_next_pay_day` = '{$nextPayDay}'\r\n WHERE cd.userId='" . $companyInfo['userId'] . "'"; $result = mysql_query($company_sql, $cnn_cs) or dieLog(mysql_error() . " ~ {$company_sql}"); $companyInfo['cd_paid_setup_fee'] = 1; $companyInfo['cd_next_pay_day'] = $nextPayDay; $companyInfo['cd_last_paid_monthlyfee'] = date("Y-m-d"); $invoice_sql = "Update `cs_merchant_invoice` set `mi_pay_info` = '" . addslashes(serialize($mi_pay_info)) . "' where mi_ID = '{$invoice_id}'"; $result = mysql_query($invoice_sql, $cnn_cs) or dieLog(mysql_error() . " ~ {$invoice_sql}"); // Update Misc Fees if ($mi_pay_info['mf_ID_list']) { $sql = "update `cs_misc_fees` set mf_paid={$invoice_id} where mf_ID in ('" . $mi_pay_info['mf_ID_list'] . "')"; $result = mysql_query($sql, $cnn_cs) or dieLog(mysql_error($cnn_cs) . " ~ {$sql}"); } return $invoice_id; }
$sessionlogin = isset($HTTP_SESSION_VARS["sessionlogin"]) ? $HTTP_SESSION_VARS["sessionlogin"] : ""; $msg = "Invalid Invoice"; if ($_REQUEST['focus'] == 'invoice') { $mi_ID = $_REQUEST['mi_ID']; $invoice_sql = "select * from cs_merchant_invoice where mi_ID ='{$mi_ID}'"; $result = mysql_query($invoice_sql, $cnn_cs) or dieLog("Cannot execute query"); if ($invoiceDetails = mysql_fetch_assoc($result)) { $mi_pay_info = unserialize($invoiceDetails['mi_pay_info']); $mi_company_info = unserialize($invoiceDetails['mi_company_info']); $companyId = $mi_company_info['userId']; $msg = $invoiceDetails['mi_title']; } $forcomp = "Invoice " . $forcomp; } else { if ($_REQUEST['focus'] == 'payDay' && $_REQUEST['thisdate'] > 0) { $nextPayDay = strtotime(pushBackOnePeriod()); $mi_pay_info = calcReal(intval($_REQUEST['thisdate'])); $date = date("F j, Y", $_REQUEST['thisdate']); $msg = "Projected Payday"; $forcomp = $msg . " " . $forcomp; $msg .= " for {$date}"; } else { if ($_REQUEST['focus'] == 'projSet' && $_REQUEST['thisdate'] > 0) { $last_pay_info = calcReal($_REQUEST['thisdate'] - $companyInfo['cd_payperiod'] * (60 * 60 * 24)); $mi_pay_info = calcReal($_REQUEST['thisdate']); $mi_pay_info['NewBalance'] = $mi_pay_info['ProjectedSales'] - $last_pay_info['ProjectedSales']; $mi_pay_info['Rollover'] = $last_pay_info['ProjectedSales']; $mi_pay_info['Balance'] = 0; $date = date("F j, Y", $_REQUEST['thisdate']); $mi_company_info = $companyInfo; $msg = "Projected Balance Estimate";
</tr> <?php $companyInfo = NULL; $display_none = "1"; //if($no_display==true) $display_none = "0"; $qry_company = "select * from {$company_table_sql} as cd where {$display_none} {$userList_sql} {$bank_sql_limit} and cd_pay_status='payable' and cd_ignore=0 order by cd_next_pay_day ASC"; //etelPrint($qry_company); $gatewayid = -1; $company_details = sql_query_read($qry_company) or dieLog("Cannot execute query: {$qry_company}"); while ($companyInfo = mysql_fetch_assoc($company_details)) { $sql = 0; $error = 0; //sleep(1); $quick_status = get_merchant_quick_status($companyInfo['userId']); while (pastPayPeriod() && $error < 1000) { $pushedBack = pushBackOnePeriod(); $sql = "Update cs_companydetails set `cd_next_pay_day` = '{$pushedBack}' where userId = '" . $companyInfo['userId'] . "'"; $companyInfo['cd_next_pay_day'] = $pushedBack; $error++; if ($error == 1000) { print "Error " . $companyInfo['userId'] . "<BR>"; } } if ($sql) { sql_query_write($sql) or dieLog(mysql_error() . " ~ {$sql}"); } $date_hold = 0; $mi_pay_info = calcReal(strtotime($companyInfo['cd_next_pay_day'])); $pay_checked = ''; if ($mi_pay_info['Status'] == 'Payable' && $quick_status['actual'] > 0) { $pay_checked = 'checked';