CHANGE_jobcard_module:ss

This commit is contained in:
sriramv 2024-04-27 12:45:57 +05:30
parent 4b4ecd8ce9
commit 3005633ef8
8 changed files with 168 additions and 96 deletions

View File

@ -3,8 +3,10 @@
namespace App\Controllers; namespace App\Controllers;
use App\Models\JobcardModel; use App\Models\JobcardModel;
use App\Models\JobcardComplaintModel;
use App\Models\JobcardCustomComplaintModel; use App\Models\JobcardCustomComplaintModel;
use App\Models\JobcardServiceModel; use App\Models\JobcardServiceModel;
use App\Models\JobcardProductModel;
use App\Models\ProductModel; use App\Models\ProductModel;
use App\Models\ServiceModel; use App\Models\ServiceModel;
use App\Models\ClientModel; use App\Models\ClientModel;
@ -34,7 +36,6 @@ class Jobcard extends BaseController
$JobcardModel = new JobcardModel(); $JobcardModel = new JobcardModel();
$jobs=$JobcardModel->getJobCardsWithProductsAndService($this->session->get('logged_user_branch_id')); $jobs=$JobcardModel->getJobCardsWithProductsAndService($this->session->get('logged_user_branch_id'));
$data['jobs']=$jobs; $data['jobs']=$jobs;
// echo "<pre>"; // echo "<pre>";
// print_r($jobs);die; // print_r($jobs);die;
@ -73,10 +74,26 @@ class Jobcard extends BaseController
} else if ($job_card_id !== '0') } else if ($job_card_id !== '0')
{ {
$productModel = new ProductModel();
$JobcardModel = new JobcardModel();
$JobcardProductModel = new JobcardProductModel();
$JobcardServiceModel = new JobcardServiceModel();
$JobcardComplaintModel = new JobcardComplaintModel();
$JobcardCustomComplaintModel = new JobcardCustomComplaintModel();
$servicedata = $JobcardModel->service_data($job_card_id, $this->session->get('logged_user_branch_id'));
foreach ($servicedata as $key => $value) {
$product_ids = json_decode($value['product_id'], true);
$product = $JobcardProductModel->whereIn('job_card_product_id', $product_ids)->findAll();
echo json_encode($product);die;
}
$data['page_name']="Edit Job Card"; $data['page_name']="Edit Job Card";
$ClientModel = new ClientModel(); $ClientModel = new ClientModel();
$client=$ClientModel->where('isactive',1)->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll(); $client=$ClientModel->where('isactive',1)->where('branch_id',$this->session->get('logged_user_branch_id'))->findAll();
$JobcardModel = new JobcardModel(); $JobcardModel = new JobcardModel();
$productdata = $JobcardModel->job_card_edit($job_card_id, $this->session->get('logged_user_branch_id'));
$jobs=$JobcardModel->where('job_card_id', $job_card_id)->get()->getRowArray(); $jobs=$JobcardModel->where('job_card_id', $job_card_id)->get()->getRowArray();
$data['jobs']=$jobs; $data['jobs']=$jobs;
$VehicleModel = new VehicleModel(); $VehicleModel = new VehicleModel();
@ -96,7 +113,7 @@ class Jobcard extends BaseController
$encodedModelId = json_encode([$modelId]); $encodedModelId = json_encode([$modelId]);
// Load the ProductModel // Load the ProductModel
$productModel = new ProductModel();
// Query the database to find the product based on make_id and model_id // Query the database to find the product based on make_id and model_id
$product = $productModel->where('make_id', $makeId) $product = $productModel->where('make_id', $makeId)
@ -116,10 +133,8 @@ class Jobcard extends BaseController
->findAll(); ->findAll();
$data['product']=$product; $data['product']=$product;
$data['service']=$service; $data['service']=$service;
$JobcardCustomComplaintModel = new JobcardCustomComplaintModel();
$jobs_product= $JobcardCustomComplaintModel->where('job_card_id', $job_card_id)->findAll(); $jobs_product= $JobcardCustomComplaintModel->where('job_card_id', $job_card_id)->findAll();
$data['jobs_product']=$jobs_product; $data['jobs_product']=$jobs_product;
$JobcardServiceModel = new JobcardServiceModel();
$jobs_service= $JobcardServiceModel->where('job_card_id', $job_card_id)->findAll(); $jobs_service= $JobcardServiceModel->where('job_card_id', $job_card_id)->findAll();
$data['jobs_service']=$jobs_service; $data['jobs_service']=$jobs_service;
$data['client']=$client; $data['client']=$client;
@ -137,130 +152,160 @@ class Jobcard extends BaseController
return view('job_card_form',$data); return view('job_card_form',$data);
} }
public function add_jobs() public function add_jobs()
{ {
// echo $this->request->getPost('products');die; // echo json_encode($this->request->getPost());die;
$JobcardModel = new JobcardModel(); $JobcardModel = new JobcardModel();
$ProductModel = new ProductModel(); $JobcardProductModel = new JobcardProductModel();
$JobcardCustomComplaintModel = new JobcardCustomComplaintModel();
$JobcardServiceModel = new JobcardServiceModel(); $JobcardServiceModel = new JobcardServiceModel();
$JobcardComplaintModel = new JobcardComplaintModel();
$JobcardCustomComplaintModel = new JobcardCustomComplaintModel();
// Job card INSERT // Job card INSERT
$data = [ $job_card_data = [
'client_name' => $this->request->getPost('client_id'), 'client_name' => $this->request->getPost('client_id'),
'mobile_no' => $this->request->getPost('mobile_no'),
'vehicle_id'=>$this->request->getPost('vehicle_id'), 'vehicle_id'=>$this->request->getPost('vehicle_id'),
'branch_id'=> $this->session->get('logged_user_branch_id'),
'billing_address' => $this->request->getPost('billing_address'), 'billing_address' => $this->request->getPost('billing_address'),
'billing_city' => $this->request->getPost('city'), 'billing_city' => $this->request->getPost('city'),
'mobile_no' => $this->request->getPost('mobile_no'),
'billing_state' => $this->request->getPost('state'), 'billing_state' => $this->request->getPost('state'),
'billing_postal_code' => $this->request->getPost('postalcode'), 'billing_postal_code' => $this->request->getPost('postalcode'),
'total'=> $this->request->getPost('grand_total'),
'status'=> $this->request->getPost('status'), 'status'=> $this->request->getPost('status'),
'isactive' => 1, // Assuming this is a default value or handled separately 'subtotal'=> $this->request->getPost('sub_total'),
'branch_id'=> $this->session->get('logged_user_branch_id'), 'tax'=> $this->request->getPost('invoicetax'),
'total'=> $this->request->getPost('grand_total'),
'isactive' => 1
]; ];
// print_r($data);die;
// Insert or update sales order // Insert or update sales order
$job_card_id = $this->request->getPost('job_card_id'); $job_card_id = $this->request->getPost('job_card_id');
if (!empty($job_card_id)) { if (!empty($job_card_id)) {
// print_r("hello");die; $JobcardModel->update($job_card_id, $job_card_data);
$JobcardModel->update($job_card_id, $data);
} else { } else {
$JobcardModel->insert($data); $JobcardModel->insert($job_card_data);
$job_card_id = $JobcardModel->getInsertID(); // Get the last inserted ID $job_card_id = $JobcardModel->getInsertID(); // Get the last inserted ID
} }
$orderNumber = 'JC-' . date('md') . '-' . str_pad($job_card_id, 5, '0', STR_PAD_LEFT); $orderNumber = 'JC-' . date('md') . '-' . str_pad($job_card_id, 5, '0', STR_PAD_LEFT);
// print_r($orderNumber);die; // print_r($orderNumber);die;
// Update sales order with generated order number // Update sales order with generated order number
// $JobcardModel->update($job_card_id, ['order_number' => $orderNumber]); $JobcardModel->update($job_card_id, ['order_number' => $orderNumber]);
// Prepare data for sales order product
// echo "<pre>";
// print_r($this->request->getPost());die;
/********** INSERT SERVICE DATA ************/ /********** INSERT SERVICE DATA ************/
$data = json_decode($_POST['products']); $data = json_decode($_POST['products']);
$service_data = $data[0]->service; $service_data = $data[0]->service;
$status = $this->request->getPost('status');
foreach ($service_data as $item) { foreach ($service_data as $item) {
// Insert into the service table // Insert into the service table
$service['job_card_id'] = $job_card_id; $service['job_card_id'] = $job_card_id;
$service['service_id'] = $item->service_id; $service['service_id'] = $item->service_id;
$service['quality'] = $item->quality; $service['qty'] = $item->quality;
$service['tax'] = $item->tax; $service['tax'] = $item->tax;
$service['amount'] = $item->amount; $service['amount'] = $item->amount;
$JobcardServiceModel->insert($service); $JobcardServiceModel->insert($service);
$service_id = $JobcardModel->getInsertID(); $service_id = $JobcardServiceModel->getInsertID();
// Insert into the product table // Insert into the product table
foreach ($item->product as $product) { $id = [];
$product['product_id'] = $product->p_id; foreach ($item->product as $pt) {
$product['qty'] = $product->qty; $product['job_card_id'] = $job_card_id;
$product['tax'] = $product->tax; $product['product_id'] = $pt->p_id;
$product['amount'] = $product->amount; $product['qty'] = $pt->qty;
$ProductModel->insert($product); $product['tax'] = $pt->tax;
$product['amount'] = $pt->amount;
$JobcardProductModel->insert($product);
$job_card_product_id = $JobcardProductModel->getInsertID();
array_push($id, $job_card_product_id);
}
$res = $JobcardServiceModel->update($service_id, ['product_id' => json_encode($id) ]);
if($res) {
$this->product_data_maintanence($id, $status);
} }
} }
/********** INSERT COMPLAINT DATA ************/
$complaint_data = $data[0]->complaint;
$product_ids = $this->request->getPost('item_details'); foreach ($complaint_data as $item) {
$quantities = $this->request->getPost('quantity'); // Insert into the service table
$discounts = $this->request->getPost('discount_amount'); $service['job_card_id'] = $job_card_id;
$discount_types = $this->request->getPost('discount_type'); $service['complaint_id'] = $item->complaint_id;
$amounts = $this->request->getPost('amount'); $service['qty'] = $item->quality;
$itemtax=$this->request->getPost('item-tax'); $service['tax'] = $item->tax;
$unitprice=$this->request->getPost('unit-price'); $service['amount'] = $item->amount;
$job_card_product_id = $this->request->getPost('job_card_product_id'); $JobcardComplaintModel->insert($service);
// print_r($product_ids);die; $complaint_id = $JobcardComplaintModel->getInsertID();
// print_r($status);die;
$status = $this->request->getPost('status');
foreach ($product_ids as $key => $product_id) {
$qty = isset($quantities[$key]) ? $quantities[$key] : 0;
if ($status == 'Created') {
// echo "hello";die;
$this->updateProductQuantity($ProductModel, $product_id, $qty);
}elseif ($status == 'Cancelled'){
// echo "cancel";die;
// echo"hello";die;
$this->addBackProductQuantity($ProductModel, $product_id, $qty);
}
$discount = isset($discounts[$key]) ? $discounts[$key] : 0; // Insert into the product table
$discount_type = isset($discount_types[$key]) ? $discount_types[$key] : ''; $id = [];
$tax=isset($itemtax[$key]) ? $itemtax[$key] : 0; foreach ($item->product as $pt) {
$rate=isset($unitprice[$key]) ? $unitprice[$key] : 0; $product['job_card_id'] = $job_card_id;
$amount = isset($amounts[$key]) ? $amounts[$key] : 0; $product['product_id'] = $pt->p_id;
// $new_qty = $this->calculateUpdatedQuantity($ProductModel, $product_id, $qty); $product['qty'] = $pt->qty;
// print_r($discount_type);die; $product['tax'] = $pt->tax;
$product_data = [ $product['amount'] = $pt->amount;
'job_card_id' => $job_card_id, $JobcardProductModel->insert($product);
'product_id' => $product_id, $job_card_product_id = $JobcardProductModel->getInsertID();
'qty' => $qty, array_push($id, $job_card_product_id);
'discount' => $discount, }
'discount_type' => $discount_type, $res =$JobcardComplaintModel->update($complaint_id, ['product_id' => json_encode($id) ]);
'net_price'=>$rate, if($res) {
'tax'=>$tax, $this->product_data_maintanence($id, $status);
'amount' => $amount, }
'isactive'=>1, }
];
// print_r($product_data);die; /********** INSERT CUSTOM COMPLAINT DATA ************/
if (!empty($job_card_product_id[$key])) { $c_complaint_data = $data[0]->custom_complaint;
foreach ($c_complaint_data as $item) {
$JobcardCustomComplaintModel->update($job_card_product_id[$key], $product_data); // Insert into the service table
$service['job_card_id'] = $job_card_id;
} else { $service['complaint_name'] = $item->complaint_id;
$JobcardCustomComplaintModel->insert($product_data); $service['qty'] = $item->quality;
$service['tax'] = $item->tax;
$service['amount'] = $item->amount;
$JobcardCustomComplaintModel->insert($service);
$cc_complaint_id = $JobcardCustomComplaintModel->getInsertID();
// Insert into the product table
$id = [];
foreach ($item->product as $pt) {
$product['job_card_id'] = $job_card_id;
$product['product_id'] = $pt->p_id;
$product['qty'] = $pt->qty;
$product['tax'] = $pt->tax;
$product['amount'] = $pt->amount;
$JobcardProductModel->insert($product);
$job_card_product_id = $JobcardProductModel->getInsertID();
array_push($id, $job_card_product_id);
}
$res =$JobcardCustomComplaintModel->update($cc_complaint_id, ['product_id' => json_encode($id) ]);
if($res) {
$this->product_data_maintanence($id, $status);
} }
} }
return redirect()->to('job_card_index'); return redirect()->to('job_card_index');
} }
public function product_data_maintanence($id_array,$status)
{
$ProductModel = new ProductModel();
$JobcardProductModel = new JobcardProductModel();
foreach ($id_array as $key => $product_id) {
$product = $JobcardProductModel->find($product_id);
$qty = isset($product['qty']) ? $product['qty'] : 0;
if ($status == 'Created') {
$this->updateProductQuantity($ProductModel, $product['product_id'], $qty);
}elseif ($status == 'Cancelled'){
$this->addBackProductQuantity($ProductModel, $product['product_id'], $qty);
}
}
}
private function addBackProductQuantity($ProductModel, $product_id, $sold_qty) private function addBackProductQuantity($ProductModel, $product_id, $sold_qty)
{ {
// Fetch current product quantity // Fetch current product quantity

View File

@ -5,5 +5,5 @@ class JobcardComplaintModel extends Model
{ {
protected $table = 'job_card_complaint'; protected $table = 'job_card_complaint';
protected $primaryKey = 'job_card_complaint_id'; protected $primaryKey = 'job_card_complaint_id';
protected $allowedFields = ['job_card_complaint_id','job_card_id','complaint_id','product_id', 'qty', 'isactive']; protected $allowedFields = ['job_card_complaint_id','job_card_id','complaint_id','product_id', 'qty', 'tax', 'amount','isactive'];
} }

View File

@ -5,5 +5,5 @@ class JobcardCustomComplaintModel extends Model
{ {
protected $table = 'job_card_custom_complaint'; protected $table = 'job_card_custom_complaint';
protected $primaryKey = 'job_card_cc_id'; protected $primaryKey = 'job_card_cc_id';
protected $allowedFields = ['job_card_cc_id', 'job_card_id', 'product_id', 'qty', 'isactive']; protected $allowedFields = ['job_card_cc_id', 'job_card_id','complaint_name', 'product_id', 'qty', 'tax', 'amount','isactive'];
} }

View File

@ -5,7 +5,10 @@ class JobcardModel extends Model
{ {
protected $table = 'job_card'; protected $table = 'job_card';
protected $primaryKey = 'job_card_id'; protected $primaryKey = 'job_card_id';
protected $allowedFields = ['job_card_id','vehicle_id','branch_id','order_number','sale_order','client_mobile_no','client_name','delivery_on','fuel','kilometer','materiality','customer_complaints','billing_address','billing_city','billing_state','billing_country','billing_postal_code','isactive','total','tax','subtotal','discount','status']; protected $allowedFields = ['job_card_id','vehicle_id','branch_id',
'order_number','client_mobile_no','client_name',
'billing_address','billing_city','billing_state','billing_country',
'billing_postal_code','status','subtotal','tax','total','isactive'];
@ -14,11 +17,11 @@ class JobcardModel extends Model
// Select required fields from both tables // Select required fields from both tables
// $this->select('job_card.*, COUNT(sales_order_product.job_card_id) AS product_count,vehicle.reg_no'); // $this->select('job_card.*, COUNT(sales_order_product.job_card_id) AS product_count,vehicle.reg_no');
// $this->select('job_card.*, COUNT(job_card_service.job_card_id) AS product_count, COUNT(job_card_service.job_card_id) AS service_count,vehicle.reg_no'); // $this->select('job_card.*, COUNT(job_card_service.job_card_id) AS product_count, COUNT(job_card_service.job_card_id) AS service_count,vehicle.reg_no');
$this->select('job_card.*, COUNT(job_card_service.job_card_id) AS product_count, vehicle.reg_no'); $this->select('job_card.*, COUNT(job_card_product.job_card_id) AS product_count, vehicle.reg_no');
// Join the sales_order_product table based on job_card_id // Join the sales_order_product table based on job_card_id
$this->join('job_card_service', 'job_card_service.job_card_id = job_card.job_card_id'); $this->join('job_card_service', 'job_card_service.job_card_id = job_card.job_card_id');
// $this->join('job_card_service', 'job_card_service.job_card_id = job_card.job_card_id'); $this->join('job_card_product', 'job_card_product.job_card_id = job_card.job_card_id');
$this->join('vehicle', 'vehicle.vehicle_id = job_card.vehicle_id'); $this->join('vehicle', 'vehicle.vehicle_id = job_card.vehicle_id');
// Group by job_card_id to get count of products per sales order // Group by job_card_id to get count of products per sales order
@ -32,5 +35,15 @@ class JobcardModel extends Model
// Get the results // Get the results
return $this->findAll(); return $this->findAll();
} }
public function service_data($job_card_id, $logged_user_branch_id)
{
$this->select('job_card.*, job_card_service.*');
$this->join('job_card_service', 'job_card_service.job_card_id = job_card.job_card_id');
$this->where('job_card_service.isactive', 1);
$this ->where('job_card.job_card_id',$job_card_id);
$this ->where('job_card.branch_id',$logged_user_branch_id);
return $this->findAll();
}
} }

View File

@ -5,5 +5,5 @@ class JobcardProductModel extends Model
{ {
protected $table = 'job_card_product'; protected $table = 'job_card_product';
protected $primaryKey = 'job_card_product_id'; protected $primaryKey = 'job_card_product_id';
protected $allowedFields = ['job_card_product_id', 'product_id', 'qty', 'tax', 'isactive']; protected $allowedFields = ['job_card_product_id','job_card_id' , 'product_id', 'qty', 'tax', 'amount', 'isactive'];
} }

View File

@ -5,7 +5,7 @@ class JobcardServiceModel extends Model
{ {
protected $table = 'job_card_service'; protected $table = 'job_card_service';
protected $primaryKey = 'job_card_service_id'; protected $primaryKey = 'job_card_service_id';
protected $allowedFields = ['job_card_service_id', 'job_card_id', 'service_id', 'product_id', 'qty', 'isactive']; protected $allowedFields = ['job_card_service_id', 'job_card_id', 'service_id', 'product_id', 'qty','tax','amount', 'isactive'];
} }

View File

@ -19,7 +19,7 @@
<div class="col-md-12"> <div class="col-md-12">
<div class="card"> <div class="card">
<div class="card-body"> <div class="card-body">
<form action="<?= base_url() . "add_jobs"; ?>" method="post"> <form id="form-submit" action="<?= base_url() . "add_jobs"; ?>" method="post">
<input type="hidden" id="product_response" name="products" value=""> <input type="hidden" id="product_response" name="products" value="">
<!-- <h4 class="header-title">Sales Order Details :</h4><br> --> <!-- <h4 class="header-title">Sales Order Details :</h4><br> -->
@ -210,7 +210,7 @@
</div> </div>
</div> </div>
<?php if (!isset($sales['status']) || $sales['status'] !== 'Paid') : ?> <?php if (!isset($sales['status']) || $sales['status'] !== 'Paid') : ?>
<button type="submit" class="btn btn-primary">Submit</button> <button type="submit" id="submit-btn" class="btn btn-primary">Submit</button>
<?php endif; ?> <?php endif; ?>
</form> </form>
</div> </div>
@ -512,9 +512,18 @@
console.log(array,'dataMapArray11'); console.log(array,'dataMapArray11');
} }
$(document).ready(function() {
$('#submit-btn').click(function(event) {
// Prevent the default form submission
event.preventDefault();
data_contruction_for_save();
$('#form-submit').submit();
});
});
// Event listener for product selection // Event listener for product selection
$(document).on('change', 'select[name="item_details[]"]', function() { $(document).on('change', 'select[name="item_details[]"]', function() {
data_contruction_for_save(); // data_contruction_for_save();
// Get class name of the select element // Get class name of the select element
var className = $(this).attr('class'); var className = $(this).attr('class');
var productId = $(this).val(); var productId = $(this).val();
@ -542,7 +551,7 @@
// Event listener for product selection // Event listener for product selection
$(document).on('change', 'select[name="service_details[]"]', function() { $(document).on('change', 'select[name="service_details[]"]', function() {
data_contruction_for_save(); // data_contruction_for_save();
// Get class name of the select element // Get class name of the select element
var className = $(this).attr('class'); var className = $(this).attr('class');
@ -569,7 +578,7 @@
// Event listener for product selection // Event listener for product selection
$(document).on('change', 'select[name="complaint_details[]"]', function() { $(document).on('change', 'select[name="complaint_details[]"]', function() {
data_contruction_for_save(); // data_contruction_for_save();
// Get class name of the select element // Get class name of the select element
var className = $(this).attr('class'); var className = $(this).attr('class');
var productId = $(this).val(); var productId = $(this).val();
@ -680,9 +689,14 @@
} }
// Event listener for quantity change // Event listener for quantity change
$(document).on('input', '.item-quantity', function() { $(document).on('input change', '.item-quantity', async function() {
// let promise = new Promise((resolve,reject) => {
calculateAmount($(this).closest('tr')); calculateAmount($(this).closest('tr'));
updateCalculations(); // Added for updating calculations when quantity changes // resolve()
// })
// promise.then(() => {
updateCalculations(); // Added for updating calculations when quantity changes
// });
}); });
// Function to calculate subtotal // Function to calculate subtotal

View File

@ -39,7 +39,7 @@
<thead> <thead>
<tr> <tr>
<th>Order Number</th> <th>Order Number</th>
<th>Reg No</th> <th>Reg No</th>
<th>Client Name</th> <th>Client Name</th>
<th>Client Mobile</th> <th>Client Mobile</th>