Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

select_delivery.js 22KB

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