diff --git a/app/Controllers/ApiIntegration.php b/app/Controllers/ApiIntegration.php index a78d5aae..a533b1cf 100644 --- a/app/Controllers/ApiIntegration.php +++ b/app/Controllers/ApiIntegration.php @@ -11,7 +11,25 @@ class ApiIntegration extends ResourceController { use ResponseTrait; + private $global_variable_business_id; + private $global_variable_user_id; + private $global_variable_user_name; + private $global_variable_role; + + public function __construct() + { + $this->global_variable_business_id = 1; + $this->global_variable_role = "online"; + $api_model = new ApiIntegrationModel(); + $table = 'users'; + $where = ['role'=>$this->global_variable_role,'business_id'=>(int)$this->global_variable_business_id]; + $existing_data = $api_model->getData($table, $where); + $this->global_variable_user_id = isset($existing_data[0]->user_id) ? (int)$existing_data[0]->user_id : NULL; + $this->global_variable_user_name = isset($existing_data[0]->user_id) ? $existing_data[0]->first_name." ".$existing_data[0]->last_name : NULL; + } + public function api_integration($api_request_text) { + $this->logger->info("Api Integration : UID = ".$this->global_variable_user_id." * ".$this->global_variable_user_name); $this->logger->info("Api Integration : Request Text = ".$api_request_text); try { $request_data = $this->request->getVar(); @@ -142,6 +160,7 @@ class ApiIntegration extends ResourceController $primary_key_id = (int)$existing_data[0][$table_column[0]]; $this->logger->info("Api CallMethods : ".$table." its PrimaryKey ID = ".$primary_key_id); $inactive_data['isactive'] = 0; + $inactive_data['updated_by'] = $this->global_variable_user_id; $update_where = ['isactive' => 1, $table_column[0] => $primary_key_id]; $api_model->updateData($table, $inactive_data, $update_where); $get_existing_child_details = $api_model->getData($table_child, $update_where); @@ -253,9 +272,10 @@ class ApiIntegration extends ResourceController $book_data['price'] = (isset($records['sale_price']) && $records['sale_price'] != '') ? $records['sale_price'] : ((isset($records['regular_price']) && $records['regular_price'] != '') ? $records['regular_price'] : ((isset($records['price']) && $records['price'] != '') ? $records['price'] : NULL)); $book_data['sku'] = isset($records['sku']) ? $records['sku'] : NULL; $book_data['isactive'] = (isset($records['status']) && strtolower($records['status']) === "publish") ? 1 : 0; - $book_data['business_id'] = 1; + $book_data['business_id'] = $this->global_variable_business_id; $images = $records['images']; if ((int)$countAll == 0) { + $book_data['created_by'] = $this->global_variable_user_id; $insert_result = $api_model->insertData('books', $book_data); $last_insert_book_id = $insert_result['info']; $extensive_correspondence .= $insert_result['info'] ? "Book id : " . $insert_result['info'] @@ -265,6 +285,7 @@ class ApiIntegration extends ResourceController $insert_result['err'] ? $this->logger->error("Api SaveBookDetails : Err = ".$insert_result['err']):""; } else { $last_insert_book_id = $book_id; + $book_data['updated_by'] = $this->global_variable_user_id; /** Draft status means entry must but inactive. publish means active that's why comment here * $book_where = ['book_id' => $book_id, 'isactive' => 1, 'wp_api_product_id' => $wordpress_book_id]; **/ @@ -310,7 +331,8 @@ class ApiIntegration extends ResourceController $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); + $data_to_batch_update = ['isactive' => 0,'updated_by'=>$this->global_variable_user_id]; + $api_model->inactiveMissingDetails('book_images',$data_to_batch_update, $inactive_where, $inactive_whereIn); } } } @@ -401,7 +423,8 @@ class ApiIntegration extends ResourceController $where2 = ['isactive' => 1]; $whereIn2[0] = 'category_id'; $whereIn2[1] = $book_categories_to_inactive; - $api_model->inactiveMissingDetails('book_categories', $where2, $whereIn2); + $data_to_batch_update2 = ['isactive' => 0,'updated_by'=>$this->global_variable_user_id]; + $api_model->inactiveMissingDetails('book_categories',$data_to_batch_update2, $where2, $whereIn2); //echo $api_model->getlastQuery(); } } // loop closed @@ -478,13 +501,14 @@ class ApiIntegration extends ResourceController $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; + $customer_data['mode'] = $this->global_variable_role; + $customer_data['business_id'] = $this->global_variable_business_id; $billing = $records['billing']; $shipping = $records['shipping']; if ((int)$countAll == 0) { + $customer_data['created_by'] = $this->global_variable_user_id; $insert_result = $api_model->insertData('customers', $customer_data); $last_insert_customer_id = $insert_result['info']; $extensive_correspondence .= $insert_result['info'] ? "Customer id : " . $insert_result['info'] @@ -493,6 +517,7 @@ class ApiIntegration extends ResourceController $insert_result['info'] ? $this->logger->info("Api SaveCustomerDetails : Customer id = ".$insert_result['info']):""; $insert_result['err'] ? $this->logger->error("Api SaveCustomerDetails : Err = ".$insert_result['err']):""; } else { + $customer_data['updated_by'] = $this->global_variable_user_id; $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); @@ -545,7 +570,8 @@ class ApiIntegration extends ResourceController $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); + $data_to_batch_update = ['isactive' => 0,'updated_by'=>$this->global_variable_user_id]; + $api_model->inactiveMissingDetails('customer_addresses',$data_to_batch_update, $inactive_where, $inactive_whereIn); } } } @@ -626,7 +652,8 @@ class ApiIntegration extends ResourceController $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); + $data_to_batch_update = ['isactive' => 0,'updated_by'=>$this->global_variable_user_id]; + $api_model->inactiveMissingDetails('customer_addresses',$data_to_batch_update, $inactive_where, $inactive_whereIn); $this->logger->info("Api SaveCustomerDetails : Missing Shipping " .implode(",",$missingShippingValues)." Are inactived"); } } @@ -715,20 +742,30 @@ class ApiIntegration extends ResourceController if(isset($records['billing']) && gettype($records['billing']) === 'object') {$billing_array = [$records['billing']];} if(isset($records['shipping']) && gettype($records['shipping']) === 'object') {$shipping_array = [$records['shipping']];} - + $where = ['wp_api_customer_id' => (int)$records['customer_id'], 'isactive' => 1]; + $local_customer = $api_model->getData('customers', $where); + $local_customer_id = !empty($local_customer) ? $local_customer[0]->customer_id : NULL; + $this->logger->info((!empty($local_customer)) ? "Api SaveSalesDetails : in wordpress customer ref ID = ".$records['customer_id']." is available on Customer table and its PrimaryKey = ".$local_customer_id."(local customer id)" : "Api SaveSalesDetails : in wordpress customer ref ID = ".$records['customer_id']." Not available on Customer table"); + if (count($billing_array) > 0) { - foreach ($billing_array as $bill) { - if($bill->first_name != ""){ - $billing_addr = "".$bill->address_1 .",". $bill->address_2 ."
".$bill->city." ".$bill->postcode."
".$bill->state." ".$bill->country.".
"; - } - } + // foreach ($billing_array as $bill) { + // if($bill->first_name != ""){ + // $billing_addr = $bill->address_1 . " " . $bill->address_2 . ",\n" . $bill->city . ",\n" . $bill->state . " - " . $bill->postal_code . ",\n" . $bill->country . "."; + // } + // } + $result_billing_addr = $this->save_invoice_address_in_customer_address($billing_array,1,(int)$local_customer_id); + $billing_addr_id = $result_billing_addr['addr_id']; + $billing_addr = $result_billing_addr['addr']; } if (count($shipping_array) > 0) { - foreach ($shipping_array as $ship) { - if($ship->first_name != ""){ - $shipping_addr = "".$ship->address_1 .",". $ship->address_2 ."
".$ship->city." ".$ship->postcode."
".$ship->state." ".$ship->country.".
"; - } - } + // foreach ($shipping_array as $ship) { + // if($ship->first_name != ""){ + // $shipping_addr = $ship->address_1 . " " . $ship->address_2 . ",\n" . $ship->city . ",\n" . $ship->state . " - " . $ship->postal_code . ",\n" . $ship->country . "."; + // } + // } + $result_shipping_addr = $this->save_invoice_address_in_customer_address($shipping_array,2,(int)$local_customer_id); + $shipping_addr_id = $result_shipping_addr['addr_id']; + $shipping_addr = $result_shipping_addr['addr']; } if (count($records['line_items']) > 0) { @@ -744,9 +781,7 @@ class ApiIntegration extends ResourceController $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']; - $where = ['wp_api_customer_id' => (int)$records['customer_id'], 'isactive' => 1]; - $local_customer = $api_model->getData('customers', $where); - $local_customer_id = !empty($local_customer) ? $local_customer[0]->customer_id : ""; + $invoice_data['invoice_id']=$invoice_id; $invoice_data['invoice_number']=$records['number']; @@ -762,16 +797,16 @@ class ApiIntegration extends ResourceController $invoice_data['order_number']=$records['order_key']; $invoice_data['payment_method']=$records['payment_method_title']; $invoice_data['event_id']=NULL; - $invoice_data['business_id']=1; + $invoice_data['business_id']=$this->global_variable_business_id; $invoice_data['shipping_address']=$shipping_addr; - $invoice_data['shipping_address_id']=NULL; + $invoice_data['shipping_address_id']=$shipping_addr_id; $invoice_data['billing_address']=$billing_addr; - $invoice_data['billing_address_id'] =NULL; + $invoice_data['billing_address_id'] =$billing_addr_id; $invoice_data['shipping_charge'] = isset($records['shipping_total'])?$records['shipping_total']:NULL; $invoice_data['notes'] = isset($records['customer_note'])?$records['customer_note']:NULL; $invoice_data['invoice_type'] = 1; - if ((int)$countAll == 0) { + $invoice_data['created_by'] = $this->global_variable_user_id; $insert_result = $api_model->insertData('invoice', $invoice_data); $last_insert_invoice_id = $insert_result['info']; $extensive_correspondence .= $insert_result['info'] ? "Invoice id : " . $insert_result['info'] @@ -782,6 +817,7 @@ class ApiIntegration extends ResourceController } else { $last_insert_invoice_id = $invoice_id; $customer_where = ['invoice_id' => $invoice_id, 'isactive' => 1, 'wp_api_order_id' => $wordpress_order_id]; + $invoice_data['updated_by'] = $this->global_variable_user_id; $update_result = $api_model->updateData('invoice', $invoice_data, $customer_where); $extensive_correspondence .= $update_result['info'] ? "Invoice id : " . $last_insert_invoice_id . " ( " . $update_result['info'] . ")" : " ( Err :" . $update_result['err'] . ")"; @@ -829,7 +865,8 @@ class ApiIntegration extends ResourceController $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); + $data_to_batch_update = ['isactive' => 0,'updated_by'=>$this->global_variable_user_id]; + $api_model->inactiveMissingDetails('invoiceitems',$data_to_batch_update, $inactive_where, $inactive_whereIn); } } } @@ -890,7 +927,7 @@ class ApiIntegration extends ResourceController $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; + $subscription_data[$x]['business_id']=$this->global_variable_business_id; $this->logger->info("Api SaveSalesDetails : Subscription Data inx = $x Request data = ".json_encode($subscription_data)); } } @@ -1089,7 +1126,7 @@ class ApiIntegration extends ResourceController $book_category_data[$a]['id'] = $masterCategoryId; $book_category_data[$a]['name'] = $r->name; $book_category_data[$a]['wp_api_category_id'] = $r->id; - $book_category_data[$a]['business_id'] = 1; + $book_category_data[$a]['business_id'] = $this->global_variable_business_id; $book_category_data[$a]['parent_wp_api_category_id'] = NULL; $a++; $this->logger->info("Api SaveCategoriesMasterDetails : Categories Data = $a Request data to save on master category = " . json_encode($book_category_data)); @@ -1101,4 +1138,61 @@ class ApiIntegration extends ResourceController $this->logger->error("Api SaveCategoriesMasterDetails : Err = Categories Data Not Found"); } } + + public function save_invoice_address_in_customer_address($request_addr_array,$flag,$customer_id){ + $this->logger->info("Api saveInvoiceAddressInCustomerAddress = ".json_encode($request_addr_array)); + $this->logger->info("Api saveInvoiceAddressInCustomerAddress Flag = ".$flag." Customer Id = ".$customer_id); + $api_model = new ApiIntegrationModel(); + $where = ['customer_id' =>$customer_id,'address_type'=>$flag, + 'first_name'=>$request_addr_array[0]->first_name,'last_name'=>$request_addr_array[0]->last_name, + 'address_1'=>$request_addr_array[0]->address_1,'address_2'=>$request_addr_array[0]->address_2, + 'city'=>$request_addr_array[0]->city,'state'=>$request_addr_array[0]->state, + 'postal_code'=>$request_addr_array[0]->postcode]; + $existing_addr = $api_model->getData('customer_addresses', $where); + $this->logger->info("Api saveInvoiceAddressInCustomerAddress Count = ".count($existing_addr)); + $final_array['addr_id'] = NULL; + $final_array['addr'] = NULL; + + $addr_data = []; + + if (!empty($existing_addr)) { + $final_array['addr_id'] = isset($existing_addr) && !empty($existing_addr) ? $existing_addr[0]->customer_address_id : NULL; + $this->logger->info("Api saveInvoiceAddressInCustomerAddress Ex Addr id = ".$final_array['addr_id']); + if($existing_addr[0]->first_name != ""){ + $final_array['addr'] = $existing_addr[0]->address_1 . " " . $existing_addr[0]->address_2 . ",\n" . $existing_addr[0]->city . ",\n" . $existing_addr[0]->state . " - " . $existing_addr[0]->postal_code . ",\n" . $existing_addr[0]->country . "."; + } + $this->logger->info("Api saveInvoiceAddressInCustomerAddress Ex Addr = ".$final_array['addr']); + } + if (empty($existing_addr)) { + foreach ($request_addr_array as $addr) { + if($addr->first_name != ""){ + $addr_data['first_name'] = $addr->first_name; + $addr_data['last_name'] = $addr->last_name; + $addr_data['company'] = $addr->company; + $addr_data['address_1'] = $addr->address_1; + $addr_data['address_2'] = $addr->address_2; + $addr_data['city'] = $addr->city; + $addr_data['state'] = $addr->state; + $addr_data['country'] = $addr->country; + $addr_data['postal_code'] = $addr->postcode; + $addr_data['email'] = isset($addr->email)? $addr->email:""; + $addr_data['mobile_no'] = isset($addr->phone)?$addr->phone:""; + $addr_data['customer_id'] = $customer_id; + $addr_data['address_type'] = $flag; + $addr_data['customer_address_id'] = NULL; + $addr_data['created_by'] = $this->global_variable_user_id; + $final_array['addr'] = $addr->address_1 . " " . $addr->address_2 . ",\n" . $addr->city . ",\n" . $addr->state . " - " . $addr->postcode . ",\n" . $addr->country . "."; + } + } + $this->logger->info("Api saveInvoiceAddressInCustomerAddress New Addr = ".$final_array['addr']); + if(!empty($addr_data)){ + + $insert_result = $api_model->insertData('customer_addresses',$addr_data); + $final_array['addr_id'] = $insert_result['info']; + $this->logger->info("Api saveInvoiceAddressInCustomerAddress New Addr id = ".$final_array['addr_id']); + } + + } + return $final_array; + } } \ No newline at end of file diff --git a/app/Controllers/Books.php b/app/Controllers/Books.php index 98604094..74cd3020 100755 --- a/app/Controllers/Books.php +++ b/app/Controllers/Books.php @@ -63,6 +63,7 @@ class Books extends BaseController ## For inserting/updating details of book public function insert_books() { + try{ helper('session'); $session_uid = get_logged_user_id(); $session_bid = get_business_id(); @@ -125,6 +126,13 @@ class Books extends BaseController } // print_r($bookId); print_r($imageData); print_r($category_id);die; + if ($BooksModel->insert($data)) { + session()->setFlashdata('success', 'Book has been added successfully.'); + $this->logger->info("Books: has been added successfully. Inserted ID = " . $BooksModel->insertID()); + } else { + session()->setFlashdata('error', 'Book could not be added. Please try again..'); + $this->logger->error("Books: Err could not be added. Please try again."); + } } else { // It's an update operation $isactive = $this->request->getPost('isactive'); @@ -142,6 +150,17 @@ class Books extends BaseController // Insert the book-category association into the book_categories table $BooksModel->db->table('book_categories')->insert(['book_id' => $book_id, 'category_id' => $category_id]); } + if ($BooksModel->update($book_id, $data)) { + session()->setFlashdata('success', 'Book has been updated successfully.'); + $this->logger->info("Books: has been updated successfully. Updated Book ID = " . $book_id); + } else { + session()->setFlashdata('error', 'Book update failed. Please try again.'); + $this->logger->error("Books: Err Failed to update ID =" . $book_id); + } + } + } catch (\Exception $e) { + $this->logger->error("Book: Err Occur = ".$e->getMessage()." File = ". $e->getFile() . " Line = " . $e->getLine()); + session()->setFlashdata('error', 'Message: ' . $e->getMessage()); } return redirect()->route('book_list'); @@ -150,21 +169,55 @@ class Books extends BaseController ## For delete the book details (Which means inactive the details) public function delete_books($id) { - - helper('session'); - $session_uid = get_logged_user_id(); - - $BooksModel = new BooksModel(); - - $existingBook = $BooksModel->find($id); - if (!$existingBook) { - return redirect()->route('book_list'); + try { + helper('session'); + $session_uid = get_logged_user_id(); + $BooksModel = new BooksModel(); + $where = ['book_id' => (int)$id, 'isactive =' => 1]; + // $existingBook = $BooksModel->find($id); + $existingBook = $BooksModel->where($where)->find($id); + if ($existingBook) { + $this->logger->Info("BOOK: Going to Inactive ID = ".$id); + $data = ['isactive' => 0 , 'updated_by' => $session_uid]; + if ($BooksModel->update($id, $data)) { + $activeBookImages = $BooksModel->getData('book_images', $where); + if(!empty($activeBookImages)){ + for ($y = 0; $y < count($activeBookImages); $y++) { + $bookImgIds[$y] = $activeBookImages[$y]->book_img_id; + } + if(!empty($bookImgIds)){ + $this->logger->Info("BOOK: Images Going to Inactived = ".implode(" ",$bookImgIds)); + $inactive_img_whereIn[0] = 'book_img_id'; + $inactive_img_whereIn[1] = $bookImgIds; + $BooksModel->inactiveMissingDetails('book_images',$data,$where, $inactive_img_whereIn); + } + } + $activeBookCategory = $BooksModel->getData('book_categories', $where); + if(!empty($activeBookCategory)){ + for ($z = 0; $z < count($activeBookCategory); $z++) { + $bookCategoryIds[$z] = $activeBookCategory[$z]->id; + } + if(!empty($bookCategoryIds)){ + $this->logger->Info("BOOK: Categories Going to Inactived = ".implode(" ",$bookCategoryIds)); + $inactive_category_whereIn[0] = 'id'; + $inactive_category_whereIn[1] = $bookCategoryIds; + $BooksModel->inactiveMissingDetails('book_categories',$data,$where, $inactive_category_whereIn); + } + } + + session()->setFlashdata('success', 'Deleted successfully.'); + $this->logger->info("BOOK: has been Inactived successfully. Inactived ID = " . $id); + } else { + $this->logger->error("BOOK: Not able to Inactive ID =" . $id); + throw new \Exception("Data Not able to Deleted"); + } + } + } catch (\Exception $e) { + $this->logger->error("Book: Err Occur = ".$e->getMessage()." File = ". $e->getFile() . " Line = " . $e->getLine()); + session()->setFlashdata('error', 'Message: ' . $e->getMessage()); } - - $data['isactive'] = 0; - $data['updated_by'] = $session_uid; - $BooksModel->update($id, $data); return redirect()->route('book_list'); } + } diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index db71d573..917b2eda 100644 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -22,6 +22,13 @@ class Home extends BaseController $model = new HomeModel(); $data['customer'] = $model->customers($where); $data['customer']['label'] = "Customer"; + $data['invoice'] = $model->invoice(); + $data['active_category'] = $model->schemes(); + $data['expiring_membership'] = $model->expiring_membership_list(); + // $data['active_category'] = array_filter($model->schemes(), function ($element) { + // return $element['count'] !== '0' ; + // }); + $data['book_published'] = $model->book_published_list(); $this->render_page('dashboard', $data); } diff --git a/app/Controllers/Invoice.php b/app/Controllers/Invoice.php index f1f53f51..fbadc66e 100644 --- a/app/Controllers/Invoice.php +++ b/app/Controllers/Invoice.php @@ -432,6 +432,13 @@ class Invoice extends BaseController $tax = ""; $total_amount = ""; $payment_method = ""; + $business_name= ""; + $business_address= ""; + $business_city= ""; + $business_state= ""; + $business_postal_code= ""; + $business_email= ""; + $business_mobile_no= ""; if (isset($details)) { $this->logger->info("Invoice: approve notification Request data type = ".gettype($details)); } @@ -449,6 +456,13 @@ class Invoice extends BaseController $tax = $rec['tax']; $total_amount = $rec['total_amount']; $payment_method = $rec['payment_method']; + $business_name= $rec['business_name']; + $business_address= $rec['business_address']; + $business_city= $rec['business_city']; + $business_state= $rec['business_state']; + $business_postal_code= $rec['business_postal_code']; + $business_email= $rec['business_email']; + $business_mobile_no= $rec['business_mobile_no']; } $records['invoice_order_number'] = $reference_number; $records['invoice_serial_number'] = $invoice_serial_number; @@ -479,12 +493,12 @@ class Invoice extends BaseController

Best regards,