Refactor zips

This commit is contained in:
David Bomba 2023-09-05 11:16:47 +10:00
parent 2fe595d4c5
commit 306364ed08
4 changed files with 35 additions and 59 deletions

View File

@ -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);
}

View File

@ -21,7 +21,6 @@ use App\Jobs\Invoice\InjectSignature;
use Illuminate\Support\Facades\Cache;
use Illuminate\Contracts\View\Factory;
use App\Models\PurchaseOrderInvitation;
use Illuminate\Support\Facades\Storage;
use App\Events\Misc\InvitationWasViewed;
use App\Jobs\Vendor\CreatePurchaseOrderPdf;
use App\Events\PurchaseOrder\PurchaseOrderWasViewed;
@ -119,14 +118,8 @@ class PurchaseOrderController extends Controller
$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'];
return response()->make($file, 200, $headers);
}
@ -156,7 +149,7 @@ class PurchaseOrderController extends Controller
$transformed_ids = $this->transformKeys($request->purchase_orders);
if ($request->input('action') == 'download') {
return $this->downloadInvoices((array) $transformed_ids);
return $this->downloadPurchaseOrders((array) $transformed_ids);
} elseif ($request->input('action') == 'accept') {
return $this->acceptPurchaseOrder($request->all());
}
@ -177,7 +170,8 @@ class PurchaseOrderController extends Controller
$purchase_count_query = clone $purchase_orders;
$purchase_orders->whereIn('status_id', [PurchaseOrder::STATUS_DRAFT, PurchaseOrder::STATUS_SENT])
->cursor()->each(function ($purchase_order) {
->cursor()
->each(function ($purchase_order) {
$purchase_order->service()
->markSent()
@ -201,39 +195,39 @@ class PurchaseOrderController extends Controller
}
}
public function downloadInvoices($ids)
public function downloadPurchaseOrders($ids)
{
$purchase_orders = PurchaseOrder::query()
->whereIn('id', $ids)
->where('vendor_id', auth()->guard('vendor')->user()->vendor_id)
$purchase_order_invitations = PurchaseOrderInvitation::query()
->with('purchase_order', 'company')
->whereIn('purchase_order_id', $ids)
->where('vendor_contact_id', auth()->guard('vendor')->user()->id)
->withTrashed()
->get();
if (count($purchase_orders) == 0) {
if (count($purchase_order_invitations) == 0) {
return back()->with(['message' => ctrans('texts.no_items_selected')]);
}
if (count($purchase_orders) == 1) {
$purchase_order = $purchase_orders->first();
$file = $purchase_order->service()->getPurchaseOrderPdf(auth()->guard('vendor')->user());
if (count($purchase_order_invitations) == 1) {
$invitation = $purchase_order_invitations->first();
$file = (new CreatePurchaseOrderPdf($invitation, $invitation->company->db))->rawPdf();
return response()->streamDownload(function () use ($file) {
echo Storage::get($file);
}, basename($file), ['Content-Type' => 'application/pdf']);
echo $file;
}, $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
$zipFile = new \PhpZip\ZipFile();
try {
foreach ($purchase_orders as $purchase_order) {
//add it to the zip
$zipFile->addFromString(basename($purchase_order->pdf_file_path()), file_get_contents($purchase_order->pdf_file_path(null, 'url', true)));
foreach ($invitations as $invitation) {
$file = (new CreatePurchaseOrderPdf($invitation, $invitation->company->db))->rawPdf();
$zipFile->addFromString($invitation->purchase_order->numberFormatter().".pdf", $file);
}
$filename = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.purchase_orders')).'.zip';

View File

@ -18,6 +18,7 @@ use App\Jobs\Vendor\CreatePurchaseOrderPdf;
use App\Libraries\MultiDB;
use App\Mail\DownloadPurchaseOrders;
use App\Models\Company;
use App\Models\PurchaseOrderInvitation;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@ -30,32 +31,12 @@ class ZipPurchaseOrders implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $purchase_orders;
private $company;
private $user;
public $settings;
public $tries = 1;
/**
* @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)
public function __construct(protected array $purchase_order_ids, protected Company $company, protected 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);
$this->settings = $this->company->settings;
// create new zip object
$zipFile = new \PhpZip\ZipFile();
$file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.purchase_orders')).'.zip';
$invitation = $this->purchase_orders->first()->invitations->first();
$path = $this->purchase_orders->first()->vendor->purchase_order_filepath($invitation);
$file_name = now()->addSeconds($this->company->timezone_offset())->format('Y-m-d-h-m-s').'_'.str_replace(' ', '_', trans('texts.purchase_orders')).'.zip';
$this->purchase_orders->each(function ($purchase_order) {
(new CreatePurchaseOrderPdf($purchase_order->invitations()->first()))->handle();
});
$invitations = PurchaseOrderInvitation::query()
->with('purchase_order')
->whereIn('purchase_order_id', $this->purchase_order_ids)
->get();
$invitation = $invitations->first();
$path = $invitation->contact->vendor->purchase_order_filepath($invitation);
try {
foreach ($this->purchase_orders as $purchase_order) {
$file = $purchase_order->service()->getPurchaseOrderPdf();
$zip_file_name = basename($file);
$zipFile->addFromString($zip_file_name, Storage::get($file));
foreach ($invitations as $invitation) {
$file = (new \App\Jobs\Vendor\CreatePurchaseOrderPdf($invitation))->rawPdf();
$zipFile->addFromString($invitation->purchase_order->numberFormatter().".pdf", $file);
}
Storage::put($path.$file_name, $zipFile->outputAsString());

View File

@ -31,7 +31,6 @@ class GetPurchaseOrderPdf extends AbstractService
$invitation = $this->purchase_order->invitations()->where('vendor_contact_id', $this->contact->id)->first();
if (! $invitation) {
$invitation = $this->purchase_order->invitations()->first();
}