riainvoice/application/modules/payments/controllers/Payments.php
2023-05-27 19:26:12 +05:30

694 lines
21 KiB
PHP

<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
* InvoicePlane
*
* @author InvoicePlane Developers & Contributors
* @copyright Copyright (c) 2012 - 2017 InvoicePlane.com
* @license https://invoiceplane.com/license.txt
* @link https://invoiceplane.com
*/
/**
* Class Payments
*/
class Payments extends Admin_Controller
{
/**
* Payments constructor.
*/
public function __construct()
{
parent::__construct();
$this->load->model('mdl_payments');
$this->load->model('invoices/mdl_invoices');
}
/**
* @param int $page
*/
public function index($page = 0)
{
$this->mdl_payments->paginate(site_url('payments/index'), $page);
$payments = $this->mdl_payments->result();
//print_r($payments);
$this->layout->set(
array(
'payments' => $payments,
'filter_display' => true,
'filter_placeholder' => trans('filter_payments'),
'filter_method' => 'filter_payments'
)
);
$this->layout->buffer('content', 'payments/index');
$this->layout->render();
}
/**
* @param null $id
*/
public function form($id = null)
{
if ($this->input->post('btn_cancel')) {
redirect('payments');
}
if ($this->mdl_payments->run_validation()) {
$id = $this->mdl_payments->save($id);
$this->load->model('custom_fields/mdl_payment_custom');
$this->mdl_payment_custom->save_custom($id, $this->input->post('custom'));
redirect('payments');
}
if (!$this->input->post('btn_submit')) {
$prep_form = $this->mdl_payments->prep_form($id);
if ($id and !$prep_form) {
show_404();
}
$this->load->model('custom_fields/mdl_payment_custom');
$this->load->model('custom_values/mdl_custom_values');
$payment_custom = $this->mdl_payment_custom->where('payment_id', $id)->get();
if ($payment_custom->num_rows()) {
$payment_custom = $payment_custom->row();
unset($payment_custom->payment_id, $payment_custom->payment_custom_id);
foreach ($payment_custom as $key => $val) {
$this->mdl_payments->set_form_value('custom[' . $key . ']', $val);
}
}
} else {
if ($this->input->post('custom')) {
foreach ($this->input->post('custom') as $key => $val) {
$this->mdl_payments->set_form_value('custom[' . $key . ']', $val);
}
}
}
$this->load->helper('custom_values');
$this->load->model('invoices/mdl_invoices');
$this->load->model('payment_methods/mdl_payment_methods');
$this->load->model('custom_fields/mdl_custom_fields');
$this->load->model('custom_values/mdl_custom_values');
$open_invoices = $this->mdl_invoices
->where('ip_invoice_amounts.invoice_balance >', 0)
->or_where('ip_invoice_amounts.invoice_balance <', 0)
->get()->result();
$Client_details= $this->mdl_invoices->GetClient_Details();
// print_r($open_invoices);
$custom_fields = $this->mdl_custom_fields->by_table('ip_payment_custom')->get()->result();
$custom_values = [];
foreach ($custom_fields as $custom_field) {
if (in_array($custom_field->custom_field_type, $this->mdl_custom_values->custom_value_fields())) {
$values = $this->mdl_custom_values->get_by_fid($custom_field->custom_field_id)->result();
$custom_values[$custom_field->custom_field_id] = $values;
}
}
$fields = $this->mdl_payment_custom->get_by_payid($id);
//$viewpayment=$this->mdl_payments->Get_PaymentDetails($id);
//print_r($viewpayment);
foreach ($custom_fields as $cfield) {
foreach ($fields as $fvalue) {
if ($fvalue->payment_custom_fieldid == $cfield->custom_field_id) {
// TODO: Hackish, may need a better optimization
$this->mdl_payments->set_form_value(
'custom[' . $cfield->custom_field_id . ']',
$fvalue->payment_custom_fieldvalue
);
break;
}
}
}
$amounts = array();
$invoice_payment_methods = array();
//print_r($amounts);
foreach ($open_invoices as $open_invoice)
{
$amounts['invoice' . $open_invoice->invoice_id] = format_amount($open_invoice->invoice_balance);
$invoice_payment_methods['invoice' . $open_invoice->invoice_id] = $open_invoice->payment_method;
}
$lastrecid= $this->mdl_payments->GetReceiptCount();
$rid='';
foreach($lastrecid as $rid)
{
$rid=$rid->rcount;
}
if($rid<=0)
{
$lastpayid='REC1';
}
else
{
$lastrid=$this->mdl_payments->GetReceiptid();
foreach($lastrid as $r)
{
$ri=$r->lrid;
}
}
$rid=(explode("C",$ri));
//print_r($rid);
$this->layout->set(
array(
'payment_id' => $id,
'payment_methods' => $this->mdl_payment_methods->get()->result(),
'open_invoices' => $open_invoices,
'custom_fields' => $custom_fields,
'custom_values' => $custom_values,
'amounts' => json_encode($amounts),
'client_name' => $Client_details,
'viewpayment'=>$this->mdl_payments->Get_PaymentDetails($id),
'PaymentLineDetails'=> $this->mdl_payments->Get_InvoicelistPayment($id),
'lastpayid'=> $this->mdl_payments->Get_LastPaymentid(),
'lastrecid'=> $this->mdl_payments->GetReceiptCount(),
'lastrid'=>$this->mdl_payments->GetReceiptid(),
// 'amountid' => json_encode($amountid),
'invoice_payment_methods' => json_encode($invoice_payment_methods)
)
);
if ($id) {
$this->layout->set('payment', $this->mdl_payments->where('ip_payments.payment_id', $id)->get()->row());
}
$this->layout->buffer('content', 'payments/form');
$this->layout->render();
}
/**
* @param $id
*/
public function delete($id)
{
$this->mdl_payments->delete($id);
redirect('payments');
}
public function Get_Invoice()
{
$client_id=$this->input->post('id');
$from_date=$this->input->post('from_date');
$to_date=$this->input->post('to_date');
$fdate=date("Y-m-d", strtotime($from_date) );
$tdate=date("Y-m-d",strtotime($to_date));
$Invoice_list = $this->mdl_payments->Get_Invoicelist($client_id,$fdate,$tdate);
$obj=json_encode($Invoice_list);
//die(json_encode(array('InvDetail' =>$Invoice_list)));
echo $obj;
}
public function Invoice_Clearence()
{
$all=$this->input->post('al');
$jj=$this->input->post('j');
$RowCount=$this->input->post('RowCount');
// echo $RowCount;
$invoice_id=$this->input->post('Inid');
$dt = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
$payment_date = $dt->format('Y-m-d');
//$payment_amount=$this->input->post('Payment');
$totalPayment=$this->input->post('TotPay');
$paymentmethod=$this->input->post('paymentmethod');
$note=$this->input->post('paynote');
$balance=$this->input->post('Balance');
$payment_amount = $this->input->post('inv_amount');
//$PayBalance=$balance - $payment_amount;
$receiptno= $this->input->post('pid');
$totalamt= $this->input->post('totalamt');
$EarlierPaid= $this->mdl_payments->GetEarlyPayment($invoice_id);
$PaidAmount=0;
foreach($EarlierPaid as $Pay)
{
$PaidAmount= $Pay->invoice_paid;
}
$InvId_check= $this->mdl_payments->CheckInvId($invoice_id);
$payment_amount1= $PaidAmount + $payment_amount;
$PayBalance= $totalPayment - $payment_amount1;
//echo $i;
if($PayBalance>0)
{
$ip_status=2;
}
else
{
$ip_status=4;
}
$balance_array=array('invoice_balance'=>$PayBalance,'invoice_paid'=>$payment_amount1);
$inv_balance=$this->mdl_payments->Save_Balance($balance_array,$invoice_id);
$invstatusarray=array('invoice_status_id'=>$ip_status,'payment_method'=>$paymentmethod);
$statusupdation=$this->mdl_payments->Save_invstatus($invstatusarray,$invoice_id);
$payt=array('payment_method_id'=>$paymentmethod,'payment_date'=>$payment_date);
$paymenttable=array('invoice_id'=>$invoice_id,'payment_method_id'=>$paymentmethod,'payment_date'=>$payment_date,'payment_amount'=>$payment_amount,'payment_note'=>$note,'receipt_no'=>$receiptno,'total_payment'=>$totalamt);
// if(count($InvId_check)==0)
// {
$paytable=$this->mdl_payments->Save_Paytable($paymenttable);
//$paytable=$this->mdl_payments->Save_Paytable($payt);
//}
// else
// {
// $paytable=$this->mdl_payments->Update_Paytable($paymenttable,$invoice_id);
// }
if(($inv_balance==1) && ($statusupdation==1) && ($paytable==1)&& ($all==$jj))
{
echo "Saved Successfully";
}
else if(($inv_balance!=1) && ($statusupdation!=1) && ($paytable!=1))
{
echo "Something went wrong Please Try Again";
}
//$email=$this->mdl_payments->GetEmailID($paymenttable);
// $email_from = 'sriramv63@gmail.com';
// $this->load->helper('template');
// $this->load->model('email_templates/mdl_email_templates');
// $email_from = 'sales@resicoindustries.com';
// $email = 'nidhilaem@gmail.com';
// //$email_message = 'Invoice Payment have been done';
// // $data = $this->layout->set(
// // array(
// // 'invoicedetails' =>$this->mdl_payments->getinvoicedetails($receiptno),
// // 'viewpayment'=>$this->mdl_payments->Get_PaymentDetails($receiptno),
// // 'PaymentLineDetails'=> $this->mdl_payments->Get_InvoicelistPayment($receiptno),
// // )
// // );
// $this->load->helper('mailer');
// if (mailer_configured())
// {
// $this->load->helper('mailer/phpmailer');
// // print_r($data);die;
// $email_message = '<html></html>';
// $email_message .='<div id="headerbar">
// <h1 class="headerbar-title">21</h1>
// <div class="headerbar-item pull-right">
// <div class="btn-group btn-group-sm">
// </div>
// </div>
// </div><br/><table border = 1px solid red>
// <thead><tr><th>S.No</th><th>Amount</th></tr></thead><tbody><tr><td>1</td><td>150</td></tr></tbody></table>';
// $email_template = $this->mdl_email_templates->get_by_id(2);
// if (!phpmail_send($email_from, $email, trans('Invoice Payment'), $email_message))
// {
// $email_failed = true;
// }
// }
// else
// {
// $this->load->library('email');
// // Set email configuration
// $config['mailtype'] = 'html';
// $this->email->initialize($config);
// // Set the email params
// $this->email->from($email_from);
// $this->email->to($email);
// $this->email->subject('test');
// $this->email->message($email_message);
// // Send the reset email
// if (!$this->email->send())
// {
// $email_failed = true;
// log_message('error', $this->email->print_debugger());
// }
// }
}
function GetReceiptno()
{
$receipt=$this->mdl_payments->GetReceiptid();
if(!empty($receipt)){
$receipt = $receipt;
}else{
$receipt = 1;
}
echo json_encode($receipt);die;
}
function ShowPaymentView($id)
{
// echo $id;
$this->layout->set(
array(
'viewpayment' => $this->mdl_payments->Get_PaymentDetails($id),
'PaymentLineDetails' => $this->mdl_payments->Get_InvoicelistPayment($id),
'invoicedetails' => $this->mdl_payments->getinvoicedetails($id),
)
);
$this->layout->buffer('content', 'payments/paymentslip');
$this->layout->render();
}
function ShowEmailView($id)
{
// echo $id;
$this->layout->set(
array(
'viewpayment' => $this->mdl_payments->Get_PaymentDetails($id),
'PaymentLineDetails' => $this->mdl_payments->Get_InvoicelistPayment($id),
'invoicedetails' => $this->mdl_payments->getinvoicedetails($id),
)
);
$this->layout->buffer('content', 'payments/editform');
$this->layout->render();
}
function Show_PaymentSlip()
{
$receiptno= $this->input->post('pid');
$invoicedetails =$this->mdl_payments->getinvoicedetails($receiptno);
$viewpayment=$this->mdl_payments->Get_PaymentDetails($receiptno);
$PaymentLineDetails=$this->mdl_payments->Get_InvoicelistPayment($receiptno);
$userdetails=$this->mdl_payments->GetuserDetails(1);
if(!empty($userdetails))
{
foreach ($userdetails as $user)
{
$user_company=$user->user_company;
$user_address_1=$user->user_address_1;
$user_address_2=$user->user_address_2;
$user_city=$user->user_city;
$user_state=$user->user_state;
$user_zip=$user->user_zip;
}
}
if(!empty($invoicedetails))
{
foreach($invoicedetails as $inv)
{
$inv_id= $inv->invoice_id;
$receiptno=$inv->receipt_no;
$clientsname=$inv->client_name;
$clientphone= $inv->client_phone;
$clientemail= $inv->client_email;
$client_address_1=$inv->client_address_1;
$client_address_2=$inv->client_address_2;
$client_city=$inv->client_city;
$client_state= $inv->client_state;
$client_zip= $inv->client_zip;
$pdt=new DateTime($inv->payment_date,new DateTimeZone('Asia/Kolkata'));
$paydt=$pdt->format('d-m-Y');
$Paymethod=$inv->payment_method_name;
$totalamt=$inv->total_payment;
$client_id=$inv->client_id;
}
}
$Emailid=$this->mdl_payments->Get_mailid($client_id);
if(!empty($Emailid))
{
foreach($Emailid as $eid)
{
$clientid=$eid->client_email;
}
}
//echo $clientid;
//echo "foreach out".$invoice_number;die;
$data = $this->layout->set(
array(
'invoicedetails' =>$this->mdl_payments->getinvoicedetails($receiptno),
'viewpayment'=>$this->mdl_payments->Get_PaymentDetails($receiptno),
'PaymentLineDetails'=> $this->mdl_payments->Get_InvoicelistPayment($receiptno),
)
);
$this->load->helper('template');
$this->load->model('email_templates/mdl_email_templates');
$email_from = 'sales@resicoindustries.com';
//$email = 'nidhilaem@gmail.com';
$email = $clientid;
$this->load->helper('mailer');
if (mailer_configured())
{
$this->load->helper('mailer/phpmailer');
// print_r($data);die;
$email_message ='Dear customer, payment for the following invoices have been done<br><br>';
$email_message .='<div>
'.'<h1>'.$user_company.'</h1><br> '.$user_address_1.'<br> '.$user_address_2.'<br>'.$user_city.'<br> '.$user_state .'<br>'.$user_zip.'
</div>';
$email_message .= '<html>
<head>
<header> <hr>
<h2 align="center"><u>PAYMENT RECEIPT</u></h2></header>
<style>
#tab
{
border-collapse: collapse;
width: 80%;
}
#tab > th, td {
padding: 8px;
text-align: center;
border-bottom: 1px solid #ddd;
}
</style>
</head>';
// $email_message .= '<div class="row">
// <div class="col-xs-12 col-md-9 clearfix">
// <div class="pull-left">
// <table class="table table-bordered">
// <tr>
// <td>Payment Date:</td>
// <td>'.$paydt.'</td>
// </tr>
// <tr>
// <td>Receipt Method:</td>
// <td>'.$Paymethod.'</td>
// </tr>
// </table>
// </div>
// </div>
// </div><br>';
$email_message .= '<br><div>
<table style="width:60%">
<tr>
<td>Payment Date :</td>
<td>'.$paydt.'</td>
<td style="background:#19840f;color:#fff;">Total Receipt<br></td>
</tr>
<tr>
<td>Receipt Method:</td>
<td>'.$Paymethod.'</td>
<td style="background:#19840f;color:#fff;">RS : '.$totalamt.'<br></td>
</tr>
</table>
</div>';
$email_message .='<br><br><div style="text-align: left"><b><font size="4">
Received From:</b></font><br>'.$clientsname.'<br>'.$client_address_1.'<br>'.$client_address_2.'
<br>'.$client_city.'<br>'.$client_state ." - " .$client_zip.'<br><br>';
$email_message .='<div style="text-align: left"><b><font size="4">
Towards Payment For</font></b></div><br>';
$email_message .= "
<div><table id='tab' border='1'>
<thead>
<tr>
<th>Invoice No</th>
<th>Item Description</th>
<th>Quantity</th>
<th>Sales With Tax</th>
<th>Amount Paid</th>
</tr>
</thead>
<tbody>";
foreach($PaymentLineDetails as $pay)
{
$amount_paid = ($pay->invoice_total-$pay->invoice_balance);
$email_message .="<tr>
<td>".$pay->invoice_number."</td>
<td>".$pay->item_description."</td>
<td>".$pay->item_quantity."</td>
<td>".$pay->invoice_total."</td>
<td>".$pay->payment_amount."</td>
</tr>
";
}
$email_message .= "</tbody>
</table><br><h3>
Total".'-'.$totalamt.'</h3></html>' ;
if (!phpmail_send($email_from, $email, trans('Invoice Payment'), $email_message))
{
$email_failed = true;
}
}
else
{
$this->load->library('email');
// Set email configuration
$config['mailtype'] = 'html';
$this->email->initialize($config);
// Set the email params
$this->email->from($email_from);
$this->email->to($email);
$this->email->subject('test');
$this->email->message($email_message);
// Send the reset email
if (!$this->email->send())
{
$email_failed = true;
log_message('error', $this->email->print_debugger());
}
}
}
}