Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component } from '@angular/core';
- import { ModalController, NavController, NavParams, ToastController, ActionSheetController, AlertController, ViewController } from 'ionic-angular';
- import { MyApp } from '../../app/app.component';
- import { ProductItemDetail } from '../product-item-detail/product-item-detail';
- import { ItemPopupAddress} from '../item-popup-address/item-popup-address';
- import { CartPage } from '../cart/cart';
- import { OrderDbHelper } from '../../providers/order-db-helper';
- import { HttpService } from '../../providers/http-service';
- import { Session } from '../../providers/session';
- import { TranslateService } from '@ngx-translate/core';
- /**
- * Generated class for the ItemReviewOrderPage page.
- *
- * See https://ionicframework.com/docs/components/#navigation for more info on
- * Ionic pages and navigation.
- */
- @Component({
- selector: 'page-item-review-order',
- templateUrl: 'item-review-order.html',
- })
- export class ItemReviewOrder {
- public nav;
- public navp;
- public buy;
- public pageTitle : string = "";
- public img : string = "";
- public itemId : string = "";
- public itemIdShop : string = "";
- public price : string = "";
- public seller : string = "";
- public sellerLocation : string = "";
- public category : string = "";
- public minimum : string = "";
- public stock = "";
- public description = "";
- public condition = "";
- public note;
- public total_price;
- public qty;
- public qty_zero = false;
- public example : string = "Example white Color/XL Size/2nd Edition";
- public jsonData;
- public chosenAddress;
- public chosenAgency;
- public list_item;
- public itemBuy;
- public image : string;
- public name : string;
- public show_image : boolean = false;
- public itemBuy_final = [];
- public static chosenAddress;
- public static TAG : string = "Item Review Order";
- public static itemId = "";
- public static itemIdShop = "";
- public static img = "";
- public static pageTitle = "";
- public static price = "";
- public static seller = "";
- public static sellerLocation = "";
- public static category = "";
- public static minimum = "";
- public static stock = "";
- public static description = "";
- public static condition = "";
- public static totalPrice = "";
- public static qty = "";
- public static note = "";
- public estimatedPrice;
- public estimatedQty;
- public btnEstPrcTrigger;
- public currentNumber = 0;
- public cart;
- public itemOrder;
- public itemProduct;
- public chooseAgency;
- public setLang;
- public imglink;
- public translate;
- public shippingAgency;
- public shippingAgencyFinal;
- public addressList;
- public addressListFinal;
- public isDataLoad: boolean = false;
- public showChosenAddress;
- constructor(
- public navCtrl: NavController,
- public navParams: NavParams,
- public toast: ToastController,
- public httpServ: HttpService,
- public session: Session,
- public modal: ModalController,
- public orderDB: OrderDbHelper,
- public alertCtrl: AlertController,
- public actionSheet: ActionSheetController,
- public translateSvc: TranslateService,
- public viewCtrl: ViewController
- )
- {
- try {
- this.navp = navParams;
- this.nav = navCtrl;
- this.translate = translateSvc;
- this.chooseAgency = {};
- this.itemOrder = {};
- this.shippingAgency = [];
- this.shippingAgencyFinal = [];
- this.addressList = [];
- this.addressListFinal = [];
- this.chosenAddress = [];
- this.chosenAgency = [];
- this.qty_zero = true;
- this.showChosenAddress = false;
- this.chosenAddress = ItemReviewOrder.chosenAddress;
- this.pageTitle = ItemReviewOrder.pageTitle;
- this.img = ItemReviewOrder.img;
- this.itemId = ItemReviewOrder.itemId;
- this.itemIdShop = ItemReviewOrder.itemIdShop;
- this.price = ItemReviewOrder.price;
- this.seller = ItemReviewOrder.seller;
- this.sellerLocation = ItemReviewOrder.sellerLocation;
- this.stock = ItemReviewOrder.stock;
- this.description = ItemReviewOrder.description;
- this.condition = ItemReviewOrder.condition;
- this.category = ItemReviewOrder.category;
- this.qty = ItemReviewOrder.qty;
- this.note = ItemReviewOrder.note;
- this.total_price = ItemReviewOrder.totalPrice
- this.itemBuy = {
- name: this.pageTitle,
- img: this.img,
- itemId: this.itemId,
- itemIdShop: this.itemIdShop,
- price: this.price,
- seller: this.seller,
- sellerLocation: this.sellerLocation,
- stock: this.stock,
- description: this.description,
- condition: this.condition,
- category: this.category,
- total_price: this.total_price,
- qty: this.qty,
- note: this.note
- }
- this.itemBuy_final.push(this.itemBuy);
- this.loadItem();
- this.totalEstPrice();
- this.loadShipping();
- this.loadShippingAgency();
- // this.loadAddress();
- } catch (error) {
- MyApp.consoleLog("Some error happen", error);
- }
- }
- ionViewDidLoad() {
- try {
- this.loadChooseAddress();
- this.showAddress();
- MyApp.consoleLog(ItemReviewOrder.TAG, "chosen Address", this.chosenAddress);
- MyApp.consoleLog(ItemReviewOrder.TAG, "flag choosen", this.showChosenAddress);
- MyApp.consoleLog(ItemReviewOrder.TAG, "loadshipping Item User", this.addressListFinal);
- } catch (error) {
- MyApp.consoleLog("Some error happen", error);
- }
- console.log('ionViewDidLoad ItemReviewOrderPage');
- MyApp.consoleLog(ItemReviewOrder.TAG, "item list: ", this.list_item);
- MyApp.consoleLog(ItemReviewOrder.TAG, "item buy: ", this.itemBuy_final);
- }
- ionViewWillLoad(){
- }
- ionViewWillUnload() {
- this.list_item = [];
- // this.addressListFinal = [];
- // this.shippingAgencyFinal = [];
- }
- ionViewWillLeave(){
- }
- zeroQtyToast() {
- const toast = this.toast.create({
- message: 'Your item qty is 0',
- duration: 500,
- position: 'bottom'
- });
- toast.present();
- }
- minus(mdata) {
- MyApp.consoleLog(ItemReviewOrder.TAG, "Minus button pressed!");
- if (mdata.qty == 0){
- MyApp.consoleLog(ItemReviewOrder.TAG, "Qty is 0");
- // this.zeroQtyToast();
- }
- else {
- mdata.qty--;
- mdata.total_price = mdata.qty * mdata.price;
- this.orderCart(mdata);
- MyApp.consoleLog(ItemReviewOrder.TAG, mdata);
- this.totalEstPrice();
- }
- }
- plus(mdata) {
- MyApp.consoleLog(ItemReviewOrder.TAG, "Plus button pressed!");
- this.qty_zero = false;
- mdata.qty++;
- mdata.total_price = mdata.qty * mdata.price;
- this.orderCart(mdata);
- MyApp.consoleLog(ItemReviewOrder.TAG, mdata);
- this.totalEstPrice();
- }
- deleteOrderCart(mdata, cartdata) {
- MyApp.consoleLog(ItemReviewOrder.TAG + "Delete mdata id " , mdata);
- let temp = [];
- // let tempOrder2 = this.cart;
- for (let i in cartdata) {
- if (mdata.id != cartdata[i].id) {
- temp.push(cartdata[i]);
- }
- }
- // cart = tempOrder;
- return temp;
- }
- orderCart(mdata) {
- MyApp.consoleLog(ItemReviewOrder.TAG + "orderCart function ", mdata);
- this.cart = ProductItemDetail.chosen_item;
- MyApp.consoleLog(ItemReviewOrder.TAG + "Data masuk ke Cart", this.cart);
- // this.cart.push(mdata);
- let exist = false;
- let del = false;
- // let itemIndex = 0;
- for (let i in this.cart) {
- MyApp.consoleLog(ItemReviewOrder.TAG + "Isi data i", i);
- if (this.cart[i].id == mdata.id) {
- if (this.cart[i].qty == 0) {
- // delete this.cart[i];
- let tempOrder = this.deleteOrderCart(mdata, this.cart);
- MyApp.consoleLog(ItemReviewOrder.TAG + "TempOrder =", tempOrder);
- del = true;
- this.cart = tempOrder;
- break;
- }
- else {
- exist = true;
- this.cart[i]=mdata;
- MyApp.consoleLog(ItemReviewOrder.TAG + "setIndex cart", this.cart);
- // itemIndex++;
- }
- }
- }
- if (exist === false && del === false) {
- MyApp.consoleLog(ItemReviewOrder.TAG + "add data", this.cart);
- this.cart.push(mdata);
- }
- let temp = [];
- for (let i in this.cart) {
- if (this.cart[i].qty != 0) {
- temp.push(this.cart[i]);
- }
- }
- this.cart = temp;
- ProductItemDetail.chosen_item = this.cart;
- MyApp.consoleLog(ItemReviewOrder.TAG + "Data masuk ke Cart 2", ProductItemDetail.chosen_item);
- }
- loadItem() {
- MyApp.consoleLog(ItemReviewOrder.TAG, "XfoodChooseOrder Load Menu 2");
- this.buildListItem(this.itemBuy_final);
- MyApp.consoleLog(ItemReviewOrder.TAG, this.list_item);
- }
- buildListItem(params){
- let list_item;
- list_item = params;
- for(let i = 0; i < params.length; i++){
- if(this.setLang == 'en'){
- list_item[i].itemId = params[i].itemId,
- list_item[i].itemIdShop = params[i].itemIdShop,
- list_item[i].img = params[i].img,
- list_item[i].note = params[i].note,
- list_item[i].price = params[i].price,
- list_item[i].shown_price = params[i].shown_price,
- list_item[i].total_price = params[i].total_price,
- list_item[i].qty = params[i].qty,
- list_item[i].name = params[i].name,
- list_item[i].description = params[i].description
- }
- if(params[i].img == ""){
- list_item[i].imgLink = "assets/img/xfood_default_menu_image.jpg"
- } else {
- list_item[i].imgLink = this.imglink + params[i].img;
- }
- }
- this.list_item = list_item;
- }
- // loadAddress(){
- // this.buildListAddress(ItemReviewOrder.choosenAddress);
- // MyApp.consoleLog(ItemReviewOrder.TAG, "load choosen address", ItemReviewOrder.choosenAddress);
- // }
- // buildListAddress(params){
- // let list_address;
- // list_address = params;
- // for(let i = 0; i < params.length; i++){
- // list_address[i].addressId = params[i].id,
- // list_address[i].id_user = params[i].id_user,
- // list_address[i].name = params[i].name,
- // list_address[i].address = params[i].address,
- // list_address[i].province = params[i].province,
- // list_address[i].postalCode = params[i].postalCode,
- // list_address[i].pickupLocation = params[i].pickupLocation
- // }
- // this.choosenAddress = list_address[i];
- // MyApp.consoleLog(ItemReviewOrder.TAG, "choosen Address data", this.choosenAddressFinal);
- // }
- totalEstPrice(){
- let sum_est = ProductItemDetail.chosen_item;
- MyApp.consoleLog(ItemReviewOrder.TAG + " isi sum_est ", sum_est);
- let pricetotal = sum_est.map(i => i.total_price);
- let sums = pricetotal.reduce((a, b) => a + b, 0);
- let qtytotal = sum_est.map(i => i.qty);
- let qtysums = qtytotal.reduce((a,b) => a + b, 0);
- this.estimatedPrice = sums;
- this.estimatedQty = qtysums;
- ProductItemDetail.estimatedPrice = sums;
- ProductItemDetail.estimatedQty = qtysums;
- MyApp.consoleLog(ItemReviewOrder.TAG + " is estimated price ", this.estimatedPrice);
- if ( this.estimatedPrice != 0) {
- this.btnEstPrcTrigger = true;
- }
- else {
- this.btnEstPrcTrigger = false;
- MyApp.consoleLog(ItemReviewOrder.TAG + " status btn ", this.btnEstPrcTrigger);
- }
- }
- gotoCart(){
- MyApp.consoleLog(ItemReviewOrder.TAG, "Go to Cart");
- //CartPage.list_order = this.cart;
- this.nav.push(CartPage);
- }
- //load address user for shipping destination
- loadShipping(){
- let link = "http://192.168.0.111/phpscring/address_setting/load_address/";
- let user;
- user = {};
- user.id = this.session.getUserID();
- let postParams = {
- "link": link,
- "data": user
- }
- this.httpServ.postService(postParams).then((data)=> {
- let response;
- response ={};
- response.data = data;
- MyApp.consoleLog(ItemReviewOrder.TAG, "data", data);
- MyApp.consoleLog(ItemReviewOrder.TAG, "response", response);
- if(response.data != 0){
- for (let i = 0; i < response.data.length; i++) {
- this.addressList[i] = {
- id: response.data[i].id,
- id_user: response.data[i].user_id,
- name: response.data[i].name,
- address: response.data[i].address,
- province: response.data[i].province,
- postalCode: response.data[i].postalCode,
- pickupLocation: response.data[i].pickupLocation
- };
- this.addressListFinal.push(this.addressList[i]);
- }
- MyApp.consoleLog(ItemReviewOrder.TAG, "address list", this.addressListFinal);
- }
- }, (error) => {
- MyApp.consoleError(ItemReviewOrder.TAG + "Couldn't connect to server", error);
- });
- }
- //load Shipping Agency for service Delivery
- loadShippingAgency(){
- let link = "http://192.168.0.111/phpscring/open_shop/load_shipping_agency/";
- let shop;
- shop = {};
- shop.id = this.itemIdShop;
- let postParams = {
- "link": link,
- "data": shop
- }
- MyApp.consoleLog(ItemReviewOrder.TAG, "shop id", shop);
- this.httpServ.postService(postParams).then((data)=> {
- let response;
- response ={};
- response.data = data;
- MyApp.consoleLog(ItemReviewOrder.TAG, "data", data);
- MyApp.consoleLog(ItemReviewOrder.TAG, "response", response);
- if(response.data != 0){
- for (let i = 0; i < response.data.length; i++) {
- this.shippingAgency[i] = {
- id: response.data[i].id,
- id_shop: response.data[i].id_shop,
- nameAgency: response.data[i].name_delivery
- };
- this.shippingAgencyFinal.push(this.shippingAgency[i]);
- }
- MyApp.consoleLog(ItemReviewOrder.TAG, "shippingAgency", this.shippingAgencyFinal);
- }
- }, (error) => {
- MyApp.consoleError(ItemReviewOrder.TAG + "Couldn't connect to server", error);
- });
- }
- setItemNote() {
- MyApp.consoleLog(ItemReviewOrder.TAG + " Data Order untuk dijadikan Note ", this.cart);
- // this.addNotes["notes"]
- this.itemProduct = this.cart;
- MyApp.consoleLog(ItemReviewOrder.TAG + " value xfoodNotes ", this.itemProduct);
- this.itemOrder.list_order = this.itemProduct;
- this.itemOrder.total = this.estimatedPrice;
- // this.XfoodOrder.total = XfoodReviewOrder.subtotalItem;
- // this.session.setXfoodNote(this.XfoodOrder);
- let itemNote;
- let colSize = {
- name : 6,
- price : 7,
- qty : 5,
- total_price : 12
- };
- let maxColSize = {
- name : 20,
- price : 20,
- qty : 20,
- total_price : 20
- };
- for (let i = 0; i < this.itemProduct.length; i++) {
- let name: string = this.itemProduct[i].name;
- let price: string = this.itemProduct[i].price.toString();
- let qty: string = this.itemProduct[i].qty.toString();
- let total_price: string = this.itemProduct[i].total_price.toString();
- if (name.length + 2 >= colSize.name) {
- if (name.length + 2>= maxColSize.name) {
- colSize.name = maxColSize.name;
- }
- else {
- colSize.name = name.length + 2;
- }
- }
- if (price.length + 2 >= colSize.price) {
- if (price.length + 2 >= maxColSize.price) {
- colSize.price = maxColSize.price;
- }
- else {
- colSize.price = price.length + 2;
- }
- }
- if (qty.length + 2 >= colSize.qty) {
- if (qty.length + 2 >= maxColSize.qty) {
- colSize.qty = maxColSize.qty;
- }
- else {
- colSize.qty = qty.length + 2;
- }
- }
- if (total_price.length + 2 >= colSize.total_price) {
- if (total_price.length + 2 >= maxColSize.total_price) {
- colSize.total_price = maxColSize.total_price;
- }
- else {
- colSize.total_price = total_price.length + 2;
- }
- }
- }
- itemNote = this.category;
- itemNote += "\n";
- itemNote += "\n";
- itemNote += "*****************************";
- itemNote += "\n";
- itemNote += this.getBeautyPrint("Qty", colSize.qty);
- itemNote += this.getBeautyPrint("Name", colSize.name);
- itemNote += this.getBeautyPrint("Price", colSize.price);
- itemNote += this.getBeautyPrint("Total Price", colSize.total_price);
- for (let i = 0; i < this.itemProduct.length; i++) {
- let foodname: string = this.itemProduct[i].name;
- let price: string = this.itemProduct[i].price.toString();
- let qty: string = this.itemProduct[i].qty.toString();
- let total_price: string = this.itemProduct[i].total_price.toString();
- let note: string = this.itemProduct[i].note.toString();
- MyApp.consoleLog (ItemReviewOrder.TAG, " test beautyPrint " + "\n" + qty +" pcs" + " " + name + " " +"KHR "+ price + " " +"KHR "+ total_price);
- itemNote += "\n";
- itemNote += this.getBeautyPrint(qty, colSize.qty);
- itemNote += this.getBeautyPrint(foodname, colSize.name);
- itemNote += this.getBeautyPrint(price, colSize.price);
- itemNote += this.getBeautyPrint(total_price, colSize.total_price);
- if(note.length != 0 ){
- itemNote += "\n";
- itemNote += "*NB: " + note;
- itemNote += "\n";
- }
- }
- itemNote += "\n";
- itemNote += "*****************************";
- if(ItemReviewOrder.note.length != 0 ){
- itemNote += "\n";
- itemNote += "\n";
- itemNote += "Notes :";
- itemNote += "\n";
- itemNote += ItemReviewOrder.note;
- }
- itemNote += "\n";
- itemNote += "\n";
- itemNote += "Estimate Price :" + "RP " + this.estimatedPrice;
- MyApp.consoleLog(ItemReviewOrder.TAG, " isi itemNote " + "\n" + itemNote);
- this.itemOrder.category = this.category;
- // this.XfoodOrder.restoaddress = this.restoaddress;
- // this.XfoodOrder.restoPhone = this.restophone;
- // this.XfoodOrder.restoPhone2 = this.restophone2;
- this.itemOrder.notes = ItemReviewOrder.note;
- // this.jsonStartLocation.Note = XfoodNote;
- // this.jsonStartLocation.XfoodNote = this.XfoodOrder;
- this.session.setStartNote(itemNote);
- }
- getBeautyPrint(val, colSize){
- let beautyText = val;
- for (let i = val.length; i < colSize; i++) {
- beautyText += " ";
- }
- MyApp.consoleLog(ItemReviewOrder.TAG, beautyText + "INI Value ColSize " + colSize);
- return beautyText;
- }
- loadChooseAddress(){
- this.EditAddress(this.addressListFinal);
- }
- EditAddress(params){
- ItemPopupAddress.addressListFinal = params;
- // ItemPopupAddress.addressId = params.id;
- // ItemPopupAddress.addressUserId = params.id_user;
- // ItemPopupAddress.address = params.address;
- // ItemPopupAddress.addressName = params.name;
- // ItemPopupAddress.province = params.province;
- // ItemPopupAddress.postalCode = params.postalCode;
- // ItemPopupAddress.pickupLocation = params.pickupLocation;
- }
- gotoEditAddress(params){
- this.nav.push(ItemPopupAddress).then(()=>{
- // let modalAddress = this.modal.create(ItemPopupAddress);
- // modalAddress.present();
- const index = this.viewCtrl.index;
- this.navCtrl.remove(index);
- });
- // this.nav.push(ItemPopupAddress);
- }
- showImage(img, name){
- this.image = img;
- this.name = name;
- this.show_image = true;
- }
- close(){
- this.show_image = false;
- }
- confirmOrder(cart){
- this.jsonData = {}
- this.jsonData.product = cart.itemId;
- this.jsonData.user = this.session.getUserID();
- this.jsonData.quantity = cart.qty;
- this.jsonData.timestamp = Math.round(new Date().getTime()/1000);
- this.jsonData.note = cart.note;
- this.jsonData.address = this.chosenAddress.id;
- this.jsonData.shippingAgency = this.chosenAgency.id;
- MyApp.consoleLog(ItemReviewOrder.TAG, "cartOrder", this.cart);
- MyApp.consoleLog(ItemReviewOrder.TAG, "orderData", this.jsonData);
- let orders = {
- 'PRODUCT': this.jsonData.product,
- 'USER': this.jsonData.user,
- 'TIMESTAMP': this.jsonData.timestamp,
- 'QUANTITY': this.jsonData.quantity,
- 'NOTE': this.jsonData.note,
- 'ADDRESS': this.jsonData.address,
- 'AGENCY': this.jsonData.shippingAgency,
- }
- let lastID = this.session.getLastTx();
- lastID++;
- this.session.setLastTx(lastID);
- this.orderDB.saveOrdersMulti(lastID,orders).then((result) => {
- this.session.setXfoodNoteMulti(lastID,this.itemOrder);
- this.session.setLastTx(lastID);
- MyApp.consoleLog(ItemReviewOrder.TAG + " SAVE ORDER", result);
- MyApp.consoleLog(ItemReviewOrder.TAG + " SAVE ORDER", true);
- }, error => {
- MyApp.consoleError(ItemReviewOrder.TAG + " Save Order", error);
- });
- this.popPage();
- }
- popPage() {
- this.nav.remove().then(()=>{
- this.nav.pop(ProductItemDetail);
- });
- }
- showAddress(){
- if (typeof this.chosenAddress == 'undefined') {
- this.showChosenAddress = false;
- }
- else {
- this.showChosenAddress = true;
- }
- }
- agencyClicked(val){
- this.chooseAgency.selectedValue = val;
- this.chosenAgency = this.chooseAgency.selectedValue;
- MyApp.consoleLog(ItemReviewOrder.TAG, "selected value", this.chosenAgency)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement