diff --git a/app/Config/Routes.php b/app/Config/Routes.php
index 09f21d13..c959a477 100644
--- a/app/Config/Routes.php
+++ b/app/Config/Routes.php
@@ -107,8 +107,10 @@ $routes->get("delete_invoice/(:any)", "Invoice::delete_invoice/$1");
# Api integration Routes
$routes->group("api", function ($routes) {
- $routes->post("create_products/", "ApiIntegration::save_book_details");
- $routes->post("create_customers/", "ApiIntegration::save_customer_details");
+ // $routes->post("create_products/", "ApiIntegration::save_book_details");
+ $routes->match(['put', 'post', 'get', 'delete'], 'products', 'ApiIntegration::book_api_integration');
+ $routes->match(['put', 'post', 'get', 'delete'], 'customers', 'ApiIntegration::customer_api_integration');
+ $routes->match(['put', 'post', 'get', 'delete'], 'orders', 'ApiIntegration::sales_api_integration');
});
/*
diff --git a/app/Controllers/ApiIntegration.php b/app/Controllers/ApiIntegration.php
index 8afc8314..15df8767 100644
--- a/app/Controllers/ApiIntegration.php
+++ b/app/Controllers/ApiIntegration.php
@@ -11,170 +11,825 @@ class ApiIntegration extends ResourceController
{
use ResponseTrait;
- public function save_book_details()
+ public function book_api_integration()
+ {
+ $request = \Config\Services::request();
+ $message = "";
+ try {
+ switch ($this->request) {
+ case $this->request->is('put'):
+ try {
+ // $url = http://localhost/vb_book/api/products?id=795;
+ $records = (array)$this->request->getVar();
+ $id = $this->request->getVar('id');
+ if ($records['id'] == $id) {
+ $response = $this->save_book_details($records, $id);
+ } else {
+ $response = ['status' => 404, 'message' => "ID Mismatched"];
+ }
+ } catch (\Exception $e) {
+ return $this->fail('PUT request failed, An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ }
+ break;
+ case $this->request->is('post'):
+ try {
+ // $url = http://localhost/vb_book/api/products;
+ $records = (array)$this->request->getVar();
+ $wordpress_book_id = $records['id'];
+ $response = $this->save_book_details($records, $wordpress_book_id);
+ } catch (\Exception $e) {
+ return $this->fail('POST request failed, An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ }
+ break;
+ case $request->is('get'):
+ try {
+ // $url = http://localhost/vb_book/api/products?id=795;
+ // $url = http://localhost/vb_book/api/products;
+ $id = $this->request->getVar('id');
+ if ($id) {
+ $where = ['wp_api_product_id' => (int)$id, 'isactive' => 1];
+ } else {
+ $where = ['isactive' => 1];
+ }
+ $api_model = new ApiIntegrationModel();
+ $book_details = $api_model->setTable('books')->where($where);
+ $response = ['status' => 200, 'message' => "Book Details", "data" => $book_details];
+ } catch (\Exception $e) {
+ return $this->fail('GET request failed, An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ }
+ break;
+ case $request->is('delete'):
+ try {
+ // $url = http://localhost/vb_book/api/products?id=795;
+ $id = $this->request->getVar('id');
+ $api_model = new ApiIntegrationModel();
+ $where = ['isactive' => 1, 'wp_api_product_id' => (int)$id];
+ $existing_book_data = $api_model->getData('books', $where);
+ if ((int)count($existing_book_data) > 0) {
+ $book_id = (int)$existing_book_data[0]->book_id;
+ $inactive_data['isactive'] = 0;
+ $update_where = ['isactive' => 1, 'book_id' => $book_id];
+ $api_model->updateData('books', $inactive_data, $update_where);
+ $getExistingBookImageDetails = $api_model->getData('book_images', $update_where);
+ if ($getExistingBookImageDetails) {
+ // $update_where = ['book_id' => $book_id];
+ $api_model->updateData('book_images', $inactive_data, $update_where);
+ }
+ }
+ $response = ['status' => 200, 'message' => "Book Details and Book Images Details Are Inactived"];
+ } catch (\Exception $e) {
+ // Handle DELETE-specific exception
+ return $this->fail('DELETE request failed, An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ }
+ break;
+ default:
+ throw new \Exception('Unsupported HTTP method');
+ // return $this->fail('Unsupported HTTP method');
+ }
+ return $this->respond($response);
+ } catch (\Exception $e) {
+ // Handle the exception, you can log it or return an error response
+ return $this->fail('An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ // return $this->fail($e->getMessage());
+ }
+ }
+
+ public function save_book_details($records, $wordpress_book_id)
{
- $request = \Config\Services::request();
$api_model = new ApiIntegrationModel();
$message = "";
+ $book_id = "";
+ if (count($records) > 0 && !empty($wordpress_book_id)) {
+ $where = ['wp_api_product_id' => $wordpress_book_id, 'isactive' => 1];
+ $book_details = $api_model->setTable('books')->where($where)->first();
+ if (isset($book_details['book_id']) && $book_details['book_id'] != "") {
+ $book_id = $book_details['book_id'];
+ $where['book_id'] = $book_details['book_id'];
+ } // update means bookid where condition added : else no where condition added.
- if ($request->is('post')) {
- $records = (array)$request->getVar();
- if (count($records) > 0 && $records['status'] == "publish" && !empty($records['id'])) {
- $where = ['wp_api_id' => $records['id']];
- $countAll = $api_model->countAll('books', $where);
- if ((int)$countAll == 0) {
- $publisher = '';
- $language = '';
- $attributes = $records['attributes'];
- if (count($attributes) > 0) {
- foreach ($attributes as $att) {
- if ($att->name == "Publisher") {
- $publisher = implode(", ", $att->options);
- }
- if ($att->name == "Book Author") {
- $publisher .= implode(", ", $att->options) . "(Book Author)";
- }
- if ($att->name == "Book Language") {
- $language = implode(", ", $att->options);
+
+ $countAll = $api_model->countAll('books', $where);
+ $publisher = '';
+ $language = '';
+ $attributes = $records['attributes'];
+ if (count($attributes) > 0) {
+ foreach ($attributes as $att) {
+ if (isset($att->name) && $att->name == "Publisher") {
+ $publisher = implode(", ", $att->options);
+ }
+ if (isset($att->name) && $att->name == "Book Author") {
+ $publisher .= implode(", ", $att->options) . "(Book Author)";
+ }
+ if (isset($att->name) && $att->name == "Book Language") {
+ $language = implode(", ", $att->options);
+ }
+ }
+ }
+ $book_data['wp_api_product_id'] = $wordpress_book_id;
+ $book_data['title'] = isset($records['name']) ? $records['name'] : "";
+ $book_data['publication_date'] = isset($records['date_created']) ? $records['date_created'] : "";
+ $book_data['publisher'] = $publisher;
+ $book_data['genre'] = "";
+ $book_data['language'] = $language;
+ $book_data['description'] = isset($records['description']) ? $records['description'] : "";
+ $book_data['page_count'] = '';
+ $book_data['price'] = (isset($records['sale_price']) && $records['sale_price'] != '') ? $records['sale_price'] : ((isset($records['regular_price']) && $records['regular_price'] != '') ? $records['regular_price'] : NULL);
+ $book_data['isactive'] = 1;
+ $book_data['business_id'] = 1;
+ $images = $records['images'];
+
+ $i = 0;
+ if ((int)$countAll == 0) {
+ $insert_result = $api_model->insertData('books', $book_data);
+ $last_insert_book_id = $insert_result['info'];
+ $message .= $insert_result['info'] ? "Book id : " . $insert_result['info']
+ : "Err :" . $insert_result['err'];
+ } else {
+ $last_insert_book_id = $book_id;
+ $book_where = ['book_id' => $book_id, 'isactive' => 1, 'wp_api_product_id' => $wordpress_book_id];
+ $update_result = $api_model->updateData('books', $book_data, $book_where);
+ $message .= $update_result['info'] ? "Book id : " . $last_insert_book_id . " ( " . $update_result['info'] . ")"
+ : " ( Err :" . $update_result['err'] . ")";
+ }
+
+ $book_image_data = [];
+
+ if (count($images) > 0 && $last_insert_book_id != "") {
+ $message .= " Its have " . count($images) . " Images ,";
+ $where = ['book_id' => (int)$last_insert_book_id, 'isactive' => 1];
+ $existing_images = $api_model->getData('book_images', $where);
+ $request_images = (array)$images;
+
+ $filteringExistingImagesWpApiId = [];
+ $filteringRequestedImagesWpApiId = [];
+ $commonElements = [];
+ if (!empty($existing_images)) {
+
+ for ($z = 0; $z < count($request_images); $z++) {
+ $filteringRequestedImagesWpApiId[$z] = $request_images[$z]->id;
+ }
+ for ($y = 0; $y < count($existing_images); $y++) {
+ $filteringExistingImagesWpApiId[$y] = $existing_images[$y]->wp_api_img_id;
+ }
+
+ if (!empty($filteringExistingImagesWpApiId)) {
+ $A = $filteringExistingImagesWpApiId;
+ $B = $filteringRequestedImagesWpApiId;
+ $missingValues = array_diff($A, $B); //Find difference element in Existing Images only
+ $commonElements = array_intersect($A, $B);
+ if (!empty($missingValues)) {
+ $inactive_where = ['isactive' => 1, 'book_id' => (int)$last_insert_book_id];
+ $inactive_whereIn[0] = 'wp_api_img_id';
+ $inactive_whereIn[1] = $missingValues;
+ $api_model->inactiveMissingDetails('book_images', $inactive_where, $inactive_whereIn);
+ }
+ }
+ }
+
+ $where = ['book_id' => (int)$last_insert_book_id, 'isactive' => 1];
+ if (!empty($commonElements)) {
+ $whereIn[0] = 'wp_api_img_id';
+ $whereIn[1] = $commonElements;
+ } else {
+ $whereIn = null;
+ }
+ $lastestActiveBookImageData = $api_model->getData('book_images', $where, $whereIn);
+
+ foreach ($images as $img) {
+ $bookImgId = 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_img_id == $img->id) {
+ $bookImgId = $item->book_img_id;
+ break; // Found the match, no need to continue looping
}
}
}
- $insertion_data['wp_api_id'] = $records['id'];
- $insertion_data['title'] = $records['name'];
- $insertion_data['publication_date'] = $records['date_created'];
- $insertion_data['publisher'] = $publisher;
- $insertion_data['genre'] = $records['description'];
- $insertion_data['language'] = $language;
- $insertion_data['description'] = $records['short_description'];
- $insertion_data['page_count'] = '';
- $insertion_data['price'] = $records['sale_price'] != '' ? $records['sale_price'] : $records['regular_price'];
- $insertion_data['created_by'] = 77;
- $insertion_data['isactive'] = 1;
- $insertion_data['business_id'] = 1;
- $images = $records['images'];
- $i = 0;
- $lastInsertId = $api_model->insertData('books', $insertion_data);
- $insertion_img_data = [];
- $message .= "book ID : " . $lastInsertId . " have Imgs" . count($images) . "
";
- // echo "book ID : ".$lastInsertId." have Imgs".count($images)."
";
- if (count($images) > 0) {
- foreach ($images as $img) {
- $insertion_img_data[$i]['img_name'] = $img->src;
- $insertion_img_data[$i]['is_cover'] = 0;
- $insertion_img_data[$i]['type'] = 2;
- $insertion_img_data[$i]['book_id'] = $lastInsertId;
- $insertion_img_data[$i]['created_by'] = 777;
- $insertion_img_data[$i]['isactive'] = 1;
- $i++;
- } // image loop closed
- $api_model->insertImagesBatch($insertion_img_data);
- $message .= "book ID : " . $lastInsertId . " Img. Batch Inserted Done
";
- // echo " Img Batch Inserted Done. ".$i. "
";
- } //image count closed
- $response = ['status' => 200, 'message' => $message, 'data' => array()];
- } else {
- $response = ['status' => 204, 'message' => 'Data Already Exist', 'data' => array()];
- }
- } //if cond. closed
- } else {
- $response = ['status' => 204, 'message' => 'Invaild Method', 'data' => array()];
+ $book_image_data[$i]['img_name'] = $img->src;
+ $book_image_data[$i]['is_cover'] = 0;
+ $book_image_data[$i]['type'] = 2;
+ $book_image_data[$i]['book_id'] = $last_insert_book_id;
+ $book_image_data[$i]['isactive'] = 1;
+ $book_image_data[$i]['wp_api_img_id'] = $img->id;
+ $book_image_data[$i]['book_img_id'] = $bookImgId;
+ $i++;
+ } // image loop closed
+ $message .= (!empty($book_image_data) ? $api_model->insertAndUpdateChildDetails($book_image_data, 'book_images', 'book_img_id') : []);
+ } //image count closed
+ $response = ['status' => 200, 'message' => $message];
+ } //if cond. closed
+ else {
+ $response = ['status' => 204, 'message' => 'Data No Found'];
}
- return $this->respond($response);
+ return $response;
}
- public function save_customer_details()
- {
-
+ public function customer_api_integration()
+ {
$request = \Config\Services::request();
+ $message = "";
+ try {
+ switch ($this->request) {
+ case $this->request->is('put'):
+ try {
+ // $url = http://localhost/vb_book/api/customers?id=795;
+ $records = (array)$this->request->getVar();
+ $id = $this->request->getVar('id');
+ if ($records['id'] == $id) {
+ $response = $this->save_customer_details($records, $id);
+ } else {
+ $response = ['status' => 404, 'message' => "ID Mismatched"];
+ }
+ } catch (\Exception $e) {
+ return $this->fail('PUT request failed, An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ }
+ break;
+ case $this->request->is('post'):
+ try {
+ // $url = http://localhost/vb_book/api/customers;
+ $records = (array)$this->request->getVar();
+ $wordpress_customer_id = $records['id'];
+ $response = $this->save_customer_details($records, $wordpress_customer_id);
+ } catch (\Exception $e) {
+ return $this->fail('POST request failed, An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ }
+ break;
+ case $request->is('get'):
+ try {
+ // $url = http://localhost/vb_book/api/customers?id=795;
+ // $url = http://localhost/vb_book/api/customers;
+ $id = $this->request->getVar('id');
+ if ($id) {
+ $where = ['wp_api_customer_id' => (int)$id, 'isactive' => 1];
+ } else {
+ $where = ['isactive' => 1];
+ }
+ $api_model = new ApiIntegrationModel();
+ $customer_details = $api_model->setTable('customers')->where($where);
+ $response = ['status' => 200, 'message' => "Customer Details", "data" => $customer_details];
+ } catch (\Exception $e) {
+ return $this->fail('GET request failed, An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ }
+ break;
+ case $request->is('delete'):
+ try {
+ // $url = http://localhost/vb_book/api/customers?id=795;
+ $id = $this->request->getVar('id');
+ $api_model = new ApiIntegrationModel();
+ $where = ['isactive' => 1, 'wp_api_customer_id' => (int)$id];
+ $existing_customer_data = $api_model->getData('customers', $where);
+ if ((int)count($existing_customer_data) > 0) {
+ $customer_id = (int)$existing_customer_data[0]->customer_id;
+ $inactive_data['isactive'] = 0;
+ $update_where = ['isactive' => 1, 'customer_id' => $customer_id];
+ $api_model->updateData('customers', $inactive_data, $update_where);
+ $getExistingCustomerImageDetails = $api_model->getData('customer_addresses', $update_where);
+ if ($getExistingCustomerImageDetails) {
+
+ $api_model->updateData('customer_addresses', $inactive_data, $update_where);
+ }
+ }
+ $response = ['status' => 200, 'message' => "customer Details and Customer Addresses Details Are Inactived"];
+ } catch (\Exception $e) {
+ // Handle DELETE-specific exception
+ return $this->fail('DELETE request failed, An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ }
+ break;
+ default:
+ throw new \Exception('Unsupported HTTP method');
+ // return $this->fail('Unsupported HTTP method');
+ }
+ return $this->respond($response);
+ } catch (\Exception $e) {
+ // Handle the exception, you can log it or return an error response
+ return $this->fail('An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ // return $this->fail($e->getMessage());
+ }
+ }
+
+ public function save_customer_details($records, $wordpress_customer_id)
+ {
+
$api_model = new ApiIntegrationModel();
$message = "";
+ $customer_id = "";
+ if (count($records) > 0 && !empty($wordpress_customer_id)) {
+ $where = ['wp_api_customer_id' => $wordpress_customer_id, 'isactive' => 1];
+ $customer_details = $api_model->setTable('customers')->where($where)->first();
+ if (isset($customer_details['customer_id']) && $customer_details['customer_id'] != "") {
+ $customer_id = $customer_details['customer_id'];
+ $where['customer_id'] = $customer_details['customer_id'];
+ }
+ $countAll = $api_model->countAll('customers', $where);
- if ($request->is('post')) {
- $records = (array)$request->getVar();
- $message = "Reponse Count : ".count($records)."
";
- $x=0;
- $records = (array)$request->getVar();
- if (count($records) > 0 && !empty($records['id'])) {
- $where = ['wp_api_id' => $records['id']];
- $countAll = $api_model->countAll('customers', $where);
- if ((int)$countAll == 0) {
- $insertion_data['wp_api_id'] = $records['id'];
- $insertion_data['first_name'] = $records['first_name'];
- $insertion_data['last_name'] = $records['last_name'];
- $insertion_data['type'] = $records['role'];
- $insertion_data['email'] = $records['email'];
- $insertion_data['mode'] = 'online';
- $insertion_data['created_by'] = 777;
- $insertion_data['business_id'] = 7;
- $billing = $records['billing'];
- $shipping = $records['shipping'];
- $lastInsertId = $api_model->insertData('customers', $insertion_data);
- $insertion_baddress_data = [];$insertion_saddress_data = [];
- $i = 0; $j = 0;
-
- if(isset($billing) && gettype($billing) === 'object') {$billing = [$billing];}
- if(isset($shipping) && gettype($shipping) === 'object') {$shipping = [$shipping];}
- $message .= "Customer ID : ".$lastInsertId." have Billing address = ".count($billing)." and Shipping address = ".count($shipping)."
";
- if (count($billing) > 0) {
- foreach ($billing as $bill) {
- if ($bill->first_name !== '') {
- $insertion_baddress_data[$i]['first_name'] = $bill->first_name;
- $insertion_baddress_data[$i]['last_name'] = $bill->last_name;
- $insertion_baddress_data[$i]['company'] = $bill->company;
- $insertion_baddress_data[$i]['address_1'] = $bill->address_1;
- $insertion_baddress_data[$i]['address_2'] = $bill->address_2;
- $insertion_baddress_data[$i]['city'] = $bill->city;
- $insertion_baddress_data[$i]['state'] = $bill->state;
- $insertion_baddress_data[$i]['country'] = $bill->country;
- $insertion_baddress_data[$i]['postal_code'] = $bill->postcode;
- $insertion_baddress_data[$i]['customer_id'] = $lastInsertId;
- $insertion_baddress_data[$i]['address_type'] = 1;
- $insertion_baddress_data[$i]['created_by'] = 777;
- $insertion_baddress_data[$i]['email'] = isset($bill->email) ? $bill->email : '';
- $insertion_baddress_data[$i]['mobile_no'] = isset($bill->phone) ? $bill->phone : '';
- $i++;
- $message .= " Billing address has a value and Inserted ".$i."
";
- }else{
- $message .= " Keys are available but Billing address has no value;
";
- }
- }
- (!empty($insertion_baddress_data) ? $api_model->insertAddressBatch($insertion_baddress_data) : "");
- }
- if (count($shipping) > 0) {
- foreach ($shipping as $ship) {
- if ($ship->first_name !== '') {
- $insertion_saddress_data[$j]['first_name'] = $ship->first_name;
- $insertion_saddress_data[$j]['last_name'] = $ship->last_name;
- $insertion_saddress_data[$j]['company'] = $ship->company;
- $insertion_saddress_data[$j]['address_1'] = $ship->address_1;
- $insertion_saddress_data[$j]['address_2'] = $ship->address_2;
- $insertion_saddress_data[$j]['city'] = $ship->city;
- $insertion_saddress_data[$j]['state'] = $ship->state;
- $insertion_saddress_data[$j]['country'] = $ship->country;
- $insertion_saddress_data[$j]['postal_code'] = $ship->postcode;
- $insertion_saddress_data[$j]['email'] = isset($ship->email)?$ship->email:"";
- $insertion_saddress_data[$j]['mobile_no'] = isset($ship->phone)?$ship->phone:"";
- $insertion_saddress_data[$j]['customer_id'] = $lastInsertId;
- $insertion_saddress_data[$j]['address_type'] = 2;
- $insertion_saddress_data[$j]['created_by'] = 777;
- $j++;
- $message .= " Shipping address has a value and Inserted ".$j."
";
- }
- else{
- $message .= " Keys are available but Shipping address has no value
";
- }
- }
- (!empty($insertion_saddress_data) ? $api_model->insertAddressBatch($insertion_saddress_data) : "");
- }
- $response = ['status' => 200, 'message' => $message, 'data' => array()];
- } else {
- $response = ['status' => 204, 'message' => 'Data Already Exist', 'data' => array()];
+ $customer_data['first_name'] = $records['first_name'];
+ $customer_data['last_name'] = $records['last_name'];
+ $customer_data['type'] = $records['role'];
+ $customer_data['email'] = $records['email'];
+ $customer_data['profile_picture'] = $records['avatar_url'];
+ $customer_data['wp_api_customer_id'] = $wordpress_customer_id;
+ $customer_data['mode'] = 'online';
+ $customer_data['business_id'] = 1;
+ $billing = $records['billing'];
+
+ $shipping = $records['shipping'];
+
+ if ((int)$countAll == 0) {
+ $insert_result = $api_model->insertData('customers', $customer_data);
+ $last_insert_customer_id = $insert_result['info'];
+ $message .= $insert_result['info'] ? "Customer id : " . $insert_result['info']
+ : "Err :" . $insert_result['err'];
+ } else {
+ $last_insert_customer_id = $customer_id;
+ $customer_where = ['customer_id' => $customer_id, 'isactive' => 1, 'wp_api_customer_id' => $wordpress_customer_id];
+ $update_result = $api_model->updateData('customers', $customer_data, $customer_where);
+ $message .= $update_result['info'] ? "Customer id : " . $last_insert_customer_id . " ( " . $update_result['info'] . ")"
+ : " ( Err :" . $update_result['err'] . ")";
+ }
+
+ $i = 0; $customer_billing_address_data = [];
+ $j = 0; $customer_shipping_address_data = [];
+
+ if(isset($billing) && gettype($billing) === 'object') {$billing = [$billing];}
+ if(isset($shipping) && gettype($shipping) === 'object') {$shipping = [$shipping];}
+ // echo "Customer ID : ".$last_insert_customer_id." have Billing address = ".count($billing)." and Shipping address = ".count($shipping)."
";
+
+ if (count($billing) > 0 && $last_insert_customer_id != "") {
+
+ $message .= " Its have " . count($billing) . " Billing address ,";
+
+ $where = ['customer_id' => (int)$last_insert_customer_id, 'isactive' => 1, 'address_type'=>1];
+ $existing_billing = $api_model->getData('customer_addresses', $where);
+ $request_billing = (array)$billing;
+
+ $filteringExistingBillingAddress = [];
+ $filteringRequestedBillingAddress = [];
+ $commonBillingElements = [];
+ if (!empty($existing_billing)) {
+
+ for ($z = 0; $z < count($request_billing); $z++) {
+ $filteringRequestedBillingAddress[$z] = $request_billing[$z]->address_1;
+ }
+ for ($y = 0; $y < count($existing_billing); $y++) {
+ $filteringExistingBillingAddress[$y] = $existing_billing[$y]->address_1;
+ }
+
+ if (!empty($filteringExistingBillingAddress)) {
+ $A = $filteringExistingBillingAddress;
+ $B = $filteringRequestedBillingAddress;
+ $missingBillingValues = array_diff($A, $B); //Find difference element in Existing Images only
+ $commonBillingElements = array_intersect($A, $B);
+ if (!empty($missingBillingValues)) {
+ $inactive_where = ['isactive' => 1, 'customer_id' => (int)$last_insert_customer_id,'address_type'=>1];
+ $inactive_whereIn[0] = 'address_1';
+ $inactive_whereIn[1] = $missingBillingValues;
+ $api_model->inactiveMissingDetails('customer_addresses', $inactive_where, $inactive_whereIn);
+ }
+ }
}
- } //reponse count if closed
- }else{
- $response = ['status' => 204, 'message' => 'Invaild Method', 'data' => array()];
- }
- return $this->respond($response);
- }
+
+ $where = ['customer_id' => (int)$last_insert_customer_id, 'isactive' => 1,'address_type'=>1];
+ if (!empty($commonBillingElements)) {
+ $whereIn[0] = 'address_1';
+ $whereIn[1] = $commonBillingElements;
+ } else {
+ $whereIn = null;
+ }
+ $lastestActiveBillingAdressData = $api_model->getData('customer_addresses', $where, $whereIn);
+
+ foreach ($billing as $bill) {
+ $billingAddressId = null;
+ if ($bill->first_name != '') {
+ if (!empty($lastestActiveBillingAdressData)) {
+ foreach ($lastestActiveBillingAdressData as $item) {
+ if ($item->address_1 == $bill->address_1) {
+ $billingAddressId = $item->customer_address_id;
+ break; // Found the match, no need to continue looping
+ }
+ }
+ }
+ $customer_billing_address_data[$i]['first_name'] = $bill->first_name;
+ $customer_billing_address_data[$i]['last_name'] = $bill->last_name;
+ $customer_billing_address_data[$i]['company'] = $bill->company;
+ $customer_billing_address_data[$i]['address_1'] = $bill->address_1;
+ $customer_billing_address_data[$i]['address_2'] = $bill->address_2;
+ $customer_billing_address_data[$i]['city'] = $bill->city;
+ $customer_billing_address_data[$i]['state'] = $bill->state;
+ $customer_billing_address_data[$i]['country'] = $bill->country;
+ $customer_billing_address_data[$i]['postal_code'] = $bill->postcode;
+ $customer_billing_address_data[$i]['customer_id'] = $last_insert_customer_id;
+ $customer_billing_address_data[$i]['address_type'] = 1;
+ $customer_billing_address_data[$i]['email'] = isset($bill->email) ? $bill->email : '';
+ $customer_billing_address_data[$i]['mobile_no'] = isset($bill->phone) ? $bill->phone : '';
+ $customer_billing_address_data[$i]['customer_address_id'] = $billingAddressId;
+ $i++;
+ // echo " Billing address has a value and Inserted ".$i."
";
+ }else{
+ // echo " Keys are available but Billing address has no value;
";
+ }
+ }
+ $message .= (!empty($customer_billing_address_data) ? $api_model->insertAndUpdateChildDetails($customer_billing_address_data, 'customer_addresses', 'customer_address_id') : []);
+
+ }
+ if (count($shipping) > 0 && $last_insert_customer_id != "") {
+ $message .= " Its have " . count($shipping) . " Shipping address ,";
+ $where = ['customer_id' => (int)$last_insert_customer_id, 'isactive' => 1, 'address_type'=>2];
+ $existing_shipping = $api_model->getData('customer_addresses', $where);
+ $request_shipping = (array)$shipping;
+
+ $filteringExistingShippingAddress = [];
+ $filteringRequestedShippingAddress = [];
+ $commonShippingElements = [];
+ if (!empty($existing_shipping)) {
+
+ for ($z = 0; $z < count($request_shipping); $z++) {
+ $filteringRequestedShippingAddress[$z] = $request_shipping[$z]->address_1;
+ }
+ for ($y = 0; $y < count($existing_shipping); $y++) {
+ $filteringExistingShippingAddress[$y] = $existing_shipping[$y]->address_1;
+ }
+
+ if (!empty($filteringExistingShippingAddress)) {
+ $A = $filteringExistingShippingAddress;
+ $B = $filteringRequestedShippingAddress;
+ $missingShippingValues = array_diff($A, $B); //Find difference element in Existing Images only
+ $commonShippingElements = array_intersect($A, $B);
+ if (!empty($missingShippingValues)) {
+ $inactive_where = ['isactive' => 1, 'customer_id' => (int)$last_insert_customer_id, 'address_type'=>2];
+ $inactive_whereIn[0] = 'address_1';
+ $inactive_whereIn[1] = $missingShippingValues;
+ $api_model->inactiveMissingDetails('customer_addresses', $inactive_where, $inactive_whereIn);
+ }
+ }
+ }
+
+ $where = ['customer_id' => (int)$last_insert_customer_id, 'isactive' => 1 ,'address_type'=>2];
+ if (!empty($commonShippingElements)) {
+ $whereIn[0] = 'address_1';
+ $whereIn[1] = $commonShippingElements;
+ } else {
+ $whereIn = null;
+ }
+ $lastestActiveShippingAdressData = $api_model->getData('customer_addresses', $where, $whereIn);
+
+ foreach ($shipping as $ship) {
+ $shippingAddressId = null;
+ if ($ship->first_name !== '') {
+ if (!empty($lastestActiveShippingAdressData)) {
+ foreach ($lastestActiveShippingAdressData as $item) {
+ if ($item->address_1 == $ship->address_1) {
+ $shippingAddressId = $item->customer_address_id;
+ break; // Found the match, no need to continue looping
+ }
+ }
+ }
+ $customer_shipping_address_data[$j]['first_name'] = $ship->first_name;
+ $customer_shipping_address_data[$j]['last_name'] = $ship->last_name;
+ $customer_shipping_address_data[$j]['company'] = $ship->company;
+ $customer_shipping_address_data[$j]['address_1'] = $ship->address_1;
+ $customer_shipping_address_data[$j]['address_2'] = $ship->address_2;
+ $customer_shipping_address_data[$j]['city'] = $ship->city;
+ $customer_shipping_address_data[$j]['state'] = $ship->state;
+ $customer_shipping_address_data[$j]['country'] = $ship->country;
+ $customer_shipping_address_data[$j]['postal_code'] = $ship->postcode;
+ $customer_shipping_address_data[$j]['email'] = isset($ship->email)?$ship->email:"";
+ $customer_shipping_address_data[$j]['mobile_no'] = isset($ship->phone)?$ship->phone:"";
+ $customer_shipping_address_data[$j]['customer_id'] = $last_insert_customer_id;
+ $customer_shipping_address_data[$j]['address_type'] = 2;
+ $customer_shipping_address_data[$j]['customer_address_id'] = $shippingAddressId;
+ $j++;
+ // echo " Shipping address has a value and Inserted ".$j."
";
+ }
+ else{
+ // echo " Keys are available but Shipping address has no value
";
+ }
+ }
+ $message .= (!empty($customer_shipping_address_data) ? $api_model->insertAndUpdateChildDetails($customer_shipping_address_data, 'customer_addresses', 'customer_address_id') : []);
+ }
+ $response = ['status' => 200, 'message' => $message];
+ } //if cond. closed
+ else {
+ $response = ['status' => 204, 'message' => 'Data No Found'];
+ }
+ return $response;
+ }
+
+ public function sales_api_integration()
+ {
+ $request = \Config\Services::request();
+ $message = "";
+ try {
+ switch ($this->request) {
+ case $this->request->is('put'):
+ try {
+ // $url = http://localhost/vb_book/api/orders?id=795;
+ $records = (array)$this->request->getVar();
+ $id = $this->request->getVar('id');
+ if ($records['id'] == $id) {
+ $response = $this->save_sales_details($records, $id);
+ } else {
+ $response = ['status' => 404, 'message' => "ID Mismatched"];
+ }
+ } catch (\Exception $e) {
+ return $this->fail('PUT request failed, An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ }
+ break;
+ case $this->request->is('post'):
+ try {
+ // $url = http://localhost/vb_book/api/orders;
+ $records = (array)$this->request->getVar();
+ $wordpress_order_id = $records['id'];
+ $response = $this->save_sales_details($records, $wordpress_order_id);
+ } catch (\Exception $e) {
+ return $this->fail('POST request failed, An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ }
+ break;
+ case $request->is('get'):
+ try {
+ // $url = http://localhost/vb_book/api/orders?id=795;
+ // $url = http://localhost/vb_book/api/orders;
+ $id = $this->request->getVar('id');
+ if ($id) {
+ $where = ['wp_api_order_id' => (int)$id, 'isactive' => 1];
+ } else {
+ $where = ['isactive' => 1];
+ }
+ $api_model = new ApiIntegrationModel();
+ $invoice_details = $api_model->setTable('invoice')->where($where);
+ $response = ['status' => 200, 'message' => "Invoice Details", "data" => $invoice_details];
+ } catch (\Exception $e) {
+ return $this->fail('GET request failed, An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ }
+ break;
+ case $request->is('delete'):
+ try {
+ // $url = http://localhost/vb_book/api/orders?id=795;
+ $id = $this->request->getVar('id');
+ $api_model = new ApiIntegrationModel();
+ $where = ['isactive' => 1, 'wp_api_order_id' => (int)$id];
+ $existing_invoice_data = $api_model->getData('invoice', $where);
+ if ((int)count($existing_invoice_data) > 0) {
+ $invoice_id = (int)$existing_invoice_data[0]->invoice_id;
+ $inactive_data['isactive'] = 0;
+ $update_where = ['isactive' => 1, 'invoice_id' => $invoice_id];
+ $api_model->updateData('invoice', $inactive_data, $update_where);
+ $getExistingInvoiceItemsDetails = $api_model->getData('invoiceitems', $update_where);
+ if ($getExistingInvoiceItemsDetails) {
+ $api_model->updateData('invoiceitems', $inactive_data, $update_where);
+ }
+ }
+ $response = ['status' => 200, 'message' => "Invoice Details and Invoice Items Details Are Inactived"];
+ } catch (\Exception $e) {
+ // Handle DELETE-specific exception
+ return $this->fail('DELETE request failed, An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ }
+ break;
+ default:
+ throw new \Exception('Unsupported HTTP method');
+ // return $this->fail('Unsupported HTTP method');
+ }
+ return $this->respond($response);
+ } catch (\Exception $e) {
+ // Handle the exception, you can log it or return an error response
+ return $this->fail('An error occurred on line ' . $e->getline() . ': ' . $e->getMessage());
+ // return $this->fail($e->getMessage());
+ }
+ }
+
+ public function save_sales_details($records, $wordpress_order_id){
+ $api_model = new ApiIntegrationModel();
+ $message = "";
+ $invoice_id = "";
+ if (count($records) > 0 && !empty($wordpress_order_id)) {
+ $where = ['wp_api_order_id' => $wordpress_order_id, 'isactive' => 1];
+ $invoice_details = $api_model->setTable('invoice')->where($where)->first();
+ if (isset($invoice_details['invoice_id']) && $invoice_details['invoice_id'] != "") {
+ $invoice_id = $invoice_details['invoice_id'];
+ $where['invoice_id'] = (int)$invoice_id;
+ }
+ $countAll = $api_model->countAll('invoice', $where);
+ $billing_addr = "";$shipping_addr = "";
+ $lineitems_array = [];
+ $billing_array = []; $shipping_array = [];
+ $lineitems_subtotal = 0; $lineitems_tax = 0;
+ $lineitems_discount = 0; $lineitems_total=0;
+
+ // $line_items = ;
+
+ if(isset($records['billing']) && gettype($records['billing']) === 'object') {$billing_array = [$records['billing']];}
+ if(isset($records['shipping']) && gettype($records['shipping']) === 'object') {$shipping_array = [$records['shipping']];}
+
+ 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 (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 (count($records['line_items']) > 0) {
+ // echo count($records['line_items']);die;
+ $lineitems_array = $records['line_items'];
+ foreach ($lineitems_array as $item) {
+ $lineitems_subtotal += (float)$item->subtotal;
+ $lineitems_tax += (float)$item->total_tax;
+ }
+ $lineitems_total += ((float)$lineitems_subtotal + (float)$lineitems_tax) - (float)$lineitems_discount;
+ }
+
+ $subtotal = $lineitems_subtotal + (float)$records['shipping_total'];
+ $tax = $lineitems_tax + (float)$records['shipping_tax'];
+ $total = ($lineitems_total)+(float)$records['shipping_total'] + (float)$records['shipping_tax'];
+
+ $invoice_data['invoice_id']=$invoice_id;
+ $invoice_data['invoice_number']=$records['number'];
+ $invoice_data['customer_id']=$records['customer_id'];
+ $invoice_data['wp_api_order_id']=$records['id'];
+ $invoice_data['invoice_date']=date("Y-m-d", strtotime($records['date_created']));
+ $invoice_data['due_date']=NULL;
+ $invoice_data['subtotal']=number_format($subtotal, 2);
+ $invoice_data['tax']=number_format($tax, 2);
+ $invoice_data['discount']=number_format($lineitems_discount, 2);
+ $invoice_data['total_amount']= number_format($total, 2);
+ $invoice_data['payment_status']="";
+ $invoice_data['order_number']=$records['order_key'];
+ $invoice_data['payment_method']=$records['payment_method'];
+ $invoice_data['event_id']=NULL;
+ $invoice_data['business_id']=1;
+ $invoice_data['shipping_address']=$shipping_addr;
+ $invoice_data['shipping_address_id']=NULL;
+ $invoice_data['billing_address']=$billing_addr;
+ $invoice_data['billing_address_id'] =NULL;
+
+ if ((int)$countAll == 0) {
+ $insert_result = $api_model->insertData('invoice', $invoice_data);
+ $last_insert_invoice_id = $insert_result['info'];
+ $message .= $insert_result['info'] ? "Invoice id : " . $insert_result['info']
+ : "Err :" . $insert_result['err'];
+ } else {
+ $last_insert_invoice_id = $invoice_id;
+ $customer_where = ['invoice_id' => $invoice_id, 'isactive' => 1, 'wp_api_order_id' => $wordpress_order_id];
+ $update_result = $api_model->updateData('invoice', $invoice_data, $customer_where);
+ $message .= $update_result['info'] ? "Invoice id : " . $last_insert_invoice_id . " ( " . $update_result['info'] . ")"
+ : " ( Err :" . $update_result['err'] . ")";
+ }
+
+ $line_item_data = [];
+ $shipping_line_item_data = [];
+ $i = 0;$j=0;
+ if (count($lineitems_array) > 0 && $last_insert_invoice_id != "") {
+ $message .= " Its have " . count($lineitems_array) . " LineItems ,";
+ $where = ['invoice_id' => (int)$last_insert_invoice_id, 'isactive' => 1];
+ $existing_invoiceitem = $api_model->getData('invoiceitems', $where);
+ $request_invoiceitem = (array)$lineitems_array;
+
+ $filteringExistingInvoiceItemWpApiId = [];
+ $filteringRequestedInvoiceItemWpApiId = [];
+ $commonElements = [];
+ if (!empty($existing_invoiceitem)) {
+
+ for ($z = 0; $z < count($request_invoiceitem); $z++) {
+ $filteringRequestedInvoiceItemWpApiId[$z] = $request_invoiceitem[$z]->id;
+ }
+ for ($y = 0; $y < count($existing_invoiceitem); $y++) {
+ $filteringExistingInvoiceItemWpApiId[$y] = $existing_invoiceitem[$y]->wp_api_line_items_id;
+ }
+
+ if (!empty($filteringExistingInvoiceItemWpApiId)) {
+ $A = $filteringExistingInvoiceItemWpApiId;
+ $B = $filteringRequestedInvoiceItemWpApiId;
+ $missingValues = array_diff($A, $B); //Find difference element in Existing Images only
+ $commonElements = array_intersect($A, $B);
+ if (!empty($missingValues)) {
+ $inactive_where = ['isactive' => 1, 'invoice_id' => (int)$last_insert_invoice_id];
+ $inactive_whereIn[0] = 'wp_api_line_items_id';
+ $inactive_whereIn[1] = $missingValues;
+ $api_model->inactiveMissingDetails('invoiceitems', $inactive_where, $inactive_whereIn);
+ }
+ }
+ }
+
+ $where = ['invoice_id' => (int)$last_insert_invoice_id, 'isactive' => 1];
+ if (!empty($commonElements)) {
+ $whereIn[0] = 'wp_api_line_items_id';
+ $whereIn[1] = $commonElements;
+ } else {
+ $whereIn = null;
+ }
+ $lastestActiveInvoiceitemsData = $api_model->getData('invoiceitems', $where, $whereIn);
+
+ foreach ($lineitems_array as $ii) {
+ $invoiceItemsId = null;
+
+ // Iterate through the array and find the 'book_img_id' based on 'wp_api_img_id'
+ if (!empty($lastestActiveInvoiceitemsData)) {
+ foreach ($lastestActiveInvoiceitemsData as $item) {
+ if ($item->wp_api_line_items_id == $ii->id) {
+ $invoiceItemsId = $item->invoice_child_id;
+ break; // Found the match, no need to continue looping
+ }
+ }
+ }
+
+ $where = ['wp_api_product_id' => (int)$ii->product_id];
+ $get_book_data_based_on_wp_product_id = $api_model->getData('books', $where);
+
+ $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;
+ $line_item_data[$i]['tax'] =(float)$ii->total_tax;
+ $line_item_data[$i]['subtotal'] =(float)$ii->total;
+ $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;
+ $i++;
+ } // line item loop closed
+ $message .= (!empty($line_item_data) ? $api_model->insertAndUpdateChildDetails($line_item_data, 'invoiceitems', 'invoice_child_id') : []);
+
+
+ }
+ $shipping_lineitems_array = $records['shipping_lines'];
+ if (count($shipping_lineitems_array) > 0 && $last_insert_invoice_id != "") {
+ $message .= " Its have " . count($shipping_lineitems_array) . " shipping details ,";
+ $where = ['invoice_id' => (int)$last_insert_invoice_id, 'isactive' => 1];
+ $existing_shipping_invoiceitem = $api_model->getData('invoiceitems', $where);
+ $request_shipping_invoiceitem = (array)$shipping_lineitems_array;
+
+ $filteringExistingShippingInvoiceItemWpApiId = [];
+ $filteringRequestedShippingInvoiceItemWpApiId = [];
+ $commonShippingElements = [];
+ if (!empty($existing_shipping_invoiceitem)) {
+
+ for ($z = 0; $z < count($request_shipping_invoiceitem); $z++) {
+ $filteringRequestedShippingInvoiceItemWpApiId[$z] = $request_shipping_invoiceitem[$z]->id;
+ }
+ for ($y = 0; $y < count($existing_shipping_invoiceitem); $y++) {
+ $filteringExistingShippingInvoiceItemWpApiId[$y] = $existing_shipping_invoiceitem[$y]->wp_api_line_items_id;
+ }
+
+ if (!empty($filteringExistingShippingInvoiceItemWpApiId)) {
+ $A = $filteringExistingShippingInvoiceItemWpApiId;
+ $B = $filteringRequestedShippingInvoiceItemWpApiId;
+ $missingShippingValues = array_diff($A, $B); //Find difference element in Existing Images only
+ $commonShippingElements = array_intersect($A, $B);
+ if (!empty($missingShippingValues)) {
+ $inactive_where = ['isactive' => 1, 'invoice_id' => (int)$last_insert_invoice_id];
+ $inactive_whereIn[0] = 'wp_api_line_items_id';
+ $inactive_whereIn[1] = $missingShippingValues;
+ $api_model->inactiveMissingDetails('invoiceitems', $inactive_where, $inactive_whereIn);
+ }
+ }
+ }
+
+ $where = ['invoice_id' => (int)$last_insert_invoice_id, 'isactive' => 1];
+ if (!empty($commonShippingElements)) {
+ $whereIn[0] = 'wp_api_line_items_id';
+ $whereIn[1] = $commonShippingElements;
+ } else {
+ $whereIn = null;
+ }
+ $lastestActiveShippingInvoiceitemsData = $api_model->getData('invoiceitems', $where, $whereIn);
+
+ foreach ($shipping_lineitems_array as $sii) {
+ $shippingInvoiceItemsId = null;
+
+ // Iterate through the array and find the 'book_img_id' based on 'wp_api_img_id'
+ if (!empty($lastestActiveShippingInvoiceitemsData)) {
+ foreach ($lastestActiveShippingInvoiceitemsData as $item) {
+ if ($item->wp_api_line_items_id == $sii->id) {
+ $shippingInvoiceItemsId = $item->invoice_child_id;
+ break; // Found the match, no need to continue looping
+ }
+ }
+ }
+
+ $where = ['title' => (int)$sii->method_title];
+ $get_book_data_based_on_title = $api_model->getData('books', $where);
+
+ $shipping_line_item_data[$j]['product'] = (!empty($get_book_data_based_on_title) ? $get_book_data_based_on_title[0]->book_id : NULL);
+ $shipping_line_item_data[$j]['quantity'] = 0;
+ $shipping_line_item_data[$j]['unit_price'] =0;
+ $shipping_line_item_data[$j]['tax'] =(float)$sii->total_tax;
+ $shipping_line_item_data[$j]['subtotal'] =(float)$sii->total;
+ $shipping_line_item_data[$j]['invoice_child_id'] = $shippingInvoiceItemsId;
+ $shipping_line_item_data[$j]['wp_api_line_items_id'] = $sii->id;
+ $shipping_line_item_data[$j]['invoice_id'] = $last_insert_invoice_id;
+ $j++;
+ } // line item loop closed
+ $message .= (!empty($shipping_line_item_data) ? $api_model->insertAndUpdateChildDetails($shipping_line_item_data, 'invoiceitems', 'invoice_child_id') : []);
+ }
+ $response = ['status' => 200, 'message' => $message];
+ }
+ else {
+ $response = ['status' => 204, 'message' => 'Data No Found'];
+ }
+ return $response;
+ }
+
+
}
diff --git a/app/Models/ApiIntegrationModel.php b/app/Models/ApiIntegrationModel.php
index 4f75e024..8a221eda 100644
--- a/app/Models/ApiIntegrationModel.php
+++ b/app/Models/ApiIntegrationModel.php
@@ -15,17 +15,28 @@ class ApiIntegrationModel extends Model
public function insertData($table, $data)
{
- $this->db->table($table)->insert($data);
- $last_insert_id = $this->db->insertID();
- return $last_insert_id;
-
+ try {
+ $this->db->table($table)->insert($data);
+ $last_insert_id = $this->db->insertID();
+ return ["info"=>$last_insert_id,"err"=>""];
+ } catch (\CodeIgniter\Database\Exceptions\DatabaseException $e) {
+ return ["info"=>"","err"=>'Database operation failed: ' . $e->getMessage()];
+ } catch (\Exception $e) {
+ return ["info"=>"","err"=>'An error occurred: ' . $e->getMessage()];
+ }
}
public function updateData($table, $data, $where)
{
- $this->db->table($table)->update($data, $where);
- $affected_rows = $this->db->affectedRows();
- return $affected_rows;
+ try {
+ $this->db->table($table)->update($data, $where);
+ $affected_rows = $this->db->affectedRows();
+ return ["info"=>$affected_rows." Affected","err"=>""];
+ } catch (\CodeIgniter\Database\Exceptions\DatabaseException $e) {
+ return ["info"=>"","err"=>'Database operation failed: ' . $e->getMessage()];
+ } catch (\Exception $e) {
+ return ["info"=>"","err"=>'Database operation failed: ' . $e->getMessage()];
+ }
}
public function countAll($table, $where = null)
@@ -38,37 +49,43 @@ class ApiIntegrationModel extends Model
}
- public function getData($table, $where = null)
+ public function getData($table, $where = null,$whereIn = null)
{
$query = $this->db->table($table);
if ($where) {
$query->where($where);
}
+ if($whereIn){
+ // $query->whereIn($column_name,$missingValues)
+ $query->whereIn($whereIn[0],$whereIn[1]);
+ }
return $query->get()->getResult();
}
- public function insertImagesBatch($imgDataArray) {
- $this->db->table('book_images')->insertBatch($imgDataArray);
+ public function insertAndUpdateChildDetails($data,$table_name,$column_name){
+ $i = 1;
+ $text = ' Child-table Name = '.str_replace("_", " ",ucwords($table_name)) .', Column = '.str_replace("_", " ",ucwords($column_name)) .'. ';
+ foreach ($data as $row) {
+ $id = isset($row[$column_name]) ? $row[$column_name] : "";
+ if($id != ''){
+ unset($row[$column_name]); // Remove the primary Id from the data to avoid updating it.
+ $where = [$column_name=>$id,'isactive'=>1];
+ $child_affected_rows = $this->updateData($table_name, $row, $where);
+ $text .= $child_affected_rows["info"] ? $i.') Id = '.$id.', '.$child_affected_rows["info"].'. '
+ : $i.') Id = '.$id.', Err = '.$child_affected_rows["err"].'. ';
+ }else{
+ $child_insert_id = $this->insertData($table_name, $row);
+ $text .= $child_insert_id["info"] ? $i.') Inserted ID = '.$child_insert_id["info"].'. '
+ : $i.') Err = '.$child_insert_id["err"].'. ' ;
+ }
+ $i++;
+ }
+ return $text;
}
- public function saveAddressDetails($data){
- $i = 0;
- $statement = [];
- foreach ($data as $row) {
- $id = $row['customer_address_id'];
- if($id != ''){
- unset($row['customer_address_id']); // Remove the id from the data to avoid updating it
- unset($row['created_by']); // bcoz here data Updating here.
- $this->db->table('customer_addresses')->where('customer_address_id', $id)->update($row);// Update the row with the specified id
- $affectedRows = $this->db->affectedRows();
- $statement[$i] = "address - ".$id." ".$affectedRows ? " Updated":" Not Updated";
- }else{
-
- $this->db->table('customer_addresses')->insert($row);
- $insertID = $this->db->insertID();
- $statement[$i] = "address - ".$insertID." Inserted";
- }
- }
- return $statement;
+ public function inactiveMissingDetails($table_name,$where,$whereIn){
+ $dataToUpdate = ['isactive' => 0];
+ $this->db->table($table_name)->where($where)->whereIn($whereIn[0],$whereIn[1])->update($dataToUpdate);
}
+
}