/
home
/
sjslayjy
/
public_html
/
mosaram
/
app
/
Http
/
Controllers
/
Upload File
HOME
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Http\Response; use Session; use App\Rules\OneOf; use App\Session as AppSession; use DB; /** * Invoice Controller */ class InvoiceController extends Controller { public function companyDi(Request $request) { $data = array(); $data['product_companies'] = \App\ProductCompany::where('is_active', 1)->get(); $data['dealers'] = \App\Dealer::where('is_active', 1)->get(); $data['warehouses'] = \App\Warehouse::where('is_active', 1)->get(); $data['products'] = \App\Product::where('is_active', 1)->get(); $data['bank_accounts'] = \App\BankAccount::where('is_active', 1)->with('bank')->get(); if ($request->isMethod('post')) { // $validator = \Validator::make($request->all(), // array( // 'product_id' =>'required', // ) // ); // if($validator->fails()){ // return redirect('user/company-di') // ->withErrors($validator) // ->withInput(); // }else{ $query = \App\CompanyDi::query(); if ($request->dealer_id != "") { $query->where('dealer_id', $request->dealer_id); $data['dealer_id'] = $request->dealer_id; } if ($request->product_company_id != "") { $query->Where('product_company_id', $request->product_company_id); $data['product_company_id'] = $request->product_company_id; } if ($request->product_id != "") { $query->where('product_id', $request->product_id); $data['product_id'] = $request->product_id; } $query = $query->orderBy('id', 'desc')->get(); $data['total'] = $query->where('product_id', $request->product_id)->sum('quantity'); $data['invoices'] = $query; //} } else { $data['invoices'] = \App\CompanyDi::orderBy('id', 'desc')->get(); $data['total'] = 0; } return view('dashboard.invoice.company-di', $data); } public function pendingCompanyDi(Request $request) { $data = array(); $data['product_companies'] = \App\ProductCompany::where('is_active', 1)->get(); $data['products'] = \App\Product::where('is_active', 1)->get(); if ($request->isMethod('post')) { $validator = \Validator::make( $request->all(), array( 'product_company_id' => 'required', 'product_id' => 'required', ) ); if ($validator->fails()) { return redirect('user/pending-company-di') ->withErrors($validator) ->withInput(); } else { $data['product_company_id'] = $request->product_company_id; $data['product_id'] = $request->product_id; $master_rake_ids = \App\MasterRake::where('product_company_id', $request->product_company_id)->pluck('id'); $invoices = array(); if (!is_null($master_rake_ids)) { $rake_products = \App\MasterRakeProduct::whereIn('master_rake_id', $master_rake_ids)->where('product_id', $request->product_id)->selectRaw('master_rake_id,sum(quantity) as total_rr')->groupBy('master_rake_id')->with('master_rake:id,name')->get(); foreach ($rake_products as $rake_product) { $companydi = \App\CompanyDi::where('master_rake_id', $rake_product->master_rake_id)->where('product_id', $request->product_id)->sum('quantity'); $temp = array(); $temp['rake'] = $rake_product->master_rake->name; $temp['total_rr'] = $rake_product->total_rr; $temp['total_di'] = $companydi; array_push($invoices, $temp); } $data['invoices'] = $invoices; } else { $data['invoices'] = array(); } } } else { $data['invoices'] = array(); } return view('dashboard.invoice.pending-company-di', $data); } public function getGenerateCompanyDi() { $data = array(); $data['master_rakes'] = \App\MasterRake::where('is_active', 1)->get(); $data['companies'] = \App\Company::where('is_active', 1)->where('for_invoice', 1)->get(); $data['dealers'] = \App\Dealer::where('is_active', 1)->get(); $data['products'] = \App\Product::where('is_active', 1)->get(); $data['units'] = \App\Unit::where('is_active', 1)->get(); return view('dashboard.invoice.generate-company-di', $data); } public function postGenerateCompanyDi(Request $request) { $validator = \Validator::make( $request->all(), array( 'master_rake_id' => 'required', 'dealer_id' => 'required', 'invoice_date' => 'required', 'product_id' => 'required', 'quantity' => 'required', 'discount' => 'required', 'secondary_freight' => 'required', 'mrp_rate' => 'required', ) ); if ($validator->fails()) { return redirect('/user/generate-company-di') ->withErrors($validator) ->withInput(); } else { $master_rake_products = \App\MasterRakeProduct::where('master_rake_id', $request->master_rake_id)->sum('quantity'); $di_amount = \App\CompanyDi::where('master_rake_id', $request->master_rake_id)->sum('quantity'); $remaining_di_amount = $master_rake_products - $di_amount; $product_company_buffer = \App\Inventory::where('product_company_id', $request->product_company_id)->where('warehouse_id', 24)->where('product_id', $request->product_id)->where('product_brand_id', $request->product_company_id)->first(); if (!is_null($product_company_buffer)) { if ($remaining_di_amount >= $request->quantity) { $dealer_buffer = \App\Inventory::where('dealer_id', $request->dealer_id)->where('warehouse_id', 24)->where('product_id', $request->product_id)->where('product_brand_id', $request->product_company_id)->first(); if ($dealer_buffer) { $dealer_buffer->quantity = $dealer_buffer->quantity + $request->quantity; if ($dealer_buffer->save()) { $product_company_buffer->quantity = $product_company_buffer->quantity - $request->quantity; $product_company_buffer->save(); } } else { $product_company_buffer->quantity = $product_company_buffer->quantity - $request->quantity; $product_company_buffer->save(); $inventory = new \App\Inventory(); $inventory->dealer_id = $request->dealer_id; $inventory->warehouse_id = 24; $inventory->product_brand_id = $request->product_company_id; $inventory->product_id = $request->product_id; $inventory->unit_id = $request->product_unit; $inventory->quantity = $request->quantity; $inventory->save(); } $total_debit = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id)->where('dealer_id', $request->dealer_id)->sum('debit'); $total_credit = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id)->where('dealer_id', $request->dealer_id)->sum('credit'); $opening_balance = $total_debit - $total_credit; $companydi = new \App\CompanyDi(); $companydi->master_rake_id = $request->master_rake_id; $companydi->product_company_id = $request->product_company_id; $companydi->document_number = $request->document_no; $companydi->dealer_id = $request->dealer_id; $companydi->invoice_number = $request->invoice_number; if ($request->invoice_date) { $companydi->invoice_date = date('Y-m-d', strtotime($request->invoice_date)); } if ($request->dc_date) { $companydi->dc_date = date('Y-m-d', strtotime($request->dc_date)); } if ($request->due_date) { $companydi->due_date = date('Y-m-d', strtotime($request->due_date)); } $companydi->product_hsn = $request->product_hsn; $companydi->cgst = $request->cgst; $companydi->sgst = $request->sgst; $companydi->igst = $request->igst; $companydi->secondary_freight = $request->secondary_freight; $companydi->taxable_amount = $request->taxable_amount; $companydi->discount = $request->discount; $companydi->total = $request->product_total_amount; $companydi->remaining_amount = $request->product_total_amount; if ($opening_balance > 0) { if ($companydi->total >= $opening_balance) { $companydi->remaining_amount = $companydi->total - $opening_balance; } else { $companydi->remaining_amount = 0; $companydi->is_paid = 1; } } $companydi->base_price = $request->product_base_amount; $companydi->rate = $request->product_rate; $companydi->mrp_rate = $request->mrp_rate; $companydi->unit = $request->product_unit; $companydi->quantity = $request->quantity; $companydi->product = getModelById('Product', $request->product_id)->name; $companydi->product_id = $request->product_id; $companydi->tcs = $request->tcs; $companydi->save(); if ($companydi->total > 0) { $ledger = new \App\CompanyInvoiceLedger(); $ledger->product_company_id = $companydi->product_company_id; $ledger->dealer_id = $companydi->dealer_id; $ledger->type = 'credit'; $ledger->particular = "Purchase @" . ($companydi->sgst + $companydi->cgst) . "% (GST)"; $ledger->credit = $companydi->total; $ledger->di_id = $companydi->id; $ledger->di_type = 1; $ledger->debit = 0; $ledger->save(); } /*--------------Update Ledger------------------*/ return redirect('/user/company-di')->with('success', 'DI Generated Successfully.'); } else { return redirect('/user/company-di')->with('error', "DI Cannot be greater than rake quantity($master_rake_products). Remaining Quantity for DI is $remaining_di_amount. Entered Quantity was $request->quantity"); } } else { return redirect('/user/generate-company-di')->with('error', 'quantity not available in buffer'); } } } public function postGenerateCompanyDiOld(Request $request) { $validator = \Validator::make( $request->all(), array( 'master_rake_id' => 'required', 'dealer_id' => 'required', 'invoice_date' => 'required', 'product_id' => 'required', 'quantity' => 'required', 'discount' => 'required', 'secondary_freight' => 'required', ) ); if ($validator->fails()) { return redirect('/user/generate-company-di') ->withErrors($validator) ->withInput(); } else { $master_rake_products = \App\MasterRakeProduct::where('master_rake_id', $request->master_rake_id)->sum('quantity'); $di_amount = \App\CompanyDi::where('master_rake_id', $request->master_rake_id)->sum('quantity'); $remaining_di_amount = $master_rake_products - $di_amount; // dd($master_rake_products); // dd($di_amount); // dd($remaining_di_amount); $product_company_buffer = \App\Inventory::where('product_company_id', $request->product_company_id)->where('warehouse_id', 24)->where('product_id', $request->product_id)->where('product_brand_id', $request->product_company_id)->first(); // dd($product_company_buffer); if (!is_null($product_company_buffer)) { if ($remaining_di_amount >= $request->quantity) { if ($product_company_buffer->quantity >= $request->quantity) { $dealer_buffer = \App\Inventory::where('dealer_id', $request->dealer_id)->where('warehouse_id', 24)->where('product_id', $request->product_id)->where('product_brand_id', $request->product_company_id)->first(); if ($dealer_buffer) { $dealer_buffer->quantity = $dealer_buffer->quantity + $request->quantity; if ($dealer_buffer->save()) { $product_company_buffer->quantity = $product_company_buffer->quantity - $request->quantity; $product_company_buffer->save(); $companydi = new \App\CompanyDi(); $companydi->master_rake_id = $request->master_rake_id; $companydi->product_company_id = $request->product_company_id; $companydi->document_number = $request->document_no; $companydi->dealer_id = $request->dealer_id; $companydi->invoice_number = $request->invoice_number; if ($request->invoice_date) { $companydi->invoice_date = date('Y-m-d', strtotime($request->invoice_date)); } if ($request->dc_date) { $companydi->dc_date = date('Y-m-d', strtotime($request->dc_date)); } if ($request->due_date) { $companydi->due_date = date('Y-m-d', strtotime($request->due_date));; } $companydi->product_hsn = $request->product_hsn; $companydi->cgst = $request->cgst; $companydi->sgst = $request->sgst; $companydi->igst = $request->igst; $companydi->secondary_freight = $request->secondary_freight; $companydi->taxable_amount = $request->taxable_amount; $companydi->discount = $request->discount; $companydi->total = $request->product_total_amount; $companydi->remaining_amount = $request->product_total_amount; $companydi->base_price = $request->product_base_amount; $companydi->rate = $request->product_rate; $companydi->unit = $request->product_unit; $companydi->quantity = $request->quantity; $companydi->product = getModelById('Product', $request->product_id)->name; $companydi->product_id = $request->product_id; $companydi->save(); /*--------------Update Ledger------------------*/ $ledger = \App\CompanyInvoiceLedger::where('dealer_id', $companydi->dealer_id)->where('product_company_id', $companydi->product_company_id)->orderBy('id', 'desc')->first(); if (!is_null($ledger)) { $balance = $ledger->balance; $ledger = new \App\CompanyInvoiceLedger(); $ledger->product_company_id = $companydi->product_company_id; $ledger->dealer_id = $companydi->dealer_id; $ledger->particular = "Purchase @" . ($companydi->sgst + $companydi->cgst) . "% (GST)"; $ledger->credit = $companydi->total; $ledger->debit = 0; $ledger->balance = $balance + $companydi->total; $ledger->save(); } else { $ledger = new \App\CompanyInvoiceLedger(); $ledger->product_company_id = $companydi->product_company_id; $ledger->dealer_id = $companydi->dealer_id; $ledger->particular = "Purchase @" . ($companydi->sgst + $companydi->cgst) . "% (GST)"; $ledger->credit = $companydi->total; $ledger->debit = 0; $ledger->balance = $companydi->total; $ledger->save(); } /*--------------Update Ledger------------------*/ return redirect('/user/company-di')->with('success', 'DI Generated Successfully.'); } } else { $product_company_buffer->quantity = $product_company_buffer->quantity - $request->quantity; $product_company_buffer->save(); $direct_loading = \App\ProductLoading::where('master_rake_id', $request->master_rake_id) ->where('product_company_id', $request->product_company_id) ->where('product_id', $request->product_id) ->where('loading_slip_type', 2) ->first(); $inventory = new \App\Inventory(); $inventory->dealer_id = $request->dealer_id; if (!is_null($direct_loading)) { $inventory->warehouse_id = $direct_loading->warehouse_id; } else { $inventory->warehouse_id = 24; } $inventory->product_brand_id = $request->product_company_id; $inventory->product_id = $request->product_id; $inventory->unit_id = $request->product_unit; $inventory->quantity = $request->quantity; $inventory->save(); $companydi = new \App\CompanyDi(); $companydi->master_rake_id = $request->master_rake_id; $companydi->product_company_id = $request->product_company_id; $companydi->document_number = $request->document_no; $companydi->dealer_id = $request->dealer_id; $companydi->invoice_number = $request->invoice_number; if ($request->invoice_date) { $companydi->invoice_date = date('Y-m-d', strtotime($request->invoice_date)); } if ($request->dc_date) { $companydi->dc_date = date('Y-m-d', strtotime($request->dc_date)); } if ($request->due_date) { $companydi->due_date = date('Y-m-d', strtotime($request->due_date)); } $companydi->product_hsn = $request->product_hsn; $companydi->cgst = $request->cgst; $companydi->sgst = $request->sgst; $companydi->igst = $request->igst; $companydi->secondary_freight = $request->secondary_freight; $companydi->taxable_amount = $request->taxable_amount; $companydi->discount = $request->discount; $companydi->total = $request->product_total_amount; $companydi->remaining_amount = $request->product_total_amount; $companydi->base_price = $request->product_base_amount; $companydi->rate = $request->product_rate; $companydi->unit = $request->product_unit; $companydi->quantity = $request->quantity; $companydi->product = getModelById('Product', $request->product_id)->name; $companydi->product_id = $request->product_id; $companydi->save(); /*--------------Update Ledger------------------*/ $ledger = \App\CompanyInvoiceLedger::where('dealer_id', $companydi->dealer_id)->where('product_company_id', $companydi->product_company_id)->orderBy('id', 'desc')->first(); if (!is_null($ledger)) { $balance = $ledger->balance; $ledger = new \App\CompanyInvoiceLedger(); $ledger->product_company_id = $companydi->product_company_id; $ledger->dealer_id = $companydi->dealer_id; $ledger->particular = "Purchase @" . ($companydi->sgst + $companydi->cgst) . "% (GST)"; $ledger->credit = $companydi->total; $ledger->debit = 0; $ledger->balance = $balance + $companydi->total; $ledger->save(); } else { $ledger = new \App\CompanyInvoiceLedger(); $ledger->product_company_id = $companydi->product_company_id; $ledger->dealer_id = $companydi->dealer_id; $ledger->particular = "Purchase @" . ($companydi->sgst + $companydi->cgst) . "% (GST)"; $ledger->credit = $companydi->total; $ledger->debit = 0; $ledger->balance = $companydi->total; $ledger->save(); } /*--------------Update Ledger------------------*/ return redirect('/user/company-di')->with('success', 'DI Generated Successfully.'); } } else if ($product_company_buffer->quantity < $request->quantity) { $total_product_stock = \App\Inventory::where('product_company_id', $request->product_company_id)->where('product_brand_id', $request->product_company_id)->where('product_id', $request->product_id)->where('unit_id', $request->product_unit)->sum('quantity'); // dd($total_product_stock); if ($total_product_stock >= $request->quantity) { $stocks = \App\Inventory::where('product_id', $request->product_id) ->where('product_company_id', $request->product_company_id) ->where('product_brand_id', $request->product_company_id) ->where('unit_id', $request->product_unit) ->where('quantity', '>', 0) ->orderBy('quantity', 'desc') ->get(); // dd($stocks); // $less_qty = $product_company_buffer->quantity; // $product_company_buffer->quantity = 0; // $product_company_buffer->save(); // $remaining_quantity = $request->quantity - $less_qty; $remaining_quantity = $request->quantity; $i = 0; foreach ($stocks as $stock) { if ($remaining_quantity > 0) { $transfer_quantity = $stock->quantity; if ($transfer_quantity <= $remaining_quantity) { $stock->quantity = 0; } else { $stock->quantity = $stock->quantity - $remaining_quantity; } $stock->save(); $dealer_buffer = \App\Inventory::where('dealer_id', $request->dealer_id)->where('warehouse_id', $stock->warehouse_id)->where('product_id', $request->product_id)->where('product_brand_id', $request->product_company_id)->first(); if (!is_null($dealer_buffer)) { if ($transfer_quantity <= $remaining_quantity) { $dealer_buffer->quantity = $dealer_buffer->quantity + $transfer_quantity; } else { $dealer_buffer->quantity = $dealer_buffer->quantity + $remaining_quantity; } $dealer_buffer->save(); if ($transfer_quantity <= $remaining_quantity) { $remaining_quantity = $remaining_quantity - $transfer_quantity; } else { $remaining_quantity = 0; } } else { $inventory = new \App\Inventory(); $inventory->dealer_id = $request->dealer_id; $inventory->warehouse_id = $stock->warehouse_id; $inventory->product_brand_id = $request->product_company_id; $inventory->product_id = $request->product_id; $inventory->unit_id = $request->product_unit; if ($transfer_quantity <= $remaining_quantity) { $inventory->quantity = $transfer_quantity; } else { $inventory->quantity = $remaining_quantity; } $inventory->save(); if ($transfer_quantity <= $remaining_quantity) { $remaining_quantity = $remaining_quantity - $transfer_quantity; } else { $remaining_quantity = 0; } } } $i++; } // exit; $companydi = new \App\CompanyDi(); $companydi->master_rake_id = $request->master_rake_id; $companydi->product_company_id = $request->product_company_id; $companydi->document_number = $request->document_no; $companydi->dealer_id = $request->dealer_id; $companydi->invoice_number = $request->invoice_number; if ($request->invoice_date) { $companydi->invoice_date = date('Y-m-d', strtotime($request->invoice_date)); } if ($request->dc_date) { $companydi->dc_date = date('Y-m-d', strtotime($request->dc_date)); } if ($request->due_date) { $companydi->due_date = date('Y-m-d', strtotime($request->due_date)); } $companydi->product_hsn = $request->product_hsn; $companydi->cgst = $request->cgst; $companydi->sgst = $request->sgst; $companydi->igst = $request->igst; $companydi->secondary_freight = $request->secondary_freight; $companydi->taxable_amount = $request->taxable_amount; $companydi->discount = $request->discount; $companydi->total = $request->product_total_amount; $companydi->remaining_amount = $request->product_total_amount; $companydi->base_price = $request->product_base_amount; $companydi->rate = $request->product_rate; $companydi->unit = $request->product_unit; $companydi->quantity = $request->quantity; $companydi->product = getModelById('Product', $request->product_id)->name; $companydi->product_id = $request->product_id; $companydi->save(); /*--------------Update Ledger------------------*/ $ledger = \App\CompanyInvoiceLedger::where('dealer_id', $companydi->dealer_id)->where('product_company_id', $companydi->product_company_id)->orderBy('id', 'desc')->first(); if (!is_null($ledger)) { $balance = $ledger->balance; $ledger = new \App\CompanyInvoiceLedger(); $ledger->product_company_id = $companydi->product_company_id; $ledger->dealer_id = $companydi->dealer_id; $ledger->particular = "Purchase @" . ($companydi->sgst + $companydi->cgst) . "% (GST)"; $ledger->credit = $companydi->total; $ledger->debit = 0; $ledger->balance = $balance + $companydi->total; $ledger->save(); } else { $ledger = new \App\CompanyInvoiceLedger(); $ledger->product_company_id = $companydi->product_company_id; $ledger->dealer_id = $companydi->dealer_id; $ledger->particular = "Purchase @" . ($companydi->sgst + $companydi->cgst) . "% (GST)"; $ledger->credit = $companydi->total; $ledger->debit = 0; $ledger->balance = $companydi->total; $ledger->save(); } /*--------------Update Ledger------------------*/ return redirect('/user/company-di')->with('success', 'DI Generated Successfully.'); } else { exit; } } else { return redirect('/user/generate-company-di')->with('error', 'Something went wrong'); } } else { return redirect('/user/company-di')->with('error', "DI Cannot be greater than rake quantity($master_rake_products). Remaining Quantity for DI is $remaining_di_amount. Entered Quantity was $request->quantity"); } } else { return redirect('/user/generate-company-di')->with('error', 'quantity not available in buffer'); } } } public function postGenerateWarehouseDi(Request $request) { //dd($request->all()); $response = array(); $validator = \Validator::make( $request->all(), array( 'to_dealer' => 'required', 'product_id' => 'required', 'unit_id' => 'required', 'product_brand_id' => 'required', 'secondary_freight' => 'required', 'mrp_rate' => 'required', ) ); if ($validator->fails()) { return redirect('/user/generate-warehouse-di') ->withErrors($validator) ->withInput(); } else { $total_quantity = array_sum($request->quantity); if ($request->transfer_type == 1) { $product_stock = \App\Inventory::where('product_company_id', $request->product_brand_id)->where('product_brand_id', $request->product_brand_id)->where('product_id', $request->product_id)->where('unit_id', $request->unit_id)->sum('quantity'); } else { $product_stock = \App\Inventory::where('product_brand_id', $request->product_brand_id)->where('dealer_id', $request->from_dealer)->where('product_id', $request->product_id)->where('unit_id', $request->unit_id)->sum('quantity'); } if ($product_stock >= $total_quantity) { $i = 0; foreach ($request->warehouse as $warehouse) { if ($request->transfer_type == 1) { $inventory = \App\Inventory::where('product_id', $request->product_id)->where('product_company_id', $request->product_company_id)->where('product_brand_id', $request->product_brand_id)->where('warehouse_id', $request->warehouse[$i])->first(); } else if ($request->transfer_type == 2) { $inventory = \App\Inventory::where('product_id', $request->product_id)->where('dealer_id', $request->from_dealer)->where('product_brand_id', $request->product_brand_id)->where('warehouse_id', $request->warehouse[$i])->first(); } if (!is_null($inventory)) { // if($inventory->quantity >= $request->quantity[$i]){ $inventory->quantity = $inventory->quantity - $request->quantity[$i]; $inventory->save(); $dealer_inventory = \App\Inventory::where('product_id', $request->product_id)->where('dealer_id', $request->to_dealer)->where('product_brand_id', $request->product_brand_id)->where('warehouse_id', $request->warehouse[$i])->first(); if (!is_null($dealer_inventory)) { $dealer_inventory->quantity = $dealer_inventory->quantity + $request->quantity[$i]; $dealer_inventory->save(); } else { $dealer_inventory = new \App\Inventory(); $dealer_inventory->dealer_id = $request->to_dealer; $dealer_inventory->warehouse_id = $request->warehouse[$i]; if ($request->transfer_type == 1) { $dealer_inventory->product_brand_id = $request->product_company_id; } else { $dealer_inventory->product_brand_id = $request->product_brand_id; } $dealer_inventory->product_id = $request->product_id; $dealer_inventory->unit_id = $request->unit_id; $dealer_inventory->quantity = $request->quantity[$i]; $dealer_inventory->save(); } if ($request->transfer_type == 1) { $total_debit = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id)->where('dealer_id', $request->to_dealer)->sum('debit'); $total_credit = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id)->where('dealer_id', $request->to_dealer)->sum('credit'); } else { $total_debit = \App\CompanyInvoiceLedger::where('from_dealer_id', $request->from_dealer)->where('dealer_id', $request->to_dealer)->sum('debit'); $total_credit = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id)->where('dealer_id', $request->to_dealer)->sum('credit'); } $opening_balance = $total_debit - $total_credit; $warehouse_id = new \App\WarehouseDi(); $warehouse_id->transfer_type = $request->transfer_type; $warehouse_id->warehouse_id = $request->warehouse[$i]; if ($request->transfer_type == 1) { $warehouse_id->product_company_id = $request->product_company_id; } else { $warehouse_id->from_dealer_id = $request->from_dealer; } $warehouse_id->document_number = $request->document_no; $warehouse_id->dealer_id = $request->to_dealer; $warehouse_id->invoice_number = $request->invoice_number; if ($request->invoice_date) { $warehouse_id->invoice_date = date('Y-m-d', strtotime($request->invoice_date)); } if ($request->dc_date) { $warehouse_id->dc_date = date('Y-m-d', strtotime($request->dc_date)); } if ($request->due_date) { $warehouse_id->due_date = date('Y-m-d', strtotime($request->due_date));; } $warehouse_id->product_hsn = $request->product_hsn; $warehouse_id->cgst = $request->cgst; $warehouse_id->sgst = $request->sgst; $warehouse_id->igst = $request->igst; $warehouse_id->secondary_freight = $request->secondary_freight; $warehouse_id->taxable_amount = $request->taxable_amount; $warehouse_id->discount = $request->discount; $warehouse_id->total = $request->product_total_amount; $warehouse_id->remaining_amount = $request->product_total_amount; $warehouse_id->base_price = $request->product_base_amount; $warehouse_id->rate = $request->product_rate; $warehouse_id->mrp_rate = $request->mrp_rate; $warehouse_id->unit = $request->unit_id; $warehouse_id->quantity = $request->quantity[$i]; $warehouse_id->product = getModelById('Product', $request->product_id)->name; $warehouse_id->product_brand_id = $request->product_brand_id; $warehouse_id->product_id = $request->product_id; $warehouse_id->tcs = $request->tcs; if ($opening_balance > 0) { if ($warehouse_id->total >= $opening_balance) { $warehouse_id->remaining_amount = $warehouse_id->total - $opening_balance; } else { $warehouse_id->remaining_amount = 0; $warehouse_id->is_paid = 1; } } $warehouse_id->save(); if ($warehouse_id->total > 0 && $request->transfer_type == 1) { $ledger = new \App\CompanyInvoiceLedger(); $ledger->product_company_id = $warehouse_id->product_company_id; $ledger->dealer_id = $warehouse_id->dealer_id; $ledger->type = 'credit'; $ledger->particular = "Purchase @" . ($warehouse_id->sgst + $warehouse_id->cgst) . "% (GST)"; $ledger->credit = $warehouse_id->total; $ledger->di_type = 2; $ledger->di_id = $warehouse_id->id; $ledger->debit = 0; $ledger->save(); } if ($warehouse_id->total > 0 && $request->transfer_type == 2) { $ledger = new \App\CompanyInvoiceLedger(); $ledger->from_dealer_id = $warehouse_id->from_dealer_id; $ledger->dealer_id = $warehouse_id->dealer_id; $ledger->type = 'credit'; $ledger->particular = "Purchase @" . ($warehouse_id->sgst + $warehouse_id->cgst) . "% (GST)"; $ledger->credit = $warehouse_id->total; $ledger->di_type = 2; $ledger->di_id = $warehouse_id->id; $ledger->debit = 0; $ledger->save(); } } $i++; } return redirect('/user/generate-warehouse-di')->with('success', "DI Done Successfully"); } else { return redirect('/user/generate-warehouse-di')->with('error', "Total Enter quantity should be less than or equal to total product stock $product_stock. Enter Quantity was $total_quantity "); } } } public function warehouseDi(Request $request) { $data = array(); $data['product_companies'] = \App\ProductCompany::where('is_active', 1)->get(); $data['dealers'] = \App\Dealer::where('is_active', 1)->get(); $data['warehouses'] = \App\Warehouse::where('is_active', 1)->get(); $data['products'] = \App\Product::where('is_active', 1)->get(); $data['bank_accounts'] = \App\BankAccount::where('is_active', 1)->with('bank')->get(); if ($request->isMethod('post')) { $validator = \Validator::make( $request->all(), array( 'type' => 'required', 'product_id' => 'required', ) ); if ($validator->fails()) { return redirect('user/warehouse-di') ->withErrors($validator) ->withInput(); } else { $query = \App\WarehouseDi::query(); if ($request->dealer_id) { $query->where('dealer_id', $request->dealer_id); $data['dealer_id'] = $request->dealer_id; } if ($request->product_company_id) { $query->where('product_company_id', $request->product_company_id); $data['product_company_id'] = $request->product_company_id; } if ($request->from_dealer_id) { $query->where('from_dealer_id', $request->from_dealer_id); $data['from_dealer_id'] = $request->from_dealer_id; } if ($request->warehouse_id) { $query->where('warehouse_id', $request->warehouse_id); $data['warehouse_id'] = $request->warehouse_id; } if ($request->product_id) { $data['total'] = $query->where('product_id', $request->product_id)->sum('quantity'); $data['product_id'] = $request->product_id; } $data['invoices'] = $query->orderBy('id', 'desc')->paginate(100); $data['type'] = $request->type; } } else { $data['invoices'] = \App\WarehouseDi::orderBy('id', 'desc')->paginate(100); $data['total'] = 0; $data['type'] = 1; } return view('dashboard.invoice.warehouse-di', $data); } public function getGenerateWarehouseDi() { $data = array(); $data['warehouses'] = \App\Warehouse::where('is_active', 1)->get(); $data['companies'] = \App\Company::where('is_active', 1)->where('for_invoice', 1)->get(); $data['dealers'] = \App\Dealer::where('is_active', 1)->get(); $data['product_companies'] = \App\ProductCompany::where('is_active', 1)->get(); $data['products'] = \App\Product::where('is_active', 1)->get(); $data['units'] = \App\Unit::where('is_active', 1)->get(); return view('dashboard.invoice.generate-warehouse-di', $data); } public function invoices(Request $request) { $data = array(); $data['dealers'] = \App\Dealer::where('is_active', 1)->get(); $data['retailer'] = \App\Retailer::where('is_active', 1)->get(); if ($request->isMethod('post')) { $query = \App\LoadingSlipInvoice::query(); if ($request->dealer_id != "") { $query = $query->where('dealer_id', $request->dealer_id); $date['dealer_id'] = $request->dealer_id; } if ($request->retailer_id != "") { $query = $query->where('retailer_id', $request->retailer_id); $date['retailer_id'] = $request->retailer_id; } $query = $query->orderBy('id', 'desc')->paginate(100); $data['loading_slip_invoices'] = $query; } else { $data['loading_slip_invoices'] = \App\LoadingSlipInvoice::with('retailer_invoice_payments')->orderBy('id', 'desc')->paginate(100); } return view('dashboard.invoice.loading_slip_invoices', $data); } public function getPunchInvoice() { $data['companies'] = \App\Company::where('is_active', 1)->where('for_invoice', 1)->get(); $data['retailers'] = \App\Retailer::where('is_active', 1)->get(); return view('dashboard.invoice.punchInvoice', $data); } public function postPunchInvoice(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'company_id' => 'required', 'retailer_id' => 'required', 'invoice_number' => 'required', 'invoice_date' => 'required', 'invoice_amount' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $validator->getMessageBag(); } else { $invoice = new \App\Invoice(); $invoice->company_id = $request->company_id; $invoice->retailer_id = $request->retailer_id; $invoice->invoice_date = $request->invoice_date; $invoice->invoice_number = $request->invoice_number; $invoice->invoice_amount = $request->invoice_amount; $invoice->invoice_remarks = $request->invoice_remark; $invoice->received_date = $request->received_date; $invoice->received_amount = $request->received_amount; $duplicateInvoice = \App\Invoice::where('invoice_number', $request->invoice_number)->first(); if ($duplicateInvoice) { $response['flag'] = false; $response['message'] = 'Invoice number already exists.'; } else { if ($request->file('invoice_doc') !== '' || !empty($request->file('invoice_doc'))) { $destinationPath = 'uploads'; $moved_file_status = $file->move($destinationPath, $file->getClientOriginalName()); if ($moved_file_status) { } } if ($invoice->save()) { $response['flag'] = true; $response['message'] = "Invoice Punched Successfully."; } else { $response['flag'] = false; $response['error'] = "Something Went Wrong"; } } } return response()->json($response); } // Function to get generated invoices public function getGeneratedInvoices() { } // Function to generate invoice public function generateLoadingSlipInvoice() { $data['acting_company'] = Session::get('acting_company'); $data['invoice_types'] = \App\InvoiceType::where('is_active', 1)->get(); $data['companies'] = \App\Company::where('is_active', 1)->where('for_invoice', 1)->get(); $data['loading_slips'] = \App\ProductLoading::where('is_invoice_generated', 0)->with('token')->get(); $data['retailers'] = \App\Retailer::where('is_active', 1)->get(); $data['products'] = \App\Product::where('is_active', 1)->get(); $data['units'] = \App\Unit::where('is_active', 1)->get(); return view('dashboard.invoice.generate-loading-slip-invoice', $data); } public function loadingSlipDetails($loading_slip_id) { $data = array(); $decoded_value = base64_decode($loading_slip_id); $loadingSlipDetailsArray = explode(',', $decoded_value); if ($loadingSlipDetailsArray[0] == "loading_slip") { $data['retailers'] = \App\Retailer::where('is_active', 1)->get(); $data['acting_company'] = Session::get('acting_company'); $data['invoice_types'] = \App\InvoiceType::where('is_active', 1)->get(); $data['companies'] = \App\Company::where('is_active', 1)->where('for_invoice', 1)->get(); $product_loading = \App\ProductLoading::where('id', $loadingSlipDetailsArray[1])->with('token')->first(); if (is_null($product_loading)) { $data['status'] = false; $data['message'] = "Loading Slip Not found"; } else if (is_null($product_loading->retailer_id)) { $data['status'] = false; $data['message'] = "Invalid Loading Slip"; } else if (is_null($product_loading->token_id) && $loading_slip->token->to_type != 2) { $data['status'] = false; $data['message'] = "Scanning Wrong QR Code"; } else if ($product_loading->qr_scan_count > 0) { $data['status'] = false; $data['message'] = "Invoice already Generated"; } else { $data['status'] = true; $data['product_loading'] = $product_loading; } } else { $data['status'] = false; $data['message'] = "Invalid QR Code"; } return view('dashboard.invoice.loading-slip-details', $data); } public function saveLoadingSlipInvoice(Request $request) { $data = array(); // dd($request->all()); $loading_slip = \App\ProductLoading::where('id', $request->loading_slip)->first(); if ($loading_slip->is_invoice_generated) { echo "invoice is already generated"; exit; } else if (is_null($loading_slip->retailer_id)) { echo "Invalid Loading Slip"; exit; } else { $loading_slip->qr_scan_count = 1; $loading_slip->processing_step = 1; $loading_slip->is_invoice_generated = 1; $loading_slip->invoice_generated_date = date('Y-m-d'); if ($loading_slip->save()) { // if(1){ $token = \App\Token::where('id', $request->token_id)->with('company')->first(); $product_details = getModelById('Product', $loading_slip->product_id); $cgst_percentage = $product_details->cgst; $sgst_percentage = $product_details->sgst; $rate = $token->rate - $request->freight_discount; if ($request->secondary_discount != "") { $rate = $rate - $request->secondary_discount; } $rates = $rate * 100 / (100 + $product_details->cgst + $product_details->sgst); $amount = $rates * $loading_slip->quantity; $cgst_amount = ($cgst_percentage / 100) * $amount; $sgst_amount = ($sgst_percentage / 100) * $amount; $total = $amount + $cgst_amount + $sgst_amount + $request->tcs; $total_invoice = \App\LoadingSlipInvoice::where('invoice_type_id', $request->invoice_type_id)->count(); if (is_null($total_invoice) || ($total_invoice == 0)) { $next_number = 1; } else { $last_invoice = \App\LoadingSlipInvoice::where('invoice_type_id', $request->invoice_type_id)->orderBy('id', 'desc')->first(); $arr = explode('/', $last_invoice->invoice_number); $next_number = $arr[2] + 1; } $loading_slip_invoice = new \App\LoadingSlipInvoice(); $loading_slip_invoice->loading_slip_id = $request->loading_slip; $loading_slip_invoice->invoice_type = getModelById('InvoiceType', $request->invoice_type_id)->invoice_type; $loading_slip_invoice->invoice_type_id = getModelById('InvoiceType', $request->invoice_type_id)->id; $loading_slip_invoice->invoice_number = getModelById('InvoiceType', $request->invoice_type_id)->invoice_type . $next_number; $loading_slip_invoice->eway_bill_number = $request->eway_bill_no; $loading_slip_invoice->company_id = $request->company_id; $loading_slip_invoice->invoice_date = $this->dateConverter($request->invoice_date); $loading_slip_invoice->dealer_id = $token->account_from_id; $loading_slip_invoice->retailer_id = $token->retailer_id; $loading_slip_invoice->retailer_name = getModelById('Retailer', $token->retailer_id)->name; $loading_slip_invoice->delivery_note = $request->invoice_remark; $loading_slip_invoice->despatched_through = $request->dispatched_through; $loading_slip_invoice->destination = $request->destination; $loading_slip_invoice->terms_of_delivery = $request->terms_of_delivery; $loading_slip_invoice->product_id = $token->product_id; $loading_slip_invoice->product = $loading_slip->product_name; $loading_slip_invoice->product_hsn = $product_details->hsn_code; $loading_slip_invoice->quantity = $loading_slip->quantity; $loading_slip_invoice->unit = $loading_slip->unit_name; $loading_slip_invoice->rate = $token->rate; $loading_slip_invoice->freight_discount = $request->freight_discount; $loading_slip_invoice->cgst = round($cgst_amount, 2); $loading_slip_invoice->sgst = round($sgst_amount, 2); $loading_slip_invoice->tcs = $request->tcs; $loading_slip_invoice->total = $total; $loading_slip_invoice->remaining_amount = $total; $loading_slip_invoice->invoice_number; if ($request->secondary_discount != "") { $loading_slip_invoice->secondary_discount = $request->secondary_discount; } if ($loading_slip_invoice->save()) { $message = "Your Invoice has been Generated for Loading slip-" . $loading_slip->id . ". Invoice Number is " . $loading_slip_invoice->invoice_number; // if(!is_null($token->retailer_id)){ // $retailer = \App\Retailer::where('id',$token->retailer_id)->first(); // if(!is_null($retailer->mobile_number) && strlen($retailer->mobile_number) == 10){ // $company = \App\Company::where('id',$request->company_id)->with('company_sms_setting')->first(); // SmsController::sendSms(array($retailer->mobile_number),$message,$company->company_sms_setting->sms_sender_id); // $sms_report = new \App\RetailerSmsReport(); // $sms_report->company_id = $request->company_id; // $sms_report->sms_sender_id = $company->company_sms_setting->sms_sender_id ; // $sms_report->retailer_id = $token->retailer_id; // $sms_report->retailer_name = $loading_slip_invoice->retailer_name; // $sms_report->company_name = $company->name; // $sms_report->mobile_number = $retailer->mobile_number; // $sms_report->message = $message; // $sms_report->save(); // } // } $loading_slip->invoice_number = $loading_slip_invoice->invoice_number; $loading_slip->save(); $sale = $sgst_percentage + $cgst_percentage; /*--------------------------------tally invoice update------------------------------*/ // $requestXML = '<ENVELOPE> // <HEADER> // <TALLYREQUEST>Import Data</TALLYREQUEST> // </HEADER> // <BODY> // <IMPORTDATA> // <REQUESTDESC> // <REPORTNAME>All Masters</REPORTNAME> // <STATICVARIABLES> // <SVCURRENTCOMPANY>MOSARAM SHIVRAM DAS</SVCURRENTCOMPANY> // </STATICVARIABLES> // </REQUESTDESC> // <REQUESTDATA> // <TALLYMESSAGE xmlns:UDF="TallyUDF"> // <VOUCHER VCHTYPE="Tax Invoice" ACTION="Create" OBJVIEW="Invoice Voucher View"> // <ADDRESS.LIST TYPE="String"> // <ADDRESS>' . $request->destination . '</ADDRESS> // </ADDRESS.LIST> // <BASICBUYERADDRESS.LIST TYPE="String"> // <BASICBUYERADDRESS>' . $request->destination . '</BASICBUYERADDRESS> // </BASICBUYERADDRESS.LIST> // <OLDAUDITENTRYIDS.LIST TYPE="Number"> // <OLDAUDITENTRYIDS>-1</OLDAUDITENTRYIDS> // </OLDAUDITENTRYIDS.LIST> // <DATE>20220402</DATE> // <GUID>2fe275e4-4606-40d4-85e2-2f4047f002e3-00019cb6</GUID> // <GSTREGISTRATIONTYPE>Consumer</GSTREGISTRATIONTYPE> // <VATDEALERTYPE>Regular</VATDEALERTYPE> // <STATENAME>Uttar Pradesh</STATENAME> // <VOUCHERTYPENAME>Tax Invoice</VOUCHERTYPENAME> // <ENTEREDBY>mrsd</ENTEREDBY> // <COUNTRYOFRESIDENCE>India</COUNTRYOFRESIDENCE> // <PLACEOFSUPPLY>Uttar Pradesh</PLACEOFSUPPLY> // <PARTYNAME>' . getModelById('Retailer', $token->retailer_id)->name . ',' . $request->destination . '</PARTYNAME> // <PARTYLEDGERNAME>' . getModelById('Retailer', $token->retailer_id)->name . ',' . $request->destination . '</PARTYLEDGERNAME> // <PARTYMAILINGNAME>' . getModelById('Retailer', $token->retailer_id)->name . ',' . $request->destination . '</PARTYMAILINGNAME> // <CONSIGNEEMAILINGNAME>' . getModelById('Retailer', $token->retailer_id)->name . ',' . $request->destination . '</CONSIGNEEMAILINGNAME> // <CONSIGNEESTATENAME>Uttar Pradesh</CONSIGNEESTATENAME> // <VOUCHERNUMBER>' . getModelById('InvoiceType', $request->invoice_type_id)->invoice_type . $next_number . '</VOUCHERNUMBER> // <BASICBASEPARTYNAME>' . getModelById('Retailer', $token->retailer_id)->name . ',' . $request->destination . '</BASICBASEPARTYNAME> // <CSTFORMISSUETYPE/> // <CSTFORMRECVTYPE/> // <FBTPAYMENTTYPE>Default</FBTPAYMENTTYPE> // <PERSISTEDVIEW>Invoice Voucher View</PERSISTEDVIEW> // <BASICBUYERNAME>' . getModelById('Retailer', $token->retailer_id)->name . ',' . $request->destination . '</BASICBUYERNAME> // <CONSIGNEECOUNTRYNAME>India</CONSIGNEECOUNTRYNAME> // <VCHGSTCLASS/> // <VCHENTRYMODE>Item Invoice</VCHENTRYMODE> // <VOUCHERTYPEORIGNAME>Tax Invoice</VOUCHERTYPEORIGNAME> // <DIFFACTUALQTY>No</DIFFACTUALQTY> // <ISMSTFROMSYNC>No</ISMSTFROMSYNC> // <ISDELETED>No</ISDELETED> // <ISSECURITYONWHENENTERED>No</ISSECURITYONWHENENTERED> // <ASORIGINAL>No</ASORIGINAL> // <AUDITED>No</AUDITED> // <FORJOBCOSTING>No</FORJOBCOSTING> // <ISOPTIONAL>No</ISOPTIONAL> // <EFFECTIVEDATE>' . $this->dateConverter($request->invoice_date) . '</EFFECTIVEDATE> // <USEFOREXCISE>No</USEFOREXCISE> // <ISFORJOBWORKIN>No</ISFORJOBWORKIN> // <ALLOWCONSUMPTION>No</ALLOWCONSUMPTION> // <USEFORINTEREST>No</USEFORINTEREST> // <USEFORGAINLOSS>No</USEFORGAINLOSS> // <USEFORGODOWNTRANSFER>No</USEFORGODOWNTRANSFER> // <USEFORCOMPOUND>No</USEFORCOMPOUND> // <USEFORSERVICETAX>No</USEFORSERVICETAX> // <ISONHOLD>No</ISONHOLD> // <ISBOENOTAPPLICABLE>No</ISBOENOTAPPLICABLE> // <ISGSTSECSEVENAPPLICABLE>No</ISGSTSECSEVENAPPLICABLE> // <ISEXCISEVOUCHER>No</ISEXCISEVOUCHER> // <EXCISETAXOVERRIDE>No</EXCISETAXOVERRIDE> // <USEFORTAXUNITTRANSFER>No</USEFORTAXUNITTRANSFER> // <IGNOREPOSVALIDATION>No</IGNOREPOSVALIDATION> // <EXCISEOPENING>No</EXCISEOPENING> // <USEFORFINALPRODUCTION>No</USEFORFINALPRODUCTION> // <ISTDSOVERRIDDEN>No</ISTDSOVERRIDDEN> // <ISTCSOVERRIDDEN>No</ISTCSOVERRIDDEN> // <ISTDSTCSCASHVCH>No</ISTDSTCSCASHVCH> // <INCLUDEADVPYMTVCH>No</INCLUDEADVPYMTVCH> // <ISSUBWORKSCONTRACT>No</ISSUBWORKSCONTRACT> // <ISVATOVERRIDDEN>No</ISVATOVERRIDDEN> // <IGNOREORIGVCHDATE>No</IGNOREORIGVCHDATE> // <ISVATPAIDATCUSTOMS>No</ISVATPAIDATCUSTOMS> // <ISDECLAREDTOCUSTOMS>No</ISDECLAREDTOCUSTOMS> // <ISSERVICETAXOVERRIDDEN>No</ISSERVICETAXOVERRIDDEN> // <ISISDVOUCHER>No</ISISDVOUCHER> // <ISEXCISEOVERRIDDEN>No</ISEXCISEOVERRIDDEN> // <ISEXCISESUPPLYVCH>No</ISEXCISESUPPLYVCH> // <ISGSTOVERRIDDEN>No</ISGSTOVERRIDDEN> // <GSTNOTEXPORTED>No</GSTNOTEXPORTED> // <IGNOREGSTINVALIDATION>No</IGNOREGSTINVALIDATION> // <ISGSTREFUND>No</ISGSTREFUND> // <OVRDNEWAYBILLAPPLICABILITY>No</OVRDNEWAYBILLAPPLICABILITY> // <ISVATPRINCIPALACCOUNT>No</ISVATPRINCIPALACCOUNT> // <IGNOREEINVVALIDATION>No</IGNOREEINVVALIDATION> // <IRNJSONEXPORTED>No</IRNJSONEXPORTED> // <IRNCANCELLED>No</IRNCANCELLED> // <ISSHIPPINGWITHINSTATE>No</ISSHIPPINGWITHINSTATE> // <ISOVERSEASTOURISTTRANS>No</ISOVERSEASTOURISTTRANS> // <ISDESIGNATEDZONEPARTY>No</ISDESIGNATEDZONEPARTY> // <ISCANCELLED>No</ISCANCELLED> // <HASCASHFLOW>No</HASCASHFLOW> // <ISPOSTDATED>No</ISPOSTDATED> // <USETRACKINGNUMBER>No</USETRACKINGNUMBER> // <ISINVOICE>Yes</ISINVOICE> // <MFGJOURNAL>No</MFGJOURNAL> // <HASDISCOUNTS>No</HASDISCOUNTS> // <ASPAYSLIP>No</ASPAYSLIP> // <ISCOSTCENTRE>No</ISCOSTCENTRE> // <ISSTXNONREALIZEDVCH>No</ISSTXNONREALIZEDVCH> // <ISEXCISEMANUFACTURERON>No</ISEXCISEMANUFACTURERON> // <ISBLANKCHEQUE>No</ISBLANKCHEQUE> // <ISVOID>No</ISVOID> // <ORDERLINESTATUS>No</ORDERLINESTATUS> // <VATISAGNSTCANCSALES>No</VATISAGNSTCANCSALES> // <VATISPURCEXEMPTED>No</VATISPURCEXEMPTED> // <ISVATRESTAXINVOICE>No</ISVATRESTAXINVOICE> // <VATISASSESABLECALCVCH>No</VATISASSESABLECALCVCH> // <ISVATDUTYPAID>Yes</ISVATDUTYPAID> // <ISDELIVERYSAMEASCONSIGNEE>No</ISDELIVERYSAMEASCONSIGNEE> // <ISDISPATCHSAMEASCONSIGNOR>No</ISDISPATCHSAMEASCONSIGNOR> // <ISDELETEDVCHRETAINED>No</ISDELETEDVCHRETAINED> // <CHANGEVCHMODE>No</CHANGEVCHMODE> // <RESETIRNQRCODE>No</RESETIRNQRCODE> // <ALLINVENTORYENTRIES.LIST> // <STOCKITEMNAME>' . $loading_slip->product_name . '</STOCKITEMNAME> // <ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE> // <ISLASTDEEMEDPOSITIVE>No</ISLASTDEEMEDPOSITIVE> // <ISAUTONEGATE>No</ISAUTONEGATE> // <ISCUSTOMSCLEARANCE>No</ISCUSTOMSCLEARANCE> // <ISTRACKCOMPONENT>No</ISTRACKCOMPONENT> // <ISTRACKPRODUCTION>No</ISTRACKPRODUCTION> // <ISPRIMARYITEM>No</ISPRIMARYITEM> // <ISSCRAP>No</ISSCRAP> // <RATE>' . round($rates, 2) . '</RATE> // <AMOUNT>' . round($amount, 2) . '</AMOUNT> // <ACTUALQTY>' . $loading_slip->quantity . '</ACTUALQTY> // <BILLEDQTY>' . $loading_slip->quantity . '</BILLEDQTY> // <BATCHALLOCATIONS.LIST> // <GODOWNNAME>' . getModelById('Warehouse', $loading_slip->from_warehouse_id)->name . '</GODOWNNAME> // <BATCHNAME>Primary Batch</BATCHNAME> // <DESTINATIONGODOWNNAME>' . getModelById('Warehouse', $loading_slip->from_warehouse_id)->name . '</DESTINATIONGODOWNNAME> // <INDENTNO/> // <ORDERNO/> // <TRACKINGNUMBER/> // <DYNAMICCSTISCLEARED>No</DYNAMICCSTISCLEARED> // <AMOUNT>' . round($amount, 2) . '</AMOUNT> // <ACTUALQTY>' . $loading_slip->quantity . '</ACTUALQTY> // <BILLEDQTY>' . $loading_slip->quantity . '</BILLEDQTY> // </BATCHALLOCATIONS.LIST> // <ACCOUNTINGALLOCATIONS.LIST> // <OLDAUDITENTRYIDS.LIST TYPE="Number"> // <OLDAUDITENTRYIDS>-1</OLDAUDITENTRYIDS> // </OLDAUDITENTRYIDS.LIST> // <LEDGERNAME>Sales @ ' . $sale . '% (Gst)</LEDGERNAME> // <GSTCLASS/> // <ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE> // <LEDGERFROMITEM>No</LEDGERFROMITEM> // <REMOVEZEROENTRIES>No</REMOVEZEROENTRIES> // <ISPARTYLEDGER>No</ISPARTYLEDGER> // <ISLASTDEEMEDPOSITIVE>No</ISLASTDEEMEDPOSITIVE> // <ISCAPVATTAXALTERED>No</ISCAPVATTAXALTERED> // <ISCAPVATNOTCLAIMED>No</ISCAPVATNOTCLAIMED> // <AMOUNT>' . round($amount, 2) . '</AMOUNT> // </ACCOUNTINGALLOCATIONS.LIST> // </ALLINVENTORYENTRIES.LIST> // <LEDGERENTRIES.LIST> // <OLDAUDITENTRYIDS.LIST TYPE="Number"> // <OLDAUDITENTRYIDS>-1</OLDAUDITENTRYIDS> // </OLDAUDITENTRYIDS.LIST> // <LEDGERNAME>' . getModelById('Retailer', $token->retailer_id)->name . ',' . $request->destination . '</LEDGERNAME> // <GSTCLASS/> // <ISDEEMEDPOSITIVE>Yes</ISDEEMEDPOSITIVE> // <LEDGERFROMITEM>No</LEDGERFROMITEM> // <REMOVEZEROENTRIES>No</REMOVEZEROENTRIES> // <ISPARTYLEDGER>Yes</ISPARTYLEDGER> // <ISLASTDEEMEDPOSITIVE>Yes</ISLASTDEEMEDPOSITIVE> // <ISCAPVATTAXALTERED>No</ISCAPVATTAXALTERED> // <ISCAPVATNOTCLAIMED>No</ISCAPVATNOTCLAIMED> // <AMOUNT>-' . round($total, 2) . '</AMOUNT> // <BILLALLOCATIONS.LIST> // <NAME>1</NAME> // <BILLTYPE>Agst Ref</BILLTYPE> // <TDSDEDUCTEEISSPECIALRATE>No</TDSDEDUCTEEISSPECIALRATE> // <AMOUNT>-' . round($total, 2) . '</AMOUNT> // </BILLALLOCATIONS.LIST> // </LEDGERENTRIES.LIST> // <LEDGERENTRIES.LIST> // <OLDAUDITENTRYIDS.LIST TYPE="Number"> // <OLDAUDITENTRYIDS>-1</OLDAUDITENTRYIDS> // </OLDAUDITENTRYIDS.LIST> // <BASICRATEOFINVOICETAX.LIST TYPE="Number"> // <BASICRATEOFINVOICETAX>' . round($cgst_percentage, 2) . '</BASICRATEOFINVOICETAX> // </BASICRATEOFINVOICETAX.LIST> // <LEDGERNAME>Input CGST @' . $cgst_percentage . '%</LEDGERNAME> // <GSTCLASS/> // <ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE> // <LEDGERFROMITEM>No</LEDGERFROMITEM> // <REMOVEZEROENTRIES>No</REMOVEZEROENTRIES> // <ISPARTYLEDGER>No</ISPARTYLEDGER> // <ISLASTDEEMEDPOSITIVE>No</ISLASTDEEMEDPOSITIVE> // <ISCAPVATTAXALTERED>No</ISCAPVATTAXALTERED> // <ISCAPVATNOTCLAIMED>No</ISCAPVATNOTCLAIMED> // <AMOUNT>' . round($cgst_amount, 2) . '</AMOUNT> // <VATEXPAMOUNT>' . round($cgst_amount, 2) . '</VATEXPAMOUNT> // </LEDGERENTRIES.LIST> // <LEDGERENTRIES.LIST> // <OLDAUDITENTRYIDS.LIST TYPE="Number"> // <OLDAUDITENTRYIDS>-1</OLDAUDITENTRYIDS> // </OLDAUDITENTRYIDS.LIST> // <LEDGERNAME>Input SGST@ ' . $sgst_percentage . '%</LEDGERNAME> // <GSTCLASS/> // <ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE> // <LEDGERFROMITEM>No</LEDGERFROMITEM> // <REMOVEZEROENTRIES>No</REMOVEZEROENTRIES> // <ISPARTYLEDGER>No</ISPARTYLEDGER> // <ISLASTDEEMEDPOSITIVE>No</ISLASTDEEMEDPOSITIVE> // <ISCAPVATTAXALTERED>No</ISCAPVATTAXALTERED> // <ISCAPVATNOTCLAIMED>No</ISCAPVATNOTCLAIMED> // <AMOUNT>' . round($sgst_amount, 2) . '</AMOUNT> // <VATEXPAMOUNT>' . round($sgst_amount, 2) . '</VATEXPAMOUNT> // </LEDGERENTRIES.LIST> // </VOUCHER> // </TALLYMESSAGE> // <TALLYMESSAGE xmlns:UDF="TallyUDF"> // <COMPANY> // <REMOTECMPINFO.LIST MERGE="Yes"> // <NAME>2fe275e4-4606-40d4-85e2-2f4047f002e3</NAME> // <REMOTECMPNAME>MOSARAM SHIVRAM DAS</REMOTECMPNAME> // <REMOTECMPSTATE>Uttar Pradesh</REMOTECMPSTATE> // </REMOTECMPINFO.LIST> // </COMPANY> // </TALLYMESSAGE> // </REQUESTDATA> // </IMPORTDATA> // </BODY> // </ENVELOPE>'; // $headers = array("Content-type: text/xml", "Content-length: " . strlen($requestXML), "Connection: close"); // $ch = curl_init(); // curl_setopt($ch, CURLOPT_URL, 'http://localhost:9000'); // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // curl_setopt($ch, CURLOPT_TIMEOUT, 100); // curl_setopt($ch, CURLOPT_POST, true); // curl_setopt($ch, CURLOPT_POSTFIELDS, $requestXML); // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // $datas = curl_exec($ch); // if (curl_errno($ch)) { // print curl_error($ch); // echo " something went wrong..... try later"; // } else { // echo " request accepted"; // print $datas; // curl_close($ch); // } /*---------------------------end tally invoice update--------------------------------*/ /*------------Update Party Ledger--------------*/ $ledger = \App\PartyInvoiceLedger::where('retailer_id', $loading_slip_invoice->retailer_id)->where('dealer_id', $loading_slip_invoice->dealer_id)->orderBy('id', 'desc')->first(); if (!is_null($ledger)) { $balance = $ledger->balance; if ($balance < 0) { if (abs($balance) < $total) { $loading_slip_invoice->remaining_amount = $total - abs($balance); $loading_slip_invoice->save(); } else { $loading_slip_invoice->remaining_amount = 0; $loading_slip_invoice->save(); } } $ledger = new \App\PartyInvoiceLedger(); $ledger->retailer_id = $loading_slip_invoice->retailer_id; $ledger->dealer_id = $loading_slip_invoice->dealer_id; $ledger->transaction_date = date('Y-m-d'); $ledger->particular = "Purchase of " . $loading_slip_invoice->product . " from " . getModelById('Dealer', $loading_slip_invoice->dealer_id)->name . "(" . getModelById('Dealer', $loading_slip_invoice->dealer_id)->address1 . ")"; $ledger->credit = 0; $ledger->debit = $loading_slip_invoice->total; $ledger->balance = $balance + $loading_slip_invoice->total; $ledger->against = $loading_slip_invoice->invoice_number; $ledger->save(); } else { $ledger = new \App\PartyInvoiceLedger(); $ledger->retailer_id = $loading_slip_invoice->retailer_id; $ledger->dealer_id = $loading_slip_invoice->dealer_id; $ledger->transaction_date = date('Y-m-d'); $ledger->particular = "Purchase of " . $loading_slip_invoice->product . " from " . getModelById('Dealer', $loading_slip_invoice->dealer_id)->name . "(" . getModelById('Dealer', $loading_slip_invoice->dealer_id)->address1 . ")"; $ledger->credit = 0; $ledger->debit = $loading_slip_invoice->total; $ledger->balance = $loading_slip_invoice->total; $ledger->against = $loading_slip_invoice->invoice_number; $ledger->save(); } /* Secondary Discount Party Ledger */ if ($request->secondary_discount != "" && $request->secondary_discount != 0) { $Secondaryledger = \App\SecondaryDiscountPartyLedger::where('retailer_id', $loading_slip_invoice->retailer_id)->where('dealer_id', $loading_slip_invoice->dealer_id)->orderBy('id', 'desc')->first(); $discount_total = $request->secondary_discount * $loading_slip->quantity; if (!is_null($Secondaryledger)) { $discount_total_balance = $Secondaryledger->balance + $discount_total; } else { $discount_total_balance = $discount_total; } $sledger = new \App\SecondaryDiscountPartyLedger(); $sledger->retailer_id = $loading_slip_invoice->retailer_id; $sledger->transaction_date = date('Y-m-d'); $sledger->dealer_id = $loading_slip_invoice->dealer_id; $sledger->particular = "Purchase of " . $loading_slip_invoice->product . " from " . getModelById('Dealer', $loading_slip_invoice->dealer_id)->name . "(" . getModelById('Dealer', $loading_slip_invoice->dealer_id)->address1 . ")"; $sledger->credit = 0; $sledger->debit = $discount_total; $sledger->balance = $discount_total_balance; $sledger->against = $loading_slip_invoice->invoice_number; $sledger->save(); } /*------------Update Party Ledger--------------*/ $data['invoice_data'] = array( "company_id" => $request->company_id, "invoice_type_id" => $request->invoice_type_id, "invoice_number" => $next_number, "invoice_date" => $this->dateConverter($request->invoice_date), "eway_bill_no" => $request->eway_bill_no, "retailer_id" => $token->retailer_id, "invoice_remark" => $request->invoice_remark, "dispatched_through" => $request->dispatched_through, "destination" => $request->destination, "terms_of_delivery" => $request->terms_of_delivery, ); $products = array(); array_push($products, $token->product_id); $quantity = array(); array_push($quantity, $loading_slip->quantity); $rate = array(); array_push($rate, $token->rate); $freight_discount = array(); array_push($freight_discount, $request->freight_discount); $secondary_discount = array(); array_push($secondary_discount, $request->secondary_discount); $unit = array(); array_push($unit, $token->unit_id); $data['product_id'] = $products; $data['quantity'] = $quantity; $data['product_rate'] = $rate; $data['freight_discount'] = $freight_discount; $data['secondary_discount'] = $secondary_discount; $data['product_unit'] = $unit; $data['tcs'] = $loading_slip_invoice->tcs; $data['company'] = \App\Company::where('id', $request->company_id)->first(); return view('dashboard.invoice.print-loading-slip-invoice', $data); } else { echo "something went wrong"; exit; } //}else { // echo "something went wrong"; exit; //} } else { echo "something went wrong"; exit; } } } public function saveMultipleLoadingSlipInvoice(Request $request) { $data = array(); // dd($request->all()); $loading_slip = \App\ProductLoading::where('id', $request->loading_slip)->first(); if ($loading_slip->is_invoice_generated) { echo "invoice is already generated"; exit; } else if (is_null($loading_slip->retailer_id)) { echo "Invalid Loading Slip"; exit; } else { $loading_slip->qr_scan_count = 1; $loading_slip->processing_step = 1; $loading_slip->is_invoice_generated = 1; $loading_slip->invoice_generated_date = date('Y-m-d'); if ($loading_slip->save()) { $invoice_numbers = ""; $i = 0; foreach ($request->retailer_id as $key => $value) { $token = \App\Token::where('id', $request->token_id)->with('company')->first(); $product_details = getModelById('Product', $loading_slip->product_id); $cgst_percentage = $product_details->cgst; $sgst_percentage = $product_details->sgst; $rate = $token->rate - $request->freight_discount[$i]; if ($request->secondary_discount[$i] != "") { $rate = $rate - $request->secondary_discount[$i]; } $rate = $rate * 100 / (100 + $product_details->cgst + $product_details->sgst); $amount = $rate * $request->quantity[$i]; $cgst_amount = ($cgst_percentage / 100) * $amount; $sgst_amount = ($sgst_percentage / 100) * $amount; $total = $amount + $cgst_amount + $sgst_amount + $request->tcs[$i]; $total_invoice = \App\LoadingSlipInvoice::where('invoice_type_id', $request->invoice_type_id)->count(); if (is_null($total_invoice) || ($total_invoice == 0)) { $next_number = 1; } else { $last_invoice = \App\LoadingSlipInvoice::where('invoice_type_id', $request->invoice_type_id)->orderBy('id', 'desc')->first(); $arr = explode('/', $last_invoice->invoice_number); $next_number = $arr[2] + 1; } $loading_slip_invoice = new \App\LoadingSlipInvoice(); $loading_slip_invoice->loading_slip_id = $request->loading_slip; $loading_slip_invoice->invoice_type = getModelById('InvoiceType', $request->invoice_type_id)->invoice_type; $loading_slip_invoice->invoice_type_id = getModelById('InvoiceType', $request->invoice_type_id)->id; $loading_slip_invoice->invoice_number = getModelById('InvoiceType', $request->invoice_type_id)->invoice_type . $next_number; $loading_slip_invoice->eway_bill_number = $request->eway_bill_no[$i]; $loading_slip_invoice->company_id = $request->company_id; $loading_slip_invoice->invoice_date = $this->dateConverter($request->invoice_date); $loading_slip_invoice->dealer_id = $token->account_from_id; $loading_slip_invoice->retailer_id = $request->retailer_id[$i]; $loading_slip_invoice->retailer_name = getModelById('Retailer', $request->retailer_id[$i])->name; $loading_slip_invoice->delivery_note = $request->invoice_remark; $loading_slip_invoice->despatched_through = $request->dispatched_through; $loading_slip_invoice->destination = $request->destination; $loading_slip_invoice->terms_of_delivery = $request->terms_of_delivery; $loading_slip_invoice->product_id = $token->product_id; $loading_slip_invoice->product = $loading_slip->product_name; $loading_slip_invoice->product_hsn = $product_details->hsn_code; $loading_slip_invoice->quantity = $request->quantity[$i]; $loading_slip_invoice->unit = $loading_slip->unit_name; $loading_slip_invoice->rate = $token->rate; $loading_slip_invoice->freight_discount = $request->freight_discount[$i]; $loading_slip_invoice->cgst = $cgst_amount; $loading_slip_invoice->sgst = $sgst_amount; $loading_slip_invoice->tcs = $request->tcs[$i]; $loading_slip_invoice->total = $total; $loading_slip_invoice->remaining_amount = $total; $loading_slip_invoice->invoice_number; if ($request->secondary_discount[$i] != "") { $loading_slip_invoice->secondary_discount = $request->secondary_discount[$i]; } if ($loading_slip_invoice->save()) { $invoice_numbers .= $loading_slip_invoice->invoice_number . ", "; $message = "Your Invoice has been Generated for Loading slip-" . $loading_slip->id . ". Invoice Number is " . $loading_slip_invoice->invoice_number; // if(!is_null($token->retailer_id)){ // $retailer = \App\Retailer::where('id',$token->retailer_id)->first(); // if(!is_null($retailer->mobile_number) && strlen($retailer->mobile_number) == 10){ // $company = \App\Company::where('id',$request->company_id)->with('company_sms_setting')->first(); // SmsController::sendSms(array($retailer->mobile_number),$message,$company->company_sms_setting->sms_sender_id); // $sms_report = new \App\RetailerSmsReport(); // $sms_report->company_id = $request->company_id; // $sms_report->sms_sender_id = $company->company_sms_setting->sms_sender_id ; // $sms_report->retailer_id = $token->retailer_id; // $sms_report->retailer_name = $loading_slip_invoice->retailer_name; // $sms_report->company_name = $company->name; // $sms_report->mobile_number = $retailer->mobile_number; // $sms_report->message = $message; // $sms_report->save(); // } // } $loading_slip->invoice_number = $invoice_numbers; $loading_slip->save(); /*------------Update Party Ledger--------------*/ $ledger = \App\PartyInvoiceLedger::where('retailer_id', $loading_slip_invoice->retailer_id)->where('dealer_id', $loading_slip_invoice->dealer_id)->orderBy('id', 'desc')->first(); if (!is_null($ledger)) { $ledger = new \App\PartyInvoiceLedger(); $ledger->retailer_id = $loading_slip_invoice->retailer_id; $ledger->dealer_id = $loading_slip_invoice->dealer_id; $ledger->transaction_date = date('Y-m-d'); $ledger->particular = "Purchase of " . $loading_slip_invoice->product . " from " . getModelById('Dealer', $loading_slip_invoice->dealer_id)->name . "(" . getModelById('Dealer', $loading_slip_invoice->dealer_id)->address1 . ")"; $ledger->credit = 0; $ledger->debit = $loading_slip_invoice->total; $ledger->balance = 0; $ledger->against = $loading_slip_invoice->invoice_number; $ledger->save(); } else { $ledger = new \App\PartyInvoiceLedger(); $ledger->retailer_id = $loading_slip_invoice->retailer_id; $ledger->dealer_id = $loading_slip_invoice->dealer_id; $ledger->transaction_date = date('Y-m-d'); $ledger->particular = "Purchase of " . $loading_slip_invoice->product . " from " . getModelById('Dealer', $loading_slip_invoice->dealer_id)->name . "(" . getModelById('Dealer', $loading_slip_invoice->dealer_id)->address1 . ")"; $ledger->credit = 0; $ledger->debit = $loading_slip_invoice->total; $ledger->balance = $loading_slip_invoice->total; $ledger->against = $loading_slip_invoice->invoice_number; $ledger->save(); } /* Secondary Discount Party Ledger */ if ($request->secondary_discount[$i] != "" && $request->secondary_discount[$i] != 0) { $Secondaryledger = \App\SecondaryDiscountPartyLedger::where('retailer_id', $loading_slip_invoice->retailer_id)->where('dealer_id', $loading_slip_invoice->dealer_id)->orderBy('id', 'desc')->first(); $discount_total = $request->secondary_discount[$i] * $request->quantity[$i]; if (!is_null($Secondaryledger)) { $discount_total_balance = $Secondaryledger->balance + $discount_total; } else { $discount_total_balance = $discount_total; } $sledger = new \App\SecondaryDiscountPartyLedger(); $sledger->retailer_id = $loading_slip_invoice->retailer_id; $sledger->dealer_id = $loading_slip_invoice->dealer_id; $sledger->transaction_date = date('Y-m-d'); $sledger->particular = "Purchase of " . $loading_slip_invoice->product . " from " . getModelById('Dealer', $loading_slip_invoice->dealer_id)->name . "(" . getModelById('Dealer', $loading_slip_invoice->dealer_id)->address1 . ")"; $sledger->credit = 0; $sledger->debit = $discount_total; $sledger->balance = 0; $sledger->against = $loading_slip_invoice->invoice_number; $sledger->save(); } } else { echo "something went wrong"; exit; } $i++; } return redirect('/user/loading-slip-invoices')->with("success", "invoices $invoice_numbers are generated."); } else { echo "something went wrong"; exit; } } } public function getCreditNote(Request $request) { $data = array(); $data['acting_company'] = Session::get('acting_company'); $data['dealers'] = \App\Dealer::where('is_active', 1)->whereIn('id', [1, 30, 31])->get(); $data['retailers'] = \App\Retailer::where('is_active', 1)->get(); if ($request->isMethod('post')) { $validator = \Validator::make( $request->all(), array( 'dealer_id' => 'required', 'retailer_id' => 'required' ) ); if ($validator->fails()) { return redirect('user/credit-note') ->withErrors($validator) ->withInput(); } else { $data['dealer_id'] = $request->dealer_id; $data['retailer_id'] = $request->retailer_id; $total_debit = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->sum('debit'); $total_credit = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->sum('credit'); $balance = $total_debit - $total_credit; // dd($balance); if ($balance < 0) { $data['party_balance'] = $balance; } else { $data['party_balance'] = 0; } $data['retailerInfo'] = \App\Retailer::where('is_active', 1)->where('id', $request->retailer_id)->first(); $data['bank_accounts'] = \App\BankAccount::where('is_active', 1)->with('bank')->get(); $data['invoices'] = $invoice = \App\LoadingSlipInvoice::where('dealer_id', $request->dealer_id)->where('retailer_id', $request->retailer_id)->orderby('invoice_date', 'desc')->get(); } } return view('dashboard.invoice.credit-note', $data); } public function getInvoiceDetails(Request $request) { $data = array(); $data['credit_invoice'] = $invoice_data = \App\LoadingSlipInvoice::find($request->invoice_id); $data['total_returned_qty'] = DB::table('credit_note_history')->where('loading_slip_invoice_id', $request->invoice_id)->where('dealer_id', $invoice_data->dealer_id)->where('retailer_id', $invoice_data->retailer_id)->sum('return_qty'); $data['credit_retailer'] = \App\Retailer::where('is_active', 1)->where('id', $invoice_data->retailer_id)->first(); $data['credit_dealer'] = \App\Dealer::where('is_active', 1)->where('id', $invoice_data->dealer_id)->first(); $data['credit_company'] = \App\Company::where('is_active', 1)->where('id', $invoice_data->company_id)->first(); $data['sessions'] = \App\Session::where('is_active', 1)->get(); return response()->json($data); } public function saveCreditNote(Request $request) { $response = array(); $is_valid = false; $validator = \Validator::make( $request->all(), array( 'return_quantity' => 'required', 'session' => 'required' ) ); if ($validator->fails()) { return response()->json(['error' => $validator->errors()]); } else { $ledger = new \App\PartyInvoiceLedger(); $ledger->retailer_id = $request->retailer_id; $ledger->dealer_id = $request->dealer_id; $ledger->credit = $request->credit_note_amount; $ledger->type = 'credit'; $ledger->debit = 0; if ($ledger->save()) { $loading_slip_invoice = \App\LoadingSlipInvoice::find($request->invoice_id); if ($loading_slip_invoice == null) { $loading_slip_invoice->is_cn = 1; $loading_slip_invoice->cn_qty = $request->return_quantity; } else { $loading_slip_invoice->is_cn = 1; $loading_slip_invoice->cn_qty = $loading_slip_invoice->cn_qty + $request->return_quantity; } $loading_slip_invoice->save(); $credit_history = DB::table('credit_note_history')->where('dealer_id', $request->dealer_id)->where('session', $request->session)->orderBy('id', 'desc')->first(); if ($credit_history != null) { $series = $credit_history->series + 1; } else { $series = 1; } $cnLastId = DB::table('credit_note_history')->insertGetId([ 'dealer_id' => $request->dealer_id, 'retailer_id' => $request->retailer_id, 'loading_slip_invoice_id' => $request->invoice_id, 'return_qty' => $request->return_quantity, 'qty' => $request->total_quantity, 'credit_note_amount' => $request->credit_note_amount, 'date' => date('Y-m-d'), 'series' => $series, 'session' => $request->session, 'user_id' => \Auth::user()->id ]); $ledger->cn_against = $cnLastId; $ledger->particular = 'credit note series-' . $series; $ledger->save(); $is_valid = true; $response['success'] = true; $response['msg'] = 'Credit Note Added Successfully'; } else { $is_valid = false; } } if ($is_valid == true) { return response()->json($response); } else { $response['success'] = false; $response['msg'] = 'Something went wrong.'; return response()->json($response); } } public function creditNoteInvoices(Request $request) { $data = array(); $data['dealers'] = \App\Dealer::where('is_active', 1)->get(); $data['retailer'] = \App\Retailer::where('is_active', 1)->get(); if ($request->isMethod('post')) { $query = \App\LoadingSlipInvoice::query(); $query = $query->join('credit_note_history', 'loading_slip_invoices.id', 'credit_note_history.loading_slip_invoice_id'); if ($request->dealer_id != "") { $query = $query->where('credit_note_history.dealer_id', $request->dealer_id); $date['dealer_id'] = $request->dealer_id; } if ($request->retailer_id != "") { $query = $query->where('credit_note_history.retailer_id', $request->retailer_id); $date['retailer_id'] = $request->retailer_id; } $query = $query->select('credit_note_history.*', 'loading_slip_invoices.invoice_number', 'loading_slip_invoices.invoice_date', 'loading_slip_invoices.rate')->orderBy('credit_note_history.id', 'desc')->paginate(100); $data['credit_note_invoices'] = $query; } else { $data['credit_note_invoices'] = \App\LoadingSlipInvoice::join('credit_note_history', 'loading_slip_invoices.id', 'credit_note_history.loading_slip_invoice_id')->select('credit_note_history.*', 'loading_slip_invoices.invoice_number', 'loading_slip_invoices.invoice_date', 'loading_slip_invoices.rate', 'loading_slip_invoices.company_id')->orderBy('credit_note_history.id', 'desc')->paginate(100); } // dd($data); return view('dashboard.invoice.credit-note-invoices', $data); } public function creditNoteInvoiceDetails($id) { $data = array(); $credit_note_invoice = \App\LoadingSlipInvoice::join('credit_note_history', 'credit_note_history.loading_slip_invoice_id', 'loading_slip_invoices.id') ->select( 'loading_slip_invoices.loading_slip_id', 'loading_slip_invoices.invoice_number', 'loading_slip_invoices.company_id', 'loading_slip_invoices.dealer_id', 'loading_slip_invoices.retailer_id', 'loading_slip_invoices.product_id', 'loading_slip_invoices.product_hsn', 'loading_slip_invoices.unit', 'loading_slip_invoices.rate', // 'loading_slip_invoices.invoice_remark', // 'loading_slip_invoices.dispatched_through', 'loading_slip_invoices.destination', 'credit_note_history.terms_of_delivery', 'credit_note_history.dispatched_through', 'credit_note_history.remarks', 'credit_note_history.loading_slip_invoice_id', 'credit_note_history.session', 'credit_note_history.series', 'credit_note_history.return_qty', 'credit_note_history.credit_note_amount', 'credit_note_history.date' ) ->where('credit_note_history.id', $id)->first(); // dd($credit_note_invoice); $product_id = \App\ProductLoading::where('id', $credit_note_invoice->loading_slip_invoice_id)->first('product_id'); $product_details = \App\Product::where('id', $product_id->product_id)->first(); // $cgst_amount = $last_invoice->cgst; // $sgst_amount = $last_invoice->sgst; if (is_null($credit_note_invoice)) { $next_number = 1; } else { $next_number = $credit_note_invoice->id + 1; } $data['invoice_data'] = array( "company_id" => $credit_note_invoice->company_id, // "invoice_type_id" => $credit_note_invoice->invoice_type_id, "credit_note_no" => $credit_note_invoice->series, "invoice_number" => $credit_note_invoice->invoice_number, // "invoice_date" => $credit_note_invoice->invoice_date, "credit_note_date" => $credit_note_invoice->date, // "eway_bill_no" => $credit_note_invoice->eway_bill_no, "retailer_id" => $credit_note_invoice->retailer_id, // "remarks" => $credit_note_invoice->remarks, "dispatched_through" => $credit_note_invoice->dispatched_through, "destination" => $credit_note_invoice->destination, "terms_of_delivery" => $credit_note_invoice->terms_of_delivery, ); // dd($data['invoice_data']); $products = array(); array_push($products, $product_details->id); $quantity = array(); array_push($quantity, $credit_note_invoice->return_qty); $rate = array(); array_push($rate, $credit_note_invoice->rate); // $freight_discount = array(); // array_push($freight_discount, $credit_note_invoice->freight_discount); $data['product_id'] = $products; $data['quantity'] = $quantity; $data['product_rate'] = $rate; // $data['freight_discount'] = $freight_discount; $data['product_unit'] = $credit_note_invoice->unit; // $data['tcs'] = $credit_note_invoice->tcs; $data['company'] = \App\Company::where('id', $credit_note_invoice->company_id)->first(); return view('dashboard.invoice.credit-note-invoice-details', $data); } public function getLoadingSlipInvoicePayment(Request $request) { $data = array(); $data['acting_company'] = Session::get('acting_company'); $data['dealers'] = \App\Dealer::where('is_active', 1)->whereIn('id', [1, 30, 31, 114, 84])->get(); $data['retailers'] = \App\Retailer::where('is_active', 1)->get(); if ($request->isMethod('post')) { $validator = \Validator::make( $request->all(), array( 'dealer_id' => 'required', 'retailer_id' => 'required' ) ); if ($validator->fails()) { return redirect('user/loading-slip-invoice-payment') ->withErrors($validator) ->withInput(); } else { $data['dealer_id'] = $request->dealer_id; $data['retailer_id'] = $request->retailer_id; // $last_transaction_ledger = \App\PartyInvoiceLedger::where('retailer_id',$request->retailer_id)->where('dealer_id',$request->dealer_id)->orderBy('id','desc')->first(); // if($last_transaction_ledger != null){ // $data['party_balance'] = $last_transaction_ledger->balance; $total_debit = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->sum('debit'); $total_credit = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->sum('credit'); $balance = $total_debit - $total_credit; // dd($balance); if ($balance < 0) { $data['party_balance'] = $balance; } else { $data['party_balance'] = 0; } $data['retailerInfo'] = \App\Retailer::where('is_active', 1)->where('id', $request->retailer_id)->first(); $data['bank_accounts'] = \App\BankAccount::where('is_active', 1)->with('bank')->get(); $data['invoices'] = \App\LoadingSlipInvoice::where('remaining_amount', '>', 0) ->where('dealer_id', $request->dealer_id)->where('retailer_id', $request->retailer_id) ->get(); } } return view('dashboard.invoice.loading-slip-invoice-payment', $data); } public function getCompanyDiPayment(Request $request) { $data = array(); $data['acting_company'] = Session::get('acting_company'); $data['bank_accounts'] = \App\BankAccount::where('is_active', 1)->with('bank')->get(); $data['product_companies'] = \App\ProductCompany::where('is_active', 1)->get(); $data['dealers'] = \App\Dealer::where('is_active', 1)->whereIn('id', [1, 30, 31])->get(); $data['dealers1'] = \App\Dealer::where('is_active', 1)->get(); if ($request->isMethod('post')) { if ($request->transfer_type == 1) { $validator = \Validator::make( $request->all(), array( 'product_company_id' => 'required', 'dealer_id' => 'required' ) ); } if ($request->transfer_type == 2) { $validator = \Validator::make( $request->all(), array( 'from_dealer' => 'required', 'dealer_id' => 'required' ) ); } if ($validator->fails()) { return redirect('user/comany-di-payment') ->withErrors($validator) ->withInput(); } else { $product_company_id = $request->product_company_id; $from_dealer_id = $request->from_dealer; $dealer_id = $request->dealer_id; //code to get advanced amount . added by alok maurya if ($request->transfer_type == 1) { $total_debit = \App\CompanyInvoiceLedger::where('dealer_id', $request->dealer_id)->where('product_company_id', $request->product_company_id)->sum('debit'); $total_credit = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id)->where('dealer_id', $request->dealer_id)->sum('credit'); } if ($request->transfer_type == 2) { $total_debit = \App\CompanyInvoiceLedger::where('dealer_id', $request->dealer_id)->where('from_dealer_id', $from_dealer_id)->sum('debit'); $total_credit = \App\CompanyInvoiceLedger::where('from_dealer_id', $from_dealer_id)->where('dealer_id', $request->dealer_id)->sum('credit'); } $balance = 0; if ($total_debit >= $total_credit) { $balance = $total_debit - $total_credit; } if ($balance > 0) { $data['advanced_payment'] = $balance; } else { $data['advanced_payment'] = 0; } // end of advanced amount code $invoices = []; if ($request->transfer_type == 1) { $company_dis = \DB::table('company_dis')->where('product_company_id', $product_company_id)->where('dealer_id', $dealer_id)->where('remaining_amount', '>', 0)->whereNotNull('invoice_number')->get(); $warehouse_dis = \DB::table('warehouse_dis')->where('dealer_id', $dealer_id)->where('product_company_id', $product_company_id)->where('remaining_amount', '>', 0)->whereNotNull('invoice_number')->get(); } if ($request->transfer_type == 2) { $company_dis = null; $warehouse_dis = \DB::table('warehouse_dis')->where('dealer_id', $dealer_id)->where('from_dealer_id', $from_dealer_id)->where('remaining_amount', '>', 0)->whereNotNull('invoice_number')->get(); } if ($company_dis != null && $warehouse_dis != null) { $n = 0; foreach ($company_dis as $key => $company_di) { $company_di->invoice_type = 'company_dis'; $invoices[$n] = $company_di; $n++; } foreach ($warehouse_dis as $key1 => $warehouse_di) { $warehouse_di->invoice_type = 'warehouse_dis'; $invoices[$n] = $warehouse_di; $n++; } } else if ($company_dis == null && $warehouse_dis != null) { $n = 0; foreach ($warehouse_dis as $key1 => $warehouse_di) { $warehouse_di->invoice_type = 'warehouse_dis'; $invoices[$n] = $warehouse_di; $n++; } } else if ($company_dis != null && $warehouse_dis == null) { $n = 0; foreach ($company_dis as $key => $company_di) { $company_di->invoice_type = 'company_dis'; $invoices[$n] = $company_di; $n++; } } $data['product_company_id'] = $request->product_company_id; $data['from_dealer_id'] = $from_dealer_id; $data['transfer_type'] = $request->transfer_type; $data['dealer_id'] = $request->dealer_id; $data['invoices'] = $invoices; } } return view('dashboard.invoice.company-di-payment', $data); } // Function to generate new invoice public function generateManualInvoice() { $data['acting_company'] = Session::get('acting_company'); $data['invoice_types'] = \App\InvoiceType::where('is_active', 1)->get(); $data['companies'] = \App\Company::where('is_active', 1)->where('for_invoice', 1)->get(); $data['retailers'] = \App\Retailer::where('is_active', 1)->get(); $data['products'] = \App\Product::where('is_active', 1)->get(); $data['units'] = \App\Unit::where('is_active', 1)->get(); return view('dashboard.invoice.generate-manual-invoice', $data); } // Function to show the invoice details to print in a specified format public function printTaxInvoice(Request $request) { $data = array(); $validator = \Validator::make( $request->all(), array( "invoice_type_id" => "required", "company_id" => "required", "invoice_date" => "required", "eway_bill_no" => "required", "retailer_id" => "required", ) ); if ($validator->fails()) { return redirect('user/generate-manual-invoice') ->withErrors($validator) ->withInput(); } else { $data['invoice_data'] = array( "company_id" => $request->company_id, "invoice_type_id" => $request->invoice_type_id, "invoice_date" => $this->dateConverter($request->invoice_date), "eway_bill_no" => $request->eway_bill_no, "retailer_id" => $request->retailer_id, "invoice_remark" => $request->invoice_remark, "dispatched_through" => $request->dispatched_through, "destination" => $request->destination, "terms_of_delivery" => $request->terms_of_delivery, ); $data['product_id'] = $request->product_id; $data['product_hsn'] = $request->product_hsn; $data['quantity'] = $request->quantity; $data['product_rate'] = $request->product_rate; $data['product_unit'] = $request->product_unit; $data['product_amount'] = $request->product_amount; $acting_company = Session::get('acting_company'); $data['company'] = \App\Company::where('id', $request->company_id)->first(); return view('dashboard.invoice.print-tax-invoice', $data); // dd($data); } } // Function to save generated invoice public function postGeneratedInvoice(Request $request) { } // public function dateConverter($date) { $temp_date = explode('/', $date); $new_date = $temp_date[2] . "-" . $temp_date[1] . "-" . $temp_date[0]; return $new_date; } public function loadingSlipInvoiceDetails($id) { $data = array(); $last_invoice = \App\LoadingSlipInvoice::where('id', $id)->first(); // dd($last_invoice); $product_id = \App\ProductLoading::where('id', $last_invoice->loading_slip_id)->first('product_id'); $product_details = \App\Product::where('id', $product_id->product_id)->first(); $cgst_amount = $last_invoice->cgst; $sgst_amount = $last_invoice->sgst; if (is_null($last_invoice)) { $next_number = 1; } else { $next_number = $last_invoice->id + 1; } $data['invoice_data'] = array( "company_id" => $last_invoice->company_id, "invoice_type_id" => $last_invoice->invoice_type_id, "invoice_number" => $last_invoice->invoice_number, "invoice_date" => $last_invoice->invoice_date, "eway_bill_number" => $last_invoice->eway_bill_number, "retailer_id" => $last_invoice->retailer_id, "irn_no" => $last_invoice->irn_no, "AckNo" => $last_invoice->AckNo, "igst" => $last_invoice->igst, "AckDt" => $last_invoice->AckDt, "eway_bill_date" => $last_invoice->eway_bill_date, "eway_bill_ValidTill" => $last_invoice->eway_bill_ValidTill, "qr_code" => $last_invoice->qr_code, "invoice_remark" => $last_invoice->invoice_remark, "dispatched_through" => $last_invoice->dispatched_through, "destination" => $last_invoice->destination, "terms_of_delivery" => $last_invoice->terms_of_delivery, ); $products = array(); array_push($products, $product_details->id); $quantity = array(); array_push($quantity, $last_invoice->quantity); $rate = array(); array_push($rate, $last_invoice->rate); $freight_discount = array(); array_push($freight_discount, $last_invoice->freight_discount); $data['product_id'] = $products; $data['quantity'] = $quantity; $data['product_rate'] = $rate; $data['freight_discount'] = $freight_discount; $data['product_unit'] = $last_invoice->unit; $data['tcs'] = $last_invoice->tcs; $data['company'] = \App\Company::where('id', $last_invoice->company_id)->first(); return view('dashboard.invoice.loading-slip-invoice-details', $data); } public function exportAsXml($id) { $data = array(); $invoice = \App\LoadingSlipInvoice::where('id', $id)->with('company', 'retailer', 'product_details')->first(); if (!is_null($invoice)) { $content = \View::make('invoice')->with('invoice', $invoice); $xml = $content; $response = Response::create($xml, 200); $response->header('Content-Type', 'text/xml'); $response->header('Cache-Control', 'public'); $response->header('Content-Description', 'File Transfer'); $response->header('Content-Disposition', 'attachment; filename=' . $invoice->invoice_number . '.xml'); $response->header('Content-Transfer-Encoding', 'binary'); return $response; } else { echo "invalid invoice"; } } public function partyInvoiceLedger(Request $request) { $data = array(); $retailers = array(); $data['retailers'] = \App\Retailer::where('is_active', 1)->get(); $data['dealers'] = \App\Dealer::where('is_active', 1)->whereIn('id', [1, 30, 31, 114, 84])->get(); $data['bank_accounts'] = \App\BankAccount::where('is_active', 1)->with('bank')->get(); $opening_balance = 0; if ($request->isMethod('post')) { $data['against_list'] = []; // $lists = \App\PartyInvoiceLedger::join('bank_statements','bank_statements.series','party_invoice_ledgers.against')->where(['party_invoice_ledgers.retailer_id'=>$request->retailer_id,'party_invoice_ledgers.dealer_id'=>$request->dealer_id,'party_invoice_ledgers.type'=>'credit'])->where('party_invoice_ledgers.against','!=','')->where('bank_statements.is_refund','!=',1)->where('bank_statements.type','receipt')->get(); $lists = \App\PartyInvoiceLedger::join('bank_statements', 'bank_statements.series', 'party_invoice_ledgers.against')->where(['party_invoice_ledgers.retailer_id' => $request->retailer_id, 'party_invoice_ledgers.dealer_id' => $request->dealer_id, 'party_invoice_ledgers.type' => 'Receipt'])->where('party_invoice_ledgers.against', '!=', '')->where('bank_statements.type', 'receipt')->select('bank_statements.series', 'bank_statements.amount', 'bank_statements.is_refund', 'bank_statements.refund_series', 'bank_statements.history', 'party_invoice_ledgers.refund_against')->get(); $totalAmount = 0; $n = 0; // dd($lists); $updateList = []; if ($lists != '') { foreach ($lists as $key => $value) { // dd($value->amount); $refund_against = json_decode($value->refund_against, true); if ($refund_against != '') { $totalRefundAmout = 0; foreach ($refund_against as $v) { // dd($v); $amount = \App\BankStatement::where(['series' => (int)$v, 'type' => 'payment'])->first(); $totalRefundAmout += $amount->amount; } // echo $totalRefundAmout.'<br>'; if ($value->amount != $totalRefundAmout) { $updateList[$n]['credit'] = $value->amount; $updateList[$n]['against'] = $value->series; $updateList[$n]['response'] = 'hello'; } } else { $updateList[$n]['credit'] = $value->amount; $updateList[$n]['against'] = $value->series; $updateList[$n]['response'] = 'he'; } // dd($refund_against); // dd(); // dd(); // dd((double)$totalAmount); # code... $n++; } // exit; // dd($updateList); } // dd($updateList); $data['against_list'] = $updateList; $validator = \Validator::make( $request->all(), array( 'retailer_id' => 'required', 'dealer_id' => 'required', ) ); if ($validator->fails()) { return redirect('user/party-invoice-ledger') ->withErrors($validator) ->withInput(); } else { if ($request->from_date != '' && $request->to_date != '') { $open_date = date('Y-m-d', strtotime('-1 day', strtotime($request->from_date))); $ledgerOpening = \DB::select('select `retailer_id`, `dealer_id`, `particular`, `type`, `credit`, `debit`, `against`, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `dealer_id` = ' . $request->dealer_id . ' and date(`created_at`)>= "' . $request->from_date . '" and date(`created_at`)<= "' . $request->to_date . '" and `particular`="opening balance" order by `created_at` asc'); $ledgerWithSeries = \DB::select('select `party_invoice_ledgers`.`retailer_id`, `party_invoice_ledgers`.`dealer_id`, `party_invoice_ledgers`.`particular`, `party_invoice_ledgers`.`type`, `party_invoice_ledgers`.`credit`, `party_invoice_ledgers`.`debit`, `party_invoice_ledgers`.`against`, `bank_statements`.`transaction_date` from `party_invoice_ledgers` join `bank_statements` on `bank_statements`.`series` = `party_invoice_ledgers`.`against` and bank_statements.type=LOWER(party_invoice_ledgers.type) where `party_invoice_ledgers`.`retailer_id` = ' . $request->retailer_id . ' and `party_invoice_ledgers`.`dealer_id` = ' . $request->dealer_id . ' and date(`bank_statements`.`transaction_date`)>= "' . $request->from_date . '" and date(`bank_statements`.`transaction_date`)<= "' . $request->to_date . '" order by `bank_statements`.`transaction_date` asc'); $ledgerWithSeriesTotal = \DB::select('select sum(party_invoice_ledgers.debit) as Debit ,sum(party_invoice_ledgers.credit) as Credit from `party_invoice_ledgers` join `bank_statements` on `bank_statements`.`series` = `party_invoice_ledgers`.`against` and bank_statements.type=LOWER(party_invoice_ledgers.type) where `party_invoice_ledgers`.`retailer_id` = ' . $request->retailer_id . ' and `party_invoice_ledgers`.`dealer_id` = ' . $request->dealer_id . ' and date(`bank_statements`.`transaction_date`)<= "' . $open_date . '" order by `bank_statements`.`transaction_date` asc'); // dd($ledgerWithSeriesTotal[0]->Credit); $ledgerInvoice = \DB::select('select `retailer_id`, `dealer_id`, `particular`, `type`, `credit`, `debit`, `against`, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `dealer_id` = ' . $request->dealer_id . ' and date(`created_at`)>= "' . $request->from_date . '" and date(`created_at`)<= "' . $request->to_date . '" and `type` is null order by `created_at` asc'); $ledgerInvoiceTotal = \DB::select('select sum(debit) as Debit ,sum(credit) as Credit from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `dealer_id` = ' . $request->dealer_id . ' and date(`created_at`)<= "' . $open_date . '" and `type` is null order by `created_at` asc'); // dd($ledgerInvoice);git $generalEntry = \DB::select('select `retailer_id`, `dealer_id`, `particular`, `type`, `credit`, `debit`, journal_series as against, `transaction_date` from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `dealer_id` = ' . $request->dealer_id . ' and `type` = "Journal Entry" and date(`transaction_date`)>= "' . $request->from_date . '" and date(`transaction_date`)<= "' . $request->to_date . '" order by `transaction_date` asc'); $generalEntryTotal = \DB::select('select sum(debit) as Debit ,sum(credit) as Credit from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `dealer_id` = ' . $request->dealer_id . ' and `type` = "Journal Entry" and date(`transaction_date`)<= "' . $open_date . '" order by `transaction_date` asc'); $CnEntry = \DB::select('select `retailer_id`, `dealer_id`, `particular`, `type`, `credit`, `debit`, cn_against as against, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `dealer_id` = ' . $request->dealer_id . ' and date(`created_at`)>= "' . $request->from_date . '" and date(`created_at`)<= "' . $request->to_date . '" and cn_against!="" order by `created_at` asc'); $CnEntryTotal = \DB::select('select sum(debit) as Debit ,sum(credit) as Credit from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `dealer_id` = ' . $request->dealer_id . ' and date(`created_at`)<= "' . $open_date . '" and cn_against!="" order by `created_at` asc'); $data['ledgers'] = array_merge(array_merge($ledgerWithSeries, $ledgerInvoice, $CnEntry), $generalEntry); array_multisort(array_column($data['ledgers'], 'transaction_date'), SORT_ASC, $data['ledgers']); $total_credit = $ledgerWithSeriesTotal[0]->Credit + $ledgerInvoiceTotal[0]->Credit + $generalEntryTotal[0]->Credit + $CnEntryTotal[0]->Credit; $total_debit = $ledgerWithSeriesTotal[0]->Debit + $ledgerInvoiceTotal[0]->Debit + $generalEntryTotal[0]->Debit + $CnEntryTotal[0]->Debit; $opening_balance = $total_debit - $total_credit; // dd($opening_balance); } else { $ledgerOpening = \DB::select('select `retailer_id`, `dealer_id`, `particular`, `type`, `credit`, `debit`, `against`, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `dealer_id` = ' . $request->dealer_id . ' and `particular`="opening balance" order by `created_at` asc'); $ledgerWithSeries = \DB::select('select `party_invoice_ledgers`.`retailer_id`, `party_invoice_ledgers`.`dealer_id`, `party_invoice_ledgers`.`particular`, `party_invoice_ledgers`.`type`, `party_invoice_ledgers`.`credit`, `party_invoice_ledgers`.`debit`, `party_invoice_ledgers`.`against`, `bank_statements`.`transaction_date` from `party_invoice_ledgers` join `bank_statements` on `bank_statements`.`series` = `party_invoice_ledgers`.`against` and bank_statements.type=LOWER(party_invoice_ledgers.type) where `party_invoice_ledgers`.`retailer_id` = ' . $request->retailer_id . ' and `party_invoice_ledgers`.`dealer_id` = ' . $request->dealer_id . ' order by `bank_statements`.`transaction_date` asc'); $ledgerInvoice = \DB::select('select `retailer_id`, `dealer_id`, `particular`, `type`, `credit`, `debit`, `against`, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `dealer_id` = ' . $request->dealer_id . ' and `type` is null order by `created_at` asc'); $generalEntry = \DB::select('select `retailer_id`, `dealer_id`, `particular`, `type`, `credit`, `debit`, journal_series as against, `transaction_date` from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `dealer_id` = ' . $request->dealer_id . ' and `type` = "Journal Entry" order by `transaction_date` asc'); $CnEntry = \DB::select('select `retailer_id`, `dealer_id`, `particular`, `type`, `credit`, `debit`, cn_against as against, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `dealer_id` = ' . $request->dealer_id . ' and cn_against!="" order by `created_at` asc'); $data['ledgers'] = array_merge(array_merge($ledgerWithSeries, $ledgerInvoice, $ledgerOpening, $CnEntry), $generalEntry); array_multisort(array_column($data['ledgers'], 'transaction_date'), SORT_ASC, $data['ledgers']); // dd($data['ledgers']); // dd(); $opening_balance = 0; } } $data['retailer_id'] = $request->retailer_id; $data['dealer_id'] = $request->dealer_id; $data['from_date'] = $request->from_date; $data['to_date'] = $request->to_date; $data['opening_balance'] = $opening_balance; } return view('dashboard.invoice.party-invoice-ledger', $data); } public function partyInvoiceLedger1(Request $request) { $data = array(); $retailers = array(); $data['retailers'] = \App\Retailer::where('is_active', 1)->get(); $data['dealers'] = \App\Dealer::where('is_active', 1)->whereIn('id', [1, 30, 31])->get(); $data['bank_accounts'] = \App\BankAccount::where('is_active', 1)->with('bank')->get(); $opening_balance = 0; if ($request->isMethod('post')) { $data['against_list'] = []; $lists = \App\PartyInvoiceLedger::join('bank_statements', 'bank_statements.series', 'party_invoice_ledgers.against')->where(['party_invoice_ledgers.retailer_id' => $request->retailer_id, 'party_invoice_ledgers.dealer_id' => $request->dealer_id, 'party_invoice_ledgers.type' => 'Receipt'])->where('party_invoice_ledgers.against', '!=', '')->where('bank_statements.type', 'receipt')->select('bank_statements.series', 'bank_statements.amount', 'bank_statements.is_refund', 'bank_statements.refund_series', 'bank_statements.history', 'party_invoice_ledgers.refund_against')->get(); // dd($lists); $totalAmount = 0; $n = 0; $updateList = []; if ($lists != '') { foreach ($lists as $key => $value) { $refund_against = json_decode($value->refund_against, true); if ($refund_against != '') { $totalRefundAmout = 0; foreach ($refund_against as $v) { $amount = \App\BankStatement::where(['series' => (int)$v, 'type' => 'payment'])->first(); $totalRefundAmout += $amount->amount; } if ($value->amount != $totalRefundAmout) { $updateList[$n]['credit'] = $value->amount; $updateList[$n]['against'] = $value->series; $updateList[$n]['response'] = 'hello'; } } else { $updateList[$n]['credit'] = $value->amount; $updateList[$n]['against'] = $value->series; $updateList[$n]['response'] = 'he'; } $n++; } } $data['against_list'] = $updateList; $validator = \Validator::make( $request->all(), array( 'retailer_id' => 'required', 'dealer_id' => 'required', ) ); if ($validator->fails()) { return redirect('user/party-invoice-ledger') ->withErrors($validator) ->withInput(); } else { if (isset($request->credit_note)) { if ($request->from_date != '' && $request->to_date != '') { $data['ledgers'] = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->where('cn_against', '!=', '')->whereDate('created_at', '>=', $request->from_date)->whereDate('created_at', '<=', $request->to_date)->orderBy('id', 'asc')->get(); $open_date = date('Y-m-d', strtotime('-1 day', strtotime($request->from_date))); $total_debit = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->where('cn_against', '!=', '')->whereDate('created_at', '<=', $open_date)->sum('debit'); $total_credit = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->where('cn_against', '!=', '')->whereDate('created_at', '<=', $open_date)->sum('credit'); $opening_balance = $total_debit - $total_credit; } else { $data['ledgers'] = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->where('cn_against', '!=', '')->orderBy('id', 'asc')->get(); $opening_balance = 0; } } else { if ($request->from_date != '' && $request->to_date != '') { $data['ledgers'] = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->whereDate('created_at', '>=', $request->from_date)->whereDate('created_at', '<=', $request->to_date)->orderBy('id', 'asc')->get(); $open_date = date('Y-m-d', strtotime('-1 day', strtotime($request->from_date))); $total_debit = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->whereDate('created_at', '<=', $open_date)->sum('debit'); $total_credit = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->whereDate('created_at', '<=', $open_date)->sum('credit'); $opening_balance = $total_debit - $total_credit; } else { $data['ledgers'] = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->orderBy('id', 'asc')->get(); $opening_balance = 0; } } } $data['retailer_id'] = $request->retailer_id; $data['dealer_id'] = $request->dealer_id; $data['from_date'] = $request->from_date; $data['to_date'] = $request->to_date; $data['opening_balance'] = $opening_balance; } return view('dashboard.invoice.party-invoice-ledger', $data); } public function partyLedger(Request $request) { // dd($request->all()); $data = array(); $retailers = array(); $opening_balance = 0; $data['retailers'] = \App\Retailer::where('is_active', 1)->get(); $data['groups'] = \App\Group::where('is_active', 1)->get(); if ($request->isMethod('post')) { $validator = \Validator::make( $request->all(), array( // 'retailer_id' => 'required', "retailer_id" => ["required_without:group_id", new OneOf($request, ["retailer_id", "group_id"])], "group_id" => ["required_without:retailer_id", new OneOf($request, ["retailer_id", "group_id"])], ) ); if ($validator->fails()) { return redirect('user/party-ledger') ->withErrors($validator) ->withInput(); } else { if ($request->retailer_id != '' && $request->to_date != '' && $request->from_date != '') { $to_date = implode('-', array_reverse(explode('/', $request->to_date))); $from_date = implode('-', array_reverse(explode('/', $request->from_date))); $open_date = date('Y-m-d', strtotime('-1 day', strtotime($request->from_date))); $ledgerOpening = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, `against`, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and date(`created_at`)>= "' . $from_date . '" and date(`created_at`)<= "' . $to_date . '" and `particular`="opening balance" order by `created_at` asc'); $ledgerWithSeries = \DB::select('select `party_invoice_ledgers`.`retailer_id`,`party_invoice_ledgers`.`particular`, `party_invoice_ledgers`.`type`, `party_invoice_ledgers`.`credit`, `party_invoice_ledgers`.`debit`, `party_invoice_ledgers`.`against`, `bank_statements`.`transaction_date` from `party_invoice_ledgers` join `bank_statements` on `bank_statements`.`series` = `party_invoice_ledgers`.`against` and bank_statements.type=LOWER(party_invoice_ledgers.type) where `party_invoice_ledgers`.`retailer_id` = ' . $request->retailer_id . ' and date(`bank_statements`.`transaction_date`)>= "' . $from_date . '" and date(`bank_statements`.`transaction_date`)<= "' . $to_date . '" order by `bank_statements`.`transaction_date` asc'); $ledgerWithSeriesTotal = \DB::select('select sum(party_invoice_ledgers.debit) as Debit ,sum(party_invoice_ledgers.credit) as Credit from `party_invoice_ledgers` join `bank_statements` on `bank_statements`.`series` = `party_invoice_ledgers`.`against` and bank_statements.type=LOWER(party_invoice_ledgers.type) where `party_invoice_ledgers`.`retailer_id` = ' . $request->retailer_id . ' and date(`bank_statements`.`transaction_date`)<= "' . $open_date . '" order by `bank_statements`.`transaction_date` asc'); $ledgerInvoice = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, `against`, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and date(`created_at`)>= "' . $from_date . '" and date(`created_at`)<= "' . $to_date . '" and `type` is null order by `created_at` asc'); $ledgerInvoiceTotal = \DB::select('select sum(debit) as Debit ,sum(credit) as Credit from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and date(`created_at`)<= "' . $open_date . '" and `type` is null order by `created_at` asc'); $generalEntry = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, journal_series as against, `transaction_date` from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `type` = "Journal Entry" and date(`transaction_date`)>= "' . $from_date . '" and date(`transaction_date`)<= "' . $to_date . '" order by `transaction_date` asc'); $generalEntryTotal = \DB::select('select sum(debit) as Debit ,sum(credit) as Credit from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `type` = "Journal Entry" and date(`transaction_date`)<= "' . $open_date . '" order by `transaction_date` asc'); $CnEntry = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, cn_against as against, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and date(`created_at`)>= "' . $from_date . '" and date(`created_at`)<= "' . $to_date . '" and cn_against!="" order by `created_at` asc'); $CnEntryTotal = \DB::select('select sum(debit) as Debit ,sum(credit) as Credit from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and date(`created_at`)<= "' . $open_date . '" and cn_against!="" order by `created_at` asc'); $secondary_discount = DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, `against`, `transaction_date` from `secondary_discount_party_ledgers` where `retailer_id` =' . $request->retailer_id . ' and date(`transaction_date`)>= "' . $from_date . '" and date(`transaction_date`)<= "' . $to_date . '" order by `transaction_date` asc'); $data['ledgers'] = array_merge(array_merge($ledgerWithSeries, $ledgerInvoice, $CnEntry), $generalEntry, $secondary_discount); array_multisort(array_column($data['ledgers'], 'transaction_date'), SORT_ASC, $data['ledgers']); $total_credit = $ledgerWithSeriesTotal[0]->Credit + $ledgerInvoiceTotal[0]->Credit + $generalEntryTotal[0]->Credit + $CnEntryTotal[0]->Credit; $total_debit = $ledgerWithSeriesTotal[0]->Debit + $ledgerInvoiceTotal[0]->Debit + $generalEntryTotal[0]->Debit + $CnEntryTotal[0]->Debit; $opening_balance = $total_debit - $total_credit; } else if ($request->retailer_id != '') { $ledgerOpening = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, `against`, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `particular`="opening balance" order by `created_at` asc'); $ledgerWithSeries = \DB::select('select `party_invoice_ledgers`.`retailer_id`, `party_invoice_ledgers`.`particular`, `party_invoice_ledgers`.`type`, `party_invoice_ledgers`.`credit`, `party_invoice_ledgers`.`debit`, `party_invoice_ledgers`.`against`, `bank_statements`.`transaction_date` from `party_invoice_ledgers` join `bank_statements` on `bank_statements`.`series` = `party_invoice_ledgers`.`against` and bank_statements.type=LOWER(party_invoice_ledgers.type) where `party_invoice_ledgers`.`retailer_id` = ' . $request->retailer_id . ' order by `bank_statements`.`transaction_date` asc'); $ledgerInvoice = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, `against`, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `type` is null order by `created_at` asc'); $generalEntry = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, journal_series as against, `transaction_date` from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and `type` = "Journal Entry" order by `transaction_date` asc'); $CnEntry = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, cn_against as against, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` = ' . $request->retailer_id . ' and cn_against!="" order by `created_at` asc'); $secondary_discount = DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, `against`, `transaction_date` from `secondary_discount_party_ledgers` where `retailer_id` =' . $request->retailer_id . ' order by `transaction_date` asc'); $data['ledgers'] = array_merge(array_merge($ledgerWithSeries, $ledgerInvoice, $ledgerOpening, $CnEntry), $generalEntry, $secondary_discount); array_multisort(array_column($data['ledgers'], 'transaction_date'), SORT_ASC, $data['ledgers']); $opening_balance = 0; } else if ($request->group_id != '' && $request->to_date != '' && $request->from_date != '') { // && $request->from_date != '' && $request->to_date != '' $to_date = implode('-', array_reverse(explode('/', $request->to_date))); $from_date = implode('-', array_reverse(explode('/', $request->from_date))); $open_date = date('Y-m-d', strtotime('-1 day', strtotime($request->from_date))); $retailer_ids = \App\Retailer::where('is_active', 1)->where('group_id', $request->group_id)->pluck('id')->toArray(); if (count($retailer_ids) > 0) { $retailer_ids = implode(', ', $retailer_ids); $ledgerOpening = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, `against`, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` IN (' . $retailer_ids . ') and date(`created_at`)>= "' . $from_date . '" and date(`created_at`)<= "' . $to_date . '" and `particular`="opening balance" order by `created_at` asc'); $ledgerWithSeries = \DB::select('select `party_invoice_ledgers`.`retailer_id`,`party_invoice_ledgers`.`particular`, `party_invoice_ledgers`.`type`, `party_invoice_ledgers`.`credit`, `party_invoice_ledgers`.`debit`, `party_invoice_ledgers`.`against`, `bank_statements`.`transaction_date` from `party_invoice_ledgers` join `bank_statements` on `bank_statements`.`series` = `party_invoice_ledgers`.`against` and bank_statements.type=LOWER(party_invoice_ledgers.type) where `party_invoice_ledgers`.`retailer_id` IN (' . $retailer_ids . ') and date(`bank_statements`.`transaction_date`)>= "' . $from_date . '" and date(`bank_statements`.`transaction_date`)<= "' . $to_date . '" order by `bank_statements`.`transaction_date` asc'); $ledgerWithSeriesTotal = \DB::select('select sum(party_invoice_ledgers.debit) as Debit ,sum(party_invoice_ledgers.credit) as Credit from `party_invoice_ledgers` join `bank_statements` on `bank_statements`.`series` = `party_invoice_ledgers`.`against` and bank_statements.type=LOWER(party_invoice_ledgers.type) where `party_invoice_ledgers`.`retailer_id` IN (' . $retailer_ids . ') and date(`bank_statements`.`transaction_date`)<= "' . $open_date . '" order by `bank_statements`.`transaction_date` asc'); $ledgerInvoice = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, `against`, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` IN (' . $retailer_ids . ') and date(`created_at`)>= "' . $from_date . '" and date(`created_at`)<= "' . $to_date . '" and `type` is null order by `created_at` asc'); $ledgerInvoiceTotal = \DB::select('select sum(debit) as Debit ,sum(credit) as Credit from `party_invoice_ledgers` where `retailer_id` IN (' . $retailer_ids . ') and date(`created_at`)<= "' . $open_date . '" and `type` is null order by `created_at` asc'); $generalEntry = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, journal_series as against, `transaction_date` from `party_invoice_ledgers` where `retailer_id` IN (' . $retailer_ids . ') and `type` = "Journal Entry" and date(`transaction_date`)>= "' . $from_date . '" and date(`transaction_date`)<= "' . $to_date . '" order by `transaction_date` asc'); $generalEntryTotal = \DB::select('select sum(debit) as Debit ,sum(credit) as Credit from `party_invoice_ledgers` where `retailer_id` IN (' . $retailer_ids . ') and `type` = "Journal Entry" and date(`transaction_date`)<= "' . $open_date . '" order by `transaction_date` asc'); $CnEntry = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, cn_against as against, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` IN (' . $retailer_ids . ') and date(`created_at`)>= "' . $from_date . '" and date(`created_at`)<= "' . $to_date . '" and cn_against!="" order by `created_at` asc'); $CnEntryTotal = \DB::select('select sum(debit) as Debit ,sum(credit) as Credit from `party_invoice_ledgers` where `retailer_id` IN (' . $retailer_ids . ') and date(`created_at`)<= "' . $open_date . '" and cn_against!="" order by `created_at` asc'); $secondary_discount = DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, `against`, `transaction_date` from `secondary_discount_party_ledgers` where `retailer_id` IN (' . $retailer_ids . ') and date(`transaction_date`)>= "' . $from_date . '" and date(`transaction_date`)<= "' . $to_date . '" order by `transaction_date` asc'); $data['ledgers'] = array_merge(array_merge($ledgerWithSeries, $ledgerInvoice, $CnEntry), $generalEntry, $secondary_discount); array_multisort(array_column($data['ledgers'], 'transaction_date'), SORT_ASC, $data['ledgers']); $total_credit = $ledgerWithSeriesTotal[0]->Credit + $ledgerInvoiceTotal[0]->Credit + $generalEntryTotal[0]->Credit + $CnEntryTotal[0]->Credit; $total_debit = $ledgerWithSeriesTotal[0]->Debit + $ledgerInvoiceTotal[0]->Debit + $generalEntryTotal[0]->Debit + $CnEntryTotal[0]->Debit; $opening_balance = $total_debit - $total_credit; } } else if ($request->group_id != '') { $retailer_ids = \App\Retailer::where('is_active', 1)->where('group_id', $request->group_id)->pluck('id')->toArray(); if (count($retailer_ids) > 0) { $retailer_ids = implode(', ', $retailer_ids); $ledgerOpening = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, `against`, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` IN (' . $retailer_ids . ') and `particular`="opening balance" order by `created_at` asc'); $ledgerWithSeries = \DB::select('select `party_invoice_ledgers`.`retailer_id`, `party_invoice_ledgers`.`particular`, `party_invoice_ledgers`.`type`, `party_invoice_ledgers`.`credit`, `party_invoice_ledgers`.`debit`, `party_invoice_ledgers`.`against`, `bank_statements`.`transaction_date` from `party_invoice_ledgers` join `bank_statements` on `bank_statements`.`series` = `party_invoice_ledgers`.`against` and bank_statements.type=LOWER(party_invoice_ledgers.type) where `party_invoice_ledgers`.`retailer_id` IN (' . $retailer_ids . ') order by `bank_statements`.`transaction_date` asc'); $ledgerInvoice = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, `against`, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` IN (' . $retailer_ids . ') and `type` is null order by `created_at` asc'); $generalEntry = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, journal_series as against, `transaction_date` from `party_invoice_ledgers` where `retailer_id` IN (' . $retailer_ids . ') and `type` = "Journal Entry" order by `transaction_date` asc'); $CnEntry = \DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, cn_against as against, DATE_FORMAT(created_at, "%Y-%m-%d") as transaction_date from `party_invoice_ledgers` where `retailer_id` IN (' . $retailer_ids . ') and cn_against!="" order by `created_at` asc'); $secondary_discount = DB::select('select `retailer_id`, `particular`, `type`, `credit`, `debit`, `against`, `transaction_date` from `secondary_discount_party_ledgers` where `retailer_id` IN (' . $retailer_ids . ') order by `transaction_date` asc'); $data['ledgers'] = array_merge(array_merge($ledgerWithSeries, $ledgerInvoice, $ledgerOpening, $CnEntry), $generalEntry, $secondary_discount); array_multisort(array_column($data['ledgers'], 'transaction_date'), SORT_ASC, $data['ledgers']); } $opening_balance = 0; } $data['retailer_id'] = $request->retailer_id; $data['from_date'] = $request->from_date; $data['to_date'] = $request->to_date; $data['opening_balance'] = $opening_balance; } } return view('dashboard.invoice.party-ledger', $data); } public function partyLedgerOld(Request $request) { $data = array(); $retailers = array(); $data['retailers'] = \App\Retailer::where('is_active', 1)->get(); $data['groups'] = \App\Group::where('is_active', 1)->get(); // $data['dealers'] = \App\Dealer::where('is_active', 1)->whereIn('id', [1, 30, 31])->get(); $data['bank_accounts'] = \App\BankAccount::where('is_active', 1)->with('bank')->get(); $opening_balance = 0; if ($request->isMethod('post')) { $data['against_list'] = []; // $lists = \App\PartyInvoiceLedger::join('bank_statements','bank_statements.series','party_invoice_ledgers.against')->where(['party_invoice_ledgers.retailer_id'=>$request->retailer_id,'party_invoice_ledgers.dealer_id'=>$request->dealer_id,'party_invoice_ledgers.type'=>'credit'])->where('party_invoice_ledgers.against','!=','')->where('bank_statements.is_refund','!=',1)->where('bank_statements.type','receipt')->get(); $lists = \App\PartyInvoiceLedger::join('bank_statements', 'bank_statements.series', 'party_invoice_ledgers.against')->where(['party_invoice_ledgers.retailer_id' => $request->retailer_id, 'party_invoice_ledgers.dealer_id' => $request->dealer_id, 'party_invoice_ledgers.type' => 'credit'])->where('party_invoice_ledgers.against', '!=', '')->where('bank_statements.type', 'receipt')->select('bank_statements.series', 'bank_statements.amount', 'bank_statements.is_refund', 'bank_statements.refund_series', 'bank_statements.history', 'party_invoice_ledgers.refund_against')->get(); $totalAmount = 0; $n = 0; // dd($lists); $updateList = []; if ($lists != '') { foreach ($lists as $key => $value) { // dd($value->amount); $refund_against = json_decode($value->refund_against, true); if ($refund_against != '') { $totalRefundAmout = 0; foreach ($refund_against as $v) { // dd($v); $amount = \App\BankStatement::where(['series' => (int)$v, 'type' => 'payment'])->first(); $totalRefundAmout += $amount->amount; } // echo $totalRefundAmout.'<br>'; if ($value->amount != $totalRefundAmout) { $updateList[$n]['credit'] = $value->amount; $updateList[$n]['against'] = $value->series; $updateList[$n]['response'] = 'hello'; } } else { $updateList[$n]['credit'] = $value->amount; $updateList[$n]['against'] = $value->series; $updateList[$n]['response'] = 'he'; } // dd($refund_against); // dd(); // dd(); // dd((double)$totalAmount); # code... $n++; } // exit; // dd($updateList); } // dd($updateList); $data['against_list'] = $updateList; $validator = \Validator::make( $request->all(), array( 'retailer_id' => 'required', ) ); if ($validator->fails()) { return redirect('user/party-ledger') ->withErrors($validator) ->withInput(); } else { if ($request->from_date != '' && $request->to_date != '') { $data['ledgers'] = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->whereDate('created_at', '>=', $request->from_date)->whereDate('created_at', '<=', $request->to_date)->orderBy('id', 'asc')->get(); $open_date = date('Y-m-d', strtotime('-1 day', strtotime($request->from_date))); $total_debit = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->whereDate('created_at', '<=', $open_date)->sum('debit'); $total_credit = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->whereDate('created_at', '<=', $open_date)->sum('credit'); $opening_balance = $total_debit - $total_credit; } else { $data['ledgers'] = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->orderBy('id', 'asc')->get(); $opening_balance = 0; } } $data['retailer_id'] = $request->retailer_id; // $data['dealer_id'] = $request->dealer_id; $data['from_date'] = $request->from_date; $data['to_date'] = $request->to_date; $data['opening_balance'] = $opening_balance; } return view('dashboard.invoice.party-ledger', $data); } public function addOpeningAmount(Request $request) { $response = array(); $partyLedger_check = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id1)->where('dealer_id', $request->dealer_id1)->where('particular', 'opening balance')->first(); if ($partyLedger_check == null) { $partyLedger = new \App\PartyInvoiceLedger(); $partyLedger->retailer_id = $request->retailer_id1; $partyLedger->dealer_id = $request->dealer_id1; $partyLedger->type = $request->type; $partyLedger->against = ""; if ($request->type == "debit") { $partyLedger->debit = $request->opening_amount; $partyLedger->balance = $request->opening_amount; $openingBalance = $request->opening_amount; $is_done = false; $invoices = \App\LoadingSlipInvoice::where('retailer_id', $request->retailer_id1)->where('dealer_id', $request->dealer_id1)->orderBy('created_at', 'desc')->get(); foreach ($invoices as $invoice) { if ($is_done == false) { if ($invoice->remaining_amount >= $openingBalance) { $invoice->remaining_amount = $openingBalance; $is_done = true; $invoice->save(); } else { $openingBalance = $openingBalance - $invoice->remaining_amount; } } else { $invoice->remaining_amount = 0; $invoice->save(); } } } else { $partyLedger->credit = $request->opening_amount; $partyLedger->balance = 0 - $request->opening_amount; $openingBalance = $request->opening_amount; $is_done = false; $invoices = \App\LoadingSlipInvoice::where('retailer_id', $request->retailer_id1)->where('dealer_id', $request->dealer_id1)->orderBy('created_at', 'desc')->get(); foreach ($invoices as $invoice) { $invoice->remaining_amount = 0; $invoice->save(); } } $partyLedger->particular = 'opening balance'; if ($partyLedger->save()) { $response['success'] = true; $response['msg'] = 'Payment Rececive Successfully !'; } else { $response['success'] = true; $response['msg'] = 'Somthing Wrong !'; } } else { $response['success'] = false; $response['msg'] = 'Already Active Ledger'; } return response()->json($response); } public function addPayment(Request $request) { $response = array(); // dd($request->all()); // dd($this->dateConverter($request->transaction_date)); // dd(\App\BankStatement::where('type','payment')->orderBy('id','desc')->first()); $partyLedger = new \App\PartyInvoiceLedger(); $partyLedger->retailer_id = $request->retailer_id2; $partyLedger->dealer_id = $request->dealer_id2; // $partyLedger->type = $request->type1; if ($request->type1 == "debit") { $partyLedger->debit = $request->amount; $partyLedger->type = 'Payment'; } else { $partyLedger->credit = $request->amount; $partyLedger->type = 'Receipt'; } $partyLedger->particular = $request->particular; $partyLedger->save(); if (!$request->general_entry) { $bankStatement = new \App\BankStatement(); //$bankStatement->series=1; $bankStatement->bank_id = $request->bank_id; if ($request->type1 == 'credit') { $series = \App\BankStatement::where('type', 'receipt')->orderBy('id', 'desc')->first(); if (!is_null($series)) { $ser = $series->series + 1; } else { $ser = 1; } $bankStatement->type = 'receipt'; $bankStatement->status = 'debit'; } if ($request->type1 == 'debit') { $series = \App\BankStatement::where('type', 'payment')->orderBy('id', 'desc')->first(); if (!is_null($series)) { $ser = $series->series + 1; } else { $ser = 1; } $bankStatement->type = 'payment'; $bankStatement->status = 'credit'; } $bankStatement->series = $ser; $bankStatement->dealer_id = $request->dealer_id2; $bankStatement->mode = $request->mode; $bankStatement->particular = $request->particular; $bankStatement->amount = $request->amount; $bankStatement->history = null; $bankStatement->transaction_date = $this->dateConverter($request->transaction_date); $bankStatement->save(); $partyLedger->against = $ser; $partyLedger->save(); } else { $genSeries = \App\PartyInvoiceLedger::where('type', 'Journal Entry')->orderBy('id', 'desc')->first(); if (!is_null($genSeries)) { $genSer = $genSeries->journal_series + 1; } else { $genSer = 1; } $partyLedger->journal_series = $genSer; $partyLedger->type = 'Journal Entry'; $partyLedger->transaction_date = $this->dateConverter($request->transaction_date); $partyLedger->save(); } $response['success'] = true; $response['msg'] = 'Payment Add Successfully !'; // } else { // $response['success'] = true; // $response['msg'] = 'Somthing Wrong !'; // } return response()->json($response); } // public function addPayment(Request $request){ // $response = array(); // // dd($this->dateConverter($request->transaction_date)); // // dd(\App\BankStatement::where('type','payment')->orderBy('id','desc')->first()); // $partyLedger= new \App\PartyInvoiceLedger(); // $partyLedger->retailer_id=$request->retailer_id2; // $partyLedger->dealer_id=$request->dealer_id2; // $partyLedger->type=$request->type1; // $partyLedger->against=""; // if($request->type1=="debit"){ // $partyLedger->debit=$request->amount; // $partyLedger->balance=$request->amount; // //$openingBalance = $request->opening_amount; // }else{ // $partyLedger->credit=$request->amount; // $partyLedger->balance=0-$request->amount; // //$openingBalance = $request->opening_amount; // } // $partyLedger->particular=$request->particular; // if($partyLedger->save()){ // $bankStatement = new \App\BankStatement(); // //$bankStatement->series=1; // $bankStatement->bank_id=$request->bank_id; // if($request->type1=='credit'){ // $series=\App\BankStatement::where('type','payment')->orderBy('id','desc')->first(); // $bankStatement->series=($series->series+1); // $bankStatement->type='payment'; // $bankStatement->status='debit'; // } // if($request->type1=='debit'){ // $series=\App\BankStatement::where('type','receipt')->orderBy('id','desc')->first(); // $bankStatement->series=($series->series+1); // $bankStatement->type='receipt'; // $bankStatement->status='credit'; // } // $bankStatement->dealer_id = $request->dealer_id2; // $bankStatement->mode=$request->mode; // $bankStatement->particular=$request->particular; // $bankStatement->amount=$request->amount; // $bankStatement->history=null; // $bankStatement->transaction_date=$this->dateConverter($request->transaction_date); // $bankStatement->save(); // $response['success']=true; // $response['msg']='Payment Add Successfully !'; // }else{ // $response['success']=true; // $response['msg']='Somthing Wrong !'; // } // return response()->json($response); // } public function addCompanyPayment(Request $request) { // dd($request->all()); $response = array(); if ($request->general_entry) { if ($request->transfer_type == 1) { $validator = \Validator::make( $request->all(), array( 'product_company_id' => 'required', 'from_dealer' => 'required', 'mydealer_id' => 'required', 'payment_date' => 'required', 'amount' => 'required|numeric', // 'bank_id' => 'required', // 'payment_mode' => 'required' ) ); } else { $validator = \Validator::make( $request->all(), array( 'from_dealer' => 'required', 'mydealer_id' => 'required', 'payment_date' => 'required', 'amount' => 'required|numeric', // 'bank_id' => 'required', // 'payment_mode' => 'required' ) ); } } else { if ($request->transfer_type == 1) { $validator = \Validator::make( $request->all(), array( 'product_company_id' => 'required', 'from_dealer' => 'required', 'mydealer_id' => 'required', 'payment_date' => 'required', 'amount' => 'required|numeric', 'bank_id' => 'required', 'payment_mode' => 'required' ) ); } else { $validator = \Validator::make( $request->all(), array( 'from_dealer' => 'required', 'mydealer_id' => 'required', 'payment_date' => 'required', 'amount' => 'required|numeric', 'bank_id' => 'required', 'payment_mode' => 'required' ) ); } } // dd($this->dateConverter($request->transaction_date)); // dd(\App\BankStatement::where('type','payment')->orderBy('id','desc')->first()); $paid_amount = $request->amount; if (!$request->general_entry) { $bank_statement = new \App\BankStatement(); $bank_statement->bank_id = $request->bank_id; if ($request->type1 == 'credit') { $bank_statement->type = 'receipt'; $receipt = \App\BankStatement::where('type', 'receipt')->orderBy('id', 'desc')->first(); } else { $bank_statement->type = 'payment'; $receipt = \App\BankStatement::where('type', 'payment')->orderBy('id', 'desc')->first(); } if ($request->type1 == 'credit') { $bank_statement->status = 'debit'; $type = 'Recepit'; } else { $bank_statement->status = 'credit'; $type = 'Payment'; } $bank_statement->amount = $paid_amount; $bank_statement->transfer_type = $request->transfer_type; if ($request->transfer_type == 1) { $product_company_id = $request->product_company_id; $from_dealer = null; $bank_statement->transferfrom = $product_company_id; } else { $product_company_id = null; $from_dealer = $request->from_dealer; $bank_statement->transferfrom = $from_dealer; } $bank_statement->dealer_id = $request->mydealer_id; $bank_statement->transaction_date = $this->dateConverter($request->payment_date); $bank_statement->reference_number = null; $bank_statement->particular = $request->particular; $bank_statement->mode = $request->payment_mode; if ($receipt != null) { $current_receipt_series = $receipt->series + 1; } else { $current_receipt_series = 1; } $bank_statement->series = $current_receipt_series; if ($bank_statement->save()) { $bank_account = \App\BankAccount::find($request->bank_id); $ledger = new \App\CompanyInvoiceLedger(); $ledger->product_company_id = $product_company_id; $ledger->dealer_id = $request->mydealer_id; $ledger->from_dealer_id = $from_dealer; $ledger->particular = $request->particular; $ledger->type = $type; $ledger->voucher_no = $bank_statement->series; if ($request->type1 == 'credit') { $ledger->credit = $paid_amount; } else { $ledger->debit = $paid_amount; } $ledger->save(); $response['success'] = true; $response['msg'] = 'Payment Add Successfully !'; } else { $response['success'] = true; $response['msg'] = 'Somthing Wrong !'; } } else { // array:12 [ // "_token" => "ordBi0hqnKr2rJauZ0kDy4A4ZYnwnIK2gd1U9s5h" // "transfer_type" => "1" // "general_entry" => "on" // "from_dealer" => "1" // "product_company_id" => "1" // "mydealer_id" => "1" // "payment_date" => "16/07/2022" // "amount" => "5000" // "bank_id" => null // "payment_mode" => null // "type1" => "credit" // "particular" => "this is particular" // ] if ($request->transfer_type == 1) { $product_company_id = $request->product_company_id; $from_dealer = null; } else { $product_company_id = null; $from_dealer = $request->from_dealer; } $ledger = new \App\CompanyInvoiceLedger(); $ledger->product_company_id = $product_company_id; $ledger->dealer_id = $request->mydealer_id; $ledger->from_dealer_id = $from_dealer; $ledger->particular = $request->particular; $ledger->type = 'Journal Entry'; $ledger->general_date = $this->dateConverter($request->payment_date); $general_entry_series = 1; $latest = \App\CompanyInvoiceLedger::select()->where('type', 'Journal Entry')->latest()->first(); if ($latest != null) { $general_entry_series = $latest->general_series + 1; } $ledger->general_series = $general_entry_series; if ($request->type1 == 'credit') { $ledger->credit = $paid_amount; } else { $ledger->debit = $paid_amount; } $ledger->save(); $response['success'] = true; $response['msg'] = 'Payment Add Successfully !'; } return response()->json($response); } // public function addCompanyPayment(Request $request) // { // // dd($request->all()); // $response = array(); // // dd($this->dateConverter($request->transaction_date)); // // dd(\App\BankStatement::where('type','payment')->orderBy('id','desc')->first()); // $paid_amount = $request->amount; // $bank_statement = new \App\BankStatement(); // $bank_statement->bank_id = $request->bank_id; // if ($request->type1 == 'credit') { // $bank_statement->type = 'receipt'; // $receipt = \App\BankStatement::where('type', 'receipt')->orderBy('id', 'desc')->first(); // } else { // $bank_statement->type = 'payment'; // $receipt = \App\BankStatement::where('type', 'payment')->orderBy('id', 'desc')->first(); // } // if ($request->type1 == 'credit') { // $bank_statement->status = 'debit'; // } else { // $bank_statement->status = 'credit'; // } // $bank_statement->amount = $paid_amount; // $bank_statement->transfer_type=$request->transfer_type; // if($request->transfer_type==1){ // $product_company_id=$request->product_company_id; // $from_dealer=null; // $bank_statement->transferfrom =$product_company_id; // }else{ // $product_company_id=null; // $from_dealer=$request->from_dealer; // $bank_statement->transferfrom =$from_dealer; // } // $bank_statement->dealer_id = $request->mydealer_id; // $bank_statement->transaction_date = $this->dateConverter($request->payment_date); // $bank_statement->reference_number = null; // $bank_statement->particular = $request->particular; // $bank_statement->mode = $request->payment_mode; // if ($receipt != null) { // $current_receipt_series = $receipt->series + 1; // } else { // $current_receipt_series = 1; // } // $bank_statement->series = $current_receipt_series; // if ($bank_statement->save()) { // $bank_account = \App\BankAccount::find($request->bank_id); // $ledger = new \App\CompanyInvoiceLedger(); // $ledger->product_company_id = $product_company_id; // $ledger->dealer_id = $request->mydealer_id; // $ledger->from_dealer_id = $from_dealer; // $ledger->particular = $request->particular; // $ledger->type = $request->type1; // $ledger->voucher_no = $bank_statement->series; // if ($request->type1 == 'credit') { // $ledger->credit = $paid_amount; // } else { // $ledger->debit = $paid_amount; // } // $ledger->save(); // $response['success'] = true; // $response['msg'] = 'Payment Add Successfully !'; // } else { // $response['success'] = true; // $response['msg'] = 'Somthing Wrong !'; // } // return response()->json($response); // } public function addSdPayment(Request $request) { // dd($request->all()); $response = array(); if ($request->general_entry) { $validator = \Validator::make( $request->all(), array( 'dealer_id2' => 'required', 'retailer_id2' => 'required', 'transaction_date' => 'required', 'amount' => 'required|numeric', 'type1' => 'required', 'particular' => 'required' ) ); } else { $validator = \Validator::make( $request->all(), array( 'dealer_id2' => 'required', 'retailer_id2' => 'required', 'transaction_date' => 'required', 'type1' => 'required', 'amount' => 'required|numeric', 'bank_id' => 'required', 'mode' => 'required', 'particular' => 'required' ) ); } if ($validator->fails()) { return response()->json(['error' => $validator->errors()]); } else { if (!$request->general_entry) { $secondaryDiscountPartyLedger = new \App\SecondaryDiscountPartyLedger(); $secondaryDiscountPartyLedger->retailer_id = $request->retailer_id2; $secondaryDiscountPartyLedger->dealer_id = $request->dealer_id2; $secondaryDiscountPartyLedger->transaction_date = $this->dateConverter($request->transaction_date); if ($request->type1 == "debit") { $secondaryDiscountPartyLedger->debit = $request->amount; $secondaryDiscountPartyLedger->type = 'Payment'; } else { $secondaryDiscountPartyLedger->credit = $request->amount; $secondaryDiscountPartyLedger->type = 'Receipt'; } $secondaryDiscountPartyLedger->particular = $request->particular; $secondaryDiscountPartyLedger->save(); $bankStatement = new \App\BankStatement(); //$bankStatement->series=1; $bankStatement->bank_id = $request->bank_id; if ($request->type1 == 'credit') { $series = \App\BankStatement::where('type', 'receipt')->orderBy('id', 'desc')->first(); if (!is_null($series)) { $ser = $series->series + 1; } else { $ser = 1; } $bankStatement->type = 'receipt'; $bankStatement->status = 'debit'; } if ($request->type1 == 'debit') { $series = \App\BankStatement::where('type', 'payment')->orderBy('id', 'desc')->first(); if (!is_null($series)) { $ser = $series->series + 1; } else { $ser = 1; } $bankStatement->type = 'payment'; $bankStatement->status = 'credit'; } $bankStatement->series = $ser; $bankStatement->dealer_id = $request->dealer_id2; $bankStatement->mode = $request->mode; $bankStatement->particular = $request->particular; $bankStatement->amount = $request->amount; $bankStatement->history = null; $bankStatement->transaction_date = $this->dateConverter($request->transaction_date); $bankStatement->save(); $secondaryDiscountPartyLedger->against = $ser; $secondaryDiscountPartyLedger->save(); } else { $secondaryDiscountPartyLedger = new \App\SecondaryDiscountPartyLedger(); $secondaryDiscountPartyLedger->retailer_id = $request->retailer_id2; $secondaryDiscountPartyLedger->dealer_id = $request->dealer_id2; $secondaryDiscountPartyLedger->transaction_date = $this->dateConverter($request->transaction_date); if ($request->type1 == "debit") { $secondaryDiscountPartyLedger->debit = $request->amount; $secondaryDiscountPartyLedger->type = 'Payment'; } else { $secondaryDiscountPartyLedger->credit = $request->amount; $secondaryDiscountPartyLedger->type = 'Receipt'; } $secondaryDiscountPartyLedger->particular = $request->particular; $secondaryDiscountPartyLedger->save(); $genSeries = \App\SecondaryDiscountPartyLedger::where('type', 'Journal Entry')->orderBy('id', 'desc')->first(); if (!is_null($genSeries)) { $genSer = $genSeries->journal_series + 1; } else { $genSer = 1; } $secondaryDiscountPartyLedger->journal_series = $genSer; $secondaryDiscountPartyLedger->type = 'Journal Entry'; // $secondaryDiscountPartyLedger->transaction_date =$this->dateConverter($request->transaction_date); $secondaryDiscountPartyLedger->save(); } $response['success'] = true; $response['msg'] = 'Payment Add Successfully !'; // } else { // $response['success'] = true; // $response['msg'] = 'Somthing Wrong !'; // } return response()->json($response); } } public function companyInvoiceLedger(Request $request) { $data = array(); $dealers = array(); $product_companies = array(); $data['mydealers'] = \App\Dealer::where('is_active', 1)->get(); $data['dealers'] = \App\Dealer::where('is_active', 1)->get(); $data['product_companies'] = \App\ProductCompany::where('is_active', 1)->get(); $data['bank_accounts'] = \App\BankAccount::where('is_active', 1)->with('bank')->get(); $opening_balance = 0; if ($request->isMethod('post')) { if ($request->transfer_type == 1) { $validator = \Validator::make( $request->all(), array( 'product_company_id' => 'required', 'mydealer_id' => 'required', ) ); } else { $validator = \Validator::make( $request->all(), array( 'from_dealer' => 'required', 'mydealer_id' => 'required', ) ); } if ($validator->fails()) { return redirect('user/company-invoice-ledger') ->withErrors($validator) ->withInput(); } else { $queryVoucher = \App\CompanyInvoiceLedger::query() ->join("bank_statements", function ($join) { $join->on('bank_statements.series', 'company_invoice_ledgers.voucher_no') ->on('bank_statements.type', DB::raw("LOWER(company_invoice_ledgers.type)")); }) ->whereNotNull('company_invoice_ledgers.voucher_no'); $queryDI = \App\CompanyInvoiceLedger::query()->whereNull('general_series')->whereNull('voucher_no'); $queryJournalEntry = \App\CompanyInvoiceLedger::query()->whereNotNull('general_series'); // $query; $is_date_range = false; if ($request->from_date && $request->to_date) { $from_date = date('Y-m-d', strtotime(preg_replace('/\//', '-', $request->from_date))); $to_date = date('Y-m-d', strtotime(preg_replace('/\//', '-', $request->to_date))); $data['from_date'] = $request->from_date; $data['to_date'] = $request->to_date; $is_date_range = true; $queryVoucher->whereBetween('bank_statements.transaction_date', [$from_date, $to_date]); $queryDI->whereBetween('company_invoice_ledgers.created_at', [$from_date, $to_date]); $queryJournalEntry->whereBetween('general_date', [$from_date, $to_date]); } if ($request->mydealer_id) { $queryVoucher->where('company_invoice_ledgers.dealer_id', $request->mydealer_id); $queryDI->where('company_invoice_ledgers.dealer_id', $request->mydealer_id); $queryJournalEntry->where('dealer_id', $request->mydealer_id); $data['mydealer_id'] = $request->mydealer_id; } if ($request->transfer_type == 1) { $ledgersVoucher = $queryVoucher->where('company_invoice_ledgers.product_company_id', $request->product_company_id)->select('company_invoice_ledgers.product_company_id', 'company_invoice_ledgers.dealer_id', 'company_invoice_ledgers.particular', 'company_invoice_ledgers.debit', 'company_invoice_ledgers.credit', 'company_invoice_ledgers.voucher_no as voucher_no', 'bank_statements.transaction_date as transaction_date')->get(); $ledgersDi = $queryDI->where('product_company_id', $request->product_company_id)->select('product_company_id', 'dealer_id', 'particular', 'debit', 'credit', 'voucher_no', 'created_at as transaction_date')->get(); $ledgersJN = $queryJournalEntry->where('product_company_id', $request->product_company_id)->select('product_company_id', 'dealer_id', 'particular', 'debit', 'credit', 'general_series as voucher_no', 'general_date as transaction_date')->get(); // dd($ledgersVoucher); $data['ledgers'] = array_merge(json_decode(json_encode($ledgersVoucher), true), json_decode(json_encode($ledgersDi), true), json_decode(json_encode($ledgersJN), true)); if (count($data['ledgers']) > 0) { // dd($data['ledgers']); foreach ($data['ledgers'] as $key => $row) { $wek[$key] = $row['transaction_date']; } array_multisort($wek, SORT_ASC, $data['ledgers']); } $data['product_company_id'] = $request->product_company_id; $total_debit = \App\CompanyInvoiceLedger::where('dealer_id', $request->mydealer_id)->where('product_company_id', $request->product_company_id)->sum('debit'); $total_credit = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id)->where('dealer_id', $request->mydealer_id)->sum('credit'); $balance = 0; if ($total_debit >= $total_credit) { $balance = $total_debit - $total_credit; } if ($balance > 0) { $data['advanced_payment'] = $balance; } else { $data['advanced_payment'] = 0; } //dd($data['advanced_payment']); if ($is_date_range == true) { $open_date = date('Y-m-d', strtotime('-1 day', strtotime($from_date))); $total_debitVoucher = \App\CompanyInvoiceLedger::join("bank_statements", function ($join) { $join->on('bank_statements.series', 'company_invoice_ledgers.voucher_no') ->on('bank_statements.type', DB::raw("LOWER(company_invoice_ledgers.type)")); }) ->whereNotNull('company_invoice_ledgers.voucher_no') ->where('company_invoice_ledgers.product_company_id', $request->product_company_id) ->where('company_invoice_ledgers.dealer_id', $request->mydealer_id) ->whereDate('bank_statements.transaction_date', '<=', $open_date) ->sum('company_invoice_ledgers.debit'); // dd($total_debitVoucher); $total_debitDi = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id) ->where('dealer_id', $request->mydealer_id) ->whereNull('general_series') ->whereNull('voucher_no') ->whereDate('created_at', '<=', $open_date) ->sum('debit'); // dd($total_debitDi); $total_debitJournalEntry = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id) ->where('dealer_id', $request->mydealer_id) ->whereNotNull('general_series') ->whereDate('general_date', '<=', $open_date) ->sum('debit'); $total_creditVoucher = \App\CompanyInvoiceLedger::join("bank_statements", function ($join) { $join->on('bank_statements.series', 'company_invoice_ledgers.voucher_no') ->on('bank_statements.type', DB::raw("LOWER(company_invoice_ledgers.type)")); }) ->whereNotNull('company_invoice_ledgers.voucher_no') ->where('company_invoice_ledgers.product_company_id', $request->product_company_id) ->where('company_invoice_ledgers.dealer_id', $request->mydealer_id) ->whereDate('bank_statements.transaction_date', '<=', $open_date) ->sum('company_invoice_ledgers.credit'); $total_creditDi = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id) ->where('dealer_id', $request->mydealer_id) ->whereNull('general_series') ->whereNull('voucher_no') ->whereDate('created_at', '<=', $open_date) ->sum('credit'); $total_creditJournalEntry = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id) ->where('dealer_id', $request->mydealer_id) ->whereNotNull('general_series') ->whereDate('general_date', '<=', $open_date) ->sum('credit'); // dd($total_creditJournalEntry); $opening_balance = ($total_debitVoucher + $total_debitDi + $total_debitJournalEntry) - ($total_creditVoucher + $total_creditDi + $total_creditJournalEntry); // $total_debitJn = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id)->where('dealer_id', $request->mydealer_id)->whereNotNull('general_series')->whereDate('general_date', '<=', $open_date)->sum('debit'); // $total_creditJn = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id)->where('dealer_id', $request->mydealer_id)->whereNotNull('general_series')->whereDate('general_date', '<=', $open_date)->sum('credit'); // $opening_balance1 = $total_debitJn - $total_creditJn; } } else { $ledgersVoucher = $queryVoucher->where('company_invoice_ledgers.from_dealer_id', $request->from_dealer)->select('company_invoice_ledgers.product_company_id', 'company_invoice_ledgers.dealer_id', 'company_invoice_ledgers.particular', 'company_invoice_ledgers.debit', 'company_invoice_ledgers.credit', 'company_invoice_ledgers.voucher_no as voucher_no', 'bank_statements.transaction_date as transaction_date')->get(); $ledgersDi = $queryDI->where('from_dealer_id', $request->from_dealer)->select('product_company_id', 'dealer_id', 'particular', 'debit', 'credit', 'voucher_no', 'created_at as transaction_date')->get(); $ledgersJN = $queryJournalEntry->where('from_dealer_id', $request->from_dealer)->select('product_company_id', 'dealer_id', 'particular', 'debit', 'credit', 'general_series as voucher_no', 'general_date as transaction_date')->get(); $data['ledgers'] = array_merge(json_decode(json_encode($ledgersVoucher), true), json_decode(json_encode($ledgersDi), true), json_decode(json_encode($ledgersJN), true)); // dd($data['ledgers']); foreach ($data['ledgers'] as $key => $row) { $wek[$key] = $row['transaction_date']; } array_multisort($wek, SORT_ASC, $data['ledgers']); $data['from_dealer'] = $request->from_dealer; $total_debit = \App\CompanyInvoiceLedger::where('dealer_id', $request->mydealer_id)->where('from_dealer_id', $request->from_dealer)->sum('debit'); $total_credit = \App\CompanyInvoiceLedger::where('from_dealer_id', $request->from_dealer)->where('dealer_id', $request->mydealer_id)->sum('credit'); $balance = 0; if ($total_debit >= $total_credit) { $balance = $total_debit - $total_credit; } if ($balance > 0) { $data['advanced_payment'] = $balance; } else { $data['advanced_payment'] = 0; } if ($is_date_range == true) { $open_date = date('Y-m-d', strtotime('-1 day', strtotime($from_date))); $total_debitVoucher = \App\CompanyInvoiceLedger::join("bank_statements", function ($join) { $join->on('bank_statements.series', 'company_invoice_ledgers.voucher_no') ->on('bank_statements.type', DB::raw("LOWER(company_invoice_ledgers.type)")); }) ->whereNotNull('company_invoice_ledgers.voucher_no') ->where('company_invoice_ledgers.from_dealer_id', $request->from_dealer) ->where('company_invoice_ledgers.dealer_id', $request->mydealer_id) ->whereDate('bank_statements.transaction_date', '<=', $open_date) ->sum('company_invoice_ledgers.debit'); // dd($total_debitVoucher); $total_debitDi = \App\CompanyInvoiceLedger::where('from_dealer_id', $request->from_dealer) ->where('dealer_id', $request->mydealer_id) ->whereNull('general_series') ->whereNull('voucher_no') ->whereDate('created_at', '<=', $open_date) ->sum('debit'); // dd($total_debitDi); $total_debitJournalEntry = \App\CompanyInvoiceLedger::where('from_dealer_id', $request->from_dealer) ->where('dealer_id', $request->mydealer_id) ->whereNotNull('general_series') ->whereDate('general_date', '<=', $open_date) ->sum('debit'); $total_creditVoucher = \App\CompanyInvoiceLedger::join("bank_statements", function ($join) { $join->on('bank_statements.series', 'company_invoice_ledgers.voucher_no') ->on('bank_statements.type', DB::raw("LOWER(company_invoice_ledgers.type)")); }) ->whereNotNull('company_invoice_ledgers.voucher_no') ->where('company_invoice_ledgers.from_dealer_id', $request->from_dealer) ->where('company_invoice_ledgers.dealer_id', $request->mydealer_id) ->whereDate('bank_statements.transaction_date', '<=', $open_date) ->sum('company_invoice_ledgers.credit'); $total_creditDi = \App\CompanyInvoiceLedger::where('from_dealer_id', $request->from_dealer) ->where('dealer_id', $request->mydealer_id) ->whereNull('general_series') ->whereNull('voucher_no') ->whereDate('created_at', '<=', $open_date) ->sum('credit'); $total_creditJournalEntry = \App\CompanyInvoiceLedger::where('from_dealer_id', $request->from_dealer) ->where('dealer_id', $request->mydealer_id) ->whereNotNull('general_series') ->whereDate('general_date', '<=', $open_date) ->sum('credit'); // dd($total_creditJournalEntry); $opening_balance = ($total_debitVoucher + $total_debitDi + $total_debitJournalEntry) - ($total_creditVoucher + $total_creditDi + $total_creditJournalEntry); } } } } $data['opening_balance'] = $opening_balance; return view('dashboard.invoice.company-invoice-ledger', $data); } public function companyInvoiceLedger1(Request $request) { $data = array(); $dealers = array(); $product_companies = array(); $data['mydealers'] = \App\Dealer::where('is_active', 1)->get(); $data['dealers'] = \App\Dealer::where('is_active', 1)->get(); $data['product_companies'] = \App\ProductCompany::where('is_active', 1)->get(); $data['bank_accounts'] = \App\BankAccount::where('is_active', 1)->with('bank')->get(); $opening_balance = 0; if ($request->isMethod('post')) { if ($request->transfer_type == 1) { $validator = \Validator::make( $request->all(), array( 'product_company_id' => 'required', 'mydealer_id' => 'required', ) ); } else { $validator = \Validator::make( $request->all(), array( 'from_dealer' => 'required', 'mydealer_id' => 'required', ) ); } if ($validator->fails()) { return redirect('user/company-invoice-ledger') ->withErrors($validator) ->withInput(); } else { $query = \App\CompanyInvoiceLedger::query(); $is_date_range = false; if ($request->from_date && $request->to_date) { $from_date = date('Y-m-d', strtotime(preg_replace('/\//', '-', $request->from_date))); $to_date = date('Y-m-d', strtotime(preg_replace('/\//', '-', $request->to_date))); $data['from_date'] = $request->from_date; $data['to_date'] = $request->to_date; $is_date_range = true; $query->whereBetween('created_at', [$from_date, $to_date]); } if ($request->mydealer_id) { $query->where('dealer_id', $request->mydealer_id); $data['mydealer_id'] = $request->mydealer_id; } if ($request->transfer_type == 1) { $data['ledgers'] = $query->where('product_company_id', $request->product_company_id)->get(); $data['product_company_id'] = $request->product_company_id; $total_debit = \App\CompanyInvoiceLedger::where('dealer_id', $request->mydealer_id)->where('product_company_id', $request->product_company_id)->sum('debit'); $total_credit = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id)->where('dealer_id', $request->mydealer_id)->sum('credit'); $balance = 0; if ($total_debit >= $total_credit) { $balance = $total_debit - $total_credit; } if ($balance > 0) { $data['advanced_payment'] = $balance; } else { $data['advanced_payment'] = 0; } //dd($data['advanced_payment']); if ($is_date_range == true) { $open_date = date('Y-m-d', strtotime('-1 day', strtotime($request->from_date))); $total_debit = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id)->where('dealer_id', $request->mydealer_id)->whereDate('created_at', '<=', $open_date)->sum('debit'); $total_credit = \App\CompanyInvoiceLedger::where('product_company_id', $request->product_company_id)->where('dealer_id', $request->mydealer_id)->whereDate('created_at', '<=', $open_date)->sum('credit'); $opening_balance = $total_debit - $total_credit; } } else { $data['ledgers'] = $query->where('from_dealer_id', $request->from_dealer)->get(); $data['from_dealer'] = $request->from_dealer; $total_debit = \App\CompanyInvoiceLedger::where('dealer_id', $request->mydealer_id)->where('from_dealer_id', $request->from_dealer)->sum('debit'); $total_credit = \App\CompanyInvoiceLedger::where('from_dealer_id', $request->from_dealer)->where('dealer_id', $request->mydealer_id)->sum('credit'); $balance = 0; if ($total_debit >= $total_credit) { $balance = $total_debit - $total_credit; } if ($balance > 0) { $data['advanced_payment'] = $balance; } else { $data['advanced_payment'] = 0; } if ($is_date_range == true) { $open_date = date('Y-m-d', strtotime('-1 day', strtotime($request->from_date))); $total_debit = \App\CompanyInvoiceLedger::where('from_dealer_id', $request->from_dealer)->where('dealer_id', $request->mydealer_id)->whereDate('created_at', '<=', $open_date)->sum('debit'); $total_credit = \App\CompanyInvoiceLedger::where('from_dealer_id', $request->from_dealer)->where('dealer_id', $request->mydealer_id)->whereDate('created_at', '<=', $open_date)->sum('credit'); $opening_balance = $total_debit - $total_credit; } } } } $data['opening_balance'] = $opening_balance; return view('dashboard.invoice.company-invoice-ledger', $data); } public function addCompanyOpeningAmount(Request $request) { $response = array(); $ledger_check = \App\CompanyInvoiceLedger::query(); if ($request->transfer_type == 1) { $ledger_check = $ledger_check->where('product_company_id', $request->product_company_id)->where('dealer_id', $request->mydealer_id); } else { $ledger_check = $ledger_check->where('from_dealer_id', $request->from_dealer)->where('dealer_id', $request->mydealer_id); } $ledger_check = $ledger_check->where('particular', 'opening balance')->first(); if ($ledger_check == null) { $partyCompanyLedger = new \App\CompanyInvoiceLedger(); if ($request->transfer_type == 1) { $partyCompanyLedger->product_company_id = $request->product_company_id; } else { $partyCompanyLedger->from_dealer_id = $request->from_dealer; } $partyCompanyLedger->dealer_id = $request->mydealer_id; $partyCompanyLedger->type = $request->type; $partyCompanyLedger->against = ""; if ($request->type == "debit") { $partyCompanyLedger->debit = $request->opening_amount; } else { $partyCompanyLedger->credit = $request->opening_amount; } $partyCompanyLedger->particular = 'opening balance'; if ($partyCompanyLedger->save()) { $response['success'] = true; $response['msg'] = 'Payment Rececive Successfully !'; } else { $response['success'] = true; $response['msg'] = 'Somthing Wrong !'; } } else { $response['success'] = false; $response['msg'] = 'Already Active Ledger'; } return response()->json($response); } public function secondary_discount_party_ledgers(Request $request) { $data = array(); $opening_balance = 0; $data['retailers'] = \App\Retailer::where('is_active', 1)->get(); $data['dealers'] = \App\Dealer::where('is_active', 1)->whereIn('id', [1, 30, 31, 114, 84])->get(); $data['modes'] = \App\PaymentMode::where('is_active', 1)->get(); $data['bank_accounts'] = \App\BankAccount::where('is_active', 1)->with('bank')->get(); if ($request->isMethod('post')) { $validator = \Validator::make( $request->all(), array( 'retailer_id' => 'required', 'dealer_id' => 'required', ) ); if ($validator->fails()) { return redirect('user/sd-party-ledger') ->withErrors($validator) ->withInput(); } else { if ($request->from_date != '' && $request->to_date != '') { // $data['ledgers'] = \App\SecondaryDiscountPartyLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->whereDate('created_at', '>=', $request->from_date)->whereDate('created_at', '<=', $request->to_date)->orderBy('id', 'asc')->get(); $data['ledgers'] = \App\SecondaryDiscountPartyLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->whereDate('transaction_date', '>=', $request->from_date)->whereDate('transaction_date', '<=', $request->to_date)->orderBy('transaction_date', 'asc')->get(); $open_date = date('Y-m-d', strtotime('-1 day', strtotime($request->from_date))); $total_debit = \App\SecondaryDiscountPartyLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->whereDate('transaction_date', '<=', $open_date)->sum('debit'); $total_credit = \App\SecondaryDiscountPartyLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->whereDate('transaction_date', '<=', $open_date)->sum('credit'); $opening_balance = $total_debit - $total_credit; } else { $data['ledgers'] = \App\SecondaryDiscountPartyLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->orderBy('transaction_date', 'asc')->get(); $opening_balance = 0; } } $data['retailer_id'] = $request->retailer_id; $data['dealer_id'] = $request->dealer_id; $data['from_date'] = $request->from_date; $data['to_date'] = $request->to_date; $data['opening_balance'] = $opening_balance; } return view('dashboard.invoice.sd-party-ledger', $data); } public function addSdOpeningAmount(Request $request) { $response = array(); $partyLedger_check = \App\SecondaryDiscountPartyLedger::where('retailer_id', $request->retailer_id1)->where('dealer_id', $request->dealer_id1)->where('particular', 'opening balance')->first(); if ($partyLedger_check == null) { $partyLedger = new \App\SecondaryDiscountPartyLedger(); $partyLedger->retailer_id = $request->retailer_id1; $partyLedger->dealer_id = $request->dealer_id1; $partyLedger->type = $request->type; $partyLedger->transaction_date = date('Y-m-d'); $partyLedger->against = ""; if ($request->type == "debit") { $partyLedger->debit = $request->opening_amount; $partyLedger->balance = $request->opening_amount; $openingBalance = $request->opening_amount; } else { $partyLedger->credit = $request->opening_amount; $partyLedger->balance = 0 - $request->opening_amount; $openingBalance = $request->opening_amount; } $partyLedger->particular = 'opening balance'; if ($partyLedger->save()) { $response['success'] = true; $response['msg'] = 'Payment Rececive Successfully !'; } else { $response['success'] = true; $response['msg'] = 'Somthing Wrong !'; } } else { $response['success'] = false; $response['msg'] = 'Already Active Ledger'; } return response()->json($response); } public function addCompanyDiPayment1(Request $request) { $response = array(); if ($request->retailer_id != "") { $sd_party_leadger_last_balance = \App\SecondaryDiscountPartyLedger::where('retailer_id', $request->retailer_id)->orderBy('id', 'desc')->first(); $sd_party_leadger = new \App\SecondaryDiscountPartyLedger(); $sd_party_leadger->retailer_id = $request->retailer_id; $sd_party_leadger->credit = $request->amount; $sd_party_leadger->against = $request->reference_no; $sd_party_leadger->mode = $request->mode; $sd_party_leadger->transaction_date = $request->transaction_date; $sd_party_leadger->particular = "By " . $request->mode; $sd_party_leadger->balance = $sd_party_leadger_last_balance->balance - $request->amount; if ($sd_party_leadger->save()) { $response['success'] = true; $response['msg'] = 'Payment Rececive Successfully !'; } else { $response['success'] = true; $response['msg'] = 'Somthing Wrong !'; } } else { $response['success'] = false; $response['msg'] = 'Retaier not selected !'; } return response()->json($response); } public function addCompanyDiPayment(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'retailer_id1' => 'required', 'dealer_id1' => 'required', 'bank_id1' => 'required', 'amount' => 'required|numeric|min:0', 'reference_no' => 'required', 'mode' => 'required', 'transaction_date' => 'required' ) ); if ($validator->fails()) { // return redirect('user/sd-party-ledger') // ->withErrors($validator) // ->withInput(); return response()->json(['errors' => $validator->errors(), 'flag' => false]); } else { $bank_statement = new \App\BankStatement(); $bank_statement->dealer_id = $request->dealer_id1; $bank_statement->amount = $request->amount; $bank_statement->reference_number = $request->reference_no; $bank_statement->mode = $request->mode; $bank_statement->transaction_date = $request->transaction_date; $bank_statement->particular = getModelById('Retailer', $request->retailer_id1)->name; $bank_statement->status = 'debit'; $bank_statement->type = 'receipt'; $bank_statement->bank_id = $request->bank_id1; $receipt = \App\BankStatement::where('type', 'receipt')->orderBy('id', 'desc')->first(); if ($receipt == null) { $receipt_series = 1; } else { $receipt_series = $receipt->series + 1; } $bank_statement->series = $receipt_series; if ($bank_statement->save()) { $sd_party_leadger = new \App\SecondaryDiscountPartyLedger(); $sd_party_leadger->retailer_id = $request->retailer_id1; $sd_party_leadger->dealer_id = $request->dealer_id1; $sd_party_leadger->credit = $request->amount; $sd_party_leadger->against = $bank_statement->series; $sd_party_leadger->mode = $request->mode; $sd_party_leadger->transaction_date = $request->transaction_date; $sd_party_leadger->particular = "By " . $request->mode; $sd_party_leadger->type = 'Receipt'; $sd_party_leadger->save(); $response['flag'] = true; $response['msg'] = 'Payment Rececive Successfully !'; } else { $response['flag'] = false; $response['msg'] = 'Somthing Wrong !'; } return response()->json($response); } } public function fieldCollection(Request $request) { $data = array(); $data['role_id'] = \Auth::user()->role_id; $data['collection_user_list'] = \App\ReceivePayment::with('user')->groupBy('user_id')->get(); if ($request->isMethod('post')) { // dd($request->all()); $validator = \Validator::make( $request->all(), array( 'collection_user' => 'required', 'collection_payment_mode' => 'required' ) ); if ($validator->fails()) { return redirect('/user/field-collection') ->withErrors($validator) ->withInput(); } else { $data['collection_user_id'] = $request->collection_user; $data['collection_payment_mode'] = $request->collection_payment_mode; $data['field_collections'] = \App\ReceivePayment::where([['user_id', '=', $request->collection_user], ['mode', '=', $request->collection_payment_mode], ['status', '=', '0']])->get(); $data['field_collections_amount'] = \App\ReceivePayment::where([['user_id', '=', $request->collection_user], ['mode', '=', $request->collection_payment_mode], ['status', '=', '0']])->sum('amount'); } } // dd($data); return view('dashboard.invoice.field-collection', $data); } public function getFCStatus($id) { $data['rp_id'] = $id; $data['role_id'] = \Auth::user()->role_id; return view('dashboard.invoice.field-collection-status', $data); } public function collectionList() { // $today = date('Y-m-d'); $response = array(); $response['collection_pending'] = \App\ReceivePayment::where('status', 0)->orderBy('id', 'desc')->get(); $response['collection_success1'] = 0; $response['collection_success2'] = 0; // $response['date_collection_success'] = \App\ReceivePayment::where('status', 1)->orderBy('id', 'desc')->get(); //$total = \App\ReceivePayment::sum('amount'); //$reject = \App\ReceivePayment::where('status', 2)->sum('amount'); $response['total_collection'] = \App\ReceivePayment::where('status', 0)->sum('amount'); //$response['client_name'] = \App\ReceivePayment::whereNotNull('user_id')->groupBy('user_id')->get(); return view('dashboard.invoice.collection-list', $response); } public function dateCollectionList(Request $request) { $response = array(); // $response['date_collection_list'] = \App\ReceivePayment::whereDate('created_at', '=', date('Y-m-d', strtotime($request->from_date)))->orderBy('id', 'desc')->get(); if ($request->client && $request->from_date) { $response['collection_pending'] = 0; $response['collection_success1'] = \App\ReceivePayment::where('user_id', $request->client)->where('status', 1)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->from_date)))->orderBy('id', 'desc')->get(); $response['collection_success2'] = \App\ReceivePayment::where('user_id', $request->client)->where('status', 2)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->from_date)))->orderBy('id', 'desc')->get(); $total = \App\ReceivePayment::where('user_id', $request->client)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->from_date)))->sum('amount'); $reject = \App\ReceivePayment::where('user_id', $request->client)->where('status', 2)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->from_date)))->sum('amount'); $pending = \App\ReceivePayment::where('user_id', $request->client)->where('status', 0)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->from_date)))->sum('amount'); $response['total_collection'] = $total - ($reject + $pending); } else { $response['collection_pending'] = 0; $response['collection_success1'] = \App\ReceivePayment::where('status', 1)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->from_date)))->orderBy('id', 'desc')->get(); $response['collection_success2'] = \App\ReceivePayment::where('status', 2)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->from_date)))->orderBy('id', 'desc')->get(); $total = \App\ReceivePayment::whereDate('created_at', '=', date('Y-m-d', strtotime($request->from_date)))->sum('amount'); $reject = \App\ReceivePayment::where('status', 2)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->from_date)))->sum('amount'); $pending = \App\ReceivePayment::where('status', 0)->whereDate('created_at', '=', date('Y-m-d', strtotime($request->from_date)))->sum('amount'); $response['total_collection'] = $total - ($reject + $pending); } return view('dashboard.invoice.collection-list', $response); } public function setFCStatus(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( 'status' => 'required', ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $validator->getMessageBag(); } else { $rp_details = \App\ReceivePayment::where('id', $request->id)->first(); if (is_null($rp_details)) { $response['flag'] = false; $response['error'] = "Item Not found"; } else { $status = $request->status; $arr = $request->id; $d = explode(',', $arr); foreach ($d as $ads) { $rp_details = \App\ReceivePayment::where('id', $ads)->first(); if ($status == 1) { $SecondryTotalDebit = \App\SecondaryDiscountPartyLedger::where(['dealer_id' => $rp_details->dealer_id, 'retailer_id' => $rp_details->retailer_id])->sum('debit'); $SecondryTotalCredit = \App\SecondaryDiscountPartyLedger::where(['dealer_id' => $rp_details->dealer_id, 'retailer_id' => $rp_details->retailer_id])->sum('credit'); $SecondryBalance = $SecondryTotalDebit - $SecondryTotalCredit; $SecondryBalances = abs($SecondryBalance); $payment = $rp_details->amount; if ($rp_details->mode == 'Cash') { $bank_id = 3; } else { $bank_id = 2; } if ($SecondryBalances >= $payment && $SecondryBalances != 0) { $retailer = getModelById('Retailer', $rp_details->retailer_id); $bank_particular = $retailer->name . " - " . $retailer->address; $bank_statement = new \App\BankStatement(); $bank_statement->dealer_id = $rp_details->dealer_id; $bank_statement->amount = $payment; $bank_statement->reference_number = $rp_details->reference_no; $bank_statement->mode = $rp_details->mode; $bank_statement->transaction_date = date('Y-m-d'); $bank_statement->particular = getModelById('Retailer', $rp_details->retailer_id)->name; $bank_statement->status = 'debit'; $bank_statement->type = 'receipt'; $bank_statement->bank_id = $bank_id; $receipt = \App\BankStatement::where('type', 'receipt')->orderBy('id', 'desc')->first(); if ($receipt == null) { $receipt_series = 1; } else { $receipt_series = $receipt->series + 1; } $bank_statement->series = $receipt_series; if ($bank_statement->save()) { $sd_party_leadger = new \App\SecondaryDiscountPartyLedger(); $sd_party_leadger->retailer_id = $rp_details->retailer_id; $sd_party_leadger->dealer_id = $rp_details->dealer_id; $sd_party_leadger->credit = $payment; $sd_party_leadger->against = $bank_statement->series; $sd_party_leadger->mode = $rp_details->mode; $sd_party_leadger->transaction_date = date('Y-m-d'); $sd_party_leadger->particular = "By " . $rp_details->mode; $sd_party_leadger->type = 'Receipt'; $sd_party_leadger->save(); } } else { $partyAmount = $payment - $SecondryBalances; $partyAmounts = abs($partyAmount); if ($SecondryBalances <= $payment && $SecondryBalances != 0) { $retailer = getModelById('Retailer', $rp_details->retailer_id); $bank_particular = $retailer->name . " - " . $retailer->address; $bank_statement = new \App\BankStatement(); $bank_statement->dealer_id = $rp_details->dealer_id; $bank_statement->amount = $SecondryBalances; $bank_statement->reference_number = $rp_details->reference_no; $bank_statement->mode = $rp_details->mode; $bank_statement->transaction_date = date('Y-m-d'); $bank_statement->particular = getModelById('Retailer', $rp_details->retailer_id)->name; $bank_statement->status = 'debit'; $bank_statement->type = 'receipt'; $bank_statement->bank_id = $bank_id; $receipt = \App\BankStatement::where('type', 'receipt')->orderBy('id', 'desc')->first(); if ($receipt == null) { $receipt_series = 1; } else { $receipt_series = $receipt->series + 1; } $bank_statement->series = $receipt_series; if ($bank_statement->save()) { $sd_party_leadger = new \App\SecondaryDiscountPartyLedger(); $sd_party_leadger->retailer_id = $rp_details->retailer_id; $sd_party_leadger->dealer_id = $rp_details->dealer_id; $sd_party_leadger->credit = $SecondryBalances; $sd_party_leadger->against = $bank_statement->series; $sd_party_leadger->mode = $rp_details->mode; $sd_party_leadger->transaction_date = date('Y-m-d'); $sd_party_leadger->particular = "By " . $rp_details->mode; $sd_party_leadger->type = 'Receipt'; $sd_party_leadger->save(); } } $partyTotalDebit = \App\PartyInvoiceLedger::where(['dealer_id' => $rp_details->dealer_id, 'retailer_id' => $rp_details->retailer_id])->sum('debit'); $partyTotalCredit = \App\PartyInvoiceLedger::where(['dealer_id' => $rp_details->dealer_id, 'retailer_id' => $rp_details->retailer_id])->sum('credit'); $balance = $partyTotalDebit - $partyTotalCredit; $invoice = \App\LoadingSlipInvoice::where(['dealer_id' => $rp_details->dealer_id, 'retailer_id' => $rp_details->retailer_id])->where('remaining_amount', '>', 0)->get(); if ($balance < 0) { $advBal = abs($balance); } else { $advBal = 0; } if ($rp_details->mode == 'Cash') { $bank_id = 3; } else { $bank_id = 2; } // dd($partyTotalCredit); $paidAmount = $advBal + $partyAmounts; if (!$invoice->isEmpty()) { $retailer = getModelById('Retailer', $rp_details->retailer_id); $bank_particular = $retailer->name . " - " . $retailer->address; $bank_statement = new \App\BankStatement(); $bank_statement->bank_id = $bank_id; $bank_statement->particular = $bank_particular; $bank_statement->type = 'receipt'; $bank_statement->status = 'debit'; $bank_statement->amount = $partyAmounts; $bank_statement->dealer_id = $rp_details->dealer_id; $bank_statement->transaction_date = date('Y-m-d'); $bank_statement->reference_number = $rp_details->reference_no; $bank_statement->mode = $rp_details->mode; $receipt = \App\BankStatement::where('type', 'receipt')->orderBy('id', 'desc')->first(); //dd($receipt); if ($receipt == null) { $current_receipt_series = 1; } else { $current_receipt_series = $receipt->series + 1; } $bank_statement->series = $current_receipt_series; if ($bank_statement->save()) { $invoiceDetails = []; $n = 0; foreach ($invoice as $key => $value) { $invoiceAmt = 0; $invoice_amount = $value->remaining_amount; $invoiceUpd = \App\LoadingSlipInvoice::find($value->id); if ($invoice_amount <= $paidAmount && $paidAmount != 0) { $payment = new \App\PartyInvoicePayment(); $invoiceAmt = $invoice_amount; $invoiceDetails[$n]['invoice'] = $value->id; $invoiceDetails[$n]['invoice_number'] = $value->invoice_number; $invoiceDetails[$n]['amount'] = $invoiceAmt; $invoiceDetails[$n]['date'] = date('Y-m-d'); $invoiceDetails[$n]['refund_amount'] = (float)0; // $invoiceDetails[$n]['remaining_amount']=$invoice_amount; $payment->invoice_id = $value->id; $payment->payment_amount = $invoiceAmt; $payment->payment_date = date('Y-m-d'); $payment->bank_account_id = $bank_id; $payment->bank_reference_number = $rp_details->reference_no; $payment->payment_mode = $rp_details->mode; $payment->receipt_id = $bank_statement->id; $invoiceUpd->is_paid = 1; $invoiceUpd->remaining_amount = $invoiceUpd->remaining_amount - $invoiceAmt; $paidAmount = $paidAmount - $invoice_amount; $payment->save(); } elseif ($invoice_amount > $paidAmount && $paidAmount != 0) { $payment = new \App\PartyInvoicePayment(); $invoiceAmt = $paidAmount; $invoiceDetails[$n]['invoice'] = $value->id; $invoiceDetails[$n]['invoice_number'] = $value->invoice_number; $invoiceDetails[$n]['amount'] = $invoiceAmt; $invoiceDetails[$n]['date'] = date('Y-m-d'); $invoiceDetails[$n]['refund_amount'] = (float)0; // $invoiceDetails[$n]['remaining_amount']=$invoice_amount; $payment->invoice_id = $value->id; $payment->payment_amount = $invoiceAmt; $payment->payment_date = date('Y-m-d'); $payment->bank_account_id = $bank_id; $payment->bank_reference_number = $rp_details->reference_no; $payment->payment_mode = $rp_details->mode; $payment->receipt_id = $bank_statement->id; $invoiceUpd->is_paid = 1; $invoiceUpd->remaining_amount = $invoiceUpd->remaining_amount - $invoiceAmt; $paidAmount = 0; $payment->save(); } $invoiceUpd->save(); $n++; // dd($invoice); } $bank_statement->history = json_encode($invoiceDetails); $bank_statement->save(); $bank_account = \App\BankAccount::find($bank_id); $perticular = "By " . $bank_account->bank->name; $ledger = new \App\PartyInvoiceLedger(); $ledger->retailer_id = $rp_details->retailer_id; $ledger->dealer_id = $rp_details->dealer_id; $ledger->particular = $perticular; $ledger->credit = $partyAmounts; $ledger->type = 'Receipt'; $ledger->debit = '0.00'; $ledger->against = $bank_statement->series; $ledger->save(); } } else { $retailer = getModelById('Retailer', $rp_details->retailer_id); $bank_particular = $retailer->name . " - " . $retailer->address; $bank_statement = new \App\BankStatement(); $bank_statement->bank_id = $bank_id; $bank_statement->particular = $bank_particular; $bank_statement->type = 'receipt'; $bank_statement->status = 'debit'; $bank_statement->amount = $partyAmounts; $bank_statement->dealer_id = $rp_details->dealer_id; $bank_statement->transaction_date = date('Y-m-d'); $bank_statement->reference_number = $rp_details->reference_no; $bank_statement->mode = $rp_details->mode; $receipt = \App\BankStatement::where('type', 'receipt')->orderBy('id', 'desc')->first(); if ($receipt == null) { $current_receipt_series = 1; } else { $current_receipt_series = $receipt->series + 1; } $bank_statement->series = $current_receipt_series; if ($bank_statement->save()) { $bank_account = \App\BankAccount::find($bank_id); $perticular = "By " . $bank_account->bank->name; $ledger = new \App\PartyInvoiceLedger(); $ledger->retailer_id = $rp_details->retailer_id; $ledger->dealer_id = $rp_details->dealer_id; $ledger->particular = $perticular; $ledger->credit = $partyAmounts; $ledger->type = 'Receipt'; $ledger->debit = 0; $ledger->against = $bank_statement->series; $ledger->save(); } } } } $rp_details->status = $request->status; $rp_details->remark = $request->remark; $rp_details->approval_user_id = \Auth::user()->id; $data = $rp_details->save(); } if ($data) { $response['flag'] = true; $response['message'] = "Status Updated Successfully"; } else { $response['flag'] = false; $response['error'] = "Something Went Wrong"; } } } return response()->json($response); } public function expenseVouchers() { $dealer_id = \Auth::user()->company_id; if ($dealer_id == 9) { //$today = date('Y-m-d'); $response = array(); $response['date_collection_all_not_paid'] = \App\ExpenseVoucher::where('approve_status', 0)->where('dealer_id', '=', 9)->orderBy('id', 'desc')->get(); $response['date_collection_all_pending'] = \App\ExpenseVoucher::where('paid_status', 0)->where('approve_status', 1)->where('dealer_id', '=', 9)->orderBy('id', 'desc')->get(); $response['date_collection_completed'] = 0; $admin = \App\ExpenseVoucher::where('approve_status', 0)->where('dealer_id', '=', 9)->sum('amount'); $account = \App\ExpenseVoucher::where('paid_status', 0)->where('approve_status', 1)->where('dealer_id', '=', 9)->sum('amount'); $response['date_total_collection'] = $admin + $account; return view('dashboard.invoice.expense-voucher', $response); } else { $response = array(); $response['date_collection_all_not_paid'] = \App\ExpenseVoucher::where('approve_status', 0)->where('dealer_id', '!=', 9)->orderBy('id', 'desc')->get(); $response['date_collection_all_pending'] = \App\ExpenseVoucher::where('paid_status', 0)->where('dealer_id', '!=', 9)->where('approve_status', 1)->orderBy('id', 'desc')->get(); $response['date_collection_completed'] = 0; $admin = \App\ExpenseVoucher::where('approve_status', 0)->where('dealer_id', '!=', 9)->sum('amount'); $account = \App\ExpenseVoucher::where('paid_status', 0)->where('dealer_id', '!=', 9)->where('approve_status', 1)->sum('amount'); $response['date_total_collection'] = $admin + $account; return view('dashboard.invoice.expense-voucher', $response); } } public function namanExpenseVoucher() { //$today = date('Y-m-d'); $response = array(); $response['date_collection_all_not_paid'] = \App\ExpenseVoucher::where('approve_status', 0)->orderBy('id', 'desc')->get(); $response['date_collection_all_pending'] = \App\ExpenseVoucher::where('paid_status', 0)->where('approve_status', 1)->orderBy('id', 'desc')->get(); $response['date_collection_completed'] = 0; $admin = \App\ExpenseVoucher::where('approve_status', 0)->sum('amount'); $account = \App\ExpenseVoucher::where('paid_status', 0)->where('approve_status', 1)->sum('amount'); $response['date_total_collection'] = $admin + $account; return view('dashboard.invoice.naman-expense-voucher', $response); } public function dateExpenseVouchers(Request $request) { $dealer_id = \Auth::user()->company_id; if ($dealer_id == 9) { $response = array(); // $data['expense_vouchers'] = \App\ExpenseVoucher::orderBy('id', 'desc')->get(); $response['date_collection_all_not_paid'] = 0; $response['date_collection_all_pending'] = 0; $response['date_collection_completed'] = \App\ExpenseVoucher::whereNotNull('transaction_date')->where('dealer_id', '=', 9)->whereDate('transaction_date', '=', date('Y-m-d', strtotime($request->from_date)))->orderBy('id', 'desc')->get(); $response['date_total_collection'] = \App\ExpenseVoucher::whereNotNull('transaction_date')->where('dealer_id', '=', 9)->whereDate('transaction_date', '=', date('Y-m-d', strtotime($request->from_date)))->sum('amount'); return view('dashboard.invoice.expense-voucher', $response); } else { $response = array(); // $data['expense_vouchers'] = \App\ExpenseVoucher::orderBy('id', 'desc')->get(); $response['date_collection_all_not_paid'] = 0; $response['date_collection_all_pending'] = 0; $response['date_collection_completed'] = \App\ExpenseVoucher::whereNotNull('transaction_date')->where('dealer_id', '!=', 9)->whereDate('transaction_date', '=', date('Y-m-d', strtotime($request->from_date)))->orderBy('id', 'desc')->get(); $response['date_total_collection'] = \App\ExpenseVoucher::whereNotNull('transaction_date')->where('dealer_id', '!=', 9)->whereDate('transaction_date', '=', date('Y-m-d', strtotime($request->from_date)))->sum('amount'); return view('dashboard.invoice.expense-voucher', $response); } } public function namandateExpenseVouchers(Request $request) { $response = array(); // $data['expense_vouchers'] = \App\ExpenseVoucher::orderBy('id', 'desc')->get(); $response['date_collection_all_not_paid'] = 0; $response['date_collection_all_pending'] = 0; $response['date_collection_completed'] = \App\ExpenseVoucher::whereNotNull('transaction_date')->whereDate('created_at', '=', date('Y-m-d', strtotime($request->from_date)))->orderBy('id', 'desc')->get(); $response['date_total_collection'] = \App\ExpenseVoucher::whereNotNull('transaction_date')->whereDate('created_at', '=', date('Y-m-d', strtotime($request->from_date)))->sum('amount'); return view('dashboard.invoice.naman-expense-voucher.blade', $response); } public function expenseVouchersPaidStatus($id) { $data = array(); $expensePaidStatus = \App\ExpenseVoucher::where('id', $id)->first(); if (empty($expensePaidStatus)) { $data['flag'] = false; $data['message'] = "Expense Voucher not found"; } else { $expensePaidStatus->paid_status = 1; $expensePaidStatus->paid_id = \Auth::user()->id; $expensePaidStatus->paid_date = date('Y-m-d H:i:s'); $expensePaidStatus->transaction_date = date('Y-m-d H:i:s'); if ($expensePaidStatus->save()) { if (date('m') <= 3) { $financial_year = (date('Y') - 1) . '-' . date('Y'); } else { $financial_year = date('Y') . '-' . (date('Y') + 1); } $session = \App\Session::where('session', $financial_year)->where('is_active', 1)->first(); if ($expensePaidStatus->mode == 'cash') { $bank_id = 3; $mode = 'Cash'; } if ($expensePaidStatus->mode == 'cheque') { $bank_id = 2; $mode = 'Cheque'; } //ExpenseLedger start $expenseLedger = new \App\ExpenseLedger(); $expenseLedger->bank_id = $bank_id; $expenseLedger->mode = $mode; $expenseLedger->amount = $expensePaidStatus->amount; $expenseLedger->date = date('Y-m-d'); $expenseLedger->session_id = $session->id; $expenseLedger->voucher_id = $id; $expenseLedger->user_id = \Auth::user()->id; $expenseLedger->save(); //ExpenseLedger End //BankStatement Start $bankStatement = new \App\BankStatement(); $bankStatement->bank_id = $bank_id; $series = \App\BankStatement::where('type', 'payment')->orderBy('id', 'desc')->first(); !is_null($series) ? $ser = $series->series + 1 : $ser = 1; $bankStatement->type = 'payment'; $bankStatement->status = 'credit'; $bankStatement->series = $ser; $bankStatement->dealer_id = $expensePaidStatus->dealer_id; $bankStatement->mode = $mode; $bankStatement->particular = 'Expense- ' . getModelById('Expense', $expensePaidStatus->type)->name . ' (' . $expensePaidStatus->remark . ')'; $bankStatement->amount = $expensePaidStatus->amount; $bankStatement->history = null; $bankStatement->transaction_date = date('Y-m-d'); $bankStatement->save(); $expenseLedger->against = $ser; $expenseLedger->save(); //BankStatement End $data['flag'] = true; $data['message'] = "Status changed to paid successfully"; } else { $data['flag'] = false; $data['message'] = "Something went wrong"; } } return response()->json($data); } public function namanExpenseVouchersPaidStatus(Request $request) { $data = array(); $expensePaidStatus = \App\ExpenseVoucher::where('id', $request->id)->first(); if (empty($expensePaidStatus)) { $data['flag'] = false; $data['message'] = "Expense Voucher not found"; } else { $expensePaidStatus->approve_status = $request->status; $expensePaidStatus->approve_date = date('Y-m-d H:i:s'); $expensePaidStatus->approve_user_id = session()->get('user_id'); $expensePaidStatus->approve_remark = $request->remark; $expensePaidStatus->save(); $data['flag'] = true; $data['message'] = "Approved successfully"; } return response()->json($data); } public function directCollection(Request $request) { $party = array(); $party['dealers'] = \App\Dealer::where('is_active', 1)->wherein('id', [1, 30, 31])->get(); $party['retailers'] = \App\Retailer::where('is_active', 1)->get(); if ($request->isMethod('post')) { $response = array(); $validator = \Validator::make( $request->all(), array( 'retailer_id' => 'required', 'dealer_id' => 'required' ) ); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $party['party'] = \App\Retailer::where('id', $request->retailer_id)->first(); $party['dealerid'] = \App\Dealer::where('id', $request->dealer_id)->first(); $total_debit = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->sum('debit'); $total_credit = \App\PartyInvoiceLedger::where('retailer_id', $request->retailer_id)->where('dealer_id', $request->dealer_id)->sum('credit'); $totalSD = \App\SecondaryDiscountPartyLedger::join('retailers', 'retailers.id', 'secondary_discount_party_ledgers.retailer_id') ->select('retailers.name as retailer_name', DB::raw('SUM(secondary_discount_party_ledgers.credit) as totalCredit'), DB::raw('SUM(secondary_discount_party_ledgers.debit) as totalDebit')) ->where('secondary_discount_party_ledgers.retailer_id', $request->retailer_id) ->where('secondary_discount_party_ledgers.dealer_id', $request->dealer_id) ->first(); $totalcredit = $total_credit + $totalSD->totalCredit; $totaldebit = $total_debit + $totalSD->totalDebit; $party['total_credit'] = $totalcredit; $party['total_debit'] = $totaldebit; $total_closing_balance = $totalcredit - $totaldebit; $party['closing_balance'] = abs($total_closing_balance); $party['type'] = ($total_closing_balance > 0) ? 'Credit' : 'Debit'; } } else { } return view('dashboard.invoice.direct_collection', $party); } public function getDirectStatus($retailerId, $dealerId) { $data = []; $data['retailer_id'] = $retailerId; $data['dealer_id'] = $dealerId; $data['user_id'] = \Auth::user()->id; return view('dashboard.invoice.direct_collection_status', $data); } public function setdirectcollection(Request $request) { $response = array(); $validator = \Validator::make( $request->all(), array( //'dealer_id' => 'required' ) ); // dd($this->dateConverter($request->transaction_date)); // dd(\App\BankStatement::where('type','payment')->orderBy('id','desc')->first()); if ($validator->fails()) { $response['flag'] = false; $response['errors'] = $this->parseErrorResponse($validator->getMessageBag()); } else { $dealer_id = $request->dealer; $retailer_id = $request->retailer; $mode = $request->mode; $amount = $request->amount; //$transaction_date = $request->transaction_date; $one = isset($request->one) ? $request->one : "0"; $two = isset($request->two) ? $request->two : "0"; $three = isset($request->three) ? $request->three : "0"; $four = isset($request->four) ? $request->four : "0"; $five = isset($request->five) ? $request->five : "0"; $six = isset($request->six) ? $request->six : "0"; $seven = isset($request->seven) ? $request->seven : "0"; $eight = isset($request->eight) ? $request->eight : "0"; $nine = isset($request->nine) ? $request->nine : "0"; $ten = isset($request->ten) ? $request->ten : "0"; $multipals_cheque_no = '{"2000":"' . $one . '","500":"' . $two . '","200":"' . $three . '","100":"' . $four . '","50":"' . $five . '","20":"' . $six . '","10":"' . $seven . '","5":"' . $eight . '","2":"' . $nine . '","1":"' . $ten . '"}'; // $location = $request->location; $user_id = $request->user_id; $reference_no = $request->reference; $addPayment = new \App\ReceivePayment(); $addPayment->dealer_id = $dealer_id; $addPayment->retailer_id = $retailer_id; $addPayment->mode = $mode; $addPayment->amount = $amount; //$addPayment->transaction_date=$transaction_date; $addPayment->multipals_cheque_no = $multipals_cheque_no; $addPayment->reference_no = $reference_no; // $addPayment->location = $location; $addPayment->user_id = $user_id; if ($addPayment->save()) { $response['flag'] = true; $response['message'] = "Payment Added Successfully !!"; } else { $response['flag'] = false; $response['message'] = "Something Wrong!!"; } } return response()->json($response); } public function generateEinvoice($id) { $invoice = \App\LoadingSlipInvoice::where('id', $id)->first(); $retailer = \App\Retailer::where('id', $invoice->retailer_id)->first(); if($invoice->invoice_active == "0"){ if($retailer->gst_number){ $ip = $_SERVER['REMOTE_ADDR']; $url = "https://api.mastergst.com/einvoice/authenticate?email=vivekkumarshukla218@gmail.com"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $headers = array( "accept: */*", "username:mastergst", "password:Malli#123", "email:vivekkumarshukla218@gmail.com", "ip_address:".$ip, "client_id:34b4e0ac-2ccf-4aea-8ffb-37e3c41d6091", "client_secret:75546e48-5158-45a6-bdcc-2956477a07cc", "gstin:29AABCT1332L000", ); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); //for debug only! curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); $server_output = curl_exec($curl); curl_close($curl); $data = $dataArray = json_decode($server_output, true); $auth = $data['data']['AuthToken']; $invoice = \App\LoadingSlipInvoice::where('id', $id)->first(); $retailer = \App\Retailer::where('id', $invoice->retailer_id)->first(); $dealer = \App\Dealer::where('id', $invoice->dealer_id)->first(); $product = \App\Product::where('id', $invoice->product_id)->first(); $loading = \App\product_loadings::where('invoice_number', $invoice->invoice_number)->first(); $d = date('d/m/Y', strtotime($invoice->invoice_date)); $response = array(); $response['Version'] = "1.1"; $response['TranDtls']['TaxSch'] = "GST"; $response['TranDtls']['SupTyp'] = "B2B"; $response['TranDtls']['RegRev'] = "N"; $response['TranDtls']['IgstOnIntra'] = "N"; $response['DocDtls']['Typ'] = "INV"; $response['DocDtls']['No'] = $invoice->invoice_number; $response['DocDtls']['Dt'] = $d; $response['SellerDtls']['Gstin'] = "29AABCT1332L000"; $response['SellerDtls']['LglNm'] = $dealer->name; $response['SellerDtls']['Addr1'] = $dealer->address1; $response['SellerDtls']['Loc'] = $dealer->address1; $response['SellerDtls']['Pin'] = 560001; $response['SellerDtls']['Stcd'] = "29"; $response['BuyerDtls']['Gstin'] = $retailer->gst_number; $response['BuyerDtls']['Gstin'] = $retailer->gst_number; $response['BuyerDtls']['LglNm'] = $retailer->name; $response['BuyerDtls']['Pos'] = "09"; $response['BuyerDtls']['Addr1'] = $retailer->address; $response['BuyerDtls']['Loc'] = $retailer->address; $response['BuyerDtls']['Pin'] = $retailer->pincode; $response['BuyerDtls']['Stcd'] = "09"; $buyer = $response['BuyerDtls']['Gstin']; $buyergst = substr($buyer, 0, 2); $selar = $response['SellerDtls']['Gstin']; $stategst = substr($selar, 0, 2); $unit = strtoupper($invoice->unit); $rate = $invoice->rate - ($invoice->freight_discount + $invoice->secondary_discount); $rate = $rate * 100 / (100 + $product->cgst + $product->sgst); $total_quantity = $invoice->quantity; $cgst_percentage = $product->cgst; $sgst_percentage = $product->sgst; $total_amount = $rate * $total_quantity; $products_total_amount = $total_amount; $cgst_amount = ($cgst_percentage / 100) * $total_amount; $sgst_amount = ($sgst_percentage / 100) * $total_amount; $total_cgst = $cgst_amount; $total_sgst = $sgst_amount; $total_igst = $total_cgst + $total_sgst; $final_show_amount = $products_total_amount + $total_sgst + $total_cgst; $rate = round($rate,2); $response['ItemList'][] = array( 'SlNo' => "1", 'IsServc' => "N", 'HsnCd' => $invoice->product_hsn, 'Qty' => $invoice->quantity, 'Unit' => $unit, 'UnitPrice' => $rate, 'TotAmt' => round($total_amount,2), 'AssAmt' => round($total_amount,2), 'GstRt' => intval($product->igst) ); if ($stategst == $buyergst) { $response['ItemList'][0]['SgstAmt'] = round($total_sgst,2); $response['ItemList'][0]['CgstAmt'] = round($total_cgst,2); } else { $response['ItemList'][0]['IgstAmt'] = round($total_sgst + $total_cgst,2); } $response['ItemList'][0]['TotItemVal'] = round($final_show_amount,2); $response['ValDtls']['AssVal'] = round($total_amount,2); if ($stategst == $buyergst) { $response['ValDtls']['CgstVal'] = round($total_cgst,2); $response['ValDtls']['SgstVal'] = round($total_sgst,2); } else { $response['ValDtls']['IgstVal'] = round($total_sgst + $total_cgst,2); } $response['ValDtls']['TotInvVal'] = round($final_show_amount,2); $url = "https://api.mastergst.com/einvoice/type/GENERATE/version/V1_03?email=vivekkumarshukla218@gmail.com"; ini_set('serialize_precision', -1); $payload = json_encode($response, JSON_UNESCAPED_SLASHES); $headers = array( 'accept: */*', 'ip_address:' . $ip, 'client_id:34b4e0ac-2ccf-4aea-8ffb-37e3c41d6091', 'client_secret:75546e48-5158-45a6-bdcc-2956477a07cc', 'username:mastergst', 'auth-token:' . $auth, 'gstin:29AABCT1332L000', 'Content-Type: application/json', ); $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POSTFIELDS, $payload); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $server_output = curl_exec($curl); curl_close($curl); $data = json_decode($server_output, true); if($data['status_cd'] == "1") { $invoice = \App\LoadingSlipInvoice::where('id', $id)->first(); $invoice->irn_no = $data['data']['Irn']; $invoice->qr_code = $data['data']['SignedQRCode']; $invoice->AckNo = $data['data']['AckNo']; $invoice->AckDt = $data['data']['AckDt']; $invoice->invoice_active = "1"; if ($stategst == $buyergst){ $invoice->igst = "1"; } $invoice->save(); $datas['flag'] = true; $datas['message'] = "E-Invoice Generated Succesfully"; } else { $datas['flag'] = false; $datas['message'] = "E-Invoice Not Generated"; } }else{ $datas['flag'] = false; $datas['message'] = "Retailer GST Invalid!"; } }else{ $datas['flag'] = false; $datas['message'] = "E-Invoice Already Generated."; } return response()->json($datas); } public function cancelIrn(Request $request) { $detail = $request->remark; $ip = $_SERVER['REMOTE_ADDR']; $url = "https://api.mastergst.com/einvoice/authenticate?email=vivekkumarshukla218@gmail.com"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $headers = array( "accept: */*", "username:mastergst", "password:Malli#123", "email:vivekkumarshukla218@gmail.com", "ip_address:".$ip, "client_id:34b4e0ac-2ccf-4aea-8ffb-37e3c41d6091", "client_secret:75546e48-5158-45a6-bdcc-2956477a07cc", "gstin:29AABCT1332L000", ); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); $server_output = curl_exec($curl); curl_close($curl); $data = $dataArray = json_decode($server_output, true); $auth = $data['data']['AuthToken']; $invoice = \App\LoadingSlipInvoice::where('id', $request->id)->first(); $irn = $invoice->irn_no; $response = array( "Irn" => $irn, "CnlRsn" => "1", "CnlRem" => $detail, ); $irnarry = json_encode($response); $url = "https://api.mastergst.com/einvoice/type/CANCEL/version/V1_03?email=vivekkumarshukla218%40gmail.com"; // ini_set('serialize_precision', -1); // $payload = json_encode($response, JSON_UNESCAPED_SLASHES); $headers = array( 'accept: */*', 'ip_address:' . $ip, 'client_id:34b4e0ac-2ccf-4aea-8ffb-37e3c41d6091', 'client_secret:75546e48-5158-45a6-bdcc-2956477a07cc', 'username: mastergst', 'auth-token:' . $auth, 'gstin:29AABCT1332L000', 'Content-Type: application/json', ); $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POSTFIELDS, $irnarry); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $server_output = curl_exec($curl); curl_close($curl); $data = json_decode($server_output, true); if($data['status_cd'] == "1") { $invoice = \App\LoadingSlipInvoice::where('id', $request->id)->first(); $invoice->cancel_irn = "1"; $invoice->save(); $datas['flag'] = true; $datas['message'] = "E-Invoice Cancellation Succesful."; } else { $datas['flag'] = false; $datas['message'] = "E-Invoice Cancellation faild!"; } return response()->json($datas); } public function cancelEinvoice($id) { $data = []; $data['invoice'] = $id; return view('dashboard.invoice.cancel_irn_status', $data); } public function createEwaybill ($id) { $invoice = \App\LoadingSlipInvoice::where('id', $id)->first(); if(!$invoice->eway_bill_number){ $loading = \App\ProductLoading::where('invoice_number', $invoice->invoice_number)->first(); $retailer = \App\Retailer::where('id', $invoice->retailer_id)->first(); $d = date('d/m/Y', strtotime($invoice->invoice_date)); $ip = $_SERVER['REMOTE_ADDR']; $url = "https://api.mastergst.com/einvoice/authenticate?email=vivekkumarshukla218%40gmail.com"; $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $headers = array( "accept: */*", "username:mastergst", "password:Malli#123", "email:vivekkumarshukla218@gmail.com", "ip_address:".$ip, "client_id:34b4e0ac-2ccf-4aea-8ffb-37e3c41d6091", "client_secret:75546e48-5158-45a6-bdcc-2956477a07cc", "gstin:29AABCT1332L000", ); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); $server_output = curl_exec($curl); curl_close($curl); $data = $dataArray = json_decode($server_output, true); $auth = $data['data']['AuthToken']; $url = "https://api.mastergst.com/einvoice/type/GENERATE_EWAYBILL/version/V1_03?email=vivekkumarshukla218%40gmail.com"; // ini_set('serialize_precision', -1); // $payload = json_encode($response, JSON_UNESCAPED_SLASHES); $retail = $retailer->gst_number; $stategst = substr($retail, 0, 2); $headers = array( 'accept: */*', 'ip_address:' . $ip, 'client_id:34b4e0ac-2ccf-4aea-8ffb-37e3c41d6091', 'client_secret:75546e48-5158-45a6-bdcc-2956477a07cc', 'username:mastergst', 'auth-token:' . $auth, 'gstin:29AABCT1332L000', 'Content-Type: application/json', ); $data = array( "Irn" => $invoice->irn_no, "Distance" => 100, "TransMode" => "1", "TransId" => "29AABCT1332L000", "TransName" => $loading->transporter_name, "TransDocDt" => $d, "VehNo" => $loading->truck_number , "VehType" => "R", "DispDtls" => array( "Nm" => $retailer->name, "Addr1" => $retailer->address, "Loc" => $retailer->address, "Pin" => $retailer->pincode, "Stcd" => $stategst ) ); // Convert the array to a JSON string $response = json_encode($data, JSON_PRETTY_PRINT); $curl = curl_init($url); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POSTFIELDS, $response); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $server_output = curl_exec($curl); curl_close($curl); $data = json_decode($server_output, true); if($data['status_cd'] == "1") { $invoice = \App\LoadingSlipInvoice::where('id', $id)->first(); $invoice->eway_bill_number = $data['data']['EwbNo']; $invoice->eway_bill_date = $data['data']['EwbDt']; $invoice->eway_bill_ValidTill = $data['data']['EwbValidTill']; $invoice->save(); $datas['flag'] = true; $datas['message'] = "E-WAy Bill Generated Succesfully."; } else { $datas['flag'] = false; $datas['message'] = "E-WAy Bill Not Generate!"; } }else { $datas['flag'] = false; $datas['message'] = "E-WAy Bill Already Generated."; } return response()->json($datas); } }