public function themSanPhamAction() { $return = array(); $form = $this->getServiceLocator()->get('Application\\Form\\ThemSanPhamForm'); $return['form'] = $form; $request = $this->getRequest(); if ($request->isPost()) { $post = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray()); $form->setData($post); if ($form->isValid()) { $san_pham_moi = new SanPham(); $san_pham_table = $this->getServiceLocator()->get('Application\\Model\\SanPhamTable'); $id_kho = $this->AuthService()->getIdKho(); $user_id = $this->AuthService()->getUserId(); $san_pham_moi->exchangeArray($post); // nếu nhập mã vạch if ($post['ma_vach']) { // nếu nhập mã vạch thì phải kiểm tra có tồn tại chưa $san_pham_ton_tai = $san_pham_table->getSanPhamByArrayConditionAndArrayColumn(array('id_kho' => $id_kho, 'ma_vach' => $post['ma_vach']), array('id_san_pham')); if ($san_pham_ton_tai) { $form->get('ma_vach')->setMessages(array('Mã vạch này đã được sử dụng')); $return['form'] = $form; return $return; } $san_pham_moi->setIdBarcode(6); } else { $barcode = $this->Barcode()->createBarcode(); $san_pham_moi->setIdBarcode($barcode['id_barcode']); $san_pham_moi->setMaVach($barcode['ma_vach']); } // nếu nhập mã sản phẩm if ($post['ma_san_pham']) { // nếu nhập mã sản phẩm thì phải kiểm tra có tồn tại chưa $san_pham_ton_tai = $san_pham_table->getSanPhamByArrayConditionAndArrayColumn(array('id_kho' => $id_kho, 'ma_san_pham' => $post['ma_san_pham']), array('id_san_pham')); if ($san_pham_ton_tai) { $form->get('ma_san_pham')->setMessages(array('Mã sản phẩm này đã được sử dụng')); $return['form'] = $form; return $return; } } else { $ma_san_pham = $san_pham_moi->getMaVach(); $san_pham_moi->setMaSanPham($ma_san_pham); } // nếu loại giá if ($post['loai_gia']) { $san_pham_moi->setGiaNhap(0); } else { $san_pham_moi->setGiaBia(0); $san_pham_moi->setChietKhau(0); } // xử lý hình ảnh $image = $post['hinh_anh']; if ($image and $image['error'] == 0) { $path = "./public/img/orther/product/" . $id_kho . "/"; $pathSave = "/img/orther/product/" . $id_kho . "/"; if (!file_exists($path)) { mkdir($path, 0700, true); } $uniqueToken = md5(uniqid(mt_rand(), true)); $newName = $this->CheckPathExist()->checkPathExist($path, $uniqueToken, $image['name']); $filter = new \Zend\Filter\File\Rename($path . $newName); $filter->filter($image); $pathSave .= $newName; } else { $pathSave = "/img/default/product/default.png"; } $san_pham_moi->setHinhAnh($pathSave); $san_pham_moi->setIdKho($id_kho); $san_pham_moi->setTonKho(0); $san_pham_moi->setState(1); $san_pham_moi->setUserId($user_id); $san_pham_table->saveSanPham($san_pham_moi); $this->flashMessenger()->addSuccessMessage('Chúc mừng, thêm sản phẩm thành công!'); return $this->redirect()->toRoute('hang_hoa', array('action' => 'index')); } else { // form not valid $return['form'] = $form; return $return; } } else { // not post value return $return; } }
public function saveSanPham(SanPham $san_pham) { $data = array('id_kho' => $san_pham->getIdKho(), 'id_don_vi_tinh' => $san_pham->getIdDonViTinh(), 'id_barcode' => $san_pham->getIdBarcode(), 'ma_san_pham' => $san_pham->getMaSanPham(), 'ma_vach' => $san_pham->getMaVach(), 'id_loai_san_pham' => $san_pham->getIdLoaiSanPham(), 'ten_san_pham' => $san_pham->getTenSanPham(), 'mo_ta' => $san_pham->getMoTa(), 'hinh_anh' => $san_pham->getHinhAnh(), 'nhan' => $san_pham->getNhan(), 'ton_kho' => $san_pham->getTonKho(), 'loai_gia' => $san_pham->getLoaiGia(), 'gia_nhap' => $san_pham->getGiaNhap(), 'gia_bia' => $san_pham->getGiaBia(), 'chiet_khau' => $san_pham->getChietKhau(), 'state' => $san_pham->getState(), 'user_id' => $san_pham->getUserId()); $id_san_pham = (int) $san_pham->getIdSanPham(); if ($id_san_pham == 0) { $this->tableGateway->insert($data); } else { if ($this->getSanPhamByArrayConditionAndArrayColumn(array('id_san_pham' => $id_san_pham), array('ten_san_pham'))) { $this->tableGateway->update($data, array('id_san_pham' => $id_san_pham)); } else { return false; } } return true; }