PageRenderTime 27ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/clients/controller/frontend/CheckOut/CheckOut.php

https://gitlab.com/vanthanhhoh/devlovebook
PHP | 319 lines | 300 code | 9 blank | 10 comment | 57 complexity | c48b05a0e3ec870ecf45cae7fc30f625 MD5 | raw file
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: THANH
  5. * Date: 13/07/2016
  6. * Time: 11:28 SA
  7. */
  8. class CheckOut extends Controller {
  9. public function Main(){
  10. if(isset($_SESSION['cart'])){
  11. if(!empty($_SESSION['cart'])){
  12. if(USER_ID > 0 && !isset($_SESSION['ship'])){
  13. $this->checkShip();
  14. }
  15. elseif(USER_ID>0 && isset($_SESSION['ship'])){
  16. $this->checkPayment();
  17. }
  18. else {
  19. $this->checkLogin();
  20. }
  21. }
  22. else {
  23. header('Location:'.APP_DOMAIN);
  24. }
  25. }
  26. else {
  27. header('Location:'.APP_DOMAIN);
  28. }
  29. }
  30. public function checkLogin(){
  31. if(isset($_SESSION['cart'])){
  32. if(!empty($_SESSION['cart'])){
  33. $ship = 0;
  34. $p = frontend::$product;
  35. $Cupon = array();
  36. if(isset($_SESSION['cupon'])){
  37. if(!empty($_SESSION['cupon'])){
  38. $Cupon = $_SESSION['cupon'];
  39. }
  40. }
  41. if(Input::Post('login',0)==1){ // Checklogin
  42. $username = Input::Post('email','');
  43. $password = Input::Post('password','');
  44. if(G::$Authorized->CheckLogin($username, $password)){
  45. header('Location:'.'/CheckOut/Main');
  46. }
  47. }
  48. if(Input::Post('resgiter',0)==1){
  49. $User = array (
  50. 'username' => '',
  51. 'email' => '',
  52. 'phone' => '',
  53. 'fullname' => '',
  54. 'avatar' => '/clients/data/images/anonymous.png',
  55. 'birthday' => '',
  56. 'regdate'=>CURRENT_TIME,
  57. 'gender' => '1',
  58. 'status' => 1,
  59. 'password' => '',
  60. 'level' => '1'
  61. );
  62. $User['username']=Input::Post('email');
  63. $User['email']=Input::Post('email');
  64. $User['password']=Input::Post('password','');
  65. $User['phone'] = Input::Post('phone','');
  66. $User['fullname'] = Input::Post('fullname','');
  67. $User['birthday'] = Input::Post('birthday','');
  68. $User['gender'] = Input::Post('gender','');
  69. $User['birthday'] = Filter::DateToUnixTime($User['birthday'],0,0);
  70. if($UserID = Authorized::AddUser($User)) {
  71. G::$Authorized->CheckLogin($User['email'], $User['password']);
  72. header('Location:'.'/CheckOut/Main');
  73. }
  74. else{
  75. Helper::Notify('error','Đăng kí thất bại');
  76. }
  77. }
  78. Theme::Assign('step',1);
  79. $v = $this->View('check_login');
  80. $v = $v ->Assign('cart',$_SESSION['cart'])
  81. ->Assign('p',$p)
  82. ->Assign('Cupon',$Cupon)
  83. ->Assign('ship',$ship);
  84. Theme::SetTitle('Đăng nhập | lovebook.vn');
  85. Theme::jsPlugin('icheck');
  86. $this->Render($v->Output(),array('layout'=>'layout.checkout'));
  87. }
  88. else {
  89. header('Location:'.APP_DOMAIN);
  90. }
  91. }
  92. else {
  93. header('Location:'.APP_DOMAIN);
  94. }
  95. }
  96. public function checkShip(){
  97. $p = frontend::$product;
  98. $ship = 0;
  99. $Cupon = array();
  100. if(isset($_SESSION['cupon'])){
  101. if(!empty($_SESSION['cupon'])){
  102. $Cupon = $_SESSION['cupon'];
  103. }
  104. }
  105. Theme::SetTitle('Địa chỉ giao hàng');
  106. Theme::Assign('step',2);
  107. $province = getLocation('province');
  108. if(Input::Post('shipSubmit',0)==1){
  109. $_SESSION['ship']=Input::Post('ship');
  110. header('Location:'.'/CheckOut/Main');
  111. }
  112. $ship_address = array(
  113. 'name' => '',
  114. 'phone' => '',
  115. 'province' => '',
  116. 'district' => '',
  117. 'address' => '',
  118. 'note' => ''
  119. );
  120. $getshipAddres = DB::Query('shipaddress')->Where('user_id','=',USER_ID)->Get();
  121. if($getshipAddres->status && $getshipAddres->num_rows==1) {
  122. $ship_address = array_merge($ship_address,$getshipAddres->Result[0]);
  123. }
  124. if(isset($_SESSION['ship'])){
  125. $ship_address = array_merge($ship_address,$_SESSION['ship']);
  126. }
  127. $v = $this->View('check_ship')
  128. ->Assign('cart',$_SESSION['cart'])
  129. ->Assign('p',$p)
  130. ->Assign('Cupon',$Cupon)
  131. ->Assign('province',$province)
  132. ->Assign('ship_address',$ship_address)
  133. ->Assign('ship',$ship);
  134. $this->Render($v->Output(),array('layout'=>'layout.checkout'));
  135. }
  136. public function checkPayment(){
  137. $p = frontend::$product;
  138. Theme::SetTitle('Thông tin thanh toán | lovebook.vn');
  139. Theme::Assign('step',3);
  140. $shipMethod = DB::Query('ship')->Get()->Result;
  141. $paymentMethod = DB::Query('payment')->Get()->Result;
  142. $Cupon = array();
  143. if(isset($_SESSION['cupon'])){
  144. if(!empty($_SESSION['cupon'])){
  145. $Cupon = $_SESSION['cupon'];
  146. }
  147. }
  148. // Nếu ở hà nội default gói ship miễn phí nội thành
  149. if($_SESSION['ship']['province']==2){
  150. $ship_choose = 2;
  151. $ship= $shipMethod[1]['fee'];
  152. }
  153. else {
  154. $ship_choose = $shipMethod[0]['ship_id'];
  155. $ship = $shipMethod[0]['fee'];
  156. }
  157. //End
  158. //Change Shipmethod
  159. if(Input::Post('submitShip',0)==1){
  160. $ship_id = Input::Post('ship_id',1);
  161. foreach($shipMethod as $shipM){
  162. if($shipM['ship_id']==$ship_id) {
  163. $ship=$shipM['fee'];
  164. $ship_choose = $ship_id;
  165. }
  166. }
  167. }
  168. //End
  169. Theme::jsPlugin('icheck');
  170. if(Input::Post('finsh',0)==1){
  171. $invoice = $_SESSION['ship'];
  172. $invoice['ship'] = Input::Post('ship',0);
  173. $invoice['payment'] = Input::Post('payment',0);
  174. $moneySend =0;
  175. $orderID = array();
  176. $productID =array();
  177. foreach($_SESSION['cart'] as $item){
  178. $item['price_old'] = $p[$item['product_id']]['price'];
  179. $moneySend = $moneySend+$item['total'];
  180. $productID[]= $item['product_id'];
  181. $insertOrder = DB::Query('order')->Insert($item);
  182. if($insertOrder->status && $insertOrder->insert_id>0){
  183. $orderID[] = $insertOrder->insert_id;
  184. }
  185. }
  186. $discountCupon = 0;
  187. if(isset($_SESSION['cupon'])){
  188. if(!empty($_SESSION['cupon'])){
  189. $invoice['cupon']=$_SESSION['cupon']['code'];
  190. $invoice['cupon_discount']=$_SESSION['cupon']['discount'];
  191. $invoice['cupon_type']=$_SESSION['cupon']['discount_type'];
  192. if($_SESSION['cupon']['discount_type']==1){
  193. $discountCupon = $_SESSION['cupon']['discount'];
  194. }
  195. else {
  196. $discountCupon = $moneySend*$_SESSION['cupon']['discount']/100;
  197. $discountCupon = ceil($discountCupon);
  198. }
  199. }
  200. }
  201. $moneySend = $moneySend-$discountCupon; // Trừ giảm giá cupon
  202. $shipFee = DB::Query('ship')->Where('ship_id','=',$invoice['ship'])->Get();
  203. if($shipFee->status && $shipFee->num_rows==1){
  204. $moneySend = $moneySend+$shipFee->Result[0]['fee']; // Cộng thêm phí ship
  205. }
  206. $invoice['total'] = $moneySend;
  207. $invoice['order_id'] = implode(',',$orderID);
  208. $invoice['product_id'] = implode(',',$productID);
  209. $invoice['invoice_status']= 1;
  210. $invoice['add_time']=CURRENT_TIME;
  211. $invoice['edit_time']=CURRENT_TIME;
  212. $invoice['user_id']= USER_ID;
  213. $invoice['code']= 'HD'.getNextNumber();
  214. $insertInvoice = DB::Query('invoice')->Insert($invoice);
  215. if($insertInvoice->status && $insertInvoice->insert_id>0){
  216. foreach($orderID as $ID){
  217. DB::Query('order')->Where('order_id','=',$ID)->Update(array('invoice_id'=>$insertInvoice->insert_id));
  218. }
  219. unset($_SESSION['cart']);unset($_SESSION['ship']);unset($_SESSION['cupon']);
  220. $paymentModule = DB::Query('payment')->Where('payment_id','=',$invoice['payment'])->Get();
  221. if($paymentModule->status && $paymentModule->num_rows==1)
  222. {
  223. $paymentModule = $paymentModule->Result[0];
  224. if($paymentModule['online']==1){
  225. $paymentModule = $paymentModule['module'];
  226. if($paymentModule!=''){
  227. header('Location: /paymentModule/'.$paymentModule.'?total='.$moneySend.'&invoice='.$insertInvoice->insert_id.'&bankcode='.Input::Post('bankcode','VCB'));
  228. }
  229. }
  230. else {
  231. header('Location: /CheckOut/Success');
  232. }
  233. }
  234. }
  235. }
  236. $province = getLocation('province');
  237. $district = getLocation('district');
  238. $v = $this->View('check_payment');
  239. $v = $v->Assign('shipMethod',$shipMethod)
  240. ->Assign('payment',$paymentMethod)
  241. ->Assign('cart',$_SESSION['cart'])
  242. ->Assign('ship_address',$_SESSION['ship'])
  243. ->Assign('province',$province)
  244. ->Assign('district',$district)
  245. ->Assign('p',$p)
  246. ->Assign('ship',$ship)
  247. ->Assign('Cupon',$Cupon)
  248. ->Assign('ship_choose',$ship_choose);
  249. $this->Render($v->Output(),array('layout'=>'layout.checkout'));
  250. }
  251. public function rePay(){
  252. if(isset(G::$Registry['Params'][ROUTER_EXTRA_KEY])) {
  253. $id = G::$Registry['Params'][ROUTER_EXTRA_KEY];
  254. $invoice = DB::Query('invoice')->Where('invoice_id','=',$id)->Get();
  255. if($invoice->status && $invoice->num_rows==1){
  256. $invoice = $invoice->Result[0];
  257. $order = DB::Query('order')->Where('order_id','INCLUDE',$invoice['order_id'])->Get()->Result;
  258. $shipMethod = DB::Query('ship')->Get()->Result;
  259. $paymentMethod = DB::Query('payment')->Get()->Result;
  260. $ship_choose = $invoice['ship'];
  261. if(Input::Post('submitShip',0)==1){
  262. $ship_id = Input::Post('ship_id',1);
  263. foreach($shipMethod as $shipM){
  264. if($shipM['ship_id']==$ship_id) {
  265. $ship=$shipM['fee'];
  266. $ship_choose = $ship_id;
  267. }
  268. }
  269. }
  270. else $ship = DB::Query('ship')->Where('ship_id','=',$invoice['ship'])->Get()->Result[0]['fee'];
  271. $p = frontend::$product;
  272. Theme::jsPlugin('icheck');
  273. Theme::SetTitle('Đơn hàng của tôi | lovebook.vn');
  274. Theme::Assign('step',3);
  275. if(Input::Post('finsh',0)==1){
  276. $invoice['ship'] = Input::Post('ship',1);
  277. $invoice['payment'] = Input::Post('payment',1);
  278. $invoice['edit_time']= CURRENT_TIME;
  279. $update = DB::Query('invoice')->Where('invoice_id','=',$id)->Update($invoice);
  280. if($update->status){
  281. $paymentModule = DB::Query('payment')->Where('payment_id','=',$invoice['payment'])->Get();
  282. if($paymentModule->status && $paymentModule->num_rows==1){
  283. $paymentModule = $paymentModule->Result[0]['module'];
  284. if($paymentModule!=''){
  285. header('Location: /paymentModule/'.$paymentModule.'?invoice='.$id.'&bankcode='.Input::Post('bankcode','VCB'));
  286. }
  287. }
  288. }
  289. }
  290. $v = $this->View('repay');
  291. $v = $v->Assign('cart',$order)
  292. ->Assign('shipMethod',$shipMethod)
  293. ->Assign('invoice',$invoice)->Assign('p',$p)
  294. ->Assign('province',getLocation('province'))
  295. ->Assign('district',getLocation('district'))
  296. ->Assign('ship',$ship)
  297. ->Assign('ship_choose',$ship_choose)
  298. ->Assign('payment',$paymentMethod);
  299. $this->Render($v->Output(),array('layout'=>'layout.checkout'));
  300. }
  301. }
  302. }
  303. public function Failed(){
  304. }
  305. public function Success(){
  306. Theme::SetTitle('Thanh toán thành công!');
  307. $v= $this->View('success');
  308. $this->Render($v->Output());
  309. }
  310. }