OLDataProvider.m 391 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550
  1. //
  2. // OLDataProvider.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 2/2/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "OLDataProvider.h"
  9. #import "iSalesDB.h"
  10. #import "RAUtils.h"
  11. #import "AESCrypt.h"
  12. #import "AppDelegate.h"
  13. #import "ZipArchive.h"
  14. #import "UIProgressView+AFNetworking.h"
  15. #import "AFHTTPSessionManager.h"
  16. @interface OLDataProvider ()
  17. @end
  18. @implementation OLDataProvider
  19. +(NSArray*) enumOfflineOrder
  20. {
  21. NSMutableArray* ret = [[NSMutableArray alloc] init];
  22. sqlite3 *db = [iSalesDB get_db];
  23. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  24. sqlite3_stmt * statement;
  25. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  26. if ( dbresult== SQLITE_OK)
  27. {
  28. while (sqlite3_step(statement) == SQLITE_ROW)
  29. {
  30. char *so_id = (char*)sqlite3_column_text(statement, 0);
  31. NSString* nsso_id=nil;
  32. if(so_id!=nil)
  33. {
  34. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  35. }
  36. [ret addObject:nsso_id];
  37. }
  38. sqlite3_finalize(statement);
  39. }
  40. [iSalesDB close_db:db];
  41. return ret;
  42. }
  43. +(void) uploadFile:(NSString*) file
  44. {
  45. NSData* data = [NSData dataWithContentsOfFile: file];
  46. UIApplication * app = [UIApplication sharedApplication];
  47. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  48. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  49. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  50. if(appDelegate.user!=nil)
  51. [params setValue:appDelegate.user forKey:@"user"];
  52. // if(appDelegate.contact_id!=nil)
  53. // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  54. if(appDelegate.password!=nil)
  55. [params setValue:appDelegate.password forKey:@"password"];
  56. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  57. [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  58. } error:nil];
  59. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  60. NSProgress *progress = nil;
  61. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  62. if (error) {
  63. NSString* err_msg = [error localizedDescription];
  64. NSLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  65. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  66. NSLog(@"data string: %@",str);
  67. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  68. } else {
  69. NSLog(@"response ");
  70. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  71. // 再将NSData转为字符串
  72. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  73. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  74. NSLog(@"data string: %@",jsonStr);
  75. NSDictionary* json = responseObject;
  76. if([[json valueForKey:@"result"] intValue]==2)
  77. {
  78. // NSString* img_url_down = json[@"img_url_aname"];
  79. // NSString* img_url_up = json[@"img_url"];
  80. }
  81. else
  82. {
  83. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  84. }
  85. }
  86. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  87. }];
  88. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  89. //
  90. [uploadTask resume];
  91. }
  92. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  93. {
  94. NSString* serial= [[NSUUID UUID] UUIDString];
  95. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  96. NSString *cachefolder = [paths objectAtIndex:0];
  97. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  98. upfolder = [upfolder stringByAppendingPathComponent:serial];
  99. NSFileManager* fileManager = [NSFileManager defaultManager];
  100. BOOL bdir=YES;
  101. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  102. {
  103. NSError *error = nil;
  104. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  105. if(!bsuccess)
  106. DebugLog(@"Create UPLOAD folder failed");
  107. }
  108. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  109. ret[@"contact"]=[self prepareContact:serial];
  110. ret[@"wishlist"]=[self prepareWishlist:serial];
  111. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  112. NSString* str= [RAUtils dict2string:ret];
  113. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  114. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  115. NSError *error=nil;
  116. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  117. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  118. ZipArchive* zip = [[ZipArchive alloc] init];
  119. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  120. for(int i=0;i<arr_files.count;i++)
  121. {
  122. NSString* file=arr_files[i];
  123. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  124. }
  125. if( ![zip CloseZipFile2] )
  126. {
  127. zippath = @"";
  128. }
  129. ret[@"file"]=zippath;
  130. return ret;
  131. }
  132. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  133. {
  134. if(filename.length==0)
  135. return false;
  136. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  137. bool ret=false;
  138. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  139. NSString *cachefolder = [paths objectAtIndex:0];
  140. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  141. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  142. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  143. upfolder = [upfolder stringByAppendingPathComponent:serial];
  144. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  145. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  146. NSFileManager* fileManager = [NSFileManager defaultManager];
  147. BOOL bdir=NO;
  148. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  149. {
  150. NSError *error = nil;
  151. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  152. {
  153. ret=false;
  154. }
  155. else
  156. {
  157. ret=true;
  158. }
  159. }
  160. return ret;
  161. }
  162. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  163. {
  164. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  165. sqlite3 *db = [iSalesDB get_db];
  166. NSString *sqlQuery =@"select _id,product_id,item_id,qty,create_time from wishlist";
  167. sqlite3_stmt * statement;
  168. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  169. int count=0;
  170. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  171. if ( dbresult== SQLITE_OK)
  172. {
  173. while (sqlite3_step(statement) == SQLITE_ROW)
  174. {
  175. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  176. int _id = sqlite3_column_int(statement, 0);
  177. int product_id = sqlite3_column_int(statement, 1);
  178. int item_id = sqlite3_column_int(statement, 2);
  179. int qty = sqlite3_column_int(statement, 3);
  180. char *create_time = (char*)sqlite3_column_text(statement, 4);
  181. NSString* nscreate_time=nil;
  182. if(create_time!=nil)
  183. {
  184. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  185. }
  186. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  187. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  188. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  189. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  190. itemjson[@"create_time"]=nscreate_time;
  191. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  192. count++;
  193. }
  194. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  195. sqlite3_finalize(statement);
  196. }
  197. ret[@"count"]=[NSNumber numberWithInt:count ];
  198. [iSalesDB close_db:db];
  199. return ret;
  200. }
  201. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  202. {
  203. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  204. UIApplication * app = [UIApplication sharedApplication];
  205. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  206. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",orderCode ];
  207. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  208. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  209. DebugLog(@"offline_login sql:%@",sqlQuery);
  210. sqlite3_stmt * statement;
  211. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  212. int count=0;
  213. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  214. if ( dbresult== SQLITE_OK)
  215. {
  216. while (sqlite3_step(statement) == SQLITE_ROW)
  217. {
  218. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  219. int product_id = sqlite3_column_int(statement, 0);
  220. char *str_price = (char*)sqlite3_column_text(statement, 1);
  221. int item_id = sqlite3_column_int(statement, 7);
  222. NSString* Price=nil;
  223. if(str_price==nil)
  224. {
  225. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  226. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  227. if(price==nil)
  228. Price=@"No Price.";
  229. else
  230. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  231. }
  232. else
  233. {
  234. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  235. }
  236. double discount = sqlite3_column_double(statement, 2);
  237. int item_count = sqlite3_column_int(statement, 3);
  238. char *line_note = (char*)sqlite3_column_text(statement, 4);
  239. NSString *nsline_note=nil;
  240. if(line_note!=nil)
  241. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  242. char *name = (char*)sqlite3_column_text(statement, 5);
  243. NSString *nsname=nil;
  244. if(name!=nil)
  245. nsname= [[NSString alloc]initWithUTF8String:name];
  246. char *description = (char*)sqlite3_column_text(statement, 6);
  247. NSString *nsdescription=nil;
  248. if(description!=nil)
  249. nsdescription= [[NSString alloc]initWithUTF8String:description];
  250. // int stockUom = sqlite3_column_int(statement, 8);
  251. // int _id = sqlite3_column_int(statement, 9);
  252. //
  253. //
  254. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  255. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  256. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  257. // int carton=[bsubtotaljson[@"carton"] intValue];
  258. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  259. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  260. itemjson[@"model"]=nsname;
  261. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  262. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  263. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  264. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  265. // itemjson[@"check"]=@"true";
  266. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  267. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  268. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  269. itemjson[@"unit_price"]=Price;
  270. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  271. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  272. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  273. itemjson[@"note"]=nsline_note;
  274. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  275. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  276. count++;
  277. }
  278. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  279. sqlite3_finalize(statement);
  280. }
  281. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  282. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  283. return ret;
  284. }
  285. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  286. {
  287. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  288. sqlite3 *db = [iSalesDB get_db];
  289. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  290. // for(int i=0;i<arr_soid.count;i++)
  291. sqlite3_stmt * statement;
  292. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  293. //int count=0;
  294. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  295. if ( dbresult== SQLITE_OK)
  296. {
  297. int count=0;
  298. while (sqlite3_step(statement) == SQLITE_ROW)
  299. {
  300. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  301. int _id = sqlite3_column_int(statement, 0);
  302. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  303. NSString* nssync_data=nil;
  304. if(sync_data!=nil)
  305. {
  306. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  307. }
  308. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  309. NSString* nsimg_1=nil;
  310. if(img_1!=nil)
  311. {
  312. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  313. [self copy_upImg:serial file:nsimg_1];
  314. }
  315. char *so_no = (char*)sqlite3_column_text(statement, 3);
  316. NSString* nsso_no=nil;
  317. if(so_no!=nil)
  318. {
  319. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  320. }
  321. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  322. itemjson[@"order_type"]=@"submit order";
  323. else
  324. itemjson[@"order_type"]=@"archive order";
  325. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  326. itemjson[@"json_data"]= nssync_data;
  327. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  328. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  329. count++;
  330. }
  331. ret[@"count"]=[NSNumber numberWithInt:arr_soid.count ];
  332. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  333. }
  334. sqlite3_finalize(statement);
  335. [iSalesDB close_db:db];
  336. return ret;
  337. }
  338. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  339. {
  340. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  341. sqlite3 *db = [iSalesDB get_db];
  342. // UIApplication * app = [UIApplication sharedApplication];
  343. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  344. NSString *sqlQuery =@"select _id,img_0,img_1,img_2,sync_data from offline_contact where is_active=1 and not sync_data is null";
  345. sqlite3_stmt * statement;
  346. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  347. int count=0;
  348. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  349. if ( dbresult== SQLITE_OK)
  350. {
  351. while (sqlite3_step(statement) == SQLITE_ROW)
  352. {
  353. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  354. int _id = sqlite3_column_int(statement, 0);
  355. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  356. NSString* nsimg_0=nil;
  357. if(img_0!=nil)
  358. {
  359. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  360. [self copy_upImg:serial file:nsimg_0];
  361. }
  362. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  363. NSString* nsimg_1=nil;
  364. if(img_1!=nil)
  365. {
  366. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  367. [self copy_upImg:serial file:nsimg_1];
  368. }
  369. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  370. NSString* nsimg_2=nil;
  371. if(img_2!=nil)
  372. {
  373. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  374. [self copy_upImg:serial file:nsimg_2];
  375. }
  376. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  377. NSString* nssync_data=nil;
  378. if(sync_data!=nil)
  379. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  380. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  381. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  382. itemjson[@"json_data"]= nssync_data;
  383. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  384. count++;
  385. }
  386. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  387. sqlite3_finalize(statement);
  388. }
  389. ret[@"count"]=[NSNumber numberWithInt:count ];
  390. [iSalesDB close_db:db];
  391. return ret;
  392. }
  393. +(bool) check_offlinedata
  394. {
  395. UIApplication * app = [UIApplication sharedApplication];
  396. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  397. sqlite3 *db = [iSalesDB get_db];
  398. NSString * where=@"1=1";
  399. // if(appDelegate.user!=nil)
  400. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  401. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  402. [iSalesDB close_db:db];
  403. if(count==0)
  404. {
  405. return false;
  406. }
  407. return true;
  408. //
  409. //[iSalesDB close_db:db];
  410. }
  411. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  412. {
  413. UIApplication * app = [UIApplication sharedApplication];
  414. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  415. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  416. sqlite3 *db = [iSalesDB get_db];
  417. NSString* collectId=params[@"collectId"];
  418. NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  419. [iSalesDB execSql:sqlQuery db:db];
  420. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  421. [iSalesDB close_db:db];
  422. appDelegate.wish_count =count;
  423. [appDelegate update_count_mark];
  424. ret[@"result"]= [NSNumber numberWithInt:2];
  425. return ret;
  426. }
  427. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  428. //{
  429. //
  430. // UIApplication * app = [UIApplication sharedApplication];
  431. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  432. //
  433. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  434. // sqlite3 *db = [iSalesDB get_db];
  435. // NSString* product_id=params[@"product_id"];
  436. //
  437. //
  438. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  439. //
  440. //
  441. // for(int i=0;i<arr.count;i++)
  442. // {
  443. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  444. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  445. // if(count==0)
  446. // {
  447. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  448. // [iSalesDB execSql:sqlQuery db:db];
  449. // }
  450. // }
  451. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  452. // [iSalesDB close_db:db];
  453. //
  454. // appDelegate.wish_count =count;
  455. //
  456. // [appDelegate update_count_mark];
  457. // ret[@"result"]= [NSNumber numberWithInt:2];
  458. // return ret;
  459. // //
  460. // //return ret;
  461. //}
  462. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  463. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  464. DebugLog(@"time interval: %lf",interval);
  465. }
  466. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  467. {
  468. UIApplication * app = [UIApplication sharedApplication];
  469. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  470. int sort = [[params objectForKey:@"sort"] intValue];
  471. NSString *sort_str = @"";
  472. switch (sort) {
  473. case 0:{
  474. sort_str = @"order by w.modify_time desc";
  475. }
  476. break;
  477. case 1:{
  478. sort_str = @"order by w.modify_time asc";
  479. }
  480. break;
  481. case 2:{
  482. sort_str = @"order by m.name asc";
  483. }
  484. break;
  485. case 3:{
  486. sort_str = @"order by m.name desc";
  487. }
  488. break;
  489. case 4:{
  490. sort_str = @"order by m.description asc";
  491. }
  492. break;
  493. default:
  494. break;
  495. }
  496. NSString* user = appDelegate.user;
  497. sqlite3 *db = [iSalesDB get_db];
  498. // order by w.create_time
  499. NSString* sqlQuery = [NSString stringWithFormat:@"select w.product_id,m.name||'\n'||m.description,w._id,w.qty from wishlist as w left join product as m on w.product_id=m.product_id %@ ;",sort_str];
  500. // NSString* sqlQuery = [NSString stringWithFormat:@"select m.product_id,(m.name||'\n'||m.description) as des,w._id from product as m,wishlist as w where m.product_id = w.product_id %@",sort_str];
  501. sqlite3_stmt * statement;
  502. NSDate *date1 = [NSDate date];
  503. NSDate *date2 = nil;
  504. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  505. int count=0;
  506. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  507. {
  508. while (sqlite3_step(statement) == SQLITE_ROW)
  509. {
  510. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  511. int product_id = sqlite3_column_double(statement, 0);
  512. char *description = (char*)sqlite3_column_text(statement, 1);
  513. if(description==nil)
  514. description= "";
  515. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  516. int item_id = sqlite3_column_double(statement, 2);
  517. NSDate *date_image = [NSDate date];
  518. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  519. printf("image : ");
  520. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  521. // char *url = (char*)sqlite3_column_text(statement, 3);
  522. // if(url==nil)
  523. // url="";
  524. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  525. int qty = sqlite3_column_int(statement, 3);
  526. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  527. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  528. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  529. item[@"description"]= nsdescription;
  530. item[@"img"]= nsurl;
  531. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  532. count++;
  533. }
  534. printf("total time:");
  535. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  536. ret[@"count"]= [NSNumber numberWithInt:count];
  537. ret[@"total_count"]= [NSNumber numberWithInt:count];
  538. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  539. ret[@"result"]= [NSNumber numberWithInt:2];
  540. appDelegate.wish_count =count;
  541. [appDelegate update_count_mark];
  542. sqlite3_finalize(statement);
  543. }
  544. [iSalesDB close_db:db];
  545. return ret;
  546. }
  547. +(NSDictionary*) offline_notimpl
  548. {
  549. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  550. ret[@"result"]=@"8";
  551. ret[@"err_msg"]=@"offline mode does not support this function.";
  552. return ret;
  553. }
  554. +(NSDictionary*) offline_home
  555. {
  556. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  557. NSString *cachefolder = [paths objectAtIndex:0];
  558. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  559. NSData* json =nil;
  560. json=[NSData dataWithContentsOfFile:img_cache];
  561. NSError *error=nil;
  562. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  563. return menu;
  564. }
  565. +(NSDictionary*) offline_category_menu
  566. {
  567. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  568. NSString *cachefolder = [paths objectAtIndex:0];
  569. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  570. NSData* json =nil;
  571. json=[NSData dataWithContentsOfFile:img_cache];
  572. NSError *error=nil;
  573. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  574. return menu;
  575. }
  576. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  577. {
  578. NSString* offline_command=params[@"offline_Command"];
  579. NSDictionary* ret=nil;
  580. if([offline_command isEqualToString:@"model_NIYMAL"])
  581. {
  582. NSString* category = params[@"category"];
  583. ret = [self refresh_model_NIYMAL:category];
  584. }
  585. return ret;
  586. }
  587. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  588. {
  589. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  590. [ret setValue:@"2" forKey:@"result"];
  591. [ret setValue:@"Regular Mode" forKey:@"mode"];
  592. sqlite3* db= [iSalesDB get_db];
  593. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  594. [iSalesDB close_db:db];
  595. [ret setObject:detail1_section forKey:@"detail_1"];
  596. return ret;
  597. }
  598. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  599. {
  600. UIApplication * app = [UIApplication sharedApplication];
  601. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  602. // NSArray* arr1 = [self get_user_all_price_type];
  603. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  604. // NSSet *set1 = [NSSet setWithArray:arr1];
  605. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  606. // if(appDelegate.contact_id==nil)
  607. // set2=[set1 mutableCopy];
  608. // else
  609. // [set2 intersectsSet:set1];
  610. // NSArray *retarr = [set2 allObjects];
  611. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  612. // sqlite3 *db = [iSalesDB get_db];
  613. NSString* sqlQuery = nil;
  614. if(appDelegate.contact_id==nil)
  615. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d order by a.order_by;",item_id];
  616. else
  617. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price from price as a left join model_price as b on a.name=b.price_name and b.item_id=%d and b.price_name in(%@) order by a.order_by;",item_id,whereprice];
  618. sqlite3_stmt * statement;
  619. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  620. int count=0;
  621. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  622. {
  623. while (sqlite3_step(statement) == SQLITE_ROW)
  624. {
  625. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  626. char *name = (char*)sqlite3_column_text(statement, 0);
  627. if(name==nil)
  628. name="";
  629. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  630. // double price = sqlite3_column_double(statement, 1);
  631. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  632. // if(isnull==nil)
  633. // item[nsname]= @"No Price";
  634. // else
  635. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  636. char *price = (char*)sqlite3_column_text(statement, 1);
  637. if(price!=nil)
  638. {
  639. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  640. nsprice=[AESCrypt fastdecrypt:nsprice];
  641. if(nsprice.length>0)
  642. {
  643. double dp= [nsprice doubleValue];
  644. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  645. }
  646. }
  647. else
  648. {
  649. item[nsname]= @"No Price";
  650. }
  651. // item[nsname]= nsprice;
  652. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  653. count++;
  654. }
  655. ret[@"count"]= [NSNumber numberWithInt:count];
  656. sqlite3_finalize(statement);
  657. }
  658. // [iSalesDB close_db:db];
  659. return ret;
  660. }
  661. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  662. //{
  663. // NSArray* arr1 = [self get_user_all_price_type:db];
  664. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  665. //
  666. // // NSSet *set1 = [NSSet setWithArray:arr1];
  667. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  668. // // [set2 intersectsSet:set1];
  669. // //
  670. // //
  671. // // NSArray *retarr = [set2 allObjects];
  672. //
  673. // NSString* whereprice=nil;
  674. // if(contact_id==nil)
  675. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  676. // else
  677. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  678. //
  679. //
  680. // // sqlite3 *db = [iSalesDB get_db];
  681. //
  682. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  683. // sqlite3_stmt * statement;
  684. //
  685. //
  686. // NSNumber* ret = nil;
  687. // double dprice=DBL_MAX;
  688. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  689. // {
  690. //
  691. //
  692. // while (sqlite3_step(statement) == SQLITE_ROW)
  693. // {
  694. //
  695. // // double val = sqlite3_column_double(statement, 0);
  696. // char *price = (char*)sqlite3_column_text(statement, 0);
  697. // if(price!=nil)
  698. // {
  699. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  700. // nsprice=[AESCrypt fastdecrypt:nsprice];
  701. // if(nsprice.length>0)
  702. // {
  703. // double dp= [nsprice doubleValue];
  704. // if(dp<dprice)
  705. // dprice=dp;
  706. // }
  707. // }
  708. // }
  709. //
  710. //
  711. //
  712. //
  713. // sqlite3_finalize(statement);
  714. //
  715. //
  716. //
  717. //
  718. // }
  719. //
  720. // // [iSalesDB close_db:db];
  721. //
  722. // if(dprice==DBL_MAX)
  723. // ret= nil;
  724. // else
  725. // ret= [NSNumber numberWithDouble:dprice];
  726. // return ret;
  727. //}
  728. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  729. {
  730. NSArray* arr1 = [self get_user_all_price_type:db];
  731. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  732. NSString* whereprice=nil;
  733. if(contact_id==nil)
  734. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  735. else
  736. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  737. // sqlite3 *db = [iSalesDB get_db];
  738. NSString *productIdCondition = @"1 = 1";
  739. if (product_id) {
  740. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  741. }
  742. NSString *itemIdCondition = @"";
  743. if (item_id) {
  744. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  745. }
  746. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  747. sqlite3_stmt * statement;
  748. NSNumber* ret = nil;
  749. double dprice=DBL_MAX;
  750. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  751. {
  752. while (sqlite3_step(statement) == SQLITE_ROW)
  753. {
  754. // double val = sqlite3_column_double(statement, 0);
  755. char *price = (char*)sqlite3_column_text(statement, 0);
  756. if(price!=nil)
  757. {
  758. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  759. nsprice=[AESCrypt fastdecrypt:nsprice];
  760. if(nsprice.length>0)
  761. {
  762. double dp= [nsprice doubleValue];
  763. if(dp<dprice)
  764. dprice=dp;
  765. }
  766. }
  767. }
  768. sqlite3_finalize(statement);
  769. }
  770. // [iSalesDB close_db:db];
  771. if(dprice==DBL_MAX)
  772. ret= nil;
  773. else
  774. ret= [NSNumber numberWithDouble:dprice];
  775. return ret;
  776. }
  777. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  778. {
  779. NSArray* ret=nil;
  780. // sqlite3 *db = [iSalesDB get_db];
  781. // no customer assigned , use login user contact_id
  782. UIApplication * app = [UIApplication sharedApplication];
  783. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  784. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  785. sqlite3_stmt * statement;
  786. // int count=0;
  787. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  788. {
  789. if (sqlite3_step(statement) == SQLITE_ROW)
  790. {
  791. char *val = (char*)sqlite3_column_text(statement, 0);
  792. if(val==nil)
  793. val="";
  794. NSString* price = [[NSString alloc]initWithUTF8String:val];
  795. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  796. }
  797. sqlite3_finalize(statement);
  798. }
  799. // [iSalesDB close_db:db];
  800. return ret;
  801. }
  802. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  803. {
  804. // sqlite3 *db = [iSalesDB get_db];
  805. if(contact_id==nil)
  806. {
  807. // no customer assigned , use login user contact_id
  808. UIApplication * app = [UIApplication sharedApplication];
  809. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  810. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  811. sqlite3_stmt * statement;
  812. // int count=0;
  813. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  814. {
  815. if (sqlite3_step(statement) == SQLITE_ROW)
  816. {
  817. char *val = (char*)sqlite3_column_text(statement, 0);
  818. if(val==nil)
  819. val="";
  820. contact_id = [[NSString alloc]initWithUTF8String:val];
  821. }
  822. sqlite3_finalize(statement);
  823. }
  824. if(contact_id.length<=0)
  825. {
  826. // [iSalesDB close_db:db];
  827. return nil;
  828. }
  829. }
  830. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  831. sqlite3_stmt * statement;
  832. NSArray* ret=nil;
  833. // int count=0;
  834. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  835. {
  836. if (sqlite3_step(statement) == SQLITE_ROW)
  837. {
  838. char *price_type = (char*)sqlite3_column_text(statement, 0);
  839. if(price_type==nil)
  840. price_type="";
  841. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  842. if(nsprice_type.length>0)
  843. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  844. else
  845. ret=nil;
  846. }
  847. sqlite3_finalize(statement);
  848. }
  849. // [iSalesDB close_db:db];
  850. return ret;
  851. }
  852. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  853. {
  854. NSString* ret= nil;
  855. // sqlite3 *db = [iSalesDB get_db];
  856. NSString *sqlQuery = nil;
  857. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  858. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  859. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  860. if(product_id==nil && model_name)
  861. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where product_id in (select product_id from product where name='%@') order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  862. else if (product_id)
  863. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where i.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  864. sqlite3_stmt * statement;
  865. // int count=0;
  866. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  867. {
  868. if (sqlite3_step(statement) == SQLITE_ROW)
  869. {
  870. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  871. if(imgurl==nil)
  872. imgurl="";
  873. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  874. ret=nsimgurl;
  875. }
  876. sqlite3_finalize(statement);
  877. }
  878. else
  879. {
  880. [ret setValue:@"8" forKey:@"result"];
  881. }
  882. // [iSalesDB close_db:db];
  883. DebugLog(@"data string: %@",ret );
  884. return ret;
  885. }
  886. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  887. {
  888. NSString* ret= nil;
  889. sqlite3 *db = [iSalesDB get_db];
  890. NSString *sqlQuery = nil;
  891. if(product_id==nil)
  892. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  893. else
  894. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  895. sqlite3_stmt * statement;
  896. // int count=0;
  897. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  898. {
  899. if (sqlite3_step(statement) == SQLITE_ROW)
  900. {
  901. char *default_category = (char*)sqlite3_column_text(statement, 0);
  902. if(default_category==nil)
  903. default_category="";
  904. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  905. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  906. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  907. ret=nsdefault_category;
  908. }
  909. sqlite3_finalize(statement);
  910. }
  911. else
  912. {
  913. [ret setValue:@"8" forKey:@"result"];
  914. }
  915. [iSalesDB close_db:db];
  916. DebugLog(@"data string: %@",ret );
  917. return ret;
  918. }
  919. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  920. {
  921. NSString* model_name = [params valueForKey:@"product_name"];
  922. NSString* product_id = [params valueForKey:@"product_id"];
  923. NSString* category = [params valueForKey:@"category"];
  924. if(category==nil)
  925. category = [self model_default_category:product_id model_name:model_name];
  926. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  927. sqlite3 *db = [iSalesDB get_db];
  928. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  929. NSString *sqlQuery = nil;
  930. if(product_id==nil)
  931. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from product where name='%@';",model_name]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where name='%@';
  932. else
  933. sqlQuery = [NSString stringWithFormat:@"select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging,item_id from product where product_id=%@;",product_id]; // select name,description,product_id,color,legcolor,availability,incoming_stock,demension,seat_height,material,box_dim,volume,weight,model_set,load_ability,default_category,fabric_content,assembling,made_in,special_remarks,stockUom,product_group,selector_field,property_field,packaging from model where product_id=%@;
  934. sqlite3_stmt * statement;
  935. [ret setValue:@"2" forKey:@"result"];
  936. [ret setValue:@"3" forKey:@"detail_section_count"];
  937. // int count=0;
  938. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  939. {
  940. if (sqlite3_step(statement) == SQLITE_ROW)
  941. {
  942. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  943. char *name = (char*)sqlite3_column_text(statement, 0);
  944. if(name==nil)
  945. name="";
  946. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  947. char *description = (char*)sqlite3_column_text(statement, 1);
  948. if(description==nil)
  949. description="";
  950. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  951. int product_id = sqlite3_column_int(statement, 2);
  952. char *color = (char*)sqlite3_column_text(statement, 3);
  953. if(color==nil)
  954. color="";
  955. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  956. //
  957. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  958. // if(legcolor==nil)
  959. // legcolor="";
  960. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  961. //
  962. //
  963. int availability = sqlite3_column_int(statement, 5);
  964. //
  965. // int incoming_stock = sqlite3_column_int(statement, 6);
  966. char *demension = (char*)sqlite3_column_text(statement, 7);
  967. if(demension==nil)
  968. demension="";
  969. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  970. // ,,,,,,,,,
  971. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  972. if(seat_height==nil)
  973. seat_height="";
  974. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  975. char *material = (char*)sqlite3_column_text(statement, 9);
  976. if(material==nil)
  977. material="";
  978. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  979. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  980. if(box_dim==nil)
  981. box_dim="";
  982. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  983. char *volume = (char*)sqlite3_column_text(statement, 11);
  984. if(volume==nil)
  985. volume="";
  986. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  987. double weight = sqlite3_column_double(statement, 12);
  988. char *model_set = (char*)sqlite3_column_text(statement, 13);
  989. if(model_set==nil)
  990. model_set="";
  991. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  992. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  993. if(load_ability==nil)
  994. load_ability="";
  995. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  996. char *default_category = (char*)sqlite3_column_text(statement, 15);
  997. if(default_category==nil)
  998. default_category="";
  999. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  1000. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  1001. if(fabric_content==nil)
  1002. fabric_content="";
  1003. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  1004. char *assembling = (char*)sqlite3_column_text(statement, 17);
  1005. if(assembling==nil)
  1006. assembling="";
  1007. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  1008. char *made_in = (char*)sqlite3_column_text(statement, 18);
  1009. if(made_in==nil)
  1010. made_in="";
  1011. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  1012. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  1013. if(special_remarks==nil)
  1014. special_remarks="";
  1015. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  1016. int stockUcom = sqlite3_column_double(statement, 20);
  1017. char *product_group = (char*)sqlite3_column_text(statement, 21);
  1018. if(product_group==nil)
  1019. product_group="";
  1020. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  1021. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  1022. // if(fashion_selector==nil)
  1023. // fashion_selector="";
  1024. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  1025. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  1026. if(selector_field==nil)
  1027. selector_field="";
  1028. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  1029. char *property_field = (char*)sqlite3_column_text(statement, 23);
  1030. if(property_field==nil)
  1031. property_field="";
  1032. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  1033. char *packaging = (char*)sqlite3_column_text(statement, 24);
  1034. if(packaging==nil)
  1035. packaging="";
  1036. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  1037. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1038. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  1039. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  1040. [img_section setValue:model_s_img forKey:@"model_s_img"];
  1041. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  1042. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  1043. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  1044. NSString* Availability=nil;
  1045. if(availability>0)
  1046. Availability=[NSString stringWithFormat:@"%d",availability];
  1047. else
  1048. Availability = @"In Production";
  1049. [img_section setValue:Availability forKey:@"Availability"];
  1050. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  1051. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1052. int item_id = sqlite3_column_int(statement, 25);
  1053. NSString* Price=nil;
  1054. if(appDelegate.bLogin==false)
  1055. Price=@"Must Sign in.";
  1056. else
  1057. {
  1058. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1059. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1060. if(price==nil)
  1061. Price=@"No Price.";
  1062. else
  1063. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1064. }
  1065. [img_section setValue:Price forKey:@"price"];
  1066. [img_section setValue:nsname forKey:@"model_name"];
  1067. [img_section setValue:nsdescription forKey:@"model_descrition"];
  1068. [ret setObject:img_section forKey:@"img_section"];
  1069. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  1070. int detail0_item_count=0;
  1071. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1072. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1073. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1074. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1075. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1076. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1077. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1078. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1079. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1080. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1081. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1082. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1083. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1084. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  1085. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  1086. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  1087. {
  1088. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  1089. [detail0_section setObject:[self model_kvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  1090. }
  1091. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  1092. [detail0_section setValue:@"kv" forKey:@"type"];
  1093. [detail0_section setValue:@"Product Information" forKey:@"title"];
  1094. [ret setObject:detail0_section forKey:@"detail_0"];
  1095. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1096. // [detail1_section setValue:@"detail" forKey:@"target"];
  1097. // [detail1_section setValue:@"popup" forKey:@"action"];
  1098. // [detail1_section setValue:@"content" forKey:@"type"];
  1099. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  1100. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  1101. // [detail1_section setValue:@"true" forKey:@"single_row"];
  1102. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  1103. [ret setObject:detail1_section forKey:@"detail_1"];
  1104. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  1105. [detail2_section setValue:@"detail" forKey:@"target"];
  1106. [detail2_section setValue:@"popup" forKey:@"action"];
  1107. [detail2_section setValue:@"content" forKey:@"type"];
  1108. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  1109. [detail2_section setValue:@"true" forKey:@"single_row"];
  1110. [detail2_section setValue:@"local" forKey:@"data"];
  1111. [ret setObject:detail2_section forKey:@"detail_2"];
  1112. }
  1113. sqlite3_finalize(statement);
  1114. }
  1115. else
  1116. {
  1117. [ret setValue:@"8" forKey:@"result"];
  1118. }
  1119. NSLog(@"count:%d",count);
  1120. [iSalesDB close_db:db];
  1121. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1122. return ret;
  1123. }
  1124. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  1125. NSString* orderCode = [params valueForKey:@"orderCode"];
  1126. NSString* keyword = [params valueForKey:@"keyword"];
  1127. keyword=keyword.lowercaseString;
  1128. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue];
  1129. int limit = [[params valueForKey:@"limit"] intValue];
  1130. int offset = [[params valueForKey:@"offset"] intValue];
  1131. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1132. NSString *limit_str = @"";
  1133. if (limited) {
  1134. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  1135. }
  1136. sqlite3 *db = [iSalesDB get_db];
  1137. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  1138. UIApplication * app = [UIApplication sharedApplication];
  1139. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1140. NSString *sqlQuery = nil;
  1141. if(exactMatch )
  1142. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name %@;",appDelegate.order_code, keyword,limit_str]; // select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%@%%' order by m.name limit %d offset %d ;
  1143. else
  1144. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, keyword,keyword,limit_str];// select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%%%@%%' or
  1145. DebugLog(@"offline_search sql:%@",sqlQuery);
  1146. sqlite3_stmt * statement;
  1147. [ret setValue:@"2" forKey:@"result"];
  1148. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  1149. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  1150. // int count=0;
  1151. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1152. {
  1153. int i=0;
  1154. while (sqlite3_step(statement) == SQLITE_ROW)
  1155. {
  1156. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1157. // char *name = (char*)sqlite3_column_text(statement, 1);
  1158. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1159. char *name = (char*)sqlite3_column_text(statement, 0);
  1160. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1161. char *description = (char*)sqlite3_column_text(statement, 1);
  1162. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  1163. int product_id = sqlite3_column_int(statement, 2);
  1164. // char *url = (char*)sqlite3_column_text(statement, 3);
  1165. // if(url==nil)
  1166. // url="";
  1167. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1168. int wid = sqlite3_column_int(statement, 3);
  1169. int closeout = sqlite3_column_int(statement, 4);
  1170. int cid = sqlite3_column_int(statement, 5);
  1171. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  1172. if(wid==0)
  1173. [item setValue:@"false" forKey:@"wish_exists"];
  1174. else
  1175. [item setValue:@"true" forKey:@"wish_exists"];
  1176. if(closeout==0)
  1177. [item setValue:@"false" forKey:@"is_closeout"];
  1178. else
  1179. [item setValue:@"true" forKey:@"is_closeout"];
  1180. if(cid==0)
  1181. [item setValue:@"false" forKey:@"cart_exists"];
  1182. else
  1183. [item setValue:@"true" forKey:@"cart_exists"];
  1184. [item addEntriesFromDictionary:imgjson];
  1185. // [item setValue:nsurl forKey:@"img"];
  1186. [item setValue:nsname forKey:@"fash_name"];
  1187. [item setValue:nsdescription forKey:@"description"];
  1188. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  1189. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  1190. i++;
  1191. }
  1192. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  1193. [ret setObject:items forKey:@"items"];
  1194. sqlite3_finalize(statement);
  1195. }
  1196. NSLog(@"count:%d",count);
  1197. [iSalesDB close_db:db];
  1198. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1199. return ret;
  1200. }
  1201. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  1202. {
  1203. return [self search:params limited:YES];
  1204. }
  1205. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  1206. {
  1207. UIApplication * app = [UIApplication sharedApplication];
  1208. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1209. appDelegate.disable_trigger=true;
  1210. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1211. appDelegate.disable_trigger=false;
  1212. //
  1213. // NSString* user = [params valueForKey:@"user"];
  1214. //
  1215. // NSString* password = [params valueForKey:@"password"];
  1216. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1217. ret[@"result"]=[NSNumber numberWithInt:2 ];
  1218. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  1219. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  1220. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  1221. [appDelegate update_count_mark];
  1222. appDelegate.can_show_price =false;
  1223. appDelegate.can_see_price =false;
  1224. appDelegate.can_create_portfolio =false;
  1225. appDelegate.can_create_order =false;
  1226. appDelegate.can_cancel_order =false;
  1227. appDelegate.can_set_cart_price =false;
  1228. appDelegate.can_delete_order =false;
  1229. appDelegate.can_submit_order =false;
  1230. appDelegate.can_set_tearsheet_price =false;
  1231. appDelegate.can_update_contact_info = false;
  1232. appDelegate.save_order_logout = false;
  1233. appDelegate.submit_order_logout = false;
  1234. appDelegate.alert_sold_in_quantities = false;
  1235. appDelegate.ipad_perm =nil ;
  1236. appDelegate.user_type = USER_ROLE_UNKNOWN;
  1237. appDelegate.OrderFilter= nil;
  1238. [appDelegate SetSo:nil];
  1239. [appDelegate set_main_button_panel];
  1240. // sqlite3 *db = [iSalesDB get_db];
  1241. //
  1242. //
  1243. //
  1244. //
  1245. //
  1246. // NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  1247. //
  1248. //
  1249. //
  1250. //
  1251. // DebugLog(@"offline_login sql:%@",sqlQuery);
  1252. // sqlite3_stmt * statement;
  1253. //
  1254. //
  1255. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1256. //
  1257. //
  1258. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1259. // {
  1260. //
  1261. //
  1262. // if (sqlite3_step(statement) == SQLITE_ROW)
  1263. // {
  1264. //
  1265. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1266. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1267. //
  1268. //
  1269. //
  1270. // int can_show_price = sqlite3_column_int(statement, 0);
  1271. // int can_see_price = sqlite3_column_int(statement, 1);
  1272. //
  1273. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1274. // if(contact_id==nil)
  1275. // contact_id="";
  1276. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1277. //
  1278. // int user_type = sqlite3_column_int(statement, 3);
  1279. //
  1280. // int can_cancel_order = sqlite3_column_int(statement, 4);
  1281. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  1282. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  1283. // int can_delete_order = sqlite3_column_int(statement, 7);
  1284. // int can_submit_order = sqlite3_column_int(statement, 8);
  1285. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1286. // int can_create_order = sqlite3_column_int(statement, 10);
  1287. //
  1288. //
  1289. // char *mode = (char*)sqlite3_column_text(statement, 11);
  1290. // if(mode==nil)
  1291. // mode="";
  1292. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1293. //
  1294. //
  1295. // char *username = (char*)sqlite3_column_text(statement, 12);
  1296. // if(username==nil)
  1297. // username="";
  1298. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1299. //
  1300. //
  1301. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1302. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1303. // [header setValue:nscontact_id forKey:@"contact_id"];
  1304. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1305. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1306. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1307. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1308. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1309. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1310. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1311. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1312. //
  1313. // [header setValue:nsusername forKey:@"username"];
  1314. //
  1315. //
  1316. // [ret setObject:header forKey:@"header"];
  1317. // [ret setValue:nsmode forKey:@"mode"];
  1318. //
  1319. //
  1320. // }
  1321. //
  1322. //
  1323. //
  1324. // sqlite3_finalize(statement);
  1325. // }
  1326. //
  1327. //
  1328. //
  1329. // [iSalesDB close_db:db];
  1330. //
  1331. //
  1332. //
  1333. //
  1334. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1335. return ret;
  1336. }
  1337. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  1338. {
  1339. UIApplication * app = [UIApplication sharedApplication];
  1340. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1341. appDelegate.disable_trigger=true;
  1342. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1343. appDelegate.disable_trigger=false;
  1344. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1345. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  1346. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  1347. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  1348. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  1349. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  1350. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  1351. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  1352. // NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  1353. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  1354. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  1355. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  1356. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  1357. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  1358. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  1359. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  1360. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  1361. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  1362. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  1363. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  1364. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  1365. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  1366. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  1367. [arr_name addObject:customer_first_name];
  1368. [arr_name addObject:customer_last_name];
  1369. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  1370. // default ship from
  1371. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,decrypt(phone) from offline_contact where contact_id = 'NPD';";
  1372. __block NSString *cid = @"";
  1373. __block NSString *name = @"";
  1374. __block NSString *ext = @"";
  1375. __block NSString *contact = @"";
  1376. __block NSString *email = @"";
  1377. __block NSString *fax = @"";
  1378. __block NSString *phone = @"";
  1379. sqlite3 *db = [iSalesDB get_db];
  1380. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  1381. cid = [self textAtColumn:0 statement:statment];
  1382. name = [self textAtColumn:1 statement:statment];
  1383. ext = [self textAtColumn:2 statement:statment];
  1384. contact = [self textAtColumn:3 statement:statment];
  1385. email = [self textAtColumn:4 statement:statment];
  1386. fax = [self textAtColumn:5 statement:statment];
  1387. phone = [self textAtColumn:6 statement:statment];
  1388. }];
  1389. NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email) values('OFFLINE'||hex(randomblob(16)),0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@')",customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email];
  1390. int result =[iSalesDB execSql:sql_neworder db:db];
  1391. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  1392. //insert into offline_order(so_id,status,contact_id,sales_rep,create_by,customer_contact) select 'OFFLINE'||hex(randomblob(16)),-1,contact_id,sales_rep,company_name from offline_contact where contact_id='99FURO0002'
  1393. //soId
  1394. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  1395. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  1396. sqlite3_stmt * statement;
  1397. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1398. {
  1399. if (sqlite3_step(statement) == SQLITE_ROW)
  1400. {
  1401. // char *name = (char*)sqlite3_column_text(statement, 1);
  1402. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  1403. //ret = sqlite3_column_int(statement, 0);
  1404. char *soId = (char*)sqlite3_column_text(statement, 0);
  1405. if(soId==nil)
  1406. soId="";
  1407. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  1408. [ret setValue:nssoId forKey:@"soId"];
  1409. [ret setValue:nssoId forKey:@"orderCode"];
  1410. }
  1411. sqlite3_finalize(statement);
  1412. }
  1413. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  1414. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1415. [iSalesDB close_db:db];
  1416. return [RAUtils dict2data:ret];
  1417. }
  1418. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  1419. {
  1420. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1421. NSString* orderCode = [params valueForKey:@"orderCode"];
  1422. UIApplication * app = [UIApplication sharedApplication];
  1423. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1424. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  1425. {
  1426. appDelegate.disable_trigger=true;
  1427. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  1428. appDelegate.disable_trigger=false;
  1429. }
  1430. sqlite3 *db = [iSalesDB get_db];
  1431. int cart_count=[self query_ordercartcount:orderCode db:db];
  1432. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1433. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1434. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1435. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1436. [iSalesDB close_db:db];
  1437. return [RAUtils dict2data:ret];
  1438. }
  1439. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  1440. {
  1441. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1442. sqlite3 *db = [iSalesDB get_db];
  1443. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  1444. int count =0;
  1445. if(params[@"count"]!=nil)
  1446. {
  1447. count = [params[@"count"] intValue];
  1448. }
  1449. NSString* product_id=params[@"product_id"];
  1450. NSString* orderCode=params[@"orderCode"];
  1451. NSString *qty = params[@"qty"];
  1452. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  1453. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  1454. for(int i=0;i<arr_id.count;i++)
  1455. {
  1456. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  1457. int item_qty= count;
  1458. if (qty) {
  1459. item_qty = [qty_arr[i] integerValue];
  1460. }
  1461. if(item_qty==0)
  1462. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  1463. NSString* sql=nil;
  1464. sqlite3_stmt *stmt;
  1465. if(_id<0)
  1466. {
  1467. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  1468. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  1469. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1470. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],[arr_id[i] length],NULL);
  1471. sqlite3_bind_text(stmt,2,[orderCode UTF8String],[orderCode length],NULL);
  1472. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1473. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  1474. }
  1475. else
  1476. {
  1477. if (qty) {
  1478. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  1479. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1480. sqlite3_bind_int(stmt, 1, _id);
  1481. } else {
  1482. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  1483. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  1484. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%d",item_qty] UTF8String],[[NSString stringWithFormat:@"%d",item_qty] length],NULL);
  1485. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],[[NSString stringWithFormat:@"%d",_id] length],NULL);
  1486. }
  1487. }
  1488. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  1489. [iSalesDB execSql:@"ROLLBACK" db:db];
  1490. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  1491. [iSalesDB close_db:db];
  1492. DebugLog(@"add to cart error");
  1493. return [RAUtils dict2data:ret];
  1494. }
  1495. }
  1496. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1497. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1498. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1499. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1500. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1501. [iSalesDB execSql:@"END TRANSACTION" db:db];
  1502. [iSalesDB close_db:db];
  1503. return [RAUtils dict2data:ret];
  1504. }
  1505. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute
  1506. {
  1507. UIApplication * app = [UIApplication sharedApplication];
  1508. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1509. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1510. NSString *sqlQuery = [NSString stringWithFormat:@"select b.item_id,b.qty,c.name,c.description,b.unit_price,b.use_unitprice from (select item_id,qty,decrypt(unit_price) as unit_price,use_unitprice from bundle where product_item_id=%d) b left join catalog c on b.item_id=c.item_id;",item_id];
  1511. sqlite3_stmt * statement;
  1512. int count=0;
  1513. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1514. {
  1515. while (sqlite3_step(statement) == SQLITE_ROW)
  1516. {
  1517. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  1518. int bitem_id = sqlite3_column_int(statement, 0);
  1519. int bitem_qty = sqlite3_column_int(statement, 1);
  1520. char *name = (char*)sqlite3_column_text(statement, 2);
  1521. if(name==nil)
  1522. name="";
  1523. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  1524. char *description = (char*)sqlite3_column_text(statement, 3);
  1525. if(description==nil)
  1526. description="";
  1527. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  1528. double unit_price = sqlite3_column_double(statement, 4);
  1529. int use_unitprice = sqlite3_column_int(statement, 5);
  1530. if(use_unitprice!=1)
  1531. {
  1532. unit_price=[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db].doubleValue;
  1533. }
  1534. itemjson[@"model"]=nsname;
  1535. itemjson[@"description"]=nsdescription;
  1536. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  1537. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  1538. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  1539. if(compute)
  1540. {
  1541. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute];
  1542. }
  1543. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1544. count++;
  1545. }
  1546. sqlite3_finalize(statement);
  1547. }
  1548. ret[@"count"]=@(count);
  1549. if(count==0)
  1550. return nil;
  1551. else
  1552. return ret;
  1553. }
  1554. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  1555. {
  1556. //compute: add part to subtotal;
  1557. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  1558. dict_item[@(item_id)]=@"1";
  1559. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  1560. double cuft=0;
  1561. double weight=0;
  1562. int carton=0;
  1563. NSString *sqlQuery = [NSString stringWithFormat:@"select ulength ,uwidth ,uheight ,uweight ,mlength ,mwidth ,mheight ,mweigth ,ilength ,iwidth ,iheight , iweight ,pcs ,mpack ,ipack,ucbf,icbf,mcbf from catalog where item_id=%d;",item_id];
  1564. sqlite3_stmt * statement;
  1565. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1566. {
  1567. if (sqlite3_step(statement) == SQLITE_ROW)
  1568. {
  1569. double ulength = sqlite3_column_double(statement, 0);
  1570. double uwidth = sqlite3_column_double(statement, 1);
  1571. double uheight = sqlite3_column_double(statement, 2);
  1572. double uweight = sqlite3_column_double(statement, 3);
  1573. double mlength = sqlite3_column_double(statement, 4);
  1574. double mwidth = sqlite3_column_double(statement, 5);
  1575. double mheight = sqlite3_column_double(statement, 6);
  1576. double mweight = sqlite3_column_double(statement, 7);
  1577. double ilength = sqlite3_column_double(statement, 8);
  1578. double iwidth = sqlite3_column_double(statement, 9);
  1579. double iheight = sqlite3_column_double(statement, 10);
  1580. double iweight = sqlite3_column_double(statement, 11);
  1581. int pcs = sqlite3_column_int(statement,12);
  1582. int mpack = sqlite3_column_int(statement, 13);
  1583. int ipack = sqlite3_column_int(statement, 14);
  1584. double ucbf = sqlite3_column_double(statement, 15);
  1585. double icbf = sqlite3_column_double(statement, 16);
  1586. double mcbf = sqlite3_column_double(statement, 17);
  1587. if(ipack==0)
  1588. {
  1589. carton= count/mpack ;
  1590. weight = mweight*carton;
  1591. cuft= carton*(mlength*mwidth*mheight);
  1592. int remain=count%mpack;
  1593. if(remain==0)
  1594. {
  1595. //do nothing;
  1596. }
  1597. else
  1598. {
  1599. carton++;
  1600. weight += uweight*remain;
  1601. cuft += (ulength*uwidth*uheight)*remain;
  1602. }
  1603. }
  1604. else
  1605. {
  1606. carton = count/(mpack*ipack);
  1607. weight = mweight*carton;
  1608. cuft= carton*(mlength*mwidth*mheight);
  1609. int remain=count%(mpack*ipack);
  1610. if(remain==0)
  1611. {
  1612. // do nothing;
  1613. }
  1614. else
  1615. {
  1616. carton++;
  1617. int icarton =remain/ipack;
  1618. int iremain=remain%ipack;
  1619. weight += iweight*icarton;
  1620. cuft += (ilength*iwidth*iheight)*icarton;
  1621. if(iremain==0)
  1622. {
  1623. //do nothing;
  1624. }
  1625. else
  1626. {
  1627. weight += uweight*iremain;
  1628. cuft += (ulength*uwidth*uheight)*iremain;
  1629. }
  1630. }
  1631. }
  1632. #ifdef BUILD_NPD
  1633. cuft=ucbf*count;
  1634. weight= uweight*count;
  1635. #endif
  1636. }
  1637. sqlite3_finalize(statement);
  1638. }
  1639. if(compute)
  1640. {
  1641. NSArray * arr_count=nil;
  1642. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  1643. for(int i=0;i<arr_bundle.count;i++)
  1644. {
  1645. dict_item[arr_bundle[i]]=@"1";
  1646. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  1647. cuft+=[bundlejson[@"cuft"] doubleValue];
  1648. weight+=[bundlejson[@"weight"] doubleValue];
  1649. carton+=[bundlejson[@"carton"] intValue];
  1650. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  1651. }
  1652. }
  1653. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  1654. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  1655. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  1656. ret[@"items"]=dict_item;
  1657. return ret;
  1658. }
  1659. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  1660. {
  1661. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  1662. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  1663. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1664. DebugLog(@"offline_login sql:%@",sqlQuery);
  1665. sqlite3_stmt * statement;
  1666. int cart_count=0;
  1667. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1668. if ( dbresult== SQLITE_OK)
  1669. {
  1670. while (sqlite3_step(statement) == SQLITE_ROW)
  1671. {
  1672. int item_id = sqlite3_column_int(statement, 0);
  1673. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  1674. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  1675. }
  1676. sqlite3_finalize(statement);
  1677. }
  1678. return cart_count;
  1679. }
  1680. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  1681. {
  1682. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1683. sqlite3 *db = [iSalesDB get_db];
  1684. UIApplication * app = [UIApplication sharedApplication];
  1685. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1686. NSString* orderCode=params[@"orderCode"];
  1687. int sort = [[params objectForKey:@"sort"] intValue];
  1688. NSString *sort_str = @"";
  1689. switch (sort) {
  1690. case 0:{
  1691. sort_str = @"order by c.modify_time desc";
  1692. }
  1693. break;
  1694. case 1:{
  1695. sort_str = @"order by c.modify_time asc";
  1696. }
  1697. break;
  1698. case 2:{
  1699. sort_str = @"order by m.name asc";
  1700. }
  1701. break;
  1702. case 3:{
  1703. sort_str = @"order by m.name desc";
  1704. }
  1705. break;
  1706. case 4:{
  1707. sort_str = @"order by m.description asc";
  1708. }
  1709. break;
  1710. default:
  1711. break;
  1712. }
  1713. NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id %@",orderCode,sort_str ];
  1714. // NSString *sqlQuery = [ NSString stringWithFormat:@"select c.product_id,decrypt(c.str_price),c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from offline_cart c left join product m on c.product_id=m.product_id where c.so_no='%@'",orderCode ];
  1715. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1716. DebugLog(@"offline_login sql:%@",sqlQuery);
  1717. sqlite3_stmt * statement;
  1718. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1719. NSDate *date1 = [NSDate date];
  1720. int count=0;
  1721. int cart_count=0;
  1722. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1723. if ( dbresult== SQLITE_OK)
  1724. {
  1725. while (sqlite3_step(statement) == SQLITE_ROW)
  1726. {
  1727. NSDate *row_date = [NSDate date];
  1728. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1729. int product_id = sqlite3_column_int(statement, 0);
  1730. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1731. int item_id = sqlite3_column_int(statement, 7);
  1732. NSString* Price=nil;
  1733. if(str_price==nil)
  1734. {
  1735. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1736. // NSDate *price_date = [NSDate date];
  1737. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  1738. // DebugLog(@"price time interval");
  1739. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  1740. if(price==nil)
  1741. Price=@"No Price.";
  1742. else
  1743. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1744. }
  1745. else
  1746. {
  1747. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1748. }
  1749. double discount = sqlite3_column_double(statement, 2);
  1750. int item_count = sqlite3_column_int(statement, 3);
  1751. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1752. NSString *nsline_note=nil;
  1753. if(line_note!=nil)
  1754. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1755. char *name = (char*)sqlite3_column_text(statement, 5);
  1756. NSString *nsname=nil;
  1757. if(name!=nil)
  1758. nsname= [[NSString alloc]initWithUTF8String:name];
  1759. char *description = (char*)sqlite3_column_text(statement, 6);
  1760. NSString *nsdescription=nil;
  1761. if(description!=nil)
  1762. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1763. int stockUom = sqlite3_column_int(statement, 8);
  1764. int _id = sqlite3_column_int(statement, 9);
  1765. // NSDate *subtotal_date = [NSDate date];
  1766. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1767. // DebugLog(@"subtotal_date time interval");
  1768. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  1769. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1770. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1771. int carton=[bsubtotaljson[@"carton"] intValue];
  1772. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  1773. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1774. // NSDate *img_date = [NSDate date];
  1775. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1776. // DebugLog(@"img_date time interval");
  1777. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  1778. itemjson[@"model"]=nsname;
  1779. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1780. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1781. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1782. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1783. itemjson[@"check"]=@"true";
  1784. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1785. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1786. itemjson[@"unit_price"]=Price;
  1787. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1788. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1789. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1790. itemjson[@"note"]=nsline_note;
  1791. // NSDate *date2 = [NSDate date];
  1792. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  1793. // DebugLog(@"model_bundle time interval");
  1794. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  1795. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1796. count++;
  1797. // DebugLog(@"row time interval");
  1798. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  1799. }
  1800. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1801. sqlite3_finalize(statement);
  1802. }
  1803. DebugLog(@"request cart total time interval");
  1804. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1805. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1806. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  1807. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  1808. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  1809. ret[@"count"]=[NSNumber numberWithInt:count ];
  1810. ret[@"mode"]=@"Regular Mode";
  1811. [iSalesDB close_db:db];
  1812. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  1813. DebugLog(@"general notes :%@",general_note);
  1814. ret[@"general_note"]= general_note;
  1815. return [RAUtils dict2data:ret];
  1816. }
  1817. +(NSData*) offline_login :(NSMutableDictionary *) params
  1818. {
  1819. NSString* user = [params valueForKey:@"user"];
  1820. NSString* password = [params valueForKey:@"password"];
  1821. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1822. sqlite3 *db = [iSalesDB get_db];
  1823. NSString *sqlQuery = [NSString stringWithFormat:@"select can_show_price,can_see_price,contact_id,user_type,can_cancel_order,can_set_cart_price,can_create_portfolio,can_delete_order,can_submit_order,can_set_tearsheet_price,can_create_order,mode,username,can_update_contact_info from offline_login where lower(username)='%@' and password='%@'",user.lowercaseString ,[AESCrypt encrypt:password password:@"usai"]];
  1824. DebugLog(@"offline_login sql:%@",sqlQuery);
  1825. sqlite3_stmt * statement;
  1826. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1827. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1828. {
  1829. if (sqlite3_step(statement) == SQLITE_ROW)
  1830. {
  1831. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1832. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  1833. int can_show_price = sqlite3_column_int(statement, 0);
  1834. int can_see_price = sqlite3_column_int(statement, 1);
  1835. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  1836. if(contact_id==nil)
  1837. contact_id="";
  1838. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1839. int user_type = sqlite3_column_int(statement, 3);
  1840. int can_cancel_order = sqlite3_column_int(statement, 4);
  1841. int can_set_cart_price = sqlite3_column_int(statement, 5);
  1842. int can_create_portfolio = sqlite3_column_int(statement, 6);
  1843. int can_delete_order = sqlite3_column_int(statement, 7);
  1844. int can_submit_order = sqlite3_column_int(statement, 8);
  1845. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  1846. int can_create_order = sqlite3_column_int(statement, 10);
  1847. char *mode = (char*)sqlite3_column_text(statement, 11);
  1848. if(mode==nil)
  1849. mode="";
  1850. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  1851. char *username = (char*)sqlite3_column_text(statement, 12);
  1852. if(username==nil)
  1853. username="";
  1854. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  1855. int can_update_contact_info = sqlite3_column_int(statement, 13);
  1856. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  1857. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  1858. [header setValue:nscontact_id forKey:@"contact_id"];
  1859. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  1860. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  1861. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  1862. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  1863. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  1864. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  1865. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  1866. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  1867. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  1868. [header setValue:nsusername forKey:@"username"];
  1869. NSError* error=nil;
  1870. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  1871. [header setValue:statusFilter forKey:@"statusFilter"];
  1872. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1873. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  1874. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  1875. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  1876. [ret setObject:header forKey:@"header"];
  1877. [ret setValue:nsmode forKey:@"mode"];
  1878. }
  1879. sqlite3_finalize(statement);
  1880. }
  1881. [iSalesDB close_db:db];
  1882. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  1883. return [RAUtils dict2data:ret];
  1884. }
  1885. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  1886. {
  1887. NSString* contactId = [params valueForKey:@"contactId"];
  1888. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1889. sqlite3 *db = [iSalesDB get_db];
  1890. NSString *sqlQuery = nil;
  1891. {
  1892. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email,img_0,img_1,img_2,price_type,notes,sales_rep from offline_contact where contact_id='%@'",contactId];
  1893. }
  1894. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  1895. sqlite3_stmt * statement;
  1896. [ret setValue:@"2" forKey:@"result"];
  1897. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1898. {
  1899. //int i = 0;
  1900. if (sqlite3_step(statement) == SQLITE_ROW)
  1901. {
  1902. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  1903. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  1904. // int editable = sqlite3_column_int(statement, 0);
  1905. char *company_name = (char*)sqlite3_column_text(statement, 1);
  1906. NSString *nscompany_name =nil;
  1907. if(company_name==nil)
  1908. nscompany_name=@"";
  1909. else
  1910. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  1911. char *country = (char*)sqlite3_column_text(statement, 2);
  1912. if(country==nil)
  1913. country="";
  1914. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  1915. // char *addr = (char*)sqlite3_column_text(statement, 3);
  1916. // if(addr==nil)
  1917. // addr="";
  1918. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  1919. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  1920. if(zipcode==nil)
  1921. zipcode="";
  1922. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  1923. char *state = (char*)sqlite3_column_text(statement, 5);
  1924. if(state==nil)
  1925. state="";
  1926. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  1927. char *city = (char*)sqlite3_column_text(statement, 6);
  1928. if(city==nil)
  1929. city="";
  1930. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  1931. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  1932. // NSString *nscontact_name = nil;
  1933. // if(contact_name==nil)
  1934. // nscontact_name=@"";
  1935. // else
  1936. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  1937. char *phone = (char*)sqlite3_column_text(statement, 8);
  1938. NSString *nsphone = nil;
  1939. if(phone==nil)
  1940. nsphone=@"";
  1941. else
  1942. nsphone= [[NSString alloc]initWithUTF8String:phone];
  1943. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  1944. if(contact_id==nil)
  1945. contact_id="";
  1946. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  1947. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  1948. if(addr_1==nil)
  1949. addr_1="";
  1950. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  1951. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  1952. if(addr_2==nil)
  1953. addr_2="";
  1954. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  1955. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  1956. if(addr_3==nil)
  1957. addr_3="";
  1958. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  1959. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  1960. if(addr_4==nil)
  1961. addr_4="";
  1962. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  1963. char *first_name = (char*)sqlite3_column_text(statement, 14);
  1964. if(first_name==nil)
  1965. first_name="";
  1966. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  1967. char *last_name = (char*)sqlite3_column_text(statement, 15);
  1968. if(last_name==nil)
  1969. last_name="";
  1970. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  1971. char *fax = (char*)sqlite3_column_text(statement, 16);
  1972. NSString *nsfax = nil;
  1973. if(fax==nil)
  1974. nsfax=@"";
  1975. else
  1976. nsfax= [[NSString alloc]initWithUTF8String:fax];
  1977. char *email = (char*)sqlite3_column_text(statement, 17);
  1978. NSString *nsemail = nil;
  1979. if(email==nil)
  1980. nsemail=@"";
  1981. else
  1982. nsemail= [[NSString alloc]initWithUTF8String:email];
  1983. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  1984. NSString *nsimg_0 = nil;
  1985. if(img_0==nil)
  1986. nsimg_0=@"";
  1987. else
  1988. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1989. [self copy_bcardImg:nsimg_0];
  1990. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  1991. NSString *nsimg_1 = nil;
  1992. if(img_1==nil)
  1993. nsimg_1=@"";
  1994. else
  1995. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1996. [self copy_bcardImg:nsimg_1];
  1997. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  1998. NSString *nsimg_2 = nil;
  1999. if(img_2==nil)
  2000. nsimg_2=@"";
  2001. else
  2002. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  2003. [self copy_bcardImg:nsimg_2];
  2004. char *price_type = (char*)sqlite3_column_text(statement, 21);
  2005. NSString *nsprice_type = nil;
  2006. if(price_type==nil)
  2007. nsprice_type=@"";
  2008. else
  2009. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  2010. char *notes = (char*)sqlite3_column_text(statement, 22);
  2011. NSString *nsnotes = nil;
  2012. if(notes==nil)
  2013. nsnotes=@"";
  2014. else
  2015. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  2016. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  2017. NSString *nssalesrep = nil;
  2018. if(salesrep==nil)
  2019. nssalesrep=@"";
  2020. else
  2021. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  2022. {
  2023. // decrypt
  2024. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  2025. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  2026. nsphone=[AESCrypt fastdecrypt:nsphone];
  2027. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2028. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  2029. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  2030. }
  2031. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2032. [arr_name addObject:nsfirst_name];
  2033. [arr_name addObject:nslast_name];
  2034. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2035. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  2036. [arr_ext addObject:nsaddr_1];
  2037. [arr_ext addObject:nsaddr_2];
  2038. [arr_ext addObject:nsaddr_3];
  2039. [arr_ext addObject:nsaddr_4];
  2040. [arr_ext addObject:@"\r\n"];
  2041. [arr_ext addObject:nscity];
  2042. [arr_ext addObject:nsstate];
  2043. [arr_ext addObject:nszipcode];
  2044. [arr_ext addObject:nscountry];
  2045. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  2046. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  2047. [item setValue:nsimg_2 forKey:@"business_card_2"];
  2048. [item setValue:nsimg_0 forKey:@"business_card_0"];
  2049. [item setValue:nscountry forKey:@"customer_country"];
  2050. [item setValue:nsphone forKey:@"customer_phone"];
  2051. [item setValue:nsimg_1 forKey:@"business_card_1"];
  2052. [item setValue:nscompany_name forKey:@"customer_name"];
  2053. [item setValue:nsprice_type forKey:@"customer_price_type"];
  2054. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  2055. [item setValue:nsext forKey:@"customer_contact_ext"];
  2056. [item setValue:nszipcode forKey:@"customer_zipcode"];
  2057. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  2058. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  2059. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  2060. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  2061. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  2062. [item setValue:nslast_name forKey:@"customer_last_name"];
  2063. [item setValue:nscity forKey:@"customer_city"];
  2064. [item setValue:nsstate forKey:@"customer_state"];
  2065. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  2066. [item setValue:contactId forKey:@"customer_cid"];
  2067. [item setValue:nscontact_name forKey:@"customer_contact"];
  2068. [item setValue:nsfax forKey:@"customer_fax"];
  2069. [item setValue:nsemail forKey:@"customer_email"];
  2070. [ret setObject:item forKey:@"customerInfo"];
  2071. // i++;
  2072. }
  2073. UIApplication * app = [UIApplication sharedApplication];
  2074. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2075. [ret setValue:appDelegate.mode forKey:@"mode"];
  2076. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  2077. sqlite3_finalize(statement);
  2078. }
  2079. [iSalesDB close_db:db];
  2080. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2081. return ret;
  2082. }
  2083. + (bool) copy_bcardImg:(NSString*) filename
  2084. {
  2085. if(filename.length==0)
  2086. return false;
  2087. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2088. bool ret=false;
  2089. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2090. NSString *cachefolder = [paths objectAtIndex:0];
  2091. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  2092. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2093. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2094. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  2095. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  2096. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2097. //
  2098. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2099. NSFileManager* fileManager = [NSFileManager defaultManager];
  2100. BOOL bdir=NO;
  2101. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  2102. {
  2103. NSError *error = nil;
  2104. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  2105. {
  2106. ret=false;
  2107. }
  2108. else
  2109. {
  2110. ret=true;
  2111. }
  2112. // NSError *error = nil;
  2113. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2114. //
  2115. // if(!bsuccess)
  2116. // {
  2117. // DebugLog(@"Create offline_createimg folder failed");
  2118. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2119. // return [RAUtils dict2data:ret];
  2120. // }
  2121. // if(bsuccess)
  2122. // {
  2123. // sqlite3 *db = [self get_db];
  2124. //
  2125. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2126. // [iSalesDB close_db:db];
  2127. // }
  2128. }
  2129. return ret;
  2130. //
  2131. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2132. // if(bsuccess)
  2133. // {
  2134. // NSError *error = nil;
  2135. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2136. // {
  2137. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2138. // }
  2139. // else
  2140. // {
  2141. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2142. //
  2143. // ret[@"img_url_aname"]=filename;
  2144. // ret[@"img_url"]=savedImagePath;
  2145. // }
  2146. // }
  2147. }
  2148. +(NSData *) offline_saveBusinesscard:(NSData *) image
  2149. {
  2150. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2151. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2152. NSString *cachefolder = [paths objectAtIndex:0];
  2153. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  2154. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  2155. NSFileManager* fileManager = [NSFileManager defaultManager];
  2156. BOOL bdir=YES;
  2157. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  2158. {
  2159. NSError *error = nil;
  2160. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  2161. if(!bsuccess)
  2162. {
  2163. DebugLog(@"Create offline_createimg folder failed");
  2164. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2165. return [RAUtils dict2data:ret];
  2166. }
  2167. // if(bsuccess)
  2168. // {
  2169. // sqlite3 *db = [self get_db];
  2170. //
  2171. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  2172. // [iSalesDB close_db:db];
  2173. // }
  2174. }
  2175. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  2176. //JEPG格式
  2177. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  2178. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  2179. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  2180. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  2181. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  2182. if(bsuccess)
  2183. {
  2184. NSError *error = nil;
  2185. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  2186. {
  2187. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2188. }
  2189. else
  2190. {
  2191. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  2192. ret[@"img_url_aname"]=filename;
  2193. ret[@"img_url"]=filename;
  2194. }
  2195. }
  2196. else
  2197. {
  2198. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  2199. }
  2200. return [RAUtils dict2data:ret];
  2201. }
  2202. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  2203. {
  2204. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  2205. category = [category substringToIndex:3];
  2206. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2207. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  2208. params[@"category"]= category;
  2209. ret[@"params"]= params;
  2210. [ret setValue:@"detail" forKey:@"target"];
  2211. [ret setValue:@"popup" forKey:@"action"];
  2212. [ret setValue:@"content" forKey:@"type"];
  2213. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  2214. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2215. [ret setValue:@"true" forKey:@"single_row"];
  2216. [ret setValue:@"true" forKey:@"partial_refresh"];
  2217. // sqlite3 *db = [iSalesDB get_db];
  2218. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2219. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.product_id,i.url,random() as aa from product m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;",category,OFFLINE_IMG_TYPE_CATEGORY]; // select m.name,m.product_id,i.url,random() as aa from model m LEFT join model_image i on m.product_id = i.product_id where m.category like'%%#%@#%%' and m.category like'%%#005#%%' and i.default_img = %d order by aa limit 10 ;
  2220. sqlite3_stmt * statement;
  2221. int count = 0;
  2222. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  2223. // int count=0;
  2224. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2225. {
  2226. int i=0;
  2227. while (sqlite3_step(statement) == SQLITE_ROW)
  2228. {
  2229. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2230. // char *name = (char*)sqlite3_column_text(statement, 1);
  2231. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2232. char *name = (char*)sqlite3_column_text(statement, 0);
  2233. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2234. int product_id = sqlite3_column_int(statement, 1);
  2235. char *url = (char*)sqlite3_column_text(statement, 2);
  2236. if(url==nil)
  2237. url="";
  2238. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2239. [item setValue:nsurl forKey:@"picture_path"];
  2240. [item setValue:nsname forKey:@"fash_name"];
  2241. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2242. [item setValue:category forKey:@"category"];
  2243. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2244. i++;
  2245. }
  2246. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2247. sqlite3_finalize(statement);
  2248. }
  2249. NSLog(@"count:%d",count);
  2250. // [iSalesDB close_db:db];
  2251. return ret;
  2252. }
  2253. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  2254. {
  2255. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2256. [ret setValue:key forKey:@"key"];
  2257. [ret setValue:value forKey:@"val"];
  2258. return ret;
  2259. }
  2260. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  2261. {
  2262. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2263. [ret setValue:@"0" forKey:@"img_count"];
  2264. // sqlite3 *db = [iSalesDB get_db];
  2265. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,property_display from product where product_id=%d ;",field,product_id]; // select %@,property_display from model where product_id=%d ;
  2266. sqlite3_stmt * statement;
  2267. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2268. {
  2269. int i=0;
  2270. if (sqlite3_step(statement) == SQLITE_ROW)
  2271. {
  2272. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2273. // char *name = (char*)sqlite3_column_text(statement, 1);
  2274. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2275. char *value = (char*)sqlite3_column_text(statement, 0);
  2276. if(value==nil)
  2277. value="";
  2278. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2279. char *key = (char*)sqlite3_column_text(statement, 1);
  2280. if(key==nil)
  2281. key="";
  2282. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  2283. [item setValue:nsvalue forKey:@"val"];
  2284. [item setValue:nskey forKey:@"key"];
  2285. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2286. [ret setValue:@"1" forKey:@"count"];
  2287. i++;
  2288. }
  2289. sqlite3_finalize(statement);
  2290. }
  2291. // [iSalesDB close_db:db];
  2292. return ret;
  2293. }
  2294. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  2295. {
  2296. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2297. [ret setValue:@"0" forKey:@"count"];
  2298. // sqlite3 *db = [iSalesDB get_db];
  2299. NSString *sqlQuery = [NSString stringWithFormat:@"select %@,selector_display,product_id,category from product where product_group='%@' order by name;",field,product_group]; // select %@,selector_display,product_id,category from model where product_group='%@' order by name;
  2300. sqlite3_stmt * statement;
  2301. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2302. {
  2303. int i=0;
  2304. while (sqlite3_step(statement) == SQLITE_ROW)
  2305. {
  2306. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2307. // char *name = (char*)sqlite3_column_text(statement, 1);
  2308. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2309. char *value = (char*)sqlite3_column_text(statement, 0);
  2310. if(value==nil)
  2311. value="";
  2312. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  2313. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  2314. if(selector_display==nil)
  2315. selector_display="";
  2316. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  2317. int product_id = sqlite3_column_int(statement, 2);
  2318. char *category = (char*)sqlite3_column_text(statement, 3);
  2319. if(category==nil)
  2320. category="";
  2321. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  2322. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  2323. [item setValue:nsvalue forKey:@"title"];
  2324. [item setValue:url forKey:@"pic_url"];
  2325. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  2326. [params setValue:@"2" forKey:@"count"];
  2327. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  2328. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  2329. [param0 setValue:@"product_id" forKey:@"name"];
  2330. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  2331. [param1 setValue:nscategory forKey:@"val"];
  2332. [param1 setValue:@"category" forKey:@"name"];
  2333. [params setObject:param0 forKey:@"param_0"];
  2334. [params setObject:param1 forKey:@"param_1"];
  2335. [item setObject:params forKey:@"params"];
  2336. [ret setValue:nsselector_display forKey:@"name"];
  2337. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2338. i++;
  2339. }
  2340. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2341. [ret setValue:@"switch" forKey:@"action"];
  2342. sqlite3_finalize(statement);
  2343. }
  2344. // [iSalesDB close_db:db];
  2345. return ret;
  2346. }
  2347. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  2348. {
  2349. // model 在 category search 显示的图片。
  2350. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  2351. // sqlite3 *db = [iSalesDB get_db];
  2352. NSString *sqlQuery = [NSString stringWithFormat:@"select url,type from model_image where product_id=%d and type<2 order by default_img desc , _id asc limit 2;",product_id];
  2353. sqlite3_stmt * statement;
  2354. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2355. {
  2356. while (sqlite3_step(statement) == SQLITE_ROW)
  2357. {
  2358. char *url = (char*)sqlite3_column_text(statement, 0);
  2359. if(url==nil)
  2360. url="";
  2361. int type = sqlite3_column_int(statement, 1);
  2362. if(type==0)
  2363. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  2364. else
  2365. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  2366. }
  2367. sqlite3_finalize(statement);
  2368. }
  2369. // [iSalesDB close_db:db];
  2370. return ret;
  2371. }
  2372. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  2373. {
  2374. int item_id=-1;
  2375. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  2376. sqlite3_stmt * statement;
  2377. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2378. {
  2379. if (sqlite3_step(statement) == SQLITE_ROW)
  2380. {
  2381. item_id = sqlite3_column_int(statement, 0);
  2382. }
  2383. sqlite3_finalize(statement);
  2384. }
  2385. return item_id;
  2386. }
  2387. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  2388. {
  2389. // NSString* ret = @"";
  2390. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  2391. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  2392. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  2393. sqlite3_stmt * statement;
  2394. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2395. {
  2396. while (sqlite3_step(statement) == SQLITE_ROW)
  2397. {
  2398. int bitem_id = sqlite3_column_int(statement, 0);
  2399. int bitem_qty = sqlite3_column_int(statement, 1);
  2400. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  2401. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  2402. }
  2403. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  2404. sqlite3_finalize(statement);
  2405. }
  2406. // if(ret==nil)
  2407. // ret=@"";
  2408. *count=arr_count;
  2409. return arr_bundle;
  2410. }
  2411. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  2412. {
  2413. // get default sold qty, return -1 if model not found;
  2414. int ret = -1;
  2415. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  2416. sqlite3_stmt * statement;
  2417. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2418. {
  2419. if (sqlite3_step(statement) == SQLITE_ROW)
  2420. {
  2421. ret = sqlite3_column_int(statement, 0);
  2422. }
  2423. sqlite3_finalize(statement);
  2424. }
  2425. return ret;
  2426. }
  2427. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  2428. {
  2429. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2430. [ret setValue:@"0" forKey:@"img_count"];
  2431. // sqlite3 *db = [iSalesDB get_db];
  2432. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  2433. sqlite3_stmt * statement;
  2434. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2435. {
  2436. int i=0;
  2437. while (sqlite3_step(statement) == SQLITE_ROW)
  2438. {
  2439. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2440. // char *name = (char*)sqlite3_column_text(statement, 1);
  2441. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2442. char *url = (char*)sqlite3_column_text(statement, 0);
  2443. if(url==nil)
  2444. url="";
  2445. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2446. [item setValue:nsurl forKey:@"s"];
  2447. [item setValue:nsurl forKey:@"l"];
  2448. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  2449. i++;
  2450. }
  2451. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  2452. sqlite3_finalize(statement);
  2453. }
  2454. // [iSalesDB close_db:db];
  2455. return ret;
  2456. }
  2457. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  2458. UIApplication * app = [UIApplication sharedApplication];
  2459. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2460. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  2461. sqlite3 *db = [iSalesDB get_db];
  2462. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2463. NSString* product_id=params[@"product_id"];
  2464. NSString *item_count_str = params[@"item_count"];
  2465. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  2466. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  2467. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2468. // NSString *sql = @"";
  2469. for(int i=0;i<arr.count;i++)
  2470. {
  2471. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  2472. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  2473. if(count==0)
  2474. {
  2475. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  2476. __block int cart_count = 0;
  2477. if (!item_count_str) {
  2478. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  2479. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2480. NSString *model_set = [self textAtColumn:0 statement:stmt];
  2481. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  2482. cart_count = [[model_set_components lastObject] integerValue];
  2483. }];
  2484. }
  2485. sqlite3_stmt *stmt;
  2486. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  2487. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  2488. if (item_count_arr) {
  2489. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  2490. } else {
  2491. sqlite3_bind_int(stmt,2,cart_count);
  2492. }
  2493. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2494. [iSalesDB execSql:@"ROLLBACK" db:db];
  2495. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2496. [iSalesDB close_db:db];
  2497. DebugLog(@"add to wishlist error");
  2498. return ret;
  2499. }
  2500. } else {
  2501. sqlQuery = [NSString stringWithFormat:@"update wishlist set qty = 1 where product_id = %@;",arr[i]];
  2502. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  2503. [iSalesDB execSql:@"ROLLBACK" db:db];
  2504. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  2505. [iSalesDB close_db:db];
  2506. DebugLog(@"add to wishlist error");
  2507. return ret;
  2508. }
  2509. }
  2510. }
  2511. // [iSalesDB execSql:sql db:db];
  2512. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2513. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2514. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  2515. [ret setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  2516. [iSalesDB close_db:db];
  2517. appDelegate.wish_count =count;
  2518. [appDelegate update_count_mark];
  2519. ret[@"result"]= [NSNumber numberWithInt:2];
  2520. return ret;
  2521. }
  2522. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  2523. // 0 category
  2524. // 1 search
  2525. // 2 itemsearch
  2526. NSData *ret = nil;
  2527. NSDictionary *items = nil;
  2528. switch (from) {
  2529. case 0:{
  2530. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  2531. }
  2532. break;
  2533. case 1:{
  2534. items = [[self search:params limited:NO] objectForKey:@"items"];
  2535. }
  2536. break;
  2537. case 2:{
  2538. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  2539. }
  2540. break;
  2541. default:
  2542. break;
  2543. }
  2544. if (!items) {
  2545. return ret;
  2546. }
  2547. int count = [[items objectForKey:@"count"] intValue];
  2548. NSMutableString *product_id_str = [@"" mutableCopy];
  2549. for (int i = 0; i < count; i++) {
  2550. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  2551. NSDictionary *item = [items objectForKey:key];
  2552. NSString *product_id = [item objectForKey:@"product_id"];
  2553. if (i == 0) {
  2554. [product_id_str appendString:product_id];
  2555. } else {
  2556. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  2557. }
  2558. }
  2559. NSString *add_to = [self valueInParams:params key:@"addTo"];
  2560. if ([add_to isEqualToString:@"cart"]) {
  2561. NSString *order_code = [params objectForKey:@"orderCode"];
  2562. if (order_code.length) {
  2563. NSDictionary *newParams = @{
  2564. @"product_id" : product_id_str,
  2565. @"orderCode" : order_code
  2566. };
  2567. ret = [self offline_add2cart:newParams.mutableCopy];
  2568. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2569. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2570. ret = [RAUtils dict2data:retDic];
  2571. }
  2572. } else if([add_to isEqualToString:@"wishlist"]) {
  2573. NSDictionary *newParams = @{
  2574. @"product_id" : product_id_str
  2575. };
  2576. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  2577. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  2578. ret = [RAUtils dict2data:retDic];
  2579. } else if([add_to isEqualToString:@"portfolio"]) {
  2580. }
  2581. return ret;
  2582. }
  2583. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  2584. {
  2585. return [self addAll:params from:0];
  2586. }
  2587. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  2588. {
  2589. return [self addAll:params from:1];
  2590. }
  2591. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  2592. {
  2593. return [self addAll:params from:2];
  2594. }
  2595. #pragma mark - Jack
  2596. #warning 做SQL操作时转义!!
  2597. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  2598. // "val_227" : {
  2599. // "check" : 1,
  2600. // "value" : "US United States",
  2601. // "value_id" : "228"
  2602. // },
  2603. if (!countryCode) {
  2604. countryCode = @"US";
  2605. }
  2606. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  2607. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  2608. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  2609. int code_id = sqlite3_column_int(stmt, 3); // id
  2610. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2611. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  2612. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  2613. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2614. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  2615. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2616. }
  2617. long n = *count;
  2618. *count = n + 1;
  2619. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  2620. [container setValue:countryDic forKey:key];
  2621. }] mutableCopy];
  2622. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2623. return ret;
  2624. }
  2625. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  2626. countryCode = [self translateSingleQuote:countryCode];
  2627. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  2628. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2629. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  2630. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  2631. if (name == NULL) {
  2632. name = "";
  2633. }
  2634. if (code == NULL) {
  2635. code = "";
  2636. }
  2637. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2638. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2639. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  2640. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2641. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  2642. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2643. }
  2644. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  2645. [container setValue:stateDic forKey:key];
  2646. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  2647. DebugLog(@"query all state error: %@",err_msg);
  2648. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2649. // [stateDic setValue:@"Other" forKey:@"value"];
  2650. // [stateDic setValue:@"" forKey:@"value_id"];
  2651. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2652. //
  2653. // if (state_code && [@"" isEqualToString:state_code]) {
  2654. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2655. // }
  2656. //
  2657. // NSString *key = [NSString stringWithFormat:@"val_0"];
  2658. // [container setValue:stateDic forKey:key];
  2659. }] mutableCopy];
  2660. // failure 可以不用了,一样的
  2661. if (ret.allKeys.count == 0) {
  2662. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2663. [stateDic setValue:@"Other" forKey:@"value"];
  2664. [stateDic setValue:@"" forKey:@"value_id"];
  2665. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2666. if (state_code && [@"" isEqualToString:state_code]) {
  2667. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2668. }
  2669. NSString *key = [NSString stringWithFormat:@"val_0"];
  2670. [ret setValue:stateDic forKey:key];
  2671. }
  2672. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2673. return ret;
  2674. }
  2675. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  2676. codeId = [self translateSingleQuote:codeId];
  2677. NSString *sql = [NSString stringWithFormat:@"select name,code from offline_state where country_code = (select code as country_code FROM offline_country where countrycode_id = %@);",codeId];
  2678. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2679. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  2680. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  2681. if (name == NULL) {
  2682. name = "";
  2683. }
  2684. if (code == NULL) {
  2685. code = "";
  2686. }
  2687. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  2688. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2689. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  2690. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2691. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  2692. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2693. }
  2694. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  2695. [container setValue:stateDic forKey:key];
  2696. }] mutableCopy];
  2697. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2698. return ret;
  2699. }
  2700. + (NSDictionary *)offline_getPrice {
  2701. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price;"];
  2702. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2703. char *name = (char *) sqlite3_column_text(stmt, 1);
  2704. int type = sqlite3_column_int(stmt, 2);
  2705. int orderBy = sqlite3_column_int(stmt, 3);
  2706. if (name == NULL) {
  2707. name = "";
  2708. }
  2709. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  2710. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  2711. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  2712. NSString *key = [NSString stringWithFormat:@"val_%d",orderBy];
  2713. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  2714. if (orderBy == 0) {
  2715. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2716. }
  2717. [container setValue:priceDic forKey:key];
  2718. }] mutableCopy];
  2719. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2720. return ret;
  2721. }
  2722. + (NSDictionary *)offline_getSalesRep {
  2723. // 首先从offline_login表中取出sales_code
  2724. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2725. NSString *user = app.user;
  2726. user = [self translateSingleQuote:user];
  2727. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  2728. __block NSString *user_code = @"";
  2729. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2730. char *code = (char *)sqlite3_column_text(stmt, 0);
  2731. if (code == NULL) {
  2732. code = "";
  2733. }
  2734. user_code = [NSString stringWithUTF8String:code];
  2735. }];
  2736. // 再取所有salesRep
  2737. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  2738. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2739. // 1 name 2 code 3 salesrep_id
  2740. char *name = (char *)sqlite3_column_text(stmt, 1);
  2741. char *code = (char *)sqlite3_column_text(stmt, 2);
  2742. int salesrep_id = sqlite3_column_int(stmt, 3);
  2743. if (name == NULL) {
  2744. name = "";
  2745. }
  2746. if (code == NULL) {
  2747. code = "";
  2748. }
  2749. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  2750. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  2751. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  2752. // 比较code 相等则check
  2753. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  2754. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  2755. }
  2756. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  2757. }] mutableCopy];
  2758. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  2759. return ret;
  2760. }
  2761. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  2762. zipcode = [self translateSingleQuote:zipcode];
  2763. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  2764. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2765. char *country = (char *)sqlite3_column_text(stmt, 0);
  2766. char *state = (char *)sqlite3_column_text(stmt, 1);
  2767. char *city = (char *)sqlite3_column_text(stmt, 2);
  2768. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  2769. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  2770. if (country == NULL) {
  2771. country = "";
  2772. }
  2773. if (state == NULL) {
  2774. state = "";
  2775. }
  2776. if (city == NULL) {
  2777. city = "";
  2778. }
  2779. if (country_code == NULL) {
  2780. country_code = "";
  2781. }
  2782. if (state_code == NULL) {
  2783. state_code = "";
  2784. }
  2785. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  2786. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  2787. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  2788. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  2789. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  2790. }] mutableCopy];
  2791. return ret;
  2792. }
  2793. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  2794. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  2795. [item setValue:value forKey:valueKey];
  2796. [dic setValue:item forKey:itemKey];
  2797. }
  2798. + (NSString *)countryCodeByid:(NSString *)code_id {
  2799. NSString *ret = nil;
  2800. code_id = [self translateSingleQuote:code_id];
  2801. sqlite3 *db = [iSalesDB get_db];
  2802. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  2803. sqlite3_stmt * statement;
  2804. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2805. while (sqlite3_step(statement) == SQLITE_ROW) {
  2806. char *code = (char *)sqlite3_column_text(statement, 0);
  2807. if (code == NULL) {
  2808. code = "";
  2809. }
  2810. ret = [NSString stringWithUTF8String:code];
  2811. }
  2812. sqlite3_finalize(statement);
  2813. }
  2814. [iSalesDB close_db:db];
  2815. return ret;
  2816. }
  2817. + (NSString *)countryCodeIdByCode:(NSString *)code {
  2818. NSString *ret = nil;
  2819. code = [self translateSingleQuote:code];
  2820. sqlite3 *db = [iSalesDB get_db];
  2821. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  2822. sqlite3_stmt * statement;
  2823. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2824. while (sqlite3_step(statement) == SQLITE_ROW) {
  2825. char *_id = (char *)sqlite3_column_text(statement, 0);
  2826. if (_id == NULL) {
  2827. _id = "";
  2828. }
  2829. ret = [NSString stringWithFormat:@"%s",_id];
  2830. }
  2831. sqlite3_finalize(statement);
  2832. }
  2833. [iSalesDB close_db:db];
  2834. return ret;
  2835. }
  2836. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  2837. NSString *name = nil;
  2838. codeId = [self translateSingleQuote:codeId];
  2839. sqlite3 *db = [iSalesDB get_db];
  2840. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  2841. sqlite3_stmt * statement;
  2842. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2843. while (sqlite3_step(statement) == SQLITE_ROW) {
  2844. char *value = (char *)sqlite3_column_text(statement, 0);
  2845. if (value == NULL) {
  2846. value = "";
  2847. }
  2848. name = [NSString stringWithUTF8String:value];
  2849. }
  2850. sqlite3_finalize(statement);
  2851. }
  2852. [iSalesDB close_db:db];
  2853. return name;
  2854. }
  2855. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  2856. NSString *ret = nil;
  2857. sqlite3 *db = [iSalesDB get_db];
  2858. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  2859. sqlite3_stmt * statement;
  2860. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2861. while (sqlite3_step(statement) == SQLITE_ROW) {
  2862. char *name = (char *)sqlite3_column_text(statement, 0);
  2863. if (name == NULL) {
  2864. name = "";
  2865. }
  2866. ret = [NSString stringWithUTF8String:name];
  2867. }
  2868. sqlite3_finalize(statement);
  2869. }
  2870. [iSalesDB close_db:db];
  2871. return ret;
  2872. }
  2873. + (NSString *)salesRepCodeById:(NSString *)_id {
  2874. NSString *ret = nil;
  2875. _id = [self translateSingleQuote:_id];
  2876. sqlite3 *db = [iSalesDB get_db];
  2877. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  2878. sqlite3_stmt * statement;
  2879. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  2880. while (sqlite3_step(statement) == SQLITE_ROW) {
  2881. char *rep = (char *)sqlite3_column_text(statement, 0);
  2882. if (rep == NULL) {
  2883. rep = "";
  2884. }
  2885. ret = [NSString stringWithUTF8String:rep];
  2886. }
  2887. sqlite3_finalize(statement);
  2888. }
  2889. [iSalesDB close_db:db];
  2890. return ret;
  2891. }
  2892. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  2893. char *tx = (char *)sqlite3_column_text(stmt, col);
  2894. if (tx == NULL) {
  2895. tx = "";
  2896. }
  2897. NSString *text = [NSString stringWithFormat:@"%s",tx];
  2898. if (!text) {
  2899. text = @"";
  2900. }
  2901. // 将字符全部为' '的字符串干掉
  2902. int spaceCount = 0;
  2903. for (int i = 0; i < text.length; i++) {
  2904. if ([text characterAtIndex:i] == ' ') {
  2905. spaceCount++;
  2906. }
  2907. }
  2908. if (spaceCount == text.length) {
  2909. text = @"";
  2910. }
  2911. return text;
  2912. }
  2913. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  2914. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  2915. NSData *data = [NSData dataWithContentsOfFile:path];
  2916. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  2917. return ret;
  2918. }
  2919. + (NSString *)textFileName:(NSString *)name {
  2920. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  2921. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  2922. if (!text) {
  2923. text = @"";
  2924. }
  2925. return text;
  2926. }
  2927. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  2928. return [[dic objectForKey:key] mutableCopy];
  2929. }
  2930. + (id)translateSingleQuote:(NSString *)string {
  2931. if ([string isKindOfClass:[NSString class]])
  2932. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2933. return string;
  2934. }
  2935. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  2936. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  2937. }
  2938. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  2939. NSString* ret= nil;
  2940. NSString *sqlQuery = nil;
  2941. // sqlQuery = [NSString stringWithFormat:@"select i.url from product m LEFT join model_image i on m.product_id = i.product_id where m.item_id=%@ order by i.default_img desc, i._id asc limit 1;",item_id];// select i.url from model m
  2942. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ order by i.default_img desc, i._id asc limit 1;",item_id];
  2943. sqlite3_stmt * statement;
  2944. // int count=0;
  2945. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2946. {
  2947. if (sqlite3_step(statement) == SQLITE_ROW)
  2948. {
  2949. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2950. if(imgurl==nil)
  2951. imgurl="";
  2952. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2953. ret=nsimgurl;
  2954. }
  2955. sqlite3_finalize(statement);
  2956. }
  2957. else
  2958. {
  2959. [ret setValue:@"8" forKey:@"result"];
  2960. }
  2961. // [iSalesDB close_db:db];
  2962. // DebugLog(@"data string: %@",ret );
  2963. return ret;
  2964. }
  2965. #pragma mark contact list
  2966. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  2967. {
  2968. // contactType = "Sales_Order_Customer";
  2969. // limit = 25;
  2970. // offset = 0;
  2971. // password = 123456;
  2972. // "price_name" = 16;
  2973. // user = EvanK;
  2974. sqlite3 *db = [iSalesDB get_db];
  2975. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  2976. if (contactType) {
  2977. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  2978. } else {
  2979. contactType = @"1 = 1";
  2980. }
  2981. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  2982. DebugLog(@"offline contact list keyword: %@",keyword);
  2983. // advanced search
  2984. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  2985. if (contact_name) {
  2986. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2987. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  2988. } else {
  2989. contact_name = @"";
  2990. }
  2991. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  2992. if (customer_phone) {
  2993. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  2994. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  2995. } else {
  2996. customer_phone = @"";
  2997. }
  2998. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  2999. if (customer_fax) {
  3000. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3001. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  3002. } else {
  3003. customer_fax = @"";
  3004. }
  3005. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  3006. if (customer_zipcode) {
  3007. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3008. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  3009. } else {
  3010. customer_zipcode = @"";
  3011. }
  3012. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  3013. if (customer_sales_rep) {
  3014. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3015. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  3016. } else {
  3017. customer_sales_rep = @"";
  3018. }
  3019. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  3020. if (customer_state) {
  3021. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3022. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  3023. } else {
  3024. customer_state = @"";
  3025. }
  3026. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  3027. if (customer_name) {
  3028. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3029. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  3030. } else {
  3031. customer_name = @"";
  3032. }
  3033. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  3034. if (customer_country) {
  3035. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3036. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  3037. } else {
  3038. customer_country = @"";
  3039. }
  3040. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  3041. if (customer_cid) {
  3042. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3043. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  3044. } else {
  3045. customer_cid = @"";
  3046. }
  3047. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  3048. if (customer_city) {
  3049. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3050. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  3051. } else {
  3052. customer_city = @"";
  3053. }
  3054. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  3055. if (customer_address) {
  3056. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3057. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  3058. } else {
  3059. customer_address = @"";
  3060. }
  3061. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  3062. if (customer_email) {
  3063. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3064. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  3065. } else {
  3066. customer_email = @"";
  3067. }
  3068. NSString *price_name = [params valueForKey:@"price_name"];
  3069. if (price_name) {
  3070. if ([price_name containsString:@","]) {
  3071. // 首先从 price表中查处name
  3072. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  3073. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  3074. for (int i = 1;i < pArray.count;i++) {
  3075. NSString *p = pArray[i];
  3076. [mutablePStr appendFormat:@" or type = %@ ",p];
  3077. }
  3078. [mutablePStr appendString:@";"];
  3079. __block NSMutableArray *price_name_array = [NSMutableArray array];
  3080. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3081. char *name = (char *)sqlite3_column_text(stmt, 0);
  3082. if (!name)
  3083. name = "";
  3084. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  3085. }];
  3086. // 再根据name 拼sql
  3087. NSMutableString *mutable_price_name = [NSMutableString string];
  3088. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  3089. for (int i = 1; i < price_name_array.count; i++) {
  3090. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  3091. }
  3092. [mutable_price_name appendString:@")"];
  3093. price_name = mutable_price_name;
  3094. } else {
  3095. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  3096. if ([price_name isEqualToString:@""]) {
  3097. price_name = @"";
  3098. } else {
  3099. __block NSString *price;
  3100. price_name = [self translateSingleQuote:price_name];
  3101. [iSalesDB jk_query:[NSString stringWithFormat:@"SELECT name FROM price where type = %@ ;",price_name] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3102. char *p = (char *)sqlite3_column_text(stmt, 0);
  3103. if (p == NULL) {
  3104. p = "";
  3105. }
  3106. price = [NSString stringWithUTF8String:p];
  3107. }];
  3108. if ([price isEqualToString:@""]) {
  3109. price_name = @"";
  3110. } else {
  3111. price = [self translateSingleQuote:price];
  3112. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  3113. }
  3114. }
  3115. }
  3116. } else {
  3117. price_name = @"";
  3118. }
  3119. int limit = [[params valueForKey:@"limit"] intValue];
  3120. int offset = [[params valueForKey:@"offset"] intValue];
  3121. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3122. NSString *where = [NSString stringWithFormat:@" %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ and is_active=1 ",contactType,contact_name,customer_phone,customer_fax,customer_zipcode,customer_sales_rep,customer_state,customer_name,customer_country,customer_cid,customer_city,customer_address,customer_email,price_name];
  3123. NSString *sql = [NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ order by decrypt(company_name) limit %d offset %d",where,limit,offset];
  3124. // int result= [iSalesDB AddExFunction:db];
  3125. int count =0;
  3126. NSString *sqlQuery = nil;
  3127. if(keyword.length==0)
  3128. {
  3129. // sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@=1 order by decrypt(company_name) limit %d offset %d",contactType , limit, offset];
  3130. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  3131. sqlQuery = sql;
  3132. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  3133. }
  3134. else
  3135. {
  3136. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  3137. keyword = keyword.lowercaseString;
  3138. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  3139. sqlQuery=[NSString stringWithFormat:@"select editable,company_name,country,addr,zipcode,state,city,contact_name,phone,contact_id,addr_1,addr_2,addr_3,addr_4,first_name,last_name,fax,email from offline_contact where %@ and (lower(contact_id ) like'%%%@%%' or lower(decrypt(company_name)) like '%%%@%%' or lower(decrypt(phone)) like '%%%@%%' or lower(decrypt(addr)) like '%%%@%%') and is_active=1 order by decrypt(company_name) limit %d offset %d",contactType , keyword,keyword,keyword,keyword,limit, offset];
  3140. count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@=1 and (lower(contact_id ) like'%@' or lower(company_name) like'%@') and is_active=1",contactType,keyword,keyword]];
  3141. }
  3142. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3143. sqlite3_stmt * statement;
  3144. [ret setValue:@"2" forKey:@"result"];
  3145. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  3146. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3147. {
  3148. int i = 0;
  3149. while (sqlite3_step(statement) == SQLITE_ROW)
  3150. {
  3151. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3152. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3153. int editable = sqlite3_column_int(statement, 0);
  3154. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3155. NSString *nscompany_name =nil;
  3156. if(company_name==nil)
  3157. nscompany_name=@"";
  3158. else
  3159. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  3160. char *country = (char*)sqlite3_column_text(statement, 2);
  3161. if(country==nil)
  3162. country="";
  3163. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3164. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3165. // if(addr==nil)
  3166. // addr="";
  3167. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3168. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3169. if(zipcode==nil)
  3170. zipcode="";
  3171. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3172. char *state = (char*)sqlite3_column_text(statement, 5);
  3173. if(state==nil)
  3174. state="";
  3175. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3176. char *city = (char*)sqlite3_column_text(statement, 6);
  3177. if(city==nil)
  3178. city="";
  3179. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3180. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3181. // NSString *nscontact_name = nil;
  3182. // if(contact_name==nil)
  3183. // nscontact_name=@"";
  3184. // else
  3185. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3186. char *phone = (char*)sqlite3_column_text(statement, 8);
  3187. NSString *nsphone = nil;
  3188. if(phone==nil)
  3189. nsphone=@"";
  3190. else
  3191. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3192. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3193. if(contact_id==nil)
  3194. contact_id="";
  3195. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3196. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3197. if(addr_1==nil)
  3198. addr_1="";
  3199. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3200. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3201. if(addr_2==nil)
  3202. addr_2="";
  3203. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3204. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3205. if(addr_3==nil)
  3206. addr_3="";
  3207. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3208. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3209. if(addr_4==nil)
  3210. addr_4="";
  3211. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3212. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3213. if(first_name==nil)
  3214. first_name="";
  3215. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3216. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3217. if(last_name==nil)
  3218. last_name="";
  3219. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3220. char *fax = (char*)sqlite3_column_text(statement, 16);
  3221. NSString *nsfax = nil;
  3222. if(fax==nil)
  3223. nsfax=@"";
  3224. else
  3225. {
  3226. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3227. if(nsfax.length>0)
  3228. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  3229. }
  3230. char *email = (char*)sqlite3_column_text(statement, 17);
  3231. NSString *nsemail = nil;
  3232. if(email==nil)
  3233. nsemail=@"";
  3234. else
  3235. {
  3236. nsemail= [[NSString alloc]initWithUTF8String:email];
  3237. if(nsemail.length>0)
  3238. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  3239. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  3240. }
  3241. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3242. [arr_name addObject:nsfirst_name];
  3243. [arr_name addObject:nslast_name];
  3244. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3245. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  3246. {
  3247. // decrypt
  3248. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3249. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3250. nsphone=[AESCrypt fastdecrypt:nsphone];
  3251. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3252. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3253. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3254. }
  3255. [arr_addr addObject:nscompany_name];
  3256. [arr_addr addObject:nscontact_name];
  3257. [arr_addr addObject:@"<br>"];
  3258. [arr_addr addObject:nsaddr_1];
  3259. [arr_addr addObject:nsaddr_2];
  3260. [arr_addr addObject:nsaddr_3];
  3261. [arr_addr addObject:nsaddr_4];
  3262. //[arr_addr addObject:nsaddr];
  3263. [arr_addr addObject:nszipcode];
  3264. [arr_addr addObject:nscity];
  3265. [arr_addr addObject:nsstate];
  3266. [arr_addr addObject:nscountry];
  3267. [arr_addr addObject:@"<br>"];
  3268. [arr_addr addObject:nsphone];
  3269. [arr_addr addObject:nsfax];
  3270. [arr_addr addObject:nsemail];
  3271. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  3272. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  3273. [item setValue:name forKey:@"name"];
  3274. [item setValue:nscontact_id forKey:@"contact_id"];
  3275. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  3276. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3277. i++;
  3278. }
  3279. UIApplication * app = [UIApplication sharedApplication];
  3280. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3281. [ret setValue:appDelegate.mode forKey:@"mode"];
  3282. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  3283. sqlite3_finalize(statement);
  3284. }
  3285. [iSalesDB close_db:db];
  3286. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3287. return ret;
  3288. }
  3289. #pragma mark contact Advanced search
  3290. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  3291. {
  3292. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  3293. return [RAUtils dict2data:contactAdvanceDic];
  3294. }
  3295. #pragma mark create new contact
  3296. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  3297. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  3298. NSData *data = [NSData dataWithContentsOfFile:path];
  3299. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3300. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3301. NSString *countryCode = nil;
  3302. NSString *countryCode_id = nil;
  3303. NSString *stateCode = nil;
  3304. NSString *city = nil;
  3305. NSString *zipCode = nil;
  3306. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3307. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  3308. NSString *code_id = params[@"country"];
  3309. countryCode_id = code_id;
  3310. countryCode = [self countryCodeByid:code_id];
  3311. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  3312. NSString *zip_code = params[@"zipcode"];
  3313. // 剔除全部为空格
  3314. int spaceCount = 0;
  3315. for (int i = 0; i < zip_code.length; i++) {
  3316. if ([zip_code characterAtIndex:i] == ' ') {
  3317. spaceCount++;
  3318. }
  3319. }
  3320. if (spaceCount == zip_code.length) {
  3321. zip_code = @"";
  3322. }
  3323. zipCode = zip_code;
  3324. if (zipCode.length > 0) {
  3325. countryCode_id = params[@"country"];
  3326. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  3327. countryCode = [dic valueForKey:@"country_code"];
  3328. if (!countryCode) {
  3329. countryCode = @"US";
  3330. }
  3331. stateCode = [dic valueForKey:@"state_code"];
  3332. city = [dic valueForKey:@"city"];
  3333. // zip code
  3334. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_10"] mutableCopy];
  3335. [zipDic setValue:zipCode forKey:@"value"];
  3336. [section_0 setValue:zipDic forKey:@"item_10"];
  3337. }
  3338. }
  3339. } else {
  3340. // default: US United States
  3341. countryCode = @"US";
  3342. countryCode_id = @"228";
  3343. }
  3344. // country
  3345. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3346. [self setValue:allCountry forItemKey:@"item_5" valueKey:@"cadedate" inDictionary:section_0];
  3347. // state
  3348. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  3349. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  3350. [self setValue:allState forItemKey:@"item_11" valueKey:@"cadedate" inDictionary:section_0];
  3351. // city
  3352. if (city) {
  3353. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_12"] mutableCopy];
  3354. [cityDic setValue:city forKey:@"value"];
  3355. [section_0 setValue:cityDic forKey:@"item_12"];
  3356. }
  3357. // price type
  3358. NSDictionary *priceDic = [self offline_getPrice];
  3359. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  3360. // Sales Rep
  3361. NSDictionary *repDic = [self offline_getSalesRep];
  3362. [self setValue:repDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  3363. [ret setValue:section_0 forKey:@"section_0"];
  3364. return [RAUtils dict2data:ret];
  3365. }
  3366. #pragma mark save
  3367. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  3368. NSString *addr = nil;
  3369. NSString *contact_name = nil;
  3370. NSString *companyName = [params objectForKey:@"company"];
  3371. if (companyName) {
  3372. companyName = [AESCrypt fastencrypt:companyName];
  3373. } else {
  3374. companyName = @"";
  3375. }
  3376. DebugLog(@"company");
  3377. companyName = [self translateSingleQuote:companyName];
  3378. NSString *addr1 = [params objectForKey:@"address"];
  3379. NSString *addr2 = [params objectForKey:@"address2"];
  3380. NSString *addr3 = [params objectForKey:@"address_3"];
  3381. NSString *addr4 = [params objectForKey:@"address_4"];
  3382. if (!addr2) {
  3383. addr2 = @"";
  3384. }
  3385. if (!addr3) {
  3386. addr3 = @"";
  3387. }
  3388. if (!addr4) {
  3389. addr4 = @"";
  3390. }
  3391. if (!addr1) {
  3392. addr1 = @"";
  3393. }
  3394. DebugLog(@"addr");
  3395. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  3396. addr = [AESCrypt fastencrypt:addr];
  3397. addr = [self translateSingleQuote:addr];
  3398. if (addr1 && ![addr1 isEqualToString:@""]) {
  3399. addr1 = [AESCrypt fastencrypt:addr1];
  3400. }
  3401. addr1 = [self translateSingleQuote:addr1];
  3402. addr2 = [self translateSingleQuote:addr2];
  3403. addr3 = [self translateSingleQuote:addr3];
  3404. addr4 = [self translateSingleQuote:addr4];
  3405. NSString *country = [params objectForKey:@"country"];
  3406. if (country) {
  3407. country = [self countryNameByCountryCodeId:country];
  3408. } else {
  3409. country = @"";
  3410. }
  3411. DebugLog(@"country");
  3412. country = [self translateSingleQuote:country];
  3413. NSString *state = [params objectForKey:@"state"];
  3414. if (!state) {
  3415. state = @"";
  3416. }
  3417. DebugLog(@"state");
  3418. state = [self translateSingleQuote:state];
  3419. NSString *city = [params objectForKey:@"city"];
  3420. if (!city) {
  3421. city = @"";
  3422. }
  3423. city = [self translateSingleQuote:city];
  3424. NSString *zipcode = [params objectForKey:@"zipcode"];
  3425. if (!zipcode) {
  3426. zipcode = @"";
  3427. }
  3428. DebugLog(@"zip");
  3429. zipcode = [self translateSingleQuote:zipcode];
  3430. NSString *fistName = [params objectForKey:@"firstname"];
  3431. if (!fistName) {
  3432. fistName = @"";
  3433. }
  3434. NSString *lastName = [params objectForKey:@"lastname"];
  3435. if (!lastName) {
  3436. lastName = @"";
  3437. }
  3438. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  3439. DebugLog(@"contact_name");
  3440. contact_name = [self translateSingleQuote:contact_name];
  3441. fistName = [self translateSingleQuote:fistName];
  3442. lastName = [self translateSingleQuote:lastName];
  3443. NSString *phone = [params objectForKey:@"phone"];
  3444. if (phone) {
  3445. phone = [AESCrypt fastencrypt:phone];
  3446. } else {
  3447. phone = @"";
  3448. }
  3449. DebugLog(@"PHONE");
  3450. phone = [self translateSingleQuote:phone];
  3451. NSString *fax = [params objectForKey:@"fax"];
  3452. if (!fax) {
  3453. fax = @"";
  3454. }
  3455. DebugLog(@"FAX");
  3456. fax = [self translateSingleQuote:fax];
  3457. NSString *email = [params objectForKey:@"email"];
  3458. if (!email) {
  3459. email = @"";
  3460. }
  3461. DebugLog(@"EMAIL:%@",email);
  3462. email = [self translateSingleQuote:email];
  3463. NSString *notes = [params objectForKey:@"contact_notes"];
  3464. if (!notes) {
  3465. notes = @"";
  3466. }
  3467. DebugLog(@"NOTE:%@",notes);
  3468. notes = [self translateSingleQuote:notes];
  3469. NSString *price = [params objectForKey:@"price_name"];
  3470. if (price) {
  3471. price = [self priceNameByPriceId:price];
  3472. } else {
  3473. price = @"";
  3474. }
  3475. DebugLog(@"PRICE");
  3476. price = [self translateSingleQuote:price];
  3477. NSString *salesRep = [params objectForKey:@"sales_rep"];
  3478. if (salesRep) {
  3479. salesRep = [self salesRepCodeById:salesRep];
  3480. } else {
  3481. salesRep = @"";
  3482. }
  3483. salesRep = [self translateSingleQuote:salesRep];
  3484. NSString *img = [params objectForKey:@"business_card"];
  3485. NSArray *array = [img componentsSeparatedByString:@","];
  3486. NSString *img_0 = array[0];
  3487. if (!img_0) {
  3488. img_0 = @"";
  3489. }
  3490. img_0 = [self translateSingleQuote:img_0];
  3491. NSString *img_1 = array[1];
  3492. if (!img_1) {
  3493. img_1 = @"";
  3494. }
  3495. img_1 = [self translateSingleQuote:img_1];
  3496. NSString *img_2 = array[2];
  3497. if (!img_2) {
  3498. img_2 = @"";
  3499. }
  3500. img_2 = [self translateSingleQuote:img_2];
  3501. NSString *contact_id = [NSUUID UUID].UUIDString;
  3502. // 判断更新时是否为customer
  3503. if (update) {
  3504. contact_id = [params objectForKey:@"contact_id"];
  3505. if (!contact_id) {
  3506. contact_id = @"";
  3507. }
  3508. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  3509. __block int customer = 0;
  3510. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3511. customer = sqlite3_column_int(stmt, 0);
  3512. }];
  3513. isCustomer = customer ? YES : NO;
  3514. }
  3515. NSMutableDictionary *sync_dic = [params mutableCopy];
  3516. if (isCustomer) {
  3517. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  3518. } else {
  3519. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  3520. }
  3521. NSString *sync_data = nil;
  3522. NSString *sql = nil;
  3523. if (update){
  3524. contact_id = [params objectForKey:@"contact_id"];
  3525. if (!contact_id) {
  3526. contact_id = @"";
  3527. }
  3528. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3529. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3530. sync_data = [RAUtils dict2string:sync_dic];
  3531. sync_data = [self translateSingleQuote:sync_data];
  3532. sql = [NSString stringWithFormat:@"update offline_contact set company_name = '%@',addr_1 = '%@',addr_2 = '%@',addr_3 = '%@',addr_4 = '%@',country = '%@',state = '%@',city = '%@',zipcode = '%@',first_name = '%@',last_name = '%@',phone = '%@',fax = '%@',email = '%@',notes = '%@',price_type = '%@',sales_rep = '%@',img_0 = '%@',img_1 = '%@',img_2 = '%@',sync_data = '%@',contact_name = '%@',addr = '%@' where contact_id = '%@';",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,sync_data,contact_name,addr,contact_id];
  3533. } else {
  3534. contact_id = [self translateSingleQuote:contact_id];
  3535. [sync_dic setValue:contact_id forKey:@"contact_id"];
  3536. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  3537. sync_data = [RAUtils dict2string:sync_dic];
  3538. sync_data = [self translateSingleQuote:sync_data];
  3539. sql = [NSString stringWithFormat:@"insert into offline_contact (is_active,company_name,addr_1,addr_2,addr_3,addr_4,country,state,city,zipcode,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2,editable,contact_id,Sales_Order_Customer,Sales_Order_Freight_Bill_To,Sales_Order_Ship_From,Sales_Order_Merchandise_Bill_To,Contact_Return_To,Sales_Order_Ship_To,sync_data,contact_name,addr) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',%d,%d,0,%d,0,1,'%@','%@','%@')",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id,isCustomer ? 1 : 0,isCustomer ? 1 : 0,isCustomer ? 1 : 0,sync_data,contact_name,addr];
  3540. }
  3541. int result = [iSalesDB execSql:sql];
  3542. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  3543. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  3544. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  3545. }
  3546. #pragma mark save new contact
  3547. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  3548. {
  3549. return [self offline_saveContact:params update:NO isCustomer:YES];
  3550. }
  3551. #pragma mark edit contact
  3552. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  3553. {
  3554. // {
  3555. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  3556. // password = 123456;
  3557. // user = EvanK;
  3558. // }
  3559. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  3560. NSData *data = [NSData dataWithContentsOfFile:path];
  3561. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  3562. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  3563. NSString *countryCode = nil;
  3564. NSString *countryCode_id = nil;
  3565. NSString *stateCode = nil;
  3566. /*------contact infor------*/
  3567. __block NSString *country = nil;
  3568. __block NSString *company_name = nil;
  3569. __block NSString *contact_id = params[@"contact_id"];
  3570. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  3571. __block NSString *zipcode = nil;
  3572. __block NSString *state = nil; // state_code
  3573. __block NSString *city = nil; //
  3574. __block NSString *firt_name,*last_name;
  3575. __block NSString *phone,*fax,*email;
  3576. __block NSString *notes,*price_type,*sales_rep;
  3577. __block NSString *img_0,*img_1,*img_2;
  3578. contact_id = [self translateSingleQuote:contact_id];
  3579. NSString *sql = [NSString stringWithFormat:@"select country,company_name,addr_1,addr_2,addr_3,addr_4,zipcode,state,city,first_name,last_name,phone,fax,email,notes,price_type,sales_rep,img_0,img_1,img_2 from offline_contact where contact_id = '%@';",contact_id];
  3580. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3581. country = [self textAtColumn:0 statement:stmt]; // country name
  3582. company_name = [self textAtColumn:1 statement:stmt];
  3583. addr_1 = [self textAtColumn:2 statement:stmt];
  3584. addr_2 = [self textAtColumn:3 statement:stmt];
  3585. addr_3 = [self textAtColumn:4 statement:stmt];
  3586. addr_4 = [self textAtColumn:5 statement:stmt];
  3587. zipcode = [self textAtColumn:6 statement:stmt];
  3588. state = [self textAtColumn:7 statement:stmt]; // state code
  3589. city = [self textAtColumn:8 statement:stmt];
  3590. firt_name = [self textAtColumn:9 statement:stmt];
  3591. last_name = [self textAtColumn:10 statement:stmt];
  3592. phone = [self textAtColumn:11 statement:stmt];
  3593. fax = [self textAtColumn:12 statement:stmt];
  3594. email = [self textAtColumn:13 statement:stmt];
  3595. notes = [self textAtColumn:14 statement:stmt];
  3596. price_type = [self textAtColumn:15 statement:stmt]; // name
  3597. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  3598. img_0 = [self textAtColumn:17 statement:stmt];
  3599. img_1 = [self textAtColumn:18 statement:stmt];
  3600. img_2 = [self textAtColumn:19 statement:stmt];
  3601. }];
  3602. // decrypt
  3603. if (company_name) {
  3604. company_name = [AESCrypt fastdecrypt:company_name];
  3605. }
  3606. if (addr_1) {
  3607. addr_1 = [AESCrypt fastdecrypt:addr_1];
  3608. }
  3609. if (phone) {
  3610. phone = [AESCrypt fastdecrypt:phone];
  3611. }
  3612. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  3613. countryCode = [iSalesDB jk_queryText:countrySql];
  3614. stateCode = state;
  3615. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  3616. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  3617. NSString *code_id = params[@"country"];
  3618. countryCode_id = code_id;
  3619. countryCode = [self countryCodeByid:code_id];
  3620. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  3621. NSString *zip_code = params[@"zipcode"];
  3622. // 剔除全部为空格
  3623. int spaceCount = 0;
  3624. for (int i = 0; i < zip_code.length; i++) {
  3625. if ([zip_code characterAtIndex:i] == ' ') {
  3626. spaceCount++;
  3627. }
  3628. }
  3629. if (spaceCount == zip_code.length) {
  3630. zip_code = @"";
  3631. }
  3632. if (zipcode.length > 0) {
  3633. zipcode = zip_code;
  3634. countryCode_id = params[@"country"];
  3635. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  3636. countryCode = [dic valueForKey:@"country_code"];
  3637. stateCode = [dic valueForKey:@"state_code"];
  3638. city = [dic valueForKey:@"city"];
  3639. // zip code
  3640. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  3641. [zipDic setValue:zipcode forKey:@"value"];
  3642. [section_0 setValue:zipDic forKey:@"item_8"];
  3643. }
  3644. }
  3645. }
  3646. // 0 Country
  3647. // 1 Company Name
  3648. // 2 Contact ID
  3649. // 3 Picture
  3650. // 4 Address 1
  3651. // 5 Address 2
  3652. // 6 Address 3
  3653. // 7 Address 4
  3654. // 8 Zip Code
  3655. // 9 State/Province
  3656. // 10 City
  3657. // 11 Contact First Name
  3658. // 12 Contact Last Name
  3659. // 13 Phone
  3660. // 14 Fax
  3661. // 15 Email
  3662. // 16 Contact Notes
  3663. // 17 Price Type
  3664. // 18 Sales Rep
  3665. // country
  3666. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  3667. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  3668. // company
  3669. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  3670. // contact_id
  3671. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  3672. // picture
  3673. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  3674. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  3675. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  3676. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  3677. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  3678. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  3679. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  3680. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  3681. // addr 1 2 3 4
  3682. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  3683. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  3684. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  3685. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  3686. // zip code
  3687. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  3688. // state
  3689. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  3690. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  3691. // city
  3692. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  3693. // first last
  3694. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  3695. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  3696. // phone fax email
  3697. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  3698. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  3699. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  3700. // notes
  3701. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  3702. // price
  3703. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  3704. for (NSString *key in priceDic.allKeys) {
  3705. if ([key containsString:@"val_"]) {
  3706. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  3707. if ([dic[@"value"] isEqualToString:price_type]) {
  3708. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3709. [priceDic setValue:dic forKey:key];
  3710. }
  3711. }
  3712. }
  3713. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  3714. // Sales Rep
  3715. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  3716. for (NSString *key in repDic.allKeys) {
  3717. if ([key containsString:@"val_"]) {
  3718. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  3719. NSString *value = dic[@"value"];
  3720. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  3721. if (code && [code isEqualToString:sales_rep]) {
  3722. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3723. [repDic setValue:dic forKey:key];
  3724. }
  3725. }
  3726. }
  3727. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  3728. [ret setValue:section_0 forKey:@"section_0"];
  3729. return [RAUtils dict2data:ret];
  3730. }
  3731. #pragma mark save contact
  3732. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  3733. {
  3734. return [self offline_saveContact:params update:YES isCustomer:YES];
  3735. }
  3736. #pragma mark category
  3737. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  3738. if (ck) {
  3739. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  3740. for (NSString *key in res.allKeys) {
  3741. if (![key isEqualToString:@"count"]) {
  3742. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  3743. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  3744. if ([val[@"value"] isEqualToString:ck]) {
  3745. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  3746. }
  3747. [res setValue:val forKey:key];
  3748. }
  3749. }
  3750. [dic setValue:res forKey:valueKey];
  3751. }
  3752. }
  3753. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  3754. // NSString* orderCode = [params valueForKey:@"orderCode"];
  3755. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3756. NSString* category = [params valueForKey:@"category"];
  3757. if (!category || [category isEqualToString:@""]) {
  3758. category = @"%";
  3759. }
  3760. category = [self translateSingleQuote:category];
  3761. int limit = [[params valueForKey:@"limit"] intValue];
  3762. int offset = [[params valueForKey:@"offset"] intValue];
  3763. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3764. NSString *limit_str = @"";
  3765. if (limited) {
  3766. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  3767. }
  3768. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  3769. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  3770. sqlite3 *db = [iSalesDB get_db];
  3771. // [iSalesDB AddExFunction:db];
  3772. int count;
  3773. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  3774. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, category,limit_str];
  3775. double price_min = 0;
  3776. double price_max = 0;
  3777. if ([params.allKeys containsObject:@"alert"]) {
  3778. // alert
  3779. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  3780. NSString *alert = params[@"alert"];
  3781. if ([alert isEqualToString:@"Display All"]) {
  3782. alert = [NSString stringWithFormat:@""];
  3783. } else {
  3784. alert = [self translateSingleQuote:alert];
  3785. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  3786. }
  3787. // available
  3788. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  3789. NSString *available = params[@"available"];
  3790. NSString *available_condition;
  3791. if ([available isEqualToString:@"Display All"]) {
  3792. available_condition = @"";
  3793. } else if ([available isEqualToString:@"Available Now"]) {
  3794. available_condition = @"and availability > 0";
  3795. } else {
  3796. available_condition = @"and availability == 0";
  3797. }
  3798. // best seller
  3799. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  3800. NSString *best_seller = @"";
  3801. NSString *order_best_seller = @"m.name asc";
  3802. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  3803. best_seller = @"and best_seller > 0";
  3804. order_best_seller = @"m.best_seller desc,m.name asc";
  3805. }
  3806. // price
  3807. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  3808. NSString *price = params[@"price"];
  3809. price_min = 0;
  3810. price_max = MAXFLOAT;
  3811. if (appDelegate.user) {
  3812. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  3813. NSMutableString *priceName = [NSMutableString string];
  3814. for (int i = 0; i < priceTypeArray.count; i++) {
  3815. NSString *pricetype = priceTypeArray[i];
  3816. pricetype = [self translateSingleQuote:pricetype];
  3817. if (i == 0) {
  3818. [priceName appendFormat:@"'%@'",pricetype];
  3819. } else {
  3820. [priceName appendFormat:@",'%@'",pricetype];
  3821. }
  3822. }
  3823. if ([price isEqualToString:@"Display All"]) {
  3824. price = [NSString stringWithFormat:@""];
  3825. } else if([price containsString:@"+"]){
  3826. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  3827. price_min = [price doubleValue];
  3828. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  3829. } else {
  3830. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  3831. price_min = [[priceArray objectAtIndex:0] doubleValue];
  3832. price_max = [[priceArray objectAtIndex:1] doubleValue];
  3833. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  3834. }
  3835. } else {
  3836. price = @"";
  3837. }
  3838. // sold_by_qty : Sold in quantities of %@
  3839. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  3840. NSString *qty = params[@"sold_by_qty"];
  3841. if ([qty isEqualToString:@"Display All"]) {
  3842. qty = @"";
  3843. } else {
  3844. qty = [self translateSingleQuote:qty];
  3845. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  3846. }
  3847. // cate
  3848. category = [self translateSingleQuote:category];
  3849. NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  3850. // where bestseller > 0 order by bestseller desc
  3851. // sql query: alert availability(int) best_seller(int) price qty
  3852. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;",cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  3853. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  3854. }
  3855. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  3856. if (!appDelegate.user) {
  3857. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  3858. }
  3859. [ret setValue:filter forKey:@"filter"];
  3860. DebugLog(@"offline_category sql:%@",sqlQuery);
  3861. sqlite3_stmt * statement;
  3862. [ret setValue:@"2" forKey:@"result"];
  3863. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  3864. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  3865. // int count=0;
  3866. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3867. {
  3868. int i=0;
  3869. while (sqlite3_step(statement) == SQLITE_ROW)
  3870. {
  3871. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3872. char *name = (char*)sqlite3_column_text(statement, 0);
  3873. if(name==nil)
  3874. name="";
  3875. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3876. char *description = (char*)sqlite3_column_text(statement, 1);
  3877. if(description==nil)
  3878. description="";
  3879. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  3880. int product_id = sqlite3_column_int(statement, 2);
  3881. int wid = sqlite3_column_int(statement, 3);
  3882. int closeout = sqlite3_column_int(statement, 4);
  3883. int cid = sqlite3_column_int(statement, 5);
  3884. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  3885. if(wid==0)
  3886. [item setValue:@"false" forKey:@"wish_exists"];
  3887. else
  3888. [item setValue:@"true" forKey:@"wish_exists"];
  3889. if(closeout==0)
  3890. [item setValue:@"false" forKey:@"is_closeout"];
  3891. else
  3892. [item setValue:@"true" forKey:@"is_closeout"];
  3893. if(cid==0)
  3894. [item setValue:@"false" forKey:@"cart_exists"];
  3895. else
  3896. [item setValue:@"true" forKey:@"cart_exists"];
  3897. [item addEntriesFromDictionary:imgjson];
  3898. // [item setValue:nsurl forKey:@"img"];
  3899. [item setValue:nsname forKey:@"name"];
  3900. [item setValue:nsdescription forKey:@"description"];
  3901. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3902. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3903. i++;
  3904. }
  3905. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3906. [ret setObject:items forKey:@"items"];
  3907. sqlite3_finalize(statement);
  3908. } else {
  3909. DebugLog(@"nothing...");
  3910. }
  3911. DebugLog(@"count:%d",count);
  3912. [iSalesDB close_db:db];
  3913. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3914. return ret;
  3915. }
  3916. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  3917. {
  3918. return [self categoryList:params limited:YES];
  3919. }
  3920. # pragma mark item search
  3921. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  3922. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  3923. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  3924. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  3925. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  3926. // category
  3927. NSDictionary *category_menu = [self offline_category_menu];
  3928. [filter setValue:category_menu forKey:@"category"];
  3929. NSString* where= nil;
  3930. NSString* orderby= @"m.name";
  3931. if (!filterSearch) {
  3932. int covertype = [[params valueForKey:@"covertype"] intValue];
  3933. switch (covertype) {
  3934. case 0:
  3935. {
  3936. where=@"m.category like'%%#005#%%'";
  3937. break;
  3938. }
  3939. case 1:
  3940. {
  3941. where=@"m.alert like '%QS%'";
  3942. break;
  3943. }
  3944. case 2:
  3945. {
  3946. where=@"m.availability>0";
  3947. break;
  3948. }
  3949. case 3:
  3950. {
  3951. where=@"m.best_seller>0";
  3952. orderby=@"m.best_seller desc,m.name asc";
  3953. break;
  3954. }
  3955. default:
  3956. where=@"1=1";
  3957. break;
  3958. }
  3959. }
  3960. int limit = [[params valueForKey:@"limit"] intValue];
  3961. int offset = [[params valueForKey:@"offset"] intValue];
  3962. NSString *limit_str = @"";
  3963. if (limited) {
  3964. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  3965. }
  3966. sqlite3 *db = [iSalesDB get_db];
  3967. // [iSalesDB AddExFunction:db];
  3968. int count;
  3969. NSString *sqlQuery = nil;
  3970. where = [where stringByAppendingString:@" and m.is_active = 1"];
  3971. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where %@ order by %@ %@;",appDelegate.order_code, where,orderby,limit_str];
  3972. double price_min = 0;
  3973. double price_max = 0;
  3974. if (filterSearch) {
  3975. // alert
  3976. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  3977. NSString *alert = params[@"alert"];
  3978. if ([alert isEqualToString:@"Display All"]) {
  3979. alert = [NSString stringWithFormat:@""];
  3980. } else {
  3981. alert = [self translateSingleQuote:alert];
  3982. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  3983. }
  3984. // available
  3985. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  3986. NSString *available = params[@"available"];
  3987. NSString *available_condition;
  3988. if ([available isEqualToString:@"Display All"]) {
  3989. available_condition = @"";
  3990. } else if ([available isEqualToString:@"Available Now"]) {
  3991. available_condition = @"and availability > 0";
  3992. } else {
  3993. available_condition = @"and availability == 0";
  3994. }
  3995. // best seller
  3996. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  3997. NSString *best_seller = @"";
  3998. NSString *order_best_seller = @"m.name asc";
  3999. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  4000. best_seller = @"and best_seller > 0";
  4001. order_best_seller = @"m.best_seller desc,m.name asc";
  4002. }
  4003. // price
  4004. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  4005. NSString *price = params[@"price"];
  4006. price_min = 0;
  4007. price_max = MAXFLOAT;
  4008. if (appDelegate.user) {
  4009. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  4010. NSMutableString *priceName = [NSMutableString string];
  4011. for (int i = 0; i < priceTypeArray.count; i++) {
  4012. NSString *pricetype = priceTypeArray[i];
  4013. pricetype = [self translateSingleQuote:pricetype];
  4014. if (i == 0) {
  4015. [priceName appendFormat:@"'%@'",pricetype];
  4016. } else {
  4017. [priceName appendFormat:@",'%@'",pricetype];
  4018. }
  4019. }
  4020. if ([price isEqualToString:@"Display All"]) {
  4021. price = [NSString stringWithFormat:@""];
  4022. } else if([price containsString:@"+"]){
  4023. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  4024. price_min = [price doubleValue];
  4025. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  4026. } else {
  4027. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  4028. price_min = [[priceArray objectAtIndex:0] doubleValue];
  4029. price_max = [[priceArray objectAtIndex:1] doubleValue];
  4030. price = [NSString stringWithFormat:@"and item_id in (select DISTINCT item_id from model_price where price_name in (%@) and cast(decrypt(price) as double) >= %.2lf and cast(decrypt(price) as double) <= %.2lf)",priceName,price_min,price_max];
  4031. }
  4032. } else {
  4033. price = @"";
  4034. }
  4035. // sold_by_qty : Sold in quantities of %@
  4036. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  4037. NSString *qty = params[@"sold_by_qty"];
  4038. if ([qty isEqualToString:@"Display All"]) {
  4039. qty = @"";
  4040. } else {
  4041. qty = [self translateSingleQuote:qty];
  4042. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  4043. }
  4044. // category
  4045. NSString *category_id = params[@"ctgId"];
  4046. NSMutableArray *cate_id_array = nil;
  4047. NSMutableString *cateWhere = [NSMutableString string];
  4048. if ([category_id isEqualToString:@""] || !category_id) {
  4049. [cateWhere appendString:@"1 = 1"];
  4050. } else {
  4051. if ([category_id containsString:@","]) {
  4052. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  4053. } else {
  4054. cate_id_array = [@[category_id] mutableCopy];
  4055. }
  4056. /*-----------*/
  4057. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];
  4058. for (int i = 0; i < cate_id_array.count; i++) {
  4059. for (NSString *key0 in cateDic.allKeys) {
  4060. if ([key0 containsString:@"category_"]) {
  4061. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy];
  4062. for (NSString *key1 in category0.allKeys) {
  4063. if ([key1 containsString:@"category_"]) {
  4064. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy];
  4065. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  4066. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  4067. cate_id_array[i] = [category1 objectForKey:@"id"];
  4068. if (i == 0) {
  4069. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  4070. } else {
  4071. [cateWhere appendFormat:@"or category like'%%#%@#%%'",cate_id_array[i]];
  4072. }
  4073. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  4074. [category0 setValue:category1 forKey:key1];
  4075. [cateDic setValue:category0 forKey:key0];
  4076. }
  4077. }
  4078. }
  4079. }
  4080. }
  4081. }
  4082. [filter setValue:cateDic forKey:@"category"];
  4083. }
  4084. // where bestseller > 0 order by bestseller desc
  4085. // sql query: alert availability(int) best_seller(int) price qty
  4086. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;", cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  4087. // count
  4088. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  4089. }
  4090. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  4091. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  4092. if (!appDelegate.user) {
  4093. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  4094. }
  4095. [ret setValue:filter forKey:@"filter"];
  4096. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  4097. sqlite3_stmt * statement;
  4098. [ret setValue:@"2" forKey:@"result"];
  4099. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  4100. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  4101. // int count=0;
  4102. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4103. {
  4104. int i=0;
  4105. while (sqlite3_step(statement) == SQLITE_ROW)
  4106. {
  4107. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4108. char *name = (char*)sqlite3_column_text(statement, 0);
  4109. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  4110. char *description = (char*)sqlite3_column_text(statement, 1);
  4111. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  4112. int product_id = sqlite3_column_int(statement, 2);
  4113. int wid = sqlite3_column_int(statement, 3);
  4114. int closeout = sqlite3_column_int(statement, 4);
  4115. int cid = sqlite3_column_int(statement, 5);
  4116. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  4117. if(wid==0)
  4118. [item setValue:@"false" forKey:@"wish_exists"];
  4119. else
  4120. [item setValue:@"true" forKey:@"wish_exists"];
  4121. if(closeout==0)
  4122. [item setValue:@"false" forKey:@"is_closeout"];
  4123. else
  4124. [item setValue:@"true" forKey:@"is_closeout"];
  4125. if(cid==0)
  4126. [item setValue:@"false" forKey:@"cart_exists"];
  4127. else
  4128. [item setValue:@"true" forKey:@"cart_exists"];
  4129. [item addEntriesFromDictionary:imgjson];
  4130. // [item setValue:nsurl forKey:@"img"];
  4131. [item setValue:nsname forKey:@"fash_name"];
  4132. [item setValue:nsdescription forKey:@"description"];
  4133. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  4134. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4135. i++;
  4136. }
  4137. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  4138. [ret setObject:items forKey:@"items"];
  4139. sqlite3_finalize(statement);
  4140. }
  4141. [iSalesDB close_db:db];
  4142. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4143. return ret;
  4144. }
  4145. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  4146. {
  4147. return [self itemsearch:params limited:YES];
  4148. }
  4149. #pragma mark order detail
  4150. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  4151. if (str1.length == 0) {
  4152. str1 = @"&nbsp";
  4153. }
  4154. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  4155. return str;
  4156. }
  4157. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  4158. {
  4159. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  4160. [fromformatter setDateFormat:from];
  4161. NSDate *date = [fromformatter dateFromString:datetime];
  4162. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  4163. [toformatter setDateFormat:to];
  4164. NSString * ret = [toformatter stringFromDate:date];
  4165. return ret;
  4166. }
  4167. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  4168. // 把毫秒去掉
  4169. if ([dateTime containsString:@"."]) {
  4170. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  4171. }
  4172. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  4173. formatter.dateFormat = formate;
  4174. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  4175. NSDate *date = [formatter dateFromString:dateTime];
  4176. formatter.dateFormat = newFormate;
  4177. return [formatter stringFromDate:date];
  4178. }
  4179. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  4180. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  4181. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  4182. }
  4183. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  4184. {
  4185. DebugLog(@"offline oderdetail params: %@",params);
  4186. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4187. int orderId = [params[@"orderId"] intValue];
  4188. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  4189. // decrypt card number and card security code
  4190. // NSString* sql=[NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,o.TotalCuft,o.TotalWeight,o.TotalCarton,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),o.credit_card_expiration,o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus from (select _id,so_id,poNumber,create_time,status,TotalCuft,TotalWeight,TotalCarton,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId ];
  4191. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  4192. sqlite3 *db = [iSalesDB get_db];
  4193. sqlite3_stmt * statement;
  4194. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  4195. NSString *nssoid = @"";
  4196. NSString* orderinfo = @"";
  4197. NSString *moreInfo = @"";
  4198. double handlingFee = 0;
  4199. double payments_and_credist = 0;
  4200. double totalPrice = 0;
  4201. double shippingFee = 0;
  4202. double lift_gate = 0;
  4203. NSString *customer_contact = @"";
  4204. NSString *customer_email = @"";
  4205. NSString *customer_fax = @"";
  4206. NSString *customer_phone = @"";
  4207. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  4208. {
  4209. if (sqlite3_step(statement) == SQLITE_ROW)
  4210. {
  4211. int order_id = sqlite3_column_int(statement, 0);
  4212. NSString *sign_url = [self textAtColumn:51 statement:statement];
  4213. ret[@"sign_url"] = sign_url;
  4214. customer_contact = [self textAtColumn:52 statement:statement];
  4215. customer_email = [self textAtColumn:53 statement:statement];
  4216. customer_phone = [self textAtColumn:54 statement:statement];
  4217. customer_fax = [self textAtColumn:55 statement:statement];
  4218. int offline_edit=sqlite3_column_int(statement, 56);
  4219. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  4220. char *soid = (char*)sqlite3_column_text(statement, 1);
  4221. if(soid==nil)
  4222. soid= "";
  4223. nssoid= [[NSString alloc]initWithUTF8String:soid];
  4224. // so#
  4225. ret[@"so#"] = nssoid;
  4226. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  4227. if(poNumber==nil)
  4228. poNumber= "";
  4229. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  4230. char *create_time = (char*)sqlite3_column_text(statement, 3);
  4231. if(create_time==nil)
  4232. create_time= "";
  4233. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  4234. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  4235. int status = sqlite3_column_int(statement, 4);
  4236. int erpStatus = sqlite3_column_int(statement, 49);
  4237. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  4238. // status
  4239. if (status > 1 && status != 3) {
  4240. status = erpStatus;
  4241. } else if (status == 3) {
  4242. status = 15;
  4243. }
  4244. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  4245. ret[@"order_status"] = nsstatus;
  4246. char *company_name = (char*)sqlite3_column_text(statement, 5);
  4247. if(company_name==nil)
  4248. company_name= "";
  4249. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  4250. // company name
  4251. ret[@"company_name"] = nscompany_name;
  4252. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  4253. if(customer_contact==nil)
  4254. customer_contact= "";
  4255. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  4256. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  4257. if(addr_1==nil)
  4258. addr_1="";
  4259. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4260. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  4261. if(addr_2==nil)
  4262. addr_2="";
  4263. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4264. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  4265. if(addr_3==nil)
  4266. addr_3="";
  4267. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4268. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  4269. if(addr_4==nil)
  4270. addr_4="";
  4271. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4272. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4273. [arr_addr addObject:nsaddr_1];
  4274. [arr_addr addObject:nsaddr_2];
  4275. [arr_addr addObject:nsaddr_3];
  4276. [arr_addr addObject:nsaddr_4];
  4277. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  4278. char *logist = (char*)sqlite3_column_text(statement, 11);
  4279. if(logist==nil)
  4280. logist= "";
  4281. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  4282. if (status == -11 || status == 10 || status == 11) {
  4283. nslogist = [self textAtColumn:59 statement:statement];
  4284. };
  4285. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  4286. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  4287. shipping = @"Shipping To Be Quoted";
  4288. } else {
  4289. shippingFee = sqlite3_column_double(statement, 12);
  4290. }
  4291. // Shipping
  4292. ret[@"Shipping"] = shipping;
  4293. int have_lift_gate = sqlite3_column_int(statement, 17);
  4294. lift_gate = sqlite3_column_double(statement, 13);
  4295. // Liftgate Fee(No loading dock)
  4296. if (!have_lift_gate) {
  4297. lift_gate = 0;
  4298. }
  4299. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  4300. if (sqlite3_column_int(statement, 57)) {
  4301. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  4302. }
  4303. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  4304. if(general_notes==nil)
  4305. general_notes= "";
  4306. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  4307. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  4308. if(internal_notes==nil)
  4309. internal_notes= "";
  4310. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  4311. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  4312. if(payment_type==nil)
  4313. payment_type= "";
  4314. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  4315. // order info
  4316. orderinfo = [self textFileName:@"order_info.html"];
  4317. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  4318. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  4319. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  4320. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  4321. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  4322. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  4323. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  4324. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  4325. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  4326. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  4327. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  4328. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  4329. NSString *payment = nil;
  4330. if([nspayment_type isEqualToString:@"Credit Card"])
  4331. {
  4332. payment = [self textFileName:@"creditcardpayment.html"];
  4333. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  4334. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  4335. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  4336. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  4337. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  4338. NSString *card_type = [self textAtColumn:42 statement:statement];
  4339. if (card_type.length > 0) { // 显示星号
  4340. card_type = @"****";
  4341. }
  4342. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  4343. if (card_number.length > 0 && card_number.length > 4) {
  4344. for (int i = 0; i < card_number.length - 4; i++) {
  4345. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  4346. }
  4347. } else {
  4348. card_number = @"";
  4349. }
  4350. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  4351. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  4352. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  4353. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  4354. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  4355. card_expiration = @"****";
  4356. }
  4357. NSString *card_city = [self textAtColumn:46 statement:statement];
  4358. NSString *card_state = [self textAtColumn:47 statement:statement];
  4359. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  4360. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  4361. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  4362. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  4363. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  4364. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  4365. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  4366. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  4367. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  4368. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  4369. }
  4370. else
  4371. {
  4372. payment=[self textFileName:@"normalpayment.html"];
  4373. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  4374. }
  4375. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  4376. ret[@"result"]= [NSNumber numberWithInt:2];
  4377. // more info
  4378. moreInfo = [self textFileName:@"more_info.html"];
  4379. /*****ship to******/
  4380. // ShipToCompany_or_&nbsp
  4381. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  4382. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  4383. NSString *shipToName = [self textAtColumn:19 statement:statement];
  4384. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  4385. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  4386. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  4387. /*****ship from******/
  4388. // ShipFromCompany_or_&nbsp
  4389. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  4390. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  4391. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  4392. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  4393. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  4394. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  4395. /*****freight to******/
  4396. // FreightBillToCompany_or_&nbsp
  4397. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  4398. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  4399. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  4400. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  4401. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  4402. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  4403. /*****merchandise to******/
  4404. // MerchandiseBillToCompany_or_&nbsp
  4405. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  4406. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  4407. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  4408. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  4409. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  4410. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  4411. /*****return to******/
  4412. // ReturnToCompany_or_&nbsp
  4413. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  4414. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  4415. NSString *returnToName = [self textAtColumn:31 statement:statement];
  4416. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  4417. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  4418. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  4419. //
  4420. // DebugLog(@"more info : %@",moreInfo);
  4421. // handling fee
  4422. handlingFee = sqlite3_column_double(statement, 33);
  4423. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  4424. if (sqlite3_column_int(statement, 58)) {
  4425. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  4426. }
  4427. //
  4428. // customer info
  4429. customerID = [self textAtColumn:36 statement:statement];
  4430. // mode
  4431. ret[@"mode"] = appDelegate.mode;
  4432. // model_count
  4433. ret[@"model_count"] = @(0);
  4434. }
  4435. sqlite3_finalize(statement);
  4436. }
  4437. [iSalesDB close_db:db];
  4438. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  4439. // "customer_email" = "Shui Hu";
  4440. // "customer_fax" = "";
  4441. // "customer_first_name" = F;
  4442. // "customer_last_name" = L;
  4443. // "customer_name" = ",da He Xiang Dong Liu A";
  4444. // "customer_phone" = "Hey Xuan Feng";
  4445. contactInfo[@"customer_phone"] = customer_phone;
  4446. contactInfo[@"customer_fax"] = customer_fax;
  4447. contactInfo[@"customer_email"] = customer_email;
  4448. NSString *first_name = @"";
  4449. NSString *last_name = @"";
  4450. if ([customer_contact isEqualToString:@""]) {
  4451. } else if ([customer_contact containsString:@" "]) {
  4452. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  4453. first_name = [customer_contact substringToIndex:first_space_index];
  4454. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  4455. }
  4456. contactInfo[@"customer_first_name"] = first_name;
  4457. contactInfo[@"customer_last_name"] = last_name;
  4458. ret[@"customerInfo"] = contactInfo;
  4459. // models
  4460. if (nssoid) {
  4461. __block double TotalCuft = 0;
  4462. __block double TotalWeight = 0;
  4463. __block int TotalCarton = 0;
  4464. __block double allItemPrice = 0;
  4465. NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",nssoid];
  4466. sqlite3 *db1 = [iSalesDB get_db];
  4467. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4468. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4469. int product_id = sqlite3_column_int(stmt, 0);
  4470. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  4471. int item_id = sqlite3_column_int(stmt, 7);
  4472. NSString* Price=nil;
  4473. if(str_price==nil)
  4474. {
  4475. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  4476. if(price==nil)
  4477. Price=@"No Price.";
  4478. else
  4479. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4480. }
  4481. else
  4482. {
  4483. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4484. }
  4485. double discount = sqlite3_column_double(stmt, 2);
  4486. int item_count = sqlite3_column_int(stmt, 3);
  4487. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  4488. NSString *nsline_note=nil;
  4489. if(line_note!=nil)
  4490. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  4491. // char *name = (char*)sqlite3_column_text(stmt, 5);
  4492. // NSString *nsname = nil;
  4493. // if(name!=nil)
  4494. // nsname= [[NSString alloc]initWithUTF8String:name];
  4495. NSString *nsname = [self textAtColumn:5 statement:stmt];
  4496. // char *description = (char*)sqlite3_column_text(stmt, 6);
  4497. // NSString *nsdescription=nil;
  4498. // if(description!=nil)
  4499. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  4500. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  4501. // int stockUom = sqlite3_column_int(stmt, 8);
  4502. // int _id = sqlite3_column_int(stmt, 9);
  4503. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  4504. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  4505. double weight=[bsubtotaljson[@"weight"] doubleValue];
  4506. int carton=[bsubtotaljson[@"carton"] intValue];
  4507. TotalCuft += cuft;
  4508. TotalWeight += weight;
  4509. TotalCarton += carton;
  4510. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4511. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  4512. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  4513. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  4514. itemjson[@"note"]=nsline_note;
  4515. itemjson[@"origin_price"] = Price;
  4516. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4517. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  4518. itemjson[@"order_item_status"] = @""; // 暂时不处理
  4519. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4520. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4521. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  4522. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4523. if(itemjson[@"combine"] != nil)
  4524. {
  4525. // int citem=0;
  4526. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4527. for(int bc=0;bc<bcount;bc++)
  4528. {
  4529. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4530. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4531. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4532. subTotal += uprice * modulus * item_count;
  4533. }
  4534. }
  4535. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4536. // NSLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  4537. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  4538. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  4539. allItemPrice += subTotal;
  4540. }];
  4541. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  4542. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  4543. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  4544. // payments/Credits
  4545. // payments_and_credist = sqlite3_column_double(statement, 34);
  4546. payments_and_credist = allItemPrice;
  4547. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4548. // // total
  4549. // totalPrice = sqlite3_column_double(statement, 35);
  4550. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4551. } else {
  4552. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  4553. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  4554. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  4555. // payments/Credits
  4556. payments_and_credist = 0;
  4557. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  4558. }
  4559. // total
  4560. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  4561. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  4562. ret[@"order_info"]= orderinfo;
  4563. ret[@"more_order_info"] = moreInfo;
  4564. return [RAUtils dict2data:ret];
  4565. }
  4566. #pragma mark order list
  4567. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  4568. NSString *nsstatus = @"";
  4569. switch (status) {
  4570. case 0:
  4571. {
  4572. nsstatus=@"Temp Order";
  4573. break;
  4574. }
  4575. case 1:
  4576. {
  4577. nsstatus=@"Saved Order";
  4578. break;
  4579. }
  4580. case 2: {
  4581. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  4582. break;
  4583. }
  4584. case 3:
  4585. case 15:
  4586. {
  4587. nsstatus=@"Cancelled";
  4588. break;
  4589. }
  4590. case 10:
  4591. {
  4592. nsstatus=@"Quote Submitted";
  4593. break;
  4594. }
  4595. case 11:
  4596. {
  4597. nsstatus=@"Sales Order Submitted";
  4598. break;
  4599. }
  4600. case 12:
  4601. {
  4602. nsstatus=@"Processing";
  4603. break;
  4604. }
  4605. case 13:
  4606. {
  4607. nsstatus=@"Shipped";
  4608. break;
  4609. }
  4610. case 14:
  4611. {
  4612. nsstatus=@"Closed";
  4613. break;
  4614. }
  4615. case -11:
  4616. {
  4617. nsstatus = @"Ready For Submit";
  4618. break;
  4619. }
  4620. default:
  4621. break;
  4622. }
  4623. return nsstatus;
  4624. }
  4625. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  4626. double total = 0;
  4627. __block double payments_and_credist = 0;
  4628. __block double allItemPrice = 0;
  4629. // sqlite3 *db1 = [iSalesDB get_db];
  4630. if (so_id) {
  4631. // NSString *modelSql = [NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id",so_id];
  4632. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id from (select product_id,decrypt(str_price) as price,discount,item_count,item_id from offline_cart where so_no='%@');",so_id];
  4633. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4634. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4635. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  4636. int product_id = sqlite3_column_int(stmt, 0);
  4637. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  4638. int discount = sqlite3_column_int(stmt, 2);
  4639. int item_count = sqlite3_column_int(stmt, 3);
  4640. // int item_id = sqlite3_column_int(stmt, 7);
  4641. int item_id = sqlite3_column_int(stmt, 4);
  4642. NSString* Price=nil;
  4643. if(str_price==nil)
  4644. {
  4645. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  4646. if(price==nil)
  4647. Price=@"No Price.";
  4648. else
  4649. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  4650. }
  4651. else
  4652. {
  4653. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  4654. }
  4655. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  4656. itemjson[@"The unit price"]=Price;
  4657. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  4658. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  4659. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  4660. if(itemjson[@"combine"] != nil)
  4661. {
  4662. // int citem=0;
  4663. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  4664. for(int bc=0;bc<bcount;bc++)
  4665. {
  4666. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  4667. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  4668. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  4669. subTotal += uprice * modulus * item_count;
  4670. }
  4671. }
  4672. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  4673. allItemPrice += subTotal;
  4674. }];
  4675. payments_and_credist = allItemPrice;
  4676. } else {
  4677. // payments/Credits
  4678. payments_and_credist = 0;
  4679. }
  4680. // lift_gate handlingFee shippingFee
  4681. __block double lift_gate = 0;
  4682. __block double handlingFee = 0;
  4683. __block double shippingFee = 0;
  4684. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  4685. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4686. int have_lift_gate = sqlite3_column_int(stmt, 0);
  4687. if (have_lift_gate) {
  4688. lift_gate = sqlite3_column_double(stmt, 1);
  4689. }
  4690. handlingFee = sqlite3_column_double(stmt, 2);
  4691. shippingFee = sqlite3_column_double(stmt, 3);
  4692. }];
  4693. // total
  4694. total = payments_and_credist + lift_gate + handlingFee + shippingFee;
  4695. if (close) {
  4696. [iSalesDB close_db:db1];
  4697. }
  4698. return total;
  4699. }
  4700. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  4701. {
  4702. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4703. int limit = [[params valueForKey:@"limit"] intValue];
  4704. int offset = [[params valueForKey:@"offset"] intValue];
  4705. NSString* keyword = [params valueForKey:@"keyWord"];
  4706. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  4707. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  4708. NSString* where=@"1 = 1";
  4709. if(keyword.length>0)
  4710. where=[where stringByAppendingString:[NSString stringWithFormat:@" and (lower(o.so_id) like '%%%@%%' or lower(o.sales_rep) like '%%%@%%' or lower(decrypt(c.company_name)) like '%%%@%%' or lower(o.create_by) like '%%%@%%')",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString]];
  4711. if (orderStatus.length > 0) {
  4712. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  4713. if (order_status_array.count == 1) {
  4714. int status_value = [[order_status_array firstObject] integerValue];
  4715. if (status_value <= 1 || status_value == 3) {
  4716. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  4717. } else {
  4718. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  4719. }
  4720. } else if (order_status_array.count > 1) {
  4721. for (int i = 0; i < order_status_array.count;i++) {
  4722. NSString *status = order_status_array[i];
  4723. NSString *condition = @" or";
  4724. if (i == 0) {
  4725. condition = @" and (";
  4726. }
  4727. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  4728. int status_value = [status integerValue];
  4729. if (status_value <= 1 || status_value == 3) {
  4730. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  4731. } else {
  4732. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  4733. }
  4734. }
  4735. where = [where stringByAppendingString:@" )"];
  4736. }
  4737. }
  4738. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,decrypt(c.company_name),o.create_time,o.total_price,o.customer_contact,o.erpOrderStatus,o.order_id is null from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.modify_time desc,o.create_time desc limit %d offset %d" ,where, limit, offset];
  4739. // DebugLog(@"order list sql: %@",sqlQuery);
  4740. sqlite3 *db = [iSalesDB get_db];
  4741. sqlite3_stmt * statement;
  4742. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  4743. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4744. {
  4745. int count=0;
  4746. while (sqlite3_step(statement) == SQLITE_ROW)
  4747. {
  4748. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  4749. int order_id = sqlite3_column_double(statement, 0);
  4750. char *soid = (char*)sqlite3_column_text(statement, 1);
  4751. if(soid==nil)
  4752. soid= "";
  4753. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  4754. int status = sqlite3_column_double(statement, 2);
  4755. int erpStatus = sqlite3_column_double(statement, 9);
  4756. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  4757. if(sales_rep==nil)
  4758. sales_rep= "";
  4759. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  4760. char *create_by = (char*)sqlite3_column_text(statement, 4);
  4761. if(create_by==nil)
  4762. create_by= "";
  4763. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  4764. char *company_name = (char*)sqlite3_column_text(statement, 5);
  4765. if(company_name==nil)
  4766. company_name= "";
  4767. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  4768. char *create_time = (char*)sqlite3_column_text(statement, 6);
  4769. if(create_time==nil)
  4770. create_time= "";
  4771. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  4772. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  4773. // double total_price = sqlite3_column_double(statement, 7);
  4774. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  4775. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  4776. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  4777. int offline_edit = sqlite3_column_int(statement, 10);
  4778. // ": "JH",
  4779. // "": "$8307.00",
  4780. // "": "MOB1608050001",
  4781. // "": "ArpithaT",
  4782. // "": "1st Stage Property Transformations",
  4783. // "": "JANICE SUTTON",
  4784. // "": 2255,
  4785. // "": "08/02/2016 09:49:18",
  4786. // "": 1,
  4787. // "": "Saved Order"
  4788. // "": "1470384050483",
  4789. // "model_count": "6 / 28"
  4790. item[@"sales_rep"]= nssales_rep;
  4791. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  4792. item[@"so#"]= nssoid;
  4793. item[@"create_by"]= nscreate_by;
  4794. item[@"customer_name"]= nscompany_name;
  4795. item[@"customer_contact"] = customer_contact;
  4796. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  4797. item[@"purchase_time"]= nscreate_time;
  4798. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",status];
  4799. item[@"order_status"]= nsstatus;
  4800. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  4801. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  4802. // item[@"model_count"]
  4803. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  4804. count++;
  4805. }
  4806. ret[@"count"]= [NSNumber numberWithInt:count];
  4807. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  4808. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  4809. ret[@"result"]= [NSNumber numberWithInt:2];
  4810. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  4811. ret[@"time_zone"] = @"PST";
  4812. sqlite3_finalize(statement);
  4813. }
  4814. int total_count = [iSalesDB get_recordcount:db table:@"offline_order o left join offline_contact c on o.customer_cid=c.contact_id" where:where];
  4815. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  4816. [iSalesDB execSql:@"END TRANSACTION" db:db];
  4817. [iSalesDB close_db:db];
  4818. return [RAUtils dict2data:ret];
  4819. }
  4820. #pragma mark update gnotes
  4821. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  4822. {
  4823. DebugLog(@"params: %@",params);
  4824. // comments = Meyoyoyoyoyoyoy;
  4825. // orderCode = MOB1608110001;
  4826. // password = 123456;
  4827. // user = EvanK;
  4828. NSString *sql = [NSString stringWithFormat:@"update offline_order set general_notes = '%@' where so_id = '%@';",[self valueInParams:params key:@"comments" translateSingleQuote:YES],[self valueInParams:params key:@"orderCode" translateSingleQuote:YES]];
  4829. int ret = [iSalesDB execSql:sql];
  4830. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4831. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4832. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4833. // [dic setValue:@"160409" forKey:@"min_ver"];
  4834. return [RAUtils dict2data:dic];
  4835. }
  4836. #pragma mark move to wishlist
  4837. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  4838. {
  4839. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  4840. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4841. _id = [NSString stringWithFormat:@"(%@)",_id];
  4842. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  4843. sqlite3 *db = [iSalesDB get_db];
  4844. __block NSString *product_id = @"";
  4845. __block NSString *item_count_str = @"";
  4846. // __block NSString *item_id = nil;
  4847. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4848. // product_id = [self textAtColumn:0 statement:stmt];
  4849. int item_count = sqlite3_column_int(stmt, 1);
  4850. // item_id = [self textAtColumn:2 statement:stmt];
  4851. NSString *p_id = [self textAtColumn:0 statement:stmt];
  4852. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  4853. if (p_id.length) {
  4854. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  4855. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  4856. }
  4857. }];
  4858. [iSalesDB close_db:db];
  4859. // 去除第一个,
  4860. product_id = [product_id substringFromIndex:1];
  4861. item_count_str = [item_count_str substringFromIndex:1];
  4862. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4863. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  4864. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  4865. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  4866. sqlite3 *db1 = [iSalesDB get_db];
  4867. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"1=1"];
  4868. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4869. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  4870. // 删除
  4871. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  4872. int ret = [iSalesDB execSql:deleteSql db:db1];
  4873. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4874. [iSalesDB close_db:db1];
  4875. return [RAUtils dict2data:dic];
  4876. }
  4877. #pragma mark cart delete
  4878. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  4879. {
  4880. // cartItemId = 548;
  4881. // orderCode = MOB1608110001;
  4882. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  4883. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4884. _id = [NSString stringWithFormat:@"(%@)",_id];
  4885. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  4886. int ret = [iSalesDB execSql:sql];
  4887. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4888. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4889. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4890. // [dic setValue:@"160409" forKey:@"min_ver"];
  4891. return [RAUtils dict2data:dic];
  4892. }
  4893. #pragma mark set price
  4894. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  4895. {
  4896. DebugLog(@"cart set price params: %@",params);
  4897. // "cartitem_id" = 1;
  4898. // discount = "0.000000";
  4899. // "item_note" = "";
  4900. // price = "269.000000";
  4901. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  4902. NSString *notes = [self valueInParams:params key:@"item_note"];
  4903. NSString *discount = [self valueInParams:params key:@"discount"];
  4904. NSString *price = [self valueInParams:params key:@"price"];
  4905. // bool badd_price_changed=false;
  4906. // sqlite3* db=[iSalesDB get_db];
  4907. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4908. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  4909. // NSRange range;
  4910. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  4911. //
  4912. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  4913. // badd_price_changed=true;
  4914. // [iSalesDB close_db:db];
  4915. //
  4916. // if(badd_price_changed)
  4917. // {
  4918. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  4919. // }
  4920. //
  4921. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  4922. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  4923. int ret = [iSalesDB execSql:sql];
  4924. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4925. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4926. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4927. // [dic setValue:@"160409" forKey:@"min_ver"];
  4928. return [RAUtils dict2data:dic];
  4929. }
  4930. #pragma mark set line notes
  4931. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  4932. {
  4933. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  4934. NSString *notes = [self valueInParams:params key:@"notes"];
  4935. notes = [self translateSingleQuote:notes];
  4936. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  4937. int ret = [iSalesDB execSql:sql];
  4938. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4939. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4940. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4941. // [dic setValue:@"160409" forKey:@"min_ver"];
  4942. return [RAUtils dict2data:dic];
  4943. }
  4944. #pragma mark set qty
  4945. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  4946. {
  4947. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  4948. int item_count = [params[@"inputInt"] integerValue];
  4949. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  4950. int ret = [iSalesDB execSql:sql];
  4951. __block int item_id = 0;
  4952. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4953. item_id = sqlite3_column_int(stmt, 0);
  4954. }];
  4955. sqlite3 *db = [iSalesDB get_db];
  4956. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  4957. [iSalesDB close_db:db];
  4958. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4959. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  4960. [dic setValue:@"Regular Mode" forKey:@"mode"];
  4961. // [dic setValue:@"160409" forKey:@"min_ver"];
  4962. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  4963. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  4964. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  4965. return [RAUtils dict2data:dic];
  4966. }
  4967. #pragma mark place order
  4968. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  4969. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  4970. if (aname) {
  4971. [dic setValue:aname forKey:@"aname"];
  4972. }
  4973. if (control) {
  4974. [dic setValue:control forKey:@"control"];
  4975. }
  4976. if (keyboard) {
  4977. [dic setValue:keyboard forKey:@"keyboard"];
  4978. }
  4979. if (name) {
  4980. [dic setValue:name forKey:@"name"];
  4981. }
  4982. if (value) {
  4983. [dic setValue:value forKey:@"value"];
  4984. }
  4985. return dic;
  4986. }
  4987. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  4988. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  4989. orderCode = [self translateSingleQuote:orderCode];
  4990. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  4991. NSString *customerSql = [NSString stringWithFormat:@"select c.contact_id,c.img_0,c.img_1,c.img_2,decrypt(c.company_name),decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,c.first_name,c.last_name,o.customer_email,o.customer_phone,o.customer_fax,c.zipcode,c.city,c.state,c.country,o.customer_contact from offline_contact as c join offline_order as o on c.contact_id = o.customer_cid where o.so_id = '%@';",orderCode];
  4992. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4993. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  4994. NSString *img0 = [self textAtColumn:1 statement:stmt];
  4995. NSString *img1 = [self textAtColumn:2 statement:stmt];
  4996. NSString *img2 = [self textAtColumn:3 statement:stmt];
  4997. NSString *company_name = [self textAtColumn:4 statement:stmt];
  4998. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  4999. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  5000. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  5001. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  5002. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  5003. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  5004. NSString *email = [self textAtColumn:11 statement:stmt];
  5005. NSString *phone = [self textAtColumn:12 statement:stmt];
  5006. NSString *fax = [self textAtColumn:13 statement:stmt];
  5007. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  5008. NSString *city = [self textAtColumn:15 statement:stmt];
  5009. NSString *state = [self textAtColumn:16 statement:stmt];
  5010. NSString *country = [self textAtColumn:17 statement:stmt];
  5011. NSString *name = [self textAtColumn:18 statement:stmt];
  5012. // contact id
  5013. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  5014. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  5015. [contact_id_dic setValue:@"text" forKey:@"control"];
  5016. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  5017. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  5018. [contact_id_dic setValue:@"true" forKey:@"required"];
  5019. [contact_id_dic setValue:contact_id forKey:@"value"];
  5020. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  5021. // business card
  5022. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  5023. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  5024. [business_card_dic setValue:@"img" forKey:@"control"];
  5025. [business_card_dic setValue:@"1" forKey:@"disable"];
  5026. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  5027. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  5028. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  5029. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  5030. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  5031. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  5032. [business_card_dic setValue:@"business_card" forKey:@"name"];
  5033. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  5034. // fax
  5035. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5036. [customer_dic setValue:fax_dic forKey:@"item_10"];
  5037. // zipcode
  5038. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  5039. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  5040. // city
  5041. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  5042. [customer_dic setValue:city_dic forKey:@"item_12"];
  5043. // state
  5044. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  5045. [customer_dic setValue:state_dic forKey:@"item_13"];
  5046. // country
  5047. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  5048. [customer_dic setValue:country_dic forKey:@"item_14"];
  5049. // company name
  5050. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  5051. [customer_dic setValue:company_dic forKey:@"item_2"];
  5052. // addr_1
  5053. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  5054. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  5055. // addr_2
  5056. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  5057. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  5058. // addr_3
  5059. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  5060. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  5061. // addr_4
  5062. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  5063. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  5064. // Contact
  5065. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  5066. [customer_dic setValue:contact_dic forKey:@"item_7"];
  5067. // email
  5068. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5069. [customer_dic setValue:email_dic forKey:@"item_8"];
  5070. // phone
  5071. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5072. [customer_dic setValue:phone_dic forKey:@"item_9"];
  5073. // title
  5074. [customer_dic setValue:@"Customer" forKey:@"title"];
  5075. // count
  5076. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  5077. }];
  5078. // setting
  5079. NSDictionary *setting = params[@"setting"];
  5080. NSNumber *hide = setting[@"CustomerHide"];
  5081. [customer_dic setValue:hide forKey:@"hide"];
  5082. return customer_dic;
  5083. }
  5084. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5085. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5086. // setting
  5087. NSDictionary *setting = params[@"setting"];
  5088. NSNumber *hide = setting[@"ShipToHide"];
  5089. [dic setValue:hide forKey:@"hide"];
  5090. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5091. orderCode = [self translateSingleQuote:orderCode];
  5092. NSString *sql = [NSString stringWithFormat:@"select receive_cid,receive_name,receive_ext,receive_contact,receive_email,receive_fax,receive_phone from offline_order where so_id = '%@';",orderCode];
  5093. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5094. NSString *cid = [self textAtColumn:0 statement:stmt];
  5095. NSString *name = [self textAtColumn:1 statement:stmt];
  5096. NSString *ext = [self textAtColumn:2 statement:stmt];
  5097. NSString *contact = [self textAtColumn:3 statement:stmt];
  5098. NSString *email = [self textAtColumn:4 statement:stmt];
  5099. NSString *fax = [self textAtColumn:5 statement:stmt];
  5100. NSString *phone = [self textAtColumn:6 statement:stmt];
  5101. // count
  5102. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5103. // title
  5104. [dic setValue:@"Ship To" forKey:@"title"];
  5105. // choose
  5106. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5107. [choose_dic setValue:@"choose" forKey:@"aname"];
  5108. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5109. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5110. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5111. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5112. @"receive_contact" : @"customer_contact",
  5113. @"receive_email" : @"customer_email",
  5114. @"receive_ext" : @"customer_contact_ext",
  5115. @"receive_fax" : @"customer_fax",
  5116. @"receive_name" : @"customer_name",
  5117. @"receive_phone" : @"customer_phone"},
  5118. @"refresh" : [NSNumber numberWithInteger:1],
  5119. @"type" : @"pull"};
  5120. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  5121. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  5122. @"name" : @"Add new address",
  5123. @"refresh" : [NSNumber numberWithInteger:1],
  5124. @"value" : @"new_addr"
  5125. };
  5126. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  5127. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  5128. @"key_map" : @{@"receive_cid" : @"customer_cid",
  5129. @"receive_contact" : @"customer_contact",
  5130. @"receive_email" : @"customer_email",
  5131. @"receive_ext" : @"customer_contact_ext",
  5132. @"receive_fax" : @"customer_fax",
  5133. @"receive_name" : @"customer_name",
  5134. @"receive_phone" : @"customer_phone"},
  5135. @"name" : @"select_ship_to",
  5136. @"refresh" : [NSNumber numberWithInteger:1],
  5137. @"value" : @"Sales_Order_Ship_To"};
  5138. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  5139. [dic setValue:choose_dic forKey:@"item_0"];
  5140. // contact id
  5141. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5142. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5143. [contact_id_dic setValue:@"true" forKey:@"required"];
  5144. [dic setValue:contact_id_dic forKey:@"item_1"];
  5145. // company name
  5146. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5147. [dic setValue:company_name_dic forKey:@"item_2"];
  5148. // address
  5149. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5150. NSNumber *required = setting[@"ShippingToAddressRequire"];
  5151. if ([required integerValue]) {
  5152. [ext_dic setValue:@"true" forKey:@"required"];
  5153. }
  5154. [dic setValue:ext_dic forKey:@"item_3"];
  5155. // contact
  5156. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5157. [dic setValue:contact_dic forKey:@"item_4"];
  5158. // phone
  5159. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5160. [dic setValue:phone_dic forKey:@"item_5"];
  5161. // fax
  5162. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5163. [dic setValue:fax_dic forKey:@"item_6"];
  5164. // email
  5165. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5166. [dic setValue:email_dic forKey:@"item_7"];
  5167. }];
  5168. return dic;
  5169. }
  5170. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  5171. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5172. // setting
  5173. NSDictionary *setting = params[@"setting"];
  5174. NSNumber *hide = setting[@"ShipFromHide"];
  5175. [dic setValue:hide forKey:@"hide"];
  5176. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5177. orderCode = [self translateSingleQuote:orderCode];
  5178. NSString *sql = [NSString stringWithFormat:@"select sender_cid,sender_name,sender_ext,sender_contact,sender_email,sender_fax,sender_phone from offline_order where so_id = '%@';",orderCode];
  5179. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,phone from offline_contact where contact_id = 'NPD';";
  5180. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5181. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  5182. __block NSString *name = [self textAtColumn:1 statement:stmt];
  5183. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  5184. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  5185. __block NSString *email = [self textAtColumn:4 statement:stmt];
  5186. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  5187. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  5188. if (!cid.length) {
  5189. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  5190. cid = [self textAtColumn:0 statement:statment];
  5191. name = [self textAtColumn:1 statement:statment];
  5192. ext = [self textAtColumn:2 statement:statment];
  5193. contact = [self textAtColumn:3 statement:statment];
  5194. email = [self textAtColumn:4 statement:statment];
  5195. fax = [self textAtColumn:5 statement:statment];
  5196. phone = [self textAtColumn:6 statement:statment];
  5197. }];
  5198. }
  5199. // count
  5200. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5201. // title
  5202. [dic setValue:@"Ship From" forKey:@"title"];
  5203. // hide
  5204. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5205. // choose
  5206. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5207. [choose_dic setValue:@"choose" forKey:@"aname"];
  5208. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5209. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5210. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  5211. @"key_map" : @{@"sender_cid" : @"customer_cid",
  5212. @"sender_contact" : @"customer_contact",
  5213. @"sender_email" : @"customer_email",
  5214. @"sender_ext" : @"customer_contact_ext",
  5215. @"sender_fax" : @"customer_fax",
  5216. @"sender_name" : @"customer_name",
  5217. @"sender_phone" : @"customer_phone"},
  5218. @"name" : @"select_cid",
  5219. @"refresh" : [NSNumber numberWithInteger:0],
  5220. @"value" : @"Sales_Order_Ship_From"};
  5221. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  5222. [dic setValue:choose_dic forKey:@"item_0"];
  5223. // contact id
  5224. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5225. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5226. [contact_id_dic setValue:@"true" forKey:@"required"];
  5227. [dic setValue:contact_id_dic forKey:@"item_1"];
  5228. // company name
  5229. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5230. [dic setValue:company_name_dic forKey:@"item_2"];
  5231. // address
  5232. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5233. [dic setValue:ext_dic forKey:@"item_3"];
  5234. // contact
  5235. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5236. [dic setValue:contact_dic forKey:@"item_4"];
  5237. // phone
  5238. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5239. [dic setValue:phone_dic forKey:@"item_5"];
  5240. // fax
  5241. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5242. [dic setValue:fax_dic forKey:@"item_6"];
  5243. // email
  5244. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5245. [dic setValue:email_dic forKey:@"item_7"];
  5246. }];
  5247. return dic;
  5248. }
  5249. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  5250. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5251. // setting
  5252. NSDictionary *setting = params[@"setting"];
  5253. NSNumber *hide = setting[@"FreightBillToHide"];
  5254. [dic setValue:hide forKey:@"hide"];
  5255. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5256. orderCode = [self translateSingleQuote:orderCode];
  5257. NSString *sql = [NSString stringWithFormat:@"select shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_email,shipping_billto_fax,shipping_billto_phone from offline_order where so_id = '%@';",orderCode];
  5258. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5259. NSString *cid = [self textAtColumn:0 statement:stmt];
  5260. NSString *name = [self textAtColumn:1 statement:stmt];
  5261. NSString *ext = [self textAtColumn:2 statement:stmt];
  5262. NSString *contact = [self textAtColumn:3 statement:stmt];
  5263. NSString *email = [self textAtColumn:4 statement:stmt];
  5264. NSString *fax = [self textAtColumn:5 statement:stmt];
  5265. NSString *phone = [self textAtColumn:6 statement:stmt];
  5266. // count
  5267. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5268. // title
  5269. [dic setValue:@"Freight Bill To" forKey:@"title"];
  5270. // hide
  5271. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5272. // choose
  5273. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5274. [choose_dic setValue:@"choose" forKey:@"aname"];
  5275. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5276. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  5277. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5278. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  5279. @"shipping_billto_contact" : @"receive_contact",
  5280. @"shipping_billto_email" : @"receive_email",
  5281. @"shipping_billto_ext" : @"receive_ext",
  5282. @"shipping_billto_fax" : @"receive_fax",
  5283. @"shipping_billto_name" : @"receive_name",
  5284. @"shipping_billto_phone" : @"receive_phone"},
  5285. @"type" : @"pull"};
  5286. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5287. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5288. @"type" : @"pull",
  5289. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  5290. @"shipping_billto_contact" : @"customer_contact",
  5291. @"shipping_billto_email" : @"customer_email",
  5292. @"shipping_billto_ext" : @"customer_contact_ext",
  5293. @"shipping_billto_fax" : @"customer_fax",
  5294. @"shipping_billto_name" : @"customer_name",
  5295. @"shipping_billto_phone" : @"customer_phone"}
  5296. };
  5297. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5298. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5299. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  5300. @"shipping_billto_contact" : @"sender_contact",
  5301. @"shipping_billto_email" : @"sender_email",
  5302. @"shipping_billto_ext" : @"sender_ext",
  5303. @"shipping_billto_fax" : @"sender_fax",
  5304. @"shipping_billto_name" : @"sender_name",
  5305. @"shipping_billto_phone" : @"sender_phone"},
  5306. @"type" : @"pull"
  5307. };
  5308. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  5309. NSDictionary *select_freight_bill_to_dic = @{
  5310. @"aname" : @"Select freight bill to",
  5311. @"name" : @"select_cid",
  5312. @"refresh" : [NSNumber numberWithInteger:0],
  5313. @"value" : @"Sales_Order_Freight_Bill_To",
  5314. @"key_map" : @{
  5315. @"shipping_billto_cid" : @"customer_cid",
  5316. @"shipping_billto_contact" : @"customer_contact",
  5317. @"shipping_billto_email" : @"customer_email",
  5318. @"shipping_billto_ext" : @"customer_contact_ext",
  5319. @"shipping_billto_fax" : @"customer_fax",
  5320. @"shipping_billto_name" : @"customer_name",
  5321. @"shipping_billto_phone" : @"customer_phone"
  5322. }
  5323. };
  5324. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  5325. [dic setValue:choose_dic forKey:@"item_0"];
  5326. // contact id
  5327. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5328. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5329. [contact_id_dic setValue:@"true" forKey:@"required"];
  5330. [dic setValue:contact_id_dic forKey:@"item_1"];
  5331. // company name
  5332. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5333. [dic setValue:company_name_dic forKey:@"item_2"];
  5334. // address
  5335. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5336. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  5337. if ([ext_required integerValue]) {
  5338. [ext_dic setValue:@"true" forKey:@"required"];
  5339. }
  5340. [dic setValue:ext_dic forKey:@"item_3"];
  5341. // contact
  5342. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5343. [dic setValue:contact_dic forKey:@"item_4"];
  5344. // phone
  5345. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5346. [dic setValue:phone_dic forKey:@"item_5"];
  5347. // fax
  5348. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5349. [dic setValue:fax_dic forKey:@"item_6"];
  5350. // email
  5351. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5352. [dic setValue:email_dic forKey:@"item_7"];
  5353. }];
  5354. return dic;
  5355. }
  5356. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5357. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5358. // setting
  5359. NSDictionary *setting = params[@"setting"];
  5360. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  5361. [dic setValue:hide forKey:@"hide"];
  5362. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5363. orderCode = [self translateSingleQuote:orderCode];
  5364. NSString *sql = [NSString stringWithFormat:@"select billing_cid,billing_name,billing_ext,billing_contact,billing_email,billing_fax,billing_phone from offline_order where so_id = '%@';",orderCode];
  5365. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5366. NSString *cid = [self textAtColumn:0 statement:stmt];
  5367. NSString *name = [self textAtColumn:1 statement:stmt];
  5368. NSString *ext = [self textAtColumn:2 statement:stmt];
  5369. NSString *contact = [self textAtColumn:3 statement:stmt];
  5370. NSString *email = [self textAtColumn:4 statement:stmt];
  5371. NSString *fax = [self textAtColumn:5 statement:stmt];
  5372. NSString *phone = [self textAtColumn:6 statement:stmt];
  5373. // count
  5374. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5375. // title
  5376. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  5377. // hide
  5378. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5379. // choose
  5380. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5381. [choose_dic setValue:@"choose" forKey:@"aname"];
  5382. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5383. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  5384. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  5385. @"key_map" : @{@"billing_cid" : @"receive_cid",
  5386. @"billing_contact" : @"receive_contact",
  5387. @"billing_email" : @"receive_email",
  5388. @"billing_ext" : @"receive_ext",
  5389. @"billing_fax" : @"receive_fax",
  5390. @"billing_name" : @"receive_name",
  5391. @"billing_phone" : @"receive_phone"},
  5392. @"type" : @"pull"};
  5393. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  5394. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  5395. @"type" : @"pull",
  5396. @"key_map" : @{@"billing_cid" : @"customer_cid",
  5397. @"billing_contact" : @"customer_contact",
  5398. @"billing_email" : @"customer_email",
  5399. @"billing_ext" : @"customer_contact_ext",
  5400. @"billing_fax" : @"customer_fax",
  5401. @"billing_name" : @"customer_name",
  5402. @"billing_phone" : @"customer_phone"}
  5403. };
  5404. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  5405. NSDictionary *select_bill_to_dic = @{
  5406. @"aname" : @"Select bill to",
  5407. @"name" : @"select_cid",
  5408. @"refresh" : [NSNumber numberWithInteger:0],
  5409. @"value" : @"Sales_Order_Merchandise_Bill_To",
  5410. @"key_map" : @{
  5411. @"billing_cid" : @"customer_cid",
  5412. @"billing_contact" : @"customer_contact",
  5413. @"billing_email" : @"customer_email",
  5414. @"billing_ext" : @"customer_contact_ext",
  5415. @"billing_fax" : @"customer_fax",
  5416. @"billing_name" : @"customer_name",
  5417. @"billing_phone" : @"customer_phone"
  5418. }
  5419. };
  5420. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  5421. [dic setValue:choose_dic forKey:@"item_0"];
  5422. // contact id
  5423. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5424. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5425. [contact_id_dic setValue:@"true" forKey:@"required"];
  5426. [dic setValue:contact_id_dic forKey:@"item_1"];
  5427. // company name
  5428. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5429. [dic setValue:company_name_dic forKey:@"item_2"];
  5430. // address
  5431. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  5432. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  5433. if ([ext_required integerValue]) {
  5434. [ext_dic setValue:@"true" forKey:@"required"];
  5435. }
  5436. [dic setValue:ext_dic forKey:@"item_3"];
  5437. // contact
  5438. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5439. [dic setValue:contact_dic forKey:@"item_4"];
  5440. // phone
  5441. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5442. [dic setValue:phone_dic forKey:@"item_5"];
  5443. // fax
  5444. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5445. [dic setValue:fax_dic forKey:@"item_6"];
  5446. // email
  5447. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5448. [dic setValue:email_dic forKey:@"item_7"];
  5449. }];
  5450. return dic;
  5451. }
  5452. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  5453. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5454. // setting
  5455. NSDictionary *setting = params[@"setting"];
  5456. NSNumber *hide = setting[@"ReturnToHide"];
  5457. [dic setValue:hide forKey:@"hide"];
  5458. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5459. orderCode = [self translateSingleQuote:orderCode];
  5460. NSString *sql = [NSString stringWithFormat:@"select returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_email,returnto_fax,returnto_phone from offline_order where so_id = '%@';",orderCode];
  5461. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5462. NSString *cid = [self textAtColumn:0 statement:stmt];
  5463. NSString *name = [self textAtColumn:1 statement:stmt];
  5464. NSString *ext = [self textAtColumn:2 statement:stmt];
  5465. NSString *contact = [self textAtColumn:3 statement:stmt];
  5466. NSString *email = [self textAtColumn:4 statement:stmt];
  5467. NSString *fax = [self textAtColumn:5 statement:stmt];
  5468. NSString *phone = [self textAtColumn:6 statement:stmt];
  5469. // count
  5470. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  5471. // title
  5472. [dic setValue:@"Return To" forKey:@"title"];
  5473. // hide
  5474. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  5475. // choose
  5476. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  5477. [choose_dic setValue:@"choose" forKey:@"aname"];
  5478. [choose_dic setValue:@"multi_action" forKey:@"control"];
  5479. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  5480. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  5481. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  5482. @"returnto_contact" : @"sender_contact",
  5483. @"returnto_email" : @"sender_email",
  5484. @"returnto_ext" : @"sender_ext",
  5485. @"returnto_fax" : @"sender_fax",
  5486. @"returnto_name" : @"sender_name",
  5487. @"returnto_phone" : @"sender_phone"},
  5488. @"type" : @"pull"};
  5489. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  5490. NSDictionary *select_return_to_dic = @{
  5491. @"aname" : @"Select return to",
  5492. @"name" : @"select_cid",
  5493. @"refresh" : [NSNumber numberWithInteger:0],
  5494. @"value" : @"Contact_Return_To",
  5495. @"key_map" : @{
  5496. @"returnto_cid" : @"customer_cid",
  5497. @"returnto_contact" : @"customer_contact",
  5498. @"returnto_email" : @"customer_email",
  5499. @"returnto_ext" : @"customer_contact_ext",
  5500. @"returnto_fax" : @"customer_fax",
  5501. @"returnto_name" : @"customer_name",
  5502. @"returnto_phone" : @"customer_phone"
  5503. }
  5504. };
  5505. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  5506. [dic setValue:choose_dic forKey:@"item_0"];
  5507. // contact id
  5508. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  5509. [contact_id_dic setValue:@"1" forKey:@"disable"];
  5510. [contact_id_dic setValue:@"true" forKey:@"required"];
  5511. [dic setValue:contact_id_dic forKey:@"item_1"];
  5512. // company name
  5513. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  5514. [dic setValue:company_name_dic forKey:@"item_2"];
  5515. // address
  5516. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  5517. [dic setValue:ext_dic forKey:@"item_3"];
  5518. // contact
  5519. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  5520. [dic setValue:contact_dic forKey:@"item_4"];
  5521. // phone
  5522. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  5523. [dic setValue:phone_dic forKey:@"item_5"];
  5524. // fax
  5525. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  5526. [dic setValue:fax_dic forKey:@"item_6"];
  5527. // email
  5528. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  5529. [dic setValue:email_dic forKey:@"item_7"];
  5530. }];
  5531. return dic;
  5532. }
  5533. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  5534. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5535. orderCode = [self translateSingleQuote:orderCode];
  5536. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5537. __block double TotalCuft = 0;
  5538. __block double TotalWeight = 0;
  5539. __block int TotalCarton = 0;
  5540. __block double payments = 0;
  5541. // setting
  5542. NSDictionary *setting = params[@"setting"];
  5543. NSNumber *hide = setting[@"ModelInformationHide"];
  5544. [dic setValue:hide forKey:@"hide"];
  5545. NSString *sql = [NSString stringWithFormat:@"select c.item_id,c.item_count,m.stockUom,c.price,c.discount,m.name,m.description,c.line_note,m.availability from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id;",orderCode];
  5546. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5547. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5548. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  5549. // item id
  5550. int item_id = sqlite3_column_int(stmt, 0);
  5551. // count
  5552. int item_count = sqlite3_column_int(stmt, 1);
  5553. // stockUom
  5554. int stockUom = sqlite3_column_int(stmt, 2);
  5555. // unit price
  5556. NSString *str_price = [self textAtColumn:3 statement:stmt];
  5557. NSString* Price=nil;
  5558. if([str_price isEqualToString:@""])
  5559. {
  5560. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  5561. if(price==nil)
  5562. Price=@"No Price.";
  5563. else
  5564. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  5565. }
  5566. else
  5567. {
  5568. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  5569. }
  5570. // discount
  5571. double discount = sqlite3_column_double(stmt, 4);
  5572. // name
  5573. NSString *name = [self textAtColumn:5 statement:stmt];
  5574. // description
  5575. NSString *description = [self textAtColumn:6 statement:stmt];
  5576. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  5577. // line note
  5578. NSString *line_note = [self textAtColumn:7 statement:stmt];
  5579. int avaulability = sqlite3_column_int(stmt, 8);
  5580. // img
  5581. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  5582. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false];
  5583. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  5584. if(combine != nil)
  5585. {
  5586. // int citem=0;
  5587. int bcount=[[combine valueForKey:@"count"] intValue];
  5588. for(int bc=0;bc<bcount;bc++)
  5589. {
  5590. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  5591. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  5592. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  5593. subTotal += uprice * modulus * item_count;
  5594. }
  5595. }
  5596. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  5597. [model_dic setValue:@"model" forKey:@"control"];
  5598. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  5599. [model_dic setValue:description forKey:@"description"];
  5600. [model_dic setValue:line_note forKey:@"note"];
  5601. [model_dic setValue:img forKey:@"img_url"];
  5602. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  5603. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  5604. [model_dic setValue:Price forKey:@"unit_price"];
  5605. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  5606. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  5607. if (combine) {
  5608. [model_dic setValue:combine forKey:@"combine"];
  5609. }
  5610. // well,what under the row is the info for total
  5611. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  5612. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  5613. double weight=[bsubtotaljson[@"weight"] doubleValue];
  5614. int carton=[bsubtotaljson[@"carton"] intValue];
  5615. TotalCuft += cuft;
  5616. TotalWeight += weight;
  5617. TotalCarton += carton;
  5618. payments += subTotal;
  5619. //---------------------------
  5620. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  5621. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  5622. }];
  5623. [dic setValue:@"Model Information" forKey:@"title"];
  5624. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  5625. return dic;
  5626. }
  5627. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  5628. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5629. [dic setValue:@"Remarks Content" forKey:@"title"];
  5630. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  5631. // setting
  5632. NSDictionary *setting = params[@"setting"];
  5633. NSNumber *hide = setting[@"RemarksContentHide"];
  5634. [dic setValue:hide forKey:@"hide"];
  5635. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5636. orderCode = [self translateSingleQuote:orderCode];
  5637. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  5638. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5639. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  5640. int mustCall = sqlite3_column_int(stmt, 1);
  5641. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  5642. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  5643. NSDictionary *po_dic = @{
  5644. @"aname" : @"PO#",
  5645. @"control" : @"edit",
  5646. @"keyboard" : @"text",
  5647. @"name" : @"poNumber",
  5648. @"value" : poNumber
  5649. };
  5650. NSDictionary *must_call_dic = @{
  5651. @"aname" : @"MUST CALL BEFORE DELIVERY",
  5652. @"control" : @"switch",
  5653. @"name" : @"must_call",
  5654. @"value" : mustCall ? @"true" : @"false"
  5655. };
  5656. NSDictionary *general_notes_dic = @{
  5657. @"aname" : @"General notes",
  5658. @"control" : @"text_view",
  5659. @"keyboard" : @"text",
  5660. @"name" : @"comments",
  5661. @"value" : generalNotes
  5662. };
  5663. NSDictionary *internal_notes_dic = @{
  5664. @"aname" : @"Internal notes",
  5665. @"control" : @"text_view",
  5666. @"keyboard" : @"text",
  5667. @"name" : @"internal_notes",
  5668. @"value" : internalNotes
  5669. };
  5670. [dic setValue:po_dic forKey:@"item_0"];
  5671. [dic setValue:must_call_dic forKey:@"item_1"];
  5672. [dic setValue:general_notes_dic forKey:@"item_2"];
  5673. [dic setValue:internal_notes_dic forKey:@"item_3"];
  5674. }];
  5675. return dic;
  5676. }
  5677. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  5678. // params
  5679. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5680. orderCode = [self translateSingleQuote:orderCode];
  5681. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5682. // setting
  5683. NSDictionary *setting = params[@"setting"];
  5684. NSNumber *hide = setting[@"OrderTotalHide"];
  5685. [dic setValue:hide forKey:@"hide"];
  5686. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  5687. __block double lift_gate_value = 0;
  5688. __block double handling_fee = 0;
  5689. __block double shipping = 0;
  5690. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5691. int lift_gate = sqlite3_column_int(stmt, 0);
  5692. lift_gate_value = sqlite3_column_double(stmt, 1);
  5693. shipping = sqlite3_column_double(stmt, 2);
  5694. handling_fee = sqlite3_column_double(stmt, 3);
  5695. if (!lift_gate) {
  5696. lift_gate_value = 0;
  5697. }
  5698. }];
  5699. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  5700. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  5701. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  5702. double payments = [[total valueForKey:@"payments"] doubleValue];
  5703. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  5704. double totalPrice = payments;
  5705. [dic setValue:@"Order Total" forKey:@"title"];
  5706. NSDictionary *payments_dic = @{
  5707. @"align" : @"right",
  5708. @"aname" : @"Payments/Credits",
  5709. @"control" : @"text",
  5710. @"name" : @"paymentsAndCredits",
  5711. @"type" : @"price",
  5712. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  5713. };
  5714. [dic setValue:payments_dic forKey:@"item_0"];
  5715. NSDictionary *handling_fee_dic = @{
  5716. @"align" : @"right",
  5717. @"aname" : @"Handling Fee",
  5718. @"control" : @"text",
  5719. @"name" : @"handling_fee_value",
  5720. @"required" : @"true",
  5721. @"type" : @"price",
  5722. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  5723. };
  5724. NSDictionary *shipping_dic = @{
  5725. @"align" : @"right",
  5726. @"aname" : @"Shipping*",
  5727. @"control" : @"text",
  5728. @"name" : @"shipping",
  5729. @"required" : @"true",
  5730. @"type" : @"price",
  5731. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  5732. };
  5733. NSDictionary *lift_gate_dic = @{
  5734. @"align" : @"right",
  5735. @"aname" : @"Liftgate Fee(No Loading Dock)",
  5736. @"control" : @"text",
  5737. @"name" : @"lift_gate_value",
  5738. @"required" : @"true",
  5739. @"type" : @"price",
  5740. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  5741. };
  5742. int item_count = 1;
  5743. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  5744. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  5745. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5746. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5747. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  5748. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5749. } else {
  5750. }
  5751. [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5752. NSDictionary *total_price_dic = @{
  5753. @"align" : @"right",
  5754. @"aname" : @"Total",
  5755. @"control" : @"text",
  5756. @"name" : @"totalPrice",
  5757. @"type" : @"price",
  5758. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  5759. };
  5760. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5761. NSDictionary *total_cuft_dic = @{
  5762. @"align" : @"right",
  5763. @"aname" : @"Total Cuft",
  5764. @"control" : @"text",
  5765. @"name" : @"",
  5766. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  5767. };
  5768. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5769. NSDictionary *total_weight_dic = @{
  5770. @"align" : @"right",
  5771. @"aname" : @"Total Weight",
  5772. @"control" : @"text",
  5773. @"name" : @"",
  5774. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  5775. };
  5776. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5777. NSDictionary *total_carton_dic = @{
  5778. @"align" : @"right",
  5779. @"aname" : @"Total Carton",
  5780. @"control" : @"text",
  5781. @"name" : @"",
  5782. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  5783. };
  5784. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  5785. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  5786. return dic;
  5787. }
  5788. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  5789. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5790. orderCode = [self translateSingleQuote:orderCode];
  5791. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5792. // setting
  5793. NSDictionary *setting = params[@"setting"];
  5794. NSNumber *hide = setting[@"SignatureHide"];
  5795. [dic setValue:hide forKey:@"hide"];
  5796. [dic setValue:@"Signature" forKey:@"title"];
  5797. __block NSString *pic_path = @"";
  5798. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  5799. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5800. pic_path = [self textAtColumn:0 statement:stmt];
  5801. }];
  5802. NSDictionary *pic_dic = @{
  5803. @"aname" : @"Signature",
  5804. @"avalue" :pic_path,
  5805. @"control" : @"signature",
  5806. @"name" : @"sign_picpath",
  5807. @"value" : pic_path
  5808. };
  5809. [dic setValue:pic_dic forKey:@"item_0"];
  5810. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5811. return dic;
  5812. }
  5813. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  5814. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  5815. // setting
  5816. NSDictionary *setting = params[@"setting"];
  5817. NSNumber *hide = setting[@"ShippingMethodHide"];
  5818. [dic setValue:hide forKey:@"hide"];
  5819. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5820. orderCode = [self translateSingleQuote:orderCode];
  5821. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  5822. __block NSString *logist = @"";
  5823. __block NSString *lift_gate = @"";
  5824. __block int lift_gate_integer = 0;
  5825. __block NSString *logistic_note = @"";
  5826. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5827. logist = [self textAtColumn:0 statement:stmt];
  5828. lift_gate_integer = sqlite3_column_int(stmt, 1);
  5829. logistic_note = [self textAtColumn:2 statement:stmt];
  5830. }];
  5831. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5832. [dic setValue:@"Shipping Method" forKey:@"title"];
  5833. // val_0
  5834. int PERSONAL_PICK_UP_check = 0;
  5835. int USE_MY_CARRIER_check = 0;
  5836. NSString *logistic_note_text = @"";
  5837. int will_call_check = 0;
  5838. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  5839. will_call_check = 1;
  5840. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  5841. PERSONAL_PICK_UP_check = 1;
  5842. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  5843. USE_MY_CARRIER_check = 1;
  5844. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  5845. logistic_note = [logistic_note_array firstObject];
  5846. if (logistic_note_array.count > 1) {
  5847. logistic_note_text = [logistic_note_array lastObject];
  5848. }
  5849. }
  5850. }
  5851. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  5852. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  5853. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  5854. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  5855. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  5856. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  5857. NSDictionary *val0_subItem_item0 = @{
  5858. @"aname" : @"Option",
  5859. @"cadedate" : @{
  5860. @"count" : [NSNumber numberWithInteger:2],
  5861. @"val_0" : @{
  5862. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  5863. @"refresh" : [NSNumber numberWithInteger:0],
  5864. @"value" : @"PERSONAL PICK UP",
  5865. @"value_id" : @"PERSONAL PICK UP"
  5866. },
  5867. @"val_1" : @{
  5868. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  5869. @"refresh" : [NSNumber numberWithInteger:0],
  5870. @"sub_item" : @{
  5871. @"count" : [NSNumber numberWithInteger:1],
  5872. @"item_0" : @{
  5873. @"aname" : @"BOL",
  5874. @"control" : @"edit",
  5875. @"keyboard" : @"text",
  5876. @"name" : @"logist_note_text",
  5877. @"refresh" : [NSNumber numberWithInteger:0],
  5878. @"required" : @"false",
  5879. @"value" : logistic_note_text
  5880. }
  5881. },
  5882. @"value" : @"USE MY CARRIER",
  5883. @"value_id" : @"USE MY CARRIER"
  5884. }
  5885. },
  5886. @"control" : @"enum",
  5887. @"name" : @"logistic_note",
  5888. @"required" : @"true",
  5889. @"single_select" : @"true"
  5890. };
  5891. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  5892. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  5893. // val_1
  5894. lift_gate = [self valueInParams:params key:@"lift_gate"];
  5895. if([lift_gate isEqualToString:@""]) {
  5896. if (lift_gate_integer == 1) {
  5897. lift_gate = @"true";
  5898. } else {
  5899. lift_gate = @"false";
  5900. }
  5901. }
  5902. int common_carrier_check = 0;
  5903. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  5904. common_carrier_check = 1;
  5905. [params setValue:lift_gate forKey:@"lift_gate"];
  5906. }
  5907. NSDictionary *val_1 = @{
  5908. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  5909. @"sub_item" : @{
  5910. @"count" : [NSNumber numberWithInteger:1],
  5911. @"item_0" : @{
  5912. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  5913. @"control" : @"switch",
  5914. @"name" : @"lift_gate",
  5915. @"refresh" : [NSNumber numberWithInteger:1],
  5916. @"required" : @"true",
  5917. @"value" : lift_gate
  5918. }
  5919. },
  5920. @"value" : @"COMMON CARRIER",
  5921. @"value_id" : @"COMMON CARRIER"
  5922. };
  5923. // cadedate
  5924. NSDictionary *cadedate = @{
  5925. @"count" : [NSNumber numberWithInteger:2],
  5926. @"val_0" : val_0,
  5927. @"val_1" : val_1
  5928. };
  5929. // item_0
  5930. NSMutableDictionary *item_0 = @{
  5931. @"aname" : @"Shipping",
  5932. @"cadedate" : cadedate,
  5933. @"control" : @"enum",
  5934. @"name" : @"logist",
  5935. @"refresh" : [NSNumber numberWithInteger:1],
  5936. @"single_select" : @"true",
  5937. }.mutableCopy;
  5938. NSNumber *required = setting[@"ShippingMethodRequired"];
  5939. if ([required integerValue]) {
  5940. [item_0 setValue:@"true" forKey:@"required"];
  5941. }
  5942. [dic setValue:item_0 forKey:@"item_0"];
  5943. return dic;
  5944. }
  5945. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  5946. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  5947. orderCode = [self translateSingleQuote:orderCode];
  5948. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  5949. __block int submit_as_integer = 0;
  5950. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5951. submit_as_integer = sqlite3_column_int(stmt, 0);
  5952. }];
  5953. int check11 = 0;
  5954. int check10 = 0;
  5955. if (submit_as_integer == 11) {
  5956. check11 = 1;
  5957. }
  5958. if (submit_as_integer == 10) {
  5959. check10 = 1;
  5960. }
  5961. // section_0
  5962. NSMutableDictionary *dic = @{
  5963. @"count" : @(1),
  5964. @"item_0" : @{
  5965. @"aname" : @"Submit Order As",
  5966. @"cadedate" : @{
  5967. @"count" : @(2),
  5968. @"val_0" : @{
  5969. @"check" : [NSNumber numberWithInteger:check11],
  5970. @"value" : @"Sales Order",
  5971. @"value_id" : @(11)
  5972. },
  5973. @"val_1" : @{
  5974. @"check" : [NSNumber numberWithInteger:check10],
  5975. @"value" : @"Quote",
  5976. @"value_id" : @(10)
  5977. }
  5978. },
  5979. @"control" : @"enum",
  5980. @"name" : @"erpOrderStatus",
  5981. @"required" : @"true",
  5982. @"single_select" : @"true"
  5983. },
  5984. @"title" : @"Order Type"
  5985. }.mutableCopy;
  5986. // setting
  5987. NSDictionary *setting = params[@"setting"];
  5988. NSNumber *hide = setting[@"OrderTypeHide"];
  5989. [dic setValue:hide forKey:@"hide"];
  5990. return dic;
  5991. }
  5992. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  5993. countryCode = [self translateSingleQuote:countryCode];
  5994. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  5995. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5996. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  5997. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  5998. if (name == NULL) {
  5999. name = "";
  6000. }
  6001. if (code == NULL) {
  6002. code = "";
  6003. }
  6004. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6005. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  6006. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  6007. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6008. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  6009. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6010. }
  6011. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  6012. [container setValue:stateDic forKey:key];
  6013. }] mutableCopy];
  6014. // failure 可以不用了,一样的
  6015. if (ret.allKeys.count == 0) {
  6016. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  6017. [stateDic setValue:@"Other" forKey:@"value"];
  6018. [stateDic setValue:@"" forKey:@"value_id"];
  6019. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  6020. if (state_code && [@"" isEqualToString:state_code]) {
  6021. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  6022. }
  6023. NSString *key = [NSString stringWithFormat:@"val_0"];
  6024. [ret setValue:stateDic forKey:key];
  6025. }
  6026. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  6027. return ret;
  6028. }
  6029. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  6030. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6031. orderCode = [self translateSingleQuote:orderCode];
  6032. NSString *sql = [NSString stringWithFormat:@"select paymentType,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,decrypt(credit_card_number),decrypt(credit_card_security_code),decrypt(credit_card_expiration_month),decrypt(credit_card_expiration_year),credit_card_city,credit_card_state from offline_order where so_id = '%@';",orderCode];
  6033. __block NSString *payType = @"";
  6034. __block NSString *firstName = @"";
  6035. __block NSString *lastName = @"";
  6036. __block NSString *addr1 = @"";
  6037. __block NSString *addr2 = @"";
  6038. __block NSString *zipcode = @"";
  6039. __block NSString *cardType = @"";
  6040. __block NSString *cardNumber = @"";
  6041. __block NSString *securityCode = @"";
  6042. __block NSString *month = @"";
  6043. __block NSString *year = @"";
  6044. __block NSString *city = @"";
  6045. __block NSString *state = @"";
  6046. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6047. payType = [self textAtColumn:0 statement:stmt];
  6048. firstName = [self textAtColumn:1 statement:stmt];
  6049. lastName = [self textAtColumn:2 statement:stmt];
  6050. addr1 = [self textAtColumn:3 statement:stmt];
  6051. addr2 = [self textAtColumn:4 statement:stmt];
  6052. zipcode = [self textAtColumn:5 statement:stmt];
  6053. cardType = [self textAtColumn:6 statement:stmt];
  6054. cardNumber = [self textAtColumn:7 statement:stmt];
  6055. securityCode = [self textAtColumn:8 statement:stmt];
  6056. month = [self textAtColumn:9 statement:stmt];
  6057. year = [self textAtColumn:10 statement:stmt];
  6058. city = [self textAtColumn:11 statement:stmt];
  6059. state = [self textAtColumn:12 statement:stmt];
  6060. }];
  6061. NSString *required = @"true";
  6062. // setting
  6063. NSDictionary *setting = params[@"setting"];
  6064. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  6065. if ([requiredNumber integerValue]) {
  6066. required = @"true";
  6067. } else {
  6068. required = @"false";
  6069. }
  6070. // "section_2"
  6071. NSMutableDictionary *dic = @{
  6072. @"count" : @(1),
  6073. @"item_0" : @{
  6074. @"aname" : @"Payment",
  6075. @"required" : required,
  6076. @"cadedate" : @{
  6077. @"count" : @(6),
  6078. @"val_0" : @{
  6079. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  6080. @"value" : @"Check",
  6081. @"value_id" : @"Check"
  6082. },
  6083. @"val_1" : @{
  6084. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  6085. @"value" : @"Cash",
  6086. @"value_id" : @"Cash"
  6087. },
  6088. @"val_2" : @{
  6089. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  6090. @"value" : @"NET 30",
  6091. @"value_id" : @"NET 30"
  6092. },
  6093. @"val_3" : @{
  6094. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  6095. @"value" : @"Wire Transfer",
  6096. @"value_id" : @"Wire Transfer"
  6097. },
  6098. @"val_4" : @{
  6099. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  6100. @"sub_item" : @{
  6101. @"count" : @(3),
  6102. @"item_0" : @{
  6103. @"aname" : @"choose",
  6104. @"control" : @"multi_action",
  6105. @"count" : @(1),
  6106. @"item_0" : @{
  6107. @"aname" : @"Same as customer",
  6108. @"key_map" : @{
  6109. @"credit_card_address1" : @"customer_address1",
  6110. @"credit_card_address2" : @"customer_address2",
  6111. @"credit_card_city" : @"customer_city",
  6112. @"credit_card_first_name" : @"customer_first_name",
  6113. @"credit_card_last_name" : @"customer_last_name",
  6114. @"credit_card_state" : @"customer_state",
  6115. @"credit_card_zipcode" : @"customer_zipcode"
  6116. },
  6117. @"type" : @"pull"
  6118. }
  6119. },
  6120. @"item_1" : @{
  6121. @"aname" : @"",
  6122. @"color" : @"red",
  6123. @"control" : @"text",
  6124. @"name" : @"",
  6125. @"value" : @"USA Credit cards only"
  6126. },
  6127. @"item_2" : @{
  6128. @"aname" : @"Fill",
  6129. @"cadedate" : @{
  6130. @"count" : @(2),
  6131. @"val_0" : @{
  6132. @"check" : @(1),
  6133. @"sub_item" : @{
  6134. @"count" : @(11),
  6135. @"item_0" : @{
  6136. @"aname" : @"Type",
  6137. @"cadedate" : @{
  6138. @"count" : @(2),
  6139. @"val_0" : @{
  6140. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  6141. @"value" : @"VISA",
  6142. @"value_id" : @(0)
  6143. },
  6144. @"val_1" : @{
  6145. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  6146. @"value" : @"MASTER CARD",
  6147. @"value_id" : @(1)
  6148. }
  6149. },
  6150. @"control" : @"enum",
  6151. @"name" : @"credit_card_type",
  6152. @"required" : @"true",
  6153. @"single_select" : @"true"
  6154. },
  6155. @"item_1" : @{
  6156. @"aname" : @"Number",
  6157. @"control" : @"edit",
  6158. @"keyboard" : @"int",
  6159. @"length" : @"16",
  6160. @"name" : @"credit_card_number",
  6161. @"required" : @"true",
  6162. @"value" : cardNumber
  6163. },
  6164. @"item_10" : @{
  6165. @"aname" : @"State",
  6166. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  6167. @"control" : @"enum",
  6168. @"enum" : @"true",
  6169. @"name" : @"credit_card_state",
  6170. @"required" : @"true",
  6171. @"single_select" : @"true"
  6172. },
  6173. @"item_2" : @{
  6174. @"aname" : @"Expiration Date",
  6175. @"control" : @"monthpicker",
  6176. @"name" : @"credit_card_expiration",
  6177. @"required" : @"true",
  6178. @"type" : @"date",
  6179. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  6180. },
  6181. @"item_3" : @{
  6182. @"aname" : @"Security Code",
  6183. @"control" : @"edit",
  6184. @"keyboard" : @"int",
  6185. @"length" : @"3",
  6186. @"name" : @"credit_card_security_code",
  6187. @"required" : @"true",
  6188. @"value" : securityCode
  6189. },
  6190. @"item_4" : @{
  6191. @"aname" : @"First Name",
  6192. @"control" : @"edit",
  6193. @"keyboard" : @"text",
  6194. @"name" : @"credit_card_first_name",
  6195. @"required" : @"true",
  6196. @"value" : firstName
  6197. },
  6198. @"item_5" : @{
  6199. @"aname" : @"Last Name",
  6200. @"control" : @"edit",
  6201. @"keyboard" : @"text",
  6202. @"name" : @"credit_card_last_name",
  6203. @"required" : @"true",
  6204. @"value" : lastName
  6205. },
  6206. @"item_6" : @{
  6207. @"aname" : @"Address 1",
  6208. @"control" : @"edit",
  6209. @"keyboard" : @"text",
  6210. @"name" : @"credit_card_address1",
  6211. @"required" : @"true",
  6212. @"value" : addr1
  6213. },
  6214. @"item_7" : @{
  6215. @"aname" : @"Address 2",
  6216. @"control" : @"edit",
  6217. @"keyboard" : @"text",
  6218. @"name" : @"credit_card_address2",
  6219. @"value" : addr2
  6220. },
  6221. @"item_8" : @{
  6222. @"aname" : @"zip code",
  6223. @"control" : @"edit",
  6224. @"keyboard" : @"text",
  6225. @"name" : @"credit_card_zipcode",
  6226. @"required" : @"true",
  6227. @"value" : zipcode
  6228. },
  6229. @"item_9" : @{
  6230. @"aname" : @"City",
  6231. @"control" : @"edit",
  6232. @"keyboard" : @"text",
  6233. @"name" : @"credit_card_city",
  6234. @"required" : @"true",
  6235. @"value" : city
  6236. }
  6237. },
  6238. @"value" : @"Fill Now",
  6239. @"value_id" : @""
  6240. },
  6241. @"val_1" : @{
  6242. @"check" : @(0),
  6243. @"value" : @"Fill Later",
  6244. @"value_id" : @""
  6245. }
  6246. },
  6247. @"control" : @"enum",
  6248. @"name" : @"",
  6249. @"single_select" : @"true"
  6250. }
  6251. },
  6252. @"value" : @"Credit Card",
  6253. @"value_id" : @"Credit Card"
  6254. },
  6255. @"val_5" : @{
  6256. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  6257. @"value" : @"FOLLOW EXISTING",
  6258. @"value_id" : @"FOLLOW EXISTING"
  6259. }
  6260. },
  6261. @"control" : @"enum",
  6262. @"name" : @"paymentType",
  6263. @"single_select" : @"true"
  6264. },
  6265. @"title" : @"Payment Information"
  6266. }.mutableCopy;
  6267. NSNumber *hide = setting[@"PaymentInformationHide"];
  6268. [dic setValue:hide forKey:@"hide"];
  6269. return dic;
  6270. }
  6271. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  6272. {
  6273. sqlite3 *db = [iSalesDB get_db];
  6274. // UISetting
  6275. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  6276. NSString *cachefolder = [paths objectAtIndex:0];
  6277. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  6278. NSData* json =nil;
  6279. json=[NSData dataWithContentsOfFile:img_cache];
  6280. NSError *error=nil;
  6281. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  6282. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  6283. [params setObject:setting forKey:@"setting"];
  6284. int section_count = 0;
  6285. // params
  6286. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6287. orderCode = [self translateSingleQuote:orderCode];
  6288. // 0 Order Type 1 Shipping Method 2 Payment Information
  6289. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  6290. // 0 Order Type
  6291. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  6292. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  6293. [ret setValue:order_type_dic forKey:key0];
  6294. // 1 Shipping Method
  6295. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  6296. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  6297. [ret setValue:shipping_method_dic forKey:key1];
  6298. // 2 Payment Information
  6299. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  6300. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  6301. [ret setValue:payment_info_dic forKey:key2];
  6302. // 3 Customer
  6303. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  6304. NSDictionary *customer_dic = [self customerDic:params db:db];
  6305. [ret setValue:customer_dic forKey:key3];
  6306. // 4 Ship To
  6307. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  6308. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  6309. [ret setValue:ship_to_dic forKey:key4];
  6310. // 5 Ship From
  6311. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  6312. if (![shipFromDisable integerValue]) {
  6313. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  6314. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  6315. [ret setValue:ship_from_dic forKey:key5];
  6316. }
  6317. // 6 Freight Bill To
  6318. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  6319. if (![freightBillToDisable integerValue]) {
  6320. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  6321. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  6322. [ret setValue:freight_bill_to forKey:key6];
  6323. }
  6324. // 7 Merchandise Bill To
  6325. NSNumber *merchandiseBillToDisable = setting[@"MerchandisebillToDisabled"];
  6326. if (![merchandiseBillToDisable integerValue]) {
  6327. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  6328. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  6329. [ret setValue:merchandise_bill_to_dic forKey:key7];
  6330. }
  6331. // 8 Return To
  6332. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  6333. if (![returnToDisable integerValue]) {
  6334. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  6335. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  6336. [ret setValue:return_to_dic forKey:key8];
  6337. }
  6338. // 9 Model Information
  6339. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  6340. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  6341. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  6342. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  6343. [ret setValue:model_info_dic forKey:key9];
  6344. // 10 Remarks Content
  6345. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  6346. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  6347. [ret setValue:remarks_content_dic forKey:key10];
  6348. // 11 Order Total
  6349. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  6350. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  6351. [ret setValue:order_total_dic forKey:key11];
  6352. // 12 Signature
  6353. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  6354. NSDictionary *sign_dic = [self signatureDic:params db:db];
  6355. [ret setValue:sign_dic forKey:key12];
  6356. [ret setValue:@(section_count) forKey:@"section_count"];
  6357. [iSalesDB close_db:db];
  6358. return [RAUtils dict2data:ret];
  6359. // return nil;
  6360. }
  6361. #pragma mark addr editor
  6362. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  6363. NSMutableDictionary *new_item = [item mutableCopy];
  6364. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  6365. return new_item;
  6366. }
  6367. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  6368. {
  6369. // "is_subaction" = true;
  6370. // orderCode = MOB1608240002;
  6371. // password = 123456;
  6372. // "subaction_tag" = 1;
  6373. // user = EvanK;
  6374. // {
  6375. // "is_subaction" = true;
  6376. // orderCode = MOB1608240002;
  6377. // password = 123456;
  6378. // "refresh_trigger" = zipcode;
  6379. // "subaction_tag" = 1;
  6380. // user = EvanK;
  6381. // }
  6382. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  6383. [ret removeObjectForKey:@"up_params"];
  6384. [ret setObject:@"New Address" forKey:@"title"];
  6385. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  6386. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  6387. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  6388. NSMutableDictionary *country_dic = [section_0[@"item_5"] mutableCopy];
  6389. [country_dic removeObjectForKey:@"refresh"];
  6390. [country_dic removeObjectForKey:@"restore"];
  6391. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  6392. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  6393. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_6"] add:YES] forKey:@"item_2"]; // Address 1*
  6394. [new_section_0 setObject:section_0[@"item_7"] forKey:@"item_3"]; // Address 2
  6395. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_4"]; // Address 3
  6396. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_5"]; // Address 4
  6397. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_10"] add:YES];
  6398. [zip_code_dic removeObjectForKey:@"refresh"];
  6399. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  6400. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_11"] add:YES] forKey:@"item_7"]; // State/Province*
  6401. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_8"]; // City*
  6402. [new_section_0 setObject:section_0[@"item_13"] forKey:@"item_9"]; // Contact First Name
  6403. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_10"]; // Contact Last Name
  6404. [new_section_0 setObject:section_0[@"item_4"] forKey:@"item_11"]; // Phone*
  6405. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_12"]; // Fax
  6406. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_3"] add:NO] forKey:@"item_13"]; // Email
  6407. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  6408. // country
  6409. NSString *countryCode = @"US";
  6410. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  6411. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  6412. // state
  6413. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:nil] mutableCopy];
  6414. NSDictionary *tmpDic = @{
  6415. @"value" : @"Other",
  6416. @"value_id" : @"",
  6417. @"check" : [NSNumber numberWithInteger:0]
  6418. };
  6419. for (int i = 0; i < allState.allKeys.count; i++) {
  6420. NSString *key = [NSString stringWithFormat:@"val_%d",i];
  6421. NSDictionary *tmp = allState[key];
  6422. [allState setValue:tmpDic forKey:key];
  6423. tmpDic = tmp;
  6424. }
  6425. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  6426. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  6427. [ret setValue:new_section_0 forKey:@"section_0"];
  6428. return [RAUtils dict2data:ret];
  6429. }
  6430. #pragma mark save addr
  6431. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  6432. {
  6433. // NSString *companyName = [self valueInParams:params key:@"company"];
  6434. // NSString *addr1 = [self valueInParams:params key:@"address"];
  6435. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  6436. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  6437. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  6438. // NSString *countryId = [self valueInParams:params key:@"country"];
  6439. // NSString *stateCode = [self valueInParams:params key:@"state"];
  6440. // NSString *city = [self valueInParams:params key:@"city"];
  6441. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  6442. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  6443. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  6444. // NSString *phone = [self valueInParams:params key:@"phone"];
  6445. // NSString *fax = [self valueInParams:params key:@"fax"];
  6446. // NSString *email = [self valueInParams:params key:@"email"];
  6447. return [self offline_saveContact:params update:NO isCustomer:NO];
  6448. }
  6449. #pragma mark cancel order
  6450. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  6451. {
  6452. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6453. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  6454. int ret = [iSalesDB execSql:sql];
  6455. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6456. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6457. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6458. // [dic setValue:@"160409" forKey:@"min_ver"];
  6459. return [RAUtils dict2data:dic];
  6460. }
  6461. #pragma mark sign order
  6462. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  6463. {
  6464. //参考 offline_saveBusinesscard
  6465. NSLog(@"sign order params: %@",params);
  6466. // orderCode = MOB1608240002;
  6467. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  6468. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6469. orderCode = [self translateSingleQuote:orderCode];
  6470. NSString *picPath = [self valueInParams:params key:@"picpath"];
  6471. picPath = [self translateSingleQuote:picPath];
  6472. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  6473. int ret = [iSalesDB execSql:sql];
  6474. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6475. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6476. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6477. // [dic setValue:@"160409" forKey:@"min_ver"];
  6478. return [RAUtils dict2data:dic];
  6479. }
  6480. #pragma mark save order
  6481. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  6482. NSString *ret = [self valueInParams:params key:key];
  6483. if (translate) {
  6484. ret = [self translateSingleQuote:ret];
  6485. }
  6486. return ret;
  6487. }
  6488. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  6489. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  6490. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  6491. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  6492. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  6493. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  6494. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  6495. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  6496. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  6497. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  6498. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  6499. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  6500. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  6501. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  6502. if (![total_price isEqualToString:@""]) {
  6503. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  6504. } else {
  6505. total_price = @"";
  6506. }
  6507. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  6508. if ([paymentsAndCredits isEqualToString:@""]) {
  6509. paymentsAndCredits = @"";
  6510. } else {
  6511. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  6512. }
  6513. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  6514. if ([handling_fee_value isEqualToString:@""]) {
  6515. handling_fee_value = @"";
  6516. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  6517. handling_fee_value = @"";
  6518. } else {
  6519. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  6520. }
  6521. NSString *handling_fee_placeholder = handling_fee_value;
  6522. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  6523. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  6524. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  6525. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  6526. NSString *lift_gate_placeholder = @"";
  6527. if ([lift_gate_value isEqualToString:@""]) {
  6528. lift_gate_placeholder = @"";
  6529. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  6530. lift_gate_placeholder = @"";
  6531. } else {
  6532. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  6533. }
  6534. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  6535. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  6536. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  6537. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  6538. logistic_note = [logistic_note isEqualToString:@"USE MY CARRIER"] ? [NSString stringWithFormat:@"logistic_note = '%@\n\t%@',",logistic_note,logistic_note_text] : [NSString stringWithFormat:@"logistic_note = '%@',",logistic_note];
  6539. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  6540. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  6541. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  6542. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  6543. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  6544. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  6545. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  6546. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  6547. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  6548. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  6549. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  6550. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  6551. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  6552. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  6553. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  6554. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  6555. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  6556. BOOL number_nil = [credit_card_number isEqualToString:@""];
  6557. if (!number_nil) {
  6558. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  6559. }
  6560. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  6561. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  6562. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  6563. if (!security_code_nil) {
  6564. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  6565. }
  6566. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  6567. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  6568. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  6569. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  6570. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  6571. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  6572. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  6573. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  6574. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  6575. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  6576. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  6577. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  6578. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  6579. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  6580. NSString *contact_id = customer_cid;
  6581. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  6582. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  6583. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  6584. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  6585. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  6586. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  6587. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  6588. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  6589. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  6590. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  6591. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  6592. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  6593. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  6594. //
  6595. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  6596. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  6597. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  6598. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  6599. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  6600. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  6601. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  6602. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  6603. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  6604. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  6605. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  6606. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  6607. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  6608. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  6609. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  6610. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  6611. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  6612. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  6613. NSString *contactSql = [NSString stringWithFormat:@"update offline_contact set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ where contact_id = '%@'",contact_contact,contact_email,contact_phone,contact_fax,customer_name,customer_address1,customer_address2,customer_address3,customer_address4,customer_country,customer_state,customer_city,customer_zipcode,contact_id];
  6614. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  6615. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  6616. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  6617. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  6618. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  6619. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  6620. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  6621. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  6622. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  6623. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  6624. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  6625. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  6626. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  6627. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  6628. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  6629. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  6630. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  6631. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  6632. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  6633. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  6634. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  6635. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  6636. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  6637. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  6638. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  6639. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  6640. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  6641. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  6642. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  6643. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  6644. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  6645. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  6646. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  6647. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  6648. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  6649. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  6650. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  6651. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  6652. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  6653. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  6654. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  6655. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  6656. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  6657. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  6658. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  6659. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  6660. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  6661. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  6662. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  6663. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  6664. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  6665. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  6666. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  6667. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  6668. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  6669. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  6670. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  6671. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  6672. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  6673. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  6674. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  6675. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  6676. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  6677. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  6678. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  6679. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  6680. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  6681. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  6682. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  6683. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  6684. NSString *order_status = @"status = 1,";
  6685. if (submit) {
  6686. order_status = @"status = -11,";
  6687. }
  6688. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  6689. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  6690. NSString *sync_sql = @"";
  6691. if (submit) {
  6692. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  6693. }
  6694. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ where so_id = '%@';",order_status,general_notes,internal_notes,poNumber,must_call,sign_picpath,total_price,paymentsAndCredits,handling_fee_placeholder,lift_gate,lift_gate_placeholder,logist,logistic_note,erpOrderStatus,paymentType,credit_card_address1,credit_card_address2,credit_card_city,credit_card_first_name,credit_card_last_name,credit_card_number,credit_card_security_code,credit_card_state,credit_card_type,credit_card_zipcode,credit_card_expiration_year,credit_card_expiration_month,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_email,receive_fax,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,so_id,sync_sql,so_id];
  6695. DebugLog(@"save order contactSql: %@",contactSql);
  6696. DebugLog(@"save order orderSql: %@",orderSql);
  6697. // int contact_ret = [iSalesDB execSql:contactSql];
  6698. int order_ret = [iSalesDB execSql:orderSql];
  6699. int ret = order_ret;
  6700. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6701. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6702. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6703. // [dic setValue:@"160409" forKey:@"min_ver"];
  6704. [dic setObject:so_id forKey:@"so#"];
  6705. return [RAUtils dict2data:dic];
  6706. }
  6707. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  6708. {
  6709. // "logistic_note" = "PERSONAL PICK UP";
  6710. // logist_note_text
  6711. // "logistic_note" = "USE MY CARRIER";
  6712. return [self saveorder:param submit:NO];
  6713. }
  6714. #pragma mark add to cart by name
  6715. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  6716. {
  6717. NSString *orderCode = [params objectForKey:@"orderCode"];
  6718. NSString *product_name = [params objectForKey:@"product_name"];
  6719. product_name = [self translateSingleQuote:product_name];
  6720. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  6721. sqlite3 *db = [iSalesDB get_db];
  6722. __block NSMutableString *product_id_string = [NSMutableString string];
  6723. for (int i = 0; i < product_name_array.count; i++) {
  6724. NSString *name = [product_name_array objectAtIndex:i];
  6725. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  6726. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6727. int product_id = sqlite3_column_int(stmt, 0);
  6728. if (i == product_name_array.count - 1) {
  6729. [product_id_string appendFormat:@"%d",product_id];
  6730. } else {
  6731. [product_id_string appendFormat:@"%d,",product_id];
  6732. }
  6733. }];
  6734. }
  6735. NSDictionary *newParams = @{
  6736. @"product_id" : product_id_string,
  6737. @"orderCode" : orderCode
  6738. };
  6739. [iSalesDB close_db:db];
  6740. return [self offline_add2cart:[newParams mutableCopy]];
  6741. }
  6742. #pragma mark reset order
  6743. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  6744. {
  6745. UIApplication * app = [UIApplication sharedApplication];
  6746. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  6747. appDelegate.disable_trigger=true;
  6748. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  6749. appDelegate.disable_trigger=false;
  6750. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6751. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  6752. return [RAUtils dict2data:dic];
  6753. }
  6754. #pragma mark submit order
  6755. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  6756. {
  6757. return [self saveorder:params submit:YES];
  6758. }
  6759. #pragma mark copy order
  6760. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  6761. {
  6762. NSMutableDictionary *ret = @{}.mutableCopy;
  6763. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  6764. sqlite3 *db = [iSalesDB get_db];
  6765. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6766. // 首先查看联系人是否active
  6767. NSString *customer_sql = [NSString stringWithFormat:@"select is_active from offline_contact where contact_id in (select customer_cid from offline_order where so_id = '%@');",order_code];
  6768. __block int customer_is_active = 1;
  6769. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6770. customer_is_active = sqlite3_column_int(stmt, 0);
  6771. }];
  6772. if (!customer_is_active) {
  6773. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  6774. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6775. [iSalesDB close_db:db];
  6776. return [RAUtils dict2data:ret];
  6777. }
  6778. // new order
  6779. NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  6780. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  6781. user = [self translateSingleQuote:user];
  6782. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,total_price,logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext) select '%@',1,submit_as,internal_notes,sales_rep,'%@',total_price,logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  6783. __block int result = 1;
  6784. result = [iSalesDB execSql:insert_order_sql db:db];
  6785. if (!result) {
  6786. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  6787. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6788. [iSalesDB close_db:db];
  6789. return [RAUtils dict2data:ret];
  6790. }
  6791. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  6792. // __block NSString *product_id = @"";
  6793. __weak typeof(self) weakSelf = self;
  6794. NSString *product_id_sql = [NSString stringWithFormat:@"select product_id,is_active from product WHERE product_id in (select product_id from offline_cart where so_no = '%@');",order_code];
  6795. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6796. int is_active = sqlite3_column_int(stmt, 1);
  6797. if (is_active) {
  6798. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  6799. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  6800. NSString *insert_cart_sql = [NSString stringWithFormat:@"insert into offline_cart (so_no,orderitem_id,product_id,item_id,item_count,bundle_item,type) select '%@',orderitem_id,product_id,item_id,item_count,bundle_item,type from offline_cart where product_id = %@ and so_no = '%@'",new_order_code,product_id,order_code];
  6801. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  6802. }
  6803. }];
  6804. // product_id = [product_id substringFromIndex:1];
  6805. //
  6806. // [self offline_add2cart:@{}.mutableCopy];
  6807. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6808. [iSalesDB close_db:db];
  6809. if (result) {
  6810. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  6811. } else {
  6812. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  6813. }
  6814. [ret setObject:new_order_code forKey:@"so_id"];
  6815. return [RAUtils dict2data:ret];
  6816. }
  6817. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  6818. NSString *collectId = params[@"collectId"];
  6819. NSString *sql = [NSString stringWithFormat:@"select product_id,qty from wishlist where _id in (%@);",collectId];
  6820. __block NSString *product_id = @"";
  6821. __block NSString *qty = @"";
  6822. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6823. int productId = sqlite3_column_int(stmt, 0);
  6824. int item_qty = sqlite3_column_int(stmt, 1);
  6825. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  6826. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  6827. }];
  6828. product_id = [product_id substringFromIndex:1];
  6829. qty = [qty substringFromIndex:1];
  6830. NSDictionary *newParams = @{
  6831. @"product_id" : product_id,
  6832. @"orderCode" : params[@"orderCode"],
  6833. @"qty" : qty
  6834. };
  6835. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  6836. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  6837. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  6838. NSString *deleteSQL = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  6839. int ret = [iSalesDB execSql:deleteSQL];
  6840. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  6841. }
  6842. return [RAUtils dict2data:retDic];
  6843. }
  6844. @end