public function addReveivPurchaseOrder($post)
 {
     $db_global = new Application_Model_DbTable_DbGlobal();
     $advance = new purchase_Model_DbTable_DbPurchaseAdvance();
     $session_user = new Zend_Session_Namespace('auth');
     $userName = $session_user->user_name;
     $GetUserId = $session_user->user_id;
     $db = $this->getAdapter();
     $data = array("purchase_order_id" => $post["purchase_id"], "vendor_id" => $post["v_id"], "location_id" => $post["location_id"], "pro_id" => $post["pro_id"], "qty_order" => $post["qty_order"], "qty_receive" => $post["qty_receive"], "qty_remain" => $post["qty_remain"], "receive_date" => $post["reveive_date"], "user_id" => $GetUserId, "mod_date" => new Zend_Date());
     $db->insert("tb_purchase_order_receive", $data);
     $rows = $db_global->inventoryLocation($post["location_id"], $post["pro_id"]);
     if ($rows) {
         $qty_on_order = array("QuantityOnHand" => $rows["QuantityOnHand"] + $post["qty_receive"], "QuantityAvailable" => $rows["QuantityAvailable"] + $post["qty_receive"], "QuantityOnOrder" => $rows["QuantityOnOrder"] - $post["qty_receive"], "Timestamp" => new zend_date());
         //update total stock
         $db_global->updateRecord($qty_on_order, $post["pro_id"], "ProdId", "tb_inventorytotal");
         unset($qty_on_order);
         $updatedata = array('qty' => $rows["qty"] + $post["qty_receive"], "last_usermod" => $GetUserId, "last_mod_date" => new Zend_Date());
         //update stock product location
         $db_global->updateRecord($updatedata, $rows["ProLocationID"], "ProLocationID", "tb_prolocation");
         unset($updatedata);
         $row_get = $advance->getItemPurchaseExist($post["purchase_id"], $post["pro_id"]);
         if ($row_get) {
             $data_update = array("qty_order" => $row_get["qty_order"] - $post["qty_receive"]);
             $db_global->updateRecord($data_update, $row_get["id"], "id", "tb_purchase_order_item_tmp");
         }
     } else {
         $insertdata = array('pro_id' => $post["pro_id"], 'LocationId' => $post["location_id"], 'qty' => $post["qty_receive"]);
         //update stock product location
         $db->insert("tb_prolocation", $insertdata);
         unset($insertdata);
         //update tmp purchase order
         $row_get = $advance->getItemPurchaseExist($post["purchase_id"], $post["pro_id"]);
         if ($row_get) {
             $data_update = array("qty_order" => $row_get["qty_order"] - $post["qty_receive"]);
             $db_global->updateRecord($data_update, $row_get["id"], "id", "tb_purchase_order_item_tmp");
         }
         $rows_stock = $db_global->InventoryExist($post["pro_id"]);
         if ($rows_stock) {
             $dataInventory = array('ProdId' => $post["pro_id"], 'QuantityOnHand' => $rows_stock["QuantityOnHand"] + $post["qty_receive"], 'QuantityOnOrder' => $rows_stock["QuantityOnOrder"] - $post["qty_receive"], 'QuantityAvailable' => $rows_stock["QuantityAvailable"] + $post["qty_receive"], 'Timestamp' => new Zend_date());
             $db_global->updateRecord($dataInventory, $rows_stock["ProdId"], "ProdId", "tb_inventorytotal");
             unset($dataInventory);
         } else {
             $addInventory = array('ProdId' => $post["pro_id"], 'QuantityOnHand' => $post["qty_receive"], 'QuantityAvailable' => $post["qty_receive"], 'Timestamp' => new Zend_date());
             $db->insert("tb_inventorytotal", $addInventory);
             unset($addInventory);
         }
     }
 }
 public function updateReturnItem($post)
 {
     $db = $this->getAdapter();
     $db_global = new Application_Model_DbTable_DbGlobal();
     $session_user = new Zend_Session_Namespace('auth');
     $userName = $session_user->user_name;
     $GetUserId = $session_user->user_id;
     $idrecord = $post['v_name'];
     $datainfo = array("contact_name" => $post['contact'], "phone" => $post['txt_phone'], "add_name" => $post["vendor_address"]);
     //updage vendor info
     $db_global->updateRecord($datainfo, $idrecord, "vendor_id", "tb_vendor");
     unset($datainfo);
     $return_id = $post["id"];
     $data_update = array("vendor_id" => $post["v_name"], "return_no" => $post['retun_order'], "date_return" => $post["return_date"], "payment_method" => $post["payment_name"], "currency_id" => $post["currency"], "remark" => $post["return_remark"], "user_mod" => $GetUserId, "timestamp" => new Zend_Date(), "paid" => $post["paid"], "all_total" => $post["all_total"], "balance" => $post["all_total"] - $post["paid"]);
     $db_global->addRecord($data_update, "tb_return");
     $db_global->updateRecord($data_update, $return_id, "return_id", "tb_return");
     unset($data_update);
     /////
     $sql_item = "SELECT iv.ProdId, iv.QuantityOnHand,iv.QuantityAvailable,rv.location_id,rv.qty_return FROM tb_return_vendor_item AS rv\n  \tINNER JOIN tb_inventorytotal AS iv ON iv.ProdId = rv.pro_id WHERE rv.return_id = {$return_id}";
     $rows_return = $db_global->getGlobalDb($sql_item);
     if ($rows_return) {
         foreach ($rows_return as $row_return) {
             $qty_on_order = array("QuantityOnHand" => $row_return["QuantityOnHand"] + $row_return["qty_return"], "QuantityAvailable" => $row_return["QuantityAvailable"] + $row_return["qty_return"], "Timestamp" => new zend_date());
             //update total stock
             $db_global->updateRecord($qty_on_order, $row_return["ProdId"], "ProdId", "tb_inventorytotal");
             unset($qty_on_order);
             $rowitem_exist = $db_global->porductLocationExist($row_return["ProdId"], $row_return["location_id"]);
             if ($rowitem_exist) {
                 $updatedata = array('qty' => $rowitem_exist["qty"] + $row_return["qty_return"], "last_usermod" => $GetUserId, "last_mod_date" => new Zend_Date());
                 //update stock product location
                 $db_global->updateRecord($updatedata, $rowitem_exist["ProLocationID"], "ProLocationID", "tb_prolocation");
                 unset($updatedata);
             }
         }
     }
     $sql = "DELETE FROM tb_return_vendor_item WHERE return_id IN ({$return_id})";
     $db_global->deleteRecords($sql);
     $ids = explode(',', $post['identity']);
     //add order in tb_inventory must update code again 9/8/13
     foreach ($ids as $i) {
         $add_data = array("return_id" => $return_id, "pro_id" => $post["item_id_" . $i], "location_id" => $post["LocationId_" . $i], "qty_return" => $post["qty_return_" . $i], "price" => $post["price_" . $i], "sub_total" => $post["sub_total_" . $i], "return_remark" => $post["remark_" . $i]);
         $db->insert("tb_return_vendor_item", $add_data);
         $rows = $db_global->inventoryLocation($post["LocationId_" . $i], $post["item_id_" . $i]);
         if ($rows) {
             $updatedata = array('qty' => $rows["qty"] - $post["qty_return_" . $i], "last_usermod" => $GetUserId, "last_mod_date" => new Zend_Date());
             //update stock product location
             $db_global->updateRecord($updatedata, $rows["ProLocationID"], "ProLocationID", "tb_prolocation");
             unset($updatedata);
             $qty_on_return = array("QuantityOnHand" => $rows["QuantityOnHand"] - $post["qty_return_" . $i], "QuantityAvailable" => $rows["QuantityAvailable"] - $post["qty_return_" . $i], "Timestamp" => new zend_date());
             //update total stock
             $db_global->updateRecord($qty_on_return, $post["item_id_" . $i], "ProdId", "tb_inventorytotal");
             unset($qty_on_return);
             //add return history
             $data_history = array('transaction_type' => 4, 'pro_id' => $post["item_id_" . $i], 'date' => new Zend_Date(), 'location_id' => $post["LocationId_" . $i], 'Remark' => $post['remark_' . $i], 'qty_edit' => $post["qty_return_" . $i], 'qty_before' => $rows["qty"], 'qty_after' => $rows["qty"] - $post["qty_return_" . $i], 'user_mod' => $GetUserId);
             $db->insert("tb_move_history", $data_history);
             unset($data_history);
         } else {
             $insertdata = array('pro_id' => $post["item_id_" . $i], 'LocationId' => $post["LocationId_" . $i], 'qty' => -$post["qty_return_" . $i]);
             //update stock product location
             $db->insert("tb_prolocation", $insertdata);
             unset($insertdata);
             //add return history
             $data_history = array('transaction_type' => 4, 'pro_id' => $post["item_id_" . $i], 'date' => new Zend_Date(), 'location_id' => $post["LocationId_" . $i], 'Remark' => $post['remark_' . $i], 'qty_edit' => $post["qty_return_" . $i], 'qty_before' => 0, 'qty_after' => -$post["qty_return_" . $i], 'user_mod' => $GetUserId);
             $db->insert("tb_move_history", $data_history);
             unset($data_history);
             $rows_stock = $db_global->InventoryExist($post["item_id_" . $i]);
             if ($rows_stock) {
                 $dataInventory = array('QuantityOnHand' => $rows_stock["QuantityOnHand"] - $post["qty_return_" . $i], 'QuantityAvailable' => $rows_stock["QuantityAvailable"] - $post["qty_return_" . $i], 'Timestamp' => new Zend_date());
                 $db_global->updateRecord($dataInventory, $rows_stock["ProdId"], "ProdId", "tb_inventorytotal");
                 unset($dataInventory);
             } else {
                 $addInventory = array('ProdId' => $post["item_id_" . $i], 'QuantityOnHand' => -$post["qty_return_" . $i], 'QuantityAvailable' => -$post["qty_return_" . $i], 'Timestamp' => new Zend_date());
                 $db->insert("tb_inventorytotal", $addInventory);
                 unset($addInventory);
             }
         }
     }
 }
 public function updateCustomerOrder($data)
 {
     try {
         $db_global = new Application_Model_DbTable_DbGlobal();
         $db = $this->getAdapter();
         $db->beginTransaction();
         $session_user = new Zend_Session_Namespace('auth');
         $userName = $session_user->user_name;
         $GetUserId = $session_user->user_id;
         //for update order by id\
         $id_order_update = $data['id'];
         $info_order = array("customer_id" => $data['customer_id'], "LocationId" => $data['LocationId'], "sales_ref" => $data['sales_ref'], "date_order" => $data['order_date'], "status" => $data['status'], "remark" => $data['remark'], "user_mod" => $GetUserId, "timestamp" => new Zend_Date(), "net_total" => $data['net_total'], "discount_value" => $data['dis_value'], "discount_real" => $data["global_disc"], "paid" => $data['paid'], "all_total" => $data['all_total'], "balance" => $data['remain']);
         //update info of order
         $db_global->updateRecord($info_order, $id_order_update, "order_id", "tb_sales_order");
         unset($info_order);
         // 			$sql_item="SELECT iv.ProdId, iv.QuantityOnHand, iv.QuantityAvailable, sum(so.qty_order) AS qtysold ,s.LocationId
         // 			FROM tb_sales_order AS s,tb_sales_order_item AS so
         // 			, tb_inventorytotal AS iv WHERE iv.ProdId = so.pro_id AND so.order_id=s.order_id AND so.order_id =$id_order_update GROUP BY so.pro_id";
         $sql = "SELECT \n\t\t\t\t\t\tp.`pro_id`,\n\t\t\t\t\t\tp.`qty_available`,\n\t\t\t\t\t\tp.`qty_onhand`,\n\t\t\t\t\t\tp.`qty_onsold`,\n\t\t\t\t\t\tSUM(soi.`qty_order`) AS qty_sold_order,\n\t\t\t\t\t\tso.`LocationId`\n\t\t\t\n\t\t\t\t\t\t\n\t\t\t\t\tFROM\n\t\t\t\t\t  tb_product AS p,\n\t\t\t\t\t  tb_sales_order AS so,\n\t\t\t\t\t  tb_sales_order_item AS soi \n\t\t\t\t\tWHERE \n\t\t\t\t\t\tp.`pro_id`=soi.`pro_id`\n\t\t\t\t\t\tAND soi.`order_id`=so.`order_id`\n\t\t\t\t\t\tAND soi.`order_id`= {$id_order_update}\n\t\t\t\t\tGROUP BY soi.`pro_id`";
         $rows_sold = $db_global->getGlobalDb($sql);
         if ($rows_sold) {
             foreach ($rows_sold as $row_sold) {
                 //just add to stock inventory tmp then withdrawal
                 if ($data["oldStatus"] == 5) {
                     $update_product = array("qty_onhand" => $row_sold["qty_onhand"] + $row_sold[" qty_sold_order"], "qty_available" => $row_sold["qty_available"] + $row_sold["qty_sold_order"], "last_mod_date" => new Zend_Date());
                     $db_global->updateRecord($update_product, $row_sold["pro_id"], "pro_id", "tb_product");
                 } else {
                     $update_pro = array("qty_onsold" => $row_sold["qty_onsold"] - $row_sold["qty_sold_order"], "qty_available" => $row_sold["qty_available"] + $row_sold["qty_sold_order"], "last_mod_date" => new Zend_Date());
                     $db_global->updateRecord($update_pro, $row_sold["pro_id"], "pro_id", "tb_inventorytotal");
                 }
                 //update total stock
                 //$row_get = $db_global->porductLocationExist($row_sold["ProdId"],$row_sold["LocationId"]);//old
                 $row_get = $db_global->porductLocationExist($row_sold["ProdId"], $data["old_location"]);
                 if ($row_get) {
                     if ($data["oldStatus"] == 5) {
                         $update_prolo = array("qty" => $row_get["qty"] + $row_sold["qty_sold_order"], "last_usermod" => $GetUserId, "last_mod_date" => new Zend_Date());
                     } else {
                         $qty_on_location = array("qty" => $row_get["qty"] + $row_sold["qty_sold_order"], "qty_onsold" => $row_get["qty"], "last_usermod" => $GetUserId, "last_mod_date" => new Zend_Date());
                     }
                     $qty_on_location = array("qty" => $row_get["qty"] + $row_sold["qty_sold_order"], "last_usermod" => $GetUserId, "last_mod_date" => new Zend_Date());
                     //update total stock
                     $db_global->updateRecord($qty_on_location, $row_get["ProLocationID"], "ProLocationID", "tb_prolocation");
                 }
             }
         }
         unset($qty_on_order);
         //delete old sale order
         unset($rows_sold);
         $sql = "DELETE FROM tb_sales_order_item WHERE order_id IN ({$id_order_update})";
         $db_global->deleteRecords($sql);
         $ids = explode(',', $data['identity']);
         foreach ($ids as $i) {
             $data_item[$i] = array('order_id' => $id_order_update, 'pro_id' => $data['item_id_' . $i], 'qty_order' => $data['qty' . $i], 'price' => $data['price' . $i], 'total_befor' => $data['total' . $i], 'sub_total' => $data['total' . $i]);
             $db->insert("tb_sales_order_item", $data_item[$i]);
             unset($data_item[$i]);
             $locationid = $data['LocationId'];
             $itemId = $data['item_id_' . $i];
             $qtyrecord = $data['qty' . $i];
             //qty on 1 record
             $rows = $db_global->productLocationInventory($itemId, $locationid);
             //to check product location
             if ($rows) {
                 $qtyold = $rows['qty'];
                 $getrecord_id = $rows["ProLocationID"];
                 $itemOnHand = array('QuantityOnHand' => $rows["QuantityOnHand"] - $qtyrecord, 'QuantityAvailable' => $rows["QuantityAvailable"] - $qtyrecord);
                 //update total stock
                 $db_global->updateRecord($itemOnHand, $itemId, "ProdId", "tb_inventorytotal");
                 unset($itemOnHand);
                 //update stock dork
                 //$newqty       = $rows['qty']-$qtyrecord;
                 $updatedata = array('qty' => $rows['qty'] - $qtyrecord);
                 //update stock product location
                 $db_global->updateRecord($updatedata, $getrecord_id, "ProLocationID", "tb_prolocation");
                 unset($updatedata);
                 //update stock record
             } else {
                 //insert stock ;
                 $rows_pro_exit = $db_global->productLocation($itemId, $locationid);
                 if ($rows_pro_exit) {
                     $updatedata = array('qty' => $rows_pro_exit['qty'] + $qtyrecord);
                     //update stock product location
                     $itemid = $db_global->updateRecord($updatedata, $rows_pro_exit['ProLocationID'], "ProLocationID", "tb_prolocation");
                     unset($updatedata);
                 } else {
                     $insertdata = array('pro_id' => $itemId, 'LocationId' => $locationid, 'last_usermod' => $GetUserId, 'qty' => -$qtyrecord, 'last_mod_date' => new Zend_Date());
                     //update stock product location
                     $db->insert("tb_prolocation", $insertdata);
                     unset($insertdata);
                 }
                 $rowitem = $db_global->InventoryExist($itemId);
                 //to check product location
                 if ($rowitem) {
                     $itemOnHand = array('QuantityOnHand' => $rowitem["QuantityOnHand"] - $qtyrecord, 'QuantityAvailable' => $rowitem["QuantityAvailable"] - $qtyrecord);
                     //update total stock
                     $itemid = $db_global->updateRecord($itemOnHand, $itemId, "ProdId", "tb_inventorytotal");
                     unset($itemOnHand);
                 } else {
                     $dataInventory = array('ProdId' => $itemId, 'QuantityOnHand' => -$data['qty' . $i], 'QuantityAvailable' => -$data['qty' . $i], 'Timestamp' => new Zend_date());
                     $db->insert("tb_inventorytotal", $dataInventory);
                     unset($dataInventory);
                     //update stock product location
                 }
             }
         }
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
     }
 }
 public function updateCustomerQuote($data)
 {
     $db = $this->getAdapter();
     $db_global = new Application_Model_DbTable_DbGlobal();
     $session_user = new Zend_Session_Namespace('auth');
     $userName = $session_user->user_name;
     $GetUserId = $session_user->user_id;
     $idrecord = $data['customer_id'];
     $datainfo = array("contact_name" => $data['contact'], "phone" => $data['txt_phone'], "add_name" => $data['remark_add']);
     //updage customer info
     $itemid = $db_global->updateRecord($datainfo, $idrecord, "customer_id", "tb_customer");
     $id = $data["id"];
     $sql_del = "DELETE FROM tb_sales_order_item WHERE order_id IN ({$id})";
     $db_global->deleteRecords($sql_del);
     $sql_sale = "DELETE FROM tb_sales_order WHERE order_id IN ({$id})";
     $db_global->deleteRecords($sql_sale);
     $new_order = str_replace("SQ", "SO", $data["order"]);
     $info_order = array("order_id" => $id, "customer_id" => $data['customer_id'], "LocationId" => $data['LocationId'], "order" => $new_order, "sales_ref" => $data['sales_ref'], "date_order" => $data['order_date'], "status" => 2, "payment_method" => $data['payment_name'], "currency_id" => $data['currency'], "remark" => $data['remark'], "user_mod" => $GetUserId, "timestamp" => new Zend_Date(), "version" => 1, "net_total" => $data['net_total'], "discount_type" => $data['discount_type'], "discount_value" => $data['discount_value'], "discount_real" => $data["discount_real"], "paid" => $data['paid'], "all_total" => $data['all_total'], "balance" => $data['all_total'] - $data['paid']);
     //and info of order
     $id_order = $db_global->addRecord($info_order, "tb_sales_order");
     unset($info_order);
     $ids = explode(',', $data['identity']);
     $qtyonhand = 0;
     foreach ($ids as $i) {
         //add history order
         $data_history[$i] = array('pro_id' => $data['item_id_' . $i], 'type' => 2, 'order' => $id_order, 'customer_id' => $data['customer_id'], 'date' => new Zend_Date(), 'status' => 2, 'order_total' => $data['all_total'], 'qty' => $data['qty' . $i], 'unit_price' => $data['price' . $i], 'sub_total' => $data['after_discount' . $i]);
         $order_history = $db->insert("tb_order_history", $data_history[$i]);
         unset($data_history[$i]);
         $data_item[$i] = array('order_id' => $id_order, 'pro_id' => $data['item_id_' . $i], 'qty_order' => $data['qty' . $i], 'price' => $data['price' . $i], 'total_befor' => $data['total' . $i], 'disc_type' => $data['dis-type-' . $i], 'disc_value' => $data['dis-value' . $i], 'sub_total' => $data['after_discount' . $i]);
         $db->insert("tb_sales_order_item", $data_item[$i]);
         unset($data_item[$i]);
         //update stock total inventory (QTY Reserved)
         $locationid = $data['LocationId'];
         $itemId = $data['item_id_' . $i];
         $qtyrecord = $data['qty' . $i];
         //qty on 1 record
         $rows = $db_global->InventoryExist($itemId);
         if ($rows) {
             $qty_on_reserved = array('QuantitySold' => $rows["QuantitySold"] + $qtyrecord, 'QuantityAvailable' => $rows["QuantityAvailable"] - $qtyrecord);
             //update total stock
             $db_global->updateRecord($qty_on_reserved, $itemId, "ProdId", "tb_inventorytotal");
         } else {
             $addInventory = array('ProdId' => $itemId, 'QuantityOnOrder' => $data['qty' . $i], 'Timestamp' => new Zend_date());
             $db_global->addRecord($addInventory, "tb_inventorytotal");
             unset($addInventory);
         }
     }
 }
 public function updateReturnItemIn($post)
 {
     try {
         $db = $this->getAdapter();
         $db->beginTransaction();
         $db_global = new Application_Model_DbTable_DbGlobal();
         $session_user = new Zend_Session_Namespace('auth');
         $userName = $session_user->user_name;
         $GetUserId = $session_user->user_id;
         $idrecord = $post['v_name'];
         // 			$datainfo=array(
         // 					"contact_name" => $post['contact'],
         // 					"phone"        => $post['txt_phone'],
         // 					"add_name"     => $post["vendor_address"]
         // 			);
         // 			//updage vendor info
         // 			$db_global->updateRecord($datainfo,$idrecord,"vendor_id","tb_vendor");
         // 			unset($datainfo);
         $return_id = $post["id"];
         $old_location = $post["old_location"];
         $sql_item = "SELECT\n\t   \t(SELECT p.pro_id FROM tb_product AS p WHERE p.pro_id = rvi.`pro_id` LIMIT 1) AS pro_id\n\t   \t\t\n\t   \t,(SELECT p.qty_onorder FROM tb_product AS p WHERE p.pro_id = rvi.`pro_id` LIMIT 1) AS qty_onorder\n\t   \n\t   \t,(SELECT p.qty_onhand \tFROM tb_product AS p WHERE p.pro_id = rvi.`pro_id` LIMIT 1) AS qty_onhand\n\t   \n\t   \t,(SELECT p.qty_available \tFROM tb_product AS p WHERE p.pro_id = rvi.`pro_id` LIMIT 1) AS qty_available\n\t   \t\t\n\t   \t, SUM(rvi.`qty_return`) AS qty_return FROM\n   \n   \ttb_return_vendor_item_in AS rvi WHERE rvi.return_id = {$return_id} GROUP BY rvi.pro_id";
         $rows_return = $db_global->getGlobalDb($sql_item);
         //	print_r($rows_return);
         if ($rows_return) {
             foreach ($rows_return as $row_return) {
                 $qty_stock = $row_return["qty_onhand"] - $row_return["qty_return"];
                 $rows = $db_global->inventoryLocation($old_location, $row_return["pro_id"]);
                 $qty_location = $rows["qty"] - $row_return["qty_return"];
                 //print_r($rows);exit();
                 if ($qty_stock < 0) {
                     Application_Form_FrmMessage::message("Your product stock is less than return");
                     Application_Form_FrmMessage::redirectUrl("/purchase/return/return-in");
                     //exit();
                 } elseif ($qty_location < 0) {
                     Application_Form_FrmMessage::message("You Items is less than item return");
                     Application_Form_FrmMessage::redirectUrl("/purchase/return/return-in");
                     //exit();
                 } else {
                     $qty_on_order = array("qty_onhand" => $row_return["qty_onhand"] - $row_return["qty_return"], "qty_available" => $row_return["qty_available"] - $row_return["qty_return"], "last_mod_date" => new Zend_date());
                     //update total stock
                     $db_global->updateRecord($qty_on_order, $row_return["pro_id"], "pro_id", "tb_product");
                     unset($qty_on_order);
                     $rowitem_exist = $db_global->porductLocationExist($row_return["pro_id"], $old_location);
                     if ($rowitem_exist) {
                         $updatedata = array('qty' => $rowitem_exist["qty"] - $row_return["qty_return"], "last_usermod" => $GetUserId, "last_mod_date" => new Zend_Date());
                         //update stock product location
                         $db_global->updateRecord($updatedata, $rowitem_exist["ProLocationID"], "ProLocationID", "tb_prolocation");
                         unset($updatedata);
                     }
                 }
             }
         }
         $data_update = array("vendor_id" => $post["v_name"], "date_return_in" => $post["return_date"], "remark" => $post["return_remark"], "user_mod" => $GetUserId, "timestamp" => new Zend_Date(), "location_id" => $post["LocationId"], "all_total" => $post["all_total"]);
         $db_global->updateRecord($data_update, $return_id, "returnin_id", "tb_return_vendor_in");
         unset($data_update);
         $sql = "DELETE FROM tb_return_vendor_item_in WHERE return_id IN ({$return_id})";
         $db_global->deleteRecords($sql);
         $delete_history = "DELETE FROM tb_return_history WHERE return_id IN({$return_id})";
         $db_global->deleteRecords($delete_history);
         $ids = explode(',', $post['identity']);
         //add order in tb_inventory must update code again 9/8/13
         foreach ($ids as $i) {
             $add_data = array("return_id" => $return_id, "pro_id" => $post["item_id_" . $i], "qty_return" => $post["qty_return_" . $i], "price" => $post["price_" . $i], "sub_total" => $post["sub_total_" . $i], "return_remark" => $post["remark_" . $i]);
             $db->insert("tb_return_vendor_item", $add_data);
             $add_data = array("return_id" => $return_id, "pro_id" => $post["item_id_" . $i], "qty_return" => $post["qty_return_" . $i], "price" => $post["price_" . $i], "sub_total" => $post["sub_total_" . $i], "return_remark" => $post["remark_" . $i]);
             $db->insert("tb_return_vendor_item_in", $add_data);
             $rows = $db_global->inventoryLocation($post["LocationId"], $post["item_id_" . $i]);
             if ($rows) {
                 $qty_on_return = array("qty_onhand" => $rows["qty_onhand"] + $post["qty_return_" . $i], "qty_available" => $rows["qty_available"] + $post["qty_return_" . $i], "last_mod_date" => new Zend_date());
                 //update total stock
                 $db_global->updateRecord($qty_on_return, $post["item_id_" . $i], "pro_id", "tb_product");
                 unset($qty_on_return);
                 $updatedata = array('qty' => $rows["qty"] + $post["qty_return_" . $i], "last_usermod" => $GetUserId, "last_mod_date" => new Zend_Date());
                 //update stock product location
                 $db_global->updateRecord($updatedata, $rows["ProLocationID"], "ProLocationID", "tb_prolocation");
                 unset($updatedata);
                 unset($qty_on_return);
                 //add return history
                 $data_history = array('transaction_type' => 4, 'pro_id' => $post["item_id_" . $i], 'date' => new Zend_Date(), 'location_id' => $post["LocationId_" . $i], 'Remark' => $post['remark_' . $i], 'qty_edit' => $post["qty_return_" . $i], 'qty_before' => $rows["qty"], 'qty_after' => $rows["qty"] - $post["qty_return_" . $i], 'user_mod' => $GetUserId);
                 $db->insert("tb_move_history", $data_history);
                 unset($data_history);
                 $history = array("return_id" => $return_id, "return_no" => $post["retun_order"], "pro_id" => $post["item_id_" . $i], "location_id" => $post["LocationId"], "return_type" => 2, "vendor_id" => 1, "return_date" => $post["return_date"], "qty_return" => $post["qty_return_" . $i], "price" => $post["price_" . $i], "total_amount" => $post["sub_total_" . $i], "user_mod" => $GetUserId, "remark" => $post["return_remark"]);
                 $db->insert("tb_return_history", $history);
                 unset($history);
             } else {
                 //    			Application_Form_FrmMessage::message("Your product in stock is not exist");
                 //    			Application_Form_FrmMessage::redirectUrl("/purchase/return");
                 //    			exit();
                 $row_location = $db_global->productLocation($post["LocationId"], $post["item_id_" . $i]);
                 if ($row_location) {
                     $updatedata = array('qty' => $rows["qty"] + $post["qty_return_" . $i], "last_usermod" => $GetUserId, "last_mod_date" => new Zend_Date());
                     //update stock product location
                     $db_global->updateRecord($updatedata, $row_location["ProLocationID"], "ProLocationID", "tb_prolocation");
                     unset($updatedata);
                 } else {
                     $insertdata = array('pro_id' => $post["item_id_" . $i], 'LocationId' => $post["LocationId_" . $i], 'qty' => -$post["qty_return_" . $i]);
                 }
                 //update stock product location
                 $db->insert("tb_prolocation", $insertdata);
                 unset($insertdata);
                 //add return history
                 $data_history = array('transaction_type' => 4, 'pro_id' => $post["item_id_" . $i], 'date' => new Zend_Date(), 'location_id' => $post["LocationId_" . $i], 'Remark' => $post['remark_' . $i], 'qty_edit' => $post["qty_return_" . $i], 'qty_before' => 0, 'qty_after' => -$post["qty_return_" . $i], 'user_mod' => $GetUserId);
                 $db->insert("tb_move_history", $data_history);
                 unset($data_history);
                 $history = array("return_id" => $return_id, "return_no" => $post["retun_order"], "pro_id" => $post["item_id_" . $i], "location_id" => $post["LocationId"], "return_type" => 2, "vendor_id" => 1, "return_date" => $post["return_date"], "qty_return" => $post["qty_return_" . $i], "price" => $post["price_" . $i], "total_amount" => $post["sub_total_" . $i], "user_mod" => $GetUserId, "remark" => $post["return_remark"]);
                 $db->insert("tb_return_history", $history);
                 unset($history);
                 $rows_stock = $db_global->InventoryExist($post["item_id_" . $i]);
                 if ($rows_stock) {
                     $dataInventory = array('qty_onhand' => $rows_stock["qty_onhand"] - $post["qty_return_" . $i], 'qty_available' => $rows_stock["qty_available"] - $post["qty_return_" . $i], 'last_mod_date' => new Zend_date());
                     $db_global->updateRecord($dataInventory, $rows_stock["pro_id"], "pro_id", "tb_product");
                     unset($dataInventory);
                 } else {
                     $addInventory = array('pro_id' => $post["item_id_" . $i], 'qty_onhand' => -$post["qty_return_" . $i], 'qty_available' => -$post["qty_return_" . $i], 'last_mod_date' => new Zend_date());
                     $db->insert("tb_product", $addInventory);
                     unset($addInventory);
                 }
             }
         }
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
         echo $e->getMessage();
     }
 }
 public function updateVendorCancellOrder($data)
 {
     try {
         $db = $this->getAdapter();
         $db->beginTransaction();
         $db_global = new Application_Model_DbTable_DbGlobal();
         $session_user = new Zend_Session_Namespace('auth');
         $userName = $session_user->user_name;
         $GetUserId = $session_user->user_id;
         //for update order by id\
         $id_order_update = $data['id'];
         //update info of order in tb_purchase order
         // 			if($data["status"]==6){
         // 			}
         $info_purchase_order = array("vendor_id" => $data['v_name'], "LocationId" => $data["LocationId"], "order" => $data['txt_order'], "date_order" => $data['order_date'], "status" => $data["status"], "remark" => $data['remark'], "user_mod" => $GetUserId, "timestamp" => new Zend_Date(), "paid" => $data['paid'], "all_total" => $data['totalAmoun'], "balance" => $data['remain']);
         $db_global->updateRecord($info_purchase_order, $id_order_update, "order_id", "tb_purchase_order");
         unset($info_purchase_order);
         // Insert recieved order
         $recieved_id = $data["recieve_id"];
         $ids = explode(',', $data['identity']);
         $sql_recieve = new purchase_Model_DbTable_DbPurchaseOrder();
         $result = $sql_recieve->recieved_info($id_order_update);
         $prifix = "SELECT * FROM tb_setting WHERE `code` =16";
         $ro = $db_global->getGlobalDbRow($prifix);
         $RO = $ro["key_value"];
         $date = new Zend_Date();
         $recieve_no = $RO . $date->get('hh-mm-ss');
         if ($result) {
             //if($data["oldStatus"]==5 or $data["oldStatus"]==4){
             if ($data["status"] == 5 or $data["status"] == 4) {
                 $data_recieved_order = array("recieve_type" => 1, "order_id" => $id_order_update, "order_no" => $data["txt_order"], "vendor_id" => $data['v_name'], "location_id" => $data["LocationId"], "date_recieve" => new Zend_Date(), "status" => $data['status'], "is_active" => 1, "paid" => $data['paid'], "all_total" => $data['totalAmoun'], "balance" => $data['remain'], "user_recieve" => $GetUserId);
                 $recieved_order = $db_global->updateRecord($data_recieved_order, $recieved_id, "recieve_id", "tb_recieve_order");
                 unset($data_recieved_order);
             } else {
                 $data_recieved_order = array("recieve_type" => 1, "order_id" => $id_order_update, "order_no" => $data["txt_order"], "vendor_id" => $data['v_name'], "location_id" => $data["LocationId"], "date_recieve" => new Zend_Date(), "status" => $data['status'], "is_active" => 0, "paid" => $data['paid'], "all_total" => $data['totalAmoun'], "balance" => $data['remain'], "user_recieve" => $GetUserId);
                 $recieved_order = $db_global->updateRecord($data_recieved_order, $recieved_id, "recieve_id", "tb_recieve_order");
                 unset($data_recieved_order);
             }
             //}
             $sqls = "DELETE FROM tb_recieve_order_item WHERE recieve_id IN ({$recieved_id})";
             $db_global->deleteRecords($sqls);
             unset($sqls);
             foreach ($ids as $i) {
                 $recieved_item[$i] = array("recieve_id" => $recieved_id, "pro_id" => $data['item_id_' . $i], "order_id" => $id_order_update, "qty_order" => $data['qty' . $i], "qty_recieve" => $data['qty' . $i], "price" => $data['price' . $i], "total_before" => $data['total' . $i], "sub_total" => $data['total' . $i]);
                 $db->insert("tb_recieve_order_item", $recieved_item[$i]);
                 unset($recieved_item[$i]);
             }
         } else {
             $sql = "SELECT * FROM tb_setting WHERE `code`=16";
             $ro = $db_global->getGlobalDbRow($sql);
             $RO = $ro["key_value"];
             $date = new Zend_Date();
             $recieve_no = $RO . $date->get('hh-mm-ss');
             if ($data["status"] == 5 or $data["status"] == 4) {
                 $data_recieved_order = array("recieve_type" => 1, "order_id" => $id_order_update, "recieve_no" => $recieve_no, "order_no" => $data["txt_order"], "vendor_id" => $data['v_name'], "location_id" => $data["LocationId"], "order_date" => new Zend_Date(), "date_recieve" => new Zend_Date(), "status" => $data['status'], "is_active" => 1, "paid" => $data['paid'], "all_total" => $data['totalAmoun'], "balance" => $data['remain'], "user_recieve" => $GetUserId);
                 $recieved_order = $db_global->addRecord($data_recieved_order, "tb_recieve_order");
                 unset($data_recieved_order);
                 foreach ($ids as $i) {
                     $recieved_item[$i] = array("recieve_id" => $recieved_order, "pro_id" => $data['item_id_' . $i], "order_id" => $id_order_update, "qty_order" => $data['qty' . $i], "qty_recieve" => $data['qty' . $i], "price" => $data['price' . $i], "total_before" => $data['total' . $i], "sub_total" => $data['total' . $i]);
                     $db->insert("tb_recieve_order_item", $recieved_item[$i]);
                     unset($recieved_item[$i]);
                 }
             }
         }
         // end update info of order in tb_purchase order
         // Delete old purchase order item before insert new purchase order item in old order_id
         $sql = "DELETE FROM tb_purchase_order_item WHERE order_id IN ({$id_order_update})";
         $db_global->deleteRecords($sql);
         unset($sql);
         $sql_history = "DELETE FROM tb_purchase_order_history WHERE `order` IN ({$id_order_update})";
         $db_global->deleteRecords($sql_history);
         unset($sql_history);
         /// update
         $ids = explode(',', $data['identity']);
         //add order in tb_inventory must update code again 9/8/13
         //print_r($ids);exit();
         foreach ($ids as $i) {
             // Insert recieved order item
             $data_item[$i] = array('order_id' => $id_order_update, 'pro_id' => $data['item_id_' . $i], 'qty_order' => $data['qty' . $i], 'price' => $data['price' . $i], 'sub_total' => $data['total' . $i], 'total_befor' => $data['total' . $i], 'remark' => $data['remark_' . $i]);
             //print_r($data_item); echo "<br />";echo "<br />";
             $db->insert("tb_purchase_order_item", $data_item[$i]);
             unset($data_item[$i]);
             $data_history[$i] = array('order' => $id_order_update, 'pro_id' => $data['item_id_' . $i], 'type' => 1, 'customer_id' => $data['v_name'], 'status' => $data["status"], 'order_total' => $data['total' . $i], 'qty' => $data['qty' . $i], 'unit_price' => $data['price' . $i], 'sub_total' => $data['total' . $i], 'date' => $data["old_history_date"], 'last_update_date' => new Zend_Date());
             //print_r($data_history);exit();
             $db->insert("tb_purchase_order_history", $data_history[$i]);
             unset($data_history[$i]);
             $locationid = $data['LocationId'];
             $itemId = $data['item_id_' . $i];
             $qtyrecord = $data['qty' . $i];
             //qty on 1 record
             // Update stock in tb_product
             $rows = $db_global->productLocationInventory($itemId, $locationid);
             //to check product location
             //print_r($rows); exit();
             if ($rows) {
                 $getrecord_id = $rows["ProLocationID"];
                 if ($data["status"] == 5 or $data["status"] == 4) {
                     $itemOnHand = array('qty_onhand' => $rows["qty_onhand"] + $qtyrecord, 'qty_available' => $rows["qty_available"] + $qtyrecord, 'last_mod_date' => new Zend_date());
                     $db_global->updateRecord($itemOnHand, $itemId, "pro_id", "tb_product");
                     unset($itemOnHand);
                 } else {
                     $itemOnHand = array('qty_onorder' => $rows["pqty_onorder"] + $qtyrecord, 'last_mod_date' => new Zend_date());
                     $db_global->updateRecord($itemOnHand, $itemId, "pro_id", "tb_product");
                     unset($itemOnHand);
                 }
                 //End update total stock
                 // Update product Location
                 if ($data["status"] == 5 or $data["status"] == 4) {
                     $updatedata = array('qty' => $rows['qty'] + $qtyrecord, 'last_mod_date' => new Zend_date());
                     $db_global->updateRecord($updatedata, $getrecord_id, "ProLocationID", "tb_prolocation");
                     unset($updatedata);
                 } else {
                     $updatedata = array('qty_onorder' => $rows['qty_onorder'] + $qtyrecord, 'last_mod_date' => new Zend_date());
                     $db_global->updateRecord($updatedata, $getrecord_id, "ProLocationID", "tb_prolocation");
                     unset($updatedata);
                 }
                 //End update stock product location						//update stock record
             } else {
                 //insert stock ;
                 $rows_pro_exit = $db_global->productLocation($itemId, $locationid);
                 // check product location exist
                 // if product exist Update qty in tb_prolocation
                 if ($rows_pro_exit) {
                     if ($data["status"] == 5 or $data["status"] == 4) {
                         $updatedata = array('qty' => $rows['qty'] + $qtyrecord, 'last_mod_date' => new Zend_date());
                         $itemid = $db_global->updateRecord($updatedata, $rows_pro_exit['ProLocationID'], "ProLocationID", "tb_prolocation");
                         unset($updatedata);
                     } else {
                         $updatedata = array('qty_onorder' => $rows['qty_onorder'] + $qtyrecord, 'last_mod_date' => new Zend_date());
                         $itemid = $db_global->updateRecord($updatedata, $rows_pro_exit['ProLocationID'], "ProLocationID", "tb_prolocation");
                         unset($updatedata);
                     }
                     // End if product exist Update qty in tb_prolocation
                 } else {
                     // If product not exist insert New product in tb_prolocation
                     $insertdata = array('pro_id' => $itemId, 'LocationId' => $locationid, 'last_usermod' => $GetUserId, 'qty' => $qtyrecord, 'last_mod_date' => new Zend_Date());
                     //update stock product location
                     $db->insert("tb_prolocation", $insertdata);
                     unset($insertdata);
                 }
                 // End If product not exist insert New product in tb_prolocation
                 $rowitem = $db_global->InventoryExist($itemId);
                 //to check product exist
                 // If productt exist update product in tb_product
                 if ($rowitem) {
                     if ($data["status"] == 5 or $data["status"] == 4) {
                         $itemOnHand = array('qty_onhand' => $rows["qty_onhand"] + $qtyrecord, 'qty_available' => $rows["qty_available"] + $qtyrecord, 'last_mod_date' => new Zend_date());
                         $itemid = $db_global->updateRecord($itemOnHand, $itemId, "pro_id", "tb_product");
                         unset($itemOnHand);
                     } else {
                         $itemOnHand = array('qty_onorder' => $rows["qty_onrder"] + $qtyrecord, 'last_mod_date' => new Zend_date());
                         $itemid = $db_global->updateRecord($itemOnHand, $itemId, "pro_id", "tb_product");
                         unset($itemOnHand);
                     }
                     //update total stock
                     // 						$itemid=$db_global->updateRecord($itemOnHand,$itemId,"pro_id","tb_product");
                     // 						unset($itemOnHand);
                     // If productt exist update product in tb_product
                 } else {
                     // If product not exist insert new product in tb_product
                     $dataInventory = array('pro_id' => $itemId, 'qty_onhand' => $qtyrecord, 'qty_available' => $qtyrecord, 'last_mod_date' => new Zend_date());
                     $db->insert("tb_product", $dataInventory);
                     unset($dataInventory);
                     //update stock product location
                 }
             }
         }
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
         $e->getMessage();
         //echo $theCauseOfErrorOnlyDoNotRedirectToError;
         //exit();
     }
 }
 public function updateVendorOrderPayment($data)
 {
     $db_global = new Application_Model_DbTable_DbGlobal();
     $db = $this->getAdapter();
     $session_user = new Zend_Session_Namespace('auth');
     $userName = $session_user->user_name;
     $GetUserId = $session_user->user_id;
     // 		$idrecord=$data['v_name'];
     // 		$datainfo=array(
     // 				"contact_name"=>$data['contact'],
     // 				"phone"       =>$data['txt_phone'],
     // 				//	"add_remark"  =>$data['remark_add']
     // 		);
     // 		//updage customer info
     // 		$itemid=$db_global->updateRecord($datainfo,$idrecord,"vendor_id","tb_vendor");
     $id_order_update = $data['id'];
     $info_order = array("vendor_id" => $data['v_name'], "LocationId" => $data["LocationId"], "order" => $data['txt_order'], "date_order" => $data['order_date'], "status" => 4, "remark" => $data['remark'], "user_mod" => $GetUserId, "timestamp" => new Zend_Date(), "version" => 1, "net_total" => $data['net_total'], "discount_type" => $data['discount_type'], "discount_value" => $data['discount_value'], "discount_real" => $data['discount_real'], "paid" => $data['all_total'], "all_total" => $data['all_total'], "balance" => 0);
     //update info of order not done
     $db_global->updateRecord($info_order, $id_order_update, "order_id", "tb_purchase_order");
     $rows_exist = $db_global->purchaseOrderHistoryExitAll($id_order_update);
     if ($rows_exist) {
         foreach ($rows_exist as $id_history) {
             $data_status = array('status' => 4);
             $db_global->updateRecord($data_status, $id_history['history_id'], "history_id", "tb_purchase_order_history");
             unset($data_status);
         }
     }
     unset($info_order);
     //if error check this here
     //and info of order
     $sql_item = "SELECT iv.ProdId, iv.QuantityOnOrder,sum(po.qty_order) AS qtyorder\n\t\tFROM tb_purchase_order_item AS po\n\t\tINNER JOIN tb_inventorytotal AS iv ON iv.ProdId = po.pro_id WHERE po.order_id = {$id_order_update} GROUP BY po.pro_id";
     $rows_order = $db_global->getGlobalDb($sql_item);
     if ($rows_order) {
         foreach ($rows_order as $row_order) {
             $qty_on_order = array("QuantityOnOrder" => $row_order["QuantityOnOrder"] - $row_order["qtyorder"]);
             //update total stock
             $db_global->updateRecord($qty_on_order, $row_order["ProdId"], "ProdId", "tb_inventorytotal");
         }
     }
     unset($rows_order);
     unset($rows_order);
     $sql = "DELETE FROM tb_purchase_order_item WHERE order_id IN ({$id_order_update})";
     $db_global->deleteRecords($sql);
     //$db->DeleteData("tb_purchase_order_item"," WHERE order_id = ".$id_order_update);
     $ids = explode(',', $data['identity']);
     $qtyonhand = 0;
     foreach ($ids as $i) {
         $data_item[$i] = array('order_id' => $id_order_update, 'pro_id' => $data['item_id_' . $i], 'qty_order' => $data['qty' . $i], 'price' => $data['price' . $i], 'total_befor' => $data['total' . $i], 'disc_type' => $data['dis-type-' . $i], 'disc_value' => $data['dis-value' . $i], 'sub_total' => $data['after_discount' . $i]);
         $db->insert("tb_purchase_order_item", $data_item[$i]);
         unset($data_item[$i]);
         //UPDATE STOCK
         //check stock product location
         $locationid = $data['LocationId'];
         $itemId = $data['item_id_' . $i];
         $qtyrecord = $data['qty' . $i];
         //qty on 1 record
         $rows = $db_global->inventoryLocation($locationid, $itemId);
         if ($rows) {
             $qty_on_order = array("QuantityAvailable" => $rows["QuantityAvailable"] + $data['qty' . $i], "QuantityOnHand" => $rows["QuantityOnHand"] + $data['qty' . $i]);
             //update total stock
             $db_global->updateRecord($qty_on_order, $itemId, "ProdId", "tb_inventorytotal");
             unset($qty_on_order);
             //update stock dork
             $newqty = $rows["qty"] + $qtyrecord;
             $updatedata = array('qty' => $newqty);
             //update stock product location
             $db_global->updateRecord($updatedata, $rows["ProLocationID"], "ProLocationID", "tb_prolocation");
             unset($updatedata);
             //update stock record
         } else {
             $insertdata = array('pro_id' => $itemId, 'LocationId' => $locationid, 'qty' => $qtyrecord);
             //update stock product location
             $db->insert("tb_prolocation", $insertdata);
             $rows_stock = $db_global->InventoryExist($itemId);
             if ($rows_stock) {
                 $dataInventory = array('ProdId' => $itemId, 'QuantityOnHand' => $rows_stock["QuantityOnHand"] + $data['qty' . $i], 'QuantityAvailable' => $rows_stock["QuantityAvailable"] + $data['qty' . $i], 'Timestamp' => new Zend_date());
                 $db_global->updateRecord($dataInventory, $rows_stock["ProdId"], "ProdId", "tb_inventorytotal");
                 unset($dataInventory);
             } else {
                 $addInventory = array('ProdId' => $itemId, 'QuantityOnHand' => $data['qty' . $i], 'QuantityAvailable' => $data['qty' . $i], 'Timestamp' => new Zend_date());
                 $db->insert("tb_inventorytotal", $addInventory);
                 unset($addInventory);
             }
         }
     }
 }
 public function RecievedPurchaseOrder($data)
 {
     try {
         $db = $this->getAdapter();
         $db->beginTransaction();
         $db_global = new Application_Model_DbTable_DbGlobal();
         $session_user = new Zend_Session_Namespace('auth');
         $userName = $session_user->user_name;
         $GetUserId = $session_user->user_id;
         $_order_no = $data["order_no"];
         $_order_id = $data["order_id"];
         $ids = explode(',', $data['identity']);
         //print_r($row);//exit();
         if ($data['invoice_no'] == "") {
             $date = new Zend_Date();
             $recieved_num = "RO" . $date->get('hh-mm-ss');
         } else {
             $recieved_num = $data['invoice_no'];
         }
         $sql_itm = "SELECT\n\t\t\t\t(SELECT p.pro_id FROM tb_product AS p WHERE p.pro_id = po.`pro_id`) AS pro_id\n\t\t\t\t\t\n\t\t\t\t,(SELECT p.qty_onorder FROM tb_product AS p WHERE p.pro_id = po.`pro_id`) AS qty_onorder\n\t\t\t\t\n\t\t\t\t,(SELECT p.qty_onhand \tFROM tb_product AS p WHERE p.pro_id = po.`pro_id`) AS qty_onhand\n\t\t\t\t\n\t\t\t\t,(SELECT p.qty_available \tFROM tb_product AS p WHERE p.pro_id = po.`pro_id`) AS qty_available\n\t\t\t\t\t\n\t\t\t\t, SUM(po.`qty_order`) AS qty_order FROM\n\t\t\t\t\n\t\t\t\ttb_purchase_order_item AS po WHERE po.order_id = {$_order_no} GROUP BY po.pro_id";
         $result = $db_global->getGlobalDb($sql_itm);
         if ($result) {
             foreach ($result as $row_pro) {
                 $row_get = $db_global->porductLocationExist($row_pro["pro_id"], $data["LocationId"]);
                 //print_r($row_get);
                 if ($row_get) {
                     $update_prolo_stock = array("qty_onorder" => $row_get["qty_onorder"] - $row_pro["qty_order"], "last_usermod" => $GetUserId, "last_mod_date" => new Zend_Date());
                     $update_data = $db_global->updateRecord($update_prolo_stock, $row_get["ProLocationID"], "ProLocationID", "tb_prolocation");
                     unset($update_prolo_stock);
                 }
                 $update_product_stock = array("qty_onorder" => $row_pro["qty_onorder"] - $row_pro["qty_order"], "last_mod_date" => new Zend_Date());
                 $sqls = $db_global->updateRecord($update_product_stock, $row_pro["pro_id"], "pro_id", "tb_product");
                 unset($update_product_stock);
             }
         }
         unset($result);
         try {
             $info_purchase_order = array("vendor_id" => $data['v_name'], "LocationId" => $data["LocationId"], "status" => 5, "remark" => $data['remark'], "user_mod" => $GetUserId, "timestamp" => new Zend_Date(), "paid" => $data['paid'], "all_total" => $data['totalAmoun'], "balance" => $data['remain']);
             $db_global->updateRecord($info_purchase_order, $_order_no, "order_id", "tb_purchase_order");
             unset($info_purchase_order);
         } catch (Exception $e) {
             echo $e->getMessage();
         }
         $sql_recieve = new purchase_Model_DbTable_DbPurchaseOrder();
         $result_recieve = $sql_recieve->recieved_info($_order_no);
         $recieved_id = $result_recieve["recieve_id"];
         if ($result_recieve) {
             $data_recieved_order = array("recieve_type" => 1, "vendor_id" => $data['v_name'], "location_id" => $data["LocationId"], "date_recieve" => new Zend_Date(), "status" => 5, "is_active" => 1, "paid" => $data['paid'], "all_total" => $data['totalAmoun'], "balance" => $data['remain'], "user_recieve" => $GetUserId);
             $recieved_order = $db_global->updateRecord($data_recieved_order, $result_recieve["recieve_id"], "recieve_id", "tb_recieve_order");
             unset($data_recieved_order);
             $sqls = "DELETE FROM tb_recieve_order_item WHERE recieve_id IN ({$recieved_id})";
             $db_global->deleteRecords($sqls);
             unset($sqls);
             foreach ($ids as $i) {
                 $recieved_item[$i] = array("recieve_id" => $recieved_id, "pro_id" => $data['item_id_' . $i], "order_id" => $_order_no, "qty_order" => $data['qty' . $i], "qty_recieve" => $data['qty' . $i], "price" => $data['price' . $i], "total_before" => $data['total' . $i], "sub_total" => $data['total' . $i]);
                 $db->insert("tb_recieve_order_item", $recieved_item[$i]);
                 unset($recieved_item[$i]);
             }
         } else {
             $recieve_order = array("recieve_no" => $recieved_num, "order_id" => $_order_no, "order_no" => $data['order_num'], "vendor_id" => $data["v_name"], "recieve_type" => 1, "location_id" => $data["LocationId"], "order_date" => $data["order_date"], "date_recieve" => new Zend_Date(), "status" => 5, "is_active" => 1, "paid" => $data["paid"], "all_total" => $data["remain"], "user_recieve" => $GetUserId);
             $this->_name = "tb_recieve_order";
             $recieved_order = $this->insert($recieve_order);
             unset($recieve_order);
             foreach ($ids as $i) {
                 $recieved_item[$i] = array("recieve_id" => $recieved_order, "pro_id" => $data['item_id_' . $i], "order_id" => $_order_no, "qty_order" => $data['qty' . $i], "qty_recieve" => $data['qty' . $i], "price" => $data['price' . $i], "total_before" => $data['total' . $i], "sub_total" => $data['total' . $i]);
                 $db->insert("tb_recieve_order_item", $recieved_item[$i]);
                 unset($recieved_item[$i]);
             }
         }
         $sql = "DELETE FROM tb_purchase_order_item WHERE order_id IN ({$_order_no})";
         $db_global->deleteRecords($sql);
         unset($sql);
         $sql_history = "DELETE FROM tb_purchase_order_history WHERE `order` IN ({$_order_no})";
         $db_global->deleteRecords($sql_history);
         unset($sql_history);
         foreach ($ids as $i) {
             // Insert New purchase order item in old order_id
             $data_item[$i] = array('order_id' => $_order_no, 'pro_id' => $data['item_id_' . $i], 'qty_order' => $data['qty' . $i], 'price' => $data['price' . $i], 'sub_total' => $data['total' . $i], 'total_befor' => $data['total' . $i], 'remark' => $data['remark_' . $i]);
             //print_r($data_item); echo "<br />";echo "<br />";
             $db->insert("tb_purchase_order_item", $data_item[$i]);
             unset($data_item[$i]);
             $data_history[$i] = array('order' => $_order_no, 'pro_id' => $data['item_id_' . $i], 'type' => 1, 'customer_id' => $data['v_name'], 'status' => 5, 'order_total' => $data['total' . $i], 'qty' => $data['qty' . $i], 'unit_price' => $data['price' . $i], 'sub_total' => $data['total' . $i], 'date' => $data["order_date"], 'last_update_date' => new Zend_Date());
             //print_r($data_history);exit();
             $db->insert("tb_purchase_order_history", $data_history[$i]);
             unset($data_history[$i]);
             $locationid = $data['LocationId'];
             $itemId = $data['item_id_' . $i];
             $qtyrecord = $data['qty' . $i];
             //qty on 1 record
             // Update stock in tb_product
             $rows = $db_global->productLocationInventory($itemId, $locationid);
             //to check product location
             if ($rows) {
                 $getrecord_id = $rows["ProLocationID"];
                 $itemOnHand = array('qty_onhand' => $rows["qty_onhand"] + $qtyrecord, 'qty_available' => $rows["qty_available"] + $qtyrecord, 'last_mod_date' => new Zend_date());
                 $db_global->updateRecord($itemOnHand, $itemId, "pro_id", "tb_product");
                 unset($itemOnHand);
                 $updatedata = array('qty' => $rows['qty'] + $qtyrecord, 'last_mod_date' => new Zend_date());
                 $db_global->updateRecord($updatedata, $getrecord_id, "ProLocationID", "tb_prolocation");
             } else {
                 //insert stock ;
                 $rows_pro_exit = $db_global->productLocation($itemId, $locationid);
                 // check product location exist
                 if ($rows_pro_exit) {
                     $updatedata = array('qty' => $rows['qty'] + $qtyrecord, 'last_mod_date' => new Zend_date());
                     $itemid = $db_global->updateRecord($updatedata, $rows_pro_exit['ProLocationID'], "ProLocationID", "tb_prolocation");
                     unset($updatedata);
                 } else {
                     // If product not exist insert New product in tb_prolocation
                     $insertdata = array('pro_id' => $itemId, 'LocationId' => $locationid, 'last_usermod' => $GetUserId, 'qty' => $qtyrecord, 'last_mod_date' => new Zend_Date());
                     //update stock product location
                     $db->insert("tb_prolocation", $insertdata);
                     unset($insertdata);
                 }
                 // End If product not exist insert New product in tb_prolocation
                 $rowitem = $db_global->InventoryExist($itemId);
                 //to check product exist
                 // If productt exist update product in tb_product
                 if ($rowitem) {
                     $itemOnHand = array('qty_onhand' => $rows["qty_onhand"] + $qtyrecord, 'qty_available' => $rows["qty_available"] + $qtyrecord, 'last_mod_date' => new Zend_date());
                     $itemid = $db_global->updateRecord($itemOnHand, $itemId, "pro_id", "tb_product");
                     unset($itemOnHand);
                 } else {
                     // If product not exist insert new product in tb_product
                     $dataInventory = array('pro_id' => $itemId, 'qty_onhand' => $qtyrecord, 'qty_available' => $qtyrecord, 'last_mod_date' => new Zend_date());
                     $db->insert("tb_product", $dataInventory);
                     unset($dataInventory);
                     //update stock product location
                 }
             }
         }
         $db->commit();
     } catch (Exception $e) {
         echo $e->getMessage();
         exit;
         $db->rollBack();
     }
 }
Example #9
0
 public function updatCat($data, $id)
 {
     try {
         $db_global = new Application_Model_DbTable_DbGlobal();
         $db = $this->getAdapter();
         $db->beginTransaction();
         $older = $data['id_code'];
         $sql = "SELECT p.`order` FROM tb_purchase_order AS p WHERE p.`order`= '{$older}'";
         $session_user = new Zend_Session_Namespace('auth');
         $userName = $session_user->user_name;
         $GetUserId = $session_user->user_id;
         $info_purchase_order = array("vendor_id" => $data['vendor'], "date_in" => $data['date'], "status" => $data['status'], "user_mod" => $GetUserId, "timestamp" => new Zend_Date(), "paid" => $data['paid'], "discount" => $data['discount'], "all_total" => $data['totalAmoun'], "balance" => $data['remain']);
         $this->_name = "tb_purchase_order";
         $where = $db->quoteInto("order_id", $id);
         $this->update($info_purchase_order, $where);
         $row_old_order = $this->getSalesOderID($id);
         if ($row_old_order) {
             foreach ($row_old_order as $rs) {
                 $pro_exist = $db_global->InventoryExist($rs["pro_id"]);
                 //foreach ($pro_exist as $rs_pro){
                 //print_r($pro_exist["qty_onorder"]);exit();
                 $itemOnOrder = array('qty_onorder' => $pro_exist["qty_onorder"] - ($rs['qty_unit'] * $pro_exist["qty_perunit"] + $rs['qty']), 'qty_onhand' => $pro_exist["qty_onhand"] - ($rs['qty_unit'] * $pro_exist["qty_perunit"] + $rs['qty']), 'qty_available' => $pro_exist["qty_available"] - ($rs['qty_unit'] * $pro_exist["qty_perunit"] + $rs['qty']));
                 //update total stock
                 $db_global->updateRecord($itemOnOrder, $rs["pro_id"], "pro_id", "tb_product");
                 //}
             }
         }
         $sql = "DELETE FROM tb_purchase_order_item WHERE order_id={$id}";
         $db->query($sql);
         $ids = explode(',', $data['identity']);
         foreach ($ids as $i) {
             $itemId = $data['item_id' . $i];
             $qtyrecord = $data['unit_qty' . $i];
             //qty on 1 record
             $data_history[$i] = array('pro_id' => $data['item_id' . $i], 'type' => 1, 'order' => $id, 'customer_id' => $data['vendor'], 'date' => new Zend_Date(), 'status' => $data['status'], 'qty_unit' => $data['unit_qty' . $i], 'qty_per_unit' => $data['unit_price' . $i], 'qty' => $data['qty' . $i], 'price' => $data['price_per_qty' . $i], "discount" => $data['discount'], 'total' => $data['total_price' . $i]);
             $order_history = $db_global->addRecord($data_history[$i], "tb_purchase_order_history");
             unset($data_history[$i]);
             $data_item[$i] = array('order_id' => $id, 'pro_id' => $data['item_id' . $i], 'qty_unit' => $data['unit_qty' . $i], 'qty_per_unit' => $data['unit_price' . $i], 'qty' => $data['qty' . $i], 'price' => $data['price_per_qty' . $i], 'total_befor' => $data['total_price' . $i], "discount" => $data['discount'], 'sub_total' => $data['total_price' . $i]);
             $recieved_order = $db->insert("tb_purchase_order_item", $data_item[$i]);
             $data_item_history[$i] = array('order_id' => $id, 'pro_id' => $data['item_id' . $i], 'qty_unit' => $data['unit_qty' . $i], 'qty_per_unit' => $data['unit_price' . $i], 'qty' => $data['qty' . $i], 'price' => $data['price_per_qty' . $i], 'total_befor' => $data['total_price' . $i], 'sub_total' => $data['total_price' . $i]);
             $recieved_order_history = $db->insert("tb_purchase_order_item_history", $data_item[$i]);
             $rows = $db_global->InventoryExist($itemId);
             //print_r($rows);exit();
             if ($rows) {
                 //$qty_onhand   = $rowitem["QuantityOnHand"]+$data['qty'.$i];
                 echo $rows["qty_perunit"];
                 $itemOnOrder = array('qty_onorder' => $rows["qty_onorder"] + $data['unit_qty' . $i] * $rows["qty_perunit"] + $data['qty' . $i], 'qty_onhand' => $rows["qty_onhand"] + $data['unit_qty' . $i] * $rows["qty_perunit"] + $data['qty' . $i], 'qty_available' => $rows["qty_available"] + $data['unit_qty' . $i] * $rows["qty_perunit"] + $data['qty' . $i], 'pro_id' => $itemId);
                 //update total stock
                 $db_global->updateRecord($itemOnOrder, $itemId, "pro_id", "tb_product");
             } else {
                 $dataInventory = array('pro_id' => $itemId, 'qty_onorder' => $rows["qty_onorder"] + $data['unit_qty' . $i] * $rows["qty_perunit"] + $data['qty' . $i], 'qty_onhand' => $rows["qty_onhand"] + $data['unit_qty' . $i] * $rows["qty_perunit"] + $data['qty' . $i], 'qty_available' => $rows["qty_available"] + $data['unit_qty' . $i] * $rows["qty_perunit"] + $data['qty' . $i], 'last_mod_date' => new Zend_date());
                 $db->insert("tb_product", $dataInventory);
                 //add move hostory
             }
         }
         // 			exit();
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
         $e->getMessage();
         exit;
     }
 }
 public function addAdjustStock($post)
 {
     try {
         $db = $this->getAdapter();
         $db->beginTransaction();
         $session_user = new Zend_Session_Namespace('auth');
         $userName = $session_user->user_name;
         $GetUserId = $session_user->user_id;
         $identity = explode(',', $post['identity']);
         $db_global = new Application_Model_DbTable_DbGlobal();
         $db_global = new Application_Model_DbTable_DbGlobal();
         foreach ($identity as $i) {
             $rows = $db_global->porductLocationExist($post['item_id_' . $i], $post['location_id_' . $i]);
             //to check product location
             if ($rows) {
                 $data_history = array('transaction_type' => 1, 'pro_id' => $post['item_id_' . $i], 'date' => new Zend_Date(), 'location_id' => $post['location_id_' . $i], 'Remark' => $post['remark_' . $i], 'qty_before' => $rows['qty'], 'qty_edit' => $post['differ_' . $i], 'qty_after' => $post['qty_after_' . $i], 'user_mod' => $GetUserId);
                 $db->insert("tb_move_history", $data_history);
                 unset($data_history);
                 //update poduct location
                 $data = array('qty' => $rows['qty'] + $post['differ_' . $i], 'last_usermod' => $GetUserId, 'last_mod_date' => new Zend_Date());
                 $itemid = $db_global->updateRecord($data, $rows['ProLocationID'], "ProLocationID", "tb_prolocation");
                 //update qty in stock inventory
                 $row_exist = $db_global->InventoryExist($post['item_id_' . $i]);
                 if ($row_exist) {
                     $datatotal = array('QuantityOnHand' => $row_exist['QuantityOnHand'] + $post['differ_' . $i], 'QuantityAvailable' => $row_exist['QuantityAvailable'] + $post['differ_' . $i], 'Timestamp' => new Zend_Date());
                     $db_global->updateRecord($datatotal, $post['item_id_' . $i], "ProdId", "tb_inventorytotal");
                     unset($datatotal);
                 } else {
                     $dataInventory = array('ProdId' => $post['item_id_' . $i], 'QuantityOnHand' => $post['qty_after_' . $i], 'QuantityAvailable' => $post['qty_after_' . $i], 'Timestamp' => new Zend_Date());
                     $db->insert("tb_inventorytotal", $dataInventory);
                     unset($dataInventory);
                 }
             } else {
                 //add add qty into pro location 26-8-13
                 $add_pro_location = array('pro_id' => $post['item_id_' . $i], 'LocationId' => $post['location_id_' . $i], 'qty' => $post['qty_after_' . $i], 'last_usermod' => $GetUserId, 'last_mod_date' => new Zend_Date());
                 $db->insert("tb_prolocation", $add_pro_location);
                 unset($add_pro_location);
                 //add move history 26-8-13
                 $data_history = array('transaction_type' => 1, 'pro_id' => $post['item_id_' . $i], 'date' => new Zend_Date(), 'location_id' => $post['location_id_' . $i], 'Remark' => $post['remark_' . $i], 'qty_edit' => $post['differ_' . $i], 'qty_after' => $post['qty_after_' . $i], 'user_mod' => $GetUserId);
                 $db->insert("tb_move_history", $data_history);
                 unset($data_history);
                 $row_exist = $db_global->InventoryExist($post['item_id_' . $i]);
                 if ($row_exist) {
                     $data_total = array('QuantityOnHand' => $row_exist['QuantityOnHand'] + $post['differ_' . $i], 'QuantityAvailable' => $row_exist['QuantityAvailable'] + $post['differ_' . $i], 'Timestamp' => new Zend_Date());
                     //update produt total inventory
                     $db_global->updateRecord($data_total, $post['item_id_' . $i], "ProdId", "tb_inventorytotal");
                     unset($data_total);
                 } else {
                     //add to pro total inventory
                     $dataInventory = array('ProdId' => $post['item_id_' . $i], 'QuantityOnHand' => $post['qty_after_' . $i], 'QuantityAvailable' => $post['qty_after_' . $i], 'Timestamp' => new Zend_Date());
                     $db->insert("tb_inventorytotal", $dataInventory);
                     unset($dataInventory);
                 }
             }
             //add data to adjust stock 26-8-13//not yet use cos have not in move history
             // 	    	$data_adjust= array(
             // 	    			'LocationId'     => $post['location_id_'.$i],
             // 	    			'QuantityBefore' => $post['qty_before_'.$i],
             // 	    			'QuantityAfter'  => $post['qty_after_'.$i],
             // 	    			'Difference'     => $post['differ_'.$i],
             // 	    			'Timestamp'      => new Zend_Date(),
             // 	    			'last_usermod'   => $GetUserId,
             // 	    			'ProdId'         => $post['item_id_'.$i],
             // 	    			'remark'         => $post['remark']
             // 	    	);
             // 	    	$adjust=$db->insert("tb_stockadjust", $data_adjust);
         }
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
     }
 }