<?php
/**
 * Created by PhpStorm.
 * User: sunil
 * Date: 9/29/18
 * Time: 4:30 PM
 */

namespace App\Console\Commands;


use App\ChargedInvoices;
use App\ConsumerApplication;
use App\Http\Controllers\Admin\ConsumerApplicationController;
use App\LoanManagement;
use Carbon\Carbon;
use Illuminate\Console\Command;

class BouncePenalGenerate extends Command
{

    protected $signature = 'invoice:generate {--type=}';

    public function __construct(){
        parent::__construct();
    }


    public function handle(){
        $this->generateInvoice();
    }

    private function generateInvoice(){

        $id = [];
        $applicationController = new ConsumerApplicationController();

        $chargedInvoices = ChargedInvoices::where('updated_at', '2023-09-07 00:00:00')->get();

        $loanManagement = new LoanManagement();

        foreach ($chargedInvoices as $chargedInvoice) {

            try {

                $application = ConsumerApplication::with('application_element', 'lender', 'proFormaInvoice')->where('id', $chargedInvoice->application_id)->first();

                if ($chargedInvoice->igst) {
                    $_gstDetails = [
                        [
                            'gst_amount' => $chargedInvoice->igst,
                            'taxes' => 'IGST',
                            'tax_type' => 18,
                        ]
                    ];
                } else {
                    $_gstDetails = [
                        [
                            'gst_amount' => $chargedInvoice->cgst,
                            'taxes' => 'CGST',
                            'tax_type' => 9,
                        ],
                        [
                            'gst_amount' => $chargedInvoice->sgst,
                            'taxes' => 'SGST',
                            'tax_type' => 9,
                        ]
                    ];
                }

//                $new_invoice_id = $loanManagement->getInvoiceNumber($applicationController, $application);

                $invoice = [
                    'application_id' => $chargedInvoice->application_id,
                    'lender_id' => $chargedInvoice->lender_id,
                    'invoice_id' => $chargedInvoice->invoice_id,
                    'amount' => $chargedInvoice->amount,
                    'net_amount' => $chargedInvoice->amount - ($chargedInvoice->igst + $chargedInvoice->cgst + $chargedInvoice->sgst),
                    'igst' => $chargedInvoice->igst,
                    'cgst' => $chargedInvoice->cgst,
                    'sgst' => $chargedInvoice->sgst,
                    'invoice_date' => $chargedInvoice->invoice_date,
                    'type' => $chargedInvoice->type,
                    'version' => $chargedInvoice->version,
                    'getGSTDetails' => $_gstDetails,
                    'created_at' => Carbon::now()->toDateTimeString(),
                ];

//                ChargedInvoices::where('id', $chargedInvoice->id)->update([
//                    'invoice_id' => $new_invoice_id,
//                    'updated_at' => Carbon::now()->toDateTimeString()
//                ]);

                $responseData[] = $invoice;
                $loanManagement->saveInvoiceS3($applicationController, ['invoice' => $invoice, 'application' => $application, 'type' => $chargedInvoice->type, 'test_bot' => 1], $chargedInvoice->application_id);
            }catch (\Exception $e){
                $file = fopen("bounce_penal_".Carbon::now()->toDateString().".txt", "a") or die("Unable to open file!");
                $txt = $chargedInvoice->id;
                fwrite($file, "\n". $txt);
                fwrite($file, "\n". $e->getMessage());
                fwrite($file, "\n". $e->getLine());
                fwrite($file, "\n". $e->getTraceAsString());
                fwrite($file, "\n". '--------------------');
                fclose($file);
            }
        }
    }
}
