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

View File

@ -5,5 +5,5 @@ class JobcardComplaintModel extends Model
{
protected $table = 'job_card_complaint';
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 $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 $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
// $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, 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
$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');
// Group by job_card_id to get count of products per sales order
@ -32,5 +35,15 @@ class JobcardModel extends Model
// Get the results
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 $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 $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="card">
<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="">
<!-- <h4 class="header-title">Sales Order Details :</h4><br> -->
@ -210,7 +210,7 @@
</div>
</div>
<?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; ?>
</form>
</div>
@ -512,9 +512,18 @@
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
$(document).on('change', 'select[name="item_details[]"]', function() {
data_contruction_for_save();
// data_contruction_for_save();
// Get class name of the select element
var className = $(this).attr('class');
var productId = $(this).val();
@ -542,7 +551,7 @@
// Event listener for product selection
$(document).on('change', 'select[name="service_details[]"]', function() {
data_contruction_for_save();
// data_contruction_for_save();
// Get class name of the select element
var className = $(this).attr('class');
@ -569,7 +578,7 @@
// Event listener for product selection
$(document).on('change', 'select[name="complaint_details[]"]', function() {
data_contruction_for_save();
// data_contruction_for_save();
// Get class name of the select element
var className = $(this).attr('class');
var productId = $(this).val();
@ -680,9 +689,14 @@
}
// 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'));
updateCalculations(); // Added for updating calculations when quantity changes
// resolve()
// })
// promise.then(() => {
updateCalculations(); // Added for updating calculations when quantity changes
// });
});
// Function to calculate subtotal

View File

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