model = $model; } public function create($request = []) { //need invoice $data $number = Invoice::getInvoiceNumber(); if($payt = $this->model->getLastShoppingPaymentTransaction()){ $invoice_date = $payt->created_at->format("d.m.Y"); } $this->invoice_date = isset($request['invoice_date']) ? $request['invoice_date'] : $invoice_date; $invoice_send_mail = isset($request['invoice_send_mail']) ? false : true; $this->invoice_number = Invoice::createInvoiceNumber($number, $this->invoice_date); $this->dir = Invoice::getInvoiceStorageDir($this->invoice_date); $this->filename = Invoice::makeInvoiceFilename($this->invoice_number); $this->delivery_dir = Invoice::getDeliveryStorageDir($this->invoice_date); $this->delivery_filename = Invoice::makeDeliveryFilename($this->invoice_number); $this->makePDF(); $user_invoice = UserInvoice::create([ 'shopping_order_id' => $this->model->id, 'year' => \Carbon::parse($this->invoice_date)->format('Y'), 'month' => \Carbon::parse($this->invoice_date)->format('m'), 'date' => $this->invoice_date, 'full_number' => $this->invoice_number, 'number' => $number, 'filename' => $this->filename, 'dir' => $this->dir, 'delivery_filename' => $this->delivery_filename, 'delivery_dir' => $this->delivery_dir, 'disk' => 'public', 'status' => $this->model->getStatusByOrder() ]); Invoice::makeNextInvoiceNumber(); if($invoice_send_mail){ Invoice::sendInvoiceMail($this->model, $user_invoice); } return $user_invoice; } public function update($request = []){ if($user_invoice = $this->model->user_invoice){ $number = $user_invoice->number; $this->invoice_date = isset($request['invoice_date']) ? $request['invoice_date'] : $user_invoice->date; $invoice_send_mail = isset($request['invoice_send_mail']) ? false: true; $this->invoice_number = Invoice::createInvoiceNumber($number, $this->invoice_date); $this->dir = Invoice::getInvoiceStorageDir($this->invoice_date); $this->filename = Invoice::makeInvoiceFilename($this->invoice_number); $this->delivery_dir = Invoice::getDeliveryStorageDir($this->invoice_date); $this->delivery_filename = Invoice::makeDeliveryFilename($this->invoice_number); $this->user_sales_volume = UserSalesVolume::where('user_invoice_id', $this->model->user_invoice->id)->first(); $this->makePDF(); $user_invoice->fill([ 'shopping_order_id' => $this->model->id, 'year' => \Carbon::parse($this->invoice_date)->format('Y'), 'month' => \Carbon::parse($this->invoice_date)->format('m'), 'date' => $this->invoice_date, 'full_number' => $this->invoice_number, 'number' => $number, 'filename' => $this->filename, 'dir' => $this->dir, 'delivery_filename' => $this->delivery_filename, 'delivery_dir' => $this->delivery_dir, 'disk' => 'public', ])->save(); if($invoice_send_mail){ Invoice::sendInvoiceMail($this->model, $user_invoice); } return $user_invoice; } return null; } private function makePDF(){ $data = [ 'shopping_order' => $this->model, 'invoice_date' => $this->invoice_date, 'invoice_number' => $this->invoice_number, 'user_sales_volume' => $this->user_sales_volume, ]; if($this->model->auth_user_id){ UserService::checkUserTaxShippingCountry($this->model->auth_user, $this->model->country_id); $data = array_merge($data, UserService::getYardInfo()); } if(!Storage::disk('public')->exists( $this->dir )){ Storage::disk('public')->makeDirectory($this->dir); //creates directory } if(!Storage::disk('public')->exists( $this->delivery_dir )){ Storage::disk('public')->makeDirectory($this->delivery_dir); //creates directory } $path = Storage::disk('public')->getAdapter()->getPathPrefix(); //invoice $pdf_file = new InvoicePDF('pdf.invoice'); $pdf_file->create($data, $this->filename, 'save', $path.$this->dir); $pdfMerger = new MyPDFMerger(); $pdfMerger->addPDF($path.$this->dir.$this->filename); $file = $pdfMerger->myMerge('string', $this->filename, 'template_invoice_de'); Storage::disk('public')->put($this->dir.$this->filename, $file); if(!$this->model->shopping_collect_order){ $pdf_file = new InvoicePDF('pdf.delivery'); $pdf_file->create($data, $this->delivery_filename, 'save', $path.$this->delivery_dir); $pdfMerger = new MyPDFMerger(); $pdfMerger->addPDF($path.$this->delivery_dir.$this->delivery_filename); $file = $pdfMerger->myMerge('string', $this->delivery_filename, 'template_invoice_de'); Storage::disk('public')->put($this->delivery_dir.$this->delivery_filename, $file); } } public function userSalesVolume() { /* status 1 => 'hinzugefügt aus Bestellung', 2 => 'hinzugefügt aus Shop', 3 => 'hinzugefügt aus Shop / pending', */ $status = UserSalesVolume::getStatusByOrder($this->model); $user_id = $this->model->auth_user_id ? $this->model->auth_user_id : $this->model->member_id; //akuteller tag / Monat. $month = date('m'); $year = date('Y'); $date = date('d.m.Y'); if($status === 3){ //shop bestellung User pending $user_id = $this->model->auth_user_id ? $this->model->auth_user_id : $this->model->member_id; $month_points = 0; $month_total_net = 0; $month_shop_points = 0; $month_shop_total_net = 0; }else{ $month_points = UserSalesVolume::where('user_id', $user_id)->where('status', 1)->where('month', $month)->where('year', $year)->sum('points'); $month_total_net = UserSalesVolume::where('user_id', $user_id)->where('status', 1)->where('month', $month)->where('year', $year)->sum('total_net'); $month_shop_points = UserSalesVolume::where('user_id', $user_id)->where('status', 2)->where('month', $month)->where('year', $year)->sum('points'); $month_shop_total_net = UserSalesVolume::where('user_id', $user_id)->where('status', 2)->where('month', $month)->where('year', $year)->sum('total_net'); } switch ($status) { case 1: //Bestellung $month_points += $this->model->points; $month_total_net += $this->model->subtotal; break; case 2: //Shop $month_shop_points += $this->model->points; $month_shop_total_net += $this->model->subtotal; break; } return UserSalesVolume::create([ 'user_id' => $user_id, 'shopping_order_id' => $this->model->id, 'month' => $month, 'year' => $year, 'date' => $date, 'points' => $this->model->points, 'month_points' => $month_points, 'month_shop_points' => $month_shop_points, 'total_net' => $this->model->subtotal, 'month_total_net' => $month_total_net, 'month_shop_total_net' => $month_shop_total_net, 'message' => '', 'status' => $status, ]); } public function createAndSalesVolume($request = []) { $this->user_sales_volume = $this->userSalesVolume(); $user_invoice = $this->create($request); $this->user_sales_volume->user_invoice_id = $user_invoice->id; $this->user_sales_volume->save(); } }