<?php

namespace App\Http\Controllers;

use App\model\Market_place;
use App\model\Source;
use App\model\Report;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use View;
use Illuminate\Support\Facades\Input;
use App\Http\Requests\ReportRequest;


class WelcomeController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Welcome Controller
    |--------------------------------------------------------------------------
    |
    | This controller renders the "marketing page" for the application and
    | is configured to only allow guests. Like most of the other sample
    | controllers, you are free to modify or remove it as you desire.
    |
    */

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('guest');
        $this->middleware('auth');
    }

    /**
     * Show the application welcome screen to the user.
     *
     * @return Response
     */
    public function index()
    {
        //return view('welcome');
        $values = Market_place::all();
        $results = Source::all();
        $market_places = array(0 => 'Select a Market Place');
        $sources = array(0 => 'Select a Source');
        foreach($values as $value)
        {
            $market_places[$value['id']]= $value['name'];
        }

        foreach($results as $result)
        {
            $sources[$result['id']]= $result['source_name'];
        }
        return View::make('pages.home')->with('market_places',$market_places)->with('sources',$sources)->with('values',$values);
    }


    /**
    * Store a report in database.
    *
    * @return Response
    */
    public function submit_market_place_data(ReportRequest $request)
    {
        $report = new Report();
        $report->report_date = date("Y-m-d",strtotime(Input::get('date')));
        if(Input::get('market_place') == 9)
        {
           $report->report_market_place_id = 1;
        }
        elseif (Input::get('market_place') == 10)
        {
           $report->report_market_place_id = 2;
        }
        else
        {
           $report->report_market_place_id = Input::get('market_place');
        }

        if(Input::get('market_place') == 1 || Input::get('market_place') == 9 || Input::get('market_place') == 5 )
        {
            if(count(Report::find_order_id(Input::get('order_id'),date("Y-m-d",strtotime(Input::get('date')))))>0)
            {
                $report->report_market_place_comminsion = Input::get('commission');
            }
            else
            {
                $report->report_market_place_comminsion = Input::get('commission')+1;
            }
        }
        else
        {
            $report->report_market_place_comminsion = Input::get('commission');
        }
        $report->report_order_id = Input::get('order_id');
        $report->report_item_sku = Input::get('sku');
        $report->report_selling_price = Input::get('selling_price');
        $report->report_shipping_cost = Input::get('shipping_cost');
        $report->report_net_cost = Input::get('net_cost');
        $report->report_item_quantity = Input::get('quantity');

        if(Input::get('tax'))
        {
            $report->report_tax = Input::get('tax');
        }
        else
        {
            $report->report_tax =0;
        }
        $report->report_source_id = Input::get('source');

        if($report->save())
        {
            return redirect('/')->with('success','Report Saved Successfully.');
        }
    }
}
