public function checkInAction()
 {
     try {
         $db = new Application_Model_DbTable_DbGlobal();
         if ($this->getRequest()->isPost()) {
             $data = $this->getRequest()->getPost();
             $recieve_order = new purchase_Model_DbTable_DbPurchaseVendor();
             if (isset($data['SaveNew'])) {
                 try {
                     $recieve_order->RecievedPurchaseOrder($data);
                     Application_Form_FrmMessage::message("Purchase has been received!");
                 } catch (Exception $e) {
                     echo $e->getMessage();
                 }
                 //Application_Form_FrmMessage::redirectUrl("/purchase/index/check-in");
                 //$this->_redirect("purchase/index/add-purchase");
             } else {
                 $recieve_order->RecievedPurchaseOrder($data);
                 $this->_redirect("/purchase/index/index");
             }
             //not yet use in this version
             // 				elseif(@$data['Save']=='Save'){
             // 					$payment_purchase_order = new purchase_Model_DbTable_DbPurchaseVendor();
             // 					$payment_purchase_order -> VendorOrder($data);
             // 					$this->_redirect("purchase/index/index");
             // 				}
             // 				elseif(@$data['New']=='New'){
             // 					$this->_redirect("purchase/index/add-purchase");
             // 				}
         }
         $user = $this->GetuserInfoAction();
         if ($user["level"] != 1 and $user["level"] != 2) {
             $this->_redirect("purchase/index/index");
         }
         $frm_purchase = new Application_Form_FrmCheckIn(null);
         $form_add_purchase = $frm_purchase->productOrder(null);
         Application_Model_Decorator::removeAllDecorator($form_add_purchase);
         $this->view->form_purchase = $form_add_purchase;
         // item option in select
         $items = new Application_Model_GlobalClass();
         $itemRows = $items->getProductOption();
         $this->view->items = $itemRows;
         //get control
         $formControl = new Application_Form_FrmAction(null);
         $formViewControl = $formControl->AllAction(null);
         Application_Model_Decorator::removeAllDecorator($formViewControl);
         $this->view->control = $formViewControl;
         // 		//for search
         // 		$search = new purchase_Form_FrmSearch();
         // 		$frmsearch= $search->formSearch();
         // 		Application_Model_Decorator::removeAllDecorator($frmsearch);
         // 		$this->view->get_frmsearch= $frmsearch;
         //for view left purchase order
         $vendor_sql = "SELECT p.order, p.all_total,p.paid,p.balance\n\t\tFROM tb_purchase_order AS p INNER JOIN tb_vendor AS v ON v.vendor_id=p.vendor_id ORDER BY p.timestamp DESC ";
         $rows = $db->getGlobalDb($vendor_sql);
         $this->view->list = $rows;
         //for add product;
         $formpopup = new Application_Form_FrmPopup(null);
         $formproduct = $formpopup->popuProduct(null);
         Application_Model_Decorator::removeAllDecorator($formproduct);
         $this->view->form = $formproduct;
         //for add vendor
         $formStockAdd = $formpopup->popupVendor(null);
         Application_Model_Decorator::removeAllDecorator($formStockAdd);
         $this->view->form_vendor = $formStockAdd;
         //for add location
         $formAdd = $formpopup->popuLocation(null);
         Application_Model_Decorator::removeAllDecorator($formAdd);
         $this->view->form_addstock = $formAdd;
     } catch (Exception $e) {
         Application_Form_FrmMessage::messageError("INSERT_ERROR", $err = $e->getMessage());
     }
 }
 public function updateAction()
 {
     if ($this->getRequest()->isPost()) {
         try {
             $add_product = new Product_Model_DbTable_DbAddProduct();
             $post = $this->getRequest()->getPost();
             if (@$post['Update'] or @$post['Deactive'] or @$post['Active']) {
                 $rs = $add_product->updateProduct($post);
                 if (!empty($rs)) {
                     $this->_redirect('/product/index/index');
                 }
             }
         } catch (Exception $e) {
             Application_Form_FrmMessage::messageError("APPLICATON_FAILED", $e->getMessage());
         }
     }
     //$session_stock=new Zend_Session_Namespace('stock');
     //if user type in wrong url
     $user = $this->GetuserInfoAction();
     if ($user["level"] == 4) {
         //echo "ture codition";
         //$this->_redirect("product/index/index");
     }
     $id = $this->getRequest()->getParam('id') ? $this->getRequest()->getParam('id') : '0';
     $gb = new Application_Model_DbTable_DbGlobal();
     if ($user["level"] != 1 and $user["level"] != 2) {
         $exist = $gb->productLocation($id, $user["location_id"]);
         if ($exist == "") {
             //$this->_redirect("product/index/index");
         }
     } else {
         $pro_exist = $gb->myProductExist($id);
         if (!$pro_exist) {
             //$this->_redirect("product/index/index");
         }
     }
     //get product info detail 23/8/13
     $productinfo = new Product_Model_DbTable_DbProduct();
     $rows = $productinfo->getProductInfo($id);
     $formStock = new Application_Form_FrmProduct();
     $formStockEdit = $formStock->AddProductForm($rows);
     Application_Model_Decorator::removeAllDecorator($formStockEdit);
     // omit default zend html tag
     $this->view->form = $formStockEdit;
     if ($rows['photo'] == "") {
         $rows['photo'] = "no-img.gif";
     }
     $this->view->photo = $rows['photo'];
     $this->view->status = $rows['is_avaliable'];
     //get product move history 23/8/13
     $rows = $productinfo->moveproduct($id);
     $list = new Application_Form_Frmlist();
     $glClass = new Application_Model_GlobalClass();
     $rows = $glClass->getTransactionType($rows, BASE_URL, true);
     $columns = array("TRANSACTION_TYPE_CAP", "DATE_CAP", "LOCATION_NAME_CAP", "QTY_CAP", "QTY_BEFORE_CAP", "QTY_AFTER_CAP", "REMARK_CAP", "BY_USER_CAP");
     $link = array('module' => 'product', 'controller' => 'index', 'action' => 'update');
     $this->view->list_history = $list->getCheckList(1, $columns, $rows, array('Name' => $link, 'Name' => $link), "", "move", "left", false, "move_history");
     //view sale order history
     $row_sale_history = $productinfo->getSaleHistory($id);
     $glClass = new Application_Model_GlobalClass();
     $row_sale_history = $glClass->getTypeHistory($row_sale_history, BASE_URL, true);
     $row_sale_history = $glClass->getStatusType($row_sale_history, BASE_URL, true);
     $columns1 = array("TYPE_CAP", "ORDER_ADD_CAP", "CUSTOMER_CAP", "ORDER_DATE_CAP", "ORDER_STATUS_CAP", "TOTAL_PRICE_CAP", "QTY_CAP", "UNIT_PRICE_CAP", "SUB_TOTAL_CAP");
     $link1 = array('module' => 'product', 'controller' => 'index', 'action' => 'update');
     $list = new Application_Form_Frmlist();
     $this->view->list_order_history = $list->getCheckList(1, $columns1, $row_sale_history, array('item_name' => $link1, 'Name' => $link1), "", "items", "left", false, "order_history");
     $row_purchase_history = $productinfo->getPurchaseHistory($id);
     $glClass = new Application_Model_GlobalClass();
     $row_purchase_history = $glClass->getTypeHistory($row_purchase_history, BASE_URL, true);
     $row_purchase_history = $glClass->getStatusType($row_purchase_history, BASE_URL, true);
     $columns1 = array("TYPE_CAP", "ORDER_ADD_CAP", "VENDOR_CAP", "ORDER_DATE_CAP", "ORDER_STATUS_CAP", "TOTAL_PRICE_CAP", "QTY_CAP", "UNIT_PRICE_CAP", "SUB_TOTAL_CAP");
     $link_pur = array('module' => 'product', 'controller' => 'index', 'action' => 'update');
     $list = new Application_Form_Frmlist();
     $this->view->list_purchase_history = $list->getCheckList(1, $columns1, $row_purchase_history, array('item_name' => $link_pur, 'Name' => $link_pur), "", "items", "left", false, "purchase_history");
     //for product qty in stock 22/8/13
     if ($user["level"] == 1 or $user["level"] == 2) {
         $rowproduct = $productinfo->getProductStock($id);
         $this->view->pro_qty = $rowproduct;
     }
     // 		$rowproduct = $productinfo->getProductStock($id);
     // 		$this->view->pro_qty = $rowproduct;
     //get controll button
     $formProduct1 = new Application_Form_FrmAction(null);
     $formStockAdd1 = $formProduct1->AllAction(null);
     Application_Model_Decorator::removeAllDecorator($formStockAdd1);
     $this->view->Event = $formStockAdd1;
     //get item of this lost
     $orderModel = new Product_Model_DbTable_DbProduct();
     $orderDetail = $orderModel->getOrderItemDetailByID($id);
     $this->view->rowsOrder = $orderDetail;
     ///view on select location form
     $items = new Application_Model_GlobalClass();
     //$itemRows = $items->getLocationSelected($session_stock->stockID);
     $itemRows = $items->getLocationOption($id);
     $this->view->itemsOption = $itemRows;
     //for add location
     $formpopup = new Application_Form_FrmPopup(null);
     $formAdd = $formpopup->popuLocation(null);
     Application_Model_Decorator::removeAllDecorator($formAdd);
     $this->view->form_addstock = $formAdd;
     $form_measure = $formpopup->popuMeasure(null);
     Application_Model_Decorator::removeAllDecorator($form_measure);
     $this->view->form_measure = $form_measure;
     $form_branch = $formpopup->popupBranch(null);
     Application_Model_Decorator::removeAllDecorator($form_branch);
     $this->view->form_brand = $form_branch;
     $form_category = $formpopup->popupCategory(null);
     Application_Model_Decorator::removeAllDecorator($form_category);
     $this->view->form_category = $form_category;
 }
 public function updateProduct($post)
 {
     try {
         //use try on 21-11-13
         $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;
         $GetUserLever = $session_user->level;
         $user_location = $session_user->location_id;
         $photoname = str_replace(" ", "_", $post['txt_name']) . '.jpg';
         $upload = new Zend_File_Transfer();
         $upload->addFilter('Rename', array('target' => PUBLIC_PATH . '/images/fi-upload/' . $photoname, 'overwrite' => true), 'photo');
         $receive = $upload->receive();
         if ($receive) {
             $post['photo'] = $photoname;
         }
         unset($post['MAX_FILE_SIZE']);
         $data = array('item_name' => $post['txt_name'], 'item_code' => $post['txt_code'], 'item_code' => $post['product_size'], 'photo' => $post['photo'], 'brand_id' => $post['branch_id'], 'cate_id' => $post['category'], 'stock_type' => $post['stock_type'], 'measure_id' => $post['measure_unit'], 'qty_perunit' => $post['qty_perunit'], 'label' => $post['label_perunit'], 'is_avaliable' => $post['status'], 'unit_sale_price' => $post['unit_sale_price'], 'price_per_qty' => $post['unit_sale_price'] / $post['qty_perunit'], 'remark' => $post['remark'], 'last_usermod' => $GetUserId, 'last_mod_date' => new Zend_Date());
         $id = $post['id'];
         $itemid = $db_global->updateRecord($data, $id, "pro_id", "tb_product");
         //for update product by user level
         if ($GetUserLever == 1 or $GetUserLever == 2) {
             $_model = new Application_Model_DbTable_DbGlobal();
             $identitys = explode(',', $post['identity']);
             $qtyonhand = 0;
             foreach ($identitys as $i) {
                 $qtyonhand = $qtyonhand + $post['qty_' . $i];
                 if (empty($post['item_price' . $i])) {
                     $post['item_price' . $i] = 0;
                 }
                 $_rs = $_model->QtyProLocation($id, $post['location_id_' . $i]);
                 if ($_rs !== "") {
                     if ($_rs['qty'] !== $post['qty_' . $i]) {
                         $_arr = array('qty' => $post['qty_' . $i], 'qty_warn' => $post['qty_warnning' . $i], 'unit_sale_price' => $post['item_price' . $i], 'price_per_qty' => $post['item_price' . $i] / $post['qty_perunit'], 'last_usermod' => $this->getUserId(), 'last_mod_date' => new Zend_Date());
                         $db_global->updateRecord($_arr, $_rs['ProLocationID'], "ProLocationID", "tb_prolocation");
                         //code here
                         $_arr_history = array('transaction_type' => 1, 'pro_id' => $id, 'date' => new Zend_Date(), 'location_id' => $post['location_id_' . $i], 'qty_edit' => $_rs['qty'] . " -> " . $post['qty_' . $i], 'qty_before' => $_rs['qty'], 'qty_after' => $post['qty_' . $i], 'user_mod' => $this->getUserId());
                         $db->insert("tb_move_history", $_arr_history);
                     }
                 } else {
                     $dataproduct = array('pro_id' => $id, 'LocationId' => $post['location_id_' . $i], 'qty' => $post['qty' . $i], 'qty_warn' => $post['qty_warnning' . $i], 'unit_sale_price' => $post['item_price' . $i], 'price_per_qty' => $post['item_price' . $i] / $post['qty_perunit'], 'last_usermod' => $this->getUserId(), 'last_mod_date' => new Zend_Date());
                     //add qty to product location
                     $db->insert("tb_prolocation", $dataproduct);
                     //add history
                     $data_history = array('transaction_type' => 1, 'pro_id' => $id, 'date' => new Zend_Date(), 'location_id' => $post['location_id_' . $i], 'qty_before' => 0, 'qty_edit' => "0 -> " . $post['qty' . $i], 'qty_after' => $post['qty' . $i], 'user_mod' => $this->getUserId());
                     $db->insert("tb_move_history", $data_history);
                     unset($dataproduct);
                     unset($data_history);
                 }
             }
             $_rs = $db_global->getQtyFromProductById($id);
             if (!empty($_rs)) {
                 $_qty_deffer = $qtyonhand - $_rs['qty_onhand'];
                 $_qtydata = array('qty_onhand' => $qtyonhand, 'qty_available' => $_rs['qty_available'] + $_qty_deffer);
                 $where = $db->quoteInto("pro_id=?", $id);
                 $this->update($_qtydata, $where);
             }
         } else {
             $identitys = explode(',', $post['identity']);
             $qty_onhand = 0;
             foreach ($identitys as $i) {
                 $qty = $post['qty_' . $i];
                 //not yet
                 if (empty($post['item_price' . $i])) {
                     $post['item_price' . $i] = 0;
                 }
                 $rows_exist = $db_global->porductLocationExist($id, $post["location_id_" . $i]);
                 $_arr = array('qty' => $post['qty_' . $i], 'qty_warn' => $post['qty_warnning' . $i], 'unit_sale_price' => $post['item_price' . $i], 'price_per_qty' => $post['item_price' . $i] / $post['qty_perunit'], 'last_usermod' => $this->getUserId(), 'last_mod_date' => new Zend_Date());
             }
             if ($rows_exist !== "") {
                 $db_global->updateRecord($_arr, $rows_exist["ProLocationID"], "ProLocationID", "tb_prolocation");
                 unset($arr);
                 if ($rows_exist["qty"] !== $qty) {
                     $data_history = array('transaction_type' => 1, 'pro_id' => $id, 'date' => new Zend_Date(), 'location_id' => $user_location, 'Remark' => $post['remark'], 'qty_edit' => $rows_exist["qty"] . " -> " . $qty, 'qty_before' => $rows_exist["qty"], 'qty_after' => $qty, 'user_mod' => $GetUserId);
                     $db->insert("tb_move_history", $data_history);
                     unset($data_history);
                     $_qty_deffer = $qty - $rows_exist["qty"];
                     $_rs = $db_global->getQtyFromProductById($id);
                     if (!empty($_rs)) {
                         $_qtydata = array('qty_onhand' => $_rs['qty_onhand'] + $_qty_deffer, 'qty_available' => $_rs['qty_available'] + $_qty_deffer);
                         $where = $db->quoteInto("pro_id=?", $id);
                         $this->update($_qtydata, $where);
                     }
                 }
             }
         }
         $db->commit();
         return true;
     } catch (Exception $e) {
         $db->rollBack();
         Application_Form_FrmMessage::messageError("UPDATE_FAIL", $e->getMessage());
     }
 }