• File: StoreConfigController.php
  • Full Path: /var/www/imalipartnersapi/app/Http/Controllers/StoreConfigController.php
  • Date Modified: 02/07/2023 8:01 PM
  • File size: 8.38 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

namespace App\Http\Controllers;

use App\Bank\Payment;
use App\Classes\TransactionGeneration;
use App\DayCloseStore;
use App\Store;
use App\StoreConfig;
use Illuminate\Http\Request;
use DB;

class StoreConfigController extends Controller
{
    public function addOrUpdateConfigs(Request $request) {

        $this->validate($request, [
            'store_id' => 'required',
            'notify' => 'required',
            'notify_sms' => 'required',
            'notify_email' => 'required',
            'notify_push' => 'required',
            'use_period' => 'required',
            'on_map' => 'required',
            'accept_payment' => 'required',
            'close_period' => 'required',
            'coin' => 'required',
            'bank_default' => 'required',
        ]);

        if ($request->isJson()) {

            $storeConfig = StoreConfig::query()->where('store_id', '=', $request->store_id)->first();

            if ($storeConfig) {
                $storeConfig->update([
                    'notify' => $request->notify,
                    'notify_sms' => $request->notify_sms,
                    'notify_email' => $request->notify_email,
                    'notify_push' => $request->notify_push,
                    'use_period' => $request->use_period,
                    'use_refund' => $request->use_refund,
                    'on_map' => $request->on_map,
                    'accept_payment' => $request->accept_payment,
                    'close_period' => $request->close_period,
                    'coin' => $request->coin,
                    'bank_default' => $request->bank_default,
                ]);

                return response()->json(['message' => 'Configurações actualizadas com sucesso']);
            } else {

                StoreConfig::create([
                    'notify' => $request->notify,
                    'notify_sms' => $request->notify_sms,
                    'notify_email' => $request->notify_email,
                    'notify_push' => $request->notify_push,
                    'use_period' => $request->use_period,
                    'use_refund' => $request->use_refund,
                    'on_map' => $request->on_map,
                    'accept_payment' => $request->accept_payment,
                    'close_period' => $request->close_period,
                    'coin' => $request->coin,
                    'bank_default' => $request->bank_default,
                    'store_id' => $request->store_id
                ]);
                return response()->json(['message' => 'Configurações adicionadas com sucesso']);
            }
        }
    }

    public function getStoreConfig($id) {

        $config = StoreConfig::query()->where('store_id', '=', $id)->first();

        if ($config) {
            return response()->json($config);
        } else {
            return response()->json(['message' => 'Está loja ainda não tem configurações.'],400);
        }
    }


    public function fecho(){

        $stores = Store::query()->get();

        foreach ($stores as $store) {

            if ($store) {

                $paymentsAmountCredited = Payment::query()
                    ->where('store_id', '=', $store->id)
                    ->where('payment_type', '=', 'directo')
                    ->whereDate('created_at', '=', date('Y-m-d', strtotime(' -1 day')))
                    ->sum('payments.amount_credited');

                $paymentsAmountRefundCredited = Payment::query()
                    ->where('store_id', '=', $store->id)
                    ->where('payment_type', '=', 'reembolso')
                    ->whereDate('created_at', '=', date('Y-m-d', strtotime(' -1 day')))
                    ->sum('payments.amount_credited');


                $amountTotal = Payment::query()
                    ->where('store_id', '=', $store->id)
                    ->where('payment_type', '=', 'directo')
                    ->whereDate('created_at', '=', date('Y-m-d', strtotime(' -1 day')))
                    ->sum('payments.amount');

                $amountRefund = Payment::query()
                    ->where('store_id', '=', $store->id)
                    ->where('payment_type', '=', 'reembolso')
                    ->whereDate('created_at', '=', date('Y-m-d', strtotime(' -1 day')))
                    ->sum('payments.amount');

                $fee = Payment::query()
                    ->where('store_id', '=', $store->id)
                    ->where('payment_type', '=', 'directo')
                    ->whereDate('created_at', '=', date('Y-m-d', strtotime(' -1 day')))
                    ->sum('payments.comissao');

                $feeRefund = Payment::query()
                    ->where('store_id', '=', $store->id)
                    ->where('payment_type', '=', 'reembolso')
                    ->whereDate('created_at', '=', date('Y-m-d', strtotime(' -1 day')))
                    ->sum('payments.comissao');

                $countRefund = Payment::query()
                    ->where('store_id', '=', $store->id)
                    ->where('payment_type', '=', 'reembolso')
                    ->whereDate('created_at', '=', date('Y-m-d', strtotime(' -1 day')))
                    ->count();

                $countDirect = Payment::query()
                    ->where('store_id', '=', $store->id)
                    ->where('payment_type', '=', 'directo')
                    ->whereDate('created_at', '=', date('Y-m-d', strtotime(' -1 day')))
                    ->count();

                $closes = DayCloseStore::query()
                    ->where('store_id', '=', $store->id)
                    ->where('date_reference', '=', date('Y-m-d', strtotime(' -1 day')))
                    ->first();

                if ($closes) {
                    $this->info('Já foi feito o fecho nessa data');
                } else {

                    if ($paymentsAmountCredited > 0) {

                        $trasactionGeneration = new TransactionGeneration();
                        $transaction = $trasactionGeneration->generateTransaction();

                        $close = DayCloseStore::create([
                            'transaction' => $transaction,
                            'description' => 'Fecho ' . date('Y-m-d', strtotime(' -1 day')),
                            'date_reference' => date('Y-m-d', strtotime(' -1 day')),
                            'nib' => $store->nib,
                            'amount' => $amountTotal,
                            'amount_credited' => $paymentsAmountCredited,
                            'fee' => $fee,
                            'amount_refund' => $amountRefund,
                            'amount_credited_refund' => $paymentsAmountRefundCredited,
                            'fee_refund' => $feeRefund,
                            'amount_total_credited' => $paymentsAmountCredited - $paymentsAmountRefundCredited,
                            'total_payments' => $countDirect,
                            'total_refunds' => $countRefund,
                            'store_id' => $store->id,
                            'user_id' => 1,
                        ]);

                        if ($close) {
//                            DB::table('stores')->where('id', $store->id)->decrement('balance', ($paymentsAmountCredited - $paymentsAmountRefundCredited));

                            $storeCheck = Store::query()->where('id', '=',$store->id)->first();
//                            DB::table('merchant_accounts')->where('id', $storeCheck->merchant_account_id)->increment('balance', ($paymentsAmountCredited - $paymentsAmountRefundCredited));

                            return response()->json(['amount' => $paymentsAmountCredited - $paymentsAmountRefundCredited]);






//                            DB::table('merchant_accounts')->update('id', $storeCheck->merchant_account_id)->increment('balance', ($paymentsAmountCredited - $paymentsAmountRefundCredited));
//                            MerchantAccount::query()->update()

//                            $this->info('Fecho confirmado com Sucesso');
                        }
                    } else {
                        $this->info('Sem pagamentos');
                    }
                }

            } else {
                $this->info('Loja Inválida');
            }

        }
    }
}