暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

select_delivery.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. function initSelectDevlivery() {
  2. let editMode = false;
  3. let countryId = null;
  4. let currencyId = null;
  5. let billingInfo = null;
  6. let userName = null;
  7. let orderBtnAction = {
  8. removeOrdernowBtn() {
  9. $('.ordernowbtn').parent().addClass('d-none');
  10. },
  11. addOrdernowBtn() {
  12. $('.ordernowbtn').parent().removeClass('d-none');
  13. }
  14. }
  15. let addresslistAction = {
  16. removeAddressContainer(){
  17. $('.addresslist').addClass('d-none');
  18. },
  19. addAddressContainer(){
  20. $('.addresslist').removeClass('d-none');
  21. }
  22. }
  23. let changeAddressBtnAction = {
  24. removeChangeAddressBtn() {
  25. $('.changeAddressBtn').addClass('d-none');
  26. },
  27. addChangeAddressBtn() {
  28. $('.changeAddressBtn').removeClass('d-none');
  29. }
  30. }
  31. let changesaveCancelBtnAction = {
  32. removeChangeCancelBtn() {
  33. $('.saveanddeliverCanel').addClass('d-none');
  34. },
  35. addChangeCancelBtn() {
  36. $('.saveanddeliverCanel').removeClass('d-none');
  37. }
  38. }
  39. let saveFormLoading = {
  40. addSpinner(){
  41. $('.saveanddeliver').find('.spinner-border').removeClass('d-none');
  42. $('.saveanddeliver').find('span').addClass('d-none');
  43. },
  44. removeSpinner(){
  45. $('.saveanddeliver').find('.spinner-border').addClass('d-none');
  46. $('.saveanddeliver').find('span').removeClass('d-none');
  47. },
  48. }
  49. let paymentgatewaypageAction = {
  50. addPaymentgatewaypage(){
  51. $('.paymentgatewaypage').removeClass('d-none');
  52. },
  53. removePaymentgatewaypage(){
  54. $('.paymentgatewaypage').addClass('d-none');
  55. }
  56. }
  57. let paymentContainerAction = {
  58. paymentContainerAdd(){
  59. $('.orderslist').parent().removeClass('d-none');
  60. },
  61. paymentContainerRemove(){
  62. $('.orderslist').parent().addClass('d-none');
  63. }
  64. }
  65. const {phonenumber_errorhandler,addressdes_errorhandler,zipcode_errorhandler,state_errorhandler} = factoryErrorControl();
  66. function init() {
  67. getAddressRes();
  68. loadEvents();
  69. updateSummaryPriceAdd();
  70. // changeAddressBtnAction.removeChangeAddressBtn();
  71. }
  72. function removeAllFormError(){
  73. phonenumber_errorhandler.removeError();
  74. addressdes_errorhandler.removeError();
  75. zipcode_errorhandler.removeError();
  76. state_errorhandler.removeError();
  77. $('.errMsgFormEdit').addClass('d-none');
  78. $('.errMsgFormEdit').html('');
  79. }
  80. async function getAddressRes() {
  81. let cookieRes = COOKIE_HELPER_ACTIONS.getCookie();
  82. console.log(cookieRes);
  83. const { errorMsg, isError, response } = await API_SERVICES_ACTIONS.getAPIService(`apis/v4/bizgaze/integrations/products/getbillinginfo/contactid/${cookieRes.userId}`);
  84. let res = JSON.parse(response.result);
  85. console.log(res, 'befores');
  86. res = res[res.length - 1];
  87. //createBillingForm();
  88. const {cityid, lobid,organizationname,addressline,cityname,phonenumber,zipcode,name } =res;
  89. billingInfo = res;
  90. debugger;
  91. console.log(res);
  92. if (cityid != 0) {
  93. $('.addnewAddressContainer').addClass('d-none');
  94. $('.detailsAddressBg').removeClass('detailsAddressBg');
  95. $('.ordersummaryheader').addClass('detailsAddressBg');
  96. $('.displayadress').removeClass('d-none')
  97. // $('.addresslist').removeClass('d-none');
  98. loadShippingProducts();
  99. userName = organizationname;
  100. setAddressHeader({name:organizationname,phonenumber,addressline,cityname,zipcode});
  101. debugger;
  102. editMode = true;
  103. // adding values
  104. $('#phonenumber').val(phonenumber);
  105. $('#addressdes').val(addressline);
  106. $('#zipcode').val(zipcode);
  107. debugger;
  108. $('.userNamelogin').html(`${userName}`);
  109. changeAddressBtnAction.addChangeAddressBtn()
  110. getCountryStateCurrency();
  111. }else{
  112. $('.userNamelogin').html(`${organizationname}`);
  113. loadShippingProducts();
  114. paymentContainerAction.paymentContainerRemove();
  115. getCountryStateCurrency();
  116. orderBtnAction.removeOrdernowBtn();
  117. $('.detailsAddressBg').removeClass('detailsAddressBg');
  118. $('.selectAddress').addClass('detailsAddressBg');
  119. }
  120. }
  121. function setAddressHeader({name,phonenumber,addressline,cityname,zipcode}){
  122. $('.innerDisplayAddress').html(`${name}-${phonenumber}, ${addressline}, ${cityname} - `);
  123. $('.zipcodeAddress').html(`${zipcode}`);
  124. }
  125. function checkForm(){
  126. let number = $('#phonenumber').val();
  127. let zipcode = $('#zipcode').val();
  128. let state = $('#state').val();
  129. let address = $('#addressdes').val();
  130. let isTrue = false;
  131. if(!number || number.length!=10){
  132. isTrue = true;
  133. phonenumber_errorhandler.addError();
  134. }
  135. if(zipcode.length<6 || zipcode.length>6){
  136. isTrue = true;
  137. zipcode_errorhandler.addError();
  138. }
  139. if(!state){
  140. isTrue = true;
  141. state_errorhandler.addError();
  142. }
  143. if(!address){
  144. isTrue = true;
  145. addressdes_errorhandler.addError();
  146. }
  147. return isTrue;
  148. }
  149. async function createBillingForm(){
  150. debugger;
  151. let cookieRes = COOKIE_HELPER_ACTIONS.getCookie();
  152. let { userId, name } = cookieRes;
  153. const ctyId = await getCountryId();
  154. let stateId = parseInt($('#state').val());
  155. let payload = {
  156. "cityid": stateId,
  157. "CityName":$(`[data-id="${stateId}"`).html(),
  158. "addressline1": $('#addressdes').val(),
  159. "OrganizationType": 2,
  160. "organizationname": name,
  161. //"OrganizationId":,
  162. "ContactName": name,
  163. "ContactId": userId,
  164. "countryname": 'india',
  165. "countryid": ctyId,
  166. "statename":$(`[data-id="${stateId}"`).html(),
  167. "phonenumber": $('#phonenumber').val(),
  168. "OrganizationId": userId,
  169. "currencyid": 2,
  170. "taxno": '0',
  171. "BillingInfoExists": false,
  172. "lobid": 0,
  173. "lobname": "IT Products",
  174. "gstindetailid": 0,
  175. zipcode:$('#zipcode').val()
  176. }
  177. const res = await API_SERVICES_ACTIONS.postAPIService('apis/v4/bizgaze/crm/address/savebillinginfo', payload);
  178. // http://localhost:3088/apis/v4/bizgaze/crm/address/savebillinginfo
  179. setAddressHeader({name,phonenumber:$('#phonenumber').val(),addressline: $('#addressdes').val(),cityname:$(`[data-id="${stateId}"`).html(),zipcode:$('#zipcode').val()});
  180. return new Promise((reslove,reject) => {
  181. reslove(res);
  182. })
  183. console.log(res);
  184. }
  185. async function saveForm() {
  186. let cookieRes = COOKIE_HELPER_ACTIONS.getCookie();
  187. let { userId, name } = cookieRes;
  188. const { errorMsg, isError, response } = await API_SERVICES_ACTIONS.getAPIService(`apis/v4/bizgaze/integrations/products/getbillinginfo/contactid/${cookieRes.userId}`);
  189. let resAnwi = JSON.parse(response.result);
  190. console.log(resAnwi, 'befores');
  191. resAnwi = resAnwi[resAnwi.length - 1];
  192. let stateId = parseInt($('#state').val());
  193. let payload = {
  194. ...resAnwi,
  195. phonenumber:$('#phonenumber').val(),
  196. addressline1:$('#addressdes').val(),
  197. zipcode:$('#zipcode').val(),
  198. cityid:stateId,
  199. CityName:$(`[data-id="${stateId}"`).html(),
  200. }
  201. console.log(payload,"ertyuioghjkl");
  202. //
  203. const res = await API_SERVICES_ACTIONS.postAPIService('apis/v4/bizgaze/crm/address/savebillinginfo', payload);
  204. return new Promise((reslove,reject) => {
  205. reslove(res);
  206. })
  207. }
  208. async function loadEvents() {
  209. $('.addnewAddressContainer').off('click').click(function (e) {
  210. $('.addnewAddressContainer').addClass('d-none');
  211. $('.addresslist').removeClass('d-none');
  212. changeAddressBtnAction.removeChangeAddressBtn();
  213. changesaveCancelBtnAction.addChangeCancelBtn();
  214. });
  215. $('.saveanddeliverCanel').off('click').click(function (){
  216. if(!editMode){
  217. $('.addnewAddressContainer').removeClass('d-none');
  218. $('.addresslist').addClass('d-none');
  219. changeAddressBtnAction.removeChangeAddressBtn();
  220. changesaveCancelBtnAction.removeChangeCancelBtn();
  221. }else{
  222. loadShippingProducts();
  223. addresslistAction.removeAddressContainer()
  224. changeAddressBtnAction.addChangeAddressBtn();
  225. paymentContainerAction.paymentContainerAdd();
  226. $('.displayadress').removeClass('d-none');
  227. }
  228. });
  229. $('.saveanddeliver').off('click').click(async function (e) {
  230. saveFormLoading.addSpinner()
  231. let isErrorForm = checkForm();
  232. if(isErrorForm){
  233. saveFormLoading.removeSpinner();
  234. return;
  235. }
  236. let res;
  237. if(editMode){
  238. res = await saveForm();
  239. }else{
  240. res = createBillingForm();
  241. }
  242. saveFormLoading.removeSpinner();
  243. const {isError,errorMsg,response} = res;
  244. console.log(errorMsg);
  245. if(isError){
  246. $('.errMsgFormEdit').removeClass('d-none');
  247. $('.errMsgFormEdit').html(`${errorMsg.message}`);
  248. return;
  249. }
  250. debugger;
  251. editMode = true;
  252. let {name} = COOKIE_HELPER.getCookie();
  253. // phonenumber:$('#phonenumber').val(),
  254. // addressline1:$('#addressdes').val(),
  255. // zipcode:$('#zipcode').val(),
  256. // cityid:stateId,
  257. // CityName:$(`[data-id="${stateId}"`).html(),
  258. let stateId = parseInt($('#state').val());
  259. setAddressHeader({name,addressline:$('#addressdes').val(),cityname:$(`[data-id="${stateId}"`).html(),phonenumber:$('#phonenumber').val(),zipcode:$('#zipcode').val()});
  260. $('.errMsgFormEdit').html('');
  261. loadShippingProducts();
  262. addresslistAction.removeAddressContainer()
  263. changeAddressBtnAction.addChangeAddressBtn();
  264. paymentContainerAction.paymentContainerAdd();
  265. $('.displayadress').removeClass('d-none');
  266. });
  267. $('.changeAddressBtn').off('click').click(function (e){
  268. console.log(e.target);
  269. removeOrderSummary();
  270. changeAddressBtnAction.removeChangeAddressBtn();
  271. $('.displayadress').addClass('d-none')
  272. })
  273. $('.ordernowbtn').off().click(async function (e){
  274. let cookieRes = COOKIE_HELPER_ACTIONS.getCookie();
  275. let { userId, name } = cookieRes;
  276. paymentgatewaypageAction.addPaymentgatewaypage();
  277. let payload = {
  278. "OrganizationName": name,
  279. "BranchId": 106632280000001,
  280. "OrderItems": [
  281. {
  282. "UnitPrice": 20,
  283. "Quantity": 1,
  284. "SKU": "11",
  285. "DiscountValue": 0,
  286. "DiscountPercent": 0
  287. }
  288. ]
  289. }
  290. $('.paymentgatewaypage').removeClass('d-none');
  291. const data = getCartData();
  292. let orderItems = [];
  293. for(let i in data){
  294. const {price,quantity,sku} = data[i];
  295. let payload = {
  296. "UnitPrice": price,
  297. "Quantity": quantity,
  298. "SKU": sku,
  299. "DiscountValue": 0,
  300. "DiscountPercent": 0
  301. }
  302. orderItems.push(payload)
  303. }
  304. payload['OrderItems'] = orderItems;
  305. debugger;
  306. try {
  307. const res = await API_SERVICES_ACTIONS.postAPIService(`apis/v4/bizgaze/integrations/salesorder/save`,payload);
  308. console.log(res.response.result);;
  309. if(!res.response.result){
  310. paymentgatewaypageAction.removePaymentgatewaypage();
  311. }
  312. // `http://localhost:3088/apis/v4/bizgaze/transact/orders/getpaymentgatewaytransaction/refid/106633780000069`
  313. let response = await API_SERVICES_ACTIONS.getAPIService(`apis/v4/bizgaze/transact/orders/getpaymentgatewaytransaction/refid/${res.response.result}`);
  314. let {PaymentLink} = JSON.parse(response.response.result);
  315. window.location.href = PaymentLink;
  316. } catch (error) {
  317. paymentgatewaypageAction.removePaymentgatewaypage();
  318. }
  319. });
  320. }
  321. function getItemProductHTML(item) {
  322. const { img, name, price, quantity, sku, id } = item;
  323. return ` <div class="align-items-center item-product-add d-flex justify-content-between" data-id="${id}" data-sku="${sku}">
  324. <div style="max-width: 100px;" class="">
  325. <img class="w-100" src="${img}" alt="">
  326. </div>
  327. <div>
  328. <div>
  329. <div style="font-size:12px">${name}</div>
  330. </div>
  331. <div>
  332. <span style="font-size:12px" class="font-weight-600">${getCurrencySymbol(price)}</span> <span>x <span class="qtyitem" style="font-size:12px">${quantity}</span></span>
  333. </div>
  334. </div>
  335. </div>`;
  336. return ` <div class="item-product-add" data-id="${id}" data-sku="${sku}">
  337. <div class="row py-2">
  338. <div class="col-sm-2">
  339. <div class="w-100 d-flex justify-content-around">
  340. <div class="d-flex flex-column align-items-baseline">
  341. <div class="px-4 py-2 selectaddressimgshow">
  342. <img class="w-100" src="${img}" alt="" class="w-100">
  343. </div>
  344. <div class="addresscontrol w-100 d-flex justify-content-center gap-1">
  345. <div class="rounded-contain btnminus">
  346. -
  347. </div>
  348. <div class="w-100p">
  349. <input class="qtyshowinput" disabled value="${quantity}" type="number">
  350. </div>
  351. <div class="rounded-contain btnplus">
  352. +
  353. </div>
  354. </div>
  355. </div>
  356. </div>
  357. </div>
  358. <div class="col-sm-10">
  359. <div class="row h-100">
  360. <div class="col-sm-8">
  361. <div class="h-100 d-flex flex-column justify-content-between">
  362. <div> ${name}</div>
  363. <div>
  364. <span>$</span> ${price} <span class="d-none">56% Off 2 offers applied</span>
  365. </div>
  366. <div class="d-flex gap-3">
  367. <span class="text-uppercase d-none">Save for later</span>
  368. <span class="text-uppercase cursor-pointer removecartItem">Remove</span>
  369. </div>
  370. </div>
  371. </div>
  372. </div>
  373. </div>
  374. </div>
  375. </div>`;
  376. }
  377. async function getCountryId(){
  378. const countryRes = await API_SERVICES_ACTIONS.getAPIService('apis/v4/bizgaze/integrations/products/getcountry');;
  379. if (countryRes.isError) {
  380. alert("something went wrong");
  381. console.log(countryRes);
  382. return;
  383. }
  384. let ctyId = 0
  385. let countryData = JSON.parse(countryRes.response.result);
  386. console.log(countryData);
  387. for (let i = 0; i < countryData.length; i++) {
  388. if (countryData[i].countryname.toLowerCase() === 'india') {
  389. countryId = countryData[i].countryid;
  390. ctyId = countryId
  391. countrynameData = countryData[i].countryname;
  392. break;
  393. }
  394. }
  395. return ctyId;
  396. }
  397. async function getCountryStateCurrency() {
  398. const countryRes = await API_SERVICES_ACTIONS.getAPIService('apis/v4/bizgaze/integrations/products/getcountry');;
  399. if (countryRes.isError) {
  400. alert("something went wrong");
  401. console.log(countryRes);
  402. return;
  403. }
  404. let countryData = JSON.parse(countryRes.response.result);
  405. console.log(countryData);
  406. for (let i = 0; i < countryData.length; i++) {
  407. if (countryData[i].countryname.toLowerCase() === 'india') {
  408. countryID = countryData[i].countryid;
  409. countrynameData = countryData[i].countryname;
  410. break;
  411. }
  412. }
  413. const stateRes = await API_SERVICES_ACTIONS.getAPIService(`apis/v4/bizgaze/integrations/products/getcities/countryid/${countryID}`);
  414. if (stateRes.isError) {
  415. alert("something went wrong");
  416. console.log(stateRes);
  417. return;
  418. }
  419. let stateData = JSON.parse(stateRes.response.result);
  420. console.log(stateData, 'state');
  421. $('#state').html('');
  422. console.log("start")
  423. let html = '';
  424. statesList = stateData;
  425. for (let i = 0; i < stateData.length; i++) {
  426. html += `<option value="${stateData[i].cityid}" data-id="${stateData[i].cityid}">${stateData[i].cityname}</option>`
  427. }
  428. $('#state').append(html);
  429. $("#state").select2({
  430. placeholder: "Select a City",
  431. allowClear: true
  432. });
  433. const currencyRes = await API_SERVICES_ACTIONS.getAPIService('apis/v4/bizgaze/integrations/products/getcurrencies');
  434. if(currencyRes.isError){
  435. alert("something went wrong");
  436. console.log(currencyRes);
  437. return;
  438. }
  439. let currencyData = JSON.parse(currencyRes.response.result);
  440. console.log(currencyData,'currencyDatacurrencyDatacurrencyData');
  441. for(let i=0;i<currencyData.length;i++){
  442. const curr = currencyData[i];
  443. if(curr.currencyname.toLowerCase().includes('india')){
  444. currencyid = curr.currencyid;
  445. }
  446. }
  447. }
  448. function loadShippingProducts(){
  449. $('.detailsAddressBg').removeClass('detailsAddressBg');
  450. $('.ordersummaryheader ').addClass('detailsAddressBg')
  451. debugger;
  452. const products = getCartData();
  453. console.log(products);
  454. if (Object.keys(products||{}).length) orderBtnAction.addOrdernowBtn();
  455. let productsHTML = '';
  456. for (let item in products) {
  457. const currItem = products[item];
  458. productsHTML += getItemProductHTML({ ...currItem, id: item })
  459. }
  460. $('.orderlistcart').html(productsHTML);
  461. // $('.orderslist').html(productsHTML);
  462. $('.orderslist').html(` <label for="online">
  463. Online
  464. <input checked data-op="online" name="payment" type="radio" class="paymentbtn " id="online">
  465. </label>
  466. <label for="cod">
  467. Cash on develiop
  468. <input data-op="cod" name="payment" type="radio" class="paymentbtn " id="cod">
  469. </label>`);
  470. $('.paymentbtn').off().click(function (e){
  471. const data = $(e.target).data('op');
  472. if(data == 'online'){
  473. orderBtnAction.addOrdernowBtn();
  474. }else{
  475. orderBtnAction.removeOrdernowBtn();
  476. }
  477. })
  478. $('.btnminus').each(function (i, el) {
  479. $(el).off().click(function (elm) {
  480. let id = $(elm.target).parents('.item-product-add').data('id');
  481. let val = parseInt($(elm.target).parents('.item-product-add').find('.qtyshowinput').val());
  482. if (val <= 1) return;
  483. val--;
  484. updateCartQuantity(id, val)
  485. $(elm.target).parents('.item-product-add').find('.qtyshowinput').val(val)
  486. });
  487. })
  488. $('.btnplus').each(function (i, el) {
  489. $(el).off().click(function (elm) {
  490. let id = $(elm.target).parents('.item-product-add').data('id');
  491. let val = parseInt($(elm.target).parents('.item-product-add').find('.qtyshowinput').val());
  492. if (val >= 11) return;
  493. val++;
  494. updateCartQuantity(id, val)
  495. $(elm.target).parents('.item-product-add').find('.qtyshowinput').val(val);
  496. });
  497. })
  498. $('.removecartItem').each(function (i, el) {
  499. $(el).click(function (elm) {
  500. let id = $(elm.target).parents('.item-product-add').data('id');
  501. let len = removeCartItem(id);
  502. $(`.item-product-add[data-id="${id}"]`).remove();
  503. if (!len) {
  504. orderBtnAction.removeOrdernowBtn();
  505. }
  506. });
  507. })
  508. }
  509. function removeOrderSummary(){
  510. $('.detailsAddressBg').removeClass('detailsAddressBg');
  511. $('.selectAddress').addClass('detailsAddressBg');
  512. $('.orderslist').html('');
  513. orderBtnAction.removeOrdernowBtn();
  514. // $('.detailsAddressBtn').addClass('d-none');
  515. addresslistAction.addAddressContainer();
  516. $('.saveanddeliver').removeClass('d-none');
  517. $('.saveanddeliverCanel').removeClass('d-none')
  518. }
  519. function factoryErrorControl(){
  520. let inputsArr = ['phonenumber','addressdes','zipcode','state'];
  521. let controls = {};
  522. for(let i=0;i<inputsArr.length;i++){
  523. const id =inputsArr[i];
  524. controls[`${id}_errorhandler`] = {
  525. addError(){
  526. debugger;
  527. $(`#${id}`).parents('.col-sm-6').find('.invalid-feedback').addClass('d-block');
  528. },
  529. removeError(){
  530. $(`#${id}`).parents('.col-sm-6').find('.invalid-feedback').addClass('d-block');
  531. }
  532. }
  533. }
  534. return controls;
  535. }
  536. init();
  537. }
  538. checkValidAuth(initSelectDevlivery);