Save PayuMoney transaction in differnet database table - php

I am using laravel 9 and making payment integration using tszk/payu package , here is reference link https://github.com/tzsk/payu/blob/1.2.4/README.md that i am following.
my code is below:
public function payment(Request $request)
{
$customer = Customer::make()
->firstName(Auth::user()->name)
->email(Auth::user()->email);
$transaction = Transaction::make()
->charge($request->amount)
->for('Product')
->against(User::find(Auth::user()->id))
->to($customer);
return Payu::initiate($transaction)->redirect(route('payment.status'));
}
Is it possible to save payment against individuals payments.

Related

how to automatically update one table column by another table column update in laravel

I have a WALLET table with a Balance Column & I have a PAYMENT table with a Price Column I want to automatically the WALLET BALANCE Column update When the PAYMENT PRICE Column is updated or NEW PAYMENT is Created.
What Should I Do?
In laravel you can use Observers, please see this
observer code sample:
<?php
namespace App\Observers;
use App\Models\Payment;
class PaymentObserver
{
public function created(Payment $payment)
{
$wallet = $payment->wallet;
$wallet->balance = $payment->price;
$wallet->save();
}
public function updated(Payment $payment)
{
if($payment->wasChanged('price')){
$wallet = $payment->wallet;
$wallet->balance = $payment->price;
$wallet->save();
}
}
}

how to apply payments to credits by seniority date automatically in Laravel

I have my .blade view where I show two tables of a specific person, the first is where the credits are shown, the second shows the payments made.
What I want to achieve is that when making a subscription, I subtract the oldest credit and place the credit as liquidated and if it has more credits and there is an excess of the subscription, the rest is applied.
Simulating an example: the person with id = 1 has two pending credits, one on 02/25/2022 and another on 02/26/2022 of $1000 each, now, he makes a payment for $1200 and this payment would subtract $1000 from the oldest then the first credit would be liquidated and no more could be paid, the remaining $200 would be applied to the next credit, leaving a balance of $800 and so on until it was $0.
It should be noted that my migration 'credits' has two columns, one is 'amount' where it stores the real value of the credit and 'amount_decrement' where it takes the value of 'amount' and later the subtraction would be performed and the column would be updated
Model Credit
public function client(){
return $this->belongsTo(Client::class);
}
public function payments(){
return $this->hasMany(Payment::class);
}
Model Client
public function credits(){
return $this->hasMany(Credit::class);
}
public function payments(){
return $this->hasManyThrough(Payment::class, Credit::class);
}
Model Payment
public function credit(){
return $this->belongsTo(Credit::class);
}
public function user(){
return $this->belongsTo(User::class);
}
Componente Livewire: ShowCreditClient
public $client;
public $credit = [];
public function loadPosts(){
$this->credit = $this->client->credits()->take(15)->get();
}
public function render()
{
return view('livewire.show-credit-client');
}

How to fetch customer information through an eloquent model

I have three models: Payment, Booking and Customer. A payment relation belongs to the Booking model and the Booking model belongs to the Customer model.
I need to make a customer relation inside the Payment model, however, they are not related, the only relation between them is through the Booking model.
My code:
public function booking(){
return $this->belongsTo(Booking::class , 'payable_id' , 'id')->with('customer');
}
// what i need , i need to consume that booking function to get customer info
// something like this
public function customer(){
// consume the booking relation
// return customer info
}
Could you please advise me on the right approach?
The answer to that is a Has One Through or Has Many Through relationship, depending on your business model.
Class Payment {
public function customer()
{
return $this->hasOneThrough('Customer', 'Booking');
}
// or:
public function customers()
{
return $this->hasManyThrough('Customer', 'Booking');
}
}
Hope it helps.
Based on your question you want to get customer information through "booking" let's say it is your lookup table where a customer id and payment id fields exist.
It should look like this
ERD of table image
In your Booking Model you have should two functions
public function customer() {
return $this->belongsTo(Customer::class, 'cust_id', 'id');
}
public function payment() {
return $this->belongsTo(Payment::class, 'payable_id', 'id');
}
In both your Customer and Payment Models add
public function bookings() {
return $this->hasMany(Booking::class, '(cust_id or payable_id respectively)', 'id');
}
in case you want to access booking via customer/payment models as well.
Now with those relationships you can access both payment and customer by accessing booking.
e.g.
$customers = Booking::all()->with('customer');
$payments = Booking::all()->with('payment');
Now you can manipulate all other data for display and/or editing or whatever you need to do with it.
You can also now access a customer's data with all of his/her bookings and payments associated. Below code results a collection of payments based on c
$customer = Customer::find(1)->with('bookings')->first();
$payments = $customer->bookings->payment;
For more questions feel free to delve in to the Official Laravel documentations.
In answering your question I based it on this.
https://laravel.com/docs/5.8/eloquent-relationships
Hope I helped and cheers!

Is there a laravel function or method for synchronizing updated items

I have build my application with laravel (one to many relations table) ,and i want to synchronize two update functions .I mean when i update item in this function will update automatically in other function.
I already create the update functions and works well.What i need to do know is to synchronize the tow updates functions.
$user= User::find($request->userid);
$fiscal = Fiscal::find($request->fiscal_id);
$contractuser->fiscal_id= $request->get('fiscal_id');
$contractuser->contract_id= $request->get('contractid');
$contractuser->save();
return redirect()->route('contracts.show',$contractuser->user_id)->with('success','User Contract has been added successfully');
This is for contractuser
public function update(Request $request, $id)
{
$fiscal=Fiscal::find($id);
$fiscal->name=$request->get('name');
$fiscal->year=$request->year;
$fiscal->save();
return redirect('fiscals');
}
This is for fiscals
public function update(Request $request, $id)
{
$contract=Contract::find($id);
$contract->name=$request->get('name');
$contract->save();
return redirect('contracts');
}
This is for contract.
The results are good but i want every time i change the fiscal or contract this change will be automatically on user contract .

How to organize this context? Two tables or just one?

I have a context of registrations at conferences. A registration has only one step if is a free registration. If is a paid registration there are two steps.
The first step of the registration consists of the user fill in a registration form and after click in the "Store Registration" button the info is stored in database and the column "status" of the registratins table is stored always with value "I" (incomplete).
If is a free registration there are no additional steps.
But if its a paid registration there is another step. In this step 2 the user needs to pay the registration before the conference start date. After the payment of the registration the column "status" of the registrations table stays with value "C" (complete).
Doubt:
If is a paid registration, after the step1 is necessary to issue a proforma invoice to the user and after the step2, that is, after the payment is necessary to issue a invoice/receipt to the user.
My doubt is how to organize the database for this context of issue proformas and invoices. In each paid registration is necessary to issue first a proforma after the user click in the "Store registration" button and all registration info is stored in db and then an issue an invoice after the payment. Is necessary to have two models/tables "Invoices" and "Proformas" and each one has a 1 to 1 relationship with Registration table. Or its just necessary 1 table related to the Registration table?
For now, I created two tables Proforma and Invoice and the relationships of these tables with the Registration table is like below 1 to 1, but I dont know if its correct.
Tables structure:
registration columns: id, status, conference_id, user_that_did_registration
proformas: id, proforma_number, registration_id
invoices: id, invoice_number, registration_id
Relevant models for the question:
Registration Model:
class Registration extends Model
{
public function customer(){
return $this->belongsTo(User::class, 'user_that_did_registration', 'id');
}
public function participants(){
return $this->hasMany('App\Participant');
}
public function registration_types(){
return $this->belongsToMany('App\RegistrationType', 'registration_registration_types');
}
public function conference(){
return $this->belongsTo('App\conference');
}
public function proforma()
{
return $this->hasOne('App\Proforma');
}
public function invoice()
{
return $this->hasOne('App\Invoice');
}
}
Proforma model:
class Proforma extends Model
{
public function registration()
{
return $this->belongsTo('App\Registration');
}
}
Invoice model:
class Invoice extends Model
{
public function registration()
{
return $this->belongsTo('App\Registration');
}
}
Based on your description, your design looks pretty close. Since not all Registrations have Proformas or Invoices, it makes sense to have Proformas and Invoices in a separate table. I would only make one change - if the relationship between Proformas and Invoices is truly 1 to 1, then you can put all the fields in a single record:
Proformas
ID
Registration_ID
Proforma_Number
Invoice_Number

Categories