diff --git a/app/Controllers/ApiIntegration.php b/app/Controllers/ApiIntegration.php index 99f1dd33..a655a5b1 100644 --- a/app/Controllers/ApiIntegration.php +++ b/app/Controllers/ApiIntegration.php @@ -556,7 +556,7 @@ class ApiIntegration extends ResourceController $this->logger->info("Api SaveCustomerDetails : lastest Active billing Data Count " .count($lastestActiveBillingAdressData)); foreach ($billing as $bill) { $billingAddressId = null; - if ($bill->first_name != '') { + if ($bill->first_name !== '') { if (!empty($lastestActiveBillingAdressData)) { foreach ($lastestActiveBillingAdressData as $item) { if ($item->address_1 == $bill->address_1) { @@ -700,7 +700,7 @@ class ApiIntegration extends ResourceController } $countAll = $api_model->countAll('invoice', $where); $this->logger->info(($countAll) ? "Api SaveSalesDetails : in wordpress product ref ID = ".$wordpress_order_id." is available on Invoice table and its PrimaryKey = ".$invoice_id : "Api SaveSalesDetails : in wordpress order ref ID = ".$wordpress_order_id." Not available on Invoice table"); - $billing_addr = "";$shipping_addr = ""; + $billing_addr = NULL;$shipping_addr = NULL; $lineitems_array = []; $billing_array = []; $shipping_array = []; $lineitems_subtotal = 0; $lineitems_tax = 0; @@ -713,12 +713,16 @@ class ApiIntegration extends ResourceController if (count($billing_array) > 0) { foreach ($billing_array as $bill) { - $billing_addr = $bill->address_1 .",". $bill->address_2 . "," . $bill->city . "," . $bill->state . "," . $bill->country . "." . $bill->postcode; + if($bill->first_name != ""){ + $billing_addr = "".$bill->address_1 .",". $bill->address_2 ."
".$bill->city." ".$bill->postcode."
".$bill->state." ".$bill->country.".
"; + } } } if (count($shipping_array) > 0) { foreach ($shipping_array as $ship) { - $shipping_addr = $ship->address_1 .",". $ship->address_2 . "," . $ship->city . "," . $ship->state . "," . $ship->country . "." . $ship->postcode; + if($ship->first_name != ""){ + $shipping_addr = "".$ship->address_1 .",". $ship->address_2 ."
".$ship->city." ".$ship->postcode."
".$ship->state." ".$ship->country.".
"; + } } } @@ -848,28 +852,68 @@ class ApiIntegration extends ResourceController $where = ['wp_api_product_id' => (int)$ii->product_id]; $get_book_data_based_on_wp_product_id = $api_model->getData('books', $where); - - $skuId = $ii->sku; - $this->logger->info("Api SaveSalesDetails : Lineitem SKU ID = " .$skuId); - - $where = ['online_sku' => $skuId, 'isactive' => 1]; - $existing_schemes = $api_model->getData('schemes', $where); - - if(!empty($skuId) && !empty($existing_schemes)){ - $duration = $existing_schemes[0]->duration; + $skuId = (string)$ii->sku; + + // $get_book_category = $api_model->getData('book_categories', ['book_id' => (int)$ii->product_id,'isactive'=>1]); + $get_book_category = $api_model->getBookCategories($ii->product_id); + // $result_book_category = array_filter($get_book_category, function ($element) { + // return $element['category_name'] === 'Membership' && $element['sku'] === $skuId; + // }); + $result_book_category = array_filter($get_book_category, function ($element) use ($skuId) { + return $element['category_name'] === 'Membership' && $element['sku'] === $skuId; + }); + $fromDate = NULL; + $toDate = NULL; + for ($x = 0; $x < count($result_book_category); $x++) { + $duration = $result_book_category[$x]['duration'] * 12; $fromDateTimestamp = strtotime($records['date_created']); $toDateTimestamp = strtotime("+$duration months", $fromDateTimestamp); $fromDate = date("Y-m-d", strtotime($records['date_created'])); $toDate = date("Y-m-d", $toDateTimestamp); - $subscription_data[$h]['scheme_id'] = $existing_schemes[0]->scheme_id; - $subscription_data[$h]['customer_id'] = $records['customer_id']; - $subscription_data[$h]['invoice_id'] = $invoice_id; - $subscription_data[$h]['from_subscription'] = $fromDate; - $subscription_data[$h]['to_subscription'] = $toDate; - $subscription_data[$h]['mode'] = "From WordPress Data"; - $h++; - $this->logger->info("Api SaveSalesDetails : Subscription Data inx = $h Request data = ".json_encode($subscription_data)); - }else{ + $scheme_id = $result_book_category[$x]['category_id']; + $dataToCheck = ['scheme_id'=>$scheme_id,'customer_id' => $records['customer_id'],'invoice_id' => $invoice_id ]; + $existing_subscription = $api_model->getData('subscription', $dataToCheck); + if (count($existing_subscription) > 0) { + $this->logger->info("Api SaveSalesDetails : Subscription ".count($existing_subscription)." data already exists = ".json_encode($dataToCheck)); + } else { + $subscription_data[$x]['scheme_id'] = $result_book_category[$x]['category_id']; + $subscription_data[$x]['customer_id'] = $records['customer_id']; + $subscription_data[$x]['invoice_id'] = $invoice_id; + $subscription_data[$x]['from_subscription'] = $fromDate; + $subscription_data[$x]['to_subscription'] = $toDate; + $subscription_data[$x]['mode'] = $result_book_category[$x]['category_name']; + $subscription_data[$x]['business_id']=1; + $this->logger->info("Api SaveSalesDetails : Subscription Data inx = $x Request data = ".json_encode($subscription_data)); + } + } + // echo $fromDate; + // echo $toDate; + // print_r($subscription_data);die; + // print_r($get_book_category); + // print_r($result_book_category); + // print_r($subscription_data); + // die; + // $skuId = $ii->sku; + // $this->logger->info("Api SaveSalesDetails : Lineitem SKU ID = " .$skuId); + + // $where = ['online_sku' => $skuId, 'isactive' => 1]; + // $existing_schemes = $api_model->getData('schemes', $where); + + // if(!empty($skuId) && !empty($existing_schemes)){ + // $duration = $existing_schemes[0]->duration; + // $fromDateTimestamp = strtotime($records['date_created']); + // $toDateTimestamp = strtotime("+$duration months", $fromDateTimestamp); + // $fromDate = date("Y-m-d", strtotime($records['date_created'])); + // $toDate = date("Y-m-d", $toDateTimestamp); + // $subscription_data[$h]['scheme_id'] = $existing_schemes[0]->scheme_id; + // $subscription_data[$h]['customer_id'] = $records['customer_id']; + // $subscription_data[$h]['invoice_id'] = $invoice_id; + // $subscription_data[$h]['from_subscription'] = $fromDate; + // $subscription_data[$h]['to_subscription'] = $toDate; + // $subscription_data[$h]['mode'] = "From WordPress Data"; + // $h++; + // $this->logger->info("Api SaveSalesDetails : Subscription Data inx = $h Request data = ".json_encode($subscription_data)); + // }else{ sku $line_item_data[$i]['product'] = (!empty($get_book_data_based_on_wp_product_id) ? $get_book_data_based_on_wp_product_id[0]->book_id : NULL); $line_item_data[$i]['quantity'] =(float)$ii->quantity; $line_item_data[$i]['unit_price'] =(float)$ii->price; @@ -878,10 +922,11 @@ class ApiIntegration extends ResourceController $line_item_data[$i]['invoice_child_id'] = $invoiceItemsId; $line_item_data[$i]['wp_api_line_items_id'] = $ii->id; $line_item_data[$i]['invoice_id'] = $last_insert_invoice_id; + $line_item_data[$i]['from_subscription'] = $fromDate; + $line_item_data[$i]['to_subscription'] = $toDate; $i++; $this->logger->info("Api SaveSalesDetails : Invoiceitem Data inx = $i Request data = ".json_encode($line_item_data)); - } - + // } } // line item loop closed $extensive_correspondence .= (!empty($line_item_data) ? $api_model->insertAndUpdateChildDetails($line_item_data, 'invoiceitems', 'invoice_child_id') : ""); $extensive_correspondence .= (!empty($subscription_data) ? $api_model->insertAndUpdateChildDetails($subscription_data, 'subscription', 'sub_id') : ""); diff --git a/app/Models/ApiIntegrationModel.php b/app/Models/ApiIntegrationModel.php index 8a221eda..b7e3f066 100644 --- a/app/Models/ApiIntegrationModel.php +++ b/app/Models/ApiIntegrationModel.php @@ -88,4 +88,14 @@ class ApiIntegrationModel extends Model $this->db->table($table_name)->where($where)->whereIn($whereIn[0],$whereIn[1])->update($dataToUpdate); } + public function getBookCategories($product_id){ + return $this->db->table('book_categories as BC') + ->join('category as C', 'C.id = BC.category_id', 'left') + ->join('books as B', 'B.book_id = BC.book_id', 'left') + ->select('BC.id,BC.book_id,BC.category_id,C.wp_api_category_id,C.name as category_name,B.title as book_name,B.sku,B.duration') + ->where(['BC.isactive'=>1,'B.wp_api_product_id'=>$product_id]) + ->get() + ->getResultArray(); + } + }