OLDataProvider.m 535 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734
  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. #import "AppDelegate.h"
  17. #import "pdfCreator.h"
  18. #import "Singleton.h"
  19. #import "QRCodeGenerator.h"
  20. #import "config.h"
  21. @interface OLDataProvider ()
  22. @end
  23. @implementation OLDataProvider
  24. +(NSString*) get_portfolio_price:(NSString*) contact_id item_id:(int) item_id price:(int)price_id db:(sqlite3*)db
  25. {
  26. NSString * sqlQuery = [NSString stringWithFormat:@"select decrypt(price) from model_price where item_id=%d and price_name in (select name from price where type=%d) ",item_id,price_id];
  27. NSString* ret=@"No Price.";
  28. sqlite3_stmt * statement;
  29. // int count=0;
  30. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  31. {
  32. if (sqlite3_step(statement) == SQLITE_ROW)
  33. {
  34. char *price = (char*)sqlite3_column_text(statement, 0);
  35. if(price==nil)
  36. price="";
  37. NSString *nsprice = [[NSString alloc]initWithUTF8String:price];
  38. double dp= [nsprice doubleValue];
  39. ret=[NSString stringWithFormat:@"%.2f",dp];
  40. }
  41. sqlite3_finalize(statement);
  42. }
  43. return ret;
  44. // // [iSalesDB close_db:db];
  45. //
  46. // return nil;
  47. //// if(dprice==DBL_MAX)
  48. //// ret= nil;
  49. //// else
  50. //// ret= [NSNumber numberWithDouble:dprice];
  51. //// return ret;
  52. }
  53. + (NSString *)portfolio_img:(NSString *)item_id db:(sqlite3 *)db
  54. {
  55. NSString* ret= nil;
  56. NSString *sqlQuery = nil;
  57. // 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
  58. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ and i.type<=1 order by i.default_img desc, i.type desc limit 1;",item_id];
  59. sqlite3_stmt * statement;
  60. // int count=0;
  61. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  62. {
  63. if (sqlite3_step(statement) == SQLITE_ROW)
  64. {
  65. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  66. if(imgurl==nil)
  67. imgurl="";
  68. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  69. ret=nsimgurl;
  70. }
  71. sqlite3_finalize(statement);
  72. }
  73. else
  74. {
  75. ret=@"";
  76. }
  77. // [iSalesDB close_db:db];
  78. // DebugLog(@"data string: %@",ret );
  79. return ret;
  80. }
  81. +(NSMutableDictionary*) prepare_portfolio_data:(NSMutableDictionary*) params
  82. {
  83. NSMutableDictionary* values=params[@"replaceValue"]; // 离线下发的参数
  84. //生成portfolio pdf需要的数据
  85. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  86. data[@"npd_url"]=@"www.newpacificdirect.com";
  87. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  88. [formatter setDateFormat:@"MMMM yyyy"];
  89. NSString* date = [formatter stringFromDate:[NSDate date]];
  90. data[@"create_month"]=date;
  91. data[@"company_name"]=@"New Pacific Direct,Inc.";
  92. data[@"catalog_name"]=params[@"catalog_name"];
  93. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  94. data[@"TOTAL_PAGE"]=0;
  95. data[@"CURRENT_PAGE"]=0;
  96. NSMutableDictionary* grid = [[NSMutableDictionary alloc]init];
  97. BOOL show_group_by = [[params objectForKey:@"show_group_by"] boolValue];
  98. bool show_stockout=[params[@"show_stockout"] boolValue];
  99. NSString *sql = nil;
  100. // v1.90 more color
  101. if(values==nil)
  102. {
  103. sql=[NSString stringWithFormat:@"select a.product_id,a.name,a.description,a.price,a.sheet_discount,a.qty,a.is_percentage,a.item_id,a.fashion_id,a.line_note,a.percent,a._id,b.availability,b.color,b.legcolor,b.demension,b.seat_height,b.material,b.box_dim,b.volume,b.weight,stockuom,b.load_ability,b.fabric_content,b.assembling,b.made_in,b.more_color from (select _id, product_id,name,description,ifnull(sheet_price,'null') as price,sheet_discount,ifnull(available_qty,'null') as qty,percentage as is_percentage,item_id,fashion_id,line_note,percent from offline_portfolio where product_id in(%@)) a left join (select * from product where product_id in(%@)) b on a.product_id=b.product_id order by a.name",params[@"product_ids"],params[@"product_ids"]];
  104. }
  105. else
  106. {
  107. sql=[NSString stringWithFormat:@"select product_id,name,description,null,null,null,null,item_id,null,null,null,-1,availability,color,legcolor,demension,seat_height,material,box_dim,volume,weight,stockuom,load_ability,fabric_content,assembling,made_in,more_color from product where product_id in(%@) order by name",params[@"product_ids"]];
  108. }
  109. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  110. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  111. sqlite3 *db = [iSalesDB get_db];
  112. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  113. // [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  114. int product_id = sqlite3_column_int(stmt, 0);
  115. // NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  116. NSString *name = [self textAtColumn:1 statement:stmt];
  117. NSString *description = [self textAtColumn:2 statement:stmt];
  118. double price = sqlite3_column_double(stmt, 3);
  119. NSString *s_price = [self textAtColumn:3 statement:stmt];
  120. double discount = sqlite3_column_double(stmt,4);
  121. int qty = sqlite3_column_int(stmt, 5);
  122. NSString *s_qty = [self textAtColumn:5 statement:stmt];
  123. int is_percentage = sqlite3_column_int(stmt, 6);
  124. NSString *s_is_percentage = [self textAtColumn:6 statement:stmt];
  125. int item_id = sqlite3_column_int(stmt, 7);
  126. // int fashion_id = sqlite3_column_int(stmt, 8);
  127. NSString *line_note = [self textAtColumn:9 statement:stmt];
  128. NSString *s_percent = [self textAtColumn:10 statement:stmt];
  129. double percent = sqlite3_column_double(stmt, 10);
  130. // int portfolio_id = sqlite3_column_int(stmt, 11);
  131. NSString *s_availability = [self textAtColumn:12 statement:stmt];
  132. int availability = sqlite3_column_int(stmt, 12);
  133. NSString *color = [self textAtColumn:13 statement:stmt];
  134. NSString *legcolor = [self textAtColumn:14 statement:stmt];
  135. NSString *demension = [self textAtColumn:15 statement:stmt];
  136. NSString *seat_height = [self textAtColumn:16 statement:stmt];
  137. NSString *material = [self textAtColumn:17 statement:stmt];
  138. NSString *box_dim = [self textAtColumn:18 statement:stmt];
  139. NSString *s_volume = [self textAtColumn:19 statement:stmt];
  140. double volume = sqlite3_column_double(stmt, 19);
  141. NSString *s_weight = [self textAtColumn:20 statement:stmt];
  142. double weight = sqlite3_column_double(stmt, 20);
  143. NSString *s_model_set = [self textAtColumn:21 statement:stmt];
  144. int model_set = sqlite3_column_int(stmt, 21);
  145. NSString *s_load_ability = [self textAtColumn:22 statement:stmt];
  146. double load_ability = sqlite3_column_double(stmt, 22);
  147. NSString *fabric_content = [self textAtColumn:23 statement:stmt];
  148. NSString *assembling = [self textAtColumn:24 statement:stmt];
  149. NSString *made_in = [self textAtColumn:25 statement:stmt];
  150. NSInteger more_color_int = sqlite3_column_int(stmt, 26);
  151. NSString* gprice = [self get_portfolio_price :appDelegate.contact_id item_id:item_id price:[params[@"regular_price"] intValue] db:db];
  152. float bprice=0;
  153. NSDictionary* bundle=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  154. for(int b=0;b< [bundle[@"count"] intValue];b++)
  155. {
  156. NSDictionary* bi=bundle[[NSString stringWithFormat:@"item_%d",b]];
  157. bprice+= [bi[@"modulus"] intValue] * [bi[@"unit_price"] floatValue];
  158. }
  159. if(gprice!=nil)
  160. gprice = [NSString stringWithFormat:@"%.2f",[gprice floatValue]+bprice];
  161. if(s_percent.length==0||is_percentage==0)
  162. {
  163. percent=100.0;
  164. if([s_qty isEqualToString:@"null"])
  165. qty=availability;
  166. }
  167. else
  168. {
  169. qty=availability;
  170. }
  171. qty=qty*percent/100+0.5;
  172. for(int i=0;i<[values[@"count"] intValue];i++)
  173. {
  174. NSMutableDictionary * valueitem=values[[NSString stringWithFormat:@"item_%d",i]];;
  175. if([valueitem[@"product_id"] intValue]==product_id)
  176. {
  177. line_note=valueitem[@"line_note"];
  178. qty=[valueitem[@"available_qty"] intValue];
  179. if([[valueitem allKeys] containsObject:@"available_qty"])
  180. s_qty=[NSString stringWithFormat:@"%d",qty];
  181. else
  182. s_qty=@"null";
  183. if(valueitem[@"regular_price"]!=nil)
  184. gprice=[NSString stringWithFormat:@"%.2f",[valueitem[@"regular_price"] floatValue]];
  185. s_price=valueitem[@"special_price"];
  186. price=[valueitem[@"special_price"] floatValue];
  187. discount=0;
  188. // v1.90
  189. more_color_int = [[valueitem objectForKey:@"more_color"] integerValue];
  190. }
  191. }
  192. if(is_percentage==0&&qty==0&&!show_stockout)
  193. {
  194. return;
  195. }
  196. if((is_percentage!=0||s_is_percentage.length!=0)&&qty==0&&!show_stockout)
  197. {
  198. return;
  199. }
  200. NSString* set_price=@"";
  201. if([params[@"entered_price"] boolValue])
  202. {
  203. if (s_price==nil ) {
  204. // set_price=[NSString stringWithFormat:@"Special: $ %@",gprice];
  205. }
  206. else
  207. {
  208. if(price*(1-discount/100.0)!=gprice.floatValue)
  209. set_price=[NSString stringWithFormat:@"Special: $ %.2f",price*(1-discount/100.0)];
  210. }
  211. }
  212. NSString* get_price=@"";
  213. {
  214. // NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  215. // DebugLog(@"price time interval");
  216. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  217. if(gprice==nil)
  218. get_price=@"Price:No Price.";
  219. else
  220. get_price=[NSString stringWithFormat:@"Price: $ %@",gprice];
  221. }
  222. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  223. // if ([qty_null isEqualToString:@"null"]) {
  224. // qty=availability;
  225. // }
  226. //
  227. // NSString *qty_percent_null = [self textAtColumn:6 statement:stmt];
  228. // if ([qty_percent_null isEqualToString:@"null"]) {
  229. // percentage=1;
  230. // }
  231. NSString * str_availability=[NSString stringWithFormat:@"<b>Availability:</b> %d",(int)qty];
  232. NSString * str_color=[NSString stringWithFormat:@"<b>Color:</b> %@",color];
  233. NSString * str_model_set=[NSString stringWithFormat:@"<b>Set:</b> Sold in QTY of %d",model_set];
  234. NSString * str_legcolor=[NSString stringWithFormat:@"<b>Leg Color:</b> %@",legcolor];
  235. NSString * str_demension=[NSString stringWithFormat:@"<b>Dimension:</b> %@",demension];
  236. NSString * str_seat_height=[NSString stringWithFormat:@"<b>Seat Height:</b> %@",seat_height];
  237. NSString * str_meterial=[NSString stringWithFormat:@"<b>Meterial:</b> %@",material];
  238. NSString * str_box_dim=[NSString stringWithFormat:@"<b>Box Dimension:</b> %@",box_dim];
  239. NSString * str_volume=[NSString stringWithFormat:@"<b>Volume:</b> %.2f",volume];
  240. NSString * str_weight=[NSString stringWithFormat:@"<b>Weight:</b> %.2f",weight];
  241. NSString * str_load_ability=[NSString stringWithFormat:@"<b>Load Ability:</b> %.2f",load_ability];
  242. NSString * str_fabric_content=[NSString stringWithFormat:@"<b>Fabric Content:</b> %@",fabric_content];
  243. NSString * str_assembling=[NSString stringWithFormat:@"<b>Assembling:</b> %@",assembling];
  244. NSString * str_made_in=[NSString stringWithFormat:@"<b>Made In:</b> %@",made_in];
  245. NSString * str_line_note=[NSString stringWithFormat:@"<b>Line Note:</b> %@",line_note];
  246. if([params[@"availability"] boolValue]==false)
  247. str_availability=@"";
  248. else
  249. {
  250. if ([s_qty isEqualToString:@"null"] && [s_availability isEqualToString:@"0"])
  251. str_availability=@"<b>Availability:</b> In Production";
  252. }
  253. if([params[@"color"] boolValue]==false || color.length==0 )
  254. str_color=@"";
  255. if([params[@"set"] boolValue]==false || s_model_set.length==0 )
  256. str_model_set=@"";
  257. if([params[@"leg_color"] boolValue]==false || legcolor.length==0 )
  258. str_legcolor=@"";
  259. if([params[@"dimension"] boolValue]==false || demension.length==0 )
  260. str_demension=@"";
  261. if([params[@"seat_height"] boolValue]==false || seat_height.length==0 )
  262. str_seat_height=@"";
  263. if([params[@"material"] boolValue]==false || material.length==0 )
  264. str_meterial=@"";
  265. if([params[@"box_dimension"] boolValue]==false || box_dim.length==0 )
  266. str_box_dim=@"";
  267. if([params[@"volume"] boolValue]==false || s_volume.length==0 )
  268. str_volume=@"";
  269. if([params[@"weight"] boolValue]==false || s_weight.length==0 )
  270. str_weight=@"";
  271. if([params[@"load_ability"] boolValue]==false || s_load_ability.length==0 )
  272. str_load_ability=@"";
  273. if([params[@"fabric_content"] boolValue]==false || fabric_content.length==0 )
  274. str_fabric_content=@"";
  275. if([params[@"assembling"] boolValue]==false || assembling.length==0 )
  276. str_assembling=@"";
  277. if([params[@"made"] boolValue]==false || made_in.length==0 )
  278. str_made_in=@"";
  279. if([params[@"show_line_note"] boolValue]==false || line_note.length==0 )
  280. str_line_note=@"";
  281. NSMutableArray* arr_detail=[[NSMutableArray alloc]init];
  282. [arr_detail addObject:str_availability];
  283. [arr_detail addObject:str_color];
  284. [arr_detail addObject:str_model_set];
  285. [arr_detail addObject:str_legcolor];
  286. [arr_detail addObject:str_demension];
  287. [arr_detail addObject:str_seat_height];
  288. [arr_detail addObject:str_meterial];
  289. [arr_detail addObject:str_box_dim];
  290. [arr_detail addObject:str_volume];
  291. [arr_detail addObject:str_weight];
  292. [arr_detail addObject:str_load_ability];
  293. [arr_detail addObject:str_fabric_content];
  294. [arr_detail addObject:str_assembling];
  295. [arr_detail addObject:str_made_in];
  296. [arr_detail addObject:str_line_note];
  297. // NSString* detail =[NSString stringWithFormat:@"<font size=0.2>%@</font>",[RAUtils arr2string:arr_detail separator:@" " trim:true]];
  298. NSString* detail =[RAUtils arr2string:arr_detail separator:@" " trim:true];
  299. //model image;
  300. NSString* img_path = [self portfolio_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  301. // qr image
  302. NSString* qrpath=nil;
  303. if([params[@"show_barcode"] boolValue])
  304. {
  305. NSString* temp = NSTemporaryDirectory();
  306. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  307. qrpath=[temp stringByAppendingPathComponent:filename];
  308. UIImage* qrimg=[QRCodeGenerator qrImageForString:name imageSize:240];
  309. [UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  310. }
  311. NSString *more_color_path = [[NSBundle mainBundle] pathForResource:@"more_color_64" ofType:@"png"];
  312. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  313. item[@"img"]=img_path;
  314. item[@"img_qr"]=qrpath;
  315. item[@"special_price"]=set_price;
  316. item[@"price"]=get_price;
  317. item[@"name"]=name;
  318. item[@"description"]=description;
  319. item[@"detail"]=detail;
  320. item[@"img"]=img_path;
  321. if (show_group_by && more_color_int != 0) {
  322. item[@"more_color"] = more_color_path;
  323. }
  324. // @{
  325. // //@"linenotes": line_note,
  326. //// @"product_id": product_id_string,
  327. //// @"available_qty": @(qty),
  328. //// @"available_percent" : @(percent),
  329. //// @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  330. //// @"item_id": [NSString stringWithFormat:@"%d",item_id],
  331. //// @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  332. // @"": ,
  333. // @"": ,
  334. // @"": ,
  335. // @"": ,
  336. // @"": ,
  337. // @"": ,
  338. // @"":
  339. // }.mutableCopy;
  340. // if (percentage) {
  341. // [item removeObjectForKey:@"available_qty"];
  342. // } else {
  343. // [item removeObjectForKey:@"available_percent"];
  344. // }
  345. //
  346. // NSString *qty_null = [self textAtColumn:5 statement:stmt];
  347. // if ([qty_null isEqualToString:@"null"]) {
  348. // [item removeObjectForKey:@"available_qty"];
  349. // }
  350. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  351. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  352. }];
  353. [iSalesDB close_db:db];
  354. // [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  355. // [dic setValue:@"" forKey:@"email_content"];
  356. // [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  357. // [dic setValue:@"Regular Mode" forKey:@"mode"];
  358. //
  359. // NSMutableDictionary* cell = [[NSMutableDictionary alloc]init];
  360. // cell[@"count"]=[NSNumber numberWithInt:10];
  361. grid[@"cell0"]=dic;
  362. data[@"grid0"]=grid;
  363. return data;
  364. }
  365. +(NSMutableDictionary*) get_pdftemplate:(NSString*) template
  366. {
  367. NSString *default_path = [[NSBundle mainBundle] pathForResource:template ofType:nil];
  368. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  369. NSString *cache_folder=[paths objectAtIndex:0];
  370. NSString *template_path = [cache_folder stringByAppendingPathComponent:[NSString stringWithFormat:@"offline_data/%@",template]];
  371. BOOL bdir=NO;
  372. NSFileManager* fileManager = [NSFileManager defaultManager];
  373. if(PDF_DEBUG)
  374. {
  375. NSData *data = [NSData dataWithContentsOfFile:default_path];
  376. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  377. return ret;
  378. }
  379. if(! [fileManager fileExistsAtPath:template_path isDirectory:&bdir] )
  380. {
  381. NSError * error=nil;
  382. if(![fileManager copyItemAtPath:default_path toPath:template_path error:&error])
  383. {
  384. return nil;
  385. }
  386. }
  387. NSData *data = [NSData dataWithContentsOfFile:template_path];
  388. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  389. return ret;
  390. }
  391. +(NSMutableDictionary*) query_login:(NSString*) user db:(sqlite3*) db
  392. {
  393. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  394. 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,first_name from offline_login where lower(username)='%@' ",user.lowercaseString ];
  395. DebugLog(@"offline_login sql:%@",sqlQuery);
  396. sqlite3_stmt * statement;
  397. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  398. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  399. {
  400. if (sqlite3_step(statement) == SQLITE_ROW)
  401. {
  402. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  403. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  404. int can_show_price = sqlite3_column_int(statement, 0);
  405. int can_see_price = sqlite3_column_int(statement, 1);
  406. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  407. if(contact_id==nil)
  408. contact_id="";
  409. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  410. int user_type = sqlite3_column_int(statement, 3);
  411. int can_cancel_order = sqlite3_column_int(statement, 4);
  412. int can_set_cart_price = sqlite3_column_int(statement, 5);
  413. int can_create_portfolio = sqlite3_column_int(statement, 6);
  414. int can_delete_order = sqlite3_column_int(statement, 7);
  415. int can_submit_order = sqlite3_column_int(statement, 8);
  416. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  417. int can_create_order = sqlite3_column_int(statement, 10);
  418. char *mode = (char*)sqlite3_column_text(statement, 11);
  419. if(mode==nil)
  420. mode="";
  421. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  422. char *username = (char*)sqlite3_column_text(statement, 12);
  423. if(username==nil)
  424. username="";
  425. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  426. int can_update_contact_info = sqlite3_column_int(statement, 13);
  427. char *first_name = (char*)sqlite3_column_text(statement, 14);
  428. if(first_name==nil)
  429. first_name="";
  430. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  431. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  432. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  433. // [header setValue:nscontact_id forKey:@"contact_id"];
  434. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  435. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  436. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  437. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  438. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  439. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  440. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  441. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  442. //
  443. // [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  444. //
  445. // [header setValue:nsusername forKey:@"username"];
  446. //
  447. // NSError* error=nil;
  448. //
  449. // NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  450. // [header setValue:statusFilter forKey:@"statusFilter"];
  451. //
  452. //
  453. //
  454. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  455. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  456. //
  457. // // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  458. // // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  459. // int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  460. // [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  461. //
  462. // [ret setObject:header forKey:@"header"];
  463. [ret setValue:nsfirst_name forKey:@"first_name"];
  464. }
  465. sqlite3_finalize(statement);
  466. }
  467. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  468. return ret;
  469. }
  470. +(NSMutableDictionary*) prepare_salesorder_data:(NSString*) order_code type:(int) type
  471. {
  472. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  473. NSMutableDictionary* data = [[NSMutableDictionary alloc]init];
  474. data[@"npd_url"]=@"www.newpacificdirect.com";
  475. NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
  476. [formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  477. NSString* date = [formatter stringFromDate:[NSDate date]];
  478. data[@"print_date"]=date;
  479. data[@"company_name"]=@"New Pacific Direct,Inc.";
  480. data[@"PAGE_INDEX"]=@"Page $.{CURRENT_PAGE}.$ of $.{TOTAL_PAGE}.$";
  481. data[@"TOTAL_PAGE"]=0;
  482. data[@"CURRENT_PAGE"]=0;
  483. // [formatter setDateFormat:@"MM/dd/yyyy"];
  484. // date = [formatter stringFromDate:[NSDate date]];
  485. // data[@"creat_date"]=date;
  486. NSString* temp = NSTemporaryDirectory();
  487. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  488. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  489. UIImage* qrimg=[RAUtils generateBarCode:order_code width:480 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  490. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  491. data[@"img_barcode"]=qrpath;
  492. data[@"so_id"]=order_code;
  493. data[@"company"]=@"NEW PACIFIC DIRECT, INC.";
  494. // NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where so_id=%@) o left join offline_contact c on o.customer_cid=c.contact_id",order_code];
  495. NSString *sql = [NSString stringWithFormat:@"select erpOrderStatus,sender_name,sender_ext,sender_phone,sender_fax,sender_email,warehouse_name,billing_name,billing_ext,billing_phone,billing_fax,billing_email,receive_name,receive_ext,receive_phone,receive_fax,receive_email,poNumber,sales_rep,carrier,vendor_no,terms,sales_terms,port_destination,etd,sign_picpath,status,create_time,shipping,lift_gate_value,handling_fee_value,billing_contact,receive_contact,general_notes,customer_cid from offline_order where so_id='%@'",order_code];
  496. sqlite3 *db = [iSalesDB get_db];
  497. data[@"print_by"]= [self query_login:appDelegate.user db:db][@"first_name"];
  498. sqlite3_stmt * statement;
  499. NSMutableDictionary* datasource = [[NSMutableDictionary alloc]init];
  500. NSMutableDictionary* row0 = [[NSMutableDictionary alloc]init];
  501. row0[@"count"]=[NSNumber numberWithInt:1];
  502. datasource[@"row0"]=row0;
  503. NSString* print_status=@"";
  504. NSString* order_type=@"Offline Quote";
  505. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  506. {
  507. if (sqlite3_step(statement) == SQLITE_ROW)
  508. {
  509. int erpOrderStatus = sqlite3_column_int(statement, 0);
  510. int status = sqlite3_column_int(statement, 26);
  511. if(status==2)
  512. {
  513. switch (erpOrderStatus) {
  514. case 10:
  515. // order_type=@"Quoted Order";
  516. print_status=@"Quote";
  517. break;
  518. case 11:
  519. print_status=@"Booked";
  520. break;
  521. case 12:
  522. print_status=@"Picked";
  523. break;
  524. case 13:
  525. print_status=@"Shipped";
  526. break;
  527. case 14:
  528. print_status=@"Closed";
  529. break;
  530. case 15:
  531. print_status=@"Canceled";
  532. break;
  533. case 16:
  534. print_status=@"All";
  535. break;
  536. case 18:
  537. print_status=@"Processing";
  538. break;
  539. default:
  540. break;
  541. }
  542. }
  543. else{
  544. switch (status) {
  545. case 0:
  546. {
  547. print_status=@"Quote";
  548. break;
  549. }
  550. case 1:
  551. {
  552. print_status=@"Saved";
  553. break;
  554. }
  555. case 3:
  556. {
  557. //canceled
  558. print_status=@"Canceled";
  559. break;
  560. }
  561. default:
  562. break;
  563. }
  564. }
  565. NSString* sender_name = [self textAtColumn:1 statement:statement];
  566. NSString* sender_ext = [self textAtColumn:2 statement:statement];
  567. NSString* sender_phone = [self textAtColumn:3 statement:statement];
  568. NSString* sender_fax = [self textAtColumn:4 statement:statement];
  569. NSString* sender_email = [self textAtColumn:5 statement:statement];
  570. NSString* warehouse_name = [self textAtColumn:6 statement:statement];
  571. NSString* billing_name = [self textAtColumn:7 statement:statement];
  572. NSString* billing_ext = [self textAtColumn:8 statement:statement];
  573. NSString* billing_phone = [self textAtColumn:9 statement:statement];
  574. if(billing_phone.length>0)
  575. billing_phone= [@"TEL: " stringByAppendingString:billing_phone];
  576. NSString* billing_fax = [self textAtColumn:10 statement:statement];
  577. if(billing_fax.length>0)
  578. billing_fax= [@"FAX: " stringByAppendingString:billing_fax];
  579. NSMutableArray* billto_arr0 = [[NSMutableArray alloc]init];
  580. [billto_arr0 addObject:billing_phone];
  581. [billto_arr0 addObject:billing_fax];
  582. NSString* billing_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  583. NSString* billing_email = [self textAtColumn:11 statement:statement];
  584. if(billing_email.length>0)
  585. billing_email= [@"EMAIL: " stringByAppendingString:billing_email];
  586. NSString* billing_contact = [self textAtColumn:31 statement:statement];
  587. if(billing_contact.length>0)
  588. billing_contact= [@"ATTN: " stringByAppendingString:billing_contact];
  589. NSString* receive_name = [self textAtColumn:12 statement:statement];
  590. NSString* receive_ext = [self textAtColumn:13 statement:statement];
  591. NSString* receive_phone = [self textAtColumn:14 statement:statement];
  592. if(receive_phone.length>0)
  593. receive_phone= [@"TEL: " stringByAppendingString:receive_phone];
  594. NSString* receive_fax = [self textAtColumn:15 statement:statement];
  595. if(receive_fax.length>0)
  596. receive_fax= [@"FAX: " stringByAppendingString:receive_fax];
  597. NSMutableArray* receive_arr0 = [[NSMutableArray alloc]init];
  598. [receive_arr0 addObject:receive_phone];
  599. [receive_arr0 addObject:receive_fax];
  600. NSString* receive_email = [self textAtColumn:16 statement:statement];
  601. NSString* receive_pf=[RAUtils arr2string:billto_arr0 separator:@" " trim:true];
  602. if(receive_email.length>0)
  603. receive_email= [@"EMAIL: " stringByAppendingString:receive_email];
  604. NSString* receive_contact = [self textAtColumn:32 statement:statement];
  605. if(receive_contact.length>0)
  606. receive_contact= [@"ATTN: " stringByAppendingString:receive_contact];
  607. NSString* poNumber = [self textAtColumn:17 statement:statement];
  608. NSString* sales_rep = [self textAtColumn:18 statement:statement];
  609. NSString* carrier = [self textAtColumn:19 statement:statement];
  610. NSString* vendor_no = [self textAtColumn:20 statement:statement];
  611. NSString* terms = [self textAtColumn:21 statement:statement];
  612. NSString* sales_terms = [self textAtColumn:22 statement:statement];
  613. NSString* port_destination = [self textAtColumn:23 statement:statement];
  614. NSString* etd = [self textAtColumn:24 statement:statement];
  615. NSString* sign_picpath = [self textAtColumn:25 statement:statement];
  616. if(sign_picpath.length==0)
  617. sign_picpath=nil;
  618. NSString* create_date = [self textAtColumn:27 statement:statement];
  619. create_date=[self changeDateTime:create_date Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy"];
  620. NSString* general_notes=[self textAtColumn:33 statement:statement];
  621. NSString *customerID = [self textAtColumn:34 statement:statement];
  622. if (customerID.length == 0) {
  623. customerID = nil;
  624. }
  625. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  626. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  627. if(general_notes.length>0)
  628. {
  629. NSMutableDictionary* row2 = [[NSMutableDictionary alloc]init];
  630. row2[@"item_0"]=@{@"g_notes":general_notes };
  631. row2[@"count"]=[NSNumber numberWithInt:1];
  632. datasource[@"row2"]=row2;
  633. }
  634. NSMutableArray* billto_arr = [[NSMutableArray alloc]init];
  635. [billto_arr addObject:billing_ext];
  636. [billto_arr addObject:billing_pf];
  637. [billto_arr addObject:billing_contact];
  638. [billto_arr addObject:billing_email];
  639. NSString* billto=[RAUtils arr2string:billto_arr separator:@"\r\n" trim:true];
  640. NSMutableArray* receive_arr = [[NSMutableArray alloc]init];
  641. [receive_arr addObject:receive_ext];
  642. [receive_arr addObject:receive_pf];
  643. [receive_arr addObject:receive_contact];
  644. [receive_arr addObject:receive_email];
  645. NSString* receive=[RAUtils arr2string:receive_arr separator:@"\r\n" trim:true];
  646. float shipping = sqlite3_column_double(statement, 28);
  647. float lift_gate_value = sqlite3_column_double(statement, 29);
  648. float handling_fee_value = sqlite3_column_double(statement, 30);
  649. data[@"company"]=sender_name;
  650. data[@"shipfrom_addr"]=sender_ext;
  651. data[@"shipfrom_phone"]=sender_phone;
  652. data[@"shipfrom_fax"]=sender_fax;
  653. data[@"shipfrom_email"]=sender_email;
  654. data[@"warehouse_name"]=warehouse_name;
  655. data[@"bill_to_name"]=billing_name;
  656. data[@"bill_to_ext"]=billto;
  657. data[@"ship_to_name"]=receive_name;
  658. data[@"ship_to_ext"]=receive;
  659. data[@"po_no"]=poNumber;
  660. data[@"rep"]=sales_rep;
  661. data[@"e_schdule"]=etd;
  662. data[@"sales_terms"]=sales_terms;
  663. data[@"port_destination"]=port_destination;
  664. data[@"terms"]=terms;
  665. data[@"carrier"]=carrier;
  666. data[@"vendor_no"]=vendor_no;
  667. data[@"customer_sign"]=sign_picpath;
  668. data[@"print_status"]=print_status;
  669. data[@"create_date"]=create_date;
  670. NSString *sql_cart=[ NSString stringWithFormat:@"select c.product_id,c.price,c.discount,c.item_count,c.line_note,m.name,m.description,c.item_id,m.stockUom,c._id from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id ",order_code];;
  671. sqlite3_stmt * statement_cart;
  672. // NSDate *date1 = [NSDate date];
  673. int count=0;
  674. int cart_count=0;
  675. float credits=0;
  676. int dbresult=sqlite3_prepare_v2(db, [sql_cart UTF8String], -1, &statement_cart, nil);
  677. if ( dbresult== SQLITE_OK)
  678. {
  679. NSMutableDictionary* row1 = [[NSMutableDictionary alloc]init];
  680. int row1count=0;
  681. while (sqlite3_step(statement_cart) == SQLITE_ROW)
  682. {
  683. NSDate *row_date = [NSDate date];
  684. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  685. // int product_id = sqlite3_column_int(statement_cart, 0);
  686. char *str_price = (char*)sqlite3_column_text(statement_cart, 1);
  687. int item_id = sqlite3_column_int(statement_cart, 7);
  688. double discount = sqlite3_column_double(statement_cart, 2);
  689. NSString* Price=nil;
  690. if(str_price==nil)
  691. {
  692. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  693. // NSDate *price_date = [NSDate date];
  694. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  695. // DebugLog(@"price time interval");
  696. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  697. if(price==nil)
  698. Price=@"No Price.";
  699. else
  700. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  701. }
  702. else
  703. {
  704. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  705. }
  706. float dunitprice=0;
  707. int item_count = sqlite3_column_int(statement_cart, 3);
  708. NSString* Amount=@"";
  709. if(![Price isEqualToString:@"No Price."])
  710. {
  711. dunitprice=[Price floatValue];
  712. dunitprice=dunitprice* (1-discount/100.0);
  713. Price=[NSString stringWithFormat:@"%.2f",dunitprice];
  714. credits+=dunitprice*item_count;
  715. Amount=[NSString stringWithFormat:@"%.2f",dunitprice*item_count];
  716. }
  717. else
  718. {
  719. Price=@"";
  720. }
  721. // char *line_note = (char*)sqlite3_column_text(statement_cart, 4);
  722. // NSString *nsline_note=nil;
  723. // if(line_note!=nil)
  724. // nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  725. char *name = (char*)sqlite3_column_text(statement_cart, 5);
  726. NSString *nsname=nil;
  727. if(name!=nil)
  728. nsname= [[NSString alloc]initWithUTF8String:name];
  729. char *description = (char*)sqlite3_column_text(statement_cart, 6);
  730. NSString *nsdescription=nil;
  731. if(description!=nil)
  732. nsdescription= [[NSString alloc]initWithUTF8String:description];
  733. // int stockUom = sqlite3_column_int(statement_cart, 8);
  734. // int _id = sqlite3_column_int(statement_cart, 9);
  735. // NSDate *subtotal_date = [NSDate date];
  736. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  737. // DebugLog(@"subtotal_date time interval");
  738. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  739. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  740. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  741. // int carton=[bsubtotaljson[@"carton"] intValue];
  742. // cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  743. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  744. // NSDate *img_date = [NSDate date];
  745. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  746. // DebugLog(@"img_date time interval");
  747. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  748. itemjson[@"model"]=nsname;
  749. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  750. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  751. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  752. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  753. // itemjson[@"check"]=@"true";
  754. // itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  755. itemjson[@"count"]=[NSString stringWithFormat:@"%d",item_count];
  756. itemjson[@"unit_price"]=Price;
  757. itemjson[@"amount"]=Amount;
  758. // itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  759. itemjson[@"description"]=[NSString stringWithFormat:@"%@",nsdescription ];
  760. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  761. // itemjson[@"note"]=nsline_note;
  762. // NSDate *date2 = [NSDate date];
  763. // itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false];
  764. // DebugLog(@"model_bundle time interval");
  765. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  766. itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  767. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=itemjson;
  768. row1count++;
  769. NSDictionary* combine =[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  770. for(int c=0;c<[combine[@"count"] intValue];c++)
  771. {
  772. NSMutableDictionary* combine_item=[combine[[NSString stringWithFormat:@"item_%d",c]] mutableCopy];
  773. combine_item[@"count"]=[NSString stringWithFormat:@"%d",[combine_item[@"modulus"] intValue]*item_count];
  774. // combine_item[@"count"]=[NSNumber numberWithInt:[combine_item[@"modulus"] intValue]*item_count];
  775. double combine_amount=[combine_item[@"modulus"] intValue]*item_count* [combine_item[@"unit_price"] floatValue];
  776. credits+=combine_amount;
  777. combine_item[@"amount"]=[NSString stringWithFormat:@"%.2f",combine_amount];
  778. combine_item[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,c+2];
  779. row1[[NSString stringWithFormat:@"item_%d", row1count] ]=combine_item;
  780. row1count++;
  781. }
  782. count++;
  783. // DebugLog(@"row time interval");
  784. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  785. }
  786. row1[@"count"]=[NSNumber numberWithInt:row1count];
  787. datasource[@"row1"]=row1;
  788. data[@"table_model"]=datasource;
  789. sqlite3_finalize(statement_cart);
  790. }
  791. NSMutableDictionary* fee = [[NSMutableDictionary alloc]init];
  792. NSMutableDictionary* fee_row0 = [[NSMutableDictionary alloc]init];
  793. // itemjson[@"ln#"]=[NSString stringWithFormat:@"%d.%d",count+1,1];
  794. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  795. // if(credits>0)
  796. fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": [NSString stringWithFormat:@"%.2f",credits]};
  797. // else
  798. // fee_row0[@"item_0"]=@{@"name":@"Payments/Credits:" ,@"value": @""};
  799. if(shipping>0)
  800. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": [NSString stringWithFormat:@"%.2f",shipping]};
  801. else
  802. fee_row0[@"item_1"]=@{@"name":@"Shipping:" ,@"value": @""};
  803. if(lift_gate_value>0)
  804. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",lift_gate_value]};
  805. else
  806. fee_row0[@"item_2"]=@{@"name":@"Lift Gate Fee:" ,@"value": @""};
  807. if(handling_fee_value>0)
  808. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": [NSString stringWithFormat:@"%.2f",handling_fee_value]};
  809. else
  810. fee_row0[@"item_3"]=@{@"name":@"Handling Fee:" ,@"value": @""};
  811. float total = credits+shipping+lift_gate_value+handling_fee_value;
  812. if(total>0)
  813. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": [NSString stringWithFormat:@"%.2f",total]};
  814. else
  815. fee_row0[@"item_4"]=@{@"name":@"Total:" ,@"value": @""};
  816. fee_row0[@"count"]=[NSNumber numberWithInt:5];
  817. fee[@"row0"]=fee_row0;
  818. data[@"table_fee"]=fee;
  819. // NSString *sign_url = [self textAtColumn:51 statement:statement];
  820. // ret[@"sign_url"] = sign_url;
  821. //
  822. // customer_contact = [self textAtColumn:52 statement:statement];
  823. // customer_email = [self textAtColumn:53 statement:statement];
  824. // customer_phone = [self textAtColumn:54 statement:statement];
  825. // customer_fax = [self textAtColumn:55 statement:statement];
  826. //
  827. //
  828. // int offline_edit=sqlite3_column_int(statement, 56);
  829. //
  830. // ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  831. //
  832. // char *soid = (char*)sqlite3_column_text(statement, 1);
  833. // if(soid==nil)
  834. // soid= "";
  835. // nssoid= [[NSString alloc]initWithUTF8String:soid];
  836. // // so#
  837. // ret[@"so#"] = nssoid;
  838. //
  839. // char *poNumber = (char*)sqlite3_column_text(statement, 2);
  840. // if(poNumber==nil)
  841. // poNumber= "";
  842. // NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  843. //
  844. //
  845. //
  846. // char *create_time = (char*)sqlite3_column_text(statement, 3);
  847. // if(create_time==nil)
  848. // create_time= "";
  849. // NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  850. // nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  851. //
  852. // int status = sqlite3_column_int(statement, 4);
  853. // int erpStatus = sqlite3_column_int(statement, 49);
  854. // NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  855. //
  856. // // status
  857. // if (status > 1 && status != 3) {
  858. // status = erpStatus;
  859. // } else if (status == 3) {
  860. // status = 15;
  861. // }
  862. // ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  863. // ret[@"order_status"] = nsstatus;
  864. //
  865. //
  866. // char *company_name = (char*)sqlite3_column_text(statement, 5);
  867. // if(company_name==nil)
  868. // company_name= "";
  869. // NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  870. // // company name
  871. // ret[@"company_name"] = nscompany_name;
  872. //
  873. // char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  874. // if(customer_contact==nil)
  875. // customer_contact= "";
  876. // NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  877. //
  878. //
  879. // char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  880. // if(addr_1==nil)
  881. // addr_1="";
  882. // NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  883. //
  884. // char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  885. // if(addr_2==nil)
  886. // addr_2="";
  887. // NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  888. //
  889. //
  890. // char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  891. // if(addr_3==nil)
  892. // addr_3="";
  893. // NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  894. //
  895. //
  896. // char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  897. // if(addr_4==nil)
  898. // addr_4="";
  899. // NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  900. //
  901. // NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  902. // [arr_addr addObject:nsaddr_1];
  903. // [arr_addr addObject:nsaddr_2];
  904. // [arr_addr addObject:nsaddr_3];
  905. // [arr_addr addObject:nsaddr_4];
  906. // NSString * customer_address = [RAUtils arr2string:arr_addr separator:@"<br>" trim:true];
  907. //
  908. //
  909. // char *logist = (char*)sqlite3_column_text(statement, 11);
  910. // if(logist==nil)
  911. // logist= "";
  912. // NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  913. // if (status == -11 || status == 10 || status == 11) {
  914. // nslogist = [self textAtColumn:59 statement:statement];
  915. // };
  916. //
  917. // NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  918. // if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  919. // shipping = @"Shipping To Be Quoted";
  920. // } else {
  921. // shippingFee = sqlite3_column_double(statement, 12);
  922. // }
  923. //
  924. // // Shipping
  925. // ret[@"Shipping"] = shipping;
  926. //
  927. // int have_lift_gate = sqlite3_column_int(statement, 17);
  928. // lift_gate = sqlite3_column_double(statement, 13);
  929. // // Liftgate Fee(No loading dock)
  930. // if (!have_lift_gate) {
  931. // lift_gate = 0;
  932. // }
  933. // ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  934. // if (sqlite3_column_int(statement, 57)) {
  935. // ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  936. // }
  937. //
  938. // char *general_notes = (char*)sqlite3_column_text(statement, 14);
  939. // if(general_notes==nil)
  940. // general_notes= "";
  941. // NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  942. //
  943. // char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  944. // if(internal_notes==nil)
  945. // internal_notes= "";
  946. // NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  947. //
  948. // char *payment_type = (char*)sqlite3_column_text(statement, 16);
  949. // if(payment_type==nil)
  950. // payment_type= "";
  951. // NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  952. //
  953. //
  954. //
  955. // // order info
  956. // orderinfo = [self textFileName:@"order_info.html"];
  957. //
  958. //
  959. // orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  960. // orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  961. // orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  962. // orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  963. //
  964. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  965. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  966. // orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  967. // orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  968. // orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  969. // orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  970. // orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  971. // orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  972. //
  973. // NSString *payment = nil;
  974. // if([nspayment_type isEqualToString:@"Credit Card"])
  975. // {
  976. // payment = [self textFileName:@"creditcardpayment.html"];
  977. //
  978. // NSString *card_first_name = [self textAtColumn:37 statement:statement];
  979. // NSString *card_last_name = [self textAtColumn:38 statement:statement];
  980. // NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  981. // NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  982. // NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  983. // NSString *card_type = [self textAtColumn:42 statement:statement];
  984. // if (card_type.length > 0) { // 显示星号
  985. // card_type = @"****";
  986. // }
  987. // NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  988. // if (card_number.length > 0 && card_number.length > 4) {
  989. // for (int i = 0; i < card_number.length - 4; i++) {
  990. // card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  991. // }
  992. // } else {
  993. // card_number = @"";
  994. // }
  995. // NSString *card_security_code = [self textAtColumn:44 statement:statement];
  996. // NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  997. // NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  998. // NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  999. // if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  1000. // card_expiration = @"****";
  1001. // }
  1002. //
  1003. //
  1004. // NSString *card_city = [self textAtColumn:46 statement:statement];
  1005. // NSString *card_state = [self textAtColumn:47 statement:statement];
  1006. //
  1007. // payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  1008. // payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  1009. // payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  1010. // payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  1011. // payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  1012. // payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  1013. // payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  1014. // payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  1015. // payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  1016. // payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  1017. //
  1018. //
  1019. // }
  1020. // else
  1021. // {
  1022. // payment=[self textFileName:@"normalpayment.html"];
  1023. // payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  1024. // }
  1025. // orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  1026. //
  1027. // ret[@"result"]= [NSNumber numberWithInt:2];
  1028. //
  1029. // // more info
  1030. // moreInfo = [self textFileName:@"more_info.html"];
  1031. //
  1032. //
  1033. // // ShipToCompany_or_&nbsp
  1034. // NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  1035. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  1036. // NSString *shipToName = [self textAtColumn:19 statement:statement];
  1037. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  1038. // NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  1039. // moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  1040. //
  1041. //
  1042. // // ShipFromCompany_or_&nbsp
  1043. // NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  1044. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  1045. // NSString *shipFromName = [self textAtColumn:22 statement:statement];
  1046. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  1047. // NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  1048. // moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  1049. //
  1050. //
  1051. // // FreightBillToCompany_or_&nbsp
  1052. // NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  1053. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  1054. // NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  1055. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  1056. // NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  1057. // moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  1058. //
  1059. //
  1060. // // MerchandiseBillToCompany_or_&nbsp
  1061. // NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  1062. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  1063. // NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  1064. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  1065. // NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  1066. // moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  1067. //
  1068. //
  1069. // // ReturnToCompany_or_&nbsp
  1070. // NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  1071. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  1072. // NSString *returnToName = [self textAtColumn:31 statement:statement];
  1073. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  1074. // NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  1075. // moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  1076. // //
  1077. //
  1078. // // DebugLog(@"more info : %@",moreInfo);
  1079. //
  1080. // // handling fee
  1081. // handlingFee = sqlite3_column_double(statement, 33);
  1082. // ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  1083. // if (sqlite3_column_int(statement, 58)) {
  1084. // ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  1085. // }
  1086. // //
  1087. //
  1088. // // customer info
  1089. // customerID = [self textAtColumn:36 statement:statement];
  1090. //
  1091. // // mode
  1092. // ret[@"mode"] = appDelegate.mode;
  1093. //
  1094. // // model_count
  1095. // ret[@"model_count"] = @(0);
  1096. }
  1097. sqlite3_finalize(statement);
  1098. }
  1099. [iSalesDB close_db:db];
  1100. data[@"order_type"]=order_type;
  1101. /*
  1102. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  1103. // "customer_email" = "Shui Hu";
  1104. // "customer_fax" = "";
  1105. // "customer_first_name" = F;
  1106. // "customer_last_name" = L;
  1107. // "customer_name" = ",da He Xiang Dong Liu A";
  1108. // "customer_phone" = "Hey Xuan Feng";
  1109. contactInfo[@"customer_phone"] = customer_phone;
  1110. contactInfo[@"customer_fax"] = customer_fax;
  1111. contactInfo[@"customer_email"] = customer_email;
  1112. NSString *first_name = @"";
  1113. NSString *last_name = @"";
  1114. if ([customer_contact isEqualToString:@""]) {
  1115. } else if ([customer_contact containsString:@" "]) {
  1116. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  1117. first_name = [customer_contact substringToIndex:first_space_index];
  1118. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  1119. }
  1120. contactInfo[@"customer_first_name"] = first_name;
  1121. contactInfo[@"customer_last_name"] = last_name;
  1122. ret[@"customerInfo"] = contactInfo;
  1123. // models
  1124. if (nssoid) {
  1125. __block double TotalCuft = 0;
  1126. __block double TotalWeight = 0;
  1127. __block int TotalCarton = 0;
  1128. __block double allItemPrice = 0;
  1129. 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];
  1130. sqlite3 *db1 = [iSalesDB get_db];
  1131. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  1132. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1133. int product_id = sqlite3_column_int(stmt, 0);
  1134. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  1135. int item_id = sqlite3_column_int(stmt, 7);
  1136. NSString* Price=nil;
  1137. if(str_price==nil)
  1138. {
  1139. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db1];
  1140. if(price==nil)
  1141. Price=@"No Price.";
  1142. else
  1143. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1144. }
  1145. else
  1146. {
  1147. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1148. }
  1149. double discount = sqlite3_column_double(stmt, 2);
  1150. int item_count = sqlite3_column_int(stmt, 3);
  1151. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  1152. NSString *nsline_note=nil;
  1153. if(line_note!=nil)
  1154. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1155. // char *name = (char*)sqlite3_column_text(stmt, 5);
  1156. // NSString *nsname = nil;
  1157. // if(name!=nil)
  1158. // nsname= [[NSString alloc]initWithUTF8String:name];
  1159. NSString *nsname = [self textAtColumn:5 statement:stmt];
  1160. // char *description = (char*)sqlite3_column_text(stmt, 6);
  1161. // NSString *nsdescription=nil;
  1162. // if(description!=nil)
  1163. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  1164. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  1165. // int stockUom = sqlite3_column_int(stmt, 8);
  1166. // int _id = sqlite3_column_int(stmt, 9);
  1167. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  1168. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1169. double weight=[bsubtotaljson[@"weight"] doubleValue];
  1170. int carton=[bsubtotaljson[@"carton"] intValue];
  1171. TotalCuft += cuft;
  1172. TotalWeight += weight;
  1173. TotalCarton += carton;
  1174. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false];
  1175. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  1176. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  1177. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1178. itemjson[@"note"]=nsline_note;
  1179. itemjson[@"origin_price"] = Price;
  1180. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1181. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  1182. itemjson[@"order_item_status"] = @""; // 暂时不处理
  1183. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  1184. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  1185. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  1186. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  1187. if(itemjson[@"combine"] != nil)
  1188. {
  1189. // int citem=0;
  1190. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  1191. for(int bc=0;bc<bcount;bc++)
  1192. {
  1193. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  1194. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  1195. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  1196. subTotal += uprice * modulus * item_count;
  1197. }
  1198. }
  1199. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  1200. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  1201. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  1202. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  1203. allItemPrice += subTotal;
  1204. }];
  1205. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  1206. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  1207. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  1208. // payments/Credits
  1209. // payments_and_credist = sqlite3_column_double(statement, 34);
  1210. payments_and_credist = allItemPrice;
  1211. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1212. // // total
  1213. // totalPrice = sqlite3_column_double(statement, 35);
  1214. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1215. } else {
  1216. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  1217. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  1218. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  1219. // payments/Credits
  1220. payments_and_credist = 0;
  1221. ret[@"Payments/Credits"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  1222. }
  1223. // total
  1224. totalPrice = payments_and_credist + lift_gate + handlingFee + shippingFee;
  1225. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  1226. ret[@"order_info"]= orderinfo;
  1227. ret[@"more_order_info"] = moreInfo;
  1228. return [RAUtils dict2data:ret];
  1229. */
  1230. DebugLog(@"debug sales order data:%@", [RAUtils dict2string:data]);
  1231. return data;
  1232. }
  1233. +(NSData*) offline_request_salesorder :(NSMutableDictionary *) params
  1234. {
  1235. NSString* template_file=nil;
  1236. switch([params[@"thumb"] intValue])
  1237. {
  1238. case 0:
  1239. template_file= @"so.json";
  1240. break;
  1241. case 1:
  1242. template_file= @"so_thumb.json";
  1243. break;
  1244. default:
  1245. template_file= @"so.json";
  1246. }
  1247. NSMutableDictionary* data = [self prepare_salesorder_data:params[@"order_code"] type:[params[@"thumb"] intValue]];
  1248. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1249. if(data[@"table_model"][@"row2"]==nil)
  1250. template[@"pages"][@"page_0"][@"contents"][@"item_0"][@"rows"][@"count"]=[NSNumber numberWithInt:1];
  1251. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1252. DebugLog(@"%@",file);
  1253. // return nil;
  1254. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1255. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1256. if (file) {
  1257. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1258. [dic setObject:file forKey:@"pdf_path"];
  1259. dic[@"isLocalFile"]=@"true";
  1260. return [RAUtils dict2data:dic];
  1261. }
  1262. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1263. dic[@"isLocalFile"]=@"true";
  1264. return [RAUtils dict2data:dic];
  1265. }
  1266. +(NSData*) offline_request_tearsheet :(NSMutableDictionary *) params
  1267. {
  1268. NSString* template_file=nil;
  1269. switch([params[@"pdf_style"] intValue])
  1270. {
  1271. case 0:
  1272. template_file= @"portfolio_2x3.json";
  1273. break;
  1274. case 1:
  1275. template_file= @"portfolio_3x2.json";
  1276. break;
  1277. default:
  1278. template_file= @"portfolio_3x2.json";
  1279. }
  1280. NSMutableDictionary* data = [self prepare_portfolio_data:params];
  1281. NSMutableDictionary* template = [self get_pdftemplate:template_file];
  1282. NSString* file=[pdfCreator CreatePdf: template dataSource:data];
  1283. DebugLog(@"%@",file);
  1284. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  1285. [dic setObject:@"Regular Mode" forKey:@"mode"];
  1286. if (file) {
  1287. [dic setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  1288. [dic setObject:file forKey:@"pdf_path"];
  1289. dic[@"isLocalFile"]=@"true";
  1290. return [RAUtils dict2data:dic];
  1291. }
  1292. [dic setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  1293. dic[@"isLocalFile"]=@"true";
  1294. return [RAUtils dict2data:dic];
  1295. }
  1296. +(NSString*) get_offline_soid:(sqlite3*)db
  1297. {
  1298. NSString* soid=nil;
  1299. NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
  1300. [formatter setDateFormat:@"yyMMdd"];
  1301. NSString* date = [formatter stringFromDate:[NSDate date]];
  1302. for(int i=1;i<999;i++)
  1303. {
  1304. soid=[NSString stringWithFormat:@"OFF%@%4d",date,i];
  1305. soid= [soid stringByReplacingOccurrencesOfString:@" " withString:@"0"];
  1306. int count=[iSalesDB get_recordcount:db table:@"offline_order" where:[NSString stringWithFormat:@"so_id='%@'",soid]];
  1307. if(count==0)
  1308. return soid;
  1309. }
  1310. return nil;
  1311. }
  1312. +(NSArray*) enumOfflineOrder
  1313. {
  1314. NSMutableArray* ret = [[NSMutableArray alloc] init];
  1315. sqlite3 *db = [iSalesDB get_db];
  1316. NSString *sqlQuery =@"select so_id from offline_order where status=-11;";
  1317. sqlite3_stmt * statement;
  1318. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1319. if ( dbresult== SQLITE_OK)
  1320. {
  1321. while (sqlite3_step(statement) == SQLITE_ROW)
  1322. {
  1323. char *so_id = (char*)sqlite3_column_text(statement, 0);
  1324. NSString* nsso_id=nil;
  1325. if(so_id!=nil)
  1326. {
  1327. nsso_id= [[NSString alloc]initWithUTF8String:so_id];
  1328. }
  1329. [ret addObject:nsso_id];
  1330. }
  1331. sqlite3_finalize(statement);
  1332. }
  1333. [iSalesDB close_db:db];
  1334. return ret;
  1335. }
  1336. //+(void) uploadFile:(NSString*) file
  1337. //{
  1338. // NSData* data = [NSData dataWithContentsOfFile: file];
  1339. // UIApplication * app = [UIApplication sharedApplication];
  1340. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1341. //
  1342. //
  1343. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  1344. //
  1345. //
  1346. //
  1347. // NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  1348. //
  1349. // if(appDelegate.user!=nil)
  1350. // [params setValue:appDelegate.user forKey:@"user"];
  1351. // // if(appDelegate.contact_id!=nil)
  1352. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  1353. // if(appDelegate.password!=nil)
  1354. // [params setValue:appDelegate.password forKey:@"password"];
  1355. //
  1356. //
  1357. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_OFFLINE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  1358. // [formData appendPartWithFileData:data name:@"upfile" fileName:[file lastPathComponent] mimeType:@"application/zip"];
  1359. // } error:nil];
  1360. //
  1361. //
  1362. //
  1363. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  1364. //
  1365. // NSProgress *progress = nil;
  1366. //
  1367. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1368. //
  1369. //
  1370. //
  1371. // if (error) {
  1372. //
  1373. // NSString* err_msg = [error localizedDescription];
  1374. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  1375. //
  1376. //
  1377. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1378. //
  1379. // DebugLog(@"data string: %@",str);
  1380. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  1381. //
  1382. // } else {
  1383. // DebugLog(@"response ");
  1384. //
  1385. //
  1386. //
  1387. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  1388. //
  1389. // // 再将NSData转为字符串
  1390. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  1391. //
  1392. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  1393. //
  1394. // DebugLog(@"data string: %@",jsonStr);
  1395. //
  1396. // NSDictionary* json = responseObject;
  1397. //
  1398. //
  1399. // if([[json valueForKey:@"result"] intValue]==2)
  1400. // {
  1401. //// NSString* img_url_down = json[@"img_url_aname"];
  1402. //// NSString* img_url_up = json[@"img_url"];
  1403. //
  1404. //
  1405. // }
  1406. // else
  1407. // {
  1408. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload file" controller:nil] ;
  1409. // }
  1410. //
  1411. //
  1412. // }
  1413. // // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  1414. // }];
  1415. //
  1416. //
  1417. //
  1418. //
  1419. // // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  1420. // //
  1421. //
  1422. //
  1423. //
  1424. //
  1425. //
  1426. //
  1427. // [uploadTask resume];
  1428. //
  1429. //}
  1430. +(NSDictionary *) prepareUpload:(NSArray*) arr_order
  1431. {
  1432. NSString* serial= [[NSUUID UUID] UUIDString];
  1433. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1434. NSString *cachefolder = [paths objectAtIndex:0];
  1435. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1436. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1437. NSFileManager* fileManager = [NSFileManager defaultManager];
  1438. BOOL bdir=YES;
  1439. if(! [fileManager fileExistsAtPath:upfolder isDirectory:&bdir])
  1440. {
  1441. NSError *error = nil;
  1442. bool bsuccess=[fileManager createDirectoryAtPath:upfolder withIntermediateDirectories:YES attributes:nil error:&error];
  1443. if(!bsuccess)
  1444. DebugLog(@"Create UPLOAD folder failed");
  1445. }
  1446. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1447. ret[@"contact"]=[self prepareContact:serial];
  1448. ret[@"wishlist"]=[self prepareWishlist:serial];
  1449. ret[@"order"]=[self prepareOrder:serial soid:arr_order];
  1450. ret[@"portfolio"] = [self preparePortfolio:serial];
  1451. ret[@"view_portfolio"] = [self preparePDF:serial];
  1452. NSString* str= [RAUtils dict2string:ret];
  1453. NSString * jsonpath= [upfolder stringByAppendingPathComponent:@"json.json"];
  1454. NSString * zippath= [upfolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",serial]];
  1455. NSError *error=nil;
  1456. [str writeToFile:jsonpath atomically:true encoding:NSUTF8StringEncoding error:&error];
  1457. NSArray* arr_files=[RAUtils allFilesAtPath:upfolder];
  1458. ZipArchive* zip = [[ZipArchive alloc] init];
  1459. BOOL result = [zip CreateZipFile2:zippath Password:@"usai2010"];
  1460. for(int i=0;i<arr_files.count;i++)
  1461. {
  1462. NSString* file=arr_files[i];
  1463. result = [zip addFileToZip:file newname:[file lastPathComponent]];
  1464. }
  1465. if( ![zip CloseZipFile2] )
  1466. {
  1467. zippath = @"";
  1468. }
  1469. ret[@"file"]=zippath;
  1470. return ret;
  1471. }
  1472. + (bool) copy_upImg:(NSString* ) serial file:(NSString*) filename
  1473. {
  1474. if(filename.length==0)
  1475. return false;
  1476. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1477. bool ret=false;
  1478. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1479. NSString *cachefolder = [paths objectAtIndex:0];
  1480. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  1481. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  1482. NSString *upfolder = [cachefolder stringByAppendingPathComponent:@"upload_data"];
  1483. upfolder = [upfolder stringByAppendingPathComponent:serial];
  1484. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  1485. NSString * toPath= [upfolder stringByAppendingPathComponent:filename];
  1486. NSFileManager* fileManager = [NSFileManager defaultManager];
  1487. BOOL bdir=NO;
  1488. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  1489. {
  1490. NSError *error = nil;
  1491. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  1492. {
  1493. ret=false;
  1494. }
  1495. else
  1496. {
  1497. ret=true;
  1498. }
  1499. }
  1500. return ret;
  1501. }
  1502. +(NSMutableDictionary*) prepareWishlist:(NSString* ) serial
  1503. {
  1504. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1505. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  1506. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  1507. NSString* where=@" 1=1";
  1508. if (ver!=nil) {
  1509. where=@"is_dirty=1";
  1510. }
  1511. sqlite3 *db = [iSalesDB get_db];
  1512. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,item_id,qty,create_time,modify_time,is_delete from wishlist where %@",where];
  1513. sqlite3_stmt * statement;
  1514. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1515. int count=0;
  1516. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1517. if ( dbresult== SQLITE_OK)
  1518. {
  1519. while (sqlite3_step(statement) == SQLITE_ROW)
  1520. {
  1521. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1522. int _id = sqlite3_column_int(statement, 0);
  1523. int product_id = sqlite3_column_int(statement, 1);
  1524. int item_id = sqlite3_column_int(statement, 2);
  1525. int qty = sqlite3_column_int(statement, 3);
  1526. char *create_time = (char*)sqlite3_column_text(statement, 4);
  1527. NSString* nscreate_time=nil;
  1528. if(create_time!=nil)
  1529. {
  1530. nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  1531. }
  1532. char *modify_time = (char*)sqlite3_column_text(statement, 5);
  1533. NSString* nsmodify_time=nil;
  1534. if(modify_time!=nil)
  1535. {
  1536. nsmodify_time= [[NSString alloc]initWithUTF8String:modify_time];
  1537. }
  1538. int is_delete = sqlite3_column_int(statement, 6);
  1539. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1540. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id];
  1541. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1542. itemjson[@"qty"]=[NSNumber numberWithInt:qty];
  1543. itemjson[@"create_time"]=nscreate_time;
  1544. itemjson[@"modify_time"]=nsmodify_time;
  1545. itemjson[@"is_delete"]=[NSNumber numberWithInt:is_delete];
  1546. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1547. count++;
  1548. }
  1549. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1550. sqlite3_finalize(statement);
  1551. }
  1552. ret[@"count"]=[NSNumber numberWithInt:count ];
  1553. [iSalesDB close_db:db];
  1554. return ret;
  1555. }
  1556. +(NSMutableDictionary*) preparecart:(NSString* ) orderCode db:(sqlite3*)db
  1557. {
  1558. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1559. UIApplication * app = [UIApplication sharedApplication];
  1560. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1561. 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,o.customer_cid from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,so_no from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id left join offline_order as o on c.so_no = o.so_id;",orderCode ];
  1562. // 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 ];
  1563. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  1564. DebugLog(@"offline_login sql:%@",sqlQuery);
  1565. sqlite3_stmt * statement;
  1566. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1567. int count=0;
  1568. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1569. if ( dbresult== SQLITE_OK)
  1570. {
  1571. while (sqlite3_step(statement) == SQLITE_ROW)
  1572. {
  1573. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1574. char *customer_cid = (char *)sqlite3_column_text(statement, 10);
  1575. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  1576. int product_id = sqlite3_column_int(statement, 0);
  1577. char *str_price = (char*)sqlite3_column_text(statement, 1);
  1578. int item_id = sqlite3_column_int(statement, 7);
  1579. NSString* Price=nil;
  1580. if(str_price==nil)
  1581. {
  1582. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  1583. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db];
  1584. if(price==nil)
  1585. Price=@"No Price.";
  1586. else
  1587. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  1588. }
  1589. else
  1590. {
  1591. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  1592. }
  1593. double discount = sqlite3_column_double(statement, 2);
  1594. int item_count = sqlite3_column_int(statement, 3);
  1595. char *line_note = (char*)sqlite3_column_text(statement, 4);
  1596. NSString *nsline_note=nil;
  1597. if(line_note!=nil)
  1598. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  1599. char *name = (char*)sqlite3_column_text(statement, 5);
  1600. NSString *nsname=nil;
  1601. if(name!=nil)
  1602. nsname= [[NSString alloc]initWithUTF8String:name];
  1603. char *description = (char*)sqlite3_column_text(statement, 6);
  1604. NSString *nsdescription=nil;
  1605. if(description!=nil)
  1606. nsdescription= [[NSString alloc]initWithUTF8String:description];
  1607. // int stockUom = sqlite3_column_int(statement, 8);
  1608. // int _id = sqlite3_column_int(statement, 9);
  1609. //
  1610. //
  1611. // NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  1612. // double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  1613. // double weight=[bsubtotaljson[@"weight"] doubleValue];
  1614. // int carton=[bsubtotaljson[@"carton"] intValue];
  1615. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1616. // itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  1617. itemjson[@"model"]=nsname;
  1618. // itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  1619. // itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  1620. // itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  1621. // itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  1622. // itemjson[@"check"]=@"true";
  1623. itemjson[@"item_id"]=[NSNumber numberWithInt:item_id];
  1624. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  1625. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  1626. itemjson[@"unit_price"]=Price;
  1627. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  1628. // itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  1629. // itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  1630. itemjson[@"note"]=nsline_note;
  1631. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:customerID];
  1632. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1633. count++;
  1634. }
  1635. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1636. sqlite3_finalize(statement);
  1637. }
  1638. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  1639. ret[@"count"]=[NSNumber numberWithInt:cart_count ];
  1640. return ret;
  1641. }
  1642. +(NSMutableDictionary*) prepareOrder:(NSString* ) serial soid:(NSArray*) arr_soid
  1643. {
  1644. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1645. sqlite3 *db = [iSalesDB get_db];
  1646. NSString *sqlQuery =@"select _id,sync_data,sign_picpath,so_id from offline_order where order_id is null";
  1647. // for(int i=0;i<arr_soid.count;i++)
  1648. sqlite3_stmt * statement;
  1649. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1650. //int count=0;
  1651. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1652. if ( dbresult== SQLITE_OK)
  1653. {
  1654. int count=0;
  1655. while (sqlite3_step(statement) == SQLITE_ROW)
  1656. {
  1657. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1658. int _id = sqlite3_column_int(statement, 0);
  1659. char *sync_data = (char*)sqlite3_column_text(statement, 1);
  1660. NSString* nssync_data=nil;
  1661. if(sync_data!=nil)
  1662. {
  1663. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1664. }
  1665. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1666. NSString* nsimg_1=nil;
  1667. if(img_1!=nil)
  1668. {
  1669. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1670. [self copy_upImg:serial file:nsimg_1];
  1671. }
  1672. char *so_no = (char*)sqlite3_column_text(statement, 3);
  1673. NSString* nsso_no=nil;
  1674. if(so_no!=nil)
  1675. {
  1676. nsso_no= [[NSString alloc]initWithUTF8String:so_no];
  1677. }
  1678. if([arr_soid indexOfObject:nsso_no]!= NSNotFound)
  1679. itemjson[@"order_type"]=@"submit order";
  1680. else
  1681. itemjson[@"order_type"]=@"archive order";
  1682. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1683. itemjson[@"json_data"]= nssync_data;
  1684. itemjson[@"order_items"]=[self preparecart:nsso_no db:db];
  1685. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1686. count++;
  1687. }
  1688. ret[@"count"]=[NSNumber numberWithInt:count ];
  1689. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1690. }
  1691. sqlite3_finalize(statement);
  1692. [iSalesDB close_db:db];
  1693. return ret;
  1694. }
  1695. +(NSMutableDictionary*) prepareContact:(NSString* ) serial
  1696. {
  1697. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1698. sqlite3 *db = [iSalesDB get_db];
  1699. // UIApplication * app = [UIApplication sharedApplication];
  1700. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1701. 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";
  1702. sqlite3_stmt * statement;
  1703. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  1704. int count=0;
  1705. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  1706. if ( dbresult== SQLITE_OK)
  1707. {
  1708. while (sqlite3_step(statement) == SQLITE_ROW)
  1709. {
  1710. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  1711. int _id = sqlite3_column_int(statement, 0);
  1712. char *img_0 = (char*)sqlite3_column_text(statement, 1);
  1713. NSString* nsimg_0=nil;
  1714. if(img_0!=nil)
  1715. {
  1716. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  1717. [self copy_upImg:serial file:nsimg_0];
  1718. }
  1719. char *img_1 = (char*)sqlite3_column_text(statement, 2);
  1720. NSString* nsimg_1=nil;
  1721. if(img_1!=nil)
  1722. {
  1723. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  1724. [self copy_upImg:serial file:nsimg_1];
  1725. }
  1726. char *img_2 = (char*)sqlite3_column_text(statement, 3);
  1727. NSString* nsimg_2=nil;
  1728. if(img_2!=nil)
  1729. {
  1730. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  1731. [self copy_upImg:serial file:nsimg_2];
  1732. }
  1733. char *sync_data = (char*)sqlite3_column_text(statement, 4);
  1734. NSString* nssync_data=nil;
  1735. if(sync_data!=nil)
  1736. nssync_data= [[NSString alloc]initWithUTF8String:sync_data];
  1737. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  1738. itemjson[@"_id"]=[NSNumber numberWithInt:_id];
  1739. itemjson[@"json_data"]= nssync_data;
  1740. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  1741. count++;
  1742. }
  1743. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  1744. sqlite3_finalize(statement);
  1745. }
  1746. ret[@"count"]=[NSNumber numberWithInt:count ];
  1747. [iSalesDB close_db:db];
  1748. return ret;
  1749. }
  1750. +(bool) check_offlinedata
  1751. {
  1752. UIApplication * app = [UIApplication sharedApplication];
  1753. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1754. sqlite3 *db = [iSalesDB get_db];
  1755. NSString * where=@"1=1";
  1756. // if(appDelegate.user!=nil)
  1757. // where=[NSString stringWithFormat:@"lower(username)='%@'",appDelegate.user.lowercaseString];
  1758. int count = [iSalesDB get_recordcount:db table:@"offline_login" where:where];
  1759. [iSalesDB close_db:db];
  1760. if(count==0)
  1761. {
  1762. return false;
  1763. }
  1764. return true;
  1765. //
  1766. //[iSalesDB close_db:db];
  1767. }
  1768. +(NSDictionary*) offline_deletewishlist :(NSMutableDictionary *) params
  1769. {
  1770. UIApplication * app = [UIApplication sharedApplication];
  1771. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1772. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1773. sqlite3 *db = [iSalesDB get_db];
  1774. NSString* collectId=params[@"collectId"];
  1775. // NSString* sqlQuery = [NSString stringWithFormat:@"delete from wishlist where _id in (%@);",collectId];
  1776. NSString* sqlQuery = [NSString stringWithFormat:@"update wishlist set is_delete = 1 where _id in (%@);",collectId];
  1777. [iSalesDB execSql:sqlQuery db:db];
  1778. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  1779. [iSalesDB close_db:db];
  1780. appDelegate.wish_count =count;
  1781. [appDelegate update_count_mark];
  1782. ret[@"result"]= [NSNumber numberWithInt:2];
  1783. return ret;
  1784. }
  1785. //+(NSDictionary*) offline_add2wishlist :(NSMutableDictionary *) params
  1786. //{
  1787. //
  1788. // UIApplication * app = [UIApplication sharedApplication];
  1789. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1790. //
  1791. // NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1792. // sqlite3 *db = [iSalesDB get_db];
  1793. // NSString* product_id=params[@"product_id"];
  1794. //
  1795. //
  1796. // NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  1797. //
  1798. //
  1799. // for(int i=0;i<arr.count;i++)
  1800. // {
  1801. // NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  1802. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  1803. // if(count==0)
  1804. // {
  1805. // NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id) values('%@');",arr[i]];
  1806. // [iSalesDB execSql:sqlQuery db:db];
  1807. // }
  1808. // }
  1809. // int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  1810. // [iSalesDB close_db:db];
  1811. //
  1812. // appDelegate.wish_count =count;
  1813. //
  1814. // [appDelegate update_count_mark];
  1815. // ret[@"result"]= [NSNumber numberWithInt:2];
  1816. // return ret;
  1817. // //
  1818. // //return ret;
  1819. //}
  1820. + (void) printTimeIntervalBetween:(NSDate *)date1 and:(NSDate *)date2 {
  1821. NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
  1822. DebugLog(@"time interval: %lf",interval);
  1823. }
  1824. +(NSDictionary*) offline_wishlist :(NSMutableDictionary *) params
  1825. {
  1826. UIApplication * app = [UIApplication sharedApplication];
  1827. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1828. int sort = [[params objectForKey:@"sort"] intValue];
  1829. NSString *sort_str = @"";
  1830. switch (sort) {
  1831. case 0:{
  1832. sort_str = @"order by w.modify_time desc";
  1833. }
  1834. break;
  1835. case 1:{
  1836. sort_str = @"order by w.modify_time asc";
  1837. }
  1838. break;
  1839. case 2:{
  1840. sort_str = @"order by m.name asc";
  1841. }
  1842. break;
  1843. case 3:{
  1844. sort_str = @"order by m.name desc";
  1845. }
  1846. break;
  1847. case 4:{
  1848. sort_str = @"order by m.description asc";
  1849. }
  1850. break;
  1851. default:
  1852. break;
  1853. }
  1854. NSString* user = appDelegate.user;
  1855. sqlite3 *db = [iSalesDB get_db];
  1856. // order by w.create_time
  1857. NSString* sqlQuery = [NSString stringWithFormat:@"select w.product_id,m.name||'\n'||m.description,w._id,w.qty from wishlist as w left join product as m on w.product_id=m.product_id where w.is_delete is NULL or w.is_delete != 1 %@ ",sort_str];
  1858. // NSString* sqlQuery = [NSString stringWithFormat:@"select m.product_id,(m.name||'\n'||m.description) as des,w._id from product as m,wishlist as w where m.product_id = w.product_id %@",sort_str];
  1859. sqlite3_stmt * statement;
  1860. NSDate *date1 = [NSDate date];
  1861. NSDate *date2 = nil;
  1862. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1863. int count=0;
  1864. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1865. {
  1866. while (sqlite3_step(statement) == SQLITE_ROW)
  1867. {
  1868. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1869. int product_id = sqlite3_column_double(statement, 0);
  1870. char *description = (char*)sqlite3_column_text(statement, 1);
  1871. if(description==nil)
  1872. description= "";
  1873. NSString *nsdescription= [[NSString alloc]initWithUTF8String:description];
  1874. int item_id = sqlite3_column_double(statement, 2);
  1875. NSDate *date_image = [NSDate date];
  1876. NSString *nsurl=[self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:product_id]] model_name:nil db:db];
  1877. printf("image : ");
  1878. [self printTimeIntervalBetween:date_image and:[NSDate date]];
  1879. // char *url = (char*)sqlite3_column_text(statement, 3);
  1880. // if(url==nil)
  1881. // url="";
  1882. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  1883. int qty = sqlite3_column_int(statement, 3);
  1884. item[@"cart_count"] = [NSNumber numberWithInt:qty];
  1885. item[@"product_id"]= [NSString stringWithFormat:@"%d",product_id];
  1886. item[@"item_id"]= [NSString stringWithFormat:@"%d",item_id];
  1887. item[@"description"]= nsdescription;
  1888. item[@"img"]= nsurl;
  1889. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  1890. count++;
  1891. }
  1892. printf("total time:");
  1893. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  1894. ret[@"count"]= [NSNumber numberWithInt:count];
  1895. ret[@"total_count"]= [NSNumber numberWithInt:count];
  1896. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  1897. ret[@"result"]= [NSNumber numberWithInt:2];
  1898. appDelegate.wish_count =count;
  1899. [appDelegate update_count_mark];
  1900. sqlite3_finalize(statement);
  1901. }
  1902. [iSalesDB close_db:db];
  1903. return ret;
  1904. }
  1905. +(NSDictionary*) offline_notimpl
  1906. {
  1907. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1908. ret[@"result"]=@"8";
  1909. ret[@"err_msg"]=@"offline mode does not support this function.";
  1910. return ret;
  1911. }
  1912. +(NSDictionary*) offline_home
  1913. {
  1914. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1915. NSString *cachefolder = [paths objectAtIndex:0];
  1916. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/home.json"];
  1917. NSData* json =nil;
  1918. json=[NSData dataWithContentsOfFile:img_cache];
  1919. if(json==nil)
  1920. return nil;
  1921. NSError *error=nil;
  1922. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1923. return menu;
  1924. }
  1925. +(NSDictionary*) offline_category_menu
  1926. {
  1927. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  1928. NSString *cachefolder = [paths objectAtIndex:0];
  1929. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/category_menu.json"];
  1930. NSData* json =nil;
  1931. json=[NSData dataWithContentsOfFile:img_cache];
  1932. if(json==nil)
  1933. return nil;
  1934. NSError *error=nil;
  1935. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  1936. return menu;
  1937. }
  1938. +(NSDictionary*) offline_commoneditor_partialrefresh :(NSMutableDictionary *) params
  1939. {
  1940. NSString* offline_command=params[@"offline_Command"];
  1941. NSDictionary* ret=nil;
  1942. if([offline_command isEqualToString:@"model_NIYMAL"])
  1943. {
  1944. NSString* category = params[@"category"];
  1945. ret = [self refresh_model_NIYMAL:category];
  1946. }
  1947. return ret;
  1948. }
  1949. +(NSDictionary*) refresh_model_NIYMAL :(NSString *) category
  1950. {
  1951. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  1952. [ret setValue:@"2" forKey:@"result"];
  1953. [ret setValue:@"Regular Mode" forKey:@"mode"];
  1954. sqlite3* db= [iSalesDB get_db];
  1955. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  1956. [iSalesDB close_db:db];
  1957. [ret setObject:detail1_section forKey:@"detail_1"];
  1958. return ret;
  1959. }
  1960. +(NSMutableDictionary*) get_model_all_price:(NSString*) contact_id item_id:(int) item_id db:(sqlite3*)db
  1961. {
  1962. UIApplication * app = [UIApplication sharedApplication];
  1963. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  1964. // NSArray* arr1 = [self get_user_all_price_type];
  1965. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  1966. // NSSet *set1 = [NSSet setWithArray:arr1];
  1967. // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  1968. // if(appDelegate.contact_id==nil)
  1969. // set2=[set1 mutableCopy];
  1970. // else
  1971. // [set2 intersectsSet:set1];
  1972. // NSArray *retarr = [set2 allObjects];
  1973. NSString* whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  1974. // sqlite3 *db = [iSalesDB get_db];
  1975. NSString* sqlQuery = nil;
  1976. if(appDelegate.contact_id==nil)
  1977. {
  1978. if(!appDelegate.bLogin)
  1979. return nil;
  1980. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type 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];
  1981. }
  1982. else
  1983. sqlQuery=[NSString stringWithFormat:@"select a.name,b.price,b.type 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];
  1984. sqlite3_stmt * statement;
  1985. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  1986. int count=0;
  1987. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  1988. {
  1989. while (sqlite3_step(statement) == SQLITE_ROW)
  1990. {
  1991. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  1992. char *name = (char*)sqlite3_column_text(statement, 0);
  1993. if(name==nil)
  1994. name="";
  1995. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  1996. // double price = sqlite3_column_double(statement, 1);
  1997. // char *isnull = (char*)sqlite3_column_text(statement, 1);
  1998. // if(isnull==nil)
  1999. // item[nsname]= @"No Price";
  2000. // else
  2001. // item[nsname]= [NSString stringWithFormat:@"%.2f",price];
  2002. char *price = (char*)sqlite3_column_text(statement, 1);
  2003. if(price!=nil)
  2004. {
  2005. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2006. nsprice=[AESCrypt fastdecrypt:nsprice];
  2007. if(nsprice.length>0)
  2008. {
  2009. double dp= [nsprice doubleValue];
  2010. item[nsname]= [NSString stringWithFormat:@"%.2f",dp];
  2011. }
  2012. }
  2013. else
  2014. {
  2015. item[nsname]= @"No Price";
  2016. }
  2017. // int type= sqlite3_column_int(statement, 2);
  2018. //item[@"type"]=@"price";
  2019. // item[nsname]= nsprice;
  2020. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  2021. count++;
  2022. }
  2023. ret[@"count"]= [NSNumber numberWithInt:count];
  2024. sqlite3_finalize(statement);
  2025. }
  2026. // [iSalesDB close_db:db];
  2027. return ret;
  2028. }
  2029. //+(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(int) product_id db:(sqlite3*)db
  2030. //{
  2031. // NSArray* arr1 = [self get_user_all_price_type:db];
  2032. // NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2033. //
  2034. // // NSSet *set1 = [NSSet setWithArray:arr1];
  2035. // // NSMutableSet *set2 = [[NSSet setWithArray:arr2] mutableCopy];
  2036. // // [set2 intersectsSet:set1];
  2037. // //
  2038. // //
  2039. // // NSArray *retarr = [set2 allObjects];
  2040. //
  2041. // NSString* whereprice=nil;
  2042. // if(contact_id==nil)
  2043. // whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2044. // else
  2045. // whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2046. //
  2047. //
  2048. // // sqlite3 *db = [iSalesDB get_db];
  2049. //
  2050. // NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where product_id='%d' and price_name in(%@);",product_id,whereprice];
  2051. // sqlite3_stmt * statement;
  2052. //
  2053. //
  2054. // NSNumber* ret = nil;
  2055. // double dprice=DBL_MAX;
  2056. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2057. // {
  2058. //
  2059. //
  2060. // while (sqlite3_step(statement) == SQLITE_ROW)
  2061. // {
  2062. //
  2063. // // double val = sqlite3_column_double(statement, 0);
  2064. // char *price = (char*)sqlite3_column_text(statement, 0);
  2065. // if(price!=nil)
  2066. // {
  2067. // NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2068. // nsprice=[AESCrypt fastdecrypt:nsprice];
  2069. // if(nsprice.length>0)
  2070. // {
  2071. // double dp= [nsprice doubleValue];
  2072. // if(dp<dprice)
  2073. // dprice=dp;
  2074. // }
  2075. // }
  2076. // }
  2077. //
  2078. //
  2079. //
  2080. //
  2081. // sqlite3_finalize(statement);
  2082. //
  2083. //
  2084. //
  2085. //
  2086. // }
  2087. //
  2088. // // [iSalesDB close_db:db];
  2089. //
  2090. // if(dprice==DBL_MAX)
  2091. // ret= nil;
  2092. // else
  2093. // ret= [NSNumber numberWithDouble:dprice];
  2094. // return ret;
  2095. //}
  2096. +(NSNumber*) get_model_default_price:(NSString*) contact_id product_id:(id) product_id item_id:(id)item_id db:(sqlite3*)db
  2097. {
  2098. NSArray* arr1 = [self get_user_all_price_type:db];
  2099. NSArray* arr2 = [self get_contact_default_price_type:contact_id db:db];
  2100. NSString* whereprice=nil;
  2101. if(contact_id==nil)
  2102. whereprice=[RAUtils arr2string:arr1 separator:@"," trim:true brackets:@"'"];
  2103. else
  2104. whereprice=[RAUtils arr2string:arr2 separator:@"," trim:true brackets:@"'"];
  2105. // sqlite3 *db = [iSalesDB get_db];
  2106. NSString *productIdCondition = @"1 = 1";
  2107. if (product_id) {
  2108. productIdCondition = [NSString stringWithFormat:@"product_id = %@",product_id];
  2109. }
  2110. NSString *itemIdCondition = @"";
  2111. if (item_id) {
  2112. itemIdCondition = [NSString stringWithFormat:@"and item_id = %@",item_id];
  2113. }
  2114. NSString* sqlQuery = [NSString stringWithFormat:@"select price from model_price where %@ %@ and price_name in(%@);",productIdCondition,itemIdCondition,whereprice];
  2115. sqlite3_stmt * statement;
  2116. NSNumber* ret = nil;
  2117. double dprice=DBL_MAX;
  2118. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2119. {
  2120. while (sqlite3_step(statement) == SQLITE_ROW)
  2121. {
  2122. // double val = sqlite3_column_double(statement, 0);
  2123. char *price = (char*)sqlite3_column_text(statement, 0);
  2124. if(price!=nil)
  2125. {
  2126. NSString* nsprice = [[NSString alloc]initWithUTF8String:price];
  2127. nsprice=[AESCrypt fastdecrypt:nsprice];
  2128. if(nsprice.length>0)
  2129. {
  2130. double dp= [nsprice doubleValue];
  2131. if(dp<dprice)
  2132. dprice=dp;
  2133. }
  2134. }
  2135. }
  2136. sqlite3_finalize(statement);
  2137. }
  2138. // [iSalesDB close_db:db];
  2139. if(dprice==DBL_MAX)
  2140. ret= nil;
  2141. else
  2142. ret= [NSNumber numberWithDouble:dprice];
  2143. return ret;
  2144. }
  2145. +(NSArray*) get_user_all_price_type:(sqlite3*)db
  2146. {
  2147. NSArray* ret=nil;
  2148. // sqlite3 *db = [iSalesDB get_db];
  2149. // no customer assigned , use login user contact_id
  2150. UIApplication * app = [UIApplication sharedApplication];
  2151. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2152. NSString* sqlQuery = [NSString stringWithFormat:@"select price from offline_login where username='%@';",appDelegate.user];
  2153. sqlite3_stmt * statement;
  2154. // int count=0;
  2155. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2156. {
  2157. if (sqlite3_step(statement) == SQLITE_ROW)
  2158. {
  2159. char *val = (char*)sqlite3_column_text(statement, 0);
  2160. if(val==nil)
  2161. val="";
  2162. NSString* price = [[NSString alloc]initWithUTF8String:val];
  2163. ret=[RAUtils string2arr:price separator:OFFLINE_ARRAY_SEPARATOR];
  2164. }
  2165. sqlite3_finalize(statement);
  2166. }
  2167. // [iSalesDB close_db:db];
  2168. return ret;
  2169. }
  2170. +(NSArray*) get_contact_default_price_type:(NSString*) contact_id db:(sqlite3*)db
  2171. {
  2172. // sqlite3 *db = [iSalesDB get_db];
  2173. if(contact_id==nil)
  2174. {
  2175. // no customer assigned , use login user contact_id
  2176. UIApplication * app = [UIApplication sharedApplication];
  2177. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2178. NSString* sqlQuery = [NSString stringWithFormat:@"select contact_id from offline_login where username='%@';",appDelegate.user];
  2179. sqlite3_stmt * statement;
  2180. // int count=0;
  2181. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2182. {
  2183. if (sqlite3_step(statement) == SQLITE_ROW)
  2184. {
  2185. char *val = (char*)sqlite3_column_text(statement, 0);
  2186. if(val==nil)
  2187. val="";
  2188. contact_id = [[NSString alloc]initWithUTF8String:val];
  2189. }
  2190. sqlite3_finalize(statement);
  2191. }
  2192. if(contact_id.length<=0)
  2193. {
  2194. // [iSalesDB close_db:db];
  2195. return nil;
  2196. }
  2197. }
  2198. NSString* sqlQuery = [NSString stringWithFormat:@"select price_type from offline_contact where contact_id='%@';",contact_id];
  2199. sqlite3_stmt * statement;
  2200. NSArray* ret=nil;
  2201. // int count=0;
  2202. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2203. {
  2204. if (sqlite3_step(statement) == SQLITE_ROW)
  2205. {
  2206. char *price_type = (char*)sqlite3_column_text(statement, 0);
  2207. if(price_type==nil)
  2208. price_type="";
  2209. NSString *nsprice_type = [[NSString alloc]initWithUTF8String:price_type];
  2210. if(nsprice_type.length>0)
  2211. ret=[RAUtils string2arr:nsprice_type separator:OFFLINE_ARRAY_SEPARATOR];
  2212. else
  2213. ret=nil;
  2214. }
  2215. sqlite3_finalize(statement);
  2216. }
  2217. // [iSalesDB close_db:db];
  2218. return ret;
  2219. }
  2220. +(NSString*) model_category_img :(NSString *) product_id model_name:(NSString *) model_name db:(sqlite3*)db
  2221. {
  2222. NSString* ret= nil;
  2223. // sqlite3 *db = [iSalesDB get_db];
  2224. NSString *sqlQuery = nil;
  2225. //select url,type from model_image where product_id=%d and type=1 order by default_img desc , _id asc limit 1;",product_id
  2226. // 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;
  2227. // 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
  2228. if(product_id==nil && model_name)
  2229. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where product_id in (select product_id from product where name='%@') order by i.default_img desc, i._id asc limit 1;",model_name];// select i.url from model m LEFT join model_image i on m.product_id = i.product_id where m.name=%@ order by i.default_img desc, i._id asc limit 1;
  2230. else if (product_id)
  2231. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image i where i.product_id=%@ order by i.default_img desc, i._id asc limit 1;",product_id];// select i.url from model m
  2232. sqlite3_stmt * statement;
  2233. // int count=0;
  2234. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2235. {
  2236. if (sqlite3_step(statement) == SQLITE_ROW)
  2237. {
  2238. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  2239. if(imgurl==nil)
  2240. imgurl="";
  2241. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  2242. ret=nsimgurl;
  2243. }
  2244. sqlite3_finalize(statement);
  2245. }
  2246. else
  2247. {
  2248. [ret setValue:@"8" forKey:@"result"];
  2249. }
  2250. // [iSalesDB close_db:db];
  2251. DebugLog(@"data string: %@",ret );
  2252. return ret;
  2253. }
  2254. +(NSString*) model_default_category :(NSString *) product_id model_name:(NSString *) model_name
  2255. {
  2256. NSString* ret= nil;
  2257. sqlite3 *db = [iSalesDB get_db];
  2258. NSString *sqlQuery = nil;
  2259. if(product_id==nil)
  2260. sqlQuery = [NSString stringWithFormat:@"select default_category from product where name='%@';",model_name];// select default_category from model where name='%@';
  2261. else
  2262. sqlQuery = [NSString stringWithFormat:@"select default_category from product where product_id=%@;",product_id]; // select default_category from model where product_id=%@;
  2263. sqlite3_stmt * statement;
  2264. // int count=0;
  2265. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2266. {
  2267. if (sqlite3_step(statement) == SQLITE_ROW)
  2268. {
  2269. char *default_category = (char*)sqlite3_column_text(statement, 0);
  2270. if(default_category==nil)
  2271. default_category="";
  2272. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2273. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  2274. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  2275. ret=nsdefault_category;
  2276. }
  2277. sqlite3_finalize(statement);
  2278. }
  2279. else
  2280. {
  2281. [ret setValue:@"8" forKey:@"result"];
  2282. }
  2283. [iSalesDB close_db:db];
  2284. DebugLog(@"data string: %@",ret );
  2285. return ret;
  2286. }
  2287. +(NSDictionary*) offline_model :(NSMutableDictionary *) params
  2288. {
  2289. NSString* model_name = [params valueForKey:@"product_name"];
  2290. NSString* product_id = [params valueForKey:@"product_id"];
  2291. NSString* category = [params valueForKey:@"category"];
  2292. if(category==nil)
  2293. category = [self model_default_category:product_id model_name:model_name];
  2294. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2295. sqlite3 *db = [iSalesDB get_db];
  2296. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  2297. NSString *sqlQuery = nil;
  2298. if(product_id==nil)
  2299. 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,ETA,item_id 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='%@';
  2300. else
  2301. 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,ETA,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=%@;
  2302. sqlite3_stmt * statement;
  2303. [ret setValue:@"2" forKey:@"result"];
  2304. [ret setValue:@"3" forKey:@"detail_section_count"];
  2305. // int count=0;
  2306. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2307. {
  2308. if (sqlite3_step(statement) == SQLITE_ROW)
  2309. {
  2310. // NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2311. char *name = (char*)sqlite3_column_text(statement, 0);
  2312. if(name==nil)
  2313. name="";
  2314. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2315. char *description = (char*)sqlite3_column_text(statement, 1);
  2316. if(description==nil)
  2317. description="";
  2318. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2319. int product_id = sqlite3_column_int(statement, 2);
  2320. char *color = (char*)sqlite3_column_text(statement, 3);
  2321. if(color==nil)
  2322. color="";
  2323. // NSString *nscolor = [[NSString alloc]initWithUTF8String:color];
  2324. //
  2325. // char *legcolor = (char*)sqlite3_column_text(statement, 4);
  2326. // if(legcolor==nil)
  2327. // legcolor="";
  2328. // NSString *nslegcolor = [[NSString alloc]initWithUTF8String:legcolor];
  2329. //
  2330. //
  2331. int availability = sqlite3_column_int(statement, 5);
  2332. //
  2333. int incoming_stock = sqlite3_column_int(statement, 6);
  2334. char *demension = (char*)sqlite3_column_text(statement, 7);
  2335. if(demension==nil)
  2336. demension="";
  2337. NSString *nsdemension = [[NSString alloc]initWithUTF8String:demension];
  2338. // ,,,,,,,,,
  2339. char *seat_height = (char*)sqlite3_column_text(statement, 8);
  2340. if(seat_height==nil)
  2341. seat_height="";
  2342. NSString *nsseat_height = [[NSString alloc]initWithUTF8String:seat_height];
  2343. char *material = (char*)sqlite3_column_text(statement, 9);
  2344. if(material==nil)
  2345. material="";
  2346. NSString *nsmaterial = [[NSString alloc]initWithUTF8String:material];
  2347. char *box_dim = (char*)sqlite3_column_text(statement, 10);
  2348. if(box_dim==nil)
  2349. box_dim="";
  2350. NSString *nsbox_dim = [[NSString alloc]initWithUTF8String:box_dim];
  2351. char *volume = (char*)sqlite3_column_text(statement, 11);
  2352. if(volume==nil)
  2353. volume="";
  2354. NSString *nsvolume = [[NSString alloc]initWithUTF8String:volume];
  2355. double weight = sqlite3_column_double(statement, 12);
  2356. char *model_set = (char*)sqlite3_column_text(statement, 13);
  2357. if(model_set==nil)
  2358. model_set="";
  2359. NSString *nsmodel_set = [[NSString alloc]initWithUTF8String:model_set];
  2360. char *load_ability = (char*)sqlite3_column_text(statement, 14);
  2361. if(load_ability==nil)
  2362. load_ability="";
  2363. NSString *nsload_ability = [[NSString alloc]initWithUTF8String:load_ability];
  2364. char *default_category = (char*)sqlite3_column_text(statement, 15);
  2365. if(default_category==nil)
  2366. default_category="";
  2367. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  2368. char *fabric_content = (char*)sqlite3_column_text(statement, 16);
  2369. if(fabric_content==nil)
  2370. fabric_content="";
  2371. NSString *nsfabric_content = [[NSString alloc]initWithUTF8String:fabric_content];
  2372. char *assembling = (char*)sqlite3_column_text(statement, 17);
  2373. if(assembling==nil)
  2374. assembling="";
  2375. NSString *nsassembling = [[NSString alloc]initWithUTF8String:assembling];
  2376. char *made_in = (char*)sqlite3_column_text(statement, 18);
  2377. if(made_in==nil)
  2378. made_in="";
  2379. NSString *nsmade_in = [[NSString alloc]initWithUTF8String:made_in];
  2380. char *special_remarks = (char*)sqlite3_column_text(statement, 19);
  2381. if(special_remarks==nil)
  2382. special_remarks="";
  2383. NSString *nsspecial_remarks = [[NSString alloc]initWithUTF8String:special_remarks];
  2384. int stockUcom = sqlite3_column_double(statement, 20);
  2385. char *product_group = (char*)sqlite3_column_text(statement, 21);
  2386. if(product_group==nil)
  2387. product_group="";
  2388. NSString *nsproduct_group = [[NSString alloc]initWithUTF8String:product_group];
  2389. // char *fashion_selector = (char*)sqlite3_column_text(statement, 22);
  2390. // if(fashion_selector==nil)
  2391. // fashion_selector="";
  2392. // NSString *nsfashion_selector = [[NSString alloc]initWithUTF8String:fashion_selector];
  2393. char *selector_field = (char*)sqlite3_column_text(statement, 22);
  2394. if(selector_field==nil)
  2395. selector_field="";
  2396. NSString *nsselector_field = [[NSString alloc]initWithUTF8String:selector_field];
  2397. char *property_field = (char*)sqlite3_column_text(statement, 23);
  2398. if(property_field==nil)
  2399. property_field="";
  2400. NSString *nsproperty_field = [[NSString alloc]initWithUTF8String:property_field];
  2401. char *packaging = (char*)sqlite3_column_text(statement, 24);
  2402. if(packaging==nil)
  2403. packaging="";
  2404. NSString *nspackaging = [[NSString alloc]initWithUTF8String:packaging];
  2405. [ret setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2406. NSMutableDictionary* img_section = [[NSMutableDictionary alloc] init];
  2407. [img_section setValue:[NSString stringWithFormat:@"%d",incoming_stock] forKey:@"incoming_stock"];
  2408. NSString* model_s_img = [self model_category_img:[NSString stringWithFormat:@"%d",product_id] model_name:model_name db:db];
  2409. [img_section setValue:model_s_img forKey:@"model_s_img"];
  2410. [img_section setObject:[self model_img:product_id db:db] forKey:@"images"];
  2411. [img_section setObject:[self model_property:product_id field:nsproperty_field db:db] forKey:@"property"];
  2412. [img_section setObject:[self model_selector:nsproduct_group field:nsselector_field db:db] forKey:@"selector"];
  2413. NSString* Availability=nil;
  2414. if(availability>0)
  2415. Availability=[NSString stringWithFormat:@"%d",availability];
  2416. else
  2417. Availability = @"Out of Stock";
  2418. [img_section setValue:Availability forKey:@"Availability"];
  2419. [img_section setValue:[NSString stringWithFormat:@"%d",stockUcom] forKey:@"stockUom"];
  2420. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  2421. char *eta = (char*)sqlite3_column_text(statement, 25);
  2422. if(eta==nil)
  2423. eta="";
  2424. NSString *nseta = [[NSString alloc]initWithUTF8String:eta];
  2425. if ([nseta isEqualToString:@"null"]) {
  2426. nseta = @"";
  2427. }
  2428. if (availability <= 0) {
  2429. [img_section setValue:nseta forKey:@"ETA"];
  2430. }
  2431. int item_id = sqlite3_column_int(statement, 26);
  2432. NSString* Price=nil;
  2433. if(appDelegate.bLogin==false)
  2434. Price=@"Must Sign in.";
  2435. else
  2436. {
  2437. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  2438. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  2439. if(price==nil)
  2440. Price=@"No Price.";
  2441. else
  2442. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  2443. }
  2444. [img_section setValue:Price forKey:@"price"];
  2445. [img_section setValue:nsname forKey:@"model_name"];
  2446. [img_section setValue:nsdescription forKey:@"model_descrition"];
  2447. if (appDelegate.order_code) { // 离线order code即so#
  2448. NSString *cartQtySql = [NSString stringWithFormat:@"select c.item_count from offline_cart as c left join offline_order as o on c.so_no = o.so_id where o.so_id = '%@' and c.item_id = %d;",appDelegate.order_code,item_id];
  2449. __block int cartQTY = 0;
  2450. [iSalesDB jk_query:cartQtySql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2451. cartQTY = sqlite3_column_int(stmt, 0);
  2452. }];
  2453. if (cartQTY > 0) {
  2454. [img_section setObject:[NSNumber numberWithInt:cartQTY] forKey:@"Model QTY in cart"];
  2455. }
  2456. }
  2457. [ret setObject:img_section forKey:@"img_section"];
  2458. NSMutableDictionary* detail0_section = [[NSMutableDictionary alloc] init];
  2459. int detail0_item_count=0;
  2460. [detail0_section setObject:[self model_kvItem:@"Dimension" value:nsdemension] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2461. [detail0_section setObject:[self model_kvItem:@"Seat Height" value:nsseat_height] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2462. [detail0_section setObject:[self model_kvItem:@"Material" value:nsmaterial] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2463. [detail0_section setObject:[self model_kvItem:@"Box dimension" value:nsbox_dim] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2464. [detail0_section setObject:[self model_kvItem:@"Volume" value:nsvolume] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2465. [detail0_section setObject:[self model_kvItem:@"Weight" value:[NSString stringWithFormat:@"%0.2f",weight]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2466. [detail0_section setObject:[self model_kvItem:@"Set" value:nsmodel_set] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2467. [detail0_section setObject:[self model_kvItem:@"Load ability" value:nsload_ability] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2468. [detail0_section setObject:[self model_kvItem:@"Fabric Content" value:nsfabric_content] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2469. [detail0_section setObject:[self model_kvItem:@"Assembling" value:nsassembling] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2470. [detail0_section setObject:[self model_kvItem:@"Made in" value:nsmade_in] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2471. [detail0_section setObject:[self model_kvItem:@"Special Remarks" value:nsspecial_remarks] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2472. [detail0_section setObject:[self model_kvItem:@"Packaging" value:nspackaging] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2473. // NSDictionary* pricejson=[self get_model_all_price:appDelegate.contact_id product_id:product_id db:db];
  2474. NSDictionary* pricejson = [self get_model_all_price:appDelegate.contact_id item_id:item_id db:db];
  2475. for(int l=0;l<[pricejson[@"count"] intValue];l++)
  2476. {
  2477. NSDictionary* price_item = pricejson[[NSString stringWithFormat:@"item_%d",l]];
  2478. [detail0_section setObject:[self model_priceKvItem:price_item.allKeys[0] value:price_item.allValues[0]] forKey:[NSString stringWithFormat:@"item_%d",detail0_item_count++]];
  2479. }
  2480. [detail0_section setValue:[NSString stringWithFormat:@"%d",detail0_item_count] forKey:@"count"];
  2481. [detail0_section setValue:@"kv" forKey:@"type"];
  2482. [detail0_section setValue:@"Product Information" forKey:@"title"];
  2483. [ret setObject:detail0_section forKey:@"detail_0"];
  2484. NSMutableDictionary* detail1_section = [[self model_NIYMAL:category db:db] mutableCopy];
  2485. // [detail1_section setValue:@"detail" forKey:@"target"];
  2486. // [detail1_section setValue:@"popup" forKey:@"action"];
  2487. // [detail1_section setValue:@"content" forKey:@"type"];
  2488. // [detail1_section setValue:@"New Items You May Also Like" forKey:@"title"];
  2489. // [detail1_section setValue:@"model_NIYMAL" forKey:@"data_interface"];
  2490. // [detail1_section setValue:@"true" forKey:@"single_row"];
  2491. // [detail1_section setValue:@"true" forKey:@"partial_refresh"];
  2492. [ret setObject:detail1_section forKey:@"detail_1"];
  2493. NSMutableDictionary* detail2_section = [[NSMutableDictionary alloc]init];
  2494. [detail2_section setValue:@"detail" forKey:@"target"];
  2495. [detail2_section setValue:@"popup" forKey:@"action"];
  2496. [detail2_section setValue:@"content" forKey:@"type"];
  2497. [detail2_section setValue:@"Recently Viewed" forKey:@"title"];
  2498. [detail2_section setValue:@"true" forKey:@"single_row"];
  2499. [detail2_section setValue:@"local" forKey:@"data"];
  2500. [ret setObject:detail2_section forKey:@"detail_2"];
  2501. }
  2502. sqlite3_finalize(statement);
  2503. }
  2504. else
  2505. {
  2506. [ret setValue:@"8" forKey:@"result"];
  2507. }
  2508. DebugLog(@"count:%d",count);
  2509. [iSalesDB close_db:db];
  2510. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2511. return ret;
  2512. }
  2513. + (NSDictionary *) search:(NSMutableDictionary *)params limited:(BOOL)limited {
  2514. NSString* orderCode = [params valueForKey:@"orderCode"];
  2515. NSString* keyword = [params valueForKey:@"keyword"];
  2516. keyword=keyword.lowercaseString;
  2517. bool exactMatch = [[params valueForKey:@"exactMatch"] boolValue]; // search by factory code
  2518. int limit = [[params valueForKey:@"limit"] intValue];
  2519. int offset = [[params valueForKey:@"offset"] intValue];
  2520. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2521. NSString *limit_str = @"";
  2522. if (limited) {
  2523. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  2524. }
  2525. sqlite3 *db = [iSalesDB get_db];
  2526. int count = [iSalesDB get_recordcount:db table:@"product" where:[NSString stringWithFormat:@"(lower(name) like'%%%@%%' or lower(description) like'%%%@%%') and is_active = 1",keyword,keyword]];
  2527. UIApplication * app = [UIApplication sharedApplication];
  2528. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2529. NSString *sqlQuery = nil;
  2530. if(exactMatch )
  2531. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where lower(name) like'%@%%' and m.is_active = 1 order by m.name %@;",appDelegate.order_code, keyword,limit_str]; // select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%@%%' order by m.name limit %d offset %d ;
  2532. else
  2533. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where (lower(name) like'%%%@%%' or lower(description) like'%%%@%%' or lower(made_in) like '%%%@%%' or lower(material) like '%%%@%%' or lower(packaging) like '%%%@%%') and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, keyword,keyword,keyword,keyword,keyword,limit_str];// select m.name,m.description,m.product_id,w._id,m.closeout from model m left join wishlist w on m.product_id=w.product_id where lower(name) like'%%%@%%' or
  2534. DebugLog(@"offline_search sql:%@",sqlQuery);
  2535. sqlite3_stmt * statement;
  2536. [ret setValue:@"2" forKey:@"result"];
  2537. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  2538. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  2539. // int count=0;
  2540. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2541. {
  2542. int i=0;
  2543. while (sqlite3_step(statement) == SQLITE_ROW)
  2544. {
  2545. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  2546. // char *name = (char*)sqlite3_column_text(statement, 1);
  2547. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2548. char *name = (char*)sqlite3_column_text(statement, 0);
  2549. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  2550. char *description = (char*)sqlite3_column_text(statement, 1);
  2551. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  2552. int product_id = sqlite3_column_int(statement, 2);
  2553. // char *url = (char*)sqlite3_column_text(statement, 3);
  2554. // if(url==nil)
  2555. // url="";
  2556. // NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  2557. int wid = sqlite3_column_int(statement, 3);
  2558. int closeout = sqlite3_column_int(statement, 4);
  2559. int cid = sqlite3_column_int(statement, 5);
  2560. int wisdelete = sqlite3_column_int(statement, 6);
  2561. int more_color = sqlite3_column_int(statement, 7);
  2562. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  2563. if(wid !=0 && wisdelete != 1)
  2564. [item setValue:@"true" forKey:@"wish_exists"];
  2565. else
  2566. [item setValue:@"false" forKey:@"wish_exists"];
  2567. if(closeout==0)
  2568. [item setValue:@"false" forKey:@"is_closeout"];
  2569. else
  2570. [item setValue:@"true" forKey:@"is_closeout"];
  2571. if(cid==0)
  2572. [item setValue:@"false" forKey:@"cart_exists"];
  2573. else
  2574. [item setValue:@"true" forKey:@"cart_exists"];
  2575. if (more_color == 0) {
  2576. [item setObject:@(false) forKey:@"more_color"];
  2577. } else if (more_color == 1) {
  2578. [item setObject:@(true) forKey:@"more_color"];
  2579. }
  2580. [item addEntriesFromDictionary:imgjson];
  2581. // [item setValue:nsurl forKey:@"img"];
  2582. [item setValue:nsname forKey:@"fash_name"];
  2583. [item setValue:nsdescription forKey:@"description"];
  2584. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  2585. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  2586. i++;
  2587. }
  2588. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  2589. [ret setObject:items forKey:@"items"];
  2590. sqlite3_finalize(statement);
  2591. }
  2592. DebugLog(@"count:%d",count);
  2593. [iSalesDB close_db:db];
  2594. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2595. return ret;
  2596. }
  2597. +(NSDictionary*) offline_search:(NSMutableDictionary *) params
  2598. {
  2599. return [self search:params limited:YES];
  2600. }
  2601. +(NSDictionary*) offline_logout :(NSMutableDictionary *) params
  2602. {
  2603. UIApplication * app = [UIApplication sharedApplication];
  2604. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2605. appDelegate.disable_trigger=true;
  2606. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2607. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2608. appDelegate.disable_trigger=false;
  2609. //
  2610. // NSString* user = [params valueForKey:@"user"];
  2611. //
  2612. // NSString* password = [params valueForKey:@"password"];
  2613. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2614. ret[@"result"]=[NSNumber numberWithInt:2 ];
  2615. appDelegate.cart_count = 0;//[[jsobj valueForKey:@"cart_count"] intValue];
  2616. appDelegate.wish_count =0;//[[jsobj valueForKey:@"wish_count"] intValue];
  2617. appDelegate.port_count =0;//[[jsobj valueForKey:@"portfolio_count"] intValue];
  2618. [appDelegate update_count_mark];
  2619. appDelegate.can_show_price =false;
  2620. appDelegate.can_see_price =false;
  2621. appDelegate.can_create_portfolio =false;
  2622. appDelegate.can_create_order =false;
  2623. appDelegate.can_cancel_order =false;
  2624. appDelegate.can_set_cart_price =false;
  2625. appDelegate.can_delete_order =false;
  2626. appDelegate.can_submit_order =false;
  2627. appDelegate.can_set_tearsheet_price =false;
  2628. appDelegate.can_update_contact_info = false;
  2629. appDelegate.save_order_logout = false;
  2630. appDelegate.submit_order_logout = false;
  2631. appDelegate.alert_sold_in_quantities = false;
  2632. appDelegate.ipad_perm =nil ;
  2633. appDelegate.user_type = USER_ROLE_UNKNOWN;
  2634. appDelegate.OrderFilter= nil;
  2635. [appDelegate SetSo:nil];
  2636. [appDelegate set_main_button_panel];
  2637. // sqlite3 *db = [iSalesDB get_db];
  2638. //
  2639. //
  2640. //
  2641. //
  2642. //
  2643. // 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"]];
  2644. //
  2645. //
  2646. //
  2647. //
  2648. // DebugLog(@"offline_login sql:%@",sqlQuery);
  2649. // sqlite3_stmt * statement;
  2650. //
  2651. //
  2652. // [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  2653. //
  2654. //
  2655. // if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2656. // {
  2657. //
  2658. //
  2659. // if (sqlite3_step(statement) == SQLITE_ROW)
  2660. // {
  2661. //
  2662. // [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  2663. // NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  2664. //
  2665. //
  2666. //
  2667. // int can_show_price = sqlite3_column_int(statement, 0);
  2668. // int can_see_price = sqlite3_column_int(statement, 1);
  2669. //
  2670. // char *contact_id = (char*)sqlite3_column_text(statement, 2);
  2671. // if(contact_id==nil)
  2672. // contact_id="";
  2673. // NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  2674. //
  2675. // int user_type = sqlite3_column_int(statement, 3);
  2676. //
  2677. // int can_cancel_order = sqlite3_column_int(statement, 4);
  2678. // int can_set_cart_price = sqlite3_column_int(statement, 5);
  2679. // int can_create_portfolio = sqlite3_column_int(statement, 6);
  2680. // int can_delete_order = sqlite3_column_int(statement, 7);
  2681. // int can_submit_order = sqlite3_column_int(statement, 8);
  2682. // int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  2683. // int can_create_order = sqlite3_column_int(statement, 10);
  2684. //
  2685. //
  2686. // char *mode = (char*)sqlite3_column_text(statement, 11);
  2687. // if(mode==nil)
  2688. // mode="";
  2689. // NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  2690. //
  2691. //
  2692. // char *username = (char*)sqlite3_column_text(statement, 12);
  2693. // if(username==nil)
  2694. // username="";
  2695. // NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  2696. //
  2697. //
  2698. // [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  2699. // [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  2700. // [header setValue:nscontact_id forKey:@"contact_id"];
  2701. // [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  2702. // [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  2703. // [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  2704. // [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  2705. // [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  2706. // [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  2707. // [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  2708. // [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  2709. //
  2710. // [header setValue:nsusername forKey:@"username"];
  2711. //
  2712. //
  2713. // [ret setObject:header forKey:@"header"];
  2714. // [ret setValue:nsmode forKey:@"mode"];
  2715. //
  2716. //
  2717. // }
  2718. //
  2719. //
  2720. //
  2721. // sqlite3_finalize(statement);
  2722. // }
  2723. //
  2724. //
  2725. //
  2726. // [iSalesDB close_db:db];
  2727. //
  2728. //
  2729. //
  2730. //
  2731. // DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  2732. return ret;
  2733. }
  2734. +(NSData*) offline_createorder :(NSMutableDictionary *) params
  2735. {
  2736. UIApplication * app = [UIApplication sharedApplication];
  2737. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2738. appDelegate.disable_trigger=true;
  2739. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2740. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2741. appDelegate.disable_trigger=false;
  2742. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2743. NSString *customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2744. NSString* customer_cid = [self translateSingleQuote:params[@"customer_cid"]];
  2745. // NSString* customer_address1 = [self translateSingleQuote:params[@"customer_address1"]];
  2746. // NSString* customer_address2 = [self translateSingleQuote:params[@"customer_address2"]];
  2747. // NSString* customer_address3 = [self translateSingleQuote:params[@"customer_address3"]];
  2748. // NSString* customer_address4 = [self translateSingleQuote:params[@"customer_address4"]];
  2749. // NSString* customer_city = [self translateSingleQuote:params[@"customer_city"]];
  2750. NSString* customer_contact = [self translateSingleQuote:params[@"customer_contact"]];
  2751. NSString* customer_contact_ext = [self translateSingleQuote:params[@"customer_contact_ext"]];
  2752. // NSString* customer_contact_notes = [self translateSingleQuote:params[@"customer_contact_notes"]];
  2753. // NSString* customer_country = [self translateSingleQuote:params[@"customer_country"]];
  2754. NSString* customer_email = [self translateSingleQuote:params[@"customer_email"]];
  2755. NSString* customer_fax = [self translateSingleQuote:params[@"customer_fax"]];
  2756. NSString* customer_first_name = [self translateSingleQuote:params[@"customer_first_name"]];
  2757. NSString* customer_last_name = [self translateSingleQuote:params[@"customer_last_name"]];
  2758. // NSString* customer_name = [self translateSingleQuote:params[@"customer_name"]];
  2759. NSString* customer_phone = [self translateSingleQuote:params[@"customer_phone"]];
  2760. // NSString* customer_price_type = [self translateSingleQuote:params[@"customer_price_type"]];
  2761. NSString* customer_sales_rep = [self translateSingleQuote:params[@"customer_sales_rep"]];
  2762. // NSString* customer_state = [self translateSingleQuote:params[@"customer_state"]];
  2763. // NSString* customer_zipcode = [self translateSingleQuote:params[@"customer_zipcode"]];
  2764. NSString* create_by = [self translateSingleQuote:params[@"user"]];
  2765. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  2766. [arr_name addObject:customer_first_name];
  2767. [arr_name addObject:customer_last_name];
  2768. NSString *contact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  2769. // default ship from
  2770. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,decrypt(phone) from offline_contact where contact_id = 'NPD';";
  2771. __block NSString *cid = @"";
  2772. __block NSString *name = @"";
  2773. __block NSString *ext = @"";
  2774. __block NSString *contact = @"";
  2775. __block NSString *email = @"";
  2776. __block NSString *fax = @"";
  2777. __block NSString *phone = @"";
  2778. sqlite3 *db = [iSalesDB get_db];
  2779. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  2780. cid = [self textAtColumn:0 statement:statment];
  2781. name = [self textAtColumn:1 statement:statment];
  2782. ext = [self textAtColumn:2 statement:statment];
  2783. contact = [self textAtColumn:3 statement:statment];
  2784. email = [self textAtColumn:4 statement:statment];
  2785. fax = [self textAtColumn:5 statement:statment];
  2786. phone = [self textAtColumn:6 statement:statment];
  2787. }];
  2788. NSString* so_id = [self get_offline_soid:db];
  2789. if(so_id==nil)
  2790. so_id=[NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString ];
  2791. NSString* sql_neworder=[NSString stringWithFormat:@"insert into offline_order(so_id,status,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,sales_rep,create_by,customer_contact,must_call,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,sales_terms) values('%@',0,'%@','%@','%@','%@','%@','%@','%@','%@',1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','FOB')",so_id,customer_cid,customer_contact,customer_email,customer_fax,customer_phone,customer_sales_rep,create_by ,contact_name,cid,name,ext,contact,phone,fax,email,customer_cid,customer_name,customer_contact_ext,customer_contact,customer_phone,customer_fax,customer_email];
  2792. int result =[iSalesDB execSql:sql_neworder db:db];
  2793. [ret setValue:[NSNumber numberWithInt:result] forKey:@"result"];
  2794. //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'
  2795. //soId
  2796. int _id=[iSalesDB get_insertid:@"offline_order" db:db];
  2797. NSString *sqlQuery = [NSString stringWithFormat:@"select so_id from offline_order where _id=%d ",_id];
  2798. sqlite3_stmt * statement;
  2799. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2800. {
  2801. if (sqlite3_step(statement) == SQLITE_ROW)
  2802. {
  2803. // char *name = (char*)sqlite3_column_text(statement, 1);
  2804. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  2805. //ret = sqlite3_column_int(statement, 0);
  2806. char *soId = (char*)sqlite3_column_text(statement, 0);
  2807. if(soId==nil)
  2808. soId="";
  2809. NSString *nssoId = [[NSString alloc]initWithUTF8String:soId];
  2810. [ret setValue:nssoId forKey:@"soId"];
  2811. [ret setValue:nssoId forKey:@"orderCode"];
  2812. }
  2813. sqlite3_finalize(statement);
  2814. }
  2815. [ret setValue:[NSNumber numberWithInt:0] forKey:@"orderStatus"];
  2816. [ret setValue:@"Regular Mode" forKey:@"mode"];
  2817. [iSalesDB close_db:db];
  2818. return [RAUtils dict2data:ret];
  2819. }
  2820. +(NSData*) offline_editorder :(NSMutableDictionary *) params
  2821. {
  2822. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2823. NSString* orderCode = [params valueForKey:@"orderCode"];
  2824. UIApplication * app = [UIApplication sharedApplication];
  2825. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2826. if(![appDelegate.order_code isEqualToString:orderCode]&& appDelegate.order_code.length!=0)
  2827. {
  2828. appDelegate.disable_trigger=true;
  2829. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  2830. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  2831. appDelegate.disable_trigger=false;
  2832. }
  2833. sqlite3 *db = [iSalesDB get_db];
  2834. int cart_count=[self query_ordercartcount:orderCode db:db];
  2835. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2836. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2837. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2838. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2839. [iSalesDB close_db:db];
  2840. return [RAUtils dict2data:ret];
  2841. }
  2842. +(NSData*) offline_add2cart :(NSMutableDictionary *) params;
  2843. {
  2844. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  2845. sqlite3 *db = [iSalesDB get_db];
  2846. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  2847. int count =0;
  2848. if(params[@"count"]!=nil)
  2849. {
  2850. count = [params[@"count"] intValue];
  2851. }
  2852. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  2853. NSString* product_id=params[@"product_id"];
  2854. NSString* orderCode=params[@"orderCode"];
  2855. NSString *qty = params[@"qty"];
  2856. NSArray* arr_id=[RAUtils string2arr:product_id separator:@","];
  2857. NSArray *qty_arr = [RAUtils string2arr:qty separator:@","];
  2858. __block int number_of_outOfStock = 0;
  2859. for(int i=0;i<arr_id.count;i++)
  2860. {
  2861. NSInteger item_qty= count;
  2862. if (qty) {
  2863. item_qty = [qty_arr[i] integerValue];
  2864. }
  2865. if(item_qty==0)
  2866. item_qty=[self model_stockUom:[arr_id[i] intValue] db:db];
  2867. // 检查新加Model数量是否大于库存
  2868. if (!appDelegate.can_create_backorder) {
  2869. __block BOOL needContinue = NO;
  2870. [iSalesDB jk_query:[NSString stringWithFormat:@"select availability from product where product_id = %@;",arr_id[i]] db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2871. int availability = sqlite3_column_int(stmt, 0);
  2872. // 库存小于购买量为缺货
  2873. if (availability < item_qty || availability <= 0) {
  2874. number_of_outOfStock++;
  2875. needContinue = YES;
  2876. }
  2877. }];
  2878. if (needContinue) {
  2879. continue;
  2880. }
  2881. }
  2882. int _id=[iSalesDB get_recordid:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@' and product_id=%@",orderCode,arr_id[i]]];
  2883. NSString* sql=nil;
  2884. sqlite3_stmt *stmt = nil;
  2885. BOOL shouldStep = NO;
  2886. if(_id<0)
  2887. {
  2888. int item_id=[self product_itemid:[arr_id[i] intValue] db:db];
  2889. sql=[NSString stringWithFormat:@"insert into offline_cart(product_id,so_no,item_count,item_id,create_time) values(?,?,?,?,datetime('now', 'localtime'))"];
  2890. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2891. sqlite3_bind_text(stmt,1,[arr_id[i] UTF8String],(int)[arr_id[i] length],NULL);
  2892. sqlite3_bind_text(stmt,2,[orderCode UTF8String],(int)[orderCode length],NULL);
  2893. sqlite3_bind_text(stmt,3,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2894. sqlite3_bind_text(stmt,4,[[NSString stringWithFormat:@"%d",item_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",item_id] length],NULL);
  2895. shouldStep = YES;
  2896. }
  2897. else
  2898. {
  2899. if (qty) { // wish list move to cart
  2900. sql=[NSString stringWithFormat:@"update offline_cart set item_count=1,modify_time = datetime('now', 'localtime') where _id=?"];
  2901. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2902. sqlite3_bind_int(stmt, 1, _id);
  2903. shouldStep = YES;
  2904. } else {
  2905. sql = [NSString stringWithFormat:@"select c.item_count + %ld,p.availability from product as p left join offline_cart as c on c.item_id = p.item_id where c._id = %d;",item_qty,_id];
  2906. __block BOOL update = YES;
  2907. if (!appDelegate.can_create_backorder) {
  2908. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  2909. int newQTY = sqlite3_column_int(stmt, 0);
  2910. int availability = sqlite3_column_int(stmt, 1);
  2911. if (newQTY > availability) { // 库存不够
  2912. update = NO;
  2913. number_of_outOfStock++;
  2914. }
  2915. }];
  2916. }
  2917. if (update) {
  2918. sql=[NSString stringWithFormat:@"update offline_cart set item_count=item_count+?,create_time = datetime('now', 'localtime') where _id=?"];
  2919. sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, nil);
  2920. sqlite3_bind_text(stmt,1,[[NSString stringWithFormat:@"%ld",item_qty] UTF8String],(int)[[NSString stringWithFormat:@"%ld",item_qty] length],NULL);
  2921. sqlite3_bind_text(stmt,2,[[NSString stringWithFormat:@"%d",_id] UTF8String],(int)[[NSString stringWithFormat:@"%d",_id] length],NULL);
  2922. shouldStep = YES;
  2923. }
  2924. }
  2925. }
  2926. if (shouldStep) {
  2927. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  2928. [iSalesDB execSql:@"ROLLBACK" db:db];
  2929. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  2930. [iSalesDB close_db:db];
  2931. DebugLog(@"add to cart error");
  2932. return [RAUtils dict2data:ret];
  2933. }
  2934. }
  2935. }
  2936. int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  2937. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  2938. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  2939. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  2940. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  2941. if (!appDelegate.can_create_backorder) {
  2942. if (number_of_outOfStock > 0) {
  2943. ret[@"result"]=[NSNumber numberWithInt:8];
  2944. ret[@"err_msg"] = @"Out of Stock.\nNo more quantity available.\nPlease try again after sync.";
  2945. /*[NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock]*/
  2946. }
  2947. }
  2948. [iSalesDB execSql:@"END TRANSACTION" db:db];
  2949. [iSalesDB close_db:db];
  2950. return [RAUtils dict2data:ret];
  2951. }
  2952. +(NSDictionary*) model_bundle:(int) item_id db:(sqlite3*)db compute_part:(bool)compute contactID:(NSString *)contactID
  2953. {
  2954. // UIApplication * app = [UIApplication sharedApplication];
  2955. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  2956. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  2957. 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];
  2958. sqlite3_stmt * statement;
  2959. int count=0;
  2960. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  2961. {
  2962. while (sqlite3_step(statement) == SQLITE_ROW)
  2963. {
  2964. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc]init];
  2965. int bitem_id = sqlite3_column_int(statement, 0);
  2966. int bitem_qty = sqlite3_column_int(statement, 1);
  2967. char *name = (char*)sqlite3_column_text(statement, 2);
  2968. if(name==nil)
  2969. name="";
  2970. NSString* nsname=[[NSString alloc]initWithUTF8String:name];
  2971. char *description = (char*)sqlite3_column_text(statement, 3);
  2972. if(description==nil)
  2973. description="";
  2974. NSString* nsdescription=[[NSString alloc]initWithUTF8String:description];
  2975. double unit_price = sqlite3_column_double(statement, 4);
  2976. int use_unitprice = sqlite3_column_int(statement, 5);
  2977. if(use_unitprice!=1)
  2978. {
  2979. unit_price=[self get_model_default_price:contactID product_id:nil item_id:@(bitem_id) db:db].doubleValue;
  2980. }
  2981. itemjson[@"model"]=nsname;
  2982. itemjson[@"description"]=nsdescription;
  2983. itemjson[@"unit_price"]=[NSString stringWithFormat:@"%.2f",unit_price];
  2984. itemjson[@"modulus"]=[NSNumber numberWithInt:bitem_qty];
  2985. itemjson[@"item_id"]=[NSNumber numberWithInt:bitem_id].stringValue;
  2986. if(compute)
  2987. {
  2988. itemjson[@"combine"]=[self model_bundle:bitem_id db:db compute_part:compute contactID:contactID];
  2989. }
  2990. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  2991. count++;
  2992. }
  2993. sqlite3_finalize(statement);
  2994. }
  2995. ret[@"count"]=@(count);
  2996. if(count==0)
  2997. return nil;
  2998. else
  2999. return ret;
  3000. }
  3001. +(NSDictionary*) model_subtotal:(int) item_id count:(int)count db:(sqlite3*)db compute_part:(bool)compute
  3002. {
  3003. //compute: add part to subtotal;
  3004. NSMutableDictionary * dict_item = [[NSMutableDictionary alloc]init];
  3005. dict_item[@(item_id)]=@"1";
  3006. NSMutableDictionary* ret = [[NSMutableDictionary alloc]init];
  3007. double cuft=0;
  3008. double weight=0;
  3009. int carton=0;
  3010. 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];
  3011. sqlite3_stmt * statement;
  3012. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3013. {
  3014. if (sqlite3_step(statement) == SQLITE_ROW)
  3015. {
  3016. double ulength = sqlite3_column_double(statement, 0);
  3017. double uwidth = sqlite3_column_double(statement, 1);
  3018. double uheight = sqlite3_column_double(statement, 2);
  3019. double uweight = sqlite3_column_double(statement, 3);
  3020. double mlength = sqlite3_column_double(statement, 4);
  3021. double mwidth = sqlite3_column_double(statement, 5);
  3022. double mheight = sqlite3_column_double(statement, 6);
  3023. double mweight = sqlite3_column_double(statement, 7);
  3024. double ilength = sqlite3_column_double(statement, 8);
  3025. double iwidth = sqlite3_column_double(statement, 9);
  3026. double iheight = sqlite3_column_double(statement, 10);
  3027. double iweight = sqlite3_column_double(statement, 11);
  3028. int pcs = sqlite3_column_int(statement,12);
  3029. int mpack = sqlite3_column_int(statement, 13);
  3030. int ipack = sqlite3_column_int(statement, 14);
  3031. double ucbf = sqlite3_column_double(statement, 15);
  3032. double icbf = sqlite3_column_double(statement, 16);
  3033. double mcbf = sqlite3_column_double(statement, 17);
  3034. if(ipack==0)
  3035. {
  3036. carton= count/mpack ;
  3037. weight = mweight*carton;
  3038. cuft= carton*(mlength*mwidth*mheight);
  3039. int remain=count%mpack;
  3040. if(remain==0)
  3041. {
  3042. //do nothing;
  3043. }
  3044. else
  3045. {
  3046. carton++;
  3047. weight += uweight*remain;
  3048. cuft += (ulength*uwidth*uheight)*remain;
  3049. }
  3050. }
  3051. else
  3052. {
  3053. carton = count/(mpack*ipack);
  3054. weight = mweight*carton;
  3055. cuft= carton*(mlength*mwidth*mheight);
  3056. int remain=count%(mpack*ipack);
  3057. if(remain==0)
  3058. {
  3059. // do nothing;
  3060. }
  3061. else
  3062. {
  3063. carton++;
  3064. int icarton =remain/ipack;
  3065. int iremain=remain%ipack;
  3066. weight += iweight*icarton;
  3067. cuft += (ilength*iwidth*iheight)*icarton;
  3068. if(iremain==0)
  3069. {
  3070. //do nothing;
  3071. }
  3072. else
  3073. {
  3074. weight += uweight*iremain;
  3075. cuft += (ulength*uwidth*uheight)*iremain;
  3076. }
  3077. }
  3078. }
  3079. #ifdef BUILD_NPD
  3080. cuft=ucbf*count;
  3081. weight= uweight*count;
  3082. #endif
  3083. }
  3084. sqlite3_finalize(statement);
  3085. }
  3086. if(compute)
  3087. {
  3088. NSArray * arr_count=nil;
  3089. NSArray * arr_bundle=[self model_bundle:item_id db:db count:&arr_count];
  3090. for(int i=0;i<arr_bundle.count;i++)
  3091. {
  3092. dict_item[arr_bundle[i]]=@"1";
  3093. NSDictionary* bundlejson=[self model_subtotal:[arr_bundle[i] intValue] count:[arr_count[i] intValue]*count db:db compute_part:compute];
  3094. cuft+=[bundlejson[@"cuft"] doubleValue];
  3095. weight+=[bundlejson[@"weight"] doubleValue];
  3096. carton+=[bundlejson[@"carton"] intValue];
  3097. [dict_item addEntriesFromDictionary:bundlejson[@"items"]];
  3098. }
  3099. }
  3100. ret[@"cuft"]= [NSNumber numberWithDouble:cuft];
  3101. ret[@"weight"]= [NSNumber numberWithDouble:weight];
  3102. ret[@"carton"]= [NSNumber numberWithInteger:carton];
  3103. ret[@"items"]=dict_item;
  3104. return ret;
  3105. }
  3106. +(int) query_ordercartcount:(NSString*) orderCode db:(sqlite3*)db
  3107. {
  3108. NSString *sqlQuery = [ NSString stringWithFormat:@"select item_id from offline_cart where so_no='%@'",orderCode];
  3109. // 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 ];
  3110. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3111. DebugLog(@"offline_login sql:%@",sqlQuery);
  3112. sqlite3_stmt * statement;
  3113. int cart_count=0;
  3114. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3115. if ( dbresult== SQLITE_OK)
  3116. {
  3117. while (sqlite3_step(statement) == SQLITE_ROW)
  3118. {
  3119. int item_id = sqlite3_column_int(statement, 0);
  3120. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:1 db:db compute_part:true];
  3121. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3122. }
  3123. sqlite3_finalize(statement);
  3124. }
  3125. return cart_count;
  3126. }
  3127. +(NSData*) offline_requestcart :(NSMutableDictionary *) params
  3128. {
  3129. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3130. sqlite3 *db = [iSalesDB get_db];
  3131. UIApplication * app = [UIApplication sharedApplication];
  3132. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3133. NSString* orderCode=params[@"orderCode"];
  3134. int sort = [[params objectForKey:@"sort"] intValue];
  3135. NSString *sort_str = @"";
  3136. switch (sort) {
  3137. case 0:{
  3138. sort_str = @"order by c.modify_time desc";
  3139. }
  3140. break;
  3141. case 1:{
  3142. sort_str = @"order by c.modify_time asc";
  3143. }
  3144. break;
  3145. case 2:{
  3146. sort_str = @"order by m.name asc";
  3147. }
  3148. break;
  3149. case 3:{
  3150. sort_str = @"order by m.name desc";
  3151. }
  3152. break;
  3153. case 4:{
  3154. sort_str = @"order by m.description asc";
  3155. }
  3156. break;
  3157. default:
  3158. break;
  3159. }
  3160. 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,m.availability from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id,modify_time from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id %@",orderCode,sort_str ];
  3161. // 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 ];
  3162. // NSString *sqlQuery = @"select * from (select decrypt(str_price) from offline_cart) c";
  3163. DebugLog(@"offline_login sql:%@",sqlQuery);
  3164. sqlite3_stmt * statement;
  3165. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3166. NSDate *date1 = [NSDate date];
  3167. int count=0;
  3168. int cart_count=0;
  3169. int dbresult=sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil);
  3170. if ( dbresult== SQLITE_OK)
  3171. {
  3172. while (sqlite3_step(statement) == SQLITE_ROW)
  3173. {
  3174. // NSDate *row_date = [NSDate date];
  3175. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  3176. int product_id = sqlite3_column_int(statement, 0);
  3177. char *str_price = (char*)sqlite3_column_text(statement, 1);
  3178. int item_id = sqlite3_column_int(statement, 7);
  3179. NSString* Price=nil;
  3180. if(str_price==nil)
  3181. {
  3182. // NSNumber* price= [self get_model_default_price:appDelegate.contact_id product_id:product_id db:db];
  3183. // NSDate *price_date = [NSDate date];
  3184. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  3185. // DebugLog(@"price time interval");
  3186. // [self printTimeIntervalBetween:price_date and:[NSDate date]];
  3187. if(price==nil)
  3188. Price=@"No Price.";
  3189. else
  3190. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  3191. }
  3192. else
  3193. {
  3194. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  3195. }
  3196. double discount = sqlite3_column_double(statement, 2);
  3197. int item_count = sqlite3_column_int(statement, 3);
  3198. char *line_note = (char*)sqlite3_column_text(statement, 4);
  3199. NSString *nsline_note=nil;
  3200. if(line_note!=nil)
  3201. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  3202. char *name = (char*)sqlite3_column_text(statement, 5);
  3203. NSString *nsname=nil;
  3204. if(name!=nil)
  3205. nsname= [[NSString alloc]initWithUTF8String:name];
  3206. char *description = (char*)sqlite3_column_text(statement, 6);
  3207. NSString *nsdescription=nil;
  3208. if(description!=nil)
  3209. nsdescription= [[NSString alloc]initWithUTF8String:description];
  3210. int stockUom = sqlite3_column_int(statement, 8);
  3211. int _id = sqlite3_column_int(statement, 9);
  3212. int availability = sqlite3_column_int(statement, 10);
  3213. // NSDate *subtotal_date = [NSDate date];
  3214. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  3215. // DebugLog(@"subtotal_date time interval");
  3216. // [self printTimeIntervalBetween:subtotal_date and:[NSDate date]];
  3217. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  3218. double weight=[bsubtotaljson[@"weight"] doubleValue];
  3219. int carton=[bsubtotaljson[@"carton"] intValue];
  3220. cart_count+=[bsubtotaljson[@"items"] allKeys].count;
  3221. // itemjson[@"img_url"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db];
  3222. // NSDate *img_date = [NSDate date];
  3223. itemjson[@"img_url"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  3224. // DebugLog(@"img_date time interval");
  3225. // [self printTimeIntervalBetween:img_date and:[NSDate date]];
  3226. itemjson[@"model"]=nsname;
  3227. itemjson[@"cuft"]=[NSNumber numberWithDouble:cuft];
  3228. itemjson[@"weight"]=[NSNumber numberWithDouble:weight];
  3229. itemjson[@"carton"]=[NSNumber numberWithInt:carton];
  3230. itemjson[@"cart_item_id"]=[NSNumber numberWithInt:_id].stringValue;
  3231. itemjson[@"check"]=@"true";
  3232. itemjson[@"product_id"]=[NSNumber numberWithInt:product_id].stringValue;
  3233. itemjson[@"count"]=[NSNumber numberWithInt:item_count];
  3234. itemjson[@"unit_price"]=Price;
  3235. itemjson[@"discount"]=[NSNumber numberWithInt:discount];
  3236. itemjson[@"description"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  3237. itemjson[@"stockUom"]=[NSNumber numberWithInt:stockUom];
  3238. itemjson[@"note"]=nsline_note;
  3239. if (!appDelegate.can_create_backorder) {
  3240. itemjson[@"is_out_of_stock"] = [NSNumber numberWithBool:availability >= item_count ? NO : YES];
  3241. }
  3242. // NSDate *date2 = [NSDate date];
  3243. itemjson[@"combine"]=[self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  3244. // DebugLog(@"model_bundle time interval");
  3245. // [self printTimeIntervalBetween:date2 and:[NSDate date]];
  3246. ret[[NSString stringWithFormat:@"item_%d",count]]=itemjson;
  3247. count++;
  3248. // DebugLog(@"row time interval");
  3249. // [self printTimeIntervalBetween:row_date and:[NSDate date]];
  3250. }
  3251. ret[@"result"]=[NSNumber numberWithInt:RESULT_TRUE ];
  3252. sqlite3_finalize(statement);
  3253. }
  3254. DebugLog(@"request cart total time interval");
  3255. [self printTimeIntervalBetween:date1 and:[NSDate date]];
  3256. //int cart_count=[iSalesDB get_recordcount:db table:@"offline_cart" where:[NSString stringWithFormat:@"so_no='%@'",orderCode]];
  3257. int wish_count=[iSalesDB get_recordcount:db table:@"offline_wishlist" where:@"1=1"];
  3258. ret[@"wish_count"]=[NSNumber numberWithInt:wish_count ];
  3259. ret[@"cart_count"]=[NSNumber numberWithInt:cart_count ];
  3260. ret[@"count"]=[NSNumber numberWithInt:count ];
  3261. ret[@"mode"]=@"Regular Mode";
  3262. [iSalesDB close_db:db];
  3263. NSString *general_note = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select general_notes from offline_order where so_id = '%@';",orderCode]];
  3264. DebugLog(@"general notes :%@",general_note);
  3265. ret[@"general_note"]= general_note;
  3266. return [RAUtils dict2data:ret];
  3267. }
  3268. +(NSData*) offline_login :(NSMutableDictionary *) params
  3269. {
  3270. NSString* user = [params valueForKey:@"user"];
  3271. NSString* password = [params valueForKey:@"password"];
  3272. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3273. sqlite3 *db = [iSalesDB get_db];
  3274. 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"]];
  3275. DebugLog(@"offline_login sql:%@",sqlQuery);
  3276. sqlite3_stmt * statement;
  3277. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  3278. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3279. {
  3280. if (sqlite3_step(statement) == SQLITE_ROW)
  3281. {
  3282. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3283. NSMutableDictionary* header = [[NSMutableDictionary alloc] init];
  3284. int can_show_price = sqlite3_column_int(statement, 0);
  3285. int can_see_price = sqlite3_column_int(statement, 1);
  3286. char *contact_id = (char*)sqlite3_column_text(statement, 2);
  3287. if(contact_id==nil)
  3288. contact_id="";
  3289. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3290. int user_type = sqlite3_column_int(statement, 3);
  3291. int can_cancel_order = sqlite3_column_int(statement, 4);
  3292. int can_set_cart_price = sqlite3_column_int(statement, 5);
  3293. int can_create_portfolio = sqlite3_column_int(statement, 6);
  3294. int can_delete_order = sqlite3_column_int(statement, 7);
  3295. int can_submit_order = sqlite3_column_int(statement, 8);
  3296. int can_set_tearsheet_price = sqlite3_column_int(statement, 9);
  3297. int can_create_order = sqlite3_column_int(statement, 10);
  3298. char *mode = (char*)sqlite3_column_text(statement, 11);
  3299. if(mode==nil)
  3300. mode="";
  3301. NSString *nsmode= [[NSString alloc]initWithUTF8String:mode];
  3302. char *username = (char*)sqlite3_column_text(statement, 12);
  3303. if(username==nil)
  3304. username="";
  3305. NSString *nsusername= [[NSString alloc]initWithUTF8String:username];
  3306. int can_update_contact_info = sqlite3_column_int(statement, 13);
  3307. [header setValue:[NSNumber numberWithBool:can_show_price] forKey:@"can_show_price"];
  3308. [header setValue:[NSNumber numberWithBool:can_see_price] forKey:@"can_see_price"];
  3309. [header setValue:nscontact_id forKey:@"contact_id"];
  3310. [header setValue:[NSNumber numberWithBool:user_type] forKey:@"user_type"];
  3311. [header setValue:[NSNumber numberWithBool:can_cancel_order] forKey:@"can_cancel_order"];
  3312. [header setValue:[NSNumber numberWithBool:can_set_cart_price] forKey:@"can_set_cart_price"];
  3313. [header setValue:[NSNumber numberWithBool:can_create_portfolio] forKey:@"can_create_portfolio"];
  3314. [header setValue:[NSNumber numberWithBool:can_delete_order] forKey:@"can_delete_order"];
  3315. [header setValue:[NSNumber numberWithBool:can_submit_order] forKey:@"can_submit_order"];
  3316. [header setValue:[NSNumber numberWithBool:can_set_tearsheet_price] forKey:@"can_set_tearsheet_price"];
  3317. [header setValue:[NSNumber numberWithBool:can_create_order] forKey:@"can_create_order"];
  3318. [header setObject:@(YES) forKey:@"can_create_backorder"];
  3319. [header setValue:[NSNumber numberWithBool:can_update_contact_info] forKey:@"can_update_contact_info"];
  3320. [header setValue:nsusername forKey:@"username"];
  3321. NSError* error=nil;
  3322. NSString* statusFilter =[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"offline_status_filter_cadedate" ofType:@"json"] encoding:NSUTF8StringEncoding error:&error];
  3323. [header setValue:statusFilter forKey:@"statusFilter"];
  3324. int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3325. [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"wish_count"];
  3326. // int portfolio_count = [iSalesDB get_recordcount:db table:@"portfoliolist" where:@"1=1"];
  3327. // [header setObject:[NSString stringWithFormat:@"%d",wish_count] forKey:@"portfolio_count"];
  3328. int portfolio_count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is NULL or is_delete != 1"];
  3329. [header setObject:[NSString stringWithFormat:@"%d",portfolio_count] forKey:@"portfolio_count"];
  3330. [ret setObject:header forKey:@"header"];
  3331. [ret setValue:nsmode forKey:@"mode"];
  3332. }
  3333. sqlite3_finalize(statement);
  3334. }
  3335. [iSalesDB close_db:db];
  3336. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3337. return [RAUtils dict2data:ret];
  3338. }
  3339. +(NSDictionary*) offline_contactinfo :(NSMutableDictionary *) params
  3340. {
  3341. NSString* contactId = [params valueForKey:@"contactId"];
  3342. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3343. sqlite3 *db = [iSalesDB get_db];
  3344. NSString *sqlQuery = nil;
  3345. {
  3346. 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,type from offline_contact where contact_id='%@'",contactId];
  3347. }
  3348. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  3349. sqlite3_stmt * statement;
  3350. [ret setValue:@"2" forKey:@"result"];
  3351. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3352. {
  3353. //int i = 0;
  3354. if (sqlite3_step(statement) == SQLITE_ROW)
  3355. {
  3356. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  3357. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3358. // int editable = sqlite3_column_int(statement, 0);
  3359. char *company_name = (char*)sqlite3_column_text(statement, 1);
  3360. NSString *nscompany_name =nil;
  3361. if(company_name==nil)
  3362. nscompany_name=@"";
  3363. else
  3364. nscompany_name=[[NSString alloc]initWithUTF8String:company_name] ;
  3365. char *country = (char*)sqlite3_column_text(statement, 2);
  3366. if(country==nil)
  3367. country="";
  3368. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  3369. // char *addr = (char*)sqlite3_column_text(statement, 3);
  3370. // if(addr==nil)
  3371. // addr="";
  3372. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  3373. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  3374. if(zipcode==nil)
  3375. zipcode="";
  3376. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  3377. char *state = (char*)sqlite3_column_text(statement, 5);
  3378. if(state==nil)
  3379. state="";
  3380. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  3381. char *city = (char*)sqlite3_column_text(statement, 6);
  3382. if(city==nil)
  3383. city="";
  3384. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  3385. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  3386. // NSString *nscontact_name = nil;
  3387. // if(contact_name==nil)
  3388. // nscontact_name=@"";
  3389. // else
  3390. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  3391. char *phone = (char*)sqlite3_column_text(statement, 8);
  3392. NSString *nsphone = nil;
  3393. if(phone==nil)
  3394. nsphone=@"";
  3395. else
  3396. nsphone= [[NSString alloc]initWithUTF8String:phone];
  3397. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  3398. if(contact_id==nil)
  3399. contact_id="";
  3400. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  3401. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  3402. if(addr_1==nil)
  3403. addr_1="";
  3404. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  3405. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  3406. if(addr_2==nil)
  3407. addr_2="";
  3408. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  3409. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  3410. if(addr_3==nil)
  3411. addr_3="";
  3412. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  3413. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  3414. if(addr_4==nil)
  3415. addr_4="";
  3416. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  3417. char *first_name = (char*)sqlite3_column_text(statement, 14);
  3418. if(first_name==nil)
  3419. first_name="";
  3420. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  3421. char *last_name = (char*)sqlite3_column_text(statement, 15);
  3422. if(last_name==nil)
  3423. last_name="";
  3424. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  3425. char *fax = (char*)sqlite3_column_text(statement, 16);
  3426. NSString *nsfax = nil;
  3427. if(fax==nil)
  3428. nsfax=@"";
  3429. else
  3430. nsfax= [[NSString alloc]initWithUTF8String:fax];
  3431. char *email = (char*)sqlite3_column_text(statement, 17);
  3432. NSString *nsemail = nil;
  3433. if(email==nil)
  3434. nsemail=@"";
  3435. else
  3436. nsemail= [[NSString alloc]initWithUTF8String:email];
  3437. char *img_0 = (char*)sqlite3_column_text(statement, 18);
  3438. NSString *nsimg_0 = nil;
  3439. if(img_0==nil)
  3440. nsimg_0=@"";
  3441. else
  3442. nsimg_0= [[NSString alloc]initWithUTF8String:img_0];
  3443. [self copy_bcardImg:nsimg_0];
  3444. char *img_1 = (char*)sqlite3_column_text(statement, 19);
  3445. NSString *nsimg_1 = nil;
  3446. if(img_1==nil)
  3447. nsimg_1=@"";
  3448. else
  3449. nsimg_1= [[NSString alloc]initWithUTF8String:img_1];
  3450. [self copy_bcardImg:nsimg_1];
  3451. char *img_2 = (char*)sqlite3_column_text(statement, 20);
  3452. NSString *nsimg_2 = nil;
  3453. if(img_2==nil)
  3454. nsimg_2=@"";
  3455. else
  3456. nsimg_2= [[NSString alloc]initWithUTF8String:img_2];
  3457. [self copy_bcardImg:nsimg_2];
  3458. char *price_type = (char*)sqlite3_column_text(statement, 21);
  3459. NSString *nsprice_type = nil;
  3460. if(price_type==nil)
  3461. nsprice_type=@"";
  3462. else
  3463. nsprice_type= [[NSString alloc]initWithUTF8String:price_type];
  3464. char *notes = (char*)sqlite3_column_text(statement, 22);
  3465. NSString *nsnotes = nil;
  3466. if(notes==nil)
  3467. nsnotes=@"";
  3468. else
  3469. nsnotes= [[NSString alloc]initWithUTF8String:notes];
  3470. char *salesrep = (char*)sqlite3_column_text(statement, 23);
  3471. NSString *nssalesrep = nil;
  3472. if(salesrep==nil)
  3473. nssalesrep=@"";
  3474. else
  3475. nssalesrep= [[NSString alloc]initWithUTF8String:salesrep];
  3476. NSString *contact_type = [self textAtColumn:24 statement:statement];
  3477. {
  3478. // decrypt
  3479. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  3480. // nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  3481. nsphone=[AESCrypt fastdecrypt:nsphone];
  3482. // nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3483. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  3484. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  3485. }
  3486. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  3487. [arr_name addObject:nsfirst_name];
  3488. [arr_name addObject:nslast_name];
  3489. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  3490. NSMutableArray* arr_ext= [[NSMutableArray alloc] init];
  3491. [arr_ext addObject:nsaddr_1];
  3492. [arr_ext addObject:nsaddr_2];
  3493. [arr_ext addObject:nsaddr_3];
  3494. [arr_ext addObject:nsaddr_4];
  3495. [arr_ext addObject:@"\r\n"];
  3496. [arr_ext addObject:nscity];
  3497. [arr_ext addObject:nsstate];
  3498. [arr_ext addObject:nszipcode];
  3499. [arr_ext addObject:nscountry];
  3500. NSString *nsext=[RAUtils arr2string:arr_ext separator:@", " trim:true];
  3501. nsext=[nsext stringByReplacingOccurrencesOfString:@", \r\n," withString:@"\r\n"];
  3502. [item setValue:nsimg_2 forKey:@"business_card_2"];
  3503. [item setValue:nsimg_0 forKey:@"business_card_0"];
  3504. [item setValue:nscountry forKey:@"customer_country"];
  3505. [item setValue:nsphone forKey:@"customer_phone"];
  3506. [item setValue:nsimg_1 forKey:@"business_card_1"];
  3507. [item setValue:nscompany_name forKey:@"customer_name"];
  3508. [item setValue:nsprice_type forKey:@"customer_price_type"];
  3509. [item setValue:nsfirst_name forKey:@"customer_first_name"];
  3510. [item setValue:nsext forKey:@"customer_contact_ext"];
  3511. [item setValue:nszipcode forKey:@"customer_zipcode"];
  3512. [item setValue:nsaddr_1 forKey:@"customer_address1"];
  3513. [item setValue:nsaddr_2 forKey:@"customer_address2"];
  3514. [item setValue:nsaddr_3 forKey:@"customer_address3"];
  3515. [item setValue:nsaddr_4 forKey:@"customer_address4"];
  3516. [item setValue:nsnotes forKey:@"customer_contact_notes"];
  3517. [item setValue:nslast_name forKey:@"customer_last_name"];
  3518. [item setValue:nscity forKey:@"customer_city"];
  3519. [item setValue:nsstate forKey:@"customer_state"];
  3520. [item setValue:nssalesrep forKey:@"customer_sales_rep"];
  3521. [item setValue:contactId forKey:@"customer_cid"];
  3522. [item setValue:nscontact_name forKey:@"customer_contact"];
  3523. [item setValue:nsfax forKey:@"customer_fax"];
  3524. [item setValue:nsemail forKey:@"customer_email"];
  3525. [item setValue:contact_type forKey:@"customer_contact_type"];
  3526. [ret setObject:item forKey:@"customerInfo"];
  3527. // i++;
  3528. }
  3529. UIApplication * app = [UIApplication sharedApplication];
  3530. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3531. [ret setValue:appDelegate.mode forKey:@"mode"];
  3532. [ret setValue:[NSNumber numberWithInt:1 ] forKey:@"count"];
  3533. sqlite3_finalize(statement);
  3534. }
  3535. [iSalesDB close_db:db];
  3536. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  3537. return ret;
  3538. }
  3539. + (bool) copy_bcardImg:(NSString*) filename
  3540. {
  3541. if(filename.length==0)
  3542. return false;
  3543. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3544. bool ret=false;
  3545. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3546. NSString *cachefolder = [paths objectAtIndex:0];
  3547. // NSString* from_subpath= [fromPath stringByReplacingOccurrencesOfString:cachefolder withString:@""];
  3548. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3549. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3550. NSString * fromPath= [offline_createimg stringByAppendingPathComponent:filename];
  3551. NSString * toPath= [img_cache stringByAppendingPathComponent:filename];
  3552. // NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3553. //
  3554. // NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3555. NSFileManager* fileManager = [NSFileManager defaultManager];
  3556. BOOL bdir=NO;
  3557. if(! [fileManager fileExistsAtPath:toPath isDirectory:&bdir])
  3558. {
  3559. NSError *error = nil;
  3560. if(![fileManager copyItemAtPath:fromPath toPath:toPath error:&error])
  3561. {
  3562. ret=false;
  3563. }
  3564. else
  3565. {
  3566. ret=true;
  3567. }
  3568. // NSError *error = nil;
  3569. // bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3570. //
  3571. // if(!bsuccess)
  3572. // {
  3573. // DebugLog(@"Create offline_createimg folder failed");
  3574. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3575. // return [RAUtils dict2data:ret];
  3576. // }
  3577. // if(bsuccess)
  3578. // {
  3579. // sqlite3 *db = [self get_db];
  3580. //
  3581. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3582. // [iSalesDB close_db:db];
  3583. // }
  3584. }
  3585. return ret;
  3586. //
  3587. // bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3588. // if(bsuccess)
  3589. // {
  3590. // NSError *error = nil;
  3591. // if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3592. // {
  3593. // [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3594. // }
  3595. // else
  3596. // {
  3597. // [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3598. //
  3599. // ret[@"img_url_aname"]=filename;
  3600. // ret[@"img_url"]=savedImagePath;
  3601. // }
  3602. // }
  3603. }
  3604. +(NSData *) offline_saveBusinesscard:(NSData *) image
  3605. {
  3606. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3607. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  3608. NSString *cachefolder = [paths objectAtIndex:0];
  3609. NSString *offline_createimg = [cachefolder stringByAppendingPathComponent:@"offline_createimg"];
  3610. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"img_cache"];
  3611. NSFileManager* fileManager = [NSFileManager defaultManager];
  3612. BOOL bdir=YES;
  3613. if(! [fileManager fileExistsAtPath:offline_createimg isDirectory:&bdir])
  3614. {
  3615. NSError *error = nil;
  3616. bool bsuccess=[fileManager createDirectoryAtPath:offline_createimg withIntermediateDirectories:YES attributes:nil error:&error];
  3617. if(!bsuccess)
  3618. {
  3619. DebugLog(@"Create offline_createimg folder failed");
  3620. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3621. return [RAUtils dict2data:ret];
  3622. }
  3623. // if(bsuccess)
  3624. // {
  3625. // sqlite3 *db = [self get_db];
  3626. //
  3627. // [self execSql:[NSString stringWithFormat:@"insert into img_cache(name) values('%@')",name] db:db];
  3628. // [iSalesDB close_db:db];
  3629. // }
  3630. }
  3631. // NSData *imagedata=UIImagePNGRepresentation(self.imgview.image);
  3632. //JEPG格式
  3633. //NSData *imagedata=UIImageJEPGRepresentation(m_imgFore,1.0);
  3634. NSString* filename =[NSString stringWithFormat:@"%@.png",[[NSUUID UUID] UUIDString]];
  3635. NSString *savedImagePath=[offline_createimg stringByAppendingPathComponent:filename];
  3636. NSString *cacheImagePath=[img_cache stringByAppendingPathComponent:filename];
  3637. bool bsuccess=[image writeToFile:savedImagePath atomically:YES];
  3638. if(bsuccess)
  3639. {
  3640. NSError *error = nil;
  3641. if(![fileManager copyItemAtPath:savedImagePath toPath:cacheImagePath error:&error])
  3642. {
  3643. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3644. }
  3645. else
  3646. {
  3647. [ret setValue:[NSNumber numberWithInt:RESULT_TRUE] forKey:@"result"];
  3648. ret[@"img_url_aname"]=filename;
  3649. ret[@"img_url"]=filename;
  3650. }
  3651. }
  3652. else
  3653. {
  3654. [ret setValue:[NSNumber numberWithInt:RESULT_ERROR] forKey:@"result"];
  3655. }
  3656. return [RAUtils dict2data:ret];
  3657. }
  3658. +(NSDictionary*) model_NIYMAL:(NSString*) category db:(sqlite3 *)db
  3659. {
  3660. // MODEL DETAIL NEW ITEM YOU MAY ALSO LIKE
  3661. category = [category substringToIndex:3];
  3662. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3663. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  3664. params[@"category"]= category;
  3665. ret[@"params"]= params;
  3666. [ret setValue:@"detail" forKey:@"target"];
  3667. [ret setValue:@"popup" forKey:@"action"];
  3668. [ret setValue:@"content" forKey:@"type"];
  3669. [ret setValue:@"New Items You May Also Like" forKey:@"title"];
  3670. [ret setValue:@"model_NIYMAL" forKey:@"data_interface"];
  3671. [ret setValue:@"true" forKey:@"single_row"];
  3672. [ret setValue:@"true" forKey:@"partial_refresh"];
  3673. // sqlite3 *db = [iSalesDB get_db];
  3674. // int count = [iSalesDB get_recordcount:db table:@"model" where:[NSString stringWithFormat:@"category like'#%%%@%%#'",category]];
  3675. 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 ;
  3676. sqlite3_stmt * statement;
  3677. int count = 0;
  3678. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"count"];
  3679. // int count=0;
  3680. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3681. {
  3682. int i=0;
  3683. while (sqlite3_step(statement) == SQLITE_ROW)
  3684. {
  3685. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3686. // char *name = (char*)sqlite3_column_text(statement, 1);
  3687. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3688. char *name = (char*)sqlite3_column_text(statement, 0);
  3689. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  3690. int product_id = sqlite3_column_int(statement, 1);
  3691. char *url = (char*)sqlite3_column_text(statement, 2);
  3692. if(url==nil)
  3693. url="";
  3694. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3695. [item setValue:nsurl forKey:@"picture_path"];
  3696. [item setValue:nsname forKey:@"fash_name"];
  3697. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  3698. [item setValue:category forKey:@"category"];
  3699. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3700. i++;
  3701. }
  3702. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3703. sqlite3_finalize(statement);
  3704. }
  3705. DebugLog(@"count:%d",count);
  3706. // [iSalesDB close_db:db];
  3707. return ret;
  3708. }
  3709. +(NSDictionary*) model_priceKvItem:(NSString*) key value:(NSString*)value
  3710. {
  3711. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3712. [ret setValue:key forKey:@"key"];
  3713. [ret setValue:value forKey:@"val"];
  3714. [ret setValue:@"price" forKey:@"type"];
  3715. return ret;
  3716. }
  3717. +(NSDictionary*) model_kvItem:(NSString*) key value:(NSString*)value
  3718. {
  3719. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3720. [ret setValue:key forKey:@"key"];
  3721. [ret setValue:value forKey:@"val"];
  3722. return ret;
  3723. }
  3724. +(NSDictionary*) model_property :(int)product_id field:(NSString*) field db:(sqlite3 *)db
  3725. {
  3726. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3727. [ret setValue:@"0" forKey:@"img_count"];
  3728. // sqlite3 *db = [iSalesDB get_db];
  3729. 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 ;
  3730. sqlite3_stmt * statement;
  3731. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3732. {
  3733. int i=0;
  3734. if (sqlite3_step(statement) == SQLITE_ROW)
  3735. {
  3736. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3737. // char *name = (char*)sqlite3_column_text(statement, 1);
  3738. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3739. char *value = (char*)sqlite3_column_text(statement, 0);
  3740. if(value==nil)
  3741. value="";
  3742. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3743. char *key = (char*)sqlite3_column_text(statement, 1);
  3744. if(key==nil)
  3745. key="";
  3746. NSString *nskey = [[NSString alloc]initWithUTF8String:key];
  3747. [item setValue:nsvalue forKey:@"val"];
  3748. [item setValue:nskey forKey:@"key"];
  3749. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3750. [ret setValue:@"1" forKey:@"count"];
  3751. i++;
  3752. }
  3753. sqlite3_finalize(statement);
  3754. }
  3755. // [iSalesDB close_db:db];
  3756. return ret;
  3757. }
  3758. +(NSDictionary*) model_selector :(NSString*)product_group field:(NSString*) field db:(sqlite3*)db
  3759. {
  3760. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3761. [ret setValue:@"0" forKey:@"count"];
  3762. // sqlite3 *db = [iSalesDB get_db];
  3763. 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;
  3764. sqlite3_stmt * statement;
  3765. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3766. {
  3767. int i=0;
  3768. while (sqlite3_step(statement) == SQLITE_ROW)
  3769. {
  3770. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3771. // char *name = (char*)sqlite3_column_text(statement, 1);
  3772. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3773. char *value = (char*)sqlite3_column_text(statement, 0);
  3774. if(value==nil)
  3775. value="";
  3776. NSString *nsvalue = [[NSString alloc]initWithUTF8String:value];
  3777. char *selector_display = (char*)sqlite3_column_text(statement, 1);
  3778. if(selector_display==nil)
  3779. selector_display="";
  3780. NSString *nsselector_display = [[NSString alloc]initWithUTF8String:selector_display];
  3781. int product_id = sqlite3_column_int(statement, 2);
  3782. char *category = (char*)sqlite3_column_text(statement, 3);
  3783. if(category==nil)
  3784. category="";
  3785. NSString *nscategory = [[NSString alloc]initWithUTF8String:category];
  3786. NSString* url = [self model_category_img:[NSNumber numberWithInt:product_id].stringValue model_name:nil db:db];//[self category_img:product_id];
  3787. [item setValue:nsvalue forKey:@"title"];
  3788. [item setValue:url forKey:@"pic_url"];
  3789. NSMutableDictionary* params=[[NSMutableDictionary alloc] init];
  3790. [params setValue:@"2" forKey:@"count"];
  3791. NSMutableDictionary* param0=[[NSMutableDictionary alloc] init];
  3792. [param0 setValue:[NSNumber numberWithInt:product_id ] forKey:@"val"];
  3793. [param0 setValue:@"product_id" forKey:@"name"];
  3794. NSMutableDictionary* param1=[[NSMutableDictionary alloc] init];
  3795. [param1 setValue:nscategory forKey:@"val"];
  3796. [param1 setValue:@"category" forKey:@"name"];
  3797. [params setObject:param0 forKey:@"param_0"];
  3798. [params setObject:param1 forKey:@"param_1"];
  3799. [item setObject:params forKey:@"params"];
  3800. [ret setValue:nsselector_display forKey:@"name"];
  3801. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  3802. i++;
  3803. }
  3804. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  3805. [ret setValue:@"switch" forKey:@"action"];
  3806. sqlite3_finalize(statement);
  3807. }
  3808. // [iSalesDB close_db:db];
  3809. return ret;
  3810. }
  3811. +(NSMutableDictionary*) category_img :(int)product_id db:(sqlite3 *)db
  3812. {
  3813. // model 在 category search 显示的图片。
  3814. NSMutableDictionary* ret =[[NSMutableDictionary alloc]init];
  3815. // sqlite3 *db = [iSalesDB get_db];
  3816. 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];
  3817. sqlite3_stmt * statement;
  3818. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3819. {
  3820. while (sqlite3_step(statement) == SQLITE_ROW)
  3821. {
  3822. char *url = (char*)sqlite3_column_text(statement, 0);
  3823. if(url==nil)
  3824. url="";
  3825. int type = sqlite3_column_int(statement, 1);
  3826. if(type==0)
  3827. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img"];
  3828. else
  3829. [ret setValue:[[NSString alloc]initWithUTF8String:url] forKey:@"img_big"];
  3830. }
  3831. sqlite3_finalize(statement);
  3832. }
  3833. // [iSalesDB close_db:db];
  3834. return ret;
  3835. }
  3836. +(int) product_itemid:(int)product_id db:(sqlite3*)db
  3837. {
  3838. int item_id=-1;
  3839. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id from product where product_id=%d;",product_id];
  3840. sqlite3_stmt * statement;
  3841. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3842. {
  3843. if (sqlite3_step(statement) == SQLITE_ROW)
  3844. {
  3845. item_id = sqlite3_column_int(statement, 0);
  3846. }
  3847. sqlite3_finalize(statement);
  3848. }
  3849. return item_id;
  3850. }
  3851. +(NSArray*) model_bundle:(int)item_id db:(sqlite3*)db count:(NSArray**)count //DEPRECATED(1_5)
  3852. {
  3853. // NSString* ret = @"";
  3854. NSMutableArray* arr_bundle = [[NSMutableArray alloc]init];
  3855. NSMutableArray* arr_count = [[NSMutableArray alloc]init];
  3856. NSString *sqlQuery = [NSString stringWithFormat:@"select item_id,qty from bundle where product_item_id=%d;",item_id];
  3857. sqlite3_stmt * statement;
  3858. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3859. {
  3860. while (sqlite3_step(statement) == SQLITE_ROW)
  3861. {
  3862. int bitem_id = sqlite3_column_int(statement, 0);
  3863. int bitem_qty = sqlite3_column_int(statement, 1);
  3864. [arr_bundle addObject:[NSNumber numberWithInt:bitem_id] ];
  3865. [arr_count addObject:[NSNumber numberWithInt:bitem_qty] ];
  3866. }
  3867. // ret=[RAUtils arr2string:arr_bundle separator:@";" trim:true];
  3868. sqlite3_finalize(statement);
  3869. }
  3870. // if(ret==nil)
  3871. // ret=@"";
  3872. *count=arr_count;
  3873. return arr_bundle;
  3874. }
  3875. +(int) model_stockUom :(int)product_id db:(sqlite3*)db
  3876. {
  3877. // get default sold qty, return -1 if model not found;
  3878. int ret = -1;
  3879. NSString *sqlQuery = [NSString stringWithFormat:@"select stockUom from product where product_id=%d;",product_id];// select stockUom from model where product_id=%d;
  3880. sqlite3_stmt * statement;
  3881. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3882. {
  3883. if (sqlite3_step(statement) == SQLITE_ROW)
  3884. {
  3885. ret = sqlite3_column_int(statement, 0);
  3886. }
  3887. sqlite3_finalize(statement);
  3888. }
  3889. return ret;
  3890. }
  3891. +(NSDictionary*) model_img :(int)product_id db:(sqlite3*)db
  3892. {
  3893. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  3894. [ret setValue:@"0" forKey:@"img_count"];
  3895. // sqlite3 *db = [iSalesDB get_db];
  3896. NSString *sqlQuery = [NSString stringWithFormat:@"select url from model_image where product_id=%d and type=1;",product_id];
  3897. sqlite3_stmt * statement;
  3898. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  3899. {
  3900. int i=0;
  3901. while (sqlite3_step(statement) == SQLITE_ROW)
  3902. {
  3903. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  3904. // char *name = (char*)sqlite3_column_text(statement, 1);
  3905. // NSString *nsNameStr = [[NSString alloc]initWithUTF8String:name];
  3906. char *url = (char*)sqlite3_column_text(statement, 0);
  3907. if(url==nil)
  3908. url="";
  3909. NSString *nsurl = [[NSString alloc]initWithUTF8String:url];
  3910. [item setValue:nsurl forKey:@"s"];
  3911. [item setValue:nsurl forKey:@"l"];
  3912. [ret setObject:item forKey:[NSString stringWithFormat:@"img_%d",i]];
  3913. i++;
  3914. }
  3915. [ret setValue:[NSString stringWithFormat:@"%d",i] forKey:@"img_count"];
  3916. sqlite3_finalize(statement);
  3917. }
  3918. // [iSalesDB close_db:db];
  3919. return ret;
  3920. }
  3921. + (NSDictionary *) offline_add2wishlist:(NSMutableDictionary *)params {
  3922. UIApplication * app = [UIApplication sharedApplication];
  3923. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  3924. NSMutableDictionary* ret=[[NSMutableDictionary alloc]init];
  3925. sqlite3 *db = [iSalesDB get_db];
  3926. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  3927. NSString* product_id=params[@"product_id"];
  3928. NSString *item_count_str = params[@"item_count"];
  3929. NSArray* arr=[RAUtils string2arr:product_id separator:@","];
  3930. NSArray *item_count_arr = item_count_str ? [RAUtils string2arr:item_count_str separator:@","] : nil;
  3931. NSString* sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3932. // NSString *sql = @"";
  3933. for(int i=0;i<arr.count;i++)
  3934. {
  3935. NSString* where=[NSString stringWithFormat:@"product_id=%@",arr[i]];
  3936. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:where];
  3937. __block int cart_count = 0;
  3938. if (!item_count_str) {
  3939. NSString *product_sold_qty_sql = [NSString stringWithFormat:@"select model_set from product where product_id = %@",arr[i]];
  3940. [iSalesDB jk_query:product_sold_qty_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  3941. NSString *model_set = [self textAtColumn:0 statement:stmt];
  3942. NSArray *model_set_components = [model_set componentsSeparatedByString:@" "];
  3943. cart_count = [[model_set_components lastObject] integerValue];
  3944. }];
  3945. }
  3946. if(count==0)
  3947. {
  3948. sqlQuery = [NSString stringWithFormat:@"insert into wishlist(product_id,qty) values(?,?);"];
  3949. sqlite3_stmt *stmt;
  3950. sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &stmt, nil);
  3951. sqlite3_bind_text(stmt,1,[arr[i] UTF8String],[arr[i] length],NULL);
  3952. if (item_count_arr) {
  3953. sqlite3_bind_int(stmt,2,[item_count_arr[i] integerValue]);
  3954. } else {
  3955. sqlite3_bind_int(stmt,2,cart_count);
  3956. }
  3957. if (sqlite3_step(stmt) == SQLITE_ERROR) {
  3958. [iSalesDB execSql:@"ROLLBACK" db:db];
  3959. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR ];
  3960. [iSalesDB close_db:db];
  3961. DebugLog(@"add to wishlist error");
  3962. return ret;
  3963. }
  3964. } else {
  3965. int qty = 0;
  3966. if (item_count_arr) {
  3967. qty = [item_count_arr[i] integerValue];
  3968. } else {
  3969. qty = cart_count;
  3970. }
  3971. sqlQuery = [NSString stringWithFormat:@"update wishlist set qty = 1 where product_id = %@ and (is_delete is null or is_delete != 1);update wishlist set qty = %d,is_delete = 0 where product_id = %@ and is_delete = 1;",arr[i],qty,arr[i]];
  3972. if ([iSalesDB execSql:sqlQuery db:db] == RESULT_FALSE) {
  3973. [iSalesDB execSql:@"ROLLBACK" db:db];
  3974. ret[@"result"]=[NSNumber numberWithInt:RESULT_ERROR];
  3975. [iSalesDB close_db:db];
  3976. DebugLog(@"add to wishlist error");
  3977. return ret;
  3978. }
  3979. }
  3980. }
  3981. // [iSalesDB execSql:sql db:db];
  3982. int count=[iSalesDB get_recordcount:db table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  3983. [iSalesDB execSql:@"END TRANSACTION" db:db];
  3984. // int wish_count = [iSalesDB get_recordcount:db table:@"wishlist" where:@"1=1"];
  3985. [ret setValue:[NSNumber numberWithInteger:count] forKey:@"wish_count"];
  3986. [iSalesDB close_db:db];
  3987. appDelegate.wish_count =count;
  3988. [appDelegate update_count_mark];
  3989. ret[@"result"]= [NSNumber numberWithInt:2];
  3990. return ret;
  3991. }
  3992. + (NSData *) addAll:(NSMutableDictionary *) params from:(int)from {
  3993. // 0 category
  3994. // 1 search
  3995. // 2 itemsearch
  3996. NSData *ret = nil;
  3997. NSDictionary *items = nil;
  3998. switch (from) {
  3999. case 0:{
  4000. items = [[self categoryList:params limited:NO] objectForKey:@"items"];
  4001. }
  4002. break;
  4003. case 1:{
  4004. items = [[self search:params limited:NO] objectForKey:@"items"];
  4005. }
  4006. break;
  4007. case 2:{
  4008. items = [[self itemsearch:params limited:NO] objectForKey:@"items"];
  4009. }
  4010. break;
  4011. default:
  4012. break;
  4013. }
  4014. if (!items) {
  4015. return ret;
  4016. }
  4017. int count = [[items objectForKey:@"count"] intValue];
  4018. NSMutableString *product_id_str = [@"" mutableCopy];
  4019. for (int i = 0; i < count; i++) {
  4020. NSString *key = [NSString stringWithFormat:@"item_%d",i];
  4021. NSDictionary *item = [items objectForKey:key];
  4022. NSString *product_id = [item objectForKey:@"product_id"];
  4023. if (i == 0) {
  4024. [product_id_str appendString:product_id];
  4025. } else {
  4026. [product_id_str appendString:[NSString stringWithFormat:@",%@",product_id]];
  4027. }
  4028. }
  4029. NSString *add_to = [self valueInParams:params key:@"addTo"];
  4030. if ([add_to isEqualToString:@"cart"]) {
  4031. NSString *order_code = [params objectForKey:@"orderCode"];
  4032. if (order_code.length) {
  4033. NSDictionary *newParams = @{
  4034. @"product_id" : product_id_str,
  4035. @"orderCode" : order_code
  4036. };
  4037. ret = [self offline_add2cart:newParams.mutableCopy];
  4038. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:ret options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4039. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4040. ret = [RAUtils dict2data:retDic];
  4041. }
  4042. } else if([add_to isEqualToString:@"wishlist"]) {
  4043. NSDictionary *newParams = @{
  4044. @"product_id" : product_id_str
  4045. };
  4046. NSMutableDictionary *retDic = [self offline_add2wishlist:newParams.mutableCopy].mutableCopy;
  4047. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4048. ret = [RAUtils dict2data:retDic];
  4049. } else if([add_to isEqualToString:@"portfolio"]) {
  4050. NSDictionary *newParams = @{
  4051. @"product_id" : product_id_str
  4052. };
  4053. NSData *data = [self offline_add2Portfolio:newParams.mutableCopy];
  4054. NSMutableDictionary *retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4055. [retDic setValue:[NSNumber numberWithInteger:count] forKey:@"count"];
  4056. ret = [RAUtils dict2data:retDic];
  4057. }
  4058. return ret;
  4059. }
  4060. +(NSData*) offline_categoryaddall :(NSMutableDictionary *) params
  4061. {
  4062. return [self addAll:params from:0];
  4063. }
  4064. +(NSData*) offline_searchaddall :(NSMutableDictionary *) params
  4065. {
  4066. return [self addAll:params from:1];
  4067. }
  4068. +(NSData*) offline_itemsearchaddall :(NSMutableDictionary *) params
  4069. {
  4070. return [self addAll:params from:2];
  4071. }
  4072. #pragma mark - Jack
  4073. #warning 做SQL操作时转义!!
  4074. + (NSDictionary *)offline_getAllCountryDefault:(NSString *)countryCode {
  4075. // "val_227" : {
  4076. // "check" : 1,
  4077. // "value" : "US United States",
  4078. // "value_id" : "228"
  4079. // },
  4080. if (!countryCode) {
  4081. countryCode = @"US";
  4082. }
  4083. NSMutableDictionary *ret = [[iSalesDB jk_query:@"select _id,name,code,countrycode_id from offline_country;" completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container,long *count) {
  4084. char *name = (char *) sqlite3_column_text(stmt, 1); // 全称
  4085. char *code = (char *) sqlite3_column_text(stmt, 2); // 缩写
  4086. int code_id = sqlite3_column_int(stmt, 3); // id
  4087. NSMutableDictionary *countryDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4088. [countryDic setValue:[NSString stringWithFormat:@"%s",name] forKey:@"value"];
  4089. [countryDic setValue:[NSString stringWithFormat:@"%d",code_id] forKey:@"value_id"];
  4090. [countryDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4091. if ([countryCode isEqualToString:[NSString stringWithUTF8String:code]]) {
  4092. [countryDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4093. }
  4094. long n = *count;
  4095. *count = n + 1;
  4096. NSString *key = [NSString stringWithFormat:@"val_%ld",n];
  4097. [container setValue:countryDic forKey:key];
  4098. }] mutableCopy];
  4099. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4100. return ret;
  4101. }
  4102. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code{
  4103. countryCode = [self translateSingleQuote:countryCode];
  4104. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  4105. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4106. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  4107. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  4108. if (name == NULL) {
  4109. name = "";
  4110. }
  4111. if (code == NULL) {
  4112. code = "";
  4113. }
  4114. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4115. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4116. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4117. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4118. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  4119. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4120. }
  4121. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4122. [container setValue:stateDic forKey:key];
  4123. } failure:^(NSMutableDictionary *container,NSString *err_msg){
  4124. DebugLog(@"query all state error: %@",err_msg);
  4125. // NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4126. // [stateDic setValue:@"Other" forKey:@"value"];
  4127. // [stateDic setValue:@"" forKey:@"value_id"];
  4128. // [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4129. //
  4130. // if (state_code && [@"" isEqualToString:state_code]) {
  4131. // [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4132. // }
  4133. //
  4134. // NSString *key = [NSString stringWithFormat:@"val_0"];
  4135. // [container setValue:stateDic forKey:key];
  4136. }] mutableCopy];
  4137. [ret removeObjectForKey:@"result"];
  4138. // failure 可以不用了,一样的
  4139. if (ret.allKeys.count == 0) {
  4140. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4141. [stateDic setValue:@"Other" forKey:@"value"];
  4142. [stateDic setValue:@"" forKey:@"value_id"];
  4143. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4144. if (state_code && [@"" isEqualToString:state_code]) {
  4145. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4146. }
  4147. NSString *key = [NSString stringWithFormat:@"val_0"];
  4148. [ret setValue:stateDic forKey:key];
  4149. }
  4150. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4151. return ret;
  4152. }
  4153. + (NSDictionary *)offline_getStateByCountryCodeId:(NSString *)codeId checkedState:(NSString *)stateCode {
  4154. codeId = [self translateSingleQuote:codeId];
  4155. 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];
  4156. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4157. char *name = (char *) sqlite3_column_text(stmt, 0); // value
  4158. char *code = (char *) sqlite3_column_text(stmt, 1); // value_id
  4159. if (name == NULL) {
  4160. name = "";
  4161. }
  4162. if (code == NULL) {
  4163. code = "";
  4164. }
  4165. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  4166. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4167. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  4168. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4169. if (stateCode && [[NSString stringWithUTF8String:code] isEqualToString:stateCode]) {
  4170. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4171. }
  4172. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4173. [container setValue:stateDic forKey:key];
  4174. }] mutableCopy];
  4175. [ret removeObjectForKey:@"result"];
  4176. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4177. return ret;
  4178. }
  4179. + (NSDictionary *)offline_getPrice {
  4180. NSString *sql = [NSString stringWithFormat:@"select _id,name,type,order_by from price where is_show = 1;"];
  4181. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4182. char *name = (char *) sqlite3_column_text(stmt, 1);
  4183. int type = sqlite3_column_int(stmt, 2);
  4184. int orderBy = sqlite3_column_int(stmt, 3);
  4185. if (name == NULL) {
  4186. name = "";
  4187. }
  4188. NSMutableDictionary *priceDic = [NSMutableDictionary dictionary];
  4189. [priceDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  4190. [priceDic setValue:[NSNumber numberWithInt:type] forKey:@"value_id"];
  4191. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  4192. [priceDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  4193. if (orderBy == 0) {
  4194. [priceDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4195. }
  4196. [container setValue:priceDic forKey:key];
  4197. }] mutableCopy];
  4198. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4199. return ret;
  4200. }
  4201. + (NSDictionary *)offline_getContactTypeChecked:(NSString *)type {
  4202. NSString *sql = @"select _id,type_name from contact_type where is_show = 1";
  4203. __block NSMutableDictionary *ret = [NSMutableDictionary dictionary];
  4204. [ret setValue:[NSNumber numberWithInt:0] forKey:@"count"];
  4205. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4206. int _id = sqlite3_column_int(stmt, 0);
  4207. NSString *name = [self textAtColumn:1 statement:stmt];
  4208. NSDictionary *typeDic = @{
  4209. @"value_id" : name,
  4210. @"value" : name,
  4211. @"check" : type && [type isEqualToString:name] ? @(1) : !type && *count == 0 ? @(1) : @(0)
  4212. };
  4213. [ret setValue:typeDic forKey:[NSString stringWithFormat:@"val_%ld",*count]];
  4214. [ret setValue:[NSNumber numberWithInt:++(*count)] forKey:@"count"];
  4215. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  4216. }];
  4217. return ret;
  4218. }
  4219. + (NSDictionary *)offline_getSalesRep {
  4220. // 首先从offline_login表中取出sales_code
  4221. AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
  4222. NSString *user = app.user;
  4223. user = [self translateSingleQuote:user];
  4224. NSString *loginSql = [NSString stringWithFormat:@"select sales_code from offline_login where username = '%@'",user];
  4225. __block NSString *user_code = @"";
  4226. [iSalesDB jk_query:loginSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4227. char *code = (char *)sqlite3_column_text(stmt, 0);
  4228. if (code == NULL) {
  4229. code = "";
  4230. }
  4231. user_code = [NSString stringWithUTF8String:code];
  4232. }];
  4233. // 再取所有salesRep
  4234. NSString *sql = [NSString stringWithFormat:@"select _id,name,code,salesrep_id from offline_salesrep;"];
  4235. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4236. // 1 name 2 code 3 salesrep_id
  4237. char *name = (char *)sqlite3_column_text(stmt, 1);
  4238. char *code = (char *)sqlite3_column_text(stmt, 2);
  4239. int salesrep_id = sqlite3_column_int(stmt, 3);
  4240. if (name == NULL) {
  4241. name = "";
  4242. }
  4243. if (code == NULL) {
  4244. code = "";
  4245. }
  4246. NSMutableDictionary *repDic = [NSMutableDictionary dictionary];
  4247. [repDic setValue:[NSString stringWithFormat:@"%s - %s",code,name] forKey:@"value"];
  4248. [repDic setValue:[NSNumber numberWithInt:salesrep_id] forKey:@"value_id"];
  4249. // 比较code 相等则check
  4250. if ([[NSString stringWithUTF8String:code] isEqualToString:user_code]) {
  4251. [repDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  4252. }
  4253. [container setValue:repDic forKey:[NSString stringWithFormat:@"val_%ld",(*count)++]];
  4254. }] mutableCopy];
  4255. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  4256. return ret;
  4257. }
  4258. + (NSDictionary *)offline_dealZipCode:(NSString *)zipcode {
  4259. zipcode = [self translateSingleQuote:zipcode];
  4260. NSString *sql = [NSString stringWithFormat:@"select country,state,city,country_code,state_code from offline_zipcode where zipcode = '%@';",zipcode];
  4261. NSMutableDictionary *ret = [[iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4262. char *country = (char *)sqlite3_column_text(stmt, 0);
  4263. char *state = (char *)sqlite3_column_text(stmt, 1);
  4264. char *city = (char *)sqlite3_column_text(stmt, 2);
  4265. char *country_code = (char *)sqlite3_column_text(stmt, 3);
  4266. char *state_code = (char *)sqlite3_column_text(stmt, 4);
  4267. if (country == NULL) {
  4268. country = "";
  4269. }
  4270. if (state == NULL) {
  4271. state = "";
  4272. }
  4273. if (city == NULL) {
  4274. city = "";
  4275. }
  4276. if (country_code == NULL) {
  4277. country_code = "";
  4278. }
  4279. if (state_code == NULL) {
  4280. state_code = "";
  4281. }
  4282. [container setValue:[NSString stringWithUTF8String:country] forKey:@"country"];
  4283. [container setValue:[NSString stringWithUTF8String:state] forKey:@"state"];
  4284. [container setValue:[NSString stringWithUTF8String:city] forKey:@"city"];
  4285. [container setValue:[NSString stringWithUTF8String:country_code] forKey:@"country_code"];
  4286. [container setValue:[NSString stringWithUTF8String:state_code] forKey:@"state_code"];
  4287. }] mutableCopy];
  4288. return ret;
  4289. }
  4290. + (void)setValue:(id)value forItemKey:(NSString *)itemKey valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  4291. NSMutableDictionary *item = [[dic objectForKey:itemKey] mutableCopy];
  4292. [item setValue:value forKey:valueKey];
  4293. [dic setValue:item forKey:itemKey];
  4294. }
  4295. + (NSString *)countryCodeByid:(NSString *)code_id {
  4296. NSString *ret = nil;
  4297. code_id = [self translateSingleQuote:code_id];
  4298. sqlite3 *db = [iSalesDB get_db];
  4299. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_country where countrycode_id = %@",code_id];
  4300. sqlite3_stmt * statement;
  4301. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4302. while (sqlite3_step(statement) == SQLITE_ROW) {
  4303. char *code = (char *)sqlite3_column_text(statement, 0);
  4304. if (code == NULL) {
  4305. code = "";
  4306. }
  4307. ret = [NSString stringWithUTF8String:code];
  4308. }
  4309. sqlite3_finalize(statement);
  4310. }
  4311. [iSalesDB close_db:db];
  4312. return ret;
  4313. }
  4314. + (NSString *)countryCodeIdByCode:(NSString *)code {
  4315. NSString *ret = nil;
  4316. code = [self translateSingleQuote:code];
  4317. sqlite3 *db = [iSalesDB get_db];
  4318. NSString *sqlQuery = [NSString stringWithFormat:@"select countrycode_id from offline_country where code = '%@';",code];
  4319. sqlite3_stmt * statement;
  4320. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4321. while (sqlite3_step(statement) == SQLITE_ROW) {
  4322. char *_id = (char *)sqlite3_column_text(statement, 0);
  4323. if (_id == NULL) {
  4324. _id = "";
  4325. }
  4326. ret = [NSString stringWithFormat:@"%s",_id];
  4327. }
  4328. sqlite3_finalize(statement);
  4329. }
  4330. [iSalesDB close_db:db];
  4331. return ret;
  4332. }
  4333. + (NSString *)countryNameByCountryCodeId:(NSString *)codeId {
  4334. NSString *name = nil;
  4335. codeId = [self translateSingleQuote:codeId];
  4336. sqlite3 *db = [iSalesDB get_db];
  4337. NSString *sqlQuery = [NSString stringWithFormat:@"select name from offline_country where countrycode_id = %@",codeId];
  4338. sqlite3_stmt * statement;
  4339. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4340. while (sqlite3_step(statement) == SQLITE_ROW) {
  4341. char *value = (char *)sqlite3_column_text(statement, 0);
  4342. if (value == NULL) {
  4343. value = "";
  4344. }
  4345. name = [NSString stringWithUTF8String:value];
  4346. }
  4347. sqlite3_finalize(statement);
  4348. }
  4349. [iSalesDB close_db:db];
  4350. return name;
  4351. }
  4352. + (NSString *)priceNameByPriceId:(NSString *)priceId {
  4353. NSString *ret = nil;
  4354. sqlite3 *db = [iSalesDB get_db];
  4355. NSString *sqlQuery = [NSString stringWithFormat:@"select name from price where type = %@;",priceId];
  4356. sqlite3_stmt * statement;
  4357. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4358. while (sqlite3_step(statement) == SQLITE_ROW) {
  4359. char *name = (char *)sqlite3_column_text(statement, 0);
  4360. if (name == NULL) {
  4361. name = "";
  4362. }
  4363. ret = [NSString stringWithUTF8String:name];
  4364. }
  4365. sqlite3_finalize(statement);
  4366. }
  4367. [iSalesDB close_db:db];
  4368. return ret;
  4369. }
  4370. + (NSString *)salesRepCodeById:(NSString *)_id {
  4371. NSString *ret = nil;
  4372. _id = [self translateSingleQuote:_id];
  4373. sqlite3 *db = [iSalesDB get_db];
  4374. NSString *sqlQuery = [NSString stringWithFormat:@"select code from offline_salesrep where salesrep_id = %@",_id];
  4375. sqlite3_stmt * statement;
  4376. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
  4377. while (sqlite3_step(statement) == SQLITE_ROW) {
  4378. char *rep = (char *)sqlite3_column_text(statement, 0);
  4379. if (rep == NULL) {
  4380. rep = "";
  4381. }
  4382. ret = [NSString stringWithUTF8String:rep];
  4383. }
  4384. sqlite3_finalize(statement);
  4385. }
  4386. [iSalesDB close_db:db];
  4387. return ret;
  4388. }
  4389. + (NSString *)textAtColumn:(int)col statement:(sqlite3_stmt *)stmt{
  4390. char *tx = (char *)sqlite3_column_text(stmt, col);
  4391. if (tx == NULL) {
  4392. tx = "";
  4393. }
  4394. // NSString *text = [NSString stringWithFormat:@"%s",tx];
  4395. NSString *text = [NSString stringWithCString:tx encoding:NSUTF8StringEncoding];
  4396. if (!text) {
  4397. text = @"";
  4398. }
  4399. // 将字符全部为' '的字符串干掉
  4400. int spaceCount = 0;
  4401. for (int i = 0; i < text.length; i++) {
  4402. if ([text characterAtIndex:i] == ' ') {
  4403. spaceCount++;
  4404. }
  4405. }
  4406. if (spaceCount == text.length) {
  4407. text = @"";
  4408. }
  4409. return text;
  4410. }
  4411. + (NSMutableDictionary *)dictionaryFileName:(NSString *)name {
  4412. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4413. NSData *data = [NSData dataWithContentsOfFile:path];
  4414. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4415. return ret;
  4416. }
  4417. + (NSString *)textFileName:(NSString *)name {
  4418. NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
  4419. NSString *text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
  4420. if (!text) {
  4421. text = @"";
  4422. }
  4423. return text;
  4424. }
  4425. + (NSMutableDictionary *)mutableDictionary:(NSString *)key inDictionary:(NSDictionary *)dic {
  4426. return [[dic objectForKey:key] mutableCopy];
  4427. }
  4428. + (id)translateSingleQuote:(NSString *)string {
  4429. if ([string isKindOfClass:[NSString class]])
  4430. return [string stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4431. return string;
  4432. }
  4433. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key {
  4434. return [params valueForKey:key] ? [params valueForKey:key] : @"";
  4435. }
  4436. + (NSString *)item_image:(NSString *)item_id db:(sqlite3 *)db {
  4437. NSString* ret= nil;
  4438. NSString *sqlQuery = nil;
  4439. // 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
  4440. sqlQuery = [NSString stringWithFormat:@"select i.url from model_image as i where item_id = %@ order by i.default_img desc, i._id asc limit 1;",item_id];
  4441. sqlite3_stmt * statement;
  4442. // int count=0;
  4443. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4444. {
  4445. if (sqlite3_step(statement) == SQLITE_ROW)
  4446. {
  4447. char *imgurl = (char*)sqlite3_column_text(statement, 0);
  4448. if(imgurl==nil)
  4449. imgurl="";
  4450. NSString *nsimgurl = [[NSString alloc]initWithUTF8String:imgurl];
  4451. ret=nsimgurl;
  4452. }
  4453. sqlite3_finalize(statement);
  4454. }
  4455. else
  4456. {
  4457. [ret setValue:@"8" forKey:@"result"];
  4458. }
  4459. // [iSalesDB close_db:db];
  4460. // DebugLog(@"data string: %@",ret );
  4461. return ret;
  4462. }
  4463. #pragma mark contact list
  4464. +(NSDictionary*) offline_contactlist :(NSMutableDictionary *) params
  4465. {
  4466. // contactType = "Sales_Order_Customer";
  4467. // limit = 25;
  4468. // offset = 0;
  4469. // password = 123456;
  4470. // "price_name" = 16;
  4471. // user = EvanK;
  4472. sqlite3 *db = [iSalesDB get_db];
  4473. NSString* contactType = [params valueForKey:@"contactType"]; // contactType = "Sales_Order_Customer";
  4474. if (contactType) {
  4475. contactType = [NSString stringWithFormat:@"%@ = 1",contactType];
  4476. } else {
  4477. contactType = @"1 = 1";
  4478. }
  4479. NSString* keyword = [params valueForKey:@"keyword"]; // search 时才有
  4480. DebugLog(@"offline contact list keyword: %@",keyword);
  4481. // advanced search
  4482. NSString* contact_name = [params valueForKey:@"contact_name"]; // "contact_name"
  4483. if (contact_name) {
  4484. contact_name = [contact_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4485. contact_name = [NSString stringWithFormat:@"and lower(contact_name) like '%%%@%%'",contact_name.lowercaseString];
  4486. } else {
  4487. contact_name = @"";
  4488. }
  4489. NSString* customer_phone = [params valueForKey:@"customer_phone"]; // "customer_phone"
  4490. if (customer_phone) {
  4491. customer_phone = [customer_phone stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4492. customer_phone = [NSString stringWithFormat:@"and lower(decrypt(phone)) like '%%%@%%'",customer_phone.lowercaseString];
  4493. } else {
  4494. customer_phone = @"";
  4495. }
  4496. NSString* customer_fax = [params valueForKey:@"customer_fax"]; // "customer_fax"
  4497. if (customer_fax) {
  4498. customer_fax = [customer_fax stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4499. customer_fax = [NSString stringWithFormat:@"and lower(fax) like '%%%@%%'",customer_fax.lowercaseString];
  4500. } else {
  4501. customer_fax = @"";
  4502. }
  4503. NSString* customer_zipcode = [params valueForKey:@"customer_zipcode"]; // "customer_zipcode"
  4504. if (customer_zipcode) {
  4505. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4506. customer_zipcode = [NSString stringWithFormat:@"and lower(zipcode) like '%%%@%%'",customer_zipcode.lowercaseString];
  4507. } else {
  4508. customer_zipcode = @"";
  4509. }
  4510. NSString* customer_sales_rep = [params valueForKey:@"customer_sales_rep"]; // "customer_sales_rep"
  4511. if (customer_sales_rep) {
  4512. customer_zipcode = [customer_zipcode stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4513. customer_sales_rep = [NSString stringWithFormat:@"and lower(sales_rep) like '%%%@%%'",customer_sales_rep.lowercaseString];
  4514. } else {
  4515. customer_sales_rep = @"";
  4516. }
  4517. NSString* customer_state = [params valueForKey:@"customer_state"]; // "customer_state"
  4518. if (customer_state) {
  4519. customer_state = [customer_state stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4520. customer_state = [NSString stringWithFormat:@"and lower(state) like '%%%@%%'",customer_state.lowercaseString];
  4521. } else {
  4522. customer_state = @"";
  4523. }
  4524. NSString* customer_name = [params valueForKey:@"customer_name"]; // "customer_name" 也就是company name
  4525. if (customer_name) {
  4526. customer_name = [customer_name stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4527. customer_name = [NSString stringWithFormat:@"and lower(decrypt(company_name)) like '%%%@%%'",customer_name.lowercaseString];
  4528. } else {
  4529. customer_name = @"";
  4530. }
  4531. NSString* customer_country = [params valueForKey:@"customer_country"]; // "customer_country"
  4532. if (customer_country) {
  4533. customer_country = [customer_country stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4534. customer_country = [NSString stringWithFormat:@"and lower(country) like '%%%@%%'",customer_country.lowercaseString];
  4535. } else {
  4536. customer_country = @"";
  4537. }
  4538. NSString* customer_cid = [params valueForKey:@"customer_cid"]; // "customer_cid"
  4539. if (customer_cid) {
  4540. customer_cid = [customer_cid stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4541. customer_cid = [NSString stringWithFormat:@"and lower(contact_id) like '%%%@%%'",customer_cid.lowercaseString];
  4542. } else {
  4543. customer_cid = @"";
  4544. }
  4545. NSString* customer_city = [params valueForKey:@"customer_city"]; // "customer_city"
  4546. if (customer_city) {
  4547. customer_city = [customer_city stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4548. customer_city = [NSString stringWithFormat:@"and lower(city) like '%%%@%%'",customer_city.lowercaseString];
  4549. } else {
  4550. customer_city = @"";
  4551. }
  4552. NSString* customer_address = [params valueForKey:@"customer_address"]; // "customer_address"
  4553. if (customer_address) {
  4554. customer_address = [customer_address stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4555. customer_address = [NSString stringWithFormat:@"and lower(decrypt(addr)) like '%%%@%%'",customer_address.lowercaseString];
  4556. } else {
  4557. customer_address = @"";
  4558. }
  4559. NSString* customer_email = [params valueForKey:@"customer_email"]; // "customer_email"
  4560. if (customer_email) {
  4561. customer_email = [customer_email stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4562. customer_email = [NSString stringWithFormat:@"and lower(email) like '%%%@%%'",customer_email.lowercaseString];
  4563. } else {
  4564. customer_email = @"";
  4565. }
  4566. NSString *price_name = [params valueForKey:@"price_name"];
  4567. if (price_name) {
  4568. if ([price_name containsString:@","]) {
  4569. // 首先从 price表中查处name
  4570. NSArray *pArray = [price_name componentsSeparatedByString:@","];
  4571. NSMutableString *mutablePStr = [NSMutableString stringWithFormat:@"SELECT name FROM price where type = %@ ",pArray[0]];
  4572. for (int i = 1;i < pArray.count;i++) {
  4573. NSString *p = pArray[i];
  4574. [mutablePStr appendFormat:@" or type = %@ ",p];
  4575. }
  4576. [mutablePStr appendString:@";"];
  4577. __block NSMutableArray *price_name_array = [NSMutableArray array];
  4578. [iSalesDB jk_query:mutablePStr db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  4579. char *name = (char *)sqlite3_column_text(stmt, 0);
  4580. if (!name)
  4581. name = "";
  4582. [price_name_array addObject:[self translateSingleQuote:[NSString stringWithUTF8String:name]]];
  4583. }];
  4584. // 再根据name 拼sql
  4585. NSMutableString *mutable_price_name = [NSMutableString string];
  4586. [mutable_price_name appendFormat:@" and( price_type like '%%%@%%' ",price_name_array[0]];
  4587. for (int i = 1; i < price_name_array.count; i++) {
  4588. [mutable_price_name appendFormat:@" or price_type like '%%%@%%' ",price_name_array[i]];
  4589. }
  4590. [mutable_price_name appendString:@")"];
  4591. price_name = mutable_price_name;
  4592. } else {
  4593. price_name = [price_name stringByReplacingOccurrencesOfString:@" " withString:@""];
  4594. if ([price_name isEqualToString:@""]) {
  4595. price_name = @"";
  4596. } else {
  4597. __block NSString *price;
  4598. price_name = [self translateSingleQuote:price_name];
  4599. [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) {
  4600. char *p = (char *)sqlite3_column_text(stmt, 0);
  4601. if (p == NULL) {
  4602. p = "";
  4603. }
  4604. price = [NSString stringWithUTF8String:p];
  4605. }];
  4606. if ([price isEqualToString:@""]) {
  4607. price_name = @"";
  4608. } else {
  4609. price = [self translateSingleQuote:price];
  4610. price_name = [NSString stringWithFormat:@"and price_type like '%%%@%%'",price];
  4611. }
  4612. }
  4613. }
  4614. } else {
  4615. price_name = @"";
  4616. }
  4617. int limit = [[params valueForKey:@"limit"] intValue];
  4618. int offset = [[params valueForKey:@"offset"] intValue];
  4619. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  4620. 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];
  4621. 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];
  4622. // int result= [iSalesDB AddExFunction:db];
  4623. int count =0;
  4624. NSString *sqlQuery = nil;
  4625. if(keyword.length==0)
  4626. {
  4627. // 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];
  4628. // count=[iSalesDB get_recordcount:db table:@"offline_contact" where:[NSString stringWithFormat:@"%@='1",contactType]];
  4629. sqlQuery = sql;
  4630. count = [iSalesDB get_recordcount:db table:@"offline_contact" where:where];
  4631. }
  4632. else
  4633. {
  4634. // NSString* encrypt_keyword= [AESCrypt fastencrypt:keyword];
  4635. keyword = keyword.lowercaseString;
  4636. keyword = [keyword stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
  4637. 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];
  4638. 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]];
  4639. }
  4640. DebugLog(@"offline_contactlist sql:%@",sqlQuery);
  4641. sqlite3_stmt * statement;
  4642. [ret setValue:@"2" forKey:@"result"];
  4643. [ret setValue:[NSNumber numberWithInt:count ] forKey:@"total"];
  4644. // sqlQuery = @"select editable,decrypt(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 Sales_Order_Customer = 1 and is_active=1 order by decrypt(company_name) limit 25 offset 0";
  4645. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  4646. {
  4647. int i = 0;
  4648. while (sqlite3_step(statement) == SQLITE_ROW)
  4649. {
  4650. [ret setValue:[NSNumber numberWithInt:AP_USER_AUTH ] forKey:@"result"];
  4651. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  4652. int editable = sqlite3_column_int(statement, 0);
  4653. char *company_name = (char*)sqlite3_column_text(statement, 1);
  4654. NSString *nscompany_name =nil;
  4655. if(company_name==nil)
  4656. nscompany_name=@"";
  4657. else
  4658. nscompany_name= [[NSString alloc]initWithUTF8String:company_name] ;
  4659. char *country = (char*)sqlite3_column_text(statement, 2);
  4660. if(country==nil)
  4661. country="";
  4662. NSString *nscountry= [[NSString alloc]initWithUTF8String:country];
  4663. // char *addr = (char*)sqlite3_column_text(statement, 3);
  4664. // if(addr==nil)
  4665. // addr="";
  4666. // NSString *nsaddr= [[NSString alloc]initWithUTF8String:addr];
  4667. char *zipcode = (char*)sqlite3_column_text(statement, 4);
  4668. if(zipcode==nil)
  4669. zipcode="";
  4670. NSString *nszipcode= [[NSString alloc]initWithUTF8String:zipcode];
  4671. char *state = (char*)sqlite3_column_text(statement, 5);
  4672. if(state==nil)
  4673. state="";
  4674. NSString *nsstate= [[NSString alloc]initWithUTF8String:state];
  4675. char *city = (char*)sqlite3_column_text(statement, 6);
  4676. if(city==nil)
  4677. city="";
  4678. NSString *nscity= [[NSString alloc]initWithUTF8String:city];
  4679. // char *contact_name = (char*)sqlite3_column_text(statement, 7);
  4680. // NSString *nscontact_name = nil;
  4681. // if(contact_name==nil)
  4682. // nscontact_name=@"";
  4683. // else
  4684. // nscontact_name= [[NSString alloc]initWithUTF8String:contact_name];
  4685. char *phone = (char*)sqlite3_column_text(statement, 8);
  4686. NSString *nsphone = nil;
  4687. if(phone==nil)
  4688. nsphone=@"";
  4689. else
  4690. nsphone= [[NSString alloc]initWithUTF8String:phone];
  4691. char *contact_id = (char*)sqlite3_column_text(statement, 9);
  4692. if(contact_id==nil)
  4693. contact_id="";
  4694. NSString *nscontact_id= [[NSString alloc]initWithUTF8String:contact_id];
  4695. char *addr_1 = (char*)sqlite3_column_text(statement, 10);
  4696. if(addr_1==nil)
  4697. addr_1="";
  4698. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  4699. char *addr_2 = (char*)sqlite3_column_text(statement, 11);
  4700. if(addr_2==nil)
  4701. addr_2="";
  4702. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  4703. char *addr_3 = (char*)sqlite3_column_text(statement, 12);
  4704. if(addr_3==nil)
  4705. addr_3="";
  4706. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  4707. char *addr_4 = (char*)sqlite3_column_text(statement, 13);
  4708. if(addr_4==nil)
  4709. addr_4="";
  4710. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  4711. char *first_name = (char*)sqlite3_column_text(statement, 14);
  4712. if(first_name==nil)
  4713. first_name="";
  4714. NSString *nsfirst_name= [[NSString alloc]initWithUTF8String:first_name];
  4715. char *last_name = (char*)sqlite3_column_text(statement, 15);
  4716. if(last_name==nil)
  4717. last_name="";
  4718. NSString *nslast_name= [[NSString alloc]initWithUTF8String:last_name];
  4719. char *fax = (char*)sqlite3_column_text(statement, 16);
  4720. NSString *nsfax = nil;
  4721. if(fax==nil)
  4722. nsfax=@"";
  4723. else
  4724. {
  4725. nsfax= [[NSString alloc]initWithUTF8String:fax];
  4726. if(nsfax.length>0)
  4727. nsfax= [NSString stringWithFormat:@"FAX:%@",nsfax];
  4728. }
  4729. char *email = (char*)sqlite3_column_text(statement, 17);
  4730. NSString *nsemail = nil;
  4731. if(email==nil)
  4732. nsemail=@"";
  4733. else
  4734. {
  4735. nsemail= [[NSString alloc]initWithUTF8String:email];
  4736. if(nsemail.length>0)
  4737. nsemail= [NSString stringWithFormat:@"EMAIL:%@",nsemail];
  4738. // nsemail= [NSString stringWithFormat:@"EMAIL:%@",[[NSString alloc]initWithUTF8String:email]];
  4739. }
  4740. NSMutableArray* arr_name= [[NSMutableArray alloc] init];
  4741. [arr_name addObject:nsfirst_name];
  4742. [arr_name addObject:nslast_name];
  4743. NSString *nscontact_name=[RAUtils arr2string:arr_name separator:@" " trim:true];
  4744. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  4745. {
  4746. // decrypt
  4747. nscompany_name=[AESCrypt fastdecrypt:nscompany_name];
  4748. nscompany_name=[NSString stringWithFormat:@"<b>%@</b>",nscompany_name];
  4749. nsphone=[AESCrypt fastdecrypt:nsphone];
  4750. nsphone=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4751. nsaddr_1=[AESCrypt fastdecrypt:nsaddr_1];
  4752. // nsaddr_1=[NSString stringWithFormat:@"TEL:%@",nsphone];
  4753. }
  4754. [arr_addr addObject:nscompany_name];
  4755. [arr_addr addObject:nscontact_name];
  4756. [arr_addr addObject:@"<br>"];
  4757. [arr_addr addObject:nsaddr_1];
  4758. [arr_addr addObject:nsaddr_2];
  4759. [arr_addr addObject:nsaddr_3];
  4760. [arr_addr addObject:nsaddr_4];
  4761. //[arr_addr addObject:nsaddr];
  4762. [arr_addr addObject:nszipcode];
  4763. [arr_addr addObject:nscity];
  4764. [arr_addr addObject:nsstate];
  4765. [arr_addr addObject:nscountry];
  4766. [arr_addr addObject:@"<br>"];
  4767. [arr_addr addObject:nsphone];
  4768. [arr_addr addObject:nsfax];
  4769. [arr_addr addObject:nsemail];
  4770. NSString * name = [RAUtils arr2string:arr_addr separator:@", " trim:true];
  4771. name=[name stringByReplacingOccurrencesOfString:@", <br>," withString:@"<br>"];
  4772. [item setValue:name forKey:@"name"];
  4773. [item setValue:nscontact_id forKey:@"contact_id"];
  4774. [item setValue:editable==1?@"true":@"false" forKey:@"can_update"];
  4775. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  4776. i++;
  4777. }
  4778. UIApplication * app = [UIApplication sharedApplication];
  4779. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  4780. [ret setValue:appDelegate.mode forKey:@"mode"];
  4781. [ret setValue:[NSNumber numberWithInt:i ] forKey:@"count"];
  4782. sqlite3_finalize(statement);
  4783. }
  4784. [iSalesDB close_db:db];
  4785. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  4786. return ret;
  4787. }
  4788. #pragma mark contact Advanced search
  4789. +(NSData *) offline_contactAdvancedSearch:(NSMutableDictionary *) params
  4790. {
  4791. NSMutableDictionary *contactAdvanceDic = [self dictionaryFileName:@"contactAdvanceSearch.json"];
  4792. return [RAUtils dict2data:contactAdvanceDic];
  4793. }
  4794. #pragma mark create new contact
  4795. + (NSData *)offline_createContact:(NSMutableDictionary *)params {
  4796. NSString *path = [[NSBundle mainBundle] pathForResource:@"createContact.json" ofType:nil];
  4797. NSData *data = [NSData dataWithContentsOfFile:path];
  4798. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  4799. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  4800. NSString *countryCode = nil;
  4801. NSString *countryCode_id = nil;
  4802. NSString *stateCode = nil;
  4803. NSString *city = nil;
  4804. NSString *zipCode = nil;
  4805. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  4806. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  4807. NSString *code_id = params[@"country"];
  4808. countryCode_id = code_id;
  4809. countryCode = [self countryCodeByid:code_id];
  4810. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  4811. NSString *zip_code = params[@"zipcode"];
  4812. // 剔除全部为空格
  4813. int spaceCount = 0;
  4814. for (int i = 0; i < zip_code.length; i++) {
  4815. if ([zip_code characterAtIndex:i] == ' ') {
  4816. spaceCount++;
  4817. }
  4818. }
  4819. if (spaceCount == zip_code.length) {
  4820. zip_code = @"";
  4821. }
  4822. zipCode = zip_code;
  4823. if (zipCode.length > 0) {
  4824. countryCode_id = params[@"country"];
  4825. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  4826. countryCode = [dic valueForKey:@"country_code"];
  4827. if (!countryCode) {
  4828. // countryCode = @"US";
  4829. NSString *code_id = params[@"country"];
  4830. countryCode = [self countryCodeByid:code_id];
  4831. }
  4832. stateCode = [dic valueForKey:@"state_code"];
  4833. if (!stateCode.length) {
  4834. stateCode = params[@"state"];
  4835. }
  4836. city = [dic valueForKey:@"city"];
  4837. if (!city.length) {
  4838. city = params[@"city"];
  4839. }
  4840. // zip code
  4841. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  4842. [zipDic setValue:zipCode forKey:@"value"];
  4843. [section_0 setValue:zipDic forKey:@"item_11"];
  4844. } else {
  4845. NSString *code_id = params[@"country"];
  4846. countryCode = [self countryCodeByid:code_id];
  4847. stateCode = params[@"state"];
  4848. city = params[@"city"];
  4849. }
  4850. }
  4851. } else {
  4852. // default: US United States
  4853. countryCode = @"US";
  4854. countryCode_id = @"228";
  4855. }
  4856. // country
  4857. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  4858. [self setValue:allCountry forItemKey:@"item_6" valueKey:@"cadedate" inDictionary:section_0];
  4859. // state
  4860. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  4861. // NSDictionary *allState = [self offline_getStateByCountryCodeId:countryCode_id checkedState:stateCode];
  4862. [self setValue:allState forItemKey:@"item_12" valueKey:@"cadedate" inDictionary:section_0];
  4863. // city
  4864. if (city) {
  4865. NSMutableDictionary *cityDic = [[section_0 valueForKey:@"item_13"] mutableCopy];
  4866. [cityDic setValue:city forKey:@"value"];
  4867. [section_0 setValue:cityDic forKey:@"item_13"];
  4868. }
  4869. // price type
  4870. NSDictionary *priceDic = [self offline_getPrice];
  4871. [self setValue:priceDic forItemKey:@"item_2" valueKey:@"cadedate" inDictionary:section_0];
  4872. // contact type
  4873. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:nil];
  4874. [self setValue:contactTypeDic forItemKey:@"item_3" valueKey:@"cadedate" inDictionary:section_0];
  4875. // Sales Rep
  4876. NSDictionary *repDic = [self offline_getSalesRep];
  4877. [self setValue:repDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  4878. [ret setValue:section_0 forKey:@"section_0"];
  4879. return [RAUtils dict2data:ret];
  4880. }
  4881. #pragma mark save
  4882. + (NSData *)offline_saveContact:(NSDictionary *)params update:(BOOL)update isCustomer:(BOOL)isCustomer{
  4883. NSString *addr = nil;
  4884. NSString *contact_name = nil;
  4885. NSString *companyName = [params objectForKey:@"company"];
  4886. if (companyName) {
  4887. companyName = [AESCrypt fastencrypt:companyName];
  4888. } else {
  4889. companyName = @"";
  4890. }
  4891. DebugLog(@"company");
  4892. companyName = [self translateSingleQuote:companyName];
  4893. NSString *addr1 = [params objectForKey:@"address"];
  4894. NSString *addr2 = [params objectForKey:@"address2"];
  4895. NSString *addr3 = [params objectForKey:@"address_3"];
  4896. NSString *addr4 = [params objectForKey:@"address_4"];
  4897. if (!addr2) {
  4898. addr2 = @"";
  4899. }
  4900. if (!addr3) {
  4901. addr3 = @"";
  4902. }
  4903. if (!addr4) {
  4904. addr4 = @"";
  4905. }
  4906. if (!addr1) {
  4907. addr1 = @"";
  4908. }
  4909. DebugLog(@"addr");
  4910. addr = [RAUtils arr2string:@[addr1,addr2,addr3,addr4] separator:@" " trim:true];
  4911. addr = [AESCrypt fastencrypt:addr];
  4912. addr = [self translateSingleQuote:addr];
  4913. if (addr1 && ![addr1 isEqualToString:@""]) {
  4914. addr1 = [AESCrypt fastencrypt:addr1];
  4915. }
  4916. addr1 = [self translateSingleQuote:addr1];
  4917. addr2 = [self translateSingleQuote:addr2];
  4918. addr3 = [self translateSingleQuote:addr3];
  4919. addr4 = [self translateSingleQuote:addr4];
  4920. NSString *country = [params objectForKey:@"country"];
  4921. if (country) {
  4922. country = [self countryNameByCountryCodeId:country];
  4923. } else {
  4924. country = @"";
  4925. }
  4926. DebugLog(@"country");
  4927. country = [self translateSingleQuote:country];
  4928. NSString *state = [params objectForKey:@"state"];
  4929. if (!state) {
  4930. state = @"";
  4931. }
  4932. DebugLog(@"state");
  4933. state = [self translateSingleQuote:state];
  4934. NSString *city = [params objectForKey:@"city"];
  4935. if (!city) {
  4936. city = @"";
  4937. }
  4938. city = [self translateSingleQuote:city];
  4939. NSString *zipcode = [params objectForKey:@"zipcode"];
  4940. if (!zipcode) {
  4941. zipcode = @"";
  4942. }
  4943. DebugLog(@"zip");
  4944. zipcode = [self translateSingleQuote:zipcode];
  4945. NSString *fistName = [params objectForKey:@"firstname"];
  4946. if (!fistName) {
  4947. fistName = @"";
  4948. }
  4949. NSString *lastName = [params objectForKey:@"lastname"];
  4950. if (!lastName) {
  4951. lastName = @"";
  4952. }
  4953. contact_name = [RAUtils arr2string:@[fistName,lastName] separator:@" " trim:true];
  4954. DebugLog(@"contact_name");
  4955. contact_name = [self translateSingleQuote:contact_name];
  4956. fistName = [self translateSingleQuote:fistName];
  4957. lastName = [self translateSingleQuote:lastName];
  4958. NSString *phone = [params objectForKey:@"phone"];
  4959. if (phone) {
  4960. phone = [AESCrypt fastencrypt:phone];
  4961. } else {
  4962. phone = @"";
  4963. }
  4964. DebugLog(@"PHONE");
  4965. phone = [self translateSingleQuote:phone];
  4966. NSString *fax = [params objectForKey:@"fax"];
  4967. if (!fax) {
  4968. fax = @"";
  4969. }
  4970. DebugLog(@"FAX");
  4971. fax = [self translateSingleQuote:fax];
  4972. NSString *email = [params objectForKey:@"email"];
  4973. if (!email) {
  4974. email = @"";
  4975. }
  4976. DebugLog(@"EMAIL:%@",email);
  4977. email = [self translateSingleQuote:email];
  4978. NSString *notes = [params objectForKey:@"contact_notes"];
  4979. if (!notes) {
  4980. notes = @"";
  4981. }
  4982. DebugLog(@"NOTE:%@",notes);
  4983. notes = [self translateSingleQuote:notes];
  4984. NSString *price = [params objectForKey:@"price_name"];
  4985. if (price) {
  4986. price = [self priceNameByPriceId:price];
  4987. } else {
  4988. price = @"";
  4989. }
  4990. DebugLog(@"PRICE");
  4991. price = [self translateSingleQuote:price];
  4992. NSString *salesRep = [params objectForKey:@"sales_rep"];
  4993. if (salesRep) {
  4994. salesRep = [self salesRepCodeById:salesRep];
  4995. } else {
  4996. salesRep = @"";
  4997. }
  4998. salesRep = [self translateSingleQuote:salesRep];
  4999. NSString *img = [params objectForKey:@"business_card"];
  5000. NSArray *array = [img componentsSeparatedByString:@","];
  5001. NSString *img_0 = array[0];
  5002. if (!img_0) {
  5003. img_0 = @"";
  5004. }
  5005. img_0 = [self translateSingleQuote:img_0];
  5006. NSString *img_1 = array[1];
  5007. if (!img_1) {
  5008. img_1 = @"";
  5009. }
  5010. img_1 = [self translateSingleQuote:img_1];
  5011. NSString *img_2 = array[2];
  5012. if (!img_2) {
  5013. img_2 = @"";
  5014. }
  5015. img_2 = [self translateSingleQuote:img_2];
  5016. NSString *contact_id = [NSUUID UUID].UUIDString;
  5017. NSString *contact_type = [params objectForKey:@"type_name"];
  5018. if (!contact_type) {
  5019. contact_type = @"";
  5020. }
  5021. contact_type = [self translateSingleQuote:contact_type];
  5022. // 判断更新时是否为customer
  5023. if (update) {
  5024. contact_id = [params objectForKey:@"contact_id"];
  5025. if (!contact_id) {
  5026. contact_id = @"";
  5027. }
  5028. NSString *checkCustomerSql = [NSString stringWithFormat:@"select Sales_Order_Customer from offline_contact where contact_id = '%@';",contact_id];
  5029. __block int customer = 0;
  5030. [iSalesDB jk_query:checkCustomerSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5031. customer = sqlite3_column_int(stmt, 0);
  5032. }];
  5033. isCustomer = customer ? YES : NO;
  5034. }
  5035. NSMutableDictionary *sync_dic = [params mutableCopy];
  5036. if (isCustomer) {
  5037. [sync_dic setValue:@"Sales_Order_Customer" forKey:@"contactType"];
  5038. } else {
  5039. [sync_dic setValue:@"Sales_Order_Ship_To" forKey:@"contactType"];
  5040. }
  5041. NSString *sync_data = nil;
  5042. NSString *sql = nil;
  5043. if (update){
  5044. contact_id = [params objectForKey:@"contact_id"];
  5045. if (!contact_id) {
  5046. contact_id = @"";
  5047. }
  5048. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5049. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5050. sync_data = [RAUtils dict2string:sync_dic];
  5051. sync_data = [self translateSingleQuote:sync_data];
  5052. 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 = '%@',type = '%@' 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_type,contact_id];
  5053. } else {
  5054. contact_id = [self translateSingleQuote:contact_id];
  5055. [sync_dic setValue:contact_id forKey:@"contact_id"];
  5056. // sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:sync_dic]];
  5057. sync_data = [RAUtils dict2string:sync_dic];
  5058. sync_data = [self translateSingleQuote:sync_data];
  5059. 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,type) values (1,'%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@',1,'%@',%d,%d,0,%d,0,1,'%@','%@','%@','%@')",companyName,addr1,addr2,addr3,addr4,country,state,city,zipcode,fistName,lastName,phone,fax,email,notes,price,salesRep,img_0,img_1,img_2,contact_id,isCustomer ? 1 : 0,isCustomer ? 1 : 0,isCustomer ? 1 : 0,sync_data,contact_name,addr,contact_type];
  5060. }
  5061. int result = [iSalesDB execSql:sql];
  5062. // NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"min_ver\":\"160409\",\"mode\":\"Regular Mode\"}",result];
  5063. NSString *retStr = [NSString stringWithFormat:@"{\"result\":%d,\"mode\":\"Regular Mode\"}",result];
  5064. return [retStr dataUsingEncoding:NSUTF8StringEncoding];
  5065. }
  5066. #pragma mark save new contact
  5067. +(NSData *) offline_saveNewContact:(NSMutableDictionary *) params
  5068. {
  5069. return [self offline_saveContact:params update:NO isCustomer:YES];
  5070. }
  5071. #pragma mark edit contact
  5072. +(NSData *) offline_editContact:(NSMutableDictionary *) params
  5073. {
  5074. // {
  5075. // "contact_id" = "CE0D2445-3C1F-40EC-B94C-A39A6900FBDA";
  5076. // password = 123456;
  5077. // user = EvanK;
  5078. // }
  5079. NSString *path = [[NSBundle mainBundle] pathForResource:@"editContact.json" ofType:nil];
  5080. NSData *data = [NSData dataWithContentsOfFile:path];
  5081. NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  5082. NSMutableDictionary *section_0 = [[ret objectForKey:@"section_0"] mutableCopy];
  5083. NSString *countryCode = nil;
  5084. NSString *countryCode_id = nil;
  5085. NSString *stateCode = nil;
  5086. /*------contact infor------*/
  5087. __block NSString *country = nil;
  5088. __block NSString *company_name = nil;
  5089. __block NSString *contact_id = params[@"contact_id"];
  5090. __block NSString *addr_1,*addr_2,*addr_3,*addr_4;
  5091. __block NSString *zipcode = nil;
  5092. __block NSString *state = nil; // state_code
  5093. __block NSString *city = nil; //
  5094. __block NSString *firt_name,*last_name;
  5095. __block NSString *phone,*fax,*email;
  5096. __block NSString *notes,*price_type,*sales_rep;
  5097. __block NSString *img_0,*img_1,*img_2;
  5098. __block NSString *contact_type;
  5099. contact_id = [self translateSingleQuote:contact_id];
  5100. 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,type from offline_contact where contact_id = '%@';",contact_id];
  5101. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5102. country = [self textAtColumn:0 statement:stmt]; // country name
  5103. company_name = [self textAtColumn:1 statement:stmt];
  5104. addr_1 = [self textAtColumn:2 statement:stmt];
  5105. addr_2 = [self textAtColumn:3 statement:stmt];
  5106. addr_3 = [self textAtColumn:4 statement:stmt];
  5107. addr_4 = [self textAtColumn:5 statement:stmt];
  5108. zipcode = [self textAtColumn:6 statement:stmt];
  5109. state = [self textAtColumn:7 statement:stmt]; // state code
  5110. city = [self textAtColumn:8 statement:stmt];
  5111. firt_name = [self textAtColumn:9 statement:stmt];
  5112. last_name = [self textAtColumn:10 statement:stmt];
  5113. phone = [self textAtColumn:11 statement:stmt];
  5114. fax = [self textAtColumn:12 statement:stmt];
  5115. email = [self textAtColumn:13 statement:stmt];
  5116. notes = [self textAtColumn:14 statement:stmt];
  5117. price_type = [self textAtColumn:15 statement:stmt]; // name
  5118. sales_rep = [self textAtColumn:16 statement:stmt]; // code
  5119. img_0 = [self textAtColumn:17 statement:stmt];
  5120. img_1 = [self textAtColumn:18 statement:stmt];
  5121. img_2 = [self textAtColumn:19 statement:stmt];
  5122. contact_type = [self textAtColumn:20 statement:stmt];
  5123. }];
  5124. // decrypt
  5125. if (company_name) {
  5126. company_name = [AESCrypt fastdecrypt:company_name];
  5127. }
  5128. if (addr_1) {
  5129. addr_1 = [AESCrypt fastdecrypt:addr_1];
  5130. }
  5131. if (phone) {
  5132. phone = [AESCrypt fastdecrypt:phone];
  5133. }
  5134. NSString *countrySql = [NSString stringWithFormat:@"select code from offline_country where name = '%@';",[self translateSingleQuote:country]];
  5135. countryCode = [iSalesDB jk_queryText:countrySql];
  5136. stateCode = state;
  5137. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  5138. if ([params[@"refresh_trigger"] isEqualToString:@"country"]) {
  5139. NSString *code_id = params[@"country"];
  5140. countryCode_id = code_id;
  5141. countryCode = [self countryCodeByid:code_id];
  5142. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) {
  5143. NSString *zip_code = params[@"zipcode"];
  5144. // 剔除全部为空格
  5145. int spaceCount = 0;
  5146. for (int i = 0; i < zip_code.length; i++) {
  5147. if ([zip_code characterAtIndex:i] == ' ') {
  5148. spaceCount++;
  5149. }
  5150. }
  5151. if (spaceCount == zip_code.length) {
  5152. zip_code = @"";
  5153. }
  5154. if (zipcode.length > 0) {
  5155. zipcode = zip_code;
  5156. countryCode_id = params[@"country"];
  5157. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  5158. countryCode = [dic valueForKey:@"country_code"];
  5159. if (!countryCode) {
  5160. NSString *code_id = params[@"country"];
  5161. countryCode = [self countryCodeByid:code_id];
  5162. }
  5163. stateCode = [dic valueForKey:@"state_code"];
  5164. if (!stateCode.length) {
  5165. stateCode = params[@"state"];
  5166. }
  5167. city = [dic valueForKey:@"city"];
  5168. if (!city.length) {
  5169. city = params[@"city"];
  5170. }
  5171. // zip code
  5172. NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_8"] mutableCopy];
  5173. [zipDic setValue:zipcode forKey:@"value"];
  5174. [section_0 setValue:zipDic forKey:@"item_8"];
  5175. } else {
  5176. NSString *code_id = params[@"country"];
  5177. countryCode = [self countryCodeByid:code_id];
  5178. stateCode = params[@"state"];
  5179. city = params[@"city"];
  5180. }
  5181. }
  5182. }
  5183. // 0 Country
  5184. // 1 Company Name
  5185. // 2 Contact ID
  5186. // 3 Picture
  5187. // 4 Address 1
  5188. // 5 Address 2
  5189. // 6 Address 3
  5190. // 7 Address 4
  5191. // 8 Zip Code
  5192. // 9 State/Province
  5193. // 10 City
  5194. // 11 Contact First Name
  5195. // 12 Contact Last Name
  5196. // 13 Phone
  5197. // 14 Fax
  5198. // 15 Email
  5199. // 16 Contact Notes
  5200. // 17 Price Type
  5201. // 18 Contact Type
  5202. // 19 Sales Rep
  5203. // country
  5204. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  5205. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:section_0];
  5206. // company
  5207. [self setValue:company_name forItemKey:@"item_1" valueKey:@"value" inDictionary:section_0];
  5208. // contact_id
  5209. [self setValue:contact_id forItemKey:@"item_2" valueKey:@"value" inDictionary:section_0];
  5210. // picture
  5211. NSString *img_avalue = [NSString stringWithFormat:@"%@,%@,%@",img_0,img_1,img_2];
  5212. [self setValue:img_avalue forItemKey:@"item_3" valueKey:@"avalue" inDictionary:section_0];
  5213. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_aname_0" inDictionary:section_0];
  5214. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_aname_1" inDictionary:section_0];
  5215. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_aname_2" inDictionary:section_0];
  5216. [self setValue:img_0 forItemKey:@"item_3" valueKey:@"img_url_0" inDictionary:section_0];
  5217. [self setValue:img_1 forItemKey:@"item_3" valueKey:@"img_url_1" inDictionary:section_0];
  5218. [self setValue:img_2 forItemKey:@"item_3" valueKey:@"img_url_2" inDictionary:section_0];
  5219. // addr 1 2 3 4
  5220. [self setValue:addr_1 forItemKey:@"item_4" valueKey:@"value" inDictionary:section_0];
  5221. [self setValue:addr_2 forItemKey:@"item_5" valueKey:@"value" inDictionary:section_0];
  5222. [self setValue:addr_3 forItemKey:@"item_6" valueKey:@"value" inDictionary:section_0];
  5223. [self setValue:addr_4 forItemKey:@"item_7" valueKey:@"value" inDictionary:section_0];
  5224. // zip code
  5225. [self setValue:zipcode forItemKey:@"item_8" valueKey:@"value" inDictionary:section_0];
  5226. // state
  5227. NSDictionary *allState = [self offline_getStateByCountryCode:countryCode checkedState:stateCode];
  5228. [self setValue:allState forItemKey:@"item_9" valueKey:@"cadedate" inDictionary:section_0];
  5229. // city
  5230. [self setValue:city forItemKey:@"item_10" valueKey:@"value" inDictionary:section_0];
  5231. // first last
  5232. [self setValue:firt_name forItemKey:@"item_11" valueKey:@"value" inDictionary:section_0];
  5233. [self setValue:last_name forItemKey:@"item_12" valueKey:@"value" inDictionary:section_0];
  5234. // phone fax email
  5235. [self setValue:phone forItemKey:@"item_13" valueKey:@"value" inDictionary:section_0];
  5236. [self setValue:fax forItemKey:@"item_14" valueKey:@"value" inDictionary:section_0];
  5237. [self setValue:email forItemKey:@"item_15" valueKey:@"value" inDictionary:section_0];
  5238. // notes
  5239. [self setValue:notes forItemKey:@"item_16" valueKey:@"value" inDictionary:section_0];
  5240. // price
  5241. NSMutableDictionary *priceDic = [[self offline_getPrice] mutableCopy];
  5242. for (NSString *key in priceDic.allKeys) {
  5243. if ([key containsString:@"val_"]) {
  5244. NSMutableDictionary *dic = [priceDic[key] mutableCopy];
  5245. if ([dic[@"value"] isEqualToString:price_type]) {
  5246. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5247. [priceDic setValue:dic forKey:key];
  5248. }
  5249. }
  5250. }
  5251. [self setValue:priceDic forItemKey:@"item_17" valueKey:@"cadedate" inDictionary:section_0];
  5252. // Contact Rep
  5253. NSDictionary *contactTypeDic = [self offline_getContactTypeChecked:contact_type];
  5254. [self setValue:contactTypeDic forItemKey:@"item_18" valueKey:@"cadedate" inDictionary:section_0];
  5255. // Sales Rep
  5256. NSMutableDictionary *repDic = [[self offline_getSalesRep] mutableCopy];
  5257. for (NSString *key in repDic.allKeys) {
  5258. if ([key containsString:@"val_"]) {
  5259. NSMutableDictionary *dic = [repDic[key] mutableCopy];
  5260. NSString *value = dic[@"value"];
  5261. NSString *code = [[[value componentsSeparatedByString:@"-"] firstObject] stringByReplacingOccurrencesOfString:@" " withString:@""];
  5262. if (code && [code isEqualToString:sales_rep]) {
  5263. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5264. [repDic setValue:dic forKey:key];
  5265. }
  5266. }
  5267. }
  5268. [self setValue:repDic forItemKey:@"item_19" valueKey:@"cadedate" inDictionary:section_0];
  5269. [ret setValue:section_0 forKey:@"section_0"];
  5270. return [RAUtils dict2data:ret];
  5271. }
  5272. #pragma mark save contact
  5273. +(NSData *) offline_saveContact:(NSMutableDictionary *) params
  5274. {
  5275. return [self offline_saveContact:params update:YES isCustomer:YES];
  5276. }
  5277. #pragma mark category
  5278. + (void)check:(NSString *)ck valueKey:(NSString *)valueKey inDictionary:(NSMutableDictionary *)dic{
  5279. if (ck) {
  5280. NSMutableDictionary *res = [self mutableDictionary:valueKey inDictionary:dic];
  5281. for (NSString *key in res.allKeys) {
  5282. if (![key isEqualToString:@"count"]) {
  5283. NSMutableDictionary *val = [self mutableDictionary:key inDictionary:res];
  5284. [val setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5285. if ([val[@"value"] isEqualToString:ck]) {
  5286. [val setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5287. }
  5288. [res setValue:val forKey:key];
  5289. }
  5290. }
  5291. [dic setValue:res forKey:valueKey];
  5292. }
  5293. }
  5294. + (NSDictionary *)categoryList:(NSMutableDictionary *)params limited:(BOOL)limited {
  5295. // NSString* orderCode = [params valueForKey:@"orderCode"];
  5296. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5297. NSString* category = [params valueForKey:@"category"];
  5298. if (!category || [category isEqualToString:@""]) {
  5299. category = @"%";
  5300. }
  5301. category = [self translateSingleQuote:category];
  5302. int limit = [[params valueForKey:@"limit"] intValue];
  5303. int offset = [[params valueForKey:@"offset"] intValue];
  5304. // NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  5305. NSString *limit_str = @"";
  5306. if (limited) {
  5307. limit_str = [NSString stringWithFormat:@"limit %d offset %d ",limit,offset];
  5308. }
  5309. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5310. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5311. sqlite3 *db = [iSalesDB get_db];
  5312. // [iSalesDB AddExFunction:db];
  5313. int count;
  5314. NSString *where = [NSString stringWithFormat:@"category like'%%#%@#%%' and is_active = 1",category];
  5315. NSString *sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where m.category like'%%#%@#%%' and m.is_active = 1 order by m.name %@ ;",appDelegate.order_code, category,limit_str];
  5316. double price_min = 0;
  5317. double price_max = 0;
  5318. if ([params.allKeys containsObject:@"alert"]) {
  5319. // alert
  5320. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5321. NSString *alert = params[@"alert"];
  5322. if ([alert isEqualToString:@"Display All"]) {
  5323. alert = [NSString stringWithFormat:@""];
  5324. } else {
  5325. alert = [self translateSingleQuote:alert];
  5326. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5327. }
  5328. // available
  5329. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5330. NSString *available = params[@"available"];
  5331. NSString *available_condition;
  5332. if ([available isEqualToString:@"Display All"]) {
  5333. available_condition = @"";
  5334. } else if ([available isEqualToString:@"Available Now"]) {
  5335. available_condition = @"and availability > 0";
  5336. } else {
  5337. available_condition = @"and availability == 0";
  5338. }
  5339. // best seller
  5340. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5341. NSString *best_seller = @"";
  5342. NSString *order_best_seller = @"m.name asc";
  5343. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5344. best_seller = @"and best_seller > 0";
  5345. order_best_seller = @"m.best_seller desc,m.name asc";
  5346. }
  5347. // price
  5348. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5349. NSString *price = params[@"price"];
  5350. price_min = 0;
  5351. price_max = MAXFLOAT;
  5352. if (appDelegate.user && price != nil) {
  5353. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5354. NSMutableString *priceName = [NSMutableString string];
  5355. for (int i = 0; i < priceTypeArray.count; i++) {
  5356. NSString *pricetype = priceTypeArray[i];
  5357. pricetype = [self translateSingleQuote:pricetype];
  5358. if (i == 0) {
  5359. [priceName appendFormat:@"'%@'",pricetype];
  5360. } else {
  5361. [priceName appendFormat:@",'%@'",pricetype];
  5362. }
  5363. }
  5364. if ([price isEqualToString:@"Display All"]) {
  5365. price = [NSString stringWithFormat:@""];
  5366. } else if([price containsString:@"+"]){
  5367. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5368. price_min = [price doubleValue];
  5369. 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];
  5370. } else {
  5371. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5372. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5373. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5374. 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];
  5375. }
  5376. } else {
  5377. price = @"";
  5378. }
  5379. // sold_by_qty : Sold in quantities of %@
  5380. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5381. NSString *qty = params[@"sold_by_qty"];
  5382. if ([qty isEqualToString:@"Display All"]) {
  5383. qty = @"";
  5384. } else {
  5385. qty = [self translateSingleQuote:qty];
  5386. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5387. }
  5388. // cate
  5389. // category = [self translateSingleQuote:category];
  5390. // NSString *cateWhere = [NSString stringWithFormat:@"category like'%%#%@#%%'",category];
  5391. // cate mutiple selection
  5392. NSString *category_id = params[@"category"];
  5393. NSMutableArray *cate_id_array = nil;
  5394. NSMutableString *cateWhere = [NSMutableString string];
  5395. if ([category_id isEqualToString:@""] || !category_id) {
  5396. [cateWhere appendString:@"1 = 1"];
  5397. } else {
  5398. if ([category_id containsString:@","]) {
  5399. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5400. } else {
  5401. cate_id_array = [@[category_id] mutableCopy];
  5402. }
  5403. [cateWhere appendString:@"("];
  5404. for (int i = 0; i < cate_id_array.count; i++) {
  5405. if (i == 0) {
  5406. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5407. } else {
  5408. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5409. }
  5410. }
  5411. [cateWhere appendString:@")"];
  5412. }
  5413. // where bestseller > 0 order by bestseller desc
  5414. // sql query: alert availability(int) best_seller(int) price qty
  5415. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.category,m.more_color from (select name,description,product_id,closeout,best_seller,category from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;",cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  5416. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5417. }
  5418. DebugLog(@"offline category where: %@",where);
  5419. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5420. if (!appDelegate.user) {
  5421. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5422. }
  5423. [ret setValue:filter forKey:@"filter"];
  5424. DebugLog(@"offline_category sql:%@",sqlQuery);
  5425. sqlite3_stmt * statement;
  5426. [ret setValue:@"2" forKey:@"result"];
  5427. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5428. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5429. // int count=0;
  5430. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5431. {
  5432. int i=0;
  5433. while (sqlite3_step(statement) == SQLITE_ROW)
  5434. {
  5435. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5436. char *name = (char*)sqlite3_column_text(statement, 0);
  5437. if(name==nil)
  5438. name="";
  5439. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5440. char *description = (char*)sqlite3_column_text(statement, 1);
  5441. if(description==nil)
  5442. description="";
  5443. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5444. int product_id = sqlite3_column_int(statement, 2);
  5445. int wid = sqlite3_column_int(statement, 3);
  5446. int closeout = sqlite3_column_int(statement, 4);
  5447. int cid = sqlite3_column_int(statement, 5);
  5448. int wisdelete = sqlite3_column_int(statement, 6);
  5449. NSString *categoryStr = [self textAtColumn:7 statement:statement];
  5450. int more_color = sqlite3_column_int(statement, 8);
  5451. // Defaul Category ID
  5452. __block NSString *categoryID = nil;
  5453. NSString *defaultCateSQL = [NSString stringWithFormat:@"select default_category from product where product_id=%d;",product_id];
  5454. [iSalesDB jk_query:defaultCateSQL db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  5455. char *default_category = (char*)sqlite3_column_text(stmt, 0);
  5456. if(default_category==nil)
  5457. default_category="";
  5458. NSString *nsdefault_category = [[NSString alloc]initWithUTF8String:default_category];
  5459. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"%#" withString:@""];
  5460. nsdefault_category=[nsdefault_category stringByReplacingOccurrencesOfString:@"#%" withString:@""];
  5461. categoryID = nsdefault_category;
  5462. }];
  5463. if (!categoryID.length) {
  5464. NSString *cateIDs = params[@"category"];
  5465. NSArray *requestCategoryArr = [cateIDs componentsSeparatedByString:@","];
  5466. NSArray *itemCategoryArr = [categoryStr componentsSeparatedByString:@","];
  5467. for (NSString *cateID in requestCategoryArr) {
  5468. BOOL needBreak = NO;
  5469. for (NSString *itemCateIDBox in itemCategoryArr) {
  5470. if (itemCateIDBox.length > 4) {
  5471. NSString *itemCategoryID = [itemCateIDBox substringFromIndex:2];
  5472. itemCategoryID = [itemCategoryID substringToIndex:itemCategoryID.length - 2];
  5473. if ([itemCategoryID isEqualToString:cateID]) {
  5474. needBreak = YES;
  5475. categoryID = itemCategoryID;
  5476. break;
  5477. }
  5478. }
  5479. }
  5480. if (needBreak) {
  5481. break;
  5482. }
  5483. }
  5484. }
  5485. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5486. if(wid !=0 && wisdelete != 1)
  5487. [item setValue:@"true" forKey:@"wish_exists"];
  5488. else
  5489. [item setValue:@"false" forKey:@"wish_exists"];
  5490. if(closeout==0)
  5491. [item setValue:@"false" forKey:@"is_closeout"];
  5492. else
  5493. [item setValue:@"true" forKey:@"is_closeout"];
  5494. if(cid==0)
  5495. [item setValue:@"false" forKey:@"cart_exists"];
  5496. else
  5497. [item setValue:@"true" forKey:@"cart_exists"];
  5498. if (more_color == 0) {
  5499. [item setObject:@(false) forKey:@"more_color"];
  5500. } else if (more_color == 1) {
  5501. [item setObject:@(true) forKey:@"more_color"];
  5502. }
  5503. [item addEntriesFromDictionary:imgjson];
  5504. // [item setValue:nsurl forKey:@"img"];
  5505. [item setValue:nsname forKey:@"name"];
  5506. [item setValue:nsdescription forKey:@"description"];
  5507. if (categoryID) {
  5508. [item setValue:categoryID forKey:@"item_category_id"];
  5509. }
  5510. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5511. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5512. i++;
  5513. }
  5514. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5515. [ret setObject:items forKey:@"items"];
  5516. sqlite3_finalize(statement);
  5517. } else {
  5518. DebugLog(@"nothing...");
  5519. }
  5520. DebugLog(@"count:%d",count);
  5521. [iSalesDB close_db:db];
  5522. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5523. return ret;
  5524. }
  5525. +(NSDictionary*) offline_category :(NSMutableDictionary *) params
  5526. {
  5527. return [self categoryList:params limited:YES];
  5528. }
  5529. # pragma mark item search
  5530. + (NSDictionary *) itemsearch:(NSMutableDictionary *)params limited:(BOOL)limited {
  5531. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5532. BOOL filterSearch = ![params.allKeys containsObject:@"covertype"];
  5533. NSMutableDictionary *ret = [self dictionaryFileName:@"category.json"];
  5534. NSMutableDictionary *filter = [[ret objectForKey:@"filter"] mutableCopy];
  5535. // category
  5536. NSDictionary *category_menu = [self offline_category_menu];
  5537. [filter setValue:category_menu forKey:@"category"];
  5538. NSString* where= nil;
  5539. NSString* orderby= @"m.name";
  5540. if (!filterSearch) {
  5541. int covertype = [[params valueForKey:@"covertype"] intValue];
  5542. switch (covertype) {
  5543. case 0:
  5544. {
  5545. where=@"m.category like'%%#005#%%'";
  5546. break;
  5547. }
  5548. case 1:
  5549. {
  5550. where=@"m.alert like '%QS%'";
  5551. break;
  5552. }
  5553. case 2:
  5554. {
  5555. where=@"m.availability>0";
  5556. break;
  5557. }
  5558. case 3:
  5559. {
  5560. where=@"m.best_seller>0";
  5561. orderby=@"m.best_seller desc,m.name asc";
  5562. break;
  5563. }
  5564. default:
  5565. where=@"1=1";
  5566. break;
  5567. }
  5568. }
  5569. int limit = [[params valueForKey:@"limit"] intValue];
  5570. int offset = [[params valueForKey:@"offset"] intValue];
  5571. NSString *limit_str = @"";
  5572. if (limited) {
  5573. limit_str = [NSString stringWithFormat:@"limit %d offset %d",limit,offset];
  5574. }
  5575. sqlite3 *db = [iSalesDB get_db];
  5576. // [iSalesDB AddExFunction:db];
  5577. int count;
  5578. NSString *sqlQuery = nil;
  5579. where = [where stringByAppendingString:@" and m.is_active = 1"];
  5580. sqlQuery=[NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from product m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id where %@ order by %@ %@;",appDelegate.order_code, where,orderby,limit_str];
  5581. double price_min = 0;
  5582. double price_max = 0;
  5583. if (filterSearch) {
  5584. // alert
  5585. [self check:params[@"alert"] valueKey:@"alert" inDictionary:filter];
  5586. NSString *alert = params[@"alert"];
  5587. if ([alert isEqualToString:@"Display All"]) {
  5588. alert = [NSString stringWithFormat:@""];
  5589. } else {
  5590. alert = [self translateSingleQuote:alert];
  5591. alert = [NSString stringWithFormat:@"and alert = '%@'",alert];
  5592. }
  5593. // available
  5594. [self check:params[@"available"] valueKey:@"availability" inDictionary:filter];
  5595. NSString *available = params[@"available"];
  5596. NSString *available_condition;
  5597. if ([available isEqualToString:@"Display All"]) {
  5598. available_condition = @"";
  5599. } else if ([available isEqualToString:@"Available Now"]) {
  5600. available_condition = @"and availability > 0";
  5601. } else {
  5602. available_condition = @"and availability == 0";
  5603. }
  5604. // best seller
  5605. [self check:params[@"bestseller"] valueKey:@"best_seller" inDictionary:filter];
  5606. NSString *best_seller = @"";
  5607. NSString *order_best_seller = @"m.name asc";
  5608. if ([params[@"bestseller"] isEqualToString:@"Yes"]) {
  5609. best_seller = @"and best_seller > 0";
  5610. order_best_seller = @"m.best_seller desc,m.name asc";
  5611. }
  5612. // price
  5613. [self check:params[@"price"] valueKey:@"price" inDictionary:filter];
  5614. NSString *price = params[@"price"];
  5615. price_min = 0;
  5616. price_max = MAXFLOAT;
  5617. if (appDelegate.user) {
  5618. NSArray *priceTypeArray = [self get_contact_default_price_type:nil db:db];
  5619. NSMutableString *priceName = [NSMutableString string];
  5620. for (int i = 0; i < priceTypeArray.count; i++) {
  5621. NSString *pricetype = priceTypeArray[i];
  5622. pricetype = [self translateSingleQuote:pricetype];
  5623. if (i == 0) {
  5624. [priceName appendFormat:@"'%@'",pricetype];
  5625. } else {
  5626. [priceName appendFormat:@",'%@'",pricetype];
  5627. }
  5628. }
  5629. if ([price isEqualToString:@"Display All"]) {
  5630. price = [NSString stringWithFormat:@""];
  5631. } else if([price containsString:@"+"]){
  5632. price = [price stringByReplacingOccurrencesOfString:@"+" withString:@""];
  5633. price_min = [price doubleValue];
  5634. 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];
  5635. } else {
  5636. NSArray *priceArray = [price componentsSeparatedByString:@"-"];
  5637. price_min = [[priceArray objectAtIndex:0] doubleValue];
  5638. price_max = [[priceArray objectAtIndex:1] doubleValue];
  5639. 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];
  5640. }
  5641. } else {
  5642. price = @"";
  5643. }
  5644. // sold_by_qty : Sold in quantities of %@
  5645. [self check:params[@"sold_by_qty"] valueKey:@"qty" inDictionary:filter];
  5646. NSString *qty = params[@"sold_by_qty"];
  5647. if ([qty isEqualToString:@"Display All"]) {
  5648. qty = @"";
  5649. } else {
  5650. qty = [self translateSingleQuote:qty];
  5651. qty = [NSString stringWithFormat:@"and model_set = 'Sold in quantities of %@'",qty];
  5652. }
  5653. // category
  5654. NSString *category_id = params[@"ctgId"];
  5655. NSMutableArray *cate_id_array = nil;
  5656. NSMutableString *cateWhere = [NSMutableString string];
  5657. if ([category_id isEqualToString:@""] || !category_id) {
  5658. [cateWhere appendString:@"1 = 1"];
  5659. } else {
  5660. if ([category_id containsString:@","]) {
  5661. cate_id_array = [[category_id componentsSeparatedByString:@","] mutableCopy];
  5662. } else {
  5663. cate_id_array = [@[category_id] mutableCopy];
  5664. }
  5665. /*-----------*/
  5666. NSMutableDictionary *cateDic = [[filter objectForKey:@"category"] mutableCopy];// 老子
  5667. [cateWhere appendString:@"("];
  5668. for (int i = 0; i < cate_id_array.count; i++) {
  5669. for (NSString *key0 in cateDic.allKeys) {
  5670. if ([key0 containsString:@"category_"]) {
  5671. NSMutableDictionary *category0 = [[cateDic objectForKey:key0] mutableCopy]; // 儿子
  5672. for (NSString *key1 in category0.allKeys) {
  5673. if ([key1 containsString:@"category_"]) {
  5674. NSMutableDictionary *category1 = [[category0 objectForKey:key1] mutableCopy]; // 孙子
  5675. [category1 setValue:[NSNumber numberWithInteger:0] forKey:@"check"];
  5676. if ([[category1 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5677. cate_id_array[i] = [category1 objectForKey:@"id"];
  5678. if (i == 0) {
  5679. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5680. } else {
  5681. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5682. }
  5683. [category1 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5684. [category0 setValue:category1 forKey:key1];
  5685. [cateDic setValue:category0 forKey:key0];
  5686. }
  5687. } else if ([key1 isEqualToString:@"cid"]) { // 2017-03-10 修复Item Search父节点下没有子节点的情况下选中父节点查询没有结果
  5688. if ([[category0 objectForKey:@"cid"] isEqualToString:cate_id_array[i]]) {
  5689. cate_id_array[i] = [category0 objectForKey:@"id"];
  5690. if (i == 0) {
  5691. [cateWhere appendFormat:@"category like'%%#%@#%%'",cate_id_array[i]];
  5692. } else {
  5693. [cateWhere appendFormat:@" or category like'%%#%@#%%'",cate_id_array[i]];
  5694. }
  5695. [category0 setValue:[NSNumber numberWithInteger:1] forKey:@"check"];
  5696. [cateDic setValue:category0 forKey:key0];
  5697. }
  5698. }
  5699. }
  5700. }
  5701. }
  5702. }
  5703. [cateWhere appendString:@")"];
  5704. [filter setValue:cateDic forKey:@"category"];
  5705. }
  5706. // where bestseller > 0 order by bestseller desc
  5707. // sql query: alert availability(int) best_seller(int) price qty
  5708. sqlQuery = [NSString stringWithFormat:@"select m.name,m.description,m.product_id,w._id,m.closeout,c._id,w.is_delete,m.more_color from (select name,description,product_id,closeout,best_seller from product where %@ %@ %@ %@ %@ %@ and is_active = 1) m left join wishlist w on m.product_id=w.product_id left join (select _id,product_id from offline_cart where so_no='%@') c on c.product_id=m.product_id order by %@ %@;", cateWhere,best_seller,alert,available_condition,qty,price,appDelegate.order_code,order_best_seller,limit_str];
  5709. // count
  5710. where =[NSString stringWithFormat:@"%@ %@ %@ %@ %@ %@ and m.is_active = 1",cateWhere,best_seller,alert,available_condition,qty,price];
  5711. }
  5712. where = [where stringByReplacingOccurrencesOfString:@"m." withString:@""];
  5713. count = [iSalesDB get_recordcount:db table:@"product" where:where];
  5714. if (!appDelegate.user) {
  5715. [filter setValue:@{@"count" : @(0)} forKey:@"price"];
  5716. }
  5717. [ret setValue:filter forKey:@"filter"];
  5718. DebugLog(@"offline_itemsearch sql:%@",sqlQuery);
  5719. sqlite3_stmt * statement;
  5720. [ret setValue:@"2" forKey:@"result"];
  5721. [ret setValue:[NSString stringWithFormat:@"%d",count] forKey:@"item_total_count"];
  5722. NSMutableDictionary* items = [[NSMutableDictionary alloc] init];
  5723. // int count=0;
  5724. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  5725. {
  5726. int i=0;
  5727. while (sqlite3_step(statement) == SQLITE_ROW)
  5728. {
  5729. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  5730. char *name = (char*)sqlite3_column_text(statement, 0);
  5731. NSString *nsname = [[NSString alloc]initWithUTF8String:name];
  5732. char *description = (char*)sqlite3_column_text(statement, 1);
  5733. NSString *nsdescription = [[NSString alloc]initWithUTF8String:description];
  5734. int product_id = sqlite3_column_int(statement, 2);
  5735. int wid = sqlite3_column_int(statement, 3);
  5736. int closeout = sqlite3_column_int(statement, 4);
  5737. int cid = sqlite3_column_int(statement, 5);
  5738. int wisdelete = sqlite3_column_int(statement, 6);
  5739. int more_color = sqlite3_column_int(statement, 7);
  5740. NSMutableDictionary* imgjson=[self category_img:product_id db:db];
  5741. if(wid !=0 && wisdelete != 1)
  5742. [item setValue:@"true" forKey:@"wish_exists"];
  5743. else
  5744. [item setValue:@"false" forKey:@"wish_exists"];
  5745. if(closeout==0)
  5746. [item setValue:@"false" forKey:@"is_closeout"];
  5747. else
  5748. [item setValue:@"true" forKey:@"is_closeout"];
  5749. if(cid==0)
  5750. [item setValue:@"false" forKey:@"cart_exists"];
  5751. else
  5752. [item setValue:@"true" forKey:@"cart_exists"];
  5753. if (more_color == 0) {
  5754. [item setObject:@(false) forKey:@"more_color"];
  5755. } else if (more_color == 1) {
  5756. [item setObject:@(true) forKey:@"more_color"];
  5757. }
  5758. [item addEntriesFromDictionary:imgjson];
  5759. // [item setValue:nsurl forKey:@"img"];
  5760. [item setValue:nsname forKey:@"fash_name"];
  5761. [item setValue:nsdescription forKey:@"description"];
  5762. [item setValue:[NSString stringWithFormat:@"%d",product_id] forKey:@"product_id"];
  5763. [items setObject:item forKey:[NSString stringWithFormat:@"item_%d",i]];
  5764. i++;
  5765. }
  5766. [items setValue:[NSString stringWithFormat:@"%d",i] forKey:@"count"];
  5767. [ret setObject:items forKey:@"items"];
  5768. sqlite3_finalize(statement);
  5769. }
  5770. [iSalesDB close_db:db];
  5771. DebugLog(@"data string: %@",[RAUtils dict2string:ret] );
  5772. return ret;
  5773. }
  5774. +(NSDictionary*) offline_itemsearch:(NSMutableDictionary *) params
  5775. {
  5776. return [self itemsearch:params limited:YES];
  5777. }
  5778. #pragma mark order detail
  5779. + (NSString *)replaceHtml:(NSString *)html String:(NSString *)str0 withString:(NSString *)str1 {
  5780. if (str1.length == 0) {
  5781. str1 = @"&nbsp";
  5782. }
  5783. NSString *str = [html stringByReplacingOccurrencesOfString:str0 withString:str1];
  5784. return str;
  5785. }
  5786. +(NSString*) timeFormat:(NSString*)datetime from:(NSString*) from to:(NSString*) to
  5787. {
  5788. NSDateFormatter *fromformatter = [[NSDateFormatter alloc] init];
  5789. [fromformatter setDateFormat:from];
  5790. NSDate *date = [fromformatter dateFromString:datetime];
  5791. NSDateFormatter * toformatter = [[NSDateFormatter alloc] init ];
  5792. [toformatter setDateFormat:to];
  5793. NSString * ret = [toformatter stringFromDate:date];
  5794. return ret;
  5795. }
  5796. + (NSString *)changeDateTime:(NSString *)dateTime Formate:(NSString *)formate withFormate:(NSString *)newFormate {
  5797. // 把毫秒去掉
  5798. if ([dateTime containsString:@"."]) {
  5799. dateTime = [[dateTime componentsSeparatedByString:@"."] firstObject];
  5800. }
  5801. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  5802. formatter.dateFormat = formate;
  5803. // [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];;
  5804. NSDate *date = [formatter dateFromString:dateTime];
  5805. formatter.dateFormat = newFormate;
  5806. NSString *result = [formatter stringFromDate:date];
  5807. return result ? result : @"";
  5808. }
  5809. + (NSString *)changeDateTimeFormate:(NSString *)dateTime {
  5810. return [self changeDateTime:dateTime Formate:@"yyyy-MM-dd HH:mm:ss" withFormate:@"MM/dd/yyyy HH:mm:ss"];
  5811. // return [self timeFormat:dateTime from:@"YYYY-MM-DD HH:mm:ss" to:@"MM/DD/YYYY HH:mm:ss"];
  5812. }
  5813. +(NSData*) offline_orderdetail :(NSMutableDictionary *) params
  5814. {
  5815. DebugLog(@"offline oderdetail params: %@",params);
  5816. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  5817. int orderId = [params[@"orderId"] intValue];
  5818. NSMutableDictionary* ret = [self dictionaryFileName:@"orderDetail.json"];
  5819. // decrypt card number and card security code
  5820. // 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 ];
  5821. NSString *sql = [NSString stringWithFormat:@"select o._id,o.so_id,o.poNumber,o.create_time,o.status,decrypt(c.company_name),o.customer_contact,decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,o.logist,o.shipping,o.lift_gate_value,o.general_notes,o.internal_notes,o.paymentType,o.lift_gate,o.receive_name,o.receive_contact,o.receive_ext,o.sender_name,o.sender_contact,o.sender_ext,o.shipping_billto_name,o.shipping_billto_contact,o.shipping_billto_ext,o.billing_name,o.billing_contact,o.billing_ext,o.returnto_name,o.returnto_contact,o.returnto_ext,o.handling_fee_value,o.paymentsAndCredits,o.total_price,o.customer_cid,o.credit_card_first_name,o.credit_card_last_name,o.credit_card_address1,o.credit_card_address2,o.credit_card_zipcode,o.credit_card_type,decrypt(o.credit_card_number),decrypt(o.credit_card_security_code),decrypt(o.credit_card_expiration_month),o.credit_card_city,o.credit_card_state,o.shipNull,o.erpOrderStatus,decrypt(o.credit_card_expiration_year),o.sign_picpath,o.customer_contact,o.customer_email,o.customer_phone,o.customer_fax,o.editable,o.lift_null,o.handling_null,o.ship_via,c.city,c.state,c.zipcode,c.country from (select _id,so_id,poNumber,create_time,status,customer_cid,customer_contact,logist,shipping,lift_gate_value,general_notes,internal_notes,paymentType,lift_gate,receive_name,receive_contact,receive_ext,sender_name,sender_contact,sender_ext,shipping_billto_name,shipping_billto_contact,shipping_billto_ext,billing_name,billing_contact,billing_ext,returnto_name,returnto_contact,returnto_ext,handling_fee_value,paymentsAndCredits,total_price,customer_cid,credit_card_first_name,credit_card_last_name,credit_card_address1,credit_card_address2,credit_card_zipcode,credit_card_type,credit_card_number,credit_card_security_code,credit_card_expiration_month,credit_card_city,credit_card_state,ifnull(shipping,'ship_null') as shipNull,erpOrderStatus,credit_card_expiration_year,sign_picpath,customer_contact,customer_email,customer_phone,customer_fax,order_id is null as editable,lift_gate_value is null as lift_null,handling_fee_value is null as handling_null,ship_via from offline_order where _id=%d) o left join offline_contact c on o.customer_cid=c.contact_id",orderId];
  5822. sqlite3 *db = [iSalesDB get_db];
  5823. sqlite3_stmt * statement;
  5824. NSString *customerID = @""; // 在查询结果中还需要调用offline_contactinfo,需要使用,其中要打开db 就会lock,故而将其置于外面,在close db后在调用
  5825. NSString *nssoid = @"";
  5826. NSString* orderinfo = @"";
  5827. NSString *moreInfo = @"";
  5828. double handlingFee = 0;
  5829. double payments_and_credist = 0;
  5830. double totalPrice = 0;
  5831. double shippingFee = 0;
  5832. double lift_gate = 0;
  5833. NSString *customer_contact = @"";
  5834. NSString *customer_email = @"";
  5835. NSString *customer_fax = @"";
  5836. NSString *customer_phone = @"";
  5837. NSString *customer_city = @"";
  5838. NSString *customer_state = @"";
  5839. NSString *customer_zipcode = @"";
  5840. NSString *customer_country = @"";
  5841. if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK)
  5842. {
  5843. if (sqlite3_step(statement) == SQLITE_ROW)
  5844. {
  5845. int order_id = sqlite3_column_int(statement, 0);
  5846. NSString *sign_url = [self textAtColumn:51 statement:statement];
  5847. ret[@"sign_url"] = sign_url;
  5848. customer_contact = [self textAtColumn:52 statement:statement];
  5849. customer_email = [self textAtColumn:53 statement:statement];
  5850. customer_phone = [self textAtColumn:54 statement:statement];
  5851. customer_fax = [self textAtColumn:55 statement:statement];
  5852. customer_city = [self textAtColumn:60 statement:statement];
  5853. customer_state = [self textAtColumn:61 statement:statement];
  5854. customer_zipcode = [self textAtColumn:62 statement:statement];
  5855. customer_country = [self textAtColumn:63 statement:statement];
  5856. int offline_edit=sqlite3_column_int(statement, 56);
  5857. ret[@"offline_edit"]= [NSNumber numberWithInt:offline_edit];
  5858. char *soid = (char*)sqlite3_column_text(statement, 1);
  5859. if(soid==nil)
  5860. soid= "";
  5861. nssoid= [[NSString alloc]initWithUTF8String:soid];
  5862. // so#
  5863. ret[@"so#"] = nssoid;
  5864. char *poNumber = (char*)sqlite3_column_text(statement, 2);
  5865. if(poNumber==nil)
  5866. poNumber= "";
  5867. NSString *nspoNumber= [[NSString alloc]initWithUTF8String:poNumber];
  5868. char *create_time = (char*)sqlite3_column_text(statement, 3);
  5869. if(create_time==nil)
  5870. create_time= "";
  5871. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  5872. nscreate_time = [[self changeDateTimeFormate:nscreate_time] stringByAppendingString:@" PST"];// 与在线统一
  5873. int status = sqlite3_column_int(statement, 4);
  5874. int erpStatus = sqlite3_column_int(statement, 49);
  5875. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  5876. // status
  5877. if (status > 1 && status != 3) {
  5878. status = erpStatus;
  5879. } else if (status == 3) {
  5880. status = 15;
  5881. }
  5882. ret[@"orderStatus"] = [NSNumber numberWithInteger:status];
  5883. ret[@"order_status"] = nsstatus;
  5884. char *company_name = (char*)sqlite3_column_text(statement, 5);
  5885. if(company_name==nil)
  5886. company_name= "";
  5887. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  5888. // company name
  5889. ret[@"company_name"] = nscompany_name;
  5890. char *customer_contact = (char*)sqlite3_column_text(statement, 6);
  5891. if(customer_contact==nil)
  5892. customer_contact= "";
  5893. NSString *nscustomer_contact= [[NSString alloc]initWithUTF8String:customer_contact];
  5894. char *addr_1 = (char*)sqlite3_column_text(statement, 7);
  5895. if(addr_1==nil)
  5896. addr_1="";
  5897. NSString *nsaddr_1= [[NSString alloc]initWithUTF8String:addr_1];
  5898. char *addr_2 = (char*)sqlite3_column_text(statement, 8);
  5899. if(addr_2==nil)
  5900. addr_2="";
  5901. NSString *nsaddr_2= [[NSString alloc]initWithUTF8String:addr_2];
  5902. char *addr_3 = (char*)sqlite3_column_text(statement, 9);
  5903. if(addr_3==nil)
  5904. addr_3="";
  5905. NSString *nsaddr_3= [[NSString alloc]initWithUTF8String:addr_3];
  5906. char *addr_4 = (char*)sqlite3_column_text(statement, 10);
  5907. if(addr_4==nil)
  5908. addr_4="";
  5909. NSString *nsaddr_4= [[NSString alloc]initWithUTF8String:addr_4];
  5910. NSMutableArray* arr_addr = [[NSMutableArray alloc] init];
  5911. [arr_addr addObject:nsaddr_1];
  5912. [arr_addr addObject:nsaddr_2];
  5913. [arr_addr addObject:nsaddr_3];
  5914. [arr_addr addObject:nsaddr_4];
  5915. [arr_addr addObject:[NSString stringWithFormat:@"%@,",customer_city]];
  5916. [arr_addr addObject:customer_state];
  5917. [arr_addr addObject:customer_zipcode];
  5918. [arr_addr addObject:customer_country];
  5919. NSString * customer_address = [RAUtils arr2string:arr_addr separator:@" " trim:true];
  5920. char *logist = (char*)sqlite3_column_text(statement, 11);
  5921. if(logist==nil)
  5922. logist= "";
  5923. NSString *nslogist=[[NSString alloc]initWithUTF8String:logist];
  5924. if (/*status == -11 || */status == 10 || status == 11) {
  5925. nslogist = [self textAtColumn:59 statement:statement];
  5926. };
  5927. NSString *shipping = [NSString stringWithFormat:@"$%.2f",sqlite3_column_double(statement, 12)];
  5928. if ([[self textAtColumn:48 statement:statement] isEqualToString:@"ship_null"]) {
  5929. shipping = @"Shipping To Be Quoted";
  5930. } else {
  5931. shippingFee = sqlite3_column_double(statement, 12);
  5932. }
  5933. // Shipping
  5934. ret[@"Shipping"] = shipping;
  5935. int have_lift_gate = sqlite3_column_int(statement, 17);
  5936. lift_gate = sqlite3_column_double(statement, 13);
  5937. // Liftgate Fee(No loading dock)
  5938. if (!have_lift_gate) {
  5939. lift_gate = 0;
  5940. }
  5941. ret[@"Liftgate Fee(No loading dock)"] = [NSString stringWithFormat:@"$%.2f",lift_gate];
  5942. if (sqlite3_column_int(statement, 57)) {
  5943. ret[@"Liftgate Fee(No loading dock)"] = @"Shipping To Be Quoted";
  5944. }
  5945. // if ([nslogist isEqualToString:@"WILL CALL"]) {
  5946. // [ret removeObjectForKey:@"Shipping"];
  5947. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  5948. // }
  5949. //
  5950. // if (have_lift_gate) {
  5951. // [ret removeObjectForKey:@"Liftgate Fee(No loading dock)"];
  5952. // }
  5953. char *general_notes = (char*)sqlite3_column_text(statement, 14);
  5954. if(general_notes==nil)
  5955. general_notes= "";
  5956. NSString *nsgeneral_notes=[[NSString alloc]initWithUTF8String:general_notes];
  5957. char *internal_notes = (char*)sqlite3_column_text(statement, 15);
  5958. if(internal_notes==nil)
  5959. internal_notes= "";
  5960. NSString *nsinternal_notes=[[NSString alloc]initWithUTF8String:internal_notes];
  5961. char *payment_type = (char*)sqlite3_column_text(statement, 16);
  5962. if(payment_type==nil)
  5963. payment_type= "";
  5964. NSString *nspayment_type=[[NSString alloc]initWithUTF8String:payment_type];
  5965. // order info
  5966. orderinfo = [self textFileName:@"order_info.html"];
  5967. orderinfo = [self replaceHtml:orderinfo String:@"SO_or_nbsp" withString:nssoid];
  5968. orderinfo = [self replaceHtml:orderinfo String:@"PO_or_nbsp" withString:nspoNumber];
  5969. orderinfo = [self replaceHtml:orderinfo String:@"OrderDate_or_nbsp" withString:nscreate_time];
  5970. orderinfo = [self replaceHtml:orderinfo String:@"OrderStatus_or_nbsp" withString:nsstatus];
  5971. orderinfo = [self replaceHtml:orderinfo String:@"CustomerInfo_or_nbsp" withString:nscompany_name];
  5972. orderinfo = [self replaceHtml:orderinfo String:@"CustomerName_or_nbsp" withString:nscustomer_contact];
  5973. orderinfo = [self replaceHtml:orderinfo String:@"CustomerAddress_or_nbsp" withString:customer_address];
  5974. orderinfo = [self replaceHtml:orderinfo String:@"ShippingMethod_or_nbsp" withString:nslogist];
  5975. orderinfo = [self replaceHtml:orderinfo String:@"Shipping_or_nbsp" withString:shipping];
  5976. orderinfo = [self replaceHtml:orderinfo String:@"LiftgateFee_or_nbsp" withString:[NSString stringWithFormat:@"%.2f",lift_gate]];
  5977. orderinfo = [self replaceHtml:orderinfo String:@"GeneralNotes_or_nbsp" withString:nsgeneral_notes];
  5978. orderinfo = [self replaceHtml:orderinfo String:@"InternalNotes_or_nbsp" withString:nsinternal_notes];
  5979. NSString *payment = nil;
  5980. if([nspayment_type isEqualToString:@"Credit Card"])
  5981. {
  5982. payment = [self textFileName:@"creditcardpayment.html"];
  5983. NSString *card_first_name = [self textAtColumn:37 statement:statement];
  5984. NSString *card_last_name = [self textAtColumn:38 statement:statement];
  5985. NSString *card_addr1 = [self textAtColumn:39 statement:statement];
  5986. NSString *card_addr2 = [self textAtColumn:40 statement:statement];
  5987. NSString *card_zipcode = [self textAtColumn:41 statement:statement];
  5988. NSString *card_type = [self textAtColumn:42 statement:statement];
  5989. if (card_type.length > 0) { // 显示星号
  5990. card_type = @"****";
  5991. }
  5992. NSString *card_number = [self textAtColumn:43 statement:statement]; // 只显示最后四位
  5993. if (card_number.length > 0 && card_number.length > 4) {
  5994. for (int i = 0; i < card_number.length - 4; i++) {
  5995. card_number = [card_number stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
  5996. }
  5997. } else {
  5998. card_number = @"";
  5999. }
  6000. NSString *card_security_code = [self textAtColumn:44 statement:statement];
  6001. NSString *card_expiration_month = [self textAtColumn:45 statement:statement];
  6002. NSString *card_expiration_year = [self textAtColumn:50 statement:statement];
  6003. NSString *card_expiration = [NSString stringWithFormat:@"%@/%@",card_expiration_month,card_expiration_year];
  6004. if (card_expiration_month.length && card_expiration_year.length) { // 显示星号
  6005. card_expiration = @"****";
  6006. }
  6007. NSString *card_city = [self textAtColumn:46 statement:statement];
  6008. NSString *card_state = [self textAtColumn:47 statement:statement];
  6009. payment = [self replaceHtml:payment String:@"CardNumber_or_nbsp" withString:card_number];
  6010. payment = [self replaceHtml:payment String:@"CardType_or_nbsp" withString:card_type];
  6011. payment = [self replaceHtml:payment String:@"CardExpireDate_or_nbsp" withString:card_expiration];
  6012. payment = [self replaceHtml:payment String:@"BillingFirstName_or_nbsp" withString:card_first_name];
  6013. payment = [self replaceHtml:payment String:@"BillingLastName_or_nbsp" withString:card_last_name];
  6014. payment = [self replaceHtml:payment String:@"BillingAddr1_or_nbsp" withString:card_addr1];
  6015. payment = [self replaceHtml:payment String:@"BillingAddr2_or_nbsp" withString:card_addr2];
  6016. payment = [self replaceHtml:payment String:@"BillingCity_or_nbsp" withString:card_city];
  6017. payment = [self replaceHtml:payment String:@"BillingState_or_nbsp" withString:card_state];
  6018. payment = [self replaceHtml:payment String:@"BillingZipCode_or_nbsp" withString:card_zipcode];
  6019. }
  6020. else
  6021. {
  6022. payment=[self textFileName:@"normalpayment.html"];
  6023. payment = [self replaceHtml:payment String:@"PAYMENTTYPE_PLACEHOLDER" withString:nspayment_type];
  6024. }
  6025. orderinfo=[self replaceHtml:orderinfo String:@"Payment_info_or_nbsp" withString:payment];
  6026. ret[@"result"]= [NSNumber numberWithInt:2];
  6027. // more info
  6028. moreInfo = [self textFileName:@"more_info.html"];
  6029. /*****ship to******/
  6030. // ShipToCompany_or_&nbsp
  6031. NSString *shipToCompany = [self textAtColumn:18 statement:statement];
  6032. moreInfo = [self replaceHtml:moreInfo String:@"ShipToCompany_or_" withString:shipToCompany];
  6033. NSString *shipToName = [self textAtColumn:19 statement:statement];
  6034. moreInfo = [self replaceHtml:moreInfo String:@"ShipToName_or_" withString:shipToName];
  6035. NSString *shipToAddr = [self textAtColumn:20 statement:statement];
  6036. moreInfo = [self replaceHtml:moreInfo String:@"ShipToAddress_or_" withString:shipToAddr];
  6037. /*****ship from******/
  6038. // ShipFromCompany_or_&nbsp
  6039. NSString *shipFromCompany = [self textAtColumn:21 statement:statement];
  6040. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromCompany_or_" withString:shipFromCompany];
  6041. NSString *shipFromName = [self textAtColumn:22 statement:statement];
  6042. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromName_or_" withString:shipFromName];
  6043. NSString *shipFromAddr = [self textAtColumn:23 statement:statement];
  6044. moreInfo = [self replaceHtml:moreInfo String:@"ShipFromAddress_or_" withString:shipFromAddr];
  6045. /*****freight to******/
  6046. // FreightBillToCompany_or_&nbsp
  6047. NSString *freightBillToCompany = [self textAtColumn:24 statement:statement];
  6048. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToCompany_or_" withString:freightBillToCompany];
  6049. NSString *freightBillToName = [self textAtColumn:25 statement:statement];
  6050. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToName_or_" withString:freightBillToName];
  6051. NSString *freightBillToAddr = [self textAtColumn:26 statement:statement];
  6052. moreInfo = [self replaceHtml:moreInfo String:@"FreightBillToAddress_or_" withString:freightBillToAddr];
  6053. /*****merchandise to******/
  6054. // MerchandiseBillToCompany_or_&nbsp
  6055. NSString *merchandiseBillToCompany = [self textAtColumn:27 statement:statement];
  6056. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToCompany_or_" withString:merchandiseBillToCompany];
  6057. NSString *merchandiseBillToName = [self textAtColumn:28 statement:statement];
  6058. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToName_or_" withString:merchandiseBillToName];
  6059. NSString *merchandiseBillToAddr = [self textAtColumn:29 statement:statement];
  6060. moreInfo = [self replaceHtml:moreInfo String:@"MerchandiseBillToAddress_or_" withString:merchandiseBillToAddr];
  6061. /*****return to******/
  6062. // ReturnToCompany_or_&nbsp
  6063. NSString *returnToCompany = [self textAtColumn:30 statement:statement];
  6064. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToCompany_or_" withString:returnToCompany];
  6065. NSString *returnToName = [self textAtColumn:31 statement:statement];
  6066. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToName_or_" withString:returnToName];
  6067. NSString *returnToAddr = [self textAtColumn:32 statement:statement];
  6068. moreInfo = [self replaceHtml:moreInfo String:@"ReturnToAddress_or_" withString:returnToAddr];
  6069. //
  6070. // DebugLog(@"more info : %@",moreInfo);
  6071. // handling fee
  6072. handlingFee = sqlite3_column_double(statement, 33);
  6073. ret[@"Handling Fee"] = [NSString stringWithFormat:@"$%.2f",handlingFee];
  6074. if (sqlite3_column_int(statement, 58)) {
  6075. ret[@"Handling Fee"] = @"Shipping To Be Quoted";
  6076. }
  6077. //
  6078. // customer info
  6079. customerID = [self textAtColumn:36 statement:statement];
  6080. // mode
  6081. ret[@"mode"] = appDelegate.mode;
  6082. // model_count
  6083. ret[@"model_count"] = @(0);
  6084. }
  6085. sqlite3_finalize(statement);
  6086. }
  6087. [iSalesDB close_db:db];
  6088. NSMutableDictionary *contactInfo = [[[self offline_contactinfo:[@{@"contactId":customerID} mutableCopy]] objectForKey:@"customerInfo"] mutableCopy];
  6089. // "customer_email" = "Shui Hu";
  6090. // "customer_fax" = "";
  6091. // "customer_first_name" = F;
  6092. // "customer_last_name" = L;
  6093. // "customer_name" = ",da He Xiang Dong Liu A";
  6094. // "customer_phone" = "Hey Xuan Feng";
  6095. contactInfo[@"customer_phone"] = customer_phone;
  6096. contactInfo[@"customer_fax"] = customer_fax;
  6097. contactInfo[@"customer_email"] = customer_email;
  6098. NSString *first_name = @"";
  6099. NSString *last_name = @"";
  6100. if ([customer_contact isEqualToString:@""]) {
  6101. } else if ([customer_contact containsString:@" "]) {
  6102. NSInteger first_space_index = [customer_contact rangeOfString:@" "].location;
  6103. first_name = [customer_contact substringToIndex:first_space_index];
  6104. last_name = [customer_contact substringFromIndex:first_space_index + 1];
  6105. }
  6106. contactInfo[@"customer_first_name"] = first_name;
  6107. contactInfo[@"customer_last_name"] = last_name;
  6108. ret[@"customerInfo"] = contactInfo;
  6109. // models
  6110. if (nssoid) {
  6111. __block double TotalCuft = 0;
  6112. __block double TotalWeight = 0;
  6113. __block int TotalCarton = 0;
  6114. __block double allItemPrice = 0;
  6115. 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];
  6116. sqlite3 *db1 = [iSalesDB get_db];
  6117. [iSalesDB jk_query:modelSql db:db1 close:YES completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6118. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6119. int product_id = sqlite3_column_int(stmt, 0);
  6120. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6121. int item_id = sqlite3_column_int(stmt, 7);
  6122. NSString* Price=nil;
  6123. if(str_price==nil)
  6124. {
  6125. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6126. if(price==nil)
  6127. Price=@"No Price.";
  6128. else
  6129. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6130. }
  6131. else
  6132. {
  6133. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6134. }
  6135. double discount = sqlite3_column_double(stmt, 2);
  6136. int item_count = sqlite3_column_int(stmt, 3);
  6137. char *line_note = (char*)sqlite3_column_text(stmt, 4);
  6138. NSString *nsline_note=nil;
  6139. if(line_note!=nil)
  6140. nsline_note= [[NSString alloc]initWithUTF8String:line_note];
  6141. // char *name = (char*)sqlite3_column_text(stmt, 5);
  6142. // NSString *nsname = nil;
  6143. // if(name!=nil)
  6144. // nsname= [[NSString alloc]initWithUTF8String:name];
  6145. NSString *nsname = [self textAtColumn:5 statement:stmt];
  6146. // char *description = (char*)sqlite3_column_text(stmt, 6);
  6147. // NSString *nsdescription=nil;
  6148. // if(description!=nil)
  6149. // nsdescription= [[NSString alloc]initWithUTF8String:description];
  6150. NSString *nsdescription = [self textAtColumn:6 statement:stmt];
  6151. // int stockUom = sqlite3_column_int(stmt, 8);
  6152. // int _id = sqlite3_column_int(stmt, 9);
  6153. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db1 compute_part:true];
  6154. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  6155. double weight=[bsubtotaljson[@"weight"] doubleValue];
  6156. int carton=[bsubtotaljson[@"carton"] intValue];
  6157. TotalCuft += cuft;
  6158. TotalWeight += weight;
  6159. TotalCarton += carton;
  6160. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6161. // itemjson[@"img"]= [self model_category_img:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] model_name:nil db:db1];
  6162. itemjson[@"img"] = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db1];
  6163. itemjson[@"Item Number"]=[NSString stringWithFormat:@"%@\n%@",nsname,nsdescription ];
  6164. itemjson[@"note"]=nsline_note;
  6165. itemjson[@"origin_price"] = Price;
  6166. // Price = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6167. itemjson[@"The unit price"] = [NSString stringWithFormat:@"%f",[Price doubleValue] * (1.0 - discount / 100.0)];
  6168. itemjson[@"order_item_status"] = @""; // 暂时不处理
  6169. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6170. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6171. itemjson[@"discount"] = [NSNumber numberWithDouble:discount];
  6172. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6173. if(itemjson[@"combine"] != nil)
  6174. {
  6175. // int citem=0;
  6176. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6177. for(int bc=0;bc<bcount;bc++)
  6178. {
  6179. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6180. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6181. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6182. subTotal += uprice * modulus * item_count;
  6183. }
  6184. }
  6185. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6186. // DebugLog(@"item unite price: %@ \n discount: %f item count: %d subTotal: %f",Price,discount,item_count,subTotal);
  6187. ret[[NSString stringWithFormat:@"item_%ld",(*count)++]]=itemjson;
  6188. ret[@"model_count"] = [NSNumber numberWithInteger:*count];
  6189. allItemPrice += subTotal;
  6190. }];
  6191. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:[NSString stringWithFormat:@"%.2f ft³",TotalCuft]];
  6192. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:[NSString stringWithFormat:@"%.2f lbs",TotalWeight]];
  6193. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:[NSString stringWithFormat:@"%d",TotalCarton]];
  6194. // payments/Credits
  6195. // payments_and_credist = sqlite3_column_double(statement, 34);
  6196. payments_and_credist = allItemPrice;
  6197. ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6198. // // total
  6199. // totalPrice = sqlite3_column_double(statement, 35);
  6200. // ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6201. } else {
  6202. orderinfo = [self replaceHtml:orderinfo String:@"TotalCuft_or_nbsp" withString:@""];
  6203. orderinfo = [self replaceHtml:orderinfo String:@"TotalWeight_or_nbsp" withString:@""];
  6204. orderinfo = [self replaceHtml:orderinfo String:@"TotalCarton_or_nbsp" withString:@""];
  6205. // payments/Credits
  6206. payments_and_credist = 0;
  6207. ret[@"Sub-Total"] = [NSString stringWithFormat:@"$%.2f",payments_and_credist];
  6208. }
  6209. // total
  6210. totalPrice = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6211. ret[@"Total"] = [NSString stringWithFormat:@"$%.2f",totalPrice];
  6212. ret[@"order_info"]= orderinfo;
  6213. ret[@"more_order_info"] = moreInfo;
  6214. return [RAUtils dict2data:ret];
  6215. }
  6216. #pragma mark order list
  6217. + (NSString *)orderStatus:(int)status erpStatus:(int)erpStatus{
  6218. NSString *nsstatus = @"";
  6219. switch (status) {
  6220. case 0:
  6221. {
  6222. nsstatus=@"Temp Order";
  6223. break;
  6224. }
  6225. case 1:
  6226. {
  6227. nsstatus=@"Saved Order";
  6228. break;
  6229. }
  6230. case 2: {
  6231. nsstatus = [self orderStatus:erpStatus erpStatus:-1];
  6232. break;
  6233. }
  6234. case 3:
  6235. case 15:
  6236. {
  6237. nsstatus=@"Cancelled";
  6238. break;
  6239. }
  6240. case 10:
  6241. {
  6242. nsstatus=@"Quote Submitted";
  6243. break;
  6244. }
  6245. case 11:
  6246. {
  6247. nsstatus=@"Sales Order Submitted";
  6248. break;
  6249. }
  6250. case 12:
  6251. {
  6252. nsstatus=@"Processing";
  6253. break;
  6254. }
  6255. case 13:
  6256. {
  6257. nsstatus=@"Shipped";
  6258. break;
  6259. }
  6260. case 14:
  6261. {
  6262. nsstatus=@"Closed";
  6263. break;
  6264. }
  6265. case -11:
  6266. {
  6267. nsstatus = @"Ready For Submit";
  6268. break;
  6269. }
  6270. default:
  6271. break;
  6272. }
  6273. return nsstatus;
  6274. }
  6275. + (double) orderTotalPrice:(NSString *)so_id db:(sqlite3 *)db1 close:(BOOL)close{
  6276. double total = 0;
  6277. __block double payments_and_credist = 0;
  6278. __block double allItemPrice = 0;
  6279. // sqlite3 *db1 = [iSalesDB get_db];
  6280. if (so_id) {
  6281. // 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];
  6282. NSString *modelSql = [NSString stringWithFormat:@"select product_id,price,discount,item_count,item_id,customer_cid from (select c.product_id,decrypt(c.str_price) as price,c.discount,c.item_count,c.item_id,o.customer_cid from offline_cart as c left join offline_order as o on c.so_no = o.so_id where c.so_no='%@');",so_id];
  6283. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6284. [iSalesDB jk_query:modelSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6285. NSMutableDictionary* itemjson = [[NSMutableDictionary alloc] init];
  6286. int product_id = sqlite3_column_int(stmt, 0);
  6287. char *str_price = (char*)sqlite3_column_text(stmt, 1);
  6288. int discount = sqlite3_column_int(stmt, 2);
  6289. int item_count = sqlite3_column_int(stmt, 3);
  6290. // int item_id = sqlite3_column_int(stmt, 7);
  6291. int item_id = sqlite3_column_int(stmt, 4);
  6292. char *customer_cid = (char *)sqlite3_column_text(stmt, 5);
  6293. NSString *customerID = [NSString stringWithUTF8String:customer_cid];
  6294. NSString* Price=nil;
  6295. if(str_price==nil)
  6296. {
  6297. NSNumber* price = [self get_model_default_price:customerID product_id:nil item_id:@(item_id) db:db1];
  6298. if(price==nil)
  6299. Price=@"No Price.";
  6300. else
  6301. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  6302. }
  6303. else
  6304. {
  6305. Price=[NSString stringWithFormat:@"%.2f",[[[NSString alloc]initWithUTF8String:str_price] floatValue]];
  6306. }
  6307. itemjson[@"combine"]=[self model_bundle:item_id db:db1 compute_part:false contactID:customerID];
  6308. itemjson[@"The unit price"]=Price;
  6309. itemjson[@"product_id"] = [NSString stringWithFormat:@"%d",product_id];
  6310. itemjson[@"QTY"] = [NSString stringWithFormat:@"%d",item_count];
  6311. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  6312. if(itemjson[@"combine"] != nil)
  6313. {
  6314. // int citem=0;
  6315. int bcount=[[itemjson[@"combine"] valueForKey:@"count"] intValue];
  6316. for(int bc=0;bc<bcount;bc++)
  6317. {
  6318. NSDictionary * bitem = [itemjson[@"combine"] objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  6319. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  6320. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  6321. subTotal += uprice * modulus * item_count;
  6322. }
  6323. }
  6324. itemjson[@"Subtotal"] = [NSString stringWithFormat:@"%.2f",subTotal];
  6325. allItemPrice += subTotal;
  6326. }];
  6327. payments_and_credist = allItemPrice;
  6328. } else {
  6329. // payments/Credits
  6330. payments_and_credist = 0;
  6331. }
  6332. // lift_gate handlingFee shippingFee
  6333. __block double lift_gate = 0;
  6334. __block double handlingFee = 0;
  6335. __block double shippingFee = 0;
  6336. NSString *orderSql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,handling_fee_value,shipping from offline_order where so_id = '%@';",so_id];
  6337. [iSalesDB jk_query:orderSql db:db1 close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6338. int have_lift_gate = sqlite3_column_int(stmt, 0);
  6339. if (have_lift_gate) {
  6340. lift_gate = sqlite3_column_double(stmt, 1);
  6341. }
  6342. handlingFee = sqlite3_column_double(stmt, 2);
  6343. shippingFee = sqlite3_column_double(stmt, 3);
  6344. }];
  6345. // total
  6346. total = payments_and_credist + lift_gate + shippingFee + handlingFee;
  6347. if (close) {
  6348. [iSalesDB close_db:db1];
  6349. }
  6350. return total;
  6351. }
  6352. +(NSData*) offline_orderlist :(NSMutableDictionary *) params
  6353. {
  6354. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  6355. int limit = [[params valueForKey:@"limit"] intValue];
  6356. int offset = [[params valueForKey:@"offset"] intValue];
  6357. NSString* keyword = [params valueForKey:@"keyWord"];
  6358. keyword = [self translateSingleQuote:keyword]; // 转义单引号
  6359. NSString *orderStatus = [params valueForKey:@"orderStatus"];
  6360. NSString* where=@"1 = 1";
  6361. if(keyword.length>0)
  6362. 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]];
  6363. if (orderStatus.length > 0) {
  6364. NSArray *order_status_array = [orderStatus componentsSeparatedByString:@","];
  6365. if (order_status_array.count == 1) {
  6366. int status_value = [[order_status_array firstObject] integerValue];
  6367. if (status_value <= 1 || status_value == 3) {
  6368. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.status = %d",status_value]];
  6369. } else {
  6370. where = [where stringByAppendingString:[NSString stringWithFormat:@" and o.erpOrderStatus = %d",status_value]];
  6371. }
  6372. } else if (order_status_array.count > 1) {
  6373. for (int i = 0; i < order_status_array.count;i++) {
  6374. NSString *status = order_status_array[i];
  6375. NSString *condition = @" or";
  6376. if (i == 0) {
  6377. condition = @" and (";
  6378. }
  6379. // where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %@",condition,status]];
  6380. int status_value = [status integerValue];
  6381. if (status_value <= 1 || status_value == 3) {
  6382. where = [where stringByAppendingString:[NSString stringWithFormat:@"%@ o.status = %d",condition,status_value]];
  6383. } else {
  6384. where = [where stringByAppendingString:[NSString stringWithFormat:@" %@ o.erpOrderStatus = %d",condition,status_value]];
  6385. }
  6386. }
  6387. where = [where stringByAppendingString:@" )"];
  6388. }
  6389. }
  6390. NSString* sqlQuery=[NSString stringWithFormat:@"SELECT o._id,o.so_id,o.status,o.sales_rep,o.create_by,decrypt(c.company_name),o.create_time,o.total_price,o.customer_contact,o.erpOrderStatus,o.order_id is null from offline_order o left join offline_contact c on o.customer_cid=c.contact_id where %@ order by o.create_time desc limit %d offset %d" ,where, limit, offset];
  6391. // DebugLog(@"order list sql: %@",sqlQuery);
  6392. sqlite3 *db = [iSalesDB get_db];
  6393. sqlite3_stmt * statement;
  6394. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  6395. if (sqlite3_prepare_v2(db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK)
  6396. {
  6397. int count=0;
  6398. while (sqlite3_step(statement) == SQLITE_ROW)
  6399. {
  6400. NSMutableDictionary* item=[[NSMutableDictionary alloc]init];
  6401. int order_id = sqlite3_column_double(statement, 0);
  6402. char *soid = (char*)sqlite3_column_text(statement, 1);
  6403. if(soid==nil)
  6404. soid= "";
  6405. NSString *nssoid= [[NSString alloc]initWithUTF8String:soid];
  6406. int status = sqlite3_column_double(statement, 2);
  6407. int erpStatus = sqlite3_column_double(statement, 9);
  6408. char *sales_rep = (char*)sqlite3_column_text(statement, 3);
  6409. if(sales_rep==nil)
  6410. sales_rep= "";
  6411. NSString *nssales_rep= [[NSString alloc]initWithUTF8String:sales_rep];
  6412. char *create_by = (char*)sqlite3_column_text(statement, 4);
  6413. if(create_by==nil)
  6414. create_by= "";
  6415. NSString *nscreate_by= [[NSString alloc]initWithUTF8String:create_by];
  6416. char *company_name = (char*)sqlite3_column_text(statement, 5);
  6417. if(company_name==nil)
  6418. company_name= "";
  6419. NSString *nscompany_name= [[NSString alloc]initWithUTF8String:company_name];
  6420. char *create_time = (char*)sqlite3_column_text(statement, 6);
  6421. if(create_time==nil)
  6422. create_time= "";
  6423. NSString *nscreate_time= [[NSString alloc]initWithUTF8String:create_time];
  6424. nscreate_time = [self changeDateTimeFormate:nscreate_time]; // 与在线统一
  6425. // double total_price = sqlite3_column_double(statement, 7);
  6426. double total_price = [self orderTotalPrice:[NSString stringWithUTF8String:soid] db:db close:NO];
  6427. NSString *customer_contact = [self textAtColumn:8 statement:statement];
  6428. NSString* nsstatus = [self orderStatus:status erpStatus:erpStatus];
  6429. int offline_edit = sqlite3_column_int(statement, 10);
  6430. // ": "JH",
  6431. // "": "$8307.00",
  6432. // "": "MOB1608050001",
  6433. // "": "ArpithaT",
  6434. // "": "1st Stage Property Transformations",
  6435. // "": "JANICE SUTTON",
  6436. // "": 2255,
  6437. // "": "08/02/2016 09:49:18",
  6438. // "": 1,
  6439. // "": "Saved Order"
  6440. // "": "1470384050483",
  6441. // "model_count": "6 / 28"
  6442. item[@"sales_rep"]= nssales_rep;
  6443. item[@"price"]= [NSString stringWithFormat:@"$%.2f",total_price];
  6444. item[@"so#"]= nssoid;
  6445. item[@"create_by"]= nscreate_by;
  6446. item[@"customer_name"]= nscompany_name;
  6447. item[@"customer_contact"] = customer_contact;
  6448. item[@"order_id"]= [NSString stringWithFormat:@"%d",order_id];
  6449. item[@"purchase_time"]= nscreate_time;
  6450. int statusCode = status;
  6451. if (statusCode == 2) {
  6452. statusCode = erpStatus;
  6453. } else if (statusCode == 3) {
  6454. statusCode = 15;
  6455. }
  6456. item[@"orderStatus"]= [NSString stringWithFormat:@"%d",statusCode];
  6457. item[@"order_status"]= nsstatus;
  6458. item[@"order_code"]= nssoid;//[NSString stringWithFormat:@"%d",order_id];
  6459. item[@"offline_edit"] = [NSNumber numberWithInt:offline_edit];
  6460. // item[@"model_count"]
  6461. ret[[NSString stringWithFormat:@"item_%d",count]]= item;
  6462. count++;
  6463. }
  6464. ret[@"count"]= [NSNumber numberWithInt:count];
  6465. //ret[@"total_count"]= [NSNumber numberWithInt:count];
  6466. // ret[@"wish_count"]= [NSNumber numberWithInt:count];
  6467. ret[@"result"]= [NSNumber numberWithInt:2];
  6468. ret[@"can_see_price"] = [NSNumber numberWithBool:YES];
  6469. ret[@"time_zone"] = @"PST";
  6470. sqlite3_finalize(statement);
  6471. }
  6472. 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];
  6473. ret[@"total_count"] = [NSNumber numberWithInteger:total_count];
  6474. [iSalesDB execSql:@"END TRANSACTION" db:db];
  6475. [iSalesDB close_db:db];
  6476. return [RAUtils dict2data:ret];
  6477. }
  6478. #pragma mark update gnotes
  6479. +(NSData*) offline_updategnotes :(NSMutableDictionary *) params
  6480. {
  6481. DebugLog(@"params: %@",params);
  6482. // comments = Meyoyoyoyoyoyoy;
  6483. // orderCode = MOB1608110001;
  6484. // password = 123456;
  6485. // user = EvanK;
  6486. NSString *sql = [NSString stringWithFormat:@"update offline_order set general_notes = '%@' where so_id = '%@';",[self valueInParams:params key:@"comments" translateSingleQuote:YES],[self valueInParams:params key:@"orderCode" translateSingleQuote:YES]];
  6487. int ret = [iSalesDB execSql:sql];
  6488. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6489. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6490. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6491. // [dic setValue:@"160409" forKey:@"min_ver"];
  6492. return [RAUtils dict2data:dic];
  6493. }
  6494. #pragma mark move to wishlist
  6495. +(NSData*) offline_movecart2wish :(NSMutableDictionary *) params
  6496. {
  6497. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6498. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6499. _id = [NSString stringWithFormat:@"(%@)",_id];
  6500. NSString *sql = [NSString stringWithFormat:@"select product_id,item_count,item_id from offline_cart where so_no = '%@' and _id in %@;",orderCode,_id];
  6501. sqlite3 *db = [iSalesDB get_db];
  6502. __block NSString *product_id = @"";
  6503. __block NSString *item_count_str = @"";
  6504. // __block NSString *item_id = nil;
  6505. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6506. // product_id = [self textAtColumn:0 statement:stmt];
  6507. int item_count = sqlite3_column_int(stmt, 1);
  6508. // item_id = [self textAtColumn:2 statement:stmt];
  6509. NSString *p_id = [self textAtColumn:0 statement:stmt];
  6510. NSString *count_str = [NSString stringWithFormat:@"%d",item_count];
  6511. if (p_id.length) {
  6512. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",p_id]];
  6513. item_count_str = [item_count_str stringByAppendingString:[NSString stringWithFormat:@",%@",count_str]];
  6514. }
  6515. }];
  6516. [iSalesDB close_db:db];
  6517. // 去除第一个,
  6518. if (product_id.length > 1) {
  6519. product_id = [product_id substringFromIndex:1];
  6520. }
  6521. if (item_count_str.length > 1) {
  6522. item_count_str = [item_count_str substringFromIndex:1];
  6523. }
  6524. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6525. dic = [self offline_add2wishlist:@{@"product_id" : product_id,@"item_count" : item_count_str}.mutableCopy].mutableCopy;
  6526. // NSString *insertIntoWishlistSql = [NSString stringWithFormat:@"insert into wishlist (product_id,item_id,qty) values (%@,%@,%d);",product_id,item_id,item_count];
  6527. // int ret = [iSalesDB execSql:insertIntoWishlistSql db:db];
  6528. sqlite3 *db1 = [iSalesDB get_db];
  6529. int wish_count = [iSalesDB get_recordcount:db1 table:@"wishlist" where:@"is_delete is NULL or is_delete != 1"];
  6530. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6531. [dic setValue:[NSNumber numberWithInteger:wish_count] forKey:@"wish_count"];
  6532. // 删除
  6533. NSString *deleteSql = [NSString stringWithFormat:@"delete from offline_cart where _id in %@;",_id];
  6534. int ret = [iSalesDB execSql:deleteSql db:db1];
  6535. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6536. [iSalesDB close_db:db1];
  6537. return [RAUtils dict2data:dic];
  6538. }
  6539. #pragma mark cart delete
  6540. +(NSData*) offline_cartremove :(NSMutableDictionary *) params
  6541. {
  6542. // cartItemId = 548;
  6543. // orderCode = MOB1608110001;
  6544. NSString *_id = [self valueInParams:params key:@"cartItemId"];
  6545. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6546. _id = [NSString stringWithFormat:@"(%@)",_id];
  6547. NSString *sql = [NSString stringWithFormat:@"delete from offline_cart where so_no = '%@' and _id in %@ ;",orderCode,_id];
  6548. int ret = [iSalesDB execSql:sql];
  6549. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6550. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6551. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6552. // [dic setValue:@"160409" forKey:@"min_ver"];
  6553. return [RAUtils dict2data:dic];
  6554. }
  6555. #pragma mark set price
  6556. +(NSData*) offline_cartsetprice :(NSMutableDictionary *) params
  6557. {
  6558. DebugLog(@"cart set price params: %@",params);
  6559. // "cartitem_id" = 1;
  6560. // discount = "0.000000";
  6561. // "item_note" = "";
  6562. // price = "269.000000";
  6563. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6564. NSString *notes = [self valueInParams:params key:@"item_note"];
  6565. NSString *discount = [self valueInParams:params key:@"discount"];
  6566. NSString *price = [self valueInParams:params key:@"price"];
  6567. // bool badd_price_changed=false;
  6568. // sqlite3* db=[iSalesDB get_db];
  6569. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6570. // NSNumber* default_price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:_id db:db];
  6571. // NSRange range;
  6572. // range = [notes rangeOfString:@"PRICE MODIFIED"];
  6573. //
  6574. // if((default_price.doubleValue!=price.doubleValue || discount.intValue!=0 ) && range.location == NSNotFound)
  6575. // badd_price_changed=true;
  6576. // [iSalesDB close_db:db];
  6577. //
  6578. // if(badd_price_changed)
  6579. // {
  6580. // notes=[NSString stringWithFormat:@"%@\n%@",@"PRICE MODIFIED",notes];
  6581. // }
  6582. //
  6583. price = [self translateSingleQuote:[AESCrypt fastencrypt:price]];
  6584. NSString *sql = [NSString stringWithFormat:@"update offline_cart set str_price = '%@',discount = %f, line_note='%@' where _id = %@",price,discount.doubleValue,notes,_id];
  6585. int ret = [iSalesDB execSql:sql];
  6586. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6587. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6588. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6589. // [dic setValue:@"160409" forKey:@"min_ver"];
  6590. return [RAUtils dict2data:dic];
  6591. }
  6592. #pragma mark set line notes
  6593. +(NSData*) offline_cartsetlnotes :(NSMutableDictionary *) params
  6594. {
  6595. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6596. NSString *notes = [self valueInParams:params key:@"notes"];
  6597. notes = [self translateSingleQuote:notes];
  6598. NSString *sql = [NSString stringWithFormat:@"update offline_cart set line_note = '%@' where _id = %@",notes,_id];
  6599. int ret = [iSalesDB execSql:sql];
  6600. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6601. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6602. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6603. // [dic setValue:@"160409" forKey:@"min_ver"];
  6604. return [RAUtils dict2data:dic];
  6605. }
  6606. #pragma mark set qty
  6607. +(NSData*) offline_cartsetqty :(NSMutableDictionary *) params
  6608. {
  6609. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6610. NSString *_id = [self valueInParams:params key:@"cartitem_id"];
  6611. int item_count = [params[@"inputInt"] intValue];
  6612. // 购买检查数量大于库存
  6613. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  6614. if (!appDelegate.can_create_backorder) {
  6615. NSString *checkSql = [NSString stringWithFormat:@"select p.availability from product as p left join offline_cart as c on c.item_id = p.item_id where c._id = %@;",_id];
  6616. __block BOOL out_of_stock = NO;
  6617. [iSalesDB jk_query:checkSql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6618. int availability = sqlite3_column_int(stmt, 0);
  6619. if (availability < item_count) {
  6620. out_of_stock = YES;
  6621. }
  6622. }];
  6623. if (out_of_stock) { // 缺货
  6624. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"result"];
  6625. [dic setObject:@"Item is out of stock" forKey:@"err_msg"];
  6626. return [RAUtils dict2data:dic];
  6627. }
  6628. }
  6629. NSString *sql = [NSString stringWithFormat:@"update offline_cart set item_count = %d where _id = %@;",item_count,_id];
  6630. int ret = [iSalesDB execSql:sql];
  6631. __block int item_id = 0;
  6632. [iSalesDB jk_query:[NSString stringWithFormat:@"select item_id from offline_cart where _id = %@;",_id] completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6633. item_id = sqlite3_column_int(stmt, 0);
  6634. }];
  6635. sqlite3 *db = [iSalesDB get_db];
  6636. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  6637. [iSalesDB close_db:db];
  6638. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  6639. [dic setValue:@"Regular Mode" forKey:@"mode"];
  6640. // [dic setValue:@"160409" forKey:@"min_ver"];
  6641. [dic setValue:bsubtotaljson[@"cuft"] forKey:@"cuft"];
  6642. [dic setValue:bsubtotaljson[@"weight"] forKey:@"weight"];
  6643. [dic setValue:bsubtotaljson[@"carton"] forKey:@"carton"];
  6644. return [RAUtils dict2data:dic];
  6645. }
  6646. #pragma mark place order
  6647. + (NSDictionary *)infoName:(NSString *)name aname:(NSString *)aname control:(NSString *)control keyboard:(NSString *)keyboard value:(id)value {
  6648. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6649. if (aname) {
  6650. [dic setValue:aname forKey:@"aname"];
  6651. }
  6652. if (control) {
  6653. [dic setValue:control forKey:@"control"];
  6654. }
  6655. if (keyboard) {
  6656. [dic setValue:keyboard forKey:@"keyboard"];
  6657. }
  6658. if (name) {
  6659. [dic setValue:name forKey:@"name"];
  6660. }
  6661. if (value) {
  6662. [dic setValue:value forKey:@"value"];
  6663. }
  6664. return dic;
  6665. }
  6666. + (NSDictionary *)customerDic:(NSDictionary *) params db:(sqlite3 *)db {
  6667. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6668. orderCode = [self translateSingleQuote:orderCode];
  6669. __block NSMutableDictionary *customer_dic = [NSMutableDictionary dictionary];
  6670. NSString *customerSql = [NSString stringWithFormat:@"select c.contact_id,c.img_0,c.img_1,c.img_2,decrypt(c.company_name),decrypt(c.addr_1),c.addr_2,c.addr_3,c.addr_4,c.first_name,c.last_name,o.customer_email,o.customer_phone,o.customer_fax,c.zipcode,c.city,c.state,c.country,o.customer_contact from offline_contact as c join offline_order as o on c.contact_id = o.customer_cid where o.so_id = '%@';",orderCode];
  6671. [iSalesDB jk_query:customerSql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6672. NSString *contact_id = [self textAtColumn:0 statement:stmt];
  6673. NSString *img0 = [self textAtColumn:1 statement:stmt];
  6674. NSString *img1 = [self textAtColumn:2 statement:stmt];
  6675. NSString *img2 = [self textAtColumn:3 statement:stmt];
  6676. NSString *company_name = [self textAtColumn:4 statement:stmt];
  6677. NSString *addr1 = [self textAtColumn:5 statement:stmt];
  6678. NSString *addr2 = [self textAtColumn:6 statement:stmt];
  6679. NSString *addr3 = [self textAtColumn:7 statement:stmt];
  6680. NSString *addr4 = [self textAtColumn:8 statement:stmt];
  6681. // NSString *first_name = [self textAtColumn:9 statement:stmt];
  6682. // NSString *last_name = [self textAtColumn:10 statement:stmt];
  6683. NSString *email = [self textAtColumn:11 statement:stmt];
  6684. NSString *phone = [self textAtColumn:12 statement:stmt];
  6685. NSString *fax = [self textAtColumn:13 statement:stmt];
  6686. NSString *zipcode = [self textAtColumn:14 statement:stmt];
  6687. NSString *city = [self textAtColumn:15 statement:stmt];
  6688. NSString *state = [self textAtColumn:16 statement:stmt];
  6689. NSString *country = [self textAtColumn:17 statement:stmt];
  6690. NSString *name = [self textAtColumn:18 statement:stmt];
  6691. // contact id
  6692. NSMutableDictionary *contact_id_dic = [NSMutableDictionary dictionary];
  6693. [contact_id_dic setValue:@"Contact ID" forKey:@"aname"];
  6694. [contact_id_dic setValue:@"text" forKey:@"control"];
  6695. [contact_id_dic setValue:@"text" forKey:@"keyboard"];
  6696. [contact_id_dic setValue:@"customer_cid" forKey:@"name"];
  6697. [contact_id_dic setValue:@"true" forKey:@"required"];
  6698. [contact_id_dic setValue:contact_id forKey:@"value"];
  6699. [customer_dic setValue:contact_id_dic forKey:@"item_0"];
  6700. // business card
  6701. NSMutableDictionary *business_card_dic = [NSMutableDictionary dictionary];
  6702. [business_card_dic setValue:@"Business Card" forKey:@"aname"];
  6703. [business_card_dic setValue:@"img" forKey:@"control"];
  6704. [business_card_dic setValue:@"1" forKey:@"disable"];
  6705. [business_card_dic setValue:img0 forKey:@"img_url_0"];
  6706. [business_card_dic setValue:img1 forKey:@"img_url_1"];
  6707. [business_card_dic setValue:img2 forKey:@"img_url_2"];
  6708. [business_card_dic setValue:img0 forKey:@"img_url_aname_0"];
  6709. [business_card_dic setValue:img1 forKey:@"img_url_aname_1"];
  6710. [business_card_dic setValue:img2 forKey:@"img_url_aname_2"];
  6711. [business_card_dic setValue:@"business_card" forKey:@"name"];
  6712. [customer_dic setValue:business_card_dic forKey:@"item_1"];
  6713. // fax
  6714. NSDictionary *fax_dic = [self infoName:@"customer_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6715. [customer_dic setValue:fax_dic forKey:@"item_10"];
  6716. // zipcode
  6717. NSDictionary *zipcode_dic = [self infoName:@"customer_zipcode" aname:@"Zipcode" control:@"text" keyboard:nil value:zipcode];
  6718. [customer_dic setValue:zipcode_dic forKey:@"item_11"];
  6719. // city
  6720. NSDictionary *city_dic = [self infoName:@"customer_city" aname:@"City" control:@"text" keyboard:nil value:city];
  6721. [customer_dic setValue:city_dic forKey:@"item_12"];
  6722. // state
  6723. NSDictionary *state_dic = [self infoName:@"customer_state" aname:@"State" control:@"text" keyboard:nil value:state];
  6724. [customer_dic setValue:state_dic forKey:@"item_13"];
  6725. // country
  6726. NSDictionary *country_dic = [self infoName:@"customer_country" aname:@"Country" control:@"text" keyboard:nil value:country];
  6727. [customer_dic setValue:country_dic forKey:@"item_14"];
  6728. // company name
  6729. NSDictionary *company_dic = [self infoName:@"customer_name" aname:@"Company Name" control:@"text" keyboard:nil value:company_name];
  6730. [customer_dic setValue:company_dic forKey:@"item_2"];
  6731. // addr_1
  6732. NSDictionary *addr1_dic = [self infoName:@"customer_address1" aname:@"Address_1" control:@"text" keyboard:nil value:addr1];
  6733. [customer_dic setValue:addr1_dic forKey:@"item_3"];
  6734. // addr_2
  6735. NSDictionary *addr2_dic = [self infoName:@"customer_address2" aname:@"Address_2" control:@"text" keyboard:nil value:addr2];
  6736. [customer_dic setValue:addr2_dic forKey:@"item_4"];
  6737. // addr_3
  6738. NSDictionary *addr3_dic = [self infoName:@"customer_address3" aname:@"Address_3" control:@"text" keyboard:nil value:addr3];
  6739. [customer_dic setValue:addr3_dic forKey:@"item_5"];
  6740. // addr_4
  6741. NSDictionary *addr4_dic = [self infoName:@"customer_address4" aname:@"Address_4" control:@"text" keyboard:nil value:addr4];
  6742. [customer_dic setValue:addr4_dic forKey:@"item_6"];
  6743. // Contact
  6744. NSDictionary *contact_dic = [self infoName:@"customer_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:name];
  6745. [customer_dic setValue:contact_dic forKey:@"item_7"];
  6746. // email
  6747. NSDictionary *email_dic = [self infoName:@"customer_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6748. [customer_dic setValue:email_dic forKey:@"item_8"];
  6749. // phone
  6750. NSDictionary *phone_dic = [self infoName:@"customer_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6751. [customer_dic setValue:phone_dic forKey:@"item_9"];
  6752. // title
  6753. [customer_dic setValue:@"Customer" forKey:@"title"];
  6754. // count
  6755. [customer_dic setValue:[NSNumber numberWithInteger:15] forKey:@"count"];
  6756. }];
  6757. // setting
  6758. NSDictionary *setting = params[@"setting"];
  6759. NSNumber *hide = setting[@"CustomerHide"];
  6760. [customer_dic setValue:hide forKey:@"hide"];
  6761. return customer_dic;
  6762. }
  6763. + (NSDictionary *)shipToDic:(NSDictionary *)params db:(sqlite3 *)db {
  6764. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6765. // setting
  6766. NSDictionary *setting = params[@"setting"];
  6767. NSNumber *hide = setting[@"ShipToHide"];
  6768. [dic setValue:hide forKey:@"hide"];
  6769. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6770. orderCode = [self translateSingleQuote:orderCode];
  6771. 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];
  6772. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6773. NSString *cid = [self textAtColumn:0 statement:stmt];
  6774. NSString *name = [self textAtColumn:1 statement:stmt];
  6775. NSString *ext = [self textAtColumn:2 statement:stmt];
  6776. NSString *contact = [self textAtColumn:3 statement:stmt];
  6777. NSString *email = [self textAtColumn:4 statement:stmt];
  6778. NSString *fax = [self textAtColumn:5 statement:stmt];
  6779. NSString *phone = [self textAtColumn:6 statement:stmt];
  6780. // count
  6781. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6782. // title
  6783. [dic setValue:@"Ship To" forKey:@"title"];
  6784. // choose
  6785. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6786. [choose_dic setValue:@"choose" forKey:@"aname"];
  6787. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6788. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  6789. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6790. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6791. @"receive_contact" : @"customer_contact",
  6792. @"receive_email" : @"customer_email",
  6793. @"receive_ext" : @"customer_contact_ext",
  6794. @"receive_fax" : @"customer_fax",
  6795. @"receive_name" : @"customer_name",
  6796. @"receive_phone" : @"customer_phone"},
  6797. @"refresh" : [NSNumber numberWithInteger:1],
  6798. @"type" : @"pull"};
  6799. [choose_dic setValue:same_as_customer_dic forKey:@"item_0"];
  6800. NSDictionary *create_new_ship_to_dic = @{@"aname" : @"Create new ship to",
  6801. @"name" : @"Add new address",
  6802. @"refresh" : [NSNumber numberWithInteger:1],
  6803. @"value" : @"new_addr"
  6804. };
  6805. [choose_dic setValue:create_new_ship_to_dic forKey:@"item_1"];
  6806. NSDictionary *select_ship_to_dic = @{@"aname" : @"Select ship to",
  6807. @"key_map" : @{@"receive_cid" : @"customer_cid",
  6808. @"receive_contact" : @"customer_contact",
  6809. @"receive_email" : @"customer_email",
  6810. @"receive_ext" : @"customer_contact_ext",
  6811. @"receive_fax" : @"customer_fax",
  6812. @"receive_name" : @"customer_name",
  6813. @"receive_phone" : @"customer_phone"},
  6814. @"name" : @"select_ship_to",
  6815. @"refresh" : [NSNumber numberWithInteger:1],
  6816. @"value" : @"Sales_Order_Ship_To"};
  6817. [choose_dic setValue:select_ship_to_dic forKey:@"item_2"];
  6818. [dic setValue:choose_dic forKey:@"item_0"];
  6819. // contact id
  6820. NSMutableDictionary *contact_id_dic = [[self infoName:@"receive_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6821. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6822. [contact_id_dic setValue:@"true" forKey:@"required"];
  6823. [dic setValue:contact_id_dic forKey:@"item_1"];
  6824. // company name
  6825. NSDictionary *company_name_dic = [self infoName:@"receive_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6826. [dic setValue:company_name_dic forKey:@"item_2"];
  6827. // address
  6828. NSMutableDictionary *ext_dic = [self infoName:@"receive_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  6829. NSNumber *required = setting[@"ShippingToAddressRequire"];
  6830. if ([required integerValue]) {
  6831. [ext_dic setValue:@"true" forKey:@"required"];
  6832. }
  6833. [dic setValue:ext_dic forKey:@"item_3"];
  6834. // contact
  6835. NSDictionary *contact_dic = [self infoName:@"receive_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6836. [dic setValue:contact_dic forKey:@"item_4"];
  6837. // phone
  6838. NSDictionary *phone_dic = [self infoName:@"receive_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6839. [dic setValue:phone_dic forKey:@"item_5"];
  6840. // fax
  6841. NSDictionary *fax_dic = [self infoName:@"receive_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6842. [dic setValue:fax_dic forKey:@"item_6"];
  6843. // email
  6844. NSDictionary *email_dic = [self infoName:@"receive_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6845. [dic setValue:email_dic forKey:@"item_7"];
  6846. }];
  6847. return dic;
  6848. }
  6849. + (NSDictionary *)shipFromDic:(NSDictionary *)params db:(sqlite3 *)db {
  6850. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6851. // setting
  6852. NSDictionary *setting = params[@"setting"];
  6853. NSNumber *hide = setting[@"ShipFromHide"];
  6854. [dic setValue:hide forKey:@"hide"];
  6855. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6856. orderCode = [self translateSingleQuote:orderCode];
  6857. 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];
  6858. NSString *ship_from_default_sql = @"select contact_id,decrypt(company_name),decrypt(addr_1),first_name || ' ' || last_name,email,fax,phone from offline_contact where contact_id = 'NPD';";
  6859. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6860. __block NSString *cid = [self textAtColumn:0 statement:stmt];
  6861. __block NSString *name = [self textAtColumn:1 statement:stmt];
  6862. __block NSString *ext = [self textAtColumn:2 statement:stmt];
  6863. __block NSString *contact = [self textAtColumn:3 statement:stmt];
  6864. __block NSString *email = [self textAtColumn:4 statement:stmt];
  6865. __block NSString *fax = [self textAtColumn:5 statement:stmt];
  6866. __block NSString *phone = [self textAtColumn:6 statement:stmt];
  6867. if (!cid.length) {
  6868. [iSalesDB jk_query:ship_from_default_sql db:db close:NO completion:^(sqlite3_stmt *statment, NSMutableDictionary *container, long *count) {
  6869. cid = [self textAtColumn:0 statement:statment];
  6870. name = [self textAtColumn:1 statement:statment];
  6871. ext = [self textAtColumn:2 statement:statment];
  6872. contact = [self textAtColumn:3 statement:statment];
  6873. email = [self textAtColumn:4 statement:statment];
  6874. fax = [self textAtColumn:5 statement:statment];
  6875. phone = [self textAtColumn:6 statement:statment];
  6876. }];
  6877. }
  6878. // count
  6879. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6880. // title
  6881. [dic setValue:@"Ship From" forKey:@"title"];
  6882. // hide
  6883. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6884. // choose
  6885. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6886. [choose_dic setValue:@"choose" forKey:@"aname"];
  6887. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6888. [choose_dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  6889. NSDictionary *select_ship_from_dic = @{@"aname" : @"Select ship from",
  6890. @"key_map" : @{@"sender_cid" : @"customer_cid",
  6891. @"sender_contact" : @"customer_contact",
  6892. @"sender_email" : @"customer_email",
  6893. @"sender_ext" : @"customer_contact_ext",
  6894. @"sender_fax" : @"customer_fax",
  6895. @"sender_name" : @"customer_name",
  6896. @"sender_phone" : @"customer_phone"},
  6897. @"name" : @"select_cid",
  6898. @"refresh" : [NSNumber numberWithInteger:0],
  6899. @"value" : @"Sales_Order_Ship_From"};
  6900. [choose_dic setValue:select_ship_from_dic forKey:@"item_0"];
  6901. [dic setValue:choose_dic forKey:@"item_0"];
  6902. // contact id
  6903. NSDictionary *contact_id_dic = [[self infoName:@"sender_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  6904. [contact_id_dic setValue:@"1" forKey:@"disable"];
  6905. [contact_id_dic setValue:@"true" forKey:@"required"];
  6906. [dic setValue:contact_id_dic forKey:@"item_1"];
  6907. // company name
  6908. NSDictionary *company_name_dic = [self infoName:@"sender_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  6909. [dic setValue:company_name_dic forKey:@"item_2"];
  6910. // address
  6911. NSDictionary *ext_dic = [self infoName:@"sender_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  6912. [dic setValue:ext_dic forKey:@"item_3"];
  6913. // contact
  6914. NSDictionary *contact_dic = [self infoName:@"sender_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  6915. [dic setValue:contact_dic forKey:@"item_4"];
  6916. // phone
  6917. NSDictionary *phone_dic = [self infoName:@"sender_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  6918. [dic setValue:phone_dic forKey:@"item_5"];
  6919. // fax
  6920. NSDictionary *fax_dic = [self infoName:@"sender_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  6921. [dic setValue:fax_dic forKey:@"item_6"];
  6922. // email
  6923. NSDictionary *email_dic = [self infoName:@"sender_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  6924. [dic setValue:email_dic forKey:@"item_7"];
  6925. }];
  6926. return dic;
  6927. }
  6928. + (NSDictionary *)freightBillToDic:(NSDictionary *) params db:(sqlite3 *)db {
  6929. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  6930. // setting
  6931. NSDictionary *setting = params[@"setting"];
  6932. NSNumber *hide = setting[@"FreightBillToHide"];
  6933. [dic setValue:hide forKey:@"hide"];
  6934. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  6935. orderCode = [self translateSingleQuote:orderCode];
  6936. 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];
  6937. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  6938. NSString *cid = [self textAtColumn:0 statement:stmt];
  6939. NSString *name = [self textAtColumn:1 statement:stmt];
  6940. NSString *ext = [self textAtColumn:2 statement:stmt];
  6941. NSString *contact = [self textAtColumn:3 statement:stmt];
  6942. NSString *email = [self textAtColumn:4 statement:stmt];
  6943. NSString *fax = [self textAtColumn:5 statement:stmt];
  6944. NSString *phone = [self textAtColumn:6 statement:stmt];
  6945. // count
  6946. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  6947. // title
  6948. [dic setValue:@"Freight Bill To" forKey:@"title"];
  6949. // hide
  6950. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  6951. // choose
  6952. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  6953. [choose_dic setValue:@"choose" forKey:@"aname"];
  6954. [choose_dic setValue:@"multi_action" forKey:@"control"];
  6955. [choose_dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  6956. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  6957. @"key_map" : @{@"shipping_billto_cid" : @"receive_cid",
  6958. @"shipping_billto_contact" : @"receive_contact",
  6959. @"shipping_billto_email" : @"receive_email",
  6960. @"shipping_billto_ext" : @"receive_ext",
  6961. @"shipping_billto_fax" : @"receive_fax",
  6962. @"shipping_billto_name" : @"receive_name",
  6963. @"shipping_billto_phone" : @"receive_phone"},
  6964. @"type" : @"pull"};
  6965. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  6966. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  6967. @"type" : @"pull",
  6968. @"key_map" : @{@"shipping_billto_cid" : @"customer_cid",
  6969. @"shipping_billto_contact" : @"customer_contact",
  6970. @"shipping_billto_email" : @"customer_email",
  6971. @"shipping_billto_ext" : @"customer_contact_ext",
  6972. @"shipping_billto_fax" : @"customer_fax",
  6973. @"shipping_billto_name" : @"customer_name",
  6974. @"shipping_billto_phone" : @"customer_phone"}
  6975. };
  6976. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  6977. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  6978. @"key_map" : @{@"shipping_billto_cid" : @"sender_cid",
  6979. @"shipping_billto_contact" : @"sender_contact",
  6980. @"shipping_billto_email" : @"sender_email",
  6981. @"shipping_billto_ext" : @"sender_ext",
  6982. @"shipping_billto_fax" : @"sender_fax",
  6983. @"shipping_billto_name" : @"sender_name",
  6984. @"shipping_billto_phone" : @"sender_phone"},
  6985. @"type" : @"pull"
  6986. };
  6987. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_2"];
  6988. NSDictionary *select_freight_bill_to_dic = @{
  6989. @"aname" : @"Select freight bill to",
  6990. @"name" : @"select_cid",
  6991. @"refresh" : [NSNumber numberWithInteger:0],
  6992. @"value" : @"Sales_Order_Freight_Bill_To",
  6993. @"key_map" : @{
  6994. @"shipping_billto_cid" : @"customer_cid",
  6995. @"shipping_billto_contact" : @"customer_contact",
  6996. @"shipping_billto_email" : @"customer_email",
  6997. @"shipping_billto_ext" : @"customer_contact_ext",
  6998. @"shipping_billto_fax" : @"customer_fax",
  6999. @"shipping_billto_name" : @"customer_name",
  7000. @"shipping_billto_phone" : @"customer_phone"
  7001. }
  7002. };
  7003. [choose_dic setValue:select_freight_bill_to_dic forKey:@"item_3"];
  7004. [dic setValue:choose_dic forKey:@"item_0"];
  7005. // contact id
  7006. NSDictionary *contact_id_dic = [[self infoName:@"shipping_billto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7007. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7008. [contact_id_dic setValue:@"true" forKey:@"required"];
  7009. [dic setValue:contact_id_dic forKey:@"item_1"];
  7010. // company name
  7011. NSDictionary *company_name_dic = [self infoName:@"shipping_billto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7012. [dic setValue:company_name_dic forKey:@"item_2"];
  7013. // address
  7014. NSMutableDictionary *ext_dic = [self infoName:@"shipping_billto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7015. NSNumber *ext_required = setting[@"FreightBillToAddressRequire"];
  7016. if ([ext_required integerValue]) {
  7017. [ext_dic setValue:@"true" forKey:@"required"];
  7018. }
  7019. [dic setValue:ext_dic forKey:@"item_3"];
  7020. // contact
  7021. NSDictionary *contact_dic = [self infoName:@"shipping_billto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7022. [dic setValue:contact_dic forKey:@"item_4"];
  7023. // phone
  7024. NSDictionary *phone_dic = [self infoName:@"shipping_billto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7025. [dic setValue:phone_dic forKey:@"item_5"];
  7026. // fax
  7027. NSDictionary *fax_dic = [self infoName:@"shipping_billto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7028. [dic setValue:fax_dic forKey:@"item_6"];
  7029. // email
  7030. NSDictionary *email_dic = [self infoName:@"shipping_billto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7031. [dic setValue:email_dic forKey:@"item_7"];
  7032. }];
  7033. return dic;
  7034. }
  7035. + (NSDictionary *)merchandiseBillToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7036. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7037. // setting
  7038. NSDictionary *setting = params[@"setting"];
  7039. NSNumber *hide = setting[@"MerchandiseBillToHide"];
  7040. [dic setValue:hide forKey:@"hide"];
  7041. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7042. orderCode = [self translateSingleQuote:orderCode];
  7043. 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];
  7044. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7045. NSString *cid = [self textAtColumn:0 statement:stmt];
  7046. NSString *name = [self textAtColumn:1 statement:stmt];
  7047. NSString *ext = [self textAtColumn:2 statement:stmt];
  7048. NSString *contact = [self textAtColumn:3 statement:stmt];
  7049. NSString *email = [self textAtColumn:4 statement:stmt];
  7050. NSString *fax = [self textAtColumn:5 statement:stmt];
  7051. NSString *phone = [self textAtColumn:6 statement:stmt];
  7052. // count
  7053. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7054. // title
  7055. [dic setValue:@"Merchandise Bill To" forKey:@"title"];
  7056. // hide
  7057. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7058. // choose
  7059. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7060. [choose_dic setValue:@"choose" forKey:@"aname"];
  7061. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7062. [choose_dic setValue:[NSNumber numberWithInteger:3] forKey:@"count"];
  7063. NSDictionary *same_as_ship_to_dic = @{@"aname" : @"Same as ship to",
  7064. @"key_map" : @{@"billing_cid" : @"receive_cid",
  7065. @"billing_contact" : @"receive_contact",
  7066. @"billing_email" : @"receive_email",
  7067. @"billing_ext" : @"receive_ext",
  7068. @"billing_fax" : @"receive_fax",
  7069. @"billing_name" : @"receive_name",
  7070. @"billing_phone" : @"receive_phone"},
  7071. @"type" : @"pull"};
  7072. [choose_dic setValue:same_as_ship_to_dic forKey:@"item_0"];
  7073. NSDictionary *same_as_customer_dic = @{@"aname" : @"Same as customer",
  7074. @"type" : @"pull",
  7075. @"key_map" : @{@"billing_cid" : @"customer_cid",
  7076. @"billing_contact" : @"customer_contact",
  7077. @"billing_email" : @"customer_email",
  7078. @"billing_ext" : @"customer_contact_ext",
  7079. @"billing_fax" : @"customer_fax",
  7080. @"billing_name" : @"customer_name",
  7081. @"billing_phone" : @"customer_phone"}
  7082. };
  7083. [choose_dic setValue:same_as_customer_dic forKey:@"item_1"];
  7084. NSDictionary *select_bill_to_dic = @{
  7085. @"aname" : @"Select bill to",
  7086. @"name" : @"select_cid",
  7087. @"refresh" : [NSNumber numberWithInteger:0],
  7088. @"value" : @"Sales_Order_Merchandise_Bill_To",
  7089. @"key_map" : @{
  7090. @"billing_cid" : @"customer_cid",
  7091. @"billing_contact" : @"customer_contact",
  7092. @"billing_email" : @"customer_email",
  7093. @"billing_ext" : @"customer_contact_ext",
  7094. @"billing_fax" : @"customer_fax",
  7095. @"billing_name" : @"customer_name",
  7096. @"billing_phone" : @"customer_phone"
  7097. }
  7098. };
  7099. [choose_dic setValue:select_bill_to_dic forKey:@"item_2"];
  7100. [dic setValue:choose_dic forKey:@"item_0"];
  7101. // contact id
  7102. NSDictionary *contact_id_dic = [[self infoName:@"billing_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7103. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7104. [contact_id_dic setValue:@"true" forKey:@"required"];
  7105. [dic setValue:contact_id_dic forKey:@"item_1"];
  7106. // company name
  7107. NSDictionary *company_name_dic = [self infoName:@"billing_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7108. [dic setValue:company_name_dic forKey:@"item_2"];
  7109. // address
  7110. NSMutableDictionary *ext_dic = [self infoName:@"billing_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext].mutableCopy;
  7111. NSNumber *ext_required = setting[@"MerchandiseBillToAddressRequire"];
  7112. if ([ext_required integerValue]) {
  7113. [ext_dic setValue:@"true" forKey:@"required"];
  7114. }
  7115. [dic setValue:ext_dic forKey:@"item_3"];
  7116. // contact
  7117. NSDictionary *contact_dic = [self infoName:@"billing_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7118. [dic setValue:contact_dic forKey:@"item_4"];
  7119. // phone
  7120. NSDictionary *phone_dic = [self infoName:@"billing_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7121. [dic setValue:phone_dic forKey:@"item_5"];
  7122. // fax
  7123. NSDictionary *fax_dic = [self infoName:@"billing_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7124. [dic setValue:fax_dic forKey:@"item_6"];
  7125. // email
  7126. NSDictionary *email_dic = [self infoName:@"billing_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7127. [dic setValue:email_dic forKey:@"item_7"];
  7128. }];
  7129. return dic;
  7130. }
  7131. + (NSDictionary *)returnToDic:(NSDictionary *)params db:(sqlite3 *)db {
  7132. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7133. // setting
  7134. NSDictionary *setting = params[@"setting"];
  7135. NSNumber *hide = setting[@"ReturnToHide"];
  7136. [dic setValue:hide forKey:@"hide"];
  7137. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7138. orderCode = [self translateSingleQuote:orderCode];
  7139. 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];
  7140. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7141. NSString *cid = [self textAtColumn:0 statement:stmt];
  7142. NSString *name = [self textAtColumn:1 statement:stmt];
  7143. NSString *ext = [self textAtColumn:2 statement:stmt];
  7144. NSString *contact = [self textAtColumn:3 statement:stmt];
  7145. NSString *email = [self textAtColumn:4 statement:stmt];
  7146. NSString *fax = [self textAtColumn:5 statement:stmt];
  7147. NSString *phone = [self textAtColumn:6 statement:stmt];
  7148. // count
  7149. [dic setValue:[NSNumber numberWithInteger:8] forKey:@"count"];
  7150. // title
  7151. [dic setValue:@"Return To" forKey:@"title"];
  7152. // hide
  7153. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"hide"];
  7154. // choose
  7155. NSMutableDictionary *choose_dic = [NSMutableDictionary dictionary];
  7156. [choose_dic setValue:@"choose" forKey:@"aname"];
  7157. [choose_dic setValue:@"multi_action" forKey:@"control"];
  7158. [choose_dic setValue:[NSNumber numberWithInteger:2] forKey:@"count"];
  7159. NSDictionary *same_as_ship_from_dic = @{@"aname" : @"Same as ship from",
  7160. @"key_map" : @{@"returnto_cid" : @"sender_cid",
  7161. @"returnto_contact" : @"sender_contact",
  7162. @"returnto_email" : @"sender_email",
  7163. @"returnto_ext" : @"sender_ext",
  7164. @"returnto_fax" : @"sender_fax",
  7165. @"returnto_name" : @"sender_name",
  7166. @"returnto_phone" : @"sender_phone"},
  7167. @"type" : @"pull"};
  7168. [choose_dic setValue:same_as_ship_from_dic forKey:@"item_0"];
  7169. NSDictionary *select_return_to_dic = @{
  7170. @"aname" : @"Select return to",
  7171. @"name" : @"select_cid",
  7172. @"refresh" : [NSNumber numberWithInteger:0],
  7173. @"value" : @"Contact_Return_To",
  7174. @"key_map" : @{
  7175. @"returnto_cid" : @"customer_cid",
  7176. @"returnto_contact" : @"customer_contact",
  7177. @"returnto_email" : @"customer_email",
  7178. @"returnto_ext" : @"customer_contact_ext",
  7179. @"returnto_fax" : @"customer_fax",
  7180. @"returnto_name" : @"customer_name",
  7181. @"returnto_phone" : @"customer_phone"
  7182. }
  7183. };
  7184. [choose_dic setValue:select_return_to_dic forKey:@"item_1"];
  7185. [dic setValue:choose_dic forKey:@"item_0"];
  7186. // contact id
  7187. NSDictionary *contact_id_dic = [[self infoName:@"returnto_cid" aname:@"Contact ID" control:@"edit" keyboard:@"text" value:cid] mutableCopy];
  7188. [contact_id_dic setValue:@"1" forKey:@"disable"];
  7189. [contact_id_dic setValue:@"true" forKey:@"required"];
  7190. [dic setValue:contact_id_dic forKey:@"item_1"];
  7191. // company name
  7192. NSDictionary *company_name_dic = [self infoName:@"returnto_name" aname:@"Company Name" control:@"edit" keyboard:@"text" value:name];
  7193. [dic setValue:company_name_dic forKey:@"item_2"];
  7194. // address
  7195. NSDictionary *ext_dic = [self infoName:@"returnto_ext" aname:@"Address" control:@"text_view" keyboard:@"text" value:ext];
  7196. [dic setValue:ext_dic forKey:@"item_3"];
  7197. // contact
  7198. NSDictionary *contact_dic = [self infoName:@"returnto_contact" aname:@"Contact" control:@"edit" keyboard:@"text" value:contact];
  7199. [dic setValue:contact_dic forKey:@"item_4"];
  7200. // phone
  7201. NSDictionary *phone_dic = [self infoName:@"returnto_phone" aname:@"Phone" control:@"edit" keyboard:@"text" value:phone];
  7202. [dic setValue:phone_dic forKey:@"item_5"];
  7203. // fax
  7204. NSDictionary *fax_dic = [self infoName:@"returnto_fax" aname:@"Fax" control:@"edit" keyboard:@"text" value:fax];
  7205. [dic setValue:fax_dic forKey:@"item_6"];
  7206. // email
  7207. NSDictionary *email_dic = [self infoName:@"returnto_email" aname:@"Email" control:@"edit" keyboard:@"text" value:email];
  7208. [dic setValue:email_dic forKey:@"item_7"];
  7209. }];
  7210. return dic;
  7211. }
  7212. + (NSDictionary *)modelInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7213. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7214. orderCode = [self translateSingleQuote:orderCode];
  7215. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7216. __block double TotalCuft = 0;
  7217. __block double TotalWeight = 0;
  7218. __block int TotalCarton = 0;
  7219. __block double payments = 0;
  7220. // setting
  7221. NSDictionary *setting = params[@"setting"];
  7222. NSNumber *hide = setting[@"ModelInformationHide"];
  7223. [dic setValue:hide forKey:@"hide"];
  7224. NSString *sql = [NSString stringWithFormat:@"select c.item_id,c.item_count,m.stockUom,c.price,c.discount,m.name,m.description,c.line_note,m.availability from (select _id,product_id,decrypt(str_price) as price,discount,item_count,line_note,item_id from offline_cart where so_no='%@') c left join product m on c.product_id=m.product_id;",orderCode];
  7225. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7226. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7227. NSMutableDictionary *model_dic = [NSMutableDictionary dictionary];
  7228. // item id
  7229. int item_id = sqlite3_column_int(stmt, 0);
  7230. // count
  7231. int item_count = sqlite3_column_int(stmt, 1);
  7232. // stockUom
  7233. int stockUom = sqlite3_column_int(stmt, 2);
  7234. // unit price
  7235. NSString *str_price = [self textAtColumn:3 statement:stmt];
  7236. NSString* Price=nil;
  7237. if([str_price isEqualToString:@""])
  7238. {
  7239. NSNumber* price = [self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db];
  7240. if(price==nil)
  7241. Price=@"No Price.";
  7242. else
  7243. Price=[NSString stringWithFormat:@"%.2f",price.floatValue];
  7244. }
  7245. else
  7246. {
  7247. Price=[NSString stringWithFormat:@"%.2f",[str_price floatValue]];
  7248. }
  7249. // discount
  7250. double discount = sqlite3_column_double(stmt, 4);
  7251. // name
  7252. NSString *name = [self textAtColumn:5 statement:stmt];
  7253. // description
  7254. NSString *description = [self textAtColumn:6 statement:stmt];
  7255. description = [NSString stringWithFormat:@"%@\n%@",name,description ];
  7256. // line note
  7257. NSString *line_note = [self textAtColumn:7 statement:stmt];
  7258. int avaulability = sqlite3_column_int(stmt, 8);
  7259. // img
  7260. NSString *img = [self item_image:[NSString stringWithFormat:@"%@",[NSNumber numberWithInt:item_id]] db:db];
  7261. NSDictionary *combine = [self model_bundle:item_id db:db compute_part:false contactID:appDelegate.contact_id];
  7262. double subTotal = item_count * [Price doubleValue] * (1.0 - discount / 100.0);
  7263. if(combine != nil)
  7264. {
  7265. // int citem=0;
  7266. int bcount=[[combine valueForKey:@"count"] intValue];
  7267. for(int bc=0;bc<bcount;bc++)
  7268. {
  7269. NSDictionary * bitem = [combine objectForKey:[NSString stringWithFormat:@"item_%d",bc]];
  7270. int modulus= [[bitem valueForKey:@"modulus"] intValue];
  7271. double uprice= [[bitem valueForKey:@"unit_price"]doubleValue];
  7272. subTotal += uprice * modulus * item_count;
  7273. }
  7274. }
  7275. [model_dic setValue:[NSString stringWithFormat:@"%d",item_id] forKey:@"cart_item_id"];
  7276. [model_dic setValue:@"model" forKey:@"control"];
  7277. [model_dic setValue:[NSString stringWithFormat:@"%d",item_count] forKey:@"count"];
  7278. [model_dic setValue:description forKey:@"description"];
  7279. [model_dic setValue:line_note forKey:@"note"];
  7280. [model_dic setValue:img forKey:@"img_url"];
  7281. [model_dic setValue:[NSNumber numberWithInteger:stockUom] forKey:@"stockUom"];
  7282. [model_dic setValue:[NSString stringWithFormat:@"%.2f",subTotal] forKey:@"total_price"];
  7283. [model_dic setValue:Price forKey:@"unit_price"];
  7284. [model_dic setValue:[NSString stringWithFormat:@"%.2f",discount] forKey:@"discount"];
  7285. [model_dic setValue:[NSNumber numberWithInt:avaulability] forKey:@"quantity_available"];
  7286. if (combine) {
  7287. [model_dic setValue:combine forKey:@"combine"];
  7288. }
  7289. // well,what under the row is the info for total
  7290. NSDictionary* bsubtotaljson=[self model_subtotal:item_id count:item_count db:db compute_part:true];
  7291. double cuft=[bsubtotaljson[@"cuft"] doubleValue];
  7292. double weight=[bsubtotaljson[@"weight"] doubleValue];
  7293. int carton=[bsubtotaljson[@"carton"] intValue];
  7294. TotalCuft += cuft;
  7295. TotalWeight += weight;
  7296. TotalCarton += carton;
  7297. payments += subTotal;
  7298. //---------------------------
  7299. [dic setValue:model_dic forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  7300. [dic setValue:[NSNumber numberWithInteger:(*count)] forKey:@"count"];
  7301. }];
  7302. [dic setValue:@"Model Information" forKey:@"title"];
  7303. [dic setValue:@{@"payments" : [NSNumber numberWithDouble:payments],@"totalCuft" : [NSNumber numberWithDouble:TotalCuft],@"totalWeight" : [NSNumber numberWithDouble:TotalWeight],@"totalCarton" : [NSNumber numberWithInteger:TotalCarton]} forKey:@"toOrderTotal"];
  7304. return dic;
  7305. }
  7306. + (NSDictionary *)remarksContentDic:(NSDictionary *)params db:(sqlite3 *)db {
  7307. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7308. [dic setValue:@"Remarks Content" forKey:@"title"];
  7309. [dic setValue:[NSNumber numberWithInteger:4] forKey:@"count"];
  7310. // setting
  7311. NSDictionary *setting = params[@"setting"];
  7312. NSNumber *hide = setting[@"RemarksContentHide"];
  7313. [dic setValue:hide forKey:@"hide"];
  7314. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7315. orderCode = [self translateSingleQuote:orderCode];
  7316. NSString *sql = [NSString stringWithFormat:@"select poNumber,must_call,internal_notes,general_notes from offline_order where so_id = '%@';",orderCode];
  7317. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7318. NSString *poNumber = [self textAtColumn:0 statement:stmt];
  7319. int mustCall = sqlite3_column_int(stmt, 1);
  7320. NSString *internalNotes = [self textAtColumn:2 statement:stmt];
  7321. NSString *generalNotes = [self textAtColumn:3 statement:stmt];
  7322. NSDictionary *po_dic = @{
  7323. @"aname" : @"PO#",
  7324. @"control" : @"edit",
  7325. @"keyboard" : @"text",
  7326. @"name" : @"poNumber",
  7327. @"value" : poNumber
  7328. };
  7329. NSDictionary *must_call_dic = @{
  7330. @"aname" : @"MUST CALL BEFORE DELIVERY",
  7331. @"control" : @"switch",
  7332. @"name" : @"must_call",
  7333. @"value" : mustCall ? @"true" : @"false"
  7334. };
  7335. NSDictionary *general_notes_dic = @{
  7336. @"aname" : @"General notes",
  7337. @"control" : @"text_view",
  7338. @"keyboard" : @"text",
  7339. @"name" : @"comments",
  7340. @"value" : generalNotes
  7341. };
  7342. NSDictionary *internal_notes_dic = @{
  7343. @"aname" : @"Internal notes",
  7344. @"control" : @"text_view",
  7345. @"keyboard" : @"text",
  7346. @"name" : @"internal_notes",
  7347. @"value" : internalNotes
  7348. };
  7349. [dic setValue:po_dic forKey:@"item_0"];
  7350. [dic setValue:must_call_dic forKey:@"item_1"];
  7351. [dic setValue:general_notes_dic forKey:@"item_2"];
  7352. [dic setValue:internal_notes_dic forKey:@"item_3"];
  7353. }];
  7354. return dic;
  7355. }
  7356. + (NSDictionary *)orderTotalDic:(NSDictionary *)params otherTotal:(NSDictionary *)total db:(sqlite3 *)db {
  7357. // params
  7358. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7359. orderCode = [self translateSingleQuote:orderCode];
  7360. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7361. // setting
  7362. NSDictionary *setting = params[@"setting"];
  7363. NSNumber *hide = setting[@"OrderTotalHide"];
  7364. [dic setValue:hide forKey:@"hide"];
  7365. NSString *sql = [NSString stringWithFormat:@"select lift_gate,lift_gate_value,shipping,handling_fee_value from offline_order where so_id = '%@';",orderCode];
  7366. __block double lift_gate_value = 0;
  7367. __block double handling_fee = 0;
  7368. __block double shipping = 0;
  7369. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7370. int lift_gate = sqlite3_column_int(stmt, 0);
  7371. lift_gate_value = sqlite3_column_double(stmt, 1);
  7372. shipping = sqlite3_column_double(stmt, 2);
  7373. handling_fee = sqlite3_column_double(stmt, 3);
  7374. if (!lift_gate) {
  7375. lift_gate_value = 0;
  7376. }
  7377. }];
  7378. double totalCuft = [[total valueForKey:@"totalCuft"] doubleValue];
  7379. double totalWeight = [[total valueForKey:@"totalWeight"] doubleValue];
  7380. int totalCarton = [[total valueForKey:@"totalCarton"] doubleValue];
  7381. double payments = [[total valueForKey:@"payments"] doubleValue];
  7382. // double totalPrice = payments + lift_gate_value + shipping + handling_fee;
  7383. double totalPrice = payments;
  7384. [dic setValue:@"Order Total" forKey:@"title"];
  7385. NSDictionary *payments_dic = @{
  7386. @"align" : @"right",
  7387. @"aname" : @"Payments/Credits",
  7388. @"control" : @"text",
  7389. @"name" : @"paymentsAndCredits",
  7390. @"type" : @"price",
  7391. @"value" : [NSString stringWithFormat:@"%.2f",payments]
  7392. };
  7393. [dic setValue:payments_dic forKey:@"item_0"];
  7394. // version 1.71 remove
  7395. // NSDictionary *handling_fee_dic = @{
  7396. // @"align" : @"right",
  7397. // @"aname" : @"Handling Fee",
  7398. // @"control" : @"text",
  7399. // @"name" : @"handling_fee_value",
  7400. // @"required" : @"true",
  7401. // @"type" : @"price",
  7402. // @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",handling_fee]*/
  7403. // };
  7404. NSDictionary *shipping_dic = @{
  7405. @"align" : @"right",
  7406. @"aname" : @"Shipping*",
  7407. @"control" : @"text",
  7408. @"name" : @"shipping",
  7409. @"required" : @"true",
  7410. @"type" : @"price",
  7411. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",shipping]*/
  7412. };
  7413. NSDictionary *lift_gate_dic = @{
  7414. @"align" : @"right",
  7415. @"aname" : @"Liftgate Fee(No Loading Dock)",
  7416. @"control" : @"text",
  7417. @"name" : @"lift_gate_value",
  7418. @"required" : @"true",
  7419. @"type" : @"price",
  7420. @"value" : @"Shipping To Be Quoted"/*[NSString stringWithFormat:@"%.2f",lift_gate_value]*/
  7421. };
  7422. int item_count = 1;
  7423. if (![[params objectForKey:@"logist"] isEqualToString:@"WILL CALL"]) {
  7424. NSString *params_lift_gate = [self valueInParams:params key:@"lift_gate"];
  7425. if (params_lift_gate && [params_lift_gate isEqualToString:@"false"]) {
  7426. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7427. [dic setValue:lift_gate_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7428. } else if (params_lift_gate && [params_lift_gate isEqualToString:@"true"]) {
  7429. [dic setValue:shipping_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7430. } else {
  7431. }
  7432. }
  7433. // version 1.71 remove
  7434. // [dic setValue:handling_fee_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7435. NSDictionary *total_price_dic = @{
  7436. @"align" : @"right",
  7437. @"aname" : @"Total",
  7438. @"control" : @"text",
  7439. @"name" : @"totalPrice",
  7440. @"type" : @"price",
  7441. @"value" : [NSString stringWithFormat:@"%.2f",totalPrice]
  7442. };
  7443. [dic setValue:total_price_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7444. NSDictionary *total_cuft_dic = @{
  7445. @"align" : @"right",
  7446. @"aname" : @"Total Cuft",
  7447. @"control" : @"text",
  7448. @"name" : @"",
  7449. @"value" : [NSString stringWithFormat:@"%.2f",totalCuft]
  7450. };
  7451. [dic setValue:total_cuft_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7452. NSDictionary *total_weight_dic = @{
  7453. @"align" : @"right",
  7454. @"aname" : @"Total Weight",
  7455. @"control" : @"text",
  7456. @"name" : @"",
  7457. @"value" : [NSString stringWithFormat:@"%.2f",totalWeight]
  7458. };
  7459. [dic setValue:total_weight_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7460. NSDictionary *total_carton_dic = @{
  7461. @"align" : @"right",
  7462. @"aname" : @"Total Carton",
  7463. @"control" : @"text",
  7464. @"name" : @"",
  7465. @"value" : [NSString stringWithFormat:@"%d",totalCarton]
  7466. };
  7467. [dic setValue:total_carton_dic forKey:[NSString stringWithFormat:@"item_%d",item_count++]];
  7468. [dic setValue:[NSNumber numberWithInteger:item_count] forKey:@"count"];
  7469. return dic;
  7470. }
  7471. + (NSDictionary *)signatureDic:(NSDictionary *)params db:(sqlite3 *)db {
  7472. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7473. orderCode = [self translateSingleQuote:orderCode];
  7474. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7475. // setting
  7476. NSDictionary *setting = params[@"setting"];
  7477. NSNumber *hide = setting[@"SignatureHide"];
  7478. [dic setValue:hide forKey:@"hide"];
  7479. [dic setValue:@"Signature" forKey:@"title"];
  7480. __block NSString *pic_path = @"";
  7481. NSString *sql = [NSString stringWithFormat:@"select sign_picpath from offline_order where so_id = '%@';",orderCode];
  7482. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7483. pic_path = [self textAtColumn:0 statement:stmt];
  7484. }];
  7485. NSDictionary *pic_dic = @{
  7486. @"aname" : @"Signature",
  7487. @"avalue" :pic_path,
  7488. @"control" : @"signature",
  7489. @"name" : @"sign_picpath",
  7490. @"value" : pic_path
  7491. };
  7492. [dic setValue:pic_dic forKey:@"item_0"];
  7493. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7494. return dic;
  7495. }
  7496. + (NSDictionary *)shippingMethodDic:(NSMutableDictionary *)params db:(sqlite3 *)db {
  7497. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  7498. // setting
  7499. NSDictionary *setting = params[@"setting"];
  7500. NSNumber *hide = setting[@"ShippingMethodHide"];
  7501. [dic setValue:hide forKey:@"hide"];
  7502. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7503. orderCode = [self translateSingleQuote:orderCode];
  7504. NSString *sql = [NSString stringWithFormat:@"select logist,lift_gate,logistic_note from offline_order where so_id = '%@';",orderCode];
  7505. __block NSString *logist = @"";
  7506. __block NSString *lift_gate = @"";
  7507. __block int lift_gate_integer = 0;
  7508. __block NSString *logistic_note = @"";
  7509. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7510. logist = [self textAtColumn:0 statement:stmt];
  7511. lift_gate_integer = sqlite3_column_int(stmt, 1);
  7512. logistic_note = [self textAtColumn:2 statement:stmt];
  7513. }];
  7514. [dic setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7515. [dic setValue:@"Shipping Method" forKey:@"title"];
  7516. // val_0
  7517. int PERSONAL_PICK_UP_check = 0;
  7518. int USE_MY_CARRIER_check = 0;
  7519. NSString *logistic_note_text = @"";
  7520. int will_call_check = 0;
  7521. if (logist && [logist isEqualToString:@"WILL CALL"]) {
  7522. will_call_check = 1;
  7523. if ([logistic_note containsString:@"PERSONAL PICK UP"]) {
  7524. PERSONAL_PICK_UP_check = 1;
  7525. } else if ([logistic_note containsString:@"USE MY CARRIER"]) {
  7526. USE_MY_CARRIER_check = 1;
  7527. NSArray *logistic_note_array = [logistic_note componentsSeparatedByString:@"\n\t"];
  7528. logistic_note = [logistic_note_array firstObject];
  7529. if (logistic_note_array.count > 1) {
  7530. logistic_note_text = [logistic_note_array lastObject];
  7531. }
  7532. }
  7533. }
  7534. NSMutableDictionary *val_0 = [NSMutableDictionary dictionary];
  7535. [val_0 setValue:[NSNumber numberWithInteger:will_call_check] forKey:@"check"];
  7536. [val_0 setValue:@"WILL CALL(requires 24 hours advance notice)" forKey:@"value"];
  7537. [val_0 setValue:@"WILL CALL" forKey:@"value_id"];
  7538. NSMutableDictionary *val0_subItem = [NSMutableDictionary dictionary];
  7539. [val0_subItem setValue:[NSNumber numberWithInteger:1] forKey:@"count"];
  7540. NSDictionary *val0_subItem_item0 = @{
  7541. @"aname" : @"Option",
  7542. @"cadedate" : @{
  7543. @"count" : [NSNumber numberWithInteger:2],
  7544. @"val_0" : @{
  7545. @"check" : [NSNumber numberWithInteger:PERSONAL_PICK_UP_check],
  7546. @"refresh" : [NSNumber numberWithInteger:0],
  7547. @"value" : @"PERSONAL PICK UP",
  7548. @"value_id" : @"PERSONAL PICK UP"
  7549. },
  7550. @"val_1" : @{
  7551. @"check" : [NSNumber numberWithInteger:USE_MY_CARRIER_check],
  7552. @"refresh" : [NSNumber numberWithInteger:0],
  7553. @"sub_item" : @{
  7554. @"count" : [NSNumber numberWithInteger:1],
  7555. @"item_0" : @{
  7556. @"aname" : @"BOL",
  7557. @"control" : @"edit",
  7558. @"keyboard" : @"text",
  7559. @"name" : @"logist_note_text",
  7560. @"refresh" : [NSNumber numberWithInteger:0],
  7561. @"required" : @"false",
  7562. @"value" : logistic_note_text
  7563. }
  7564. },
  7565. @"value" : @"USE MY CARRIER",
  7566. @"value_id" : @"USE MY CARRIER"
  7567. }
  7568. },
  7569. @"control" : @"enum",
  7570. @"name" : @"logistic_note",
  7571. @"required" : @"true",
  7572. @"single_select" : @"true"
  7573. };
  7574. [val0_subItem setValue:val0_subItem_item0 forKey:@"item_0"];
  7575. [val_0 setValue:val0_subItem forKey:@"sub_item"];
  7576. // val_1
  7577. lift_gate = [self valueInParams:params key:@"lift_gate"];
  7578. if([lift_gate isEqualToString:@""]) {
  7579. if (lift_gate_integer == 1) {
  7580. lift_gate = @"true";
  7581. } else {
  7582. lift_gate = @"false";
  7583. }
  7584. }
  7585. int common_carrier_check = 0;
  7586. if (logist && [logist isEqualToString:@"COMMON CARRIER"]) {
  7587. common_carrier_check = 1;
  7588. [params setValue:lift_gate forKey:@"lift_gate"];
  7589. }
  7590. NSDictionary *val_1 = @{
  7591. @"check" : [NSNumber numberWithInteger:common_carrier_check],
  7592. @"sub_item" : @{
  7593. @"count" : [NSNumber numberWithInteger:1],
  7594. @"item_0" : @{
  7595. @"aname" : @"Do you have Loading Dock Available ? Slide to green for YES",
  7596. @"control" : @"switch",
  7597. @"name" : @"lift_gate",
  7598. @"refresh" : [NSNumber numberWithInteger:1],
  7599. @"required" : @"true",
  7600. @"value" : lift_gate
  7601. }
  7602. },
  7603. @"value" : @"COMMON CARRIER",
  7604. @"value_id" : @"COMMON CARRIER"
  7605. };
  7606. // cadedate
  7607. NSDictionary *cadedate = @{
  7608. @"count" : [NSNumber numberWithInteger:2],
  7609. @"val_0" : val_0,
  7610. @"val_1" : val_1
  7611. };
  7612. // item_0
  7613. NSMutableDictionary *item_0 = @{
  7614. @"aname" : @"Shipping",
  7615. @"cadedate" : cadedate,
  7616. @"control" : @"enum",
  7617. @"name" : @"logist",
  7618. @"refresh" : [NSNumber numberWithInteger:1],
  7619. @"single_select" : @"true",
  7620. }.mutableCopy;
  7621. NSNumber *required = setting[@"ShippingMethodRequire"];
  7622. if ([required integerValue]) {
  7623. [item_0 setValue:@"true" forKey:@"required"];
  7624. }
  7625. [dic setValue:item_0 forKey:@"item_0"];
  7626. if ([logist isEqualToString:@"WILL CALL"]) {
  7627. [dic removeObjectForKey:@"lift_gate"];
  7628. }
  7629. return dic;
  7630. }
  7631. + (NSDictionary *)submitAsDic:(NSDictionary *)params db:(sqlite3 *)db {
  7632. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7633. orderCode = [self translateSingleQuote:orderCode];
  7634. NSString *sql = [NSString stringWithFormat:@"select submit_as from offline_order where so_id = '%@';",orderCode];
  7635. __block int submit_as_integer = 0;
  7636. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7637. submit_as_integer = sqlite3_column_int(stmt, 0);
  7638. }];
  7639. int check11 = 0;
  7640. int check10 = 0;
  7641. if (submit_as_integer == 11) {
  7642. check11 = 1;
  7643. }
  7644. if (submit_as_integer == 10) {
  7645. check10 = 1;
  7646. }
  7647. // section_0
  7648. NSMutableDictionary *dic = @{
  7649. @"count" : @(1),
  7650. @"item_0" : @{
  7651. @"aname" : @"Submit Order As",
  7652. @"cadedate" : @{
  7653. @"count" : @(2),
  7654. @"val_0" : @{
  7655. @"check" : [NSNumber numberWithInteger:check11],
  7656. @"value" : @"Sales Order",
  7657. @"value_id" : @(11)
  7658. },
  7659. @"val_1" : @{
  7660. @"check" : [NSNumber numberWithInteger:check10],
  7661. @"value" : @"Quote",
  7662. @"value_id" : @(10)
  7663. }
  7664. },
  7665. @"control" : @"enum",
  7666. @"name" : @"erpOrderStatus",
  7667. @"required" : @"true",
  7668. @"single_select" : @"true"
  7669. },
  7670. @"title" : @"Order Type"
  7671. }.mutableCopy;
  7672. // setting
  7673. NSDictionary *setting = params[@"setting"];
  7674. NSNumber *hide = setting[@"OrderTypeHide"];
  7675. [dic setValue:hide forKey:@"hide"];
  7676. return dic;
  7677. }
  7678. + (NSDictionary *)offline_getStateByCountryCode:(NSString *)countryCode checkedState:(NSString *)state_code db:(sqlite3 *)db {
  7679. countryCode = [self translateSingleQuote:countryCode];
  7680. NSString *sqlQuery = [NSString stringWithFormat:@"select _id,name,code,country_code from offline_state where country_code = '%@';",countryCode];
  7681. NSMutableDictionary *ret = [[iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7682. char *name = (char *) sqlite3_column_text(stmt, 1); // value
  7683. char *code = (char *) sqlite3_column_text(stmt, 2); // value_id
  7684. if (name == NULL) {
  7685. name = "";
  7686. }
  7687. if (code == NULL) {
  7688. code = "";
  7689. }
  7690. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7691. [stateDic setValue:[NSString stringWithUTF8String:name] forKey:@"value"];
  7692. [stateDic setValue:[NSString stringWithUTF8String:code] forKey:@"value_id"];
  7693. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7694. if (state_code && [[NSString stringWithUTF8String:code] isEqualToString:state_code]) {
  7695. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7696. }
  7697. NSString *key = [NSString stringWithFormat:@"val_%ld",(*count)++];
  7698. [container setValue:stateDic forKey:key];
  7699. }] mutableCopy];
  7700. [ret removeObjectForKey:@"result"];
  7701. // failure 可以不用了,一样的
  7702. if (ret.allKeys.count == 0) {
  7703. NSMutableDictionary *stateDic = [NSMutableDictionary dictionaryWithCapacity:2];
  7704. [stateDic setValue:@"Other" forKey:@"value"];
  7705. [stateDic setValue:@"" forKey:@"value_id"];
  7706. [stateDic setValue:[NSNumber numberWithInt:0] forKey:@"check"];
  7707. if (state_code && [@"" isEqualToString:state_code]) {
  7708. [stateDic setValue:[NSNumber numberWithInt:1] forKey:@"check"];
  7709. }
  7710. NSString *key = [NSString stringWithFormat:@"val_0"];
  7711. [ret setValue:stateDic forKey:key];
  7712. }
  7713. [ret setValue:[NSNumber numberWithInt:ret.allKeys.count] forKey:@"count"];
  7714. return ret;
  7715. }
  7716. + (NSDictionary *)paymentInfoDic:(NSDictionary *)params db:(sqlite3 *)db {
  7717. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7718. orderCode = [self translateSingleQuote:orderCode];
  7719. 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];
  7720. __block NSString *payType = @"";
  7721. __block NSString *firstName = @"";
  7722. __block NSString *lastName = @"";
  7723. __block NSString *addr1 = @"";
  7724. __block NSString *addr2 = @"";
  7725. __block NSString *zipcode = @"";
  7726. __block NSString *cardType = @"";
  7727. __block NSString *cardNumber = @"";
  7728. __block NSString *securityCode = @"";
  7729. __block NSString *month = @"";
  7730. __block NSString *year = @"";
  7731. __block NSString *city = @"";
  7732. __block NSString *state = @"";
  7733. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7734. payType = [self textAtColumn:0 statement:stmt];
  7735. firstName = [self textAtColumn:1 statement:stmt];
  7736. lastName = [self textAtColumn:2 statement:stmt];
  7737. addr1 = [self textAtColumn:3 statement:stmt];
  7738. addr2 = [self textAtColumn:4 statement:stmt];
  7739. zipcode = [self textAtColumn:5 statement:stmt];
  7740. cardType = [self textAtColumn:6 statement:stmt];
  7741. cardNumber = [self textAtColumn:7 statement:stmt];
  7742. securityCode = [self textAtColumn:8 statement:stmt];
  7743. month = [self textAtColumn:9 statement:stmt];
  7744. year = [self textAtColumn:10 statement:stmt];
  7745. city = [self textAtColumn:11 statement:stmt];
  7746. state = [self textAtColumn:12 statement:stmt];
  7747. }];
  7748. NSString *required = @"true";
  7749. // setting
  7750. NSDictionary *setting = params[@"setting"];
  7751. NSNumber *requiredNumber = setting[@"PaymentRequire"];
  7752. if ([requiredNumber integerValue]) {
  7753. required = @"true";
  7754. } else {
  7755. required = @"false";
  7756. }
  7757. // "section_2"
  7758. NSMutableDictionary *dic = @{
  7759. @"count" : @(1),
  7760. @"item_0" : @{
  7761. @"aname" : @"Payment",
  7762. @"required" : required,
  7763. @"cadedate" : @{
  7764. @"count" : @(6),
  7765. @"val_3" : @{
  7766. @"check" : [payType isEqualToString:@"Check"] ? @(1) : @(0),
  7767. @"value" : @"Check",
  7768. @"value_id" : @"Check"
  7769. },
  7770. @"val_2" : @{
  7771. @"check" : [payType isEqualToString:@"Cash"] ? @(1) : @(0),
  7772. @"value" : @"Cash",
  7773. @"value_id" : @"Cash"
  7774. },
  7775. @"val_1" : @{
  7776. @"check" : [payType isEqualToString:@"NET 30"] ? @(1) : @(0),
  7777. @"value" : @"NET 60",
  7778. @"value_id" : @"NET 30"
  7779. },
  7780. @"val_4" : @{
  7781. @"check" : [payType isEqualToString:@"Wire Transfer"] ? @(1) : @(0),
  7782. @"value" : @"Wire Transfer",
  7783. @"value_id" : @"Wire Transfer"
  7784. },
  7785. @"val_0" : @{
  7786. @"check" : [payType isEqualToString:@"Credit Card"] ? @(1) : @(0),
  7787. @"sub_item" : @{
  7788. @"count" : @(3),
  7789. @"item_0" : @{
  7790. @"aname" : @"choose",
  7791. @"control" : @"multi_action",
  7792. @"count" : @(1),
  7793. @"item_0" : @{
  7794. @"aname" : @"Same as customer",
  7795. @"key_map" : @{
  7796. @"credit_card_address1" : @"customer_address1",
  7797. @"credit_card_address2" : @"customer_address2",
  7798. @"credit_card_city" : @"customer_city",
  7799. @"credit_card_first_name" : @"customer_first_name",
  7800. @"credit_card_last_name" : @"customer_last_name",
  7801. @"credit_card_state" : @"customer_state",
  7802. @"credit_card_zipcode" : @"customer_zipcode"
  7803. },
  7804. @"type" : @"pull"
  7805. }
  7806. },
  7807. @"item_1" : @{
  7808. @"aname" : @"",
  7809. @"color" : @"red",
  7810. @"control" : @"text",
  7811. @"name" : @"",
  7812. @"value" : @"USA Credit cards only"
  7813. },
  7814. @"item_2" : @{
  7815. @"aname" : @"Fill",
  7816. @"cadedate" : @{
  7817. @"count" : @(2),
  7818. @"val_0" : @{
  7819. @"check" : @(1),
  7820. @"sub_item" : @{
  7821. @"count" : @(11),
  7822. @"item_0" : @{
  7823. @"aname" : @"Type",
  7824. @"cadedate" : @{
  7825. @"count" : @(2),
  7826. @"val_0" : @{
  7827. @"check" : [cardType isEqualToString:@"VISA"] ? @(1) : @(0),
  7828. @"value" : @"VISA",
  7829. @"value_id" : @(0)
  7830. },
  7831. @"val_1" : @{
  7832. @"check" : [cardType isEqualToString:@"MASTER CARD"] ? @(1) : @(0),
  7833. @"value" : @"MASTER CARD",
  7834. @"value_id" : @(1)
  7835. }
  7836. },
  7837. @"control" : @"enum",
  7838. @"name" : @"credit_card_type",
  7839. @"required" : @"true",
  7840. @"single_select" : @"true"
  7841. },
  7842. @"item_1" : @{
  7843. @"aname" : @"Number",
  7844. @"control" : @"edit",
  7845. @"keyboard" : @"int",
  7846. @"length" : @"16",
  7847. @"name" : @"credit_card_number",
  7848. @"required" : @"true",
  7849. @"value" : cardNumber
  7850. },
  7851. @"item_10" : @{
  7852. @"aname" : @"State",
  7853. @"cadedate" : [self offline_getStateByCountryCode:@"US" checkedState:state db:db],
  7854. @"control" : @"enum",
  7855. @"enum" : @"true",
  7856. @"name" : @"credit_card_state",
  7857. @"required" : @"true",
  7858. @"single_select" : @"true"
  7859. },
  7860. @"item_2" : @{
  7861. @"aname" : @"Expiration Date",
  7862. @"control" : @"monthpicker",
  7863. @"name" : @"credit_card_expiration",
  7864. @"required" : @"true",
  7865. @"type" : @"date",
  7866. @"value" : [month isEqualToString:@""] || [year isEqualToString:@""] ? @"" : [NSString stringWithFormat:@"%@/%@",month,year]
  7867. },
  7868. @"item_3" : @{
  7869. @"aname" : @"Security Code",
  7870. @"control" : @"edit",
  7871. @"keyboard" : @"int",
  7872. @"length" : @"3",
  7873. @"name" : @"credit_card_security_code",
  7874. @"required" : @"true",
  7875. @"value" : securityCode
  7876. },
  7877. @"item_4" : @{
  7878. @"aname" : @"First Name",
  7879. @"control" : @"edit",
  7880. @"keyboard" : @"text",
  7881. @"name" : @"credit_card_first_name",
  7882. @"required" : @"true",
  7883. @"value" : firstName
  7884. },
  7885. @"item_5" : @{
  7886. @"aname" : @"Last Name",
  7887. @"control" : @"edit",
  7888. @"keyboard" : @"text",
  7889. @"name" : @"credit_card_last_name",
  7890. @"required" : @"true",
  7891. @"value" : lastName
  7892. },
  7893. @"item_6" : @{
  7894. @"aname" : @"Address 1",
  7895. @"control" : @"edit",
  7896. @"keyboard" : @"text",
  7897. @"name" : @"credit_card_address1",
  7898. @"required" : @"true",
  7899. @"value" : addr1
  7900. },
  7901. @"item_7" : @{
  7902. @"aname" : @"Address 2",
  7903. @"control" : @"edit",
  7904. @"keyboard" : @"text",
  7905. @"name" : @"credit_card_address2",
  7906. @"value" : addr2
  7907. },
  7908. @"item_8" : @{
  7909. @"aname" : @"zip code",
  7910. @"control" : @"edit",
  7911. @"keyboard" : @"text",
  7912. @"name" : @"credit_card_zipcode",
  7913. @"required" : @"true",
  7914. @"value" : zipcode
  7915. },
  7916. @"item_9" : @{
  7917. @"aname" : @"City",
  7918. @"control" : @"edit",
  7919. @"keyboard" : @"text",
  7920. @"name" : @"credit_card_city",
  7921. @"required" : @"true",
  7922. @"value" : city
  7923. }
  7924. },
  7925. @"value" : @"Fill Now",
  7926. @"value_id" : @""
  7927. },
  7928. @"val_1" : @{
  7929. @"check" : @(0),
  7930. @"value" : @"Fill Later",
  7931. @"value_id" : @""
  7932. }
  7933. },
  7934. @"control" : @"enum",
  7935. @"name" : @"",
  7936. @"single_select" : @"true"
  7937. }
  7938. },
  7939. @"value" : @"Visa/Master",
  7940. @"value_id" : @"Credit Card"
  7941. },
  7942. @"val_5" : @{
  7943. @"check" : [payType isEqualToString:@"FOLLOW EXISTING"] ? @(1) : @(0),
  7944. @"value" : @"FOLLOW EXISTING PAYMENT TYPE",
  7945. @"value_id" : @"FOLLOW EXISTING"
  7946. }
  7947. },
  7948. @"control" : @"enum",
  7949. @"name" : @"paymentType",
  7950. @"single_select" : @"true"
  7951. },
  7952. @"title" : @"Payment Information"
  7953. }.mutableCopy;
  7954. NSNumber *hide = setting[@"PaymentInformationHide"];
  7955. [dic setValue:hide forKey:@"hide"];
  7956. return dic;
  7957. }
  7958. + (BOOL)orderOutOfStock:(NSDictionary *)params db:(sqlite3 *)db{
  7959. // params
  7960. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7961. orderCode = [self translateSingleQuote:orderCode];
  7962. // 缺货检查
  7963. NSString *out_of_stock_SQL = [NSString stringWithFormat:@"select p.availability,c.item_count from product as p left join offline_cart as c on p.product_id = c.product_id where c.so_no = '%@';",orderCode];
  7964. __block BOOL outOfStock = NO;
  7965. sqlite3 *database = db;
  7966. if (!db) {
  7967. database = [iSalesDB get_db];
  7968. }
  7969. [iSalesDB jk_query:out_of_stock_SQL db:database close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  7970. int availability = sqlite3_column_int(stmt, 0);
  7971. int item_qty = sqlite3_column_int(stmt, 1);
  7972. outOfStock = outOfStock || (availability < item_qty ? YES : NO);
  7973. }];
  7974. if (!db) {
  7975. [iSalesDB close_db:database];
  7976. }
  7977. return outOfStock;
  7978. }
  7979. +(NSData*) offline_placeorder :(NSMutableDictionary *) params
  7980. {
  7981. sqlite3 *db = [iSalesDB get_db];
  7982. // params
  7983. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  7984. orderCode = [self translateSingleQuote:orderCode];
  7985. // 缺货检查
  7986. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  7987. if (!appDelegate.can_create_backorder) {
  7988. BOOL out_of_stock = [self orderOutOfStock:params db:db];
  7989. if (out_of_stock) {
  7990. [iSalesDB close_db:db];
  7991. NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
  7992. [resultDic setObject:@"some item(s) are out of stock,please remove them from cart before placing order with NPD or try again after sync" forKey:@"err_msg"];
  7993. [resultDic setObject:[NSNumber numberWithInt:8] forKey:@"result"];
  7994. return [RAUtils dict2data:resultDic];
  7995. }
  7996. }
  7997. // UISetting
  7998. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  7999. NSString *cachefolder = [paths objectAtIndex:0];
  8000. NSString *img_cache = [cachefolder stringByAppendingPathComponent:@"offline_data/UISetting.json"];
  8001. NSData* json =nil;
  8002. json=[NSData dataWithContentsOfFile:img_cache];
  8003. NSError *error=nil;
  8004. NSMutableDictionary* menu = [[NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableLeaves error:&error] mutableCopy];
  8005. NSDictionary *setting = [menu objectForKey:@"placeOrder"];
  8006. [params setObject:setting forKey:@"setting"];
  8007. int section_count = 0;
  8008. // 0 Order Type 1 Shipping Method 2 Payment Information
  8009. __block NSMutableDictionary *ret = [self dictionaryFileName:@"placeOrderTemplate.json"];
  8010. // 0 Order Type
  8011. NSString *key0 = [NSString stringWithFormat:@"section_%d",section_count++];
  8012. NSDictionary *order_type_dic = [self submitAsDic:params db:db];
  8013. [ret setValue:order_type_dic forKey:key0];
  8014. // 1 Shipping Method
  8015. NSString *key1 = [NSString stringWithFormat:@"section_%d",section_count++];
  8016. NSMutableDictionary *shipping_method_dic = [[self shippingMethodDic:params db:db] mutableCopy];
  8017. [ret setValue:shipping_method_dic forKey:key1];
  8018. // 2 Payment Information
  8019. NSString *key2 = [NSString stringWithFormat:@"section_%d",section_count++];
  8020. NSDictionary *payment_info_dic = [self paymentInfoDic:params db:db];
  8021. [ret setValue:payment_info_dic forKey:key2];
  8022. // 3 Customer
  8023. NSString *key3 = [NSString stringWithFormat:@"section_%d",section_count++];
  8024. NSDictionary *customer_dic = [self customerDic:params db:db];
  8025. [ret setValue:customer_dic forKey:key3];
  8026. // 4 Ship To
  8027. NSString *key4 = [NSString stringWithFormat:@"section_%d",section_count++];
  8028. NSDictionary *ship_to_dic = [self shipToDic:params db:db];
  8029. [ret setValue:ship_to_dic forKey:key4];
  8030. // 5 Ship From
  8031. NSNumber *shipFromDisable = setting[@"ShipFromDisabled"];
  8032. if (![shipFromDisable integerValue]) {
  8033. NSString *key5 = [NSString stringWithFormat:@"section_%d",section_count++];
  8034. NSDictionary *ship_from_dic = [self shipFromDic:params db:db];
  8035. [ret setValue:ship_from_dic forKey:key5];
  8036. }
  8037. // 6 Freight Bill To
  8038. NSNumber *freightBillToDisable = setting[@"FreightBillToDisabled"];
  8039. if (![freightBillToDisable integerValue]) {
  8040. NSString *key6 = [NSString stringWithFormat:@"section_%d",section_count++];
  8041. NSDictionary *freight_bill_to = [self freightBillToDic:params db:db];
  8042. [ret setValue:freight_bill_to forKey:key6];
  8043. }
  8044. // 7 Merchandise Bill To
  8045. NSNumber *merchandiseBillToDisable = setting[@"MerchandiseBillToDisabled"];
  8046. if (![merchandiseBillToDisable integerValue]) {
  8047. NSString *key7 = [NSString stringWithFormat:@"section_%d",section_count++];
  8048. NSDictionary *merchandise_bill_to_dic = [self merchandiseBillToDic:params db:db];
  8049. [ret setValue:merchandise_bill_to_dic forKey:key7];
  8050. }
  8051. // 8 Return To
  8052. NSNumber *returnToDisable = setting[@"ReturnToDisabled"];
  8053. if (![returnToDisable integerValue]) {
  8054. NSString *key8 = [NSString stringWithFormat:@"section_%d",section_count++];
  8055. NSDictionary *return_to_dic = [self returnToDic:params db:db];
  8056. [ret setValue:return_to_dic forKey:key8];
  8057. }
  8058. // 9 Model Information
  8059. NSString *key9 = [NSString stringWithFormat:@"section_%d",section_count++];
  8060. NSMutableDictionary *model_info_dic = [[self modelInfoDic:params db:db] mutableCopy];
  8061. NSDictionary *toOrderTotal = [model_info_dic valueForKey:@"toOrderTotal"];
  8062. [model_info_dic removeObjectForKey:@"toOrderTotal"];
  8063. [ret setValue:model_info_dic forKey:key9];
  8064. // 10 Remarks Content
  8065. NSString *key10 = [NSString stringWithFormat:@"section_%d",section_count++];
  8066. NSDictionary *remarks_content_dic = [self remarksContentDic:params db:db];
  8067. [ret setValue:remarks_content_dic forKey:key10];
  8068. // 11 Order Total
  8069. NSString *key11 = [NSString stringWithFormat:@"section_%d",section_count++];
  8070. NSDictionary *order_total_dic = [self orderTotalDic:params otherTotal:toOrderTotal db:db];
  8071. [ret setValue:order_total_dic forKey:key11];
  8072. // 12 Signature
  8073. NSString *key12 = [NSString stringWithFormat:@"section_%d",section_count++];
  8074. NSDictionary *sign_dic = [self signatureDic:params db:db];
  8075. [ret setValue:sign_dic forKey:key12];
  8076. [ret setValue:@(section_count) forKey:@"section_count"];
  8077. [iSalesDB close_db:db];
  8078. return [RAUtils dict2data:ret];
  8079. // return nil;
  8080. }
  8081. #pragma mark addr editor
  8082. + (NSMutableDictionary *)anameDealWithStarAtItem:(NSDictionary *)item add:(BOOL)add {
  8083. NSMutableDictionary *new_item = [item mutableCopy];
  8084. [new_item setValue:add ? @"true" : @"false" forKey:@"required"];
  8085. return new_item;
  8086. }
  8087. +(NSData*) offline_addreditor :(NSMutableDictionary *) params
  8088. {
  8089. // "is_subaction" = true;
  8090. // orderCode = MOB1608240002;
  8091. // password = 123456;
  8092. // "subaction_tag" = 1;
  8093. // user = EvanK;
  8094. // {
  8095. // "is_subaction" = true;
  8096. // orderCode = MOB1608240002;
  8097. // password = 123456;
  8098. // "refresh_trigger" = zipcode;
  8099. // "subaction_tag" = 1;
  8100. // user = EvanK;
  8101. // }
  8102. NSString *country_code = nil;
  8103. NSString *zipCode = nil;
  8104. NSString *stateCode = nil;
  8105. NSString *city = nil;
  8106. if ([params.allKeys containsObject:@"refresh_trigger"]) {
  8107. if([params[@"refresh_trigger"] isEqualToString:@"country"]) { // choose country
  8108. NSString *code_id = params[@"country"];
  8109. country_code = [self countryCodeByid:code_id];
  8110. } else if ([params[@"refresh_trigger"] isEqualToString:@"zipcode"]) { // zipcode
  8111. NSString *zip_code = params[@"zipcode"];
  8112. // 剔除全部为空格
  8113. int spaceCount = 0;
  8114. for (int i = 0; i < zip_code.length; i++) {
  8115. if ([zip_code characterAtIndex:i] == ' ') {
  8116. spaceCount++;
  8117. }
  8118. }
  8119. if (spaceCount == zip_code.length) {
  8120. zip_code = @"";
  8121. }
  8122. zipCode = zip_code;
  8123. if (zipCode.length > 0) {
  8124. NSDictionary *dic = [self offline_dealZipCode:zip_code];
  8125. country_code = [dic valueForKey:@"country_code"];
  8126. if (!country_code) {
  8127. // country_code = @"US";
  8128. NSString *code_id = params[@"country"];
  8129. country_code = [self countryCodeByid:code_id];
  8130. }
  8131. stateCode = [dic valueForKey:@"state_code"];
  8132. if(!stateCode.length) {
  8133. stateCode = params[@"state"];
  8134. }
  8135. city = [dic valueForKey:@"city"];
  8136. if (!city.length) {
  8137. city = params[@"city"];
  8138. }
  8139. // zip code
  8140. // NSMutableDictionary *zipDic = [[section_0 valueForKey:@"item_11"] mutableCopy];
  8141. // [zipDic setValue:zipCode forKey:@"value"];
  8142. // [section_0 setValue:zipDic forKey:@"item_11"];
  8143. } else {
  8144. NSString *code_id = params[@"country"];
  8145. country_code = [self countryCodeByid:code_id];
  8146. stateCode = params[@"state"];
  8147. city = params[@"city"];
  8148. }
  8149. }
  8150. }
  8151. NSMutableDictionary * ret = [self dictionaryFileName:@"createContact.json"];
  8152. // [ret removeObjectForKey:@"up_params"];
  8153. [ret setObject:@"New Address" forKey:@"title"];
  8154. NSDictionary * section_0 = [ret objectForKey:@"section_0"];
  8155. NSMutableDictionary *new_section_0 = [NSMutableDictionary dictionary];
  8156. [new_section_0 setObject:[NSNumber numberWithInteger:14] forKey:@"count"];
  8157. NSMutableDictionary *country_dic = [section_0[@"item_6"] mutableCopy];
  8158. // [country_dic removeObjectForKey:@"refresh"];
  8159. // [country_dic removeObjectForKey:@"restore"];
  8160. [new_section_0 setObject:country_dic forKey:@"item_0"]; // Country
  8161. [new_section_0 setObject:section_0[@"item_0"] forKey:@"item_1"]; // company name*
  8162. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_7"] add:YES] forKey:@"item_2"]; // Address 1*
  8163. [new_section_0 setObject:section_0[@"item_8"] forKey:@"item_3"]; // Address 2
  8164. [new_section_0 setObject:section_0[@"item_9"] forKey:@"item_4"]; // Address 3
  8165. [new_section_0 setObject:section_0[@"item_10"] forKey:@"item_5"]; // Address 4
  8166. NSMutableDictionary *zip_code_dic = [self anameDealWithStarAtItem:section_0[@"item_11"] add:YES];
  8167. // [zip_code_dic removeObjectForKey:@"refresh"];
  8168. [zip_code_dic setValue:zipCode ? zipCode :@"" forKey:@"value"];
  8169. [new_section_0 setObject:zip_code_dic forKey:@"item_6"]; // Zip Code*
  8170. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_12"] add:YES] forKey:@"item_7"]; // State/Province*
  8171. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_13"] add:YES] forKey:@"item_8"]; // City*
  8172. NSDictionary *cityDic = [[new_section_0 objectForKey:@"item_8"] mutableCopy];
  8173. [cityDic setValue:city ? city : @"" forKey:@"value"];
  8174. [new_section_0 setObject:cityDic forKey:@"item_8"];
  8175. [new_section_0 setObject:section_0[@"item_14"] forKey:@"item_9"]; // Contact First Name
  8176. [new_section_0 setObject:section_0[@"item_15"] forKey:@"item_10"]; // Contact Last Name
  8177. [new_section_0 setObject:section_0[@"item_5"] forKey:@"item_11"]; // Phone*
  8178. [new_section_0 setObject:section_0[@"item_16"] forKey:@"item_12"]; // Fax
  8179. [new_section_0 setObject:[self anameDealWithStarAtItem:section_0[@"item_4"] add:NO] forKey:@"item_13"]; // Email
  8180. [new_section_0 setObject:section_0[@"title"] forKey:@"title"];
  8181. // country
  8182. NSString *countryCode = country_code == nil ? @"US" : country_code;
  8183. NSDictionary *allCountry = [self offline_getAllCountryDefault:countryCode];
  8184. [self setValue:allCountry forItemKey:@"item_0" valueKey:@"cadedate" inDictionary:new_section_0];
  8185. // state
  8186. NSMutableDictionary *allState = [[self offline_getStateByCountryCode:countryCode checkedState:stateCode] mutableCopy];
  8187. // NSDictionary *tmpDic = @{
  8188. // @"value" : @"Other",
  8189. // @"value_id" : @"",
  8190. // @"check" : [NSNumber numberWithInteger:0]
  8191. // };
  8192. //
  8193. // for (int i = 0; i < allState.allKeys.count; i++) {
  8194. // NSString *key = [NSString stringWithFormat:@"val_%d",i];
  8195. //
  8196. // NSDictionary *tmp = allState[key];
  8197. // [allState setValue:tmpDic forKey:key];
  8198. // tmpDic = tmp;
  8199. // }
  8200. [allState setValue:[NSNumber numberWithInteger:allState.allKeys.count - 1] forKey:@"count"];
  8201. [self setValue:allState forItemKey:@"item_7" valueKey:@"cadedate" inDictionary:new_section_0];
  8202. [ret setValue:new_section_0 forKey:@"section_0"];
  8203. return [RAUtils dict2data:ret];
  8204. }
  8205. #pragma mark save addr
  8206. +(NSData*) offline_saveaddr :(NSMutableDictionary *) params
  8207. {
  8208. // NSString *companyName = [self valueInParams:params key:@"company"];
  8209. // NSString *addr1 = [self valueInParams:params key:@"address"];
  8210. // NSString *addr2 = [self valueInParams:params key:@"address2"];
  8211. // NSString *addr3 = [self valueInParams:params key:@"address_3"];
  8212. // NSString *addr4 = [self valueInParams:params key:@"address_4"];
  8213. // NSString *countryId = [self valueInParams:params key:@"country"];
  8214. // NSString *stateCode = [self valueInParams:params key:@"state"];
  8215. // NSString *city = [self valueInParams:params key:@"city"];
  8216. // NSString *zipCode = [self valueInParams:params key:@"zipcode"];
  8217. // NSString *firstName = [self valueInParams:params key:@"firstname"];
  8218. // NSString *lastName = [self valueInParams:params key:@"lastname"];
  8219. // NSString *phone = [self valueInParams:params key:@"phone"];
  8220. // NSString *fax = [self valueInParams:params key:@"fax"];
  8221. // NSString *email = [self valueInParams:params key:@"email"];
  8222. return [self offline_saveContact:params update:NO isCustomer:NO];
  8223. }
  8224. #pragma mark cancel order
  8225. +(NSData*) offline_cancelorder :(NSMutableDictionary *) params
  8226. {
  8227. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8228. NSString *order_id = [self valueInParams:params key:@"orderId"];
  8229. NSString *sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where so_id = '%@';",orderCode];
  8230. if (order_id.length) {
  8231. sql = [NSString stringWithFormat:@"update offline_order set status = 2,erpOrderStatus = 15 where _id = %@;",order_id];
  8232. }
  8233. int ret = [iSalesDB execSql:sql];
  8234. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8235. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8236. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8237. // [dic setValue:@"160409" forKey:@"min_ver"];
  8238. return [RAUtils dict2data:dic];
  8239. }
  8240. #pragma mark sign order
  8241. +(NSData*) offline_signorder :(NSMutableDictionary *) params
  8242. {
  8243. //参考 offline_saveBusinesscard
  8244. DebugLog(@"sign order params: %@",params);
  8245. // orderCode = MOB1608240002;
  8246. // picpath = "16360511-BA55-4D66-9112-EF9DEA4A14F0.png";
  8247. NSString *orderCode = [self valueInParams:params key:@"orderCode"];
  8248. orderCode = [self translateSingleQuote:orderCode];
  8249. NSString *picPath = [self valueInParams:params key:@"picpath"];
  8250. picPath = [self translateSingleQuote:picPath];
  8251. NSString *sql = [NSString stringWithFormat:@"update offline_order set sign_picpath = '%@' where so_id = '%@';",picPath,orderCode];
  8252. int ret = [iSalesDB execSql:sql];
  8253. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8254. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8255. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8256. // [dic setValue:@"160409" forKey:@"min_ver"];
  8257. return [RAUtils dict2data:dic];
  8258. }
  8259. #pragma mark save order
  8260. + (NSString *)valueInParams:(NSDictionary *)params key:(NSString *)key translateSingleQuote:(BOOL)translate {
  8261. NSString *ret = [self valueInParams:params key:key];
  8262. if (translate) {
  8263. ret = [self translateSingleQuote:ret];
  8264. }
  8265. return ret;
  8266. }
  8267. + (NSData *)saveorder:(NSMutableDictionary *)param submit:(BOOL)submit {
  8268. NSString *so_id = [self valueInParams:param key:@"orderCode" translateSingleQuote:YES];
  8269. NSString *general_notes = [self valueInParams:param key:@"comments" translateSingleQuote:YES];
  8270. general_notes = [NSString stringWithFormat:@"general_notes = '%@',",general_notes] ;
  8271. NSString *internal_notes = [self valueInParams:param key:@"internal_notes" translateSingleQuote:YES];
  8272. internal_notes = [NSString stringWithFormat:@"internal_notes = '%@',",internal_notes];
  8273. NSString *poNumber = [self valueInParams:param key:@"poNumber" translateSingleQuote:YES];
  8274. poNumber = [NSString stringWithFormat:@"poNumber = '%@',",poNumber];
  8275. NSString *must_call = [self valueInParams:param key:@"must_call"]; // false
  8276. int must_call_integer = [must_call isEqualToString:@"true"] ? 1 : 0;
  8277. must_call = [NSString stringWithFormat:@"must_call = %d,",must_call_integer];
  8278. NSString *sign_picpath = [self valueInParams:param key:@"sign_picpath" translateSingleQuote:YES];
  8279. sign_picpath = [NSString stringWithFormat:@"sign_picpath = '%@',",sign_picpath];
  8280. NSString *total_price = [self valueInParams:param key:@"totalPrice"];
  8281. if (![total_price isEqualToString:@""]) {
  8282. total_price = [NSString stringWithFormat:@"total_price = %@,",total_price];
  8283. } else {
  8284. total_price = @"";
  8285. }
  8286. NSString *paymentsAndCredits = [self valueInParams:param key:@"paymentsAndCredits"];
  8287. if ([paymentsAndCredits isEqualToString:@""]) {
  8288. paymentsAndCredits = @"";
  8289. } else {
  8290. paymentsAndCredits = [NSString stringWithFormat:@"paymentsAndCredits = %@,",paymentsAndCredits];
  8291. }
  8292. NSString *handling_fee_value = [self valueInParams:param key:@"handling_fee_value"];
  8293. if ([handling_fee_value isEqualToString:@""]) {
  8294. handling_fee_value = @"";
  8295. } else if ([handling_fee_value isEqualToString:@"Shipping To Be Quoted"]) {
  8296. handling_fee_value = @"";
  8297. } else {
  8298. handling_fee_value = [NSString stringWithFormat:@"handling_fee_value = %@,",handling_fee_value];
  8299. }
  8300. NSString *handling_fee_placeholder = handling_fee_value;
  8301. NSString *lift_gate = [self valueInParams:param key:@"lift_gate"]; // false
  8302. int lift_gate_integer = [lift_gate isEqualToString:@"true"] ? 1 : 0;
  8303. lift_gate = [NSString stringWithFormat:@"lift_gate = %d,",lift_gate_integer];
  8304. NSString *lift_gate_value = [self valueInParams:param key:@"lift_gate_value"]; // Shipping To Be Quoted
  8305. NSString *lift_gate_placeholder = @"";
  8306. if ([lift_gate_value isEqualToString:@""]) {
  8307. lift_gate_placeholder = @"";
  8308. } else if ([lift_gate_value isEqualToString:@"Shipping To Be Quoted"]) {
  8309. lift_gate_placeholder = @"";
  8310. } else {
  8311. lift_gate_placeholder = [NSString stringWithFormat:@"lift_gate_value = %@,",lift_gate_value];
  8312. }
  8313. NSString *logist = [self valueInParams:param key:@"logist" translateSingleQuote:YES];
  8314. logist = [NSString stringWithFormat:@"logist = '%@',",logist];
  8315. NSString *logistic_note_text = [self valueInParams:param key:@"logist_note_text" translateSingleQuote:YES];
  8316. NSString *logistic_note = [self valueInParams:param key:@"logistic_note" translateSingleQuote:YES];
  8317. 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];
  8318. // NSString *erpOrderStatus = [self valueInParams:param key:@"erpOrderStatus"];
  8319. NSNumber *erpOrderStatus_number = [param objectForKey:@"erpOrderStatus"];
  8320. NSString *erpOrderStatus = !erpOrderStatus_number ? @"" : [NSString stringWithFormat:@"submit_as = %@,",erpOrderStatus_number]; // submit as
  8321. NSString *paymentType = [self valueInParams:param key:@"paymentType" translateSingleQuote:YES];
  8322. paymentType = [NSString stringWithFormat:@"paymentType = '%@',",paymentType];
  8323. NSString *credit_card_address1 = [self valueInParams:param key:@"credit_card_address1" translateSingleQuote:YES];
  8324. credit_card_address1 = [NSString stringWithFormat:@"credit_card_address1 = '%@',",credit_card_address1];
  8325. NSString *credit_card_address2 = [self valueInParams:param key:@"credit_card_address2" translateSingleQuote:YES];
  8326. credit_card_address2 = [NSString stringWithFormat:@"credit_card_address2 = '%@',",credit_card_address2];
  8327. NSString *credit_card_city = [self valueInParams:param key:@"credit_card_city" translateSingleQuote:YES];
  8328. credit_card_city = [NSString stringWithFormat:@"credit_card_city = '%@',",credit_card_city];
  8329. NSString *credit_card_expiration = [self valueInParams:param key:@"credit_card_expiration" translateSingleQuote:YES];
  8330. NSString *credit_card_first_name = [self valueInParams:param key:@"credit_card_first_name" translateSingleQuote:YES];
  8331. credit_card_first_name = [NSString stringWithFormat:@"credit_card_first_name = '%@',",credit_card_first_name];
  8332. NSString *credit_card_last_name = [self valueInParams:param key:@"credit_card_last_name" translateSingleQuote:YES];
  8333. credit_card_last_name = [NSString stringWithFormat:@"credit_card_last_name = '%@',",credit_card_last_name];
  8334. NSString *credit_card_number = [self valueInParams:param key:@"credit_card_number" translateSingleQuote:YES];
  8335. BOOL number_nil = [credit_card_number isEqualToString:@""];
  8336. if (!number_nil) {
  8337. credit_card_number = [AESCrypt fastencrypt:credit_card_number];
  8338. }
  8339. credit_card_number = [NSString stringWithFormat:@"credit_card_number = '%@',",credit_card_number];
  8340. NSString *credit_card_security_code = [self valueInParams:param key:@"credit_card_security_code" translateSingleQuote:YES];
  8341. BOOL security_code_nil = [credit_card_security_code isEqualToString:@""];
  8342. if (!security_code_nil) {
  8343. credit_card_security_code = [AESCrypt fastencrypt:credit_card_security_code];
  8344. }
  8345. credit_card_security_code = [NSString stringWithFormat:@"credit_card_security_code = '%@',",credit_card_security_code];
  8346. NSString *credit_card_state = [self valueInParams:param key:@"credit_card_state" translateSingleQuote:YES];
  8347. credit_card_state = [NSString stringWithFormat:@"credit_card_state = '%@',",credit_card_state];
  8348. NSNumber *card_type_number = [param objectForKey:@"credit_card_type"];
  8349. NSString *credit_card_type = [NSString stringWithFormat:@"credit_card_type = '%@',",[card_type_number integerValue] == 0 ? @"VISA" : @"MASTER CARD"];
  8350. NSString *credit_card_zipcode = [self valueInParams:param key:@"credit_card_zipcode" translateSingleQuote:YES];
  8351. credit_card_zipcode = [NSString stringWithFormat:@"credit_card_zipcode = '%@',",credit_card_zipcode];
  8352. NSString *credit_card_expiration_year = [[credit_card_expiration componentsSeparatedByString:@"/"] lastObject];
  8353. credit_card_expiration_year = [AESCrypt fastencrypt:credit_card_expiration_year];
  8354. credit_card_expiration_year = [NSString stringWithFormat:@"credit_card_expiration_year = '%@',",credit_card_expiration_year];
  8355. NSString *credit_card_expiration_month = [[credit_card_expiration componentsSeparatedByString:@"/"] firstObject];
  8356. credit_card_expiration_month = [AESCrypt fastencrypt:credit_card_expiration_month];
  8357. credit_card_expiration_month = [NSString stringWithFormat:@"credit_card_expiration_month = '%@',",credit_card_expiration_month];
  8358. NSString *customer_cid = [self valueInParams:param key:@"customer_cid" translateSingleQuote:YES];
  8359. NSString *contact_id = customer_cid;
  8360. customer_cid = [NSString stringWithFormat:@"customer_cid = '%@',",customer_cid];
  8361. NSString *customer_contact = [self valueInParams:param key:@"customer_contact" translateSingleQuote:YES];
  8362. NSString *contact_contact = [NSString stringWithFormat:@"contact_name = '%@',",customer_contact];
  8363. customer_contact = [NSString stringWithFormat:@"customer_contact = '%@',",customer_contact];
  8364. NSString *customer_email = [self valueInParams:param key:@"customer_email" translateSingleQuote:YES];
  8365. NSString *contact_email = [NSString stringWithFormat:@"email = '%@',",customer_email];
  8366. customer_email = [NSString stringWithFormat:@"customer_email = '%@',",customer_email];
  8367. NSString *customer_phone = [self valueInParams:param key:@"customer_phone" translateSingleQuote:YES];
  8368. NSString *contact_phone = [NSString stringWithFormat:@"phone = '%@',",[AESCrypt fastencrypt:customer_phone]];
  8369. customer_phone = [NSString stringWithFormat:@"customer_phone = '%@',",customer_phone];
  8370. NSString *customer_fax = [self valueInParams:param key:@"customer_fax" translateSingleQuote:YES];
  8371. NSString *contact_fax = [NSString stringWithFormat:@"fax = '%@',",customer_fax];
  8372. customer_fax = [NSString stringWithFormat:@"customer_fax = '%@',",customer_fax];
  8373. //
  8374. NSString *customer_name = [self valueInParams:param key:@"customer_name" translateSingleQuote:YES];
  8375. customer_name = [NSString stringWithFormat:@"company_name = '%@',",[AESCrypt fastencrypt:customer_name]];
  8376. NSString *customer_address1 = [self valueInParams:param key:@"customer_address1" translateSingleQuote:YES];
  8377. customer_address1 = [NSString stringWithFormat:@"addr_1 = '%@',",[AESCrypt fastencrypt:customer_address1]];
  8378. NSString *customer_address2 = [self valueInParams:param key:@"customer_address2" translateSingleQuote:YES];
  8379. customer_address2 = [NSString stringWithFormat:@"addr_2 = '%@',",customer_address2];
  8380. NSString *customer_address3 = [self valueInParams:param key:@"customer_address3" translateSingleQuote:YES];
  8381. customer_address3 = [NSString stringWithFormat:@"addr_3 = '%@',",customer_address3];
  8382. NSString *customer_address4 = [self valueInParams:param key:@"customer_address4" translateSingleQuote:YES];
  8383. customer_address4 = [NSString stringWithFormat:@"addr_4 = '%@',",customer_address4];
  8384. NSString *customer_country = [self valueInParams:param key:@"customer_country" translateSingleQuote:YES];
  8385. customer_country = [NSString stringWithFormat:@"country = '%@',",customer_country];
  8386. NSString *customer_state = [self valueInParams:param key:@"customer_state" translateSingleQuote:YES];
  8387. customer_state = [NSString stringWithFormat:@"state = '%@',",customer_state];
  8388. NSString *customer_city = [self valueInParams:param key:@"customer_city" translateSingleQuote:YES];
  8389. customer_city = [NSString stringWithFormat:@"city = '%@',",customer_city];
  8390. NSString *customer_zipcode = [self valueInParams:param key:@"customer_zipcode" translateSingleQuote:YES];
  8391. customer_zipcode = [NSString stringWithFormat:@"zipcode = '%@'",customer_zipcode];
  8392. 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];
  8393. NSString *receive_cid = [self valueInParams:param key:@"receive_cid" translateSingleQuote:YES];
  8394. if (receive_cid.length) {
  8395. receive_cid = [NSString stringWithFormat:@"receive_cid = '%@',",receive_cid];
  8396. }
  8397. NSString *receive_name = [self valueInParams:param key:@"receive_name" translateSingleQuote:YES];
  8398. if (receive_name.length) {
  8399. receive_name = [NSString stringWithFormat:@"receive_name = '%@',",receive_name];
  8400. }
  8401. NSString *receive_ext = [self valueInParams:param key:@"receive_ext" translateSingleQuote:YES];
  8402. if (receive_ext.length) {
  8403. receive_ext = [NSString stringWithFormat:@"receive_ext = '%@',",receive_ext];
  8404. }
  8405. NSString *receive_contact = [self valueInParams:param key:@"receive_contact" translateSingleQuote:YES];
  8406. if (receive_contact.length) {
  8407. receive_contact = [NSString stringWithFormat:@"receive_contact = '%@',",receive_contact];
  8408. }
  8409. NSString *receive_phone = [self valueInParams:param key:@"receive_phone" translateSingleQuote:YES];
  8410. if (receive_phone.length) {
  8411. receive_phone = [NSString stringWithFormat:@"receive_phone = '%@',",receive_phone];
  8412. }
  8413. NSString *receive_email = [self valueInParams:param key:@"receive_email" translateSingleQuote:YES];
  8414. if (receive_email.length) {
  8415. receive_email = [NSString stringWithFormat:@"receive_email = '%@',",receive_email];
  8416. }
  8417. NSString *receive_fax = [self valueInParams:param key:@"receive_fax" translateSingleQuote:YES];
  8418. if (receive_fax.length) {
  8419. receive_fax = [NSString stringWithFormat:@"receive_fax = '%@',",receive_fax];
  8420. }
  8421. NSString *sender_cid = [self valueInParams:param key:@"sender_cid" translateSingleQuote:YES];
  8422. if (sender_cid.length) {
  8423. sender_cid = [NSString stringWithFormat:@"sender_cid = '%@',",sender_cid];
  8424. }
  8425. NSString *sender_name = [self valueInParams:param key:@"sender_name" translateSingleQuote:YES];
  8426. if (sender_name.length) {
  8427. sender_name = [NSString stringWithFormat:@"sender_name = '%@',",sender_name];
  8428. }
  8429. NSString *sender_ext = [self valueInParams:param key:@"sender_ext" translateSingleQuote:YES];
  8430. if (sender_ext.length) {
  8431. sender_ext = [NSString stringWithFormat:@"sender_ext = '%@',",sender_ext];
  8432. }
  8433. NSString *sender_contact = [self valueInParams:param key:@"sender_contact" translateSingleQuote:YES];
  8434. if(sender_contact.length) {
  8435. sender_contact = [NSString stringWithFormat:@"sender_contact = '%@',",sender_contact];
  8436. }
  8437. NSString *sender_phone = [self valueInParams:param key:@"sender_phone" translateSingleQuote:YES];
  8438. if (sender_phone.length) {
  8439. sender_phone = [NSString stringWithFormat:@"sender_phone = '%@',",sender_phone];
  8440. }
  8441. NSString *sender_fax = [self valueInParams:param key:@"sender_fax" translateSingleQuote:YES];
  8442. if (sender_fax.length) {
  8443. sender_fax = [NSString stringWithFormat:@"sender_fax = '%@',",sender_fax];
  8444. }
  8445. NSString *sender_email = [self valueInParams:param key:@"sender_email" translateSingleQuote:YES];
  8446. if (sender_email.length) {
  8447. sender_email = [NSString stringWithFormat:@"sender_email = '%@',",sender_email];
  8448. }
  8449. NSString *shipping_billto_cid = [self valueInParams:param key:@"shipping_billto_cid" translateSingleQuote:YES];
  8450. if (shipping_billto_cid.length) {
  8451. shipping_billto_cid = [NSString stringWithFormat:@"shipping_billto_cid = '%@',",shipping_billto_cid];
  8452. }
  8453. NSString *shipping_billto_name = [self valueInParams:param key:@"shipping_billto_name" translateSingleQuote:YES];
  8454. if (shipping_billto_name.length) {
  8455. shipping_billto_name = [NSString stringWithFormat:@"shipping_billto_name = '%@',",shipping_billto_name];
  8456. }
  8457. NSString *shipping_billto_ext = [self valueInParams:param key:@"shipping_billto_ext" translateSingleQuote:YES];
  8458. if (shipping_billto_ext.length) {
  8459. shipping_billto_ext = [NSString stringWithFormat:@"shipping_billto_ext = '%@',",shipping_billto_ext];
  8460. }
  8461. NSString *shipping_billto_contact = [self valueInParams:param key:@"shipping_billto_contact" translateSingleQuote:YES];
  8462. if (shipping_billto_contact.length) {
  8463. shipping_billto_contact = [NSString stringWithFormat:@"shipping_billto_contact = '%@',",shipping_billto_contact];
  8464. }
  8465. NSString *shipping_billto_phone = [self valueInParams:param key:@"shipping_billto_phone" translateSingleQuote:YES];
  8466. if (shipping_billto_phone.length) {
  8467. shipping_billto_phone = [NSString stringWithFormat:@"shipping_billto_phone = '%@',",shipping_billto_phone];
  8468. }
  8469. NSString *shipping_billto_fax = [self valueInParams:param key:@"shipping_billto_fax" translateSingleQuote:YES];
  8470. if (shipping_billto_fax.length) {
  8471. shipping_billto_fax = [NSString stringWithFormat:@"shipping_billto_fax = '%@',",shipping_billto_fax];
  8472. }
  8473. NSString *shipping_billto_email = [self valueInParams:param key:@"shipping_billto_email" translateSingleQuote:YES];
  8474. if (shipping_billto_email.length) {
  8475. shipping_billto_email = [NSString stringWithFormat:@"shipping_billto_email = '%@',",shipping_billto_email];
  8476. }
  8477. NSString *billing_cid = [self valueInParams:param key:@"billing_cid" translateSingleQuote:YES];
  8478. if (billing_cid.length) {
  8479. billing_cid = [NSString stringWithFormat:@"billing_cid = '%@',",billing_cid];
  8480. }
  8481. NSString *billing_name = [self valueInParams:param key:@"billing_name" translateSingleQuote:YES];
  8482. if (billing_name.length) {
  8483. billing_name = [NSString stringWithFormat:@"billing_name = '%@',",billing_name];
  8484. }
  8485. NSString *billing_ext = [self valueInParams:param key:@"billing_ext" translateSingleQuote:YES];
  8486. if (billing_ext.length) {
  8487. billing_ext = [NSString stringWithFormat:@"billing_ext = '%@',",billing_ext];
  8488. }
  8489. NSString *billing_contact = [self valueInParams:param key:@"billing_contact" translateSingleQuote:YES];
  8490. if (billing_contact.length) {
  8491. billing_contact = [NSString stringWithFormat:@"billing_contact = '%@',",billing_contact];
  8492. }
  8493. NSString *billing_phone = [self valueInParams:param key:@"billing_phone" translateSingleQuote:YES];
  8494. if (billing_phone.length) {
  8495. billing_phone = [NSString stringWithFormat:@"billing_phone = '%@',",billing_phone];
  8496. }
  8497. NSString *billing_fax = [self valueInParams:param key:@"billing_fax" translateSingleQuote:YES];
  8498. if (billing_fax.length) {
  8499. billing_fax = [NSString stringWithFormat:@"billing_fax = '%@',",billing_fax];
  8500. }
  8501. NSString *billing_email = [self valueInParams:param key:@"billing_email" translateSingleQuote:YES];
  8502. if (billing_email.length) {
  8503. billing_email = [NSString stringWithFormat:@"billing_email = '%@',",billing_email];
  8504. }
  8505. NSString *returnto_cid = [self valueInParams:param key:@"returnto_cid" translateSingleQuote:YES];
  8506. if (returnto_cid.length) {
  8507. returnto_cid = [NSString stringWithFormat:@"returnto_cid = '%@',",returnto_cid];
  8508. }
  8509. NSString *returnto_name = [self valueInParams:param key:@"returnto_name" translateSingleQuote:YES];
  8510. if (returnto_name.length) {
  8511. returnto_name = [NSString stringWithFormat:@"returnto_name = '%@',",returnto_name];
  8512. }
  8513. NSString *returnto_ext = [self valueInParams:param key:@"returnto_ext" translateSingleQuote:YES];
  8514. if (returnto_ext.length) {
  8515. returnto_ext = [NSString stringWithFormat:@"returnto_ext = '%@',",returnto_ext];
  8516. }
  8517. NSString *returnto_contact = [self valueInParams:param key:@"returnto_contact" translateSingleQuote:YES];
  8518. if (returnto_contact.length) {
  8519. returnto_contact = [NSString stringWithFormat:@"returnto_contact = '%@',",returnto_contact];
  8520. }
  8521. NSString *returnto_phone = [self valueInParams:param key:@"returnto_phone" translateSingleQuote:YES];
  8522. if (returnto_phone.length) {
  8523. returnto_phone = [NSString stringWithFormat:@"returnto_phone = '%@',",returnto_phone];
  8524. }
  8525. NSString *returnto_fax = [self valueInParams:param key:@"returnto_fax" translateSingleQuote:YES];
  8526. if (returnto_fax.length) {
  8527. returnto_fax = [NSString stringWithFormat:@"returnto_fax = '%@',",returnto_fax];
  8528. }
  8529. NSString *returnto_email = [self valueInParams:param key:@"returnto_email" translateSingleQuote:YES];
  8530. if (returnto_email.length) {
  8531. returnto_email = [NSString stringWithFormat:@"returnto_email = '%@',",returnto_email];
  8532. }
  8533. NSString *order_status = @"status = 1,";
  8534. if (submit) {
  8535. order_status = @"status = -11,";
  8536. }
  8537. // NSString *sync_data = [AESCrypt fastencrypt:[RAUtils dict2string:param]];
  8538. NSString *sync_sql = @"";
  8539. if (submit) {
  8540. NSString *sales_rep = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select sales_rep from offline_order where so_id = '%@';",so_id]];
  8541. [param setValue:sales_rep forKey:@"sales_rep"];
  8542. NSString *sync_data = [self translateSingleQuote:[RAUtils dict2string:param]];
  8543. sync_sql = [NSString stringWithFormat:@",sync_data = '%@' ",sync_data];
  8544. }
  8545. NSString *orderSql = [NSString stringWithFormat:@"update offline_order set %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ %@ so_id = '%@' %@ where so_id = '%@';",order_status,general_notes,internal_notes,poNumber,must_call,sign_picpath,total_price,paymentsAndCredits,handling_fee_placeholder,lift_gate,lift_gate_placeholder,logist,logistic_note,erpOrderStatus,paymentType,credit_card_address1,credit_card_address2,credit_card_city,credit_card_first_name,credit_card_last_name,credit_card_number,credit_card_security_code,credit_card_state,credit_card_type,credit_card_zipcode,credit_card_expiration_year,credit_card_expiration_month,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_email,receive_fax,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,so_id,sync_sql,so_id];
  8546. DebugLog(@"save order contactSql: %@",contactSql);
  8547. DebugLog(@"save order orderSql: %@",orderSql);
  8548. // int contact_ret = [iSalesDB execSql:contactSql];
  8549. int order_ret = [iSalesDB execSql:orderSql];
  8550. int ret = order_ret;
  8551. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8552. [dic setValue:[NSNumber numberWithInteger:ret] forKey:@"result"];
  8553. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8554. // [dic setValue:@"160409" forKey:@"min_ver"];
  8555. [dic setObject:so_id forKey:@"so#"];
  8556. return [RAUtils dict2data:dic];
  8557. }
  8558. +(NSData*) offline_saveorder :(NSMutableDictionary *) param
  8559. {
  8560. // id foo = nil;
  8561. // NSMutableArray *a = @[].mutableCopy;
  8562. // [a addObject:foo];
  8563. return [self saveorder:param submit:NO];
  8564. }
  8565. #pragma mark add to cart by name
  8566. +(NSData*) offline_add2cartbyname :(NSMutableDictionary *) params
  8567. {
  8568. NSString *orderCode = [params objectForKey:@"orderCode"];
  8569. NSString *product_name = [params objectForKey:@"product_name"];
  8570. product_name = [self translateSingleQuote:product_name];
  8571. NSArray *product_name_array = [product_name componentsSeparatedByString:@","];
  8572. sqlite3 *db = [iSalesDB get_db];
  8573. __block NSMutableString *product_id_string = [NSMutableString string];
  8574. for (int i = 0; i < product_name_array.count; i++) {
  8575. NSString *name = [product_name_array objectAtIndex:i];
  8576. NSString *sql = [NSString stringWithFormat:@"select product_id from product where name = '%@';",name];
  8577. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8578. int product_id = sqlite3_column_int(stmt, 0);
  8579. if (i == product_name_array.count - 1) {
  8580. [product_id_string appendFormat:@"%d",product_id];
  8581. } else {
  8582. [product_id_string appendFormat:@"%d,",product_id];
  8583. }
  8584. }];
  8585. }
  8586. NSDictionary *newParams = @{
  8587. @"product_id" : product_id_string,
  8588. @"orderCode" : orderCode
  8589. };
  8590. [iSalesDB close_db:db];
  8591. return [self offline_add2cart:[newParams mutableCopy]];
  8592. }
  8593. #pragma mark reset order
  8594. +(NSData*) offline_resetorder :(NSMutableDictionary *) params
  8595. {
  8596. UIApplication * app = [UIApplication sharedApplication];
  8597. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  8598. appDelegate.disable_trigger=true;
  8599. [iSalesDB execSql:@"delete from offline_cart where so_no in (select so_id from offline_order where status = 0);"];
  8600. [iSalesDB execSql:@"delete from offline_order where status=0;"];
  8601. appDelegate.disable_trigger=false;
  8602. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8603. [dic setValue:[NSNumber numberWithInteger:2] forKey:@"result"];
  8604. return [RAUtils dict2data:dic];
  8605. }
  8606. #pragma mark submit order
  8607. +(NSData*) offline_submitorder :(NSMutableDictionary *) params;
  8608. {
  8609. return [self saveorder:params submit:YES];
  8610. }
  8611. #pragma mark copy order
  8612. +(NSData*) offline_copyorder :(NSMutableDictionary *) params;
  8613. {
  8614. NSMutableDictionary *ret = @{}.mutableCopy;
  8615. NSString *order_code = [self valueInParams:params key:@"code" translateSingleQuote:YES];
  8616. sqlite3 *db = [iSalesDB get_db];
  8617. [iSalesDB execSql:@"BEGIN TRANSACTION" db:db];
  8618. // 首先查看联系人是否active
  8619. NSString *customer_sql = [NSString stringWithFormat:@"select is_active from offline_contact where contact_id in (select customer_cid from offline_order where so_id = '%@');",order_code];
  8620. __block int customer_is_active = 1;
  8621. [iSalesDB jk_query:customer_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8622. customer_is_active = sqlite3_column_int(stmt, 0);
  8623. }];
  8624. if (!customer_is_active) {
  8625. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8626. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8627. [iSalesDB close_db:db];
  8628. return [RAUtils dict2data:ret];
  8629. }
  8630. // new order
  8631. // NSString *new_order_code = [NSString stringWithFormat:@"OFFLINE%@",[NSUUID UUID].UUIDString];
  8632. NSString *new_order_code = [self get_offline_soid:db];
  8633. NSString *user = ((AppDelegate *)[UIApplication sharedApplication].delegate).user;
  8634. user = [self translateSingleQuote:user];
  8635. NSString *insert_order_sql = [NSString stringWithFormat:@"insert into offline_order (so_id,status,submit_as,internal_notes,sales_rep,create_by,logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext,sales_terms) select '%@',1,submit_as,internal_notes,sales_rep,'%@',logist,lift_gate,logistic_note,customer_cid,customer_contact,customer_email,customer_phone,customer_fax,receive_cid,receive_name,receive_ext,receive_contact,receive_phone,receive_fax,receive_email,sender_cid,sender_name,sender_ext,sender_contact,sender_phone,sender_fax,sender_email,shipping_billto_cid,shipping_billto_name,shipping_billto_ext,shipping_billto_contact,shipping_billto_phone,shipping_billto_fax,shipping_billto_email,billing_cid,billing_name,billing_ext,billing_contact,billing_phone,billing_fax,billing_email,returnto_cid,returnto_name,returnto_ext,returnto_contact,returnto_phone,returnto_fax,returnto_email,must_call,poNumber,comments,comments_ext,sales_terms from offline_order where so_id = '%@';",new_order_code,user,order_code]; // ,paymentsAndCredits,shipping,lift_gate_value,handling_fee_value
  8636. __block int result = 1;
  8637. result = [iSalesDB execSql:insert_order_sql db:db];
  8638. if (!result) {
  8639. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8640. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8641. [iSalesDB close_db:db];
  8642. return [RAUtils dict2data:ret];
  8643. }
  8644. // 查询product_id 及其对应的 is_active,若is_active = 0,那么就忽略之
  8645. // __block NSString *product_id = @"";
  8646. __weak typeof(self) weakSelf = self;
  8647. NSString *product_id_sql = [NSString stringWithFormat:@"select product_id,is_active from product WHERE product_id in (select product_id from offline_cart where so_no = '%@');",order_code];
  8648. [iSalesDB jk_query:product_id_sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8649. int is_active = sqlite3_column_int(stmt, 1);
  8650. if (is_active) {
  8651. // product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%@",[weakSelf textAtColumn:0 statement:stmt]]];
  8652. NSString *product_id = [weakSelf textAtColumn:0 statement:stmt];
  8653. NSString *insert_cart_sql = [NSString stringWithFormat:@"insert into offline_cart (so_no,orderitem_id,product_id,item_id,item_count,bundle_item,type) select '%@',orderitem_id,product_id,item_id,item_count,bundle_item,type from offline_cart where product_id = %@ and so_no = '%@'",new_order_code,product_id,order_code];
  8654. result = result && [iSalesDB execSql:insert_cart_sql db:db];
  8655. }
  8656. }];
  8657. // product_id = [product_id substringFromIndex:1];
  8658. //
  8659. // [self offline_add2cart:@{}.mutableCopy];
  8660. [iSalesDB execSql:@"END TRANSACTION" db:db];
  8661. [iSalesDB close_db:db];
  8662. if (result) {
  8663. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8664. } else {
  8665. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8666. }
  8667. [ret setObject:new_order_code forKey:@"so_id"];
  8668. return [RAUtils dict2data:ret];
  8669. }
  8670. #pragma mark move wish list to cart
  8671. + (NSData*)offline_movewish2cart:(NSMutableDictionary *)params {
  8672. // cart中存在的Model在wish list move to cart之后,cart中Model数量为1
  8673. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8674. NSString *collectId = params[@"collectId"];
  8675. NSString *sql = [NSString stringWithFormat:@"select w.product_id,w.qty,p.availability,w._id from wishlist as w left join product as p on w.product_id = p.product_id where w._id in (%@);",collectId];
  8676. __block NSString *product_id = @"";
  8677. __block NSString *qty = @"";
  8678. __block int number_of_outOfStock = 0;
  8679. __block NSMutableArray *delete_collectId = [NSMutableArray array];
  8680. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8681. int productId = sqlite3_column_int(stmt, 0);
  8682. int item_qty = sqlite3_column_int(stmt, 1);
  8683. int availability = sqlite3_column_int(stmt, 2);
  8684. int _id = sqlite3_column_int(stmt, 3);
  8685. if (!appDelegate.can_create_backorder) {
  8686. // 库存小于购买量为缺货
  8687. if (availability >= item_qty) {
  8688. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8689. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8690. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  8691. } else {
  8692. number_of_outOfStock++;
  8693. }
  8694. } else {
  8695. product_id = [product_id stringByAppendingString:[NSString stringWithFormat:@",%d",productId]];
  8696. qty = [qty stringByAppendingString:[NSString stringWithFormat:@",%d",item_qty]];
  8697. [delete_collectId addObject:[NSString stringWithFormat:@"%d",_id]];
  8698. }
  8699. }];
  8700. NSMutableDictionary *retDic = nil;
  8701. if (!appDelegate.can_create_backorder) {
  8702. if (delete_collectId.count == 0) {
  8703. retDic = [NSMutableDictionary dictionary];
  8704. retDic[@"result"] = [NSNumber numberWithInteger:8];
  8705. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  8706. return [RAUtils dict2data:retDic];
  8707. }
  8708. }
  8709. if (product_id.length > 1) {
  8710. product_id = [product_id substringFromIndex:1];
  8711. }
  8712. if (qty.length > 1) {
  8713. qty = [qty substringFromIndex:1];
  8714. }
  8715. NSDictionary *newParams = @{
  8716. @"product_id" : product_id,
  8717. @"orderCode" : params[@"orderCode"],
  8718. @"qty" : qty
  8719. };
  8720. NSData *data = [self offline_add2cart:newParams.mutableCopy];
  8721. retDic = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
  8722. if ([retDic[@"result"] integerValue] == RESULT_TRUE) {
  8723. NSInteger ret = [[[self offline_deletewishlist:@{@"collectId" : [delete_collectId componentsJoinedByString:@","]}.mutableCopy] valueForKey:@"result"] integerValue];
  8724. retDic[@"result"] = [NSNumber numberWithInteger:ret];
  8725. }
  8726. if (!appDelegate.can_create_backorder) {
  8727. if (number_of_outOfStock > 0) {
  8728. retDic[@"result"] = [NSNumber numberWithInteger:8];
  8729. retDic[@"err_msg"] = [NSString stringWithFormat:@"%d item is out of stock",number_of_outOfStock];
  8730. }
  8731. }
  8732. return [RAUtils dict2data:retDic];
  8733. }
  8734. #pragma mark - portfolio
  8735. + (NSData *)offline_portfolioList:(NSMutableDictionary *)params {
  8736. int sort = [[params valueForKey:@"sort"] intValue];
  8737. int offset = [[params valueForKey:@"offset"] intValue];
  8738. int limit = [[params valueForKey:@"limit"] intValue];
  8739. NSString *orderBy = @"";
  8740. switch (sort) {
  8741. case 0:{
  8742. orderBy = @"p.modify_time desc";
  8743. }
  8744. break;
  8745. case 1:{
  8746. orderBy = @"modify_time asc";
  8747. }
  8748. break;
  8749. case 2:{
  8750. orderBy = @"p.name asc";
  8751. }
  8752. break;
  8753. case 3:{
  8754. orderBy = @"p.name desc";
  8755. }
  8756. break;
  8757. case 4:{
  8758. orderBy = @"p.description asc";
  8759. }
  8760. break;
  8761. case 5: {
  8762. orderBy = @"m.default_category asc";
  8763. }
  8764. default:
  8765. break;
  8766. }
  8767. // NSString *sql = [NSString stringWithFormat:@"select p.product_id,p.name,p.description,ifnull(p.sheet_price,'null'),p.sheet_discount,ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.fashion_id,i.url,p.line_note,p.percent from offline_portfolio as p left join model_image as i on i.product_id = p.product_id where i.type = 1 and (p.is_delete is null or p.is_delete = 0) group by p.product_id order by %@ limit %d offset %d;",orderBy,limit,offset];
  8768. NSString *sql = [NSString stringWithFormat:@"select * from (select p.product_id,p.name,p.description,ifnull(p.sheet_price,'null'),p.sheet_discount,ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.fashion_id,i.url,p.line_note,p.percent,p.modify_time from offline_portfolio as p left join model_image as i on i.product_id = p.product_id where i.type = 1 and (p.is_delete is null or p.is_delete = 0) group by p.product_id limit %d offset %d) as p left join product as m on p.product_id = m.product_id order by %@;",limit,offset,orderBy];
  8769. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8770. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8771. sqlite3 *db = [iSalesDB get_db];
  8772. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8773. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8774. int product_id = sqlite3_column_int(stmt, 0);
  8775. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8776. NSString *name = [self textAtColumn:1 statement:stmt];
  8777. NSString *description = [self textAtColumn:2 statement:stmt];
  8778. double price = sqlite3_column_double(stmt, 3);
  8779. double discount = sqlite3_column_double(stmt,4);
  8780. int qty = sqlite3_column_int(stmt, 5);
  8781. int percentage = sqlite3_column_int(stmt, 6);
  8782. int item_id = sqlite3_column_int(stmt, 7);
  8783. // int fashion_id = sqlite3_column_int(stmt, 8);
  8784. NSString *img_path = [self textAtColumn:9 statement:stmt];
  8785. NSString *line_note = [self textAtColumn:10 statement:stmt];
  8786. double percent = sqlite3_column_double(stmt, 11);
  8787. NSString *price_null = [self textAtColumn:3 statement:stmt];
  8788. if ([price_null isEqualToString:@"null"]) {
  8789. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8790. }
  8791. NSMutableDictionary *item = @{
  8792. @"linenotes": line_note,
  8793. @"check": @(1),
  8794. @"product_id": product_id_string,
  8795. @"available_qty": @(qty),
  8796. @"available_percent" : @(percent),
  8797. @"description": [NSString stringWithFormat:@"%@\n%@",name,description],
  8798. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8799. @"fashion_id": [NSString stringWithFormat:@"%d",product_id],
  8800. @"img": img_path,
  8801. @"tear_sheet_discount": [NSString stringWithFormat:@"%f",discount],
  8802. @"tear_sheet_price": [NSString stringWithFormat:@"%f",price]
  8803. }.mutableCopy;
  8804. if (percentage) {
  8805. [item removeObjectForKey:@"available_qty"];
  8806. } else {
  8807. [item removeObjectForKey:@"available_percent"];
  8808. }
  8809. NSString *qty_null = [self textAtColumn:5 statement:stmt];
  8810. if ([qty_null isEqualToString:@"null"]) {
  8811. [item removeObjectForKey:@"available_qty"];
  8812. }
  8813. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8814. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8815. }];
  8816. if ([[resultDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  8817. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  8818. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  8819. }
  8820. [iSalesDB close_db:db];
  8821. [dic setValue:[NSNumber numberWithBool:appDelegate.can_see_price] forKey:@"can_see_price"];
  8822. [dic setValue:@"" forKey:@"email_content"];
  8823. [dic setValue:[dic objectForKey:@"count"] forKey:@"total_count"];
  8824. [dic setValue:@"Regular Mode" forKey:@"mode"];
  8825. return [RAUtils dict2data:dic];
  8826. }
  8827. + (int)model_QTY:(NSString *)product_id db:(sqlite3 *)db {
  8828. __block int qty = 0;
  8829. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  8830. [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8831. qty = sqlite3_column_int(stmt, 0);
  8832. }];
  8833. return qty;
  8834. }
  8835. + (NSData *)offline_savePDF:(NSMutableDictionary *)params direct:(BOOL)direct {
  8836. NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
  8837. [resultDictionary setObject:@"Regular Mode" forKey:@"mode"];
  8838. NSMutableDictionary * values = params[@"replaceValue"];
  8839. NSNumber *tear_sheet_id = [params objectForKey:@"tearsheetsId"]; // _id
  8840. NSString *tear_name = [self valueInParams:params key:@"pdfName"];
  8841. NSString *tear_note = [self valueInParams:params key:@"pdfNote"];
  8842. NSString *configureParams = [self valueInParams:params key:@"configureParams"];
  8843. NSString *pdf_path = @"";
  8844. if (direct) {
  8845. configureParams = [self valueInParams:params key:@"pdfUrl"];
  8846. } else {
  8847. pdf_path = [self valueInParams:params key:@"pdfPath"];
  8848. }
  8849. NSString *create_user = [self valueInParams:params key:@"user"];
  8850. NSString *product_ids = [self valueInParams:params key:@"product_ids"];
  8851. NSString *item_ids = [self valueInParams:params key:@"item_ids"];
  8852. // model info
  8853. // NSString *sql = [NSString stringWithFormat:@"select product_id,ifnull(sheet_price,'null'),ifnull(sheet_discount,'null'),ifnull(available_qty,'null'),percentage,item_id,line_note,percent from offline_portfolio where product_id in (%@);",product_ids];
  8854. // V1.90 more color
  8855. NSString *sql = [NSString stringWithFormat:@"select p.product_id,ifnull(p.sheet_price,'null'),ifnull(p.sheet_discount,'null'),ifnull(p.available_qty,'null'),p.percentage,p.item_id,p.line_note,p.percent,m.more_color from offline_portfolio as p left join product as m on p.product_id = m.product_id where p.product_id in (%@);",product_ids];
  8856. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8857. __block NSMutableString *product_ids_string = [NSMutableString string]; // configure参数
  8858. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  8859. sqlite3 *db = [iSalesDB get_db];
  8860. NSDictionary *resultDic = [iSalesDB jk_query:sql db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  8861. [dic setValue:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  8862. int product_id = sqlite3_column_int(stmt, 0);
  8863. NSString *product_id_string = [NSString stringWithFormat:@"%d",product_id];
  8864. double price = sqlite3_column_double(stmt, 1);
  8865. double discount = sqlite3_column_double(stmt,2);
  8866. int qty = sqlite3_column_int(stmt, 3);
  8867. int percentage = sqlite3_column_int(stmt, 4);
  8868. int item_id = sqlite3_column_int(stmt, 5);
  8869. NSString *line_note = [self textAtColumn:6 statement:stmt];
  8870. double percent = sqlite3_column_double(stmt, 7);
  8871. int more_color = sqlite3_column_int(stmt, 8);
  8872. NSString *price_null = [self textAtColumn:1 statement:stmt];
  8873. /* if ([price_null isEqualToString:@"null"]) {
  8874. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  8875. }
  8876. */
  8877. [product_ids_string appendFormat:@"%@,",product_id_string];
  8878. // Regular Price
  8879. int regular_price = [[params objectForKey:@"regular_price"] integerValue];
  8880. NSString *regular_price_str = [self get_portfolio_price:appDelegate.contact_id item_id:item_id price:regular_price db:db];
  8881. // QTY
  8882. NSString *qty_null = [self textAtColumn:3 statement:stmt];
  8883. if ([qty_null isEqualToString:@"null"]) {
  8884. // 查available
  8885. qty = [self model_QTY:product_id_string db:db];
  8886. }
  8887. if (percentage) {
  8888. qty = qty * percent / 100;
  8889. }
  8890. // Special Price
  8891. if ([price_null isEqualToString:@"null"]) {
  8892. // price = regular price
  8893. price = [regular_price_str doubleValue];
  8894. }
  8895. NSString *discount_null = [self textAtColumn:2 statement:stmt];
  8896. if (![discount_null isEqualToString:@"null"]) {
  8897. price = price * (1 - discount / 100.0);
  8898. }
  8899. NSMutableDictionary *item = @{
  8900. @"line_note": line_note,
  8901. @"product_id": product_id_string,
  8902. @"available_qty": @(qty),
  8903. @"more_color":@(more_color),
  8904. @"item_id": [NSString stringWithFormat:@"%d",item_id],
  8905. @"regular_price" : regular_price_str,
  8906. @"special_price" : [NSString stringWithFormat:@"%.2f",price]
  8907. }.mutableCopy;
  8908. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  8909. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  8910. }];
  8911. [iSalesDB close_db:db];
  8912. if (product_ids_string.length > 0) {
  8913. [product_ids_string deleteCharactersInRange:NSMakeRange(product_ids_string.length - 1, 1)];
  8914. }
  8915. if ([[resultDic objectForKey:@"result"] integerValue] != RESULT_TRUE) {
  8916. [resultDictionary setObject:[resultDic objectForKey:@"result"] forKey:@"result"];
  8917. return [RAUtils dict2data:resultDictionary];
  8918. }
  8919. NSString *model_info = [RAUtils dict2string:dic];
  8920. // 创建PDF
  8921. // 在preview情况下保存,则不需要新建了
  8922. if (direct) {
  8923. NSMutableDictionary *tear_sheet_params = params;
  8924. // if (tear_sheet_id) {
  8925. // tear_sheet_params = values;
  8926. // }
  8927. NSData *pdfData = [self offline_request_tearsheet:tear_sheet_params]; // 新建PDF,获取本地路径
  8928. NSDictionary *pdfInfo = [NSJSONSerialization JSONObjectWithData:pdfData options:NSJSONReadingMutableContainers error:nil];
  8929. pdf_path = [pdfInfo objectForKey:@"pdf_path"];
  8930. if ([[pdfInfo objectForKey:@"result"] integerValue] == RESULT_TRUE) {
  8931. resultDictionary = pdfInfo.mutableCopy;
  8932. } else { // 创建PDF失败
  8933. return pdfData;
  8934. }
  8935. } else {
  8936. // pdf_path 就是本地路径
  8937. [resultDictionary setObject:pdf_path forKey:@"pdf_path"];
  8938. }
  8939. // 将文件移动到PDF Cache文件夹
  8940. NSString *newPath = [pdf_path lastPathComponent];
  8941. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8942. NSString *cachefolder = [paths objectAtIndex:0];
  8943. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  8944. newPath = [pdfFolder stringByAppendingPathComponent:newPath];
  8945. NSFileManager *fileManager = [NSFileManager defaultManager];
  8946. NSError *error = nil;
  8947. [fileManager moveItemAtPath:pdf_path toPath:newPath error:&error];
  8948. if (error) { // 移动文件失败
  8949. [resultDictionary setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  8950. return [RAUtils dict2data:resultDictionary];
  8951. }
  8952. [resultDictionary setObject:newPath forKey:@"pdf_path"];
  8953. pdf_path = [newPath lastPathComponent];
  8954. // 保存信息
  8955. // [params setObject:[params objectForKey:@"product_ids"] forKey:@"productIds"];
  8956. NSString *off_params = [RAUtils dict2string:params];
  8957. // configureParams = [configureParams stringByAppendingFormat:@"productIds=%@",product_ids_string];
  8958. pdf_path = [self translateSingleQuote:pdf_path];
  8959. create_user = [self translateSingleQuote:create_user];
  8960. tear_note = [self translateSingleQuote:tear_note];
  8961. tear_name = [self translateSingleQuote:tear_name];
  8962. model_info = [self translateSingleQuote:model_info];
  8963. configureParams = [self translateSingleQuote:configureParams];
  8964. off_params = [self translateSingleQuote:off_params];
  8965. NSString *save_pdf_sql = [NSString stringWithFormat:@"insert into offline_pdf (pdf_path,create_user,tear_note,tear_name,model_info,configureParams,is_local,off_params) values ('%@','%@','%@','%@','%@','%@',1,'%@');",pdf_path,create_user,tear_note,tear_name,model_info,configureParams,off_params];
  8966. if (tear_sheet_id) {
  8967. int _id = [tear_sheet_id integerValue];
  8968. save_pdf_sql = [NSString stringWithFormat:@"update offline_pdf set pdf_path = '%@' where _id = %d;",pdf_path,_id];
  8969. }
  8970. int result = [iSalesDB execSql:save_pdf_sql];
  8971. [resultDictionary setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  8972. //
  8973. BOOL remove_Item = [[params objectForKey:@"remove_item"] boolValue];
  8974. if (remove_Item) {
  8975. // portfolioId
  8976. [self offline_removePortfolio:@{@"portfolioId" : item_ids}.mutableCopy];
  8977. }
  8978. return [RAUtils dict2data:resultDictionary];
  8979. }
  8980. + (NSData *)offline_direct_save_TearSheet:(NSMutableDictionary *)params {
  8981. return [self offline_savePDF:params direct:YES];
  8982. }
  8983. + (NSData *)offline_save_TearSheet:(NSMutableDictionary *)params {
  8984. return [self offline_savePDF:params direct:NO];
  8985. }
  8986. + (NSData *)offline_pdfList:(NSMutableDictionary *)params {
  8987. int offset = [[params valueForKey:@"offset"] integerValue];
  8988. int limit = [[params valueForKey:@"limit"] integerValue];
  8989. NSString *keyword = [params valueForKey:@"keyWord"];
  8990. NSString *where = @"where is_delete is null or is_delete = 0";
  8991. if (keyword.length) {
  8992. keyword = [self translateSingleQuote:keyword];
  8993. where = [NSString stringWithFormat:@"where (is_delete is null or is_delete = 0 ) and lower(create_user) like '%%%@%%' or lower(tear_note) like '%%%@%%' or lower(tear_name) like '%%%@%%'",keyword.lowercaseString,keyword.lowercaseString,keyword.lowercaseString];
  8994. }
  8995. NSString *sql = [NSString stringWithFormat:@"select tear_name,tear_note,createtime,create_user,pdf_path,_id,model_info,off_params from offline_pdf %@ order by createtime desc limit %d offset %d;",where,limit,offset];// tearsheets_id ---> _id
  8996. __block NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  8997. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  8998. NSString *cachefolder = [paths objectAtIndex:0];
  8999. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9000. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9001. NSString *name = [self textAtColumn:0 statement:stmt];
  9002. NSString *note = [self textAtColumn:1 statement:stmt];
  9003. NSString *time = [self textAtColumn:2 statement:stmt];
  9004. NSString *user = [self textAtColumn:3 statement:stmt];
  9005. NSString *path = [self textAtColumn:4 statement:stmt];
  9006. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9007. NSString *off_params = [self textAtColumn:7 statement:stmt];
  9008. path = [pdfFolder stringByAppendingPathComponent:path];
  9009. BOOL bdir=NO;
  9010. NSFileManager* fileManager = [NSFileManager defaultManager];
  9011. if(! [fileManager fileExistsAtPath:path isDirectory:&bdir]|| bdir)
  9012. {
  9013. //pdf文件不存在
  9014. path=nil;
  9015. }
  9016. time = [self changeDateTimeFormate:time];
  9017. time = [time stringByAppendingString:@" PST"];
  9018. int sheet_id = sqlite3_column_int(stmt, 5);
  9019. NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
  9020. item[@"tearsheetsId"]=@(sheet_id);
  9021. item[@"pdf_path"]=path;
  9022. item[@"create_time"]=time;
  9023. item[@"create_user"]=user;
  9024. item[@"tear_note"]=note;
  9025. item[@"tear_name"]=name;
  9026. item[@"isLocalFile"]=[NSNumber numberWithBool:YES];
  9027. item[@"model_info"]=model_info;
  9028. item[@"off_params"]=off_params;
  9029. [dic setObject:item forKey:[NSString stringWithFormat:@"item_%ld",*count]];
  9030. [dic setValue:[NSNumber numberWithLong:++(*count)] forKey:@"count"];
  9031. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9032. [dic setValue:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9033. [dic setValue:[NSNumber numberWithInteger:0] forKey:@"count"];
  9034. }];
  9035. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9036. return [RAUtils dict2data:dic];
  9037. }
  9038. + (NSData *)offline_add2Portfolio:(NSMutableDictionary *)params {
  9039. NSString *prodct_ids = [params objectForKey:@"product_id"];
  9040. // NSString *user = [params objectForKey:@"user"];
  9041. // NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and (select count(0) from offline_portfolio where product_id in (%@) and (is_delete is null or is_delete = 0)) = 0);",prodct_ids,prodct_ids];
  9042. NSString *sql = [NSString stringWithFormat:@"insert into offline_portfolio (product_id,name,description,item_id) select * from (select p.product_id,p.name,p.description,p.item_id from product as p where p.product_id in (%@) and p.product_id not in (select product_id from offline_portfolio));insert into offline_portfolio (product_id,name,description,item_id,is_delete) select * from (select p.product_id,p.name,p.description,p.item_id,0 from product as p left join offline_portfolio as f on p.product_id = f.product_id where p.product_id in (%@) and p.product_id in (select product_id from offline_portfolio) and f.is_delete = 1);delete from offline_portfolio where (select count(0) from offline_portfolio where product_id in (%@)) > 1 and is_delete = 1;",prodct_ids,prodct_ids,prodct_ids];
  9043. int result = [iSalesDB execSql:sql];
  9044. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9045. [dic setValue:[NSNumber numberWithInteger:result] forKey:@"result"];
  9046. if (result == RESULT_TRUE) {
  9047. /*NSArray *product_id_arr = [prodct_ids componentsSeparatedByString:@","];
  9048. [dic setValue:[NSNumber numberWithInteger:product_id_arr.count] forKey:@"portfolio_count"];*/
  9049. sqlite3 *db = [iSalesDB get_db];
  9050. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"is_delete is null or is_delete = 0"];
  9051. [dic setValue:[NSNumber numberWithInteger:count] forKey:@"portfolio_count"];
  9052. [iSalesDB close_db:db];
  9053. }
  9054. [dic setValue:@"Regular Mode" forKey:@"mode"];
  9055. return [RAUtils dict2data:dic];
  9056. }
  9057. + (NSData *)offline_createTearSheet:(NSMutableDictionary *)params {
  9058. NSMutableDictionary *dic = [self dictionaryFileName:@"TearSheet.json"];
  9059. [dic setObject:[params objectForKey:@"product_ids"] forKey:@"product_ids"];
  9060. [dic setObject:[params objectForKey:@"item_ids"] forKey:@"item_ids"];
  9061. // Regurlar Price
  9062. NSMutableDictionary *section1 = [[dic objectForKey:@"section_1"] mutableCopy];
  9063. NSMutableDictionary *price = [[section1 objectForKey:@"item_2"] mutableCopy];
  9064. __block NSMutableDictionary *cadedate = [NSMutableDictionary dictionary];
  9065. __block long val_count = 0;
  9066. NSString *sql = @"select name,type,order_by from price order by order_by";
  9067. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9068. NSString *name = [self textAtColumn:0 statement:stmt];
  9069. int type = sqlite3_column_int(stmt, 1);
  9070. int order_by = sqlite3_column_int(stmt, 2);
  9071. // AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9072. NSDictionary *price_dic = @{
  9073. @"value" : name,
  9074. @"value_id" : [NSNumber numberWithInteger:type],
  9075. @"check" : order_by == 0 ? @(1) : @(0)
  9076. };
  9077. [cadedate setObject:price_dic forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9078. val_count = ++(*count);
  9079. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9080. val_count = 0;
  9081. }];
  9082. [cadedate setObject:@{@"value" : @"None",
  9083. @"value_id" : @(0)} forKey:[NSString stringWithFormat:@"val_%ld",val_count]];
  9084. val_count++;
  9085. [cadedate setObject:[NSNumber numberWithInteger:val_count] forKey:@"count"];
  9086. [price setObject:cadedate forKey:@"cadedate"];
  9087. [section1 setObject:price forKey:@"item_2"];
  9088. [dic setObject:section1 forKey:@"section_1"];
  9089. return [RAUtils dict2data:dic];
  9090. }
  9091. + (NSData *)offline_model_qty:(NSMutableDictionary *)params {
  9092. NSString *product_id = [params objectForKey:@"fashionId"];
  9093. NSString *sql = [NSString stringWithFormat:@"select availability from product where product_id = %@;",product_id];
  9094. __block int result = RESULT_TRUE;
  9095. __block int qty = 0;
  9096. [iSalesDB jk_query:sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9097. qty = sqlite3_column_int(stmt, 0);
  9098. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9099. result = RESULT_FALSE;
  9100. }];
  9101. NSMutableDictionary *dic = @{
  9102. @"err_msg" : result == RESULT_FALSE ? @"Failed." : @"Success.",
  9103. @"mode" : @"Regular Mode",
  9104. @"quantity_available" : @(qty),
  9105. @"result" : @(result),
  9106. }.mutableCopy;
  9107. return [RAUtils dict2data:dic];
  9108. }
  9109. + (NSData *)offline_editPortfolio:(NSMutableDictionary *)params {
  9110. NSString *item_ids = [params objectForKey:@"item_id"];
  9111. NSString *line_notes = [params objectForKey:@"notes"];
  9112. NSString *price_str = [params objectForKey:@"price"];
  9113. NSString *discount_str = [params objectForKey:@"discount"];
  9114. NSString *percent = [params objectForKey:@"available_percent"];
  9115. NSString *qty = [params objectForKey:@"available_qty"];
  9116. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set "];
  9117. int dot = 0;
  9118. if (line_notes) {
  9119. line_notes = [self translateSingleQuote:line_notes];
  9120. line_notes = [NSString stringWithFormat:@"line_note = '%@'",line_notes];
  9121. sql = [sql stringByAppendingString:line_notes];
  9122. dot = 1;
  9123. } else {
  9124. line_notes = @"";
  9125. }
  9126. if (price_str) {
  9127. if (dot) {
  9128. price_str = [NSString stringWithFormat:@",sheet_price = %@",price_str];
  9129. } else {
  9130. price_str = [NSString stringWithFormat:@"sheet_price = %@",price_str];
  9131. dot = 1;
  9132. }
  9133. sql = [sql stringByAppendingString:price_str];
  9134. } else {
  9135. price_str = @"";
  9136. }
  9137. if (discount_str) {
  9138. if (dot) {
  9139. discount_str = [NSString stringWithFormat:@",sheet_discount = %@",discount_str];
  9140. } else {
  9141. discount_str = [NSString stringWithFormat:@"sheet_discount = %@",discount_str];
  9142. dot = 1;
  9143. }
  9144. sql = [sql stringByAppendingString:discount_str];
  9145. } else {
  9146. discount_str = @"";
  9147. }
  9148. if (percent) {
  9149. if (dot) {
  9150. percent = [NSString stringWithFormat:@",percent = %@,percentage = 1",percent];
  9151. } else {
  9152. percent = [NSString stringWithFormat:@"percent = %@,percentage = 1",percent];
  9153. dot = 1;
  9154. }
  9155. sql = [sql stringByAppendingString:percent];
  9156. } else {
  9157. percent = @"";
  9158. }
  9159. if (qty) {
  9160. if (dot) {
  9161. qty = [NSString stringWithFormat:@",available_qty = %@,percentage = 0",qty];
  9162. } else {
  9163. qty = [NSString stringWithFormat:@"available_qty = %@,percentage = 0",qty];
  9164. dot = 1;
  9165. }
  9166. sql = [sql stringByAppendingString:qty];
  9167. } else {
  9168. qty = @"";
  9169. }
  9170. NSString *where = [NSString stringWithFormat:@" where item_id in (%@) and (is_delete is null or is_delete = 0);",item_ids];
  9171. sql = [sql stringByAppendingString:where];
  9172. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9173. int result = [iSalesDB execSql:sql];
  9174. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9175. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9176. return [RAUtils dict2data:dic];
  9177. }
  9178. + (NSData *)offline_removePortfolio:(NSMutableDictionary *)params {
  9179. NSString *portfolio_ids = [params objectForKey:@"portfolioId"];
  9180. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9181. sqlite3 *db = [iSalesDB get_db];
  9182. NSString *sql = [NSString stringWithFormat:@"update offline_portfolio set is_delete = 1 where item_id in (%@) and (is_delete is null or is_delete = 0);",portfolio_ids];
  9183. int result = [iSalesDB execSql:sql db:db];
  9184. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9185. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9186. int count = [iSalesDB get_recordcount:db table:@"offline_portfolio" where:@"(is_delete is null or is_delete = 0)"];
  9187. [dic setObject:[NSNumber numberWithInt:count] forKey:@"portfolio_count"];
  9188. [iSalesDB close_db:db];
  9189. return [RAUtils dict2data:dic];
  9190. }
  9191. + (void)offline_removePDFWithName:(NSString *)name {
  9192. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9193. NSString *cachefolder = [paths objectAtIndex:0];
  9194. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9195. NSString *path = [pdfFolder stringByAppendingPathComponent:name];
  9196. NSFileManager *fileManager = [NSFileManager defaultManager];
  9197. [fileManager removeItemAtPath:path error:nil];
  9198. }
  9199. + (void)offline_clear_PDFCache {
  9200. NSFileManager *fileManager = [NSFileManager defaultManager];
  9201. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  9202. NSString *cachefolder = [paths objectAtIndex:0];
  9203. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  9204. NSArray *pdf_files = [fileManager contentsOfDirectoryAtPath:pdfFolder error:nil];
  9205. for (NSString *path in pdf_files) {
  9206. [self offline_removePDFWithName:[path lastPathComponent]];
  9207. }
  9208. }
  9209. + (NSData *)offline_removePDF:(NSMutableDictionary *)params {
  9210. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  9211. NSInteger tearsheetsId = [[params objectForKey:@"tearsheetsId"] integerValue];
  9212. NSString *user = [params objectForKey:@"user"];
  9213. NSString *create_user = [iSalesDB jk_queryText:[NSString stringWithFormat:@"select create_user from offline_pdf where _id = %d;",tearsheetsId]]; // tearsheets_id ---> _id
  9214. if (![create_user isEqualToString:user]) {
  9215. [dic setObject:[NSNumber numberWithInt:RESULT_FALSE] forKey:@"result"];
  9216. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9217. [dic setObject:@"Only Delete Your Owns" forKey:@"err_msg"];
  9218. return [RAUtils dict2data:dic];
  9219. }
  9220. NSString *is_local_sql = [NSString stringWithFormat:@"select is_local,pdf_path from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9221. __block int is_local = 0;
  9222. __block NSString *path = @"";
  9223. [iSalesDB jk_query:is_local_sql completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9224. is_local = sqlite3_column_int(stmt, 0);
  9225. path = [self textAtColumn:1 statement:stmt];
  9226. }];
  9227. NSString *sql = [NSString stringWithFormat:@"update offline_pdf set is_delete = 1 where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9228. if (is_local == 1) {
  9229. sql = [NSString stringWithFormat:@"delete from offline_pdf where _id = %d and create_user = '%@';",tearsheetsId,[self translateSingleQuote:user]];
  9230. // 删除文件
  9231. [self offline_removePDFWithName:path];
  9232. }
  9233. int result = [iSalesDB execSql:sql];
  9234. [dic setObject:[NSNumber numberWithInt:result] forKey:@"result"];
  9235. [dic setObject:@"Regular Mode" forKey:@"mode"];
  9236. return [RAUtils dict2data:dic];
  9237. }
  9238. +(NSMutableDictionary*) preparePortfolio:(NSString* ) serial
  9239. {
  9240. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9241. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9242. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9243. NSString* where=@"1=1";
  9244. if (ver!=nil) {
  9245. where=@"is_dirty=1";
  9246. }
  9247. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,product_id,name,description,item_id,fashion_id,ifnull(available_qty,'null'),ifnull(percentage,'null'),ifnull(percent,'null'),ifnull(sheet_price,'null'),sheet_discount,img,line_note,is_delete,createtime,modify_time from offline_portfolio where %@",where];
  9248. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9249. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  9250. sqlite3 *db = [iSalesDB get_db];
  9251. NSDictionary *queryDic = [iSalesDB jk_query:sqlQuery db:db close:NO completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9252. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9253. NSInteger _id = sqlite3_column_int(stmt, 0);
  9254. NSInteger product_id = sqlite3_column_int(stmt, 1);
  9255. NSString *name = [self textAtColumn:2 statement:stmt];
  9256. NSString *desc = [self textAtColumn:3 statement:stmt];
  9257. NSInteger item_id = sqlite3_column_int(stmt, 4);
  9258. NSInteger fashion_id = sqlite3_column_int(stmt, 5);
  9259. NSInteger qty = sqlite3_column_int(stmt, 6);
  9260. NSInteger is_percent = sqlite3_column_int(stmt, 7);
  9261. double percent = sqlite3_column_double(stmt, 8);
  9262. double price = sqlite3_column_double(stmt, 9);
  9263. double discount = sqlite3_column_double(stmt, 10);
  9264. NSString *img = [self textAtColumn:11 statement:stmt];
  9265. NSString *line_note = [self textAtColumn:12 statement:stmt];
  9266. NSInteger is_delete = sqlite3_column_int(stmt, 13);
  9267. NSString *create_time = [self textAtColumn:14 statement:stmt];
  9268. NSString *modify_time = [self textAtColumn:15 statement:stmt];
  9269. NSString *price_null = [self textAtColumn:9 statement:stmt];
  9270. if ([price_null isEqualToString:@"null"]) {
  9271. price = [[self get_model_default_price:appDelegate.contact_id product_id:nil item_id:@(item_id) db:db] doubleValue];
  9272. }
  9273. NSString *qty_null = [self textAtColumn:6 statement:stmt];
  9274. NSString *is_percent_null = [self textAtColumn:7 statement:stmt];
  9275. NSString *percent_null = [self textAtColumn:8 statement:stmt];
  9276. [item setValue:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9277. [item setValue:[NSNumber numberWithInteger:product_id] forKey:@"product_id"];
  9278. [item setValue:[NSNumber numberWithInteger:item_id] forKey:@"item_id"];
  9279. [item setValue:[NSNumber numberWithInteger:fashion_id] forKey:@"fashion_id"];
  9280. [item setValue:[NSNumber numberWithInteger:is_delete] forKey:@"is_delete"];
  9281. if ([qty_null isEqualToString:@"null"]) {
  9282. // [item setValue:@"null" forKey:@"available_qty"];
  9283. } else {
  9284. [item setValue:[NSNumber numberWithInteger:qty] forKey:@"available_qty"];
  9285. }
  9286. if ([is_percent_null isEqualToString:@"null"]) {
  9287. // [item setValue:@"null" forKey:@"percentage"];
  9288. } else {
  9289. [item setValue:[NSNumber numberWithInteger:is_percent] forKey:@"percentage"];
  9290. }
  9291. if ([percent_null isEqualToString:@"null"]) {
  9292. // [item setValue:@"null" forKey:@"percent"];
  9293. } else {
  9294. [item setValue:[NSNumber numberWithDouble:percent] forKey:@"percent"];
  9295. }
  9296. [item setValue:[NSNumber numberWithDouble:price] forKey:@"sheet_price"];
  9297. [item setValue:[NSNumber numberWithDouble:discount] forKey:@"sheet_discount"];
  9298. [item setValue:name forKey:@"name"];
  9299. [item setValue:desc forKey:@"description"];
  9300. [item setValue:img forKey:@"img"];
  9301. [item setValue:line_note forKey:@"line_note"];
  9302. [item setValue:create_time forKey:@"createtime"];
  9303. [item setValue:modify_time forKey:@"modifytime"];
  9304. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9305. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9306. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9307. }];
  9308. [iSalesDB close_db:db];
  9309. if ([[queryDic objectForKey:@"result"] integerValue] == RESULT_FALSE) {
  9310. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9311. }
  9312. return ret;
  9313. }
  9314. +(NSMutableDictionary*) preparePDF:(NSString* ) serial
  9315. {
  9316. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  9317. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  9318. NSString* ver = [defaults stringForKey:@"OfflineVer"] ;
  9319. NSString* where=@"1=1";
  9320. if (ver!=nil) {
  9321. where=@"is_dirty=1";
  9322. }
  9323. NSString *sqlQuery =[NSString stringWithFormat:@"select _id,ifnull(tearsheets_id,'null'),pdf_path,create_user,tear_note,tear_name,model_info,createtime,modify_time,configureParams,off_params,is_delete,is_local from offline_pdf where %@",where];
  9324. [ret setValue:[NSNumber numberWithInt:AP_USER_NOT_AUTH ] forKey:@"result"];
  9325. [iSalesDB jk_query:sqlQuery completion:^(sqlite3_stmt *stmt, NSMutableDictionary *container, long *count) {
  9326. NSMutableDictionary *item = [NSMutableDictionary dictionary];
  9327. NSInteger _id = sqlite3_column_int(stmt, 0);
  9328. NSInteger tearsheets_id = sqlite3_column_int(stmt, 1);
  9329. NSString *pdf_path = [self textAtColumn:2 statement:stmt];
  9330. NSString *create_user = [self textAtColumn:3 statement:stmt];
  9331. NSString *tear_note = [self textAtColumn:4 statement:stmt];
  9332. NSString *tear_name = [self textAtColumn:5 statement:stmt];
  9333. NSString *model_info = [self textAtColumn:6 statement:stmt];
  9334. NSString *createtime = [self textAtColumn:7 statement:stmt];
  9335. NSString *modifytime = [self textAtColumn:8 statement:stmt];
  9336. NSString *urlParams = [self textAtColumn:9 statement:stmt];
  9337. NSString *off_params = [self textAtColumn:10 statement:stmt];
  9338. NSString *uuid = [NSUUID UUID].UUIDString;
  9339. int is_delete = sqlite3_column_int(stmt, 11);
  9340. NSString *tearsheet_id_null = [self textAtColumn:1 statement:stmt];
  9341. int is_local = sqlite3_column_int(stmt, 12);
  9342. [item setObject:[NSNumber numberWithInteger:_id] forKey:@"_id"];
  9343. if (![tearsheet_id_null isEqualToString:@"null"]) {
  9344. [item setObject:[NSNumber numberWithInteger:tearsheets_id] forKey:@"tearsheets_id"];
  9345. }
  9346. [item setObject:pdf_path forKey:@"pdf_path"];
  9347. [item setObject:create_user forKey:@"create_user"];
  9348. [item setObject:tear_note forKey:@"tear_note"];
  9349. [item setObject:tear_name forKey:@"tear_name"];
  9350. [item setObject:model_info forKey:@"model_info"];
  9351. [item setObject:createtime forKey:@"createtime"];
  9352. [item setObject:modifytime forKey:@"modifytime"];
  9353. [item setObject:urlParams forKey:@"urlParams"];
  9354. [item setObject:off_params forKey:@"off_params"];
  9355. [item setObject:uuid forKey:@"pdf_token"];
  9356. [item setObject:[NSNumber numberWithInt:is_delete] forKey:@"is_delete"];
  9357. NSDictionary *off_Params_dic = [RAUtils string2dict:off_params];
  9358. [item setObject:[off_Params_dic objectForKey:@"product_ids"] forKey:@"product_ids"];
  9359. if (!is_local && !is_delete) { // 已经同步过的,并且没被删除,就不需要上传。
  9360. } else {
  9361. [ret setObject:item forKey:[NSString stringWithFormat:@"item_%ld",(*count)++]];
  9362. }
  9363. [ret setObject:[NSNumber numberWithInteger:RESULT_TRUE] forKey:@"result"];
  9364. [ret setObject:[NSNumber numberWithInteger:*count] forKey:@"count"];
  9365. } failure:^(NSMutableDictionary *container, NSString *err_msg) {
  9366. [ret setObject:[NSNumber numberWithInteger:RESULT_FALSE] forKey:@"result"];
  9367. }];
  9368. return ret;
  9369. }
  9370. @end