EVOLUTION-NINJA
Edit File: Project.php
<?php namespace App\Controllers; use App\Models\DealerModel; use App\Models\NewExpenditureModel; class Project extends BaseController{ public function __construct() { $this->db = \Config\Database::connect(); date_default_timezone_set('Asia/Kolkata'); } public function create_project() { $dealerModel = new DealerModel(); $data = [ 'dealer_code' => 'DLR' . rand(1000,9999), 'name' => $this->request->getPost('name'), 'mobile' => $this->request->getPost('mobile'), 'email' => $this->request->getPost('email'), 'address' => $this->request->getPost('address'), 'tan_no' => $this->request->getPost('tan_no'), 'created_at' => date('Y-m-d H:i:s') ]; if ($dealerModel->insert($data)) { return redirect()->to(base_url('dealer_list')) ->with('msg', 'Dealer Added Successfully'); } else { return redirect()->to(base_url('dealer_list')) ->with('msg', 'Failed to Add Dealer'); } } public function dealerlist() { $dealerModel = new DealerModel(); $name = $this->request->getGet('name'); $mobile = $this->request->getGet('mobile'); $email = $this->request->getGet('email'); $builder = $dealerModel->builder(); if (!empty($name)) { $builder->like('name', $name); } if (!empty($mobile)) { $builder->like('mobile', $mobile); } if (!empty($email)) { $builder->like('email', $email); } $builder->orderBy('id', 'DESC'); $data['dealers'] = $builder->get()->getResultArray(); return view('createproject/dealerlist', $data); } public function dealer_delete($id) { $dealerModel = new DealerModel(); $dealerModel->delete($id); return redirect()->to(base_url('dealer_list')) ->with('msg', 'Dealer Deleted Successfully'); } public function dealer_edit($id) { $model = new DealerModel(); // FIXED $data['dealer'] = $model->find($id); return view('createproject/dealer_edit', $data); } public function dealer_update($id) { $model = new DealerModel(); // FIXED $model->update($id, [ 'dealer_code' => $this->request->getPost('dealer_code'), 'name' => $this->request->getPost('name'), 'mobile' => $this->request->getPost('mobile'), 'email' => $this->request->getPost('email'), 'address' => $this->request->getPost('address'), 'tan_no' => $this->request->getPost('tan_no'), ]); return redirect()->to(base_url('dealer_list')) ->with('success','Updated Successfully'); } // ✅ Aadhaar 5 Years Validation public function create_customer_submit() { helper(['form']); // Validation Rules $rules = [ 'farmer_name' => 'required|min_length[3]|alpha_space', 'taluk' => 'required', 'address' => 'required', 'dealer_id' => 'required', 'category' => 'required', 'phone_no' => 'required|numeric|exact_length[10]', 'aadhar_no' => 'permit_empty|numeric|exact_length[12]', 'fid_no' => 'permit_empty|max_length[50]', 'unique_code' => 'permit_empty|max_length[50]', 'engine_no' => 'permit_empty|max_length[100]', 'chasis_no' => 'permit_empty|max_length[100]', 'utr_no' => 'permit_empty|max_length[100]', 'photo' => 'permit_empty|is_image[photo]|max_size[photo,2048]|mime_in[photo,image/jpg,image/jpeg,image/png]', 'invoice_file' => 'permit_empty|max_size[invoice_file,4096]|ext_in[invoice_file,pdf,jpg,jpeg,png]' ]; // Validate if (!$this->validate($rules)) { return $this->response->setJSON([ 'result' => 0, 'message' => $this->validator->listErrors() ]); } $aadhar_no = trim($this->request->getPost('aadhar_no')); if (!empty($aadhar_no)) { $existingCustomer = $this->db->table('create_customer') ->where('aadhar_no', $aadhar_no) ->orderBy('id', 'DESC') ->get() ->getRowArray(); // Aadhaar already exists if ($existingCustomer) { // Check created_at exists if (!empty($existingCustomer['created_at'])) { $oldDate = strtotime($existingCustomer['created_at']); // Next eligible date after 5 years $nextEligibleDate = strtotime('+5 years', $oldDate); // Current timestamp $today = time(); // Still within 5 years if ($today < $nextEligibleDate) { return $this->response->setJSON([ 'result' => 0, 'message' => 'This Aadhaar number already purchased a product. Eligible again after 5 years.' ]); } } else { // If old record has no created_at return $this->response->setJSON([ 'result' => 0, 'message' => 'This Aadhaar number already exists.' ]); } } } $dealer_id = $this->request->getPost('dealer_id'); $dealer = $this->db->table('dealers') ->where('id', $dealer_id) ->get() ->getRowArray(); if (!$dealer) { return $this->response->setJSON([ 'result' => 0, 'message' => 'Dealer not found' ]); } $dealer_code = $dealer['dealer_code']; $dealer_name = $dealer['name']; // Month + Year $date_part = date('my'); // Get last farmer_id $builder = $this->db->table('create_customer'); $builder->like('farmer_id', $dealer_code . $date_part, 'after'); $builder->orderBy('id', 'DESC'); $last = $builder->get()->getRowArray(); if ($last) { $last_id = $last['farmer_id']; $number = (int) substr($last_id, -3); $number++; } else { $number = 1; } // Generate 001,002... $auto_number = str_pad($number, 3, '0', STR_PAD_LEFT); // Final Farmer ID $farmer_id = $dealer_code . $date_part . $auto_number; // Upload Photo $photoName = null; $photoFile = $this->request->getFile('photo'); if ($photoFile && $photoFile->isValid() && !$photoFile->hasMoved()) { $photoName = time() . '_' . $photoFile->getRandomName(); $photoFile->move(ROOTPATH . 'public/uploads/customer_photos', $photoName); } // Upload Invoice $invoiceName = null; $invoiceFile = $this->request->getFile('invoice_file'); if ($invoiceFile && $invoiceFile->isValid() && !$invoiceFile->hasMoved()) { $invoiceName = time() . '_' . $invoiceFile->getRandomName(); $invoiceFile->move( ROOTPATH . 'public/uploads/invoices', $invoiceName ); } // Save Data $data = [ 'farmer_id' => $farmer_id, 'farmer_name' => $this->request->getPost('farmer_name'), 'share_amount'=> $this->request->getPost('share_amount'), 'taluk' => $this->request->getPost('taluk'), 'address' => $this->request->getPost('address'), // ==================================== // DEALER // ==================================== 'dealer_id' => $dealer_code, 'dealer_name' => $dealer_name, // ==================================== // CUSTOMER DETAILS // ==================================== 'category' => $this->request->getPost('category'), 'phone_no' => $this->request->getPost('phone_no'), 'fid_no' => $this->request->getPost('fid_no'), 'unique_code' => $this->request->getPost('unique_code'), 'engine_no' => $this->request->getPost('engine_no'), 'chasis_no' => $this->request->getPost('chasis_no'), 'utr_no' => $this->request->getPost('utr_no'), 'aadhar_no' => $this->request->getPost('aadhar_no'), 'photo' => $photoName, 'invoice_file' => $invoiceName, // ==================================== // PRODUCT DETAILS // ==================================== 'product_type' => $this->request->getPost('product_type'), 'product_id' => $this->request->getPost('product_id'), 'rc_price' => $this->request->getPost('rc_price'), 'gst_percent' => $this->request->getPost('gst_percent'), 'basic_price' => $this->request->getPost('basic_price'), // ==================================== // SYSTEM // ==================================== 'created_at' => date('Y-m-d H:i:s'), 'created_by' => session()->get('username'), ]; $insert = $this->db->table('create_customer')->insert($data); if ($insert) { return $this->response->setJSON([ 'result' => 1, 'message' => 'Customer created successfully', 'farmer_id' => $farmer_id ]); } else { return $this->response->setJSON([ 'result' => 0, 'message' => 'Failed to create customer' ]); } } // public function project_list_data() { // $farmer = $this->request->getPost('farmer_name'); // $taluk = $this->request->getPost('taluk'); // $dealer = $this->request->getPost('dealer_name'); // $builder = $this->db->table('create_customer'); // if (!empty($farmer)) { // $builder->where('farmer_name', $farmer); // } // if (!empty($taluk)) { // $builder->where('taluk', $taluk); // } // if (!empty($dealer)) { // $builder->where('dealer_name', $dealer); // } // $query = $builder->get(); // return $this->response->setJSON($query->getResult()); // } public function project_list_data() { $farmer = $this->request->getPost('farmer_name'); $taluk = $this->request->getPost('taluk'); $dealer = $this->request->getPost('dealer_name'); // ===================================== // TABLE // ===================================== $builder = $this->db->table('create_customer c'); // ===================================== // JOIN PRODUCTS TABLE // ===================================== $builder->join( 'products p', 'p.product_id = c.product_id', 'left' ); // ===================================== // SELECT // ===================================== $builder->select(' c.id, c.farmer_name, c.farmer_id, c.address, c.taluk, c.dealer_name, c.category, c.phone_no, c.fid_no, c.engine_no, c.chasis_no, c.paid_amount, c.payment_type, c.aadhar_no, c.upi, c.product_type, p.model_name AS product_model, c.rc_price, c.gst_percent, c.basic_price, c.invoice_file '); // ===================================== // FILTERS // ===================================== if (!empty($farmer)) { $builder->where( 'c.farmer_name', $farmer ); } if (!empty($taluk)) { $builder->where( 'c.taluk', $taluk ); } if (!empty($dealer)) { $builder->where( 'c.dealer_name', $dealer ); } $query = $builder->get(); return $this->response->setJSON( $query->getResult() ); } public function get_filters() { // Farmer Names $farmer = $this->db->table('create_customer') ->select('farmer_name') ->distinct() ->where('farmer_name IS NOT NULL') ->get() ->getResult(); // Taluk $taluk = $this->db->table('create_customer') ->select('taluk') ->distinct() ->where('taluk IS NOT NULL') ->get() ->getResult(); // Dealer $dealer = $this->db->table('create_customer') ->select('dealer_name') ->distinct() ->where('dealer_name IS NOT NULL') ->get() ->getResult(); return $this->response->setJSON([ 'farmer' => $farmer, 'taluk' => $taluk, 'dealer' => $dealer ]); } public function project_delete($id) { $this->db->table('create_customer')->delete(['id' => $id]); return $this->response->setJSON(['status'=>1]); } public function project_edit($id) { $model = new \App\Models\CreateCustomerModel(); $project = $model->find($id); if (!$project) { return redirect()->to('/projectlist')->with('error', 'Project not found'); } // ✅ FETCH DEALERS $dealerBuilder = $this->db->table('dealers'); $dealers = $dealerBuilder->get()->getResultArray(); $data = [ 'project' => $project, 'dealers' => $dealers, // ✅ ADD THIS LINE ]; return view('createproject/customer_edit', $data); } public function project_update() { $model = new \App\Models\CreateCustomerModel(); $id = $this->request->getPost('id'); // Validation $validation = \Config\Services::validation(); $rules = [ 'farmer_name' => 'required', 'taluk' => 'required', 'address' => 'required', 'phone_no' => 'required|numeric|min_length[10]|max_length[10]', 'aadhar_no' => 'permit_empty|numeric|min_length[12]|max_length[12]', 'photo' => 'permit_empty|max_size[photo,2048]|is_image[photo]', 'invoice_file' => 'permit_empty|max_size[invoice_file,4096]|ext_in[invoice_file,pdf,jpg,jpeg,png]' ]; // remove uploaded rule if no file selected // REMOVE PHOTO VALIDATION if (empty($_FILES['photo']['name'])) { unset($rules['photo']); } // REMOVE INVOICE VALIDATION if (empty($_FILES['invoice_file']['name'])) { unset($rules['invoice_file']); } if (!$this->validate($rules)) { return $this->response->setJSON([ 'result' => 0, 'message' => $validation->listErrors() ]); } $data = [ 'farmer_name' => $this->request->getPost('farmer_name'), 'taluk' => $this->request->getPost('taluk'), 'address' => $this->request->getPost('address'), 'dealer_name' => $this->request->getPost('dealer_name'), 'category' => $this->request->getPost('category'), 'fid_no' => $this->request->getPost('fid_no'), 'unique_code' => $this->request->getPost('unique_code'), 'phone_no' => $this->request->getPost('phone_no'), 'engine_no' => $this->request->getPost('engine_no'), 'utr_no' => $this->request->getPost('utr_no'), 'chasis_no' => $this->request->getPost('chasis_no'), 'farmer_id' => $this->request->getPost('farmer_id'), 'aadhar_no' => $this->request->getPost('aadhar_no'), // ========================= // PRODUCT DETAILS // ========================= 'product_type' => $this->request->getPost('product_type'), 'product_id' => $this->request->getPost('product_id'), 'rc_price' => $this->request->getPost('rc_price'), 'gst_percent' => $this->request->getPost('gst_percent'), 'basic_price' => $this->request->getPost('basic_price') ]; // Upload photo $photo = $this->request->getFile('photo'); if ($photo && $photo->isValid() && !$photo->hasMoved()) { $newName = time() . '_' . $photo->getRandomName(); $photo->move(ROOTPATH . 'public/uploads/customer_photos', $newName); $data['photo'] = $newName; } // =================================== // UPLOAD INVOICE // =================================== // =================================== // UPLOAD INVOICE // =================================== $invoice = $this->request->getFile('invoice_file'); if ($invoice && $invoice->isValid() && !$invoice->hasMoved()) { $invoiceName = time().'_'.$invoice->getRandomName(); $invoice->move( ROOTPATH . 'public/uploads/invoices', $invoiceName ); $data['invoice_file'] = $invoiceName; } $update = $model->update($id, $data); if ($update) { return $this->response->setJSON([ 'result' => 1, 'message' => 'Customer Updated Successfully' ]); } else { return $this->response->setJSON([ 'result' => 0, 'message' => 'Failed to Update' ]); } } public function create_customer() { $builder = $this->db->table('dealers'); $query = $builder->get(); $data['dealers'] = $query->getResultArray(); // send to view return view('createproject/create_customer', $data); } public function getDealerCode() { $dealer_id = $this->request->getPost('dealer_id'); $dealer = $this->db->table('dealers') ->where('id', $dealer_id) ->get() ->getRowArray(); if ($dealer) { return $this->response->setJSON([ 'status' => 1, 'dealer_code' => $dealer['dealer_code'] ]); } return $this->response->setJSON(['status' => 0]); } public function project_payment_edit($id) { $model = new \App\Models\CreateCustomerModel(); // Customer data $customer = $model->find($id); if (!$customer) { return redirect()->to('/customer-list') ->with('error', 'Customer not found'); } // Database connection $db = \Config\Database::connect(); // Fetch farmer_share from ae_transactions $transaction = $db->table('ae_transactions') ->select('farmer_share') ->where('customer_id', $id) ->get() ->getRowArray(); // Set paid_amount $customer['paid_amount'] = $transaction['farmer_share'] ?? 0; $data['customer'] = $customer; return view('createproject/project_payment_edit', $data); } public function save_payment() { $model = new \App\Models\CreateCustomerModel(); $id = $this->request->getPost('id'); $file = $this->request->getFile('upload'); $data = [ 'paid_amount' => $this->request->getPost('paid_amount'), 'payment_type' => $this->request->getPost('payment_type'), 'upi' => $this->request->getPost('upi'), // Saved to create_customer table 'date' => $this->request->getPost('payment_date'), // Saved to create_customer table ]; // File upload logic if ($file && $file->isValid() && !$file->hasMoved()) { $fileName = $file->getRandomName(); $file->move('public/payment_files', $fileName); $data['payment_file'] = $fileName; } // This updates the 'create_customer' table because that's what the model is linked to if ($model->update($id, $data)) { return redirect()->to('/customer-list')->with('msg', 'Payment Updated Successfully'); } else { return redirect()->back()->with('msg', 'Failed to update payment'); } } public function getNextFarmerId() { $dealer_id = $this->request->getPost('dealer_id'); $dealer = $this->db->table('dealers') ->where('id', $dealer_id) ->get() ->getRowArray(); if (!$dealer) { return $this->response->setJSON(['status' => 0]); } $dealer_code = $dealer['dealer_code']; $date_part = date('my'); $builder = $this->db->table('create_customer'); $builder->like('farmer_id', $dealer_code . $date_part, 'after'); $builder->orderBy('id', 'DESC'); $last = $builder->get()->getRowArray(); if ($last) { $number = (int) substr($last['farmer_id'], -3); $number++; } else { $number = 1; } $auto_number = str_pad($number, 3, '0', STR_PAD_LEFT); $farmer_id = $dealer_code . $date_part . $auto_number; return $this->response->setJSON([ 'status' => 1, 'farmer_id' => $farmer_id ]); } public function getUniqueCode() { $builder = $this->db->table('create_customer'); $builder->select('unique_code'); $builder->orderBy('id', 'DESC'); $last = $builder->get()->getRowArray(); if ($last && !empty($last['unique_code'])) { // Extract number $number = (int) preg_replace('/[^0-9]/', '', $last['unique_code']); $number++; } else { $number = 1; } // Format like UC001 $unique_code = 'UC' . str_pad($number, 3, '0', STR_PAD_LEFT); return $this->response->setJSON([ 'status' => 1, 'unique_code' => $unique_code ]); } public function payment($id) { $db = \Config\Database::connect(); // customer details $customerModel = $db->table('create_customer'); $customer = $customerModel->where('id', $id)->get()->getRowArray(); // fetch farmer_share from ae_transactions $transaction = $db->table('ae_transactions') ->select('farmer_share') ->where('customer_id', $id) ->get() ->getRowArray(); // assign farmer_share to paid_amount $customer['paid_amount'] = $transaction['farmer_share'] ?? 0; return view('payment/projectPayment', ['customer' => $customer]); } public function viewInvoice($id) { $model = new \App\Models\CreateCustomerModel(); $data['customer'] = $model ->where('id', $id) ->first(); return view('invoice/view_invoice', $data); } // public function invoice($id) // { // $db = \Config\Database::connect(); // $builder = $db->table('create_customer'); // $builder->select(' // create_customer.*, // dealers.name as dealer_name, // dealers.address as dealer_address, // dealers.mobile as dealer_mobile, // dealers.gst_percent, // dealers.district // '); // $builder->join( // 'dealers', // 'dealers.dealer_code = create_customer.dealer_id', // 'left' // ); // $builder->where('create_customer.id', $id); // $query = $builder->get(); // $data['customer'] = $query->getRowArray(); // return view('invoice/view_invoice', $data); // } }?>