mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-03 23:07:32 -05:00 
			
		
		
		
	Refactor zips
This commit is contained in:
		
							parent
							
								
									2fe595d4c5
								
							
						
					
					
						commit
						306364ed08
					
				@ -502,7 +502,7 @@ class PurchaseOrderController extends BaseController
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            ZipPurchaseOrders::dispatch($purchase_orders, $purchase_orders->first()->company, auth()->user());
 | 
					            ZipPurchaseOrders::dispatch($purchase_orders->pluck("id")->toArray(), $purchase_orders->first()->company, auth()->user());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return response()->json(['message' => ctrans('texts.sent_message')], 200);
 | 
					            return response()->json(['message' => ctrans('texts.sent_message')], 200);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
@ -21,7 +21,6 @@ use App\Jobs\Invoice\InjectSignature;
 | 
				
			|||||||
use Illuminate\Support\Facades\Cache;
 | 
					use Illuminate\Support\Facades\Cache;
 | 
				
			||||||
use Illuminate\Contracts\View\Factory;
 | 
					use Illuminate\Contracts\View\Factory;
 | 
				
			||||||
use App\Models\PurchaseOrderInvitation;
 | 
					use App\Models\PurchaseOrderInvitation;
 | 
				
			||||||
use Illuminate\Support\Facades\Storage;
 | 
					 | 
				
			||||||
use App\Events\Misc\InvitationWasViewed;
 | 
					use App\Events\Misc\InvitationWasViewed;
 | 
				
			||||||
use App\Jobs\Vendor\CreatePurchaseOrderPdf;
 | 
					use App\Jobs\Vendor\CreatePurchaseOrderPdf;
 | 
				
			||||||
use App\Events\PurchaseOrder\PurchaseOrderWasViewed;
 | 
					use App\Events\PurchaseOrder\PurchaseOrderWasViewed;
 | 
				
			||||||
@ -119,14 +118,8 @@ class PurchaseOrderController extends Controller
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        $file = (new CreatePurchaseOrderPdf($invitation, $invitation->company->db))->rawPdf();
 | 
					        $file = (new CreatePurchaseOrderPdf($invitation, $invitation->company->db))->rawPdf();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // $headers = ['Content-Type' => 'application/pdf'];
 | 
					 | 
				
			||||||
        // $entity_string = $data['entity_type'];
 | 
					 | 
				
			||||||
        // $file_name = $invitation->{$entity_string}->numberFormatter().'.pdf';
 | 
					 | 
				
			||||||
        // return response()->streamDownload(function () use ($file) {
 | 
					 | 
				
			||||||
        //     echo $file;
 | 
					 | 
				
			||||||
        // }, $file_name, $headers);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $headers = ['Content-Type' => 'application/pdf'];
 | 
					        $headers = ['Content-Type' => 'application/pdf'];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return response()->make($file, 200, $headers);
 | 
					        return response()->make($file, 200, $headers);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -156,7 +149,7 @@ class PurchaseOrderController extends Controller
 | 
				
			|||||||
        $transformed_ids = $this->transformKeys($request->purchase_orders);
 | 
					        $transformed_ids = $this->transformKeys($request->purchase_orders);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if ($request->input('action') == 'download') {
 | 
					        if ($request->input('action') == 'download') {
 | 
				
			||||||
            return $this->downloadInvoices((array) $transformed_ids);
 | 
					            return $this->downloadPurchaseOrders((array) $transformed_ids);
 | 
				
			||||||
        } elseif ($request->input('action') == 'accept') {
 | 
					        } elseif ($request->input('action') == 'accept') {
 | 
				
			||||||
            return $this->acceptPurchaseOrder($request->all());
 | 
					            return $this->acceptPurchaseOrder($request->all());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -177,7 +170,8 @@ class PurchaseOrderController extends Controller
 | 
				
			|||||||
        $purchase_count_query = clone $purchase_orders;
 | 
					        $purchase_count_query = clone $purchase_orders;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $purchase_orders->whereIn('status_id', [PurchaseOrder::STATUS_DRAFT, PurchaseOrder::STATUS_SENT])
 | 
					        $purchase_orders->whereIn('status_id', [PurchaseOrder::STATUS_DRAFT, PurchaseOrder::STATUS_SENT])
 | 
				
			||||||
                        ->cursor()->each(function ($purchase_order) {
 | 
					                        ->cursor()
 | 
				
			||||||
 | 
					                        ->each(function ($purchase_order) {
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
                            $purchase_order->service()
 | 
					                            $purchase_order->service()
 | 
				
			||||||
                                        ->markSent()
 | 
					                                        ->markSent()
 | 
				
			||||||
@ -201,39 +195,39 @@ class PurchaseOrderController extends Controller
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public function downloadInvoices($ids)
 | 
					    public function downloadPurchaseOrders($ids)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $purchase_orders = PurchaseOrder::query()
 | 
					        $purchase_order_invitations = PurchaseOrderInvitation::query()
 | 
				
			||||||
                            ->whereIn('id', $ids)
 | 
					                            ->with('purchase_order', 'company')
 | 
				
			||||||
                            ->where('vendor_id', auth()->guard('vendor')->user()->vendor_id)
 | 
					                            ->whereIn('purchase_order_id', $ids)
 | 
				
			||||||
 | 
					                            ->where('vendor_contact_id', auth()->guard('vendor')->user()->id)
 | 
				
			||||||
                            ->withTrashed()
 | 
					                            ->withTrashed()
 | 
				
			||||||
                            ->get();
 | 
					                            ->get();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (count($purchase_orders) == 0) {
 | 
					        if (count($purchase_order_invitations) == 0) {
 | 
				
			||||||
            return back()->with(['message' => ctrans('texts.no_items_selected')]);
 | 
					            return back()->with(['message' => ctrans('texts.no_items_selected')]);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (count($purchase_orders) == 1) {
 | 
					        if (count($purchase_order_invitations) == 1) {
 | 
				
			||||||
            $purchase_order = $purchase_orders->first();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            $file = $purchase_order->service()->getPurchaseOrderPdf(auth()->guard('vendor')->user());
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $invitation = $purchase_order_invitations->first();
 | 
				
			||||||
 | 
					            $file = (new CreatePurchaseOrderPdf($invitation, $invitation->company->db))->rawPdf();
 | 
				
			||||||
            return response()->streamDownload(function () use ($file) {
 | 
					            return response()->streamDownload(function () use ($file) {
 | 
				
			||||||
                echo Storage::get($file);
 | 
					                echo $file;
 | 
				
			||||||
            }, basename($file), ['Content-Type' => 'application/pdf']);
 | 
					            }, $invitation->purchase_order->numberFormatter().".pdf", ['Content-Type' => 'application/pdf']);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return $this->buildZip($purchase_orders);
 | 
					        return $this->buildZip($purchase_order_invitations);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private function buildZip($purchase_orders)
 | 
					    private function buildZip($invitations)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        // create new archive
 | 
					        // create new archive
 | 
				
			||||||
        $zipFile = new \PhpZip\ZipFile();
 | 
					        $zipFile = new \PhpZip\ZipFile();
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            foreach ($purchase_orders as $purchase_order) {
 | 
					            foreach ($invitations as $invitation) {
 | 
				
			||||||
                //add it to the zip
 | 
					                $file = (new CreatePurchaseOrderPdf($invitation, $invitation->company->db))->rawPdf();
 | 
				
			||||||
                $zipFile->addFromString(basename($purchase_order->pdf_file_path()), file_get_contents($purchase_order->pdf_file_path(null, 'url', true)));
 | 
					                $zipFile->addFromString($invitation->purchase_order->numberFormatter().".pdf", $file);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            $filename = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.purchase_orders')).'.zip';
 | 
					            $filename = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.purchase_orders')).'.zip';
 | 
				
			||||||
 | 
				
			|||||||
@ -18,6 +18,7 @@ use App\Jobs\Vendor\CreatePurchaseOrderPdf;
 | 
				
			|||||||
use App\Libraries\MultiDB;
 | 
					use App\Libraries\MultiDB;
 | 
				
			||||||
use App\Mail\DownloadPurchaseOrders;
 | 
					use App\Mail\DownloadPurchaseOrders;
 | 
				
			||||||
use App\Models\Company;
 | 
					use App\Models\Company;
 | 
				
			||||||
 | 
					use App\Models\PurchaseOrderInvitation;
 | 
				
			||||||
use App\Models\User;
 | 
					use App\Models\User;
 | 
				
			||||||
use Illuminate\Bus\Queueable;
 | 
					use Illuminate\Bus\Queueable;
 | 
				
			||||||
use Illuminate\Contracts\Queue\ShouldQueue;
 | 
					use Illuminate\Contracts\Queue\ShouldQueue;
 | 
				
			||||||
@ -30,32 +31,12 @@ class ZipPurchaseOrders implements ShouldQueue
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 | 
					    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public $purchase_orders;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private $company;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    private $user;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    public $settings;
 | 
					    public $settings;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public $tries = 1;
 | 
					    public $tries = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    public function __construct(protected array $purchase_order_ids, protected Company $company, protected User $user)
 | 
				
			||||||
     * @param $purchase_orders
 | 
					 | 
				
			||||||
     * @param Company $company
 | 
					 | 
				
			||||||
     * @param $email
 | 
					 | 
				
			||||||
     * @deprecated confirm to be deleted
 | 
					 | 
				
			||||||
     * Create a new job instance.
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public function __construct($purchase_orders, Company $company, User $user)
 | 
					 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $this->purchase_orders = $purchase_orders;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $this->company = $company;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $this->user = $user;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $this->settings = $company->settings;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
@ -67,21 +48,23 @@ class ZipPurchaseOrders implements ShouldQueue
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        MultiDB::setDb($this->company->db);
 | 
					        MultiDB::setDb($this->company->db);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->settings = $this->company->settings;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // create new zip object
 | 
					        // create new zip object
 | 
				
			||||||
        $zipFile = new \PhpZip\ZipFile();
 | 
					        $zipFile = new \PhpZip\ZipFile();
 | 
				
			||||||
        $file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.purchase_orders')).'.zip';
 | 
					        $file_name = now()->addSeconds($this->company->timezone_offset())->format('Y-m-d-h-m-s').'_'.str_replace(' ', '_', trans('texts.purchase_orders')).'.zip';
 | 
				
			||||||
        $invitation = $this->purchase_orders->first()->invitations->first();
 | 
					 | 
				
			||||||
        $path = $this->purchase_orders->first()->vendor->purchase_order_filepath($invitation);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->purchase_orders->each(function ($purchase_order) {
 | 
					        $invitations = PurchaseOrderInvitation::query()
 | 
				
			||||||
            (new CreatePurchaseOrderPdf($purchase_order->invitations()->first()))->handle();
 | 
					                                            ->with('purchase_order')
 | 
				
			||||||
        });
 | 
					                                            ->whereIn('purchase_order_id', $this->purchase_order_ids)
 | 
				
			||||||
 | 
					                                            ->get();
 | 
				
			||||||
 | 
					        $invitation = $invitations->first();
 | 
				
			||||||
 | 
					        $path = $invitation->contact->vendor->purchase_order_filepath($invitation);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            foreach ($this->purchase_orders as $purchase_order) {
 | 
					            foreach ($invitations as $invitation) {
 | 
				
			||||||
                $file = $purchase_order->service()->getPurchaseOrderPdf();
 | 
					                $file = (new \App\Jobs\Vendor\CreatePurchaseOrderPdf($invitation))->rawPdf();
 | 
				
			||||||
                $zip_file_name = basename($file);
 | 
					                $zipFile->addFromString($invitation->purchase_order->numberFormatter().".pdf", $file);
 | 
				
			||||||
                $zipFile->addFromString($zip_file_name, Storage::get($file));
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Storage::put($path.$file_name, $zipFile->outputAsString());
 | 
					            Storage::put($path.$file_name, $zipFile->outputAsString());
 | 
				
			||||||
 | 
				
			|||||||
@ -31,7 +31,6 @@ class GetPurchaseOrderPdf extends AbstractService
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        $invitation = $this->purchase_order->invitations()->where('vendor_contact_id', $this->contact->id)->first();
 | 
					        $invitation = $this->purchase_order->invitations()->where('vendor_contact_id', $this->contact->id)->first();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (! $invitation) {
 | 
					        if (! $invitation) {
 | 
				
			||||||
            $invitation = $this->purchase_order->invitations()->first();
 | 
					            $invitation = $this->purchase_order->invitations()->first();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user