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

namespace App\Imports;

use App\ApplicationEMICalculation;
use App\ApprovedPincodeLists;
use App\Company;
use App\ConsumerApplication;
use App\EmiPayment;
use App\Exports\Exporter;
use App\Http\Controllers\Admin\AccountingController;
use App\Http\Controllers\Admin\ConsumerApplicationController;
use App\Http\Controllers\Admin\ConsumerDocument;
use App\Http\Controllers\Admin\MerchantController;
use App\Http\Controllers\ApiController;
use App\Invoices;
use App\Lenders;
use App\LoanManagement;
use App\Mandate;
use App\Merchant;
use App\MerchantCategory;
use App\MerchantLocation;
use App\Moratorium;
use App\NachSupportedBank;
use App\PaymentEmi;
use App\PaymentTransaction;
use App\Pincode;
use App\ProFormaInvoice;
use App\Settlement;
use App\Slack;
use App\StateModel;
use App\StatusLogs;
use App\Tally\TallyListener;
use App\User;
use App\WhatsApp;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Maatwebsite\Excel\Facades\Excel;
use MathPHP\Finance;

class ExcelImport implements ToCollection
{

    use Importable;
    private $request;

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

    /**
     * @param Collection $collection
     * @return int
     */
    public function collection(Collection $collection)
    {

        unset($collection[0]);
        $total_amount = $collection[count($collection)][5];
        $narration = '';
        $paymentsData = [];
        foreach ($collection as $key => $item){
            if(isset($item['id'])) {
                $id = $item['id'];
                $name = $item['name'];
                $type = $item['source'];
                $amount = $item['amount'];

                $narration = $customerLedger = $name . '-' . $id;


                if ($key == 1) {
                    TallyListener::setDebtors('EMI CREDIT THROUGH ' . strtoupper($type), $total_amount);
                }

                TallyListener::setCreditors($customerLedger, $amount);

                $paymentsData = array_merge($paymentsData, TallyListener::$creditorAndDebtors);
                TallyListener::$creditorAndDebtors = [];
            }
        }


        $voucher = (object)[
            'id' => $collection[1]['id'],
            'ledger_id' => $collection[1]['id'],
            'name' => $collection[1]['name'],
            'narration' => $narration,
            'date' => date('Ymd', strtotime($collection[1]['date'])),
            'lender_id' => $collection[1]['lender_id']
        ];


        $request = new Request(['subvention' => $paymentsData, 'voucher' => $voucher, 'return_type' => 'xml']);

        return  $response = response((new TallyListener($request))->subventionEntry(), 200, [
            'Content-Type' => 'application/xml',
            'Content-Disposition' => 'attachment; filename="'.$collection[1]['date'].'_'.$collection[1]['source'].'.xml"',
        ]);

    }

    public function collectionCSV($collection)
    {

        unset($collection[0]);
        $total_amount = $collection[count($collection)][5];
        $narration = '';
        $paymentsData = [];
        foreach ($collection as $key => $item){
            if($item[0]) {
                $id = $item[0];
                $name = $item[1];
                $type = $item[3];
                $amount = $item[5];

                $narration = $customerLedger = $name . '-' . $id;


                if ($key == 1) {
                    TallyListener::setDebtors('EMI CREDIT THROUGH ' . strtoupper($type), $total_amount);
                }

                TallyListener::setCreditors($customerLedger, $amount);

                $paymentsData = array_merge($paymentsData, TallyListener::$creditorAndDebtors);
                TallyListener::$creditorAndDebtors = [];
            }
        }


        $voucher = (object)[
            'id' => $collection[1][0],
            'ledger_id' => $collection[1][0],
            'name' => $collection[1][1],
            'narration' => $narration,
            'date' => date('Ymd', strtotime($collection[1][2])),
            'lender_id' => $collection[1][4]
        ];


        $request = new Request(['subvention' => $paymentsData, 'voucher' => $voucher, 'return_type' => 'xml']);

        return  $response = response((new TallyListener($request))->subventionEntry(), 200, [
            'Content-Type' => 'application/xml',
            'Content-Disposition' => 'attachment; filename="'.$collection[1][2].'_'.$collection[1][3].'.xml"',
        ]);

    }

}
