457 lines
18 KiB
PHP
Executable File
457 lines
18 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Models\PurchaseOrderModel;
|
|
use App\Models\ProductModel;
|
|
use App\Models\VendorModel;
|
|
use App\Models\BranchModel;
|
|
use App\Models\BusinessModel;
|
|
use App\Models\PurchaseOrderChildModel;
|
|
use App\Models\ReturnModel;
|
|
use App\Models\ReturnOrderChildModel;
|
|
use Mpdf\Mpdf;
|
|
|
|
|
|
class ReturnOrder extends BaseController
|
|
{
|
|
public $session;
|
|
protected $returnModel;
|
|
public function __construct()
|
|
{
|
|
$this->session = session();
|
|
$this->returnModel = new ReturnModel();
|
|
}
|
|
|
|
public function return_order_index()
|
|
{
|
|
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
|
|
$PurchaseOrderModel = new PurchaseOrderModel();
|
|
|
|
$purchase = $PurchaseOrderModel->getPurchaseOrdersWithProducts($this->session->get('logged_user_branch_id'));
|
|
|
|
$data['purchase']=$purchase;
|
|
|
|
|
|
|
|
$return = $this->returnModel->findAll();
|
|
$data['return']=$return;
|
|
|
|
// echo "<pre>";
|
|
// dd($data);
|
|
return view('return_order_liist',$data);
|
|
}
|
|
|
|
public function return_order()
|
|
{
|
|
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
|
|
$ReturnModel = new ReturnModel();
|
|
|
|
$return_orders = $ReturnModel->select('return_order.*, vendor.vendor_name as vendor_name, COUNT(return_order_child.return_order_id) AS return_count, SUM(return_order_child.qty) AS return_qty')
|
|
->join('vendor', 'vendor.vendor_id = return_order.vendor_id')
|
|
->join('return_order_child', 'return_order_child.return_order_id = return_order.return_order_id', 'left')
|
|
->where('return_order.branch_id', $this->session->get('logged_user_branch_id'))
|
|
->groupBy('return_order.return_order_id')
|
|
->orderBy('return_order.return_order_id','desc')
|
|
->findAll();
|
|
|
|
|
|
$data['return_order']=$return_orders;
|
|
|
|
// echo "<pre>";
|
|
// print_r($data);die;
|
|
return view('return_list',$data);
|
|
}
|
|
public function new_return($purchase_order_id)
|
|
{
|
|
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
|
|
|
|
$data['page_name']="Create Return Order";
|
|
|
|
$PurchaseOrderModel = new PurchaseOrderModel();
|
|
$purchase=$PurchaseOrderModel->where('purchase_order_id', $purchase_order_id)->get()->getRowArray();
|
|
$data['purchase'] = $purchase;
|
|
|
|
|
|
|
|
$VendorModel = new VendorModel();
|
|
$vendor = $VendorModel->findAll();
|
|
$data['vendor'] = $vendor;
|
|
|
|
$data['vendor_id'] = $purchase['vendor_id'];
|
|
|
|
$vendorId=$purchase['vendor_id'];
|
|
$encodedVendorId = json_encode([$vendorId]);
|
|
$productModel = new ProductModel();
|
|
|
|
// Query the database to find the product based on make_id and model_id
|
|
$product = $productModel->select('products.*, manufacturer.manufacturer_name')
|
|
->join('manufacturer', 'manufacturer.manufacturer_id = products.manufacturer_id','left')
|
|
->where('JSON_CONTAINS(vendor, \'' . $encodedVendorId . '\')', null, false)
|
|
->findAll();
|
|
$data['product']=$product;
|
|
|
|
|
|
|
|
|
|
$PurchaseOrderChildModel = new PurchaseOrderChildModel();
|
|
$purchase_product=$PurchaseOrderChildModel->where('purchase_order_id', $purchase_order_id)->where('received_qty >', 0)->where('is_selected', 1)->findAll();
|
|
$data['purchase_product'] = $purchase_product;
|
|
|
|
|
|
echo view('return_order_form',$data);
|
|
}
|
|
|
|
public function add_return_purchase()
|
|
{
|
|
if(!$this->session->has('logged_user')) { return redirect()->to(base_url()); }
|
|
|
|
|
|
$ProductModel = new ProductModel();
|
|
// print_r($this->request->getPost());die;
|
|
$data = [
|
|
'purchase_order_id' => $this->request->getPost('purchase_order_id'),
|
|
'vendor_id' => $this->request->getPost('vendor_id'),
|
|
'order_date' => $this->request->getPost('order_date'),
|
|
'contact_person_name' => $this->request->getPost('contact_person_name'),
|
|
'contact_person_mobile'=> $this->request->getPost('contact_person_mobile'),
|
|
'status'=> $this->request->getPost('purchase_status'),
|
|
'billing_address' => $this->request->getPost('billing_address'),
|
|
'billing_state' => $this->request->getPost('billing_state'),
|
|
'billing_city' => $this->request->getPost('billing_city'),
|
|
'billing_postal_code' => $this->request->getPost('billing_postal_code'),
|
|
'shipping_address' => $this->request->getPost('shipping_address'),
|
|
'shipping_state' => $this->request->getPost('shipping_state'),
|
|
'shipping_city' =>$this->request->getPost('shipping_city'),
|
|
'shipping_postal_code' => $this->request->getPost('shipping_postal_code'),
|
|
'return_date' => $this->request->getPost('return_date') ? $this->request->getPost('return_date') : '',
|
|
'terms_condition' => $this->request->getPost('terms_condition'),
|
|
'branch_id'=> $this->session->get('logged_user_branch_id'),
|
|
];
|
|
|
|
if ($this->request->getPost('purchase_status') == 'Returned') {
|
|
|
|
|
|
|
|
$return_order_id = $this->request->getPost('return_order_id');
|
|
|
|
$ReturnModel = new ReturnModel();
|
|
$ReturnOrderChildModel = new ReturnOrderChildModel();
|
|
$PurchaseOrderChildModel = new PurchaseOrderChildModel();
|
|
|
|
$return = $ReturnModel->where('return_order_id', $return_order_id)->first();
|
|
|
|
|
|
$return_data_update =['status'=>$this->request->getPost('purchase_status')];
|
|
|
|
|
|
if ($return) {
|
|
$quantity =$this->request->getPost('quantity');
|
|
$item_details =$this->request->getPost('item_details');
|
|
foreach ($item_details as $index => $item_detail) {
|
|
|
|
// reduce qty in purchaase child
|
|
$purchase_id = $this->request->getPost('purchase_order_id');
|
|
$purchase_order_data = $PurchaseOrderChildModel->where('purchase_order_id', $purchase_id)->where('product_id', $item_detail)->first();
|
|
$received_qty = $purchase_order_data['received_qty'] - $quantity[$index];
|
|
$PurchaseOrderChildModel->update($purchase_order_data['purchase_order_child_id'], [ 'received_qty' => $received_qty ]);
|
|
|
|
|
|
$product = $ProductModel->where('product_id',$item_detail)->first();
|
|
$changed_quantity = $product['qty_stock'] - $quantity[$index];
|
|
$product_id = $item_detail;
|
|
|
|
$changed_quantity_DB=['qty_stock'=>$changed_quantity];
|
|
$ProductModel->update($product_id, $changed_quantity_DB);
|
|
}
|
|
$ReturnModel->update($return_order_id, $return_data_update);
|
|
}
|
|
return redirect()->to('return_order');
|
|
} else {
|
|
|
|
|
|
$return_order_id = $this->request->getPost('return_order_id');
|
|
// echo $this->request->getPost('return_order_id');die;
|
|
if (!empty($return_order_id)) {
|
|
|
|
$this->returnModel->update($return_order_id, $data);
|
|
} else {
|
|
|
|
$this->returnModel->insert($data);
|
|
$return_order_id = $this->returnModel->getInsertID();
|
|
}
|
|
$orderNumber = 'RO' . str_pad($return_order_id, 8, '0', STR_PAD_LEFT);
|
|
|
|
$this->returnModel->update($return_order_id, ['order_number' => $orderNumber]);
|
|
|
|
// die;
|
|
$product_ids = $this->request->getPost('item_details');
|
|
$quantities = $this->request->getPost('quantity');
|
|
$amounts = $this->request->getPost('amount');
|
|
$itemtax=$this->request->getPost('item-tax');
|
|
$unitprice=$this->request->getPost('unit-price');
|
|
$selected_items=$this->request->getPost('return_selected_items');
|
|
$purchase_order_child_id = $this->request->getPost('purchase_order_child_id');
|
|
|
|
|
|
$status = $this->request->getPost('purchase_status');
|
|
foreach ($selected_items as $key => $product_id) {
|
|
|
|
$qty = isset($quantities[$product_id]) ? $quantities[$product_id] : 0;
|
|
$selected_item= isset($selected_items[$key]) ? $selected_items[$key] : 0;
|
|
// print_r($selected_item);die;
|
|
if ($status == 'Released' && $selected_item == 1) {
|
|
$this->addBackProductQuantity($ProductModel, $product_id, $qty);
|
|
}
|
|
|
|
|
|
$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;
|
|
$child_data = [
|
|
'return_order_id' => $return_order_id,
|
|
'product_id' => $product_ids[$key],
|
|
'qty' => $qty,
|
|
'net_price'=>$rate,
|
|
'tax'=>$tax,
|
|
'amount' => $amount,
|
|
'is_selected'=>$selected_item,
|
|
'isactive'=>1,
|
|
];
|
|
|
|
// echo "<pre>";
|
|
// print_r($child_data);die;
|
|
$ReturnOrderChildModel = new ReturnOrderChildModel();
|
|
|
|
|
|
$ReturnOrderChildModel->insert($child_data);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
return redirect()->to('return_order');
|
|
}
|
|
|
|
|
|
private function addBackProductQuantity($ProductModel, $product_id, $sold_qty)
|
|
{
|
|
// Fetch current product quantity
|
|
$product = $ProductModel->find($product_id);
|
|
|
|
$data['qty_stock'] = $product['qty_stock'] + $sold_qty;
|
|
|
|
if($product['per'] == 'barrel')
|
|
{
|
|
$data['barrel'] = $data['qty_stock'] / $product['ltr_per_barrel'];
|
|
}
|
|
else if($product['per'] == 'box')
|
|
{
|
|
$data['box'] = $data['qty_stock'] / $product['qty_per_box'];
|
|
}
|
|
|
|
// Update quantity in Product table
|
|
$ProductModel->update($product_id, $data );
|
|
}
|
|
|
|
public function delete_purchase($purchase_order_id)
|
|
{
|
|
$PurchaseOrderModel = new PurchaseOrderModel();
|
|
$where = ['purchase_order_id' => $purchase_order_id, 'isactive' => 1 ]; // Assuming 'isactive' is a column in your database
|
|
$existingPurchase = $PurchaseOrderModel->where($where)->first();
|
|
|
|
if ($existingPurchase) {
|
|
$data['isactive'] = 0;
|
|
|
|
if ($PurchaseOrderModel->update($purchase_order_id, $data)) {
|
|
session()->setFlashdata('success', 'Deleted successfully.');
|
|
$this->logger->info("Purchase Order: has been Inactivated successfully. Inactivated ID = ".$purchase_order_id);
|
|
}
|
|
}
|
|
|
|
|
|
return redirect()->to('purchase_index');
|
|
}
|
|
|
|
|
|
|
|
|
|
public function get_vendor($vendor_id)
|
|
{
|
|
|
|
$VendorModel = new VendorModel();
|
|
|
|
$products =$VendorModel->where('vendor_id',$vendor_id)->select('vendor_name,contact_person_name1,contact_person_mobile1')->get()->getRowArray();
|
|
|
|
|
|
return json_encode($products);
|
|
|
|
}
|
|
public function getVendorProducts()
|
|
{
|
|
// Retrieve make_id and model_id from the request
|
|
// $makeId = $this->request->getPost('');
|
|
$vendorId = $this->request->getPost('vendor_id');
|
|
|
|
// Encode the modelId before searching
|
|
$encodedVendorId = json_encode([$vendorId]);
|
|
|
|
// Load the ProductModel
|
|
$productModel = new ProductModel();
|
|
|
|
// Query the database to find the product based on make_id and model_id
|
|
$product = $productModel->select('products.*, manufacturer.manufacturer_name')
|
|
->join('manufacturer', 'manufacturer.manufacturer_id = products.manufacturer_id','left')
|
|
->where('JSON_CONTAINS(vendor, \'' . $encodedVendorId . '\')', null, false)
|
|
->findAll();
|
|
|
|
// print_r($product);die;
|
|
if($product){
|
|
// Send JSON response
|
|
return $this->response->setJSON(['product'=>$product] );
|
|
} else {
|
|
// If product is not found, return an empty response or appropriate message
|
|
return $this->response->setJSON(['product'=>[] ]);
|
|
}
|
|
}
|
|
public function delete_purchase_product()
|
|
{
|
|
$purchase_order_child_id = $this->request->getPost('purchase_order_child_id');
|
|
|
|
if (!empty($purchase_order_child_id)) {
|
|
$PurchaseOrderChildModel = new PurchaseOrderChildModel();
|
|
$data['isactive'] = 0;
|
|
|
|
if ($PurchaseOrderChildModel->update($purchase_order_child_id, $data)) {
|
|
// Return success response
|
|
return $this->response->setJSON(['success' => true]);
|
|
}
|
|
}
|
|
|
|
// Return error response if deletion fails
|
|
return $this->response->setJSON(['success' => false]);
|
|
}
|
|
public function download_purchase_invoice($purchase_order_id)
|
|
{
|
|
// echo "hello"; die;
|
|
// Fetch the invoice data based on $invoice_id
|
|
$model = new PurchaseOrderModel();
|
|
$data = $model->getPurchasePdf($purchase_order_id);
|
|
|
|
$items = $model->getPurchaseOrderProductsForPdf($purchase_order_id);
|
|
|
|
|
|
|
|
// print_r($items);die();
|
|
|
|
// print_r($invoiceItems);die();
|
|
|
|
// Create an mPDF object
|
|
$mpdf = new Mpdf([
|
|
'mode' => '',
|
|
'format' => 'A5',
|
|
'default_font_size' => 0,
|
|
'default_font' => '',
|
|
|
|
// 'margin_right' => 1,
|
|
'margin_top' => 6,
|
|
'margin_bottom' => 6,
|
|
'margin_header' => 6,
|
|
'margin_footer' =>-30,
|
|
'orientation' => 'P',
|
|
]);
|
|
|
|
|
|
|
|
$mpdf->autoLangToFont = true; $mpdf->autoScriptToLang = true;
|
|
// Set PDF properties
|
|
$mpdf->SetTitle('Invoice');
|
|
$mpdf->SetAuthor($data[0]->branch_name);
|
|
$mpdf->SetCreator('');
|
|
|
|
|
|
|
|
// Generate the PDF content (HTML)
|
|
// if ($data[0]->status === 'Draft') {
|
|
// // Set the watermark text and options
|
|
// $mpdf->SetWatermarkText('Draft');
|
|
// $mpdf->showWatermarkText = true;
|
|
// }
|
|
// if ($data[0]->status === 'Cancelled') {
|
|
// // Set the watermark text and options
|
|
// $mpdf->SetWatermarkText('Cancelled');
|
|
// $mpdf->showWatermarkText = true;
|
|
// }
|
|
// if ($data[0]->status === 'Void') {
|
|
// // Set the watermark text and options
|
|
// $mpdf->SetWatermarkText('Void');
|
|
// $mpdf->showWatermarkText = true;
|
|
// }
|
|
|
|
|
|
// Generate the PDF content (HTML) with data
|
|
$html = view('invoice_purchase_template', ['sales' => $data,'items'=>$items]);
|
|
// echo $html;die;
|
|
// Load HTML into the mPDF instance
|
|
$mpdf->WriteHTML($html);
|
|
|
|
// Output the PDF to the browser for download
|
|
$mpdf->Output('invoice_' . date('Y-m-d H-i-s') . '.pdf', 'D');
|
|
}
|
|
|
|
|
|
public function edit_return_order($return_order_id){
|
|
|
|
$data['page_name']= 'Edit Return Order';
|
|
|
|
|
|
$ReturnModel = new ReturnModel();
|
|
$return_order=$ReturnModel->where('return_order_id', $return_order_id)->first();
|
|
$data['return_order'] = $return_order;
|
|
|
|
$purchase_order_id = $return_order['purchase_order_id'];
|
|
|
|
$PurchaseOrderModel = new PurchaseOrderModel();
|
|
$purchase=$PurchaseOrderModel->where('purchase_order_id', $purchase_order_id)->get()->getRowArray();
|
|
$data['purchase'] = $purchase;
|
|
|
|
|
|
|
|
$VendorModel = new VendorModel();
|
|
$vendor = $VendorModel->findAll();
|
|
$data['vendor'] = $vendor;
|
|
|
|
$data['vendor_id'] = $purchase['vendor_id'];
|
|
|
|
$vendorId=$purchase['vendor_id'];
|
|
$encodedVendorId = json_encode([$vendorId]);
|
|
$productModel = new ProductModel();
|
|
|
|
// Query the database to find the product based on make_id and model_id
|
|
$product = $productModel->select('products.*, manufacturer.manufacturer_name')
|
|
->join('manufacturer', 'manufacturer.manufacturer_id = products.manufacturer_id','left')
|
|
->where('JSON_CONTAINS(vendor, \'' . $encodedVendorId . '\')', null, false)
|
|
->findAll();
|
|
$data['product']=$product;
|
|
|
|
|
|
|
|
|
|
$ReturnOrderChildModel = new ReturnOrderChildModel();
|
|
$return_order_child=$ReturnOrderChildModel->where('return_order_id', $return_order_id)->findAll();
|
|
$data['purchase_product']=$return_order_child;
|
|
|
|
// echo"<pre>";
|
|
// print_r($data);die;
|
|
|
|
return view('edit_return_order', $data);
|
|
}
|
|
|
|
} |