public function new_sales_report(array $sales_report_data) { $sales_report_id_from_branch = $sales_report_data['id']; $branch_id = $sales_report_data['branch_id']; $main_id = $sales_report_data['main_id']; $settings_repo = new Settings_Repository($this->db); $branches_repo = new Branches_Repository($this->db); if ($settings_repo->get_settings()->app_id != $main_id) { return -2; // invalid main } else { if (!$branches_repo->branch_exists($branch_id)) { return -1; // invalid branch } else { if (!$this->sales_report_exists_via_sales_report_id_from_branch($sales_report_id_from_branch, $branch_id)) { $this->db->insert('sales_reports', array('branch_id' => $branch_id, 'sales_report_id_from_branch' => $sales_report_id_from_branch, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'))); $sales_report_id = $this->db->insert_id(); $users_repo = new Users_Repository($this->db); foreach ($sales_report_data['sales'] as $sale) { $this->db->insert('receipts', array('receipt_id_from_branch' => $sale['receipt_id'], 'sales_report_id_from_branch' => $sales_report_id_from_branch, 'created_at_from_branch' => $sale['created_at'], 'updated_at_from_branch' => $sale['updated_at'], 'sales_report_id' => $sales_report_id, 'user_id' => $users_repo->get_user_via_username($sale['username'])->id, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s'))); $receipt_id = $this->db->insert_id(); foreach ($sale['items'] as $item) { $this->db->insert('receipt_items', array('item_id' => $item['item_id'], 'receipt_id' => $receipt_id, 'price' => $item['price'], 'quantity' => $item['quantity'])); } } return $sales_report_id; } else { return 0; // sales report exists } } } }
public function get_user_via_username_post() { $users_repo = new Users_Repository($this->base_model->get_db_instance()); $user = $users_repo->get_user_via_username($this->input->post('username')); $data = array(); array_push($data, array('id' => $user->id, 'username' => $user->username, 'user_level_id' => $user->user_level_id, 'user_level' => $user->user_level, 'last_name' => $user->last_name, 'first_name' => $user->first_name, 'middle_name' => $user->middle_name, 'created_at' => $user->created_at, 'updated_at' => $user->updated_at)); echo json_encode($data); }
public function login_post() { $username = $this->input->post('username'); $password = $this->input->post('password'); $user_repo = new Users_Repository($this->base_model->get_db_instance()); if ($user_repo->user_exists_via_username_and_password($username, $password)) { $user = $user_repo->get_user_via_username($username); $data = array(); array_push($data, array('id' => $user->id, 'username' => $user->username, 'password' => $user->password, 'user_level_id' => $user->user_level_id, 'user_level' => $user->user_level, 'last_name' => $user->last_name, 'first_name' => $user->first_name, 'middle_name' => $user->middle_name, 'created_at' => $user->created_at, 'updated_at' => $user->updated_at)); $settings_repo = new Settings_Repository($this->base_model->get_db_instance()); $this->session->set_userdata('auth', array('user_id' => $user->id, 'name' => $user->first_name . ' ' . $user->last_name, 'app_id' => $settings_repo->get_settings()->app_id)); echo json_encode($data); } else { echo 0; } }