<?php
namespace App\Http\Controllers;

use App\Http\Requests;
use Illuminate\Http\Request;
use DB;
use App\Main;
use App\Sort;
use App\Branch;
use App\Store_Branch;
use App\Store_History;
use App\Payment_Method;
use App\Supplier;
use App\Waste;
use App\Packingmethod;
use App\Physicalcondition;
use App\Driver;
use App\Vehicle;
use App\User;
use App\Customer;
use Carbon\Carbon;
use Auth;
use App\Safe_Account;
use App\Client;
use App\Invoice;
use App\Invoice_Item;
use App\Invoice_Payment;
use App\Cheque;
use App\Bank;
use App\Client_Sheet;
use App\Receipt;
use App\Notifications;
use \stdClass;

class BackendController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        
    }

    
//////////////////////////////////////////////////
// Invoice
public function get_invoices()
{
    $branches = Branch::get();
    $invoices = Invoice::with('client', 'items' ,'payments')->where("confirmed","confirmed")->orderBy('id', 'DESC')->limit(20)->get();
    $invoices2 = [];
    foreach ($invoices as $key => $invoice) {
       foreach ($invoice["items"] as $key => $item) {
         $sell_store = Store_Branch::with('store', 'branch' )->where('id',$item->sell_store_id)->first();
         $item["sell_store"] = $sell_store;  
        }
    }

    return \Response::json(['invoices' => $invoices , 'branches'=>$branches]);

}

public function get_sell_stores_of_branch($branch_id)
{
    
    $sell_stors = Store_Branch::with('branch', 'store' )->where("branch_id",$branch_id)->get();
    

    return \Response::json(['sell_stors' => $sell_stors ]);

}

public function get_safe_accounts_of_branch($branch_id)
{
    
    $safe_accounts = Safe_Account::with('branch', 'payment_method' )->where("branch_id",$branch_id)->get();
    

    return \Response::json(['safe_accounts' => $safe_accounts ]);

}

public function get_notifications()
{
    
    $notifications = Notifications::orderBy('id', 'DESC')->get();
    return \Response::json(['notifications' => $notifications ]);

}



public function client_all_accounts()
{
    
    $clients = Client::with('branch')->get();
    $final_arr=[];

    $total_start_leeh=0;
    $total_start_aleeh=0;
    $total_total_leeh=0;
    $total_total_aleeh=0;
    $rejected_cheeques=0;
    
     $sum_last_moda_leeh = 0 ;  //دائن رصيد اخر المدة
    $sum_last_moda_aleeh = 0 ;  // رصيد اخر المدة مدين
    
    foreach ($clients as $client) {
        $total_leeh =  Client_Sheet::where('client_id' ,$client->id)->sum('leeh');  // دفع
        $total_aleeh = Client_Sheet::where('client_id' ,$client->id)->sum('aleeh');  // خد 

        // rejected cheque
        $rejected_cheeques = Client_Sheet::where('client_id' ,$client->id)
                                         ->where('invoice_id' ,NULL)
                                         ->where('receipt_id' ,NULL)
                                         ->sum('refused_cheque');  // دفع
        $obj = new stdClass();
        $obj->id = $client->id;
        $obj->name = $client->name;
        $obj->branch = $client->branch->name;
        $obj->start_leeh= $client->start_leeh;  
        $obj->start_aleeh = $client->start_aleeh;  
        
                            

        $total_leeh = $total_leeh - $rejected_cheeques;
        $total_aleeh = $total_aleeh - $rejected_cheeques;
        
       // $obj->total_leeh =   $total_leeh - $rejected_cheeques;
    //    $obj->total_aleeh = $total_aleeh - $rejected_cheeques;  
        $obj->total_leeh =   $total_leeh > $total_aleeh ? $total_leeh - $total_aleeh : 0;
        $obj->total_aleeh = $total_aleeh > $total_leeh ? $total_aleeh - $total_leeh : 0;  

        $obj->rejected_cheeques=$rejected_cheeques;
        
        //echo "$client->id -  (r $rejected_cheeques )  ,";
        // tfooter 
        $total_start_leeh = $total_start_leeh +   $client->start_leeh ; // sum اول 
        $total_start_aleeh = $total_start_aleeh + $client->start_aleeh; // sum اول 
        $total_total_leeh = $total_total_leeh +   $total_leeh - $rejected_cheeques;  // sum حركة
        $total_total_aleeh = $total_total_aleeh + $total_aleeh - $rejected_cheeques; // sum حركة

if ( $total_leeh >$total_aleeh ) {
    $sum_last_moda_leeh = $sum_last_moda_leeh + ( $total_leeh-$total_aleeh ) ;
   }else{
    $sum_last_moda_aleeh = $sum_last_moda_aleeh + ( $total_aleeh-$total_leeh ) ;
  }
  
        $final_arr[]=$obj;
    }

  
    return \Response::json([
                            'clients' => $final_arr , 
                            "total_start_leeh"=>$total_start_leeh , 
                            "total_start_aleeh"=>$total_start_aleeh,
                            "total_total_leeh"=>$total_total_leeh ,
                           "total_total_aleeh"=>$total_total_aleeh,
                            "sum_last_moda_leeh" =>$sum_last_moda_leeh,
                            "sum_last_moda_aleeh"=>$sum_last_moda_aleeh,
                              
                            
                                                    ]);
} 



public function get_sell_stores(){
    $branch_id =4 ;
    $sell_stores = Store_Branch::with('store')->where('branch_id',$branch_id)->get();
    return \Response::json(['sell_stores' => $sell_stores ]);
}


 



}
