diff --git a/app/Controllers/ApiIntegration.php b/app/Controllers/ApiIntegration.php index 4a225616..a655a5b1 100644 --- a/app/Controllers/ApiIntegration.php +++ b/app/Controllers/ApiIntegration.php @@ -200,22 +200,22 @@ class ApiIntegration extends ResourceController $page_count = NULL; $genre = NULL; $author = NULL; - $category = 1; // Book + $category = NULL; // Book $attributes = $records['attributes']; $categories = $records['categories']; - if (count($categories) > 0) { - $categoryNames = []; - foreach ($categories as $category) { - if (isset($category->name) && $category->name != "") { - if($category->name !== "Membership"){ - $categoryNames[] = $category->name; } - else{ $category = 2; } - } - } - if (!empty($categoryNames)) { - $genre = implode(", ", $categoryNames); - } - } + // if (count($categories) > 0) { + // $categoryNames = []; + // foreach ($categories as $category) { + // if (isset($category->name) && $category->name != "") { + // if($category->name !== "Membership"){ + // $category = 1;$categoryNames[] = $category->name; } + // else{ $category = 2; } + // } + // } + // if (!empty($categoryNames)) { + // $genre = implode(", ", $categoryNames); + // } + // } if (count($attributes) > 0) { foreach ($attributes as $att) { if (isset($att->name) && $att->name == "Publisher") { @@ -340,8 +340,8 @@ class ApiIntegration extends ResourceController $book_image_data[$i]['isactive'] = 1; $book_image_data[$i]['wp_api_img_id'] = isset($img->id) && !empty($img->id) ? $img->id : NULL; $book_image_data[$i]['book_img_id'] = $bookImgId; - $this->logger->info("Api SaveBookDetails : Image Data = $i "); $i++; + $this->logger->info("Api SaveBookDetails : Image Data = $i Request data = ".json_encode($book_image_data)); } // image loop closed $extensive_correspondence .= (!empty($book_image_data) ? $api_model->insertAndUpdateChildDetails($book_image_data, 'book_images', 'book_img_id') : ""); $this->logger->info("Api SaveBookDetails : ".$extensive_correspondence); @@ -349,6 +349,97 @@ class ApiIntegration extends ResourceController else{ $this->logger->error("Api SaveBookDetails : Err = Image Data Not Found"); } + if (count($categories) > 0 && $last_insert_book_id != "") { + //$this->save_categories_master_details($categories); + $extensive_correspondence .= " Its have " . count($categories) . " Categories"; + $basic_message .= " And Its have " . count($categories) . " Categories"; + $this->logger->info("Api SaveBookDetails : ".$extensive_correspondence); + $master_categories = $api_model->getData('category', ['isactive' => 1]); + $this->logger->info("Api SaveBookDetails : total master categories count = ".count($master_categories)); + $where = ['book_id' => (int)$last_insert_book_id, 'isactive' => 1 ]; + $existing_categories = $api_model->getData('book_categories', $where); + $this->logger->info("Api SaveBookDetails : existing categories count = ".count($existing_categories)); + $request_categories = (array)$categories; + $filtering_existing_categories_wpApiId = []; + $filtering_requested_categories_wpApiId = []; + $missing_category_values = []; + $common_category_element = []; + + if (!empty($existing_categories)) { + + for ($z = 0; $z < count($request_categories); $z++) { + $filtering_requested_categories_wpApiId[$z] = $request_categories[$z]->id; + } + for ($y = 0; $y < count($master_categories); $y++) { + $filtering_existing_categories_wpApiId[$y] = $master_categories[$y]->wp_api_category_id; + } + $filtering_existing_categories_wpApiId = array_diff($filtering_existing_categories_wpApiId, array("")); + if (!empty($filtering_requested_categories_wpApiId)) { + $this->logger->info("Api SaveBookDetails : Incoming Categories Wp Id " .implode(",",$filtering_requested_categories_wpApiId)); + $this->logger->info("Api SaveBookDetails : Existing Categories Wp Id " .implode(",",$filtering_existing_categories_wpApiId)); + + $A = $filtering_existing_categories_wpApiId; + $B = $filtering_requested_categories_wpApiId; + $missing_category_values = array_diff($A, $B); //Find difference element in Existing Images only + $common_category_element = array_intersect($A, $B); + $this->logger->info("Api SaveBookDetails : Missing Categories Wp Id " .implode(",",$missing_category_values)." Are inactived"); + if (!empty($missing_category_values)) { + $this->logger->info("Api SaveBookDetails : Missing Categories Wp Id " .implode(",",$missing_category_values)." Are inactived"); + $where1 = ['isactive' => 1]; + $whereIn1[0] = 'wp_api_category_id'; + $whereIn1[1] = array_filter($missing_category_values); + $get_master_category_id_to_inactive = $api_model->getData('category', $where1, $whereIn1); + for ($x = 0; $x < count($get_master_category_id_to_inactive); $x++) { + $book_categories_to_inactive[$x] = $get_master_category_id_to_inactive[$x]->id; + } + $this->logger->info("Api SaveBookDetails : Missing Categories Wp Id " .implode(",",$missing_category_values)." catgory pkid => ".implode(",",$book_categories_to_inactive). "Are inactived"); + $where2 = ['isactive' => 1]; + $whereIn2[0] = 'category_id'; + $whereIn2[1] = $book_categories_to_inactive; + $api_model->inactiveMissingDetails('book_categories', $where2, $whereIn2); + //echo $api_model->getlastQuery(); + } + } // loop closed + } + + $where = ['isactive' => 1]; + if (!empty($common_category_element)) { + $whereIn[0] = 'wp_api_category_id'; + $whereIn[1] = $common_category_element; + } else { + $whereIn = null; + } + $get_master_category_based_WPid = $api_model->getData('category', $where, $whereIn); + $this->logger->info("Api SaveBookDetails : Common Categories Wp Id " .implode(",",$common_category_element)." Are already inserted data"); + $this->logger->info("Api SaveBookDetails : available category data count = " .count($request_categories)); + $book_categories_data = []; + $j = 0; + foreach ($categories as $cate) { + $categories_id = null; + $book_category_primarykey = null; + // Iterate through the array and find the 'book_img_id' based on 'wp_api_img_id' + if (!empty($get_master_category_based_WPid)) { + foreach ($get_master_category_based_WPid as $master) { + if ($master->wp_api_category_id == $cate->id) { + $categories_id = $master->id; + $exist_categories = $api_model->getData('book_categories', ['isactive'=>1,'category_id'=>$categories_id,'book_id' => (int)$last_insert_book_id]); + $book_category_primarykey = isset($exist_categories) && !empty($exist_categories) ? $exist_categories[0]->id : NULL; + $this->logger->info("Api SaveBookDetails : Master Category ID = " .$categories_id." => WP CategoryID = ".$master->wp_api_category_id); + break; // Found the match, no need to continue looping + } + } + } + $book_categories_data[$j]['id'] = $book_category_primarykey; + $book_categories_data[$j]['category_id'] = (int)$categories_id; + $book_categories_data[$j]['book_id'] = $last_insert_book_id; + $j++; + } // loop closed + $extensive_correspondence .= (!empty($book_categories_data) ? $api_model->insertAndUpdateChildDetails($book_categories_data, 'book_categories', 'id') : ""); + $this->logger->info("Api SaveBookDetails : ".$extensive_correspondence); + } //category count closed + else{ + $this->logger->error("Api SaveBookDetails : Err = Category Data Not Found"); + } $response = ['status' => 200, 'message' => $basic_message ,'indetails' => $extensive_correspondence]; } //if cond. closed else { @@ -465,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) { @@ -609,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; @@ -622,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.".
"; + } } } @@ -757,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; @@ -787,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') : ""); @@ -891,7 +1027,78 @@ class ApiIntegration extends ResourceController $response = ['status' => 204, 'message' => 'Data No Found']; } return $response; - } + } - + public function save_categories_master_details($records) + { + $api_model = new ApiIntegrationModel(); + if (count($records) > 0) { + $this->logger->info("Api SaveCategoriesMasterDetails : Categories Count = ".count($records)); + $existing_categories = $api_model->getData('category', ['isactive' => 1]); + $request_categories = (array)$records; + + $filteringExistingCategoriesWpApiId = []; + $filteringRequestedCategoriesWpApiId = []; + $commonCategoriesElement = []; + if (!empty($existing_categories)) { + for ($z = 0; $z < count($request_categories); $z++) { + $filteringRequestedCategoriesWpApiId[$z] = $request_categories[$z]->id; + } + for ($y = 0; $y < count($existing_categories); $y++) { + $filteringExistingCategoriesWpApiId[$y] = $existing_categories[$y]->wp_api_category_id; + } + + if (!empty($filteringExistingCategoriesWpApiId)) { + $this->logger->info("Api SaveCategoriesMasterDetails : Incoming Categories Wp Id " .implode(",",$filteringRequestedCategoriesWpApiId)); + $this->logger->info("Api SaveCategoriesMasterDetails : Existing Categories Wp Id " .implode(",",$filteringExistingCategoriesWpApiId)); + $A = $filteringExistingCategoriesWpApiId; + $B = $filteringRequestedCategoriesWpApiId; + $missingCategoriesValues = array_diff($A, $B); //Find difference element in Existing Images only + $commonCategoriesElement = array_intersect($A, $B); + if (!empty($missingCategoriesValues)) { + $this->logger->info("Api SaveCategoriesMasterDetails : Missing Categories Wp Id " .implode(",",$missingCategoriesValues)." Are inactived"); + $inactive_where = ['isactive' => 1]; + $inactive_whereIn[0] = 'wp_api_category_id'; + $inactive_whereIn[1] = $missingCategoriesValues; + $api_model->inactiveMissingDetails('category', $inactive_where, $inactive_whereIn); + } + } + } + + $where = ['isactive' => 1]; + if (!empty($commonCategoriesElement)) { + $whereIn[0] = 'wp_api_category_id'; + $whereIn[1] = $commonCategoriesElement; + } else { + $whereIn = null; + } + $lastestActiveCategoriesData = $api_model->getData('category', $where, $whereIn); + $this->logger->info("Api SaveCategoriesMasterDetails : lastest Active Book Categories Data Count " .count($lastestActiveCategoriesData)); + $book_category_data = []; + $a = 0; + foreach ($records as $r) { + $categoryId = null; + // Iterate through the array and find the 'book_img_id' based on 'wp_api_img_id' + if (!empty($lastestActiveBookImageData)) { + foreach ($lastestActiveBookImageData as $item) { + if ($item->wp_api_category_id == $r->id) { + $categoryId = $item->id; + break; // Found the match, no need to continue looping + } + } + } + $book_category_data[$a]['id'] = $categoryId; + $book_category_data[$a]['name'] = $r->name; + $book_category_data[$a]['wp_api_category_id'] = $r->id; + $book_category_data[$a]['parent_wp_api_category_id'] =NULL; + $a++; + $this->logger->info("Api SaveCategoriesMasterDetails : Categories Data = $a Request data to save master = ".json_encode($book_category_data)); + } // loop closed + $extensive_correspondence = (!empty($book_image_data) ? $api_model->insertAndUpdateChildDetails($book_category_data, 'category', 'id') : ""); + $this->logger->info("Api SaveCategoriesMasterDetails : ".$extensive_correspondence); + } // count closed + else{ + $this->logger->error("Api SaveCategoriesMasterDetails : Err = Categories Data Not Found"); + } + } } \ No newline at end of file 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(); + } + } diff --git a/app/Views/template/footer.php b/app/Views/template/footer.php index b730f3e6..f605a216 100644 --- a/app/Views/template/footer.php +++ b/app/Views/template/footer.php @@ -36,9 +36,9 @@

CONTACT US