app/Http/Controllers/StockoutController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $re)
    {
        $now = Carbon::now()->addYear(1)->toDateString('Y-m-d');
        $a=0;
        $j =0;
        $this->validate($re,[
            'pre_id'=>'required'
        ],[
            'pre_id.required' => 'Please select invoice number'
        ]);
        $user_id = Auth::user()->id;
        $invoiceN = $re->input('pre_id');
        if($invoiceN!=0){
            $pres = Prescription::find($invoiceN);
            $product = $pres->products()->get();
            $stockout = new Stockout();
            $stockout->date = Carbon::now()->toDateString();
            $stockout->prescription_id = $invoiceN;
            $stockout->user_id = $user_id;
            $stockout->status = 0;
            $stockout->save();
            $stockout_id = $stockout->id;
            foreach ($product as $p) {
                $qtyIn = $p->pivot->qty;
                $product_id = $p->id;
                $qt=$qtyIn;
                $result = DB::select("SELECT id, qty, import_id FROM `import_product` WHERE product_id = {$product_id} AND qty > 0"); 
                foreach ($result as $r){
                    $res =DB::table('import_product')->select('id','qty','import_id','product_id','mfd','expd')->where('import_id',$r->import_id)->where('product_id',$product_id)->where('expd','>',0)->where('expd','>',$now)->get();
                    foreach ($res as $s){
                        $qt=$qt;
                        if( $a>=$qtyIn | $s->qty >= $qtyIn){
                            if($j!=$product_id){
                                $m = $s->qty - $qt;
                                if($m >=0){
                                    DB::table('import_product')->where('id', $s->id)->update(array('qty'=>$m));
                                    $Up = Prescription::find($invoiceN);
                                    $Up->isExport=1;
                                    $Up->save();
                                    DB::table('import_stockout')->insert(['stockout_id'=>$stockout_id,'import_id'=>$s->import_id, 'product_id' => $product_id, 'qty' => $qt,'expd' =>$s->expd]);
                                    $j = $product_id;
                                    $a=0;
                                }
                            }
                        }
                        elseif($s->qty < $qtyIn || $a < $qtyIn)
                        {
                            if($j!=$product_id)
                            {
                                $a = $a + $s->qty;
                                if ($a >= $qtyIn)
                                {
                                    $m = $s->qty - $qt;
                                    DB::table('import_product')->where('id', $s->id)->update(array('qty'=>$m));
                                    $Up = Prescription::find($invoiceN);
                                    $Up->isExport=1;
                                    $Up->save();
                                    DB::table('import_stockout')->insert(['stockout_id'=>$stockout_id,'import_id'=>$s->import_id, 'product_id'=>$product_id, 'qty'=>$qt, 'expd'=>$s->expd]);
                                    $j = $product_id;
                                    $a = 0;
                                }
                                elseif ($a < $qtyIn)
                                {
                                    $m = $qt - $s->qty;
                                    $qt = $m;
                                    DB::table('import_product')->where('id', $s->id)->update(array('qty'=>0));
                                    DB::table('import_stockout')->insert(['stockout_id'=>$stockout_id,'import_id'=>$s->import_id, 'product_id' => $product_id, 'qty' => $s->qty, 'expd' => $s->expd]);
                                }
                            }
                        }
                    }
                }
            }
        }
        return redirect()->back();
    }
public function store ( Request $ re ) { $ now = Carbon :: now ( ) -> addYear ( 1 ) -> toDateString ( 'Y-m-d' ) ; $ a = 0 ; $ j = 0 ; $ this -> validate ( $ re , [ 'pre_id' => 'required' ] , [ 'pre_id.required' => 'Please select invoice number' ] ) ; $ user_id = Auth :: user ( ) -> id ; $ invoiceN = $ re -> input ( 'pre_id' ) ; if ( $ invoiceN != 0 ) { $ pres = Prescription :: find ( $ invoiceN ) ; $ product = $ pres -> products ( ) -> get ( ) ; $ stockout = new Stockout ( ) ; $ stockout -> date = Carbon :: now ( ) -> toDateString ( ) ; $ stockout -> prescription_id = $ invoiceN ; $ stockout -> user_id = $ user_id ; $ stockout -> status = 0 ; $ stockout -> save ( ) ; $ stockout_id = $ stockout -> id ; foreach ( $ product as $ p ) { $ qtyIn = $ p -> pivot -> qty ; $ product_id = $ p -> id ; $ qt = $ qtyIn ; $ result = DB :: select ( " SELECT id, qty, import_id FROM `import_product` WHERE product_id =  { $ product_id }  AND qty > 0 " ) ; foreach ( $ result as $ r ) { $ res = DB :: table ( 'import_product' ) -> select ( 'id' , 'qty' , 'import_id' , 'product_id' , 'mfd' , 'expd' ) -> where ( 'import_id' , $ r -> import_id ) -> where ( 'product_id' , $ product_id ) -> where ( 'expd' , '>' , 0 ) -> where ( 'expd' , '>' , $ now ) -> get ( ) ; foreach ( $ res as $ s ) { $ qt = $ qt ; if ( $ a >= $ qtyIn | $ s -> qty >= $ qtyIn ) { if ( $ j != $ product_id ) { $ m = $ s -> qty - $ qt ; if ( $ m >= 0 ) { DB :: table ( 'import_product' ) -> where ( 'id' , $ s -> id ) -> update ( array ( 'qty' => $ m ) ) ; $ Up = Prescription :: find ( $ invoiceN ) ; $ Up -> isExport = 1 ; $ Up -> save ( ) ; DB :: table ( 'import_stockout' ) -> insert ( [ 'stockout_id' => $ stockout_id , 'import_id' => $ s -> import_id , 'product_id' => $ product_id , 'qty' => $ qt , 'expd' => $ s -> expd ] ) ; $ j = $ product_id ; $ a = 0 ; } } } elseif ( $ s -> qty < $ qtyIn || $ a < $ qtyIn ) { if ( $ j != $ product_id ) { $ a = $ a + $ s -> qty ; if ( $ a >= $ qtyIn ) { $ m = $ s -> qty - $ qt ; DB :: table ( 'import_product' ) -> where ( 'id' , $ s -> id ) -> update ( array ( 'qty' => $ m ) ) ; $ Up = Prescription :: find ( $ invoiceN ) ; $ Up -> isExport = 1 ; $ Up -> save ( ) ; DB :: table ( 'import_stockout' ) -> insert ( [ 'stockout_id' => $ stockout_id , 'import_id' => $ s -> import_id , 'product_id' => $ product_id , 'qty' => $ qt , 'expd' => $ s -> expd ] ) ; $ j = $ product_id ; $ a = 0 ; } elseif ( $ a < $ qtyIn ) { $ m = $ qt - $ s -> qty ; $ qt = $ m ; DB :: table ( 'import_product' ) -> where ( 'id' , $ s -> id ) -> update ( array ( 'qty' => 0 ) ) ; DB :: table ( 'import_stockout' ) -> insert ( [ 'stockout_id' => $ stockout_id , 'import_id' => $ s -> import_id , 'product_id' => $ product_id , 'qty' => $ s -> qty , 'expd' => $ s -> expd ] ) ; } } } } } } } return redirect ( ) -> back ( ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
//Get Invoice number from combobox in Form //Find purchase by In that got from comboBox from form // Get all product by Invoice/Purchase Order //Insert to table stockout // Select all product in import //$res = DB::select("SELECT id, qty, import_id,product_id, mfd, expd FROM `import_product` WHERE import_id = {$r->import_id} AND product_id={$product_id} AND qty > 0 AND expd >{$now}");//select one by one from import //Update field delivery to 1