Viewing file: SaleController.php (34.29 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\Sale;
use App\Models\SaleItem;
use App\Models\SaleFee;
use App\Models\ProductVariants;
use App\Models\CustomerPayment;
use App\Models\Deliveries;
use App\Models\ShopSettings;
use App\Models\StoreSettings;
use App\Models\SaleState;
use App\Models\ReturnRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Carbon\Carbon;
use DateTime;
use GuzzleHttp\Client;
use App\Http\Controllers\SaleFeesController;
use Ramsey\Uuid\Uuid;
use App\Helpers\NotificationHelper;
class SaleController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
function customerNotification($sale_id,$vendor_id) {
$data=Sale::with('customer_Data')->find($sale_id);
if ($data->customer_Data->psid && $data->sale_status >2 && $data->payment_method!==2) {
$client = new Client();
$res = $client->request('post', env("DCORE").'/status_change',["json"=>[
"page_id"=> $data->customer_Data->page_id,
"invoice_no"=>$data->invoice_no,
"sender_psid"=>$data->customer_Data->psid,
"status"=>$data->sale_status
]]);
}
if (!($data->sale_status == 2 || $data->sale_status == 5 || ($data->sale_status ==1 && $data->payment_method==2))) {
$notification=new NotificationHelper();
$notification->send('/order',["json"=>[
'vendor_id' => $vendor_id,
'customer'=> $data->customer_Data->name,
'invoice_no'=>$data->invoice_no,
'status'=>$data->sale_status
]]);
}
if ($data->sale_status<3 && $data->payment_method==2) {
return "Done";
}else {
$vendor=StoreSettings::select('image_folder')->where('vendor_id',$vendor_id)->first();
$client = new Client();
$res = $client->request('post', env("SPACE_PROXY").'/invoice',["json"=>[
"sale_id"=>$sale_id,
"invoice_no"=>$data->invoice_no,
"folder_name"=>$vendor->image_folder
]]);
}
return "done";
}
private function saleStatusChange($sale_id,$invoice_no,$state,$vendor_id) {
register_shutdown_function(function () use ($sale_id,$invoice_no,$state,$vendor_id) {
try {
SaleState::updateOrCreate([
'sale_id' => $sale_id
],
[
'invoice_no'=>$invoice_no,
'status' =>$state
]);
$this->customerNotification($sale_id,$vendor_id);
}catch (\Exception $e) {
\Log::error('Error API call failed', ['error' => $e->getMessage()]);
}
});
}
// get orders
function index(Request $req)
{
return response()->json('unauthorized',401);
}
// create new order
public function create(Request $req){
try {
if (auth()->user()) {
$vendor_id =auth()->user()->id;
}else {
$vendor_id=$req->input("vendor_id");
}
if (empty($req->input("sale_item"))) {
return response()->json(["error"=>true,"message"=>"No Product Is selected"]);
}
$ref=Sale::select("invoice_no")->where('vendor_id',$vendor_id)->where("sale_status","!=",6)->orderBy('id','desc')->limit(1)->get();
if (count($ref)>0) {
$inv_no=intval($ref[0]->invoice_no)+1;
}else{
$inv_no=100000001;
}
$uuid = Uuid::uuid4()->toString();
$numericUuid = preg_replace('/[^0-9]/', '', $uuid);
if (strlen($numericUuid) < 16) {
$order_id = str_pad($numericUuid, 15, '0'); // Pad with zeros if too short
} else {
$order_id = substr($numericUuid, 0, 15); // Trim to 16 digits if too long
}
DB::beginTransaction();
$sale=Sale::create([
'invoice_no' => $inv_no,
'order_id' => $order_id,
"shop_id" => $req->input("shop_id"),
"receiver_name" => $req->input("receiver_name"),
"receiver_phone" => $req->input("receiver_phone"),
'customer_id' => $req->input("customer_id"),
'customer' => $req->input("customer"),
'vendor_id' => $vendor_id,
'note' => $req->input("note"),
'total' => $req->input("total"),
'discount_code' => 0,
'total_discount' => 0,
'shipping' => $req->input("shipping"),
'grand_total' => $req->input("grand_total"),
'total_items' => $req->input("total_items"),
'sale_status' => ($req->input("sale_status")=="")?1:$req->input("sale_status"),
'payment_status' => 0,
"type" => $req->input('type'),
"delivery_partner" => $req->input('delivery_partner'),
'paid' => 0,
'payment_method' => $req->input('payment_method'),
'address' => $req->input("address")
]);
} catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
if ($sale) {
try {
$products=explode("||",$req->input("sale_item"));
for ($i=0; $i <count($products) ; $i++) {
$product_info=explode(",",$products[$i]);
SaleItem::create([
'sale_id'=>$sale->id,
'product_id'=>$product_info[0],
"product_code"=>$product_info[1],
"product_name"=>$product_info[2],
"variant_id"=>$product_info[3],
"unit_price"=>$product_info[4],
"quantity"=>$product_info[5],
"subtotal"=>$product_info[4]*$product_info[5]
]);
}
$customer=[
"receiver_name" => $req->input("receiver_name"),
"receiver_phone" => $req->input("receiver_phone"),
'address' => $req->input("address")
];
$shop_data=ShopSettings::with('vendor_info')->where("id",$req->input("shop_id"))->first();
$shop=[
'shop_name' => $shop_data->shop_name,
"phone" => $shop_data->vendor_info->phone,
"address" =>$shop_data->address
];
Deliveries::create([
"sale_id"=> $sale->id,
"order_id"=>$sale->order_id,
"invoice_no"=>$sale->invoice_no,
"customer"=>json_encode($customer),
"vendor"=>json_encode($shop),
"delivered_by"=>1 ,
"shop_id" =>$shop_data->id
]);
} catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
DB::commit();
(new SaleFeesController)->orderFees($vendor_id,$shop_data->id,$sale->id,$sale->invoice_no,$req->input("total"),$req->input("grand_total"),$req->input('delivery_partner'),$req->input('payment_method'),0);
$d = new DateTime();
$t=$d->format("ymdHisv");
$data=["id"=>$sale->id,"invoice_no"=>$sale->invoice_no,"_id"=>$t,"created_at"=>$sale->created_at];
// try {
// $client = new Client([
// 'auth' => ['Notification', 'jiFFNotification@2023!@!@!@']
// ]);
// $res= $client->request('POST', 'https://notification.businesscloud.xyz/new_order',[
// "json"=>[
// 'vendor_id' => auth()->user()['id'],
// 'customer'=>$req->input('customer'),
// 'invoice_no'=>$sale->invoice_no
// ]
// ]);
// } catch (\Throwable $th) {
// //throw $th;
// }
// if ($req->input("sale_status")==2) {
// $this->sendToAccounting($sale->id);
// }
$vendor=StoreSettings::select('image_folder')->where('vendor_id',$vendor_id)->first();
$this->saleStatusChange($sale->id,$sale->invoice_no,$sale->sale_status,$vendor_id);
return response()->json(['message' => "Successfully Ordered",'data' => $data]);
} else {
DB::rollback();
return response()->json(['error' => 'true','message' => "Something is wrong please try again"]);
}
}
function sendToAccounting($sale_id) {
$sale=Sale::find($sale_id);
$sale_fee=SaleFee::where('sale_id',$sale_id)->first();
// $client = new Client();
// $req_data = [
// "id": strval($sale_id),
// "name": $sale->customer,
// "page_id": strval($sale->page_id),
// "vendor_id": strval($sale->vendor_id),
// "customer_id": strval($sale->customer_id),
// "invoice_no": strval($sale->invoice_no),
// "total_order_amount": $sale->grand_total,
// "total_item_amount": $sale->total,
// "total_fees": $sale_fee->total_fee,
// "jiff_fees": $sale_fee->jiff_fee,
// "payment_status": $sale->payment_method,
// "order_staus": $sale->sale_status,
// "vendor_payable": $sale_fee->vendor_payable,
// "vendor_receivable":$sale_fee->vendor_receivable
// ];
// $res = $client->request('POST', 'http://157.245.155.94:3039/create-customer',[
// 'json' => $req_data
// ]);
// if ($res->getStatusCode()==200) {
// $customer=Customer::find($customer->id);
// $customer->is_synced= 1;
// $ven=$customer->save();
// }
}
// create new order
public function createSMOrder($vendor_id,Request $req)
{
try {
// $vendor_id =$req->input("sale_item");
if (empty($req->input("sale_item"))) {
return response()->json(["error"=>true,"message"=>"No Product Is selected"]);
}
$ref=Sale::select("invoice_no")->where('vendor_id',$vendor_id)->where("sale_status","!=",6)->orderBy('id','desc')->limit(1)->get();
if (count($ref)>0) {
$last_inv_no=$ref[0]->invoice_no;
}else{
$last_inv_no=100000000;
}
DB::beginTransaction();
$sale=Sale::create([
'invoice_no' => $last_inv_no+1,
"shop_id" => $req->input("shop_id"),
"receiver_name" => $req->input("receiver_name"),
"receiver_phone" => $req->input("receiver_phone"),
'customer_id' => $req->input("customer_id"),
'customer' => $req->input("customer"),
'vendor_id' => $vendor_id,
'note' => $req->input("note"),
'total' => $req->input("total"),
'discount_code' => 0,
'total_discount' => 0,
'shipping' => $req->input("shipping"),
'grand_total' => $req->input("grand_total"),
'total_items' => $req->input("total_items"),
'sale_status' => 1,
'payment_status' => 0,
"type" => $req->input('type'),
"delivery_partner" => $req->input('delivery_partner'),
'paid' => 0,
'payment_method' => $req->input('payment_method'),
'address' => $req->input("address")
]);
} catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
if ($sale) {
try {
$products=explode("||",$req->input("sale_item"));
for ($i=0; $i <count($products) ; $i++) {
$product_info=explode(",",$products[$i]);
SaleItem::create([
'sale_id'=>$sale->id,
'product_id'=>$product_info[0],
"product_code"=>$product_info[1],
"product_name"=>$product_info[2],
"variant_id"=>$product_info[3],
"unit_price"=>$product_info[4],
"quantity"=>$product_info[5],
"subtotal"=>$product_info[4]*$product_info[5]
]);
}
} catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
DB::commit();
(new SaleFeesController)->orderFees($vendor_id,$req->input("shop_id"),$sale->id,$sale->invoice_no,$req->input("total"),$req->input("grand_total"),$req->input('delivery_partner'),$req->input('payment_method'),0);
$d = new DateTime();
$t=$d->format("ymdHisv");
$data=["id"=>$sale->id,"invoice_no"=>$sale->invoice_no,"_id"=>$t,"created_at"=>$sale->created_at];
$client = new Client([
'auth' => ['Notification', 'jiFFNotification@2023!@!@!@']
]);
$res= $client->request('POST', 'https://notification.businesscloud.xyz/new_order',[
"json"=>[
'vendor_id' => auth()->user()['id'],
'customer'=>$req->input('customer'),
'invoice_no'=>$sale->invoice_no
]
]);
return response()->json(['message' => "Successfully Ordered",'data' => $data]);
} else {
DB::rollback();
return response()->json(['error' => 'true','message' => "Something is wrong please try again"]);
}
}
// update order
function update(Request $req)
{
try{
$vendor_id =auth()->user()->id;
$change_items=$req->input("change_items");
DB::beginTransaction();
$sale=Sale::find($req->input('sale_id'));
if ($change_items!=="") {
$change_items=explode("||",$req->input("change_items"));
for ($i=0; $i <count($change_items) ; $i++) {
$product_info=explode(",",$change_items[$i]);
// product_id,variant_id,quantity,subtotal
$item=SaleItem::where('sale_id',$req->input("sale_id"))->where('product_id',$product_info[0])->where('variant_id',$product_info[1])->first();
if ($item) {
$item->quantity=floatval($product_info[2]);
$item->subtotal=floatval($product_info[3]);
$item->edited=1;
$item->save();
}
}
}
$new_items=$req->input("new_items");
if ($new_items!=="") {
$new_items=explode("||",$req->input("new_items"));
for ($i=0; $i <count($new_items) ; $i++) {
$product_info=explode(",",$new_items[$i]);
SaleItem::updateOrCreate([
'sale_id' => $sale->id,
'product_id'=>$product_info[0],
"variant_id"=>$product_info[3]
],
[
'sale_id'=>$sale->id,
'product_id'=>$product_info[0],
"product_code"=>$product_info[1],
"product_name"=>$product_info[2],
"variant_id"=>$product_info[3],
"unit_price"=>$product_info[4],
"quantity"=>$product_info[5],
"subtotal"=>$product_info[4]*$product_info[5],
"edited"=>1
]);
}
}
$sale->receiver_name=$req->input("receiver_name");
$sale->receiver_phone=$req->input("receiver_phone");
$sale->address=$req->input("address");
$sale->total=$req->input("total");
$sale->note=$req->input("note");
$sale->shipping=$req->input("shipping");
$sale->grand_total=$req->input("grand_total");
$sale->total_items=$req->input("total_items");
$sale->delivery_partner=$req->input("delivery_partner");
$sale->save();
(new SaleFeesController)->orderFees($vendor_id,null,$sale->id,$sale->invoice_no,$req->input("total"),$req->input("grand_total"),$req->input('delivery_partner'),$req->input('payment_method'),1);
$vendor=StoreSettings::select('image_folder')->where('vendor_id',$vendor_id)->first();
$client = new Client();
$res = $client->request('post', env("SPACE_PROXY").'/invoice',["json"=>[
"sale_id"=>$sale->id,
"invoice_no"=>$sale->invoice_no,
"folder_name"=>$vendor->image_folder
]]);
DB::commit();
return response()->json(["message"=>"Order Updated"]);
} catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
}
// online payment
function paymentStatus(Request $req) {
try {
ob_start();
// Send the response to the client
echo response()->json(["message"=>"Payment Done"]);
// Flush the output buffer and send the response to the client
ob_flush();
flush();
$vendor_id=explode('_',$req->payerReference)[0];
$sale=Sale::where("invoice_no",$req->merchantInvoiceNumber)->where("vendor_id",$vendor_id)->first();
if (empty($sale)) {
return response()->json(["error"=>true,"message"=>"Invalid order"]);
}
$sale->paid=floatval($req->amount);
$sale->payment_status=1;
$sale->save();
$req_body=[
"invoice_no"=>$req->merchantInvoiceNumber,
"vendor_id"=>$vendor_id,
"page_psid" => str_replace("$vendor_id"."_","",$req->payerReference),
"amount"=>floatval($req->amount)
];
CustomerPayment::create([
'payment_id'=>$req->paymentID,
'trx_id'=>$req->trxID,
'order_id'=>$sale->id,
'payment_method'=>2,
'amount'=>floatval($req->amount),
'dateTime'=>$req->paymentExecuteTime,
'payment_data'=>json_encode($req->all())
]);
$vendor=StoreSettings::select('image_folder')->where('vendor_id',$vendor_id)->first();
$client = new Client();
$res = $client->request('post', env("SPACE_PROXY").'/invoice',["json"=>[
"sale_id"=>$sale->id,
"invoice_no"=>$req->merchantInvoiceNumber,
"folder_name"=>$vendor->image_folder
]]);
$client = new Client([
'auth' => ['Mongo_API', 'jiFFMongo@2023!@!@!@']
]);
$res = $client->request('POST', 'https://api.businesscloud.xyz/online_payment',["json"=>$req_body]);
// return response()->json(["message"=>"Payment Done"]);
} catch (\Exception $exception) {
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
}
// cancel order
function cancelOrder(Request $req) {
try {
$sale=Sale::where("invoice_no",$req->invoice_no)->where("vendor_id",$req->vendor_id)->first();
if (empty($sale)) {
return response()->json(["error"=>true,"message"=>"Invalid order"]);
}
$sale->sale_status=5;
$sale->save();
$req_body=array_merge($req->all(), ['sale_id' => $sale->id]);
$client = new Client([
'auth' => ['Mongo_API', 'jiFFMongo@2023!@!@!@']
]);
$res = $client->request('POST', 'https://api.businesscloud.xyz/cancel_order',["json"=>$req_body]);
$this->saleStatusChange($sale->id,$req->invoice_no,5,$req->vendor_id);
return response()->json(["message"=>"Order Canceled successfully"]);
} catch (\Exception $exception) {
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
}
// update sale status by vendor
function saleStatus($sale_id,Request $req)
{
try{
$sale=Sale::find($sale_id);
if (empty($sale)) {
return response()->json(["error"=>true,"message"=>"Invalid order"]);
}
if (auth()->user()) {
$vendor_id =auth()->user()->id;
}else {
$vendor_id=$sale->vendor_id;
}
$sale->sale_status=$req->input("sale_status");
$sale->save();
if ($req->input("sale_status")==3 || $req->input("sale_status")==4) {
if ($req->input("sale_status")==3) {
$stage="pickedup";
}elseif ($req->input("sale_status")==4) {
$stage="delivered";
}
// $client = new Client([
// 'auth' => ['Notification', 'jiFFNotification@2023!@!@!@']
// ]);
// $res= $client->request('POST', 'https://notification.businesscloud.xyz/delivery_order',[
// "json"=>[
// 'vendor_id' => auth()->user()['id'],
// 'stage'=>$stage,
// 'invoice_no'=>$sale->invoice_no
// ]
// ]);
}
if ($req->input("sale_status")==4) {
// $sale=SaleItem::select('variant_id')->where('sale_id',$sale_id)->get();
ProductVariants::join('sale_items', 'product_variants.id', '=', 'sale_items.variant_id')
->where('sale_items.sale_id', $sale_id)
->update(['product_variants.inventory' =>DB::raw('product_variants.inventory - sale_items.quantity')] );
}
$this->saleStatusChange($sale_id,$sale->invoice_no,$req->input("sale_status"),$vendor_id);
return response()->json(["message"=>"Order State changed successfully"]);
} catch (\Exception $exception) {
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
}
function saleStatusByCourier($sale_id,Request $req) {
try{
$sale=Sale::find($sale_id);
if (empty($sale)) {
return response()->json(["error"=>true,"message"=>"Invalid order"]);
}
$vendor_id=$sale->vendor_id;
$sale->sale_status=$req->input("sale_status");
$sale->save();
if ($req->input("sale_status")==3 || $req->input("sale_status")==4) {
if ($req->input("sale_status")==3) {
$stage="pickedup";
}elseif ($req->input("sale_status")==4) {
$stage="delivered";
}
// $client = new Client([
// 'auth' => ['Notification', 'jiFFNotification@2023!@!@!@']
// ]);
// $res= $client->request('POST', 'https://notification.businesscloud.xyz/delivery_order',[
// "json"=>[
// 'vendor_id' => auth()->user()['id'],
// 'stage'=>$stage,
// 'invoice_no'=>$sale->invoice_no
// ]
// ]);
}
if ($req->input("sale_status")==4) {
if ($sale->total_items!==$req->input("no_of_items")) {
ReturnRequest::create([
'sale_id'=>$sale_id,
'invoice_no'=>$sale->invoice_no,
'no_of_items'=>$sale->total_items-$req->input("no_of_items"),
'status'=>1,
'shop_id'=>$sale->shop_id,
'note'=>$req->input("note")
]);
}
$req_body=[
'sale_id'=>$sale_id,
'invoice_no'=>$sale->invoice_no,
'no_of_items'=>$sale->total_items-$req->input("no_of_items"),
'status'=>1,
'shop_id'=>$sale->shop_id,
'note'=>$req->input("note"),
"vendor_id" => $vendor_id
];
$client = new Client([
'auth' => ['Mongo_API', 'jiFFMongo@2023!@!@!@']
]);
$res = $client->request('POST', 'https://api.businesscloud.xyz/complete_sale',["json"=>$req_body]);
// $sale=SaleItem::select('variant_id')->where('sale_id',$sale_id)->get();
ProductVariants::join('sale_items', 'product_variants.id', '=', 'sale_items.variant_id')
->where('sale_items.sale_id', $sale_id)
->update(['product_variants.inventory' =>DB::raw('product_variants.inventory - sale_items.quantity')] );
}
$this->saleStatusChange($sale_id,$sale->invoice_no,$req->input("sale_status"),$vendor_id);
return response()->json(["message"=>"Order State changed successfully"]);
} catch (\Exception $exception) {
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
}
// return sale
function returnSale($sale_id,Request $req) {
try {
if (auth()->user()) {
$vendor_id =auth()->user()->id;
}else {
$vendor_id=$req->input("vendor_id");
}
// if (empty($req->input("sale_item"))) {
// return response()->json(["error"=>true,"message"=>"No Product Is selected"]);
// }
$sale=Sale::find($sale_id);
if (empty($sale)) {
return response()->json(["error"=>true,"message"=>"Invalid order"]);
}
// return response()->json($sale->invoice_no);
DB::beginTransaction();
$return_sale=Sale::create([
'invoice_no' => "Re-".$sale->invoice_no,
"shop_id" => $sale->shop_id,
"receiver_name" => $sale->receiver_name,
"receiver_phone" => $sale->receiver_phone,
'customer_id' => $sale->customer_id,
'customer' => $sale->customer,
'vendor_id' => $vendor_id,
'note' => $req->input("note"),
'total' => $req->input("total"),
// 'shipping' => $req->input("shipping"),
'grand_total' => $req->input("grand_total"),
'total_items' => $req->input("total_items"),
'sale_status' => 6,
'payment_status' => 2,
"type" => $sale->type,
"delivery_partner" => $sale->delivery_partner,
'address' => $sale->address,
'sale_id' => $sale->id
]);
// return response()->json(["message"=>"Done","data"=> auth()->user()->id]);
if ($return_sale) {
$sale->return_id=$return_sale->id;
$sale->return_sale_ref=$return_sale->invoice_no;
$sale->return_sale_total=$req->input("grand_total");
$sale->save();
try {
$products=explode("||",$req->input("return_items"));
for ($i=0; $i <count($products) ; $i++) {
$product_info=explode(",",$products[$i]);
$item = SaleItem::where('sale_id', '=', $sale->id)->where('variant_id', '=', $product_info[0])->first();
SaleItem::create([
'sale_id'=>$return_sale->id,
'product_id'=>$item->product_id,
"product_code"=>$item->product_code,
"product_name"=>$item->product_name,
"variant_id"=>$item->variant_id,
"unit_price"=>$item->unit_price,
"quantity"=>$product_info[1],
"subtotal"=>$item->unit_price*$product_info[1],
"sale_item_id"=>$item->id
]);
}
$sale_fee=SaleFee::where("sale_id",$sale_id)->first();
$jiff_fee=round($sale_fee->jiff_fee*($sale->total-$req->input("total"))/$sale->total,2);
if ($req->input("total_items")==$sale->total_items) {
$jiff_fee=0;
}
$new_fee_total=$sale_fee->total_fee+$jiff_fee-$sale_fee->jiff_fee;
// delivered by jiff or online payment
if ($sale->delivery_partner==1 ||$sale->payment_method==2) {
$vendor_rcv=$sale->grand_total-$req->input("grand_total")-$new_fee_total;
$vendor_pay=0;
if ($vendor_rcv<=0) {
$vendor_rcv=0;
$vendor_pay=$new_fee_total;
}
}
// delivered by vendor and cod payment
if ($sale->delivery_partner==2 && $sale->payment_method==1) {
$vendor_rcv=0;
$vendor_pay=$new_fee_total;
}
// customer on shop pickup and cod payment
if ($sale->delivery_partner==3 && $sale->payment_method==1) {
$vendor_rcv=0;
$vendor_pay=$new_fee_total;
}
$sale_fee->jiff_fee=$jiff_fee;
$sale_fee->grand_total=$sale->grand_total-$req->input("grand_total");
$sale_fee->vendor_payable=$vendor_pay;
$sale_fee->vendor_receivable=$vendor_rcv;
$sale_fee->total_fee=$new_fee_total;
$sale_fee->save();
$sales_dist=[
"jiff_fee"=>$jiff_fee,
"grand_total"=>$sale->grand_total-$req->input("grand_total"),
"total_fee"=>$new_fee_total,
"vendor_payable"=>round($vendor_pay,2),
"vendor_receivable"=>round($vendor_rcv,2),
'vendor_id' => $vendor_id,
"sale_id"=>$sale_id
];
$client = new Client([
'auth' => ['Mongo_API', 'jiFFMongo@2023!@!@!@']
]);
$res = $client->request('PUT', 'https://api.businesscloud.xyz/sale_fees',["json"=>$sales_dist]);
ProductVariants::join('sale_items', 'product_variants.id', '=', 'sale_items.variant_id')
->where('sale_items.sale_id', $return_sale->id)
->update(['product_variants.inventory' =>DB::raw('product_variants.inventory + ABS(sale_items.quantity)')] );
} catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
DB::commit();
// (new SaleFeesController)->orderFees($vendor_id,$sale->id,$sale->invoice_no,$req->input("total"),$req->input("grand_total"),$req->input('delivery_partner'),$req->input('payment_method'),0);
$d = new DateTime();
$t=$d->format("ymdHisv");
$data=["id"=>$return_sale->id,"_id"=>$t,"created_at"=>$return_sale->created_at,'invoice_no'=>$return_sale->invoice_no];
$client = new Client();
$vendor=StoreSettings::select('image_folder')->where('vendor_id',$vendor_id)->first();
$res = $client->request('post', env("SPACE_PROXY").'/invoice',["json"=>[
"sale_id"=>$sale->id,
"invoice_no"=>$sale->invoice_no,
"folder_name"=>$vendor->image_folder,
]]);
return response()->json(['message' => "Successfully Returned",'data' => $data]);
} else {
DB::rollback();
return response()->json(['error' => 'true','message' => "Something is wrong please try again"]);
}
} catch (\Exception $exception) {
DB::rollback();
return response()->json(['error' => 'true', 'message' =>$exception->getMessage()],500);
}
}
}
|