<?php

namespace App\Http\Controllers\Administrator\Construction;

use App\ConstructionModel\PurchaseOrderModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\JournalModel;
use App\ProjectModel;
use Illuminate\Support\Facades\DB;

class ModifyingAccountingController extends Controller
{
    function modify_accounting(){
        
        /*********************Modified Purchase********************/
        $journal_purchase_orders = JournalModel::where("transaction_type","Purchase-Order");
        if(!empty($journal_purchase_orders->get())&&count($journal_purchase_orders->get())>0){
            $journal_purchase_orders->delete();
        }

        $purchase_orders = PurchaseOrderModel::where("status",6)->get();
        if(!empty($purchase_orders)&&count($purchase_orders)>0){
            foreach($purchase_orders as $po_key=>$po_value){
                \Globals::purchase_add_accounting($po_value->id);
            }
        }
        /*************************End****************************/

        /************************Purchase Payment****************/
        $journal_purchase_payment = JournalModel::where("transaction_type","Purchase-Order-Payment");
        if(!empty($journal_purchase_payment->get())&&count($journal_purchase_payment->get())>0){
            $journal_purchase_payment->delete();
        }
        $purchase_payment = DB::table("nso007_purchase_payment")->get();
        if(!empty($purchase_payment)&&count($purchase_payment)>0){
            foreach($purchase_payment as $key_payment=>$value_payment){
                $purchase_id = $value_payment->purchase_id;
                $purchase = PurchaseOrderModel::find($purchase_id);

                $project = ProjectModel::find($purchase->project_id);
                
                if($purchase->status==6){

                    $j=JournalModel::select(DB::raw("Max(journal_tran_id) as last_num"))->first();
                    $journal = new JournalModel();
                    $journal->branch_id=$project->branch_id;
                    $journal->journal_parentid=0;
                    $journal->journal_type=1;
                    $journal->journal_acccode=21001;
                    $journal->voucher_type=2;
                    $journal->voucher_ref = "V-".\Globals::generate_voucher_number(($j->last_num+1),8);
                    $journal->journal_des = (!empty($purchase->task_group_work_title)?("សំរាប់ការងារ ".$purchase->task_group_work_title):"");
                    $journal->journal_debit = $value_payment->payment_amount;
                    $journal->journal_credit = 0;
                    $journal->journal_tran_id = $j->last_num+1;
                    $journal->journal_transactiondate = date("Y-m-d H:i:s");
                    $journal->journal_paydate = date("Y-m-d",strtotime($value_payment->date));
                    $journal->journal_currency =1;
                    $journal->transaction_type = 'Purchase-Order-Payment';
                    $journal->journal_referenceid = $value_payment->id;
                    $journal->journal_by_project = $purchase->project_id;
                    $journal->journal_by = auth()->user()->id;
                    $journal->save();

                    $jj=JournalModel::select(DB::raw("Max(id) as last_id"))->first();

                    $journal=new JournalModel;
                    $journal->branch_id=$project->branch_id;
                    $journal->journal_parentid=$jj->last_id;
                    $journal->journal_type=1;
                    $journal->journal_acccode= $value_payment->cash_type;
                    $journal->voucher_type=2;
                    $journal->voucher_ref="V-".\Globals::generate_voucher_number(($j->last_num+1),8);
                    $journal->journal_des = (!empty($purchase->task_group_work_title)?("សំរាប់ការងារ ".$purchase->task_group_work_title):"");
                    $journal->journal_debit=0;
                    $journal->journal_credit=$value_payment->payment_amount;
                    $journal->journal_tran_id=$j->last_num+1;
                    $journal->journal_transactiondate = date("Y-m-d H:i:s");
                    $journal->journal_paydate = date("Y-m-d",strtotime($value_payment->date));
                    $journal->journal_currency =1;
                    $journal->transaction_type = 'Purchase-Order-Payment';
                    $journal->journal_referenceid = $value_payment->id;
                    $journal->journal_by_project = $purchase->project_id;
                    $journal->journal_by = auth()->user()->id;
                    $journal->save();
                }
            }
        }

        /***********************End******************************/

        /************************Stock Out***********************/
        $journal_stock_out = JournalModel::where("transaction_type","Stock-Out");
        if(!empty($journal_stock_out->get())&&count($journal_stock_out->get())>0){
            $journal_stock_out->delete();
        }
        $stock_out_purchase_detail = DB::table("nso007_stock_out_purchase_detail")
            ->join("nso007_construction_purchase_details","nso007_construction_purchase_details.id","nso007_stock_out_purchase_detail.purchase_detail_id")
            ->select("nso007_construction_purchase_details.*","nso007_stock_out_purchase_detail.id as stock_out_purchase_detail_id","nso007_stock_out_purchase_detail.qty as using_qty","nso007_stock_out_purchase_detail.unit_price","nso007_stock_out_purchase_detail.stock_out_detail_id")
            ->get();
        if(!empty($stock_out_purchase_detail)&&count($stock_out_purchase_detail)>0){
            foreach($stock_out_purchase_detail as $key_s=>$value_s){

                $request_detail = DB::table("nso007_request_warehouse_detail_construction")->join("nso007_request_warehouse_construction","nso007_request_warehouse_construction.id","nso007_request_warehouse_detail_construction.request_warehouse_id")->where("nso007_request_warehouse_detail_construction.id",$value_s->stock_out_detail_id)->select("nso007_request_warehouse_construction.project_id","nso007_request_warehouse_construction.date")->first();

                $using_amount = $value_s->unit_price*$value_s->using_qty;

                \Globals::stock_out_add_accounting($value_s->stock_out_purchase_detail_id,$value_s->purchase_id,$value_s->product_id,$request_detail->project_id,$using_amount,date('Y-m-d',strtotime($request_detail->date)));
            }
        }


        /************************End*****************************/

        dd("Processing is done !");
    }
}
