<?php
/**
 * Created by PhpStorm.
 * User: sunil
 * Date: 12/11/18
 * Time: 2:02 PM
 */

namespace App\Imports;

use App\BulkPayments;
use App\Console\Commands\TallyBridge;
use App\Http\Controllers\Admin\ConsumerApplicationController;
use App\LoanManagement;
use App\Mandate;
use App\MandateLogs;
use App\PaymentMakerChecker;
use App\PaymentTransaction;
use App\ProFormaInvoice;
use App\Settlement;
use App\Slack;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToCollection;

class BulkManualPaymentFile implements ToCollection
{

    use Importable;
    private $request;

    public function __construct(Request $request){
        $this->request = $request;
    }

    /**
     * @param Collection $collection
     */
    public function collection(Collection $collection)
    {
        $data = $collection->toArray();

        unset($data[0]);

        $toBeProcessed = 0;
        $total = 0;
        $error = 0;
        foreach ($data as $item){
            if($item[0]) {
                try {
                    $user = User::where('email', trim($item[6]))->where('user_type', '>=', 5)->whereNotNull('email')->whereNull('deleted_at')->first(['id']);
                    $resItem = [
                        'payment_type' => trim($item[2]),
                        'mode' => trim($item[3]),
                        'paid_by' => trim($item[4]),
                        'application_id' => trim($item[0]),
                        'collected_by' => $user->id ?? 107,
                        'collected_by_type' => $item[5],
                        'adjust_with_disbursement' => !empty($item[11]) ? trim($item[11]) : 0,
                        'trasanction_id' => trim($item[7]),
                        'go_maker_check' => true,
                        'bank_id' => trim($item[1]),
                        'wallet_name' => '',
                        'payment_date' => trim($item[8]),
                        'payment_reconciliation_date' => trim($item[9]),
                        'fore_closure_date' => '',
                        'amount' => trim($item[10]),
                        'advance_amount' => 0,
                        'trasanction_acknowledgement' => '',
                        'online_payment_id' => ''
                    ];

                    PaymentMakerChecker::pendingReconcile(['application_id' => trim($item[0]), 'payment_type' => $resItem['payment_type'], 'data' => json_encode($resItem), 'maker_id' => $this->request->get('user_id'), 'checker_id' => 0, 'type' => 'payment', 'status' => 1, 'created_at' => Carbon::now()->toDateTimeString()]);
                    $toBeProcessed++;
                }catch (\Exception $e){
                    $error++;
                }

                $total++;
            }
        }

        (new Slack(new Request(['title' => 'Bulk Manual Payment Upload', 'fields' => ['Total Payment Received' => $total, 'Payment To Be Processed' => $toBeProcessed, 'Couldn\'t Processed' => $error ,'Processed By' => Auth::check() ? Auth::user()->name : 'System'], 'group' => 'payment_collection'])))->send();
    }
}
