$sale->discount = $_POST['discount'];
         $user = new user();
         $user->id = $_SESSION['user_id'];
         $user->getUser();
         $sale->company_id = $user->company_id;
         $sales_items = array();
         foreach ($_POST['items'] as $sales_array_item) {
             $sales_item = new sales_items();
             $sales_item->item_id = $sales_array_item['id'];
             $sales_item->quantity = $sales_array_item['quantity'];
             $sales_item->rate = $sales_array_item['rate'];
             $sales_item->tax = $sales_array_item['tax'];
             $sales_item->discount = $sales_array_item['discount'];
             array_push($sales_items, $sales_item);
         }
         $sale->setSalesItems($sales_items);
         $inserted_id = $sale->addSales();
         $message = "Sale completed successfuly";
         $responce = array('status' => 'success', 'error' => '', 'data' => array("message" => $message, "id" => $inserted_id));
     } else {
         ob_start();
         $a = ob_get_clean();
         $responce = array('status' => 'failed', 'error' => 'Data missing' . $a, 'data' => array());
     }
 } else {
     if ($form_id == 4) {
         //bank deposit
         if (isset($_POST['amount']) and !empty($_POST['amount']) and isset($_POST['bank_id']) and !empty($_POST['bank_id']) and isset($_POST['description']) and !empty($_POST['description'])) {
             $bank_deposit = new bank_deposits();
             $bank_deposit->description = $_POST['description'];
             $bank_deposit->amount = $_POST['amount'];
 $sale->tax_amount = $_POST['tax_amount'];
 $sale->discount = $_POST['discount'];
 $sales_items_prev = $sale->getSalesItems();
 $sales_items_new = array();
 if (!($_POST['items'] == 'no_items')) {
     foreach ($_POST['items'] as $sales_array_item) {
         $sales_item = new sales_items();
         $sales_item->item_id = $sales_array_item['id'];
         $sales_item->quantity = $sales_array_item['quantity'];
         $sales_item->rate = $sales_array_item['rate'];
         $sales_item->tax = $sales_array_item['tax'];
         $sales_item->discount = $sales_array_item['discount'];
         array_push($sales_items_new, $sales_item);
     }
 }
 $sale->setSalesItems($sales_items_new);
 $sale->updateSale();
 if (isset($sale->customer_id) and !empty($sale->customer_id)) {
     $customer = new customer();
     $customer->id = $sale->customer_id;
     $customer->getCustomer();
     $customer->total_purchace_amount = $customer->total_purchace_amount + $balance;
     $customer->updateCustomer();
 }
 ///updating stock
 //fixing multiple occurences of same item in new array
 for ($i = 0; $i < sizeof($sales_items_new); $i++) {
     $item_id = $sales_items_new[$i]->item_id;
     for ($j = $i + 1; $j < sizeof($sales_items_new); $j++) {
         if ($item_id == $sales_items_new[$j]->item_id) {
             $sales_items_new[$i]->quantity = $sales_items_new[$i]->quantity + $sales_items_new[$j]->quantity;