Sh3ll
OdayForums


Server : Apache
System : Linux 145.162.205.92.host.secureserver.net 5.14.0-611.45.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 1 05:56:53 EDT 2026 x86_64
User : tradze ( 1001)
PHP Version : 8.1.34
Disable Function : NONE
Directory :  /home/tradze/www/dev-test/app/Modules/Invoices/Repositories/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/tradze/www/dev-test/app/Modules/Invoices/Repositories/BookingInvoiceRepository.php
<?php
namespace App\Modules\Invoices\Repositories;

use App\Modules\Accounts\Models\Account;
use App\Modules\Accounts\Models\Seriesdoc;
use App\Modules\Invoices\Models\Invoice;
use App\Modules\Invoices\Models\Invoice_item;
use Illuminate\Support\Facades\DB;
use Laracasts\Flash\Flash;


class BookingInvoiceRepository
{

    protected $invoice_first_no = 1;
    protected $invoice_prefix = '';

    /**
     * Create new invoice based on user bookings
     * @param $booking
     */
    public function new_invoice($booking, $isAPI = false)
    {

        //get default account
        $account = Account::where('is_default','1')->first();
        //get series and invoice number
        $docSeries = Seriesdoc::where('account_id',$account->id)
        ->where('type','invoice_client')
        ->first();
        
        if(!$docSeries){
            if($isAPI) {
                return false;
            }else{
                flash('Something went wrong. Please check if the account you are trying generate an invoice for has a "Document Series" created with type "Client Invoice".','danger');
                return redirect(route('admin.invoices.index'));
            }
        }
        
        //set invoice prefix and invoice next number
        $invoice_prefix = $docSeries->series;
        $invoice_no = $docSeries->last_number+1;

        //prevent generating invoice for invalid booking
        if (!$booking->can_generate_invoice)
            return false;

        //get booking details
        $details = json_decode($booking->orderInfo,true);

        //create invoice array
        $inv_address = $booking->user->address()->orderBy('is_main','desc')->first();
        $invData = new Invoice([
            'account_id' => $account->id,
            'client_info' => json_encode([
                'name' => $booking->user->name,
                'address' => $inv_address?$inv_address->address:'',
                'postcode' => $inv_address?$inv_address->postcode:'',
            ]),
            'booking_id' => $booking->id,
            'user_id' => $booking->user_id,
            'prefix' => $invoice_prefix,
            'number' => $invoice_no,
            'inv_date' => date('Y-m-d'),
            'inv_duedate' => date('Y-m-d'),
            'amount_net' => $booking->amount,
            'tax_value' => 0,
            'amount' => $booking->amount,
        ]);

        //start transaction
        DB::beginTransaction();

        //create invoice
        //        $invoice = Invoice::create($invData);
        $booking->invoiceMorph()->save($invData);
        $invoice = $booking->invoiceMorph->first();

        //create invoice items/positions
        $invDetailsData[] = [
            'invoice_id'=>$invoice->id,
            'name'=>$details['massage_type'].' '.$details['duration'],
            'notes' => $details['date'].' '.$details['hour'],
            'qty'=>1,
            'um'=>'hour',
            'unit_price' => $details['price_net'],
            'amount_net' => $details['price_net'],
            'amount' => $details['price_net'],
            'tax_name' => '',
            'tax_value' => 0,
        ];

        //massage table
        if ($details['has_table']){
            $invDetailsData[] = [
                'invoice_id'=>$invoice->id,
                'name'=> 'massage table',
                'notes' => '',
                'qty'=>1,
                'um'=>'pcs',
                'unit_price' => $details['table_value'],
                'amount_net' => $details['table_value'],
                'amount' => $details['table_value'],
                'tax_name' => '',
                'tax_value' => 0,
            ];
        } //endif has table

        //transport
        if ($details['has_transport']) {
            $invDetailsData[] = [
                'invoice_id'=>$invoice->id,
                'qty' => 1,
                'name' => 'travel supplements',
                'notes' => '',
                'um'=>'pcs',
                'unit_price' => $details['transport_cost'],
                'amount_net' => $details['transport_cost'],
                'amount' => $details['transport_cost'],
                'tax_name' => '',
                'tax_value' => 0,
            ];
        } //endif has transport

        //voucher
        if ($details['has_voucher']){
            $voucher = $details['voucher'];
            $invDetailsData[] = [
                'invoice_id'=>$invoice->id,
                'qty'=>1,
                'name'=> 'Discount ',
                'notes' => 'according to voucher code: '.$voucher['code'],
                'um'=>'pcs',
                'unit_price' => $voucher['discount'],
                'amount_net' => $voucher['discount'],
                'amount' => $voucher['discount'],
                'tax_name' => '',
                'tax_value' => 0,
            ];
        } //endif has voucher

        if (@$details['has_extension']){
            if(isset($details['extension']) && count($details['extension']) > 0)
            {
                foreach ($details['extension'] as $extension)
                {
                    $invDetailsData[] = [
                        'invoice_id'=>$invoice->id,
                        'qty'=>1,
                        'name'=> 'Extension ',
                        'notes' => "of ".$extension['duration'],
                        'um'=>'pcs',
                        'unit_price' => $extension['price'],
                        'amount_net' => $extension['price'],
                        'amount' => $extension['price'],
                        'tax_name' => '',
                        'tax_value' => 0,
                    ];
                }
            }
        }

        //save invoice details
        foreach($invDetailsData as $invitem)
            Invoice_item::create($invitem);

        //increment series doc
        $docSeries->last_number = $docSeries->last_number+1;
        $docSeries->save();

        //commit/rollback
        if ($invoice){
            //commit transaction
            DB::commit();

            //return invoice object
            return $invoice;
        }
        else{
            //rollback
            DB::rollback();
            return null;
        } //endif commit/rollback
    }

    //salon
    public function new_salon_invoice($booking)
    {
        // dd($booking, "here");
        //get default account
        $account = Account::where('is_default','1')->first();

        //get series and invoice number
        $docSeries = Seriesdoc::where('account_id',$account->id)
            ->where('type','invoice_client')
            ->first();

        if(!$docSeries){
            flash('Something went wrong. Please check if the account you are trying generate an invoice for has a "Document Series" created with type "Client Invoice".','danger');
            return redirect(route('admin.invoices.index'));
        }

        //set invoice prefix and invoice next number
        $invoice_prefix = $docSeries->series;
        $invoice_no = $docSeries->last_number+1;

        //prevent generating invoice for invalid booking
        if (!$booking->can_generate_invoice)
            return false;

        //get booking details
        $details = json_decode($booking->orderInfo,true);
        // dd($details);
        //create invoice array
        $inv_address = $booking->user->address()->orderBy('is_main','desc')->first();
        $invData = new Invoice([
            'account_id' => $account->id,
            'client_info' => json_encode([
                'name' => $booking->user->name,
                'address' => $inv_address?$inv_address->address:'',
                'postcode' => $inv_address?$inv_address->postcode:'',
            ]),
            'booking_id' => $booking->id,
            'user_id' => $booking->user_id,
            'prefix' => $invoice_prefix,
            'number' => $invoice_no,
            'inv_date' => date('Y-m-d'),
            'inv_duedate' => date('Y-m-d'),
            'amount_net' => $booking->amount,
            'tax_value' => 0,
            'amount' => $booking->amount,
        ]);

        //start transaction
        DB::beginTransaction();

        //create invoice
//        $invoice = Invoice::create($invData);
        $booking->invoiceMorph()->save($invData);
        $invoice = $booking->invoiceMorph->first();

        //create invoice items/positions
        $invDetailsData[] = [
            'invoice_id'=>$invoice->id,
            'name'=>$details['treatment']['name'].' '.$details['duration']['duration'],
            'notes' => $details['booking_date'].' '.$details['booking_time'],
            'qty'=>1,
            'um'=>'hour',
            'unit_price' => $booking->amount,
            'amount_net' => $booking->amount,
            'amount' => $booking->amount,
            'tax_name' => '',
            'tax_value' => 0,
        ];

        if(!empty($details['has_table'])) {
            //massage table
            if ($details['has_table']){
                $invDetailsData[] = [
                    'invoice_id'=>$invoice->id,
                    'name'=> 'massage table',
                    'notes' => '',
                    'qty'=>1,
                    'um'=>'pcs',
                    'unit_price' => $details['table_value'],
                    'amount_net' => $details['table_value'],
                    'amount' => $details['table_value'],
                    'tax_name' => '',
                    'tax_value' => 0,
                ];
            } //endif has table
        }
        if(!empty($details['has_transport'])) {
            //transport
            if ($details['has_transport']) {
                $invDetailsData[] = [
                    'invoice_id'=>$invoice->id,
                    'qty' => 1,
                    'name' => 'travel supplements',
                    'notes' => '',
                    'um'=>'pcs',
                    'unit_price' => $details['transport_cost'],
                    'amount_net' => $details['transport_cost'],
                    'amount' => $details['transport_cost'],
                    'tax_name' => '',
                    'tax_value' => 0,
                ];
            } //endif has transport
        }

        if(!empty($details['has_voucher'])) {
            //voucher
            if ($details['has_voucher']){
                $voucher = $details['voucher'];
                $invDetailsData[] = [
                    'invoice_id'=>$invoice->id,
                    'qty'=>1,
                    'name'=> 'Discount ',
                    'notes' => 'according to voucher code: '.$voucher['code'],
                    'um'=>'pcs',
                    'unit_price' => $voucher['discount'],
                    'amount_net' => $voucher['discount'],
                    'amount' => $voucher['discount'],
                    'tax_name' => '',
                    'tax_value' => 0,
                ];
            } //endif has voucher
        }

        if(!empty(@$details['has_extension'])) {
            if (@$details['has_extension']){
                if(isset($details['extension']) && count($details['extension']) > 0)
                {
                    foreach ($details['extension'] as $extension)
                    {
                        $invDetailsData[] = [
                            'invoice_id'=>$invoice->id,
                            'qty'=>1,
                            'name'=> 'Extension ',
                            'notes' => "of ".$extension['duration'],
                            'um'=>'pcs',
                            'unit_price' => $extension['price'],
                            'amount_net' => $extension['price'],
                            'amount' => $extension['price'],
                            'tax_name' => '',
                            'tax_value' => 0,
                        ];
                    }
                }
            }
        }

        //save invoice details
        foreach($invDetailsData as $invitem)
            Invoice_item::create($invitem);

        //increment series doc
        $docSeries->last_number = $docSeries->last_number+1;
        $docSeries->save();

        //commit/rollback
        if ($invoice){
            //commit transaction
            DB::commit();

            //return invoice object
            return $invoice;
        }
        else{
            //rollback
            DB::rollback();
            return null;
        } //endif commit/rollback
    }

    /**
     * Cancel invoice
     * @param $invoice
     */
    public function cancel_invoice($invoice_id)
    {
        //get invoice
        $invoice = Invoice::find($invoice_id);
        if (!$invoice)
            return false;


        //update invoice status
        $invoice->is_canceled=1;
//        $invoice->is_refunded=1;
        $invoice->save();

        //return success response
        return true;
    }

    /**
     * Get next invoice number
     * @return int
     */
    protected function get_next_invoice_no()
    {
        //first invoice number
        $number=$this->invoice_first_no;

        $last = Invoice::orderBy('id','desc')->first();
        if (!$last)
            return $number;

        //next invoice no
        $number = $last->number+1;

        //return next invoice number
        return $number;
    }


}

ZeroDay Forums Mini