<?php
 namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Validation\Rule; use Session; use Validator; use Redirect; use File; use View; class Production_formula extends Setup { public function __construct() { parent::__construct(); } public function view(Request $request) { if(check_user_access('view_production') === FALSE){ Redirect::to('/')->send(); } $branch_id = active_branch_id(); $extractions = DB::table('new_production_formula')->where('branch',$branch_id); $extractions = $extractions->get(); $extractions = json_decode(json_encode($extractions),true); $this->data["pageData"] = $extractions; $this->data["items"] = set_array_keys(fetch_table_branch('items'),'uid'); $this->data["title"] = print_lang($this->data["app_lang"],'mod_production_new_formula_view_title'); $this->data["pageToLoad"] = "new_production_formula/view.php"; $this->data["activeLink"] = "new_production_formula"; $this->load_view($this->data); } public function add(Request $request) { if(check_user_access('add_production') === FALSE){ Redirect::to('/')->send(); } $this->data["items"] = fetch_table_branch('items'); $this->data["title"] = print_lang($this->data["app_lang"],'mod_production_new_formula_add_title'); $this->data["pageToLoad"] = "new_production_formula/add.php"; $this->data["activeLink"] = "new_production_formula"; $this->load_view($this->data); } public function get_items_data(Request $request) { $this->data["item"] = $request->item; $this->data["percentage"] = $request->percentage; $this->data["items"] = fetch_table_branch('items'); $item_data = (string)View::make('user.new_production_formula.get_form_appended_data',$this->data); echo json_encode(array('status' => 'success', 'html' => $item_data)); } public function save(Request $request) { $active_branch_id = active_branch_id(); $items = []; if(!empty($request->item)) { foreach($request->item as $key => $row) { $items[] = ['item' => $request->item[$key], 'percentage' => $request->percentage[$key]]; } } $uid = getUniqueId('PDRL', 'new_production_formula', 'uid'); $db_array = array( 'uid' => $uid, 'branch' => $active_branch_id, 'name' => $request->name, 'items' => json_encode($items), ); insert_table('new_production_formula',$db_array); echo json_encode(array('status' => 'success', 'mood' => 'new', 'message_head' => print_lang($this->data["app_lang"],'mod_production_new_formula_added'), 'message_body' => print_lang($this->data["app_lang"],'mod_production_new_formula_save_successfully'))); } public function edit(Request $request, $uid) { if(check_user_access('edit_production') === FALSE){ Redirect::to('/')->send(); } $this->data["editing_data"] = fetch_table_where('new_production_formula','uid',$uid,true); if(!is_array($this->data["editing_data"])){ Redirect::to('/purchases')->send(); } $this->data["items"] = set_array_keys(fetch_table_branch('items'),'uid'); $this->data["title"] = print_lang($this->data["app_lang"],'mod_production_new_formula_edit_title'); $this->data["pageToLoad"] = "new_production_formula/edit.php"; $this->data["activeLink"] = "new_production_formula"; $this->load_view($this->data); } public function update(Request $request, $uid) { $active_branch_id = active_branch_id(); $items = []; if(!empty($request->item)) { foreach($request->item as $key => $row) { $items[] = ['item' => $request->item[$key], 'percentage' => $request->percentage[$key]]; } } $db_array = array( 'name' => $request->name, 'items' => json_encode($items), ); update_table('new_production_formula','uid',$uid,$db_array); echo json_encode(array('status' => 'success', 'mood' => 'update', 'message_head' => print_lang($this->data["app_lang"],'mod_production_new_formula_update'), 'message_body' => print_lang($this->data["app_lang"],'mod_production_new_formula_update_successfully'))); } public function delete($uid) { if(check_user_access('delete_production') === FALSE){ Redirect::to('/')->send(); } delete_table('new_production_formula','uid',$uid); echo json_encode(array('status' => 'success', 'mode' => 'deleted')); } }