Advertisement
aadhar-matellio

csv-generate-payment-cloud-consultations-list

Dec 21st, 2021
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.55 KB | None | 0 0
  1. /**
  2.      * csvGeneratePaymentCloudConsultation function to CSV Generate Coverwallet Consultation
  3.      *
  4.      * @param
  5.      * @return view
  6.      * @author matellio
  7.      * @Cron_time At 00:00  every day at midnight.
  8.      * @url : 0 0 * * 0 curl http://54.92.241.100/v1/admin/public/csv-generate-payment-cloud-consultations-list
  9.      */
  10.     public function csvGeneratePaymentCloudConsultation()
  11.     {
  12.         try {
  13.             ini_set('memory_limit', '6144M');
  14.             ini_set('max_execution_time', 0); //300 seconds = 5 minutes
  15.             $today = new \DateTime();
  16.             $date = $today->format('Y-m-d');
  17.             $weekDays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
  18.             $yesterday = date("Y-m-d", strtotime( '-1 days' ) );
  19.             if (in_array(date('l'), $weekDays)) {
  20.                 $query_order_status = Incorporations::select(
  21.                     "incorporations.order_id",
  22.                     "orders.customer_id",
  23.                     "orders.order_completion_date",
  24.                     "order_statuses.approval_date",
  25.                     "states.abbr",
  26.                     "business_types.bt_name",
  27.                     "business_infos.biz_name",
  28.                     "business_types.id As biz_type",
  29.                     "business_infos.biz_address",
  30.                     "states2.abbr as biz_formation_state",
  31.                     "business_infos.biz_city",
  32.                     "business_infos.biz_zip",
  33.                     "business_infos.biz_category"
  34.                 )
  35.                     ->join("orders", "orders.id", "=", "incorporations.order_id")
  36.                     ->join("order_statuses", "order_statuses.order_id", "=", "incorporations.order_id")
  37.                     ->leftJoin("business_infos", "incorporations.order_id", "=", "business_infos.order_id")
  38.                     ->leftJoin("business_types", "business_infos.biz_type", "=", "business_types.id")
  39.                     ->leftJoin("states", "business_infos.biz_state", "=", "states.id")
  40.                     ->leftJoin("states As states2", "business_infos.biz_formation_state", "=", "states2.id")
  41.                     ->where("orders.parent_order_status", $this->order_status_active)
  42.                     //->where("orders.order_completion_date", ">=", '2020-11-03 00:00:00')
  43.                 ->where("incorporations.payment_cloud", "Yes");
  44.                
  45.                 if(date('l') == "Monday"){
  46.                     $from = date("Y-m-d", strtotime( '-3 days' ) );
  47.                         $query_order_status = $query_order_status->whereRaw("(orders.order_completion_date >= ? AND orders.order_completion_date <= ?)",[$from." 00:00:00", $yesterday." 23:59:59"]);
  48.                 }else{
  49.                         $query_order_status = $query_order_status->whereDate('orders.order_completion_date',$yesterday);
  50.                 }
  51.                    
  52.                     $query_order_status = $query_order_status->get();
  53.                     //->whereNull("order_statuses.payroll_processed")
  54.                
  55.  
  56.                 $excel_rows = 'First Name' . "\t" .
  57.                     'Last Name' . "\t" .
  58.                     'Business Name' . "\t" .
  59.                     'Email' . "\t" .
  60.                     'Phone Number' . "\t" .
  61.                     'Address' . "\t" .
  62.                     'City' . "\t" .
  63.                     'State' . "\t" .
  64.                     'Zip' . "\t" .
  65.                     'Industry' . "\t" .
  66.                     'Entity Type' . "\t" .
  67.                     'Date Completed' . "\n";
  68.                 if ($query_order_status->count() > 0) {
  69.                     foreach ($query_order_status as $key => $row_order_status) {
  70.                         $order_id = $row_order_status->order_id;
  71.                         // Get the customer's details
  72.                         $customerInfo = User::where(['id' => $row_order_status->customer_id])->first();
  73.                         if (preg_match('/[\'^£$%&*()}{@#~?><>,0-9|=_+¬-]/', $customerInfo->first_name) || preg_match('/[\'^£$%&*()}{@#~?><>,0-9|=_+¬-]/', $customerInfo->last_name)) {
  74.                             continue;
  75.                         }
  76.                         $indicatorIncluded = false;
  77.  
  78.                         if (!empty($row_order_status->biz_type)) {
  79.                             if ($row_order_status->biz_type == 1) {
  80.                                 $validIndicators = array('LLC', 'L.L.C.', 'LC', 'L.C.', 'Ltd. Co.', 'Ltd. Co', 'Ltd Co', 'Ltd Co.', 'Ltd. Company', 'Ltd. Liability Co.', 'Ltd. Liability Company', 'Limited Liability Co.', 'Limited Liability Company', 'PLLC');
  81.                                 for ($i = 0; $i <= count($validIndicators) - 1; $i++) {
  82.                                     if (stripos($row_order_status->biz_name, $validIndicators[$i]) !== false) {
  83.                                         $indicatorIncluded = true;
  84.                                     }
  85.                                 }
  86.                                 if (!$indicatorIncluded) {
  87.                                     // Customer did not supply a valid indicator. Default to ', LLC'
  88.                                     $row_order_status->biz_name = $row_order_status->biz_name . ', LLC';
  89.                                 }
  90.                             } else if ($row_order_status->biz_type == 2 || $row_order_status->biz_type == 3) {
  91.                                 $validIndicators = array('Inc', 'Inc.', ', Inc', ',Inc', 'Corp', 'Incorporated', 'Corporation', 'P.C.', 'Professional Corporation');
  92.                                 for ($i = 0; $i <= count($validIndicators) - 1; $i++) {
  93.                                     if (stripos($row_order_status->biz_name, $validIndicators[$i]) !== false) {
  94.                                         $indicatorIncluded = true;
  95.                                     }
  96.                                 }
  97.                                 if (!$indicatorIncluded) {
  98.                                     // Customer did not supply a valid indicator. Default to ', LLC'
  99.                                     $row_order_status->biz_name = $row_order_status->biz_name . ', Inc';
  100.                                 }
  101.                             } else {
  102.                                 // Nonprofit
  103.                                 $validIndicators = array('Inc', 'Inc.', ', Inc', ',Inc', 'Corp', 'Incorporated', 'Corporation', 'P.C.', 'Professional Corporation');
  104.                                 for ($i = 0; $i <= count($validIndicators) - 1; $i++) {
  105.                                     if (stripos($row_order_status->biz_name, $validIndicators[$i]) !== false) {
  106.                                         $indicatorIncluded = true;
  107.                                     }
  108.                                 }
  109.                                 if (!$indicatorIncluded) {
  110.                                     // Customer did not supply a valid indicator. Default to ', LLC'
  111.                                     $row_order_status->biz_name = $row_order_status->biz_name . ', Inc';
  112.                                 }
  113.                             }
  114.                         }
  115.                         $excel_rows .=
  116.                             ucfirst(strtolower($customerInfo->first_name)) . "\t" .
  117.                             ucfirst(strtolower($customerInfo->last_name)) . "\t" .
  118.                             preg_replace('/[\t|\n\,]+/', '', $row_order_status->biz_name) . "\t" .
  119.                             $customerInfo->email . "\t" .
  120.                             $customerInfo->phone_number . "\t" .
  121.                             ucwords(strtolower(preg_replace('/[\t|\n\,]+/', '', $row_order_status->biz_address))) . "\t" .
  122.                             ucwords(strtolower($row_order_status->biz_city)) . "\t" .
  123.                             (!empty($row_order_status->abbr) ? trim(preg_replace('/[\t,\,]+/', '', $row_order_status->abbr))  : "") . "\t" .
  124.                             $row_order_status->biz_zip . "\t" .
  125.                             $row_order_status->biz_category . "\t" .
  126.                             $row_order_status->bt_name . "\t" .
  127.                             date('m/d/Y', strtotime((empty($row_order_status->approval_date) ? $row_order_status->order_completion_date : $row_order_status->approval_date))) . "\t" . "\n";
  128.                         // // Update the order status to show the tax consultation has been processed
  129.                         // $updateOrder = OrderStatuses::where(['order_id' => $order_id])->first();
  130.                         // $updateOrder->bank_of_america_processed = date("Y-m-d H:i:s");
  131.                         // $updateOrder->save();
  132.                        
  133.                         // Update the order status to show the tax consultation has been processed
  134.                         $updateOrder = OrderStatuses::where(['order_id' => $order_id])->first();
  135.                         $updateOrder->business_insurance_processed  = date('Y-m-d H:i:s', strtotime(gmdate("Y-m-d\TH:i:s\Z")));
  136.                         $updateOrder->save();
  137.                     } // end of for loop
  138.                     // Get last week's date range
  139.                     $previous_day = strtotime("-1 day");
  140.                     $day = date("Y-m-d", $previous_day);
  141.                     $file_name = "Payment Cloud" . $day . ".xls";
  142.                     \Storage::disk('local')->put('Payment_Cloud/' . $file_name, $excel_rows);
  143.                     $my_file = \Storage::disk('local')->path("Payment_Cloud" . "/" . $file_name);
  144.                     // Email CSV to 'bpack@1800accountant.com'.
  145.                     $emailToUsers = [
  146.                         ["name"=>"David Costa","email"=>"david.costa@swyftfilings.com"],
  147.                         //["name"=>"Mario Ramos","email"=>"mario.ramos@coverwallet.com"],
  148.                         //["name"=>"Jeke Chen","email"=>"jeke.chen@coverwallet.com"],
  149.                         ["name"=>"Wsy Wsy","email"=>"wsy@paymentcloudinc.com"]
  150.                     ];
  151.                     $emailTemplateKeywords['{data}'] = "Tax Consultations" . $day;
  152.                     // $emailTemplateKeywords['{name}'] = 'David Costa';
  153.                     // $emailTemplateKeywords['{email}'] = 'david.costa@swyftfilings.com';
  154.                     foreach($emailToUsers as $userData){
  155.                         $emailTemplateKeywords['{name}'] = $userData['name'];
  156.                         $emailTemplateKeywords['{email}'] = $userData['email'];
  157.                         $data = sendEmail('Overwallet_Consultations', $emailTemplateKeywords, $userData['name'], checkEmailSendToThis($userData['email']));
  158.                         Mail::send('mails.common_mail', $data, function ($message) use ($data, $day, $my_file) {
  159.                             $message->to($data['receiver'], $data['receiver_name'])->subject($data['subject']);
  160.                             $message->from($data['sender'], $data['sender_name']);
  161.                             $message->attach($my_file, [
  162.                                 // 'as' => 'Man', // If you want you can chnage original name to custom name
  163.                                 'mime' => 'application/xls'
  164.                             ]);
  165.                         });
  166.                     }
  167.                    
  168.                     //remove file after sned email
  169.                     unlink($my_file);
  170.                 }
  171.             }
  172.             die("Success");
  173.            
  174.         } catch (\Exception $e) {
  175.             $logmessage = 'Exception at Line : ' . $e->getLine().' | '.$e->getMessage().'.';
  176.             Log::debug($logmessage);
  177.         } catch (\Error $e) {
  178.             $logmessage = 'Error at Line : ' . $e->getLine().' | '.$e->getMessage().'.';
  179.             Log::error($logmessage);
  180.         }
  181.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement