One Hat Cyber Team
Your IP :
3.23.128.248
Server IP :
104.21.16.1
Server :
Linux agrigation-prod 5.15.0-67-generic #74-Ubuntu SMP Wed Feb 22 14:14:39 UTC 2023 x86_64
Server Software :
nginx/1.24.0
PHP Version :
7.4.33
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
home
/
forge
/
app.gftag.com
/
app
/
Http
/
Controllers
/
Edit File:
PurchaseOrderController.php
<?php namespace App\Http\Controllers; use PDF; use App\Models\Item; use App\Models\Supplier; use Illuminate\Http\Request; use App\Models\PurchaseOrder; use App\Models\PurchaseOrderItem; use App\Models\CashBookTransaction; use Illuminate\Support\Facades\View; use App\Services\OrganisationService; class PurchaseOrderController extends Controller { public function getPoGroups() { $group = [ 'FM' => 'Farm Machinery', 'FI' => 'Farm Input', 'FP' => 'Farm Produce', 'AP' => 'Allied-Agri Produce', 'PE' => 'Processed Eatable', 'AT' => 'Agri - Tech', 'AS' => 'Agri - Services', 'AO' => 'Any Other' ]; return $group; } public function purchaseOrderView(Request $request) { $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); if ($request->ajax()) { $filter_purchase_orders = $this->filterPurchaseOrder($request, $org_id); return view('purchaseorder.partials.filtered-table-body-data', ['request' => $request], compact('filter_purchase_orders'))->render(); } else { $purchaseOrders = PurchaseOrder::where('organisation_id', $org_id)->orderBy('po_date', 'DESC')->paginate(10); foreach ($purchaseOrders as $po) { $po->expense = CashBookTransaction::where('type', 'expense')->where('reference_id', $po->id)->sum('amount'); } return view('purchaseorder.index', ['purchaseorders' => $purchaseOrders]); } } public function filterPurchaseOrder($request, $org_id) { $data = $request->all(); $text_search = isset($data['text_search']) ? $data['text_search'] : ''; $limit = isset($data['records']) ? $data['records'] : ''; $purchase_order_query = PurchaseOrder::when(isset($text_search) && $text_search != '', function ($query) use ($text_search) { $query->whereHas('supplier', function ($sub_query) use ($text_search) { $sub_query->where('name', 'like', '%' . $text_search . '%'); }); $query->orWhereHas('purchaseOrderItem.item', function ($sub_query) use ($text_search) { $sub_query->where('name', 'like', '%' . $text_search . '%'); }); })->where('organisation_id', $org_id)->orderBy('po_date', 'DESC'); $purchaseOrders = $purchase_order_query->paginate($limit); foreach ($purchaseOrders as $po) { $po->expense = CashBookTransaction::where('type', 'expense')->where('reference_id', $po->id)->sum('amount'); } return $purchaseOrders; } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function createPurchaseOrder() { if (View::exists('purchaseorder.create')) { // get all supplier under the same organisation $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); // get all purchase order along supplier details // $suppliers = Supplier::where('organisation_id', $org_id)->get(); $items = Item::where('organisation_id', $org_id)->get(); $pono = PurchaseOrder::select('po_id')->where('organisation_id', $org_id)->orderBy('created_at', 'desc')->first(); if (!isset($pono)) { $pono = 1; } else { $pono = (int)$pono->po_id + 1; } $groups = $this->getPoGroups(); return view('purchaseorder.create', ['suppliers' => $suppliers, 'items' => $items, 'pono' => $pono, 'groups' => $groups]); } } public function getSupplierItem(Request $request) { $org_id = Supplier::find($request->sid)->organisation->id; $items = Item::where('organisation_id', $org_id)->where('type', 'product')->get(); return json_encode($items); } public function allocate(Request $request) { $pitemid = $request->get('pitemid'); $totqty = $request->get('totqty'); $arr = array(); $arr = $request->get('tbdata'); $purchaseorderitem = new PurchaseOrderItem(); $purchaseorderitem->remove_item_from_allotment($pitemid); foreach ($arr as $data) { $alloc = $purchaseorderitem->allocate_data($pitemid, $data['cluster'], $data['hub'], $data['qty']); } $piorder = PurchaseOrderItem::whereId($pitemid)->first(); $piorder->quantity = $totqty; $piorder->save(); $status = [ "stat" => "Success", "message" => "Allocation done successfully!!!" ]; return json_encode($status); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->validate($request, [ 'pono' => 'required', 'podate' => 'required', 'supplier' => 'required', 'po_prefix' => 'required', 'po_description' => 'required', 'item_name' => 'required|array|min:1', 'item_name.*' => 'required|integer|min:1', 'item_price' => 'required|array|min:1', 'item_price.*' => 'required|numeric|min:0', 'item_unit' => 'required|array|min:1', 'item_unit.*' => 'required|string|max:50', 'item_quantity' => 'required|array|min:1', 'item_quantity.*' => 'required|numeric|min:1', ], [ 'item_name.required' => 'The item name field is required.', 'item_name.array' => 'The item name must be an array.', 'item_name.min' => 'At least one item name must be provided.', 'item_name.*.required' => 'Each item name is required.', 'item_name.*.integer' => 'Each item name must be an integer.', 'item_name.*.min' => 'Each item name must be greater than 0.', 'item_price.required' => 'The item price field is required.', 'item_price.array' => 'The item price must be an array.', 'item_price.min' => 'At least one item price must be provided.', 'item_price.*.required' => 'Each item price is required.', 'item_price.*.numeric' => 'Each item price must be a number.', 'item_price.*.min' => 'Each item price must be at least 0.', 'item_unit.required' => 'The item unit field is required.', 'item_unit.array' => 'The item unit must be an array.', 'item_unit.min' => 'At least one item unit must be provided.', 'item_unit.*.required' => 'Each item unit is required.', 'item_unit.*.string' => 'Each item unit must be a string.', 'item_unit.*.max' => 'Each item unit may not be greater than 50 characters.', 'item_quantity.required' => 'The item quantity field is required.', 'item_quantity.array' => 'The item quantity must be an array.', 'item_quantity.min' => 'At least one item quantity must be provided.', 'item_quantity.*.required' => 'Each item quantity is required.', 'item_quantity.*.numeric' => 'Each item quantity must be a number.', 'item_quantity.*.min' => 'Each item quantity must be at least 0.', ]); $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); $all_data = $request->all(); $pono = $all_data['pono']; $podate = $all_data['podate']; $supplier = $all_data['supplier']; $po_prefix = $all_data['po_prefix']; $po_description = $all_data['po_description']; $po = PurchaseOrder::create([ "po_id" => $pono, 'organisation_id' => $org_id, "po_date" => $podate, "supplier_id" => $supplier, "status" => 'confirmed', "po_prefix" => $po_prefix, "po_description" => $po_description ]); $arr_item_code = array(); $arr_item_qty = array(); $arr_item_code = $request->get('item_name'); $arr_item_price = $request->get('item_price'); $arr_item_unit = $request->get('item_unit'); $arr_item_qty = $request->get('item_quantity'); $amount = 0; for ($i = 0; $i < count($arr_item_code); $i++) { $item = Item::find($arr_item_code[$i]); $qty = $arr_item_qty[$i] == "" ? "0" : $arr_item_qty[$i]; $price = $qty * $arr_item_price[$i]; $gst = ($po->organisation->state_id == $po->supplier->state_id) ? ($item->sgst_rate + $item->cgst_rate) : $item->igst_rate; $gst_amount = round(($price * $gst) / 100); $amount = $amount + ($price + $gst_amount); PurchaseOrderItem::create([ "purchase_order_id" => $po->id, "organisation_id" => $org_id, "item_id" => $arr_item_code[$i], "price" => $arr_item_price[$i], "unit" => $arr_item_unit[$i], "quantity" => $arr_item_qty[$i] == "" ? "0" : $arr_item_qty[$i], "status" => 'pending' ]); } $po->amount = $amount; $po->save(); if ($po) { return redirect()->back()->with('message', 'Purchase Order created successfully !'); } else { return redirect()->back()->with('message', 'Purchase Order cannot be created !'); } } public function editPurchaseOrder($id) { $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); $pos = PurchaseOrder::where('id', $id)->where('organisation_id', $org_id)->first(); if (!$pos) { abort('404'); } $status = 0; $purchaseorderitems = PurchaseOrderItem::where('purchase_order_id', $id)->where('organisation_id', $org_id)->get(); foreach ($purchaseorderitems as $purchaseorderitem) { if ($purchaseorderitem->status == 'confirmed') { $status = 1; } } if ($status) { return redirect()->back()->with('message', 'Purchase Order Cannot Be Edited Some Items Are Already Accepted !'); } $pois = $purchaseorderitems; $items = []; $groups = $this->getPoGroups(); return view('purchaseorder.edit')->with('pos', $pos)->with('pois', $pois)->with('items', $items)->with('groups', $groups); } public function update(Request $request) { $this->validate($request, [ 'poid' => 'required', 'pono' => 'required', 'supplier' => 'required', 'podate' => 'required', 'supplier' => 'required', 'pur_item_id' => 'required', 'item_name' => 'required', 'item_quantity' => 'required', ]); $organisationService = new OrganisationService(); $org_id = $organisationService->getOrganisationByUser(); $all_data = $request->all(); $po_id = $all_data['poid']; $pono = $all_data['pono']; $podate = $all_data['podate']; $supplier = $all_data['supplier']; $po_prefix = $all_data['po_prefix']; $po_description = $all_data['po_description']; $arr_item_id = array(); $arr_item_price = array(); $arr_item_unit = array(); $arr_item_qty = array(); $arr_pur_item_id = array(); $arr_item_id = $request->get('item_name'); $arr_item_price = $request->get('item_price'); $arr_item_unit = $request->get('item_unit'); $arr_item_qty = $request->get('item_quantity'); $arr_pur_item_id = $request->get('pur_item_id'); $porder = PurchaseOrder::whereId($po_id)->first(); $porder->po_date = $podate; $porder->supplier_id = $supplier; $porder->po_prefix = $po_prefix; $porder->po_description = $po_description; $porder->save(); $amount = 0; for ($i = 0; $i < count($arr_item_id); $i++) { $item = Item::find($arr_item_id[$i]); $qty = $arr_item_qty[$i] == "" ? "0" : $arr_item_qty[$i]; $price = $qty * $arr_item_price[$i]; $gst = ($porder->organisation->state_id == $porder->supplier->state_id) ? ($item->sgst_rate + $item->cgst_rate) : $item->igst_rate; $gst_amount = round(($price * $gst) / 100); $amount = $amount + ($price + $gst_amount); if ($arr_pur_item_id[$i] == 0) { PurchaseOrderItem::create([ "purchase_order_id" => $po_id, "organisation_id" => $org_id, "item_id" => $arr_item_id[$i], "price" => $arr_item_price[$i], "unit" => $arr_item_unit[$i], "status" => 'pending', "quantity" => $arr_item_qty[$i] == "" ? 0 : $arr_item_qty[$i] ]); } else { // update the quantity only $porderitem = PurchaseOrderItem::whereId($arr_pur_item_id[$i])->first(); $porderitem->price = $arr_item_price[$i]; $porderitem->unit = $arr_item_unit[$i]; $porderitem->quantity = $arr_item_qty[$i] == "" ? "0" : $arr_item_qty[$i]; $porderitem->save(); } } $porder->amount = $amount; $porder->save(); return redirect()->back()->with('message', ' Purchase Orders Item updated successfully !'); } public function removeitem(Request $request) { $poid = new PurchaseOrderItem(); $poiid = $request->get('poiid'); $stat = $request->get('stat'); $deletedRowspoi = PurchaseOrderItem::where('id', $poiid)->delete(); if ($stat == "Confirmed") { $poid->remove_item_from_allotment($poiid); } } public function printPo($id) { $data = array(); $purchaseorder = PurchaseOrder::findOrFail($id); $purchaseorderitems = PurchaseOrderItem::with('item')->where('purchase_order_id', $purchaseorder->id)->get(); // $purchase_item_location = []; // foreach($purchaseorderitems as $key => $item){ // $purchase_item_location[$key]['item'] = $item; // $purchase_item_location[$key]['item_allocation'] = $purchase_item_allocation_details; // } // $supplier = new Supplier(); // $suppliers = $supplier->get_supplier_info($pos[0]->sid); $data['po_details'] = $purchaseorder; $data['po_items'] = $purchaseorderitems; $data['po_supplier'] = $purchaseorder->supplier; // $data['po_item_alloaction'] = $purchase_item_location; $pdf = PDF::loadView('purchaseorder.print', ['data' => $data]); return $pdf->stream(); // return view('purchaseorder.print')->with('data', $data); } }
Simpan