<?php

namespace App\Http\Controllers;

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

use App\Http\Requests\ReportRequest;


use App\Http\Controllers\Controller; 
use View;
use Input;

class ReportController extends Controller 
{
    /**
    * Display a listing of the market place report.
    *
    * @return Response
    */
    public function get_reports()
    {  
        if(Input::get('report_type'))  
        {
            $report_type = Input::get('report_type');  
            $date_from = date("Y-m-d",strtotime(Input::get('date_from')));
            $date_to = date("Y-m-d",strtotime(Input::get('date_to')));             
            $reports = Report::market_place_reports($report_type,$date_from,$date_to);                  
            $group = array(); 
            if($report_type == 'monthly')
            {
                foreach ($reports as $key=>$report)
                {
                    $group[date('F Y',strtotime($report->report_date))][] = ['report_id'=>$report->report_id,'market_place_name'=>$report->name,'order_id'=>$report->report_order_id,'item_sku'=>$report->report_item_sku,'selling_price'=>$report->report_selling_price,'shipping_cost'=>$report->report_shipping_cost,'net_cost'=>$report->report_net_cost,'quantity'=>$report->report_item_quantity,'tax'=>$report->report_tax,'source_name'=>$report->source_name,'commission'=>$report->report_market_place_comminsion,'refund'=>$report->report_refund];                            
                }
            }
            else
            {
                foreach ($reports as $key=>$report)
                {
                    $group[$report->report_date][] = ['report_id'=>$report->report_id,'market_place_name'=>$report->name,'order_id'=>$report->report_order_id,'item_sku'=>$report->report_item_sku,'selling_price'=>$report->report_selling_price,'shipping_cost'=>$report->report_shipping_cost,'net_cost'=>$report->report_net_cost,'quantity'=>$report->report_item_quantity,'tax'=>$report->report_tax,'source_name'=>$report->source_name,'commission'=>$report->report_market_place_comminsion,'refund'=>$report->report_refund];                            
                } 
            }            
            return View::make('pages.report')->with('reports',$group)->with('reprt_type',$report_type);
        }
        else
        {
            return View::make('pages.report'); 
        }
    }
    
    
    
    /**
    * Display a list of the reports to modificaton.
    *
    * @return Response
    */
    public function show_reports()
    {
       $reports = Report::show_reports();
       return View::make('pages.show_report')->with('reports',$reports);       
    }
    
    
    
    /**
    * Show the form for editing the specified resource.
    *
    * @param  int  $report_id
    * @return Response
    */
    public function edit($report_id)
    {
        $report_to_edit = Report::find($report_id);        
        $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)->with('report_to_edit',$report_to_edit); 
    } 
    
    
    /**
    * Update the specified resource in storage.
    *
    * @param  int  $id
    * @return Response
    */
    public function update(ReportRequest $request)
    {
        $report_id = Input::get('report_id'); 
        $report = Report::find($report_id);        
        
        $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('reports')->with('success','Report Updated Successfully.');            
            //return View::make('pages.report')->with('success','Report Updated Successfully.');
        }
    }    
    
    
    /**
    * Remove the specified resource from storage.
    *
    * @param  int  $report_id
    * @return Response
    */
    public function destroy($report_id)
    {
        if(Report::destroy($report_id))
        {
            return redirect('reports')->with('success','Report Deleted Successfully.');
        }
    }
    
    
    /**
    * Display a form for Order Refund 
    *
    * 
    * 
    */
    public function refund_display()
    {
        return View::make('pages.refund_display');
    }
    
    
    
    /**
    * Insert refund amount in database
    *
    * 
    * 
    */
    public function refund()
    {        
        if(Input::get('refund_order_id'))
        {
            $order_id = Input::get('refund_order_id');
            $report = Report::find_an_report($order_id);  
            return View::make('pages.refund_display')->with('report',$report);
        }
        else
        {
            $report_id = Input::get('report_id');
            $refund_amount = Input::get('refund_amount');
            $report = Report::find($report_id);
            $report->report_refund = $refund_amount;
            $report->save(); 
            return redirect('refund')->with('success','Refund saved Successfully.');                  
        }
    }
    
}