OLDataProvider.m 353 KB

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