public function suaChietKhauAction() { $request = $this->getRequest(); if ($request->isPost()) { $post = $request->getPost(); $form = $this->getServiceLocator()->get('Application\\Form\\SuaChietKhauForm'); $form->setData($post); if ($form->isValid()) { $id_kho = $this->AuthService()->getIdKho(); $kenh_phan_phoi_table = $this->getServiceLocator()->get('Application\\Model\\KenhPhanPhoiTable'); $san_pham_table = $this->getServiceLocator()->get('Application\\Model\\SanPhamTable'); $gia_xuat_table = $this->getServiceLocator()->get('Application\\Model\\GiaXuatTable'); $chiet_khau = $kenh_phan_phoi_table->getKenhPhanPhoiByArrayConditionAndArrayColumn(array('id_kho' => $id_kho, 'id_kenh_phan_phoi' => $post['id_kenh_phan_phoi']), array()); if (!$chiet_khau) { $this->flashMessenger()->addErrorMessage('Lỗi không tìm thấy kênh cần sửa'); return $this->redirect()->toRoute('chinh_sach'); } // sửa lại chiết khấu trong bảng kênh phân phối $kenh_phan_phoi_moi = new KenhPhanPhoi(); $kenh_phan_phoi_moi->exchangeArray($chiet_khau[0]); $kenh_phan_phoi_moi->setChietKhauXuong($post['chiet_khau_xuong']); $kenh_phan_phoi_moi->setChietKhauLen($post['chiet_khau_len']); $kenh_phan_phoi_table->saveKenhPhanPhoi($kenh_phan_phoi_moi); if ($post['sua_gia_xuat'] == 1) { // sửa lại tất cả giá xuất của sản phẩm $danh_sach_gia_xuat = $san_pham_table->getAllSanPhamAndGiaXuatByIdKenhPhanPhoiAndIdKho(array('id_kenh_phan_phoi' => $post['id_kenh_phan_phoi'], 'id_kho' => $id_kho)); foreach ($danh_sach_gia_xuat as $key => $gia_xuat) { if ($gia_xuat['loai_gia'] == 1 and $gia_xuat['gia_bia'] > 0) { $gia_xuat_moi = new GiaXuat(); $gia_xuat_moi->exchangeArray($gia_xuat); $gia_xuat_moi->setIdKenhPhanPhoi($post['id_kenh_phan_phoi']); $loi_nhuan = (double) ceil($gia_xuat['gia_bia'] * $post['chiet_khau_xuong'] / 100); $gx = (double) $gia_xuat['gia_bia'] - (double) $loi_nhuan; $gia_xuat_moi->setGiaXuat($gx); $gia_xuat_table->saveGiaXuat($gia_xuat_moi); } elseif ($gia_xuat['loai_gia'] == 0 and $gia_xuat['gia_nhap'] > 0) { $gia_xuat_moi = new GiaXuat(); $gia_xuat_moi->exchangeArray($gia_xuat); $gia_xuat_moi->setIdKenhPhanPhoi($post['id_kenh_phan_phoi']); $loi_nhuan = (double) ceil($gia_xuat['gia_nhap'] * $post['chiet_khau_len'] / 100); $gx = (double) $gia_xuat['gia_nhap'] + (double) $loi_nhuan; $gia_xuat_moi->setGiaXuat($gx); $gia_xuat_table->saveGiaXuat($gia_xuat_moi); } } } $this->flashMessenger()->addSuccessMessage('Chúc mừng, cập nhật thông tin chiết khấu thành công'); return $this->redirect()->toRoute('chinh_sach'); } else { // ngược lại form not valid $this->flashMessenger()->addErrorMessage('Lỗi dữ liệu nhập không đúng'); return $this->redirect()->toRoute('chinh_sach'); } } else { // ngược lại không phải post $this->flashMessenger()->addErrorMessage('Lỗi không thực thi'); return $this->redirect()->toRoute('chinh_sach'); } }
public function saveGiaXuat(GiaXuat $gia_xuat) { $data = array('id_san_pham' => $gia_xuat->getIdSanPham(), 'id_kenh_phan_phoi' => $gia_xuat->getIdKenhPhanPhoi(), 'gia_xuat' => $gia_xuat->getGiaXuat()); $id_gia_xuat = (int) $gia_xuat->getIdGiaXuat(); if ($id_gia_xuat == 0) { $this->tableGateway->insert($data); } else { if ($this->getGiaXuatByArrayConditionAndArrayColumn(array('id_gia_xuat' => $id_gia_xuat), array('gia_xuat'))) { $this->tableGateway->update($data, array('id_gia_xuat' => $id_gia_xuat)); } else { return false; } } return true; }
public function nhapHangHoaAction() { $request = $this->getRequest(); if ($request->isPost()) { $post = $request->getPost(); if (isset($post['id_nha_cung_cap']) and $post['id_nha_cung_cap'] and isset($post['id_san_pham']) and $post['id_san_pham'] and isset($post['so_luong']) and $post['so_luong'] and isset($post['gia_nhap']) and $post['gia_nhap']) { $id_phieu_nhap = ''; $user_id = $this->AuthService()->getUserId(); $id_kho = $this->AuthService()->getIdKho(); $phieu_nhap_table = $this->getServiceLocator()->get('Application\\Model\\PhieuNhapTable'); $ct_phieu_nhap_table = $this->getServiceLocator()->get('Application\\Model\\CtPhieuNhapTable'); $san_pham_table = $this->getServiceLocator()->get('Application\\Model\\SanPhamTable'); $kenh_phan_phoi_table = $this->getServiceLocator()->get('Application\\Model\\KenhPhanPhoiTable'); $gia_xuat_table = $this->getServiceLocator()->get('Application\\Model\\GiaXuatTable'); // kiểm tra vs id_kho đó có tồn tại id_nha_cung_cap vừa post không /*code*/ // tạo phiếu nhập và chi tiết phiếu nhập foreach ($post['id_san_pham'] as $key => $id_san_pham) { $form = $this->getServiceLocator()->get('Application\\Form\\NhapHangHoaForm'); $data = array(); $data['id_nha_cung_cap'] = $post['id_nha_cung_cap']; $data['id_san_pham'] = $id_san_pham; $data['so_luong'] = ceil($post['so_luong'][$key]); if ($post['loai_gia'][$key] == 1) { $loi_nhuan = (double) ((double) $post['gia_bia'][$key] * (double) $post['chiet_khau'][$key] / 100); $data['gia_nhap'] = ceil((double) $post['gia_bia'][$key] - $loi_nhuan); } else { $data['gia_nhap'] = ceil($post['gia_nhap'][$key]); } $form->setData($data); if ($form->isValid()) { if (!$id_phieu_nhap) { $phieu_nhap_moi = new PhieuNhap(); $ma_phieu_nhap = $this->TaoMaTuDong()->taoMaPhieuNhap(); $phieu_nhap_moi->setMaPhieuNhap($ma_phieu_nhap); $phieu_nhap_moi->setIdUser($user_id); $phieu_nhap_moi->setIdNhaCungCap($post['id_nha_cung_cap']); $date = date('Y-m-d h:i:s a', time()); $phieu_nhap_moi->setNgayNhap($date); $phieu_nhap_moi->setState(0); $phieu_nhap_table->savePhieuNhap($phieu_nhap_moi); $phieu_nhap = $phieu_nhap_table->getPhieuNhapByArrayConditionAndArrayColumn(array('ma_phieu_nhap' => $ma_phieu_nhap), array('id_phieu_nhap')); $id_phieu_nhap = $phieu_nhap[0]['id_phieu_nhap']; } $ct_phieu_nhap_moi = new CtPhieuNhap(); $ct_phieu_nhap_moi->exchangeArray($data); $ct_phieu_nhap_moi->setIdPhieuNhap($id_phieu_nhap); $ct_phieu_nhap_table->saveCtPhieuNhap($ct_phieu_nhap_moi); } else { // xóa phiếu nhập if ($id_phieu_nhap) { $phieu_nhap_table->deletePhieuNhap(array('id_phieu_nhap' => $id_phieu_nhap)); } // thông báo lỗi $this->flashMessenger()->addErrorMessage('Lỗi, nhập hàng hóa không thành công'); return $this->redirect()->toRoute('hang_hoa', array('action' => 'nhap-hang-hoa')); } } // get danh sách kênh phân phối $danh_sach_kenh_phan_phoi = $kenh_phan_phoi_table->getKenhPhanPhoiByArrayConditionAndArrayColumn(array('id_kho' => $id_kho), array('id_kenh_phan_phoi', 'chiet_khau_xuong', 'chiet_khau_len')); // cập nhật lại số lượng và loại giá, giá nhập, giá bìa, chiết khấu trong csdl foreach ($post['id_san_pham'] as $key => $id_san_pham) { $san_pham = $san_pham_table->getSanPhamByArrayConditionAndArrayColumn(array('id_san_pham' => $id_san_pham), array()); $san_pham_moi = new SanPham(); $san_pham_moi->exchangeArray($san_pham[0]); $san_pham_moi->setLoaiGia($post['loai_gia'][$key]); if ($post['loai_gia'][$key] == 1) { $san_pham_moi->setGiaBia(ceil($post['gia_bia'][$key])); $san_pham_moi->setChietKhau($post['chiet_khau'][$key]); $loi_nhuan = (double) ((double) $post['gia_bia'][$key] * (double) $post['chiet_khau'][$key] / 100); $gia_nhap = ceil((double) $post['gia_bia'][$key] - $loi_nhuan); $san_pham_moi->setGiaNhap($gia_nhap); } else { $san_pham_moi->setGiaNhap(ceil($post['gia_nhap'][$key])); $san_pham_moi->setGiaBia(0); $san_pham_moi->setChietKhau(0); } $ton_kho = $san_pham[0]['ton_kho']; $so_luong = ceil($post['so_luong'][$key]); $ton_kho += $so_luong; $san_pham_moi->setTonKho($ton_kho); $san_pham_table->saveSanPham($san_pham_moi); // xóa hết giá xuất $gia_xuat_table->deleteGiaXuat(array('id_san_pham' => $id_san_pham)); // lưu giá xuất mới foreach ($danh_sach_kenh_phan_phoi as $kenh_phan_phoi) { $gia_xuat = 0; if ($post['loai_gia'][$key] == 1) { $loi_nhuan = (double) ((double) $post['gia_bia'][$key] * (double) $kenh_phan_phoi['chiet_khau_xuong'] / 100); $gia_xuat = ceil((double) $post['gia_bia'][$key] - $loi_nhuan); } else { $loi_nhuan = (double) ((double) $post['gia_nhap'][$key] * (double) $kenh_phan_phoi['chiet_khau_len'] / 100); $gia_xuat = ceil((double) $post['gia_nhap'][$key] + $loi_nhuan); } $gia_xuat_moi = new GiaXuat(); $gia_xuat_moi->setIdSanPham($id_san_pham); $gia_xuat_moi->setIdKenhPhanPhoi($kenh_phan_phoi['id_kenh_phan_phoi']); $gia_xuat_moi->setGiaXuat($gia_xuat); $gia_xuat_table->saveGiaXuat($gia_xuat_moi); } } // kiểm tra bảng công nợ nhà cung cấp, nếu chưa tồn tại dòng công nợ nào của nhà cung cấp thì thêm vào, vì nếu ko có dòng công nợ đó thì phần ( thanh_toan => nhà cung cấp ) chạy sai $cong_no_nha_cung_cap_table = $this->getServiceLocator()->get('Application\\Model\\CongNoNhaCungCapTable'); $cong_no_exist = $cong_no_nha_cung_cap_table->getCongNoNhaCungCapByArrayConditionAndArrayColumn(array('id_nha_cung_cap' => $post['id_nha_cung_cap']), array('id_cong_no' => new Expression('max(id_cong_no)'))); if (!$cong_no_exist[0]['id_cong_no']) { $cong_no_moi = new CongNoNhaCungCap(); $cong_no_moi->setIdNhaCungCap($post['id_nha_cung_cap']); $date = date('Y-m-d h:i:s a', time()); $cong_no_moi->setKi($date); $cong_no_moi->setNoDauKi(0); $cong_no_moi->setNoPhatSinh(0); $cong_no_moi->setDuNo(0); $cong_no_nha_cung_cap_table->saveCongNo($cong_no_moi); } // lưu thành công $this->flashMessenger()->addSuccessMessage('Chúc mừng, nhập hàng thành công!'); return $this->redirect()->toRoute('hang_hoa', array('action' => 'in-phieu-nhap', 'id' => $id_phieu_nhap)); } else { $this->flashMessenger()->addErrorMessage('Lỗi, không tìm thấy sản phẩm cần nhập'); return $this->redirect()->toRoute('hang_hoa', array('action' => 'nhap-hang-hoa')); } } }