import { Component, OnInit } from '@angular/core'; import { ProductsService } from '../../services/products/products.service'; import { CookieService } from 'ngx-cookie-service'; import { CouponsService } from '../../services/products/coupons.service'; import { DatePipe } from '@angular/common'; import { OrdersService } from '../../services/orders/orders.service'; @Component({ selector: 'app-create-coupon-code', templateUrl: './create-coupon-code.component.html', styleUrls: ['./create-coupon-code.component.scss'] }) export class CreateCouponCodeComponent implements OnInit { companyId: number; discountTypes: any = []; couponId: any = 1; coupon: any = {} // Usage limits variables usageLimits: any = {}; OneUse: any = []; NumberOfTimesPerCustomer: number; NumberOfTimesInTotal: number; DoNotUseIfBulkDiscount: any = []; DoNotUseIfQuantityDiscount: any = []; NumberOfTimesInTotalCheck = false; NumberOfTimesPerCustomerCheck = false; // Discount value flag variables discountTypePersentage: any = {}; discountFixedFlag = false; discountPercentageFlag = true; discountInputFlag = true; buyXgetYFlag = false countryFlag = false; minimumRequirementFlag = true NoneMinimumRequirementCheck = true; MinimumPurchaseAmountCheck: any; MinimumQuantityItemCheck: any; MinimumPurchaseAmount: number; MinimumQuantityItem: number; // Coupon post variables couponName: any; couponCode: any; selectedDiscount: any; selectedDiscountValue: any; cities2: { name: string; code: string; }[]; StartDate: any; StartTime: any; EndDate: any; EndTime: any; //Customer eligibility varaibles CustomerEligibility: any; customers: any = []; CustomerGroups: any = []; specificCustomer: any = {}; CustomerGroup: any = {}; //Coupon category variables ProductCategories: any = [{}]; selectedNode: any = {}; selectedCategories: any = []; actionType: string; //Shipping Variables ShippingRateAmountFlag = false; ShippingRateAmount: any; ShippingCountries: any = "All"; shippingDetails: any = {}; countries: any = []; selectedCountries: any = []; selectedFile:any constructor(private couponService: CouponsService, private cookieService: CookieService, private productService: ProductsService, public datepipe: DatePipe , private ordersService: OrdersService) { } ngOnInit() { this.discountTypePersentage.DiscountTypeId = 1; this.cities2 = [ { name: 'New York', code: 'NY' }, { name: 'Rome', code: 'RM' }, { name: 'London', code: 'LDN' }, { name: 'Istanbul', code: 'IST' }, { name: 'Paris', code: 'PRS' } ]; this.companyId = +this.cookieService.get('CompanyId'); this.getDiscountTypes(); this.getProductCategories(); this.getCustomers(); this.getCustomerGroups(); this.getCountries(); } //Get countries getCountries() { this.productService.GetCountries() .subscribe(res => { this.countries = res; console.log("Country Responce", res); }, error => { console.log("Error", error) }) } // Get discount types getDiscountTypes() { this.couponService.GetAllDiscountTypes(this.companyId) .subscribe(res => { this.discountTypes = res console.log("Res", res) }, error => { console.log('Error', error) }) } // Get customers getCustomers() { this.ordersService.GetCustomers(this.companyId) .subscribe(res => { this.customers = res this.specificCustomer.FirstName = this.customers[0].FirstName; console.log("Res customer", res) }, error => { console.log('Error', error) }) } // Get customer groups getCustomerGroups() { this.ordersService.GetCustomerGroups(this.companyId) .subscribe(res => { this.CustomerGroups = res this.CustomerGroup.GroupName = this.CustomerGroups[0].GroupName; console.log("Res customer groups", res) }, error => { console.log('Error', error) }) } // Save usage limits saveUsageLimits() { if (this.OneUse[0] === "OneUse") { this.usageLimits.OneUse = true; } else { this.usageLimits.OneUse = false; } this.usageLimits.NumberOfTimesPerCustomer = this.NumberOfTimesPerCustomer; this.usageLimits.NumberOfTimesInTotal = this.NumberOfTimesInTotal; if (this.DoNotUseIfBulkDiscount[0] === "DoNotUseIfBulkDiscount") { this.usageLimits.DoNotUseIfBulkDiscount = true; } else { this.usageLimits.DoNotUseIfBulkDiscount = false; } if (this.DoNotUseIfQuantityDiscount[0] === "DoNotUseIfQuantityDiscount") { this.usageLimits.DoNotUseIfQuantityDiscount = true; } else { this.usageLimits.DoNotUseIfQuantityDiscount = false; } this.couponService.SaveUsageLimits(this.usageLimits, this.companyId) .subscribe(res => { console.log("Usage limit responce", res) }, error => { console.log("Error", error) }) console.log(this.usageLimits); } // Automatically check the checkbox in input onChangeNumberOfTotal(value) { if (value.trim() !== null && value.trim() !== "") { this.NumberOfTimesInTotalCheck = true } else { this.NumberOfTimesInTotalCheck = false } } // Automatically check the checkbox in input onChangeNumberOfTimesCustomer(value) { if (value.trim() !== null && value.trim() !== "") { this.NumberOfTimesPerCustomerCheck = true } else { this.NumberOfTimesPerCustomerCheck = false } } // Automatically change radio button onChangeMinimumPurchaseAmount(value) { this.discountTypePersentage.MinimumQuantityOfItems = null; if (value !== null && value !== "") { this.MinimumPurchaseAmountCheck = "Option 2" } else { this.MinimumPurchaseAmountCheck = false } } onChangeMinimumItemQuantity(value) { this.discountTypePersentage.MinimumPurchaseAmount = null; if (value !== null && value !== "") { this.MinimumQuantityItemCheck = "Option 3" } else { this.MinimumQuantityItemCheck = false } } // On change discount type discountTypeOnChange(value) { this.discountTypePersentage.DiscountTypeId = value.Id; if (value.Id === 1) { this.discountFixedFlag = false; this.discountPercentageFlag = true; this.discountInputFlag = true; this.buyXgetYFlag = false; this.countryFlag = false; this.minimumRequirementFlag = true; } else if (value.Id === 2) { this.discountFixedFlag = true; this.discountPercentageFlag = false; this.discountInputFlag = true; this.buyXgetYFlag = false; this.countryFlag = false; this.minimumRequirementFlag = true; } else if (value.Id === 3) { this.discountFixedFlag = false; this.discountPercentageFlag = false; this.discountInputFlag = false; this.buyXgetYFlag = false; this.countryFlag = true; this.minimumRequirementFlag = false; } else { this.discountFixedFlag = false; this.discountPercentageFlag = false; this.discountInputFlag = false; this.buyXgetYFlag = true; this.countryFlag = false; this.minimumRequirementFlag = false; } } // Get product categories getProductCategories() { this.productService.GetAllProductCategories(this.companyId) .subscribe(res => { this.ProductCategories[0].label = "All products"; this.ProductCategories[0].data = "All products"; this.ProductCategories[0].children = res; for (var a = 0; a < this.ProductCategories[0].children.length; a++) { this.ProductCategories[0].children[a].label = this.ProductCategories[0].children[a].ProductCategoryName; this.ProductCategories[0].children[a].children = this.ProductCategories[0].children[a].SubCategories; for (var b = 0; b < this.ProductCategories[0].children[a].children.length; b++) { this.ProductCategories[0].children[a].children[b].label = this.ProductCategories[0].children[a].children[b].ProductSubCategoryName } } }, error => console.log('Error', error)) } // Select node from tree selectNode(e) { this.selectedNode = e.node; if (e.node.ProductCategoryName === undefined) { const selectedCategories = { CouponId: this.couponId, ProductCategoryId: this.selectedNode.ProductCategoryId, ProductSubCategoryId: this.selectedNode.Id } this.selectedCategories.push(selectedCategories) } else { const selectedCategories = { CouponId: this.couponId, ProductCategoryId: this.selectedNode.Id, ProductSubCategoryId: 0 } this.selectedCategories.push(selectedCategories) } } // Save categories of products saveCategoriesOfProducts(couponId) { if (this.selectedCategories.length > 0) { for (var i = 0; i < this.selectedCategories.length; i++) { this.selectedCategories[i].CouponId = couponId; this.couponService.CreateCategoriesOfCoupons(this.selectedCategories[i], this.companyId) .subscribe(res => { }, error => { console.log('Error', error) }) } } } // Save coupon code saveCoupon() { this.coupon.CouponName = this.couponName this.coupon.CouponCode = this.couponCode this.coupon.DiscountTypeId = this.discountTypePersentage.DiscountTypeId this.coupon.StartDate = this.StartDate this.coupon.StartTime = this.StartTime this.coupon.EndDate = this.EndDate this.coupon.EndTime = this.EndTime if (this.CustomerEligibility === "Specific") { this.coupon.CustomerEligibility = this.specificCustomer.FirstName; } else if (this.CustomerEligibility === "Group") { this.coupon.CustomerEligibility = this.CustomerGroup.GroupName; } else { this.coupon.CustomerEligibility = "Everyone"; } if (this.discountTypePersentage.DiscountTypeId === 1) { this.coupon.PrecentageDetails = this.discountTypePersentage } else { this.coupon.FixedAmountDetails = this.discountTypePersentage; } this.couponService.SaveCouponCodes(this.coupon, this.companyId, this.actionType = "SET") .subscribe(res => { console.log("Save coupon responce", res) if (this.selectedCategories.length > 0) { this.saveCategoriesOfProducts(res); } if (this.usageLimits) { this.usageLimits.CouponId = res; this.saveUsageLimits(); } }, error => { console.log("Error", error) }) console.log(this.coupon) } // Save shipping details saveShippingDetails() { this.shippingDetails.CouponId = 1 if (this.ShippingCountries === "All") { const obj = [{ Id: 0, CountryName: "All", ISOCode: "0", CountryCode: "0" }] this.shippingDetails.Countries = obj; } else { this.shippingDetails.Countries = this.selectedCountries } this.shippingDetails.ShippingRateAmount = this.ShippingRateAmount; this.couponService.SaveShippingDetails(this.shippingDetails, this.companyId, "SET" ) .subscribe(res => { console.log("Res of shipping") }, error => {console.log("Error", error)}) } onChangeCountrySelect(e){ this.ShippingCountries = "Specifiec" } onChangeNumberOfShippingRateAmount(e){ } }