OrderEnsureCtrlWithMultiCurrency.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. define([ 'app/app' ], function(app) {
  2. 'use strict';
  3. app.register.controller('OrderEnsureCtrl', [ '$scope', '$stateParams', '$location', '$modal', '$filter', 'toaster', 'Order', 'OrderSimpleInfo', 'Cart', 'ShippingAddress', 'PickUpAddress' , 'SessionService', '$window' ,'Bill', 'bankInfoService', '$interval', 'ExpressPrice', '$q', '$rootScope', 'Ysepay', 'AuthenticationService', function($scope, $stateParams, $location, $modal, $filter, toaster, Order, OrderSimpleInfo , Cart, ShippingAddress, PickUpAddress, SessionService, $window, Bill, bankInfoService, $interval, ExpressPrice, $q, $rootScope, Ysepay, AuthenticationService) {
  4. /************************************************************************
  5. * general
  6. ************************************************************************/
  7. var enIdFilter = $filter('EncryptionFilter');
  8. // 使银行账号只显示最后四位
  9. var hideBankFilter = $filter("hideBankFilter");
  10. // 直接购买不显示进度条
  11. var a = SessionService.get("buyNow")? SessionService.get("buyNow") : 'false';
  12. if (a == 'true') {
  13. $scope.isBuyNow = true;
  14. } else {
  15. $scope.isBuyNow = false;
  16. }
  17. //定时器
  18. var setTime = function() {
  19. if($scope.time > 0) {
  20. setTimeout(function() {
  21. $scope.$apply(function() {
  22. $scope.time--;
  23. setTime();
  24. });
  25. }, 1000);
  26. }else {
  27. if($scope.errorData) {
  28. var spit = $scope.errorData.split(':');
  29. if(spit.length == 3) {
  30. OrderSimpleInfo.setUnavailable({orderid: enIdFilter(spit[1])}, null, function(data) {
  31. SessionService.set('todoState','unavailable');
  32. window.location.replace('user#/home/myOrder_todo');
  33. toaster.pop('sucess', '失效成功,此订单因库存不足已失效');
  34. }, function(res){
  35. toaster.pop('info', '订单未失效,请刷新界面');
  36. })
  37. }else {
  38. toaster.pop('error', '订单确认失败,');
  39. window.location.replace('user#/home/myOrder_todo');
  40. }
  41. }
  42. }
  43. };
  44. // 失效订单
  45. var unavailable = function() {
  46. $scope.loading = true;
  47. $scope.errorloading = true;
  48. $scope.time = 5;
  49. setTime();
  50. $scope.$watch('time', function(newValue, oldValue, scope) {
  51. if(!newValue) {
  52. //跳转到未完成订单界面
  53. SessionService.set('todoState','unavailable');
  54. $window.location.href = "user#/home/myOrder_todo";
  55. }
  56. });
  57. };
  58. // 每个下拉框是否显示
  59. $scope.dropdownState = {
  60. deliveryMode: false,
  61. receivingInfo: false,
  62. paytype: false,
  63. invoiceType: false,
  64. selectCurrency: false
  65. };
  66. // 显示或关闭某个下拉列表
  67. $scope.showDropdown = function(item) {
  68. switch (item) {
  69. case 'deliveryMode': $scope.dropdownState.deliveryMode = !$scope.dropdownState.deliveryMode; break;
  70. case 'receivingInfo': $scope.dropdownState.receivingInfo = !$scope.dropdownState.receivingInfo; break;
  71. case 'paytype': $scope.dropdownState.paytype = !$scope.dropdownState.paytype; break;
  72. case 'invoiceType': $scope.dropdownState.invoiceType = !$scope.dropdownState.invoiceType; break;
  73. case 'selectCurrency': $scope.dropdownState.selectCurrency = !$scope.dropdownState.selectCurrency; break;
  74. };
  75. };
  76. // 检查订单是否可提交
  77. var checkCanSubmitOrderOrNot = function() {
  78. if($scope.order && $scope.selectAddress) {
  79. for(var i = 0; i < $scope.order.orderDetails.length; i++) {
  80. var detail = $scope.order.orderDetails[i];
  81. if($scope.qtyInvalid(detail)) {
  82. $scope.canSubmitOrder = false; return;
  83. };
  84. };
  85. if($scope.order.invoicetype != 1207 && angular.equals($scope.selecInvoice, {})) {
  86. $scope.canSubmitOrder = false;
  87. return;
  88. };
  89. if($scope.order.currency === 'USD' && $scope.order.paytype === 1102) {
  90. $scope.canSubmitOrder = false;
  91. return;
  92. }
  93. $scope.canSubmitOrder = true; return;
  94. }else if($scope.order && !$scope.selectAddress) {
  95. $scope.canSubmitOrder = false; return;
  96. };
  97. };
  98. /************************************************************************
  99. * main init
  100. ************************************************************************/
  101. // 直接初始化不需要从服务器获取的数据,这样不会产生异步。
  102. var initCommonData = function() {
  103. $scope.orderid = $stateParams.orderid;
  104. $scope.purchaseSub = false;//购买主体,默认是公司
  105. $scope.select = {};
  106. $scope.select.purKind = false;//应付账户的类别, 默认是企业
  107. $scope.select.sellerKind = false;//应收账户的类别,默认是企业
  108. $scope.canSubmitOrder = false;
  109. };
  110. initCommonData();
  111. // 初始化订单明细
  112. var initOrderDetails = function(order) {
  113. angular.forEach(order.orderDetails, function(detail, k) {
  114. if(detail.number <= detail.reserve) {
  115. detail.canAddToCart = true;
  116. }else {
  117. detail.canAddToCart = false;
  118. }
  119. refreshUnitrice(detail);
  120. })
  121. };
  122. // 初始化订单相关数据。由于订单确认页现在只处理单张且同一币别订单,因此只处理获取的订单中的第一张订单。
  123. var initOrder = function() {
  124. // 默认为普通发票 --1205专用增值税发票 --1206普通发票 --1207不开发票
  125. $scope.order.invoicetype = 1205;
  126. // 默认选择UU配送 --1301UU配送 --1302上门自提
  127. $scope.order.deliverytype = 1301;
  128. // 默认选择在线支付 --1102在线支付
  129. $scope.order.paytype = 1102;
  130. };
  131. var getOrderData = function() {
  132. //加载订单数据
  133. return Order.query({orderid: $scope.orderid}, function(data){
  134. //这里判断一下data里面的值是否有异常
  135. if(data[0].exception) {
  136. toaster.pop('error', '订单确认失败 ', data[0].exception);
  137. unavailable();
  138. return;
  139. }
  140. $scope.orders = data;
  141. if($scope.orders.length != 0) {
  142. /*TODO 所有订单都需要的属性*/
  143. $scope.order = $scope.orders[0];
  144. }
  145. //检查订单状态
  146. if($scope.order.status != 501){
  147. /*TODO 订单无效的逻辑*/
  148. unavailable();
  149. toaster.pop('error', '错误', '此订单状态不可操作!!');
  150. }
  151. }, function(res){
  152. toaster.pop('error', '系统错误', '获取订单信息失败');
  153. });
  154. };
  155. // 加载用户收货地址
  156. var loadAddrs = function(isLast){
  157. return ShippingAddress.get({send: false}, function(data) {
  158. //为每个设置选择状态
  159. angular.forEach(data, function(addr){
  160. addr.isSelect = false;
  161. });
  162. $scope.addrs = data;
  163. if($scope.addrs.length > 0) {
  164. if(isLast) {
  165. $scope.addrs[$scope.addrs.length-1].isSelect = true;
  166. $scope.selectAddress = $scope.addrs[$scope.addrs.length-1];
  167. }
  168. if(!$scope.selectAddress) {
  169. $scope.addrs[0].isSelect = true;
  170. $scope.selectAddress = $scope.addrs[0];
  171. }else {
  172. // 如果原来保存的地址 被删除,就设置第一个为默认的地址
  173. var isExist = false;
  174. angular.forEach($scope.addrs, function(addr) {
  175. if(addr.id == $scope.selectAddress.id) {
  176. addr.isSelect = true;
  177. $scope.selectAddress = addr;
  178. $scope.selectAddress.isSelect = true;
  179. isExist = true;
  180. }
  181. });
  182. if(!isExist) {
  183. $scope.addrs[0].isSelect = true;
  184. $scope.selectAddress = $scope.addrs[0];
  185. }
  186. }
  187. }
  188. checkCanSubmitOrderOrNot(); //检查订单是否可提交
  189. }, function(response) {
  190. toaster.pop('error', '系统错误', '获取收货地址失败');
  191. });
  192. };
  193. // 获取发票信息方法 1205为增值税专用发票 1206为增值税普通发票 1207为不开发票
  194. var getInvoiceInfo = function() {
  195. return Bill.getListPersonal(null, function(data) {
  196. $scope.specialInvoice = {};
  197. angular.forEach(data, function(bill) {
  198. if(bill.kind == 1205) {
  199. $scope.specialInvoice = bill;
  200. }
  201. });
  202. }, function(response) {
  203. toaster.pop('error', '获取发票信息失败');
  204. });
  205. };
  206. // 计算运费
  207. var calculateFright = function() {
  208. var address = $scope.selectAddress.area + "," + $scope.selectAddress.detailAddress;
  209. var volume = 0;
  210. var weight = 0;
  211. angular.forEach($scope.order.orderDetails, function(v, k) {
  212. weight += v.weight||0;
  213. volume += v.volume||0;
  214. });
  215. weight = weight === 0 ? 1 : weight; // 默认1克
  216. volume = volume === 0 ? 1 : volume; // 默认1立方厘米
  217. ExpressPrice.getFreight({address : address, volume : volume, weight : weight}, function(data) {
  218. $scope.freight = data.freight;
  219. })
  220. };
  221. // 初始化币别
  222. var initCurrency = function() {
  223. $scope.order.currency = "RMB";
  224. if($scope.selectAddress) {
  225. if($scope.selectAddress.area.startsWith("香港")) {
  226. $scope.order.currency = "USD";
  227. $scope.order.invoicetype = "1207";
  228. $scope.order.selecInvoice = {};
  229. }else {
  230. $scope.order.currency = "RMB";
  231. $scope.order.invoicetype = "1205";
  232. $scope.order.selecInvoice = $scope.specialInvoice;
  233. }
  234. }
  235. }
  236. // 计算商品数量、金额
  237. var calculateQtyAndAmount = function() {
  238. $scope.totalQty = 0; // 已选购的总数量
  239. $scope.productsAmount = 0; // 已选购的商品总额
  240. var importedAmount = 0.0; // 进口商品人民币总价
  241. var cmps = [];
  242. angular.forEach($scope.order.orderDetails, function(detail, k) {
  243. $scope.totalQty += Number(detail.number);
  244. // 保留两位小数,第三位存在则加一,如99.021 -> 99.03
  245. $scope.productsAmount += Math.ceil(detail.taxUnitprice * detail.number * 100) / 100;
  246. if(cmps.indexOf(detail.cmpCode) === -1) {
  247. cmps.push(detail.cmpCode);
  248. }
  249. if(detail.currencyLackOfPrice === 'RMB' && $scope.order.currency === "RMB") {
  250. importedAmount += Math.ceil(detail.taxUnitprice * detail.number * 100) / 100;
  251. }
  252. });
  253. $scope.importExtraCharges = Math.ceil(importedAmount * $scope.order.importExtraChargeRate * 100) / 100; //进口额外支付费用
  254. if(0 < $scope.importExtraCharges && $scope.importExtraCharges < 300) {
  255. $scope.importExtraCharges = 300;
  256. }
  257. $scope.cmpNum = cmps.length;
  258. };
  259. // 订单、地址、运费初始化。运费必须在订单与地址获取并resolve之后才能初始化,因此使用Promise.all或$q.all来保证所有promise解析之后再计算运费
  260. var init = function() {
  261. /*Promise.all([getOrderData(), loadAddrs()]).then(initFright) 两种方式都可以*/
  262. $q.all([getOrderData().$promise, loadAddrs().$promise, getInvoiceInfo().$promise]).then(function(){
  263. initOrder();// 订单初始化
  264. initCurrency(); // 初始化币别
  265. $scope.selectInvoiceType($scope.order.invoicetype);
  266. initOrderDetails($scope.order); //订单明细初始化,需要根据币别来计算taxUnitPrice,因此需在获取地址后进行。
  267. calculateQtyAndAmount();
  268. //calculateFright(); // 需求变更,暂不做运费处理,保留接口
  269. checkCanSubmitOrderOrNot(); //检查订单是否可提交
  270. });
  271. };
  272. init();
  273. /************************************************************************
  274. * product confirm
  275. ************************************************************************/
  276. // 展开或隐藏分段价格
  277. $scope.showPrices = function(detail, currency) {
  278. if(currency === 'RMB') {
  279. detail.$pricesShowRmb = !detail.$pricesShowRmb;
  280. }else if(currency === 'USD') {
  281. detail.$pricesShowUsd = !detail.$pricesShowUsd;
  282. }
  283. };
  284. // 选择币别
  285. $scope.selectCurrency = function(currency) {
  286. if($scope.order.currency !== currency) {
  287. if($scope.selectAddress) {
  288. var prevAddress = angular.fromJson(SessionService.get($scope.order.orderid + 'addr'));
  289. if(currency === "USD" && !$scope.selectAddress.area.startsWith("香港")) {
  290. $scope.order.invoicetype = "1207";
  291. $scope.selecInvoice = {};
  292. if(prevAddress && prevAddress.area.startsWith("香港")) {
  293. $scope.selectAddress = prevAddress;
  294. } else {
  295. var isExistHKAddr = false;
  296. for(var i = 0; i < $scope.addrs.length; i++) {
  297. var addr = $scope.addrs[i];
  298. if(addr.area.startsWith("香港")) {
  299. if($scope.selectAddress) {
  300. $scope.selectAddress.isSelect = false;
  301. };
  302. addr.isSelect = true;
  303. SessionService.set($scope.order.orderid + 'addr', angular.toJson($scope.selectAddress));
  304. $scope.selectAddress = addr;
  305. isExistHKAddr = true;
  306. break;
  307. }
  308. }
  309. if(!isExistHKAddr) {
  310. alert("只能在香港以美元购买商品,请添加香港收货地址,并修改收货地址为香港");
  311. return;
  312. }
  313. }
  314. }
  315. if(currency === "RMB" && $scope.selectAddress.area.startsWith("香港")) {
  316. $scope.order.invoicetype = "1205";
  317. $scope.selecInvoice = $scope.specialInvoice;
  318. if(prevAddress && !prevAddress.area.startsWith("香港")) {
  319. $scope.selectAddress = prevAddress;
  320. } else {
  321. var isExistMainlandAddr = false;
  322. for(var i = 0; i < $scope.addrs.length; i++) {
  323. var addr = $scope.addrs[i];
  324. if(!addr.area.startsWith("香港")) {
  325. if($scope.selectAddress) {
  326. $scope.selectAddress.isSelect = false;
  327. };
  328. addr.isSelect = true;
  329. SessionService.set($scope.order.orderid + 'addr', angular.toJson($scope.selectAddress));
  330. $scope.selectAddress = addr;
  331. isExistMainlandAddr = true;
  332. break;
  333. }
  334. }
  335. if(!isExistMainlandAddr) {
  336. alert("只能在大陆以人民币购买商品,请添加大陆收货地址,并修改收货地址为大陆收货地址");
  337. return;
  338. }
  339. }
  340. }
  341. $scope.order.currency = currency;
  342. angular.forEach($scope.order.orderDetails, function(detail, k) {
  343. refreshUnitrice(detail);
  344. })
  345. calculateQtyAndAmount();
  346. $scope.dropdownState.selectCurrency = false;
  347. }else {
  348. alert("币别与收货地址相关,请您先添加收货地址");
  349. }
  350. }
  351. }
  352. // 验证批次购买数量是否无效
  353. $scope.qtyInvalid = function(detail) {
  354. if(!detail.number) {
  355. detail.noBuyQty = true;
  356. detail.ltMinPackQty = false;
  357. detail.notMultipleOfminPackQty = false;
  358. detail.greaterThanReserve = false;
  359. return true;
  360. };
  361. detail.number = Number(detail.number);
  362. if(detail.number < detail.minBuyQty) {
  363. detail.ltMinPackQty = true;
  364. return true;
  365. }
  366. if(detail.number % detail.minPackQty) {
  367. detail.notMultipleOfminPackQty = true;
  368. return true;
  369. }
  370. if(detail.number > detail.reserve) {
  371. detail.greaterThanReserve = true;
  372. return true;
  373. }
  374. detail.noBuyQty = false;
  375. detail.ltMinPackQty = false;
  376. detail.notMultipleOfminPackQty = false;
  377. detail.greaterThanReserve = false;
  378. return false;
  379. };
  380. // 增加或减少购买数量
  381. $scope.changeBuyQty = function(detail, qty){
  382. detail.number = Number(detail.number) + qty;
  383. $scope.refresh(detail); // ng-change监测不到页面输入以外造成的改变
  384. };
  385. // 按分段更新单价
  386. var refreshUnitrice = function(detail) {
  387. for(var i = 0; i < detail.rmbPrices.length; i++){
  388. var price = detail.rmbPrices[i];
  389. if(price.start <= detail.number && detail.number <= price.end) {
  390. detail.rmbTaxUnitprice = price.taxPrice;
  391. detail.rmbUnitprice = price.price;
  392. if($scope.order.currency === "RMB") {
  393. detail.taxUnitprice = price.taxPrice;
  394. }
  395. break;
  396. }
  397. }
  398. for(var i = 0; i < detail.usdPrices.length; i++){
  399. var price = detail.usdPrices[i];
  400. if(price.start <= detail.number && detail.number <= price.end) {
  401. detail.usdTaxUnitprice = price.taxPrice;
  402. detail.usdUnitprice = price.price;
  403. if($scope.order.currency === "USD") {
  404. detail.taxUnitprice = price.taxPrice;
  405. }
  406. break;
  407. }
  408. }
  409. };
  410. // 更新商品总数量、总金额、运费
  411. $scope.refresh = function(detail) {
  412. if(!$scope.qtyInvalid(detail)) {
  413. detail.number = Number(detail.number);
  414. refreshUnitrice(detail);
  415. //calculateFright(); // 需求变更,暂不做运费处理,保留接口
  416. calculateQtyAndAmount();
  417. detail.canAddToCart = true;
  418. }else {
  419. detail.canAddToCart = false;
  420. }
  421. checkCanSubmitOrderOrNot(); //检查订单是否可提交
  422. };
  423. // 取消订单明细
  424. $scope.cancelOrderDetail = function(detail, order) {
  425. Order.deleteDetails({},{
  426. detailIds : detail.id,
  427. orderid: order.orderid
  428. }, function(data) {
  429. var index = $scope.order.orderDetails.indexOf(detail);
  430. $scope.order.orderDetails.splice(index, 1);
  431. calculateQtyAndAmount();
  432. toaster.pop('sucess', '取消订单明细成功');
  433. if($scope.order.orderDetails.length === 0) {
  434. $scope.loading = true;
  435. toaster.pop('sucess', '订单为空,即将返回商品首页重新购买商品');
  436. $location.path("product#/home");
  437. $scope.time = 5;
  438. setTime();
  439. }
  440. }, function(err) {
  441. toaster.pop('error', '取消失败' + err);
  442. });
  443. };
  444. // 加入购物车
  445. $scope.addToCart = function(detail){
  446. $scope.qtyInvalid(detail);
  447. if(!$rootScope.userInfo || !$rootScope.userInfo.userUU) {
  448. AuthenticationService.redirectSignin();
  449. return;
  450. }
  451. var goods = {};
  452. goods.uuid = detail.uuid;
  453. goods.number = detail.number;
  454. goods.batchCode = detail.batchCode;
  455. goods.taxes = detail.taxes;
  456. if($scope.isBuy){// 1、如果是立即购买,直接生成订单,返回订单
  457. } else {// 2、如果是加入购物车,组装cart对象,提交
  458. if (goods.number > 0) {
  459. Cart.save({uuid: goods.uuid}, goods, function(data){
  460. toaster.pop('success', '保存成功', '添加购物车成功');
  461. Cart.getCount({}, function(data){
  462. $rootScope.countCart = data.count;
  463. }, function(res){
  464. });
  465. }, function(res){
  466. toaster.pop('error', '警告', res.data);
  467. });
  468. } else {
  469. toaster.pop('warning', '提示', '该商品库存为0,请等待上货或联系客服');
  470. }
  471. }
  472. };
  473. /************************************************************************
  474. * logistics confirm
  475. ************************************************************************/
  476. //选择送货方式--1301为UU配送 1302 为上门自提
  477. $scope.selectDelivery = function(code){
  478. delete $scope.pickAddress;
  479. $scope.order.deliverytype = code;
  480. $scope.dropdownState.deliveryMode = false;
  481. };
  482. /**
  483. * 送货方式为1302(上门自提)时,需要选择提货地址
  484. */
  485. //获取提货地址列表
  486. $scope.getPickUpAddress = function(code){
  487. $scope.selectDelivery(code);
  488. PickUpAddress.get({}, function(data){
  489. $scope.pickUpAddress = data;
  490. $scope.pickUpAddress[0].isActive = true;
  491. $scope.pickAddress = $scope.pickUpAddress[0];
  492. });
  493. }
  494. $scope.setBorderGray = function(index){
  495. $scope.pickUpAddress[index].isFocus = true;
  496. };
  497. $scope.cancelBorderGray = function(index){
  498. $scope.pickUpAddress[index].isFocus = false;
  499. };
  500. //选择自取地址
  501. $scope.selectPick = function(pick, pickUpAddress){
  502. angular.forEach(pickUpAddress, function(p) {
  503. p.isActive = false;
  504. });
  505. pick.isActive = true;
  506. $scope.pickAddress = pick;
  507. };
  508. //选择收货地址
  509. $scope.selectAddr = function(addr, addrs){
  510. if(addr !== $scope.selectAddress) {
  511. if(addr.area.startsWith("香港")) {
  512. $scope.order.currency = "USD";
  513. $scope.order.invoicetype = "1207";
  514. $scope.selecInvoice = {};
  515. }else {
  516. $scope.order.currency = "RMB";
  517. $scope.order.invoicetype = "1205";
  518. $scope.selecInvoice = $scope.specialInvoice;
  519. }
  520. angular.forEach(addrs, function(ad) {
  521. ad.isSelect = false;
  522. });
  523. addr.isSelect = true;
  524. $scope.selectAddress = addr;
  525. angular.forEach($scope.order.orderDetails, function(detail, k) {
  526. refreshUnitrice(detail);
  527. })
  528. calculateQtyAndAmount();
  529. checkCanSubmitOrderOrNot(); //检查订单是否可提交
  530. }
  531. $scope.dropdownState.receivingInfo = false;
  532. };
  533. //删除收货地址
  534. $scope.deleteAddr = function(addr){
  535. $scope.isnotCheck = true;
  536. var isSure = confirm('确认删除?删除后不可恢复,请谨慎操作!');
  537. if(isSure) {
  538. var id = addr.id;
  539. ShippingAddress.del({addid: id}, {}, function(data){
  540. //重新加载购物数据
  541. loadAddrs();
  542. }, function(res){
  543. toaster.pop('error', '系统错误', '删除收货地址失败');
  544. });
  545. }
  546. };
  547. //编辑收货地址
  548. $scope.editAddr = function(isSetTop, addr) {
  549. $scope.isnotCheck = true;
  550. $modal.open({
  551. templateUrl : 'static/view/prod/modal/editAddr_modal.html',
  552. controller : 'editAddrCtrl',
  553. size : 'lg',
  554. resolve : {
  555. isSetTop : function(){
  556. //必须用 angular.copy深拷贝一份
  557. return angular.copy(isSetTop);
  558. },
  559. addr : function(){
  560. return angular.copy(addr);
  561. }
  562. }
  563. }).result.then(function(address){
  564. if(!addr) {
  565. loadAddrs(true);
  566. }else {
  567. loadAddrs();
  568. }
  569. }, function(reason){
  570. toaster.pop('info', '提示 ' + '您已取消收货地址的编辑');
  571. });
  572. };
  573. /************************************************************************
  574. * pay confirm
  575. ************************************************************************/
  576. $scope.selecInvoice = {};
  577. // 选择发票类型
  578. $scope.selectInvoiceType = function(type) {
  579. $scope.order.invoicetype = type;
  580. switch(type) {
  581. case 1207:
  582. $scope.selecInvoice = {};
  583. $scope.needCreateInvoice = false;
  584. break;
  585. case 1205:
  586. $scope.selecInvoice = $scope.specialInvoice;
  587. if(!$scope.specialInvoice) {
  588. $scope.needCreateInvoice = true;
  589. };
  590. break;
  591. };
  592. $scope.dropdownState.invoiceType = false; // 关闭下拉选择列表
  593. checkCanSubmitOrderOrNot(); //检查订单是否可提交
  594. }
  595. // 新建发票类型 1205专用增值税发票 --1206普通发票 --1207不开发票
  596. $scope.addInvoiceInfo = function () {
  597. $modal.open({
  598. templateUrl : 'static/view/prod/modal/edit-invoice-modal.html',
  599. controller : 'BillInputCtrl',
  600. size : 'lg',
  601. resolve : {
  602. invoiceInfo: function() {
  603. if($scope.order.invoicetype == 1205) {
  604. return 1205 + "";
  605. }
  606. if($scope.order.invoicetype == 1206) {
  607. return 1206 + "";
  608. }
  609. }
  610. }
  611. }).result.then(function(invoice){
  612. if(invoice) {
  613. $q.all([getInvoiceInfo().$promise]).then(function() {
  614. // 1205专用增值税发票 1206普通发票
  615. if(invoice.kind == 1205) {
  616. $scope.selectInvoiceType(1205);
  617. }else if(invoice.kind == 1206) {
  618. $scope.selectInvoiceType(1206);
  619. }
  620. })
  621. }
  622. }, function(reason){
  623. toaster.pop('info', '提示 ' + '您已取消新建发票信息');
  624. });
  625. }
  626. $scope.deleteById = function(id) {
  627. var isToDelete = confirm("确定要删除吗?");
  628. if(!isToDelete) {
  629. return ;
  630. }
  631. Bill.deleteById({id: id}, function(data) {
  632. toaster.pop('success', '删除成功');
  633. $q.all([getInvoiceInfo().$promise]).then(function(){
  634. $scope.selectInvoiceType($scope.order.invoicetype);
  635. checkCanSubmitOrderOrNot(); //检查订单是否可提交
  636. });
  637. }, function(reponse) {
  638. toaster.pop('error', '删除发票资料失败');
  639. });
  640. }
  641. $scope.viewDetail = function(bill) {
  642. var modalInstance = $modal.open({
  643. templateUrl : 'static/view/common/billInfoModal.html',
  644. controller : 'BillInfoCtrl',
  645. resolve : {
  646. bill : function() {
  647. //深拷贝一份
  648. return angular.copy(bill);
  649. }
  650. }
  651. });
  652. }
  653. $scope.modify = function(invoiceId) {
  654. $modal.open({
  655. templateUrl : 'static/view/prod/modal/edit-invoice-modal.html',
  656. controller : 'BillInputCtrl',
  657. size : 'lg',
  658. resolve : {
  659. invoiceInfo: function() {
  660. if($scope.order.invoicetype == 1205) {
  661. return 1205 + "-" +invoiceId;
  662. }
  663. if($scope.order.invoicetype == 1206) {
  664. return 1206 + "-" +invoiceId;
  665. }
  666. },
  667. }
  668. }).result.then(function(invoice){
  669. if(invoice) {
  670. $q.all([getInvoiceInfo().$promise]).then(function() {
  671. // 1205专用增值税发票 1206普通发票
  672. if(invoice.kind == 1205) {
  673. $scope.selectInvoiceType(1205);
  674. }else if(invoice.kind == 1206) {
  675. $scope.selectInvoiceType(1206);
  676. }
  677. })
  678. }
  679. }, function(reason){
  680. toaster.pop('info', '提示 ' + '您已取消编辑发票信息');
  681. });
  682. }
  683. //选择付款方式,获取订单数据时已初始化为1102。 1102为线上支付,1103为公司转账
  684. $scope.selectPayType = function(code){
  685. $scope.order.paytype = code;
  686. checkCanSubmitOrderOrNot(); //检查订单是否可提交
  687. $scope.dropdownState.paytype = false;
  688. };
  689. //解析数据,从返回的数据中找到要解析的数据
  690. var resolveData = function(data) {
  691. var arr = new Array();
  692. for(var key in data) {
  693. var numb= Number(key);
  694. if(angular.isNumber(numb)&&(!isNaN(numb))) {
  695. arr.push(data[key]);
  696. }
  697. }
  698. return arr;
  699. };
  700. // 获取数据的第一行
  701. var getFirst = function(data) {
  702. var result = {};
  703. if(data&&data.length) {
  704. result = data[0];
  705. }else {
  706. result = null;
  707. }
  708. return result;
  709. };
  710. //获取管理平台账户信息
  711. var getSellerAccount = function() {
  712. if(!$scope.select.sellerKind) {//获取企业的账户信息
  713. bankInfoService.getAdminEnterAccount('', function(data) {
  714. $scope.saleAccountInfos = resolveData(data);
  715. angular.forEach($scope.saleAccountInfos, function(saleAccountInfo) {
  716. // saleAccountInfo.filterAccount = hideBankFilter(saleAccountInfo.number);
  717. if(saleAccountInfo.currency === 'RMB') {
  718. $scope.saleRMBAccount = saleAccountInfo;
  719. }
  720. if(saleAccountInfo.currency === 'USD') {
  721. $scope.saleUSDAccount = saleAccountInfo;
  722. }
  723. });
  724. $scope.saleEnterAccounts = $scope.saleAccountInfos;
  725. }, function(res) {
  726. toaster.pop('error', '错误', '获取商城账户信息失败');
  727. });
  728. }
  729. }
  730. getSellerAccount();
  731. /***********************************************************************
  732. * order info
  733. ***********************************************************************/
  734. $scope.orderResult = true;//存放订单提交结果 orderInfo
  735. // 提取要提交到服务器的订单明细数据
  736. var convertOrderDetails = function() {
  737. var orderDetails = [];
  738. angular.forEach($scope.order.orderDetails, function(v, k) {
  739. orderDetails.push({
  740. id: v.id,
  741. number: v.number,
  742. taxUnitprice: v.taxUnitprice
  743. });
  744. });
  745. return orderDetails;
  746. };
  747. // 产生要提交的单个订单数据
  748. var generateOrderInfo = function() {
  749. /*订单信息:orderInfo含有信息:id、orderid、deliverytype、add_id、invoicetype、invoiceid、paytype、totalprice(price)、
  750. * currency、orderRemark、orderDetails(id, number, taxUnitprice)*/
  751. var orderInfo = {};
  752. orderInfo.id = $scope.order.id;
  753. orderInfo.orderid = $scope.order.orderid;
  754. orderInfo.deliverytype = $scope.order.deliverytype; // 配送方式
  755. // 收货地址,上门自提暂不提供
  756. if($scope.order.deliverytype == '1301'){
  757. /*var address = angular.fromJson($scope.selectAddress);
  758. delete address.isSelect;
  759. orderInfo.jsonAddress = angular.toJson(address);*/
  760. orderInfo.add_id = $scope.selectAddress.id;
  761. }else if($scope.order.deliverytype == '1302'){
  762. orderInfo.add_id = $scope.pickAddress.id;
  763. }
  764. orderInfo.invoicetype = $scope.order.invoicetype; // 发票类型
  765. orderInfo.invoiceid = $scope.selecInvoice.id // 发票主键
  766. orderInfo.paytype = $scope.order.paytype; // 支付类型
  767. orderInfo.totalprice = $scope.productsAmount + $scope.importExtraCharges; // 应付总额
  768. orderInfo.currency = $scope.order.currency; // 币别
  769. orderInfo.orderRemark = $scope.order.orderRemark; // 交易备注
  770. orderInfo.orderDetails = convertOrderDetails(); // 订单明细信息:id、number
  771. return orderInfo;
  772. }
  773. /***********************************************************************
  774. * order submit
  775. ***********************************************************************/
  776. // 是否显示取消订单的警告
  777. $scope.showCancelTip = function(show){
  778. $scope.isShowCancelTip = show;
  779. };
  780. //返回之前的地方
  781. $scope.back = function(){
  782. /*TODO
  783. * 这里需求是如果有来源信息(由【购物车】,【立即购买】跳转而来),返回按钮才回显示出来。
  784. * */
  785. //将此订单失效掉
  786. Order.cancle({orderid: $scope.orderid}, {}, function(data){
  787. }, function(res){
  788. });
  789. window.location.replace('product#/cart');
  790. };
  791. // 确认付款
  792. var paymentEnsure= function() {
  793. // 银盛支付
  794. $scope.ysepayRequest={};
  795. $scope.ysepayRequest.orderid = $scope.order.orderid;// 唯一订单号
  796. // 签名
  797. Ysepay.paymentSign({orderid:$scope.ysepayRequest.orderid},{},function(data) {
  798. $scope.ysepayRequest = data;
  799. console.log($scope.ysepayRequest);
  800. $scope.$apply();
  801. var form = document.getElementById('paymentForm');
  802. form.action = "https://openapi.ysepay.com/gateway.do";
  803. form.method = "POST";
  804. form.submit();
  805. },function(res){
  806. toaster.pop('error', '获取支付请求参数错误', res.data);
  807. });
  808. };
  809. // 根据订单明细id得到购买数量
  810. var getPurchaseNumber = function(id, orderDetails) {
  811. for(var i = 0; i < orderDetails.length; i++) {
  812. var detail = orderDetails[i];
  813. if(detail.id == id) {
  814. return detail.number;
  815. }
  816. }
  817. }
  818. // 填充之前用户输入的数据
  819. var fillPrevUserInput = function() {
  820. var orderInfo = angular.fromJson(SessionService.get($scope.order.orderid + "-" + "ensureOrderInput"));
  821. // 填充购买数量
  822. if(orderInfo) {
  823. angular.forEach($scope.order.orderDetails, function(detail, k) {
  824. detail.number = getPurchaseNumber(detail.id, orderInfo.orderDetails);
  825. })
  826. $scope.order.deliverytype = orderInfo.deliverytype; // 1301UU配送、1302上门自提
  827. $scope.order.invoicetype = orderInfo.invoicetype; // 1205专用增值税发票、1206普通发票、1207不开发票
  828. $scope.order.paytype = orderInfo.paytype; // 1102在线支付、1103线下付款
  829. $scope.order.currency = orderInfo.currency; // 币别
  830. $scope.order.orderRemark = orderInfo.orderRemark; // 交易备注
  831. }
  832. }
  833. // 确认提交订单
  834. $scope.ensureOrder = function() {
  835. var orderInfos = [], orderInfo;
  836. orderInfo = generateOrderInfo();
  837. orderInfos.push(orderInfo);
  838. SessionService.set($scope.order.orderid + "-" + "ensureOrderInput", angular.toJson(orderInfo)); // 保存用户输入的信息,用于订单确认失败时恢复
  839. Order.ensure({orderid: $scope.orderid}, orderInfos, function(data){
  840. var availabletime = $filter('date')(data[0].availabletime,'yyyy-MM-dd HH:MM');
  841. toaster.pop('success', '提示 ', '您成功确认订单,请于' + availabletime + '之前付款');
  842. if($scope.order.paytype == 1102) {
  843. paymentEnsure();
  844. }else {
  845. $location.path("orderEnsured/" +$scope.orderid);
  846. }
  847. }, function(res){
  848. $scope.loading = true;
  849. toaster.pop('error', '订单确认失败', res.data + '<br>正在为你更新订单数据,请重新确认...', 3000, 'trustedHtml');
  850. /*$scope.time = 5;
  851. $scope.errorData = res.data;
  852. setTime();*/
  853. $q.all([getOrderData().$promise]).then(function() {
  854. fillPrevUserInput();
  855. $scope.selectInvoiceType($scope.order.invoicetype);
  856. initCurrency(); // 初始化币别
  857. initOrderDetails($scope.order); //订单明细初始化,需要根据币别来计算taxUnitPrice,因此需在获取地址后进行。
  858. calculateQtyAndAmount();
  859. checkCanSubmitOrderOrNot(); //检查订单是否可提交
  860. $scope.loading = false;
  861. })
  862. });
  863. };
  864. }]);
  865. app.register.controller('BankInfoCtrl', ['$scope', '$modalInstance', 'account', 'kind', function($scope, $modalInstance, account, kind){
  866. $scope.account = account;
  867. if($scope.account) {
  868. $scope.eidt = true;
  869. } else {
  870. delete $scope.eidt;
  871. }
  872. $scope.kind = kind;
  873. if($scope.account) {
  874. $scope.title = "修改账户";
  875. }else {
  876. $scope.title = "新增账户";
  877. $scope.account = {};
  878. }
  879. $scope.set = function(kind) {
  880. $scope.kind = kind;
  881. }
  882. $scope.confirm = function() {
  883. $scope.account.kind = $scope.kind;
  884. $modalInstance.close($scope.account);
  885. }
  886. $scope.cancel = function() {
  887. $modalInstance.dismiss();
  888. }
  889. }]);
  890. //申请议价
  891. app.register.controller('applyChargeCtrl', ['$scope', '$modalInstance', 'toaster', '$http', 'detail', 'Order', function($scope, $modalInstance, toaster, $http, detail, Order){
  892. $scope.charge = detail;
  893. $scope.charge.originalPrice = $scope.charge.unitprice;
  894. $scope.save = function () {
  895. //检查一下数据是否有缺失
  896. if($scope.charge) {
  897. Order.applyCharge({detailid:$scope.charge.id}, $scope.charge, function(data) {
  898. toaster.pop('success', '申请议价成功!');
  899. $modalInstance.close(data);
  900. }, function(res){
  901. toaster.pop('info', '状态非法,不允许议价');
  902. })
  903. }
  904. }
  905. $scope.cancel = function() {
  906. $scope.ysepayRequest={};
  907. $modalInstance.dismiss();
  908. };
  909. }]);
  910. //地址编辑模态框
  911. app.register.controller('editAddrCtrl', ['$scope', 'isSetTop', 'addr', '$modalInstance', 'toaster', '$http', 'ShippingAddress', function($scope, isSetTop, addr, $modalInstance, toaster, $http, ShippingAddress){
  912. $scope.isSetTop = isSetTop;
  913. //验证数据
  914. $scope.checkeds = {};
  915. $scope.checkform = function(name,num) {
  916. if(num == 1) {
  917. if(angular.isUndefined(name)) {
  918. $scope.checkeds.name = false;
  919. } else {
  920. $scope.checkeds.name = true;
  921. }
  922. } else if(num == 2) {
  923. if(angular.isUndefined(name)) {
  924. $scope.checkeds.detailAddress = false;
  925. } else {
  926. $scope.checkeds.detailAddress = true;
  927. }
  928. } else if(num == 3) {
  929. if(angular.isUndefined(name)) {
  930. $scope.checkeds.tel = false;
  931. } else {
  932. $scope.checkeds.tel = true;
  933. }
  934. }
  935. }
  936. $http.get('static/js/prod/data/city.json').success(function(data) {
  937. $scope.division = data;
  938. if(addr){
  939. $scope.address = addr;
  940. //拼装下拉选择框
  941. var arr = addr.area.split(',');
  942. addr.province = arr[0];
  943. addr.city = arr[1];
  944. addr.district = arr[2];
  945. $scope.address = addr;
  946. $scope.addr = true;
  947. }
  948. }).error(function(e) {
  949. toaster.pop('error', '系统错误 ' + '加载城市信息失败');
  950. });
  951. $scope.save = function () {
  952. var address = $scope.address;
  953. //拼装地区
  954. /**
  955. * TODO 这里没做校验
  956. */
  957. var strAres = address.province + ',' + address.city + ',' + address.district;
  958. address.area = strAres;
  959. // send属性 控制本地址是否是发货地址
  960. ShippingAddress.save({isSetTop: $scope.isSetTop, send: false, isPersonal: true}, address, function(data){
  961. toaster.pop('success', '成功 ', '保存收货地址成功');
  962. $modalInstance.close(data);
  963. }, function(res){
  964. toaster.pop('error', '系统错误 ', '保存收货地址失败');
  965. });
  966. }
  967. $scope.cancel = function() {
  968. $modalInstance.dismiss();
  969. };
  970. }]);
  971. // 发票编辑模态框
  972. app.register.controller('BillInputCtrl', ['$scope', '$http', 'BaseService', 'Bill', 'toaster', '$stateParams', '$state', 'invoiceInfo', '$upload', '$modalInstance', function($scope, $http, BaseService, Bill, toaster, $stateParams, $state, invoiceInfo, $upload, $modalInstance) {
  973. //BaseService.scrollBackToTop();
  974. /*$scope.bill = {};
  975. $scope.bill.address = {};
  976. $scope.bill.is_agree = true;
  977. $scope.bill.kind = 1206;
  978. $scope.isNormal = true;
  979. $scope.isSpecial = true;
  980. $scope.invoiceType = Number(invoiceInfo.split("-")[0]);
  981. if(invoiceInfo.split("-").length == 2) {
  982. $scope.invoiceId = Number(invoiceInfo.split("-")[1]);
  983. }
  984. $scope.setType = function() {
  985. switch($scope.invoiceType) {
  986. case 1206:
  987. $scope.bill.kind = 1206;
  988. $scope.isNormal = true;
  989. $scope.isSpecial = false; break;
  990. case 1205:
  991. $scope.bill.kind = 1205;
  992. $scope.isNormal = false;
  993. $scope.isSpecial = true; break;
  994. default:
  995. $scope.isNormal = true;
  996. $scope.isSpecial = true;
  997. }
  998. }
  999. $scope.setType();
  1000. $scope.getData = function() {
  1001. if($scope.invoiceId) {
  1002. Bill.getBillById({id: $scope.invoiceId}, function(data) {
  1003. $scope.bill = data;
  1004. if($scope.bill.kind == 1205) {
  1005. $scope.isNormal = false;
  1006. }else {
  1007. $scope.isSpecial = false;
  1008. }
  1009. $http.get('static/js/prod/data/city.json').success(function(data) {
  1010. $scope.division = data;
  1011. if($scope.bill.area){
  1012. $scope.bill.address = {};
  1013. //拼装下拉选择框
  1014. var arr = $scope.bill.area.split(',');
  1015. $scope.bill.address.province = arr[0];
  1016. $scope.bill.address.city = arr[1];
  1017. $scope.bill.address.district = arr[2];
  1018. }
  1019. }).error(function(e) {
  1020. toaster.pop('error', '系统错误 ' + '加载城市信息失败, 请重新加载界面!');
  1021. });
  1022. $scope.bill.is_agree = true;
  1023. }, function(response) {
  1024. toaster.pop('error', '获取指定的发票信息失败');
  1025. });
  1026. }else {
  1027. $http.get('static/js/prod/data/city.json').success(function(data) {
  1028. $scope.division = data;
  1029. }).error(function(e) {
  1030. toaster.pop('error', '系统错误 ' + '加载城市信息失败');
  1031. });
  1032. }
  1033. }
  1034. $scope.getData();
  1035. $scope.saveBill = function() {
  1036. $scope.bill.area = $scope.bill.address.province + "," + $scope.bill.address.city + "," + $scope.bill.address.district;
  1037. var file = null
  1038. if($scope.bill.billInfo&&$scope.bill.billInfo[0]) {
  1039. file = $scope.bill.billInfo[0];
  1040. }
  1041. $upload.upload({
  1042. url: 'trade/bill/save',
  1043. file: file,
  1044. method: 'POST',
  1045. data: {
  1046. bill: $scope.bill
  1047. }
  1048. }).success(function(data){
  1049. toaster.pop('success', '保存发票信息成功');
  1050. $modalInstance.close(data);
  1051. }).error(function(data){
  1052. toaster.pop('error', '保存发票信息失败');
  1053. });
  1054. };
  1055. $scope.exit = function() {
  1056. $modalInstance.dismiss();
  1057. }*/
  1058. $scope.bill = {};
  1059. $scope.invoiceType = Number(invoiceInfo.split("-")[0]);
  1060. if(invoiceInfo.split("-").length == 2) {
  1061. $scope.invoiceId = Number(invoiceInfo.split("-")[1]);
  1062. }
  1063. $scope.isSpecial = true; //专票,等于true为不存在
  1064. $scope.isNormal = true; //普票,等于true为不存在
  1065. // 获取发票信息方法 1205为增值税专用发票 1206为增值税普通发票 1207为不开发票
  1066. $scope.setType = function() {
  1067. switch($scope.invoiceType) {
  1068. case 1206:
  1069. $scope.bill.kind = 1206;
  1070. $scope.isNormal = true;
  1071. $scope.isSpecial = false; break;
  1072. case 1205:
  1073. $scope.bill.kind = 1205;
  1074. $scope.isNormal = false;
  1075. $scope.isSpecial = true; break;
  1076. default:
  1077. $scope.isNormal = true;
  1078. $scope.isSpecial = true;
  1079. }
  1080. };
  1081. $scope.setType();
  1082. $scope.getData = function() {
  1083. if($scope.invoiceId) {
  1084. Bill.getBillById({id: $scope.invoiceId}, function(data) {
  1085. $scope.bill = data;
  1086. if($scope.bill.kind == 1205) {
  1087. $scope.isNormal = false;
  1088. }else {
  1089. $scope.isSpecial = false;
  1090. }
  1091. $http.get('static/js/prod/data/city.json').success(function(data) {
  1092. $scope.division = data;
  1093. if($scope.bill.area){
  1094. $scope.bill.address = {};
  1095. //拼装下拉选择框
  1096. var arr = $scope.bill.area.split(',');
  1097. $scope.bill.address.province = arr[0];
  1098. $scope.bill.address.city = arr[1];
  1099. $scope.bill.address.district = arr[2];
  1100. }
  1101. }).error(function(e) {
  1102. toaster.pop('error', '系统错误 ' + '加载城市信息失败, 请重新加载界面!');
  1103. });
  1104. $scope.bill.is_agree = true;
  1105. }, function(response) {
  1106. toaster.pop('error', '获取指定的发票信息失败');
  1107. });
  1108. }else {
  1109. $http.get('static/js/prod/data/city.json').success(function(data) {
  1110. $scope.division = data;
  1111. }).error(function(e) {
  1112. toaster.pop('error', '系统错误 ' + '加载城市信息失败');
  1113. });
  1114. }
  1115. };
  1116. $scope.getData();
  1117. $scope.bill.address = {};
  1118. //保存发票信息
  1119. $scope.saveBill = function(flag) {
  1120. var dataValidFlag = $scope.checkValidFrom();
  1121. if (!flag && dataValidFlag && $scope.bill.is_agree) {
  1122. if (!$scope.isAdd) { //修改
  1123. doSave('修改发票信息');
  1124. } else { // 新增
  1125. doSave('添加发票');
  1126. }
  1127. } else if (flag || !dataValidFlag) {
  1128. toaster.pop('error', '请填写正确的发票信息');
  1129. } else {
  1130. toaster.pop('error', '请勾选并阅读《发票须知》');
  1131. }
  1132. };
  1133. var doSave = function (message) {
  1134. $scope.bill.area = $scope.bill.address.province + "," + $scope.bill.address.city + "," + $scope.bill.address.district;
  1135. var file = null;
  1136. if($scope.bill.billInfo&&$scope.bill.billInfo[0]) {
  1137. file = $scope.bill.billInfo[0];
  1138. }
  1139. $upload.upload({
  1140. url: 'trade/bill/save',
  1141. file: file,
  1142. method: 'POST',
  1143. data: {
  1144. bill: $scope.bill
  1145. }
  1146. }).success(function(data){
  1147. toaster.pop('success', message + '成功');
  1148. $modalInstance.close(data);
  1149. }).error(function(data){
  1150. toaster.pop('error', message + '失败');
  1151. });
  1152. }
  1153. $scope.isDoUpload = false;
  1154. //上传发票许可证
  1155. $scope.onUploadPermission = function () {
  1156. $scope.isDoUpload = true;
  1157. if (event.target.files[0].size < 3*1024*1024) {
  1158. $scope.bill.attachUrl = event.target.files[0].name;
  1159. } else {
  1160. $scope.bill.attachUrl = '';
  1161. }
  1162. }
  1163. //判断中文字符串的长度
  1164. var getRealStringLen = function (str) {
  1165. var realLength = 0, len = str.length, charCode = -1;
  1166. for (var i = 0; i < len; i++) {
  1167. charCode = str.charCodeAt(i);
  1168. if (charCode >= 0 && charCode <= 128) realLength += 1;
  1169. else realLength += 2;
  1170. }
  1171. return realLength;
  1172. }
  1173. $scope.validForm = {
  1174. validBillHead: true,
  1175. validBillName: true,
  1176. validBankName: true,
  1177. validDetailAddress: true,
  1178. validCompanyAddress: true
  1179. }
  1180. $scope.initFormFlag = function () {
  1181. $scope.initFlag = {
  1182. initBillHead: true,
  1183. initBillName: true,
  1184. initBankName: true,
  1185. initDetailAddress: true,
  1186. initCompanyAddress: true,
  1187. initCompanyPhone: true,
  1188. initCompanyTaxNum: true,
  1189. initBankAccount: true,
  1190. initTelephone: true
  1191. }
  1192. }
  1193. $scope.initFormFlag();
  1194. $scope.checkValidFrom = function () {
  1195. var flag = true
  1196. angular.forEach($scope.validForm, function (item) {
  1197. if (!item) {
  1198. flag = false;
  1199. }
  1200. })
  1201. return flag;
  1202. }
  1203. $scope.checkValidFrom();
  1204. //发票抬头check
  1205. $scope.checkBillHead = function () {
  1206. var len = getRealStringLen($scope.bill.head);
  1207. if (len > 100) {
  1208. $scope.validForm.validBillHead = false;
  1209. } else {
  1210. $scope.validForm.validBillHead = true;
  1211. }
  1212. }
  1213. //收票人check
  1214. $scope.checkBillName = function () {
  1215. var len = getRealStringLen($scope.bill.name);
  1216. if (len > 20) {
  1217. $scope.validForm.validBillName = false;
  1218. } else {
  1219. $scope.validForm.validBillName = true;
  1220. }
  1221. }
  1222. //开户银行Check
  1223. $scope.checkBankName = function () {
  1224. var len = getRealStringLen($scope.bill.bankName);
  1225. if (len > 60) {
  1226. $scope.validForm.validBankName = false;
  1227. } else {
  1228. $scope.validForm.validBankName = true;
  1229. }
  1230. }
  1231. //详细地址Check
  1232. $scope.checkDetailAddress = function () {
  1233. var len = getRealStringLen($scope.bill.detailAddress);
  1234. if (len > 60) {
  1235. $scope.validForm.validDetailAddress = false;
  1236. } else {
  1237. $scope.validForm.validDetailAddress = true;
  1238. }
  1239. }
  1240. //单位地址check
  1241. $scope.checkCompanyAddress = function () {
  1242. var len = getRealStringLen($scope.bill.companyAddress);
  1243. if (len > 100) {
  1244. $scope.validForm.validCompanyAddress = false;
  1245. } else {
  1246. $scope.validForm.validCompanyAddress = true;
  1247. }
  1248. }
  1249. $scope.exitEdit = function () {
  1250. $modalInstance.dismiss();
  1251. }
  1252. }]);
  1253. // 订单详情模态框
  1254. app.register.controller('BillInfoCtrl', ['$scope', '$modalInstance', 'bill', function($scope, $modalInstance, bill) {
  1255. $scope.bill = bill;
  1256. $scope.dismiss = function() {
  1257. $modalInstance.dismiss();
  1258. }
  1259. }]);
  1260. });