<?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\ConsumerApplicationFiles;
use App\Http\Controllers\Admin\ConsumerApplicationController;
use App\Http\Controllers\TestingController;
use App\Invoices;
use App\LoanManagement;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Razorpay\Api\Invoice;

class SubventionProcessingInvoice extends Command
{

    protected $signature = 'invoice:create {--invoice_type=}';

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


    public function handle($invoice_type = null){

//        if (!$invoice_type) {
//            $invoice_type = $this->option('invoice_type');
//        }

        $invoices = Invoices::whereIn('application_id', [210498,218487,223550,225906,227053,227483,227912,228194,228586,229099,229268,229791,229927,230803,231550,231558,231687,233356,233433,233494,233904,233919,233944,233957,234513,234519,234735,234739,234742,234766,234830,234904,235079,235604,235704,235904,236207,236260,236284,236817,237023,237146,237147,237148,237291,237575,237606,237846,238057,238087,238238,238322,238323,238371,238389,238457,238497,238669,238699,238793,238897,239038,239122,239196,239209,239394,239411,239622,239690,239752,239906,239924,239931,239938,239960,239983,240004,240112,240134,240246,240251,240300,240310,240477,240531,240654,240699,240773,240793,240913,240965,241015,241018,241074,241107,241110,241298,241312,241382,241450,241485,241551,241563,241578,241579,241646,241734,241826,241853,241926,241964,241984,242009,242048,242099,242103,242145,242185,242246,242283,242302,242324,242354,242372,242382,242406,242411,242427,242433,242457,242485,242546,242593,242631,242747,242750,242758,242857,242896,242944,243032,243044,243064,243103,243161,243167,243180,243190,243194,243222,243223,243224,243326,243337,243371,243377,243457,243458,243511,243515,243519,243551,243659,243786,243812,243814,243826,243831,243835,243843,243894,243895,243901,244015,244074,244091,244138,244218,244222,244310,244345,244447,244491,244494,244504,244509,244545,244563,244618,244620,244656,244659,244664,244665,244686,244698,244704,244753,244757,244839,244967,244983,245043,245049,245070,245099,245174,245193,245208,245211,245229,245232,245245,245272,245351,245387,245388,245389,245418,245446,245448,245497,245509,245562,245564,245609,245631,245670,245680,245687,245763,245784,245796,245800,245859,245885,245886,245927,245931,245975,245989,246021,246032,246058,246066,246110,246202,246240,246246,246263,246271,246301,246313,246328,246335,246337,246360,246380,246385,246395,246400,246408,246441,246546,246573,246580,246581,246619,246645,246647,246664,246708,246719,246729,246741,246795,246833,246870,246892,246905,246906,246912,246923,246939,246967,247025,247029,247155,247166,247171,247214,247234,247242,247255,247277,247329,247330,247354,247356,247369,247400,247403,247458,247501,247503,247508,247523,247527,247544,247547,247582,247584,247604,247613,247633,247641,247651,247699,247711,247716,247732,247741,247751,247752,247753,247756,247772,247792,247794,247803,247821,247822,247832,247863,247903,247936,247956,247988,248001,248021,248086,248100,248104,248105,248108,248110,248113,248120,248135,248167,248186,248250,248261,248288,248297,248338,248349,248353,248359,248364,248386,248400,248427,248438,248449,248452,248462,248467,248488,248490,248514,248527,248584,248606,248610,248613,248621,248651,248659,248670,248674,248691,248695,248698,248719,248746,248765,248789,248800,248802,248808,248812,248815,248830,248852,248888,248898,248900,248912,248938,248947,248952,248954,248962,248965,248966,248969,248970,248971,248973,248974,248989,248997,249012,249014,249018,249021,249032,249035,249039,249043,249044,249045,249054,249055,249056,249067,249079,249098,249103,249108,249110,249126,249169,249172,249176,249185,249189,249190,249196,249198,249211,249215,249220,249232,249241,249257,249258,249261,249263,249266,249268,249272,249274,249275,249283,249297,249303,249307,249316,249318,249359,249361,249372,249376,249382,249395,249396,249399,249411,249413,249425,249430,249436,249442,249456,249457,249462,249468,249479,249488,249503,249520,249521,249533,249534,249535,249538,249545,249553,249555,249565,249580,249595,249603,249605,249609,249614,249615,249616,249617,249618,249619,249623,249633,249640,249642,249647,249665,249667,249669,249675,249689,249697,249702,249707,249713,249727,249729,249749,249754,249758,249767,249788,249802,249807,249812,249823,249828,249838,249858,249910,249922,249925,249949,249951,249955,249960,249965,249968,249970,249971,249977,249982,249984,249992,250007,250012,250022,250023,250030,250033,250040,250049,250056,250073,250089,250093,250094,250100,250106,250111,250115,250116,250119,250133,250134,250136,250144,250148,250153,250159,250161,250167,250170,250176,250183,250185,250186,250198,250199,250216,250218,250219,250236,250238,250254,250257,250258,250260,250264,250279,250284,250288,250299,250321,250334,250335,250339,250340,250347,250353,250355,250364,250369,250373,250381,250392,250404,250406,250408,250415,250423,250432,250434,250436,250438,250475,250489,250491,250492,250505,250511,250525,250526,250533,250552,250606,250613,250618,250620,250639,250640,250652,250660,250665,250678,250681,250683,250732,250759,250760,250768,250778,250780,250789,250841,250847,250861,250863,250864,250873,250903,250908,250909,250910,250912,250921,250931,250972,250975,250987,250989,251000,251012,251018,251022,251028,251038,251052,251075,251084,251088,251093,251107,251123,251125,251127,251128,251130,251138,251142,251144,251153,251158,251188,251196,251226,251234,251239,251257,251278,251282,251283,251288,251290,251306,251313,251363,251371,251382,251412,251416,251426,251429,251433,251437,251442,251450,251458,251465,251472,251487,251490,251494,251510,251532,251554,251626,251646,251661,251675,251685,251747,251755,251783,251803,251819,251825,251885,251892,251893,251895,251902,251905,251907,251921,251924,251927,251936,251940,251967,251971,251988,252007,252031,252034,252062,252090,252102,252121,252161,252168,252210,252242,252250,252252,252282,252289,252320,252346,252348,252370,252390,252411,252422,252426,252512,252528,252534,252555,252585,252593,252651,252655,252670,252712,252714,252722,252772,252861,252882])->where('type', 'processing_invoice')->whereNull('deleted_at')->orderBy('created_at','desc')->get();
//        $invoices = Invoices::where('type', 'processing_invoice')->where('created_at', '>=', '2023-04-01 00:00:00')->whereNull('deleted_at')->orderBy('created_at','desc')->get();

        /* Lender based invoices */
//        $invoices = Invoices::where('type', 'processing_invoice')->whereIn('lender_id', [3,4,5])->orderBy('created_at','asc')->get(['application_id']);
//        $invoices = Invoices::where('type', 'processing_invoice')->whereIn('lender_id', [3,4,5])->orderBy('created_at','asc')->get(['application_id']);

//        $ids = [];
//        foreach ($invoices as $prod) {
//            $ids[] = $prod->application_id;
//        }
//        $files = ConsumerApplicationFiles::whereIn('consumer_application_id', $ids)->where('file_type', 'Processing Fee Invoice')->get(['consumer_application_id']);
//        $id_file = [];
//        foreach ($files as $prod) {
//            $id_file[] = $prod->consumer_application_id;
//        }
//        $rem = array_diff($ids, $id_file);
//
//        $inv = Invoices::whereIn('application_id',$rem)->where('type', 'processing_invoice')->orderBy('created_at','asc')->get();
//
        $testController = new TestingController();
        foreach ($invoices as $invoice) {

            if($invoice->application_id) {
                try {

                    if ($invoice->type == 'subvention_invoice') {
                        $testController->generateSubventionInvoice($invoice);
                    } else if ($invoice->type == 'processing_invoice') {
                        $testController->generateProcessingInvoice($invoice);
                    }

                } catch (\Exception $e) {
                    $file = fopen("invoice_" . $invoice_type . "_" . Carbon::now()->toDateString() . ".txt", "a") or die("Unable to open file!");
                    fwrite($file, "\n" . $invoice->id);
                    fwrite($file, "\n" . $e->getMessage());
                    fwrite($file, "\n" . $e->getLine());
                    fwrite($file, "\n" . $e->getTraceAsString());
                    fwrite($file, "\n" . '--------------------');
                    fclose($file);
                }
            }
        }
    }
}
