Copy Copied! Back
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\DB; use App\Helpers\Logger\Logger; use \App\Helpers\Oversize\OversizeHelper as OversizeHelper; class OversizeConfirmationNotice extends Command { protected $signature = 'command:OversizeConfirmationNotice'; protected $description = 'Command Send Confirmation of Estimate Notice to buyer'; private $_loggerObject = null; private $OversizeHelper; public function __construct() { parent::__construct(); $this->_loggerObject = new Logger("OversizeConfirmationNotice"); $this->sas_url = config('domain.sas'); // "127.0.0.1:8002"; $this->OversizeHelper = new OversizeHelper(); } public function handle() { $this->_loggerObject->logBatchInfo("OversizeConfirmationNotice Batch: Start"); $bundling_id = null; $mail_to = null; //Get list of orders to email $obj = DB::table('t_bundling as tbb') ->where ('confirmation_mail_status_id', '=', '1') ->whereNull('tbb.deleted_at') ->get(); $bundlings = json_decode($obj, true); foreach ($bundlings as $bundling){ try{ $bundling_id = $bundling['bundling_id']; $buyer_id = $bundling['buyer_id']; $shipping_type = $bundling['shipping_type']; if ($bundling['shipping_fixed_flg'] == 1){ $shipping_fixed_flg = true; } else{ $shipping_fixed_flg = false; } $obj = DB::table('h_bundling_log as hbl') ->where('hbl.bundling_id', '=', $bundling_id) ->orderBy('bundling_log_id', 'desc') ->limit(1) ->get(); if ($obj->isEmpty()) { $this->OversizeHelper->insertError('confirmation_mail_status_id', $bundling_id); $this->_loggerObject->logBatchError('Payment Information NOT available for '.$bundling_id.'.'); continue; } $h_bundling_info = json_decode($obj, true)[0]; $main_sales_id = $bundling['main_sales_id']; $order_no = (substr($main_sales_id, -4)); $arr["buyer_id"] = $buyer_id; //uses name_jp $arr["final_amount_yen"] = number_format($h_bundling_info['air_freight_total_yen'] + $h_bundling_info['fsc_total_yen'] + $h_bundling_info['duty'] + $h_bundling_info['tax'] + $h_bundling_info['cost_in_japan']); $arr["air_freight_total_yen"] = number_format($h_bundling_info['air_freight_total_yen']); $arr["fsc_total_yen"] = number_format($h_bundling_info['fsc_total_yen']); $arr["due_payment_date"] = date("m-d", strtotime("+8 days")); $arr["order_no"] = $order_no; $arr["tax"] = number_format($bundling['tax'] + $bundling['cost_in_japan'] ?? 0); $arr["duty"] = number_format($bundling['duty']); if ($shipping_type == "Oversize") { $arr['template_name'] = ($shipping_fixed_flg) ? 'oversize_estimate_comp_new' : 'oversize_estimate_comp'; } else if ($shipping_type == "Air Cargo") { $arr['template_name'] = ($shipping_fixed_flg) ? 'aircargo_estimate_comp_new' : 'aircargo_estimate_comp'; } else if ($shipping_type == "Air Cargo Dangerous") { $arr['template_name'] = ($shipping_fixed_flg) ? 'aircargo_danger_estimate_comp_new' : 'aircargo_danger_estimate_comp'; } else if ($shipping_type == "Expensive" || $shipping_type == "Expensive Register") { $arr["air_freight_total_yen_and_fsc"] = number_format($h_bundling_info['air_freight_total_yen'] + $h_bundling_info['fsc_total_yen']); $arr["duty_and_tax"] = number_format($bundling['tax'] + $bundling['duty']); $arr['template_name'] = 'expensive_estimate_comp'; } $url = $this->sas_url . '/api/customer/?user_id='.$bundling['buyer_id']; $customer = \Helper::callApi($url); $mail_to = $customer[0]['email']; //get customer email //$mail_to = 'patrick.gourley@shopairlines.com'; //testing if (!filter_var($mail_to, FILTER_VALIDATE_EMAIL)) { $this->OversizeHelper->insertError('confirmation_mail_status_id', $bundling_id); $this->_loggerObject->logBatchError('Error - Buyer email '.$mail_to.' is not valid'); continue; } //insert Payment $buyer_payment_id = $this->OversizeHelper->insertPayFromBuyer($bundling['buyer_payment_id'], $h_bundling_info['final_amount_yen'], $bundling['buyer_id'], $bundling['shipping_type'], $main_sales_id); if (!is_numeric($buyer_payment_id)) { $this->OversizeHelper->insertError('confirmation_mail_status_id', $bundling_id); $this->_loggerObject->logBatchError('Inserting Payment Information for '.$bundling_id.' Failed.'); continue; } $sales = $this->OversizeHelper->getSalesInfo($bundling_id); $contacted_buyer_date = date('Y-m-d'); $remind_buyer_date = date('Y-m-d', strtotime("+8 day", strtotime($contacted_buyer_date))); $t_bundling = [ 'manual_status' => "Contacted Buyer", 'buyer_payment_id' => $buyer_payment_id, 'contacted_buyer_date' => $contacted_buyer_date, 'remind_buyer_date' => $remind_buyer_date, 'confirmation_mail_status_id' => 2, 'updated_staff_id' => 'batch' ]; DB::beginTransaction(); DB::table('t_bundling')->where('bundling_id', $bundling_id) ->update($t_bundling); if($bundling['shipping_type'] == "Expensive"){ $box_action = "Expensive_Contacted Buyer"; }else{ $box_action = "Oversize_Contacted Buyer"; } $sales_ids_unique = array_unique(array_flatten($this->OversizeHelper->getSalesIds($bundling_id))); $this->OversizeHelper->insertItemStatus($sales_ids_unique, $box_action); $this->OversizeHelper->sendMail($mail_to, $sales, $arr, $bundling['main_sales_id']); $this->_loggerObject->logBatchInfo('The email for '. $bundling_id.' has been sent to '.$mail_to. ' successfully'); DB::commit(); } catch (\Exception $ex) { DB::rollback(); $this->OversizeHelper->insertError('confirmation_mail_status_id', $bundling_id); $this->_loggerObject->logBatchError('The email for '.$bundling_id.' could not be sent to '.$mail_to. ': '. $ex->getMessage()); } } $this->_loggerObject->logBatchInfo("OversizeConfirmationNotice Batch: End"); } }