Browse Source

generic action implemented in order page

robert2206 8 years ago
parent
commit
ce6b455a58
2 changed files with 183 additions and 28 deletions
  1. 19 17
      src/pages/order/order.html
  2. 164 11
      src/pages/order/order.ts

+ 19 - 17
src/pages/order/order.html

@@ -20,28 +20,30 @@
             Productos
         </ion-item-divider>
         <ion-list>
-            <ion-item *ngFor="let item of getCart()" (doubleTap)="openOptions()">
+            <ion-item *ngFor="let item of getCart()" >
                 <ion-avatar item-left>
                     <img src="./assets/images/product.png" />
 			    </ion-avatar>
-                <h2>{{ item.name }}</h2>
-                <p>
-                    <strong>Precio:</strong>
-                    {{ item.price }}
-                </p>
-                <p>
-                    <strong>Cantidad:</strong>
-                    {{ item.qty }}:
-                </p>
-                <p>
-                    <strong>Subtotal:</strong>
-                    {{ item.subtotal }}
-                </p>
+                <div (doubleTap)="openOptions(item)">
+                    <h2>{{ item.name }}</h2>
+                    <p>
+                        <strong>Precio:</strong>
+                        {{ item.price }}
+                    </p>
+                    <p>
+                        <strong>Cantidad:</strong>
+                        {{ item.qty }}
+                    </p>
+                    <p>
+                        <strong>Subtotal:</strong>
+                        {{ item.subtotal }}
+                    </p>
+                </div>
                 <div item-right>
-				    <button ion-button icon-only color="light">
+				    <button ion-button icon-only color="light" (click)="updateQuantity(item, 1)">
 					    <i class="fa fa-plus"></i>
 				    </button>
-				    <button ion-button icon-only color="light">
+				    <button ion-button icon-only color="light" (click)="updateQuantity(item, -1)">
 					    <i class="fa fa-minus"></i>
 				    </button>
 			    </div>
@@ -51,7 +53,7 @@
             Total
         </ion-item-divider>
         <ion-item>
-            <ion-input type="number" value="{{ getTotal() }}" readonly></ion-input>
+            <ion-input type="number" value="{{ updateTotal() }}" readonly></ion-input>
         </ion-item>
     </form>
 

+ 164 - 11
src/pages/order/order.ts

@@ -1,7 +1,8 @@
 import {  Component } from '@angular/core';
-import { NavController, NavParams } from 'ionic-angular';
+import { NavController, NavParams, ActionSheetController, AlertController } from 'ionic-angular';
 
 import { SaleOrder } from "../../odoo/models/sale.order";
+import { ProductProduct } from "../../odoo/models/product.product";
 // import { SaleOrderLine } from "../../odoo/models/sale.order.line";
 import { BaseDetailsView } from "../../base/base-details-view";
 
@@ -16,25 +17,29 @@ export class OrderPage extends BaseDetailsView<SaleOrder> {
 
     cart = [
         {
+            id: 1,
             name: "Producto 001",
             price: 2000,
             qty: 1,
             subtotal: 2000
         },
         {
-            name: "Producto 001",
+            id: 2,
+            name: "Producto 002",
             price: 2000,
             qty: 1,
             subtotal: 2000
         },
         {
-            name: "Producto 001",
+            id: 3,
+            name: "Producto 003",
             price: 2000,
             qty: 1,
             subtotal: 2000
         },
         {
-            name: "Producto 001",
+            id: 4,
+            name: "Producto 004",
             price: 2000,
             qty: 1,
             subtotal: 2000
@@ -43,14 +48,16 @@ export class OrderPage extends BaseDetailsView<SaleOrder> {
 
     constructor(
         public navCtrl: NavController,
-        public navParams: NavParams
+        public navParams: NavParams,
+        public actionSheetCtrl: ActionSheetController,
+        public alertCtrl: AlertController
     ) { 
         super(SaleOrder, navParams.data.item, navParams.data.kind);
 
         let titleKind = super.getKind() === "budget" ? "Presupuesto" : "Venta";
         super.setTitle(super.itemExists() ? "Editar " + titleKind : "Crear " + titleKind);
 
-        this.getTotal();
+        this.updateTotal();
     }
 
     /**
@@ -67,11 +74,46 @@ export class OrderPage extends BaseDetailsView<SaleOrder> {
         this.navCtrl.push(CustomerSelectionPage);
     }
 
+    /**
+     * 
+     */
     getCart(): Array<any> {
         return this.cart;
     }
 
-    getTotal(): number {
+    /**
+     * 
+     * @param item 
+     * @param quantity 
+     */
+    updateQuantity(item: any, quantity: number) {
+        if ((item.qty + quantity) <= 0) {
+            this.askIfRemove(item);
+            return;
+        }
+
+        item.qty = item.qty + quantity;
+        item.subtotal = item.price * item.qty;
+
+        this.updateTotal();
+    }
+
+    /**
+     * 
+     * @param item 
+     * @param price 
+     */
+    updatePrice(item: any, price: number) {
+        item.price = price;
+        item.subtotal = item.price * item.qty;
+
+        this.updateTotal();
+    }
+
+    /**
+     * 
+     */
+    updateTotal(): number {
         let total: number = 0;
 
         for (let i = 0; i < this.getCart().length; i++) {
@@ -85,7 +127,6 @@ export class OrderPage extends BaseDetailsView<SaleOrder> {
      * 
      */
     openBarcodeReader(): void {
-
     }
 
     /**
@@ -98,9 +139,121 @@ export class OrderPage extends BaseDetailsView<SaleOrder> {
     /**
      * 
      */
-    openOptions(): void {
-        console.log("options");
-        
+    openOptions(item: any): void {
+        this.actionSheetCtrl.create({
+            title: "Opciones",
+            buttons: [
+                {
+                    text: "Aplicar descuento",
+                    icon: "calculator",
+                    handler: () => {
+                        this.showDiscount(item);
+                    }
+                },
+                {
+                    text: "Agregar cantidad",
+                    icon: "cart",
+                    handler: () => {
+                        this.showQuantity(item);
+                    }
+                },
+                {
+                    text: "Borrar",
+                    icon: "remove",
+                    handler: () => {
+                        this.askIfRemove(item);
+                    }
+                },
+                {
+                    text: "Cancelar",
+                    role: "cancel"
+                }
+            ]
+        }).present();
+    }
+
+    /**
+     * 
+     * @param item 
+     */
+    showDiscount(item: any): void {
+        this.alertCtrl.create({
+            title: "Descuento",
+            inputs: [
+                {
+                    name: "discount",
+                    type: "number",
+                    value: item.price
+                }
+            ],
+            buttons: [
+                {
+                    text: "Cancelar"
+                },
+                {
+                    text: "Aceptar",
+                    handler: (e) => {
+                        this.updatePrice(item, e.discount);
+                    }
+                }
+            ]
+        }).present();
+    }
+
+    /**
+     * 
+     * @param item 
+     */
+    showQuantity(item: any): void {
+        this.alertCtrl.create({
+            title: "Cantidad",
+            inputs: [
+                {
+                    name: "quantity",
+                    type: "number",
+                    value: item.qty
+                }
+            ],
+            buttons: [
+                {
+                    text: "Cancelar"
+                },
+                {
+                    text: "Aceptar",
+                    handler: (e) => {
+                        this.updateQuantity(item, e.quantity);
+                    }
+                }
+            ]
+        }).present();
+    }
+
+    /**
+     * 
+     */
+    askIfRemove(item: any): void {
+        this.alertCtrl.create({
+            title: "Borrar producto",
+            buttons: [
+                {
+                    text: "Cancelar"
+                },
+                {
+                    text: "Aceptar",
+                    handler: () => {
+                        this.remove(item);
+                    }
+                }
+            ]
+        }).present();
+    }
+
+    /**
+     * 
+     * @param item 
+     */
+    remove(item: any): void {
+        console.log(item);
     }
 
     /**