<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\DraftOrder;
use App\Models\InvoiceDump;

class DraftOrderController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }


    public function index()
    {
        $view = \DB::table('draft_orders')
        ->where('user_id', auth()->user()->id)->get();

        return view('invoice.draft-orders', [
            'drafts' => $view
        ]);

    }



public function saveToDump($id)
{
   $cart = session()->get('cart', []);
//    TESTING ID
   return $id = 1;
   $transfer = \DB::table("draft_invoices")->where('invoice_number', $id)->get();
    foreach ($transfer as $inv) {
        $cart[$id] = [
            'id' => $id,
            'user_id' => auth()->user()->id,
            "branch" => auth()->user()->branch,
            "customer_id" => "",
            "invoice_product_name" => $inv -> invoice_product_name,
            "invoice_status" => $inv -> invoice_status,
            "invoice_sales_person_category" => auth()->user()->sales_person_category,
            "invoice_quantity"=> $inv -> invoice_quantity,
            "invoice_unit_price" => $inv -> invoice_unit_price,
            "invoice_total_price"=>  $inv -> invoice_total_price,
            "item_category"=> $inv -> invoice_category,
            ];
   }
   \DB::table("draft_invoices")->where('invoice_number', $id)->delete();
   \DB::table("draft_orders")->where('order_id', $id)->delete();
   return redirect()->to('/ceqa-dashboard/create-invoice');
   

    // \DB::table("draft_invoices")->where('order_id', auth()->user()->id)->delete();
}

}