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 saveKenhPhanPhoi(KenhPhanPhoi $kenh_phan_phoi)
 {
     $data = array('id_kho' => $kenh_phan_phoi->getIdKho(), 'kenh_phan_phoi' => $kenh_phan_phoi->getKenhPhanPhoi(), 'chiet_khau_xuong' => $kenh_phan_phoi->getChietKhauXuong(), 'chiet_khau_len' => $kenh_phan_phoi->getChietKhauLen());
     $id_kenh_phan_phoi = (int) $kenh_phan_phoi->getIdKenhPhanPhoi();
     if ($id_kenh_phan_phoi == 0) {
         $this->tableGateway->insert($data);
     } else {
         if ($this->getKenhPhanPhoiByArrayConditionAndArrayColumn(array('id_kenh_phan_phoi' => $id_kenh_phan_phoi), array('kenh_phan_phoi'))) {
             $this->tableGateway->update($data, array('id_kenh_phan_phoi' => $id_kenh_phan_phoi));
         } else {
             return false;
         }
     }
     return true;
 }
 public function taoChiNhanhAction()
 {
     $form = $this->getServiceLocator()->get('Application\\Form\\TaoChiNhanhForm');
     $return = array('form' => $form);
     $request = $this->getRequest();
     if ($request->isPost()) {
         $post = $request->getPost();
         $form->setData($post);
         if ($form->isValid()) {
             $kho_table = $this->getServiceLocator()->get('Application\\Model\\KhoTable');
             $kho = $kho_table->getKhoByArrayConditionAndArrayColumn(array('dia_chi_kho' => $post['dia_chi_kho']), array('id_kho'));
             if ($kho) {
                 $this->flashMessenger()->addErrorMessage('Địa chỉ kho đã tồn tại');
                 return $this->redirect()->toRoute('chi_nhanh');
             }
             $kho_moi = new Kho();
             $kho_moi->exchangeArray($post);
             $kho_table->saveKho($kho_moi);
             $id_kho_moi = $kho_table->getKhoByArrayConditionAndArrayColumn(array('dia_chi_kho' => $post['dia_chi_kho']), array('id_kho'));
             $id_kho_moi = $id_kho_moi[0]['id_kho'];
             // tạo kênh phân phối
             $id_kho = $this->AuthService()->getIdKho();
             $kenh_phan_phoi_table = $this->getServiceLocator()->get('Application\\Model\\KenhPhanPhoiTable');
             $danh_sach_kenh_phan_phoi = $kenh_phan_phoi_table->getKenhPhanPhoiByIdKho($id_kho);
             foreach ($danh_sach_kenh_phan_phoi as $key => $value) {
                 $kenh_phan_phoi_moi = new KenhPhanPhoi();
                 $kenh_phan_phoi_moi->setIdKho($id_kho_moi);
                 $kenh_phan_phoi_moi->setKenhPhanPhoi($value);
                 $kenh_phan_phoi_moi->setChietKhauXuong(0);
                 $kenh_phan_phoi_moi->setChietKhauLen(0);
                 $kenh_phan_phoi_table->saveKenhPhanPhoi($kenh_phan_phoi_moi);
             }
             $this->flashMessenger()->addSuccessMessage('Chúc mừng, tạo chi nhánh mới thành công');
             return $this->redirect()->toRoute('chi_nhanh');
         } else {
             $return['form'] = $form;
             return $return;
         }
     } else {
         return $return;
     }
 }