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')); } } }
public function savePhieuNhap(PhieuNhap $phieu_nhap) { $data = array('id_user' => $phieu_nhap->getIdUser(), 'id_nha_cung_cap' => $phieu_nhap->getIdNhaCungCap(), 'ma_phieu_nhap' => $phieu_nhap->getMaPhieuNhap(), 'ngay_nhap' => $phieu_nhap->getNgayNhap(), 'state' => $phieu_nhap->getState()); $id_phieu_nhap = (int) $phieu_nhap->getIdPhieuNhap(); if ($id_phieu_nhap == 0) { $this->tableGateway->insert($data); } else { if ($this->getPhieuNhapByArrayConditionAndArrayColumn(array('id_phieu_nhap' => $id_phieu_nhap), array('ma_phieu_nhap'))) { $this->tableGateway->update($data, array('id_phieu_nhap' => $id_phieu_nhap)); } else { return false; } } return true; }