插入查询在Laravel 5.4中不起作用

时间:2017-05-31 09:02:04

标签: php mysql laravel laravel-5.3 laravel-5.4

我编写了以下查询以插入或更新采购发票项目。如果我启用dd()插入工作正常。当我发表评论时。插入不起作用。更新工作良好。插入是我的问题。有人帮我解决这个问题。

<?php

namespace App\Http\Controllers;

use DB;
use App\Roles;
use App\User;
use App\Purchase;
use App\Items;
use App\Ledger;
use App\Units;
use App\Stock;
use App\Company;
use App\Stockledger;
use App\Vatamount;
use App\Purchaseitems;
use App\Companyledger;
use App\Groups;
use App\Openningbalance;
use App\Companysettings;
use Session;
use Input;
use PDF;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class PurchaseController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');

        $this->middleware('checkcomp');

    }

    public function updatepo(Request $request)
    {
        $data = $_POST;
        $usersid = Auth::user()->id;
        $companyids = Session()->get('companyids');

        $getcurrentpurchaseData = Purchase::Where('id', $data['hdnid'])->first();

        $getcurrentPurchaseItems = Purchaseitems::Where('purchaseid', $data['hdnid'])->get();


        /* Remove Previous Purchase Stock */

        foreach ($getcurrentPurchaseItems as $purchaseItemsDatas) {

            $currentstockfrmtbl = Stock::Where('prod_id', $purchaseItemsDatas->product)->first();
            $needtoupdatestock = $currentstockfrmtbl->curent_stock - $purchaseItemsDatas->quantity;

            $updatecurrentstock = Stock::Where('prod_id', $purchaseItemsDatas->product)
                ->update(['curent_stock' => $needtoupdatestock,]);

            $deletestockledeger = Stockledger::Where('item_id', $purchaseItemsDatas->product)
                ->Where('trans_type', 'Purchase')
                ->Where('ref_id', $data['hdnid'])
                ->delete();

        }

        /* Add Purchase Stock */

        $countupdateproduct = count($data['product']);


        for ($t = 0; $t < $countupdateproduct; $t++) {

            $updateproductstock = Stock::Where('prod_id', $data['product'][$t])->first();
            $updatecurrentstockamts = $updateproductstock->curent_stock + $data['quantity'][$t];

            $updatecurrentpurchasestock = Stock::Where('prod_id', $data['product'][$t])
                ->update(['curent_stock' => $updatecurrentstockamts,]);

            $insertstockledger = Stockledger::create([
                'item_id' => $data['product'][$t],
                'rec_date' => $data['purchase_date_submit'],
                'trans_type' => 'Purchase',
                'ref_id' => $data['hdnid'],
                'credit_item' => $data['quantity'][$t],
                'credit_unit' => $data['unit'][$t],
                'company_id' => $companyids,
                'user_id' => $usersid,
                'created_by' => $usersid
            ]);

        }

        /* Product Items Insert / Update */

        $producttotalamount = 0;
        $productvatamount = 0;
        $productgrandtotalamount = 0;

        for ($i = 0; $i < $countupdateproduct; $i++) {

            $producttotalamount += $data['quantity'][$i] * $data['prodct_rate'][$i];
            $productvatamount += $data['vat_amt'][$i];
            $productgrandtotalamount += $data['prod_tot_amt'][$i];

            //$updateorcreatepurchaseitems = Purchaseitems::updateOrCreate(['id' => $data["purchaseitemsid"][$i]],['purchaseid' => $data['hdnid'],'product' => $data['product'][$i],'quantity' => $data['quantity'][$i],'unit' => $data['unit'][$i],'add_quantity' => $data['quantity_add'][$i],'add_unit' => $data['unit_add'][$i],'product_rate'=>$data['prodct_rate'][$i],'product_amount' => $data['quantity'][$i]*$data['prodct_rate'][$i],'vat_percentage' => $data['vatpercentage'][$i],'vat_amount' => $data['vat_amt'][$i],'prod_total_amount'=>$data['prod_tot_amt'][$i],'company_id'=>$companyids,'user_id' => $usersid,'created_by' => $usersid,]);

            $chekpurchaseitems = Purchaseitems::where('id', $data["purchaseitemsid"][$i])->count();

            if ($chekpurchaseitems > 0) {

                $updatepurchaseitems = Purchaseitems::where('id', $data['purchaseitemsid'][$i])
                    ->update([
                        'product' => $data['product'][$i],
                        'quantity' => $data['quantity'][$i],
                        'unit' => $data['unit'][$i],
                        'add_quantity' => $data['quantity_add'][$i],
                        'add_unit' => $data['unit_add'][$i],
                        'product_rate' => $data['prodct_rate'][$i],
                        'product_amount' => $data['quantity'][$i] * $data['prodct_rate'][$i],
                        'vat_percentage' => $data['vatpercentage'][$i],
                        'vat_amount' => $data['vat_amt'][$i],
                        'prod_total_amount' => $data['prod_tot_amt'][$i],
                        'company_id' => $companyids,
                        'user_id' => $usersid,
                        'created_by' => $usersid
                    ]);

            } else {

                $insertpurchaseitems = Purchaseitems::create([
                    'purchaseid' => $data['hdnid'],
                    'product' => $data['product'][$i],
                    'quantity' => $data['quantity'][$i],
                    'unit' => $data['unit'][$i],
                    'add_quantity' => $data['quantity_add'][$i],
                    'add_unit' => $data['unit_add'][$i],
                    'product_rate' => $data['prodct_rate'][$i],
                    'product_amount' => $data['quantity'][$i] * $data['prodct_rate'][$i],
                    'vat_percentage' => $data['vatpercentage'][$i],
                    'vat_amount' => $data['vat_amt'][$i],
                    'prod_total_amount' => $data['prod_tot_amt'][$i],
                    'company_id' => $companyids,
                    'user_id' => $usersid,
                    'created_by' => $usersid
                ]);

            }
        }

        /* Delete Removed Items */

        $posteditemsids = array_filter($data['purchaseitemsid']); // Remove Empty Elements in Array

        $deleteitems = Purchaseitems::where('purchaseid', $data['hdnid'])
            ->whereNotIn('id', $posteditemsids)
            ->delete();

        /* Update Purchase Main Table */

        $updateqry = DB::table('purchase')->where('id', $data['hdnid'])->update([
            'purchase_no' => $data['purchase_no'],
            'purchase_from' => $data['purchase_from'],
            'purchase_date' => $data['purchase_date_submit'],
            'ref_inv_no' => $data['ref_inv_no'],
            'purch_address' => $data['purch_address'],
            'purchase_ledger' => $data['purchase_ledger'],
            'sub_total' => $producttotalamount,
            'vat_total' => $productvatamount,
            'grand_total' => $productgrandtotalamount,
            'company_id' => $companyids,
            'updated_by' => $usersid,]);

        alert()->success('Purchase Entry Updated Successfully.', 'Success!');

        return redirect('purchasentry');
    }
}

0 个答案:

没有答案